|
1 | 1 | """AgentRun Server 模块 / AgentRun Server Module |
2 | 2 |
|
3 | 3 | 提供 HTTP Server 集成能力,支持符合 AgentRun 规范的 Agent 调用接口。 |
| 4 | +支持 OpenAI Chat Completions 和 AG-UI 两种协议。 |
4 | 5 |
|
5 | | -Example (基本使用): |
6 | | ->>> from agentrun.server import AgentRunServer, AgentRequest, AgentResponse |
| 6 | +Example (基本使用 - 同步): |
| 7 | +>>> from agentrun.server import AgentRunServer, AgentRequest |
7 | 8 | >>> |
8 | | ->>> def invoke_agent(request: AgentRequest) -> AgentResponse: |
| 9 | +>>> def invoke_agent(request: AgentRequest): |
9 | 10 | ... # 实现你的 Agent 逻辑 |
10 | | -... return AgentResponse(...) |
| 11 | +... return "Hello, world!" |
11 | 12 | >>> |
12 | 13 | >>> server = AgentRunServer(invoke_agent=invoke_agent) |
13 | 14 | >>> server.start(host="0.0.0.0", port=8080) |
14 | 15 |
|
15 | | -Example (异步处理): |
16 | | ->>> async def invoke_agent(request: AgentRequest) -> AgentResponse: |
17 | | -... # 异步实现你的 Agent 逻辑 |
18 | | -... return AgentResponse(...) |
19 | | ->>> |
20 | | ->>> server = AgentRunServer(invoke_agent=invoke_agent) |
21 | | ->>> server.start() |
| 16 | +Example (使用生命周期钩子 - 同步,推荐): |
| 17 | +>>> def invoke_agent(request: AgentRequest): |
| 18 | +... hooks = request.hooks |
| 19 | +... |
| 20 | +... # 发送步骤开始事件 (使用 emit_* 同步方法) |
| 21 | +... yield hooks.emit_step_start("processing") |
| 22 | +... |
| 23 | +... # 处理逻辑... |
| 24 | +... yield "Hello, " |
| 25 | +... yield "world!" |
| 26 | +... |
| 27 | +... # 发送步骤结束事件 |
| 28 | +... yield hooks.emit_step_finish("processing") |
22 | 29 |
|
23 | | -Example (流式响应): |
| 30 | +Example (使用生命周期钩子 - 异步): |
24 | 31 | >>> async def invoke_agent(request: AgentRequest): |
25 | | -... if request.stream: |
26 | | -... async def stream(): |
27 | | -... for chunk in generate_chunks(): |
28 | | -... yield AgentStreamResponse(...) |
29 | | -... return stream() |
30 | | -... return AgentResponse(...)""" |
| 32 | +... hooks = request.hooks |
| 33 | +... |
| 34 | +... # 发送步骤开始事件 (使用 on_* 异步方法) |
| 35 | +... async for event in hooks.on_step_start("processing"): |
| 36 | +... yield event |
| 37 | +... |
| 38 | +... # 处理逻辑... |
| 39 | +... yield "Hello, world!" |
| 40 | +... |
| 41 | +... # 发送步骤结束事件 |
| 42 | +... async for event in hooks.on_step_finish("processing"): |
| 43 | +... yield event |
31 | 44 |
|
| 45 | +Example (访问原始请求): |
| 46 | +>>> def invoke_agent(request: AgentRequest): |
| 47 | +... # 访问原始请求头 |
| 48 | +... auth = request.raw_headers.get("Authorization") |
| 49 | +... |
| 50 | +... # 访问原始请求体 |
| 51 | +... custom_field = request.raw_body.get("custom_field") |
| 52 | +... |
| 53 | +... return "Hello, world!" |
| 54 | +""" |
| 55 | + |
| 56 | +from .agui_protocol import ( |
| 57 | + AGUIBaseEvent, |
| 58 | + AGUICustomEvent, |
| 59 | + AGUIEvent, |
| 60 | + AGUIEventType, |
| 61 | + AGUILifecycleHooks, |
| 62 | + AGUIMessage, |
| 63 | + AGUIMessagesSnapshotEvent, |
| 64 | + AGUIProtocolHandler, |
| 65 | + AGUIRawEvent, |
| 66 | + AGUIRole, |
| 67 | + AGUIRunAgentInput, |
| 68 | + AGUIRunErrorEvent, |
| 69 | + AGUIRunFinishedEvent, |
| 70 | + AGUIRunStartedEvent, |
| 71 | + AGUIStateDeltaEvent, |
| 72 | + AGUIStateSnapshotEvent, |
| 73 | + AGUIStepFinishedEvent, |
| 74 | + AGUIStepStartedEvent, |
| 75 | + AGUITextMessageContentEvent, |
| 76 | + AGUITextMessageEndEvent, |
| 77 | + AGUITextMessageStartEvent, |
| 78 | + AGUIToolCallArgsEvent, |
| 79 | + AGUIToolCallEndEvent, |
| 80 | + AGUIToolCallResultEvent, |
| 81 | + AGUIToolCallStartEvent, |
| 82 | + create_agui_event, |
| 83 | +) |
32 | 84 | from .model import ( |
| 85 | + AgentEvent, |
| 86 | + AgentLifecycleHooks, |
33 | 87 | AgentRequest, |
34 | 88 | AgentResponse, |
35 | 89 | AgentResponseChoice, |
|
45 | 99 | Tool, |
46 | 100 | ToolCall, |
47 | 101 | ) |
48 | | -from .openai_protocol import OpenAIProtocolHandler |
| 102 | +from .openai_protocol import OpenAILifecycleHooks, OpenAIProtocolHandler |
49 | 103 | from .protocol import ( |
50 | 104 | AsyncInvokeAgentHandler, |
| 105 | + BaseProtocolHandler, |
51 | 106 | InvokeAgentHandler, |
52 | 107 | ProtocolHandler, |
53 | 108 | SyncInvokeAgentHandler, |
|
76 | 131 | "InvokeAgentHandler", |
77 | 132 | "AsyncInvokeAgentHandler", |
78 | 133 | "SyncInvokeAgentHandler", |
79 | | - # Protocol |
| 134 | + # Lifecycle Hooks & Events |
| 135 | + "AgentLifecycleHooks", |
| 136 | + "AgentEvent", |
| 137 | + # Protocol Base |
80 | 138 | "ProtocolHandler", |
| 139 | + "BaseProtocolHandler", |
| 140 | + # Protocol - OpenAI |
81 | 141 | "OpenAIProtocolHandler", |
| 142 | + "OpenAILifecycleHooks", |
| 143 | + # Protocol - AG-UI |
| 144 | + "AGUIProtocolHandler", |
| 145 | + "AGUILifecycleHooks", |
| 146 | + "AGUIEventType", |
| 147 | + "AGUIRole", |
| 148 | + "AGUIBaseEvent", |
| 149 | + "AGUIEvent", |
| 150 | + "AGUIRunStartedEvent", |
| 151 | + "AGUIRunFinishedEvent", |
| 152 | + "AGUIRunErrorEvent", |
| 153 | + "AGUIStepStartedEvent", |
| 154 | + "AGUIStepFinishedEvent", |
| 155 | + "AGUITextMessageStartEvent", |
| 156 | + "AGUITextMessageContentEvent", |
| 157 | + "AGUITextMessageEndEvent", |
| 158 | + "AGUIToolCallStartEvent", |
| 159 | + "AGUIToolCallArgsEvent", |
| 160 | + "AGUIToolCallEndEvent", |
| 161 | + "AGUIToolCallResultEvent", |
| 162 | + "AGUIStateSnapshotEvent", |
| 163 | + "AGUIStateDeltaEvent", |
| 164 | + "AGUIMessagesSnapshotEvent", |
| 165 | + "AGUIRawEvent", |
| 166 | + "AGUICustomEvent", |
| 167 | + "AGUIMessage", |
| 168 | + "AGUIRunAgentInput", |
| 169 | + "create_agui_event", |
82 | 170 | ] |
0 commit comments