| 1 | import { ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | |
| 4 | import { ContinueConfig, ILLMLogger } from "../.."; |
| 5 | import { llmsFromModelConfig } from "./models"; |
| 6 | |
| 7 | // Mock the LLM classes |
| 8 | vi.mock("../../llm/llms", () => ({ |
| 9 | LLMClasses: [ |
| 10 | class MockOpenAI { |
| 11 | static providerName = "openai"; |
| 12 | static defaultOptions = { |
| 13 | completionOptions: { |
| 14 | maxTokens: 2048, |
| 15 | }, |
| 16 | }; |
| 17 | |
| 18 | constructor(options: any) { |
| 19 | Object.assign(this, options); |
| 20 | } |
| 21 | |
| 22 | async listModels() { |
| 23 | return ["gpt-4", "gpt-3.5-turbo"]; |
| 24 | } |
| 25 | }, |
| 26 | class MockAnthropic { |
| 27 | static providerName = "anthropic"; |
| 28 | static defaultOptions = { |
| 29 | completionOptions: { |
| 30 | maxTokens: 4096, |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | constructor(options: any) { |
| 35 | Object.assign(this, options); |
| 36 | } |
| 37 | |
| 38 | async listModels() { |
| 39 | return ["claude-3-5-sonnet-20241022", "claude-3-haiku-20240307"]; |
| 40 | } |
| 41 | }, |
| 42 | ], |
| 43 | })); |
| 44 | |
| 45 | describe("llmsFromModelConfig requestOptions merging", () => { |
| 46 | let mockLLMLogger: ILLMLogger; |
| 47 | let mockConfig: ContinueConfig; |
| 48 | |
| 49 | beforeEach(() => { |
| 50 | mockLLMLogger = { log: vi.fn() } as any; |
| 51 | mockConfig = { |
| 52 | requestOptions: { |
| 53 | timeout: 30000, |
| 54 | headers: { |
| 55 | "User-Agent": "Continue/1.0.0", |
| 56 | }, |
| 57 | proxy: "global-proxy", |
| 58 | }, |
| 59 | contextProviders: [], |
| 60 | mcpServerStatuses: [], |
| 61 | rules: [], |
| 62 | selectedModelByRole: { |
| 63 | apply: null, |
| 64 | autocomplete: null, |
| 65 | chat: null, |
| 66 | edit: null, |
| 67 | embed: null, |
| 68 | rerank: null, |
| 69 | summarize: null, |
| 70 | }, |
| 71 | modelsByRole: { |
| 72 | apply: [], |
| 73 | autocomplete: [], |
| 74 | chat: [], |
| 75 | edit: [], |
| 76 | embed: [], |
| 77 | rerank: [], |
| 78 | summarize: [], |
| 79 | }, |
| 80 | slashCommands: [], |
| 81 | tools: [], |
| 82 | allowAnonymousTelemetry: false, |
| 83 | } as ContinueConfig; |
| 84 | vi.clearAllMocks(); |
| 85 | }); |
| 86 | |
| 87 | it("should merge model requestOptions with config requestOptions", async () => { |
| 88 | const model: ModelConfig = { |
| 89 | name: "test-openai", |
| 90 | provider: "openai", |
| 91 | model: "gpt-4", |
| 92 | requestOptions: { |
| 93 | timeout: 60000, |
| 94 | headers: { |
| 95 | Authorization: "Bearer token123", |
| 96 | }, |
| 97 | proxy: "model-proxy", |
| 98 | }, |
| 99 | }; |
| 100 | |
| 101 | const result = await llmsFromModelConfig({ |
| 102 | model, |
| 103 | uniqueId: "test-id", |
| 104 | llmLogger: mockLLMLogger, |
| 105 | config: mockConfig, |
| 106 | }); |
| 107 | |
| 108 | expect(result).toHaveLength(1); |
| 109 | const llm = result[0]; |
| 110 | |
| 111 | // The LLM should have merged requestOptions |
| 112 | expect(llm.requestOptions).toEqual({ |
| 113 | timeout: 60000, // model-specific takes precedence |
| 114 | headers: { |
| 115 | "User-Agent": "Continue/1.0.0", // from global request options |
| 116 | Authorization: "Bearer token123", // from model |
| 117 | }, |
| 118 | proxy: "model-proxy", // model-specific takes precedence |
| 119 | }); |
| 120 | }); |
| 121 | |
| 122 | it("should use config requestOptions when model has no requestOptions", async () => { |
| 123 | const model: ModelConfig = { |
| 124 | name: "test-openai", |
| 125 | provider: "openai", |
| 126 | model: "gpt-4", |
| 127 | // no requestOptions |
| 128 | }; |
| 129 | |
| 130 | const result = await llmsFromModelConfig({ |
| 131 | model, |
| 132 | uniqueId: "test-id", |
| 133 | llmLogger: mockLLMLogger, |
| 134 | config: mockConfig, |
| 135 | }); |
| 136 | |
| 137 | expect(result).toHaveLength(1); |
| 138 | const llm = result[0]; |
| 139 | |
| 140 | // The LLM should have config requestOptions |
| 141 | expect(llm.requestOptions).toEqual(mockConfig.requestOptions); |
| 142 | }); |
| 143 | |
| 144 | it("should use model requestOptions when config has no requestOptions", async () => { |
| 145 | const model: ModelConfig = { |
| 146 | name: "test-openai", |
| 147 | provider: "openai", |
| 148 | model: "gpt-4", |
| 149 | requestOptions: { |
| 150 | timeout: 45000, |
| 151 | headers: { |
| 152 | "API-Key": "secret", |
| 153 | }, |
| 154 | }, |
| 155 | }; |
| 156 | |
| 157 | const configWithoutOptions = { ...mockConfig }; |
| 158 | delete configWithoutOptions.requestOptions; |
| 159 | |
| 160 | const result = await llmsFromModelConfig({ |
| 161 | model, |
| 162 | uniqueId: "test-id", |
| 163 | llmLogger: mockLLMLogger, |
| 164 | config: configWithoutOptions, |
| 165 | }); |
| 166 | |
| 167 | expect(result).toHaveLength(1); |
| 168 | const llm = result[0]; |
| 169 | |
| 170 | // The LLM should have model requestOptions |
| 171 | expect(llm.requestOptions).toEqual(model.requestOptions); |
| 172 | }); |
| 173 | |
| 174 | it("should handle empty headers correctly in merge", async () => { |
| 175 | const model: ModelConfig = { |
| 176 | name: "test-openai", |
| 177 | provider: "openai", |
| 178 | model: "gpt-4", |
| 179 | requestOptions: { |
| 180 | timeout: 60000, |
| 181 | headers: {}, |
| 182 | }, |
| 183 | }; |
| 184 | |
| 185 | const configWithEmptyHeaders = { |
| 186 | ...mockConfig, |
| 187 | requestOptions: { |
| 188 | ...mockConfig.requestOptions!, |
| 189 | headers: {}, |
| 190 | }, |
| 191 | }; |
| 192 | |
| 193 | const result = await llmsFromModelConfig({ |
| 194 | model, |
| 195 | uniqueId: "test-id", |
| 196 | llmLogger: mockLLMLogger, |
| 197 | config: configWithEmptyHeaders, |
| 198 | }); |
| 199 | |
| 200 | expect(result).toHaveLength(1); |
| 201 | const llm = result[0]; |
| 202 | |
| 203 | // When headers merge to empty object, they should be undefined |
| 204 | expect(llm.requestOptions).toEqual({ |
| 205 | timeout: 60000, |
| 206 | proxy: "global-proxy", |
| 207 | headers: undefined, |
| 208 | }); |
| 209 | }); |
| 210 | |
| 211 | it("should handle autodetect models with merged requestOptions", async () => { |
| 212 | const model: ModelConfig = { |
| 213 | name: "autodetect-openai", |
| 214 | provider: "openai", |
| 215 | model: "AUTODETECT", |
| 216 | requestOptions: { |
| 217 | timeout: 120000, |
| 218 | headers: { |
| 219 | "X-Custom": "autodetect", |
| 220 | }, |
| 221 | }, |
| 222 | }; |
| 223 | |
| 224 | const result = await llmsFromModelConfig({ |
| 225 | model, |
| 226 | uniqueId: "test-id", |
| 227 | llmLogger: mockLLMLogger, |
| 228 | config: mockConfig, |
| 229 | }); |
| 230 | |
| 231 | // Should return multiple models from listModels() |
| 232 | expect(result.length).toBeGreaterThan(0); |
| 233 | |
| 234 | // Each detected model should have the merged requestOptions |
| 235 | result.forEach((llm) => { |
| 236 | expect(llm.requestOptions).toEqual({ |
| 237 | timeout: 120000, // model-specific takes precedence |
| 238 | headers: { |
| 239 | "User-Agent": "Continue/1.0.0", // from global request options |
| 240 | "X-Custom": "autodetect", // from model |
| 241 | }, |
| 242 | proxy: "global-proxy", // from global request options |
| 243 | }); |
| 244 | }); |
| 245 | }); |
| 246 | |
| 247 | it("should handle unknown provider gracefully", async () => { |
| 248 | const model: ModelConfig = { |
| 249 | name: "unknown-provider", |
| 250 | provider: "unknown", |
| 251 | model: "unknown-model", |
| 252 | requestOptions: { |
| 253 | timeout: 60000, |
| 254 | }, |
| 255 | }; |
| 256 | |
| 257 | const result = await llmsFromModelConfig({ |
| 258 | model, |
| 259 | uniqueId: "test-id", |
| 260 | llmLogger: mockLLMLogger, |
| 261 | config: mockConfig, |
| 262 | }); |
| 263 | |
| 264 | // Should return empty array for unknown provider |
| 265 | expect(result).toHaveLength(0); |
| 266 | }); |
| 267 | |
| 268 | it("should preserve other model properties while merging requestOptions", async () => { |
| 269 | const model: ModelConfig = { |
| 270 | name: "test-anthropic", |
| 271 | provider: "anthropic", |
| 272 | model: "claude-3-5-sonnet-20241022", |
| 273 | roles: ["chat", "summarize"], |
| 274 | capabilities: ["tool_use", "image_input"], |
| 275 | defaultCompletionOptions: { |
| 276 | temperature: 0.7, |
| 277 | maxTokens: 1000, |
| 278 | }, |
| 279 | requestOptions: { |
| 280 | timeout: 90000, |
| 281 | }, |
| 282 | }; |
| 283 | |
| 284 | const result = await llmsFromModelConfig({ |
| 285 | model, |
| 286 | uniqueId: "test-id", |
| 287 | llmLogger: mockLLMLogger, |
| 288 | config: mockConfig, |
| 289 | }); |
| 290 | |
| 291 | expect(result).toHaveLength(1); |
| 292 | const llm = result[0]; |
| 293 | |
| 294 | // Should preserve model properties |
| 295 | expect(llm.title).toBe("test-anthropic"); |
| 296 | expect(llm.completionOptions?.temperature).toBe(0.7); |
| 297 | expect(llm.completionOptions?.maxTokens).toBe(1000); |
| 298 | expect(llm.capabilities?.tools).toBe(true); |
| 299 | expect(llm.capabilities?.uploadImage).toBe(true); |
| 300 | |
| 301 | // Should have merged requestOptions |
| 302 | expect(llm.requestOptions).toEqual({ |
| 303 | timeout: 90000, |
| 304 | headers: { |
| 305 | "User-Agent": "Continue/1.0.0", |
| 306 | }, |
| 307 | proxy: "global-proxy", |
| 308 | }); |
| 309 | }); |
| 310 | |
| 311 | it("should handle complex header merging scenarios", async () => { |
| 312 | const model: ModelConfig = { |
| 313 | name: "complex-headers", |
| 314 | provider: "openai", |
| 315 | model: "gpt-4", |
| 316 | requestOptions: { |
| 317 | headers: { |
| 318 | Authorization: "Bearer model-token", |
| 319 | Accept: "application/json", // Should override config Accept |
| 320 | "X-Model-Specific": "true", |
| 321 | }, |
| 322 | }, |
| 323 | }; |
| 324 | |
| 325 | const configWithMoreHeaders = { |
| 326 | ...mockConfig, |
| 327 | requestOptions: { |
| 328 | ...mockConfig.requestOptions!, |
| 329 | headers: { |
| 330 | ...mockConfig.requestOptions!.headers, |
| 331 | Accept: "text/plain", // Should be overridden by model |
| 332 | "Cache-Control": "no-cache", |
| 333 | }, |
| 334 | }, |
| 335 | }; |
| 336 | |
| 337 | const result = await llmsFromModelConfig({ |
| 338 | model, |
| 339 | uniqueId: "test-id", |
| 340 | llmLogger: mockLLMLogger, |
| 341 | config: configWithMoreHeaders, |
| 342 | }); |
| 343 | |
| 344 | expect(result).toHaveLength(1); |
| 345 | const llm = result[0]; |
| 346 | |
| 347 | expect(llm.requestOptions?.headers).toEqual({ |
| 348 | "User-Agent": "Continue/1.0.0", // from global request options |
| 349 | "Cache-Control": "no-cache", // from global request options |
| 350 | Authorization: "Bearer model-token", // from model |
| 351 | Accept: "application/json", // from model (overrides config) |
| 352 | "X-Model-Specific": "true", // from model |
| 353 | }); |
| 354 | }); |
| 355 | }); |