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工具调用折叠

openJiuwen-bot410bf68d164b
diff --git a/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/compact-tool-renderers.ts b/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/compact-tool-renderers.ts
index 2c8d647b17..a6f4eee615 100644
--- a/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/compact-tool-renderers.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/compact-tool-renderers.ts
@@ -126,6 +126,14 @@ export function renderCompactToolLines(
return lines;
}
+export function renderHiddenToolsLine(width: number, hiddenCount: number): string[] {
+ return renderToolBranch(
+ width,
+ `+${hiddenCount} earlier tool${hiddenCount === 1 ? "" : "s"} (${TOOL_EXPAND_HINT})`,
+ palette.text.dim,
+ );
+}
+
export function renderCollapsedCompactSummaryLines(
entry: {
id: string;
diff --git a/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/tool-group-message.ts b/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/tool-group-message.ts
index 6d90f8d85f..16b2bf0515 100644
--- a/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/tool-group-message.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/tool-group-message.ts
@@ -1,7 +1,8 @@
import type { Component } from "@mariozechner/pi-tui";
import type { HistoryItem } from "../../../core/types.js";
-import { renderCompactToolLines } from "./compact-tool-renderers.js";
+import { renderCompactToolLines, renderHiddenToolsLine } from "./compact-tool-renderers.js";
import { renderDetailedToolLines } from "./detailed-tool-renderers.js";
+import { MAX_VISIBLE_TOOLS } from "./tool-render-shared.js";
export class ToolGroupMessageComponent implements Component {
constructor(
@@ -15,20 +16,31 @@ export class ToolGroupMessageComponent implements Component {
render(width: number): string[] {
const lines: string[] = [];
- const tools = this.collapsed ? this.entry.tools.slice(-1) : this.entry.tools;
+ const allTools = this.entry.tools;
- for (const [index, tool] of tools.entries()) {
- lines.push(
- ...(this.showDetails
- ? renderDetailedToolLines(tool, width, {
- showDetails: this.showDetails,
- animationPhase: this.animationPhase,
- })
- : renderCompactToolLines(tool, width, this.animationPhase)),
- );
+ if (this.showDetails) {
+ const tools = this.collapsed ? allTools.slice(-1) : allTools;
+ for (const [index, tool] of tools.entries()) {
+ lines.push(
+ ...renderDetailedToolLines(tool, width, {
+ showDetails: this.showDetails,
+ animationPhase: this.animationPhase,
+ }),
+ );
+ if (index < tools.length - 1) {
+ lines.push(" ".repeat(width));
+ }
+ }
+ } else {
+ const visibleTools = this.collapsed ? allTools.slice(-1) : allTools.slice(-MAX_VISIBLE_TOOLS);
+ const hiddenCount = allTools.length - visibleTools.length;
+
+ for (const tool of visibleTools) {
+ lines.push(...renderCompactToolLines(tool, width, this.animationPhase));
+ }
- if (this.showDetails && index < tools.length - 1) {
- lines.push(" ".repeat(width));
+ if (hiddenCount > 0) {
+ lines.push(...renderHiddenToolsLine(width, hiddenCount));
}
}
diff --git a/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/tool-render-shared.ts b/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/tool-render-shared.ts
index 36e0674ffd..26dd779788 100644
--- a/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/tool-render-shared.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/ui/components/tools/tool-render-shared.ts
@@ -1,6 +1,8 @@
import { summarize } from "../../rendering/text.js";
import { isPlainObject, tryParseStructuredText } from "./tool-structured-data.js";
+export const MAX_VISIBLE_TOOLS = 3;
+
export * from "./tool-kind-utils.js";
export * from "./tool-line-renderers.js";
export * from "./tool-structured-data.js";
diff --git a/jiuwenclaw/channels/tui/frontend/src/ui/transcript-renderer.ts b/jiuwenclaw/channels/tui/frontend/src/ui/transcript-renderer.ts
index f9fb7021ce..6171dffd52 100644
--- a/jiuwenclaw/channels/tui/frontend/src/ui/transcript-renderer.ts
+++ b/jiuwenclaw/channels/tui/frontend/src/ui/transcript-renderer.ts
@@ -1,6 +1,8 @@
import type { AppSnapshot } from "../app-state.js";
+import type { HistoryItem } from "../core/types.js";
import { renderHistoryEntry } from "./components/messages/index.js";
import { shouldEmphasizeAssistantTransition } from "./components/messages/presentation-rules.js";
+import { MAX_VISIBLE_TOOLS, TOOL_EXPAND_HINT, renderToolBranch } from "./components/tools/tool-render-shared.js";
import { prefixedLines, renderStyledMarkdownLines } from "./rendering/text.js";
import { palette } from "./theme.js";
import { selectTranscriptEntries } from "./transcript-entry-selection.js";
@@ -19,6 +21,47 @@ function renderPendingUserInput(width: number, content: string): string[] {
return prefixedLines(lines, width, "> ", palette.text.user, " ");
}
+function isToolEntry(entry: HistoryItem): boolean {
+ return entry.kind === "tool_group" || entry.kind === "collapsed_tool_group";
+}
+
+function computeHiddenToolIndices(entries: HistoryItem[]): Set<number> {
+ const hidden = new Set<number>();
+ let runStart = -1;
+ let runLength = 0;
+
+ const flushRun = () => {
+ if (runLength > MAX_VISIBLE_TOOLS && runStart >= 0) {
+ const hideCount = runLength - MAX_VISIBLE_TOOLS;
+ for (let j = runStart; j < runStart + hideCount; j++) {
+ hidden.add(j);
+ }
+ }
+ runStart = -1;
+ runLength = 0;
+ };
+
+ for (let i = 0; i < entries.length; i++) {
+ if (isToolEntry(entries[i]!)) {
+ if (runStart === -1) runStart = i;
+ runLength++;
+ } else {
+ flushRun();
+ }
+ }
+ flushRun();
+
+ return hidden;
+}
+
+function renderHiddenToolsSummary(width: number, hiddenCount: number): string[] {
+ return renderToolBranch(
+ width,
+ `+${hiddenCount} earlier tool${hiddenCount === 1 ? "" : "s"} (${TOOL_EXPAND_HINT})`,
+ palette.text.dim,
+ );
+}
+
export function buildTranscriptLines(
snapshot: AppSnapshot,
width: number,
@@ -41,10 +84,33 @@ export function buildTranscriptLines(
allLines.push(...renderPendingUserInput(width, pendingInput));
}
- for (const [index, entry] of displayEntries.entries()) {
- const nextEntry = displayEntries[index + 1];
+ const hiddenIndices = showToolDetails
+ ? new Set<number>()
+ : computeHiddenToolIndices(displayEntries);
+ let pendingHiddenCount = 0;
+
+ for (let i = 0; i < displayEntries.length; i++) {
+ if (hiddenIndices.has(i)) {
+ pendingHiddenCount++;
+ continue;
+ }
+
+ if (pendingHiddenCount > 0) {
+ allLines.push(...renderHiddenToolsSummary(width, pendingHiddenCount));
+ pendingHiddenCount = 0;
+ }
+
+ const entry = displayEntries[i]!;
+ let nextVisible: HistoryItem | undefined;
+ for (let j = i + 1; j < displayEntries.length; j++) {
+ if (!hiddenIndices.has(j)) {
+ nextVisible = displayEntries[j]!;
+ break;
+ }
+ }
+
const collapsed =
- (entry.kind === "tool_group" || entry.kind === "collapsed_tool_group") &&
+ isToolEntry(entry) &&
snapshot.collapsedToolGroupIds.has(entry.id);
const rendered = renderHistoryEntry(entry, width, {
compact: snapshot.transcriptMode === "compact",
@@ -60,12 +126,16 @@ export function buildTranscriptLines(
allLines.push(" ".repeat(width));
}
if (
- shouldEmphasizeAssistantTransition(entry, nextEntry, snapshot.transcriptMode === "compact")
+ shouldEmphasizeAssistantTransition(entry, nextVisible, snapshot.transcriptMode === "compact")
) {
allLines.push(" ".repeat(width));
}
}
+ if (pendingHiddenCount > 0) {
+ allLines.push(...renderHiddenToolsSummary(width, pendingHiddenCount));
+ }
+
if (displayEntries.length > 0 && showPendingInput) {
allLines.push(...renderPendingUserInput(width, pendingInput));
}