-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
346 lines (300 loc) · 14 KB
/
client.py
File metadata and controls
346 lines (300 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import asyncio
import logging
import pydantic
from .transports import Transport, WebSocketTransport
from .types import (
InvocationMessage,
CommandExecutionMessage,
AgentRuntimeMessageInterface,
CancelMessage,
ErrorMessageInterface,
ResultMessageInterface,
CommandExecutionResultMessageInterface,
QueryResult,
CommandExecutionResult,
CommandInterface,
AssistantMessageInterface,
ArtifactUploadRequestMessageInterface,
ArtifactUploadResponseMessageInterface,
QueryOptions,
ExecuteCommandsOptions,
)
from .exceptions import AgentRuntimeError, CancelledException
_default_logger = logging.getLogger(__name__)
class RuntimeUseClient:
"""Client for communicating with a runtimeuse agent runtime.
Handles message dispatch, artifact upload handshake, cancellation, and
structured result parsing.
Args:
ws_url: WebSocket URL for the agent runtime. Used to create the default
WebSocketTransport. Ignored when a custom transport is provided.
transport: Optional custom transport implementing the Transport protocol.
When provided, ws_url is not required.
"""
def __init__(
self,
ws_url: str | None = None,
transport: Transport | None = None,
):
if transport is not None:
self._transport = transport
elif ws_url is not None:
self._transport = WebSocketTransport(ws_url)
else:
raise ValueError("Either ws_url or transport must be provided")
self._abort_event = asyncio.Event()
def abort(self) -> None:
"""Signal the current query to cancel.
Sends a cancel message to the agent runtime and causes ``query``
to raise :class:`CancelledException`. Safe to call from any
coroutine on the same event loop.
"""
self._abort_event.set()
async def query(
self,
prompt: str,
options: QueryOptions,
) -> QueryResult:
"""Send a prompt to the agent runtime and return the result.
Builds an :class:`InvocationMessage` from *prompt* and *options*,
sends it over the transport, processes the response stream, and
returns a :class:`QueryResult`.
Access ``result.data`` for the :class:`TextResult` or
:class:`StructuredOutputResult`, and ``result.metadata`` for
execution metadata.
Args:
prompt: The user prompt to send to the agent.
options: Query configuration including system prompt, model,
output schema, callbacks, and timeout.
Raises:
AgentRuntimeError: If the runtime sends an error or no result is produced.
CancelledException: If the query is cancelled via :meth:`abort`.
TimeoutError: If the timeout is exceeded.
"""
logger = options.logger or _default_logger
self._abort_event = asyncio.Event()
invocation = InvocationMessage(
message_type="invocation_message",
user_prompt=prompt,
system_prompt=options.system_prompt,
model=options.model,
output_format_json_schema_str=options.output_format_json_schema_str,
source_id=options.source_id,
secrets_to_redact=options.secrets_to_redact,
artifacts_dir=options.artifacts_dir,
pre_agent_invocation_commands=options.pre_agent_invocation_commands,
post_agent_invocation_commands=options.post_agent_invocation_commands,
pre_agent_downloadables=options.pre_agent_downloadables,
)
send_queue: asyncio.Queue[dict] = asyncio.Queue()
await send_queue.put(invocation.model_dump(mode="json"))
wire_result: ResultMessageInterface | None = None
async with asyncio.timeout(options.timeout):
async for message in self._transport(send_queue=send_queue):
if self._abort_event.is_set():
logger.info("Query cancelled by caller")
await send_queue.put(
CancelMessage(message_type="cancel_message").model_dump(
mode="json"
)
)
await send_queue.join()
raise CancelledException("Query was cancelled")
try:
message_interface = AgentRuntimeMessageInterface.model_validate(
message
)
except pydantic.ValidationError:
logger.error(
f"Received unknown message type from agent runtime: {message}"
)
continue
if message_interface.message_type == "result_message":
wire_result = ResultMessageInterface.model_validate(message)
logger.info(
f"Received result message from agent runtime: {message}"
)
continue
elif message_interface.message_type == "assistant_message":
if options.on_assistant_message is not None:
assistant_message_interface = (
AssistantMessageInterface.model_validate(message)
)
await options.on_assistant_message(assistant_message_interface)
continue
elif message_interface.message_type == "error_message":
try:
error_message_interface = ErrorMessageInterface.model_validate(
message
)
except pydantic.ValidationError:
logger.error(
f"Received malformed error message from agent runtime: {message}",
)
raise AgentRuntimeError(str(message))
logger.error(
f"Error from agent runtime: {error_message_interface}",
)
raise AgentRuntimeError(
error_message_interface.error,
metadata=error_message_interface.metadata,
)
elif (
message_interface.message_type == "artifact_upload_request_message"
):
logger.info(
f"Received artifact upload request message from agent runtime: {message}"
)
if options.on_artifact_upload_request is not None:
artifact_upload_request_message_interface = (
ArtifactUploadRequestMessageInterface.model_validate(
message
)
)
upload_result = await options.on_artifact_upload_request(
artifact_upload_request_message_interface
)
artifact_upload_response_message_interface = ArtifactUploadResponseMessageInterface(
message_type="artifact_upload_response_message",
filename=artifact_upload_request_message_interface.filename,
filepath=artifact_upload_request_message_interface.filepath,
presigned_url=upload_result.presigned_url,
content_type=upload_result.content_type,
)
await send_queue.put(
artifact_upload_response_message_interface.model_dump(
mode="json"
)
)
continue
else:
logger.info(
f"Received non-result message from agent runtime: {message}"
)
if wire_result is None:
raise AgentRuntimeError("No result message received")
return QueryResult(data=wire_result.data, metadata=wire_result.metadata)
async def execute_commands(
self,
commands: list[CommandInterface],
options: ExecuteCommandsOptions,
) -> CommandExecutionResult:
"""Execute commands in the runtime without invoking the agent.
Sends a :class:`CommandExecutionMessage`, processes the response
stream, and returns a :class:`CommandExecutionResult` with
per-command exit codes.
Args:
commands: Commands to execute in the runtime environment.
options: Execution configuration including secrets, callbacks,
artifacts, and timeout.
Raises:
AgentRuntimeError: If a command fails or the runtime sends an error.
CancelledException: If cancelled via :meth:`abort`.
TimeoutError: If the timeout is exceeded.
"""
logger = options.logger or _default_logger
self._abort_event = asyncio.Event()
message = CommandExecutionMessage(
message_type="command_execution_message",
source_id=options.source_id,
secrets_to_redact=options.secrets_to_redact,
commands=commands,
artifacts_dir=options.artifacts_dir,
pre_execution_downloadables=options.pre_execution_downloadables,
)
send_queue: asyncio.Queue[dict] = asyncio.Queue()
await send_queue.put(message.model_dump(mode="json"))
wire_result: CommandExecutionResultMessageInterface | None = None
async with asyncio.timeout(options.timeout):
async for msg in self._transport(send_queue=send_queue):
if self._abort_event.is_set():
logger.info("Command execution cancelled by caller")
await send_queue.put(
CancelMessage(message_type="cancel_message").model_dump(
mode="json"
)
)
await send_queue.join()
raise CancelledException("Command execution was cancelled")
try:
message_interface = AgentRuntimeMessageInterface.model_validate(
msg
)
except pydantic.ValidationError:
logger.error(
f"Received unknown message type from agent runtime: {msg}"
)
continue
if (
message_interface.message_type
== "command_execution_result_message"
):
wire_result = (
CommandExecutionResultMessageInterface.model_validate(msg)
)
logger.info(
f"Received command execution result from agent runtime: {msg}"
)
continue
elif message_interface.message_type == "assistant_message":
if options.on_assistant_message is not None:
assistant_message_interface = (
AssistantMessageInterface.model_validate(msg)
)
await options.on_assistant_message(
assistant_message_interface
)
continue
elif message_interface.message_type == "error_message":
try:
error_message_interface = (
ErrorMessageInterface.model_validate(msg)
)
except pydantic.ValidationError:
logger.error(
f"Received malformed error message from agent runtime: {msg}",
)
raise AgentRuntimeError(str(msg))
logger.error(
f"Error from agent runtime: {error_message_interface}",
)
raise AgentRuntimeError(
error_message_interface.error,
metadata=error_message_interface.metadata,
)
elif (
message_interface.message_type
== "artifact_upload_request_message"
):
logger.info(
f"Received artifact upload request message from agent runtime: {msg}"
)
if options.on_artifact_upload_request is not None:
artifact_upload_request_message_interface = (
ArtifactUploadRequestMessageInterface.model_validate(
msg
)
)
upload_result = await options.on_artifact_upload_request(
artifact_upload_request_message_interface
)
artifact_upload_response_message_interface = ArtifactUploadResponseMessageInterface(
message_type="artifact_upload_response_message",
filename=artifact_upload_request_message_interface.filename,
filepath=artifact_upload_request_message_interface.filepath,
presigned_url=upload_result.presigned_url,
content_type=upload_result.content_type,
)
await send_queue.put(
artifact_upload_response_message_interface.model_dump(
mode="json"
)
)
continue
else:
logger.info(
f"Received non-result message from agent runtime: {msg}"
)
if wire_result is None:
raise AgentRuntimeError("No result message received")
return CommandExecutionResult(results=wire_result.results)