From b452c3e4d66ea259410822d697af3aad2bd1a355 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Wed, 23 Apr 2025 18:37:43 -0700 Subject: [PATCH] simple tool call UI component --- .../components/mainInput/ContinueInputBox.tsx | 2 +- .../components/mainInput/Lump/LumpToolbar.tsx | 4 +- .../belowMainInput/ContextItemsPeek.tsx | 16 +-- gui/src/pages/gui/ToolCallDiv/ToolCall.tsx | 115 ++++-------------- .../pages/gui/ToolCallDiv/ToolCallArgs.tsx | 59 +++++++++ .../gui/ToolCallDiv/ToolCallStatusMessage.tsx | 69 +++++++++++ gui/src/pages/gui/ToolCallDiv/ToolOutput.tsx | 69 +++++++---- gui/src/pages/gui/ToolCallDiv/index.tsx | 54 ++++---- 8 files changed, 233 insertions(+), 155 deletions(-) create mode 100644 gui/src/pages/gui/ToolCallDiv/ToolCallArgs.tsx create mode 100644 gui/src/pages/gui/ToolCallDiv/ToolCallStatusMessage.tsx diff --git a/gui/src/components/mainInput/ContinueInputBox.tsx b/gui/src/components/mainInput/ContinueInputBox.tsx index 1bcbb3c8c..09691802b 100644 --- a/gui/src/components/mainInput/ContinueInputBox.tsx +++ b/gui/src/components/mainInput/ContinueInputBox.tsx @@ -5,7 +5,7 @@ import styled, { keyframes } from "styled-components"; import { defaultBorderRadius, vscBackground } from ".."; import { useAppSelector } from "../../redux/hooks"; import { selectSlashCommandComboBoxInputs } from "../../redux/selectors"; -import ContextItemsPeek from "./belowMainInput/ContextItemsPeek"; +import { ContextItemsPeek } from "./belowMainInput/ContextItemsPeek"; import { ToolbarOptions } from "./InputToolbar"; import { Lump } from "./Lump"; import { TipTapEditor } from "./TipTapEditor"; diff --git a/gui/src/components/mainInput/Lump/LumpToolbar.tsx b/gui/src/components/mainInput/Lump/LumpToolbar.tsx index dd91d4e66..482b4b033 100644 --- a/gui/src/components/mainInput/Lump/LumpToolbar.tsx +++ b/gui/src/components/mainInput/Lump/LumpToolbar.tsx @@ -101,7 +101,9 @@ export function LumpToolbar() { if (toolCallState?.status === "generated") { return ( - +
+ Tool call +
>; title?: JSX.Element | string; - showWhenNoResults?: boolean; } interface ContextItemsPeekItemProps { contextItem: ContextItemWithId; } -function ContextItemsPeekItem({ contextItem }: ContextItemsPeekItemProps) { +export function ContextItemsPeekItem({ + contextItem, +}: ContextItemsPeekItemProps) { const ideMessenger = useContext(IdeMessengerContext); const isUrl = contextItem.uri?.type === "url"; @@ -150,12 +151,11 @@ function ContextItemsPeekItem({ contextItem }: ContextItemsPeekItemProps) { ); } -function ContextItemsPeek({ +export function ContextItemsPeek({ contextItems, isCurrentContextPeek, icon, title, - showWhenNoResults, }: ContextItemsPeekProps) { const ctxItems = useMemo(() => { return contextItems?.filter((ctxItem) => !ctxItem.hidden) ?? []; @@ -165,11 +165,7 @@ function ContextItemsPeek({ const indicateIsGathering = isCurrentContextPeek && isGatheringContext; - if ( - !showWhenNoResults && - (!ctxItems || ctxItems.length === 0) && - !indicateIsGathering - ) { + if ((!ctxItems || ctxItems.length === 0) && !indicateIsGathering) { return null; } @@ -198,5 +194,3 @@ function ContextItemsPeek({ ); } - -export default ContextItemsPeek; diff --git a/gui/src/pages/gui/ToolCallDiv/ToolCall.tsx b/gui/src/pages/gui/ToolCallDiv/ToolCall.tsx index 0abee8f3f..d53486434 100644 --- a/gui/src/pages/gui/ToolCallDiv/ToolCall.tsx +++ b/gui/src/pages/gui/ToolCallDiv/ToolCall.tsx @@ -1,99 +1,31 @@ import { ChevronDownIcon, ChevronUpIcon } from "@heroicons/react/24/outline"; -import { Tool, ToolCallDelta, ToolCallState } from "core"; -import Mustache from "mustache"; -import { ReactNode, useMemo, useState } from "react"; +import { Tool, ToolCallState } from "core"; +import { useMemo, useState } from "react"; import { ToolTip } from "../../../components/gui/Tooltip"; -import { useAppSelector } from "../../../redux/hooks"; +import { ToolCallStatusMessage } from "./ToolCallStatusMessage"; interface ToolCallDisplayProps { children: React.ReactNode; icon: React.ReactNode; - toolCall: ToolCallDelta; + tool: Tool | undefined; toolCallState: ToolCallState; } -export function getToolCallStatusMessage( - tool: Tool | undefined, - toolCallState: ToolCallState, -) { - if (!tool) return "Agent tool use"; - - const defaultToolDescription = ( - <> - {tool.displayTitle ?? tool.function.name} tool - - ); - - const futureMessage = tool.wouldLikeTo ? ( - Mustache.render(tool.wouldLikeTo, toolCallState.parsedArgs) - ) : ( - <> - use the {defaultToolDescription} - - ); - - let intro = ""; - let message: ReactNode = ""; - - if ( - toolCallState.status === "done" || - (tool.isInstant && toolCallState.status === "calling") - ) { - intro = ""; - message = tool.hasAlready ? ( - Mustache.render(tool.hasAlready, toolCallState.parsedArgs) - ) : ( - <> - used the {defaultToolDescription} - - ); - } else if (toolCallState.status === "generating") { - intro = "is generating output to"; - message = futureMessage; - } else if (toolCallState.status === "generated") { - intro = "wants to"; - message = futureMessage; - } else if (toolCallState.status === "calling") { - intro = "is"; - message = tool.isCurrently ? ( - Mustache.render(tool.isCurrently, toolCallState.parsedArgs) - ) : ( - <> - calling the {defaultToolDescription} - - ); - } else if (toolCallState.status === "canceled") { - intro = "tried to"; - message = futureMessage; - } - return ( -
- Continue {intro} {message} -
- ); -} - -export function ToolCallDisplay(props: ToolCallDisplayProps) { +export function ToolCallDisplay({ + tool, + toolCallState, + children, + icon, +}: ToolCallDisplayProps) { const [isExpanded, setIsExpanded] = useState(false); - const availableTools = useAppSelector((state) => state.config.config.tools); - - const tool = useMemo(() => { - return availableTools.find( - (tool) => props.toolCall.function?.name === tool.function.name, - ); - }, [availableTools, props.toolCall]); - - const statusMessage = useMemo(() => { - return getToolCallStatusMessage(tool, props.toolCallState); - }, [props.toolCallState, tool]); const args: [string, any][] = useMemo(() => { - return Object.entries(props.toolCallState.parsedArgs); - }, [props.toolCallState.parsedArgs]); + return Object.entries(toolCallState.parsedArgs); + }, [toolCallState.parsedArgs]); const argsTooltipId = useMemo(() => { - return "args-hover-" + props.toolCallState.toolCallId; - }, [props.toolCallState]); + return "args-hover-" + toolCallState.toolCallId; + }, [toolCallState]); return ( <> @@ -101,22 +33,17 @@ export function ToolCallDisplay(props: ToolCallDisplayProps) {
-
- {props.icon} +
+ {icon}
{tool?.faviconUrl && ( )}
- {statusMessage} +
{!!args.length ? ( @@ -148,7 +75,7 @@ export function ToolCallDisplay(props: ToolCallDisplayProps) {
)}
-
{props.children}
+
{children}
); diff --git a/gui/src/pages/gui/ToolCallDiv/ToolCallArgs.tsx b/gui/src/pages/gui/ToolCallDiv/ToolCallArgs.tsx new file mode 100644 index 000000000..986239c4c --- /dev/null +++ b/gui/src/pages/gui/ToolCallDiv/ToolCallArgs.tsx @@ -0,0 +1,59 @@ +import { useMemo } from "react"; +import { ToolTip } from "../../../components/gui/Tooltip"; + +interface ArgsToggleIconProps { + isShowing: boolean; + setIsShowing: (val: boolean) => void; + toolCallId: string; +} + +export const ArgsToggleIcon = ({ + isShowing, + setIsShowing, + toolCallId, +}: ArgsToggleIconProps) => { + const argsTooltipId = useMemo(() => { + return "args-hover-" + toolCallId; + }, [toolCallId]); + + return ( + <> +
setIsShowing(!isShowing)} + className={`p-1 hover:opacity-80 ${isShowing ? "bg-red-200" : "bg-white"}`} + > + {`{}`} +
+ + {isShowing ? "Hide args" : "Show args"} + + + ); +}; + +interface ArgsItemsProps { + isShowing: boolean; + args: [string, string][]; +} + +export const ArgsItems = ({ args, isShowing }: ArgsItemsProps) => { + if (args.length === 0) { + return null; + } + + if (!isShowing) { + return null; + } + + return ( +
+ {args.map(([key, value]) => ( +
+ {key}: + {value.toString()} +
+ ))} +
+ ); +}; diff --git a/gui/src/pages/gui/ToolCallDiv/ToolCallStatusMessage.tsx b/gui/src/pages/gui/ToolCallDiv/ToolCallStatusMessage.tsx new file mode 100644 index 000000000..423d6f9ba --- /dev/null +++ b/gui/src/pages/gui/ToolCallDiv/ToolCallStatusMessage.tsx @@ -0,0 +1,69 @@ +import { Tool, ToolCallState } from "core"; +import Mustache from "mustache"; +import { ReactNode } from "react"; + +interface ToolCallStatusMessageProps { + tool: Tool | undefined; + toolCallState: ToolCallState; +} + +export function ToolCallStatusMessage({ + tool, + toolCallState, +}: ToolCallStatusMessageProps) { + if (!tool) return "Agent tool use"; + + const defaultToolDescription = ( + <> + {tool.displayTitle ?? tool.function.name} tool + + ); + + const futureMessage = tool.wouldLikeTo ? ( + Mustache.render(tool.wouldLikeTo, toolCallState.parsedArgs) + ) : ( + <> + use the {defaultToolDescription} + + ); + + let intro = ""; + let message: ReactNode = ""; + + if ( + toolCallState.status === "done" || + (tool.isInstant && toolCallState.status === "calling") + ) { + intro = ""; + message = tool.hasAlready ? ( + Mustache.render(tool.hasAlready, toolCallState.parsedArgs) + ) : ( + <> + used the {defaultToolDescription} + + ); + } else if (toolCallState.status === "generating") { + intro = "is generating output to"; + message = futureMessage; + } else if (toolCallState.status === "generated") { + intro = "wants to"; + message = futureMessage; + } else if (toolCallState.status === "calling") { + intro = "is"; + message = tool.isCurrently ? ( + Mustache.render(tool.isCurrently, toolCallState.parsedArgs) + ) : ( + <> + calling the {defaultToolDescription} + + ); + } else if (toolCallState.status === "canceled") { + intro = "tried to"; + message = futureMessage; + } + return ( +
+ Continue {intro} {message} +
+ ); +} diff --git a/gui/src/pages/gui/ToolCallDiv/ToolOutput.tsx b/gui/src/pages/gui/ToolCallDiv/ToolOutput.tsx index 069f839ca..4835e34f7 100644 --- a/gui/src/pages/gui/ToolCallDiv/ToolOutput.tsx +++ b/gui/src/pages/gui/ToolCallDiv/ToolOutput.tsx @@ -1,31 +1,56 @@ -import { ContextItemWithId } from "core"; -import { ComponentType } from "react"; -import ContextItemsPeek from "../../../components/mainInput/belowMainInput/ContextItemsPeek"; +import { ContextItemWithId, Tool, ToolCallState } from "core"; +import { ComponentType, useMemo, useState } from "react"; +import { ContextItemsPeekItem } from "../../../components/mainInput/belowMainInput/ContextItemsPeek"; +import ToggleDiv from "../../../components/ToggleDiv"; +import { ArgsToggleIcon } from "./ToolCallArgs"; +import { ToolCallStatusMessage } from "./ToolCallStatusMessage"; -interface ToolOutputProps { +interface SimpleToolCallUIProps { + toolCallState: ToolCallState; + tool: Tool | undefined; contextItems: ContextItemWithId[]; - toolCallId: string; icon?: ComponentType; - title?: JSX.Element | string; } -function ToolOutput(props: ToolOutputProps) { - // Terminal has dedicated UI to show the output - if (props.contextItems.some((ci) => ci.name === "Terminal")) { - return null; - } +export function SimpleToolCallUI({ + contextItems, + icon, + toolCallState, + tool, +}: SimpleToolCallUIProps) { + const ctxItems = useMemo(() => { + return contextItems?.filter((ctxItem) => !ctxItem.hidden) ?? []; + }, [contextItems]); + + const [showingArgs, setShowingArgs] = useState(false); return ( -
- -
+ +
+ +
+
+ +
+
+ } + > + {ctxItems.length ? ( + ctxItems.map((contextItem, idx) => ( + + )) + ) : ( +
+ No tool call output +
+ )} + ); } - -export default ToolOutput; diff --git a/gui/src/pages/gui/ToolCallDiv/index.tsx b/gui/src/pages/gui/ToolCallDiv/index.tsx index 06cf74515..a17b35d8e 100644 --- a/gui/src/pages/gui/ToolCallDiv/index.tsx +++ b/gui/src/pages/gui/ToolCallDiv/index.tsx @@ -8,6 +8,7 @@ import { FolderIcon, FolderOpenIcon, GlobeAltIcon, + ListBulletIcon, MagnifyingGlassIcon, MapIcon, XMarkIcon, @@ -24,8 +25,8 @@ import { vscButtonBackground } from "../../../components"; import Spinner from "../../../components/gui/Spinner"; import { useAppSelector } from "../../../redux/hooks"; import FunctionSpecificToolCallDiv from "./FunctionSpecificToolCallDiv"; -import { getToolCallStatusMessage, ToolCallDisplay } from "./ToolCall"; -import ToolOutput from "./ToolOutput"; +import { ToolCallDisplay } from "./ToolCall"; +import { SimpleToolCallUI } from "./ToolOutput"; interface ToolCallDivProps { toolCall: ToolCallDelta; @@ -43,23 +44,27 @@ const toolCallIcons: Record = { [BuiltInToolNames.ViewDiff]: CodeBracketIcon, [BuiltInToolNames.ViewRepoMap]: MapIcon, [BuiltInToolNames.ViewSubdirectory]: FolderOpenIcon, + [BuiltInToolNames.CreateRuleBlock]: ListBulletIcon, + // EditExistingFile = "builtin_edit_existing_file", + // CreateNewFile = "builtin_create_new_file", + // RunTerminalCommand = "builtin_run_terminal_command", }; -export function ToolCallDiv(props: ToolCallDivProps) { - function getIcon(state: ToolStatus) { - switch (state) { - case "generating": - case "calling": - return ; - case "generated": - return ; - case "done": - return ; - case "canceled": - return ; - } +function getStatusIcon(state: ToolStatus) { + switch (state) { + case "generating": + case "calling": + return ; + case "generated": + return ; + case "done": + return ; + case "canceled": + return ; } +} +export function ToolCallDiv(props: ToolCallDivProps) { const availableTools = useAppSelector((state) => state.config.config.tools); const tool = useMemo(() => { return availableTools.find( @@ -67,23 +72,20 @@ export function ToolCallDiv(props: ToolCallDivProps) { ); }, [availableTools, props.toolCall]); - const statusMessage = useMemo(() => { - return getToolCallStatusMessage(tool, props.toolCallState); - }, [props.toolCallState, tool]); - const icon = props.toolCall.function?.name && - toolCallIcons[props.toolCall.function?.name]; - if (icon && props.toolCall.id) { + toolCallIcons[props.toolCall.function.name]; + + if (icon) { return (
-
); @@ -91,8 +93,8 @@ export function ToolCallDiv(props: ToolCallDivProps) { return (