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