From 4071dcc6eeb1a9f3fedb1837d4e1780efde5bc9e Mon Sep 17 00:00:00 2001 From: oudecheng <13802883547@139.com> Date: Thu, 2 Jul 2026 18:27:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E6=9E=84=E5=BB=BA=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E5=B7=B2=E7=94=9F=E6=88=90=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=8C=85=E5=90=AB=E5=9C=A8=E5=8F=96=E6=B6=88=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/agent/agent_loop.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/agent/agent_loop.rs b/src/agent/agent_loop.rs index 9960c73..86f97c6 100644 --- a/src/agent/agent_loop.rs +++ b/src/agent/agent_loop.rs @@ -915,7 +915,7 @@ impl AgentLoop { if let Some(ref mutex) = self.cancel_token { if mutex.lock().await.has_changed().unwrap_or(true) { tracing::info!(iteration, "Agent execution cancelled by user"); - let cancel = Self::build_cancel_result(iteration, emitted_messages.len()); + let cancel = Self::build_cancel_result(iteration, emitted_messages); self.emit_live_tool_call_message(cancel.final_response.clone()).await; return Ok(cancel); } @@ -1027,7 +1027,7 @@ impl AgentLoop { // 显式 drop 外部 stream_callback → delta_tx 释放 → channel 关闭。 drop(stream_callback); let _ = consumer_task.await; - let cancel = Self::build_cancel_result(iteration, emitted_messages.len()); + let cancel = Self::build_cancel_result(iteration, emitted_messages); self.emit_live_tool_call_message(cancel.final_response.clone()).await; return Ok(cancel); } @@ -1035,7 +1035,12 @@ impl AgentLoop { llm_result = result; } } + // LLM 调用正常完成:clone 的 Arc 已被 select! drop, + // 但外部 stream_callback 仍存活。显式 drop 以关闭 mpsc channel, + // 让 consumer_task 能自然退出。 + drop(stream_callback); } else { + // 无取消令牌:stream_callback 被 move 进 chat_with_streaming,调用完成即释放。 llm_result = self.provider.chat_with_streaming(request, stream_callback).await; } @@ -1147,7 +1152,7 @@ impl AgentLoop { let tool_results = if self.cancel_token.is_some() { tokio::select! { _ = self.cancel_signal() => { - let cancel = Self::build_cancel_result(iteration, emitted_messages.len()); + let cancel = Self::build_cancel_result(iteration, emitted_messages); self.emit_live_tool_call_message(cancel.final_response.clone()).await; return Ok(cancel); } @@ -1282,7 +1287,7 @@ impl AgentLoop { if self.cancel_token.is_some() { tokio::select! { _ = self.cancel_signal() => { - let cancel = Self::build_cancel_result(self.max_iterations, emitted_messages.len()); + let cancel = Self::build_cancel_result(self.max_iterations, emitted_messages); self.emit_live_tool_call_message(cancel.final_response.clone()).await; return Ok(cancel); } @@ -1342,15 +1347,22 @@ impl AgentLoop { } /// 构建取消响应,包含已完成的迭代次数和已生成的消息数量。 - fn build_cancel_result(iteration: usize, emitted_count: usize) -> AgentProcessResult { + /// 构建取消响应,将取消通知追加到 `emitted_messages` 末尾后一并返回。 + /// 这样 finalize_result 会把中间消息加入内存历史,确保下一个 LLM 调用有完整上下文。 + fn build_cancel_result( + iteration: usize, + mut emitted_messages: Vec, + ) -> AgentProcessResult { + let emitted_count = emitted_messages.len(); let cancel_message = format!( "\n\n[用户已取消执行。已迭代 {} 次,取消前共生成了 {} 条消息。]", iteration, emitted_count ); let assistant_message = ChatMessage::assistant(cancel_message); + emitted_messages.push(assistant_message.clone()); AgentProcessResult { - final_response: assistant_message.clone(), - emitted_messages: vec![assistant_message], + final_response: assistant_message, + emitted_messages, } }