| 1 | // @ts-nocheck |
| 2 | // Generated by continue |
| 3 | import { ChatMessage, MessagePart } from "../index.js"; |
| 4 | import { |
| 5 | compileChatMessages, |
| 6 | countTokens, |
| 7 | countTokensAsync, |
| 8 | pruneLinesFromBottom, |
| 9 | pruneLinesFromTop, |
| 10 | pruneRawPromptFromTop, |
| 11 | pruneStringFromBottom, |
| 12 | pruneStringFromTop, |
| 13 | } from "./countTokens.js"; |
| 14 | |
| 15 | describe.skip("countTokens", () => { |
| 16 | it("should count tokens for a simple string", () => { |
| 17 | const content = "Hello world!"; |
| 18 | const tokenCount = countTokens(content, "gpt-4"); |
| 19 | expect(tokenCount).toBeGreaterThan(0); |
| 20 | }); |
| 21 | |
| 22 | it("should count tokens for an array of MessagePart", () => { |
| 23 | const content: MessagePart[] = [{ type: "text", text: "Hello world!" }]; |
| 24 | const tokenCount = countTokens(content, "gpt-4"); |
| 25 | expect(tokenCount).toBeGreaterThan(0); |
| 26 | }); |
| 27 | }); |
| 28 | |
| 29 | describe.skip("countTokensAsync", () => { |
| 30 | it("should count tokens asynchronously for a simple string", async () => { |
| 31 | const content = "Hello world!"; |
| 32 | const tokenCount = await countTokensAsync(content, "gpt-4"); |
| 33 | expect(tokenCount).toBeGreaterThan(0); |
| 34 | }); |
| 35 | |
| 36 | it("should count tokens asynchronously for an array of MessagePart", async () => { |
| 37 | const content: MessagePart[] = [{ type: "text", text: "Hello world!" }]; |
| 38 | const tokenCount = await countTokensAsync(content, "gpt-4"); |
| 39 | expect(tokenCount).toBeGreaterThan(0); |
| 40 | }); |
| 41 | }); |
| 42 | |
| 43 | describe.skip("pruneLinesFromTop", () => { |
| 44 | it("should prune lines from the top to fit within max tokens", () => { |
| 45 | const prompt = "Line 1\nLine 2\nLine 3\nLine 4"; |
| 46 | const pruned = pruneLinesFromTop(prompt, 5, "gpt-4"); |
| 47 | expect(pruned.split("\n").length).toBeLessThan(prompt.split("\n").length); |
| 48 | }); |
| 49 | |
| 50 | it("should return the original prompt if it's within max tokens", () => { |
| 51 | const prompt = "Line 1\nLine 2"; |
| 52 | const pruned = pruneLinesFromTop(prompt, 10, "gpt-4"); |
| 53 | expect(pruned).toEqual(prompt); |
| 54 | }); |
| 55 | |
| 56 | it("should return an empty string if maxTokens is 0", () => { |
| 57 | const prompt = "Line 1\nLine 2\nLine 3\nLine 4"; |
| 58 | const pruned = pruneLinesFromTop(prompt, 0, "gpt-4"); |
| 59 | expect(pruned).toEqual(""); |
| 60 | }); |
| 61 | |
| 62 | it("should handle an empty prompt string", () => { |
| 63 | const prompt = ""; |
| 64 | const pruned = pruneLinesFromTop(prompt, 5, "gpt-4"); |
| 65 | expect(pruned).toEqual(""); |
| 66 | }); |
| 67 | |
| 68 | it("should handle a prompt with a single line that exceeds maxTokens", () => { |
| 69 | const prompt = |
| 70 | "This is a single long line that will exceed the token limit"; |
| 71 | const pruned = pruneLinesFromTop(prompt, 5, "gpt-4"); |
| 72 | |
| 73 | expect(pruned).toEqual(""); |
| 74 | }); |
| 75 | |
| 76 | it("should correctly prune when all lines together exceed maxTokens but individual lines do not", () => { |
| 77 | const prompt = "L1\nL2\nL3\nL4"; |
| 78 | |
| 79 | const pruned = pruneLinesFromTop(prompt, 5, "gpt-4"); |
| 80 | expect(pruned).toEqual("L3\nL4"); |
| 81 | }); |
| 82 | }); |
| 83 | |
| 84 | describe.skip("pruneLinesFromBottom", () => { |
| 85 | it("should prune lines from the bottom to fit within max tokens", () => { |
| 86 | const prompt = "Line 1\nLine 2\nLine 3\nLine 4"; |
| 87 | const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4"); |
| 88 | expect(pruned.split("\n").length).toBeLessThan(prompt.split("\n").length); |
| 89 | }); |
| 90 | |
| 91 | it("should return the original prompt if it's within max tokens", () => { |
| 92 | const prompt = "Line 1\nLine 2"; |
| 93 | const pruned = pruneLinesFromBottom(prompt, 10, "gpt-4"); |
| 94 | expect(pruned).toEqual(prompt); |
| 95 | }); |
| 96 | |
| 97 | it("should return an empty string if maxTokens is 0", () => { |
| 98 | const prompt = "Line 1\nLine 2\nLine 3\nLine 4"; |
| 99 | const pruned = pruneLinesFromBottom(prompt, 0, "gpt-4"); |
| 100 | expect(pruned).toEqual(""); |
| 101 | }); |
| 102 | |
| 103 | it("should handle an empty prompt string", () => { |
| 104 | const prompt = ""; |
| 105 | const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4"); |
| 106 | expect(pruned).toEqual(""); |
| 107 | }); |
| 108 | |
| 109 | it("should handle a prompt with a single line that exceeds maxTokens", () => { |
| 110 | const prompt = |
| 111 | "This is a single long line that will exceed the token limit"; |
| 112 | const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4"); |
| 113 | |
| 114 | expect(pruned).toEqual(""); |
| 115 | }); |
| 116 | |
| 117 | it("should correctly prune when all lines together exceed maxTokens but individual lines do not", () => { |
| 118 | const prompt = "L1\nL2\nL3\nL4"; |
| 119 | |
| 120 | const pruned = pruneLinesFromBottom(prompt, 5, "gpt-4"); |
| 121 | expect(pruned).toEqual("L1\nL2"); |
| 122 | }); |
| 123 | }); |
| 124 | |
| 125 | describe.skip("pruneRawPromptFromTop", () => { |
| 126 | it("should prune string from the top based on maxTokens", () => { |
| 127 | const result = pruneRawPromptFromTop("gpt-4", 5, "Hello world!", 2); |
| 128 | expect(result.length).toBeLessThan("Hello world!".length); |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | describe.skip("pruneStringFromTop", () => { |
| 133 | it("should prune string from the top based on maxTokens", () => { |
| 134 | const result = pruneStringFromTop("gpt-4", 5, "Hello world!"); |
| 135 | expect(result.length).toBeLessThan("Hello world!".length); |
| 136 | }); |
| 137 | }); |
| 138 | |
| 139 | describe.skip("pruneStringFromBottom", () => { |
| 140 | it("should prune string from the bottom based on maxTokens", () => { |
| 141 | const result = pruneStringFromBottom("gpt-4", 5, "Hello world!"); |
| 142 | expect(result.length).toBeLessThan("Hello world!".length); |
| 143 | }); |
| 144 | }); |
| 145 | |
| 146 | describe.skip("compileChatMessages", () => { |
| 147 | it("should compile and handle an empty or undefined message list", () => { |
| 148 | const compiled = compileChatMessages("gpt-4", undefined, 100, 50, false); |
| 149 | expect(compiled.length).toBe(0); |
| 150 | }); |
| 151 | |
| 152 | it("should compile chat messages without truncating if within context length", () => { |
| 153 | const msgs: ChatMessage[] = [ |
| 154 | { role: "user", content: "Hello world!" }, |
| 155 | { role: "assistant", content: "Hi there!" }, |
| 156 | ]; |
| 157 | const compiled = compileChatMessages("gpt-4", msgs, 100, 10, false); |
| 158 | expect(compiled.length).toBe(2); |
| 159 | }); |
| 160 | |
| 161 | it("should throw an error if maxTokens is close to or exceeds contextLength", () => { |
| 162 | expect(() => { |
| 163 | compileChatMessages("gpt-4", [], 100, 90, false); |
| 164 | }).toThrow(); |
| 165 | }); |
| 166 | |
| 167 | it("should filter out any empty or system messages", () => { |
| 168 | const msgs: ChatMessage[] = [ |
| 169 | { role: "system", content: "" }, |
| 170 | { role: "user", content: "" }, |
| 171 | { role: "assistant", content: "Hi there!" }, |
| 172 | ]; |
| 173 | const compiled = compileChatMessages("gpt-4", msgs, 100, 10, false); |
| 174 | expect(compiled.length).toBe(1); |
| 175 | }); |
| 176 | }); |