|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from types import TracebackType |
| 3 | +from typing import Any, List, Literal, Mapping, Optional, Type |
| 4 | + |
| 5 | +from pydantic import BaseModel, Field |
| 6 | +from typing_extensions import Annotated, Self |
| 7 | + |
| 8 | +from .._cancellation_token import CancellationToken |
| 9 | +from .._component_config import ComponentBase |
| 10 | +from .._image import Image |
| 11 | +from ._base import ToolSchema |
| 12 | + |
| 13 | + |
| 14 | +class TextResultContent(BaseModel): |
| 15 | + """ |
| 16 | + Text result content of a tool execution. |
| 17 | + """ |
| 18 | + |
| 19 | + type: Literal["TextResultContent"] = "TextResultContent" |
| 20 | + |
| 21 | + content: str |
| 22 | + """The text content of the result.""" |
| 23 | + |
| 24 | + |
| 25 | +class ImageResultContent(BaseModel): |
| 26 | + """ |
| 27 | + Image result content of a tool execution. |
| 28 | + """ |
| 29 | + |
| 30 | + type: Literal["ImageResultContent"] = "ImageResultContent" |
| 31 | + |
| 32 | + content: Image |
| 33 | + """The image content of the result.""" |
| 34 | + |
| 35 | + |
| 36 | +ResultContent = Annotated[TextResultContent | ImageResultContent, Field(discriminator="type")] |
| 37 | + |
| 38 | + |
| 39 | +class ToolResult(BaseModel): |
| 40 | + """ |
| 41 | + A result of a tool execution by a workbench. |
| 42 | + """ |
| 43 | + |
| 44 | + type: Literal["ToolResult"] = "ToolResult" |
| 45 | + |
| 46 | + name: str |
| 47 | + """The name of the tool that was executed.""" |
| 48 | + |
| 49 | + result: List[ResultContent] |
| 50 | + """The result of the tool execution.""" |
| 51 | + |
| 52 | + is_error: bool = False |
| 53 | + """Whether the tool execution resulted in an error.""" |
| 54 | + |
| 55 | + |
| 56 | +class Workbench(ABC, ComponentBase[BaseModel]): |
| 57 | + """ |
| 58 | + A workbench is a component that provides a set of tools that may share |
| 59 | + resources and state. |
| 60 | +
|
| 61 | + A workbench is responsible for managing the lifecycle of the tools and |
| 62 | + providing a single interface to call them. The tools provided by the workbench |
| 63 | + may be dynamic and their availabilities may change after each tool execution. |
| 64 | +
|
| 65 | + A workbench can be started by calling the :meth:`~autogen_core.tools.Workbench.start` method |
| 66 | + and stopped by calling the :meth:`~autogen_core.tools.Workbench.stop` method. |
| 67 | + It can also be used as an asynchronous context manager, which will automatically |
| 68 | + start and stop the workbench when entering and exiting the context. |
| 69 | + """ |
| 70 | + |
| 71 | + component_type = "workbench" |
| 72 | + |
| 73 | + @abstractmethod |
| 74 | + async def list_tools(self) -> List[ToolSchema]: |
| 75 | + """ |
| 76 | + List the currently available tools in the workbench as :class:`ToolSchema` |
| 77 | + objects. |
| 78 | +
|
| 79 | + The list of tools may be dynamic, and their content may change after |
| 80 | + tool execution. |
| 81 | + """ |
| 82 | + ... |
| 83 | + |
| 84 | + @abstractmethod |
| 85 | + async def call_tool( |
| 86 | + self, name: str, arguments: Mapping[str, Any] | None = None, cancellation_token: CancellationToken | None = None |
| 87 | + ) -> ToolResult: |
| 88 | + """ |
| 89 | + Call a tool in the workbench. |
| 90 | +
|
| 91 | + Args: |
| 92 | + name (str): The name of the tool to call. |
| 93 | + arguments (Mapping[str, Any] | None): The arguments to pass to the tool. |
| 94 | + If None, the tool will be called with no arguments. |
| 95 | + cancellation_token (CancellationToken | None): An optional cancellation token |
| 96 | + to cancel the tool execution. |
| 97 | + Returns: |
| 98 | + ToolResult: The result of the tool execution. |
| 99 | + """ |
| 100 | + ... |
| 101 | + |
| 102 | + @abstractmethod |
| 103 | + async def start(self) -> None: |
| 104 | + """ |
| 105 | + Start the workbench and initialize any resources. |
| 106 | +
|
| 107 | + This method should be called before using the workbench. |
| 108 | + """ |
| 109 | + ... |
| 110 | + |
| 111 | + @abstractmethod |
| 112 | + async def stop(self) -> None: |
| 113 | + """ |
| 114 | + Stop the workbench and release any resources. |
| 115 | +
|
| 116 | + This method should be called when the workbench is no longer needed. |
| 117 | + """ |
| 118 | + ... |
| 119 | + |
| 120 | + @abstractmethod |
| 121 | + async def reset(self) -> None: |
| 122 | + """ |
| 123 | + Reset the workbench to its initialized, started state. |
| 124 | + """ |
| 125 | + ... |
| 126 | + |
| 127 | + @abstractmethod |
| 128 | + async def save_state(self) -> Mapping[str, Any]: |
| 129 | + """ |
| 130 | + Save the state of the workbench. |
| 131 | +
|
| 132 | + This method should be called to persist the state of the workbench. |
| 133 | + """ |
| 134 | + ... |
| 135 | + |
| 136 | + @abstractmethod |
| 137 | + async def load_state(self, state: Mapping[str, Any]) -> None: |
| 138 | + """ |
| 139 | + Load the state of the workbench. |
| 140 | +
|
| 141 | + Args: |
| 142 | + state (Mapping[str, Any]): The state to load into the workbench. |
| 143 | + """ |
| 144 | + ... |
| 145 | + |
| 146 | + async def __aenter__(self) -> Self: |
| 147 | + """ |
| 148 | + Enter the workbench context manager. |
| 149 | +
|
| 150 | + This method is called when the workbench is used in a `with` statement. |
| 151 | + It calls the :meth:`~autogen_core.tools.WorkBench.start` method to start the workbench. |
| 152 | + """ |
| 153 | + await self.start() |
| 154 | + return self |
| 155 | + |
| 156 | + async def __aexit__( |
| 157 | + self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType] |
| 158 | + ) -> None: |
| 159 | + """ |
| 160 | + Exit the workbench context manager. |
| 161 | + This method is called when the workbench is used in a `with` statement. |
| 162 | + It calls the :meth:`~autogen_core.tools.WorkBench.stop` method to stop the workbench. |
| 163 | + """ |
| 164 | + await self.stop() |
0 commit comments