agentrun-sdk 0.0.4__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.
Files changed (128) hide show
  1. agentrun/__init__.py +209 -0
  2. agentrun/agent_runtime/__client_async_template.py +466 -0
  3. agentrun/agent_runtime/__endpoint_async_template.py +345 -0
  4. agentrun/agent_runtime/__init__.py +53 -0
  5. agentrun/agent_runtime/__runtime_async_template.py +477 -0
  6. agentrun/agent_runtime/api/__data_async_template.py +58 -0
  7. agentrun/agent_runtime/api/__init__.py +6 -0
  8. agentrun/agent_runtime/api/control.py +1362 -0
  9. agentrun/agent_runtime/api/data.py +98 -0
  10. agentrun/agent_runtime/client.py +868 -0
  11. agentrun/agent_runtime/endpoint.py +649 -0
  12. agentrun/agent_runtime/model.py +362 -0
  13. agentrun/agent_runtime/runtime.py +904 -0
  14. agentrun/credential/__client_async_template.py +177 -0
  15. agentrun/credential/__credential_async_template.py +216 -0
  16. agentrun/credential/__init__.py +28 -0
  17. agentrun/credential/api/__init__.py +5 -0
  18. agentrun/credential/api/control.py +606 -0
  19. agentrun/credential/client.py +319 -0
  20. agentrun/credential/credential.py +381 -0
  21. agentrun/credential/model.py +248 -0
  22. agentrun/integration/__init__.py +21 -0
  23. agentrun/integration/agentscope/__init__.py +12 -0
  24. agentrun/integration/agentscope/adapter.py +17 -0
  25. agentrun/integration/agentscope/builtin.py +65 -0
  26. agentrun/integration/agentscope/message_adapter.py +185 -0
  27. agentrun/integration/agentscope/model_adapter.py +60 -0
  28. agentrun/integration/agentscope/tool_adapter.py +59 -0
  29. agentrun/integration/builtin/__init__.py +16 -0
  30. agentrun/integration/builtin/model.py +97 -0
  31. agentrun/integration/builtin/sandbox.py +276 -0
  32. agentrun/integration/builtin/toolset.py +47 -0
  33. agentrun/integration/crewai/__init__.py +12 -0
  34. agentrun/integration/crewai/adapter.py +9 -0
  35. agentrun/integration/crewai/builtin.py +65 -0
  36. agentrun/integration/crewai/model_adapter.py +27 -0
  37. agentrun/integration/crewai/tool_adapter.py +26 -0
  38. agentrun/integration/google_adk/__init__.py +12 -0
  39. agentrun/integration/google_adk/adapter.py +15 -0
  40. agentrun/integration/google_adk/builtin.py +65 -0
  41. agentrun/integration/google_adk/message_adapter.py +144 -0
  42. agentrun/integration/google_adk/model_adapter.py +43 -0
  43. agentrun/integration/google_adk/tool_adapter.py +25 -0
  44. agentrun/integration/langchain/__init__.py +9 -0
  45. agentrun/integration/langchain/adapter.py +15 -0
  46. agentrun/integration/langchain/builtin.py +71 -0
  47. agentrun/integration/langchain/message_adapter.py +141 -0
  48. agentrun/integration/langchain/model_adapter.py +37 -0
  49. agentrun/integration/langchain/tool_adapter.py +50 -0
  50. agentrun/integration/langgraph/__init__.py +13 -0
  51. agentrun/integration/langgraph/adapter.py +20 -0
  52. agentrun/integration/langgraph/builtin.py +65 -0
  53. agentrun/integration/pydantic_ai/__init__.py +12 -0
  54. agentrun/integration/pydantic_ai/adapter.py +13 -0
  55. agentrun/integration/pydantic_ai/builtin.py +65 -0
  56. agentrun/integration/pydantic_ai/model_adapter.py +44 -0
  57. agentrun/integration/pydantic_ai/tool_adapter.py +19 -0
  58. agentrun/integration/utils/__init__.py +112 -0
  59. agentrun/integration/utils/adapter.py +167 -0
  60. agentrun/integration/utils/canonical.py +157 -0
  61. agentrun/integration/utils/converter.py +134 -0
  62. agentrun/integration/utils/model.py +107 -0
  63. agentrun/integration/utils/tool.py +1714 -0
  64. agentrun/model/__client_async_template.py +357 -0
  65. agentrun/model/__init__.py +57 -0
  66. agentrun/model/__model_proxy_async_template.py +270 -0
  67. agentrun/model/__model_service_async_template.py +267 -0
  68. agentrun/model/api/__init__.py +6 -0
  69. agentrun/model/api/control.py +1173 -0
  70. agentrun/model/api/data.py +196 -0
  71. agentrun/model/client.py +674 -0
  72. agentrun/model/model.py +218 -0
  73. agentrun/model/model_proxy.py +439 -0
  74. agentrun/model/model_service.py +438 -0
  75. agentrun/sandbox/__browser_sandbox_async_template.py +113 -0
  76. agentrun/sandbox/__client_async_template.py +466 -0
  77. agentrun/sandbox/__code_interpreter_sandbox_async_template.py +466 -0
  78. agentrun/sandbox/__init__.py +54 -0
  79. agentrun/sandbox/__sandbox_async_template.py +398 -0
  80. agentrun/sandbox/__template_async_template.py +150 -0
  81. agentrun/sandbox/api/__browser_data_async_template.py +140 -0
  82. agentrun/sandbox/api/__code_interpreter_data_async_template.py +206 -0
  83. agentrun/sandbox/api/__init__.py +17 -0
  84. agentrun/sandbox/api/__sandbox_data_async_template.py +100 -0
  85. agentrun/sandbox/api/browser_data.py +172 -0
  86. agentrun/sandbox/api/code_interpreter_data.py +396 -0
  87. agentrun/sandbox/api/control.py +1051 -0
  88. agentrun/sandbox/api/playwright_async.py +492 -0
  89. agentrun/sandbox/api/playwright_sync.py +492 -0
  90. agentrun/sandbox/api/sandbox_data.py +140 -0
  91. agentrun/sandbox/browser_sandbox.py +191 -0
  92. agentrun/sandbox/client.py +878 -0
  93. agentrun/sandbox/code_interpreter_sandbox.py +829 -0
  94. agentrun/sandbox/model.py +269 -0
  95. agentrun/sandbox/sandbox.py +737 -0
  96. agentrun/sandbox/template.py +215 -0
  97. agentrun/server/__init__.py +82 -0
  98. agentrun/server/invoker.py +131 -0
  99. agentrun/server/model.py +225 -0
  100. agentrun/server/openai_protocol.py +798 -0
  101. agentrun/server/protocol.py +96 -0
  102. agentrun/server/server.py +192 -0
  103. agentrun/toolset/__client_async_template.py +62 -0
  104. agentrun/toolset/__init__.py +51 -0
  105. agentrun/toolset/__toolset_async_template.py +204 -0
  106. agentrun/toolset/api/__init__.py +17 -0
  107. agentrun/toolset/api/control.py +262 -0
  108. agentrun/toolset/api/mcp.py +100 -0
  109. agentrun/toolset/api/openapi.py +1184 -0
  110. agentrun/toolset/client.py +102 -0
  111. agentrun/toolset/model.py +160 -0
  112. agentrun/toolset/toolset.py +271 -0
  113. agentrun/utils/__data_api_async_template.py +715 -0
  114. agentrun/utils/__init__.py +5 -0
  115. agentrun/utils/__resource_async_template.py +158 -0
  116. agentrun/utils/config.py +258 -0
  117. agentrun/utils/control_api.py +78 -0
  118. agentrun/utils/data_api.py +1110 -0
  119. agentrun/utils/exception.py +149 -0
  120. agentrun/utils/helper.py +34 -0
  121. agentrun/utils/log.py +77 -0
  122. agentrun/utils/model.py +168 -0
  123. agentrun/utils/resource.py +291 -0
  124. agentrun_sdk-0.0.4.dist-info/METADATA +262 -0
  125. agentrun_sdk-0.0.4.dist-info/RECORD +128 -0
  126. agentrun_sdk-0.0.4.dist-info/WHEEL +5 -0
  127. agentrun_sdk-0.0.4.dist-info/licenses/LICENSE +201 -0
  128. agentrun_sdk-0.0.4.dist-info/top_level.txt +1 -0
