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.
- agentrun/__init__.py +325 -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 +93 -0
- agentrun/integration/builtin/sandbox.py +1234 -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 +31 -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 +46 -0
- agentrun/integration/google_adk/tool_adapter.py +235 -0
- agentrun/integration/langchain/__init__.py +30 -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 +35 -0
- agentrun/integration/langgraph/adapter.py +20 -0
- agentrun/integration/langgraph/agent_converter.py +1073 -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 +560 -0
- agentrun/integration/utils/canonical.py +164 -0
- agentrun/integration/utils/converter.py +134 -0
- agentrun/integration/utils/model.py +110 -0
- agentrun/integration/utils/tool.py +1759 -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 +235 -0
- agentrun/model/model_proxy.py +439 -0
- agentrun/model/model_service.py +438 -0
- agentrun/sandbox/__aio_sandbox_async_template.py +523 -0
- agentrun/sandbox/__browser_sandbox_async_template.py +110 -0
- agentrun/sandbox/__client_async_template.py +491 -0
- agentrun/sandbox/__code_interpreter_sandbox_async_template.py +463 -0
- agentrun/sandbox/__init__.py +69 -0
- agentrun/sandbox/__sandbox_async_template.py +463 -0
- agentrun/sandbox/__template_async_template.py +152 -0
- agentrun/sandbox/aio_sandbox.py +905 -0
- agentrun/sandbox/api/__aio_data_async_template.py +335 -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 +19 -0
- agentrun/sandbox/api/__sandbox_data_async_template.py +107 -0
- agentrun/sandbox/api/aio_data.py +551 -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 +154 -0
- agentrun/sandbox/browser_sandbox.py +185 -0
- agentrun/sandbox/client.py +925 -0
- agentrun/sandbox/code_interpreter_sandbox.py +823 -0
- agentrun/sandbox/model.py +397 -0
- agentrun/sandbox/sandbox.py +848 -0
- agentrun/sandbox/template.py +217 -0
- agentrun/server/__init__.py +191 -0
- agentrun/server/agui_normalizer.py +180 -0
- agentrun/server/agui_protocol.py +797 -0
- agentrun/server/invoker.py +309 -0
- agentrun/server/model.py +427 -0
- agentrun/server/openai_protocol.py +535 -0
- agentrun/server/protocol.py +140 -0
- agentrun/server/server.py +208 -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 +1251 -0
- agentrun/toolset/client.py +102 -0
- agentrun/toolset/model.py +321 -0
- agentrun/toolset/toolset.py +270 -0
- agentrun/utils/__data_api_async_template.py +720 -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 +1120 -0
- agentrun/utils/exception.py +151 -0
- agentrun/utils/helper.py +108 -0
- agentrun/utils/log.py +77 -0
- agentrun/utils/model.py +168 -0
- agentrun/utils/resource.py +291 -0
- agentrun_inner_test-0.0.46.dist-info/METADATA +263 -0
- agentrun_inner_test-0.0.46.dist-info/RECORD +135 -0
- agentrun_inner_test-0.0.46.dist-info/WHEEL +5 -0
- agentrun_inner_test-0.0.46.dist-info/licenses/LICENSE +201 -0
- agentrun_inner_test-0.0.46.dist-info/top_level.txt +1 -0
agentrun/server/model.py
ADDED
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
"""AgentRun Server 模型定义 / AgentRun Server Model Definitions
|
|
2
|
+
|
|
3
|
+
定义标准化的 AgentRequest 和 AgentEvent 数据结构。
|
|
4
|
+
采用协议无关的设计,支持多协议转换(OpenAI、AG-UI 等)。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from typing import (
|
|
9
|
+
Any,
|
|
10
|
+
AsyncGenerator,
|
|
11
|
+
AsyncIterator,
|
|
12
|
+
Dict,
|
|
13
|
+
Generator,
|
|
14
|
+
Iterator,
|
|
15
|
+
List,
|
|
16
|
+
Optional,
|
|
17
|
+
TYPE_CHECKING,
|
|
18
|
+
Union,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
# 导入 Request 类,用于类型提示和运行时使用
|
|
22
|
+
from starlette.requests import Request
|
|
23
|
+
|
|
24
|
+
from ..utils.helper import MergeOptions
|
|
25
|
+
from ..utils.model import BaseModel, Field
|
|
26
|
+
|
|
27
|
+
# ============================================================================
|
|
28
|
+
# 协议配置
|
|
29
|
+
# ============================================================================
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ProtocolConfig(BaseModel):
|
|
33
|
+
prefix: Optional[str] = None
|
|
34
|
+
enable: bool = True
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class AGUIProtocolConfig(ProtocolConfig):
|
|
38
|
+
"""AG-UI 协议配置
|
|
39
|
+
|
|
40
|
+
Attributes:
|
|
41
|
+
prefix: 协议路由前缀,默认 "/ag-ui/agent"
|
|
42
|
+
enable: 是否启用协议
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
enable: bool = True
|
|
46
|
+
prefix: Optional[str] = "/ag-ui/agent"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class ServerConfig(BaseModel):
|
|
50
|
+
openai: Optional["OpenAIProtocolConfig"] = None
|
|
51
|
+
agui: Optional["AGUIProtocolConfig"] = None
|
|
52
|
+
cors_origins: Optional[List[str]] = None
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# ============================================================================
|
|
56
|
+
# 消息角色和消息体定义
|
|
57
|
+
# ============================================================================
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class MessageRole(str, Enum):
|
|
61
|
+
"""消息角色 / Message Role"""
|
|
62
|
+
|
|
63
|
+
SYSTEM = "system"
|
|
64
|
+
USER = "user"
|
|
65
|
+
ASSISTANT = "assistant"
|
|
66
|
+
TOOL = "tool"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class ToolCall(BaseModel):
|
|
70
|
+
"""工具调用 / Tool Call"""
|
|
71
|
+
|
|
72
|
+
id: str
|
|
73
|
+
type: str = "function"
|
|
74
|
+
function: Dict[str, Any]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class Message(BaseModel):
|
|
78
|
+
"""标准化消息体 / Standardized Message
|
|
79
|
+
|
|
80
|
+
兼容 AG-UI 和 OpenAI 消息格式。
|
|
81
|
+
|
|
82
|
+
Attributes:
|
|
83
|
+
id: 消息唯一标识(AG-UI 格式)
|
|
84
|
+
role: 消息角色
|
|
85
|
+
content: 消息内容(字符串或多模态内容列表)
|
|
86
|
+
name: 发送者名称(可选)
|
|
87
|
+
tool_calls: 工具调用列表(assistant 消息)
|
|
88
|
+
tool_call_id: 对应的工具调用 ID(tool 消息)
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
id: Optional[str] = None
|
|
92
|
+
role: MessageRole
|
|
93
|
+
content: Optional[Union[str, List[Dict[str, Any]]]] = None
|
|
94
|
+
name: Optional[str] = None
|
|
95
|
+
tool_calls: Optional[List[ToolCall]] = None
|
|
96
|
+
tool_call_id: Optional[str] = None
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class Tool(BaseModel):
|
|
100
|
+
"""工具定义 / Tool Definition
|
|
101
|
+
|
|
102
|
+
兼容 AG-UI 和 OpenAI 工具格式。
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
type: str = "function"
|
|
106
|
+
function: Dict[str, Any]
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
# ============================================================================
|
|
110
|
+
# 事件类型定义(协议无关)
|
|
111
|
+
# ============================================================================
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class EventType(str, Enum):
|
|
115
|
+
"""事件类型(协议无关)
|
|
116
|
+
|
|
117
|
+
定义核心事件类型,框架会自动转换为对应协议格式(OpenAI、AG-UI 等)。
|
|
118
|
+
用户只需关心语义,无需关心具体协议细节。
|
|
119
|
+
|
|
120
|
+
边界事件(如消息开始/结束、生命周期开始/结束)由协议层自动处理,
|
|
121
|
+
用户无需关心。
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
# =========================================================================
|
|
125
|
+
# 核心事件(用户主要使用)
|
|
126
|
+
# =========================================================================
|
|
127
|
+
TEXT = "TEXT" # 文本内容块
|
|
128
|
+
TOOL_CALL = "TOOL_CALL" # 完整工具调用(含 id, name, args)
|
|
129
|
+
TOOL_CALL_CHUNK = "TOOL_CALL_CHUNK" # 工具调用参数片段(流式场景)
|
|
130
|
+
TOOL_RESULT = "TOOL_RESULT" # 工具执行结果(最终结果,标识流式输出结束)
|
|
131
|
+
TOOL_RESULT_CHUNK = "TOOL_RESULT_CHUNK" # 工具执行结果片段(流式输出场景)
|
|
132
|
+
ERROR = "ERROR" # 错误事件
|
|
133
|
+
STATE = "STATE" # 状态更新(快照或增量)
|
|
134
|
+
|
|
135
|
+
# =========================================================================
|
|
136
|
+
# 人机交互事件
|
|
137
|
+
# =========================================================================
|
|
138
|
+
HITL = "HITL" # Human-in-the-Loop,请求人类介入
|
|
139
|
+
|
|
140
|
+
# =========================================================================
|
|
141
|
+
# 扩展事件
|
|
142
|
+
# =========================================================================
|
|
143
|
+
CUSTOM = "CUSTOM" # 自定义事件(协议层会正确处理)
|
|
144
|
+
RAW = "RAW" # 原始协议数据(直接透传到响应流)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
# ============================================================================
|
|
148
|
+
# Addition 合并参数(使用 MergeOptions)
|
|
149
|
+
# ============================================================================
|
|
150
|
+
# 使用 MergeOptions(来自 utils.helper.merge)控制 addition 的合并行为:
|
|
151
|
+
# - 默认 (None): 深度合并,允许新增字段
|
|
152
|
+
# - no_new_field=True: 仅覆盖已有字段(等价于原 PROTOCOL_ONLY)
|
|
153
|
+
# - concat_list / ignore_empty_list: 透传给 merge 控制列表合并策略
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
# ============================================================================
|
|
157
|
+
# AgentEvent(标准化事件)
|
|
158
|
+
# ============================================================================
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
class AgentEvent(BaseModel):
|
|
162
|
+
"""Agent 执行事件
|
|
163
|
+
|
|
164
|
+
标准化的事件结构,协议无关设计。
|
|
165
|
+
框架层会自动将 AgentEvent 转换为对应协议的格式(OpenAI、AG-UI 等)。
|
|
166
|
+
|
|
167
|
+
Attributes:
|
|
168
|
+
event: 事件类型
|
|
169
|
+
data: 事件数据
|
|
170
|
+
addition: 额外附加字段(可选,用于协议特定扩展)
|
|
171
|
+
addition_merge_options: 合并选项(透传给 utils.helper.merge,默认深度合并)
|
|
172
|
+
|
|
173
|
+
Example (文本消息):
|
|
174
|
+
>>> yield AgentEvent(
|
|
175
|
+
... event=EventType.TEXT,
|
|
176
|
+
... data={"delta": "Hello, world!"}
|
|
177
|
+
... )
|
|
178
|
+
|
|
179
|
+
Example (完整工具调用):
|
|
180
|
+
>>> yield AgentEvent(
|
|
181
|
+
... event=EventType.TOOL_CALL,
|
|
182
|
+
... data={
|
|
183
|
+
... "id": "tc-1",
|
|
184
|
+
... "name": "get_weather",
|
|
185
|
+
... "args": '{"location": "Beijing"}'
|
|
186
|
+
... }
|
|
187
|
+
... )
|
|
188
|
+
|
|
189
|
+
Example (流式工具调用):
|
|
190
|
+
>>> yield AgentEvent(
|
|
191
|
+
... event=EventType.TOOL_CALL_CHUNK,
|
|
192
|
+
... data={"id": "tc-1", "name": "search", "args_delta": '{"q":'}
|
|
193
|
+
... )
|
|
194
|
+
>>> yield AgentEvent(
|
|
195
|
+
... event=EventType.TOOL_CALL_CHUNK,
|
|
196
|
+
... data={"id": "tc-1", "args_delta": '"test"}'}
|
|
197
|
+
... )
|
|
198
|
+
|
|
199
|
+
Example (工具执行结果):
|
|
200
|
+
>>> yield AgentEvent(
|
|
201
|
+
... event=EventType.TOOL_RESULT,
|
|
202
|
+
... data={"id": "tc-1", "result": "Sunny, 25°C"}
|
|
203
|
+
... )
|
|
204
|
+
|
|
205
|
+
Example (流式工具执行结果):
|
|
206
|
+
流式工具输出的使用流程:
|
|
207
|
+
1. TOOL_RESULT_CHUNK 事件会被缓存,不会立即发送
|
|
208
|
+
2. 必须发送 TOOL_RESULT 事件来标识流式输出结束
|
|
209
|
+
3. TOOL_RESULT 会将缓存的 chunks 拼接到最终结果前面
|
|
210
|
+
|
|
211
|
+
>>> # 工具执行过程中流式输出(这些会被缓存)
|
|
212
|
+
>>> yield AgentEvent(
|
|
213
|
+
... event=EventType.TOOL_RESULT_CHUNK,
|
|
214
|
+
... data={"id": "tc-1", "delta": "Executing step 1...\n"}
|
|
215
|
+
... )
|
|
216
|
+
>>> yield AgentEvent(
|
|
217
|
+
... event=EventType.TOOL_RESULT_CHUNK,
|
|
218
|
+
... data={"id": "tc-1", "delta": "Step 1 complete.\n"}
|
|
219
|
+
... )
|
|
220
|
+
>>> # 最终结果(必须发送,标识流式输出结束)
|
|
221
|
+
>>> # 发送后会拼接为: "Executing step 1...\nStep 1 complete.\nAll steps completed."
|
|
222
|
+
>>> yield AgentEvent(
|
|
223
|
+
... event=EventType.TOOL_RESULT,
|
|
224
|
+
... data={"id": "tc-1", "result": "All steps completed."}
|
|
225
|
+
... )
|
|
226
|
+
>>> # 如果只有流式输出,result 可以为空字符串
|
|
227
|
+
>>> yield AgentEvent(
|
|
228
|
+
... event=EventType.TOOL_RESULT,
|
|
229
|
+
... data={"id": "tc-1", "result": ""} # 只使用缓存的 chunks
|
|
230
|
+
... )
|
|
231
|
+
|
|
232
|
+
Example (HITL - Human-in-the-Loop,请求人类介入):
|
|
233
|
+
HITL 有两种使用方式:
|
|
234
|
+
1. 关联已存在的工具调用:设置 tool_call_id,复用现有工具
|
|
235
|
+
2. 创建独立的 HITL 工具调用:只设置 id
|
|
236
|
+
|
|
237
|
+
>>> # 方式 1:关联已存在的工具调用(先发送 TOOL_CALL,再发送 HITL)
|
|
238
|
+
>>> yield AgentEvent(
|
|
239
|
+
... event=EventType.TOOL_CALL,
|
|
240
|
+
... data={"id": "tc-delete", "name": "delete_file", "args": '{"file": "a.txt"}'}
|
|
241
|
+
... )
|
|
242
|
+
>>> yield AgentEvent(
|
|
243
|
+
... event=EventType.HITL,
|
|
244
|
+
... data={
|
|
245
|
+
... "id": "hitl-1",
|
|
246
|
+
... "tool_call_id": "tc-delete", # 关联已存在的工具调用
|
|
247
|
+
... "type": "confirmation",
|
|
248
|
+
... "prompt": "确认删除文件 a.txt?"
|
|
249
|
+
... }
|
|
250
|
+
... )
|
|
251
|
+
>>> # 方式 2:创建独立的 HITL 工具调用
|
|
252
|
+
>>> yield AgentEvent(
|
|
253
|
+
... event=EventType.HITL,
|
|
254
|
+
... data={
|
|
255
|
+
... "id": "hitl-2",
|
|
256
|
+
... "type": "input",
|
|
257
|
+
... "prompt": "请输入密码:",
|
|
258
|
+
... "options": ["确认", "取消"], # 可选
|
|
259
|
+
... "schema": {"type": "string", "minLength": 8} # 可选
|
|
260
|
+
... }
|
|
261
|
+
... )
|
|
262
|
+
>>> # 用户响应将通过下一轮对话的 messages 中的 tool message 传回
|
|
263
|
+
|
|
264
|
+
Example (自定义事件):
|
|
265
|
+
>>> yield AgentEvent(
|
|
266
|
+
... event=EventType.CUSTOM,
|
|
267
|
+
... data={"name": "step_started", "value": {"step": "thinking"}}
|
|
268
|
+
... )
|
|
269
|
+
|
|
270
|
+
Example (原始协议数据):
|
|
271
|
+
>>> yield AgentEvent(
|
|
272
|
+
... event=EventType.RAW,
|
|
273
|
+
... data={"raw": "data: {...}\\n\\n"}
|
|
274
|
+
... )
|
|
275
|
+
"""
|
|
276
|
+
|
|
277
|
+
event: EventType
|
|
278
|
+
data: Dict[str, Any] = Field(default_factory=dict)
|
|
279
|
+
addition: Optional[Dict[str, Any]] = None
|
|
280
|
+
addition_merge_options: Optional[MergeOptions] = None
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
# 兼容别名
|
|
284
|
+
AgentResult = AgentEvent
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
# ============================================================================
|
|
288
|
+
# AgentRequest(标准化请求)
|
|
289
|
+
# ============================================================================
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
class AgentRequest(BaseModel):
|
|
293
|
+
"""Agent 请求参数(协议无关)
|
|
294
|
+
|
|
295
|
+
标准化的请求结构,统一了 OpenAI 和 AG-UI 协议的输入格式。
|
|
296
|
+
|
|
297
|
+
Attributes:
|
|
298
|
+
protocol: 当前交互协议名称(如 "openai", "agui")
|
|
299
|
+
messages: 对话历史消息列表(标准化格式)
|
|
300
|
+
stream: 是否使用流式输出
|
|
301
|
+
tools: 可用的工具列表
|
|
302
|
+
raw_request: 原始 HTTP 请求对象(Starlette Request)
|
|
303
|
+
|
|
304
|
+
Example (基本使用):
|
|
305
|
+
>>> def invoke_agent(request: AgentRequest):
|
|
306
|
+
... user_msg = request.messages[-1].content
|
|
307
|
+
... return f"你说的是: {user_msg}"
|
|
308
|
+
|
|
309
|
+
Example (流式输出):
|
|
310
|
+
>>> async def invoke_agent(request: AgentRequest):
|
|
311
|
+
... for word in ["Hello", " ", "World"]:
|
|
312
|
+
... yield word
|
|
313
|
+
|
|
314
|
+
Example (使用事件):
|
|
315
|
+
>>> async def invoke_agent(request: AgentRequest):
|
|
316
|
+
... yield AgentEvent(
|
|
317
|
+
... event=EventType.CUSTOM,
|
|
318
|
+
... data={"name": "step_started", "value": {"step": "thinking"}}
|
|
319
|
+
... )
|
|
320
|
+
... yield "I'm thinking..."
|
|
321
|
+
... yield AgentEvent(
|
|
322
|
+
... event=EventType.CUSTOM,
|
|
323
|
+
... data={"name": "step_finished", "value": {"step": "thinking"}}
|
|
324
|
+
... )
|
|
325
|
+
|
|
326
|
+
Example (工具调用):
|
|
327
|
+
>>> async def invoke_agent(request: AgentRequest):
|
|
328
|
+
... # 完整工具调用
|
|
329
|
+
... yield AgentEvent(
|
|
330
|
+
... event=EventType.TOOL_CALL,
|
|
331
|
+
... data={
|
|
332
|
+
... "id": "tc-1",
|
|
333
|
+
... "name": "search",
|
|
334
|
+
... "args": '{"query": "weather"}'
|
|
335
|
+
... }
|
|
336
|
+
... )
|
|
337
|
+
... # 执行工具并返回结果
|
|
338
|
+
... result = do_search("weather")
|
|
339
|
+
... yield AgentEvent(
|
|
340
|
+
... event=EventType.TOOL_RESULT,
|
|
341
|
+
... data={"id": "tc-1", "result": result}
|
|
342
|
+
... )
|
|
343
|
+
|
|
344
|
+
Example (根据协议差异化处理):
|
|
345
|
+
>>> async def invoke_agent(request: AgentRequest):
|
|
346
|
+
... if request.protocol == "openai":
|
|
347
|
+
... # OpenAI 特定处理
|
|
348
|
+
... pass
|
|
349
|
+
... elif request.protocol == "agui":
|
|
350
|
+
... # AG-UI 特定处理
|
|
351
|
+
... pass
|
|
352
|
+
|
|
353
|
+
Example (访问原始请求):
|
|
354
|
+
>>> async def invoke_agent(request: AgentRequest):
|
|
355
|
+
... # 访问原始请求头
|
|
356
|
+
... auth = request.raw_request.headers.get("Authorization")
|
|
357
|
+
... # 访问原始请求体(已解析的 JSON)
|
|
358
|
+
... body = await request.raw_request.json()
|
|
359
|
+
... # 访问查询参数
|
|
360
|
+
... params = request.raw_request.query_params
|
|
361
|
+
... # 访问客户端 IP
|
|
362
|
+
... client_ip = request.raw_request.client.host
|
|
363
|
+
"""
|
|
364
|
+
|
|
365
|
+
model_config = {"arbitrary_types_allowed": True}
|
|
366
|
+
|
|
367
|
+
# 协议信息
|
|
368
|
+
protocol: str = Field("unknown", description="当前交互协议名称")
|
|
369
|
+
|
|
370
|
+
# 标准化参数
|
|
371
|
+
messages: List[Message] = Field(
|
|
372
|
+
default_factory=list, description="对话历史消息列表"
|
|
373
|
+
)
|
|
374
|
+
stream: bool = Field(False, description="是否使用流式输出")
|
|
375
|
+
tools: Optional[List[Tool]] = Field(None, description="可用的工具列表")
|
|
376
|
+
|
|
377
|
+
# 原始请求对象
|
|
378
|
+
raw_request: Optional[Request] = Field(
|
|
379
|
+
None, description="原始 HTTP 请求对象(Starlette Request)"
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
# ============================================================================
|
|
384
|
+
# OpenAI 协议配置(前置声明)
|
|
385
|
+
# ============================================================================
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
class OpenAIProtocolConfig(ProtocolConfig):
|
|
389
|
+
"""OpenAI 协议配置"""
|
|
390
|
+
|
|
391
|
+
enable: bool = True
|
|
392
|
+
prefix: Optional[str] = "/openai/v1"
|
|
393
|
+
model_name: Optional[str] = None
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
# ============================================================================
|
|
397
|
+
# 返回值类型别名
|
|
398
|
+
# ============================================================================
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
# 单个结果项:可以是字符串或 AgentEvent
|
|
402
|
+
AgentEventItem = Union[str, AgentEvent]
|
|
403
|
+
|
|
404
|
+
# 兼容别名
|
|
405
|
+
AgentResultItem = AgentEventItem
|
|
406
|
+
|
|
407
|
+
# 同步生成器
|
|
408
|
+
SyncAgentEventGenerator = Generator[AgentEventItem, None, None]
|
|
409
|
+
SyncAgentResultGenerator = SyncAgentEventGenerator # 兼容别名
|
|
410
|
+
|
|
411
|
+
# 异步生成器
|
|
412
|
+
AsyncAgentEventGenerator = AsyncGenerator[AgentEventItem, None]
|
|
413
|
+
AsyncAgentResultGenerator = AsyncAgentEventGenerator # 兼容别名
|
|
414
|
+
|
|
415
|
+
# Agent 函数返回值类型
|
|
416
|
+
AgentReturnType = Union[
|
|
417
|
+
# 简单返回
|
|
418
|
+
str, # 直接返回字符串
|
|
419
|
+
AgentEvent, # 返回单个事件
|
|
420
|
+
List[AgentEvent], # 返回多个事件(非流式)
|
|
421
|
+
Dict[str, Any], # 返回字典(如 OpenAI/AG-UI 非流式响应)
|
|
422
|
+
# 迭代器/生成器返回(流式)
|
|
423
|
+
Iterator[AgentEventItem],
|
|
424
|
+
AsyncIterator[AgentEventItem],
|
|
425
|
+
SyncAgentEventGenerator,
|
|
426
|
+
AsyncAgentEventGenerator,
|
|
427
|
+
]
|