agent_context/context/event/inbound.rs
1//! 请求者 → AgentContext 的请求消息与参数。
2//!
3//! 以 `Request` 前缀命名,与 [`super::outbound`] 中的 `Notify` 前缀对应。
4
5use crate::role::Role;
6
7// ---------------------------------------------------------------------------
8// 变更消息
9// ---------------------------------------------------------------------------
10
11/// 追加一条消息到 incremental 区末尾。Reply = `()`。
12///
13/// 通知所有订阅者 [`super::NotifyChange::Appended`]。
14pub struct RequestAppend<M> {
15 /// 要追加的消息
16 pub message: M,
17}
18
19/// 批量追加消息到 incremental 区。Reply = `()`。
20///
21/// 每条消息单独通知所有订阅者 [`super::NotifyChange::Appended`]。
22pub struct RequestExtend<M> {
23 /// 要批量追加的消息列表
24 pub messages: Vec<M>,
25}
26
27/// 替换 incremental 区指定索引的消息。Reply = `Result<(), AgentError>`。
28///
29/// 仅对 incremental 区有效。通知所有订阅者 [`super::NotifyChange::Updated`]。
30pub struct RequestUpdate<M> {
31 /// incremental 区索引
32 pub index: usize,
33 /// 新消息
34 pub message: M,
35}
36
37/// 在 incremental 区指定索引插入消息。Reply = `Result<(), AgentError>`。
38///
39/// 通知所有订阅者 [`super::NotifyChange::Inserted`]。
40pub struct RequestInsert<M> {
41 /// incremental 区插入位置
42 pub index: usize,
43 /// 要插入的消息
44 pub message: M,
45}
46
47/// 移除 incremental 区指定索引的消息。Reply = `Result<(), AgentError>`。
48///
49/// 通知所有订阅者 [`super::NotifyChange::Removed`]。
50pub struct RequestRemove {
51 /// incremental 区索引
52 pub index: usize,
53}
54
55/// 弹出 incremental 区最后一条消息。Reply = `Option<Message>`。
56///
57/// 通知所有订阅者 [`super::NotifyChange::Popped`]。
58pub struct RequestPop;
59
60/// 按角色保留 incremental 区消息,其余移除。Reply = `()`。
61///
62/// 通知所有订阅者 [`super::NotifyChange::Retained`]。
63pub struct RequestRetain {
64 /// 要保留的角色
65 pub role: Role,
66}
67
68/// 清空整个 incremental 区。Reply = `()`。
69///
70/// 通知所有订阅者 [`super::NotifyChange::Cleared`]。
71pub struct RequestClear;
72
73// ---------------------------------------------------------------------------
74// 查询消息
75// ---------------------------------------------------------------------------
76
77/// 获取三区总消息数。Reply = `usize`。
78pub struct RequestLen;
79
80/// 检查三区是否全部为空。Reply = `bool`。
81pub struct RequestIsEmpty;
82
83/// 按全局索引获取消息。Reply = `Option<Message>`。
84///
85/// 索引按 immutable → compressed → incremental 顺序计算。
86/// 越界返回 `None`。
87pub struct RequestGet(pub usize);
88
89/// 获取三区全部消息的拼接结果。Reply = `Vec<Message>`。
90///
91/// 顺序:immutable → compressed → incremental。
92pub struct RequestMessages;
93
94/// 获取 immutable 区的消息副本。Reply = `Vec<Message>`。
95pub struct RequestImmutable;
96
97/// 获取 compressed 区的消息副本。Reply = `Vec<Message>`。
98pub struct RequestCompressed;
99
100/// 获取 incremental 区的消息副本。Reply = `Vec<Message>`。
101pub struct RequestIncremental;
102
103/// 按角色筛选三区全部消息。Reply = `Vec<Message>`。
104pub struct RequestFindByRole(pub Role);
105
106// ---------------------------------------------------------------------------
107// 对话消息
108// ---------------------------------------------------------------------------
109
110/// 非流式对话。Reply = `Result<Response, AgentError>`。
111///
112/// 发送前根据 [`crate::CommonOpts`] 检查上下文是否已满,满则自动压缩或返回错误。
113/// 然后拼接三区消息 + scratch,发送给后端 LLM。
114/// 成功后自动将响应消息存入 incremental 区,通知所有订阅者 [`super::NotifyChange::Appended`]。
115pub struct RequestSend<O> {
116 /// 传递给后端 `send()` 的请求选项
117 pub opts: O,
118}
119
120/// 流式对话。Reply = `Result<AgentSendStream, AgentError>`。
121///
122/// 发送前根据 [`crate::CommonOpts`] 检查上下文是否已满,满则自动压缩或返回错误。
123/// 然后拼接三区消息 + scratch,发送给后端 LLM。
124pub struct RequestSendStream<O> {
125 /// 传递给后端 `send_stream()` 的请求选项
126 pub opts: O,
127}
128
129// ---------------------------------------------------------------------------
130// 压缩消息
131// ---------------------------------------------------------------------------
132
133/// 压缩策略参数。
134#[derive(Debug, Clone)]
135pub enum CompressStrategy {
136 /// 摘要压缩:保留最近 `keep` 条,将更早的消息交由后端 LLM 生成摘要存入 compressed 区。
137 Summarize {
138 /// 保留的最新消息条数
139 keep: usize,
140 /// 自定义摘要提示词,`None` 时使用内置默认提示词
141 prompt: Option<String>,
142 },
143}
144
145/// 触发上下文压缩。Reply = `Result<(), AgentError>`。
146///
147/// 根据 [`CompressStrategy`] 对 incremental 区执行压缩。
148/// 摘要由后端 LLM 生成,通过 `opts` 传递请求选项。
149/// 也可通过 [`crate::CommonOpts::auto_compress`] 在 [`RequestSend`]/[`RequestSendStream`] 时自动触发。
150pub struct RequestCompress<O> {
151 /// 压缩策略
152 pub strategy: CompressStrategy,
153 /// 传递给后端 `send()` 的请求选项
154 pub opts: O,
155}
156
157// ---------------------------------------------------------------------------
158// 工具消息
159// ---------------------------------------------------------------------------
160
161/// 估算三区全部消息的 token 数量。Reply = `usize`。
162///
163/// 委托给后端的 [`estimate_tokens`](crate::ContextBackend::estimate_tokens)。
164/// 失败时降级返回 0。
165pub struct RequestEstimateTokens;
166
167/// 将三区全部消息导出为 JSONL 字符串,每行一条 JSON。Reply = `Result<String, AgentError>`。
168///
169/// 消息按 immutable → compressed → incremental 顺序输出。
170pub struct RequestToJsonl;
171
172/// 从 JSONL 字符串加载消息到 incremental 区。Reply = `Result<(), AgentError>`。
173///
174/// 每行一条 JSON,空行跳过。解析失败或触发 `preserve_reasoning` 时返回错误。
175/// 加载的消息逐条通知所有订阅者 [`super::NotifyChange::Appended`]。
176pub struct RequestFromJsonl {
177 /// JSONL 字符串,每行一条消息
178 pub jsonl: String,
179}