Skip to main content

j_agent/
message_types.rs

1use crate::chat_error::ChatError;
2use crate::storage::ToolCallItem;
3use crate::tools::ImageData;
4use std::sync::mpsc;
5
6// ========== Plan 审批决策类型 ==========
7
8/// Plan 审批决策结果
9#[derive(Debug, Clone, PartialEq, Default)]
10pub enum PlanDecision {
11    /// 无 Plan 决策(普通工具结果)
12    #[default]
13    None,
14    /// 批准计划,保留当前上下文
15    Approve,
16    /// 批准计划并清空探索上下文
17    ApproveAndClearContext,
18    /// 驳回计划,留在 Plan Mode 修改
19    Reject,
20}
21
22// ========== 消息类型(跨线程通信)==========
23
24/// 后台线程发送给 TUI 的消息类型
25pub enum StreamMsg {
26    /// 收到一个流式文本块
27    Chunk,
28    /// LLM 请求执行工具(附带完整工具调用列表)
29    ToolCallRequest(Vec<ToolCallItem>),
30    /// 流式响应完成
31    Done,
32    /// 发生错误
33    Error(ChatError),
34    /// 用户主动取消
35    Cancelled,
36    /// 正在重试(网络波动等可恢复错误)
37    Retrying {
38        /// 当前第几次重试(从 1 开始)
39        attempt: u32,
40        /// 最大重试次数
41        max_attempts: u32,
42        /// 本次等待毫秒数
43        delay_ms: u64,
44        /// 原始错误简要描述
45        error: String,
46    },
47    /// 上下文正在压缩(auto_compact 执行中)
48    Compacting,
49    /// 上下文压缩完成(携带压缩前的消息数量,供 UI 展示)
50    Compacted {
51        /// 压缩前的消息数量
52        messages_before: usize,
53    },
54}
55
56/// 工具执行状态
57#[derive(Debug)]
58pub enum ToolExecStatus {
59    /// 等待用户确认
60    PendingConfirm,
61    /// 执行中
62    Executing,
63    /// 完成(摘要)
64    #[allow(dead_code)]
65    Done(String),
66    /// 用户拒绝
67    Rejected,
68    /// 执行失败
69    Failed(String),
70}
71
72/// 工具调用执行状态(运行时,不序列化)
73#[derive(Debug)]
74pub struct ToolCallStatus {
75    pub tool_call_id: String,
76    pub tool_name: String,
77    pub arguments: String,
78    pub confirm_message: String,
79    pub status: ToolExecStatus,
80    /// 工具调用的简短描述(仅 Bash 工具从 arguments.description 提取)
81    pub tool_description: Option<String>,
82}
83
84/// 主线程 → 后台线程的工具结果消息
85#[derive(Debug)]
86pub struct ToolResultMsg {
87    pub tool_call_id: String,
88    pub result: String,
89    #[allow(dead_code)]
90    pub is_error: bool,
91    /// 工具返回的图片数据(用于多模态模型)
92    pub images: Vec<ImageData>,
93    /// Plan 审批决策(仅 ExitPlanMode 工具会设置非 None 值)
94    pub plan_decision: PlanDecision,
95}
96
97/// Worker 线程完成后写入共享状态,供 UI poll 更新显示
98#[derive(Debug)]
99pub struct CompletedToolResult {
100    pub tool_call_id: String,
101    pub summary: String,
102    pub is_error: bool,
103}
104
105/// ask 工具选项
106#[derive(Debug, Clone)]
107pub struct AskOption {
108    pub label: String,
109    pub description: String,
110}
111
112/// ask 工具单个问题
113#[derive(Debug, Clone)]
114pub struct AskQuestion {
115    pub question: String,
116    pub header: String,
117    pub options: Vec<AskOption>,
118    pub multi_select: bool,
119}
120
121/// ask 工具单题答案
122#[derive(Clone)]
123pub enum AskAnswer {
124    /// 选中的选项索引(单选/多选)
125    Selected(Vec<usize>),
126    /// 自由输入文本
127    FreeText(String),
128}
129
130/// ask 工具 → 主线程的请求消息
131#[derive(Debug)]
132pub struct AskRequest {
133    pub questions: Vec<AskQuestion>,
134    pub response_tx: mpsc::Sender<String>,
135}