uagents-core 0.1.3__py3-none-any.whl → 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,130 +0,0 @@
1
- import json
2
- import urllib.parse
3
- from typing import Any, List, Optional
4
- from uuid import UUID, uuid4
5
-
6
- import requests
7
-
8
- from uagents_core.communication import parse_identifier, weighted_random_sample
9
- from uagents_core.config import (
10
- DEFAULT_ALMANAC_API_PATH,
11
- DEFAULT_MAX_ENDPOINTS,
12
- AgentverseConfig,
13
- )
14
- from uagents_core.crypto import Identity
15
- from uagents_core.envelope import Envelope
16
- from uagents_core.logger import get_logger
17
-
18
- logger = get_logger("uagents_core.utils.communication")
19
-
20
-
21
- def lookup_endpoint_for_agent(
22
- agent_identifier: str,
23
- *,
24
- max_endpoints: int = DEFAULT_MAX_ENDPOINTS,
25
- agentverse_config: Optional[AgentverseConfig] = None,
26
- ) -> List[str]:
27
- """
28
- Look up the endpoints for an agent using the Almanac API.
29
-
30
- Args:
31
- destination (str): The destination address to look up.
32
-
33
- Returns:
34
- List[str]: The endpoint(s) for the agent.
35
- """
36
- _, _, agent_address = parse_identifier(agent_identifier)
37
-
38
- agentverse_config = agentverse_config or AgentverseConfig()
39
- almanac_api = urllib.parse.urljoin(agentverse_config.url, DEFAULT_ALMANAC_API_PATH)
40
-
41
- request_meta: dict[str, Any] = {
42
- "agent_address": agent_address,
43
- "lookup_url": almanac_api,
44
- }
45
- logger.debug("looking up endpoint for agent", extra=request_meta)
46
- r = requests.get(f"{almanac_api}/agents/{agent_address}")
47
- r.raise_for_status()
48
-
49
- request_meta["response_status"] = r.status_code
50
- logger.info(
51
- "Got response looking up agent endpoint",
52
- extra=request_meta,
53
- )
54
-
55
- endpoints = r.json().get("endpoints", [])
56
-
57
- if len(endpoints) > 0:
58
- urls = [val.get("url") for val in endpoints]
59
- weights = [val.get("weight") for val in endpoints]
60
- return weighted_random_sample(
61
- urls,
62
- weights=weights,
63
- k=min(max_endpoints, len(endpoints)),
64
- )
65
-
66
- return []
67
-
68
-
69
- def send_message(
70
- destination: str,
71
- message_schema_digest: str,
72
- message_body: Any,
73
- sender: Identity,
74
- *,
75
- session_id: Optional[UUID] = None,
76
- protocol_digest: Optional[str] = None,
77
- agentverse_config: Optional[AgentverseConfig] = None,
78
- ):
79
- """
80
- Send a message (dict) to an agent.
81
-
82
- Args:
83
- destination (str): The address of the target agent.
84
- message_schema_digest (str): The digest of the model that is being used
85
- message_body (Any): The payload of the message.
86
- sender (Identity): The identity of the sender.
87
- session (UUID): The unique identifier for the dialogue between two agents
88
- protocol_digest (str): The digest of the protocol that is being used
89
- agentverse_config (AgentverseConfig): The configuration for the agentverse API
90
- Returns:
91
- None
92
- """
93
- json_payload = json.dumps(message_body, separators=(",", ":"))
94
-
95
- env = Envelope(
96
- version=1,
97
- sender=sender.address,
98
- target=destination,
99
- session=session_id or uuid4(),
100
- schema_digest=message_schema_digest,
101
- protocol_digest=protocol_digest,
102
- )
103
-
104
- env.encode_payload(json_payload)
105
- env.sign(sender)
106
-
107
- logger.debug("Sending message to agent", extra={"envelope": env.model_dump()})
108
-
109
- # query the almanac to lookup the destination agent
110
- agentverse_config = agentverse_config or AgentverseConfig()
111
- endpoints = lookup_endpoint_for_agent(
112
- destination, agentverse_config=agentverse_config
113
- )
114
-
115
- if len(endpoints) == 0:
116
- logger.error(
117
- "No endpoints found for agent", extra={"agent_address": destination}
118
- )
119
- return
120
-
121
- # send the envelope to the destination agent
122
- request_meta = {"agent_address": destination, "agent_endpoint": endpoints[0]}
123
- logger.debug("Sending message to agent", extra=request_meta)
124
- r = requests.post(
125
- endpoints[0],
126
- headers={"content-type": "application/json"},
127
- data=env.model_dump_json(),
128
- )
129
- r.raise_for_status()
130
- logger.info("Sent message to agent", extra=request_meta)
@@ -1,15 +0,0 @@
1
- uagents_core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- uagents_core/communication.py,sha256=-QinbuCxEHuL79cRkMcfJ3FBPa0XNsU_KeudOhtBIsY,2067
3
- uagents_core/config.py,sha256=YLZo19ciWi83J1F4EEKOsc1SoMrcPs_5GmOHt-rxQvk,514
4
- uagents_core/crypto.py,sha256=oLbKrVPiZ52zz4vrvRQIr7gNyFmr34t-8KvZTlGqXU4,4833
5
- uagents_core/envelope.py,sha256=Lh8Uw85p8sdHncLV-F_ExAyyHxlc9915jQhP7bpgBZM,4931
6
- uagents_core/logger.py,sha256=FH0pUINtXubRNsGxsZ0b2Vu-19zxa7DRshB8_fWbVGQ,945
7
- uagents_core/models.py,sha256=DhDQQQJ34QMHhLCO7nAJl5td7IdaIK8pAaoLhXU3VhA,1002
8
- uagents_core/registration.py,sha256=42xyyEzgW3hPx197KyOTYDrhFNu9uGOcRpo34EXmcA0,2134
9
- uagents_core/types.py,sha256=2tV23954ADupIp0SggLSO4PG8kTRXw9uHzfqMgbcdJk,236
10
- uagents_core/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
- uagents_core/utils/communication.py,sha256=LjWDfEecu1t3WTAJdJp0kjPgeYA2gWzkh3Rk5QmElRo,4000
12
- uagents_core/utils/registration.py,sha256=1NPvpm20f3LE9LYJb2HP-SyEPlPa7ADM2HXmvHhx0fQ,7165
13
- uagents_core-0.1.3.dist-info/METADATA,sha256=h0FQXdGQzkm7QsUEFW3zcWnnZMiB1hE2We2xNJDTLeo,1025
14
- uagents_core-0.1.3.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
15
- uagents_core-0.1.3.dist-info/RECORD,,