Skip to main content

agent_context/context/
event.rs

1//! 变更事件与压缩策略。
2//!
3//! [`ChangeEvent`] 用于 `on_change` 回调通知 incremental 区的消息变动。
4//! [`CompressStrategy`] 定义上下文压缩的策略选项。
5
6use crate::role::Role;
7
8// ---------------------------------------------------------------------------
9// ChangeEvent
10// ---------------------------------------------------------------------------
11
12/// incremental 区的变更事件,通过 [`with_on_change`](crate::AgentContext::with_on_change) 回调发送。
13#[derive(Debug, Clone)]
14pub enum ChangeEvent<M> {
15    /// 追加了一条消息
16    Appended(M),
17    /// 更新了指定索引的消息,同时给出旧值和新值
18    Updated {
19        /// incremental 区索引
20        index: usize,
21        /// 旧消息
22        old: M,
23        /// 新消息
24        new: M,
25    },
26    /// 在指定索引插入了消息
27    Inserted {
28        /// incremental 区索引
29        index: usize,
30        /// 插入的消息
31        message: M,
32    },
33    /// 移除了指定索引的消息
34    Removed {
35        /// incremental 区索引
36        index: usize,
37        /// 被移除的消息
38        message: M,
39    },
40    /// 弹出了最后一条消息
41    Popped(M),
42    /// 按角色保留,其余消息被移除
43    Retained {
44        /// 保留的角色
45        role: Role,
46        /// 被移除的消息列表
47        removed: Vec<M>,
48    },
49    /// 清空了整个 incremental 区
50    Cleared {
51        /// 被清空的所有消息
52        removed: Vec<M>,
53    },
54}
55
56// ---------------------------------------------------------------------------
57// CompressStrategy
58// ---------------------------------------------------------------------------
59
60/// 上下文压缩策略。
61#[derive(Debug, Clone)]
62pub enum CompressStrategy {
63    /// 摘要压缩:保留最近 `keep` 条,将更早的消息交由后端 LLM 生成摘要存入 compressed 区。
64    Summarize {
65        /// 保留的最新消息条数
66        keep: usize,
67        /// 自定义摘要提示词,`None` 时使用内置默认提示词
68        prompt: Option<String>,
69    },
70}