|
| 1 | +<a id="src.uagents.context"></a> |
| 2 | + |
| 3 | +# src.uagents.context |
| 4 | + |
| 5 | +Agent Context and Message Handling |
| 6 | + |
| 7 | +<a id="src.uagents.context.MsgDigest"></a> |
| 8 | + |
| 9 | +## MsgDigest Objects |
| 10 | + |
| 11 | +```python |
| 12 | +@dataclass |
| 13 | +class MsgDigest() |
| 14 | +``` |
| 15 | + |
| 16 | +Represents a message digest containing a message and its schema digest. |
| 17 | + |
| 18 | +**Attributes**: |
| 19 | + |
| 20 | +- `message` _Any_ - The message content. |
| 21 | +- `schema_digest` _str_ - The schema digest of the message. |
| 22 | + |
| 23 | +<a id="src.uagents.context.Context"></a> |
| 24 | + |
| 25 | +## Context Objects |
| 26 | + |
| 27 | +```python |
| 28 | +class Context() |
| 29 | +``` |
| 30 | + |
| 31 | +Represents the context in which messages are handled and processed. |
| 32 | + |
| 33 | +**Attributes**: |
| 34 | + |
| 35 | +- `storage` _KeyValueStore_ - The key-value store for storage operations. |
| 36 | +- `wallet` _LocalWallet_ - The local wallet instance for managing identities. |
| 37 | +- `ledger` _LedgerClient_ - The ledger client for interacting with distributed ledgers. |
| 38 | +- `_name` _Optional[str]_ - The optional name associated with the context. |
| 39 | +- `_address` _str_ - The address of the context. |
| 40 | +- `_resolver` _Resolver_ - The resolver for name-to-address resolution. |
| 41 | +- `_identity` _Identity_ - The identity associated with the context. |
| 42 | +- `_queries` _Dict[str, asyncio.Future]_ - Dictionary of query names and their asyncio Futures. |
| 43 | +- `_session` _Optional[uuid.UUID]_ - The optional session UUID. |
| 44 | +- `_replies` _Optional[Dict[str, Set[Type[Model]]]]_ - The optional dictionary of reply models. |
| 45 | +- `_interval_messages` _Optional[Set[str]]_ - The optional set of interval messages. |
| 46 | +- `_message_received` _Optional[MsgDigest]_ - The optional message digest received. |
| 47 | +- `_protocols` _Optional[Dict[str, Protocol]]_ - The optional dictionary of protocols. |
| 48 | +- `_logger` _Optional[logging.Logger]_ - The optional logger instance. |
| 49 | + |
| 50 | + Properties: |
| 51 | +- `name` _str_ - The name associated with the context, or a truncated address if name is None. |
| 52 | +- `address` _str_ - The address of the context. |
| 53 | +- `logger` _logging.Logger_ - The logger instance. |
| 54 | +- `protocols` _Optional[Dict[str, Protocol]]_ - The dictionary of protocols. |
| 55 | + |
| 56 | + |
| 57 | +**Methods**: |
| 58 | + |
| 59 | +- `get_message_protocol(message_schema_digest)` - Get the protocol associated with a message schema digest. |
| 60 | + send(destination, message, timeout): Send a message to a destination. |
| 61 | + |
| 62 | +<a id="src.uagents.context.Context.__init__"></a> |
| 63 | + |
| 64 | +#### `__`init`__` |
| 65 | + |
| 66 | +```python |
| 67 | +def __init__(address: str, |
| 68 | + name: Optional[str], |
| 69 | + storage: KeyValueStore, |
| 70 | + resolve: Resolver, |
| 71 | + identity: Identity, |
| 72 | + wallet: LocalWallet, |
| 73 | + ledger: LedgerClient, |
| 74 | + queries: Dict[str, asyncio.Future], |
| 75 | + session: Optional[uuid.UUID] = None, |
| 76 | + replies: Optional[Dict[str, Set[Type[Model]]]] = None, |
| 77 | + interval_messages: Optional[Set[str]] = None, |
| 78 | + message_received: Optional[MsgDigest] = None, |
| 79 | + protocols: Optional[Dict[str, Protocol]] = None, |
| 80 | + logger: Optional[logging.Logger] = None) |
| 81 | +``` |
| 82 | + |
| 83 | +Initialize the Context instance. |
| 84 | + |
| 85 | +**Arguments**: |
| 86 | + |
| 87 | +- `address` _str_ - The address of the context. |
| 88 | +- `name` _Optional[str]_ - The optional name associated with the context. |
| 89 | +- `storage` _KeyValueStore_ - The key-value store for storage operations. |
| 90 | +- `resolve` _Resolver_ - The resolver for name-to-address resolution. |
| 91 | +- `identity` _Identity_ - The identity associated with the context. |
| 92 | +- `wallet` _LocalWallet_ - The local wallet instance for managing identities. |
| 93 | +- `ledger` _LedgerClient_ - The ledger client for interacting with distributed ledgers. |
| 94 | +- `queries` _Dict[str, asyncio.Future]_ - Dictionary of query names and their asyncio Futures. |
| 95 | +- `session` _Optional[uuid.UUID]_ - The optional session UUID. |
| 96 | +- `replies` _Optional[Dict[str, Set[Type[Model]]]]_ - The optional dictionary of reply models. |
| 97 | +- `interval_messages` _Optional[Set[str]]_ - The optional set of interval messages. |
| 98 | +- `message_received` _Optional[MsgDigest]_ - The optional message digest received. |
| 99 | +- `protocols` _Optional[Dict[str, Protocol]]_ - The optional dictionary of protocols. |
| 100 | +- `logger` _Optional[logging.Logger]_ - The optional logger instance. |
| 101 | + |
| 102 | +<a id="src.uagents.context.Context.name"></a> |
| 103 | + |
| 104 | +#### name |
| 105 | + |
| 106 | +```python |
| 107 | +@property |
| 108 | +def name() -> str |
| 109 | +``` |
| 110 | + |
| 111 | +Get the name associated with the context or a truncated address if name is None. |
| 112 | + |
| 113 | +**Returns**: |
| 114 | + |
| 115 | +- `str` - The name or truncated address. |
| 116 | + |
| 117 | +<a id="src.uagents.context.Context.address"></a> |
| 118 | + |
| 119 | +#### address |
| 120 | + |
| 121 | +```python |
| 122 | +@property |
| 123 | +def address() -> str |
| 124 | +``` |
| 125 | + |
| 126 | +Get the address of the context. |
| 127 | + |
| 128 | +**Returns**: |
| 129 | + |
| 130 | +- `str` - The address of the context. |
| 131 | + |
| 132 | +<a id="src.uagents.context.Context.logger"></a> |
| 133 | + |
| 134 | +#### logger |
| 135 | + |
| 136 | +```python |
| 137 | +@property |
| 138 | +def logger() -> logging.Logger |
| 139 | +``` |
| 140 | + |
| 141 | +Get the logger instance associated with the context. |
| 142 | + |
| 143 | +**Returns**: |
| 144 | + |
| 145 | +- `logging.Logger` - The logger instance. |
| 146 | + |
| 147 | +<a id="src.uagents.context.Context.protocols"></a> |
| 148 | + |
| 149 | +#### protocols |
| 150 | + |
| 151 | +```python |
| 152 | +@property |
| 153 | +def protocols() -> Optional[Dict[str, Protocol]] |
| 154 | +``` |
| 155 | + |
| 156 | +Get the dictionary of protocols associated with the context. |
| 157 | + |
| 158 | +**Returns**: |
| 159 | + |
| 160 | + Optional[Dict[str, Protocol]]: The dictionary of protocols. |
| 161 | + |
| 162 | +<a id="src.uagents.context.Context.session"></a> |
| 163 | + |
| 164 | +#### session |
| 165 | + |
| 166 | +```python |
| 167 | +@property |
| 168 | +def session() -> uuid.UUID |
| 169 | +``` |
| 170 | + |
| 171 | +Get the session UUID associated with the context. |
| 172 | + |
| 173 | +**Returns**: |
| 174 | + |
| 175 | +- `uuid.UUID` - The session UUID. |
| 176 | + |
| 177 | +<a id="src.uagents.context.Context.get_message_protocol"></a> |
| 178 | + |
| 179 | +#### get`_`message`_`protocol |
| 180 | + |
| 181 | +```python |
| 182 | +def get_message_protocol(message_schema_digest) -> Optional[str] |
| 183 | +``` |
| 184 | + |
| 185 | +Get the protocol associated with a given message schema digest. |
| 186 | + |
| 187 | +**Arguments**: |
| 188 | + |
| 189 | +- `message_schema_digest` _str_ - The schema digest of the message. |
| 190 | + |
| 191 | + |
| 192 | +**Returns**: |
| 193 | + |
| 194 | +- `Optional[str]` - The protocol digest associated with the message schema digest, or None if not found. |
| 195 | + |
| 196 | +<a id="src.uagents.context.Context.send"></a> |
| 197 | + |
| 198 | +#### send |
| 199 | + |
| 200 | +```python |
| 201 | +async def send(destination: str, |
| 202 | + message: Model, |
| 203 | + timeout: Optional[int] = DEFAULT_ENVELOPE_TIMEOUT_SECONDS) |
| 204 | +``` |
| 205 | + |
| 206 | +Send a message to the specified destination. |
| 207 | + |
| 208 | +**Arguments**: |
| 209 | + |
| 210 | +- `destination` _str_ - The destination address to send the message to. |
| 211 | +- `message` _Model_ - The message to be sent. |
| 212 | +- `timeout` _Optional[int]_ - The optional timeout for sending the message, in seconds. |
| 213 | + |
| 214 | +<a id="src.uagents.context.Context.send_raw"></a> |
| 215 | + |
| 216 | +#### send`_`raw |
| 217 | + |
| 218 | +```python |
| 219 | +async def send_raw(destination: str, |
| 220 | + json_message: JsonStr, |
| 221 | + schema_digest: str, |
| 222 | + message_type: Optional[Type[Model]] = None, |
| 223 | + timeout: Optional[int] = DEFAULT_ENVELOPE_TIMEOUT_SECONDS) |
| 224 | +``` |
| 225 | + |
| 226 | +Send a raw message to the specified destination. |
| 227 | + |
| 228 | +**Arguments**: |
| 229 | + |
| 230 | +- `destination` _str_ - The destination address to send the message to. |
| 231 | +- `json_message` _JsonStr_ - The JSON-encoded message to be sent. |
| 232 | +- `schema_digest` _str_ - The schema digest of the message. |
| 233 | +- `message_type` _Optional[Type[Model]]_ - The optional type of the message being sent. |
| 234 | +- `timeout` _Optional[int]_ - The optional timeout for sending the message, in seconds. |
| 235 | + |
0 commit comments