| 1 | import { Tool, ToolCallState } from "core"; |
| 2 | import { useMemo, useState } from "react"; |
| 3 | import { ArgsItems, ArgsToggleIcon } from "./ToolCallArgs"; |
| 4 | import { ToolCallStatusMessage } from "./ToolCallStatusMessage"; |
| 5 | |
| 6 | interface ToolCallDisplayProps { |
| 7 | children: React.ReactNode; |
| 8 | icon: React.ReactNode; |
| 9 | tool: Tool | undefined; |
| 10 | toolCallState: ToolCallState; |
| 11 | } |
| 12 | |
| 13 | export function ToolCallDisplay({ |
| 14 | tool, |
| 15 | toolCallState, |
| 16 | children, |
| 17 | icon, |
| 18 | }: ToolCallDisplayProps) { |
| 19 | const [argsExpanded, setArgsExpanded] = useState(false); |
| 20 | |
| 21 | const args: [string, any][] = useMemo(() => { |
| 22 | return Object.entries(toolCallState.parsedArgs); |
| 23 | }, [toolCallState.parsedArgs]); |
| 24 | |
| 25 | return ( |
| 26 | <> |
| 27 | <div className="flex flex-col justify-center p-4 pb-0"> |
| 28 | <div className="mb-4 flex flex-col"> |
| 29 | <div className="flex flex-row items-center justify-between gap-1.5"> |
| 30 | <div className="flex min-w-0 flex-row items-center gap-2"> |
| 31 | <div className="mt-[1px] h-4 w-4 flex-shrink-0 font-semibold"> |
| 32 | {icon} |
| 33 | </div> |
| 34 | {tool?.faviconUrl && ( |
| 35 | <img src={tool.faviconUrl} className="h-4 w-4 rounded-sm" /> |
| 36 | )} |
| 37 | <ToolCallStatusMessage |
| 38 | tool={tool} |
| 39 | toolCallState={toolCallState} |
| 40 | /> |
| 41 | </div> |
| 42 | {!!args.length ? ( |
| 43 | <ArgsToggleIcon |
| 44 | isShowing={argsExpanded} |
| 45 | setIsShowing={setArgsExpanded} |
| 46 | toolCallId={toolCallState.toolCallId} |
| 47 | /> |
| 48 | ) : null} |
| 49 | </div> |
| 50 | {argsExpanded && !!args.length && ( |
| 51 | <ArgsItems args={args} isShowing={argsExpanded} /> |
| 52 | )} |
| 53 | </div> |
| 54 | <div>{children}</div> |
| 55 | </div> |
| 56 | </> |
| 57 | ); |
| 58 | } |