lellm_agent/runtime/event.rs
1//! Agent 层流式事件与停止原因。
2
3/// Agent 层流式事件 — 封闭、强类型、exhaustive match
4///
5/// 终态契约:
6/// - 正常结束:`LoopEnd` 恰好一次,然后 channel 关闭
7/// - 异常结束:`LoopError` 恰好一次,然后 channel 关闭
8/// - 终态事件后不再发送任何事件
9#[derive(Debug)]
10pub enum AgentEvent {
11 /// Provider 层事件
12 Provider(lellm_provider::ProviderEvent),
13 /// 工具开始执行
14 ToolStart { tool_call_id: String, name: String },
15 /// 工具执行完成
16 ToolEnd {
17 tool_call_id: String,
18 result: super::ToolResult,
19 },
20 /// 工具重试(RetryPolicy 触发)
21 Retry {
22 tool_call_id: String,
23 attempt: usize,
24 max_attempts: usize,
25 reason: String,
26 },
27 /// 上下文压缩完成(可观测性事件)
28 ContextCompacted {
29 before_tokens: usize,
30 after_tokens: usize,
31 removed_messages: usize,
32 },
33 /// Agent loop 正常结束(恰好一次,后不再发送)
34 LoopEnd { result: super::ToolUseResult },
35 /// Agent loop 异常结束(恰好一次,后不再发送)— 不含 messages,消费者自行维护
36 LoopError {
37 error: lellm_core::LlmError,
38 iterations: usize,
39 },
40}
41
42/// Agent loop 停止原因 — 描述"为什么停止",而非"响应长什么样"
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub enum StopReason {
45 /// Agent 已获得最终答案并正常结束
46 Complete,
47 /// 达到最大轮次
48 MaxIterationsReached,
49 /// 外部取消(消费者断开、task 终止等)
50 Cancelled,
51 /// 输出预算超限(单轮或总输出 token 超过限制)
52 OutputBudgetExceeded,
53 /// 推理预算超限(thinking token 超过限制)
54 ReasoningBudgetExceeded,
55 // NOTE: LoopDetected 将在 v0.2 LoopDetector 实现时加回
56}
57
58/// Agent 层流式事件通道类型别名
59pub type AgentStream = tokio::sync::mpsc::Receiver<AgentEvent>;