pub enum AgentEvent {
Show 27 variants
AgentStarted {
session_id: SessionId,
agent_name: String,
model_id: ModelId,
},
AgentEnded {
session_id: SessionId,
finish_reason: AgentFinishReason,
total_usage: Option<Usage>,
steps: u32,
},
StepStarted {
step_index: u32,
model_id: ModelId,
agent_name: String,
},
StepEnded {
step_index: u32,
finish_reason: FinishReason,
usage: Option<Usage>,
},
StepFailed {
step_index: u32,
error: String,
},
TextStarted {
content_index: usize,
},
TextDelta {
content_index: usize,
delta: String,
},
TextEnded {
content_index: usize,
text: String,
},
ReasoningStarted {
content_index: usize,
},
ReasoningDelta {
content_index: usize,
delta: String,
},
ReasoningEnded {
content_index: usize,
text: String,
},
ToolInputStarted {
call_id: ToolCallId,
tool_name: String,
},
ToolInputDelta {
call_id: ToolCallId,
delta: String,
},
ToolInputEnded {
call_id: ToolCallId,
arguments: Value,
},
ToolCalled {
call_id: ToolCallId,
tool_name: String,
arguments: Value,
},
ToolProgress {
call_id: ToolCallId,
message: String,
data: Option<Value>,
},
ToolSucceeded {
call_id: ToolCallId,
tool_name: String,
output: ToolOutput,
},
ToolFailed {
call_id: ToolCallId,
tool_name: String,
error: String,
is_retryable: bool,
},
Retried {
attempt: u32,
error: String,
delay_ms: u64,
},
PruneCompleted {
tokens_freed: u64,
parts_pruned: usize,
},
CompactionStarted {
trigger: CompactTrigger,
strategy: CompactionStrategy,
tokens_before: u64,
},
CompactionDelta {
delta: String,
},
CompactionEnded {
result: CompactionResult,
},
TokenBudgetChanged {
used_tokens: u64,
context_window: u64,
state: TokenBudgetState,
},
ModelSwitched {
from: Option<ModelId>,
to: ModelId,
},
AgentSwitched {
from_agent: Option<AgentId>,
to_agent: AgentId,
agent_name: String,
},
UserPrompted {
content_preview: String,
},
}Expand description
Agent 语义层事件 — Agent loop 在执行过程中产出的已发生事实。
事件是 不可变的已发生事实,消费者只能观察,不能拦截或修改。 如需拦截/修改 Agent 行为,请使用 Hook 系统(future)。
§事件分层
AgentStarted
├── StepStarted
│ ├── TextStarted → TextDelta* → TextEnded
│ ├── ReasoningStarted → ReasoningDelta* → ReasoningEnded
│ ├── ToolInputStarted → ToolInputDelta* → ToolInputEnded
│ │ └── ToolCalled → ToolProgress* → ToolSucceeded / ToolFailed
│ └── StepEnded / StepFailed
├── Retried
├── PruneCompleted
├── CompactionStarted → CompactionDelta* → CompactionEnded
├── TokenBudgetChanged
├── ModelSwitched / AgentSwitched / UserPrompted
└── AgentEnded§Serde 格式
所有事件序列化为 {"type": "snake_case_variant", ...fields} 格式,
与 StreamEvent 保持风格一致。
§Examples
use katu_core::agent_event::AgentEvent;
use katu_core::{SessionId, ModelId};
let event = AgentEvent::AgentStarted {
session_id: SessionId::new(),
agent_name: "coder".into(),
model_id: ModelId::new("gpt-4o"),
};
let json = serde_json::to_string(&event).unwrap();
assert!(json.contains(r#""type":"agent_started""#));Variants§
AgentStarted
Agent loop 开始执行。
AgentEnded
Agent loop 执行结束。
Fields
§
finish_reason: AgentFinishReasonStepStarted
一个推理步骤开始(一次 LLM API 调用)。
StepEnded
一个推理步骤正常完成。
StepFailed
一个推理步骤失败(API 错误、网络错误等)。
TextStarted
文本内容块开始。
TextDelta
文本增量。
TextEnded
文本内容块完成,携带完整文本。
ReasoningStarted
思维链内容块开始。
ReasoningDelta
思维链增量。
ReasoningEnded
思维链内容块完成,携带完整文本。
ToolInputStarted
LLM 开始生成工具调用参数。
ToolInputDelta
工具参数 JSON 增量。
ToolInputEnded
工具参数生成完毕,携带完整参数。
ToolCalled
工具开始执行(参数已解析、权限已通过)。
ToolProgress
工具执行中间进度。
ToolSucceeded
工具执行成功。
ToolFailed
工具执行失败。
Retried
API 调用重试。
PruneCompleted
旧工具输出修剪完成。
Prune 是轻量级的上下文优化,独立于全量压缩。 截断旧的、体积大的工具输出,释放 token 空间。
CompactionStarted
上下文压缩开始。
CompactionDelta
压缩摘要增量。
CompactionEnded
上下文压缩完成。
Fields
§
result: CompactionResult压缩完整结果。
TokenBudgetChanged
Token 用量预算状态变更。
当 token 用量跨越阈值边界时产出,用于 UI 进度条渲染。
Fields
§
state: TokenBudgetState新状态。
ModelSwitched
模型已切换。
AgentSwitched
Agent 已切换(进入或退出子 Agent)。
UserPrompted
用户提交了新 prompt。
Implementations§
Source§impl AgentEvent
impl AgentEvent
Sourcepub fn kind(&self) -> AgentEventKind
pub fn kind(&self) -> AgentEventKind
返回此事件的类型标签。
Sourcepub fn is_terminal(&self) -> bool
pub fn is_terminal(&self) -> bool
是否为终态事件(AgentEnded)。
Sourcepub fn is_text_delta(&self) -> bool
pub fn is_text_delta(&self) -> bool
是否为文本增量事件。
Sourcepub fn as_text_delta(&self) -> Option<&str>
pub fn as_text_delta(&self) -> Option<&str>
提取文本增量内容,非 TextDelta 事件返回 None。
Sourcepub fn is_tool_event(&self) -> bool
pub fn is_tool_event(&self) -> bool
是否为工具生命周期事件。
Sourcepub fn tool_call_id(&self) -> Option<&ToolCallId>
pub fn tool_call_id(&self) -> Option<&ToolCallId>
提取关联的 ToolCallId(如果是工具事件)。
Trait Implementations§
Source§impl Clone for AgentEvent
impl Clone for AgentEvent
Source§fn clone(&self) -> AgentEvent
fn clone(&self) -> AgentEvent
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for AgentEvent
impl Debug for AgentEvent
Source§impl<'de> Deserialize<'de> for AgentEvent
impl<'de> Deserialize<'de> for AgentEvent
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl PartialEq for AgentEvent
impl PartialEq for AgentEvent
Source§fn eq(&self, other: &AgentEvent) -> bool
fn eq(&self, other: &AgentEvent) -> bool
Tests for
self and other values to be equal, and is used by ==.Source§impl Serialize for AgentEvent
impl Serialize for AgentEvent
impl StructuralPartialEq for AgentEvent
Auto Trait Implementations§
impl Freeze for AgentEvent
impl RefUnwindSafe for AgentEvent
impl Send for AgentEvent
impl Sync for AgentEvent
impl Unpin for AgentEvent
impl UnsafeUnpin for AgentEvent
impl UnwindSafe for AgentEvent
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more