From c92bd0999be3a2bbda9155c00db09d445acb5c5c Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 2 Dec 2026 18:15:39 +0800 Subject: [PATCH] fix(agents): render ANSI styles in tool output (#7495) Agent tool cards now render ANSI colours and emphasis in command output with the shared console parser, instead of showing raw escape codes. Plain output keeps its text rendering path. --- src/features/agents/session/toolEntries.tsx | 21 +++++++++++++++++++-- src/features/agents/sessionState.test.tsx | 10 ++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/features/agents/session/toolEntries.tsx b/src/features/agents/session/toolEntries.tsx index 3298401df..30c38732e 100644 --- a/src/features/agents/session/toolEntries.tsx +++ b/src/features/agents/session/toolEntries.tsx @@ -2,6 +2,7 @@ import type { AgentEntry, AgentTask } from "@/bindings"; import { useTx } from "@/platform/i18n"; import { basename } from "@/platform/project"; import { Icon, IconButton, Spinner, T } from "@/ui"; +import { AnsiStream, spanStyle } from "@/ui/ansi"; import clsx from "clsx"; import { memo, useLayoutEffect } from "react"; import { AgentReply } from "../AgentReply"; @@ -108,6 +109,16 @@ export function pathInTitle(title: string, path: string): { from: number; to: nu return { from, to: at + name.length + (end < 0 ? rest.length : end) }; } +function ToolOutput({ text }: { text: string }) { + if (!text.includes("\x1b")) return text; + return new AnsiStream().parse(text).map((span, index) => ( + // oxlint-disable-next-line react/no-array-index-key -- ANSI spans follow their order in the output. + + {span.text} + + )); +} + export const ToolEntry = memo(function ToolEntry({ task, entry, @@ -240,10 +251,16 @@ export const ToolEntry = memo(function ToolEntry({
               $ {commandText}
             
- {run?.output &&
{run.output}
} + {run?.output && ( +
+                
+              
+ )} ) : ( -
{entry.output}
+
+            
+          
)} diff --git a/src/features/agents/sessionState.test.tsx b/src/features/agents/sessionState.test.tsx index 1bbac4968..c5feaeeb8 100644 --- a/src/features/agents/sessionState.test.tsx +++ b/src/features/agents/sessionState.test.tsx @@ -1272,6 +1272,16 @@ describe("scrolling", () => { }); describe("tool calls", () => { + it.each(["pnpm dev:app", null])("renders ANSI diagnostics with command %s", (command) => { + const output = "\x1b[1m\x1b[33mwarning\x1b[0m: 未使用的字段\n\x1b[94m12 |\x1b[0m authors"; + const entry = tool("cargo", { kind: "execute", status: "running", command, output }); + useAgents.setState({ entries: new Map([["A", [entry]]]) }); + render(); + const warning = screen.getByText("warning"); + expect(warning.style.color).toBe("var(--ansi-yellow)"); + expect(warning.closest("pre")?.textContent).toBe("warning: 未使用的字段\n12 | authors"); + }); + it.each(["pending", "running"] as const)("keeps %s tools in their run, named on its summary", async (status) => { backend.entries.set("A", [tool("done one"), tool("done two"), tool("live", { status, output: "Reading files…" })]); useAgents.setState({ entries: new Map(backend.entries) });