Stack Trace
Displays a failed tool call and its stack frames.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { For, createMemo } from "solid-js"
import {
useDemoChat,
type UIDataTypes,
type UIMessage,
createChat,
getMessageText
} from "~/lib/ai-demo-chat"
import { Bubble, BubbleContent } from "~/components/ui/bubble"
import { Button } from "~/components/ui/button"
import { CodeBlock, CodeBlockContent } from "~/components/ui/code-block"
import { Message, MessageContent } from "~/components/ui/message"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle
} from "~/components/ui/trace"
type ChatMessage = UIMessage<
unknown,
UIDataTypes,
{
runCommand: {
input: { command: string }
output: { stdout: string }
}
}
>
type ToolPart = Extract<
ChatMessage["parts"][number],
{ type: "tool-runCommand" }
>
const STACK = `TypeError: Cannot read properties of undefined (reading 'email')
at seedUsers (prisma/seed.ts:27:34)
at main (prisma/seed.ts:51:9)
at Module._compile (node:internal/modules/cjs/loader:1554:14)
at Object.transformer (node_modules/tsx/dist/register.cjs:3:1104)
at Module.load (node:internal/modules/cjs/loader:1289:32)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)`
const chat = createChat<ChatMessage>()
.user("Reset the database and seed it.")
.assistant(({ writer }) => {
writer
.tool("runCommand", {
input: { command: "pnpm prisma migrate reset --skip-seed" }
})
.sleep(900)
.output({
stdout: `Database reset successful
The following migrations have been applied:
migrations/
└─ 20260912_init/
└─ 20260920_add_profiles/`
})
writer
.tool("runCommand", { input: { command: "pnpm prisma db seed" } })
.sleep(1200)
.error(STACK)
writer.text(
"The migrations ran, but the seed failed in seedUsers. One fixture has no profile, so profile.email is undefined."
)
})
const transcript = chat.get()
const [question] = chat.get(1)
const transport = chat.transport({ delayMs: 20 })
function parseFrame(line: string) {
const match = line.match(/^at (.+) \((.+):(\d+):(\d+)\)$/)
return {
line,
name: match?.[1],
file: match?.[2],
lineNumber: match?.[3],
internal: line.includes("node_modules") || line.includes("node:")
}
}
function StackTrace(componentProps: { stack: string }) {
const content = createMemo(() => {
const { stack } = componentProps
const [error, ...lines] = stack.split("\n").map((line) => line.trim())
const frames = lines.map(parseFrame)
const appFrames = frames.filter((frame) => !frame.internal)
const internalFrames = frames.filter((frame) => frame.internal)
return (
<TraceCollapsible defaultOpen>
<TraceCollapsibleTrigger>
<TraceLine status="error">
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M12 19h8" />
<path d="m4 17 6-6-6-6" />
</svg>
</TraceIcon>
<TraceTitle>Failed: {error}</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent class="flex flex-col gap-2">
<p class="font-mono text-xs text-destructive">{error}</p>
<Trace>
{
<For each={appFrames}>
{(frame) => (
<a href={`#${frame.file}:${frame.lineNumber}`}>
<TraceLine>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z" />
<path d="M14 2v5a1 1 0 0 0 1 1h5" />
<path d="M10 12.5 8 15l2 2.5" />
<path d="m14 12.5 2 2.5-2 2.5" />
</svg>
</TraceIcon>
<TraceTitle>
{frame.name} in {frame.file}:{frame.lineNumber}
</TraceTitle>
</TraceLine>
</a>
)}
</For>
}
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83z" />
<path d="M2 12a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 12" />
<path d="M2 17a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 17" />
</svg>
</TraceIcon>
<TraceTitle>
{internalFrames.length} framework frames
</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<ol class="flex flex-col gap-1 overflow-x-auto rounded-md bg-muted p-3 font-mono text-xs">
{
<For each={internalFrames}>
{(frame) => (
<li class="whitespace-nowrap">{frame.line}</li>
)}
</For>
}
</ol>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>
</TraceCollapsibleContent>
</TraceCollapsible>
)
})
return <>{content()}</>
}
function CommandRow(componentProps: { part: ToolPart; streaming: boolean }) {
const content = createMemo(() => {
const { part, streaming } = componentProps
const command = part.input?.command
if (part.state === "output-error") {
return <StackTrace stack={part.errorText} />
}
if (part.state === "output-available") {
return (
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M12 19h8" />
<path d="m4 17 6-6-6-6" />
</svg>
</TraceIcon>
<TraceTitle>Ran {command}</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<CodeBlock size="sm">
<CodeBlockContent wrap class="max-h-64">
{part.output.stdout}
</CodeBlockContent>
</CodeBlock>
</TraceCollapsibleContent>
</TraceCollapsible>
)
}
return (
<TraceLine status={streaming ? "running" : "error"}>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M12 19h8" />
<path d="m4 17 6-6-6-6" />
</svg>
</TraceIcon>
<TraceTitle>
{streaming ? "Running" : "Failed to run"} {command}
</TraceTitle>
</TraceLine>
)
})
return <>{content()}</>
}
export default function StackTraceDemo() {
const { messages, sendMessage, setMessages, status } = useDemoChat({
messages: transcript,
transport
})
const busy = () => status() === "submitted" || status() === "streaming"
return (
<div class="flex min-h-72 w-full max-w-sm flex-col gap-4 py-6 text-sm">
{
<For each={messages()}>
{(message) => {
const content = createMemo(() => {
if (message.role === "user") {
return (
<Message align="end">
<MessageContent>
<Bubble align="end" variant="muted">
<BubbleContent>{getMessageText(message)}</BubbleContent>
</Bubble>
</MessageContent>
</Message>
)
}
const streaming = busy() && message === messages().at(-1)
const tools = message.parts.filter(
(part) => part.type === "tool-runCommand"
)
const text = getMessageText(message)
return (
<Message>
<MessageContent class="gap-4">
{tools.length > 0 && (
<Trace>
{
<For each={tools}>
{(part) => (
<CommandRow part={part} streaming={streaming} />
)}
</For>
}
</Trace>
)}
{text && <p>{text}</p>}
</MessageContent>
</Message>
)
})
return <>{content()}</>
}}
</For>
}
{!busy() && (
<Button
variant="outline"
size="sm"
class="mt-auto self-start"
onClick={() => {
setMessages([])
sendMessage(question)
}}
>
Replay
</Button>
)}
</div>
)
}Stack Trace is a line for a tool call that failed. The error is the title, like “Failed: TypeError”. Open it for the stack frames, which can link to the file and line.
Stack Trace is built with Trace with a TraceCollapsible for the frames.
Installation
Stack Trace is built with the Trace and Code Block components.
Install the required Solid UI dependencies:
Copy and paste the following code into your project.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import type { ComponentProps, JSX } from "@solidjs/web"
import { merge, omit } from "solid-js"
import { cn } from "~/lib/utils"
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger
} from "~/components/ui/collapsible"
export function Trace(
props: Omit<ComponentProps<"div">, "class"> & { class?: string }
) {
return (
<div
{...props}
data-slot="trace"
class={cn(
"gap-2 *:has-[>[data-slot=trace-line]]:rounded-md *:has-[>[data-slot=trace-line]]:focus-visible:ring-3 *:has-[>[data-slot=trace-line]]:focus-visible:ring-ring/50 flex w-full min-w-0 flex-col *:has-[>[data-slot=trace-line]]:flex *:has-[>[data-slot=trace-line]]:w-fit *:has-[>[data-slot=trace-line]]:max-w-full *:has-[>[data-slot=trace-line]]:min-w-0 *:has-[>[data-slot=trace-line]]:text-left *:has-[>[data-slot=trace-line]]:outline-none",
props.class
)}
/>
)
}
export function TraceLine(
props: Omit<ComponentProps<"span">, "class"> & {
class?: string
status?: "running" | "error" | "pending" | undefined
}
) {
const rest = omit(props, "class", "status")
return (
<span
{...rest}
data-slot="trace-line"
data-status={props.status ?? undefined}
class={cn(
"gap-2 text-sm flex w-fit max-w-full min-w-0 items-center text-muted-foreground transition-colors in-[a,button]:cursor-pointer in-[a:hover,button:hover]:text-foreground data-[status=error]:text-destructive data-[status=error]:in-[a:hover,button:hover]:text-destructive data-[status=pending]:opacity-60 data-[status=running]:*:data-[slot=trace-title]:shimmer",
props.class
)}
/>
)
}
export function TraceIcon(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-icon"
class={cn(
"size-4 [&_svg:not([class*='size-'])]:size-4 [[data-slot=trace-title]~&]:-ml-1 flex shrink-0 items-center justify-center [&_svg]:pointer-events-none",
props.class
)}
/>
)
}
export function TraceTitle(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-title"
class={cn(
"min-w-0 truncate motion-reduce:shimmer-none",
props.class
)}
/>
)
}
export function TraceDescription(
props: Omit<ComponentProps<"span">, "class"> & { class?: string }
) {
return (
<span
{...props}
data-slot="trace-description"
class={cn(
"shrink-0 text-muted-foreground/70 tabular-nums",
props.class
)}
/>
)
}
export function TraceCollapsible(props: ComponentProps<typeof Collapsible>) {
const rest = omit(props, "class", "render")
return (
<Collapsible
{...rest}
class={cn(
"flex w-full min-w-0 flex-col",
props.class
)}
render={(part, state) => {
const forwarded = merge(part, { "data-slot": "trace-collapsible" })
return props.render ? (
props.render(forwarded, state)
) : (
<div {...forwarded} />
)
}}
/>
)
}
export function TraceCollapsibleTrigger(
props: ComponentProps<typeof CollapsibleTrigger>
) {
const rest = omit(props, "class", "children", "render")
return (
<CollapsibleTrigger
{...rest}
class={cn(
"gap-1 rounded-md focus-visible:ring-3 focus-visible:ring-ring/50 group/trace-collapsible-trigger flex w-fit max-w-full min-w-0 cursor-pointer items-center text-left text-muted-foreground transition-colors outline-none hover:text-foreground has-[[data-slot=trace-line][data-status=error]]:text-destructive",
props.class
)}
render={(part, state) => {
const forwarded = merge(part, {
"data-slot": "trace-collapsible-trigger"
})
return props.render ? (
props.render(forwarded, state)
) : (
<button {...forwarded} />
)
}}
>
{props.children}
<TraceIcon class="transition-transform group-aria-expanded/trace-collapsible-trigger:rotate-90">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<path d="m9 18 6-6-6-6" />
</svg>
</TraceIcon>
</CollapsibleTrigger>
)
}
export function TraceCollapsibleContent(
props: Omit<ComponentProps<typeof CollapsibleContent>, "children"> & {
children?: JSX.Element
}
) {
const rest = omit(props, "class", "children", "render")
return (
<CollapsibleContent
{...rest}
class="duration-200 h-(--collapsible-panel-height) min-w-0 overflow-hidden transition-[height] data-ending-style:h-0 data-starting-style:h-0"
render={(part, state) => {
const forwarded = merge(part, {
"data-slot": "trace-collapsible-content"
})
return props.render ? (
props.render(forwarded, state)
) : (
<div {...forwarded} />
)
}}
>
<div
data-slot="trace-collapsible-content-inner"
class={cn(
"mt-2 mb-1 ml-[calc(--spacing(2)-1px)] border-l pr-1 pl-4 leading-relaxed min-w-0 wrap-break-word text-muted-foreground",
props.class
)}
>
{props.children}
</div>
</CollapsibleContent>
)
}Update the import paths to match your project setup.
Usage
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle
} from "~/components/ui/trace"import { For } from "solid-js"
const [error, ...frames] = stack.split("\n").map((line) => line.trim())
<Trace>
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine status="error">
<TraceIcon>
<CircleXIcon />
</TraceIcon>
<TraceTitle>Failed: {error}</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<div class="flex flex-col gap-2 rounded-md bg-muted p-3 font-mono text-xs">
<p class="text-destructive">{error}</p>
<ol>
{<For each={frames}>{(frame, index) => ((
<li
class={
frame.includes("node_modules") ? undefined : "text-foreground"
}
>
{frame}
</li>
))}</For>}
</ol>
</div>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>The title truncates, so repeat the full error at the top of the content. Frames from node_modules stay muted, so your own frames stand out.
Frame Links
Turn your own frames into links that open the file at the line. Fold the framework frames into a collapsible line of their own, so the frames you can fix come first.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { For } from "solid-js"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle
} from "~/components/ui/trace"
const STACK = `TypeError: Cannot read properties of undefined (reading 'total')
at formatInvoice (app/lib/billing.ts:42:31)
at InvoiceRow (app/components/invoice-row.tsx:18:12)
at renderWithHooks (node_modules/react-dom/cjs/react-dom.development.js:14985:18)
at beginWork (node_modules/react-dom/cjs/react-dom.development.js:19049:16)
at performUnitOfWork (node_modules/react-dom/cjs/react-dom.development.js:22776:12)
at workLoopSync (node_modules/react-dom/cjs/react-dom.development.js:22707:5)`
function parseFrame(line: string) {
const match = line.match(/^at (.+) \((.+):(\d+):(\d+)\)$/)
return {
line,
name: match?.[1],
file: match?.[2],
lineNumber: match?.[3],
internal: line.includes("node_modules")
}
}
const [error, ...lines] = STACK.split("\n").map((line) => line.trim())
const frames = lines.map(parseFrame)
const appFrames = frames.filter((frame) => !frame.internal)
const internalFrames = frames.filter((frame) => frame.internal)
export default function StackTraceFrames() {
return (
<Trace class="max-w-sm">
<TraceCollapsible defaultOpen>
<TraceCollapsibleTrigger>
<TraceLine status="error">
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<circle cx="12" cy="12" r="10" />
<path d="m15 9-6 6" />
<path d="m9 9 6 6" />
</svg>
</TraceIcon>
<TraceTitle>Failed: {error}</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent class="flex flex-col gap-2">
<p class="font-mono text-xs text-destructive">{error}</p>
<Trace>
{
<For each={appFrames}>
{(frame) => (
<a href={`#${frame.file}:${frame.lineNumber}`}>
<TraceLine>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z" />
<path d="M14 2v5a1 1 0 0 0 1 1h5" />
<path d="M10 12.5 8 15l2 2.5" />
<path d="m14 12.5 2 2.5-2 2.5" />
</svg>
</TraceIcon>
<TraceTitle>
{frame.name} in {frame.file}:{frame.lineNumber}
</TraceTitle>
</TraceLine>
</a>
)}
</For>
}
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83z" />
<path d="M2 12a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 12" />
<path d="M2 17a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 17" />
</svg>
</TraceIcon>
<TraceTitle>
{internalFrames.length} framework frames
</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<ol class="flex flex-col gap-1 overflow-x-auto rounded-md bg-muted p-3 font-mono text-xs">
{
<For each={internalFrames}>
{(frame) => (
<li class="whitespace-nowrap">{frame.line}</li>
)}
</For>
}
</ol>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>
)
}AI SDK
When a tool throws, its part is output-error. By default errorText is only the error message, with no frames. Return error.stack from onError in toUIMessageStreamResponse to send them.
return result.toUIMessageStreamResponse({
onError: (error) =>
error instanceof Error ? (error.stack ?? error.message) : String(error)
})A stack shows file paths from your server, so only send it in development.
if (part.state === "output-error") {
const [error, ...frames] = part.errorText
.split("\n")
.map((line) => line.trim())
return <TraceCollapsible>…</TraceCollapsible>
}Map the other tool states as in Tool.
Migrating from AI SDK Elements
If you added Stack Trace from AI SDK Elements, your project has a components/ai-elements/stack-trace.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Trace. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/stack-trace.tsx from AI SDK Elements to the Solid UI Trace component.
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
1. Install Trace: npx solidui-cli@latest add trace
2. Read https://www.solid-ui.com/docs/ai-elements/stack-trace.md and build Stack Trace the same way.
3. Rebuild each part:
- StackTrace renders a Trace with a TraceCollapsible. Keep trace, open, defaultOpen, onOpenChange and onFilePathClick, and the parsing.
- StackTraceHeader renders a TraceCollapsibleTrigger with a TraceLine with status="error" and a TraceIcon.
- StackTraceError renders a TraceTitle, like "Failed: TypeError: …". Keep StackTraceErrorType and StackTraceErrorMessage as spans inside it.
- StackTraceContent renders a TraceCollapsibleContent.
- StackTraceFrames renders the frames as a list in a muted, monospace block. Dim the frames from node_modules, and keep the file path buttons.
- StackTraceExpandButton renders nothing, since the whole line opens the frames.
- Buttons cannot sit inside a trigger. Render StackTraceActions and StackTraceCopyButton next to the TraceCollapsibleTrigger, not inside it.
4. Remove the imports the file no longer uses.
5. Run the type checker and fix any errors.API Reference
See Trace.