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