From d4f9c8104f4dec92cdbe8315052ad90b4377d08a Mon Sep 17 00:00:00 2001 From: abel533 Date: Sat, 4 Apr 2026 17:40:22 +0800 Subject: [PATCH] =?UTF-8?q?enhance:=20=E6=94=B9=E8=BF=9B=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=20=E2=80=94=20=E6=B5=81=E5=BC=8Ftoken?= =?UTF-8?q?=E5=92=8C=E5=B7=A5=E5=85=B7=E8=B0=83=E7=94=A8=E4=B8=AD=E4=B9=9F?= =?UTF-8?q?=E6=A3=80=E6=9F=A5cancelled?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - streamIteration 中 token 回调检查 cancelled 标志 - executeToolCalls 中每个工具调用前检查 cancelled - 被取消的工具调用返回 'Cancelled by user' Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/main/java/com/claudecode/core/AgentLoop.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/claudecode/core/AgentLoop.java b/src/main/java/com/claudecode/core/AgentLoop.java index 780498e..bfb1270 100644 --- a/src/main/java/com/claudecode/core/AgentLoop.java +++ b/src/main/java/com/claudecode/core/AgentLoop.java @@ -295,7 +295,7 @@ public class AgentLoop { // 实时输出文本 token String text = output.getText(); - if (text != null && !text.isEmpty()) { + if (text != null && !text.isEmpty() && !cancelled) { // 第一个 token 到达时通知 UI(停止 spinner) if (firstToken[0]) { firstToken[0] = false; @@ -338,6 +338,13 @@ public class AgentLoop { List toolResponses = new ArrayList<>(); for (AssistantMessage.ToolCall toolCall : toolCalls) { + // 检查取消标志 + if (cancelled) { + toolResponses.add(new ToolResponseMessage.ToolResponse( + toolCall.id(), toolCall.name(), "Cancelled by user")); + continue; + } + String toolName = toolCall.name(); String toolArgs = toolCall.arguments(); String callId = toolCall.id();