simple tool call UI component

This commit is contained in:
Dallin Romney 2025-04-23 18:37:43 -07:00
parent 6ff2f1f683
commit b452c3e4d6
8 changed files with 233 additions and 155 deletions

View file

@ -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";

View file

@ -101,7 +101,9 @@ export function LumpToolbar() {
if (toolCallState?.status === "generated") {
return (
<Container>
<GeneratingIndicator />
<div>
<span className="hidden italic text-gray-400 sm:flex">Tool call</span>
</div>
<div className="flex gap-2 pb-0.5">
<StopButton

View file

@ -18,14 +18,15 @@ interface ContextItemsPeekProps {
isCurrentContextPeek: boolean;
icon?: ComponentType<React.SVGProps<SVGSVGElement>>;
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({
</ToggleDiv>
);
}
export default ContextItemsPeek;

View file

@ -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 = (
<>
<code>{tool.displayTitle ?? tool.function.name}</code> <span>tool</span>
</>
);
const futureMessage = tool.wouldLikeTo ? (
Mustache.render(tool.wouldLikeTo, toolCallState.parsedArgs)
) : (
<>
<span>use the</span> {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)
) : (
<>
<span>used the</span> {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)
) : (
<>
<span>calling the</span> {defaultToolDescription}
</>
);
} else if (toolCallState.status === "canceled") {
intro = "tried to";
message = futureMessage;
}
return (
<div className="block">
<span>Continue</span> {intro} {message}
</div>
);
}
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) {
<div className="mb-4 flex flex-col">
<div className="flex flex-row items-center justify-between gap-3">
<div className="flex flex-row gap-2">
<div
style={{
width: `16px`,
height: `16px`,
fontWeight: "bolder",
marginTop: "1px",
flexShrink: 0,
}}
>
{props.icon}
<div className="mt-[1px] h-4 w-4 flex-shrink-0 font-semibold">
{icon}
</div>
{tool?.faviconUrl && (
<img src={tool.faviconUrl} className="h-4 w-4 rounded-sm" />
)}
<div className="flex" data-testid="tool-call-status-message">
{statusMessage}
<ToolCallStatusMessage
tool={tool}
toolCallState={toolCallState}
/>
</div>
</div>
{!!args.length ? (
@ -148,7 +75,7 @@ export function ToolCallDisplay(props: ToolCallDisplayProps) {
</div>
)}
</div>
<div>{props.children}</div>
<div>{children}</div>
</div>
</>
);

View file

@ -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 (
<>
<div
data-tooltip-id={argsTooltipId}
onClick={() => setIsShowing(!isShowing)}
className={`p-1 hover:opacity-80 ${isShowing ? "bg-red-200" : "bg-white"}`}
>
<code>{`{}`}</code>
</div>
<ToolTip id={argsTooltipId}>
{isShowing ? "Hide args" : "Show args"}
</ToolTip>
</>
);
};
interface ArgsItemsProps {
isShowing: boolean;
args: [string, string][];
}
export const ArgsItems = ({ args, isShowing }: ArgsItemsProps) => {
if (args.length === 0) {
return null;
}
if (!isShowing) {
return null;
}
return (
<div className="ml-7 mt-1">
{args.map(([key, value]) => (
<div key={key} className="flex gap-2 py-0.5">
<span className="text-lightgray">{key}:</span>
<code className="line-clamp-1">{value.toString()}</code>
</div>
))}
</div>
);
};

View file

@ -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 = (
<>
<code>{tool.displayTitle ?? tool.function.name}</code> <span>tool</span>
</>
);
const futureMessage = tool.wouldLikeTo ? (
Mustache.render(tool.wouldLikeTo, toolCallState.parsedArgs)
) : (
<>
<span>use the</span> {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)
) : (
<>
<span>used the</span> {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)
) : (
<>
<span>calling the</span> {defaultToolDescription}
</>
);
} else if (toolCallState.status === "canceled") {
intro = "tried to";
message = futureMessage;
}
return (
<div className="block">
<span>Continue</span> {intro} {message}
</div>
);
}

View file

@ -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 (
<div>
<ContextItemsPeek
showWhenNoResults={true}
title={props.title}
icon={props.icon}
isCurrentContextPeek={false}
contextItems={props.contextItems}
/>
</div>
<ToggleDiv
icon={icon}
title={
<div className="flex flex-row items-center justify-between">
<div>
<ToolCallStatusMessage tool={tool} toolCallState={toolCallState} />
</div>
<div>
<ArgsToggleIcon
isShowing={showingArgs}
setIsShowing={setShowingArgs}
toolCallId={toolCallState.toolCallId}
/>
</div>
</div>
}
>
{ctxItems.length ? (
ctxItems.map((contextItem, idx) => (
<ContextItemsPeekItem key={idx} contextItem={contextItem} />
))
) : (
<div className="pl-2 text-xs italic text-gray-400">
No tool call output
</div>
)}
</ToggleDiv>
);
}
export default ToolOutput;

View file

@ -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<string, ComponentType> = {
[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 <Spinner />;
case "generated":
return <ArrowRightIcon color={vscButtonBackground} />;
case "done":
return <CheckIcon className="text-green-500" />;
case "canceled":
return <XMarkIcon className="text-red-500" />;
}
function getStatusIcon(state: ToolStatus) {
switch (state) {
case "generating":
case "calling":
return <Spinner />;
case "generated":
return <ArrowRightIcon color={vscButtonBackground} />;
case "done":
return <CheckIcon className="text-green-500" />;
case "canceled":
return <XMarkIcon className="text-red-500" />;
}
}
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 (
<div className="ml-4 mt-2">
<ToolOutput
title={statusMessage}
<SimpleToolCallUI
tool={tool}
toolCallState={props.toolCallState}
icon={
props.toolCallState.status === "generated" ? ArrowRightIcon : icon
}
contextItems={props.output ?? []}
toolCallId={props.toolCall.id}
/>
</div>
);
@ -91,8 +93,8 @@ export function ToolCallDiv(props: ToolCallDivProps) {
return (
<ToolCallDisplay
icon={getIcon(props.toolCallState.status)}
toolCall={props.toolCall}
icon={getStatusIcon(props.toolCallState.status)}
tool={tool}
toolCallState={props.toolCallState}
>
<FunctionSpecificToolCallDiv