Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: tui /mode切出code.team时提示确认取消任务

jingjinduo5156edc77ae8
diff --git a/jiuwenswarm/channels/tui/frontend/src/app-state.ts b/jiuwenswarm/channels/tui/frontend/src/app-state.ts
index 2743a67e41..fab07575e1 100644
--- a/jiuwenswarm/channels/tui/frontend/src/app-state.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/app-state.ts
@@ -210,6 +210,8 @@ export class CliPiAppState {
/** 本地中断请求标志,cancel() 调用时立即置 true,用于 long-running 命令的中断检测。 */
private interruptRequested = false;
private lastVisibleUserRequest: VisibleUserRequest | null = null;
+ /** 保存 askQuestions 之前的 streamingState,用于在对话框关闭后恢复。 */
+ private streamingStateBeforeQuestion: StreamingState | null = null;
/** 当前回合的起始时间戳,用于在回合结束时计算执行耗时。 */
private turnStartedAt: number | null = null;
/** Harness extension ready info (for file tree display) */
@@ -233,6 +235,7 @@ export class CliPiAppState {
},
setStreamingState: (state) => {
this.streamingState = state;
+ this.emitChange();
},
setPendingQuestion: (question) => {
this.pendingQuestion = question;
@@ -669,6 +672,11 @@ export class CliPiAppState {
getUsageSummary: () => this.getUsageSummary(),
restartStatusLine: () => this.restartStatusLinePoll(),
getStatusLineJsonInput: () => this.buildStatusLineJsonInput(),
+ hasRunningTeamTasks: () => {
+ const snapshot = this.getSnapshot();
+ // Use cancellableWork which covers all stages: processing, running tools, subtasks, team working
+ return snapshot.cancellableWork;
+ },
};
}
@@ -1023,7 +1031,9 @@ export class CliPiAppState {
const resolver = this.localPendingQuestion;
this.localPendingQuestion = null;
this.pendingQuestion = null;
- this.streamingState = StreamingState.Idle;
+ // 恢复之前的 streamingState
+ this.streamingState = this.streamingStateBeforeQuestion ?? StreamingState.Idle;
+ this.streamingStateBeforeQuestion = null;
resolver.resolve(answers);
this.emitChange();
return;
@@ -1054,6 +1064,7 @@ export class CliPiAppState {
}
this.pendingQuestion = null;
this.streamingState = StreamingState.Idle;
+ this.streamingStateBeforeQuestion = null;
this.emitChange();
}
@@ -1073,6 +1084,8 @@ export class CliPiAppState {
}
const requestId = `local_${Date.now().toString(16)}_${Math.random().toString(36).slice(2, 6)}`;
+ // 保存之前的 streamingState,用于在对话框关闭后恢复(如果之前是在运行状态)
+ this.streamingStateBeforeQuestion = this.streamingState;
this.pendingQuestion = {
requestId,
source,
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/mode.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/mode.ts
index 6514abd4d7..ce3f239bb3 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/mode.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/mode.ts
@@ -1,5 +1,6 @@
import type { AutocompleteItem } from "@mariozechner/pi-tui";
import type { ClientMode } from "../../modes.js";
+import { isTeamMode } from "../../modes.js";
import { makeItem } from "../helpers.js";
import { CommandKind, type SlashCommand } from "../types.js";
@@ -81,6 +82,34 @@ export function createModeCommand(): SlashCommand {
);
return;
}
+
+ // Check if leaving team mode with running tasks
+ const currentMode = ctx.mode;
+ const isLeavingTeamMode = isTeamMode(currentMode) && !isTeamMode(nextMode);
+ if (isLeavingTeamMode && ctx.hasRunningTeamTasks?.()) {
+ const answers = await ctx.askQuestions(
+ [
+ {
+ header: "模式切换",
+ question: `当前有 team 任务正在运行,切换到 ${nextMode} 模式会中断这些任务。`,
+ options: [
+ { label: "中断任务并切换", description: "停止当前任务,切换到新模式" },
+ { label: "取消切换", description: "继续执行当前任务" },
+ ],
+ },
+ ],
+ "mode_switch_confirm",
+ );
+
+ const selected = answers[0]?.selected_options?.[0];
+ if (selected !== "中断任务并切换") {
+ ctx.addItem(makeItem(ctx.sessionId, "info", "模式切换已取消", "m"));
+ return;
+ }
+ // User confirmed interrupt, send cancel request
+ ctx.sendEventOnly("chat.interrupt", { intent: "cancel", mode: currentMode });
+ }
+
try {
await ctx.request("mode.set", { mode: nextMode });
} catch {
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/switch.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/switch.ts
index 279de3cd39..1eebe57cb9 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/switch.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/switch.ts
@@ -1,4 +1,5 @@
import type { ClientMode } from "../../modes.js";
+import { isTeamMode } from "../../modes.js";
import { makeItem } from "../helpers.js";
import { CommandKind, type SlashCommand } from "../types.js";
@@ -47,6 +48,33 @@ export function createSwitchCommand(): SlashCommand {
return;
}
+ // Check if leaving team mode with running tasks
+ const currentMode = ctx.mode;
+ const isLeavingTeamMode = isTeamMode(currentMode) && !isTeamMode(requestedMode);
+ if (isLeavingTeamMode && ctx.hasRunningTeamTasks?.()) {
+ const answers = await ctx.askQuestions(
+ [
+ {
+ header: "模式切换",
+ question: `当前有 team 任务正在运行,切换到 ${requestedMode} 模式会中断这些任务。`,
+ options: [
+ { label: "中断任务并切换", description: "停止当前任务,切换到新模式" },
+ { label: "取消切换", description: "继续执行当前任务" },
+ ],
+ },
+ ],
+ "mode_switch_confirm",
+ );
+
+ const selected = answers[0]?.selected_options?.[0];
+ if (selected !== "中断任务并切换") {
+ ctx.addItem(makeItem(ctx.sessionId, "info", "模式切换已取消", "s"));
+ return;
+ }
+ // User confirmed interrupt, send cancel request
+ ctx.sendEventOnly("chat.interrupt", { intent: "cancel", mode: currentMode });
+ }
+
try {
await ctx.request("mode.set", { mode: requestedMode });
} catch {
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
index dd27b4744c..fafb842a27 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
@@ -94,6 +94,8 @@ export interface CommandContext {
restartStatusLine?: () => void;
/** Get the current JSON data that would be piped to the statusline command */
getStatusLineJsonInput?: () => Record<string, unknown>;
+ /** Check if there are running team-related tasks that would be interrupted by mode switch */
+ hasRunningTeamTasks?: () => boolean;
}
export interface SlashCommand {
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
index 5cd34db9fe..0e7155ae51 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
@@ -1389,6 +1389,63 @@ export class AppScreen implements Component, Focusable {
}
if (text.startsWith("/")) {
+ // Check for mode switch when there's ongoing work
+ if (/^\/(?:mode|switch)\s/.test(text) && snapshot.cancellableWork) {
+ const currentMode = snapshot.mode;
+ const isTeamMode = currentMode === "code.team" || currentMode === "team";
+ // Parse the target mode from the command
+ const modeMatch = text.match(/^\/(?:mode|switch)\s+(\S+)/);
+ const targetMode = modeMatch?.[1] ?? "";
+ const targetIsTeamMode = targetMode === "code.team" || targetMode === "team";
+ // Only warn when leaving team mode
+ if (isTeamMode && !targetIsTeamMode) {
+ const answers = await this.state.askQuestions(
+ [
+ {
+ header: "模式切换",
+ question: `当前有任务正在运行,切换到 ${targetMode} 模式会中断这些任务。`,
+ options: [
+ { label: "中断任务并切换", description: "停止当前任务,切换到新模式" },
+ { label: "取消切换", description: "继续执行当前任务" },
+ ],
+ },
+ ],
+ "mode_switch_confirm",
+ );
+ const selected = answers[0]?.selected_options?.[0];
+ if (selected !== "中断任务并切换") {
+ this.state.addItem(addInfo(snapshot.sessionId, "模式切换已取消", "m"));
+ this.editor.addToHistory(text);
+ this.editor.setText("");
+ return;
+ }
+ // User confirmed, send cancel request and wait for it to complete
+ this.state.sendEventOnly("chat.interrupt", { intent: "cancel", mode: currentMode });
+ // Wait for the interrupt to complete: streamingState changes and entries are updated
+ // Since setStreamingState now calls emitChange, we just need to poll for state change
+ const waitForInterrupt = (timeoutMs = 10000): Promise<void> => {
+ return new Promise((resolve) => {
+ const startTime = Date.now();
+ const check = () => {
+ const elapsed = Date.now() - startTime;
+ if (elapsed >= timeoutMs) {
+ resolve();
+ return;
+ }
+ const snap = this.state.getSnapshot();
+ if (!snap.cancellableWork && snap.streamingState !== "responding") {
+ // Give a brief delay to ensure termination entries are rendered
+ setTimeout(resolve, 50);
+ } else {
+ setTimeout(check, 100);
+ }
+ };
+ check();
+ });
+ };
+ await waitForInterrupt();
+ }
+ }
if (/^\/(?:resume|continue)\s*$/.test(text)) {
this.editor.addToHistory(text);
this.editor.setText("");