翻篇收录说明
翻篇网页快照

Tool - shadcn/ui

https://ui.shadcn.com/docs/ai-elements/aria/tool

最近抓取:2026-12-04 04:17:49(北京时间)

最近内容变化:2026-12-01 01:45:07(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Displays a tool call the agent makes, from input to output.

正文摘录

SectionsIntroductionComponentsAI ElementsInstallationThemingCLITypesetSkillsRegistryChangelogComponentsAccordionAlertAlert DialogAspect RatioAttachmentAvatarBadgeBreadcrumbBubbleButtonButton GroupCalendarCardCarouselChartCheckboxCode BlockCollapsibleComboboxCommandContext MenuData TableDate PickerDialogDirectionDrawerDropdown MenuEmptyFieldHover CardInputInput GroupInput OTPItemKbdLabelMarkerMessageMessage ScrollerNative SelectPaginationPopoverProgressQuestionnaireRadio GroupResizableScroll AreaSelectSeparatorSheetSidebarSkeletonSliderSonnerSpinnerSwitchTableTabsTextareaToastToggleToggle GroupTooltipTraceTypographyAI ElementsIntroductionChain of ThoughtChatCheckpointConfirmationContextFile TreeInline CitationMessageModel SelectorPlanPrompt InputQueueReasoningSourcesStack TraceSubagentSuggestionTaskTerminalTest ResultsToolGet StartedInstallationcomponents.jsonPackage ImportsThemingTypesetDark ModeCLIMonorepoSkillsJavaScriptFigmallms.txtLegacy Docs@shadcn/reactMessage ScrollerQuestionnaire@shadcn/helpersAI SDKTanStack AIFormsReact Hook FormTanStack FormFormischUtilitiesscroll-fadeshimmerRegistryIntroductionGetting StartedGitHub RegistriesRegistry DirectoryRegistry HealthExamplesNamespacesAuthenticationDynamic SearchMCP ServerOpen in v0FAQAPI Referenceregistry.jsonregistry-item.jsonToolCopy PagePreviousNextDisplays a tool call the agent makes, from input to output.Base UIReact AriaRadix UIWhich package managers are installed?Ran node --versionRan npm --versionFailed to run pnpm --versioncommand not found: pnpmNode 24 and npm 11 are installed. pnpm is not.ReplayCopy"use client" import { useChat } from "@ai-sdk/react"View Code Tool is a tool call the agent makes, such as running a command or reading a file. The line follows the call from input to output, including errors and calls that wait for approval. Tool is built with Trace with a TraceCollapsible for each call that has output. Installation Tool is built with the Trace and Code Block components. CommandManualpnpmnpmyarnbunpnpm dlx shadcn@latest add trace code-blockCopy Usage Copyimport { CodeBlock, CodeBlockContent } from "@/components/ui/code-block" import { Trace, TraceCollapsible, TraceCollapsibleContent, TraceCollapsibleTrigger, TraceIcon, TraceLine, TraceTitle, } from "@/components/ui/trace" Copy<Trace> <TraceCollapsible> <TraceCollapsibleTrigger> <TraceLine> <TraceIcon> <TerminalIcon /> </TraceIcon> <TraceTitle>Ran git status --short</TraceTitle> </TraceLine> </TraceCollapsibleTrigger> <TraceCollapsibleContent> <CodeBlock size="sm"> <CodeBlockContent wrap className="max-h-64"> {output} </CodeBlockContent> </CodeBlock> </TraceCollapsibleContent> </TraceCollapsible> </Trace> States Trace does not read tool parts. Map each part's state to a status and a title. Ran git status --shortFailed to run pnpm devDenied rm -rf distRunning pnpm testCopyimport { TerminalIcon } from "lucide-react" import { CodeBlock, CodeBlockContent } from "@/components/ui/code-block"View Code Tool part statestatusTitleinput-streaming, input-available"running" while the chat streamsRunning …input-streaming, input-available after the chat stops"error"Failed to run …approval-requested-Not a line. An Item outside Trace with Approve and Deny buttons.approval-responded"running" if approved, none if notRunning … if approved, Denied … if notoutput-availablenoneRan …output-error"error"Failed to run …output-deniednoneDenied … A tool is a plain TraceLine while it runs, and a TraceCollapsible once its output arrives. Copyfunction ToolLine({ part, streaming }: { part: ToolPart; streaming: boolean }) { const command = part.input?.command if ( part.state === "output-denied" || (part.state === "approval-responded" && !part.approval.approved) ) { return ( <TraceLine> <TraceIcon> <TerminalIcon /> </TraceIcon> <TraceTitle>Denied {command}</TraceTitle> </TraceLine> ) } if (part.state === "output-available" || part.state === "output-error") { const failed = part.state === "output-error" return ( <TraceCollapsible> <TraceCollapsibleTrigger> <TraceLine status={failed ? "error" : undefined}> <TraceIcon> <TerminalIcon /> </TraceIcon> <TraceTitle> {failed ? "Failed to run" : "Ran"} {command} </TraceTitle> </TraceLine> </TraceCollapsibleTrigger> <TraceCollapsibleContent> <CodeBlock size="sm"> <CodeBlockContent wrap className="max-h-64"> {failed ? part.errorText : part.output.stdout} </CodeBlockContent> </CodeBlock> </TraceCollapsibleContent> </TraceCollapsible> ) } return ( <TraceLine status={streaming ? "running" : "error"}> <TraceIcon> <TerminalIcon /> </TraceIcon> <TraceTitle> {streaming ? "Running" : "Failed to run"} {command} </TraceTitle> </TraceLine> ) } Copy<Trace> {message.parts .filter((part) => part.type === "tool-runCommand") .map((part) => ( <ToolLine key={part.toolCallId} part={part} streaming={streaming} /> ))} </Trace> A tool that runs on the client stays input-available after the chat stops until you call addToolOutput. Treat it as waiting, not failed. Output in a Sheet For long output, wrap the TraceLine in React Aria's Button inside a SheetTrigger and show the output in the sheet. Send messageCopy"use client" import { useChat } from "@ai-sdk/react"View Code See Trace. Approval A pending approval is a question for the person, not a step the agent took, so it sits outside Trace as an Item. Once the person answers, the tool goes back to being a line. See Confirmation. Send messageCopy"use client" import * as React from "react"View Code Copyif (part.state === "approval-requested") { return ( <Item variant="outline" size="sm"> <ItemMedia variant="icon"> <TerminalIcon /> </ItemMedia> <ItemContent> <ItemTitle>Run {part.input.command}?</ItemTitle> </ItemContent> <ItemActions> <Button size="sm" variant="outline" onPress={() => addToolApprovalResponse({ id: part.approval.id, approved: false }) } > Deny </Button> <Button size="sm" onPress={() => addToolApprovalResponse({ id: part.approval.id, approved: true }) } > Approve </Button> </ItemActions> </Item> ) } Pass sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses to useChat so the chat continues once every approval has an answer. Migrating from AI SDK Elements If you added Tool from AI SDK Elements, your project has a components/ai-elements/tool.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. PromptCopyMigrate components/ai-elements/tool.tsx from AI SDK Elements to the shadcn/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 shadcn@latest add trace code-block 2. Read https://ui.shadcn.com/docs/ai-elements/aria/tool.md and build Tool the same way. 3. Rebuild each part: - Import CodeBlock and CodeBlockContent from @/components/ui/code-block, not from ./code-block. - Tool renders a Trace with a TraceCollapsible. - ToolHeader renders a TraceCollapsibleTrigger with a TraceLine, a TraceIcon and a TraceTitle, instead of a status Badge. Map state to the status and title: "running" and "Running …" while the input streams or is available, no status and "Ran …" for output-available, "error" and "Failed to run …" for output-error, and "Denied …" for output-denied. - ToolContent renders a TraceCollapsibleContent. - ToolInput and ToolOutput render a CodeBlock with size="sm" and a CodeBlockContent. ToolOutput shows errorText when the tool failed. - Keep getStatusBadge and ToolPart exported, even if nothing renders the badge. 4. Remove the imports the file no longer uses. 5. Run the type checker and fix any errors.Expand API Reference See Trace. Test ResultsIntroduction On This PageInstallationUsageStatesOutput in a SheetApprovalMigrating from AI SDK ElementsAPI ReferenceDeploy your shadcn/ui app on VercelTrusted by OpenAI, Sonos, Adobe, and more.Vercel provides tools and infrastructure to deploy apps and features at scale.Deploy NowDeploy to Vercel