agentscope-runtime 0.1.0__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.
- agentscope_runtime/__init__.py +4 -0
- agentscope_runtime/engine/__init__.py +9 -0
- agentscope_runtime/engine/agents/__init__.py +2 -0
- agentscope_runtime/engine/agents/agentscope_agent/__init__.py +6 -0
- agentscope_runtime/engine/agents/agentscope_agent/agent.py +342 -0
- agentscope_runtime/engine/agents/agentscope_agent/hooks.py +156 -0
- agentscope_runtime/engine/agents/agno_agent.py +220 -0
- agentscope_runtime/engine/agents/base_agent.py +29 -0
- agentscope_runtime/engine/agents/langgraph_agent.py +59 -0
- agentscope_runtime/engine/agents/llm_agent.py +51 -0
- agentscope_runtime/engine/deployers/__init__.py +3 -0
- agentscope_runtime/engine/deployers/adapter/__init__.py +0 -0
- agentscope_runtime/engine/deployers/adapter/a2a/__init__.py +2 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_adapter_utils.py +425 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_agent_adapter.py +69 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_protocol_adapter.py +60 -0
- agentscope_runtime/engine/deployers/adapter/protocol_adapter.py +24 -0
- agentscope_runtime/engine/deployers/base.py +17 -0
- agentscope_runtime/engine/deployers/local_deployer.py +586 -0
- agentscope_runtime/engine/helpers/helper.py +127 -0
- agentscope_runtime/engine/llms/__init__.py +3 -0
- agentscope_runtime/engine/llms/base_llm.py +60 -0
- agentscope_runtime/engine/llms/qwen_llm.py +47 -0
- agentscope_runtime/engine/misc/__init__.py +0 -0
- agentscope_runtime/engine/runner.py +186 -0
- agentscope_runtime/engine/schemas/__init__.py +0 -0
- agentscope_runtime/engine/schemas/agent_schemas.py +551 -0
- agentscope_runtime/engine/schemas/context.py +54 -0
- agentscope_runtime/engine/services/__init__.py +9 -0
- agentscope_runtime/engine/services/base.py +77 -0
- agentscope_runtime/engine/services/context_manager.py +129 -0
- agentscope_runtime/engine/services/environment_manager.py +50 -0
- agentscope_runtime/engine/services/manager.py +174 -0
- agentscope_runtime/engine/services/memory_service.py +270 -0
- agentscope_runtime/engine/services/sandbox_service.py +198 -0
- agentscope_runtime/engine/services/session_history_service.py +256 -0
- agentscope_runtime/engine/tracing/__init__.py +40 -0
- agentscope_runtime/engine/tracing/base.py +309 -0
- agentscope_runtime/engine/tracing/local_logging_handler.py +356 -0
- agentscope_runtime/engine/tracing/tracing_metric.py +69 -0
- agentscope_runtime/engine/tracing/wrapper.py +321 -0
- agentscope_runtime/sandbox/__init__.py +14 -0
- agentscope_runtime/sandbox/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/base/__init__.py +0 -0
- agentscope_runtime/sandbox/box/base/base_sandbox.py +37 -0
- agentscope_runtime/sandbox/box/base/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/browser_sandbox.py +176 -0
- agentscope_runtime/sandbox/box/dummy/__init__.py +0 -0
- agentscope_runtime/sandbox/box/dummy/dummy_sandbox.py +26 -0
- agentscope_runtime/sandbox/box/filesystem/__init__.py +0 -0
- agentscope_runtime/sandbox/box/filesystem/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +87 -0
- agentscope_runtime/sandbox/box/sandbox.py +115 -0
- agentscope_runtime/sandbox/box/shared/__init__.py +0 -0
- agentscope_runtime/sandbox/box/shared/app.py +44 -0
- agentscope_runtime/sandbox/box/shared/dependencies/__init__.py +5 -0
- agentscope_runtime/sandbox/box/shared/dependencies/deps.py +22 -0
- agentscope_runtime/sandbox/box/shared/routers/__init__.py +12 -0
- agentscope_runtime/sandbox/box/shared/routers/generic.py +173 -0
- agentscope_runtime/sandbox/box/shared/routers/mcp.py +207 -0
- agentscope_runtime/sandbox/box/shared/routers/mcp_utils.py +153 -0
- agentscope_runtime/sandbox/box/shared/routers/runtime_watcher.py +187 -0
- agentscope_runtime/sandbox/box/shared/routers/workspace.py +325 -0
- agentscope_runtime/sandbox/box/training_box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/training_box/base.py +120 -0
- agentscope_runtime/sandbox/box/training_box/env_service.py +752 -0
- agentscope_runtime/sandbox/box/training_box/environments/__init__.py +0 -0
- agentscope_runtime/sandbox/box/training_box/environments/appworld/appworld_env.py +987 -0
- agentscope_runtime/sandbox/box/training_box/registry.py +54 -0
- agentscope_runtime/sandbox/box/training_box/src/trajectory.py +278 -0
- agentscope_runtime/sandbox/box/training_box/training_box.py +219 -0
- agentscope_runtime/sandbox/build.py +213 -0
- agentscope_runtime/sandbox/client/__init__.py +5 -0
- agentscope_runtime/sandbox/client/http_client.py +527 -0
- agentscope_runtime/sandbox/client/training_client.py +265 -0
- agentscope_runtime/sandbox/constant.py +5 -0
- agentscope_runtime/sandbox/custom/__init__.py +16 -0
- agentscope_runtime/sandbox/custom/custom_sandbox.py +40 -0
- agentscope_runtime/sandbox/custom/example.py +37 -0
- agentscope_runtime/sandbox/enums.py +68 -0
- agentscope_runtime/sandbox/manager/__init__.py +4 -0
- agentscope_runtime/sandbox/manager/collections/__init__.py +22 -0
- agentscope_runtime/sandbox/manager/collections/base_mapping.py +20 -0
- agentscope_runtime/sandbox/manager/collections/base_queue.py +25 -0
- agentscope_runtime/sandbox/manager/collections/base_set.py +25 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_mapping.py +22 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_queue.py +28 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_set.py +27 -0
- agentscope_runtime/sandbox/manager/collections/redis_mapping.py +26 -0
- agentscope_runtime/sandbox/manager/collections/redis_queue.py +27 -0
- agentscope_runtime/sandbox/manager/collections/redis_set.py +23 -0
- agentscope_runtime/sandbox/manager/container_clients/__init__.py +8 -0
- agentscope_runtime/sandbox/manager/container_clients/base_client.py +39 -0
- agentscope_runtime/sandbox/manager/container_clients/docker_client.py +170 -0
- agentscope_runtime/sandbox/manager/sandbox_manager.py +694 -0
- agentscope_runtime/sandbox/manager/server/__init__.py +0 -0
- agentscope_runtime/sandbox/manager/server/app.py +194 -0
- agentscope_runtime/sandbox/manager/server/config.py +68 -0
- agentscope_runtime/sandbox/manager/server/models.py +17 -0
- agentscope_runtime/sandbox/manager/storage/__init__.py +10 -0
- agentscope_runtime/sandbox/manager/storage/data_storage.py +16 -0
- agentscope_runtime/sandbox/manager/storage/local_storage.py +44 -0
- agentscope_runtime/sandbox/manager/storage/oss_storage.py +89 -0
- agentscope_runtime/sandbox/manager/utils.py +78 -0
- agentscope_runtime/sandbox/mcp_server.py +192 -0
- agentscope_runtime/sandbox/model/__init__.py +12 -0
- agentscope_runtime/sandbox/model/api.py +16 -0
- agentscope_runtime/sandbox/model/container.py +72 -0
- agentscope_runtime/sandbox/model/manager_config.py +158 -0
- agentscope_runtime/sandbox/registry.py +129 -0
- agentscope_runtime/sandbox/tools/__init__.py +12 -0
- agentscope_runtime/sandbox/tools/base/__init__.py +8 -0
- agentscope_runtime/sandbox/tools/base/tool.py +52 -0
- agentscope_runtime/sandbox/tools/browser/__init__.py +57 -0
- agentscope_runtime/sandbox/tools/browser/tool.py +597 -0
- agentscope_runtime/sandbox/tools/filesystem/__init__.py +32 -0
- agentscope_runtime/sandbox/tools/filesystem/tool.py +319 -0
- agentscope_runtime/sandbox/tools/function_tool.py +321 -0
- agentscope_runtime/sandbox/tools/mcp_tool.py +191 -0
- agentscope_runtime/sandbox/tools/sandbox_tool.py +104 -0
- agentscope_runtime/sandbox/tools/tool.py +123 -0
- agentscope_runtime/sandbox/tools/utils.py +68 -0
- agentscope_runtime/version.py +2 -0
- agentscope_runtime-0.1.0.dist-info/METADATA +327 -0
- agentscope_runtime-0.1.0.dist-info/RECORD +131 -0
- agentscope_runtime-0.1.0.dist-info/WHEEL +5 -0
- agentscope_runtime-0.1.0.dist-info/entry_points.txt +4 -0
- agentscope_runtime-0.1.0.dist-info/licenses/LICENSE +202 -0
- agentscope_runtime-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from typing import Optional, Any, Dict, Set, List
|
|
3
|
+
|
|
4
|
+
from ..enums import SandboxType
|
|
5
|
+
from ..box.sandbox import Sandbox
|
|
6
|
+
from .sandbox_tool import SandboxTool
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MCPTool(SandboxTool):
|
|
10
|
+
"""MCP tool class."""
|
|
11
|
+
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
*,
|
|
15
|
+
sandbox: Optional[Any] = None,
|
|
16
|
+
name: Optional[str] = None,
|
|
17
|
+
sandbox_type: Optional[SandboxType | str] = None,
|
|
18
|
+
tool_type: Optional[str] = None,
|
|
19
|
+
schema: Optional[Dict] = None,
|
|
20
|
+
server_configs: Optional[Dict] = None,
|
|
21
|
+
):
|
|
22
|
+
"""
|
|
23
|
+
Initialize the MCP tool with an additional MCP server configuration.
|
|
24
|
+
"""
|
|
25
|
+
super().__init__(
|
|
26
|
+
sandbox=sandbox,
|
|
27
|
+
name=name,
|
|
28
|
+
sandbox_type=sandbox_type,
|
|
29
|
+
tool_type=tool_type,
|
|
30
|
+
schema=schema,
|
|
31
|
+
)
|
|
32
|
+
self._server_configs = server_configs
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def server_configs(self) -> Optional[Dict]:
|
|
36
|
+
return self._server_configs
|
|
37
|
+
|
|
38
|
+
@server_configs.setter
|
|
39
|
+
def server_configs(self, config: Dict):
|
|
40
|
+
if not isinstance(config, dict) and "mcpServers" not in config:
|
|
41
|
+
raise ValueError(
|
|
42
|
+
"MCP server configuration must be a valid dictionary.",
|
|
43
|
+
)
|
|
44
|
+
self._server_configs = config
|
|
45
|
+
|
|
46
|
+
def bind(self, sandbox: Sandbox):
|
|
47
|
+
"""
|
|
48
|
+
Return a new instance bound with a specific sandbox (immutable mode).
|
|
49
|
+
"""
|
|
50
|
+
if not isinstance(sandbox, Sandbox):
|
|
51
|
+
raise TypeError(
|
|
52
|
+
"The provided sandbox must be an instance of `Sandbox`.",
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
assert self.sandbox_type == sandbox.sandbox_type, (
|
|
56
|
+
f"Sandbox type mismatch! The tool requires a sandbox of type "
|
|
57
|
+
f"`{self.sandbox_type}`, but a sandbox of type"
|
|
58
|
+
f" `{sandbox.sandbox_type}` was provided."
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
return self.__class__(
|
|
62
|
+
sandbox=sandbox,
|
|
63
|
+
name=self._name,
|
|
64
|
+
sandbox_type=self._sandbox_type,
|
|
65
|
+
tool_type=self._tool_type,
|
|
66
|
+
schema=self._schema,
|
|
67
|
+
server_configs=self._server_configs,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class MCPConfigConverter:
|
|
72
|
+
"""MCP configuration management class."""
|
|
73
|
+
|
|
74
|
+
def __init__(
|
|
75
|
+
self,
|
|
76
|
+
server_configs: Dict,
|
|
77
|
+
*,
|
|
78
|
+
sandbox: Optional[Sandbox] = None,
|
|
79
|
+
whitelist: Optional[Set[str]] = None,
|
|
80
|
+
blacklist: Optional[Set[str]] = None,
|
|
81
|
+
) -> None:
|
|
82
|
+
"""
|
|
83
|
+
Initializes MCP configuration.
|
|
84
|
+
|
|
85
|
+
Args:
|
|
86
|
+
server_configs: Dictionary of MCP server configurations.
|
|
87
|
+
whitelist: Whitelist; if set, only functions in the whitelist
|
|
88
|
+
are allowed.
|
|
89
|
+
blacklist: Blacklist; functions in the blacklist will be excluded.
|
|
90
|
+
"""
|
|
91
|
+
self.server_configs = server_configs
|
|
92
|
+
self.whitelist = whitelist or set()
|
|
93
|
+
self.blacklist = blacklist or set()
|
|
94
|
+
self._sandbox = sandbox
|
|
95
|
+
|
|
96
|
+
assert (
|
|
97
|
+
"mcpServers" in server_configs
|
|
98
|
+
), "MCP server config must contain 'mcpServers'"
|
|
99
|
+
|
|
100
|
+
def bind(self, sandbox: Sandbox) -> "MCPConfigConverter":
|
|
101
|
+
"""
|
|
102
|
+
Binds a sandbox and returns a new instance.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
sandbox: The sandbox object to bind.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
A new configuration instance bound to the sandbox.
|
|
109
|
+
"""
|
|
110
|
+
new_config = self.__class__(
|
|
111
|
+
server_configs=self.server_configs.copy(),
|
|
112
|
+
sandbox=sandbox,
|
|
113
|
+
whitelist=self.whitelist.copy(),
|
|
114
|
+
blacklist=self.blacklist.copy(),
|
|
115
|
+
)
|
|
116
|
+
return new_config
|
|
117
|
+
|
|
118
|
+
def to_builtin_tools(
|
|
119
|
+
self,
|
|
120
|
+
*,
|
|
121
|
+
sandbox: Optional[Sandbox] = None,
|
|
122
|
+
) -> List[MCPTool]:
|
|
123
|
+
"""
|
|
124
|
+
Converts to a list of MCPTool instances.
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
A list of bound MCPTool objects.
|
|
128
|
+
"""
|
|
129
|
+
box = sandbox or self._sandbox
|
|
130
|
+
if box is None:
|
|
131
|
+
from ..registry import SandboxRegistry
|
|
132
|
+
|
|
133
|
+
cls_ = SandboxRegistry.get_classes_by_type("base")
|
|
134
|
+
# Use proper context manager
|
|
135
|
+
with cls_() as tmp_box:
|
|
136
|
+
return self._process_tools(tmp_box)
|
|
137
|
+
else:
|
|
138
|
+
return self._process_tools(box)
|
|
139
|
+
|
|
140
|
+
def _process_tools(self, box: Sandbox) -> List[MCPTool]:
|
|
141
|
+
"""Helper method to process tools with the given sandbox."""
|
|
142
|
+
tools_to_add = []
|
|
143
|
+
box.add_mcp_servers(
|
|
144
|
+
server_configs=self.server_configs,
|
|
145
|
+
overwrite=False,
|
|
146
|
+
)
|
|
147
|
+
for server_name in self.server_configs["mcpServers"]:
|
|
148
|
+
tools = box.list_tools(tool_type=server_name).get(server_name, [])
|
|
149
|
+
for tool_name, tool_info in tools.items():
|
|
150
|
+
if self.whitelist and tool_name not in self.whitelist:
|
|
151
|
+
continue
|
|
152
|
+
if self.blacklist and tool_name in self.blacklist:
|
|
153
|
+
continue
|
|
154
|
+
|
|
155
|
+
tools_to_add.append(
|
|
156
|
+
MCPTool(
|
|
157
|
+
sandbox=box,
|
|
158
|
+
name=tool_name,
|
|
159
|
+
sandbox_type=SandboxType.BASE,
|
|
160
|
+
tool_type=server_name,
|
|
161
|
+
schema=tool_info["json_schema"]["function"],
|
|
162
|
+
server_configs={
|
|
163
|
+
"mcpServers": {
|
|
164
|
+
server_name: self.server_configs["mcpServers"][
|
|
165
|
+
server_name
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
),
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
return tools_to_add
|
|
173
|
+
|
|
174
|
+
@classmethod
|
|
175
|
+
def from_dict(
|
|
176
|
+
cls,
|
|
177
|
+
config_dict: Dict,
|
|
178
|
+
whitelist: Optional[Set[str]] = None,
|
|
179
|
+
blacklist: Optional[Set[str]] = None,
|
|
180
|
+
) -> "MCPConfigConverter":
|
|
181
|
+
"""Creates a configuration instance from a dictionary.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
config_dict: Configuration dictionary.
|
|
185
|
+
whitelist: Whitelist.
|
|
186
|
+
blacklist: Blacklist.
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
An instance of McpConfig.
|
|
190
|
+
"""
|
|
191
|
+
return cls(config_dict, whitelist=whitelist, blacklist=blacklist)
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from typing import Optional, Any, Dict
|
|
3
|
+
|
|
4
|
+
from .tool import Tool
|
|
5
|
+
from ..enums import SandboxType
|
|
6
|
+
from ..box.sandbox import Sandbox
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SandboxTool(Tool):
|
|
10
|
+
"""Built-in tool class"""
|
|
11
|
+
|
|
12
|
+
_name: str = None
|
|
13
|
+
_sandbox_type: SandboxType | str = None
|
|
14
|
+
_tool_type: str = None
|
|
15
|
+
_schema: Dict = None
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
*,
|
|
20
|
+
sandbox: Optional[Any] = None,
|
|
21
|
+
name: Optional[str] = None,
|
|
22
|
+
sandbox_type: Optional[SandboxType | str] = None,
|
|
23
|
+
tool_type: Optional[str] = None,
|
|
24
|
+
schema: Optional[Dict] = None,
|
|
25
|
+
):
|
|
26
|
+
"""
|
|
27
|
+
Initialize the tool.
|
|
28
|
+
|
|
29
|
+
Note: Once the sandbox is set, it does not change.
|
|
30
|
+
"""
|
|
31
|
+
self._sandbox = sandbox
|
|
32
|
+
self._name = name or self.__class__._name
|
|
33
|
+
self._sandbox_type = sandbox_type or self.__class__._sandbox_type
|
|
34
|
+
self._tool_type = tool_type or self.__class__._tool_type
|
|
35
|
+
self._schema = schema or self.__class__._schema
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def name(self) -> str:
|
|
39
|
+
return self._name
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def sandbox_type(self) -> SandboxType:
|
|
43
|
+
return SandboxType(self._sandbox_type)
|
|
44
|
+
|
|
45
|
+
@property
|
|
46
|
+
def tool_type(self) -> str:
|
|
47
|
+
return self._tool_type
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def schema(self) -> Dict:
|
|
51
|
+
return {
|
|
52
|
+
"type": "function",
|
|
53
|
+
"function": self._schema,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def sandbox(self):
|
|
58
|
+
return self._sandbox
|
|
59
|
+
|
|
60
|
+
def __call__(self, *, sandbox: Optional[Any] = None, **kwargs):
|
|
61
|
+
"""Call the tool, allowing a temporary sandbox to be specified"""
|
|
62
|
+
return self.call(sandbox=sandbox, **kwargs)
|
|
63
|
+
|
|
64
|
+
def call(self, *, sandbox: Optional[Any] = None, **kwargs):
|
|
65
|
+
"""
|
|
66
|
+
Execute the tool call.
|
|
67
|
+
Args:
|
|
68
|
+
sandbox: Temporarily used sandbox, highest priority
|
|
69
|
+
**kwargs: Tool parameters
|
|
70
|
+
"""
|
|
71
|
+
# Priority: temporary sandbox > instance sandbox > None (dryrun)
|
|
72
|
+
box = sandbox or self._sandbox
|
|
73
|
+
if box is None:
|
|
74
|
+
return self._dryrun_call(**kwargs)
|
|
75
|
+
else:
|
|
76
|
+
return box.call_tool(self.name, arguments=kwargs)
|
|
77
|
+
|
|
78
|
+
def bind(self, sandbox: Sandbox):
|
|
79
|
+
"""
|
|
80
|
+
Return a new instance bound with a specific sandbox (immutable mode).
|
|
81
|
+
"""
|
|
82
|
+
if not isinstance(sandbox, Sandbox):
|
|
83
|
+
raise TypeError(
|
|
84
|
+
"The provided sandbox must be an instance of `Sandbox`.",
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
assert self.sandbox_type == sandbox.sandbox_type, (
|
|
88
|
+
f"Sandbox type mismatch! The tool requires a sandbox of type "
|
|
89
|
+
f"`{self.sandbox_type}`, but a sandbox of type"
|
|
90
|
+
f" `{sandbox.sandbox_type}` was provided."
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
return self.__class__(sandbox=sandbox)
|
|
94
|
+
|
|
95
|
+
def _dryrun_call(self, **kwargs):
|
|
96
|
+
"""
|
|
97
|
+
Dryrun mode: temporarily create a sandbox
|
|
98
|
+
"""
|
|
99
|
+
from ..registry import SandboxRegistry
|
|
100
|
+
|
|
101
|
+
cls_ = SandboxRegistry.get_classes_by_type(self.sandbox_type)
|
|
102
|
+
|
|
103
|
+
with cls_() as box:
|
|
104
|
+
return box.call_tool(self.name, arguments=kwargs)
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# pylint: disable=unused-argument
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import Optional, Any, Dict
|
|
5
|
+
from ..enums import SandboxType
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Tool(ABC):
|
|
9
|
+
"""Abstract base class for all tools.
|
|
10
|
+
|
|
11
|
+
This abstract class defines the common interface that all tool
|
|
12
|
+
implementations must follow, including both SandboxTool and FunctionTool.
|
|
13
|
+
|
|
14
|
+
Key responsibilities:
|
|
15
|
+
- Define the standard tool interface
|
|
16
|
+
- Ensure consistent behavior across different tool types
|
|
17
|
+
- Provide common functionality where applicable
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
*,
|
|
23
|
+
name: Optional[str] = None,
|
|
24
|
+
tool_type: Optional[str] = None,
|
|
25
|
+
**kwargs,
|
|
26
|
+
):
|
|
27
|
+
"""
|
|
28
|
+
Initialize the tool with basic parameters.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
name: Tool name
|
|
32
|
+
tool_type: Tool type identifier
|
|
33
|
+
**kwargs: Additional tool-specific parameters
|
|
34
|
+
"""
|
|
35
|
+
self._name = name
|
|
36
|
+
self._tool_type = tool_type
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
@abstractmethod
|
|
40
|
+
def name(self) -> str:
|
|
41
|
+
"""Get the tool name."""
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
@abstractmethod
|
|
45
|
+
def tool_type(self) -> str:
|
|
46
|
+
"""Get the tool type."""
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
@abstractmethod
|
|
50
|
+
def schema(self) -> Dict:
|
|
51
|
+
"""Get the tool schema in OpenAI function calling format."""
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
@abstractmethod
|
|
55
|
+
def sandbox_type(self) -> SandboxType:
|
|
56
|
+
"""Get the required sandbox type for this tool."""
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
@abstractmethod
|
|
60
|
+
def sandbox(self) -> Optional[Any]:
|
|
61
|
+
"""Get the current sandbox instance (if any)."""
|
|
62
|
+
|
|
63
|
+
def __call__(self, *, sandbox: Optional[Any] = None, **kwargs):
|
|
64
|
+
"""Call the tool with optional sandbox override.
|
|
65
|
+
|
|
66
|
+
This is a convenience method that delegates to the call() method.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
sandbox: Optional sandbox to use for this call
|
|
70
|
+
**kwargs: Tool parameters
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
Tool execution result
|
|
74
|
+
"""
|
|
75
|
+
return self.call(sandbox=sandbox, **kwargs)
|
|
76
|
+
|
|
77
|
+
@abstractmethod
|
|
78
|
+
def call(self, *, sandbox: Optional[Any] = None, **kwargs) -> Dict:
|
|
79
|
+
"""Execute the tool call.
|
|
80
|
+
|
|
81
|
+
This is the core method that each tool implementation must provide.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
sandbox: Optional sandbox to use for this call
|
|
85
|
+
**kwargs: Tool parameters
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
Tool execution result in standardized format:
|
|
89
|
+
{
|
|
90
|
+
"meta": Optional[Dict],
|
|
91
|
+
"content": List[Dict],
|
|
92
|
+
"isError": bool
|
|
93
|
+
}
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
@abstractmethod
|
|
97
|
+
def bind(self, *args, **kwargs) -> "Tool":
|
|
98
|
+
"""Bind parameters or context to create a new tool instance.
|
|
99
|
+
|
|
100
|
+
The specific binding behavior depends on the tool type:
|
|
101
|
+
- SandboxTool: binds to a sandbox
|
|
102
|
+
- FunctionTool: binds function parameters
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
New tool instance with bound parameters/context
|
|
106
|
+
"""
|
|
107
|
+
|
|
108
|
+
def __str__(self) -> str:
|
|
109
|
+
"""String representation of the tool."""
|
|
110
|
+
return (
|
|
111
|
+
f"{self.__class__.__name__}(name='{self.name}', type="
|
|
112
|
+
f"'{self.tool_type}')"
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
def __repr__(self) -> str:
|
|
116
|
+
"""Detailed string representation of the tool."""
|
|
117
|
+
return (
|
|
118
|
+
f"{self.__class__.__name__}("
|
|
119
|
+
f"name='{self.name}', "
|
|
120
|
+
f"tool_type='{self.tool_type}', "
|
|
121
|
+
f"sandbox_type='{self.sandbox_type}'"
|
|
122
|
+
f")"
|
|
123
|
+
)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
def setup_tools(
|
|
3
|
+
tools,
|
|
4
|
+
environment_manager,
|
|
5
|
+
session_id,
|
|
6
|
+
user_id,
|
|
7
|
+
include_schemas=False,
|
|
8
|
+
):
|
|
9
|
+
"""
|
|
10
|
+
Generic function to activate tools.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
tools: List of tools to activate.
|
|
14
|
+
environment_manager: Environment manager
|
|
15
|
+
session_id: ID of the session.
|
|
16
|
+
user_id: ID of the user.
|
|
17
|
+
include_schemas: Boolean flag to indicate if schemas should be
|
|
18
|
+
included.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
tuple: (activated_tools, schemas) if include_schemas=True,
|
|
22
|
+
else only activated_tools.
|
|
23
|
+
"""
|
|
24
|
+
# Lazy import
|
|
25
|
+
from .tool import Tool
|
|
26
|
+
from .sandbox_tool import SandboxTool
|
|
27
|
+
from ...sandbox.box.dummy.dummy_sandbox import DummySandbox
|
|
28
|
+
|
|
29
|
+
enable_sandbox = False
|
|
30
|
+
|
|
31
|
+
# Check tool types and determine if sandbox needs to be enabled
|
|
32
|
+
for tool in tools:
|
|
33
|
+
assert isinstance(tool, Tool), f"{tool} must be an instance of Tool"
|
|
34
|
+
if isinstance(tool, SandboxTool):
|
|
35
|
+
enable_sandbox = True
|
|
36
|
+
break
|
|
37
|
+
|
|
38
|
+
# Check environment service
|
|
39
|
+
if enable_sandbox and environment_manager is None:
|
|
40
|
+
raise ValueError("environment_manager is not set")
|
|
41
|
+
|
|
42
|
+
# Connect to sandbox if required
|
|
43
|
+
if enable_sandbox:
|
|
44
|
+
sandboxes = environment_manager.connect_sandbox(
|
|
45
|
+
session_id=session_id,
|
|
46
|
+
user_id=user_id,
|
|
47
|
+
tools=tools,
|
|
48
|
+
)
|
|
49
|
+
else:
|
|
50
|
+
sandboxes = [DummySandbox()]
|
|
51
|
+
|
|
52
|
+
# Bind tools to sandbox and prepare schemas if required
|
|
53
|
+
schemas = [] # Initialize schemas list
|
|
54
|
+
activated_tools = [] # Initialize activated tools list
|
|
55
|
+
|
|
56
|
+
for tool in tools:
|
|
57
|
+
if include_schemas:
|
|
58
|
+
schemas.append(tool.schema) # Collect tool schemas
|
|
59
|
+
for sandbox in sandboxes:
|
|
60
|
+
if sandbox.sandbox_type == tool.sandbox_type:
|
|
61
|
+
bound_tool = tool.bind(sandbox) # Bind tool to the sandbox
|
|
62
|
+
activated_tools.append(
|
|
63
|
+
bound_tool,
|
|
64
|
+
) # Add to the activated tools list
|
|
65
|
+
|
|
66
|
+
if include_schemas:
|
|
67
|
+
return activated_tools, schemas
|
|
68
|
+
return activated_tools
|