-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathToolCall.tsx
More file actions
231 lines (204 loc) · 5.93 KB
/
Copy pathToolCall.tsx
File metadata and controls
231 lines (204 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import type { JSX } from 'react';
import React, { useMemo } from 'react';
import { LoadingIcon, MemoryIcon, SearchIcon, ToolIcon } from '../icons';
import type {
AIToolPart,
MemoryToolPart,
SearchToolPart,
ToolCalls,
ToolDefinition,
} from '../types/AskiAi';
import { getSearchToolQueries, isSearchToolPart } from '../utils/ai';
import { ToolState } from './ui/ToolState';
export type ToolCallTranslations = {
/** Text shown while assistant is preparing tool call. */
preToolCallText: string;
/** Text shown while assistant is performing search tool call. */
searchingText: string;
/** Text shown while assistant is finished performing tool call. */
toolCallResultText: string;
/** Text shown when the agent saved related information to memory. */
savedMemoryToolResultText: string;
/** Text shown when the agent used the memory tool to enhance results. */
memoryToolResultText: string;
};
interface ToolCallProps {
part: AIToolPart;
translations: ToolCallTranslations;
tools: ToolCalls;
onSearchQueryClick?: (query: string) => void;
memoryEnabled?: boolean;
}
interface SearchToolProps {
part: SearchToolPart;
translations: ToolCallTranslations;
onSearchQueryClick?: (query: string) => void;
}
function SearchTool({
part,
translations,
onSearchQueryClick,
}: SearchToolProps) {
const { searchingText, preToolCallText, toolCallResultText } = translations;
switch (part.state) {
case 'input-streaming':
return (
<ToolState
shimmer={true}
icon={
<LoadingIcon className="DocSearch-AskAiScreen-SmallerLoadingIcon" />
}
variant="PartialCall"
>
<span>{searchingText}</span>
</ToolState>
);
case 'input-available': {
const queries = getSearchToolQueries(part);
return (
<>
{queries.map((q, index) => (
<ToolState
// oxlint-disable-next-line react/no-array-index-key
key={`${part.toolCallId}-call-${index}`}
shimmer={true}
icon={
<LoadingIcon className="DocSearch-AskAiScreen-SmallerLoadingIcon" />
}
variant="Call"
>
<span>
{preToolCallText} {`"${q}" ...`}
</span>
</ToolState>
))}
</>
);
}
case 'output-available': {
const queries = getSearchToolQueries(part);
return (
<>
{queries.map((q, index) => (
<ToolState
// oxlint-disable-next-line react/no-array-index-key
key={`${part.toolCallId}-result-${index}`}
icon={<SearchIcon />}
variant="Result"
>
<span>
{toolCallResultText}{' '}
{onSearchQueryClick ? (
<span
role="button"
tabIndex={0}
className="DocSearch-AskAiScreen-MessageContent-Tool-Query"
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
onSearchQueryClick(q);
}
}}
onClick={() => onSearchQueryClick(q)}
>
{' '}
"{q}"
</span>
) : (
<span className="DocSearch-AskAiScreen-MessageContent-Tool-Query">
{' '}
"{q}"
</span>
)}
</span>
</ToolState>
))}
</>
);
}
default:
return null;
}
}
interface CustomToolProps {
tool: ToolDefinition;
part: AIToolPart;
}
function CustomTool({ tool, part }: CustomToolProps) {
const toolOutput = useMemo(() => {
if (part.state !== 'output-available') return null;
return tool.render({ message: { input: part.input, output: part.output } });
}, [part.input, part.output, tool, part.state]);
if (part.state === 'output-error') {
return null;
}
if (part.state !== 'output-available') {
const { callingToolText = 'Loading tool' } = tool.translations || {};
return (
<ToolState shimmer={true} variant="PartialCall" icon={<ToolIcon />}>
<span>{callingToolText}</span>
</ToolState>
);
}
if (!toolOutput) return null;
return (
<ToolState icon={<ToolIcon />} variant="Result">
<span>{toolOutput}</span>
</ToolState>
);
}
function MemoryTool({
part,
translations,
}: {
part: MemoryToolPart;
translations: ToolCallTranslations;
}) {
const { savedMemoryToolResultText, memoryToolResultText } = translations;
if (part.state === 'output-error') return null;
if (part.type === 'tool-algolia_memorize') {
return (
<ToolState variant="Result" icon={<MemoryIcon />}>
{savedMemoryToolResultText}
</ToolState>
);
}
return (
<ToolState variant="Result" icon={<MemoryIcon />}>
{memoryToolResultText}
</ToolState>
);
}
function isMemoryToolPart(part: AIToolPart): part is MemoryToolPart {
return (
part.type === 'tool-algolia_ponder' ||
part.type === 'tool-algolia_memorize' ||
part.type === 'tool-algolia_memory_search'
);
}
export function ToolCall({
part,
translations,
tools,
onSearchQueryClick,
memoryEnabled = false,
}: ToolCallProps): JSX.Element | null {
const normalizedToolName = part.type.replace('tool-', '');
const customTool = tools[normalizedToolName];
if (customTool) {
return <CustomTool tool={customTool} part={part} />;
}
if (memoryEnabled && isMemoryToolPart(part)) {
return <MemoryTool part={part} translations={translations} />;
}
if (isSearchToolPart(part)) {
return (
<SearchTool
part={part}
translations={translations}
onSearchQueryClick={onSearchQueryClick}
/>
);
}
return null;
}