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,309 @@
1
+ """Agent 调用器 / Agent Invoker
2
+
3
+ 负责处理 Agent 调用的通用逻辑,包括:
4
+ - 同步/异步调用处理
5
+ - 字符串到 AgentEvent 的自动转换
6
+ - 流式/非流式结果处理
7
+ - TOOL_CALL 事件的展开
8
+
9
+ 边界事件(如生命周期开始/结束、文本消息开始/结束)由协议层处理。
10
+ """
11
+
12
+ import asyncio
13
+ import inspect
14
+ from typing import (
15
+ Any,
16
+ AsyncGenerator,
17
+ AsyncIterator,
18
+ Awaitable,
19
+ cast,
20
+ Iterator,
21
+ List,
22
+ Union,
23
+ )
24
+ import uuid
25
+
26
+ from .model import AgentEvent, AgentRequest, EventType
27
+ from .protocol import (
28
+ AsyncInvokeAgentHandler,
29
+ InvokeAgentHandler,
30
+ SyncInvokeAgentHandler,
31
+ )
32
+
33
+
34
+ class AgentInvoker:
35
+ """Agent 调用器
36
+
37
+ 职责:
38
+ 1. 调用用户的 invoke_agent
39
+ 2. 处理同步/异步调用
40
+ 3. 自动转换 string 为 AgentEvent(TEXT)
41
+ 4. 展开 TOOL_CALL 为 TOOL_CALL_CHUNK
42
+ 5. 处理流式和非流式返回
43
+
44
+ 协议层负责:
45
+ - 生成生命周期事件(RUN_STARTED, RUN_FINISHED 等)
46
+ - 生成文本边界事件(TEXT_MESSAGE_START, TEXT_MESSAGE_END 等)
47
+ - 生成工具调用边界事件(TOOL_CALL_START, TOOL_CALL_END 等)
48
+
49
+ Example:
50
+ >>> def my_agent(request: AgentRequest) -> str:
51
+ ... return "Hello" # 自动转换为 TEXT 事件
52
+ >>>
53
+ >>> invoker = AgentInvoker(my_agent)
54
+ >>> async for event in invoker.invoke_stream(AgentRequest(...)):
55
+ ... print(event) # AgentEvent 对象
56
+ """
57
+
58
+ def __init__(self, invoke_agent: InvokeAgentHandler):
59
+ """初始化 Agent 调用器
60
+
61
+ Args:
62
+ invoke_agent: Agent 处理函数,可以是同步或异步
63
+ """
64
+ self.invoke_agent = invoke_agent
65
+ # 检测是否是异步函数或异步生成器
66
+ self.is_async = inspect.iscoroutinefunction(
67
+ invoke_agent
68
+ ) or inspect.isasyncgenfunction(invoke_agent)
69
+
70
+ async def invoke(
71
+ self, request: AgentRequest
72
+ ) -> Union[List[AgentEvent], AsyncGenerator[AgentEvent, None]]:
73
+ """调用 Agent 并返回结果
74
+
75
+ 根据返回值类型决定返回:
76
+ - 非迭代器: 返回 List[AgentEvent]
77
+ - 迭代器: 返回 AsyncGenerator[AgentEvent, None]
78
+
79
+ Args:
80
+ request: AgentRequest 请求对象
81
+
82
+ Returns:
83
+ List[AgentEvent] 或 AsyncGenerator[AgentEvent, None]
84
+ """
85
+ raw_result = await self._call_handler(request)
86
+
87
+ if self._is_iterator(raw_result):
88
+ return self._wrap_stream(raw_result)
89
+ else:
90
+ return self._wrap_non_stream(raw_result)
91
+
92
+ async def invoke_stream(
93
+ self, request: AgentRequest
94
+ ) -> AsyncGenerator[AgentEvent, None]:
95
+ """调用 Agent 并返回流式结果
96
+
97
+ 始终返回流式结果,即使原始返回值是非流式的。
98
+ 只输出核心事件,边界事件由协议层生成。
99
+
100
+ Args:
101
+ request: AgentRequest 请求对象
102
+
103
+ Yields:
104
+ AgentEvent: 事件结果
105
+ """
106
+ try:
107
+ raw_result = await self._call_handler(request)
108
+
109
+ if self._is_iterator(raw_result):
110
+ # 流式结果 - 逐个处理
111
+ async for item in self._iterate_async(raw_result):
112
+ if item is None:
113
+ continue
114
+
115
+ if isinstance(item, str):
116
+ if not item: # 跳过空字符串
117
+ continue
118
+ yield AgentEvent(
119
+ event=EventType.TEXT,
120
+ data={"delta": item},
121
+ )
122
+
123
+ elif isinstance(item, AgentEvent):
124
+ # 处理用户返回的事件
125
+ for processed_event in self._process_user_event(item):
126
+ yield processed_event
127
+ else:
128
+ # 非流式结果
129
+ results = self._wrap_non_stream(raw_result)
130
+ for result in results:
131
+ yield result
132
+
133
+ except Exception as e:
134
+ # 发送错误事件
135
+ from agentrun.utils.log import logger
136
+
137
+ logger.error(f"Agent 调用出错: {e}", exc_info=True)
138
+ yield AgentEvent(
139
+ event=EventType.ERROR,
140
+ data={"message": str(e), "code": type(e).__name__},
141
+ )
142
+
143
+ def _process_user_event(
144
+ self,
145
+ event: AgentEvent,
146
+ ) -> Iterator[AgentEvent]:
147
+ """处理用户返回的事件
148
+
149
+ - TOOL_CALL 事件会被展开为 TOOL_CALL_CHUNK
150
+ - 其他事件直接传递
151
+
152
+ Args:
153
+ event: 用户返回的事件
154
+
155
+ Yields:
156
+ 处理后的事件
157
+ """
158
+ # 展开 TOOL_CALL 为 TOOL_CALL_CHUNK
159
+ if event.event == EventType.TOOL_CALL:
160
+ tool_id = event.data.get("id", str(uuid.uuid4()))
161
+ tool_name = event.data.get("name", "")
162
+ tool_args = event.data.get("args", "")
163
+
164
+ # 发送包含名称的 chunk(首个 chunk 包含名称)
165
+ yield AgentEvent(
166
+ event=EventType.TOOL_CALL_CHUNK,
167
+ data={
168
+ "id": tool_id,
169
+ "name": tool_name,
170
+ "args_delta": tool_args,
171
+ },
172
+ )
173
+ return
174
+
175
+ # 其他事件直接传递
176
+ yield event
177
+
178
+ async def _call_handler(self, request: AgentRequest) -> Any:
179
+ """调用用户的 handler
180
+
181
+ Args:
182
+ request: AgentRequest 请求对象
183
+
184
+ Returns:
185
+ 原始返回值
186
+ """
187
+ if self.is_async:
188
+ async_handler = cast(AsyncInvokeAgentHandler, self.invoke_agent)
189
+ raw_result = async_handler(request)
190
+
191
+ if inspect.isawaitable(raw_result):
192
+ result = await cast(Awaitable[Any], raw_result)
193
+ elif inspect.isasyncgen(raw_result):
194
+ result = raw_result
195
+ else:
196
+ result = raw_result
197
+ else:
198
+ sync_handler = cast(SyncInvokeAgentHandler, self.invoke_agent)
199
+ loop = asyncio.get_running_loop()
200
+ result = await loop.run_in_executor(None, sync_handler, request)
201
+
202
+ return result
203
+
204
+ def _wrap_non_stream(self, result: Any) -> List[AgentEvent]:
205
+ """包装非流式结果为 AgentEvent 列表
206
+
207
+ Args:
208
+ result: 原始返回值
209
+
210
+ Returns:
211
+ AgentEvent 列表
212
+ """
213
+ results: List[AgentEvent] = []
214
+
215
+ if result is None:
216
+ return results
217
+
218
+ if isinstance(result, str):
219
+ results.append(
220
+ AgentEvent(
221
+ event=EventType.TEXT,
222
+ data={"delta": result},
223
+ )
224
+ )
225
+
226
+ elif isinstance(result, AgentEvent):
227
+ # 处理可能的 TOOL_CALL 展开
228
+ results.extend(self._process_user_event(result))
229
+
230
+ elif isinstance(result, list):
231
+ for item in result:
232
+ if isinstance(item, AgentEvent):
233
+ results.extend(self._process_user_event(item))
234
+ elif isinstance(item, str) and item:
235
+ results.append(
236
+ AgentEvent(
237
+ event=EventType.TEXT,
238
+ data={"delta": item},
239
+ )
240
+ )
241
+
242
+ return results
243
+
244
+ async def _wrap_stream(
245
+ self, iterator: Any
246
+ ) -> AsyncGenerator[AgentEvent, None]:
247
+ """包装迭代器为 AgentEvent 异步生成器
248
+
249
+ Args:
250
+ iterator: 原始迭代器
251
+
252
+ Yields:
253
+ AgentEvent: 事件结果
254
+ """
255
+ async for item in self._iterate_async(iterator):
256
+ if item is None:
257
+ continue
258
+
259
+ if isinstance(item, str):
260
+ if not item:
261
+ continue
262
+ yield AgentEvent(
263
+ event=EventType.TEXT,
264
+ data={"delta": item},
265
+ )
266
+
267
+ elif isinstance(item, AgentEvent):
268
+ for processed_event in self._process_user_event(item):
269
+ yield processed_event
270
+
271
+ async def _iterate_async(
272
+ self, content: Union[Iterator[Any], AsyncIterator[Any]]
273
+ ) -> AsyncGenerator[Any, None]:
274
+ """统一迭代同步和异步迭代器
275
+
276
+ 对于同步迭代器,每次 next() 调用都在线程池中执行,避免阻塞事件循环。
277
+
278
+ Args:
279
+ content: 迭代器
280
+
281
+ Yields:
282
+ 迭代器中的元素
283
+ """
284
+ if hasattr(content, "__aiter__"):
285
+ async for chunk in content:
286
+ yield chunk
287
+ else:
288
+ loop = asyncio.get_running_loop()
289
+ iterator = iter(content)
290
+
291
+ _STOP = object()
292
+
293
+ def _safe_next() -> Any:
294
+ try:
295
+ return next(iterator)
296
+ except StopIteration:
297
+ return _STOP
298
+
299
+ while True:
300
+ chunk = await loop.run_in_executor(None, _safe_next)
301
+ if chunk is _STOP:
302
+ break
303
+ yield chunk
304
+
305
+ def _is_iterator(self, obj: Any) -> bool:
306
+ """检查对象是否是迭代器"""
307
+ if isinstance(obj, (str, bytes, dict, list, AgentEvent)):
308
+ return False
309
+ return hasattr(obj, "__iter__") or hasattr(obj, "__aiter__")