Skip to main content

AgentEvent

Enum AgentEvent 

Source
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 开始执行。

Fields

§session_id: SessionId
§agent_name: String
§model_id: ModelId
§

AgentEnded

Agent loop 执行结束。

Fields

§session_id: SessionId
§finish_reason: AgentFinishReason
§total_usage: Option<Usage>

整个 Agent 运行的累计 token 用量。

§steps: u32

总执行步数。

§

StepStarted

一个推理步骤开始(一次 LLM API 调用)。

Fields

§step_index: u32
§model_id: ModelId
§agent_name: String
§

StepEnded

一个推理步骤正常完成。

Fields

§step_index: u32
§finish_reason: FinishReason
§usage: Option<Usage>

本步骤的 token 用量。

§

StepFailed

一个推理步骤失败(API 错误、网络错误等)。

Fields

§step_index: u32
§error: String
§

TextStarted

文本内容块开始。

Fields

§content_index: usize
§

TextDelta

文本增量。

Fields

§content_index: usize
§delta: String
§

TextEnded

文本内容块完成,携带完整文本。

Fields

§content_index: usize
§text: String
§

ReasoningStarted

思维链内容块开始。

Fields

§content_index: usize
§

ReasoningDelta

思维链增量。

Fields

§content_index: usize
§delta: String
§

ReasoningEnded

思维链内容块完成,携带完整文本。

Fields

§content_index: usize
§text: String
§

ToolInputStarted

LLM 开始生成工具调用参数。

Fields

§call_id: ToolCallId
§tool_name: String
§

ToolInputDelta

工具参数 JSON 增量。

Fields

§call_id: ToolCallId
§delta: String
§

ToolInputEnded

工具参数生成完毕,携带完整参数。

Fields

§call_id: ToolCallId
§arguments: Value
§

ToolCalled

工具开始执行(参数已解析、权限已通过)。

Fields

§call_id: ToolCallId
§tool_name: String
§arguments: Value
§

ToolProgress

工具执行中间进度。

Fields

§call_id: ToolCallId
§message: String

进度消息(如 “正在读取文件…”)。

§data: Option<Value>

可选的结构化进度数据。

§

ToolSucceeded

工具执行成功。

Fields

§call_id: ToolCallId
§tool_name: String
§output: ToolOutput
§

ToolFailed

工具执行失败。

Fields

§call_id: ToolCallId
§tool_name: String
§error: String
§is_retryable: bool

是否可重试。

§

Retried

API 调用重试。

Fields

§attempt: u32

当前重试次数(从 1 开始)。

§error: String

触发重试的错误。

§delay_ms: u64

重试间隔(毫秒)。

§

PruneCompleted

旧工具输出修剪完成。

Prune 是轻量级的上下文优化,独立于全量压缩。 截断旧的、体积大的工具输出,释放 token 空间。

Fields

§tokens_freed: u64

修剪释放的估计 token 数。

§parts_pruned: usize

被修剪的工具输出条数。

§

CompactionStarted

上下文压缩开始。

Fields

§trigger: CompactTrigger

触发原因。

§strategy: CompactionStrategy

使用的压缩策略。

§tokens_before: u64

压缩前的 prompt token 数。

§

CompactionDelta

压缩摘要增量。

Fields

§delta: String
§

CompactionEnded

上下文压缩完成。

Fields

§result: CompactionResult

压缩完整结果。

§

TokenBudgetChanged

Token 用量预算状态变更。

当 token 用量跨越阈值边界时产出,用于 UI 进度条渲染。

Fields

§used_tokens: u64

当前已使用 token 数。

§context_window: u64

模型 context window 大小。

§state: TokenBudgetState

新状态。

§

ModelSwitched

模型已切换。

Fields

§

AgentSwitched

Agent 已切换(进入或退出子 Agent)。

Fields

§from_agent: Option<AgentId>
§to_agent: AgentId
§agent_name: String
§

UserPrompted

用户提交了新 prompt。

Fields

§content_preview: String

用户输入内容的摘要(可能截断,避免事件过大)。

Implementations§

Source§

impl AgentEvent

Source

pub fn kind(&self) -> AgentEventKind

返回此事件的类型标签。

Source

pub fn is_terminal(&self) -> bool

是否为终态事件(AgentEnded)。

Source

pub fn is_text_delta(&self) -> bool

是否为文本增量事件。

Source

pub fn as_text_delta(&self) -> Option<&str>

提取文本增量内容,非 TextDelta 事件返回 None。

Source

pub fn is_tool_event(&self) -> bool

是否为工具生命周期事件。

Source

pub fn tool_call_id(&self) -> Option<&ToolCallId>

提取关联的 ToolCallId(如果是工具事件)。

Source

pub fn is_delta(&self) -> bool

是否为 delta 类事件(高频、增量)。

Trait Implementations§

Source§

impl Clone for AgentEvent

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentEvent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for AgentEvent

Source§

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

Source§

fn eq(&self, other: &AgentEvent) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for AgentEvent

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for AgentEvent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,