src.uagents.resolver
Endpoint Resolver.
weighted_random_sample - Github
def weighted_random_sample(items: List[Any],
weights: Optional[List[float]] = None,
k: int = 1,
rng=random) -> List[Any]Weighted random sample from a list of items without replacement.
Ref: Efraimidis, Pavlos S. “Weighted random sampling over data streams.”
Arguments:
itemsList[Any] - The list of items to sample from.weightsOptional[List[float]] - The optional list of weights for each item.kint - The number of items to sample.rngrandom - The random number generator.
Returns:
List[Any]- The sampled items.
is_valid_address - Github
def is_valid_address(address: str) -> boolCheck if the given string is a valid address.
Arguments:
addressstr - The address to be checked.
Returns:
bool- True if the address is valid; False otherwise.
is_valid_prefix - Github
def is_valid_prefix(prefix: str) -> boolCheck if the given string is a valid prefix.
Arguments:
prefixstr - The prefix to be checked.
Returns:
bool- True if the prefix is valid; False otherwise.
parse_identifier - Github
def parse_identifier(identifier: str) -> Tuple[str, str, str]Parse an agent identifier string into prefix, name, and address.
Arguments:
identifierstr - The identifier string to be parsed.
Returns:
Tuple[str, str, str]: A tuple containing the prefix, name, and address as strings.
query_record - Github
def query_record(agent_address: str, service: str, test: bool) -> dictQuery a record from the Almanac contract.
Arguments:
agent_addressstr - The address of the agent.servicestr - The type of service to query.
Returns:
dict- The query result.
get_agent_address - Github
def get_agent_address(name: str, test: bool) -> Optional[str]Get the agent address associated with the provided name from the name service contract.
Arguments:
namestr - The name to query.testbool - Whether to use the testnet or mainnet contract.
Returns:
Optional[str]- The associated agent address if found.
Resolver Objects - Github
class Resolver(ABC)resolve - Github
@abstractmethod
async def resolve(destination: str) -> Tuple[Optional[str], List[str]]Resolve the destination to an address and endpoint.
Arguments:
destinationstr - The destination name or address to resolve.
Returns:
Tuple[Optional[str], List[str]]: The address (if available) and resolved endpoints.
GlobalResolver Objects - Github
class GlobalResolver(Resolver)init - Github
def __init__(max_endpoints: Optional[int] = None,
almanac_api_url: Optional[str] = None)Initialize the GlobalResolver.
Arguments:
max_endpointsOptional[int] - The maximum number of endpoints to return.almanac_api_urlOptional[str] - The url for almanac api
resolve - Github
async def resolve(destination: str) -> Tuple[Optional[str], List[str]]Resolve the destination using the appropriate resolver.
Arguments:
destinationstr - The destination name or address to resolve.
Returns:
Tuple[Optional[str], List[str]]: The address (if available) and resolved endpoints.
AlmanacContractResolver Objects - Github
class AlmanacContractResolver(Resolver)init - Github
def __init__(max_endpoints: Optional[int] = None)Initialize the AlmanacContractResolver.
Arguments:
max_endpointsOptional[int] - The maximum number of endpoints to return.
resolve - Github
async def resolve(destination: str) -> Tuple[Optional[str], List[str]]Resolve the destination using the Almanac contract.
Arguments:
destinationstr - The destination address to resolve.
Returns:
Tuple[str, List[str]]: The address and resolved endpoints.
AlmanacApiResolver Objects - Github
class AlmanacApiResolver(Resolver)init - Github
def __init__(max_endpoints: Optional[int] = None,
almanac_api_url: Optional[str] = None)Initialize the AlmanacApiResolver.
Arguments:
max_endpointsOptional[int] - The maximum number of endpoints to return.almanac_api_urlOptional[str] - The url for almanac api
resolve - Github
async def resolve(destination: str) -> Tuple[Optional[str], List[str]]Resolve the destination using the Almanac API. If the resolution using API fails, it retries using the Almanac Contract.
Arguments:
destinationstr - The destination address to resolve.
Returns:
Tuple[Optional[str], List[str]]: The address and resolved endpoints.
NameServiceResolver Objects - Github
class NameServiceResolver(Resolver)init - Github
def __init__(max_endpoints: Optional[int] = None)Initialize the NameServiceResolver.
Arguments:
max_endpointsOptional[int] - The maximum number of endpoints to return.
resolve - Github
async def resolve(destination: str) -> Tuple[Optional[str], List[str]]Resolve the destination using the NameService contract.
Arguments:
destinationstr - The destination name to resolve.
Returns:
Tuple[Optional[str], List[str]]: The address (if available) and resolved endpoints.
RulesBasedResolver Objects - Github
class RulesBasedResolver(Resolver)init - Github
def __init__(rules: Dict[str, str], max_endpoints: Optional[int] = None)Initialize the RulesBasedResolver with the provided rules.
Arguments:
rulesDict[str, str] - A dictionary of rules mapping destinations to endpoints.max_endpointsOptional[int] - The maximum number of endpoints to return.
resolve - Github
async def resolve(destination: str) -> Tuple[Optional[str], List[str]]Resolve the destination using the provided rules.
Arguments:
destinationstr - The destination to resolve.
Returns:
Tuple[str, List[str]]: The address and resolved endpoints.