Elffuss BraidHub

kikocis/continue public

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