agentrun-inner-test 0.0.46__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 (135) hide show
  1. agentrun/__init__.py +325 -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 +93 -0
  31. agentrun/integration/builtin/sandbox.py +1234 -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 +31 -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 +46 -0
  43. agentrun/integration/google_adk/tool_adapter.py +235 -0
  44. agentrun/integration/langchain/__init__.py +30 -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 +35 -0
  51. agentrun/integration/langgraph/adapter.py +20 -0
  52. agentrun/integration/langgraph/agent_converter.py +1073 -0
  53. agentrun/integration/langgraph/builtin.py +65 -0
  54. agentrun/integration/pydantic_ai/__init__.py +12 -0
  55. agentrun/integration/pydantic_ai/adapter.py +13 -0
  56. agentrun/integration/pydantic_ai/builtin.py +65 -0
  57. agentrun/integration/pydantic_ai/model_adapter.py +44 -0
  58. agentrun/integration/pydantic_ai/tool_adapter.py +19 -0
  59. agentrun/integration/utils/__init__.py +112 -0
  60. agentrun/integration/utils/adapter.py +560 -0
  61. agentrun/integration/utils/canonical.py +164 -0
  62. agentrun/integration/utils/converter.py +134 -0
  63. agentrun/integration/utils/model.py +110 -0
  64. agentrun/integration/utils/tool.py +1759 -0
  65. agentrun/model/__client_async_template.py +357 -0
  66. agentrun/model/__init__.py +57 -0
  67. agentrun/model/__model_proxy_async_template.py +270 -0
  68. agentrun/model/__model_service_async_template.py +267 -0
  69. agentrun/model/api/__init__.py +6 -0
  70. agentrun/model/api/control.py +1173 -0
  71. agentrun/model/api/data.py +196 -0
  72. agentrun/model/client.py +674 -0
  73. agentrun/model/model.py +235 -0
  74. agentrun/model/model_proxy.py +439 -0
  75. agentrun/model/model_service.py +438 -0
  76. agentrun/sandbox/__aio_sandbox_async_template.py +523 -0
  77. agentrun/sandbox/__browser_sandbox_async_template.py +110 -0
  78. agentrun/sandbox/__client_async_template.py +491 -0
  79. agentrun/sandbox/__code_interpreter_sandbox_async_template.py +463 -0
  80. agentrun/sandbox/__init__.py +69 -0
  81. agentrun/sandbox/__sandbox_async_template.py +463 -0
  82. agentrun/sandbox/__template_async_template.py +152 -0
  83. agentrun/sandbox/aio_sandbox.py +905 -0
  84. agentrun/sandbox/api/__aio_data_async_template.py +335 -0
  85. agentrun/sandbox/api/__browser_data_async_template.py +140 -0
  86. agentrun/sandbox/api/__code_interpreter_data_async_template.py +206 -0
  87. agentrun/sandbox/api/__init__.py +19 -0
  88. agentrun/sandbox/api/__sandbox_data_async_template.py +107 -0
  89. agentrun/sandbox/api/aio_data.py +551 -0
  90. agentrun/sandbox/api/browser_data.py +172 -0
  91. agentrun/sandbox/api/code_interpreter_data.py +396 -0
  92. agentrun/sandbox/api/control.py +1051 -0
  93. agentrun/sandbox/api/playwright_async.py +492 -0
  94. agentrun/sandbox/api/playwright_sync.py +492 -0
  95. agentrun/sandbox/api/sandbox_data.py +154 -0
  96. agentrun/sandbox/browser_sandbox.py +185 -0
  97. agentrun/sandbox/client.py +925 -0
  98. agentrun/sandbox/code_interpreter_sandbox.py +823 -0
  99. agentrun/sandbox/model.py +397 -0
  100. agentrun/sandbox/sandbox.py +848 -0
  101. agentrun/sandbox/template.py +217 -0
  102. agentrun/server/__init__.py +191 -0
  103. agentrun/server/agui_normalizer.py +180 -0
  104. agentrun/server/agui_protocol.py +797 -0
  105. agentrun/server/invoker.py +309 -0
  106. agentrun/server/model.py +427 -0
  107. agentrun/server/openai_protocol.py +535 -0
  108. agentrun/server/protocol.py +140 -0
  109. agentrun/server/server.py +208 -0
  110. agentrun/toolset/__client_async_template.py +62 -0
  111. agentrun/toolset/__init__.py +51 -0
  112. agentrun/toolset/__toolset_async_template.py +204 -0
  113. agentrun/toolset/api/__init__.py +17 -0
  114. agentrun/toolset/api/control.py +262 -0
  115. agentrun/toolset/api/mcp.py +100 -0
  116. agentrun/toolset/api/openapi.py +1251 -0
  117. agentrun/toolset/client.py +102 -0
  118. agentrun/toolset/model.py +321 -0
  119. agentrun/toolset/toolset.py +270 -0
  120. agentrun/utils/__data_api_async_template.py +720 -0
  121. agentrun/utils/__init__.py +5 -0
  122. agentrun/utils/__resource_async_template.py +158 -0
  123. agentrun/utils/config.py +258 -0
  124. agentrun/utils/control_api.py +78 -0
  125. agentrun/utils/data_api.py +1120 -0
  126. agentrun/utils/exception.py +151 -0
  127. agentrun/utils/helper.py +108 -0
  128. agentrun/utils/log.py +77 -0
  129. agentrun/utils/model.py +168 -0
  130. agentrun/utils/resource.py +291 -0
  131. agentrun_inner_test-0.0.46.dist-info/METADATA +263 -0
  132. agentrun_inner_test-0.0.46.dist-info/RECORD +135 -0
  133. agentrun_inner_test-0.0.46.dist-info/WHEEL +5 -0
  134. agentrun_inner_test-0.0.46.dist-info/licenses/LICENSE +201 -0
  135. agentrun_inner_test-0.0.46.dist-info/top_level.txt +1 -0
