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-frontend): swarmflows running icon

chensheng12345f8998290274a
diff --git a/jiuwenswarm/channels/tui/frontend/src/app-state.ts b/jiuwenswarm/channels/tui/frontend/src/app-state.ts
index 588c922388..f721eaa945 100644
--- a/jiuwenswarm/channels/tui/frontend/src/app-state.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/app-state.ts
@@ -173,6 +173,8 @@ export interface AppSnapshot {
btwOverlayTotal: number;
/** BTW 是否处于活动状态(加载中或 overlay 可见),Esc 优先消费 */
btwActive: boolean;
+ /** /btw 正在回答的问题;非空时显示加载动画。 */
+ btwPendingQuestion: string | null;
}
function formatElapsed(ms: number): string {
@@ -412,6 +414,8 @@ export class CliPiAppState {
private btwOverlayIndex = -1;
/** BTW 是否处于活动状态(加载中或 overlay 可见),用于 Esc 优先级判断 */
private _btwActive = false;
+ /** /btw 正在回答的问题;与回答 overlay 的可见状态分离。 */
+ private btwPendingQuestion: string | null = null;
/** 本地中断请求标志,cancel() 调用时立即置 true,用于 long-running 命令的中断检测。 */
private interruptRequested = false;
/** 是否有一个 cancel interrupt 在途(已发送但未收到 interrupt_result 确认)。
@@ -1099,6 +1103,7 @@ export class CliPiAppState {
btwOverlayIndex: this.btwOverlayIndex,
btwOverlayTotal: this.btwHistory.length,
btwActive: this._btwActive,
+ btwPendingQuestion: this.btwPendingQuestion,
};
}
@@ -1165,6 +1170,7 @@ export class CliPiAppState {
setBtwOverlay: this.setBtwOverlay,
clearBtwOverlay: this.clearBtwOverlay,
setBtwActive: this.setBtwActive,
+ setBtwPendingQuestion: this.setBtwPendingQuestion,
clearEntries: this.clearEntries,
restoreHistory: this.restoreHistory,
exitApp: () => {
@@ -1657,6 +1663,7 @@ export class CliPiAppState {
this.btwHistory = [];
this.btwOverlayIndex = -1;
this._btwActive = false;
+ this.btwPendingQuestion = null;
this.pendingPlanEntrySource = null;
if (this.accentColor !== "default") {
this.accentColor = "default";
@@ -1694,11 +1701,16 @@ export class CliPiAppState {
if (item.kind === "user") {
this.autoRecapState = "idle";
// 用户发送新消息时自动清除 /btw overlay(含历史)
- if (this.btwOverlay !== null || this.btwHistory.length > 0) {
+ if (
+ this.btwOverlay !== null ||
+ this.btwHistory.length > 0 ||
+ this.btwPendingQuestion !== null
+ ) {
this.btwOverlay = null;
this.btwHistory = [];
this.btwOverlayIndex = -1;
this._btwActive = false;
+ this.btwPendingQuestion = null;
}
}
this.emitChange();
@@ -1706,6 +1718,7 @@ export class CliPiAppState {
/** 设置 /btw 侧问题覆盖层(独立于 transcript 渲染,不受滚动影响) */
readonly setBtwOverlay = (question: string, answer: string): void => {
+ this.btwPendingQuestion = null;
this.btwOverlay = { question, answer };
this.btwHistory.push({ question, answer });
this.btwOverlayIndex = this.btwHistory.length - 1;
@@ -1714,19 +1727,35 @@ export class CliPiAppState {
/** 设置 BTW 活动状态(加载中或 overlay 可见),用于 Esc 优先级判断 */
readonly setBtwActive = (active: boolean): void => {
- if (this._btwActive !== active) {
+ const pendingChanged = !active && this.btwPendingQuestion !== null;
+ if (this._btwActive !== active || pendingChanged) {
this._btwActive = active;
+ if (!active) {
+ this.btwPendingQuestion = null;
+ }
+ this.emitChange();
+ }
+ };
+
+ readonly setBtwPendingQuestion = (question: string | null): void => {
+ if (this.btwPendingQuestion !== question) {
+ this.btwPendingQuestion = question;
this.emitChange();
}
};
/** 清除 /btw 侧问题覆盖层(同时清空历史,Esc 视为放弃这批侧问) */
readonly clearBtwOverlay = (): void => {
- if (this.btwOverlay !== null || this.btwHistory.length > 0) {
+ if (
+ this.btwOverlay !== null ||
+ this.btwHistory.length > 0 ||
+ this.btwPendingQuestion !== null
+ ) {
this.btwOverlay = null;
this.btwHistory = [];
this.btwOverlayIndex = -1;
this._btwActive = false;
+ this.btwPendingQuestion = null;
this.emitChange();
}
};
@@ -1751,6 +1780,7 @@ export class CliPiAppState {
this.btwOverlay = null;
this.btwOverlayIndex = -1;
this._btwActive = false;
+ this.btwPendingQuestion = null;
} else {
this.btwOverlayIndex = Math.min(this.btwOverlayIndex, len - 1);
this.btwOverlay = this.btwHistory[this.btwOverlayIndex];
@@ -1803,6 +1833,7 @@ export class CliPiAppState {
this.btwHistory = [];
this.btwOverlayIndex = -1;
this._btwActive = false;
+ this.btwPendingQuestion = null;
this.setStreamingStateInternal(StreamingState.Idle);
this.collapsedToolGroupIds.clear();
this.activeSubtasks.clear();
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/btw.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/btw.ts
index 7af2fa7f37..4eaaa46bb0 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/btw.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/builtins/btw.ts
@@ -9,7 +9,7 @@
* 等待响应期间可按 Esc 取消请求。
*/
import { addError, addInfo } from "../helpers.js";
-import { CommandKind, type CommandContext, type SlashCommand } from "../types.js";
+import { CommandKind, type SlashCommand } from "../types.js";
interface BtwResponse {
status: "ok" | "no_context" | "failed";
@@ -47,17 +47,7 @@ export function createBtwCommand(): SlashCommand {
// 标记 BTW 活动状态,确保 Esc 优先消费(不干扰主会话)
ctx.setBtwActive?.(true);
- // Dim indicator while the side query is running — placed in transcript
- const thinkingId = `btw-thinking-${Date.now()}`;
- ctx.addItem({
- kind: "info",
- id: thinkingId,
- sessionId: ctx.sessionId,
- content: `Answering: ${question} (Esc to cancel)`,
- icon: "💭",
- at: new Date().toISOString(),
- meta: { view: "dim" as const },
- });
+ ctx.setBtwPendingQuestion?.(question);
let overlayShown = false;
@@ -108,6 +98,7 @@ export function createBtwCommand(): SlashCommand {
const message = error instanceof Error ? error.message : String(error);
ctx.addItem(addError(ctx.sessionId, `btw failed: ${message}`));
} finally {
+ ctx.setBtwPendingQuestion?.(null);
// 只有在 overlay 未显示时才清除活动状态
// overlay 显示时保持 btwActive = true,由 Esc 处理清除
if (!overlayShown) {
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
index 4234cc8d98..71c055f777 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
@@ -64,6 +64,8 @@ export interface CommandContext {
clearBtwOverlay?: () => void;
/** 设置 BTW 活动状态(加载中或 overlay 可见),用于 Esc 优先级判断 */
setBtwActive?: (active: boolean) => void;
+ /** 设置 /btw 正在回答的问题;null 表示加载已结束。 */
+ setBtwPendingQuestion?: (question: string | null) => void;
clearEntries: () => void;
restoreHistory: (sessionId: string) => Promise<void>;
exitApp: () => void;
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
index 0d138a6300..a65e244196 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
@@ -1382,6 +1382,11 @@ function formatWorkflowStatus(status: WorkflowStatus): string {
return workflowStatusTone(status)(`${workflowStatusIcon(status)} ${status}`);
}
+function formatSwarmWorkflowListStatus(status: WorkflowStatus): string {
+ const icon = status === "running" ? "●" : workflowStatusIcon(status);
+ return workflowStatusTone(status)(`${icon} ${status}`);
+}
+
function formatWorkflowStatusWord(status: WorkflowStatus): string {
return status === "waiting_for_human" ? "waiting" : status;
}
@@ -5542,7 +5547,7 @@ export class AppScreen implements Component, Focusable {
const progress = workflow.status === "running" ? `${completed}/${total}` : `${total}`;
return {
value: workflow.id,
- label: `${formatWorkflowStatus(workflow.status)} ${workflow.name}`,
+ label: `${formatSwarmWorkflowListStatus(workflow.status)} ${workflow.name}`,
description: `${progress} agents`,
};
});
@@ -8321,9 +8326,12 @@ export class AppScreen implements Component, Focusable {
(workflow) => workflow.status === "running",
);
const hasRunningWorkflow = runningWorkflows.length > 0;
+ const hasBtwLoading = snapshot.btwPendingQuestion !== null;
const runningWorkflow = runningWorkflows[0];
const shouldAnimate =
- !snapshot.isInterrupted && (snapshot.isProcessing || hasRunningTools || teamWorking || hasRunningWorkflow);
+ hasBtwLoading ||
+ (!snapshot.isInterrupted &&
+ (snapshot.isProcessing || hasRunningTools || teamWorking || hasRunningWorkflow));
if (!shouldAnimate) {
const nowMs = Date.now();
if (this.runningStoppedAtMs === null) {
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts b/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts
index 4d4d607516..3e2447e037 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts
@@ -4,7 +4,7 @@ import { renderTeamPanel } from "./components/team-panel.js";
import { isTeamWorking } from "./components/team-shared.js";
import { renderTodoList } from "./components/todo-list.js";
import { APP_SCREEN_KEY_BINDINGS } from "./keymap.js";
-import { padToWidth, renderWrappedText } from "./rendering/text.js";
+import { padToWidth, renderStyledMarkdownLines, renderWrappedText } from "./rendering/text.js";
import { palette } from "./theme.js";
import { buildTranscriptLines } from "./transcript-renderer.js";
import { loadTuiConfig } from "../core/tui-config-store.js";
@@ -255,9 +255,9 @@ function renderBtwOverlay(
};
}
- const answerLines = overlay.answer
- .split("\n")
- .flatMap((line) => renderWrappedText(safeWidth, line, palette.text.secondary));
+ const answerLines = renderStyledMarkdownLines(safeWidth, overlay.answer, {
+ color: palette.text.secondary,
+ });
const maxOffset = Math.max(0, answerLines.length - bodyHeight);
const offset = Math.min(maxOffset, Math.max(0, Math.floor(scrollOffset)));
const visibleAnswerLines = answerLines.slice(offset, offset + bodyHeight);
@@ -298,6 +298,19 @@ function buildShortcutLines(width: number): string[] {
return lines;
}
+function renderBtwLoading(width: number, question: string, animationPhase: number): string[] {
+ const pulseTone = [
+ palette.text.dim,
+ palette.text.secondary,
+ palette.text.accent,
+ palette.text.secondary,
+ ][animationPhase % 4]!;
+ return renderWrappedText(
+ width,
+ `${pulseTone("●")} ${palette.text.dim(`Answering: ${question} (Esc to cancel)`)}`,
+ );
+}
+
export function buildAppScreenLines(snapshot: AppSnapshot, options: ScreenLayoutOptions): string[] {
const statusLines = buildStatusLines(
snapshot,
@@ -351,11 +364,15 @@ export function buildAppScreenLines(snapshot: AppSnapshot, options: ScreenLayout
options.viewedTeamMemberId,
)
: [];
+ const btwLoadingLines = snapshot.btwPendingQuestion
+ ? renderBtwLoading(options.width, snapshot.btwPendingQuestion, options.animationPhase)
+ : [];
const fixedLinesBeforeBtw = [
...todoLines,
...(todoLines.length > 0 && teamPanelLines.length > 0 ? [" ".repeat(options.width)] : []),
...teamPanelLines,
...options.questionLines,
+ ...btwLoadingLines,
];
const fixedLinesAfterBtw = [
...options.editorLines,
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/theme.ts b/jiuwenswarm/channels/tui/frontend/src/ui/theme.ts
index d2189c29f8..e2fc399bf7 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/theme.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/theme.ts
@@ -283,8 +283,31 @@ export const editorTheme: EditorTheme = {
selectList: selectListTheme,
};
+function stripAnsiSgr(value: string): string {
+ let visible = "";
+ let index = 0;
+ while (index < value.length) {
+ if (value.charCodeAt(index) === 27 && value[index + 1] === "[") {
+ const end = value.indexOf("m", index + 2);
+ if (end !== -1) {
+ index = end + 1;
+ continue;
+ }
+ }
+ visible += value[index];
+ index++;
+ }
+ return visible;
+}
+
export const markdownTheme: MarkdownTheme = {
heading: (value: string) => {
+ const visibleValue = stripAnsiSgr(value);
+ // pi-tui renders the prefix for level 3-6 headings as a separate,
+ // ANSI-styled theme call. Suppress only that prefix-only fragment.
+ if (/^#{3,6} $/.test(visibleValue)) {
+ return "";
+ }
if (value.startsWith("# ")) {
const text = value.slice(2);
return chalk.bold(text);
@@ -324,7 +347,7 @@ export const markdownTheme: MarkdownTheme = {
italic: (value: string) => chalk.italic(value),
strikethrough: (value: string) => chalk.strikethrough(value),
underline: (value: string) => chalk.underline(value),
- highlightCode: (code: string, lang?: string): string[] => {
+ highlightCode: (code: string, _lang?: string): string[] => {
return highlightCodeBlock(code);
},
codeBlockIndent: " ",
diff --git a/jiuwenswarm/channels/tui/frontend/tests/run-tests.mjs b/jiuwenswarm/channels/tui/frontend/tests/run-tests.mjs
index 12910f9468..246f4be79a 100644
--- a/jiuwenswarm/channels/tui/frontend/tests/run-tests.mjs
+++ b/jiuwenswarm/channels/tui/frontend/tests/run-tests.mjs
@@ -202,6 +202,7 @@ const teamSnapshot = {
btwOverlayIndex: -1,
btwOverlayTotal: 0,
btwActive: false,
+ btwPendingQuestion: null,
};
const teamLayoutOptions = {
width: 80,
@@ -229,6 +230,77 @@ const expandedTeamLines = buildAppScreenLines(teamSnapshot, {
});
assert.equal(expandedTeamLines.some((line) => line.includes("teammate")), true);
+const stripAnsi = (value) => value.replace(/\u001b\[[0-9;]*m/g, "");
+const btwMarkdownLines = buildAppScreenLines(
+ {
+ ...teamSnapshot,
+ btwOverlay: {
+ question: "Explain React Hooks",
+ answer: "**React Hooks** use `useState`.\n\n- Manage state",
+ },
+ btwOverlayIndex: 0,
+ btwOverlayTotal: 1,
+ btwActive: true,
+ },
+ teamLayoutOptions,
+);
+const btwMarkdownText = stripAnsi(btwMarkdownLines.join("\n"));
+assert.equal(btwMarkdownText.includes("React Hooks"), true);
+assert.equal(btwMarkdownText.includes("useState"), true);
+assert.equal(btwMarkdownText.includes("**React Hooks**"), false);
+assert.equal(btwMarkdownText.includes("`useState`"), false);
+assert.equal(btwMarkdownText.includes("- Manage state"), false);
+
+const headingCases = [
+ ["#", "Level one"],
+ ["##", "Level two"],
+ ["###", "Level three"],
+ ["####", "Level four"],
+ ["#####", "Level five"],
+ ["######", "Level six"],
+];
+const btwHeadingLines = buildAppScreenLines(
+ {
+ ...teamSnapshot,
+ btwOverlay: {
+ question: "Render headings",
+ answer: `${headingCases.map(([prefix, title]) => `${prefix} ${title}`).join("\n\n")}\n\n\`\`\`text\n### code comment\n\`\`\`\n\n\\### literal marker`,
+ },
+ btwOverlayIndex: 0,
+ btwOverlayTotal: 1,
+ btwActive: true,
+ },
+ teamLayoutOptions,
+);
+const btwHeadingText = stripAnsi(btwHeadingLines.join("\n"));
+for (const [prefix, title] of headingCases) {
+ assert.equal(btwHeadingText.includes(title), true);
+ assert.equal(btwHeadingText.includes(`${prefix} ${title}`), false);
+}
+assert.equal(btwHeadingText.includes("### code comment"), true);
+assert.equal(btwHeadingText.includes("### literal marker"), true);
+
+const btwLoadingSnapshot = {
+ ...teamSnapshot,
+ btwActive: true,
+ btwPendingQuestion: "Explain React Hooks",
+};
+const btwPulseDim = buildAppScreenLines(btwLoadingSnapshot, {
+ ...teamLayoutOptions,
+ animationPhase: 0,
+});
+const btwPulseBright = buildAppScreenLines(btwLoadingSnapshot, {
+ ...teamLayoutOptions,
+ animationPhase: 2,
+});
+assert.equal(visibleWidth("●"), 1);
+assert.equal(
+ stripAnsi(btwPulseDim.join("\n")).includes("● Answering: Explain React Hooks"),
+ true,
+);
+assert.equal(stripAnsi(btwPulseDim.join("\n")), stripAnsi(btwPulseBright.join("\n")));
+assert.notEqual(btwPulseDim.join("\n"), btwPulseBright.join("\n"));
+
const slashCommands = AppScreen.prototype.buildSlashCommands.call({
commands: {
getAll: () => [