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
feat(tui): /btw history navigation, delete, and stale-interrupt fix
ldstyle85eb456343007
diff --git a/jiuwenswarm/channels/tui/frontend/src/app-state.ts b/jiuwenswarm/channels/tui/frontend/src/app-state.tsindex f32b83fe70..15c859d3b0 100644--- a/jiuwenswarm/channels/tui/frontend/src/app-state.ts+++ b/jiuwenswarm/channels/tui/frontend/src/app-state.ts@@ -145,6 +145,10 @@ export interface AppSnapshot {currentQueryUsage: CurrentQueryUsage;/** /btw 侧问题覆盖层:独立于 transcript 渲染,不受滚动影响 */btwOverlay: { question: string; answer: string } | null;+ /** 当前 btw overlay 在历史中的下标(-1 表示无选中) */+ btwOverlayIndex: number;+ /** btw 历史总数(用于提示 i/n) */+ btwOverlayTotal: number;/** BTW 是否处于活动状态(加载中或 overlay 可见),Esc 优先消费 */btwActive: boolean;}@@ -367,6 +371,10 @@ export class CliPiAppState {private suppressInterruptResult = false;/** /btw 侧问题覆盖层:独立于 transcript 渲染 */private btwOverlay: { question: string; answer: string } | null = null;+ /** /btw 历史记录(按提问先后顺序),overlay 是其中"当前选中"条目的视图 */+ private btwHistory: { question: string; answer: string }[] = [];+ /** 当前 overlay 在 btwHistory 中的下标,-1 表示无选中 */+ private btwOverlayIndex = -1;/** BTW 是否处于活动状态(加载中或 overlay 可见),用于 Esc 优先级判断 */private _btwActive = false;/** 本地中断请求标志,cancel() 调用时立即置 true,用于 long-running 命令的中断检测。 */@@ -984,6 +992,8 @@ export class CliPiAppState {this.lastStreamActivityAt === null ? null : Date.now() - this.lastStreamActivityAt,currentQueryUsage: { ...this.currentQueryUsage },btwOverlay: this.btwOverlay,+ btwOverlayIndex: this.btwOverlayIndex,+ btwOverlayTotal: this.btwHistory.length,btwActive: this._btwActive,};}@@ -1279,6 +1289,8 @@ export class CliPiAppState {this.resetCurrentUsageTokens();this.workflowRuns = [];this.btwOverlay = null;+ this.btwHistory = [];+ this.btwOverlayIndex = -1;this._btwActive = false;this.pendingPlanEntrySource = null;if (this.accentColor !== "default") {@@ -1316,9 +1328,11 @@ export class CliPiAppState {// 用户发言后重置自动回顾状态,允许下一次空闲时触发新的回顾if (item.kind === "user") {this.autoRecapState = "idle";- // 用户发送新消息时自动清除 /btw overlay- if (this.btwOverlay !== null) {+ // 用户发送新消息时自动清除 /btw overlay(含历史)+ if (this.btwOverlay !== null || this.btwHistory.length > 0) {this.btwOverlay = null;+ this.btwHistory = [];+ this.btwOverlayIndex = -1;this._btwActive = false;}}@@ -1328,6 +1342,8 @@ export class CliPiAppState {/** 设置 /btw 侧问题覆盖层(独立于 transcript 渲染,不受滚动影响) */readonly setBtwOverlay = (question: string, answer: string): void => {this.btwOverlay = { question, answer };+ this.btwHistory.push({ question, answer });+ this.btwOverlayIndex = this.btwHistory.length - 1;this.emitChange();};@@ -1339,15 +1355,44 @@ export class CliPiAppState {}};- /** 清除 /btw 侧问题覆盖层 */+ /** 清除 /btw 侧问题覆盖层(同时清空历史,Esc 视为放弃这批侧问) */readonly clearBtwOverlay = (): void => {- if (this.btwOverlay !== null) {+ if (this.btwOverlay !== null || this.btwHistory.length > 0) {this.btwOverlay = null;+ this.btwHistory = [];+ this.btwOverlayIndex = -1;this._btwActive = false;this.emitChange();}};+ /** 在 btw 历史中前后切换当前 overlay(仅 ≥2 条时生效) */+ readonly navigateBtw = (direction: -1 | 1): void => {+ if (this.btwHistory.length < 2 || this.btwOverlayIndex < 0) return;+ const len = this.btwHistory.length;+ const next = Math.max(0, Math.min(len - 1, this.btwOverlayIndex + direction));+ if (next === this.btwOverlayIndex) return;+ this.btwOverlayIndex = next;+ this.btwOverlay = this.btwHistory[next];+ this.emitChange();+ };++ /** 删除当前 btw 条目;剩余非空则跳到相邻条目,为空则关闭 overlay */+ readonly deleteCurrentBtwEntry = (): void => {+ if (this.btwOverlayIndex < 0 || this.btwHistory.length === 0) return;+ this.btwHistory.splice(this.btwOverlayIndex, 1);+ const len = this.btwHistory.length;+ if (len === 0) {+ this.btwOverlay = null;+ this.btwOverlayIndex = -1;+ this._btwActive = false;+ } else {+ this.btwOverlayIndex = Math.min(this.btwOverlayIndex, len - 1);+ this.btwOverlay = this.btwHistory[this.btwOverlayIndex];+ }+ this.emitChange();+ };+readonly isHelpVisible = (): boolean => {if (this.entries.length === 0) return false;const lastEntry = this.entries[this.entries.length - 1];@@ -1390,6 +1435,8 @@ export class CliPiAppState {this.pendingQuestion = null;this.lastError = null;this.btwOverlay = null;+ this.btwHistory = [];+ this.btwOverlayIndex = -1;this._btwActive = false;this.setStreamingStateInternal(StreamingState.Idle);this.collapsedToolGroupIds.clear();diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/btw.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/btw.tsindex e223ebcd1c..7af2fa7f37 100644--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/btw.ts+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/btw.ts@@ -37,6 +37,13 @@ export function createBtwCommand(): SlashCommand {return;}+ // 清除上一轮残留的中断标志(如 Esc 后快速发消息导致 suppressInterruptResult+ // 吞掉了 clearInterruptRequested,使 interruptRequested 残留为 true)。+ // 否则 await 返回后 isInterruptRequested() 仍为 true,btw 会被误判为已取消。+ if (ctx.isInterruptRequested?.()) {+ ctx.clearInterruptRequested();+ }+// 标记 BTW 活动状态,确保 Esc 优先消费(不干扰主会话)ctx.setBtwActive?.(true);@@ -63,6 +70,7 @@ export function createBtwCommand(): SlashCommand {// Check if cancelled mid-request (Esc pressed during wait)if (ctx.isInterruptRequested?.()) {+ ctx.clearInterruptRequested();ctx.addItem(addInfo(ctx.sessionId, CANCELLED_MSG, "i", { view: "dim" as const }),);@@ -91,6 +99,7 @@ export function createBtwCommand(): SlashCommand {} catch (error) {// Cancelled by Esc → the WS request was aborted; show dim noticeif (ctx.isInterruptRequested?.()) {+ ctx.clearInterruptRequested();ctx.addItem(addInfo(ctx.sessionId, CANCELLED_MSG, "i", { view: "dim" as const }),);diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.tsindex 98a93cb1ca..deea6891e8 100644--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts@@ -2537,6 +2537,8 @@ export class AppScreen implements Component, Focusable {onBtwOverlayScrollOffsetChange: (offset) => {this.btwOverlayScrollOffset = offset;},+ btwOverlayIndex: snapshot.btwOverlayIndex,+ btwOverlayTotal: snapshot.btwOverlayTotal,runningElapsedMs:!snapshot.isInterrupted &&(snapshot.isProcessing ||@@ -3009,6 +3011,24 @@ export class AppScreen implements Component, Focusable {return true;}+ // ←/→ 在 btw 历史间切换(必须在 scroll 之前消费,避免落入 composer)+ if (matchesKey(data, "left")) {+ this.state.navigateBtw(-1);+ this.tui.requestRender();+ return true;+ }+ if (matchesKey(data, "right")) {+ this.state.navigateBtw(1);+ this.tui.requestRender();+ return true;+ }+ // x 删除当前 btw 条目(剩余非空则跳到相邻,为空则关闭 overlay)+ if (data.toLowerCase() === "x") {+ this.state.deleteCurrentBtwEntry();+ this.tui.requestRender();+ return true;+ }+const pageSize = Math.max(1, Math.floor(this.tui.terminal.rows * 0.8));if (matchesKey(data, "up")) {this.btwOverlayScrollOffset = Math.max(0, this.btwOverlayScrollOffset - 1);diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts b/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.tsindex 2b99d3c831..d3ff84c011 100644--- a/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts+++ b/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts@@ -32,6 +32,10 @@ export interface ScreenLayoutOptions {onTranscriptScrollOffsetChange?: (offset: number) => void;btwOverlayScrollOffset?: number;onBtwOverlayScrollOffsetChange?: (offset: number) => void;+ /** 当前 btw overlay 在历史中的下标(-1 无),用于提示 i/n */+ btwOverlayIndex?: number;+ /** btw 历史总数 */+ btwOverlayTotal?: number;}function formatSubtaskStatus(status: string): string {@@ -215,6 +219,8 @@ function renderBtwOverlay(width: number,maxHeight: number,scrollOffset: number,+ overlayIndex?: number,+ overlayTotal?: number,): { lines: string[]; offset: number } {const lines: string[] = [];const safeWidth = Math.max(1, width);@@ -234,10 +240,15 @@ function renderBtwOverlay(// 回答内容:完整展示,不折叠(btw 本身是单轮简短回答,不会过长)const bodyHeight = Math.max(0, availableHeight - lines.length - footerHeight);if (bodyHeight <= 0) {+ const totalEarly = overlayTotal ?? (overlayIndex !== undefined && overlayIndex >= 0 ? 1 : 0);+ const earlyHint =+ totalEarly > 1+ ? `Esc dismiss | ←/→ history ${(overlayIndex ?? 0) + 1}/${totalEarly} | x delete`+ : "Esc dismiss | x delete";return {lines: [padToWidth(palette.text.accent(headerText), safeWidth),- padToWidth(palette.text.dim("Esc to dismiss"), safeWidth),+ padToWidth(palette.text.dim(earlyHint), safeWidth),].slice(-availableHeight),offset: 0,};@@ -254,10 +265,17 @@ function renderBtwOverlay(}const rangeStart = answerLines.length === 0 ? 0 : offset + 1;const rangeEnd = Math.min(offset + visibleAnswerLines.length, answerLines.length);- const scrollHint =- answerLines.length > bodyHeight- ? `Esc to dismiss | ↑/↓ scroll | PgUp/PgDn page | ${rangeStart}-${rangeEnd}/${answerLines.length}`- : "Esc to dismiss";+ const total = overlayTotal ?? (overlayIndex !== undefined && overlayIndex >= 0 ? 1 : 0);+ const showHistory = total > 1;+ const posLabel = showHistory ? `${(overlayIndex ?? 0) + 1}/${total}` : "";+ // 用数组拼接避免尾部多余管道符(不可滚动分支末尾不再出现 " | ")+ const hintParts = ["Esc dismiss"];+ if (showHistory) hintParts.push(`←/→ history ${posLabel}`);+ hintParts.push("x delete");+ if (answerLines.length > bodyHeight) {+ hintParts.push("↑/↓ scroll", "PgUp/PgDn page", `${rangeStart}-${rangeEnd}/${answerLines.length}`);+ }+ const scrollHint = hintParts.join(" | ");// 提示行: Esc to dismisslines.push(padToWidth(palette.text.dim(scrollHint), safeWidth));@@ -374,6 +392,8 @@ export function buildAppScreenLines(snapshot: AppSnapshot, options: ScreenLayoutoptions.width,btwMaxHeight,requestedBtwOverlayScrollOffset,+ options.btwOverlayIndex,+ options.btwOverlayTotal,): { lines: [], offset: 0 };if (renderedBtwOverlay.offset !== requestedBtwOverlayScrollOffset) {