Changesets · Branches · Tags · Compare
| Changeset | a84925a020033f98896e1e41116aef88976ba810b8f85846485a20b3a6edee56 |
|---|---|
| Author | Nate <sestinj@gmail.com> · 2025-08-23 10:57 -07:00 |
| Committer | Nate <sestinj@gmail.com> · 2025-08-23 10:57 -07:00 |
| Parents | 1c83b0b1adf2 |
7 files changed
modified extensions/cli/src/commands/chat.ts +4 −1
| @@ -174,7 +174,10 @@ | ||
| 174 | 174 | // Custom callbacks for headless mode console output |
| 175 | 175 | const callbacks = { |
| 176 | 176 | onSystemMessage: (message: string) => { |
| 177 | - if (message.includes("Auto-compacting") || message.includes("Approaching")) { | |
| 177 | + if ( | |
| 178 | + message.includes("Auto-compacting") || | |
| 179 | + message.includes("Approaching") | |
| 180 | + ) { | |
| 178 | 181 | if (!isHeadless) { |
| 179 | 182 | console.info(chalk.yellow(`\n${message}`)); |
| 180 | 183 | } else if (format === "json") { |
modified extensions/cli/src/compaction.infiniteLoop.test.ts +37 −25
| @@ -24,13 +24,15 @@ | ||
| 24 | 24 | defaultCompletionOptions: { |
| 25 | 25 | maxTokens: 1000, |
| 26 | 26 | contextLength: 4000, |
| 27 | - } | |
| 27 | + }, | |
| 28 | 28 | } as ModelConfig; |
| 29 | 29 | |
| 30 | 30 | const mockLlmApi = {} as BaseLlmApi; |
| 31 | 31 | |
| 32 | 32 | it("should not loop infinitely when pruning doesn't reduce history size", async () => { |
| 33 | - const { countChatHistoryTokens, getModelContextLimit } = await import("./util/tokenizer.js"); | |
| 33 | + const { countChatHistoryTokens, getModelContextLimit } = await import( | |
| 34 | + "./util/tokenizer.js" | |
| 35 | + ); | |
| 34 | 36 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 35 | 37 | const mockCountTokens = vi.mocked(countChatHistoryTokens); |
| 36 | 38 | const mockGetContextLimit = vi.mocked(getModelContextLimit); |
| @@ -38,11 +40,13 @@ | ||
| 38 | 40 | // Setup mocks |
| 39 | 41 | mockGetContextLimit.mockReturnValue(4000); |
| 40 | 42 | 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 | - }); | |
| 43 | + mockStreamResponse.mockImplementation( | |
| 44 | + async (history, model, api, controller, callbacks) => { | |
| 45 | + callbacks?.onContent?.("Summary"); | |
| 46 | + callbacks?.onContentComplete?.(); | |
| 47 | + return "Summary"; | |
| 48 | + }, | |
| 49 | + ); | |
| 46 | 50 | |
| 47 | 51 | // History that can't be pruned further (only system message) |
| 48 | 52 | const history: ChatCompletionMessageParam[] = [ |
| @@ -51,14 +55,16 @@ | ||
| 51 | 55 | |
| 52 | 56 | // This should not hang - it should break out of the loop |
| 53 | 57 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 54 | - | |
| 58 | + | |
| 55 | 59 | // Should complete successfully even though token count is still too high |
| 56 | 60 | expect(result.compactedHistory).toBeDefined(); |
| 57 | 61 | expect(mockCountTokens).toHaveBeenCalled(); |
| 58 | 62 | }); |
| 59 | 63 | |
| 60 | 64 | it("should not loop infinitely with history ending in assistant message", async () => { |
| 61 | - const { countChatHistoryTokens, getModelContextLimit } = await import("./util/tokenizer.js"); | |
| 65 | + const { countChatHistoryTokens, getModelContextLimit } = await import( | |
| 66 | + "./util/tokenizer.js" | |
| 67 | + ); | |
| 62 | 68 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 63 | 69 | const mockCountTokens = vi.mocked(countChatHistoryTokens); |
| 64 | 70 | const mockGetContextLimit = vi.mocked(getModelContextLimit); |
| @@ -66,11 +72,13 @@ | ||
| 66 | 72 | // Setup mocks |
| 67 | 73 | mockGetContextLimit.mockReturnValue(4000); |
| 68 | 74 | 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 | - }); | |
| 75 | + mockStreamResponse.mockImplementation( | |
| 76 | + async (history, model, api, controller, callbacks) => { | |
| 77 | + callbacks?.onContent?.("Summary"); | |
| 78 | + callbacks?.onContentComplete?.(); | |
| 79 | + return "Summary"; | |
| 80 | + }, | |
| 81 | + ); | |
| 74 | 82 | |
| 75 | 83 | // History that ends with assistant - pruning won't change it |
| 76 | 84 | const history: ChatCompletionMessageParam[] = [ |
| @@ -81,19 +89,21 @@ | ||
| 81 | 89 | |
| 82 | 90 | // This should not hang |
| 83 | 91 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 84 | - | |
| 92 | + | |
| 85 | 93 | expect(result.compactedHistory).toBeDefined(); |
| 86 | 94 | }); |
| 87 | 95 | |
| 88 | 96 | it("should successfully prune when pruning actually reduces size", async () => { |
| 89 | - const { countChatHistoryTokens, getModelContextLimit } = await import("./util/tokenizer.js"); | |
| 97 | + const { countChatHistoryTokens, getModelContextLimit } = await import( | |
| 98 | + "./util/tokenizer.js" | |
| 99 | + ); | |
| 90 | 100 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 91 | 101 | const mockCountTokens = vi.mocked(countChatHistoryTokens); |
| 92 | 102 | const mockGetContextLimit = vi.mocked(getModelContextLimit); |
| 93 | 103 | |
| 94 | 104 | // Setup mocks |
| 95 | 105 | mockGetContextLimit.mockReturnValue(4000); |
| 96 | - | |
| 106 | + | |
| 97 | 107 | // Mock token counting to show reduction after pruning |
| 98 | 108 | let callCount = 0; |
| 99 | 109 | mockCountTokens.mockImplementation(() => { |
| @@ -102,13 +112,15 @@ | ||
| 102 | 112 | if (callCount === 2) return 3000; // After pruning, fits |
| 103 | 113 | return 2000; // Subsequent calls |
| 104 | 114 | }); |
| 105 | - | |
| 106 | - mockStreamResponse.mockImplementation(async (history, model, api, controller, callbacks) => { | |
| 107 | - callbacks?.onContent?.("Summary"); | |
| 108 | - callbacks?.onContentComplete?.(); | |
| 109 | - return "Summary"; | |
| 110 | - }); | |
| 111 | 115 | |
| 116 | + mockStreamResponse.mockImplementation( | |
| 117 | + async (history, model, api, controller, callbacks) => { | |
| 118 | + callbacks?.onContent?.("Summary"); | |
| 119 | + callbacks?.onContentComplete?.(); | |
| 120 | + return "Summary"; | |
| 121 | + }, | |
| 122 | + ); | |
| 123 | + | |
| 112 | 124 | // History that can be successfully pruned |
| 113 | 125 | const history: ChatCompletionMessageParam[] = [ |
| 114 | 126 | { role: "system", content: "System message" }, |
| @@ -118,9 +130,9 @@ | ||
| 118 | 130 | ]; |
| 119 | 131 | |
| 120 | 132 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 121 | - | |
| 133 | + | |
| 122 | 134 | expect(result.compactedHistory).toBeDefined(); |
| 123 | 135 | // The function will call countTokens multiple times during the process |
| 124 | 136 | expect(mockCountTokens).toHaveBeenCalled(); |
| 125 | 137 | }); |
| 126 | -}); | |
| No newline at the end of the file | ||
| 138 | +}); |
modified extensions/cli/src/compaction.pruneLastMessage.test.ts +28 −12
| @@ -53,16 +53,18 @@ | ||
| 53 | 53 | ]; |
| 54 | 54 | |
| 55 | 55 | const result = pruneLastMessage(history); |
| 56 | - expect(result).toEqual([ | |
| 57 | - { role: "system", content: "System" }, | |
| 58 | - ]); | |
| 56 | + expect(result).toEqual([{ role: "system", content: "System" }]); | |
| 59 | 57 | }); |
| 60 | 58 | |
| 61 | 59 | it("should remove assistant+tool sequence when second-to-last is assistant with tool calls", () => { |
| 62 | 60 | const history: ChatCompletionMessageParam[] = [ |
| 63 | 61 | { role: "system", content: "System" }, |
| 64 | 62 | { role: "user", content: "Run command" }, |
| 65 | - { role: "assistant", content: "Running...", tool_calls: [{ id: "1" } as any] }, | |
| 63 | + { | |
| 64 | + role: "assistant", | |
| 65 | + content: "Running...", | |
| 66 | + tool_calls: [{ id: "1" } as any], | |
| 67 | + }, | |
| 66 | 68 | { role: "tool", content: "Command output", tool_call_id: "1" }, |
| 67 | 69 | ]; |
| 68 | 70 | |
| @@ -95,16 +97,18 @@ | ||
| 95 | 97 | ]; |
| 96 | 98 | |
| 97 | 99 | const result = pruneLastMessage(history); |
| 98 | - expect(result).toEqual([ | |
| 99 | - { role: "system", content: "System" }, | |
| 100 | - ]); | |
| 100 | + expect(result).toEqual([{ role: "system", content: "System" }]); | |
| 101 | 101 | }); |
| 102 | 102 | |
| 103 | 103 | it("should handle tool call sequences by removing user after tool", () => { |
| 104 | 104 | const history: ChatCompletionMessageParam[] = [ |
| 105 | 105 | { role: "system", content: "System" }, |
| 106 | 106 | { role: "user", content: "Do something" }, |
| 107 | - { role: "assistant", content: "I'll help", tool_calls: [{ id: "1" } as any] }, | |
| 107 | + { | |
| 108 | + role: "assistant", | |
| 109 | + content: "I'll help", | |
| 110 | + tool_calls: [{ id: "1" } as any], | |
| 111 | + }, | |
| 108 | 112 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 109 | 113 | { role: "user", content: "Follow up question" }, |
| 110 | 114 | ]; |
| @@ -113,7 +117,11 @@ | ||
| 113 | 117 | expect(result).toEqual([ |
| 114 | 118 | { role: "system", content: "System" }, |
| 115 | 119 | { role: "user", content: "Do something" }, |
| 116 | - { role: "assistant", content: "I'll help", tool_calls: [{ id: "1" } as any] }, | |
| 120 | + { | |
| 121 | + role: "assistant", | |
| 122 | + content: "I'll help", | |
| 123 | + tool_calls: [{ id: "1" } as any], | |
| 124 | + }, | |
| 117 | 125 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 118 | 126 | ]); |
| 119 | 127 | }); |
| @@ -134,7 +142,11 @@ | ||
| 134 | 142 | { role: "user", content: "Request 1" }, |
| 135 | 143 | { role: "assistant", content: "Response 1" }, |
| 136 | 144 | { role: "user", content: "Request 2" }, |
| 137 | - { role: "assistant", content: "Using tool", tool_calls: [{ id: "1" } as any] }, | |
| 145 | + { | |
| 146 | + role: "assistant", | |
| 147 | + content: "Using tool", | |
| 148 | + tool_calls: [{ id: "1" } as any], | |
| 149 | + }, | |
| 138 | 150 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 139 | 151 | { role: "assistant", content: "Final response" }, |
| 140 | 152 | { role: "user", content: "Follow up 1" }, |
| @@ -147,7 +159,11 @@ | ||
| 147 | 159 | { role: "user", content: "Request 1" }, |
| 148 | 160 | { role: "assistant", content: "Response 1" }, |
| 149 | 161 | { role: "user", content: "Request 2" }, |
| 150 | - { role: "assistant", content: "Using tool", tool_calls: [{ id: "1" } as any] }, | |
| 162 | + { | |
| 163 | + role: "assistant", | |
| 164 | + content: "Using tool", | |
| 165 | + tool_calls: [{ id: "1" } as any], | |
| 166 | + }, | |
| 151 | 167 | { role: "tool", content: "Tool result", tool_call_id: "1" }, |
| 152 | 168 | { role: "assistant", content: "Final response" }, |
| 153 | 169 | ]); |
| @@ -166,4 +182,4 @@ | ||
| 166 | 182 | { role: "assistant", content: "Hi" }, |
| 167 | 183 | ]); |
| 168 | 184 | }); |
| 169 | -}); | |
| No newline at the end of the file | ||
| 185 | +}); |
modified extensions/cli/src/slashCommands.test.ts +93 −44
| @@ -1,5 +1,12 @@ | ||
| 1 | 1 | import type { AssistantUnrolled } from "@continuedev/config-yaml"; |
| 2 | -import { describe, it, expect, vi, beforeEach, type MockedFunction } from "vitest"; | |
| 2 | +import { | |
| 3 | + describe, | |
| 4 | + it, | |
| 5 | + expect, | |
| 6 | + vi, | |
| 7 | + beforeEach, | |
| 8 | + type MockedFunction, | |
| 9 | +} from "vitest"; | |
| 3 | 10 | |
| 4 | 11 | import type { AuthConfig, AuthenticatedConfig } from "./auth/workos.js"; |
| 5 | 12 | import type { ConfigServiceState } from "./services/types.js"; |
| @@ -9,58 +16,60 @@ | ||
| 9 | 16 | vi.mock("./services/index.js", () => ({ |
| 10 | 17 | services: { |
| 11 | 18 | config: { |
| 12 | - getState: vi.fn() | |
| 13 | - } | |
| 19 | + getState: vi.fn(), | |
| 20 | + }, | |
| 14 | 21 | }, |
| 15 | 22 | reloadService: vi.fn(), |
| 16 | 23 | SERVICE_NAMES: { |
| 17 | - AUTH: 'AUTH' | |
| 18 | - } | |
| 24 | + AUTH: "AUTH", | |
| 25 | + }, | |
| 19 | 26 | })); |
| 20 | 27 | |
| 21 | 28 | // Mock auth functions |
| 22 | 29 | vi.mock("./auth/workos.js", () => ({ |
| 23 | 30 | isAuthenticated: vi.fn(), |
| 24 | 31 | isAuthenticatedConfig: vi.fn(), |
| 25 | - loadAuthConfig: vi.fn() | |
| 32 | + loadAuthConfig: vi.fn(), | |
| 26 | 33 | })); |
| 27 | 34 | |
| 28 | 35 | // Mock telemetry |
| 29 | 36 | vi.mock("./telemetry/posthogService.js", () => ({ |
| 30 | 37 | posthogService: { |
| 31 | - capture: vi.fn() | |
| 32 | - } | |
| 38 | + capture: vi.fn(), | |
| 39 | + }, | |
| 33 | 40 | })); |
| 34 | 41 | |
| 35 | 42 | // Mock commands |
| 36 | 43 | vi.mock("./commands/commands.js", () => ({ |
| 37 | - getAllSlashCommands: vi.fn(() => []) | |
| 44 | + getAllSlashCommands: vi.fn(() => []), | |
| 38 | 45 | })); |
| 39 | 46 | |
| 40 | 47 | // Mock os and path |
| 41 | 48 | vi.mock("os", () => ({ |
| 42 | 49 | default: { |
| 43 | - homedir: vi.fn(() => "/home/test") | |
| 44 | - } | |
| 50 | + homedir: vi.fn(() => "/home/test"), | |
| 51 | + }, | |
| 45 | 52 | })); |
| 46 | 53 | |
| 47 | 54 | vi.mock("path", () => ({ |
| 48 | 55 | default: { |
| 49 | - join: vi.fn((...parts) => parts.join("/")) | |
| 56 | + join: vi.fn((...parts) => parts.join("/")), | |
| 50 | 57 | }, |
| 51 | - join: vi.fn((...parts) => parts.join("/")) | |
| 58 | + join: vi.fn((...parts) => parts.join("/")), | |
| 52 | 59 | })); |
| 53 | 60 | |
| 54 | 61 | // Mock session functions |
| 55 | 62 | vi.mock("./session.js", () => ({ |
| 56 | - getSessionFilePath: vi.fn(() => "/home/test/.continue/cli-sessions/continue-cli-pid-12345.json"), | |
| 57 | - hasSession: vi.fn(() => false) | |
| 63 | + getSessionFilePath: vi.fn( | |
| 64 | + () => "/home/test/.continue/cli-sessions/continue-cli-pid-12345.json", | |
| 65 | + ), | |
| 66 | + hasSession: vi.fn(() => false), | |
| 58 | 67 | })); |
| 59 | 68 | |
| 60 | 69 | describe("slashCommands", () => { |
| 61 | 70 | const mockAssistant: AssistantUnrolled = { |
| 62 | 71 | name: "test-assistant", |
| 63 | - version: "1.0.0" | |
| 72 | + version: "1.0.0", | |
| 64 | 73 | }; |
| 65 | 74 | |
| 66 | 75 | beforeEach(() => { |
| @@ -71,11 +80,17 @@ | ||
| 71 | 80 | it("should handle /info command when not authenticated", async () => { |
| 72 | 81 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 73 | 82 | const { services } = await import("./services/index.js"); |
| 74 | - | |
| 75 | - (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(false); | |
| 76 | - (services.config.getState as MockedFunction<typeof services.config.getState>).mockReturnValue({ | |
| 83 | + | |
| 84 | + ( | |
| 85 | + isAuthenticated as MockedFunction<typeof isAuthenticated> | |
| 86 | + ).mockReturnValue(false); | |
| 87 | + ( | |
| 88 | + services.config.getState as MockedFunction< | |
| 89 | + typeof services.config.getState | |
| 90 | + > | |
| 91 | + ).mockReturnValue({ | |
| 77 | 92 | config: null, |
| 78 | - configPath: "/test/config.yaml" | |
| 93 | + configPath: "/test/config.yaml", | |
| 79 | 94 | } as ConfigServiceState); |
| 80 | 95 | |
| 81 | 96 | const result = await handleSlashCommands("/info", mockAssistant); |
| @@ -91,46 +106,66 @@ | ||
| 91 | 106 | }); |
| 92 | 107 | |
| 93 | 108 | it("should handle /info command when authenticated via environment", async () => { |
| 94 | - const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = await import("./auth/workos.js"); | |
| 109 | + const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = | |
| 110 | + await import("./auth/workos.js"); | |
| 95 | 111 | const { services } = await import("./services/index.js"); |
| 96 | - | |
| 97 | - (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(true); | |
| 98 | - (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue({} as AuthConfig); | |
| 112 | + | |
| 113 | + ( | |
| 114 | + isAuthenticated as MockedFunction<typeof isAuthenticated> | |
| 115 | + ).mockReturnValue(true); | |
| 116 | + (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue( | |
| 117 | + {} as AuthConfig, | |
| 118 | + ); | |
| 99 | 119 | (isAuthenticatedConfig as any).mockReturnValue(false); |
| 100 | - (services.config.getState as MockedFunction<typeof services.config.getState>).mockReturnValue({ | |
| 120 | + ( | |
| 121 | + services.config.getState as MockedFunction< | |
| 122 | + typeof services.config.getState | |
| 123 | + > | |
| 124 | + ).mockReturnValue({ | |
| 101 | 125 | config: null, |
| 102 | - configPath: undefined | |
| 126 | + configPath: undefined, | |
| 103 | 127 | } as ConfigServiceState); |
| 104 | 128 | |
| 105 | 129 | const result = await handleSlashCommands("/info", mockAssistant); |
| 106 | 130 | |
| 107 | 131 | expect(result).toBeDefined(); |
| 108 | 132 | expect(result?.output).toContain("Authentication:"); |
| 109 | - expect(result?.output).toContain("Authenticated via environment variable"); | |
| 133 | + expect(result?.output).toContain( | |
| 134 | + "Authenticated via environment variable", | |
| 135 | + ); | |
| 110 | 136 | expect(result?.output).toContain("Configuration:"); |
| 111 | 137 | expect(result?.output).toContain("Config not found"); |
| 112 | 138 | expect(result?.exit).toBe(false); |
| 113 | 139 | }); |
| 114 | 140 | |
| 115 | 141 | it("should handle /info command when authenticated with user config", async () => { |
| 116 | - const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = await import("./auth/workos.js"); | |
| 142 | + const { isAuthenticated, loadAuthConfig, isAuthenticatedConfig } = | |
| 143 | + await import("./auth/workos.js"); | |
| 117 | 144 | const { services } = await import("./services/index.js"); |
| 118 | - | |
| 145 | + | |
| 119 | 146 | const mockAuthConfig: AuthenticatedConfig = { |
| 120 | 147 | userId: "test-user-id", |
| 121 | 148 | userEmail: "test@example.com", |
| 122 | 149 | accessToken: "test-token", |
| 123 | 150 | refreshToken: "test-refresh-token", |
| 124 | 151 | expiresAt: Date.now() + 3600000, |
| 125 | - organizationId: "test-org-id" | |
| 152 | + organizationId: "test-org-id", | |
| 126 | 153 | }; |
| 127 | 154 | |
| 128 | - (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(true); | |
| 129 | - (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue(mockAuthConfig); | |
| 155 | + ( | |
| 156 | + isAuthenticated as MockedFunction<typeof isAuthenticated> | |
| 157 | + ).mockReturnValue(true); | |
| 158 | + (loadAuthConfig as MockedFunction<typeof loadAuthConfig>).mockReturnValue( | |
| 159 | + mockAuthConfig, | |
| 160 | + ); | |
| 130 | 161 | (isAuthenticatedConfig as any).mockReturnValue(true); |
| 131 | - (services.config.getState as MockedFunction<typeof services.config.getState>).mockReturnValue({ | |
| 162 | + ( | |
| 163 | + services.config.getState as MockedFunction< | |
| 164 | + typeof services.config.getState | |
| 165 | + > | |
| 166 | + ).mockReturnValue({ | |
| 132 | 167 | config: null, |
| 133 | - configPath: "/custom/config.yaml" | |
| 168 | + configPath: "/custom/config.yaml", | |
| 134 | 169 | } as ConfigServiceState); |
| 135 | 170 | |
| 136 | 171 | const result = await handleSlashCommands("/info", mockAssistant); |
| @@ -147,9 +182,15 @@ | ||
| 147 | 182 | it("should handle config service errors gracefully", async () => { |
| 148 | 183 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 149 | 184 | const { services } = await import("./services/index.js"); |
| 150 | - | |
| 151 | - (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(false); | |
| 152 | - (services.config.getState as MockedFunction<typeof services.config.getState>).mockImplementation(() => { | |
| 185 | + | |
| 186 | + ( | |
| 187 | + isAuthenticated as MockedFunction<typeof isAuthenticated> | |
| 188 | + ).mockReturnValue(false); | |
| 189 | + ( | |
| 190 | + services.config.getState as MockedFunction< | |
| 191 | + typeof services.config.getState | |
| 192 | + > | |
| 193 | + ).mockImplementation(() => { | |
| 153 | 194 | throw new Error("Service not ready"); |
| 154 | 195 | }); |
| 155 | 196 | |
| @@ -163,14 +204,22 @@ | ||
| 163 | 204 | const { isAuthenticated } = await import("./auth/workos.js"); |
| 164 | 205 | const { services } = await import("./services/index.js"); |
| 165 | 206 | const { getSessionFilePath } = await import("./session.js"); |
| 166 | - | |
| 207 | + | |
| 167 | 208 | // Mock the session path for this specific test |
| 168 | - (getSessionFilePath as any).mockReturnValue("/test-home/.continue/cli-sessions/continue-cli-test-123.json"); | |
| 169 | - | |
| 170 | - (isAuthenticated as MockedFunction<typeof isAuthenticated>).mockReturnValue(false); | |
| 171 | - (services.config.getState as MockedFunction<typeof services.config.getState>).mockReturnValue({ | |
| 209 | + (getSessionFilePath as any).mockReturnValue( | |
| 210 | + "/test-home/.continue/cli-sessions/continue-cli-test-123.json", | |
| 211 | + ); | |
| 212 | + | |
| 213 | + ( | |
| 214 | + isAuthenticated as MockedFunction<typeof isAuthenticated> | |
| 215 | + ).mockReturnValue(false); | |
| 216 | + ( | |
| 217 | + services.config.getState as MockedFunction< | |
| 218 | + typeof services.config.getState | |
| 219 | + > | |
| 220 | + ).mockReturnValue({ | |
| 172 | 221 | config: null, |
| 173 | - configPath: undefined | |
| 222 | + configPath: undefined, | |
| 174 | 223 | } as ConfigServiceState); |
| 175 | 224 | |
| 176 | 225 | const result = await handleSlashCommands("/info", mockAssistant); |
| @@ -179,4 +228,4 @@ | ||
| 179 | 228 | expect(result?.output).toContain(".json"); |
| 180 | 229 | }); |
| 181 | 230 | }); |
| 182 | -}); | |
| No newline at the end of the file | ||
| 231 | +}); |
modified extensions/cli/src/streamChatResponse.autoCompaction.test.ts +40 −23
| @@ -72,17 +72,25 @@ | ||
| 72 | 72 | }); |
| 73 | 73 | |
| 74 | 74 | it("should perform auto-compaction when context limit is approaching", async () => { |
| 75 | - const { shouldAutoCompact, getAutoCompactMessage } = await import("./util/tokenizer.js"); | |
| 75 | + const { shouldAutoCompact, getAutoCompactMessage } = await import( | |
| 76 | + "./util/tokenizer.js" | |
| 77 | + ); | |
| 76 | 78 | const { compactChatHistory } = await import("./compaction.js"); |
| 77 | 79 | const { saveSession } = await import("./session.js"); |
| 78 | 80 | |
| 79 | 81 | vi.mocked(shouldAutoCompact).mockReturnValue(true); |
| 80 | 82 | vi.mocked(getAutoCompactMessage).mockReturnValue("Auto-compacting..."); |
| 81 | - | |
| 83 | + | |
| 82 | 84 | const mockCompactionResult = { |
| 83 | 85 | compactedHistory: [ |
| 84 | - { role: "system", content: "You are a helpful assistant." } as ChatCompletionMessageParam, | |
| 85 | - { role: "assistant", content: "[COMPACTED HISTORY]\nConversation summary..." } as ChatCompletionMessageParam, | |
| 86 | + { | |
| 87 | + role: "system", | |
| 88 | + content: "You are a helpful assistant.", | |
| 89 | + } as ChatCompletionMessageParam, | |
| 90 | + { | |
| 91 | + role: "assistant", | |
| 92 | + content: "[COMPACTED HISTORY]\nConversation summary...", | |
| 93 | + } as ChatCompletionMessageParam, | |
| 86 | 94 | ], |
| 87 | 95 | compactionIndex: 1, |
| 88 | 96 | compactionContent: "Conversation summary...", |
| @@ -112,10 +120,16 @@ | ||
| 112 | 120 | mockLlmApi, |
| 113 | 121 | expect.any(Object), |
| 114 | 122 | ); |
| 115 | - expect(saveSession).toHaveBeenCalledWith(mockCompactionResult.compactedHistory); | |
| 116 | - | |
| 117 | - expect(mockCallbacks.onSystemMessage).toHaveBeenCalledWith("Auto-compacting..."); | |
| 118 | - expect(mockCallbacks.onSystemMessage).toHaveBeenCalledWith("✓ Chat history auto-compacted successfully."); | |
| 123 | + expect(saveSession).toHaveBeenCalledWith( | |
| 124 | + mockCompactionResult.compactedHistory, | |
| 125 | + ); | |
| 126 | + | |
| 127 | + expect(mockCallbacks.onSystemMessage).toHaveBeenCalledWith( | |
| 128 | + "Auto-compacting...", | |
| 129 | + ); | |
| 130 | + expect(mockCallbacks.onSystemMessage).toHaveBeenCalledWith( | |
| 131 | + "✓ Chat history auto-compacted successfully.", | |
| 132 | + ); | |
| 119 | 133 | |
| 120 | 134 | expect(result).toEqual({ |
| 121 | 135 | chatHistory: mockCompactionResult.compactedHistory, |
| @@ -124,13 +138,17 @@ | ||
| 124 | 138 | }); |
| 125 | 139 | |
| 126 | 140 | it("should handle compaction errors gracefully", async () => { |
| 127 | - const { shouldAutoCompact, getAutoCompactMessage } = await import("./util/tokenizer.js"); | |
| 141 | + const { shouldAutoCompact, getAutoCompactMessage } = await import( | |
| 142 | + "./util/tokenizer.js" | |
| 143 | + ); | |
| 128 | 144 | const { compactChatHistory } = await import("./compaction.js"); |
| 129 | 145 | const { logger } = await import("./util/logger.js"); |
| 130 | 146 | |
| 131 | 147 | vi.mocked(shouldAutoCompact).mockReturnValue(true); |
| 132 | 148 | vi.mocked(getAutoCompactMessage).mockReturnValue("Auto-compacting..."); |
| 133 | - vi.mocked(compactChatHistory).mockRejectedValue(new Error("Compaction failed")); | |
| 149 | + vi.mocked(compactChatHistory).mockRejectedValue( | |
| 150 | + new Error("Compaction failed"), | |
| 151 | + ); | |
| 134 | 152 | |
| 135 | 153 | const mockCallbacks = { |
| 136 | 154 | onSystemMessage: vi.fn(), |
| @@ -161,14 +179,18 @@ | ||
| 161 | 179 | }); |
| 162 | 180 | |
| 163 | 181 | it("should not call system message callback in headless mode", async () => { |
| 164 | - const { shouldAutoCompact, getAutoCompactMessage } = await import("./util/tokenizer.js"); | |
| 182 | + const { shouldAutoCompact, getAutoCompactMessage } = await import( | |
| 183 | + "./util/tokenizer.js" | |
| 184 | + ); | |
| 165 | 185 | const { compactChatHistory } = await import("./compaction.js"); |
| 166 | 186 | |
| 167 | 187 | vi.mocked(shouldAutoCompact).mockReturnValue(true); |
| 168 | 188 | vi.mocked(getAutoCompactMessage).mockReturnValue("Auto-compacting..."); |
| 169 | - | |
| 189 | + | |
| 170 | 190 | const mockCompactionResult = { |
| 171 | - compactedHistory: [{ role: "system", content: "System" } as ChatCompletionMessageParam], | |
| 191 | + compactedHistory: [ | |
| 192 | + { role: "system", content: "System" } as ChatCompletionMessageParam, | |
| 193 | + ], | |
| 172 | 194 | compactionIndex: 0, |
| 173 | 195 | compactionContent: "Summary", |
| 174 | 196 | }; |
| @@ -179,15 +201,10 @@ | ||
| 179 | 201 | onSystemMessage: vi.fn(), |
| 180 | 202 | }; |
| 181 | 203 | |
| 182 | - await handleAutoCompaction( | |
| 183 | - mockChatHistory, | |
| 184 | - mockModel, | |
| 185 | - mockLlmApi, | |
| 186 | - { | |
| 187 | - isHeadless: true, | |
| 188 | - callbacks: mockCallbacks, | |
| 189 | - }, | |
| 190 | - ); | |
| 204 | + await handleAutoCompaction(mockChatHistory, mockModel, mockLlmApi, { | |
| 205 | + isHeadless: true, | |
| 206 | + callbacks: mockCallbacks, | |
| 207 | + }); | |
| 191 | 208 | |
| 192 | 209 | expect(mockCallbacks.onSystemMessage).not.toHaveBeenCalled(); |
| 193 | 210 | }); |
| @@ -205,4 +222,4 @@ | ||
| 205 | 222 | compactionIndex: null, |
| 206 | 223 | }); |
| 207 | 224 | }); |
| 208 | -}); | |
| No newline at the end of the file | ||
| 225 | +}); |
modified extensions/cli/src/streamChatResponse.autoCompaction.ts +19 −15
| @@ -8,21 +8,20 @@ | ||
| 8 | 8 | import { DisplayMessage } from "./ui/types.js"; |
| 9 | 9 | import { formatError } from "./util/formatError.js"; |
| 10 | 10 | import { logger } from "./util/logger.js"; |
| 11 | -import { | |
| 12 | - getAutoCompactMessage, | |
| 13 | - shouldAutoCompact, | |
| 14 | -} from "./util/tokenizer.js"; | |
| 11 | +import { getAutoCompactMessage, shouldAutoCompact } from "./util/tokenizer.js"; | |
| 15 | 12 | |
| 16 | 13 | interface AutoCompactionCallbacks { |
| 17 | 14 | // For streaming mode |
| 18 | 15 | onSystemMessage?: (message: string) => void; |
| 19 | 16 | onContent?: (content: string) => void; |
| 20 | - | |
| 17 | + | |
| 21 | 18 | // For TUI mode |
| 22 | 19 | setMessages?: React.Dispatch<React.SetStateAction<DisplayMessage[]>>; |
| 23 | - setChatHistory?: React.Dispatch<React.SetStateAction<ChatCompletionMessageParam[]>>; | |
| 20 | + setChatHistory?: React.Dispatch< | |
| 21 | + React.SetStateAction<ChatCompletionMessageParam[]> | |
| 22 | + >; | |
| 24 | 23 | setCompactionIndex?: React.Dispatch<React.SetStateAction<number | null>>; |
| 25 | - | |
| 24 | + | |
| 26 | 25 | // For headless mode - no callbacks needed, just return values |
| 27 | 26 | } |
| 28 | 27 | |
| @@ -41,7 +40,7 @@ | ||
| 41 | 40 | callbacks?: AutoCompactionCallbacks, |
| 42 | 41 | ) { |
| 43 | 42 | if (isHeadless) return; |
| 44 | - | |
| 43 | + | |
| 45 | 44 | if (callbacks?.onSystemMessage) { |
| 46 | 45 | callbacks.onSystemMessage(message); |
| 47 | 46 | } else if (callbacks?.setMessages) { |
| @@ -65,12 +64,16 @@ | ||
| 65 | 64 | callbacks?: AutoCompactionCallbacks, |
| 66 | 65 | ) { |
| 67 | 66 | if (isHeadless) return; |
| 68 | - | |
| 67 | + | |
| 69 | 68 | const successMessage = "✓ Chat history auto-compacted successfully."; |
| 70 | - | |
| 69 | + | |
| 71 | 70 | if (callbacks?.onSystemMessage) { |
| 72 | 71 | callbacks.onSystemMessage(successMessage); |
| 73 | - } else if (callbacks?.setMessages && callbacks?.setChatHistory && callbacks?.setCompactionIndex) { | |
| 72 | + } else if ( | |
| 73 | + callbacks?.setMessages && | |
| 74 | + callbacks?.setChatHistory && | |
| 75 | + callbacks?.setCompactionIndex | |
| 76 | + ) { | |
| 74 | 77 | callbacks.setChatHistory(result.compactedHistory); |
| 75 | 78 | callbacks.setCompactionIndex(result.compactionIndex); |
| 76 | 79 | callbacks.setMessages((prev) => [ |
| @@ -96,9 +99,9 @@ | ||
| 96 | 99 | logger.error(errorMessage); |
| 97 | 100 | |
| 98 | 101 | if (isHeadless) return; |
| 99 | - | |
| 102 | + | |
| 100 | 103 | const warningMessage = `Warning: ${errorMessage}. Continuing without compaction...`; |
| 101 | - | |
| 104 | + | |
| 102 | 105 | if (callbacks?.onSystemMessage) { |
| 103 | 106 | callbacks.onSystemMessage(warningMessage); |
| 104 | 107 | } else if (callbacks?.setMessages) { |
| @@ -136,7 +139,9 @@ | ||
| 136 | 139 | return { chatHistory, compactionIndex: null }; |
| 137 | 140 | } |
| 138 | 141 | |
| 139 | - logger.info(`Auto-compaction triggered${isHeadless ? ' in headless mode' : ''}`); | |
| 142 | + logger.info( | |
| 143 | + `Auto-compaction triggered${isHeadless ? " in headless mode" : ""}`, | |
| 144 | + ); | |
| 140 | 145 | |
| 141 | 146 | // Notify about compaction start |
| 142 | 147 | notifyCompactionStart(getAutoCompactMessage(model), isHeadless, callbacks); |
| @@ -173,4 +178,3 @@ | ||
| 173 | 178 | return { chatHistory, compactionIndex: null }; |
| 174 | 179 | } |
| 175 | 180 | } |
| 176 | - |
modified extensions/cli/src/tools/readFile.ts +4 −4
| @@ -39,13 +39,13 @@ | ||
| 39 | 39 | const content = fs.readFileSync(args.filepath, "utf-8"); |
| 40 | 40 | // Mark this file as read for the edit tool |
| 41 | 41 | markFileAsRead(args.filepath); |
| 42 | - | |
| 43 | - const lines = content.split('\n'); | |
| 42 | + | |
| 43 | + const lines = content.split("\n"); | |
| 44 | 44 | if (lines.length > 5000) { |
| 45 | - const truncatedContent = lines.slice(0, 5000).join('\n'); | |
| 45 | + const truncatedContent = lines.slice(0, 5000).join("\n"); | |
| 46 | 46 | return `Content of ${args.filepath} (truncated to first 5000 lines of ${lines.length} total):\n${truncatedContent}`; |
| 47 | 47 | } |
| 48 | - | |
| 48 | + | |
| 49 | 49 | return `Content of ${args.filepath}:\n${content}`; |
| 50 | 50 | } catch (error) { |
| 51 | 51 | return `Error reading file: ${ |