• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
drewmcdonald

drewmcdonald

promptCompare

Public
Like
promptCompare
Home
Code
7
.claude
3
backend
2
docs
1
frontend
4
shared
.mcp.json
deno.json
Environment variables
2
Branches
1
Pull requests
Remixes
History
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
.claude
/
skills
/
ai-elements
/
references
/
stack-trace.md
Code
/
.claude
/
skills
/
ai-elements
/
references
/
stack-trace.md
Search
…
Viewing readonly version of main branch: v140
View latest version
stack-trace.md

Stack Trace

Displays formatted JavaScript/Node.js error stack traces with syntax highlighting and collapsible frames.

The StackTrace component displays formatted JavaScript/Node.js error stack traces with clickable file paths, internal frame dimming, and collapsible content.

See scripts/stack-trace.tsx for this example.

Installation

npx ai-elements@latest add stack-trace

Usage with AI SDK

Build an error display tool that shows stack traces from AI-generated code using the useChat hook.

Add the following component to your frontend:

Create val
"use client"; import { useChat } from "@ai-sdk/react"; import { StackTrace, StackTraceActions, StackTraceContent, StackTraceCopyButton, StackTraceError, StackTraceErrorMessage, StackTraceErrorType, StackTraceExpandButton, StackTraceFrames, StackTraceHeader, } from "@/components/ai-elements/stack-trace"; export default function Page() { const { messages } = useChat({ api: "/api/run-code", }); return ( <div className="max-w-4xl mx-auto p-6"> {messages.map((message) => { const toolInvocations = message.parts?.filter( (part) => part.type === "tool-invocation", ); return toolInvocations?.map((tool) => { if (tool.toolName === "runCode" && tool.result?.error) { return ( <StackTrace key={tool.toolCallId} trace={tool.result.error} defaultOpen > <StackTraceHeader> <StackTraceError> <StackTraceErrorType /> <StackTraceErrorMessage /> </StackTraceError> <StackTraceActions> <StackTraceCopyButton /> <StackTraceExpandButton /> </StackTraceActions> </StackTraceHeader> <StackTraceContent> <StackTraceFrames /> </StackTraceContent> </StackTrace> ); } return null; }); })} </div> ); }

Add the following route to your backend:

Create val
import { streamText, tool } from "ai"; import { z } from "zod"; export async function POST(req: Request) { const { messages } = await req.json(); const result = streamText({ model: "openai/gpt-4o", messages, tools: { runCode: tool({ description: "Execute JavaScript code and return any errors", parameters: z.object({ code: z.string(), }), execute: async ({ code }) => { try { // Execute code in sandbox eval(code); return { success: true }; } catch (error) { return { error: (error as Error).stack }; } }, }), }, }); return result.toDataStreamResponse(); }

Features

  • Parses standard JavaScript/Node.js stack trace format
  • Highlights error type in red
  • Dims internal frames (node_modules, node: paths)
  • Collapsible content with smooth animation
  • Copy full stack trace to clipboard
  • Clickable file paths with line/column numbers

Examples

Collapsed by Default

See scripts/stack-trace-collapsed.tsx for this example.

Hide Internal Frames

See scripts/stack-trace-no-internal.tsx for this example.

Props

<StackTrace />

PropTypeDefaultDescription
tracestring-The raw stack trace string to parse and display.
openboolean-Controlled open state.
defaultOpenbooleanfalseWhether the content is expanded by default.
onOpenChange(open: boolean) => void-Callback when open state changes.
onFilePathClick(path: string, line?: number, column?: number) => void-Callback when a file path is clicked. Receives the file path, line number, and column number.
childrenReact.ReactNode-Child elements (StackTraceHeader, StackTraceContent, etc.).
classNamestring-Additional CSS classes.
...propsReact.HTMLAttributes<HTMLDivElement>-Any other props are spread to the root div.

<StackTraceHeader />

PropTypeDefaultDescription
childrenReact.ReactNode-Header content (typically StackTraceError and StackTraceActions).
classNamestring-Additional CSS classes.
...propsReact.ComponentProps<typeof CollapsibleTrigger>-Any other props are spread to the CollapsibleTrigger.

<StackTraceError />

PropTypeDefaultDescription
childrenReact.ReactNode-Error content (typically StackTraceErrorType and StackTraceErrorMessage).
classNamestring-Additional CSS classes.
...propsReact.HTMLAttributes<HTMLDivElement>-Any other props are spread to the container div.

<StackTraceErrorType />

PropTypeDefaultDescription
childrenReact.ReactNode-Custom content. Defaults to the parsed error type (e.g.,
classNamestring-Additional CSS classes.
...propsReact.HTMLAttributes<HTMLSpanElement>-Any other props are spread to the span element.

<StackTraceErrorMessage />

PropTypeDefaultDescription
childrenReact.ReactNode-Custom content. Defaults to the parsed error message.
classNamestring-Additional CSS classes.
...propsReact.HTMLAttributes<HTMLSpanElement>-Any other props are spread to the span element.

<StackTraceActions />

PropTypeDefaultDescription
childrenReact.ReactNode-Action buttons (typically StackTraceCopyButton and StackTraceExpandButton).
classNamestring-Additional CSS classes.
...propsReact.HTMLAttributes<HTMLDivElement>-Any other props are spread to the container div.

<StackTraceCopyButton />

PropTypeDefaultDescription
onCopy() => void-Callback fired after a successful copy.
onError(error: Error) => void-Callback fired if copying fails.
timeoutnumber2000How long to show the copied state (ms).
childrenReact.ReactNode-Custom content for the button. Defaults to copy/check icons.
classNamestring-Additional CSS classes.
...propsReact.ComponentProps<typeof Button>-Any other props are spread to the underlying shadcn/ui Button component.

<StackTraceExpandButton />

PropTypeDefaultDescription
classNamestring-Additional CSS classes.
...propsReact.HTMLAttributes<HTMLDivElement>-Any other props are spread to the container div.

<StackTraceContent />

PropTypeDefaultDescription
maxHeightnumber400Maximum height of the content area. Enables scrolling when content exceeds this height.
childrenReact.ReactNode-Content to display (typically StackTraceFrames).
classNamestring-Additional CSS classes.
...propsReact.ComponentProps<typeof CollapsibleContent>-Any other props are spread to the CollapsibleContent.

<StackTraceFrames />

PropTypeDefaultDescription
showInternalFramesbooleantrueWhether to show internal frames (node_modules, node: paths).
classNamestring-Additional CSS classes.
...propsReact.HTMLAttributes<HTMLDivElement>-Any other props are spread to the container div.
FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
© 2026 Val Town, Inc.