优化: AI模块(provider重试兼容+aichat交互+工具卡片可读化+diff高亮)
This commit is contained in:
@@ -253,6 +253,42 @@ pub async fn ai_chat_clear(state: State<'_, AppState>) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 强制发送消息(B-260616-02: L2 发送韧性)
|
||||
///
|
||||
/// 当后端 generating=true 残留(HMR/异常退出等)导致 sendMessage 被拦截时,
|
||||
/// 前端可调此命令强制复位 generating + 清审批,然后走 ai_chat_send 同款流程发消息。
|
||||
/// 等价于"先软停止 → 再发送"的原子操作,避免竞态窗口。
|
||||
#[tauri::command]
|
||||
pub async fn ai_chat_force_send(
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
message: String,
|
||||
language: Option<String>,
|
||||
skill: Option<String>,
|
||||
) -> Result<String, String> {
|
||||
// 原子复位:清 generating + 清积压审批 + 置 stop_flag,与 ai_chat_stop 审批分支一致
|
||||
let old_conv_id = {
|
||||
let mut session = state.ai_session.lock().await;
|
||||
let old = session.active_conversation_id.clone();
|
||||
session.generating = false;
|
||||
session.pending_approvals.clear();
|
||||
session.stop_flag.store(true, Ordering::SeqCst);
|
||||
old
|
||||
};
|
||||
// 通知前端旧生成已结束(若有残留 conv)
|
||||
if let Some(ref cid) = old_conv_id {
|
||||
let _ = app.emit("ai-chat-event", AiChatEvent::AiCompleted {
|
||||
total_tokens: 0,
|
||||
prompt_tokens: 0,
|
||||
completion_tokens: 0,
|
||||
conversation_id: Some(cid.clone()),
|
||||
});
|
||||
}
|
||||
// 复位完成后走 ai_chat_send 同款流程(内部会重新设 generating=true 并 spawn loop)
|
||||
// 直接内联而非递归调 ai_chat_send,避免 IPC 嵌套
|
||||
ai_chat_send(app, state, message, language, skill).await
|
||||
}
|
||||
|
||||
/// 停止当前 AI 生成
|
||||
///
|
||||
/// 两种场景:
|
||||
|
||||
Reference in New Issue
Block a user