Elffuss BraidHub

kikocis/continue public

main · 9.2 KiBRawPermalink
1import { ToolCallState, ToolStatus } from "core";
2import { ChatHistoryItemWithMessageId } from "../slices/sessionSlice";
3import {
4 findAllCurToolCalls,
5 findAllCurToolCallsByStatus,
6 findToolCallById,
7 hasCurrentToolCalls,
8} from "./index";
9
10// Helper function to create a tool call state
11function createToolCallState(
12 toolCallId: string,
13 functionName: string,
14 status: ToolStatus = "generated",
15): ToolCallState {
16 return {
17 toolCallId,
18 toolCall: {
19 id: toolCallId,
20 type: "function",
21 function: {
22 name: functionName,
23 arguments: "{}",
24 },
25 },
26 status,
27 parsedArgs: {},
28 };
29}
30
31// Helper function to create a chat history item
32function createChatHistoryItem(
33 role: "user" | "assistant" | "tool",
34 content: string,
35 options?: {
36 toolCallStates?: ToolCallState[];
37 toolCallId?: string;
38 },
39): ChatHistoryItemWithMessageId {
40 let message: any = {
41 id: "test-id",
42 role,
43 content,
44 };
45
46 if (role === "tool" && options?.toolCallId) {
47 message.toolCallId = options.toolCallId;
48 }
49
50 const historyItem: ChatHistoryItemWithMessageId = {
51 message,
52 contextItems: [],
53 };
54
55 if (options?.toolCallStates) {
56 historyItem.toolCallStates = options.toolCallStates;
57 }
58
59 return historyItem;
60}
61
62describe("hasCurrentToolCalls", () => {
63 it("should return false for empty chat history", () => {
64 const result = hasCurrentToolCalls([]);
65 expect(result).toBe(false);
66 });
67
68 it("should return false when last message has no tool calls", () => {
69 const chatHistory = [
70 createChatHistoryItem("user", "Hello"),
71 createChatHistoryItem("assistant", "Hi there!"),
72 ];
73 const result = hasCurrentToolCalls(chatHistory);
74 expect(result).toBe(false);
75 });
76
77 it("should return true when last message has tool calls", () => {
78 const toolCallStates = [
79 createToolCallState("tool-1", "get_weather"),
80 createToolCallState("tool-2", "get_time"),
81 ];
82 const chatHistory = [
83 createChatHistoryItem("user", "What's the weather?"),
84 createChatHistoryItem("assistant", "I'll check the weather for you", {
85 toolCallStates,
86 }),
87 ];
88 const result = hasCurrentToolCalls(chatHistory);
89 expect(result).toBe(true);
90 });
91
92 it("should return false when toolCallStates is empty array", () => {
93 const chatHistory = [
94 createChatHistoryItem("assistant", "I'll help you", {
95 toolCallStates: [],
96 }),
97 ];
98 const result = hasCurrentToolCalls(chatHistory);
99 expect(result).toBe(false);
100 });
101});
102
103describe("findCurrentToolCallsByStatus", () => {
104 it("should return empty array for empty chat history", () => {
105 const result = findAllCurToolCallsByStatus([], "generated");
106 expect(result).toEqual([]);
107 });
108
109 it("should return empty array when no tool calls match status", () => {
110 const toolCallStates = [
111 createToolCallState("tool-1", "get_weather", "calling"),
112 createToolCallState("tool-2", "get_time", "done"),
113 ];
114 const chatHistory = [
115 createChatHistoryItem("assistant", "I'll check the weather", {
116 toolCallStates,
117 }),
118 ];
119 const result = findAllCurToolCallsByStatus(chatHistory, "generated");
120 expect(result).toEqual([]);
121 });
122
123 it("should return tool calls matching the specified status", () => {
124 const generatedToolCall = createToolCallState(
125 "tool-1",
126 "get_weather",
127 "generated",
128 );
129 const callingToolCall = createToolCallState(
130 "tool-2",
131 "get_time",
132 "calling",
133 );
134 const toolCallStates = [generatedToolCall, callingToolCall];
135 const chatHistory = [
136 createChatHistoryItem("assistant", "I'll check the weather", {
137 toolCallStates,
138 }),
139 ];
140 const result = findAllCurToolCallsByStatus(chatHistory, "generated");
141 expect(result).toEqual([generatedToolCall]);
142 });
143
144 it("should work with multiple tool calls", () => {
145 const toolCallStates = [
146 createToolCallState("tool-1", "get_weather", "generated"),
147 createToolCallState("tool-2", "get_time", "generated"),
148 createToolCallState("tool-3", "get_location", "calling"),
149 ];
150 const chatHistory = [
151 createChatHistoryItem("assistant", "I'll help you", {
152 toolCallStates,
153 }),
154 ];
155 const result = findAllCurToolCallsByStatus(chatHistory, "generated");
156 expect(result).toHaveLength(2);
157 expect(result[0].toolCallId).toBe("tool-1");
158 expect(result[1].toolCallId).toBe("tool-2");
159 });
160});
161
162describe("findCurrentToolCalls", () => {
163 it("should return empty array for empty chat history", () => {
164 const result = findAllCurToolCalls([]);
165 expect(result).toEqual([]);
166 });
167
168 it("should return empty array when last message has no tool calls", () => {
169 const chatHistory = [
170 createChatHistoryItem("user", "Hello"),
171 createChatHistoryItem("assistant", "Hi there!"),
172 ];
173 const result = findAllCurToolCalls(chatHistory);
174 expect(result).toEqual([]);
175 });
176
177 it("should return multiple tool call states", () => {
178 const toolCallStates = [
179 createToolCallState("tool-1", "get_weather"),
180 createToolCallState("tool-2", "get_time"),
181 ];
182 const chatHistory = [
183 createChatHistoryItem("assistant", "I'll check the weather", {
184 toolCallStates,
185 }),
186 ];
187 const result = findAllCurToolCalls(chatHistory);
188 expect(result).toEqual(toolCallStates);
189 });
190});
191
192describe("findToolCall", () => {
193 it("should return undefined for empty chat history", () => {
194 const result = findToolCallById([], "non-existent-id");
195 expect(result).toBeUndefined();
196 });
197
198 it("should return undefined when tool call ID is not found", () => {
199 const toolCallStates = [createToolCallState("tool-1", "get_weather")];
200 const chatHistory = [
201 createChatHistoryItem("assistant", "I'll check the weather", {
202 toolCallStates,
203 }),
204 ];
205 const result = findToolCallById(chatHistory, "non-existent-id");
206 expect(result).toBeUndefined();
207 });
208
209 it("should find tool call in toolCallStates array", () => {
210 const toolCallState = createToolCallState("tool-1", "get_weather");
211 const toolCallStates = [toolCallState];
212 const chatHistory = [
213 createChatHistoryItem("assistant", "I'll check the weather", {
214 toolCallStates,
215 }),
216 ];
217 const result = findToolCallById(chatHistory, "tool-1");
218 expect(result).toBe(toolCallState);
219 });
220
221 it("should find tool call in multiple tool calls", () => {
222 const toolCallState1 = createToolCallState("tool-1", "get_weather");
223 const toolCallState2 = createToolCallState("tool-2", "get_time");
224 const toolCallStates = [toolCallState1, toolCallState2];
225 const chatHistory = [
226 createChatHistoryItem("assistant", "I'll help you", {
227 toolCallStates,
228 }),
229 ];
230
231 expect(findToolCallById(chatHistory, "tool-1")).toBe(toolCallState1);
232 expect(findToolCallById(chatHistory, "tool-2")).toBe(toolCallState2);
233 });
234
235 it("should search across multiple messages", () => {
236 const toolCallStates1 = [createToolCallState("tool-1", "get_weather")];
237 const toolCallStates2 = [
238 createToolCallState("tool-2", "get_time"),
239 createToolCallState("tool-3", "get_location"),
240 ];
241 const chatHistory = [
242 createChatHistoryItem("assistant", "I'll check the weather", {
243 toolCallStates: toolCallStates1,
244 }),
245 createChatHistoryItem("user", "Also get the time"),
246 createChatHistoryItem("assistant", "I'll get the time and location", {
247 toolCallStates: toolCallStates2,
248 }),
249 ];
250
251 expect(findToolCallById(chatHistory, "tool-1")).toBe(toolCallStates1[0]);
252 expect(findToolCallById(chatHistory, "tool-2")).toBe(toolCallStates2[0]);
253 expect(findToolCallById(chatHistory, "tool-3")).toBe(toolCallStates2[1]);
254 });
255});
256
257describe("Edge cases and integration", () => {
258 it("should handle messages with mixed content and tool calls", () => {
259 const toolCallStates = [
260 createToolCallState("multi-1", "get_weather"),
261 createToolCallState("multi-2", "get_time"),
262 ];
263 const chatHistory = [
264 createChatHistoryItem("user", "What's the weather and time?"),
265 createChatHistoryItem("assistant", "I'll get both for you", {
266 toolCallStates,
267 }),
268 ];
269
270 // hasCurrentToolCalls should return true
271 expect(hasCurrentToolCalls(chatHistory)).toBe(true);
272
273 // findCurrentToolCalls should return all tool calls
274 expect(findAllCurToolCalls(chatHistory)).toEqual(toolCallStates);
275
276 // findToolCall should find all tool calls
277 expect(findToolCallById(chatHistory, "multi-1")).toBe(toolCallStates[0]);
278 expect(findToolCallById(chatHistory, "multi-2")).toBe(toolCallStates[1]);
279 });
280
281 it("should work correctly when only some messages have tool calls", () => {
282 const toolCallStates = [createToolCallState("tool-1", "get_weather")];
283 const chatHistory = [
284 createChatHistoryItem("user", "Hello"),
285 createChatHistoryItem("assistant", "Hi there!"),
286 createChatHistoryItem("user", "What's the weather?"),
287 createChatHistoryItem("assistant", "I'll check the weather", {
288 toolCallStates,
289 }),
290 ];
291
292 expect(hasCurrentToolCalls(chatHistory)).toBe(true);
293 expect(findAllCurToolCalls(chatHistory)).toEqual(toolCallStates);
294 expect(findToolCallById(chatHistory, "tool-1")).toBe(toolCallStates[0]);
295 });
296});