Elffuss BraidHub

kikocis/continue public

1c83b0b1adf2 · 4.7 KiBRawPermalink
1import { ChatCompletionMessageParam } from "openai/resources.mjs";
2import { describe, it, expect, vi } from "vitest";
3import { ModelConfig } from "@continuedev/config-yaml";
4import { BaseLlmApi } from "@continuedev/openai-adapters";
5
6import { compactChatHistory } from "./compaction.js";
7import { streamChatResponse } from "./streamChatResponse.js";
8
9// Mock the dependencies
10vi.mock("./streamChatResponse.js", () => ({
11 streamChatResponse: vi.fn(),
12}));
13
14vi.mock("./util/tokenizer.js", () => ({
15 countChatHistoryTokens: vi.fn(),
16 getModelContextLimit: vi.fn(),
17}));
18
19describe("compaction infinite loop prevention", () => {
20 const mockModel: ModelConfig = {
21 name: "test-model",
22 provider: "test",
23 model: "test-model",
24 defaultCompletionOptions: {
25 maxTokens: 1000,
26 contextLength: 4000,
27 }
28 } as ModelConfig;
29
30 const mockLlmApi = {} as BaseLlmApi;
31
32 it("should not loop infinitely when pruning doesn't reduce history size", async () => {
33 const { countChatHistoryTokens, getModelContextLimit } = await import("./util/tokenizer.js");
34 const mockStreamResponse = vi.mocked(streamChatResponse);
35 const mockCountTokens = vi.mocked(countChatHistoryTokens);
36 const mockGetContextLimit = vi.mocked(getModelContextLimit);
37
38 // Setup mocks
39 mockGetContextLimit.mockReturnValue(4000);
40 mockCountTokens.mockReturnValue(5000); // Always too big
41 mockStreamResponse.mockImplementation(async (history, model, api, controller, callbacks) => {
42 callbacks?.onContent?.("Summary");
43 callbacks?.onContentComplete?.();
44 return "Summary";
45 });
46
47 // History that can't be pruned further (only system message)
48 const history: ChatCompletionMessageParam[] = [
49 { role: "system", content: "System message" },
50 ];
51
52 // This should not hang - it should break out of the loop
53 const result = await compactChatHistory(history, mockModel, mockLlmApi);
54
55 // Should complete successfully even though token count is still too high
56 expect(result.compactedHistory).toBeDefined();
57 expect(mockCountTokens).toHaveBeenCalled();
58 });
59
60 it("should not loop infinitely with history ending in assistant message", async () => {
61 const { countChatHistoryTokens, getModelContextLimit } = await import("./util/tokenizer.js");
62 const mockStreamResponse = vi.mocked(streamChatResponse);
63 const mockCountTokens = vi.mocked(countChatHistoryTokens);
64 const mockGetContextLimit = vi.mocked(getModelContextLimit);
65
66 // Setup mocks
67 mockGetContextLimit.mockReturnValue(4000);
68 mockCountTokens.mockReturnValue(5000); // Always too big
69 mockStreamResponse.mockImplementation(async (history, model, api, controller, callbacks) => {
70 callbacks?.onContent?.("Summary");
71 callbacks?.onContentComplete?.();
72 return "Summary";
73 });
74
75 // History that ends with assistant - pruning won't change it
76 const history: ChatCompletionMessageParam[] = [
77 { role: "system", content: "System message" },
78 { role: "user", content: "Hello" },
79 { role: "assistant", content: "Hi there" },
80 ];
81
82 // This should not hang
83 const result = await compactChatHistory(history, mockModel, mockLlmApi);
84
85 expect(result.compactedHistory).toBeDefined();
86 });
87
88 it("should successfully prune when pruning actually reduces size", async () => {
89 const { countChatHistoryTokens, getModelContextLimit } = await import("./util/tokenizer.js");
90 const mockStreamResponse = vi.mocked(streamChatResponse);
91 const mockCountTokens = vi.mocked(countChatHistoryTokens);
92 const mockGetContextLimit = vi.mocked(getModelContextLimit);
93
94 // Setup mocks
95 mockGetContextLimit.mockReturnValue(4000);
96
97 // Mock token counting to show reduction after pruning
98 let callCount = 0;
99 mockCountTokens.mockImplementation(() => {
100 callCount++;
101 if (callCount === 1) return 5000; // Initial too big
102 if (callCount === 2) return 3000; // After pruning, fits
103 return 2000; // Subsequent calls
104 });
105
106 mockStreamResponse.mockImplementation(async (history, model, api, controller, callbacks) => {
107 callbacks?.onContent?.("Summary");
108 callbacks?.onContentComplete?.();
109 return "Summary";
110 });
111
112 // History that can be successfully pruned
113 const history: ChatCompletionMessageParam[] = [
114 { role: "system", content: "System message" },
115 { role: "user", content: "Hello" },
116 { role: "assistant", content: "Hi there" },
117 { role: "user", content: "Another question" },
118 ];
119
120 const result = await compactChatHistory(history, mockModel, mockLlmApi);
121
122 expect(result.compactedHistory).toBeDefined();
123 // The function will call countTokens multiple times during the process
124 expect(mockCountTokens).toHaveBeenCalled();
125 });
126});