重构: AI聊天可靠性批次(审批累计计数/write_file diff预览/会话隔离补清/token缓存)

This commit is contained in:
2026-06-16 19:56:29 +08:00
parent ba7f35552b
commit d00b30f0ba
12 changed files with 238 additions and 45 deletions

View File

@@ -96,7 +96,9 @@ pub enum AiChatEvent {
/// 工具调用完成
AiToolCallCompleted { id: String, result: serde_json::Value, conversation_id: Option<String> },
/// 需要人工审批
AiApprovalRequired { id: String, name: String, args: serde_json::Value, reason: String, conversation_id: Option<String> },
/// diffAE-2025-03路径 B仅 write_file 注入(旧文件 vs 新内容行级 unified diff
/// 供审批卡即时预览。旧文件不存在(新建)或非 write_file 工具为 None。
AiApprovalRequired { id: String, name: String, args: serde_json::Value, reason: String, diff: Option<String>, conversation_id: Option<String> },
/// 审批结果
AiApprovalResult { id: String, approved: bool, conversation_id: Option<String> },
/// AI 响应完成
@@ -199,6 +201,18 @@ pub struct AiSession {
/// `Arc<Notify>` 可 CloneNotify 内部用 Atomic 计数Clone 等价于 Arc 引用 +1
/// 不影响 AiSession 其他字段语义。
pub notify: Arc<tokio::sync::Notify>,
/// agentic loop 累计已跑 iteration 计数F-260616-11 决策 a
///
/// **语义**「下一轮起算值」。run_agentic_loop 每轮写 `iteration + 1`,审批等待/
/// 达 max 暂停退出时即「当前轮+1」。续跑时由调用方决定如何接
/// - **审批续跑**ai_approve传本字段作 start_iteration**累计**,防多次审批反复
/// 跑满 max_iterations 致 token 失控);
/// - **达 max 续跑**ai_continue_loop重置本字段=0 + 传 0**重计**F-260616-03
/// 决策 a用户点继续=授权重来);
/// - **新消息首次发送**ai_chat_send重置本字段=0新对话生命周期从头计数
///
/// 单例一份:当前 AiSession 是全局单例F-09 B 多会话架构落地时改 per-conv
pub iteration_used: usize,
}
impl AiSession {
@@ -213,6 +227,7 @@ impl AiSession {
agent_language: None,
stop_flag: Arc::new(AtomicBool::new(false)),
notify: Arc::new(tokio::sync::Notify::new()),
iteration_used: 0,
}
}
@@ -261,6 +276,10 @@ pub struct PendingApproval {
pub conversation_id: Option<String>,
/// 重启恢复的积压审批:无 live loop 持有 session.messages,审批后不 save(防空 messages 污染老对话)、不续跑
pub recovered: bool,
/// AE-2025-03路径 Bwrite_file 的行级 unified diff旧文件 vs 新内容)。
/// 仅挂起审批前预读注入,供前端审批卡预览;旧文件不存在(新建)或非 write_file 为 None。
/// recovered 审批(启动重建)无 diff文件可能已变重读无意义
pub diff: Option<String>,
}
/// 工具调用草稿(流式收集时的临时结构)