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.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-02 18:15:39 +08:00
1 джерело 880835f0ac
коміт c92bd0999b
2 files changed
+29 -2

No files matched your search

+19 -2
Переглянути файл
@@ -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 key={index} style={spanStyle(span)}>
{span.text}
</span>
));
}
export const ToolEntry = memo(function ToolEntry({
task,
entry,
@@ -240,10 +251,16 @@ export const ToolEntry = memo(function ToolEntry({
<pre className={styles.terminalCommand}>
<span className={styles.prompt}>$</span> {commandText}
</pre>
{run?.output && <pre className={styles.terminalOutput}>{run.output}</pre>}
{run?.output && (
<pre className={styles.terminalOutput}>
<ToolOutput text={run.output} />
</pre>
)}
</div>
) : (
<pre className={styles.output}>{entry.output}</pre>
<pre className={styles.output}>
<ToolOutput text={entry.output} />
</pre>
)}
</Fold>
</div>
+10
Переглянути файл
@@ -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(<SessionEditor input={input("A")} />);
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) });