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.
- agentrun/__init__.py +209 -0
- agentrun/agent_runtime/__client_async_template.py +466 -0
- agentrun/agent_runtime/__endpoint_async_template.py +345 -0
- agentrun/agent_runtime/__init__.py +53 -0
- agentrun/agent_runtime/__runtime_async_template.py +477 -0
- agentrun/agent_runtime/api/__data_async_template.py +58 -0
- agentrun/agent_runtime/api/__init__.py +6 -0
- agentrun/agent_runtime/api/control.py +1362 -0
- agentrun/agent_runtime/api/data.py +98 -0
- agentrun/agent_runtime/client.py +868 -0
- agentrun/agent_runtime/endpoint.py +649 -0
- agentrun/agent_runtime/model.py +362 -0
- agentrun/agent_runtime/runtime.py +904 -0
- agentrun/credential/__client_async_template.py +177 -0
- agentrun/credential/__credential_async_template.py +216 -0
- agentrun/credential/__init__.py +28 -0
- agentrun/credential/api/__init__.py +5 -0
- agentrun/credential/api/control.py +606 -0
- agentrun/credential/client.py +319 -0
- agentrun/credential/credential.py +381 -0
- agentrun/credential/model.py +248 -0
- agentrun/integration/__init__.py +21 -0
- agentrun/integration/agentscope/__init__.py +12 -0
- agentrun/integration/agentscope/adapter.py +17 -0
- agentrun/integration/agentscope/builtin.py +65 -0
- agentrun/integration/agentscope/message_adapter.py +185 -0
- agentrun/integration/agentscope/model_adapter.py +60 -0
- agentrun/integration/agentscope/tool_adapter.py +59 -0
- agentrun/integration/builtin/__init__.py +16 -0
- agentrun/integration/builtin/model.py +97 -0
- agentrun/integration/builtin/sandbox.py +276 -0
- agentrun/integration/builtin/toolset.py +47 -0
- agentrun/integration/crewai/__init__.py +12 -0
- agentrun/integration/crewai/adapter.py +9 -0
- agentrun/integration/crewai/builtin.py +65 -0
- agentrun/integration/crewai/model_adapter.py +27 -0
- agentrun/integration/crewai/tool_adapter.py +26 -0
- agentrun/integration/google_adk/__init__.py +12 -0
- agentrun/integration/google_adk/adapter.py +15 -0
- agentrun/integration/google_adk/builtin.py +65 -0
- agentrun/integration/google_adk/message_adapter.py +144 -0
- agentrun/integration/google_adk/model_adapter.py +43 -0
- agentrun/integration/google_adk/tool_adapter.py +25 -0
- agentrun/integration/langchain/__init__.py +9 -0
- agentrun/integration/langchain/adapter.py +15 -0
- agentrun/integration/langchain/builtin.py +71 -0
- agentrun/integration/langchain/message_adapter.py +141 -0
- agentrun/integration/langchain/model_adapter.py +37 -0
- agentrun/integration/langchain/tool_adapter.py +50 -0
- agentrun/integration/langgraph/__init__.py +13 -0
- agentrun/integration/langgraph/adapter.py +20 -0
- agentrun/integration/langgraph/builtin.py +65 -0
- agentrun/integration/pydantic_ai/__init__.py +12 -0
- agentrun/integration/pydantic_ai/adapter.py +13 -0
- agentrun/integration/pydantic_ai/builtin.py +65 -0
- agentrun/integration/pydantic_ai/model_adapter.py +44 -0
- agentrun/integration/pydantic_ai/tool_adapter.py +19 -0
- agentrun/integration/utils/__init__.py +112 -0
- agentrun/integration/utils/adapter.py +167 -0
- agentrun/integration/utils/canonical.py +157 -0
- agentrun/integration/utils/converter.py +134 -0
- agentrun/integration/utils/model.py +107 -0
- agentrun/integration/utils/tool.py +1714 -0
- agentrun/model/__client_async_template.py +357 -0
- agentrun/model/__init__.py +57 -0
- agentrun/model/__model_proxy_async_template.py +270 -0
- agentrun/model/__model_service_async_template.py +267 -0
- agentrun/model/api/__init__.py +6 -0
- agentrun/model/api/control.py +1173 -0
- agentrun/model/api/data.py +196 -0
- agentrun/model/client.py +674 -0
- agentrun/model/model.py +218 -0
- agentrun/model/model_proxy.py +439 -0
- agentrun/model/model_service.py +438 -0
- agentrun/sandbox/__browser_sandbox_async_template.py +113 -0
- agentrun/sandbox/__client_async_template.py +466 -0
- agentrun/sandbox/__code_interpreter_sandbox_async_template.py +466 -0
- agentrun/sandbox/__init__.py +54 -0
- agentrun/sandbox/__sandbox_async_template.py +398 -0
- agentrun/sandbox/__template_async_template.py +150 -0
- agentrun/sandbox/api/__browser_data_async_template.py +140 -0
- agentrun/sandbox/api/__code_interpreter_data_async_template.py +206 -0
- agentrun/sandbox/api/__init__.py +17 -0
- agentrun/sandbox/api/__sandbox_data_async_template.py +100 -0
- agentrun/sandbox/api/browser_data.py +172 -0
- agentrun/sandbox/api/code_interpreter_data.py +396 -0
- agentrun/sandbox/api/control.py +1051 -0
- agentrun/sandbox/api/playwright_async.py +492 -0
- agentrun/sandbox/api/playwright_sync.py +492 -0
- agentrun/sandbox/api/sandbox_data.py +140 -0
- agentrun/sandbox/browser_sandbox.py +191 -0
- agentrun/sandbox/client.py +878 -0
- agentrun/sandbox/code_interpreter_sandbox.py +829 -0
- agentrun/sandbox/model.py +269 -0
- agentrun/sandbox/sandbox.py +737 -0
- agentrun/sandbox/template.py +215 -0
- agentrun/server/__init__.py +82 -0
- agentrun/server/invoker.py +131 -0
- agentrun/server/model.py +225 -0
- agentrun/server/openai_protocol.py +798 -0
- agentrun/server/protocol.py +96 -0
- agentrun/server/server.py +192 -0
- agentrun/toolset/__client_async_template.py +62 -0
- agentrun/toolset/__init__.py +51 -0
- agentrun/toolset/__toolset_async_template.py +204 -0
- agentrun/toolset/api/__init__.py +17 -0
- agentrun/toolset/api/control.py +262 -0
- agentrun/toolset/api/mcp.py +100 -0
- agentrun/toolset/api/openapi.py +1184 -0
- agentrun/toolset/client.py +102 -0
- agentrun/toolset/model.py +160 -0
- agentrun/toolset/toolset.py +271 -0
- agentrun/utils/__data_api_async_template.py +715 -0
- agentrun/utils/__init__.py +5 -0
- agentrun/utils/__resource_async_template.py +158 -0
- agentrun/utils/config.py +258 -0
- agentrun/utils/control_api.py +78 -0
- agentrun/utils/data_api.py +1110 -0
- agentrun/utils/exception.py +149 -0
- agentrun/utils/helper.py +34 -0
- agentrun/utils/log.py +77 -0
- agentrun/utils/model.py +168 -0
- agentrun/utils/resource.py +291 -0
- agentrun_sdk-0.0.4.dist-info/METADATA +262 -0
- agentrun_sdk-0.0.4.dist-info/RECORD +128 -0
- agentrun_sdk-0.0.4.dist-info/WHEEL +5 -0
- agentrun_sdk-0.0.4.dist-info/licenses/LICENSE +201 -0
- agentrun_sdk-0.0.4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,438 @@
|
|
|
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/model/__model_service_async_template.py
|
|
10
|
+
|
|
11
|
+
Model Service 高层 API / Model Service High-Level API
|
|
12
|
+
|
|
13
|
+
此模块定义模型服务资源的高级API。
|
|
14
|
+
This module defines the high-level API for model service resources.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from typing import List, Optional
|
|
18
|
+
|
|
19
|
+
from agentrun.model.api.data import BaseInfo, ModelCompletionAPI
|
|
20
|
+
from agentrun.utils.config import Config
|
|
21
|
+
from agentrun.utils.model import PageableInput
|
|
22
|
+
from agentrun.utils.resource import ResourceBase
|
|
23
|
+
|
|
24
|
+
from .model import (
|
|
25
|
+
BackendType,
|
|
26
|
+
ModelServiceCreateInput,
|
|
27
|
+
ModelServiceImmutableProps,
|
|
28
|
+
ModelServiceListInput,
|
|
29
|
+
ModelServiceMutableProps,
|
|
30
|
+
ModelServicesSystemProps,
|
|
31
|
+
ModelServiceUpdateInput,
|
|
32
|
+
ModelType,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ModelService(
|
|
37
|
+
ModelServiceImmutableProps,
|
|
38
|
+
ModelServiceMutableProps,
|
|
39
|
+
ModelServicesSystemProps,
|
|
40
|
+
ResourceBase,
|
|
41
|
+
):
|
|
42
|
+
"""模型服务"""
|
|
43
|
+
|
|
44
|
+
@classmethod
|
|
45
|
+
def __get_client(cls):
|
|
46
|
+
from .client import ModelClient
|
|
47
|
+
|
|
48
|
+
return ModelClient()
|
|
49
|
+
|
|
50
|
+
@classmethod
|
|
51
|
+
async def create_async(
|
|
52
|
+
cls, input: ModelServiceCreateInput, config: Optional[Config] = None
|
|
53
|
+
):
|
|
54
|
+
"""创建模型服务(异步)
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
input: 模型服务输入参数
|
|
58
|
+
config: 配置
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
ModelService: 创建的模型服务对象
|
|
62
|
+
"""
|
|
63
|
+
return await cls.__get_client().create_async(input, config=config)
|
|
64
|
+
|
|
65
|
+
@classmethod
|
|
66
|
+
def create(
|
|
67
|
+
cls, input: ModelServiceCreateInput, config: Optional[Config] = None
|
|
68
|
+
):
|
|
69
|
+
"""创建模型服务(同步)
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
input: 模型服务输入参数
|
|
73
|
+
config: 配置
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
ModelService: 创建的模型服务对象
|
|
77
|
+
"""
|
|
78
|
+
return cls.__get_client().create(input, config=config)
|
|
79
|
+
|
|
80
|
+
@classmethod
|
|
81
|
+
async def delete_by_name_async(
|
|
82
|
+
cls, model_service_name: str, config: Optional[Config] = None
|
|
83
|
+
):
|
|
84
|
+
"""根据名称删除模型服务(异步)
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
model_service_name: 模型服务名称
|
|
88
|
+
config: 配置
|
|
89
|
+
"""
|
|
90
|
+
return await cls.__get_client().delete_async(
|
|
91
|
+
model_service_name, backend_type=BackendType.SERVICE, config=config
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
@classmethod
|
|
95
|
+
def delete_by_name(
|
|
96
|
+
cls, model_service_name: str, config: Optional[Config] = None
|
|
97
|
+
):
|
|
98
|
+
"""根据名称删除模型服务(同步)
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
model_service_name: 模型服务名称
|
|
102
|
+
config: 配置
|
|
103
|
+
"""
|
|
104
|
+
return cls.__get_client().delete(
|
|
105
|
+
model_service_name, backend_type=BackendType.SERVICE, config=config
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
@classmethod
|
|
109
|
+
async def update_by_name_async(
|
|
110
|
+
cls,
|
|
111
|
+
model_service_name: str,
|
|
112
|
+
input: ModelServiceUpdateInput,
|
|
113
|
+
config: Optional[Config] = None,
|
|
114
|
+
):
|
|
115
|
+
"""根据名称更新模型服务(异步)
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
model_service_name: 模型服务名称
|
|
119
|
+
input: 模型服务更新输入参数
|
|
120
|
+
config: 配置
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
ModelService: 更新后的模型服务对象
|
|
124
|
+
"""
|
|
125
|
+
return await cls.__get_client().update_async(
|
|
126
|
+
model_service_name, input, config=config
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
@classmethod
|
|
130
|
+
def update_by_name(
|
|
131
|
+
cls,
|
|
132
|
+
model_service_name: str,
|
|
133
|
+
input: ModelServiceUpdateInput,
|
|
134
|
+
config: Optional[Config] = None,
|
|
135
|
+
):
|
|
136
|
+
"""根据名称更新模型服务(同步)
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
model_service_name: 模型服务名称
|
|
140
|
+
input: 模型服务更新输入参数
|
|
141
|
+
config: 配置
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
ModelService: 更新后的模型服务对象
|
|
145
|
+
"""
|
|
146
|
+
return cls.__get_client().update(
|
|
147
|
+
model_service_name, input, config=config
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
@classmethod
|
|
151
|
+
async def get_by_name_async(
|
|
152
|
+
cls, model_service_name: str, config: Optional[Config] = None
|
|
153
|
+
):
|
|
154
|
+
"""根据名称获取模型服务(异步)
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
model_service_name: 模型服务名称
|
|
158
|
+
config: 配置
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
ModelService: 模型服务对象
|
|
162
|
+
"""
|
|
163
|
+
return await cls.__get_client().get_async(
|
|
164
|
+
model_service_name, backend_type=BackendType.SERVICE, config=config
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
@classmethod
|
|
168
|
+
def get_by_name(
|
|
169
|
+
cls, model_service_name: str, config: Optional[Config] = None
|
|
170
|
+
):
|
|
171
|
+
"""根据名称获取模型服务(同步)
|
|
172
|
+
|
|
173
|
+
Args:
|
|
174
|
+
model_service_name: 模型服务名称
|
|
175
|
+
config: 配置
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
ModelService: 模型服务对象
|
|
179
|
+
"""
|
|
180
|
+
return cls.__get_client().get(
|
|
181
|
+
model_service_name, backend_type=BackendType.SERVICE, config=config
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
@classmethod
|
|
185
|
+
async def _list_page_async(
|
|
186
|
+
cls, page_input: PageableInput, config: Config | None = None, **kwargs
|
|
187
|
+
):
|
|
188
|
+
return await cls.__get_client().list_async(
|
|
189
|
+
input=ModelServiceListInput(
|
|
190
|
+
**kwargs,
|
|
191
|
+
**page_input.model_dump(),
|
|
192
|
+
),
|
|
193
|
+
config=config,
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
@classmethod
|
|
197
|
+
def _list_page(
|
|
198
|
+
cls, page_input: PageableInput, config: Config | None = None, **kwargs
|
|
199
|
+
):
|
|
200
|
+
return cls.__get_client().list(
|
|
201
|
+
input=ModelServiceListInput(
|
|
202
|
+
**kwargs,
|
|
203
|
+
**page_input.model_dump(),
|
|
204
|
+
),
|
|
205
|
+
config=config,
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
@classmethod
|
|
209
|
+
async def list_all_async(
|
|
210
|
+
cls,
|
|
211
|
+
*,
|
|
212
|
+
model_type: Optional[ModelType] = None,
|
|
213
|
+
provider: Optional[str] = None,
|
|
214
|
+
config: Optional[Config] = None,
|
|
215
|
+
) -> List["ModelService"]:
|
|
216
|
+
return await cls._list_all_async(
|
|
217
|
+
lambda m: m.model_service_id or "",
|
|
218
|
+
config=config,
|
|
219
|
+
model_type=model_type,
|
|
220
|
+
provider=provider,
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
@classmethod
|
|
224
|
+
def list_all(
|
|
225
|
+
cls,
|
|
226
|
+
*,
|
|
227
|
+
model_type: Optional[ModelType] = None,
|
|
228
|
+
provider: Optional[str] = None,
|
|
229
|
+
config: Optional[Config] = None,
|
|
230
|
+
) -> List["ModelService"]:
|
|
231
|
+
return cls._list_all(
|
|
232
|
+
lambda m: m.model_service_id or "",
|
|
233
|
+
config=config,
|
|
234
|
+
model_type=model_type,
|
|
235
|
+
provider=provider,
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
async def update_async(
|
|
239
|
+
self, input: ModelServiceUpdateInput, config: Optional[Config] = None
|
|
240
|
+
):
|
|
241
|
+
"""更新模型服务(异步)
|
|
242
|
+
|
|
243
|
+
Args:
|
|
244
|
+
input: 模型服务更新输入参数
|
|
245
|
+
config: 配置
|
|
246
|
+
|
|
247
|
+
Returns:
|
|
248
|
+
ModelService: 更新后的模型服务对象
|
|
249
|
+
"""
|
|
250
|
+
if self.model_service_name is None:
|
|
251
|
+
raise ValueError(
|
|
252
|
+
"model_service_name is required to update a ModelService"
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
result = await self.update_by_name_async(
|
|
256
|
+
self.model_service_name, input, config=config
|
|
257
|
+
)
|
|
258
|
+
self.update_self(result)
|
|
259
|
+
|
|
260
|
+
return self
|
|
261
|
+
|
|
262
|
+
def update(
|
|
263
|
+
self, input: ModelServiceUpdateInput, config: Optional[Config] = None
|
|
264
|
+
):
|
|
265
|
+
"""更新模型服务(同步)
|
|
266
|
+
|
|
267
|
+
Args:
|
|
268
|
+
input: 模型服务更新输入参数
|
|
269
|
+
config: 配置
|
|
270
|
+
|
|
271
|
+
Returns:
|
|
272
|
+
ModelService: 更新后的模型服务对象
|
|
273
|
+
"""
|
|
274
|
+
if self.model_service_name is None:
|
|
275
|
+
raise ValueError(
|
|
276
|
+
"model_service_name is required to update a ModelService"
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
result = self.update_by_name(
|
|
280
|
+
self.model_service_name, input, config=config
|
|
281
|
+
)
|
|
282
|
+
self.update_self(result)
|
|
283
|
+
|
|
284
|
+
return self
|
|
285
|
+
|
|
286
|
+
async def delete_async(self, config: Optional[Config] = None):
|
|
287
|
+
"""删除模型服务(异步)
|
|
288
|
+
|
|
289
|
+
Args:
|
|
290
|
+
config: 配置
|
|
291
|
+
"""
|
|
292
|
+
if self.model_service_name is None:
|
|
293
|
+
raise ValueError(
|
|
294
|
+
"model_service_name is required to delete a ModelService"
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
return await self.delete_by_name_async(
|
|
298
|
+
self.model_service_name, config=config
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
def delete(self, config: Optional[Config] = None):
|
|
302
|
+
"""删除模型服务(同步)
|
|
303
|
+
|
|
304
|
+
Args:
|
|
305
|
+
config: 配置
|
|
306
|
+
"""
|
|
307
|
+
if self.model_service_name is None:
|
|
308
|
+
raise ValueError(
|
|
309
|
+
"model_service_name is required to delete a ModelService"
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
return self.delete_by_name(self.model_service_name, config=config)
|
|
313
|
+
|
|
314
|
+
async def get_async(self, config: Optional[Config] = None):
|
|
315
|
+
"""刷新模型服务信息(异步)
|
|
316
|
+
|
|
317
|
+
Args:
|
|
318
|
+
config: 配置
|
|
319
|
+
|
|
320
|
+
Returns:
|
|
321
|
+
ModelService: 刷新后的模型服务对象
|
|
322
|
+
"""
|
|
323
|
+
if self.model_service_name is None:
|
|
324
|
+
raise ValueError(
|
|
325
|
+
"model_service_name is required to refresh a ModelService"
|
|
326
|
+
)
|
|
327
|
+
|
|
328
|
+
result = await self.get_by_name_async(
|
|
329
|
+
self.model_service_name, config=config
|
|
330
|
+
)
|
|
331
|
+
self.update_self(result)
|
|
332
|
+
|
|
333
|
+
return self
|
|
334
|
+
|
|
335
|
+
def get(self, config: Optional[Config] = None):
|
|
336
|
+
"""刷新模型服务信息(同步)
|
|
337
|
+
|
|
338
|
+
Args:
|
|
339
|
+
config: 配置
|
|
340
|
+
|
|
341
|
+
Returns:
|
|
342
|
+
ModelService: 刷新后的模型服务对象
|
|
343
|
+
"""
|
|
344
|
+
if self.model_service_name is None:
|
|
345
|
+
raise ValueError(
|
|
346
|
+
"model_service_name is required to refresh a ModelService"
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
result = self.get_by_name(self.model_service_name, config=config)
|
|
350
|
+
self.update_self(result)
|
|
351
|
+
|
|
352
|
+
return self
|
|
353
|
+
|
|
354
|
+
async def refresh_async(self, config: Optional[Config] = None):
|
|
355
|
+
"""刷新模型服务信息(异步)
|
|
356
|
+
|
|
357
|
+
Args:
|
|
358
|
+
config: 配置
|
|
359
|
+
|
|
360
|
+
Returns:
|
|
361
|
+
ModelService: 刷新后的模型服务对象
|
|
362
|
+
"""
|
|
363
|
+
return await self.get_async(config=config)
|
|
364
|
+
|
|
365
|
+
def refresh(self, config: Optional[Config] = None):
|
|
366
|
+
"""刷新模型服务信息(同步)
|
|
367
|
+
|
|
368
|
+
Args:
|
|
369
|
+
config: 配置
|
|
370
|
+
|
|
371
|
+
Returns:
|
|
372
|
+
ModelService: 刷新后的模型服务对象
|
|
373
|
+
"""
|
|
374
|
+
return self.get(config=config)
|
|
375
|
+
|
|
376
|
+
def model_info(self, config: Optional[Config] = None) -> BaseInfo:
|
|
377
|
+
cfg = Config.with_configs(self._config, config)
|
|
378
|
+
|
|
379
|
+
assert self.provider_settings is not None
|
|
380
|
+
assert self.provider_settings.base_url is not None
|
|
381
|
+
|
|
382
|
+
api_key = self.provider_settings.api_key or ""
|
|
383
|
+
if not api_key and self.credential_name:
|
|
384
|
+
from agentrun.credential import Credential
|
|
385
|
+
|
|
386
|
+
credential = Credential.get_by_name(
|
|
387
|
+
self.credential_name, config=cfg
|
|
388
|
+
)
|
|
389
|
+
api_key = credential.credential_secret or ""
|
|
390
|
+
|
|
391
|
+
default_model = (
|
|
392
|
+
self.provider_settings.model_names[0]
|
|
393
|
+
if self.provider_settings.model_names is not None
|
|
394
|
+
and len(self.provider_settings.model_names) > 0
|
|
395
|
+
else None
|
|
396
|
+
)
|
|
397
|
+
|
|
398
|
+
return BaseInfo(
|
|
399
|
+
api_key=api_key,
|
|
400
|
+
base_url=self.provider_settings.base_url,
|
|
401
|
+
model=default_model,
|
|
402
|
+
headers=cfg.get_headers(),
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
def completions(
|
|
406
|
+
self,
|
|
407
|
+
messages: list,
|
|
408
|
+
model: Optional[str] = None,
|
|
409
|
+
stream: bool = False,
|
|
410
|
+
**kwargs,
|
|
411
|
+
):
|
|
412
|
+
info = self.model_info(config=kwargs.get("config"))
|
|
413
|
+
|
|
414
|
+
m = ModelCompletionAPI(
|
|
415
|
+
api_key=info.api_key or "",
|
|
416
|
+
base_url=info.base_url or "",
|
|
417
|
+
model=model or info.model or self.model_service_name or "",
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
return m.completions(**kwargs, messages=messages, stream=stream)
|
|
421
|
+
|
|
422
|
+
def responses(
|
|
423
|
+
self,
|
|
424
|
+
messages: list,
|
|
425
|
+
model: Optional[str] = None,
|
|
426
|
+
stream: bool = False,
|
|
427
|
+
**kwargs,
|
|
428
|
+
):
|
|
429
|
+
info = self.model_info(config=kwargs.get("config"))
|
|
430
|
+
|
|
431
|
+
m = ModelCompletionAPI(
|
|
432
|
+
api_key=info.api_key or "",
|
|
433
|
+
base_url=info.base_url or "",
|
|
434
|
+
model=model or info.model or self.model_service_name or "",
|
|
435
|
+
provider=(self.provider or "openai").lower(),
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
return m.responses(**kwargs, messages=messages, stream=stream)
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""浏览器沙箱高层API模板 / Browser Sandbox High-Level API Template
|
|
2
|
+
|
|
3
|
+
此模板用于生成浏览器沙箱资源的高级API代码。
|
|
4
|
+
This template is used to generate high-level API code for browser sandbox resources.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import time # noqa: F401
|
|
9
|
+
from typing import Optional
|
|
10
|
+
|
|
11
|
+
from agentrun.sandbox.api import BrowserDataAPI
|
|
12
|
+
from agentrun.sandbox.model import TemplateType
|
|
13
|
+
from agentrun.utils.log import logger
|
|
14
|
+
|
|
15
|
+
from .sandbox import Sandbox
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class BrowserSandbox(Sandbox):
|
|
19
|
+
_template_type = TemplateType.BROWSER
|
|
20
|
+
|
|
21
|
+
_data_api: Optional["BrowserDataAPI"] = None
|
|
22
|
+
|
|
23
|
+
async def __aenter__(self):
|
|
24
|
+
# Poll health check asynchronously
|
|
25
|
+
max_retries = 60 # Maximum 60 seconds
|
|
26
|
+
retry_count = 0
|
|
27
|
+
|
|
28
|
+
logger.debug("Waiting for browser to be ready...")
|
|
29
|
+
|
|
30
|
+
while retry_count < max_retries:
|
|
31
|
+
retry_count += 1
|
|
32
|
+
|
|
33
|
+
try:
|
|
34
|
+
health = await self.check_health_async()
|
|
35
|
+
|
|
36
|
+
if health["status"] == "ok":
|
|
37
|
+
logger.debug(
|
|
38
|
+
f"✓ Browser is ready! (took {retry_count} seconds)"
|
|
39
|
+
)
|
|
40
|
+
return self
|
|
41
|
+
|
|
42
|
+
logger.debug(
|
|
43
|
+
"[%d/%d] Health status: %d %s",
|
|
44
|
+
retry_count,
|
|
45
|
+
max_retries,
|
|
46
|
+
health.get("code"),
|
|
47
|
+
health.get("message"),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
except Exception as e:
|
|
51
|
+
logger.error(
|
|
52
|
+
f"[{retry_count}/{max_retries}] Health check failed: {e}"
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
if retry_count < max_retries:
|
|
56
|
+
await asyncio.sleep(1)
|
|
57
|
+
|
|
58
|
+
raise RuntimeError(
|
|
59
|
+
f"Health check timeout after {max_retries} seconds. "
|
|
60
|
+
"Browser did not become ready in time."
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
64
|
+
if self.sandbox_id is None:
|
|
65
|
+
raise ValueError("Sandbox ID is not set")
|
|
66
|
+
logger.debug(f"Deleting browser sandbox {self.sandbox_id}...")
|
|
67
|
+
await self.delete_async()
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def data_api(self):
|
|
71
|
+
if self._data_api is None:
|
|
72
|
+
if self.sandbox_id is None:
|
|
73
|
+
raise ValueError("Sandbox ID is not set")
|
|
74
|
+
|
|
75
|
+
self._data_api = BrowserDataAPI(
|
|
76
|
+
sandbox_id=self.sandbox_id, config=self._config
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
return self._data_api
|
|
80
|
+
|
|
81
|
+
async def check_health_async(self):
|
|
82
|
+
return await self.data_api.check_health_async()
|
|
83
|
+
|
|
84
|
+
def get_cdp_url(self, record: Optional[bool] = False):
|
|
85
|
+
return self.data_api.get_cdp_url(record=record)
|
|
86
|
+
|
|
87
|
+
def get_vnc_url(self, record: Optional[bool] = False):
|
|
88
|
+
return self.data_api.get_vnc_url(record=record)
|
|
89
|
+
|
|
90
|
+
def sync_playwright(self, record: Optional[bool] = False):
|
|
91
|
+
return self.data_api.sync_playwright(record=record)
|
|
92
|
+
|
|
93
|
+
def async_playwright(self, record: Optional[bool] = False):
|
|
94
|
+
return self.data_api.async_playwright(record=record)
|
|
95
|
+
|
|
96
|
+
async def list_recordings_async(self):
|
|
97
|
+
return await self.data_api.list_recordings_async()
|
|
98
|
+
|
|
99
|
+
async def download_recording_async(self, filename: str, save_path: str):
|
|
100
|
+
"""
|
|
101
|
+
Asynchronously download a recording video file and save it to local path.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
filename: The name of the recording file to download
|
|
105
|
+
save_path: Local file path to save the downloaded video file (.mkv)
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
Dictionary with 'saved_path' and 'size' keys
|
|
109
|
+
"""
|
|
110
|
+
return await self.data_api.download_recording_async(filename, save_path)
|
|
111
|
+
|
|
112
|
+
async def delete_recording_async(self, filename: str):
|
|
113
|
+
return await self.data_api.delete_recording_async(filename)
|