Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions packages/app/src/features/runs/event-body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,33 @@ export function traceClasses(type: string) {
};
return variants[type] ?? "bg-white dark:bg-slate-900";
}
const MAX_TOOL_TEXT = 4000;
function shortenMiddle(value: string) {
if (value.length <= MAX_TOOL_TEXT) return value;
const half = Math.floor((MAX_TOOL_TEXT - 3) / 2);
return `${value.slice(0, half)}...${value.slice(-half)}`;
}
export function EventBody({ event }: { event: Event }) {
// Raw HTML is rendered as text. No rehype-raw or innerHTML; Markdown's default
// URL transform excludes javascript/data links, and external links are isolated.
if (["user_message", "assistant_message", "reasoning"].includes(event.type)) return <div className="trace-content"><Markdown components={{ code: ({ children, className }) => <code className={className}>{className && typeof children === "string" ? children.split(/("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\b(?:const|let|var|function|return|if|else|import|export|async|await|class|def|true|false|null|None)\b)/g).map((part, index) => /^(?:["']|const\b|let\b|var\b|function\b|return\b|if\b|else\b|import\b|export\b|async\b|await\b|class\b|def\b|true\b|false\b|null\b|None\b)/.test(part) ? <span key={index} className={part.startsWith('"') || part.startsWith("'") ? "text-emerald-800 dark:text-emerald-300" : "text-factorize-700 dark:text-factorize-500"}>{part}</span> : part) : children}</code>, pre: ({ children }) => <pre tabIndex={0}>{children}</pre>, a: ({ children, href }) => <a href={href} target="_blank" rel="noopener noreferrer">{children}</a> }}>{event.preview}</Markdown></div>;
let content = event.preview;
if (event.type === "tool_call") { try { content = JSON.stringify(JSON.parse(content), null, 2); } catch {} }
if (event.type === "tool_call") {
let args = event.display.arguments;
if (args === undefined) {
try { args = JSON.parse(event.preview); } catch { args = event.preview; }
}
const command = typeof args === "string" ? args : JSON.stringify(args, null, 2);
return <div className="rounded-lg border border-stone-300 bg-white p-3 dark:border-slate-700 dark:bg-slate-900">
<h3 className="mb-2 text-sm font-semibold text-slate-800 dark:text-slate-100">{event.title || "Tool call"}</h3>
<pre aria-label={`${event.title || "Tool"} command`} className="max-h-96 overflow-auto whitespace-pre-wrap break-words rounded bg-stone-50 p-3 text-xs dark:bg-slate-950"><code>{shortenMiddle(command ?? "")}</code></pre>
{Object.keys(event.display).length > 0 && <Disclosure><Summary>Event metadata</Summary><pre className="whitespace-pre-wrap break-words">{JSON.stringify(event.display, null, 2)}</pre></Disclosure>}
</div>;
}
if (event.type === "tool_result") return <div className="rounded-lg border border-stone-300 bg-stone-50 p-3 dark:border-slate-700 dark:bg-slate-950">
<h4 className="mb-2 text-xs font-semibold uppercase tracking-wide text-slate-600 dark:text-slate-300">Output</h4>
<pre aria-label="Tool output" className="max-h-96 overflow-auto whitespace-pre-wrap break-words text-xs"><code><AnsiText text={shortenMiddle(event.preview)} /></code></pre>
{Object.keys(event.display).length > 0 && <Disclosure><Summary>Event metadata</Summary><pre className="whitespace-pre-wrap break-words">{JSON.stringify(event.display, null, 2)}</pre></Disclosure>}
</div>;
const content = event.preview;
return <div>{event.type === "command" && <pre className="whitespace-pre-wrap break-words"><code>{event.title}</code></pre>}<pre className="terminal whitespace-pre-wrap break-words"><code><AnsiText text={content} /></code></pre>{Object.keys(event.display).length > 0 && <Disclosure><Summary>Event metadata</Summary><pre className="whitespace-pre-wrap break-words">{JSON.stringify(event.display, null, 2)}</pre></Disclosure>}</div>;
}
11 changes: 10 additions & 1 deletion packages/app/test/runs.browser.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ test("run rename supports keyboard save and cancel, validates names, and preserv
test("run trace uses pull-to-load history, preserves the anchor, and never crashes on disclosure toggles", async () => {
const { context, page } = await setup(); const requests = [];
const event = (sequence, type = "assistant_message") => ({ id: `trace-${sequence}`, sequence, type, title: `Trace ${sequence}`, preview: `Trace ${sequence}`, display: {} });
const recent = [...Array.from({ length: 14 }, (_, index) => event(index + 11)), event(25, "tool_call"), event(26, "tool_result")];
const longCommand = `start-${"x".repeat(5000)}-end`;
const call = { ...event(25, "tool_call"), title: "bash", preview: JSON.stringify({ command: longCommand }), display: { arguments: { command: longCommand } } };
const result = { ...event(26, "tool_result"), title: "bash", preview: `output-start-${"y".repeat(5000)}-output-end`, display: { isError: false } };
const recent = [...Array.from({ length: 14 }, (_, index) => event(index + 11)), call, result];
const olderPages = [Array.from({ length: 10 }, (_, index) => event(index + 1)), Array.from({ length: 10 }, (_, index) => event(index + 27)), Array.from({ length: 10 }, (_, index) => event(index + 37)), Array.from({ length: 10 }, (_, index) => event(index + 47)), Array.from({ length: 10 }, (_, index) => event(index + 57)), Array.from({ length: 10 }, (_, index) => event(index + 67))];
await page.route(`**/api/v1/runs/${runId}/status`, route => route.fulfill({ json: { id: runId, job_id: jobId, job_name: "Job", run_name: "Run", state: "succeeded", finalizing: false, trace_revision: "rev", artifact_state: "stored", created_at: time, updated_at: time, started_at: time, destination_url: null } }));
await page.route(`**/api/v1/runs/${runId}/trace-pages?**`, route => {
Expand All @@ -154,6 +157,12 @@ test("run trace uses pull-to-load history, preserves the anchor, and never crash
const presentation = await scroller.evaluate(element => { const style = getComputedStyle(element); return { border: style.borderWidth, radius: style.borderRadius, padding: style.padding }; });
assert.equal(presentation.border, "0px"); assert.equal(presentation.radius, "0px"); assert.equal(presentation.padding, "0px");
await page.getByText("1 Tool Call", { exact: true }).click();
await page.getByRole("heading", { name: "bash", exact: true }).waitFor();
await page.getByRole("heading", { name: "Output", exact: true }).waitFor();
assert.match(await page.getByLabel("bash command").textContent(), /^\{\n "command": "start-/);
assert.match(await page.getByLabel("bash command").textContent(), /\.\.\./);
assert.match(await page.getByLabel("Tool output").textContent(), /^output-start-/);
assert.match(await page.getByLabel("Tool output").textContent(), /\.\.\./);
assert.equal(await page.getByText("Cannot read properties of null", { exact: false }).count(), 0);
const initial = await scroller.evaluate(element => ({ top: element.scrollTop, height: element.scrollHeight, client: element.clientHeight }));
assert.ok(initial.height > initial.client && initial.top > 0, "initial trace should be bottom-aligned inside its own scroller");
Expand Down
Loading