@@ -0,0 +1,97 @@
1
+ """内置模型集成函数 / Built-in Model Integration Functions
2
+
3
+ 提供快速创建通用模型对象的便捷函数。
4
+ Provides convenient functions for quickly creating common model objects.
5
+ """
6
+
7
+ from typing import Optional, overload, TypedDict, Union
8
+
9
+ from typing_extensions import NotRequired, Unpack
10
+
11
+ from agentrun.integration.utils.model import CommonModel
12
+ from agentrun.model import ModelService
13
+ from agentrun.model.model import BackendType
14
+ from agentrun.model.model_proxy import ModelProxy
15
+ from agentrun.utils.config import Config
16
+
17
+
18
+ class ModelArgs(TypedDict):
19
+ model: NotRequired[Optional[str]]
20
+ backend_type: NotRequired[Optional[BackendType]]
21
+ config: NotRequired[Optional[Config]]
22
+
23
+
24
+ @overload
25
+ def model(input: ModelService, **kwargs: Unpack[ModelArgs]) -> CommonModel:
26
+ ...
27
+
28
+
29
+ @overload
30
+ def model(input: ModelProxy, **kwargs: Unpack[ModelArgs]) -> CommonModel:
31
+ ...
32
+
33
+
34
+ @overload
35
+ def model(input: str, **kwargs: Unpack[ModelArgs]) -> CommonModel:
36
+ ...
37
+
38
+
39
+ def model(
40
+ input: Union[str, ModelProxy, ModelService], **kwargs: Unpack[ModelArgs]
41
+ ) -> CommonModel:
42
+ """获取 AgentRun 模型并封装为通用 Model 对象 / Get AgentRun model and wrap as CommonModel
43
+
44
+ 等价于 ModelClient.get(),但返回通用 Model 对象。
45
+ Equivalent to ModelClient.get(), but returns a CommonModel object.
46
+
47
+ Args:
48
+ input: AgentRun 模型名称、ModelProxy 或 ModelService 实例 / Model name, ModelProxy or ModelService instance
49
+ model: 要请求的模型名称(默认请求数组的第一个模型或模型治理的自动负载均衡模型) / Model name to request (defaults to the first model in the array or the auto-load balancing model of model governance)
50
+ backend_type: 后端类型(PROXY 或 SERVICE) / Backend type (PROXY or SERVICE)
51
+ config: 配置对象 / Configuration object
52
+
53
+ Returns:
54
+ CommonModel: 通用模型实例 / CommonModel instance
55
+
56
+ Examples:
57
+ >>> # 从模型名称创建 / Create from model name
58
+ >>> m = model("qwen-max")
59
+ >>>
60
+ >>> # 从 ModelProxy 创建 / Create from ModelProxy
61
+ >>> proxy = ModelProxy.get_by_name("my-proxy")
62
+ >>> m = model(proxy)
63
+ >>>
64
+ >>> # 从 ModelService 创建 / Create from ModelService
65
+ >>> service = ModelService.get_by_name("my-service")
66
+ >>> m = model(service)
67
+ """
68
+ config = kwargs.get("config")
69
+ backend_type = kwargs.get("backend_type")
70
+ model = kwargs.get("model")
71
+
72
+ if isinstance(input, ModelProxy):
73
+ return CommonModel(
74
+ model=model or "",
75
+ model_obj=input,
76
+ backend_type=BackendType.PROXY,
77
+ config=config,
78
+ )
79
+ elif isinstance(input, ModelService):
80
+ return CommonModel(
81
+ model=model or "",
82
+ model_obj=input,
83
+ backend_type=BackendType.SERVICE,
84
+ config=config,
85
+ )
86
+
87
+ from agentrun.model.client import ModelClient
88
+
89
+ client = ModelClient(config=config)
90
+ model_obj = client.get(name=input, backend_type=backend_type, config=config)
91
+
92
+ return CommonModel(
93
+ model=input,
94
+ model_obj=model_obj,
95
+ backend_type=backend_type,
96
+ config=config,
97
+ )
@@ -0,0 +1,276 @@
1
+ from __future__ import annotations
2
+
3
+ import atexit
4
+ import threading
5
+ import time
6
+ from typing import Any, Callable, Dict, Optional
7
+
8
+ from agentrun.integration.utils.tool import CommonToolSet, tool
9
+ from agentrun.sandbox import Sandbox, TemplateType
10
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
11
+ from agentrun.sandbox.client import SandboxClient
12
+ from agentrun.sandbox.code_interpreter_sandbox import CodeInterpreterSandbox
13
+ from agentrun.utils.config import Config
14
+ from agentrun.utils.log import logger
15
+
16
+
17
+ class SandboxToolSet(CommonToolSet):
18
+
19
+ def __init__(
20
+ self,
21
+ template_name: str,
22
+ template_type: TemplateType,
23
+ *,
24
+ sandbox_idle_timeout_seconds: int,
25
+ config: Optional[Config],
26
+ ):
27
+ super().__init__()
28
+
29
+ self.config = config
30
+ self.client = SandboxClient(config)
31
+ self.lock = threading.Lock()
32
+
33
+ self.template_name = template_name
34
+ self.template_type = template_type
35
+ self.sandbox_idle_timeout_seconds = sandbox_idle_timeout_seconds
36
+
37
+ self.sandbox: Optional[Sandbox] = None
38
+ self.sandbox_id = ""
39
+
40
+ def close(self):
41
+ if self.sandbox:
42
+ try:
43
+ self.sandbox.stop()
44
+ except Exception as e:
45
+ logger.debug("delete sandbox failed, due to %s", e)
46
+
47
+ def _ensure_sandbox(self):
48
+ # 先确定当前是否有 sandbox,如果有的话提供这个
49
+ if self.sandbox is not None:
50
+ return self.sandbox
51
+
52
+ # 如果没有,则创建一个新的 Sandbox
53
+ with self.lock:
54
+ if self.sandbox is None:
55
+ self.sandbox = Sandbox.create(
56
+ template_type=self.template_type,
57
+ template_name=self.template_name,
58
+ sandbox_idle_timeout_seconds=self.sandbox_idle_timeout_seconds,
59
+ config=self.config,
60
+ )
61
+ self.sandbox_id = self.sandbox.sandbox_id
62
+ self.sandbox.__enter__()
63
+
64
+ return self.sandbox
65
+
66
+ def _run_in_sandbox(self, callback: Callable[[Sandbox], Any]):
67
+ sb = self._ensure_sandbox()
68
+ try:
69
+ return callback(sb)
70
+ except Exception as e:
71
+ try:
72
+ logger.debug(
73
+ "run in sandbox failed, due to %s, try to re-create"
74
+ " sandbox",
75
+ e,
76
+ )
77
+
78
+ self.sandbox = None
79
+ sb = self._ensure_sandbox()
80
+ return callback(sb)
81
+ except Exception as e2:
82
+ logger.debug("re-created sandbox run failed, due to %s", e2)
83
+ return {"error": f"{e!s}"}
84
+
85
+
86
+ class CodeInterpreterToolSet(SandboxToolSet):
87
+ """LangChain 代码沙箱工具适配器。"""
88
+
89
+ def __init__(
90
+ self,
91
+ template_name: str,
92
+ config: Optional[Config],
93
+ sandbox_idle_timeout_seconds: int,
94
+ ) -> None:
95
+ super().__init__(
96
+ template_name=template_name,
97
+ template_type=TemplateType.CODE_INTERPRETER,
98
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
99
+ config=config,
100
+ )
101
+
102
+ @tool()
103
+ def execute_code(
104
+ self,
105
+ code: str,
106
+ language: str = "python",
107
+ timeout: int = 60,
108
+ ) -> Dict[str, Any]:
109
+ "在指定的 Code Interpreter 沙箱中执行代码"
110
+
111
+ def inner(sb: Sandbox):
112
+ assert isinstance(sb, CodeInterpreterSandbox)
113
+ with sb.context.create() as ctx:
114
+ try:
115
+ result = ctx.execute(code=code, timeout=timeout)
116
+ finally:
117
+ try:
118
+ ctx.delete()
119
+ except Exception:
120
+ pass
121
+ return {
122
+ "stdout": result.get("stdout"),
123
+ "stderr": result.get("stderr"),
124
+ "raw": result,
125
+ }
126
+
127
+ return self._run_in_sandbox(inner)
128
+
129
+ @tool()
130
+ def list_directory(self, path: str = "/") -> Dict[str, Any]:
131
+ """列出沙箱中的文件"""
132
+
133
+ def inner(sb: Sandbox):
134
+ assert isinstance(sb, CodeInterpreterSandbox)
135
+ return {
136
+ "path": path,
137
+ "entries": sb.file_system.list(path=path),
138
+ }
139
+
140
+ return self._run_in_sandbox(inner)
141
+
142
+ @tool()
143
+ def read_file(self, path: str) -> Dict[str, Any]:
144
+ """读取沙箱文件内容"""
145
+
146
+ def inner(sb: Sandbox):
147
+ assert isinstance(sb, CodeInterpreterSandbox)
148
+ return {
149
+ "path": path,
150
+ "content": sb.file.read(path=path),
151
+ }
152
+
153
+ return self._run_in_sandbox(inner)
154
+
155
+ @tool(
156
+ name="sandbox_write_file",
157
+ description="向沙箱写入文本文件",
158
+ )
159
+ def write_file(self, path: str, content: str) -> Dict[str, Any]:
160
+
161
+ def inner(sb: Sandbox):
162
+ assert isinstance(sb, CodeInterpreterSandbox)
163
+ return {
164
+ "path": path,
165
+ "result": sb.file.write(path=path, content=content),
166
+ }
167
+
168
+ return self._run_in_sandbox(inner)
169
+
170
+
171
+ class BrowserToolSet(SandboxToolSet):
172
+ """LangChain 浏览器工具适配器。"""
173
+
174
+ def __init__(
175
+ self,
176
+ template_name: str,
177
+ config: Optional[Config],
178
+ sandbox_idle_timeout_seconds: int,
179
+ ) -> None:
180
+
181
+ super().__init__(
182
+ template_name=template_name,
183
+ template_type=TemplateType.BROWSER,
184
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
185
+ config=config,
186
+ )
187
+
188
+ @tool()
189
+ def goto(self, url: str):
190
+ """导航到 URL"""
191
+
192
+ def inner(sb: Sandbox):
193
+ assert isinstance(sb, BrowserSandbox)
194
+ with sb.sync_playwright() as p:
195
+ return p.goto(url)
196
+
197
+ return self._run_in_sandbox(inner)
198
+
199
+ @tool()
200
+ def html_content(
201
+ self,
202
+ ):
203
+ """获取页面 html 内容"""
204
+
205
+ def inner(sb: Sandbox):
206
+ assert isinstance(sb, BrowserSandbox)
207
+ with sb.sync_playwright() as p:
208
+ return p.html_content()
209
+
210
+ return self._run_in_sandbox(inner)
211
+
212
+ @tool()
213
+ def fill(self, selector: str, value: str):
214
+ """在页面中填充输入框"""
215
+
216
+ def inner(sb: Sandbox):
217
+ assert isinstance(sb, BrowserSandbox)
218
+ with sb.sync_playwright() as p:
219
+ return p.fill(selector, value)
220
+
221
+ return self._run_in_sandbox(inner)
222
+
223
+ @tool()
224
+ def click(self, selector: str):
225
+ """
226
+ 在网页上执行点击操作
227
+
228
+ Args:
229
+ selector: 要点击的元素选择器
230
+ """
231
+
232
+ def inner(sb: Sandbox):
233
+ assert isinstance(sb, BrowserSandbox)
234
+ with sb.sync_playwright() as p:
235
+ return p.click(selector)
236
+
237
+ return self._run_in_sandbox(inner)
238
+
239
+ @tool()
240
+ def evaluate(self, expression: str):
241
+ """
242
+ 在网页上执行 js 脚本
243
+
244
+ Args:
245
+ expression: 要执行的脚本
246
+ """
247
+
248
+ def inner(sb: Sandbox):
249
+ assert isinstance(sb, BrowserSandbox)
250
+ with sb.sync_playwright() as p:
251
+ return p.evaluate(expression)
252
+
253
+ return self._run_in_sandbox(inner)
254
+
255
+
256
+ def sandbox_toolset(
257
+ template_name: str,
258
+ *,
259
+ template_type: TemplateType = TemplateType.CODE_INTERPRETER,
260
+ config: Optional[Config] = None,
261
+ sandbox_idle_timeout_seconds: int = 5 * 60,
262
+ ) -> CommonToolSet:
263
+ """将沙箱模板封装为 LangChain ``StructuredTool`` 列表。"""
264
+
265
+ if template_type != TemplateType.CODE_INTERPRETER:
266
+ return BrowserToolSet(
267
+ template_name=template_name,
268
+ config=config,
269
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
270
+ )
271
+ else:
272
+ return CodeInterpreterToolSet(
273
+ template_name=template_name,
274
+ config=config,
275
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
276
+ )
@@ -0,0 +1,47 @@
1
+ """内置工具集集成函数 / Built-in ToolSet Integration Functions
2
+
3
+ 提供快速创建通用工具集对象的便捷函数。
4
+ Provides convenient functions for quickly creating common toolset objects.
5
+ """
6
+
7
+ from typing import Optional, Union
8
+
9
+ from agentrun.integration.utils.tool import CommonToolSet
10
+ from agentrun.toolset import ToolSet, ToolSetClient
11
+ from agentrun.utils.config import Config
12
+
13
+
14
+ def toolset(
15
+ input: Union[str, ToolSet], config: Optional[Config] = None
16
+ ) -> CommonToolSet:
17
+ """将内置工具集封装为通用工具集 / Wrap built-in toolset as CommonToolSet
18
+
19
+ 支持从工具集名称或 ToolSet 实例创建通用工具集。
20
+ Supports creating CommonToolSet from toolset name or ToolSet instance.
21
+
22
+ Args:
23
+ input: 工具集名称或 ToolSet 实例 / Toolset name or ToolSet instance
24
+ config: 配置对象 / Configuration object
25
+
26
+ Returns:
27
+ CommonToolSet: 通用工具集实例 / CommonToolSet instance
28
+
29
+ Examples:
30
+ >>> # 从工具集名称创建 / Create from toolset name
31
+ >>> ts = toolset("my-toolset")
32
+ >>>
33
+ >>> # 从 ToolSet 实例创建 / Create from ToolSet instance
34
+ >>> toolset_obj = ToolSetClient().get("my-toolset")
35
+ >>> ts = toolset(toolset_obj)
36
+ >>>
37
+ >>> # 转换为 LangChain 工具 / Convert to LangChain tools
38
+ >>> lc_tools = ts.to_langchain()
39
+ """
40
+
41
+ toolset = (
42
+ input
43
+ if isinstance(input, ToolSet)
44
+ else ToolSetClient().get(name=input, config=config)
45
+ )
46
+
47
+ return CommonToolSet.from_agentrun_toolset(toolset, config=config)
@@ -0,0 +1,12 @@
1
+ """CrewAI 集成模块。 / CrewAI 集成 Module
2
+
3
+ 提供 AgentRun 模型与沙箱工具的 CrewAI 适配入口。 / 提供 AgentRun 模型with沙箱工具的 CrewAI 适配入口。
4
+ CrewAI 与 LangChain 兼容,因此直接复用 LangChain 的转换逻辑。 / CrewAI with LangChain 兼容,因此直接复用 LangChain 的转换逻辑。
5
+ """
6
+
7
+ from .builtin import model, sandbox_toolset
8
+
9
+ __all__ = [
10
+ "model",
11
+ "sandbox_toolset",
12
+ ]
@@ -0,0 +1,9 @@
1
+ """CrewAI 适配器 / CrewAI Adapters"""
2
+
3
+ from .model_adapter import CrewAIModelAdapter
4
+ from .tool_adapter import CrewAIToolAdapter
5
+
6
+ __all__ = [
7
+ "CrewAIToolAdapter",
8
+ "CrewAIModelAdapter",
9
+ ]
@@ -0,0 +1,65 @@
1
+ """CrewAI 内置集成函数 / CrewAI Built-in Integration Functions
2
+
3
+ 提供快速创建 CrewAI 兼容模型和工具的便捷函数。
4
+ Provides convenient functions for quickly creating CrewAI-compatible models and tools.
5
+ """
6
+
7
+ from typing import Any, Callable, List, Optional, Union
8
+
9
+ from typing_extensions import Unpack
10
+
11
+ from agentrun.integration.builtin import model as _model
12
+ from agentrun.integration.builtin import ModelArgs
13
+ from agentrun.integration.builtin import sandbox_toolset as _sandbox_toolset
14
+ from agentrun.integration.builtin import toolset as _toolset
15
+ from agentrun.integration.utils.tool import Tool
16
+ from agentrun.model import ModelProxy, ModelService
17
+ from agentrun.sandbox import TemplateType
18
+ from agentrun.toolset import ToolSet
19
+ from agentrun.utils.config import Config
20
+
21
+
22
+ def model(
23
+ name: Union[str, ModelProxy, ModelService],
24
+ **kwargs: Unpack[ModelArgs],
25
+ ):
26
+ """获取 AgentRun 模型并转换为 LangChain ``BaseChatModel``。 / CrewAI Built-in Integration Functions"""
27
+
28
+ m = _model(input=name, **kwargs) # type: ignore
29
+ return m.to_crewai()
30
+
31
+
32
+ def toolset(
33
+ name: Union[str, ToolSet],
34
+ *,
35
+ prefix: Optional[str] = None,
36
+ modify_tool_name: Optional[Callable[[Tool], Tool]] = None,
37
+ filter_tools_by_name: Optional[Callable[[str], bool]] = None,
38
+ config: Optional[Config] = None,
39
+ ) -> List[Any]:
40
+ """将内置工具集封装为 LangChain ``StructuredTool`` 列表。 / CrewAI Built-in Integration Functions"""
41
+
42
+ ts = _toolset(input=name, config=config)
43
+ return ts.to_crewai(
44
+ prefix=prefix,
45
+ modify_tool_name=modify_tool_name,
46
+ filter_tools_by_name=filter_tools_by_name,
47
+ )
48
+
49
+
50
+ def sandbox_toolset(
51
+ template_name: str,
52
+ *,
53
+ template_type: TemplateType = TemplateType.CODE_INTERPRETER,
54
+ config: Optional[Config] = None,
55
+ sandbox_idle_timeout_seconds: int = 600,
56
+ prefix: Optional[str] = None,
57
+ ) -> List[Any]:
58
+ """将沙箱模板封装为 LangChain ``StructuredTool`` 列表。 / CrewAI Built-in Integration Functions"""
59
+
60
+ return _sandbox_toolset(
61
+ template_name=template_name,
62
+ template_type=template_type,
63
+ config=config,
64
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
65
+ ).to_crewai(prefix=prefix)
@@ -0,0 +1,27 @@
1
+ """LangChain 模型适配器 / CrewAI Model Adapter
2
+
3
+ 将 CommonModel 包装为 LangChain BaseChatModel。"""
4
+
5
+ from typing import Any
6
+
7
+ from agentrun.integration.utils.adapter import ModelAdapter
8
+
9
+
10
+ class CrewAIModelAdapter(ModelAdapter):
11
+ """CrewAI 模型适配器 / CrewAI Model Adapter
12
+
13
+ 将 CommonModel 包装为 CrewAI BaseChatModel。"""
14
+
15
+ def wrap_model(self, common_model: Any) -> Any:
16
+ """包装 CommonModel 为 CrewAI BaseChatModel / CrewAI Model Adapter"""
17
+ from crewai import LLM
18
+
19
+ info = common_model.get_model_info() # 确保模型可用
20
+ return LLM(
21
+ api_key=info.api_key,
22
+ model=f"{info.provider or 'openai'}/{info.model}",
23
+ base_url=info.base_url,
24
+ default_headers=info.headers,
25
+ stream_options={"include_usage": True},
26
+ # async_client=AsyncClient(headers=info.headers),
27
+ )
@@ -0,0 +1,26 @@
1
+ """LangChain 工具适配器 / CrewAI Tool Adapter
2
+
3
+ 将标准工具定义转换为 LangChain StructuredTool 格式。"""
4
+
5
+ from typing import Any, List
6
+
7
+ from agentrun.integration.utils.adapter import ToolAdapter
8
+ from agentrun.integration.utils.canonical import CanonicalTool
9
+
10
+
11
+ class CrewAIToolAdapter(ToolAdapter):
12
+ """CrewAI 工具适配器 / CrewAI Tool Adapter
13
+
14
+ 实现 CanonicalTool → CrewAI StructuredTool 的转换。"""
15
+
16
+ def from_canonical(self, tools: List[CanonicalTool]) -> Any:
17
+ """将标准格式转换为 CrewAI StructuredTool / CrewAI Tool Adapter"""
18
+ try:
19
+ from crewai.tools import tool
20
+ except ImportError as e:
21
+ raise ImportError(
22
+ "LangChain is not installed. "
23
+ "Install it with: pip install langchain-core"
24
+ ) from e
25
+
26
+ return [tool(t) for t in self.function_tools(tools)]
@@ -0,0 +1,12 @@
1
+ """Google ADK 集成模块。 / Google ADK 集成 Module
2
+
3
+ 提供与 Google Agent Development Kit 的模型与沙箱工具集成。 / 提供with Google Agent Development Kit 的模型with沙箱工具集成。
4
+ """
5
+
6
+ from .builtin import model, sandbox_toolset, toolset
7
+
8
+ __all__ = [
9
+ "model",
10
+ "toolset",
11
+ "sandbox_toolset",
12
+ ]
@@ -0,0 +1,15 @@
1
+ """Google ADK 适配器 / Google ADK Adapters
2
+
3
+ 提供 Google ADK 框架的消息、工具和模型适配器。"""
4
+
5
+ from agentrun.integration.google_adk.message_adapter import (
6
+ GoogleADKMessageAdapter,
7
+ )
8
+ from agentrun.integration.google_adk.model_adapter import GoogleADKModelAdapter
9
+ from agentrun.integration.google_adk.tool_adapter import GoogleADKToolAdapter
10
+
11
+ __all__ = [
12
+ "GoogleADKMessageAdapter",
13
+ "GoogleADKToolAdapter",
14
+ "GoogleADKModelAdapter",
15
+ ]
@@ -0,0 +1,65 @@
1
+ """Google ADK 内置集成函数 / Google ADK Built-in Integration Functions
2
+
3
+ 提供快速创建 Google ADK 兼容模型和工具的便捷函数。
4
+ Provides convenient functions for quickly creating Google ADK-compatible models and tools.
5
+ """
6
+
7
+ from typing import Any, Callable, List, Optional, Union
8
+
9
+ from typing_extensions import Unpack
10
+
11
+ from agentrun.integration.builtin import model as _model
12
+ from agentrun.integration.builtin import ModelArgs
13
+ from agentrun.integration.builtin import sandbox_toolset as _sandbox_toolset
14
+ from agentrun.integration.builtin import toolset as _toolset
15
+ from agentrun.integration.utils.tool import Tool
16
+ from agentrun.model import ModelProxy, ModelService
17
+ from agentrun.sandbox import TemplateType
18
+ from agentrun.toolset import ToolSet
19
+ from agentrun.utils.config import Config
20
+
21
+
22
+ def model(
23
+ name: Union[str, ModelProxy, ModelService],
24
+ **kwargs: Unpack[ModelArgs],
25
+ ):
26
+ """获取 AgentRun 模型并转换为 LangChain ``BaseChatModel``。 / Google ADK Built-in Integration Functions"""
27
+
28
+ m = _model(input=name, **kwargs) # type: ignore
29
+ return m.to_google_adk()
30
+
31
+
32
+ def toolset(
33
+ name: Union[str, ToolSet],
34
+ *,
35
+ prefix: Optional[str] = None,
36
+ modify_tool_name: Optional[Callable[[Tool], Tool]] = None,
37
+ filter_tools_by_name: Optional[Callable[[str], bool]] = None,
38
+ config: Optional[Config] = None,
39
+ ) -> List[Any]:
40
+ """将内置工具集封装为 LangChain ``StructuredTool`` 列表。 / Google ADK Built-in Integration Functions"""
41
+
42
+ ts = _toolset(input=name, config=config)
43
+ return ts.to_google_adk(
44
+ prefix=prefix,
45
+ modify_tool_name=modify_tool_name,
46
+ filter_tools_by_name=filter_tools_by_name,
47
+ )
48
+
49
+
50
+ def sandbox_toolset(
51
+ template_name: str,
52
+ *,
53
+ template_type: TemplateType = TemplateType.CODE_INTERPRETER,
54
+ config: Optional[Config] = None,
55
+ sandbox_idle_timeout_seconds: int = 600,
56
+ prefix: Optional[str] = None,
57
+ ) -> List[Any]:
58
+ """将沙箱模板封装为 LangChain ``StructuredTool`` 列表。 / Google ADK Built-in Integration Functions"""
59
+
60
+ return _sandbox_toolset(
61
+ template_name=template_name,
62
+ template_type=template_type,
63
+ config=config,
64
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
65
+ ).to_google_adk(prefix=prefix)