| 1 | import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import * as openaiAdapters from "@continuedev/openai-adapters"; |
| 3 | import { describe, expect, beforeEach, vi, test } from "vitest"; |
| 4 | |
| 5 | // Mock dependencies before imports |
| 6 | vi.mock("@continuedev/openai-adapters"); |
| 7 | vi.mock("../config.js"); |
| 8 | vi.mock("../auth/workos.js"); |
| 9 | |
| 10 | import { AuthConfig } from "../auth/workos.js"; |
| 11 | import * as workos from "../auth/workos.js"; |
| 12 | import * as config from "../config.js"; |
| 13 | |
| 14 | import { ModelService } from "./ModelService.js"; |
| 15 | |
| 16 | describe("ModelService", () => { |
| 17 | let service: ModelService; |
| 18 | let mockAssistant: AssistantUnrolled; |
| 19 | let mockAuthConfig: AuthConfig; |
| 20 | const mockLlmApi = { complete: vi.fn(), stream: vi.fn() }; |
| 21 | |
| 22 | beforeEach(() => { |
| 23 | vi.clearAllMocks(); |
| 24 | service = new ModelService(); |
| 25 | |
| 26 | mockAssistant = { |
| 27 | name: "test-assistant", |
| 28 | version: "1.0.0", |
| 29 | models: [ |
| 30 | { |
| 31 | provider: "openai", |
| 32 | model: "gpt-4", |
| 33 | name: "GPT-4", |
| 34 | apiKey: "test-key", |
| 35 | roles: ["chat"], |
| 36 | } as ModelConfig, |
| 37 | { |
| 38 | provider: "anthropic", |
| 39 | model: "claude-3", |
| 40 | name: "Claude 3", |
| 41 | apiKey: "test-key", |
| 42 | roles: ["chat"], |
| 43 | } as ModelConfig, |
| 44 | { |
| 45 | provider: "openai", |
| 46 | model: "gpt-3.5", |
| 47 | name: "GPT-3.5", |
| 48 | apiKey: "test-key", |
| 49 | roles: ["embed"], |
| 50 | } as ModelConfig, |
| 51 | ], |
| 52 | } as AssistantUnrolled; |
| 53 | |
| 54 | mockAuthConfig = { |
| 55 | accessToken: "test-token", |
| 56 | refreshToken: "test-refresh", |
| 57 | userEmail: "test@example.com", |
| 58 | userId: "test-user", |
| 59 | organizationId: "test-org", |
| 60 | expiresAt: Date.now() + 3600000, // 1 hour from now |
| 61 | }; |
| 62 | }); |
| 63 | |
| 64 | describe("State Management", () => { |
| 65 | test("should initialize with default model", async () => { |
| 66 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 67 | mockLlmApi as any, |
| 68 | mockAssistant.models![0] as ModelConfig, |
| 69 | ]); |
| 70 | vi.mocked(workos.getModelName).mockReturnValue(null); |
| 71 | |
| 72 | const state = await service.initialize(mockAssistant, mockAuthConfig); |
| 73 | |
| 74 | expect(state).toEqual({ |
| 75 | llmApi: mockLlmApi, |
| 76 | model: mockAssistant.models![0], |
| 77 | }); |
| 78 | }); |
| 79 | |
| 80 | test("should initialize with persisted model if valid", async () => { |
| 81 | vi.mocked(workos.getModelName).mockReturnValue("Claude 3"); |
| 82 | vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue( |
| 83 | mockLlmApi as any, |
| 84 | ); |
| 85 | |
| 86 | const state = await service.initialize(mockAssistant, mockAuthConfig); |
| 87 | |
| 88 | expect(state.model?.name).toBe("Claude 3"); |
| 89 | expect(state.llmApi).toStrictEqual(mockLlmApi); |
| 90 | }); |
| 91 | |
| 92 | test("should fall back to default if persisted model not found", async () => { |
| 93 | vi.mocked(workos.getModelName).mockReturnValue("Non-existent Model"); |
| 94 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 95 | mockLlmApi as any, |
| 96 | mockAssistant.models![0] as ModelConfig, |
| 97 | ]); |
| 98 | |
| 99 | const state = await service.initialize(mockAssistant, mockAuthConfig); |
| 100 | |
| 101 | expect(state.model).toBe(mockAssistant.models![0]); |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | describe("update()", () => { |
| 106 | test("should update model with new assistant config", async () => { |
| 107 | // Initialize first |
| 108 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 109 | mockLlmApi as any, |
| 110 | mockAssistant.models![0] as ModelConfig, |
| 111 | ]); |
| 112 | await service.initialize(mockAssistant, mockAuthConfig); |
| 113 | |
| 114 | // Update with new assistant |
| 115 | const newAssistant = { |
| 116 | ...mockAssistant, |
| 117 | models: [ |
| 118 | { |
| 119 | provider: "openai", |
| 120 | model: "gpt-4-turbo", |
| 121 | name: "GPT-4 Turbo", |
| 122 | apiKey: "new-key", |
| 123 | roles: ["chat"], |
| 124 | } as ModelConfig, |
| 125 | ], |
| 126 | }; |
| 127 | const newLlmApi = { complete: vi.fn(), stream: vi.fn() }; |
| 128 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 129 | newLlmApi as any, |
| 130 | newAssistant.models![0] as ModelConfig, |
| 131 | ]); |
| 132 | |
| 133 | const state = await service.update(newAssistant, mockAuthConfig); |
| 134 | |
| 135 | expect(state).toEqual({ |
| 136 | llmApi: newLlmApi, |
| 137 | model: newAssistant.models![0], |
| 138 | }); |
| 139 | }); |
| 140 | }); |
| 141 | |
| 142 | describe("switchModel()", () => { |
| 143 | test("should switch to a different model by index", async () => { |
| 144 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 145 | mockLlmApi as any, |
| 146 | mockAssistant.models![0] as ModelConfig, |
| 147 | ]); |
| 148 | await service.initialize(mockAssistant, mockAuthConfig); |
| 149 | |
| 150 | const newLlmApi = { complete: vi.fn(), stream: vi.fn() }; |
| 151 | vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue( |
| 152 | newLlmApi as any, |
| 153 | ); |
| 154 | |
| 155 | const state = await service.switchModel(1); |
| 156 | |
| 157 | expect(state).toEqual({ |
| 158 | llmApi: newLlmApi, |
| 159 | model: mockAssistant.models![1], |
| 160 | }); |
| 161 | expect(service.getState()).toEqual(state); |
| 162 | }); |
| 163 | |
| 164 | test("should throw error for invalid model index", async () => { |
| 165 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 166 | mockLlmApi as any, |
| 167 | mockAssistant.models![0] as ModelConfig, |
| 168 | ]); |
| 169 | await service.initialize(mockAssistant, mockAuthConfig); |
| 170 | |
| 171 | await expect(service.switchModel(5)).rejects.toThrow( |
| 172 | "Invalid model index: 5. Available models: 0-1", |
| 173 | ); |
| 174 | await expect(service.switchModel(-1)).rejects.toThrow( |
| 175 | "Invalid model index: -1. Available models: 0-1", |
| 176 | ); |
| 177 | }); |
| 178 | |
| 179 | test("should throw error when service not initialized", async () => { |
| 180 | await expect(service.switchModel(0)).rejects.toThrow( |
| 181 | "ModelService not initialized", |
| 182 | ); |
| 183 | }); |
| 184 | |
| 185 | test("should handle continue-proxy provider specially", async () => { |
| 186 | const proxyModel = { |
| 187 | provider: "continue-proxy", |
| 188 | name: "Proxy Model", |
| 189 | apiBase: "https://proxy.continue.dev", |
| 190 | apiKeyLocation: "env.PROXY_KEY", |
| 191 | roles: ["chat"], |
| 192 | } as ModelConfig; |
| 193 | |
| 194 | mockAssistant.models = [proxyModel]; |
| 195 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 196 | mockLlmApi as any, |
| 197 | proxyModel, |
| 198 | ]); |
| 199 | await service.initialize(mockAssistant, mockAuthConfig); |
| 200 | |
| 201 | vi.mocked(workos.getAccessToken).mockReturnValue("proxy-token"); |
| 202 | vi.mocked(workos.getOrganizationId).mockReturnValue("org-123"); |
| 203 | vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue( |
| 204 | mockLlmApi as any, |
| 205 | ); |
| 206 | |
| 207 | await service.switchModel(0); |
| 208 | |
| 209 | expect(vi.mocked(openaiAdapters.constructLlmApi)).toHaveBeenCalledWith({ |
| 210 | provider: "continue-proxy", |
| 211 | apiBase: "https://proxy.continue.dev", |
| 212 | apiKey: "proxy-token", |
| 213 | env: { |
| 214 | apiKeyLocation: "env.PROXY_KEY", |
| 215 | orgScopeId: "org-123", |
| 216 | proxyUrl: undefined, |
| 217 | }, |
| 218 | }); |
| 219 | }); |
| 220 | }); |
| 221 | |
| 222 | describe("getAvailableChatModels()", () => { |
| 223 | test("should return only models with chat role", async () => { |
| 224 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 225 | mockLlmApi as any, |
| 226 | mockAssistant.models![0] as ModelConfig, |
| 227 | ]); |
| 228 | await service.initialize(mockAssistant, mockAuthConfig); |
| 229 | |
| 230 | const models = service.getAvailableChatModels(); |
| 231 | |
| 232 | expect(models).toHaveLength(2); |
| 233 | expect(models[0]).toEqual({ |
| 234 | provider: "openai", |
| 235 | name: "GPT-4", |
| 236 | index: 0, |
| 237 | }); |
| 238 | expect(models[1]).toEqual({ |
| 239 | provider: "anthropic", |
| 240 | name: "Claude 3", |
| 241 | index: 1, |
| 242 | }); |
| 243 | }); |
| 244 | |
| 245 | test("should include models with undefined roles", async () => { |
| 246 | // Add a model with undefined roles to the assistant |
| 247 | const assistantWithUndefinedRoles = { |
| 248 | ...mockAssistant, |
| 249 | models: [ |
| 250 | ...mockAssistant.models!, |
| 251 | { |
| 252 | provider: "local", |
| 253 | model: "llama-2", |
| 254 | name: "Llama 2", |
| 255 | apiKey: "test-key", |
| 256 | // roles is undefined |
| 257 | } as ModelConfig, |
| 258 | ], |
| 259 | }; |
| 260 | |
| 261 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 262 | mockLlmApi as any, |
| 263 | assistantWithUndefinedRoles.models![0] as ModelConfig, |
| 264 | ]); |
| 265 | await service.initialize(assistantWithUndefinedRoles, mockAuthConfig); |
| 266 | |
| 267 | const models = service.getAvailableChatModels(); |
| 268 | |
| 269 | // Should include the two chat models and the model with undefined roles |
| 270 | expect(models).toHaveLength(3); |
| 271 | expect(models[2]).toEqual({ |
| 272 | provider: "local", |
| 273 | name: "Llama 2", |
| 274 | index: 2, |
| 275 | }); |
| 276 | }); |
| 277 | }); |
| 278 | |
| 279 | describe("getCurrentModelIndex()", () => { |
| 280 | test("should return correct index of current model", async () => { |
| 281 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 282 | mockLlmApi as any, |
| 283 | mockAssistant.models![0] as ModelConfig, |
| 284 | ]); |
| 285 | await service.initialize(mockAssistant, mockAuthConfig); |
| 286 | |
| 287 | expect(service.getCurrentModelIndex()).toBe(0); |
| 288 | |
| 289 | vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue( |
| 290 | mockLlmApi as any, |
| 291 | ); |
| 292 | await service.switchModel(1); |
| 293 | expect(service.getCurrentModelIndex()).toBe(1); |
| 294 | }); |
| 295 | |
| 296 | test("should return -1 when no model is set", () => { |
| 297 | expect(service.getCurrentModelIndex()).toBe(-1); |
| 298 | }); |
| 299 | }); |
| 300 | |
| 301 | describe("getModelIndexByName()", () => { |
| 302 | test("should find model by name", async () => { |
| 303 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 304 | mockLlmApi as any, |
| 305 | mockAssistant.models![0] as ModelConfig, |
| 306 | ]); |
| 307 | await service.initialize(mockAssistant, mockAuthConfig); |
| 308 | |
| 309 | expect(service.getModelIndexByName("GPT-4")).toBe(0); |
| 310 | expect(service.getModelIndexByName("Claude 3")).toBe(1); |
| 311 | expect(service.getModelIndexByName("Non-existent")).toBe(-1); |
| 312 | }); |
| 313 | |
| 314 | test("should find model by name and provider", async () => { |
| 315 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 316 | mockLlmApi as any, |
| 317 | mockAssistant.models![0] as ModelConfig, |
| 318 | ]); |
| 319 | await service.initialize(mockAssistant, mockAuthConfig); |
| 320 | |
| 321 | expect(service.getModelIndexByName("GPT-4", "openai")).toBe(0); |
| 322 | expect(service.getModelIndexByName("GPT-4", "anthropic")).toBe(-1); |
| 323 | }); |
| 324 | }); |
| 325 | |
| 326 | describe("getModelInfo()", () => { |
| 327 | test("should return current model info", async () => { |
| 328 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 329 | mockLlmApi as any, |
| 330 | mockAssistant.models![0] as ModelConfig, |
| 331 | ]); |
| 332 | await service.initialize(mockAssistant, mockAuthConfig); |
| 333 | |
| 334 | expect(service.getModelInfo()).toEqual({ |
| 335 | provider: "openai", |
| 336 | name: "GPT-4", |
| 337 | }); |
| 338 | }); |
| 339 | |
| 340 | test("should return null when no model set", () => { |
| 341 | expect(service.getModelInfo()).toBeNull(); |
| 342 | }); |
| 343 | }); |
| 344 | |
| 345 | describe("isReady()", () => { |
| 346 | test("should return false when not initialized", () => { |
| 347 | expect(service.isReady()).toBe(false); |
| 348 | }); |
| 349 | |
| 350 | test("should return true when initialized with model and llmApi", async () => { |
| 351 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 352 | mockLlmApi as any, |
| 353 | mockAssistant.models![0] as ModelConfig, |
| 354 | ]); |
| 355 | await service.initialize(mockAssistant, mockAuthConfig); |
| 356 | |
| 357 | expect(service.isReady()).toBe(true); |
| 358 | }); |
| 359 | |
| 360 | test("should return false if llmApi is null", async () => { |
| 361 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 362 | null as any, |
| 363 | mockAssistant.models![0] as ModelConfig, |
| 364 | ]); |
| 365 | await service.initialize(mockAssistant, mockAuthConfig); |
| 366 | |
| 367 | expect(service.isReady()).toBe(false); |
| 368 | }); |
| 369 | }); |
| 370 | |
| 371 | describe("getDependencies()", () => { |
| 372 | test("should declare auth and config dependencies", () => { |
| 373 | expect(service.getDependencies()).toEqual(["auth", "config"]); |
| 374 | }); |
| 375 | }); |
| 376 | |
| 377 | describe("Event Emission", () => { |
| 378 | test("should emit stateChanged on model switch", async () => { |
| 379 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 380 | mockLlmApi as any, |
| 381 | mockAssistant.models![0] as ModelConfig, |
| 382 | ]); |
| 383 | await service.initialize(mockAssistant, mockAuthConfig); |
| 384 | |
| 385 | const listener = vi.fn(); |
| 386 | service.on("stateChanged", listener); |
| 387 | |
| 388 | const newLlmApi = { complete: vi.fn(), stream: vi.fn() }; |
| 389 | vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue( |
| 390 | newLlmApi as any, |
| 391 | ); |
| 392 | await service.switchModel(1); |
| 393 | |
| 394 | expect(listener).toHaveBeenCalledWith( |
| 395 | expect.objectContaining({ |
| 396 | model: mockAssistant.models![1], |
| 397 | llmApi: newLlmApi, |
| 398 | }), |
| 399 | expect.objectContaining({ |
| 400 | model: mockAssistant.models![0], |
| 401 | llmApi: mockLlmApi, |
| 402 | }), |
| 403 | ); |
| 404 | }); |
| 405 | |
| 406 | test("should emit error on switch failure", async () => { |
| 407 | vi.mocked(config.getLlmApi).mockReturnValue([ |
| 408 | mockLlmApi as any, |
| 409 | mockAssistant.models![0] as ModelConfig, |
| 410 | ]); |
| 411 | await service.initialize(mockAssistant, mockAuthConfig); |
| 412 | |
| 413 | const errorListener = vi.fn(); |
| 414 | service.on("error", errorListener); |
| 415 | |
| 416 | vi.mocked(openaiAdapters.constructLlmApi).mockReturnValue(null as any); |
| 417 | |
| 418 | await expect(service.switchModel(1)).rejects.toThrow( |
| 419 | "Failed to initialize LLM", |
| 420 | ); |
| 421 | expect(errorListener).toHaveBeenCalledWith(expect.any(Error)); |
| 422 | }); |
| 423 | }); |
| 424 | }); |