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,463 @@
1
+ """Sandbox 高层 API / Sandbox High-Level API
2
+
3
+ 此模块定义沙箱资源的高级API。
4
+ This module defines the high-level API for sandbox resources.
5
+ """
6
+
7
+ from typing import (
8
+ Any,
9
+ Dict,
10
+ List,
11
+ Literal,
12
+ Optional,
13
+ overload,
14
+ TYPE_CHECKING,
15
+ Union,
16
+ )
17
+
18
+ from agentrun.sandbox.model import TemplateType
19
+ from agentrun.utils.config import Config
20
+ from agentrun.utils.model import BaseModel
21
+
22
+ if TYPE_CHECKING:
23
+ from agentrun.sandbox.aio_sandbox import AioSandbox
24
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
25
+ from agentrun.sandbox.code_interpreter_sandbox import CodeInterpreterSandbox
26
+ from agentrun.sandbox.model import (
27
+ ListSandboxesInput,
28
+ ListSandboxesOutput,
29
+ NASConfig,
30
+ OSSMountConfig,
31
+ PageableInput,
32
+ PolarFsConfig,
33
+ TemplateInput,
34
+ )
35
+ from agentrun.sandbox.template import Template
36
+
37
+
38
+ class Sandbox(BaseModel):
39
+ """Sandbox 实例
40
+
41
+ 封装了 Sandbox 的基本信息和操作方法
42
+ """
43
+
44
+ _template_type: Optional[TemplateType]
45
+
46
+ created_at: Optional[str] = None
47
+ """沙箱创建时间 / Sandbox creation time"""
48
+ ended_at: Optional[str] = None
49
+ """沙箱结束时间 / Sandbox end time"""
50
+ last_updated_at: Optional[str] = None
51
+ """最后更新时间 / Last updated time"""
52
+ metadata: Optional[Dict[str, Any]] = None
53
+ """元数据 / Metadata"""
54
+ sandbox_arn: Optional[str] = None
55
+ """沙箱全局唯一资源名称 / Sandbox ARN"""
56
+ sandbox_id: Optional[str] = None
57
+ """沙箱 ID / Sandbox ID"""
58
+ sandbox_idle_ttlin_seconds: Optional[int] = None
59
+ """沙箱空闲 TTL(秒) / Sandbox Idle TTL (seconds)"""
60
+ sandbox_idle_timeout_seconds: Optional[int] = None
61
+ """沙箱空闲超时时间(秒) / Sandbox Idle Timeout (seconds)"""
62
+ status: Optional[str] = None
63
+ """沙箱状态 / Sandbox status"""
64
+ template_id: Optional[str] = None
65
+ """模板 ID / Template ID"""
66
+ template_name: Optional[str] = None
67
+ """模板名称 / Template name"""
68
+ _config: Optional[Config] = None
69
+ """配置对象,用于子类的 data_api 初始化 / Config object for data_api initialization"""
70
+
71
+ @classmethod
72
+ def __get_client(cls):
73
+ """获取 Sandbox 客户端"""
74
+ from .client import SandboxClient
75
+
76
+ return SandboxClient()
77
+
78
+ @classmethod
79
+ @overload
80
+ async def create_async(
81
+ cls,
82
+ template_type: Literal[TemplateType.CODE_INTERPRETER],
83
+ template_name: Optional[str] = None,
84
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
85
+ nas_config: Optional["NASConfig"] = None,
86
+ oss_mount_config: Optional["OSSMountConfig"] = None,
87
+ polar_fs_config: Optional["PolarFsConfig"] = None,
88
+ config: Optional[Config] = None,
89
+ ) -> "CodeInterpreterSandbox":
90
+ ...
91
+
92
+ @classmethod
93
+ @overload
94
+ async def create_async(
95
+ cls,
96
+ template_type: Literal[TemplateType.BROWSER],
97
+ template_name: Optional[str] = None,
98
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
99
+ nas_config: Optional["NASConfig"] = None,
100
+ oss_mount_config: Optional["OSSMountConfig"] = None,
101
+ polar_fs_config: Optional["PolarFsConfig"] = None,
102
+ config: Optional[Config] = None,
103
+ ) -> "BrowserSandbox":
104
+ ...
105
+
106
+ @classmethod
107
+ @overload
108
+ async def create_async(
109
+ cls,
110
+ template_type: Literal[TemplateType.AIO],
111
+ template_name: Optional[str] = None,
112
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
113
+ nas_config: Optional["NASConfig"] = None,
114
+ oss_mount_config: Optional["OSSMountConfig"] = None,
115
+ polar_fs_config: Optional["PolarFsConfig"] = None,
116
+ config: Optional[Config] = None,
117
+ ) -> "AioSandbox":
118
+ ...
119
+
120
+ @classmethod
121
+ async def create_async(
122
+ cls,
123
+ template_type: TemplateType,
124
+ template_name: Optional[str] = None,
125
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
126
+ nas_config: Optional["NASConfig"] = None,
127
+ oss_mount_config: Optional["OSSMountConfig"] = None,
128
+ polar_fs_config: Optional["PolarFsConfig"] = None,
129
+ config: Optional[Config] = None,
130
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
131
+
132
+ if template_name is None:
133
+ # todo 可以考虑为用户创建一个模板?
134
+ raise ValueError("template_name is required")
135
+
136
+ # 先根据传入的template_name,获取template的类型
137
+ template = await cls.get_template_async(template_name, config=config)
138
+
139
+ # 根据 template 类型创建相应的 Sandbox 子类
140
+ from agentrun.sandbox.aio_sandbox import AioSandbox
141
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
142
+ from agentrun.sandbox.code_interpreter_sandbox import (
143
+ CodeInterpreterSandbox,
144
+ )
145
+
146
+ if template_type != template.template_type:
147
+ raise ValueError(
148
+ f"template_type of {template_name} is {template.template_type},"
149
+ f" not {template_type}"
150
+ )
151
+
152
+ # 创建 Sandbox(返回基类实例)
153
+ base_sandbox = await cls.__get_client().create_sandbox_async(
154
+ template_name=template_name,
155
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
156
+ nas_config=nas_config,
157
+ oss_mount_config=oss_mount_config,
158
+ polar_fs_config=polar_fs_config,
159
+ )
160
+
161
+ # 根据 template 类型转换为对应的子类实例
162
+ sandbox = None
163
+ if template.template_type == TemplateType.CODE_INTERPRETER:
164
+ sandbox = CodeInterpreterSandbox.model_validate(
165
+ base_sandbox.model_dump(by_alias=False)
166
+ )
167
+ elif template.template_type == TemplateType.BROWSER:
168
+ sandbox = BrowserSandbox.model_validate(
169
+ base_sandbox.model_dump(by_alias=False)
170
+ )
171
+ elif template.template_type == TemplateType.AIO:
172
+ sandbox = AioSandbox.model_validate(
173
+ base_sandbox.model_dump(by_alias=False)
174
+ )
175
+ else:
176
+ raise ValueError(
177
+ f"template_type {template.template_type} is not supported"
178
+ )
179
+
180
+ sandbox._config = config
181
+ return sandbox
182
+
183
+ @classmethod
184
+ async def stop_by_id_async(cls, sandbox_id: str):
185
+ """通过 ID 停止 Sandbox(异步)
186
+
187
+ Args:
188
+ sandbox_id: Sandbox ID
189
+ config: 配置对象
190
+
191
+ Returns:
192
+ Sandbox: Sandbox 对象
193
+ """
194
+ if sandbox_id is None:
195
+ raise ValueError("sandbox_id is required")
196
+ # todo 后续适配后使用 stop()
197
+ return await cls.__get_client().delete_sandbox_async(sandbox_id)
198
+
199
+ @classmethod
200
+ async def delete_by_id_async(cls, sandbox_id: str):
201
+ """通过 ID 删除 Sandbox(异步)
202
+
203
+ Args:
204
+ sandbox_id: Sandbox ID
205
+ config: 配置对象
206
+
207
+ Returns:
208
+ Sandbox: Sandbox 对象
209
+ """
210
+ if sandbox_id is None:
211
+ raise ValueError("sandbox_id is required")
212
+ return await cls.__get_client().delete_sandbox_async(sandbox_id)
213
+
214
+ @classmethod
215
+ async def list_async(
216
+ cls,
217
+ input: Optional["ListSandboxesInput"] = None,
218
+ config: Optional[Config] = None,
219
+ ) -> "ListSandboxesOutput":
220
+ """列出 Sandboxes(异步)
221
+
222
+ Args:
223
+ input: 列表查询配置
224
+ config: 配置对象
225
+
226
+ Returns:
227
+ ListSandboxesOutput: Sandbox 列表结果
228
+ """
229
+ return await cls.__get_client().list_sandboxes_async(input, config)
230
+
231
+ @classmethod
232
+ @overload
233
+ async def connect_async(
234
+ cls,
235
+ sandbox_id: str,
236
+ template_type: Literal[TemplateType.CODE_INTERPRETER],
237
+ config: Optional[Config] = None,
238
+ ) -> "CodeInterpreterSandbox":
239
+ ...
240
+
241
+ @classmethod
242
+ @overload
243
+ async def connect_async(
244
+ cls,
245
+ sandbox_id: str,
246
+ template_type: Literal[TemplateType.BROWSER],
247
+ config: Optional[Config] = None,
248
+ ) -> "BrowserSandbox":
249
+ ...
250
+
251
+ @classmethod
252
+ @overload
253
+ async def connect_async(
254
+ cls,
255
+ sandbox_id: str,
256
+ template_type: Literal[TemplateType.AIO],
257
+ config: Optional[Config] = None,
258
+ ) -> "AioSandbox":
259
+ ...
260
+
261
+ @classmethod
262
+ @overload
263
+ async def connect_async(
264
+ cls,
265
+ sandbox_id: str,
266
+ template_type: None = None,
267
+ config: Optional[Config] = None,
268
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
269
+ ...
270
+
271
+ @classmethod
272
+ async def connect_async(
273
+ cls,
274
+ sandbox_id: str,
275
+ template_type: Optional[TemplateType] = None,
276
+ config: Optional[Config] = None,
277
+ ) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
278
+ """连接一个SandBox(异步)
279
+
280
+ Args:
281
+ sandbox_id: Sandbox ID
282
+ type: 可选的类型参数,用于类型提示和运行时验证
283
+ config: 配置对象
284
+
285
+ Returns:
286
+ Sandbox: 根据模板类型返回对应的 Sandbox 子类对象
287
+
288
+ Raises:
289
+ ValueError: 如果模板类型不支持或与预期类型不匹配
290
+ """
291
+ if sandbox_id is None:
292
+ raise ValueError("sandbox_id is required")
293
+
294
+ # 先获取 sandbox 信息
295
+ sandbox = await cls.__get_client().get_sandbox_async(
296
+ sandbox_id, config=config
297
+ )
298
+
299
+ # 根据 template_name 获取 template 类型
300
+ if sandbox.template_name is None:
301
+ raise ValueError(f"Sandbox {sandbox_id} has no template_name")
302
+
303
+ template = await cls.get_template_async(
304
+ sandbox.template_name, config=config
305
+ )
306
+
307
+ # 如果提供了 type 参数,验证类型是否匹配
308
+ if (
309
+ template_type is not None
310
+ and template.template_type != template_type
311
+ ):
312
+ raise ValueError(
313
+ f"Sandbox {sandbox_id} has template type"
314
+ f" {template.template_type}, but expected {template_type}"
315
+ )
316
+
317
+ # 根据 template 类型创建相应的 Sandbox 子类
318
+ from agentrun.sandbox.aio_sandbox import AioSandbox
319
+ from agentrun.sandbox.browser_sandbox import BrowserSandbox
320
+ from agentrun.sandbox.code_interpreter_sandbox import (
321
+ CodeInterpreterSandbox,
322
+ )
323
+
324
+ result = None
325
+ if template.template_type == TemplateType.CODE_INTERPRETER:
326
+ result = CodeInterpreterSandbox.model_validate(
327
+ sandbox.model_dump(by_alias=False)
328
+ )
329
+ elif template.template_type == TemplateType.BROWSER:
330
+ result = BrowserSandbox.model_validate(
331
+ sandbox.model_dump(by_alias=False)
332
+ )
333
+ elif template.template_type == TemplateType.AIO:
334
+ result = AioSandbox.model_validate(
335
+ sandbox.model_dump(by_alias=False)
336
+ )
337
+ else:
338
+ raise ValueError(
339
+ f"Unsupported template type: {template.template_type}. "
340
+ "Expected 'code-interpreter', 'browser' or 'aio'"
341
+ )
342
+
343
+ result._config = config
344
+ return result
345
+
346
+ # ==================== Template 相关类方法 ====================
347
+
348
+ @classmethod
349
+ async def create_template_async(
350
+ cls, input: "TemplateInput", config: Optional[Config] = None
351
+ ) -> "Template":
352
+ """创建 Template(异步)
353
+
354
+ Args:
355
+ input: Template 配置
356
+ config: 配置对象
357
+
358
+ Returns:
359
+ Template: 创建的 Template 对象
360
+ """
361
+ if input.template_type is None:
362
+ raise ValueError("template_type is required")
363
+ return await cls.__get_client().create_template_async(
364
+ input, config=config
365
+ )
366
+
367
+ @classmethod
368
+ async def get_template_async(
369
+ cls, template_name: str, config: Optional[Config] = None
370
+ ) -> "Template":
371
+ """获取 Template(异步)
372
+
373
+ Args:
374
+ template_name: Template 名称
375
+ config: 配置对象
376
+
377
+ Returns:
378
+ Template: Template 对象
379
+ """
380
+ if template_name is None:
381
+ raise ValueError("template_name is required")
382
+ return await cls.__get_client().get_template_async(
383
+ template_name, config=config
384
+ )
385
+
386
+ @classmethod
387
+ async def update_template_async(
388
+ cls,
389
+ template_name: str,
390
+ input: "TemplateInput",
391
+ config: Optional[Config] = None,
392
+ ) -> "Template":
393
+ """更新 Template(异步)
394
+
395
+ Args:
396
+ template_name: Template 名称
397
+ input: Template 更新配置
398
+ config: 配置对象
399
+
400
+ Returns:
401
+ Template: 更新后的 Template 对象
402
+ """
403
+ if template_name is None:
404
+ raise ValueError("template_name is required")
405
+ return await cls.__get_client().update_template_async(
406
+ template_name, input, config=config
407
+ )
408
+
409
+ @classmethod
410
+ async def delete_template_async(
411
+ cls, template_name: str, config: Optional[Config] = None
412
+ ) -> "Template":
413
+ """删除 Template(异步)
414
+
415
+ Args:
416
+ template_name: Template 名称
417
+ config: 配置对象
418
+
419
+ Returns:
420
+ Template: 删除的 Template 对象
421
+ """
422
+ if template_name is None:
423
+ raise ValueError("template_name is required")
424
+ return await cls.__get_client().delete_template_async(
425
+ template_name, config=config
426
+ )
427
+
428
+ @classmethod
429
+ async def list_templates_async(
430
+ cls,
431
+ input: Optional["PageableInput"] = None,
432
+ config: Optional[Config] = None,
433
+ ) -> List["Template"]:
434
+ """列出 Templates(异步)
435
+
436
+ Args:
437
+ input: 分页配置
438
+ config: 配置对象
439
+
440
+ Returns:
441
+ List[Template]: Template 列表
442
+ """
443
+ return await cls.__get_client().list_templates_async(
444
+ input, config=config
445
+ )
446
+
447
+ async def get_async(self):
448
+ if self.sandbox_id is None:
449
+ raise ValueError("sandbox_id is required to get a Sandbox")
450
+
451
+ return await self.connect_async(self.sandbox_id)
452
+
453
+ async def delete_async(self):
454
+ if self.sandbox_id is None:
455
+ raise ValueError("sandbox_id is required to delete a Sandbox")
456
+
457
+ return await self.delete_by_id_async(self.sandbox_id)
458
+
459
+ async def stop_async(self):
460
+ if self.sandbox_id is None:
461
+ raise ValueError("sandbox_id is required to stop a Sandbox")
462
+ # todo 后续适配后使用 stop()
463
+ return await self.delete_by_id_async(self.sandbox_id)
@@ -0,0 +1,152 @@
1
+ """Template 高层 API / Template High-Level API
2
+
3
+ 此模块定义沙箱模板资源的高级API。
4
+ This module defines the high-level API for sandbox template resources.
5
+ """
6
+
7
+ from typing import Dict, List, Optional
8
+
9
+ from agentrun.sandbox.model import (
10
+ PageableInput,
11
+ TemplateContainerConfiguration,
12
+ TemplateCredentialConfiguration,
13
+ TemplateInput,
14
+ TemplateLogConfiguration,
15
+ TemplateMcpOptions,
16
+ TemplateMcpState,
17
+ TemplateNetworkConfiguration,
18
+ TemplateOssConfiguration,
19
+ TemplateType,
20
+ )
21
+ from agentrun.utils.config import Config
22
+ from agentrun.utils.model import BaseModel
23
+
24
+
25
+ class Template(BaseModel):
26
+ """Template 实例
27
+
28
+ 封装了 Template 的基本信息和操作方法
29
+ """
30
+
31
+ template_id: Optional[str] = None
32
+ """模板 ID / Template ID"""
33
+ template_name: Optional[str] = None
34
+ """模板名称 / Template Name"""
35
+ template_version: Optional[str] = None
36
+ """模板版本 / Template Version"""
37
+ template_arn: Optional[str] = None
38
+ """模板 ARN / Template ARN"""
39
+ resource_name: Optional[str] = None
40
+ """资源名称 / Resource Name"""
41
+ template_type: Optional[TemplateType] = None
42
+ """模板类型 / Template Type"""
43
+ cpu: Optional[float] = None
44
+ """CPU 核数 / CPU Cores"""
45
+ memory: Optional[int] = None
46
+ """内存大小(MB) / Memory Size (MB)"""
47
+ disk_size: Optional[int] = None
48
+ """磁盘大小(GB) / Disk Size (GB)"""
49
+ description: Optional[str] = None
50
+ """描述 / Description"""
51
+ execution_role_arn: Optional[str] = None
52
+ """执行角色 ARN / Execution Role ARN"""
53
+ sandbox_idle_timeout_in_seconds: Optional[int] = None
54
+ """沙箱空闲超时时间(秒) / Sandbox Idle Timeout (seconds)"""
55
+ sandbox_ttlin_seconds: Optional[int] = None
56
+ """沙箱存活时间(秒) / Sandbox TTL (seconds)"""
57
+ share_concurrency_limit_per_sandbox: Optional[int] = None
58
+ """每个沙箱的最大并发会话数 / Max Concurrency Limit Per Sandbox"""
59
+ template_configuration: Optional[Dict] = None
60
+ """模板配置 / Template Configuration"""
61
+ environment_variables: Optional[Dict] = None
62
+ """环境变量 / Environment Variables"""
63
+ network_configuration: Optional[TemplateNetworkConfiguration] = None
64
+ """网络配置 / Network Configuration"""
65
+ oss_configuration: Optional[List[TemplateOssConfiguration]] = None
66
+ """OSS 配置列表 / OSS Configuration List"""
67
+ log_configuration: Optional[TemplateLogConfiguration] = None
68
+ """日志配置 / Log Configuration"""
69
+ credential_configuration: Optional[TemplateCredentialConfiguration] = None
70
+ """凭证配置 / Credential Configuration"""
71
+ container_configuration: Optional[TemplateContainerConfiguration] = None
72
+ """容器配置 / Container Configuration"""
73
+ mcp_options: Optional[TemplateMcpOptions] = None
74
+ """MCP 选项 / MCP Options"""
75
+ mcp_state: Optional[TemplateMcpState] = None
76
+ """MCP 状态 / MCP State"""
77
+ allow_anonymous_manage: Optional[bool] = None
78
+ """是否允许匿名管理 / Whether to allow anonymous management"""
79
+ created_at: Optional[str] = None
80
+ """创建时间 / Creation Time"""
81
+ last_updated_at: Optional[str] = None
82
+ """最后更新时间 / Last Updated Time"""
83
+ status: Optional[str] = None
84
+ """状态 / Status"""
85
+ status_reason: Optional[str] = None
86
+ """状态原因 / Status Reason"""
87
+
88
+ @classmethod
89
+ def __get_client(cls, config: Optional[Config] = None):
90
+ """获取 Sandbox 客户端"""
91
+ from .client import SandboxClient
92
+
93
+ return SandboxClient(config)
94
+
95
+ @classmethod
96
+ async def create_async(
97
+ cls, input: TemplateInput, config: Optional[Config] = None
98
+ ):
99
+ return await cls.__get_client(config=config).create_template_async(
100
+ input, config=config
101
+ )
102
+
103
+ @classmethod
104
+ async def delete_by_name_async(
105
+ cls, template_name: str, config: Optional[Config] = None
106
+ ):
107
+ return await cls.__get_client(config=config).delete_template_async(
108
+ template_name=template_name, config=config
109
+ )
110
+
111
+ @classmethod
112
+ async def update_by_name_async(
113
+ cls,
114
+ template_name: str,
115
+ input: TemplateInput,
116
+ config: Optional[Config] = None,
117
+ ):
118
+ return await cls.__get_client(config=config).update_template_async(
119
+ template_name=template_name, input=input, config=config
120
+ )
121
+
122
+ @classmethod
123
+ async def get_by_name_async(
124
+ cls, template_name: str, config: Optional[Config] = None
125
+ ):
126
+ return await cls.__get_client(config=config).get_template_async(
127
+ template_name=template_name, config=config
128
+ )
129
+
130
+ @classmethod
131
+ async def list_templates_async(
132
+ cls,
133
+ input: Optional[PageableInput] = None,
134
+ config: Optional[Config] = None,
135
+ ):
136
+ return await cls.__get_client(config=config).list_templates_async(
137
+ input, config=config
138
+ )
139
+
140
+ async def create_sandbox_async(
141
+ self,
142
+ sandbox_idle_timeout_seconds: Optional[int] = None,
143
+ config: Optional[Config] = None,
144
+ ):
145
+ if self.template_name is None:
146
+ raise ValueError("Template name is required")
147
+
148
+ return await self.__get_client(config=config).create_sandbox_async(
149
+ self.template_name,
150
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
151
+ config=config,
152
+ )