|
11 | 11 | from askui.models.models import ActModel |
12 | 12 | from askui.models.shared.computer_agent_cb_param import OnMessageCb, OnMessageCbParam |
13 | 13 | from askui.models.shared.computer_agent_message_param import ( |
14 | | - Base64ImageSourceParam, |
15 | | - ContentBlockParam, |
16 | 14 | ImageBlockParam, |
17 | 15 | MessageParam, |
18 | 16 | TextBlockParam, |
19 | | - ToolResultBlockParam, |
20 | 17 | ) |
| 18 | +from askui.models.shared.tools import ToolCollection |
21 | 19 | from askui.reporting import Reporter |
22 | | -from askui.tools.agent_os import AgentOs |
23 | | -from askui.tools.anthropic import ComputerTool, ToolCollection, ToolResult |
24 | 20 |
|
25 | 21 | from ...logger import logger |
26 | 22 |
|
@@ -189,21 +185,19 @@ class ComputerAgent(ActModel, ABC, Generic[ComputerAgentSettings]): |
189 | 185 | def __init__( |
190 | 186 | self, |
191 | 187 | settings: ComputerAgentSettings, |
192 | | - agent_os: AgentOs, |
| 188 | + tool_collection: ToolCollection, |
193 | 189 | reporter: Reporter, |
194 | 190 | ) -> None: |
195 | 191 | """Initialize the computer agent. |
196 | 192 |
|
197 | 193 | Args: |
198 | 194 | settings (ComputerAgentSettings): The settings for the computer agent. |
199 | | - agent_os (AgentOs): The operating system agent for executing commands. |
| 195 | + tool_collection (ToolCollection): Collection of tools to be used |
200 | 196 | reporter (Reporter): The reporter for logging messages and actions. |
201 | 197 | """ |
202 | 198 | self._settings = settings |
203 | 199 | self._reporter = reporter |
204 | | - self._tool_collection = ToolCollection( |
205 | | - ComputerTool(agent_os), |
206 | | - ) |
| 200 | + self._tool_collection = tool_collection |
207 | 201 | self._system = BetaTextBlockParam( |
208 | 202 | type="text", |
209 | 203 | text=f"{SYSTEM_PROMPT}", |
@@ -315,24 +309,20 @@ def _use_tools( |
315 | 309 | MessageParam | None: A message containing tool results or `None` |
316 | 310 | if no tools were used. |
317 | 311 | """ |
318 | | - tool_result_content: list[ContentBlockParam] = [] |
319 | 312 | if isinstance(message.content, str): |
320 | 313 | return None |
321 | 314 |
|
322 | | - for content_block in message.content: |
323 | | - if content_block.type == "tool_use": |
324 | | - result = self._tool_collection.run( |
325 | | - name=content_block.name, |
326 | | - tool_input=content_block.input, # type: ignore[arg-type] |
327 | | - ) |
328 | | - tool_result_content.append( |
329 | | - self._make_api_tool_result(result, content_block.id) |
330 | | - ) |
331 | | - if len(tool_result_content) == 0: |
| 315 | + tool_use_content_blocks = [ |
| 316 | + content_block |
| 317 | + for content_block in message.content |
| 318 | + if content_block.type == "tool_use" |
| 319 | + ] |
| 320 | + content = self._tool_collection.run(tool_use_content_blocks) |
| 321 | + if len(content) == 0: |
332 | 322 | return None |
333 | 323 |
|
334 | 324 | return MessageParam( |
335 | | - content=tool_result_content, |
| 325 | + content=content, |
336 | 326 | role="user", |
337 | 327 | ) |
338 | 328 |
|
@@ -391,62 +381,3 @@ def _maybe_filter_to_n_most_recent_images( |
391 | 381 | new_content.append(content) |
392 | 382 | tool_result.content = new_content |
393 | 383 | return messages |
394 | | - |
395 | | - def _make_api_tool_result( |
396 | | - self, result: ToolResult, tool_use_id: str |
397 | | - ) -> ToolResultBlockParam: |
398 | | - """Convert a tool result to an API tool result block. |
399 | | -
|
400 | | - Args: |
401 | | - result (ToolResult): The tool result to convert. |
402 | | - tool_use_id (str): The ID of the tool use block. |
403 | | -
|
404 | | - Returns: |
405 | | - ToolResultBlockParam: The API tool result block. |
406 | | - """ |
407 | | - tool_result_content: list[TextBlockParam | ImageBlockParam] | str = [] |
408 | | - is_error = False |
409 | | - if result.error: |
410 | | - is_error = True |
411 | | - tool_result_content = self._maybe_prepend_system_tool_result( |
412 | | - result, result.error |
413 | | - ) |
414 | | - else: |
415 | | - assert isinstance(tool_result_content, list) |
416 | | - if result.output: |
417 | | - tool_result_content.append( |
418 | | - TextBlockParam( |
419 | | - text=self._maybe_prepend_system_tool_result( |
420 | | - result, result.output |
421 | | - ), |
422 | | - ) |
423 | | - ) |
424 | | - if result.base64_image: |
425 | | - tool_result_content.append( |
426 | | - ImageBlockParam( |
427 | | - source=Base64ImageSourceParam( |
428 | | - media_type="image/png", |
429 | | - data=result.base64_image, |
430 | | - ), |
431 | | - ) |
432 | | - ) |
433 | | - return ToolResultBlockParam( |
434 | | - content=tool_result_content, |
435 | | - tool_use_id=tool_use_id, |
436 | | - is_error=is_error, |
437 | | - ) |
438 | | - |
439 | | - @staticmethod |
440 | | - def _maybe_prepend_system_tool_result(result: ToolResult, result_text: str) -> str: |
441 | | - """Prepend system message to tool result text if available. |
442 | | -
|
443 | | - Args: |
444 | | - result (ToolResult): The tool result. |
445 | | - result_text (str): The result text. |
446 | | -
|
447 | | - Returns: |
448 | | - str: The result text with optional system message prepended. |
449 | | - """ |
450 | | - if result.system: |
451 | | - result_text = f"<system>{result.system}</system>\n{result_text}" |
452 | | - return result_text |
0 commit comments