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