| 1 | import { ChevronRightIcon } from "@heroicons/react/24/outline"; |
| 2 | import { ContextItemWithId, Tool, ToolCallState } from "core"; |
| 3 | import { ComponentType, useContext, useMemo, useState } from "react"; |
| 4 | import { |
| 5 | ContextItemsPeekItem, |
| 6 | openContextItem, |
| 7 | } from "../../../components/mainInput/belowMainInput/ContextItemsPeek"; |
| 8 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 9 | import { ArgsItems, ArgsToggleIcon } from "./ToolCallArgs"; |
| 10 | import { ToolCallStatusMessage } from "./ToolCallStatusMessage"; |
| 11 | |
| 12 | interface SimpleToolCallUIProps { |
| 13 | toolCallState: ToolCallState; |
| 14 | tool: Tool | undefined; |
| 15 | contextItems: ContextItemWithId[]; |
| 16 | icon?: ComponentType<React.SVGProps<SVGSVGElement>>; |
| 17 | } |
| 18 | |
| 19 | export function SimpleToolCallUI({ |
| 20 | contextItems, |
| 21 | icon: Icon, |
| 22 | toolCallState, |
| 23 | tool, |
| 24 | }: SimpleToolCallUIProps) { |
| 25 | const ideMessenger = useContext(IdeMessengerContext); |
| 26 | |
| 27 | const ctxItems = useMemo(() => { |
| 28 | return contextItems?.filter((ctxItem) => !ctxItem.hidden) ?? []; |
| 29 | }, [contextItems]); |
| 30 | |
| 31 | const [open, setOpen] = useState(false); |
| 32 | const [isHovered, setIsHovered] = useState(false); |
| 33 | const [showingArgs, setShowingArgs] = useState(false); |
| 34 | |
| 35 | const args: [string, any][] = useMemo(() => { |
| 36 | return Object.entries(toolCallState.parsedArgs); |
| 37 | }, [toolCallState.parsedArgs]); |
| 38 | |
| 39 | const isToggleable = ctxItems.length > 1; |
| 40 | const isSingleItem = ctxItems.length === 1; |
| 41 | const shouldShowContent = isToggleable ? open : false; |
| 42 | const isClickable = isToggleable || isSingleItem; |
| 43 | |
| 44 | function handleClick() { |
| 45 | if (isToggleable) { |
| 46 | setOpen((prev) => !prev); |
| 47 | } else if (isSingleItem) { |
| 48 | openContextItem(ctxItems[0], ideMessenger); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | function renderIcon() { |
| 53 | if (!Icon && !isToggleable) { |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | if (isToggleable) { |
| 58 | const showChevron = isHovered || shouldShowContent; |
| 59 | return showChevron ? ( |
| 60 | <ChevronRightIcon |
| 61 | className={`text-description h-4 w-4 transition-transform duration-200 ease-in-out ${ |
| 62 | shouldShowContent ? "rotate-90" : "rotate-0" |
| 63 | }`} |
| 64 | /> |
| 65 | ) : ( |
| 66 | Icon && <Icon className="text-description h-4 w-4" /> |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | return Icon ? <Icon className="text-description h-4 w-4" /> : null; |
| 71 | } |
| 72 | |
| 73 | return ( |
| 74 | <div className="flex flex-col pl-5 pr-2 pt-4"> |
| 75 | <div className="flex min-w-0 flex-row items-center justify-between gap-2"> |
| 76 | <div |
| 77 | className={`text-description flex min-w-0 flex-row items-center justify-between gap-1.5 text-xs transition-colors duration-200 ease-in-out ${ |
| 78 | isClickable ? "cursor-pointer hover:brightness-125" : "" |
| 79 | }`} |
| 80 | onClick={isClickable ? handleClick : undefined} |
| 81 | onMouseEnter={isToggleable ? () => setIsHovered(true) : undefined} |
| 82 | onMouseLeave={isToggleable ? () => setIsHovered(false) : undefined} |
| 83 | data-testid="context-items-peek" |
| 84 | > |
| 85 | <div className="flex h-4 w-4 flex-shrink-0 flex-col items-center justify-center"> |
| 86 | {renderIcon()} |
| 87 | </div> |
| 88 | <ToolCallStatusMessage tool={tool} toolCallState={toolCallState} /> |
| 89 | </div> |
| 90 | {args.length > 0 && ( |
| 91 | <ArgsToggleIcon |
| 92 | isShowing={showingArgs} |
| 93 | setIsShowing={setShowingArgs} |
| 94 | toolCallId={toolCallState.toolCallId} |
| 95 | /> |
| 96 | )} |
| 97 | </div> |
| 98 | |
| 99 | <ArgsItems args={args} isShowing={showingArgs} /> |
| 100 | |
| 101 | {isToggleable && ( |
| 102 | <div |
| 103 | className={`mt-2 overflow-y-auto transition-all duration-300 ease-in-out ${ |
| 104 | shouldShowContent ? "max-h-[50vh] opacity-100" : "max-h-0 opacity-0" |
| 105 | }`} |
| 106 | > |
| 107 | {ctxItems.length > 0 ? ( |
| 108 | ctxItems.map((contextItem, idx) => ( |
| 109 | <ContextItemsPeekItem key={idx} contextItem={contextItem} /> |
| 110 | )) |
| 111 | ) : ( |
| 112 | <div className="text-description pl-5 text-xs italic"> |
| 113 | No tool call output |
| 114 | </div> |
| 115 | )} |
| 116 | </div> |
| 117 | )} |
| 118 | </div> |
| 119 | ); |
| 120 | } |