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,466 @@
1
+ """Sandbox客户端模板 / Sandbox Client Template
2
+
3
+ 此模板用于生成沙箱客户端代码。
4
+ This template is used to generate sandbox client code.
5
+ """
6
+
7
+ import time
8
+ from typing import List, Optional, TYPE_CHECKING
9
+
10
+ from alibabacloud_agentrun20250910.models import (
11
+ CreateTemplateInput,
12
+ ListSandboxesRequest,
13
+ ListTemplatesRequest,
14
+ UpdateTemplateInput,
15
+ )
16
+
17
+ from agentrun.sandbox.api import SandboxControlAPI, SandboxDataAPI
18
+ from agentrun.sandbox.model import (
19
+ ListSandboxesInput,
20
+ ListSandboxesOutput,
21
+ PageableInput,
22
+ TemplateInput,
23
+ )
24
+ from agentrun.utils.config import Config
25
+ from agentrun.utils.exception import (
26
+ AgentRunError,
27
+ ClientError,
28
+ ResourceNotExistError,
29
+ )
30
+
31
+ from .sandbox import Sandbox
32
+
33
+ if TYPE_CHECKING:
34
+ from agentrun.sandbox.template import Template
35
+
36
+
37
+ class SandboxClient:
38
+ """Sandbox 客户端 / Sandbox Client
39
+
40
+ 用于管理 Sandbox 和 Template。
41
+ Used for managing Sandboxes and Templates.
42
+ """
43
+
44
+ def __init__(self, config: Optional[Config] = None):
45
+ """初始化 Sandbox 客户端 / Initialize Sandbox client
46
+
47
+ Args:
48
+ config: 配置对象,可选 / Configuration object, optional
49
+ """
50
+ self.__control_api = SandboxControlAPI(config=config)
51
+ self.__sandbox_data_api = SandboxDataAPI(config=config)
52
+
53
+ async def _wait_template_ready_async(
54
+ self,
55
+ template_name: str,
56
+ config: Optional[Config] = None,
57
+ interval_seconds: int = 5,
58
+ timeout_seconds: int = 300,
59
+ ) -> "Template":
60
+ """Wait for Template to be ready (async)
61
+
62
+ Args:
63
+ template_name: Template name
64
+ config: Config object
65
+ interval_seconds: Polling interval in seconds
66
+ timeout_seconds: Timeout in seconds
67
+
68
+ Returns:
69
+ Template: Ready Template object
70
+
71
+ Raises:
72
+ TimeoutError: Timeout error
73
+ ClientError: Client error
74
+ """
75
+ import asyncio
76
+
77
+ start_time = time.time()
78
+ while True:
79
+ template = await self.get_template_async(
80
+ template_name, config=config
81
+ )
82
+
83
+ # Check if ready
84
+ if template.status == "READY":
85
+ return template
86
+
87
+ # Check if failed
88
+ if (
89
+ template.status == "CREATE_FAILED"
90
+ or template.status == "UPDATE_FAILED"
91
+ ):
92
+ raise AgentRunError(
93
+ f"Template {template_name} creation failed, status:"
94
+ f" {template.status}"
95
+ )
96
+
97
+ # Check timeout
98
+ if time.time() - start_time > timeout_seconds:
99
+ raise TimeoutError(
100
+ f"Timeout waiting for Template {template_name} to be ready,"
101
+ f" current status: {template.status}"
102
+ )
103
+
104
+ await asyncio.sleep(interval_seconds)
105
+
106
+ async def create_template_async(
107
+ self, input: TemplateInput, config: Optional[Config] = None
108
+ ) -> "Template":
109
+ """创建 Template(异步)
110
+
111
+ Args:
112
+ input: Template 配置
113
+ config: 配置对象
114
+
115
+ Returns:
116
+ Template: 创建的 Template 对象
117
+
118
+ Raises:
119
+ ClientError: 客户端错误
120
+ ServerError: 服务器错误
121
+ """
122
+ from agentrun.sandbox.template import Template
123
+
124
+ # 转换为 SDK 需要的格式
125
+ sdk_input = CreateTemplateInput().from_map(
126
+ input.model_dump(by_alias=True)
127
+ )
128
+ result = await self.__control_api.create_template_async(
129
+ sdk_input, config=config
130
+ )
131
+ template = Template.from_inner_object(result)
132
+
133
+ # Poll and wait for Template to be ready
134
+ template = await self._wait_template_ready_async(
135
+ template.template_name or "", config=config
136
+ )
137
+
138
+ return template
139
+
140
+ async def delete_template_async(
141
+ self, template_name: str, config: Optional[Config] = None
142
+ ) -> "Template":
143
+ """删除 Template(异步)
144
+
145
+ Args:
146
+ template_name: Template 名称
147
+ config: 配置对象
148
+
149
+ Returns:
150
+ Template: 删除的 Template 对象
151
+
152
+ Raises:
153
+ ResourceNotExistError: Template 不存在
154
+ ClientError: 客户端错误
155
+ ServerError: 服务器错误
156
+ """
157
+ from agentrun.sandbox.template import Template
158
+
159
+ try:
160
+ result = await self.__control_api.delete_template_async(
161
+ template_name, config=config
162
+ )
163
+ return Template.from_inner_object(result)
164
+ except ClientError as e:
165
+ if e.status_code == 404:
166
+ raise ResourceNotExistError("Template", template_name) from e
167
+ raise e
168
+
169
+ async def update_template_async(
170
+ self,
171
+ template_name: str,
172
+ input: TemplateInput,
173
+ config: Optional[Config] = None,
174
+ ) -> "Template":
175
+ """更新 Template(异步)
176
+
177
+ Args:
178
+ template_name: Template 名称
179
+ input: Template 更新配置
180
+ config: 配置对象
181
+
182
+ Returns:
183
+ Template: 更新后的 Template 对象
184
+
185
+ Raises:
186
+ ResourceNotExistError: Template 不存在
187
+ ClientError: 客户端错误
188
+ ServerError: 服务器错误
189
+ """
190
+ from agentrun.sandbox.template import Template
191
+
192
+ try:
193
+ # 转换为 SDK 需要的格式
194
+ sdk_input = UpdateTemplateInput().from_map(
195
+ input.model_dump(by_alias=True, exclude_none=True)
196
+ )
197
+ result = await self.__control_api.update_template_async(
198
+ template_name, sdk_input, config=config
199
+ )
200
+ return Template.from_inner_object(result)
201
+ except ClientError as e:
202
+ if e.status_code == 404:
203
+ raise ResourceNotExistError("Template", template_name) from e
204
+ raise e
205
+
206
+ async def get_template_async(
207
+ self, template_name: str, config: Optional[Config] = None
208
+ ) -> "Template":
209
+ """获取 Template(异步)
210
+
211
+ Args:
212
+ template_name: Template 名称
213
+ config: 配置对象
214
+
215
+ Returns:
216
+ Template: Template 对象
217
+
218
+ Raises:
219
+ ResourceNotExistError: Template 不存在
220
+ ClientError: 客户端错误
221
+ ServerError: 服务器错误
222
+ """
223
+ from agentrun.sandbox.template import Template
224
+
225
+ try:
226
+ result = await self.__control_api.get_template_async(
227
+ template_name, config=config
228
+ )
229
+ return Template.from_inner_object(result)
230
+ except ClientError as e:
231
+ if e.status_code == 404:
232
+ raise ResourceNotExistError("Template", template_name) from e
233
+ raise e
234
+
235
+ async def list_templates_async(
236
+ self,
237
+ input: Optional[PageableInput] = None,
238
+ config: Optional[Config] = None,
239
+ ) -> List["Template"]:
240
+ """枚举 Templates(异步)
241
+
242
+ Args:
243
+ input: 分页配置
244
+ config: 配置对象
245
+
246
+ Returns:
247
+ List[Template]: Template 列表
248
+
249
+ Raises:
250
+ ClientError: 客户端错误
251
+ ServerError: 服务器错误
252
+ TimeoutError: Timeout waiting for Template to be ready
253
+ """
254
+ from agentrun.sandbox.template import Template
255
+
256
+ if input is None:
257
+ input = PageableInput()
258
+
259
+ # 转换为 SDK 需要的格式
260
+ sdk_input = ListTemplatesRequest().from_map(
261
+ input.model_dump(by_alias=True)
262
+ )
263
+ results = await self.__control_api.list_templates_async(
264
+ sdk_input, config=config
265
+ )
266
+ return (
267
+ [Template.from_inner_object(item) for item in results.items]
268
+ if results.items
269
+ else []
270
+ )
271
+
272
+ async def create_sandbox_async(
273
+ self,
274
+ template_name: str,
275
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
276
+ config: Optional[Config] = None,
277
+ ) -> Sandbox:
278
+ """创建 Sandbox(异步)
279
+
280
+ Args:
281
+ input: Sandbox 配置
282
+ config: 配置对象
283
+
284
+ Returns:
285
+ Sandbox: 创建的 Sandbox 对象
286
+
287
+ Raises:
288
+ ClientError: 客户端错误
289
+ ServerError: 服务器错误
290
+ """
291
+
292
+ result = await self.__sandbox_data_api.create_sandbox_async(
293
+ template_name=template_name,
294
+ sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
295
+ config=config,
296
+ )
297
+
298
+ # 判断返回结果是否成功
299
+ if result.get("code") != "SUCCESS":
300
+ raise ClientError(
301
+ status_code=0,
302
+ message=(
303
+ "Failed to create sandbox:"
304
+ f" {result.get('message', 'Unknown error')}"
305
+ ),
306
+ )
307
+
308
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
309
+ data = result.get("data", {})
310
+ return Sandbox.model_validate(data, by_alias=True)
311
+
312
+ async def stop_sandbox_async(
313
+ self, sandbox_id: str, config: Optional[Config] = None
314
+ ) -> Sandbox:
315
+ """停止 Sandbox(异步)
316
+
317
+ Args:
318
+ sandbox_id: Sandbox ID
319
+ config: 配置对象
320
+
321
+ Returns:
322
+ Sandbox: 停止后的 Sandbox 对象
323
+
324
+ Raises:
325
+ ResourceNotExistError: Sandbox 不存在
326
+ ClientError: 客户端错误
327
+ ServerError: 服务器错误
328
+ """
329
+ try:
330
+ result = await self.__sandbox_data_api.stop_sandbox_async(
331
+ sandbox_id
332
+ )
333
+
334
+ # 判断返回结果是否成功
335
+ if result.get("code") != "SUCCESS":
336
+ raise ClientError(
337
+ status_code=0,
338
+ message=(
339
+ "Failed to stop sandbox:"
340
+ f" {result.get('message', 'Unknown error')}"
341
+ ),
342
+ )
343
+
344
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
345
+ data = result.get("data", {})
346
+ return Sandbox.model_validate(data, by_alias=True)
347
+ except ClientError as e:
348
+ if e.status_code == 404:
349
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
350
+ raise e
351
+
352
+ async def delete_sandbox_async(
353
+ self, sandbox_id: str, config: Optional[Config] = None
354
+ ) -> Sandbox:
355
+ """删除 Sandbox(异步)
356
+
357
+ Args:
358
+ sandbox_id: Sandbox ID
359
+ config: 配置对象
360
+
361
+ Returns:
362
+ Sandbox: 停止后的 Sandbox 对象
363
+
364
+ Raises:
365
+ ResourceNotExistError: Sandbox 不存在
366
+ ClientError: 客户端错误
367
+ ServerError: 服务器错误
368
+ """
369
+ try:
370
+ result = await self.__sandbox_data_api.delete_sandbox_async(
371
+ sandbox_id
372
+ )
373
+
374
+ # 判断返回结果是否成功
375
+ if result.get("code") != "SUCCESS":
376
+ raise ClientError(
377
+ status_code=0,
378
+ message=(
379
+ "Failed to stop sandbox:"
380
+ f" {result.get('message', 'Unknown error')}"
381
+ ),
382
+ )
383
+
384
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
385
+ data = result.get("data", {})
386
+ return Sandbox.model_validate(data, by_alias=True)
387
+ except ClientError as e:
388
+ if e.status_code == 404:
389
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
390
+ raise e
391
+
392
+ async def get_sandbox_async(
393
+ self,
394
+ sandbox_id: str,
395
+ config: Optional[Config] = None,
396
+ ) -> Sandbox:
397
+ """获取 Sandbox(异步)
398
+
399
+ Args:
400
+ sandbox_id: Sandbox ID
401
+ config: 配置对象
402
+
403
+ Returns:
404
+ Sandbox: Sandbox 对象
405
+
406
+ Raises:
407
+ ResourceNotExistError: Sandbox 不存在
408
+ ClientError: 客户端错误
409
+ ServerError: 服务器错误
410
+ """
411
+ try:
412
+ result = await self.__sandbox_data_api.get_sandbox_async(sandbox_id)
413
+
414
+ # 判断返回结果是否成功
415
+ if result.get("code") != "SUCCESS":
416
+ raise ClientError(
417
+ status_code=0,
418
+ message=(
419
+ "Failed to get sandbox:"
420
+ f" {result.get('message', 'Unknown error')}"
421
+ ),
422
+ )
423
+
424
+ # 从 data 字段中提取数据并实例化(使用 model_validate 从字典创建)
425
+ data = result.get("data", {})
426
+ return Sandbox.model_validate(data, by_alias=True)
427
+ except ClientError as e:
428
+ if e.status_code == 404:
429
+ raise ResourceNotExistError("Sandbox", sandbox_id) from e
430
+ raise e
431
+
432
+ async def list_sandboxes_async(
433
+ self,
434
+ input: Optional[ListSandboxesInput] = None,
435
+ config: Optional[Config] = None,
436
+ ) -> ListSandboxesOutput:
437
+ """枚举 Sandboxes(异步)
438
+
439
+ Args:
440
+ input: 分页配置
441
+ config: 配置对象
442
+
443
+ Returns:
444
+ List[Sandbox]: Sandbox 列表
445
+
446
+ Raises:
447
+ ClientError: 客户端错误
448
+ ServerError: 服务器错误
449
+ """
450
+ if input is None:
451
+ input = ListSandboxesInput()
452
+
453
+ # 转换为 SDK 需要的格式
454
+ sdk_input = ListSandboxesRequest().from_map(
455
+ input.model_dump(by_alias=True)
456
+ )
457
+
458
+ results = await self.__control_api.list_sandboxes_async(
459
+ sdk_input, config=config
460
+ )
461
+ return ListSandboxesOutput(
462
+ sandboxes=[
463
+ Sandbox.from_inner_object(item) for item in results.items
464
+ ],
465
+ next_token=results.next_token,
466
+ )