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

feat(TUI): enhance tool rendering with firstNonEmptyLines utility and formatLeafValue improvements

zhengkailanc839a028953d
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/components/tools/command-tool-renderers.ts b/jiuwenswarm/channels/tui/frontend/src/ui/components/tools/command-tool-renderers.ts
index 7a3811bc71..39814c5b13 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/components/tools/command-tool-renderers.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/components/tools/command-tool-renderers.ts
@@ -19,6 +19,14 @@ import {
toolStateColor,
} from "./tool-render-shared.js";
+function firstNonEmptyLines(...values: unknown[]): string[] {
+ for (const value of values) {
+ const lines = nonEmptyLines(value);
+ if (lines.length > 0) return lines;
+ }
+ return [];
+}
+
export function renderRunTool(
tool: ToolCallDisplay,
width: number,
@@ -50,41 +58,40 @@ export function renderRunTool(
const exitCode =
getNumericArg(payload ?? {}, "exit_code", "exitCode", "code") ??
getNumericArg(args, "exit_code", "exitCode", "code");
- const stdoutLines =
- nonEmptyLines(payload?.stdout) ||
- nonEmptyLines(payload?.output) ||
- nonEmptyLines(payload?.content) ||
- nonEmptyLines(payload?.result);
+ const stdoutLines = firstNonEmptyLines(
+ payload?.stdout,
+ payload?.output,
+ payload?.content,
+ payload?.result,
+ );
const stderrLines = nonEmptyLines(payload?.stderr);
const summaryParts: string[] = [];
if (exitCode !== undefined) {
summaryParts.push(`exit ${exitCode}`);
}
- if (stdoutLines.length > 0) {
- summaryParts.push(`${stdoutLines.length} line${stdoutLines.length === 1 ? "" : "s"}`);
- } else if (stderrLines.length > 0) {
- summaryParts.push(`${stderrLines.length} stderr line${stderrLines.length === 1 ? "" : "s"}`);
+ const outputLines = stdoutLines.length > 0 || stderrLines.length > 0;
+ const summaryText = tool.summary ?? summaryParts.join(" | ");
+ if (summaryText || !outputLines) {
+ lines.push(
+ ...renderToolTail(
+ width,
+ summaryText || summarize(tool.result, 120),
+ toolStateColor(tool),
+ ),
+ );
}
- lines.push(
- ...renderToolTail(
- width,
- tool.summary ?? (summaryParts.join(" | ") || summarize(tool.result, 120)),
- toolStateColor(tool),
- ),
- );
-
- if (stdoutLines.length > 0 || stderrLines.length > 0) {
+ if (outputLines) {
if (tool.isError || tool.status === "timeout") {
lines.push(
...renderPreviewLines(
width,
stderrLines.length > 0 ? stderrLines : stdoutLines,
- palette.status.warning,
+ tool.isError ? palette.status.error : palette.status.warning,
4,
2,
- false,
+ options.showDetails,
"lines",
),
);
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/components/tools/tool-structured-data.ts b/jiuwenswarm/channels/tui/frontend/src/ui/components/tools/tool-structured-data.ts
index 138e4d1914..c40520e624 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/components/tools/tool-structured-data.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/components/tools/tool-structured-data.ts
@@ -105,11 +105,11 @@ function isPrimitive(value: unknown): value is string | number | boolean | null
);
}
-function formatLeafValue(value: unknown): string {
+function formatLeafValue(value: unknown, preserveString = false): string {
if (value === null) return "null";
if (typeof value === "string") {
const compact = value.replace(/\s+/g, " ").trim();
- return compact ? summarize(compact, 96) : '""';
+ return compact ? (preserveString ? compact : summarize(compact, 96)) : '""';
}
if (typeof value === "number" || typeof value === "boolean") return String(value);
return summarize(JSON.stringify(value), 96);
@@ -140,11 +140,11 @@ export function formatStructuredValue(
if (label && !append(`${indent}${label}:`)) return;
const visible = compactLines.slice(0, Math.max(1, maxLines - output.length));
for (const line of visible) {
- if (!append(`${indent} ${summarize(line, 96)}`)) return;
+ if (!append(`${indent} ${maxDepth > 2 ? line : summarize(line, 96)}`)) return;
}
return;
}
- append(`${indent}${label ? `${label}: ` : ""}${formatLeafValue(parsed)}`);
+ append(`${indent}${label ? `${label}: ` : ""}${formatLeafValue(parsed, maxDepth > 2)}`);
return;
}
@@ -163,7 +163,7 @@ export function formatStructuredValue(
for (const item of parsed) {
if (output.length >= maxLines) return;
if (isPrimitive(item)) {
- append(`${childIndent}- ${formatLeafValue(item)}`);
+ append(`${childIndent}- ${formatLeafValue(item, maxDepth > 2)}`);
continue;
}
if (!append(`${childIndent}-`)) return;
@@ -287,6 +287,7 @@ export function getStringListFromValue(value: unknown): string[] {
export function nonEmptyLines(value: unknown): string[] {
if (typeof value !== "string") return [];
return value
+ .replace(/\\r\\n|\\n|\\r/g, "\n")
.split("\n")
.map((line) => line.trimEnd())
.filter((line) => line.trim().length > 0);