"use client"; import type { DynamicToolUIPart, ToolUIPart } from "ai"; import type { ComponentProps, ReactNode } from "react"; import { Badge } from "@/components/ui/badge"; import { cn } from "@/lib/utils"; import { CheckCircleIcon, ChevronDownIcon, CircleIcon, ClockIcon, WrenchIcon, XCircleIcon, } from "lucide-react"; import { isValidElement } from "react"; export type ToolProps = ComponentProps<"details">; export const Tool = ({ className, children, ...props }: ToolProps) => (
{children}
); export type ToolPart = ToolUIPart | DynamicToolUIPart; export type ToolHeaderProps = { title?: string; className?: string; } & ( | { type: ToolUIPart["type"]; state: ToolUIPart["state"]; toolName?: never } | { type: DynamicToolUIPart["type"]; state: DynamicToolUIPart["state"]; toolName: string; } ); const statusLabels: Record = { "approval-requested": "Awaiting Approval", "approval-responded": "Responded", "input-available": "Running", "input-streaming": "Pending", "output-available": "Completed", "output-denied": "Denied", "output-error": "Error", }; const statusIcons: Record = { "approval-requested": , "approval-responded": , "input-available": , "input-streaming": , "output-available": , "output-denied": , "output-error": , }; export const getStatusBadge = (status: ToolPart["state"]) => ( {statusIcons[status]} {statusLabels[status]} ); export const ToolHeader = ({ className, title, type, state, toolName, ...props }: ToolHeaderProps) => { const derivedName = type === "dynamic-tool" ? toolName : type.split("-").slice(1).join("-"); return (
{title ?? derivedName} {getStatusBadge(state)}
); }; export type ToolContentProps = ComponentProps<"div">; export const ToolContent = ({ className, children, ...props }: ToolContentProps) => (
{children}
); export type ToolInputProps = ComponentProps<"div"> & { input: ToolPart["input"]; }; export const ToolInput = ({ className, input, ...props }: ToolInputProps) => { if (typeof input === "undefined") { return null; } return (

Parameters

        {JSON.stringify(input, null, 2)}
      
); }; export type ToolOutputProps = ComponentProps<"div"> & { output: ToolPart["output"]; errorText: ToolPart["errorText"]; }; export const ToolOutput = ({ className, output, errorText, ...props }: ToolOutputProps) => { if (!(output || errorText)) { return null; } let renderedOutput: ReactNode = null; if (typeof output === "object" && output != null && !isValidElement(output)) { renderedOutput = (
        {JSON.stringify(output, null, 2)}
      
); } else if (typeof output === "string") { renderedOutput = (
        {output}
      
); } else if (output != null) { renderedOutput =
{output as ReactNode}
; } return (

{errorText ? "Error" : "Result"}

{errorText ? (
{errorText}
) : null} {renderedOutput}
); };