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,492 @@
1
+ """Playwright同步API封装 / Playwright Sync API Wrapper
2
+
3
+ 提供Playwright的同步API封装,用于浏览器沙箱操作。
4
+ Provides sync API wrapper for Playwright, used for browser sandbox operations.
5
+ """
6
+
7
+ # THIS FILE IS GENERATED BY playwright_async.py, DO NOT MODIFY THIS FILE
8
+
9
+ from pathlib import Path
10
+ from typing import Any, Dict, List, Literal, Optional, Sequence, Union
11
+
12
+ from playwright.sync_api import (
13
+ Browser,
14
+ BrowserContext,
15
+ FloatRect,
16
+ Locator,
17
+ Page,
18
+ Playwright,
19
+ Position,
20
+ Response,
21
+ sync_playwright,
22
+ )
23
+
24
+
25
+ class BrowserPlaywrightSync:
26
+ """A small helper wrapper around Playwright's Sync API.
27
+
28
+ This class connects to an existing Chromium instance over CDP and exposes a set
29
+ of common page operations. Internally it lazily ensures a `Browser`,
30
+ `BrowserContext`, and `Page` and keeps references to reuse them across calls.
31
+
32
+ Notes
33
+ -----
34
+ - Connection is established via CDP using the given `url`.
35
+ - If `auto_close_browser`/`auto_close_page` are enabled, `close()` will attempt
36
+ to close the browser/page respectively.
37
+ - Methods that act on the page automatically bring the page to the front.
38
+ """
39
+
40
+ def __init__(
41
+ self,
42
+ url: str,
43
+ browser_type: str = "chrome",
44
+ auto_close_browser: bool = False,
45
+ auto_close_page: bool = False,
46
+ headers: Optional[Dict[str, str]] = None,
47
+ ):
48
+ self.url = url
49
+ self.browser_type = browser_type
50
+ self.auto_close_browser = auto_close_browser
51
+ self.auto_close_page = auto_close_page
52
+ self.headers = headers
53
+
54
+ self._playwright = sync_playwright()
55
+ self._playwright_instance: Optional[Playwright] = None
56
+ self._browser: Optional[Browser] = None
57
+ self._context: Optional[BrowserContext] = None
58
+ self._page: Optional[Page] = None
59
+
60
+ def open(self) -> "BrowserPlaywrightSync":
61
+ """Establish a connection to the remote browser if not already connected.
62
+
63
+ Returns
64
+ -------
65
+ BrowserPlaywrightSync
66
+ The current instance for fluent chaining.
67
+ """
68
+ if self._browser:
69
+ return self
70
+
71
+ self._playwright_instance = self._playwright.start()
72
+ self._browser = self._playwright_instance.chromium.connect_over_cdp(
73
+ self.url, headers=self.headers
74
+ )
75
+
76
+ return self
77
+
78
+ def close(self) -> None:
79
+ """Close held resources according to the auto-close flags.
80
+
81
+ Closes page and/or browser if the corresponding auto-close flags are set
82
+ and stops the Playwright driver when present.
83
+ """
84
+ if self.auto_close_page and self._page:
85
+ self._page.close()
86
+
87
+ if self.auto_close_browser and self._browser:
88
+ self._browser.close()
89
+
90
+ if self._playwright_instance:
91
+ self._playwright_instance.stop()
92
+
93
+ def __enter__(self) -> "BrowserPlaywrightSync":
94
+ """Enter context by ensuring the connection is open.
95
+
96
+ Returns
97
+ -------
98
+ BrowserPlaywrightSync
99
+ The current instance.
100
+ """
101
+ return self.open()
102
+
103
+ def __exit__(self, *args: Any) -> None:
104
+ """Exit context by closing resources based on auto-close flags."""
105
+ self.close()
106
+
107
+ def ensure_browser(self) -> Browser:
108
+ """Ensure a `Browser` instance is available.
109
+
110
+ Returns
111
+ -------
112
+ Browser
113
+ A connected Playwright `Browser` instance.
114
+ """
115
+ if self._browser:
116
+ return self._browser
117
+
118
+ self.open()
119
+
120
+ assert self._browser is not None
121
+
122
+ return self._browser
123
+
124
+ def ensure_context(self) -> BrowserContext:
125
+ """Ensure a `BrowserContext` is available, creating one if necessary.
126
+
127
+ Returns
128
+ -------
129
+ BrowserContext
130
+ The ensured `BrowserContext`.
131
+ """
132
+ browser = self.ensure_browser()
133
+
134
+ if self._context:
135
+ return self._context
136
+
137
+ if len(browser.contexts) > 0:
138
+ self._context = browser.contexts[0]
139
+ else:
140
+ self._context = browser.new_context()
141
+
142
+ return self._context
143
+
144
+ def ensure_page(self) -> Page:
145
+ """Ensure a `Page` is available in the current context.
146
+
147
+ Returns
148
+ -------
149
+ Page
150
+ The ensured `Page` which is brought to the front.
151
+ """
152
+ ctx = self.ensure_context()
153
+
154
+ if self._page:
155
+ self._page.bring_to_front()
156
+ return self._page
157
+
158
+ if len(ctx.pages) > 0:
159
+ self._page = ctx.pages[0]
160
+ else:
161
+ self._page = ctx.new_page()
162
+
163
+ self._page.bring_to_front()
164
+ return self._page
165
+
166
+ def _use_page(self, page: Page) -> Page:
167
+ """Set the active page and context.
168
+
169
+ Parameters
170
+ ----------
171
+ page : Page
172
+ The page to make active.
173
+
174
+ Returns
175
+ -------
176
+ Page
177
+ The provided page.
178
+ """
179
+ page.bring_to_front()
180
+ self._page = page
181
+ self._context = page.context
182
+ return self._page
183
+
184
+ def list_pages(self) -> List[Page]:
185
+ """List all pages across all contexts in the connected browser."""
186
+ pages: List[Page] = []
187
+
188
+ browser = self.ensure_browser()
189
+ for context in browser.contexts:
190
+ for page in context.pages:
191
+ pages.append(page)
192
+
193
+ return pages
194
+
195
+ def new_page(self) -> Page:
196
+ """Create and switch to a new page in the ensured context."""
197
+ context = self.ensure_context()
198
+ page = context.new_page()
199
+ return self._use_page(page)
200
+
201
+ def select_tab(self, index: int) -> Page:
202
+ """Select a page by index across all open tabs.
203
+
204
+ Parameters
205
+ ----------
206
+ index : int
207
+ Zero-based page index.
208
+
209
+ Returns
210
+ -------
211
+ Page
212
+ The selected page.
213
+
214
+ Raises
215
+ ------
216
+ IndexError
217
+ If the index is out of bounds.
218
+ """
219
+ pages = self.list_pages()
220
+ if 0 <= index < len(pages):
221
+ return self._use_page(pages[index])
222
+ else:
223
+ raise IndexError("Tab index out of range")
224
+
225
+ def goto(
226
+ self,
227
+ url: str,
228
+ timeout: Optional[float] = None,
229
+ wait_until: Optional[
230
+ Literal["commit", "domcontentloaded", "load", "networkidle"]
231
+ ] = None,
232
+ referer: Optional[str] = None,
233
+ ) -> Optional[Response]:
234
+ """Navigate to a URL on the active page.
235
+
236
+ Returns
237
+ -------
238
+ Optional[Response]
239
+ The main resource response if available; otherwise `None`.
240
+ """
241
+ page = self.ensure_page()
242
+ return page.goto(
243
+ url, timeout=timeout, wait_until=wait_until, referer=referer
244
+ )
245
+
246
+ def click(
247
+ self,
248
+ selector: str,
249
+ modifiers: Optional[
250
+ Sequence[
251
+ Literal["Alt", "Control", "ControlOrMeta", "Meta", "Shift"]
252
+ ]
253
+ ] = None,
254
+ position: Optional[Position] = None,
255
+ delay: Optional[float] = None,
256
+ button: Optional[Literal["left", "middle", "right"]] = None,
257
+ click_count: Optional[int] = None,
258
+ timeout: Optional[float] = None,
259
+ force: Optional[bool] = None,
260
+ no_wait_after: Optional[bool] = None,
261
+ trial: Optional[bool] = None,
262
+ strict: Optional[bool] = None,
263
+ ) -> None:
264
+ """Click an element matching the selector on the active page."""
265
+ page = self.ensure_page()
266
+ return page.click(
267
+ selector,
268
+ modifiers=modifiers,
269
+ position=position,
270
+ delay=delay,
271
+ button=button,
272
+ click_count=click_count,
273
+ timeout=timeout,
274
+ force=force,
275
+ no_wait_after=no_wait_after,
276
+ trial=trial,
277
+ strict=strict,
278
+ )
279
+
280
+ def drag_and_drop(
281
+ self,
282
+ source: str,
283
+ target: str,
284
+ source_position: Optional[Position] = None,
285
+ target_position: Optional[Position] = None,
286
+ force: Optional[bool] = None,
287
+ no_wait_after: Optional[bool] = None,
288
+ timeout: Optional[float] = None,
289
+ strict: Optional[bool] = None,
290
+ trial: Optional[bool] = None,
291
+ ) -> None:
292
+ """Alias for `drag` using Playwright's `drag_and_drop` under the hood."""
293
+ page = self.ensure_page()
294
+ return page.drag_and_drop(
295
+ source=source,
296
+ target=target,
297
+ source_position=source_position,
298
+ target_position=target_position,
299
+ force=force,
300
+ no_wait_after=no_wait_after,
301
+ timeout=timeout,
302
+ strict=strict,
303
+ trial=trial,
304
+ )
305
+
306
+ def dblclick(
307
+ self,
308
+ selector: str,
309
+ modifiers: Optional[
310
+ Sequence[
311
+ Literal["Alt", "Control", "ControlOrMeta", "Meta", "Shift"]
312
+ ]
313
+ ] = None,
314
+ position: Optional[Position] = None,
315
+ delay: Optional[float] = None,
316
+ button: Optional[Literal["left", "middle", "right"]] = None,
317
+ timeout: Optional[float] = None,
318
+ force: Optional[bool] = None,
319
+ no_wait_after: Optional[bool] = None,
320
+ strict: Optional[bool] = None,
321
+ trial: Optional[bool] = None,
322
+ ) -> None:
323
+ """Double-click an element matching the selector on the active page."""
324
+ page = self.ensure_page()
325
+ return page.dblclick(
326
+ selector=selector,
327
+ modifiers=modifiers,
328
+ position=position,
329
+ delay=delay,
330
+ button=button,
331
+ timeout=timeout,
332
+ force=force,
333
+ no_wait_after=no_wait_after,
334
+ strict=strict,
335
+ trial=trial,
336
+ )
337
+
338
+ def fill(
339
+ self,
340
+ selector: str,
341
+ value: str,
342
+ timeout: Optional[float] = None,
343
+ no_wait_after: Optional[bool] = None,
344
+ strict: Optional[bool] = None,
345
+ force: Optional[bool] = None,
346
+ ) -> None:
347
+ """Fill an input/textarea matched by selector with the provided value."""
348
+ page = self.ensure_page()
349
+ return page.fill(
350
+ selector=selector,
351
+ value=value,
352
+ timeout=timeout,
353
+ no_wait_after=no_wait_after,
354
+ strict=strict,
355
+ force=force,
356
+ )
357
+
358
+ def hover(
359
+ self,
360
+ selector: str,
361
+ modifiers: Optional[
362
+ Sequence[
363
+ Literal["Alt", "Control", "ControlOrMeta", "Meta", "Shift"]
364
+ ]
365
+ ] = None,
366
+ position: Optional[Position] = None,
367
+ timeout: Optional[float] = None,
368
+ no_wait_after: Optional[bool] = None,
369
+ force: Optional[bool] = None,
370
+ strict: Optional[bool] = None,
371
+ trial: Optional[bool] = None,
372
+ ) -> None:
373
+ """Hover over the element matched by the selector."""
374
+ page = self.ensure_page()
375
+ return page.hover(
376
+ selector=selector,
377
+ modifiers=modifiers,
378
+ position=position,
379
+ timeout=timeout,
380
+ no_wait_after=no_wait_after,
381
+ force=force,
382
+ strict=strict,
383
+ trial=trial,
384
+ )
385
+
386
+ def type(
387
+ self,
388
+ selector: str,
389
+ text: str,
390
+ delay: Optional[float] = None,
391
+ timeout: Optional[float] = None,
392
+ no_wait_after: Optional[bool] = None,
393
+ strict: Optional[bool] = None,
394
+ ) -> None:
395
+ """Type text into an element matched by the selector."""
396
+ page = self.ensure_page()
397
+ return page.type(
398
+ selector=selector,
399
+ text=text,
400
+ delay=delay,
401
+ timeout=timeout,
402
+ no_wait_after=no_wait_after,
403
+ strict=strict,
404
+ )
405
+
406
+ def go_forward(
407
+ self,
408
+ timeout: Optional[float] = None,
409
+ wait_until: Optional[
410
+ Literal["commit", "domcontentloaded", "load", "networkidle"]
411
+ ] = None,
412
+ ) -> Optional[Response]:
413
+ """Go forward in the page history if possible."""
414
+ page = self.ensure_page()
415
+ return page.go_forward(timeout=timeout, wait_until=wait_until)
416
+
417
+ def go_back(
418
+ self,
419
+ timeout: Optional[float] = None,
420
+ wait_until: Optional[
421
+ Literal["commit", "domcontentloaded", "load", "networkidle"]
422
+ ] = None,
423
+ ) -> Optional[Response]:
424
+ """Go back in the page history if possible."""
425
+ page = self.ensure_page()
426
+ return page.go_back(timeout=timeout, wait_until=wait_until)
427
+
428
+ def evaluate(
429
+ self,
430
+ expression: str,
431
+ arg: Optional[Any] = None,
432
+ ) -> Any:
433
+ """Evaluate a JavaScript expression in the page context."""
434
+
435
+ page = self.ensure_page()
436
+ return page.evaluate(expression=expression, arg=arg)
437
+
438
+ def wait(self, timeout: float) -> None:
439
+ """Wait for the given timeout in milliseconds."""
440
+ page = self.ensure_page()
441
+ return page.wait_for_timeout(timeout=timeout)
442
+
443
+ def html_content(
444
+ self,
445
+ ) -> str:
446
+ """Get the current page's HTML content as a string."""
447
+ page = self.ensure_page()
448
+ return page.content()
449
+
450
+ def screenshot(
451
+ self,
452
+ timeout: Optional[float] = None,
453
+ type: Optional[Literal["jpeg", "png"]] = None,
454
+ path: Union[Path, str, None] = None,
455
+ quality: Optional[int] = None,
456
+ omit_background: Optional[bool] = None,
457
+ full_page: Optional[bool] = None,
458
+ clip: Optional[FloatRect] = None,
459
+ animations: Optional[Literal["allow", "disabled"]] = None,
460
+ caret: Optional[Literal["hide", "initial"]] = None,
461
+ scale: Optional[Literal["css", "device"]] = None,
462
+ mask: Optional[Sequence[Locator]] = None,
463
+ mask_color: Optional[str] = None,
464
+ style: Optional[str] = None,
465
+ ) -> bytes:
466
+ """Capture a screenshot of the page.
467
+
468
+ Returns
469
+ -------
470
+ bytes
471
+ The image bytes of the screenshot.
472
+ """
473
+ page = self.ensure_page()
474
+ return page.screenshot(
475
+ timeout=timeout,
476
+ type=type,
477
+ path=path,
478
+ quality=quality,
479
+ omit_background=omit_background,
480
+ full_page=full_page,
481
+ clip=clip,
482
+ animations=animations,
483
+ caret=caret,
484
+ scale=scale,
485
+ mask=mask,
486
+ mask_color=mask_color,
487
+ style=style,
488
+ )
489
+
490
+ def title(self) -> str:
491
+ page = self.ensure_page()
492
+ return page.title()
@@ -0,0 +1,154 @@
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/api/__sandbox_data_async_template.py
10
+
11
+ Sandbox数据API模板 / Sandbox Data API Template
12
+
13
+ 此模板用于生成沙箱数据API代码。
14
+ This template is used to generate sandbox data API code.
15
+ """
16
+
17
+ from typing import Any, Dict, Optional
18
+
19
+ from agentrun.utils.config import Config
20
+ from agentrun.utils.data_api import DataAPI, ResourceType
21
+
22
+
23
+ class SandboxDataAPI(DataAPI):
24
+
25
+ def __init__(
26
+ self,
27
+ *,
28
+ sandbox_id: Optional[str] = None,
29
+ template_name: Optional[str] = None,
30
+ config: Optional[Config] = None,
31
+ ):
32
+
33
+ super().__init__(
34
+ resource_name="",
35
+ resource_type=ResourceType.Template,
36
+ namespace="sandboxes",
37
+ config=config,
38
+ )
39
+ self.access_token_map = {}
40
+
41
+ if sandbox_id or template_name:
42
+ self.__refresh_access_token(
43
+ sandbox_id=sandbox_id,
44
+ template_name=template_name,
45
+ config=config,
46
+ )
47
+
48
+ def __refresh_access_token(
49
+ self,
50
+ *,
51
+ sandbox_id: Optional[str] = None,
52
+ template_name: Optional[str] = None,
53
+ config: Optional[Config] = None,
54
+ ):
55
+ cfg = Config.with_configs(config, self.config)
56
+ token = self.access_token_map.get(sandbox_id or template_name)
57
+ if sandbox_id:
58
+ self.resource_name = sandbox_id
59
+ self.resource_type = ResourceType.Sandbox
60
+ self.namespace = f"sandboxes/{sandbox_id}"
61
+ else:
62
+ self.resource_name = template_name
63
+ self.resource_type = ResourceType.Template
64
+ self.namespace = "sandboxes"
65
+
66
+ if token:
67
+ self.access_token = token
68
+ return
69
+
70
+ # 没有缓存过的 token
71
+
72
+ self.access_token = None
73
+ self.auth(config=cfg)
74
+ self.access_token_map[sandbox_id or template_name] = self.access_token
75
+
76
+ async def check_health_async(self):
77
+ return await self.get_async("/health")
78
+
79
+ def check_health(self):
80
+ return self.get("/health")
81
+
82
+ async def create_sandbox_async(
83
+ self,
84
+ template_name: str,
85
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
86
+ nas_config: Optional[Dict[str, Any]] = None,
87
+ oss_mount_config: Optional[Dict[str, Any]] = None,
88
+ polar_fs_config: Optional[Dict[str, Any]] = None,
89
+ config: Optional[Config] = None,
90
+ ):
91
+ self.__refresh_access_token(template_name=template_name, config=config)
92
+ data: Dict[str, Any] = {
93
+ "templateName": template_name,
94
+ "sandboxIdleTimeoutSeconds": sandbox_idle_timeout_seconds,
95
+ }
96
+ if nas_config is not None:
97
+ data["nasConfig"] = nas_config
98
+ if oss_mount_config is not None:
99
+ data["ossMountConfig"] = oss_mount_config
100
+ if polar_fs_config is not None:
101
+ data["polarFsConfig"] = polar_fs_config
102
+ return await self.post_async("/", data=data)
103
+
104
+ def create_sandbox(
105
+ self,
106
+ template_name: str,
107
+ sandbox_idle_timeout_seconds: Optional[int] = 600,
108
+ nas_config: Optional[Dict[str, Any]] = None,
109
+ oss_mount_config: Optional[Dict[str, Any]] = None,
110
+ polar_fs_config: Optional[Dict[str, Any]] = None,
111
+ config: Optional[Config] = None,
112
+ ):
113
+ self.__refresh_access_token(template_name=template_name, config=config)
114
+ data: Dict[str, Any] = {
115
+ "templateName": template_name,
116
+ "sandboxIdleTimeoutSeconds": sandbox_idle_timeout_seconds,
117
+ }
118
+ if nas_config is not None:
119
+ data["nasConfig"] = nas_config
120
+ if oss_mount_config is not None:
121
+ data["ossMountConfig"] = oss_mount_config
122
+ if polar_fs_config is not None:
123
+ data["polarFsConfig"] = polar_fs_config
124
+ return self.post("/", data=data)
125
+
126
+ async def delete_sandbox_async(
127
+ self, sandbox_id: str, config: Optional[Config] = None
128
+ ):
129
+ self.__refresh_access_token(sandbox_id=sandbox_id, config=config)
130
+ return await self.delete_async("/")
131
+
132
+ def delete_sandbox(self, sandbox_id: str, config: Optional[Config] = None):
133
+ self.__refresh_access_token(sandbox_id=sandbox_id, config=config)
134
+ return self.delete("/")
135
+
136
+ async def stop_sandbox_async(
137
+ self, sandbox_id: str, config: Optional[Config] = None
138
+ ):
139
+ self.__refresh_access_token(sandbox_id=sandbox_id, config=config)
140
+ return await self.post_async("/stop")
141
+
142
+ def stop_sandbox(self, sandbox_id: str, config: Optional[Config] = None):
143
+ self.__refresh_access_token(sandbox_id=sandbox_id, config=config)
144
+ return self.post("/stop")
145
+
146
+ async def get_sandbox_async(
147
+ self, sandbox_id: str, config: Optional[Config] = None
148
+ ):
149
+ self.__refresh_access_token(sandbox_id=sandbox_id, config=config)
150
+ return await self.get_async("/")
151
+
152
+ def get_sandbox(self, sandbox_id: str, config: Optional[Config] = None):
153
+ self.__refresh_access_token(sandbox_id=sandbox_id, config=config)
154
+ return self.get("/")