@@ -0,0 +1,848 @@
1
+ """
2
+ This file is auto generated by the code generation script.
3
+ Do not modify this file manually.
4
+ Use the `make codegen` command to regenerate.
5
+
6
+ 当前文件为自动生成的控制 API 客户端代码。请勿手动修改此文件。
7
+ 使用 `make codegen` 命令重新生成。
8
+
9
+ source: agentrun/sandbox/__sandbox_async_template.py
10
+
11
+ Sandbox 高层 API / Sandbox High-Level API
12
+
13
+ 此模块定义沙箱资源的高级API。
14
+ This module defines the high-level API for sandbox resources.
15
+ """
16
+
17
+ from typing import (
18
+ Any,
19
+ Dict,
20
+ List,
21
+ Literal,
22
+ Optional,
23
+ overload,
24
+ TYPE_CHECKING,
25
+ Union,
26
+ )
27
+
28
+ from agentrun.sandbox.model import TemplateType
29
+ from agentrun.utils.config import Config
30
+ from agentrun.utils.model import BaseModel
31
+
32
+ if TYPE_CHECKING:
33
+ from agentrun.sandbox.aio_sandbox import AioSandbox
34
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
35
+ from agentrun.sandbox.code_interpreter_sandbox import CodeInterpreterSandbox
36
+ from agentrun.sandbox.model import (
37
+ ListSandboxesInput,
38
+ ListSandboxesOutput,
39
+ NASConfig,
40
+ OSSMountConfig,
41
+ PageableInput,
42
+ PolarFsConfig,
43
+ TemplateInput,
44
+ )
45
+ from agentrun.sandbox.template import Template
46
+
47
+
48
+ class Sandbox(BaseModel):
49
+ """Sandbox 实例
50
+
51
+ 封装了 Sandbox 的基本信息和操作方法
52
+ """
53
+
54
+ _template_type: Optional[TemplateType]
55
+
56
+ created_at: Optional[str] = None
57
+ """沙箱创建时间 / Sandbox creation time"""
58
+ ended_at: Optional[str] = None
59
+ """沙箱结束时间 / Sandbox end time"""
60
+ last_updated_at: Optional[str] = None
61
+ """最后更新时间 / Last updated time"""
62
+ metadata: Optional[Dict[str, Any]] = None
63
+ """元数据 / Metadata"""
64
+ sandbox_arn: Optional[str] = None
65
+ """沙箱全局唯一资源名称 / Sandbox ARN"""
66
+ sandbox_id: Optional[str] = None
67
+ """沙箱 ID / Sandbox ID"""
68
+ sandbox_idle_ttlin_seconds: Optional[int] = None
69
+ """沙箱空闲 TTL(秒) / Sandbox Idle TTL (seconds)"""
70
+ sandbox_idle_timeout_seconds: Optional[int] = None
71
+ """沙箱空闲超时时间(秒) / Sandbox Idle Timeout (seconds)"""
72
+ status: Optional[str] = None
73
+ """沙箱状态 / Sandbox status"""
74
+ template_id: Optional[str] = None
75
+ """模板 ID / Template ID"""
76
+ template_name: Optional[str] = None
77
+ """模板名称 / Template name"""
78
+ _config: Optional[Config] = None
79
+ """配置对象,用于子类的 data_api 初始化 / Config object for data_api initialization"""
80
+
81
+ @classmethod
82
+ def __get_client(cls):
83
+ """获取 Sandbox 客户端"""
84
+ from .client import SandboxClient
85
+
86
+ return SandboxClient()
87
+
88
+ @classmethod
89
+ @overload
90
+ async def create_async(
91
+ cls,
92
+ template_type: Literal[TemplateType.CODE_INTERPRETER],
93
+ template_name: Optional[str] = None,
94
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
95
+ nas_config: Optional["NASConfig"] = None,
96
+ oss_mount_config: Optional["OSSMountConfig"] = None,
97
+ polar_fs_config: Optional["PolarFsConfig"] = None,
98
+ config: Optional[Config] = None,
99
+ ) -> "CodeInterpreterSandbox":
100
+ ...
101
+
102
+ @classmethod
103
+ @overload
104
+ def create(
105
+ cls,
106
+ template_type: Literal[TemplateType.CODE_INTERPRETER],
107
+ template_name: Optional[str] = None,
108
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
109
+ nas_config: Optional["NASConfig"] = None,
110
+ oss_mount_config: Optional["OSSMountConfig"] = None,
111
+ polar_fs_config: Optional["PolarFsConfig"] = None,
112
+ config: Optional[Config] = None,
113
+ ) -> "CodeInterpreterSandbox":
114
+ ...
115
+
116
+ @classmethod
117
+ @overload
118
+ async def create_async(
119
+ cls,
120
+ template_type: Literal[TemplateType.BROWSER],
121
+ template_name: Optional[str] = None,
122
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
123
+ nas_config: Optional["NASConfig"] = None,
124
+ oss_mount_config: Optional["OSSMountConfig"] = None,
125
+ polar_fs_config: Optional["PolarFsConfig"] = None,
126
+ config: Optional[Config] = None,
127
+ ) -> "BrowserSandbox":
128
+ ...
129
+
130
+ @classmethod
131
+ @overload
132
+ def create(
133
+ cls,
134
+ template_type: Literal[TemplateType.BROWSER],
135
+ template_name: Optional[str] = None,
136
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
137
+ nas_config: Optional["NASConfig"] = None,
138
+ oss_mount_config: Optional["OSSMountConfig"] = None,
139
+ polar_fs_config: Optional["PolarFsConfig"] = None,
140
+ config: Optional[Config] = None,
141
+ ) -> "BrowserSandbox":
142
+ ...
143
+
144
+ @classmethod
145
+ @overload
146
+ async def create_async(
147
+ cls,
148
+ template_type: Literal[TemplateType.AIO],
149
+ template_name: Optional[str] = None,
150
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
151
+ nas_config: Optional["NASConfig"] = None,
152
+ oss_mount_config: Optional["OSSMountConfig"] = None,
153
+ polar_fs_config: Optional["PolarFsConfig"] = None,
154
+ config: Optional[Config] = None,
155
+ ) -> "AioSandbox":
156
+ ...
157
+
158
+ @classmethod
159
+ @overload
160
+ def create(
161
+ cls,
162
+ template_type: Literal[TemplateType.AIO],
163
+ template_name: Optional[str] = None,
164
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
165
+ nas_config: Optional["NASConfig"] = None,
166
+ oss_mount_config: Optional["OSSMountConfig"] = None,
167
+ polar_fs_config: Optional["PolarFsConfig"] = None,
168
+ config: Optional[Config] = None,
169
+ ) -> "AioSandbox":
170
+ ...
171
+
172
+ @classmethod
173
+ async def create_async(
174
+ cls,
175
+ template_type: TemplateType,
176
+ template_name: Optional[str] = None,
177
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
178
+ nas_config: Optional["NASConfig"] = None,
179
+ oss_mount_config: Optional["OSSMountConfig"] = None,
180
+ polar_fs_config: Optional["PolarFsConfig"] = None,
181
+ config: Optional[Config] = None,
182
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
183
+
184
+ if template_name is None:
185
+ # todo 可以考虑为用户创建一个模板?
186
+ raise ValueError("template_name is required")
187
+
188
+ # 先根据传入的template_name,获取template的类型
189
+ template = await cls.get_template_async(template_name, config=config)
190
+
191
+ # 根据 template 类型创建相应的 Sandbox 子类
192
+ from agentrun.sandbox.aio_sandbox import AioSandbox
193
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
194
+ from agentrun.sandbox.code_interpreter_sandbox import (
195
+ CodeInterpreterSandbox,
196
+ )
197
+
198
+ if template_type != template.template_type:
199
+ raise ValueError(
200
+ f"template_type of {template_name} is {template.template_type},"
201
+ f" not {template_type}"
202
+ )
203
+
204
+ # 创建 Sandbox(返回基类实例)
205
+ base_sandbox = await cls.__get_client().create_sandbox_async(
206
+ template_name=template_name,
207
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
208
+ nas_config=nas_config,
209
+ oss_mount_config=oss_mount_config,
210
+ polar_fs_config=polar_fs_config,
211
+ )
212
+
213
+ # 根据 template 类型转换为对应的子类实例
214
+ sandbox = None
215
+ if template.template_type == TemplateType.CODE_INTERPRETER:
216
+ sandbox = CodeInterpreterSandbox.model_validate(
217
+ base_sandbox.model_dump(by_alias=False)
218
+ )
219
+ elif template.template_type == TemplateType.BROWSER:
220
+ sandbox = BrowserSandbox.model_validate(
221
+ base_sandbox.model_dump(by_alias=False)
222
+ )
223
+ elif template.template_type == TemplateType.AIO:
224
+ sandbox = AioSandbox.model_validate(
225
+ base_sandbox.model_dump(by_alias=False)
226
+ )
227
+ else:
228
+ raise ValueError(
229
+ f"template_type {template.template_type} is not supported"
230
+ )
231
+
232
+ sandbox._config = config
233
+ return sandbox
234
+
235
+ @classmethod
236
+ def create(
237
+ cls,
238
+ template_type: TemplateType,
239
+ template_name: Optional[str] = None,
240
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
241
+ nas_config: Optional["NASConfig"] = None,
242
+ oss_mount_config: Optional["OSSMountConfig"] = None,
243
+ polar_fs_config: Optional["PolarFsConfig"] = None,
244
+ config: Optional[Config] = None,
245
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
246
+
247
+ if template_name is None:
248
+ # todo 可以考虑为用户创建一个模板?
249
+ raise ValueError("template_name is required")
250
+
251
+ # 先根据传入的template_name,获取template的类型
252
+ template = cls.get_template(template_name, config=config)
253
+
254
+ # 根据 template 类型创建相应的 Sandbox 子类
255
+ from agentrun.sandbox.aio_sandbox import AioSandbox
256
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
257
+ from agentrun.sandbox.code_interpreter_sandbox import (
258
+ CodeInterpreterSandbox,
259
+ )
260
+
261
+ if template_type != template.template_type:
262
+ raise ValueError(
263
+ f"template_type of {template_name} is {template.template_type},"
264
+ f" not {template_type}"
265
+ )
266
+
267
+ # 创建 Sandbox(返回基类实例)
268
+ base_sandbox = cls.__get_client().create_sandbox(
269
+ template_name=template_name,
270
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
271
+ nas_config=nas_config,
272
+ oss_mount_config=oss_mount_config,
273
+ polar_fs_config=polar_fs_config,
274
+ )
275
+
276
+ # 根据 template 类型转换为对应的子类实例
277
+ sandbox = None
278
+ if template.template_type == TemplateType.CODE_INTERPRETER:
279
+ sandbox = CodeInterpreterSandbox.model_validate(
280
+ base_sandbox.model_dump(by_alias=False)
281
+ )
282
+ elif template.template_type == TemplateType.BROWSER:
283
+ sandbox = BrowserSandbox.model_validate(
284
+ base_sandbox.model_dump(by_alias=False)
285
+ )
286
+ elif template.template_type == TemplateType.AIO:
287
+ sandbox = AioSandbox.model_validate(
288
+ base_sandbox.model_dump(by_alias=False)
289
+ )
290
+ else:
291
+ raise ValueError(
292
+ f"template_type {template.template_type} is not supported"
293
+ )
294
+
295
+ sandbox._config = config
296
+ return sandbox
297
+
298
+ @classmethod
299
+ async def stop_by_id_async(cls, sandbox_id: str):
300
+ """通过 ID 停止 Sandbox(异步)
301
+
302
+ Args:
303
+ sandbox_id: Sandbox ID
304
+ config: 配置对象
305
+
306
+ Returns:
307
+ Sandbox: Sandbox 对象
308
+ """
309
+ if sandbox_id is None:
310
+ raise ValueError("sandbox_id is required")
311
+ # todo 后续适配后使用 stop()
312
+ return await cls.__get_client().delete_sandbox_async(sandbox_id)
313
+
314
+ @classmethod
315
+ def stop_by_id(cls, sandbox_id: str):
316
+ """通过 ID 停止 Sandbox(同步)
317
+
318
+ Args:
319
+ sandbox_id: Sandbox ID
320
+ config: 配置对象
321
+
322
+ Returns:
323
+ Sandbox: Sandbox 对象
324
+ """
325
+ if sandbox_id is None:
326
+ raise ValueError("sandbox_id is required")
327
+ # todo 后续适配后使用 stop()
328
+ return cls.__get_client().delete_sandbox(sandbox_id)
329
+
330
+ @classmethod
331
+ async def delete_by_id_async(cls, sandbox_id: str):
332
+ """通过 ID 删除 Sandbox(异步)
333
+
334
+ Args:
335
+ sandbox_id: Sandbox ID
336
+ config: 配置对象
337
+
338
+ Returns:
339
+ Sandbox: Sandbox 对象
340
+ """
341
+ if sandbox_id is None:
342
+ raise ValueError("sandbox_id is required")
343
+ return await cls.__get_client().delete_sandbox_async(sandbox_id)
344
+
345
+ @classmethod
346
+ def delete_by_id(cls, sandbox_id: str):
347
+ """通过 ID 删除 Sandbox(同步)
348
+
349
+ Args:
350
+ sandbox_id: Sandbox ID
351
+ config: 配置对象
352
+
353
+ Returns:
354
+ Sandbox: Sandbox 对象
355
+ """
356
+ if sandbox_id is None:
357
+ raise ValueError("sandbox_id is required")
358
+ return cls.__get_client().delete_sandbox(sandbox_id)
359
+
360
+ @classmethod
361
+ async def list_async(
362
+ cls,
363
+ input: Optional["ListSandboxesInput"] = None,
364
+ config: Optional[Config] = None,
365
+ ) -> "ListSandboxesOutput":
366
+ """列出 Sandboxes(异步)
367
+
368
+ Args:
369
+ input: 列表查询配置
370
+ config: 配置对象
371
+
372
+ Returns:
373
+ ListSandboxesOutput: Sandbox 列表结果
374
+ """
375
+ return await cls.__get_client().list_sandboxes_async(input, config)
376
+
377
+ @classmethod
378
+ def list(
379
+ cls,
380
+ input: Optional["ListSandboxesInput"] = None,
381
+ config: Optional[Config] = None,
382
+ ) -> "ListSandboxesOutput":
383
+ """列出 Sandboxes(同步)
384
+
385
+ Args:
386
+ input: 列表查询配置
387
+ config: 配置对象
388
+
389
+ Returns:
390
+ ListSandboxesOutput: Sandbox 列表结果
391
+ """
392
+ return cls.__get_client().list_sandboxes(input, config)
393
+
394
+ @classmethod
395
+ @overload
396
+ async def connect_async(
397
+ cls,
398
+ sandbox_id: str,
399
+ template_type: Literal[TemplateType.CODE_INTERPRETER],
400
+ config: Optional[Config] = None,
401
+ ) -> "CodeInterpreterSandbox":
402
+ ...
403
+
404
+ @classmethod
405
+ @overload
406
+ def connect(
407
+ cls,
408
+ sandbox_id: str,
409
+ template_type: Literal[TemplateType.CODE_INTERPRETER],
410
+ config: Optional[Config] = None,
411
+ ) -> "CodeInterpreterSandbox":
412
+ ...
413
+
414
+ @classmethod
415
+ @overload
416
+ async def connect_async(
417
+ cls,
418
+ sandbox_id: str,
419
+ template_type: Literal[TemplateType.BROWSER],
420
+ config: Optional[Config] = None,
421
+ ) -> "BrowserSandbox":
422
+ ...
423
+
424
+ @classmethod
425
+ @overload
426
+ def connect(
427
+ cls,
428
+ sandbox_id: str,
429
+ template_type: Literal[TemplateType.BROWSER],
430
+ config: Optional[Config] = None,
431
+ ) -> "BrowserSandbox":
432
+ ...
433
+
434
+ @classmethod
435
+ @overload
436
+ async def connect_async(
437
+ cls,
438
+ sandbox_id: str,
439
+ template_type: Literal[TemplateType.AIO],
440
+ config: Optional[Config] = None,
441
+ ) -> "AioSandbox":
442
+ ...
443
+
444
+ @classmethod
445
+ @overload
446
+ def connect(
447
+ cls,
448
+ sandbox_id: str,
449
+ template_type: Literal[TemplateType.AIO],
450
+ config: Optional[Config] = None,
451
+ ) -> "AioSandbox":
452
+ ...
453
+
454
+ @classmethod
455
+ @overload
456
+ async def connect_async(
457
+ cls,
458
+ sandbox_id: str,
459
+ template_type: None = None,
460
+ config: Optional[Config] = None,
461
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
462
+ ...
463
+
464
+ @classmethod
465
+ @overload
466
+ def connect(
467
+ cls,
468
+ sandbox_id: str,
469
+ template_type: None = None,
470
+ config: Optional[Config] = None,
471
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
472
+ ...
473
+
474
+ @classmethod
475
+ async def connect_async(
476
+ cls,
477
+ sandbox_id: str,
478
+ template_type: Optional[TemplateType] = None,
479
+ config: Optional[Config] = None,
480
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
481
+ """连接一个SandBox(异步)
482
+
483
+ Args:
484
+ sandbox_id: Sandbox ID
485
+ type: 可选的类型参数,用于类型提示和运行时验证
486
+ config: 配置对象
487
+
488
+ Returns:
489
+ Sandbox: 根据模板类型返回对应的 Sandbox 子类对象
490
+
491
+ Raises:
492
+ ValueError: 如果模板类型不支持或与预期类型不匹配
493
+ """
494
+ if sandbox_id is None:
495
+ raise ValueError("sandbox_id is required")
496
+
497
+ # 先获取 sandbox 信息
498
+ sandbox = await cls.__get_client().get_sandbox_async(
499
+ sandbox_id, config=config
500
+ )
501
+
502
+ # 根据 template_name 获取 template 类型
503
+ if sandbox.template_name is None:
504
+ raise ValueError(f"Sandbox {sandbox_id} has no template_name")
505
+
506
+ template = await cls.get_template_async(
507
+ sandbox.template_name, config=config
508
+ )
509
+
510
+ # 如果提供了 type 参数,验证类型是否匹配
511
+ if (
512
+ template_type is not None
513
+ and template.template_type != template_type
514
+ ):
515
+ raise ValueError(
516
+ f"Sandbox {sandbox_id} has template type"
517
+ f" {template.template_type}, but expected {template_type}"
518
+ )
519
+
520
+ # 根据 template 类型创建相应的 Sandbox 子类
521
+ from agentrun.sandbox.aio_sandbox import AioSandbox
522
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
523
+ from agentrun.sandbox.code_interpreter_sandbox import (
524
+ CodeInterpreterSandbox,
525
+ )
526
+
527
+ result = None
528
+ if template.template_type == TemplateType.CODE_INTERPRETER:
529
+ result = CodeInterpreterSandbox.model_validate(
530
+ sandbox.model_dump(by_alias=False)
531
+ )
532
+ elif template.template_type == TemplateType.BROWSER:
533
+ result = BrowserSandbox.model_validate(
534
+ sandbox.model_dump(by_alias=False)
535
+ )
536
+ elif template.template_type == TemplateType.AIO:
537
+ result = AioSandbox.model_validate(
538
+ sandbox.model_dump(by_alias=False)
539
+ )
540
+ else:
541
+ raise ValueError(
542
+ f"Unsupported template type: {template.template_type}. "
543
+ "Expected 'code-interpreter', 'browser' or 'aio'"
544
+ )
545
+
546
+ result._config = config
547
+ return result
548
+
549
+ # ==================== Template 相关类方法 ====================
550
+
551
+ @classmethod
552
+ def connect(
553
+ cls,
554
+ sandbox_id: str,
555
+ template_type: Optional[TemplateType] = None,
556
+ config: Optional[Config] = None,
557
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
558
+ """连接一个SandBox(同步)
559
+
560
+ Args:
561
+ sandbox_id: Sandbox ID
562
+ type: 可选的类型参数,用于类型提示和运行时验证
563
+ config: 配置对象
564
+
565
+ Returns:
566
+ Sandbox: 根据模板类型返回对应的 Sandbox 子类对象
567
+
568
+ Raises:
569
+ ValueError: 如果模板类型不支持或与预期类型不匹配
570
+ """
571
+ if sandbox_id is None:
572
+ raise ValueError("sandbox_id is required")
573
+
574
+ # 先获取 sandbox 信息
575
+ sandbox = cls.__get_client().get_sandbox(sandbox_id, config=config)
576
+
577
+ # 根据 template_name 获取 template 类型
578
+ if sandbox.template_name is None:
579
+ raise ValueError(f"Sandbox {sandbox_id} has no template_name")
580
+
581
+ template = cls.get_template(sandbox.template_name, config=config)
582
+
583
+ # 如果提供了 type 参数,验证类型是否匹配
584
+ if (
585
+ template_type is not None
586
+ and template.template_type != template_type
587
+ ):
588
+ raise ValueError(
589
+ f"Sandbox {sandbox_id} has template type"
590
+ f" {template.template_type}, but expected {template_type}"
591
+ )
592
+
593
+ # 根据 template 类型创建相应的 Sandbox 子类
594
+ from agentrun.sandbox.aio_sandbox import AioSandbox
595
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
596
+ from agentrun.sandbox.code_interpreter_sandbox import (
597
+ CodeInterpreterSandbox,
598
+ )
599
+
600
+ result = None
601
+ if template.template_type == TemplateType.CODE_INTERPRETER:
602
+ result = CodeInterpreterSandbox.model_validate(
603
+ sandbox.model_dump(by_alias=False)
604
+ )
605
+ elif template.template_type == TemplateType.BROWSER:
606
+ result = BrowserSandbox.model_validate(
607
+ sandbox.model_dump(by_alias=False)
608
+ )
609
+ elif template.template_type == TemplateType.AIO:
610
+ result = AioSandbox.model_validate(
611
+ sandbox.model_dump(by_alias=False)
612
+ )
613
+ else:
614
+ raise ValueError(
615
+ f"Unsupported template type: {template.template_type}. "
616
+ "Expected 'code-interpreter', 'browser' or 'aio'"
617
+ )
618
+
619
+ result._config = config
620
+ return result
621
+
622
+ # ==================== Template 相关类方法 ====================
623
+
624
+ @classmethod
625
+ async def create_template_async(
626
+ cls, input: "TemplateInput", config: Optional[Config] = None
627
+ ) -> "Template":
628
+ """创建 Template(异步)
629
+
630
+ Args:
631
+ input: Template 配置
632
+ config: 配置对象
633
+
634
+ Returns:
635
+ Template: 创建的 Template 对象
636
+ """
637
+ if input.template_type is None:
638
+ raise ValueError("template_type is required")
639
+ return await cls.__get_client().create_template_async(
640
+ input, config=config
641
+ )
642
+
643
+ @classmethod
644
+ def create_template(
645
+ cls, input: "TemplateInput", config: Optional[Config] = None
646
+ ) -> "Template":
647
+ """创建 Template(同步)
648
+
649
+ Args:
650
+ input: Template 配置
651
+ config: 配置对象
652
+
653
+ Returns:
654
+ Template: 创建的 Template 对象
655
+ """
656
+ if input.template_type is None:
657
+ raise ValueError("template_type is required")
658
+ return cls.__get_client().create_template(input, config=config)
659
+
660
+ @classmethod
661
+ async def get_template_async(
662
+ cls, template_name: str, config: Optional[Config] = None
663
+ ) -> "Template":
664
+ """获取 Template(异步)
665
+
666
+ Args:
667
+ template_name: Template 名称
668
+ config: 配置对象
669
+
670
+ Returns:
671
+ Template: Template 对象
672
+ """
673
+ if template_name is None:
674
+ raise ValueError("template_name is required")
675
+ return await cls.__get_client().get_template_async(
676
+ template_name, config=config
677
+ )
678
+
679
+ @classmethod
680
+ def get_template(
681
+ cls, template_name: str, config: Optional[Config] = None
682
+ ) -> "Template":
683
+ """获取 Template(同步)
684
+
685
+ Args:
686
+ template_name: Template 名称
687
+ config: 配置对象
688
+
689
+ Returns:
690
+ Template: Template 对象
691
+ """
692
+ if template_name is None:
693
+ raise ValueError("template_name is required")
694
+ return cls.__get_client().get_template(template_name, config=config)
695
+
696
+ @classmethod
697
+ async def update_template_async(
698
+ cls,
699
+ template_name: str,
700
+ input: "TemplateInput",
701
+ config: Optional[Config] = None,
702
+ ) -> "Template":
703
+ """更新 Template(异步)
704
+
705
+ Args:
706
+ template_name: Template 名称
707
+ input: Template 更新配置
708
+ config: 配置对象
709
+
710
+ Returns:
711
+ Template: 更新后的 Template 对象
712
+ """
713
+ if template_name is None:
714
+ raise ValueError("template_name is required")
715
+ return await cls.__get_client().update_template_async(
716
+ template_name, input, config=config
717
+ )
718
+
719
+ @classmethod
720
+ def update_template(
721
+ cls,
722
+ template_name: str,
723
+ input: "TemplateInput",
724
+ config: Optional[Config] = None,
725
+ ) -> "Template":
726
+ """更新 Template(同步)
727
+
728
+ Args:
729
+ template_name: Template 名称
730
+ input: Template 更新配置
731
+ config: 配置对象
732
+
733
+ Returns:
734
+ Template: 更新后的 Template 对象
735
+ """
736
+ if template_name is None:
737
+ raise ValueError("template_name is required")
738
+ return cls.__get_client().update_template(
739
+ template_name, input, config=config
740
+ )
741
+
742
+ @classmethod
743
+ async def delete_template_async(
744
+ cls, template_name: str, config: Optional[Config] = None
745
+ ) -> "Template":
746
+ """删除 Template(异步)
747
+
748
+ Args:
749
+ template_name: Template 名称
750
+ config: 配置对象
751
+
752
+ Returns:
753
+ Template: 删除的 Template 对象
754
+ """
755
+ if template_name is None:
756
+ raise ValueError("template_name is required")
757
+ return await cls.__get_client().delete_template_async(
758
+ template_name, config=config
759
+ )
760
+
761
+ @classmethod
762
+ def delete_template(
763
+ cls, template_name: str, config: Optional[Config] = None
764
+ ) -> "Template":
765
+ """删除 Template(同步)
766
+
767
+ Args:
768
+ template_name: Template 名称
769
+ config: 配置对象
770
+
771
+ Returns:
772
+ Template: 删除的 Template 对象
773
+ """
774
+ if template_name is None:
775
+ raise ValueError("template_name is required")
776
+ return cls.__get_client().delete_template(template_name, config=config)
777
+
778
+ @classmethod
779
+ async def list_templates_async(
780
+ cls,
781
+ input: Optional["PageableInput"] = None,
782
+ config: Optional[Config] = None,
783
+ ) -> List["Template"]:
784
+ """列出 Templates(异步)
785
+
786
+ Args:
787
+ input: 分页配置
788
+ config: 配置对象
789
+
790
+ Returns:
791
+ List[Template]: Template 列表
792
+ """
793
+ return await cls.__get_client().list_templates_async(
794
+ input, config=config
795
+ )
796
+
797
+ @classmethod
798
+ def list_templates(
799
+ cls,
800
+ input: Optional["PageableInput"] = None,
801
+ config: Optional[Config] = None,
802
+ ) -> List["Template"]:
803
+ """列出 Templates(同步)
804
+
805
+ Args:
806
+ input: 分页配置
807
+ config: 配置对象
808
+
809
+ Returns:
810
+ List[Template]: Template 列表
811
+ """
812
+ return cls.__get_client().list_templates(input, config=config)
813
+
814
+ async def get_async(self):
815
+ if self.sandbox_id is None:
816
+ raise ValueError("sandbox_id is required to get a Sandbox")
817
+
818
+ return await self.connect_async(self.sandbox_id)
819
+
820
+ def get(self):
821
+ if self.sandbox_id is None:
822
+ raise ValueError("sandbox_id is required to get a Sandbox")
823
+
824
+ return self.connect(self.sandbox_id)
825
+
826
+ async def delete_async(self):
827
+ if self.sandbox_id is None:
828
+ raise ValueError("sandbox_id is required to delete a Sandbox")
829
+
830
+ return await self.delete_by_id_async(self.sandbox_id)
831
+
832
+ def delete(self):
833
+ if self.sandbox_id is None:
834
+ raise ValueError("sandbox_id is required to delete a Sandbox")
835
+
836
+ return self.delete_by_id(self.sandbox_id)
837
+
838
+ async def stop_async(self):
839
+ if self.sandbox_id is None:
840
+ raise ValueError("sandbox_id is required to stop a Sandbox")
841
+ # todo 后续适配后使用 stop()
842
+ return await self.delete_by_id_async(self.sandbox_id)
843
+
844
+ def stop(self):
845
+ if self.sandbox_id is None:
846
+ raise ValueError("sandbox_id is required to stop a Sandbox")
847
+ # todo 后续适配后使用 stop()
848
+ return self.delete_by_id(self.sandbox_id)