enhance: 改进取消机制 — 流式token和工具调用中也检查cancelled

- streamIteration 中 token 回调检查 cancelled 标志
- executeToolCalls 中每个工具调用前检查 cancelled
- 被取消的工具调用返回 'Cancelled by user'

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
pull/1/head
abel533 1 month ago
parent 1b53357ded
commit d4f9c8104f
  1. 9
      src/main/java/com/claudecode/core/AgentLoop.java

@ -295,7 +295,7 @@ public class AgentLoop {
// 实时输出文本 token // 实时输出文本 token
String text = output.getText(); String text = output.getText();
if (text != null && !text.isEmpty()) { if (text != null && !text.isEmpty() && !cancelled) {
// 第一个 token 到达时通知 UI(停止 spinner) // 第一个 token 到达时通知 UI(停止 spinner)
if (firstToken[0]) { if (firstToken[0]) {
firstToken[0] = false; firstToken[0] = false;
@ -338,6 +338,13 @@ public class AgentLoop {
List<ToolResponseMessage.ToolResponse> toolResponses = new ArrayList<>(); List<ToolResponseMessage.ToolResponse> toolResponses = new ArrayList<>();
for (AssistantMessage.ToolCall toolCall : toolCalls) { 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 toolName = toolCall.name();
String toolArgs = toolCall.arguments(); String toolArgs = toolCall.arguments();
String callId = toolCall.id(); String callId = toolCall.id();

Loading…
Cancel
Save