Terminal
Displays a command the agent runs, with its output as it streams.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { For, createSignal, untrack, createMemo } from "solid-js"
import {
useDemoChat,
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,
{
terminal: { output: string }
},
{
runCommand: {
input: { command: string }
output: { exitCode: number }
}
}
>
type ToolPart = Extract<
ChatMessage["parts"][number],
{ type: "tool-runCommand" }
>
const COMMANDS = [
{
id: "lint",
command: "pnpm lint",
output: ["> acme-chat@0.1.0 lint", "> eslint ."]
},
{
id: "test",
command: "pnpm test",
output: [
"> acme-chat@0.1.0 test",
"> vitest run",
"",
" ✓ lib/ai.test.ts (6 tests) 41ms",
" ✓ components/chat.test.tsx (9 tests) 212ms",
"",
" Test Files 2 passed (2)",
" Tests 15 passed (15)"
]
},
{
id: "build",
command: "pnpm build",
output: [
"> acme-chat@0.1.0 build",
"> next build",
"",
" ▲ Next.js 16.3.0",
" Creating an optimized production build ...",
" ✓ Compiled successfully in 4.2s",
" ✓ Generating static pages (12/12)",
"",
"Route (app) Size First Load JS",
"┌ ○ / 1.9 kB 112 kB",
"└ ƒ /api/chat 141 B 102 kB"
]
}
]
const chat = createChat<ChatMessage>()
.user("Run the checks, then build the app.")
.assistant(({ writer }) => {
for (const { id, command, output } of COMMANDS) {
const tool = writer.tool("runCommand", {
toolCallId: id,
input: { command }
})
// The tool streams its output as a data part with the tool call's id.
output.forEach((_, index) => {
writer
.data({
type: "data-terminal",
id,
data: { output: output.slice(0, index + 1).join("\n") }
})
.sleep(200)
})
tool.output({ exitCode: 0 })
}
writer.text("Lint and tests pass, and the build compiled both routes.")
})
const transcript = chat.get()
const [question] = chat.get(1)
const transport = chat.transport({ delayMs: 20 })
function getTitle(part: ToolPart, running: boolean) {
const command = part.input?.command
if (part.state === "output-available") {
return part.output.exitCode === 0
? `Ran ${command}`
: `${command} exited with code ${part.output.exitCode}`
}
return running ? `Running ${command}` : `Failed to run ${command}`
}
function getOutput(message: ChatMessage, toolCallId: string) {
for (const part of message.parts) {
if (part.type === "data-terminal" && part.id === toolCallId) {
return part.data.output
}
}
return ""
}
function CommandRow(componentProps: {
part: ToolPart
output: string
streaming: boolean
defaultOpen?: boolean | undefined
}) {
const [open, setOpen] = createSignal(
untrack(() => componentProps.defaultOpen)
)
const content = createMemo(() => {
const { part, output, streaming } = componentProps
const running =
streaming &&
(part.state === "input-streaming" || part.state === "input-available")
const failed =
!running &&
(part.state !== "output-available" || part.output.exitCode !== 0)
return (
<TraceCollapsible
open={open() ?? (running && output.length > 0)}
onOpenChange={setOpen}
>
<TraceCollapsibleTrigger>
<TraceLine
status={running ? "running" : failed ? "error" : undefined}
>
<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>{getTitle(part, running)}</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<CodeBlock size="sm">
<CodeBlockContent wrap class="max-h-64">
{part.state === "output-error" ? part.errorText : output}
</CodeBlockContent>
</CodeBlock>
</TraceCollapsibleContent>
</TraceCollapsible>
)
})
return <>{content()}</>
}
export default function TerminalDemo() {
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}
output={getOutput(message, part.toolCallId)}
streaming={streaming}
defaultOpen={
busy() ? undefined : part === tools.at(-1)
}
/>
)}
</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>
)
}Terminal is a shell command the agent runs, such as a lint, a test run or a build. The line shows the command, and its output streams in underneath while it runs.
Terminal is built with Trace with a TraceCollapsible for each command.
Installation
Terminal 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 { CodeBlock, CodeBlockContent } from "~/components/ui/code-block"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle
} from "~/components/ui/trace"<Trace>
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine>
<TraceIcon>
<TerminalIcon />
</TraceIcon>
<TraceTitle>Ran pnpm build</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<CodeBlock size="sm">
<CodeBlockContent wrap class="max-h-64">
{output}
</CodeBlockContent>
</CodeBlock>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>Exit Code
A command that exits with any code other than 0 failed. Set status="error".
> acme-chat@0.1.0 lint
> eslint .
app/api/chat/route.ts
14:7 error 'model' is assigned a value but never used no-unused-vars
components/chat.tsx
42:5 error React Hook useEffect has a missing dependency: 'messages' react-hooks/exhaustive-deps
✖ 2 problems (2 errors, 0 warnings)
ELIFECYCLE Command failed with exit code 1.// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { CodeBlock, CodeBlockContent } from "~/components/ui/code-block"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle
} from "~/components/ui/trace"
const LINT_OUTPUT = `> acme-chat@0.1.0 lint
> eslint .
app/api/chat/route.ts
14:7 error 'model' is assigned a value but never used no-unused-vars
components/chat.tsx
42:5 error React Hook useEffect has a missing dependency: 'messages' react-hooks/exhaustive-deps
✖ 2 problems (2 errors, 0 warnings)
ELIFECYCLE Command failed with exit code 1.`
export default function TerminalError() {
return (
<Trace class="max-w-sm">
<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 pnpm install</TraceTitle>
</TraceLine>
<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>pnpm lint exited with code 1</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<CodeBlock size="sm">
<CodeBlockContent wrap class="max-h-64">
{LINT_OUTPUT}
</CodeBlockContent>
</CodeBlock>
</TraceCollapsibleContent>
</TraceCollapsible>
</Trace>
)
}The command is a tool call, so its states map the same way as in Tool.
Open While Running
Open the line once the first output arrives, so the output is visible while the command runs. Leave it open or closed after that, as in Trace.
AI SDK
A tool result arrives only once the command exits. To stream the output while it runs, write it from the tool as a data part with the tool call’s id. The AI SDK replaces a data part that has the same id, so the output grows in place.
writer.write({
type: "data-terminal",
id: toolCallId,
data: { output }
})On the client, find the data part that matches each tool part.
function getOutput(message: ChatMessage, toolCallId: string) {
for (const part of message.parts) {
if (part.type === "data-terminal" && part.id === toolCallId) {
return part.data.output
}
}
return ""
}Migrating from AI SDK Elements
If you added Terminal from AI SDK Elements, your project has a components/ai-elements/terminal.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Trace and Code Block. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/terminal.tsx from AI SDK Elements to the Solid UI Trace and Code Block components.
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 the components: npx solidui-cli@latest add trace code-block
2. Read https://www.solid-ui.com/docs/ai-elements/terminal.md and build Terminal the same way.
3. Rebuild each part:
- Import CodeBlock and CodeBlockContent from ~/components/ui/code-block, not from ./code-block.
- Terminal renders a Trace with a TraceCollapsible. Keep output, isStreaming, autoScroll and onClear.
- TerminalHeader renders a TraceCollapsibleTrigger with a TraceLine. Set status="running" while isStreaming is true.
- TerminalTitle renders a TraceIcon with a TerminalIcon and a TraceTitle, like "Ran pnpm build".
- TerminalContent renders a TraceCollapsibleContent with a CodeBlock with size="sm", and a CodeBlockContent with wrap and class="max-h-64" for the output.
- Open the line once output arrives while the command runs.
- Buttons cannot sit inside a trigger. Render TerminalStatus, TerminalActions and the copy and clear buttons next to the TraceCollapsibleTrigger, not inside it.
4. Remove ansi-to-react from the dependencies if nothing else uses it.
5. Remove the imports the file no longer uses.
6. Run the type checker and fix any errors.API Reference
See Trace.