Tool
shadcn/uiDisplays an agent tool invocation lifecycle across streaming, approval, output, and error states.
Tool
Parameters
{
"command": "bun run db:migrate",
"environment": "stagingParameters
{
"query": "trace collapsible slots",
"limit": 5
}Parameters
{
"path": "registry/optics/ai-elements/tool.tsx"
}Result
{
"lines": 276,
"exports": 5
}Parameters
{
"endpoint": "https://api.internal.local/schema.json"
}Error
Request timed out after 5000ms (ETIMEDOUT)
import { useState } from "react";import { Tool, ToolContent, ToolHeader, ToolInput, ToolOutput, type ToolState,} from "@/components/optics/ai-elements/tool";import { Button } from "@/components/optics/button";const LIFECYCLE_STATES = [ "input-streaming", "approval-requested", "output-available", "output-error",] as const satisfies readonly ToolState[];type LifecycleState = (typeof LIFECYCLE_STATES)[number];interface LifecyclePayload { input: unknown; output?: string; errorText?: string;}const LIFECYCLE_DATA: Record<LifecycleState, LifecyclePayload> = { "input-streaming": { input: '{\n "command": "bun run db:migrate",\n "environment": "staging', }, "approval-requested": { input: { command: "bun run db:migrate", environment: "staging", dryRun: false }, }, "output-available": { input: { command: "bun run db:migrate", environment: "staging", dryRun: false }, output: JSON.stringify( { appliedMigrations: 2, durationMs: 318, status: "healthy" }, null, 2, ), }, "output-error": { input: { command: "bun run db:migrate", environment: "staging", dryRun: false }, errorText: 'MigrationError: relation "sessions" already exists (code 42P07)', },};function ToolDemo() { const [lifecycleState, setLifecycleState] = useState<LifecycleState>("input-streaming"); const activePayload = LIFECYCLE_DATA[lifecycleState]; function handleAdvanceState() { setLifecycleState((prev) => { const currentIndex = LIFECYCLE_STATES.indexOf(prev); return LIFECYCLE_STATES[(currentIndex + 1) % LIFECYCLE_STATES.length]; }); } return ( <div className="flex w-full max-w-lg flex-col gap-3"> <div className="flex flex-wrap items-center justify-between gap-2"> <div className="flex flex-wrap items-center gap-1.5"> {LIFECYCLE_STATES.map((stateOption) => ( <Button key={stateOption} type="button" size="xs" variant={lifecycleState === stateOption ? "default" : "outline"} onClick={() => setLifecycleState(stateOption)} > {stateOption} </Button> ))} </div> <Button type="button" size="xs" variant="outline" onClick={handleAdvanceState}> Next state </Button> </div> <div className="flex flex-col gap-3 rounded-xl border bg-background p-4"> <Tool defaultOpen> <ToolHeader name="run_migration" state={lifecycleState} /> <ToolContent> <ToolInput input={activePayload.input} /> <ToolOutput output={activePayload.output} errorText={activePayload.errorText} /> </ToolContent> </Tool> <div className="flex flex-col gap-2 border-t pt-3"> <Tool> <ToolHeader name="search_docs" state="input-available" /> <ToolContent> <ToolInput input={{ query: "trace collapsible slots", limit: 5 }} /> </ToolContent> </Tool> <Tool> <ToolHeader name="read_file" state="output-available" /> <ToolContent> <ToolInput input={{ path: "registry/optics/ai-elements/tool.tsx" }} /> <ToolOutput output={JSON.stringify({ lines: 276, exports: 5 }, null, 2)} /> </ToolContent> </Tool> <Tool> <ToolHeader name="fetch_schema" state="output-error" /> <ToolContent> <ToolInput input={{ endpoint: "https://api.internal.local/schema.json" }} /> <ToolOutput errorText="Request timed out after 5000ms (ETIMEDOUT)" /> </ToolContent> </Tool> </div> </div> </div> );}<ToolDemo />import { useState } from "react";import { Tool, ToolContent, ToolHeader, ToolInput, ToolOutput, type ToolState,} from "@/components/optics/ai-elements/tool";import { Button } from "@/components/optics/button";const LIFECYCLE_STATES = [ "input-streaming", "approval-requested", "output-available", "output-error",] as const satisfies readonly ToolState[];type LifecycleState = (typeof LIFECYCLE_STATES)[number];interface LifecyclePayload { input: unknown; output?: string; errorText?: string;}const LIFECYCLE_DATA: Record<LifecycleState, LifecyclePayload> = { "input-streaming": { input: '{\n "command": "bun run db:migrate",\n "environment": "staging', }, "approval-requested": { input: { command: "bun run db:migrate", environment: "staging", dryRun: false }, }, "output-available": { input: { command: "bun run db:migrate", environment: "staging", dryRun: false }, output: JSON.stringify( { appliedMigrations: 2, durationMs: 318, status: "healthy" }, null, 2, ), }, "output-error": { input: { command: "bun run db:migrate", environment: "staging", dryRun: false }, errorText: 'MigrationError: relation "sessions" already exists (code 42P07)', },};function ToolDemo() { const [lifecycleState, setLifecycleState] = useState<LifecycleState>("input-streaming"); const activePayload = LIFECYCLE_DATA[lifecycleState]; function handleAdvanceState() { setLifecycleState((prev) => { const currentIndex = LIFECYCLE_STATES.indexOf(prev); return LIFECYCLE_STATES[(currentIndex + 1) % LIFECYCLE_STATES.length]; }); } return ( <div className="flex w-full max-w-lg flex-col gap-3"> <div className="flex flex-wrap items-center justify-between gap-2"> <div className="flex flex-wrap items-center gap-1.5"> {LIFECYCLE_STATES.map((stateOption) => ( <Button key={stateOption} type="button" size="xs" variant={lifecycleState === stateOption ? "default" : "outline"} onClick={() => setLifecycleState(stateOption)} > {stateOption} </Button> ))} </div> <Button type="button" size="xs" variant="outline" onClick={handleAdvanceState}> Next state </Button> </div> <div className="flex flex-col gap-3 rounded-xl border bg-background p-4"> <Tool defaultOpen> <ToolHeader name="run_migration" state={lifecycleState} /> <ToolContent> <ToolInput input={activePayload.input} /> <ToolOutput output={activePayload.output} errorText={activePayload.errorText} /> </ToolContent> </Tool> <div className="flex flex-col gap-2 border-t pt-3"> <Tool> <ToolHeader name="search_docs" state="input-available" /> <ToolContent> <ToolInput input={{ query: "trace collapsible slots", limit: 5 }} /> </ToolContent> </Tool> <Tool> <ToolHeader name="read_file" state="output-available" /> <ToolContent> <ToolInput input={{ path: "registry/optics/ai-elements/tool.tsx" }} /> <ToolOutput output={JSON.stringify({ lines: 276, exports: 5 }, null, 2)} /> </ToolContent> </Tool> <Tool> <ToolHeader name="fetch_schema" state="output-error" /> <ToolContent> <ToolInput input={{ endpoint: "https://api.internal.local/schema.json" }} /> <ToolOutput errorText="Request timed out after 5000ms (ETIMEDOUT)" /> </ToolContent> </Tool> </div> </div> </div> );}<ToolDemo />Installation
pnpm dlx shadcn@latest add @optics/ai-tool
Props
<Tool />
Name | Type | |
|---|---|---|
defaultOpen | boolean(default: false) | |
open | boolean | |
onOpenChange | ((open: boolean) => void) |
<ToolHeader />
Name | Type | |
|---|---|---|
render | React.ReactElement | |
nativeButton | boolean |
<ToolContent />
Name | Type | |
|---|---|---|
render | React.ReactElement | |
hiddenUntilFound | boolean | |
keepMounted | boolean | |
forceMount | boolean |
<ToolOutput />
Name | Type | |
|---|---|---|
output | React.ReactNode | |
errorText | string |