Elffuss BraidHub

kikocis/continue public

627d7d0d1f85 · 12.1 KiBRawPermalink
1// @ts-nocheck
2// Generated by continue
3import { ChatMessage, MessagePart, ToolCall } from "../index.js";
4import {
5 compileChatMessages,
6 countTokens,
7 countTokensAsync,
8 extractToolSequence,
9 pruneLinesFromBottom,
10 pruneLinesFromTop,
11 pruneRawPromptFromTop,
12 pruneStringFromBottom,
13 pruneStringFromTop,
14} from "./countTokens.js";
15
16describe.skip("countTokens", () => {
17 it("should count tokens for a simple string", () => {
18 const content = "Hello world!";
19 const tokenCount = countTokens(content, "gpt-4");
20 expect(tokenCount).toBeGreaterThan(0);
21 });
22
23 it("should count tokens for an array of MessagePart", () => {
24 const content: MessagePart[] = [{ type: "text", text: "Hello world!" }];
25 const tokenCount = countTokens(content, "gpt-4");
26 expect(tokenCount).toBeGreaterThan(0);
27 });
28});
29
30describe.skip("countTokensAsync", () => {
31 it("should count tokens asynchronously for a simple string", async () => {
32 const content = "Hello world!";
33 const tokenCount = await countTokensAsync(content, "gpt-4");
34 expect(tokenCount).toBeGreaterThan(0);
35 });
36
37 it("should count tokens asynchronously for an array of MessagePart", async () => {
38 const content: MessagePart[] = [{ type: "text", text: "Hello world!" }];
39 const tokenCount = await countTokensAsync(content, "gpt-4");
40 expect(tokenCount).toBeGreaterThan(0);
41 });
42});
43
44describe.skip("pruneLinesFromTop", () => {
45 it("should prune lines from the top to fit within max tokens", () => {
46 const prompt = "Line 1\nLine 2\nLine 3\nLine 4";
47 const pruned = pruneLinesFromTop(prompt, 5, "gpt-4");
48 expect(pruned.split("\n").length).toBeLessThan(prompt.split("\n").length);
49 });
50
51 it("should return the original prompt if it's within max tokens", () => {
52 const prompt = "Line 1\nLine 2";
53 const pruned = pruneLinesFromTop(prompt, 10, "gpt-4");
54 expect(pruned).toEqual(prompt);
55 });
56
57 it("should return an empty string if maxTokens is 0", () => {
58 const prompt = "Line 1\nLine 2\nLine 3\nLine 4";
59 const pruned = pruneLinesFromTop(prompt, 0, "gpt-4");
60 expect(pruned).toEqual("");
61 });
62
63 it("should handle an empty prompt string", () => {
64 const prompt = "";
65 const pruned = pruneLinesFromTop(prompt, 5, "gpt-4");
66 expect(pruned).toEqual("");
67 });
68
69 it("should handle a prompt with a single line that exceeds maxTokens", () => {
70 const prompt =
71 "This is a single long line that will exceed the token limit";
72 const pruned = pruneLinesFromTop(prompt, 5, "gpt-4");
73
74 expect(pruned).toEqual("");
75 });
76
77 it("should correctly prune when all lines together exceed maxTokens but individual lines do not", () => {
78 const prompt = "L1\nL2\nL3\nL4";
79
80 const pruned = pruneLinesFromTop(prompt, 5, "gpt-4");
81 expect(pruned).toEqual("L3\nL4");
82 });
83});
84
85describe.skip("pruneLinesFromBottom", () => {
86 it("should prune lines from the bottom to fit within max tokens", () => {
87 const prompt = "Line 1\nLine 2\nLine 3\nLine 4";
88 const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4");
89 expect(pruned.split("\n").length).toBeLessThan(prompt.split("\n").length);
90 });
91
92 it("should return the original prompt if it's within max tokens", () => {
93 const prompt = "Line 1\nLine 2";
94 const pruned = pruneLinesFromBottom(prompt, 10, "gpt-4");
95 expect(pruned).toEqual(prompt);
96 });
97
98 it("should return an empty string if maxTokens is 0", () => {
99 const prompt = "Line 1\nLine 2\nLine 3\nLine 4";
100 const pruned = pruneLinesFromBottom(prompt, 0, "gpt-4");
101 expect(pruned).toEqual("");
102 });
103
104 it("should handle an empty prompt string", () => {
105 const prompt = "";
106 const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4");
107 expect(pruned).toEqual("");
108 });
109
110 it("should handle a prompt with a single line that exceeds maxTokens", () => {
111 const prompt =
112 "This is a single long line that will exceed the token limit";
113 const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4");
114
115 expect(pruned).toEqual("");
116 });
117
118 it("should correctly prune when all lines together exceed maxTokens but individual lines do not", () => {
119 const prompt = "L1\nL2\nL3\nL4";
120
121 const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4");
122 expect(pruned).toEqual("L1\nL2");
123 });
124});
125
126describe.skip("pruneRawPromptFromTop", () => {
127 it("should prune string from the top based on maxTokens", () => {
128 const result = pruneRawPromptFromTop("gpt-4", 5, "Hello world!", 2);
129 expect(result.length).toBeLessThan("Hello world!".length);
130 });
131});
132
133describe.skip("pruneStringFromTop", () => {
134 it("should prune string from the top based on maxTokens", () => {
135 const result = pruneStringFromTop("gpt-4", 5, "Hello world!");
136 expect(result.length).toBeLessThan("Hello world!".length);
137 });
138});
139
140describe.skip("pruneStringFromBottom", () => {
141 it("should prune string from the bottom based on maxTokens", () => {
142 const result = pruneStringFromBottom("gpt-4", 5, "Hello world!");
143 expect(result.length).toBeLessThan("Hello world!".length);
144 });
145});
146
147describe.skip("compileChatMessages", () => {
148 it("should compile and handle an empty or undefined message list", () => {
149 const compiled = compileChatMessages("gpt-4", undefined, 100, 50, false);
150 expect(compiled.length).toBe(0);
151 });
152
153 it("should compile chat messages without truncating if within context length", () => {
154 const msgs: ChatMessage[] = [
155 { role: "user", content: "Hello world!" },
156 { role: "assistant", content: "Hi there!" },
157 ];
158 const compiled = compileChatMessages("gpt-4", msgs, 100, 10, false);
159 expect(compiled.length).toBe(2);
160 });
161
162 it("should throw an error if maxTokens is close to or exceeds contextLength", () => {
163 expect(() => {
164 compileChatMessages("gpt-4", [], 100, 90, false);
165 }).toThrow();
166 });
167
168 it("should filter out any empty or system messages", () => {
169 const msgs: ChatMessage[] = [
170 { role: "system", content: "" },
171 { role: "user", content: "" },
172 { role: "assistant", content: "Hi there!" },
173 ];
174 const compiled = compileChatMessages("gpt-4", msgs, 100, 10, false);
175 expect(compiled.length).toBe(1);
176 });
177});
178
179describe("extractToolSequence", () => {
180 // Helper function to create mock messages
181 const createUserMessage = (content: string): ChatMessage => ({
182 role: "user",
183 content,
184 });
185
186 const createAssistantMessage = (
187 content: string,
188 toolCalls?: ToolCall[],
189 ): ChatMessage => ({
190 role: "assistant",
191 content,
192 toolCalls,
193 });
194
195 const createToolMessage = (
196 content: string,
197 toolCallId: string,
198 ): ChatMessage => ({
199 role: "tool",
200 content,
201 toolCallId,
202 });
203
204 const createToolCall = (
205 id: string,
206 name: string,
207 args: object,
208 ): ToolCall => ({
209 id,
210 type: "function",
211 function: {
212 name,
213 arguments: JSON.stringify(args),
214 },
215 });
216
217 test("extractToolSequence should handle a single user message", () => {
218 const messages: ChatMessage[] = [createUserMessage("Hello world!")];
219 const sequence = extractToolSequence(messages);
220
221 expect(sequence).toHaveLength(1);
222 expect(sequence[0].role).toBe("user");
223 expect(sequence[0].content).toBe("Hello world!");
224 expect(messages).toHaveLength(0); // Original array should be modified
225 });
226
227 test("extractToolSequence should handle a single tool message with assistant", () => {
228 const toolCall = createToolCall("tc_1", "read_file", { path: "test.txt" });
229 const messages: ChatMessage[] = [
230 createAssistantMessage("I'll read the file for you.", [toolCall]),
231 createToolMessage("File contents here", "tc_1"),
232 ];
233 const sequence = extractToolSequence(messages);
234
235 expect(sequence).toHaveLength(2);
236 expect(sequence[0].role).toBe("assistant");
237 expect(sequence[1].role).toBe("tool");
238 expect(sequence[1].toolCallId).toBe("tc_1");
239 expect(messages).toHaveLength(0);
240 });
241
242 test("extractToolSequence should handle multiple consecutive tool messages", () => {
243 const toolCall1 = createToolCall("tc_1", "read_file", { path: "test.txt" });
244 const toolCall2 = createToolCall("tc_2", "write_file", {
245 path: "output.txt",
246 content: "data",
247 });
248 const messages: ChatMessage[] = [
249 createAssistantMessage("I'll read and write files.", [
250 toolCall1,
251 toolCall2,
252 ]),
253 createToolMessage("File contents 1", "tc_1"),
254 createToolMessage("File written successfully", "tc_2"),
255 ];
256 const sequence = extractToolSequence(messages);
257
258 expect(sequence).toHaveLength(3);
259 expect(sequence[0].role).toBe("assistant");
260 expect(sequence[1].role).toBe("tool");
261 expect(sequence[1].toolCallId).toBe("tc_1");
262 expect(sequence[2].role).toBe("tool");
263 expect(sequence[2].toolCallId).toBe("tc_2");
264 expect(messages).toHaveLength(0);
265 });
266
267 test("extractToolSequence should handle tool sequence with previous context", () => {
268 const toolCall = createToolCall("tc_1", "search", { query: "test" });
269 const messages: ChatMessage[] = [
270 createUserMessage("What's in the codebase?"),
271 createAssistantMessage("Let me search for you.", [toolCall]),
272 createToolMessage("Search results here", "tc_1"),
273 ];
274 const sequence = extractToolSequence(messages);
275
276 expect(sequence).toHaveLength(2);
277 expect(sequence[0].role).toBe("assistant");
278 expect(sequence[1].role).toBe("tool");
279 expect(messages).toHaveLength(1); // User message should remain
280 expect(messages[0].role).toBe("user");
281 });
282
283 test("extractToolSequence should throw error when tool message has no matching tool call", () => {
284 const toolCall = createToolCall("tc_1", "read_file", { path: "test.txt" });
285 const messages: ChatMessage[] = [
286 createAssistantMessage("I'll read the file for you.", [toolCall]),
287 createToolMessage("File contents here", "tc_2"), // Wrong tool call ID
288 ];
289
290 expect(() => extractToolSequence(messages)).toThrow(
291 'Error parsing chat history: no tool call found to match tool output for id "tc_2"',
292 );
293 });
294
295 test("extractToolSequence should throw error when last message is not user or tool", () => {
296 const messages: ChatMessage[] = [
297 createUserMessage("Hello"),
298 createAssistantMessage("Hi there!"),
299 ];
300
301 expect(() => extractToolSequence(messages)).toThrow(
302 "Error parsing chat history: no user/tool message found",
303 );
304 });
305
306 test("extractToolSequence should throw error when messages array is empty", () => {
307 const messages: ChatMessage[] = [];
308
309 expect(() => extractToolSequence(messages)).toThrow(
310 "Error parsing chat history: no user/tool message found",
311 );
312 });
313
314 test("extractToolSequence should handle tool sequence without assistant message", () => {
315 const messages: ChatMessage[] = [
316 createToolMessage("Orphaned tool message", "tc_1"),
317 ];
318 const sequence = extractToolSequence(messages);
319
320 expect(sequence).toHaveLength(1);
321 expect(sequence[0].role).toBe("tool");
322 expect(sequence[0].toolCallId).toBe("tc_1");
323 expect(messages).toHaveLength(0);
324 });
325
326 test("extractToolSequence should handle complex tool sequence with multiple tool calls", () => {
327 const toolCall1 = createToolCall("tc_1", "read_file", {
328 path: "file1.txt",
329 });
330 const toolCall2 = createToolCall("tc_2", "read_file", {
331 path: "file2.txt",
332 });
333 const toolCall3 = createToolCall("tc_3", "write_file", {
334 path: "output.txt",
335 content: "merged",
336 });
337
338 const messages: ChatMessage[] = [
339 createUserMessage("Merge these files"),
340 createAssistantMessage("I'll read both files and merge them.", [
341 toolCall1,
342 toolCall2,
343 toolCall3,
344 ]),
345 createToolMessage("Contents of file1", "tc_1"),
346 createToolMessage("Contents of file2", "tc_2"),
347 createToolMessage("Files merged successfully", "tc_3"),
348 ];
349
350 const sequence = extractToolSequence(messages);
351
352 expect(sequence).toHaveLength(4);
353 expect(sequence[0].role).toBe("assistant");
354 expect(sequence[1].role).toBe("tool");
355 expect(sequence[1].toolCallId).toBe("tc_1");
356 expect(sequence[2].role).toBe("tool");
357 expect(sequence[2].toolCallId).toBe("tc_2");
358 expect(sequence[3].role).toBe("tool");
359 expect(sequence[3].toolCallId).toBe("tc_3");
360 expect(messages).toHaveLength(1); // User message should remain
361 });
362});