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,925 @@
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/__client_async_template.py
10
+
11
+ Sandbox客户端模板 / Sandbox Client Template
12
+
13
+ 此模板用于生成沙箱客户端代码。
14
+ This template is used to generate sandbox client code.
15
+ """
16
+
17
+ import time
18
+ from typing import Any, Dict, List, Optional, TYPE_CHECKING
19
+
20
+ from alibabacloud_agentrun20250910.models import (
21
+ CreateTemplateInput,
22
+ ListSandboxesRequest,
23
+ ListTemplatesRequest,
24
+ UpdateTemplateInput,
25
+ )
26
+
27
+ from agentrun.sandbox.api import SandboxControlAPI, SandboxDataAPI
28
+ from agentrun.sandbox.model import (
29
+ ListSandboxesInput,
30
+ ListSandboxesOutput,
31
+ NASConfig,
32
+ OSSMountConfig,
33
+ PageableInput,
34
+ PolarFsConfig,
35
+ TemplateInput,
36
+ )
37
+ from agentrun.utils.config import Config
38
+ from agentrun.utils.exception import (
39
+ AgentRunError,
40
+ ClientError,
41
+ ResourceNotExistError,
42
+ )
43
+
44
+ from .sandbox import Sandbox
45
+
46
+ if TYPE_CHECKING:
47
+ from agentrun.sandbox.template import Template
48
+
49
+
50
+ class SandboxClient:
51
+ """Sandbox 客户端 / Sandbox Client
52
+
53
+ 用于管理 Sandbox 和 Template。
54
+ Used for managing Sandboxes and Templates.
55
+ """
56
+
57
+ def __init__(self, config: Optional[Config] = None):
58
+ """初始化 Sandbox 客户端 / Initialize Sandbox client
59
+
60
+ Args:
61
+ config: 配置对象,可选 / Configuration object, optional
62
+ """
63
+ self.__control_api = SandboxControlAPI(config=config)
64
+ self.__sandbox_data_api = SandboxDataAPI(config=config)
65
+
66
+ async def _wait_template_ready_async(
67
+ self,
68
+ template_name: str,
69
+ config: Optional[Config] = None,
70
+ interval_seconds: int = 5,
71
+ timeout_seconds: int = 300,
72
+ ) -> "Template":
73
+ """Wait for Template to be ready (async)
74
+
75
+ Args:
76
+ template_name: Template name
77
+ config: Config object
78
+ interval_seconds: Polling interval in seconds
79
+ timeout_seconds: Timeout in seconds
80
+
81
+ Returns:
82
+ Template: Ready Template object
83
+
84
+ Raises:
85
+ TimeoutError: Timeout error
86
+ ClientError: Client error
87
+ """
88
+ import asyncio
89
+
90
+ start_time = time.time()
91
+ while True:
92
+ template = await self.get_template_async(
93
+ template_name, config=config
94
+ )
95
+
96
+ # Check if ready
97
+ if template.status == "READY":
98
+ return template
99
+
100
+ # Check if failed
101
+ if (
102
+ template.status == "CREATE_FAILED"
103
+ or template.status == "UPDATE_FAILED"
104
+ ):
105
+ raise AgentRunError(
106
+ f"Template {template_name} creation failed, status:"
107
+ f" {template.status}"
108
+ )
109
+
110
+ # Check timeout
111
+ if time.time() - start_time > timeout_seconds:
112
+ raise TimeoutError(
113
+ f"Timeout waiting for Template {template_name} to be ready,"
114
+ f" current status: {template.status}"
115
+ )
116
+
117
+ await asyncio.sleep(interval_seconds)
118
+
119
+ def _wait_template_ready(
120
+ self,
121
+ template_name: str,
122
+ config: Optional[Config] = None,
123
+ interval_seconds: int = 5,
124
+ timeout_seconds: int = 300,
125
+ ) -> "Template":
126
+ """Wait for Template to be ready (async)
127
+
128
+ Args:
129
+ template_name: Template name
130
+ config: Config object
131
+ interval_seconds: Polling interval in seconds
132
+ timeout_seconds: Timeout in seconds
133
+
134
+ Returns:
135
+ Template: Ready Template object
136
+
137
+ Raises:
138
+ TimeoutError: Timeout error
139
+ ClientError: Client error
140
+ """
141
+
142
+ start_time = time.time()
143
+ while True:
144
+ template = self.get_template(template_name, config=config)
145
+
146
+ # Check if ready
147
+ if template.status == "READY":
148
+ return template
149
+
150
+ # Check if failed
151
+ if (
152
+ template.status == "CREATE_FAILED"
153
+ or template.status == "UPDATE_FAILED"
154
+ ):
155
+ raise AgentRunError(
156
+ f"Template {template_name} creation failed, status:"
157
+ f" {template.status}"
158
+ )
159
+
160
+ # Check timeout
161
+ if time.time() - start_time > timeout_seconds:
162
+ raise TimeoutError(
163
+ f"Timeout waiting for Template {template_name} to be ready,"
164
+ f" current status: {template.status}"
165
+ )
166
+
167
+ time.sleep(interval_seconds)
168
+
169
+ async def create_template_async(
170
+ self, input: TemplateInput, config: Optional[Config] = None
171
+ ) -> "Template":
172
+ """创建 Template(异步)
173
+
174
+ Args:
175
+ input: Template 配置
176
+ config: 配置对象
177
+
178
+ Returns:
179
+ Template: 创建的 Template 对象
180
+
181
+ Raises:
182
+ ClientError: 客户端错误
183
+ ServerError: 服务器错误
184
+ """
185
+ from agentrun.sandbox.template import Template
186
+
187
+ # 转换为 SDK 需要的格式
188
+ sdk_input = CreateTemplateInput().from_map(
189
+ input.model_dump(by_alias=True)
190
+ )
191
+ result = await self.__control_api.create_template_async(
192
+ sdk_input, config=config
193
+ )
194
+ template = Template.from_inner_object(result)
195
+
196
+ # Poll and wait for Template to be ready
197
+ template = await self._wait_template_ready_async(
198
+ template.template_name or "", config=config
199
+ )
200
+
201
+ return template
202
+
203
+ def create_template(
204
+ self, input: TemplateInput, config: Optional[Config] = None
205
+ ) -> "Template":
206
+ """创建 Template(同步)
207
+
208
+ Args:
209
+ input: Template 配置
210
+ config: 配置对象
211
+
212
+ Returns:
213
+ Template: 创建的 Template 对象
214
+
215
+ Raises:
216
+ ClientError: 客户端错误
217
+ ServerError: 服务器错误
218
+ """
219
+ from agentrun.sandbox.template import Template
220
+
221
+ # 转换为 SDK 需要的格式
222
+ sdk_input = CreateTemplateInput().from_map(
223
+ input.model_dump(by_alias=True)
224
+ )
225
+ result = self.__control_api.create_template(sdk_input, config=config)
226
+ template = Template.from_inner_object(result)
227
+
228
+ # Poll and wait for Template to be ready
229
+ template = self._wait_template_ready(
230
+ template.template_name or "", config=config
231
+ )
232
+
233
+ return template
234
+
235
+ async def delete_template_async(
236
+ self, template_name: str, config: Optional[Config] = None
237
+ ) -> "Template":
238
+ """删除 Template(异步)
239
+
240
+ Args:
241
+ template_name: Template 名称
242
+ config: 配置对象
243
+
244
+ Returns:
245
+ Template: 删除的 Template 对象
246
+
247
+ Raises:
248
+ ResourceNotExistError: Template 不存在
249
+ ClientError: 客户端错误
250
+ ServerError: 服务器错误
251
+ """
252
+ from agentrun.sandbox.template import Template
253
+
254
+ try:
255
+ result = await self.__control_api.delete_template_async(
256
+ template_name, config=config
257
+ )
258
+ return Template.from_inner_object(result)
259
+ except ClientError as e:
260
+ if e.status_code == 404:
261
+ raise ResourceNotExistError("Template", template_name) from e
262
+ raise e
263
+
264
+ def delete_template(
265
+ self, template_name: str, config: Optional[Config] = None
266
+ ) -> "Template":
267
+ """删除 Template(同步)
268
+
269
+ Args:
270
+ template_name: Template 名称
271
+ config: 配置对象
272
+
273
+ Returns:
274
+ Template: 删除的 Template 对象
275
+
276
+ Raises:
277
+ ResourceNotExistError: Template 不存在
278
+ ClientError: 客户端错误
279
+ ServerError: 服务器错误
280
+ """
281
+ from agentrun.sandbox.template import Template
282
+
283
+ try:
284
+ result = self.__control_api.delete_template(
285
+ template_name, config=config
286
+ )
287
+ return Template.from_inner_object(result)
288
+ except ClientError as e:
289
+ if e.status_code == 404:
290
+ raise ResourceNotExistError("Template", template_name) from e
291
+ raise e
292
+
293
+ async def update_template_async(
294
+ self,
295
+ template_name: str,
296
+ input: TemplateInput,
297
+ config: Optional[Config] = None,
298
+ ) -> "Template":
299
+ """更新 Template(异步)
300
+
301
+ Args:
302
+ template_name: Template 名称
303
+ input: Template 更新配置
304
+ config: 配置对象
305
+
306
+ Returns:
307
+ Template: 更新后的 Template 对象
308
+
309
+ Raises:
310
+ ResourceNotExistError: Template 不存在
311
+ ClientError: 客户端错误
312
+ ServerError: 服务器错误
313
+ """
314
+ from agentrun.sandbox.template import Template
315
+
316
+ try:
317
+ # 转换为 SDK 需要的格式
318
+ sdk_input = UpdateTemplateInput().from_map(
319
+ input.model_dump(by_alias=True, exclude_none=True)
320
+ )
321
+ result = await self.__control_api.update_template_async(
322
+ template_name, sdk_input, config=config
323
+ )
324
+ return Template.from_inner_object(result)
325
+ except ClientError as e:
326
+ if e.status_code == 404:
327
+ raise ResourceNotExistError("Template", template_name) from e
328
+ raise e
329
+
330
+ def update_template(
331
+ self,
332
+ template_name: str,
333
+ input: TemplateInput,
334
+ config: Optional[Config] = None,
335
+ ) -> "Template":
336
+ """更新 Template(同步)
337
+
338
+ Args:
339
+ template_name: Template 名称
340
+ input: Template 更新配置
341
+ config: 配置对象
342
+
343
+ Returns:
344
+ Template: 更新后的 Template 对象
345
+
346
+ Raises:
347
+ ResourceNotExistError: Template 不存在
348
+ ClientError: 客户端错误
349
+ ServerError: 服务器错误
350
+ """
351
+ from agentrun.sandbox.template import Template
352
+
353
+ try:
354
+ # 转换为 SDK 需要的格式
355
+ sdk_input = UpdateTemplateInput().from_map(
356
+ input.model_dump(by_alias=True, exclude_none=True)
357
+ )
358
+ result = self.__control_api.update_template(
359
+ template_name, sdk_input, config=config
360
+ )
361
+ return Template.from_inner_object(result)
362
+ except ClientError as e:
363
+ if e.status_code == 404:
364
+ raise ResourceNotExistError("Template", template_name) from e
365
+ raise e
366
+
367
+ async def get_template_async(
368
+ self, template_name: str, config: Optional[Config] = None
369
+ ) -> "Template":
370
+ """获取 Template(异步)
371
+
372
+ Args:
373
+ template_name: Template 名称
374
+ config: 配置对象
375
+
376
+ Returns:
377
+ Template: Template 对象
378
+
379
+ Raises:
380
+ ResourceNotExistError: Template 不存在
381
+ ClientError: 客户端错误
382
+ ServerError: 服务器错误
383
+ """
384
+ from agentrun.sandbox.template import Template
385
+
386
+ try:
387
+ result = await self.__control_api.get_template_async(
388
+ template_name, config=config
389
+ )
390
+ return Template.from_inner_object(result)
391
+ except ClientError as e:
392
+ if e.status_code == 404:
393
+ raise ResourceNotExistError("Template", template_name) from e
394
+ raise e
395
+
396
+ def get_template(
397
+ self, template_name: str, config: Optional[Config] = None
398
+ ) -> "Template":
399
+ """获取 Template(同步)
400
+
401
+ Args:
402
+ template_name: Template 名称
403
+ config: 配置对象
404
+
405
+ Returns:
406
+ Template: Template 对象
407
+
408
+ Raises:
409
+ ResourceNotExistError: Template 不存在
410
+ ClientError: 客户端错误
411
+ ServerError: 服务器错误
412
+ """
413
+ from agentrun.sandbox.template import Template
414
+
415
+ try:
416
+ result = self.__control_api.get_template(
417
+ template_name, config=config
418
+ )
419
+ return Template.from_inner_object(result)
420
+ except ClientError as e:
421
+ if e.status_code == 404:
422
+ raise ResourceNotExistError("Template", template_name) from e
423
+ raise e
424
+
425
+ async def list_templates_async(
426
+ self,
427
+ input: Optional[PageableInput] = None,
428
+ config: Optional[Config] = None,
429
+ ) -> List["Template"]:
430
+ """枚举 Templates(异步)
431
+
432
+ Args:
433
+ input: 分页配置
434
+ config: 配置对象
435
+
436
+ Returns:
437
+ List[Template]: Template 列表
438
+
439
+ Raises:
440
+ ClientError: 客户端错误
441
+ ServerError: 服务器错误
442
+ TimeoutError: Timeout waiting for Template to be ready
443
+ """
444
+ from agentrun.sandbox.template import Template
445
+
446
+ if input is None:
447
+ input = PageableInput()
448
+
449
+ # 转换为 SDK 需要的格式
450
+ sdk_input = ListTemplatesRequest().from_map(
451
+ input.model_dump(by_alias=True)
452
+ )
453
+ results = await self.__control_api.list_templates_async(
454
+ sdk_input, config=config
455
+ )
456
+ return (
457
+ [Template.from_inner_object(item) for item in results.items]
458
+ if results.items
459
+ else []
460
+ )
461
+
462
+ def list_templates(
463
+ self,
464
+ input: Optional[PageableInput] = None,
465
+ config: Optional[Config] = None,
466
+ ) -> List["Template"]:
467
+ """枚举 Templates(同步)
468
+
469
+ Args:
470
+ input: 分页配置
471
+ config: 配置对象
472
+
473
+ Returns:
474
+ List[Template]: Template 列表
475
+
476
+ Raises:
477
+ ClientError: 客户端错误
478
+ ServerError: 服务器错误
479
+ TimeoutError: Timeout waiting for Template to be ready
480
+ """
481
+ from agentrun.sandbox.template import Template
482
+
483
+ if input is None:
484
+ input = PageableInput()
485
+
486
+ # 转换为 SDK 需要的格式
487
+ sdk_input = ListTemplatesRequest().from_map(
488
+ input.model_dump(by_alias=True)
489
+ )
490
+ results = self.__control_api.list_templates(sdk_input, config=config)
491
+ return (
492
+ [Template.from_inner_object(item) for item in results.items]
493
+ if results.items
494
+ else []
495
+ )
496
+
497
+ async def create_sandbox_async(
498
+ self,
499
+ template_name: str,
500
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
501
+ nas_config: Optional[NASConfig] = None,
502
+ oss_mount_config: Optional[OSSMountConfig] = None,
503
+ polar_fs_config: Optional[PolarFsConfig] = None,
504
+ config: Optional[Config] = None,
505
+ ) -> Sandbox:
506
+ """创建 Sandbox(异步) / Create Sandbox (async)
507
+
508
+ Args:
509
+ template_name: 模板名称 / Template name
510
+ sandbox_idle_timeout_seconds: 沙箱空闲超时时间(秒) / Sandbox idle timeout (seconds)
511
+ nas_config: NAS 配置 / NAS configuration
512
+ oss_mount_config: OSS 挂载配置 / OSS mount configuration
513
+ polar_fs_config: PolarFS 配置 / PolarFS configuration
514
+ config: 配置对象 / Config object
515
+
516
+ Returns:
517
+ Sandbox: 创建的 Sandbox 对象 / Created Sandbox object
518
+
519
+ Raises:
520
+ ClientError: 客户端错误 / Client error
521
+ ServerError: 服务器错误 / Server error
522
+ """
523
+ # 将配置对象转换为字典格式
524
+ nas_config_dict: Optional[Dict[str, Any]] = None
525
+ if nas_config is not None:
526
+ nas_config_dict = nas_config.model_dump(by_alias=True)
527
+
528
+ oss_mount_config_dict: Optional[Dict[str, Any]] = None
529
+ if oss_mount_config is not None:
530
+ oss_mount_config_dict = oss_mount_config.model_dump(by_alias=True)
531
+
532
+ polar_fs_config_dict: Optional[Dict[str, Any]] = None
533
+ if polar_fs_config is not None:
534
+ polar_fs_config_dict = polar_fs_config.model_dump(by_alias=True)
535
+
536
+ result = await self.__sandbox_data_api.create_sandbox_async(
537
+ template_name=template_name,
538
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
539
+ nas_config=nas_config_dict,
540
+ oss_mount_config=oss_mount_config_dict,
541
+ polar_fs_config=polar_fs_config_dict,
542
+ config=config,
543
+ )
544
+
545
+ # 判断返回结果是否成功
546
+ if result.get("code") != "SUCCESS":
547
+ raise ClientError(
548
+ status_code=0,
549
+ message=(
550
+ "Failed to create sandbox:"
551
+ f" {result.get('message', 'Unknown error')}"
552
+ ),
553
+ )
554
+
555
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
556
+ data = result.get("data", {})
557
+ return Sandbox.model_validate(data, by_alias=True)
558
+
559
+ def create_sandbox(
560
+ self,
561
+ template_name: str,
562
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
563
+ nas_config: Optional[NASConfig] = None,
564
+ oss_mount_config: Optional[OSSMountConfig] = None,
565
+ polar_fs_config: Optional[PolarFsConfig] = None,
566
+ config: Optional[Config] = None,
567
+ ) -> Sandbox:
568
+ """创建 Sandbox(同步) / Create Sandbox (async)
569
+
570
+ Args:
571
+ template_name: 模板名称 / Template name
572
+ sandbox_idle_timeout_seconds: 沙箱空闲超时时间(秒) / Sandbox idle timeout (seconds)
573
+ nas_config: NAS 配置 / NAS configuration
574
+ oss_mount_config: OSS 挂载配置 / OSS mount configuration
575
+ polar_fs_config: PolarFS 配置 / PolarFS configuration
576
+ config: 配置对象 / Config object
577
+
578
+ Returns:
579
+ Sandbox: 创建的 Sandbox 对象 / Created Sandbox object
580
+
581
+ Raises:
582
+ ClientError: 客户端错误 / Client error
583
+ ServerError: 服务器错误 / Server error
584
+ """
585
+ # 将配置对象转换为字典格式
586
+ nas_config_dict: Optional[Dict[str, Any]] = None
587
+ if nas_config is not None:
588
+ nas_config_dict = nas_config.model_dump(by_alias=True)
589
+
590
+ oss_mount_config_dict: Optional[Dict[str, Any]] = None
591
+ if oss_mount_config is not None:
592
+ oss_mount_config_dict = oss_mount_config.model_dump(by_alias=True)
593
+
594
+ polar_fs_config_dict: Optional[Dict[str, Any]] = None
595
+ if polar_fs_config is not None:
596
+ polar_fs_config_dict = polar_fs_config.model_dump(by_alias=True)
597
+
598
+ result = self.__sandbox_data_api.create_sandbox(
599
+ template_name=template_name,
600
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
601
+ nas_config=nas_config_dict,
602
+ oss_mount_config=oss_mount_config_dict,
603
+ polar_fs_config=polar_fs_config_dict,
604
+ config=config,
605
+ )
606
+
607
+ # 判断返回结果是否成功
608
+ if result.get("code") != "SUCCESS":
609
+ raise ClientError(
610
+ status_code=0,
611
+ message=(
612
+ "Failed to create sandbox:"
613
+ f" {result.get('message', 'Unknown error')}"
614
+ ),
615
+ )
616
+
617
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
618
+ data = result.get("data", {})
619
+ return Sandbox.model_validate(data, by_alias=True)
620
+
621
+ async def stop_sandbox_async(
622
+ self, sandbox_id: str, config: Optional[Config] = None
623
+ ) -> Sandbox:
624
+ """停止 Sandbox(异步)
625
+
626
+ Args:
627
+ sandbox_id: Sandbox ID
628
+ config: 配置对象
629
+
630
+ Returns:
631
+ Sandbox: 停止后的 Sandbox 对象
632
+
633
+ Raises:
634
+ ResourceNotExistError: Sandbox 不存在
635
+ ClientError: 客户端错误
636
+ ServerError: 服务器错误
637
+ """
638
+ try:
639
+ result = await self.__sandbox_data_api.stop_sandbox_async(
640
+ sandbox_id
641
+ )
642
+
643
+ # 判断返回结果是否成功
644
+ if result.get("code") != "SUCCESS":
645
+ raise ClientError(
646
+ status_code=0,
647
+ message=(
648
+ "Failed to stop sandbox:"
649
+ f" {result.get('message', 'Unknown error')}"
650
+ ),
651
+ )
652
+
653
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
654
+ data = result.get("data", {})
655
+ return Sandbox.model_validate(data, by_alias=True)
656
+ except ClientError as e:
657
+ if e.status_code == 404:
658
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
659
+ raise e
660
+
661
+ def stop_sandbox(
662
+ self, sandbox_id: str, config: Optional[Config] = None
663
+ ) -> Sandbox:
664
+ """停止 Sandbox(同步)
665
+
666
+ Args:
667
+ sandbox_id: Sandbox ID
668
+ config: 配置对象
669
+
670
+ Returns:
671
+ Sandbox: 停止后的 Sandbox 对象
672
+
673
+ Raises:
674
+ ResourceNotExistError: Sandbox 不存在
675
+ ClientError: 客户端错误
676
+ ServerError: 服务器错误
677
+ """
678
+ try:
679
+ result = self.__sandbox_data_api.stop_sandbox(sandbox_id)
680
+
681
+ # 判断返回结果是否成功
682
+ if result.get("code") != "SUCCESS":
683
+ raise ClientError(
684
+ status_code=0,
685
+ message=(
686
+ "Failed to stop sandbox:"
687
+ f" {result.get('message', 'Unknown error')}"
688
+ ),
689
+ )
690
+
691
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
692
+ data = result.get("data", {})
693
+ return Sandbox.model_validate(data, by_alias=True)
694
+ except ClientError as e:
695
+ if e.status_code == 404:
696
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
697
+ raise e
698
+
699
+ async def delete_sandbox_async(
700
+ self, sandbox_id: str, config: Optional[Config] = None
701
+ ) -> Sandbox:
702
+ """删除 Sandbox(异步)
703
+
704
+ Args:
705
+ sandbox_id: Sandbox ID
706
+ config: 配置对象
707
+
708
+ Returns:
709
+ Sandbox: 停止后的 Sandbox 对象
710
+
711
+ Raises:
712
+ ResourceNotExistError: Sandbox 不存在
713
+ ClientError: 客户端错误
714
+ ServerError: 服务器错误
715
+ """
716
+ try:
717
+ result = await self.__sandbox_data_api.delete_sandbox_async(
718
+ sandbox_id
719
+ )
720
+
721
+ # 判断返回结果是否成功
722
+ if result.get("code") != "SUCCESS":
723
+ raise ClientError(
724
+ status_code=0,
725
+ message=(
726
+ "Failed to stop sandbox:"
727
+ f" {result.get('message', 'Unknown error')}"
728
+ ),
729
+ )
730
+
731
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
732
+ data = result.get("data", {})
733
+ return Sandbox.model_validate(data, by_alias=True)
734
+ except ClientError as e:
735
+ if e.status_code == 404:
736
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
737
+ raise e
738
+
739
+ def delete_sandbox(
740
+ self, sandbox_id: str, config: Optional[Config] = None
741
+ ) -> Sandbox:
742
+ """删除 Sandbox(同步)
743
+
744
+ Args:
745
+ sandbox_id: Sandbox ID
746
+ config: 配置对象
747
+
748
+ Returns:
749
+ Sandbox: 停止后的 Sandbox 对象
750
+
751
+ Raises:
752
+ ResourceNotExistError: Sandbox 不存在
753
+ ClientError: 客户端错误
754
+ ServerError: 服务器错误
755
+ """
756
+ try:
757
+ result = self.__sandbox_data_api.delete_sandbox(sandbox_id)
758
+
759
+ # 判断返回结果是否成功
760
+ if result.get("code") != "SUCCESS":
761
+ raise ClientError(
762
+ status_code=0,
763
+ message=(
764
+ "Failed to stop sandbox:"
765
+ f" {result.get('message', 'Unknown error')}"
766
+ ),
767
+ )
768
+
769
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
770
+ data = result.get("data", {})
771
+ return Sandbox.model_validate(data, by_alias=True)
772
+ except ClientError as e:
773
+ if e.status_code == 404:
774
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
775
+ raise e
776
+
777
+ async def get_sandbox_async(
778
+ self,
779
+ sandbox_id: str,
780
+ config: Optional[Config] = None,
781
+ ) -> Sandbox:
782
+ """获取 Sandbox(异步)
783
+
784
+ Args:
785
+ sandbox_id: Sandbox ID
786
+ config: 配置对象
787
+
788
+ Returns:
789
+ Sandbox: Sandbox 对象
790
+
791
+ Raises:
792
+ ResourceNotExistError: Sandbox 不存在
793
+ ClientError: 客户端错误
794
+ ServerError: 服务器错误
795
+ """
796
+ try:
797
+ result = await self.__sandbox_data_api.get_sandbox_async(sandbox_id)
798
+
799
+ # 判断返回结果是否成功
800
+ if result.get("code") != "SUCCESS":
801
+ raise ClientError(
802
+ status_code=0,
803
+ message=(
804
+ "Failed to get sandbox:"
805
+ f" {result.get('message', 'Unknown error')}"
806
+ ),
807
+ )
808
+
809
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
810
+ data = result.get("data", {})
811
+ return Sandbox.model_validate(data, by_alias=True)
812
+ except ClientError as e:
813
+ if e.status_code == 404:
814
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
815
+ raise e
816
+
817
+ def get_sandbox(
818
+ self,
819
+ sandbox_id: str,
820
+ config: Optional[Config] = None,
821
+ ) -> Sandbox:
822
+ """获取 Sandbox(同步)
823
+
824
+ Args:
825
+ sandbox_id: Sandbox ID
826
+ config: 配置对象
827
+
828
+ Returns:
829
+ Sandbox: Sandbox 对象
830
+
831
+ Raises:
832
+ ResourceNotExistError: Sandbox 不存在
833
+ ClientError: 客户端错误
834
+ ServerError: 服务器错误
835
+ """
836
+ try:
837
+ result = self.__sandbox_data_api.get_sandbox(sandbox_id)
838
+
839
+ # 判断返回结果是否成功
840
+ if result.get("code") != "SUCCESS":
841
+ raise ClientError(
842
+ status_code=0,
843
+ message=(
844
+ "Failed to get sandbox:"
845
+ f" {result.get('message', 'Unknown error')}"
846
+ ),
847
+ )
848
+
849
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
850
+ data = result.get("data", {})
851
+ return Sandbox.model_validate(data, by_alias=True)
852
+ except ClientError as e:
853
+ if e.status_code == 404:
854
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
855
+ raise e
856
+
857
+ async def list_sandboxes_async(
858
+ self,
859
+ input: Optional[ListSandboxesInput] = None,
860
+ config: Optional[Config] = None,
861
+ ) -> ListSandboxesOutput:
862
+ """枚举 Sandboxes(异步)
863
+
864
+ Args:
865
+ input: 分页配置
866
+ config: 配置对象
867
+
868
+ Returns:
869
+ List[Sandbox]: Sandbox 列表
870
+
871
+ Raises:
872
+ ClientError: 客户端错误
873
+ ServerError: 服务器错误
874
+ """
875
+ if input is None:
876
+ input = ListSandboxesInput()
877
+
878
+ # 转换为 SDK 需要的格式
879
+ sdk_input = ListSandboxesRequest().from_map(
880
+ input.model_dump(by_alias=True)
881
+ )
882
+
883
+ results = await self.__control_api.list_sandboxes_async(
884
+ sdk_input, config=config
885
+ )
886
+ return ListSandboxesOutput(
887
+ sandboxes=[
888
+ Sandbox.from_inner_object(item) for item in results.items
889
+ ],
890
+ next_token=results.next_token,
891
+ )
892
+
893
+ def list_sandboxes(
894
+ self,
895
+ input: Optional[ListSandboxesInput] = None,
896
+ config: Optional[Config] = None,
897
+ ) -> ListSandboxesOutput:
898
+ """枚举 Sandboxes(同步)
899
+
900
+ Args:
901
+ input: 分页配置
902
+ config: 配置对象
903
+
904
+ Returns:
905
+ List[Sandbox]: Sandbox 列表
906
+
907
+ Raises:
908
+ ClientError: 客户端错误
909
+ ServerError: 服务器错误
910
+ """
911
+ if input is None:
912
+ input = ListSandboxesInput()
913
+
914
+ # 转换为 SDK 需要的格式
915
+ sdk_input = ListSandboxesRequest().from_map(
916
+ input.model_dump(by_alias=True)
917
+ )
918
+
919
+ results = self.__control_api.list_sandboxes(sdk_input, config=config)
920
+ return ListSandboxesOutput(
921
+ sandboxes=[
922
+ Sandbox.from_inner_object(item) for item in results.items
923
+ ],
924
+ next_token=results.next_token,
925
+ )