|
1 |
| -import { ChevronDownIcon, ChevronUpIcon } from "@heroicons/react/24/outline"; |
2 |
| -import { Tool, ToolCallDelta, ToolCallState } from "core"; |
3 |
| -import Mustache from "mustache"; |
4 |
| -import { ReactNode, useMemo, useState } from "react"; |
5 |
| -import { ToolTip } from "../../../components/gui/Tooltip"; |
6 |
| -import { useAppSelector } from "../../../redux/hooks"; |
| 1 | +import { Tool, ToolCallState } from "core"; |
| 2 | +import { useMemo, useState } from "react"; |
| 3 | +import { ArgsItems, ArgsToggleIcon } from "./ToolCallArgs"; |
| 4 | +import { ToolCallStatusMessage } from "./ToolCallStatusMessage"; |
7 | 5 |
|
8 | 6 | interface ToolCallDisplayProps {
|
9 | 7 | children: React.ReactNode;
|
10 | 8 | icon: React.ReactNode;
|
11 |
| - toolCall: ToolCallDelta; |
| 9 | + tool: Tool | undefined; |
12 | 10 | toolCallState: ToolCallState;
|
13 | 11 | }
|
14 | 12 |
|
15 |
| -export function getToolCallStatusMessage( |
16 |
| - tool: Tool | undefined, |
17 |
| - toolCallState: ToolCallState, |
18 |
| -) { |
19 |
| - if (!tool) return "Agent tool use"; |
20 |
| - |
21 |
| - const defaultToolDescription = ( |
22 |
| - <> |
23 |
| - <code>{tool.displayTitle ?? tool.function.name}</code> <span>tool</span> |
24 |
| - </> |
25 |
| - ); |
26 |
| - |
27 |
| - const futureMessage = tool.wouldLikeTo ? ( |
28 |
| - Mustache.render(tool.wouldLikeTo, toolCallState.parsedArgs) |
29 |
| - ) : ( |
30 |
| - <> |
31 |
| - <span>use the</span> {defaultToolDescription} |
32 |
| - </> |
33 |
| - ); |
34 |
| - |
35 |
| - let intro = ""; |
36 |
| - let message: ReactNode = ""; |
37 |
| - |
38 |
| - if ( |
39 |
| - toolCallState.status === "done" || |
40 |
| - (tool.isInstant && toolCallState.status === "calling") |
41 |
| - ) { |
42 |
| - intro = ""; |
43 |
| - message = tool.hasAlready ? ( |
44 |
| - Mustache.render(tool.hasAlready, toolCallState.parsedArgs) |
45 |
| - ) : ( |
46 |
| - <> |
47 |
| - <span>used the</span> {defaultToolDescription} |
48 |
| - </> |
49 |
| - ); |
50 |
| - } else if (toolCallState.status === "generating") { |
51 |
| - intro = "is generating output to"; |
52 |
| - message = futureMessage; |
53 |
| - } else if (toolCallState.status === "generated") { |
54 |
| - intro = "wants to"; |
55 |
| - message = futureMessage; |
56 |
| - } else if (toolCallState.status === "calling") { |
57 |
| - intro = "is"; |
58 |
| - message = tool.isCurrently ? ( |
59 |
| - Mustache.render(tool.isCurrently, toolCallState.parsedArgs) |
60 |
| - ) : ( |
61 |
| - <> |
62 |
| - <span>calling the</span> {defaultToolDescription} |
63 |
| - </> |
64 |
| - ); |
65 |
| - } else if ( |
66 |
| - toolCallState.status === "canceled" || |
67 |
| - toolCallState.status === "errored" |
68 |
| - ) { |
69 |
| - intro = "tried to"; |
70 |
| - message = futureMessage; |
71 |
| - } |
72 |
| - return ( |
73 |
| - <div className="block"> |
74 |
| - <span>Continue</span> {intro} {message} |
75 |
| - </div> |
76 |
| - ); |
77 |
| -} |
78 |
| - |
79 |
| -export function ToolCallDisplay(props: ToolCallDisplayProps) { |
80 |
| - const [isExpanded, setIsExpanded] = useState(false); |
81 |
| - const availableTools = useAppSelector((state) => state.config.config.tools); |
82 |
| - |
83 |
| - const tool = useMemo(() => { |
84 |
| - return availableTools.find( |
85 |
| - (tool) => props.toolCall.function?.name === tool.function.name, |
86 |
| - ); |
87 |
| - }, [availableTools, props.toolCall]); |
88 |
| - |
89 |
| - const statusMessage = useMemo(() => { |
90 |
| - return getToolCallStatusMessage(tool, props.toolCallState); |
91 |
| - }, [props.toolCallState, tool]); |
| 13 | +export function ToolCallDisplay({ |
| 14 | + tool, |
| 15 | + toolCallState, |
| 16 | + children, |
| 17 | + icon, |
| 18 | +}: ToolCallDisplayProps) { |
| 19 | + const [argsExpanded, setArgsExpanded] = useState(false); |
92 | 20 |
|
93 | 21 | const args: [string, any][] = useMemo(() => {
|
94 |
| - return Object.entries(props.toolCallState.parsedArgs); |
95 |
| - }, [props.toolCallState.parsedArgs]); |
96 |
| - |
97 |
| - const argsTooltipId = useMemo(() => { |
98 |
| - return "args-hover-" + props.toolCallState.toolCallId; |
99 |
| - }, [props.toolCallState]); |
| 22 | + return Object.entries(toolCallState.parsedArgs); |
| 23 | + }, [toolCallState.parsedArgs]); |
100 | 24 |
|
101 | 25 | return (
|
102 | 26 | <>
|
103 | 27 | <div className="relative flex flex-col justify-center p-4 pb-0">
|
104 | 28 | <div className="mb-4 flex flex-col">
|
105 | 29 | <div className="flex flex-row items-center justify-between gap-3">
|
106 | 30 | <div className="flex flex-row gap-2">
|
107 |
| - <div |
108 |
| - style={{ |
109 |
| - width: `16px`, |
110 |
| - height: `16px`, |
111 |
| - fontWeight: "bolder", |
112 |
| - marginTop: "1px", |
113 |
| - flexShrink: 0, |
114 |
| - }} |
115 |
| - > |
116 |
| - {props.icon} |
| 31 | + <div className="mt-[1px] h-4 w-4 flex-shrink-0 font-semibold"> |
| 32 | + {icon} |
117 | 33 | </div>
|
118 | 34 | {tool?.faviconUrl && (
|
119 | 35 | <img src={tool.faviconUrl} className="h-4 w-4 rounded-sm" />
|
120 | 36 | )}
|
121 | 37 | <div className="flex" data-testid="tool-call-status-message">
|
122 |
| - {statusMessage} |
| 38 | + <ToolCallStatusMessage |
| 39 | + tool={tool} |
| 40 | + toolCallState={toolCallState} |
| 41 | + /> |
123 | 42 | </div>
|
124 | 43 | </div>
|
125 | 44 | {!!args.length ? (
|
126 |
| - <div |
127 |
| - data-tooltip-id={argsTooltipId} |
128 |
| - onClick={() => setIsExpanded(!isExpanded)} |
129 |
| - className="ml-2 cursor-pointer hover:opacity-80" |
130 |
| - > |
131 |
| - {isExpanded ? ( |
132 |
| - <ChevronUpIcon className="h-4 w-4" /> |
133 |
| - ) : ( |
134 |
| - <ChevronDownIcon className="h-4 w-4" /> |
135 |
| - )} |
136 |
| - </div> |
| 45 | + <ArgsToggleIcon |
| 46 | + isShowing={argsExpanded} |
| 47 | + setIsShowing={setArgsExpanded} |
| 48 | + toolCallId={toolCallState.toolCallId} |
| 49 | + /> |
137 | 50 | ) : null}
|
138 |
| - <ToolTip id={argsTooltipId}> |
139 |
| - {isExpanded ? "Hide args" : "Show args"} |
140 |
| - </ToolTip> |
141 | 51 | </div>
|
142 |
| - |
143 |
| - {isExpanded && !!args.length && ( |
144 |
| - <div className="ml-7 mt-1"> |
145 |
| - {args.map(([key, value]) => ( |
146 |
| - <div key={key} className="flex gap-2 py-0.5"> |
147 |
| - <span className="text-lightgray">{key}:</span> |
148 |
| - <code className="line-clamp-1">{value.toString()}</code> |
149 |
| - </div> |
150 |
| - ))} |
151 |
| - </div> |
| 52 | + {argsExpanded && !!args.length && ( |
| 53 | + <ArgsItems args={args} isShowing={argsExpanded} /> |
152 | 54 | )}
|
153 | 55 | </div>
|
154 |
| - <div>{props.children}</div> |
| 56 | + <div>{children}</div> |
155 | 57 | </div>
|
156 | 58 | </>
|
157 | 59 | );
|
|
0 commit comments