| 1 | import { Tool, ToolCallState } from "core"; |
| 2 | import { ComponentType, useContext, useMemo, useState } from "react"; |
| 3 | import { |
| 4 | ContextItemsPeekItem, |
| 5 | openContextItem, |
| 6 | } from "../../../components/mainInput/belowMainInput/ContextItemsPeek"; |
| 7 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 8 | import { ToggleWithIcon } from "./ToggleWithIcon"; |
| 9 | import { ArgsItems, ArgsToggleIcon } from "./ToolCallArgs"; |
| 10 | import { ToolCallStatusMessage } from "./ToolCallStatusMessage"; |
| 11 | import { ToolTruncateHistoryIcon } from "./ToolTruncateHistoryIcon"; |
| 12 | import { toolCallStateToContextItems } from "./utils"; |
| 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 [showingArgs, setShowingArgs] = useState(false); |
| 35 | |
| 36 | const args: [string, any][] = useMemo(() => { |
| 37 | return Object.entries(toolCallState.parsedArgs); |
| 38 | }, [toolCallState.parsedArgs]); |
| 39 | |
| 40 | const isToggleable = shownContextItems.length > 1; |
| 41 | const isSingleItem = shownContextItems.length === 1; |
| 42 | const shouldShowContent = isToggleable ? open : false; |
| 43 | const isClickable = isToggleable || isSingleItem; |
| 44 | |
| 45 | function handleClick() { |
| 46 | if (isToggleable) { |
| 47 | setOpen((prev) => !prev); |
| 48 | } else if (isSingleItem) { |
| 49 | openContextItem(shownContextItems[0], ideMessenger); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | function handleToggleClick() { |
| 54 | if (isToggleable) { |
| 55 | setOpen((prev) => !prev); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | function handleIconClick() { |
| 60 | if (isSingleItem) { |
| 61 | openContextItem(shownContextItems[0], ideMessenger); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return ( |
| 66 | <div className="mt-1 flex flex-col px-4"> |
| 67 | <div className="flex min-w-0 flex-row items-center justify-between gap-2"> |
| 68 | <div |
| 69 | 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 ${ |
| 70 | isClickable ? "cursor-pointer hover:brightness-125" : "" |
| 71 | }`} |
| 72 | onClick={isClickable ? handleClick : undefined} |
| 73 | data-testid="context-items-peek" |
| 74 | > |
| 75 | <ToggleWithIcon |
| 76 | icon={Icon} |
| 77 | isToggleable={isToggleable} |
| 78 | open={shouldShowContent} |
| 79 | onClick={isToggleable ? handleToggleClick : handleIconClick} |
| 80 | isClickable={isSingleItem} |
| 81 | /> |
| 82 | <ToolCallStatusMessage tool={tool} toolCallState={toolCallState} /> |
| 83 | </div> |
| 84 | <div className="flex flex-row items-center gap-1.5"> |
| 85 | {!!toolCallState.output?.length && ( |
| 86 | <ToolTruncateHistoryIcon historyIndex={historyIndex} /> |
| 87 | )} |
| 88 | {args.length > 0 ? ( |
| 89 | <ArgsToggleIcon |
| 90 | isShowing={showingArgs} |
| 91 | setIsShowing={setShowingArgs} |
| 92 | /> |
| 93 | ) : null} |
| 94 | </div> |
| 95 | </div> |
| 96 | <ArgsItems args={args} isShowing={showingArgs} /> |
| 97 | |
| 98 | {isToggleable && ( |
| 99 | <div |
| 100 | className={`mt-2 overflow-y-auto transition-all duration-300 ease-in-out ${ |
| 101 | shouldShowContent ? "max-h-[50vh] opacity-100" : "max-h-0 opacity-0" |
| 102 | }`} |
| 103 | > |
| 104 | {shownContextItems.length > 0 ? ( |
| 105 | shownContextItems.map((contextItem, idx) => ( |
| 106 | <ContextItemsPeekItem key={idx} contextItem={contextItem} /> |
| 107 | )) |
| 108 | ) : ( |
| 109 | <div className="text-description pl-5 text-xs italic"> |
| 110 | No tool call output |
| 111 | </div> |
| 112 | )} |
| 113 | </div> |
| 114 | )} |
| 115 | </div> |
| 116 | ); |
| 117 | } |