Changesets · Branches · Tags · Compare
| Changeset | b3a6a97ce7a5857026c5bdeba0e4b73de1eec0a0548676a451f1a8dae9b56727 |
|---|---|
| Author | Nate <sestinj@gmail.com> · 2025-08-24 17:52 -07:00 |
| Committer | Nate <sestinj@gmail.com> · 2025-08-24 17:52 -07:00 |
| Parents | fc89067dc2f1 |
21 files changed
modified extensions/cli/src/args-mcp.test.ts +4 −10
| @@ -38,13 +38,7 @@ | ||
| 38 | 38 | }); |
| 39 | 39 | |
| 40 | 40 | it("should handle MCP flag with prompt", () => { |
| 41 | - process.argv = [ | |
| 42 | - "node", | |
| 43 | - "test", | |
| 44 | - "--mcp", | |
| 45 | - "my-org/my-mcp", | |
| 46 | - "hello world" | |
| 47 | - ]; | |
| 41 | + process.argv = ["node", "test", "--mcp", "my-org/my-mcp", "hello world"]; | |
| 48 | 42 | const result = parseArgs(); |
| 49 | 43 | expect(result.mcps).toEqual(["my-org/my-mcp"]); |
| 50 | 44 | expect(result.prompt).toBe("hello world"); |
| @@ -57,8 +51,8 @@ | ||
| 57 | 51 | "actual prompt", |
| 58 | 52 | "--rule", |
| 59 | 53 | "my-rule", |
| 60 | - "--mcp", | |
| 61 | - "my-org/my-mcp" | |
| 54 | + "--mcp", | |
| 55 | + "my-org/my-mcp", | |
| 62 | 56 | ]; |
| 63 | 57 | const result = parseArgs(); |
| 64 | 58 | expect(result.prompt).toBe("actual prompt"); |
| @@ -78,4 +72,4 @@ | ||
| 78 | 72 | expect(result.prompt).toBe("my prompt"); |
| 79 | 73 | expect(result.mcps).toEqual([]); |
| 80 | 74 | }); |
| 81 | -}); | |
| No newline at the end of the file | ||
| 75 | +}); |
modified extensions/cli/src/args.ts +15 −4
| @@ -37,7 +37,10 @@ | ||
| 37 | 37 | /** |
| 38 | 38 | * Extract value for a single-value flag |
| 39 | 39 | */ |
| 40 | -function extractSingleFlag(args: string[], flagName: string): string | undefined { | |
| 40 | +function extractSingleFlag( | |
| 41 | + args: string[], | |
| 42 | + flagName: string, | |
| 43 | +): string | undefined { | |
| 41 | 44 | const index = args.indexOf(flagName); |
| 42 | 45 | if (index !== -1 && index + 1 < args.length) { |
| 43 | 46 | return args[index + 1]; |
| @@ -69,10 +72,10 @@ | ||
| 69 | 72 | } |
| 70 | 73 | |
| 71 | 74 | result.configPath = extractSingleFlag(args, "--config"); |
| 72 | - | |
| 75 | + | |
| 73 | 76 | const orgValue = extractSingleFlag(args, "--org"); |
| 74 | 77 | if (orgValue) { |
| 75 | - result.organizationSlug = | |
| 78 | + result.organizationSlug = | |
| 76 | 79 | orgValue.toLowerCase() === "personal" ? undefined : orgValue; |
| 77 | 80 | } |
| 78 | 81 | |
| @@ -107,7 +110,15 @@ | ||
| 107 | 110 | } |
| 108 | 111 | |
| 109 | 112 | // Extract prompt from non-flag arguments |
| 110 | - const flagsWithValues = ["--config", "--org", "--rule", "--mcp", "--model", "--prompt", "--format"]; | |
| 113 | + const flagsWithValues = [ | |
| 114 | + "--config", | |
| 115 | + "--org", | |
| 116 | + "--rule", | |
| 117 | + "--mcp", | |
| 118 | + "--model", | |
| 119 | + "--prompt", | |
| 120 | + "--format", | |
| 121 | + ]; | |
| 111 | 122 | const nonFlagArgs = args.filter((arg, index) => { |
| 112 | 123 | if (arg.startsWith("--") || arg === "-p") return false; |
| 113 | 124 | const prevArg = index > 0 ? args[index - 1] : ""; |
modified extensions/cli/src/configEnhancer.test.ts +30 −10
| @@ -150,10 +150,26 @@ | ||
| 150 | 150 | |
| 151 | 151 | // Models from --model flag should be prepended (at the start) |
| 152 | 152 | expect(config.models).toHaveLength(4); |
| 153 | - expect(config.models?.[0]).toEqual({ name: "GPT-5", provider: "openai", model: "gpt-5" }); | |
| 154 | - expect(config.models?.[1]).toEqual({ name: "Claude-3", provider: "anthropic", model: "claude-3" }); | |
| 155 | - expect(config.models?.[2]).toEqual({ name: "GPT-4", provider: "openai", model: "gpt-4" }); | |
| 156 | - expect(config.models?.[3]).toEqual({ name: "Claude-2", provider: "anthropic", model: "claude-2" }); | |
| 153 | + expect(config.models?.[0]).toEqual({ | |
| 154 | + name: "GPT-5", | |
| 155 | + provider: "openai", | |
| 156 | + model: "gpt-5", | |
| 157 | + }); | |
| 158 | + expect(config.models?.[1]).toEqual({ | |
| 159 | + name: "Claude-3", | |
| 160 | + provider: "anthropic", | |
| 161 | + model: "claude-3", | |
| 162 | + }); | |
| 163 | + expect(config.models?.[2]).toEqual({ | |
| 164 | + name: "GPT-4", | |
| 165 | + provider: "openai", | |
| 166 | + model: "gpt-4", | |
| 167 | + }); | |
| 168 | + expect(config.models?.[3]).toEqual({ | |
| 169 | + name: "Claude-2", | |
| 170 | + provider: "anthropic", | |
| 171 | + model: "claude-2", | |
| 172 | + }); | |
| 157 | 173 | }); |
| 158 | 174 | |
| 159 | 175 | it("should prepend MCPs from --mcp flag", async () => { |
| @@ -164,9 +180,7 @@ | ||
| 164 | 180 | ]); |
| 165 | 181 | |
| 166 | 182 | // Set up existing MCPs in config |
| 167 | - mockConfig.mcpServers = [ | |
| 168 | - { name: "Existing-MCP", command: "existing-mcp" }, | |
| 169 | - ]; | |
| 183 | + mockConfig.mcpServers = [{ name: "Existing-MCP", command: "existing-mcp" }]; | |
| 170 | 184 | |
| 171 | 185 | const options: BaseCommandOptions = { |
| 172 | 186 | mcp: ["test/new-mcp"], |
| @@ -176,7 +190,13 @@ | ||
| 176 | 190 | |
| 177 | 191 | // MCPs from --mcp flag should be prepended (at the start) |
| 178 | 192 | expect(config.mcpServers).toHaveLength(2); |
| 179 | - expect(config.mcpServers?.[0]).toEqual({ name: "New-MCP", command: "new-mcp" }); | |
| 180 | - expect(config.mcpServers?.[1]).toEqual({ name: "Existing-MCP", command: "existing-mcp" }); | |
| 193 | + expect(config.mcpServers?.[0]).toEqual({ | |
| 194 | + name: "New-MCP", | |
| 195 | + command: "new-mcp", | |
| 196 | + }); | |
| 197 | + expect(config.mcpServers?.[1]).toEqual({ | |
| 198 | + name: "Existing-MCP", | |
| 199 | + command: "existing-mcp", | |
| 200 | + }); | |
| 181 | 201 | }); |
| 182 | -}); | |
| No newline at the end of the file | ||
| 202 | +}); |
modified extensions/cli/src/configEnhancer.ts +7 −6
| @@ -57,12 +57,13 @@ | ||
| 57 | 57 | for (const ruleSpec of rules) { |
| 58 | 58 | try { |
| 59 | 59 | const processedContent = await processRule(ruleSpec); |
| 60 | - | |
| 60 | + | |
| 61 | 61 | // Check if this is a hub slug (contains / but doesn't start with . or /) |
| 62 | - const isHubSlug = ruleSpec.includes("/") && | |
| 63 | - !ruleSpec.startsWith(".") && | |
| 64 | - !ruleSpec.startsWith("/"); | |
| 65 | - | |
| 62 | + const isHubSlug = | |
| 63 | + ruleSpec.includes("/") && | |
| 64 | + !ruleSpec.startsWith(".") && | |
| 65 | + !ruleSpec.startsWith("/"); | |
| 66 | + | |
| 66 | 67 | if (isHubSlug) { |
| 67 | 68 | // Store as RuleObject with name (slug) and rule (content) |
| 68 | 69 | processedRules.push({ |
| @@ -149,4 +150,4 @@ | ||
| 149 | 150 | /** |
| 150 | 151 | * Global config enhancer instance |
| 151 | 152 | */ |
| 152 | -export const configEnhancer = new ConfigEnhancer(); | |
| No newline at the end of the file | ||
| 153 | +export const configEnhancer = new ConfigEnhancer(); |
modified extensions/cli/src/configLoader.ts +10 −0
| @@ -301,6 +301,16 @@ | ||
| 301 | 301 | renderSecrets: true, |
| 302 | 302 | platformClient: new CLIPlatformClient(organizationId, apiClient), |
| 303 | 303 | onPremProxyUrl: null, |
| 304 | + injectBlocks: [ | |
| 305 | + { | |
| 306 | + uriType: "slug", | |
| 307 | + fullSlug: { | |
| 308 | + ownerSlug: "openai", | |
| 309 | + packageSlug: "gpt-5", | |
| 310 | + versionSlug: "latest", | |
| 311 | + }, | |
| 312 | + }, | |
| 313 | + ], | |
| 304 | 314 | }, |
| 305 | 315 | ); |
| 306 | 316 |
modified extensions/cli/src/hubLoader.test.ts +43 −23
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | // Mock JSZip |
| 19 | 19 | vi.mock("jszip", () => ({ |
| 20 | - default: vi.fn() | |
| 20 | + default: vi.fn(), | |
| 21 | 21 | })); |
| 22 | 22 | |
| 23 | 23 | const mockedJSZip = vi.fn(); |
| @@ -45,7 +45,9 @@ | ||
| 45 | 45 | |
| 46 | 46 | await expect( |
| 47 | 47 | loadPackageFromHub("owner/package", ruleProcessor), |
| 48 | - ).rejects.toThrow('Failed to load rule from hub "owner/package": HTTP 404: Not Found'); | |
| 48 | + ).rejects.toThrow( | |
| 49 | + 'Failed to load rule from hub "owner/package": HTTP 404: Not Found', | |
| 50 | + ); | |
| 49 | 51 | }); |
| 50 | 52 | |
| 51 | 53 | it("should load rule content", async () => { |
| @@ -54,9 +56,9 @@ | ||
| 54 | 56 | // Check if JSZip is mocked |
| 55 | 57 | const JSZipModule = await import("jszip"); |
| 56 | 58 | const JSZip = JSZipModule.default; |
| 57 | - | |
| 59 | + | |
| 58 | 60 | // Since JSZip is mocked, it should be a mock function |
| 59 | - if (typeof (JSZip as any).mockImplementation === 'function') { | |
| 61 | + if (typeof (JSZip as any).mockImplementation === "function") { | |
| 60 | 62 | (JSZip as any).mockImplementation(() => ({ |
| 61 | 63 | loadAsync: vi.fn().mockResolvedValueOnce({ |
| 62 | 64 | files: { |
| @@ -65,7 +67,7 @@ | ||
| 65 | 67 | async: vi.fn().mockResolvedValue(ruleContent), |
| 66 | 68 | }, |
| 67 | 69 | }, |
| 68 | - }) | |
| 70 | + }), | |
| 69 | 71 | })); |
| 70 | 72 | } else { |
| 71 | 73 | // If mock doesn't work, skip this test |
| @@ -84,7 +86,7 @@ | ||
| 84 | 86 | |
| 85 | 87 | it("should load MCP configuration", async () => { |
| 86 | 88 | const mcpConfig = { |
| 87 | - content: "name: test-mcp\nversion: 1.0.0" | |
| 89 | + content: "name: test-mcp\nversion: 1.0.0", | |
| 88 | 90 | }; |
| 89 | 91 | |
| 90 | 92 | mockFetch.mockResolvedValueOnce({ |
| @@ -96,7 +98,7 @@ | ||
| 96 | 98 | // Should parse the YAML content, not return the wrapper |
| 97 | 99 | expect(result).toEqual({ |
| 98 | 100 | name: "test-mcp", |
| 99 | - version: "1.0.0" | |
| 101 | + version: "1.0.0", | |
| 100 | 102 | }); |
| 101 | 103 | }); |
| 102 | 104 | |
| @@ -104,9 +106,9 @@ | ||
| 104 | 106 | // Check if JSZip is mocked |
| 105 | 107 | const JSZipModule = await import("jszip"); |
| 106 | 108 | const JSZip = JSZipModule.default; |
| 107 | - | |
| 109 | + | |
| 108 | 110 | // Since JSZip is mocked, it should be a mock function |
| 109 | - if (typeof (JSZip as any).mockImplementation === 'function') { | |
| 111 | + if (typeof (JSZip as any).mockImplementation === "function") { | |
| 110 | 112 | (JSZip as any).mockImplementation(() => ({ |
| 111 | 113 | loadAsync: vi.fn().mockResolvedValueOnce({ |
| 112 | 114 | files: { |
| @@ -115,7 +117,7 @@ | ||
| 115 | 117 | async: vi.fn().mockResolvedValue("not a rule"), |
| 116 | 118 | }, |
| 117 | 119 | }, |
| 118 | - }) | |
| 120 | + }), | |
| 119 | 121 | })); |
| 120 | 122 | } else { |
| 121 | 123 | // If mock doesn't work, skip this test |
| @@ -140,23 +142,35 @@ | ||
| 140 | 142 | it("should define rule processor correctly", () => { |
| 141 | 143 | expect(ruleProcessor.type).toBe("rule"); |
| 142 | 144 | expect(ruleProcessor.expectedFileExtensions).toEqual([".md"]); |
| 143 | - expect(ruleProcessor.parseContent("test content", "test.md")).toBe("test content"); | |
| 145 | + expect(ruleProcessor.parseContent("test content", "test.md")).toBe( | |
| 146 | + "test content", | |
| 147 | + ); | |
| 144 | 148 | }); |
| 145 | 149 | |
| 146 | 150 | it("should define MCP processor correctly", () => { |
| 147 | 151 | expect(mcpProcessor.type).toBe("mcp"); |
| 148 | - expect(mcpProcessor.expectedFileExtensions).toEqual([".json", ".yaml", ".yml"]); | |
| 152 | + expect(mcpProcessor.expectedFileExtensions).toEqual([ | |
| 153 | + ".json", | |
| 154 | + ".yaml", | |
| 155 | + ".yml", | |
| 156 | + ]); | |
| 149 | 157 | }); |
| 150 | 158 | |
| 151 | 159 | it("should define model processor correctly", () => { |
| 152 | 160 | expect(modelProcessor.type).toBe("model"); |
| 153 | - expect(modelProcessor.expectedFileExtensions).toEqual([".json", ".yaml", ".yml"]); | |
| 161 | + expect(modelProcessor.expectedFileExtensions).toEqual([ | |
| 162 | + ".json", | |
| 163 | + ".yaml", | |
| 164 | + ".yml", | |
| 165 | + ]); | |
| 154 | 166 | }); |
| 155 | 167 | |
| 156 | 168 | it("should define prompt processor correctly", () => { |
| 157 | 169 | expect(promptProcessor.type).toBe("prompt"); |
| 158 | 170 | expect(promptProcessor.expectedFileExtensions).toEqual([".md", ".txt"]); |
| 159 | - expect(promptProcessor.parseContent("test content", "test.md")).toBe("test content"); | |
| 171 | + expect(promptProcessor.parseContent("test content", "test.md")).toBe( | |
| 172 | + "test content", | |
| 173 | + ); | |
| 160 | 174 | }); |
| 161 | 175 | }); |
| 162 | 176 | |
| @@ -171,8 +185,11 @@ | ||
| 171 | 185 | }); |
| 172 | 186 | |
| 173 | 187 | it("should load rule from real hub: sanity/sanity-opinionated", async () => { |
| 174 | - const result = await loadPackageFromHub("sanity/sanity-opinionated", ruleProcessor); | |
| 175 | - | |
| 188 | + const result = await loadPackageFromHub( | |
| 189 | + "sanity/sanity-opinionated", | |
| 190 | + ruleProcessor, | |
| 191 | + ); | |
| 192 | + | |
| 176 | 193 | expect(result).toBeDefined(); |
| 177 | 194 | expect(typeof result).toBe("string"); |
| 178 | 195 | expect(result.length).toBeGreaterThan(0); |
| @@ -184,10 +201,10 @@ | ||
| 184 | 201 | // Restore real fetch and JSZip for this test |
| 185 | 202 | global.fetch = originalFetch; |
| 186 | 203 | vi.unmock("jszip"); |
| 187 | - | |
| 204 | + | |
| 188 | 205 | const testSlug = "upstash/context7-mcp"; |
| 189 | 206 | const result = await loadPackageFromHub(testSlug, mcpProcessor); |
| 190 | - | |
| 207 | + | |
| 191 | 208 | expect(result).toBeDefined(); |
| 192 | 209 | expect(typeof result).toBe("object"); |
| 193 | 210 | // Should now return the parsed MCP configuration |
| @@ -202,10 +219,10 @@ | ||
| 202 | 219 | // Restore real fetch and JSZip for this test |
| 203 | 220 | global.fetch = originalFetch; |
| 204 | 221 | vi.unmock("jszip"); |
| 205 | - | |
| 222 | + | |
| 206 | 223 | const testSlug = "openai/gpt-5"; |
| 207 | 224 | const result = await loadPackageFromHub(testSlug, modelProcessor); |
| 208 | - | |
| 225 | + | |
| 209 | 226 | expect(result).toBeDefined(); |
| 210 | 227 | expect(typeof result).toBe("object"); |
| 211 | 228 | // Should now return the extracted model from the models array |
| @@ -219,11 +236,14 @@ | ||
| 219 | 236 | }, 30000); |
| 220 | 237 | |
| 221 | 238 | it("should load prompt from real hub: launchdarkly/using-flags", async () => { |
| 222 | - const result = await loadPackageFromHub("launchdarkly/using-flags", promptProcessor); | |
| 223 | - | |
| 239 | + const result = await loadPackageFromHub( | |
| 240 | + "launchdarkly/using-flags", | |
| 241 | + promptProcessor, | |
| 242 | + ); | |
| 243 | + | |
| 224 | 244 | expect(result).toBeDefined(); |
| 225 | 245 | expect(typeof result).toBe("string"); |
| 226 | 246 | expect(result.length).toBeGreaterThan(0); |
| 227 | 247 | }, 30000); |
| 228 | 248 | }); |
| 229 | -}); | |
| No newline at the end of the file | ||
| 249 | +}); |
modified extensions/cli/src/hubLoader.ts +39 −20
| @@ -41,13 +41,17 @@ | ||
| 41 | 41 | const yaml = await import("yaml"); |
| 42 | 42 | parsed = yaml.parse(content); |
| 43 | 43 | } |
| 44 | - | |
| 44 | + | |
| 45 | 45 | // If the parsed content has an 'mcpServers' array, extract the first MCP server |
| 46 | 46 | // This handles hub packages that return a full config block |
| 47 | - if (parsed.mcpServers && Array.isArray(parsed.mcpServers) && parsed.mcpServers.length > 0) { | |
| 47 | + if ( | |
| 48 | + parsed.mcpServers && | |
| 49 | + Array.isArray(parsed.mcpServers) && | |
| 50 | + parsed.mcpServers.length > 0 | |
| 51 | + ) { | |
| 48 | 52 | return parsed.mcpServers[0]; |
| 49 | 53 | } |
| 50 | - | |
| 54 | + | |
| 51 | 55 | return parsed; |
| 52 | 56 | }, |
| 53 | 57 | }; |
| @@ -66,13 +70,17 @@ | ||
| 66 | 70 | const yaml = await import("yaml"); |
| 67 | 71 | parsed = yaml.parse(content); |
| 68 | 72 | } |
| 69 | - | |
| 73 | + | |
| 70 | 74 | // If the parsed content has a 'models' array, extract the first model |
| 71 | 75 | // This handles hub packages that return a full config block |
| 72 | - if (parsed.models && Array.isArray(parsed.models) && parsed.models.length > 0) { | |
| 76 | + if ( | |
| 77 | + parsed.models && | |
| 78 | + Array.isArray(parsed.models) && | |
| 79 | + parsed.models.length > 0 | |
| 80 | + ) { | |
| 73 | 81 | return parsed.models[0]; |
| 74 | 82 | } |
| 75 | - | |
| 83 | + | |
| 76 | 84 | return parsed; |
| 77 | 85 | }, |
| 78 | 86 | }; |
| @@ -101,7 +109,7 @@ | ||
| 101 | 109 | } |
| 102 | 110 | |
| 103 | 111 | const [ownerSlug, packageSlug] = parts; |
| 104 | - | |
| 112 | + | |
| 105 | 113 | // Use different endpoints based on package type |
| 106 | 114 | let downloadUrl: URL; |
| 107 | 115 | if (processor.type === "mcp" || processor.type === "model") { |
| @@ -129,7 +137,7 @@ | ||
| 129 | 137 | if (processor.type === "mcp" || processor.type === "model") { |
| 130 | 138 | // Registry endpoints return JSON with a 'content' field containing YAML/JSON |
| 131 | 139 | const jsonResponse = await response.json(); |
| 132 | - | |
| 140 | + | |
| 133 | 141 | // Extract and parse the content field |
| 134 | 142 | if (jsonResponse.content) { |
| 135 | 143 | // Parse the YAML/JSON content |
| @@ -149,12 +157,14 @@ | ||
| 149 | 157 | const zipContents = await zip.loadAsync(arrayBuffer); |
| 150 | 158 | |
| 151 | 159 | // Find files matching expected extensions |
| 152 | - const matchingFiles = Object.keys(zipContents.files).filter((filename) => { | |
| 153 | - if (zipContents.files[filename].dir) return false; | |
| 154 | - return processor.expectedFileExtensions.some((ext) => | |
| 155 | - filename.endsWith(ext), | |
| 156 | - ); | |
| 157 | - }); | |
| 160 | + const matchingFiles = Object.keys(zipContents.files).filter( | |
| 161 | + (filename) => { | |
| 162 | + if (zipContents.files[filename].dir) return false; | |
| 163 | + return processor.expectedFileExtensions.some((ext) => | |
| 164 | + filename.endsWith(ext), | |
| 165 | + ); | |
| 166 | + }, | |
| 167 | + ); | |
| 158 | 168 | |
| 159 | 169 | if (matchingFiles.length === 0) { |
| 160 | 170 | throw new Error( |
| @@ -173,8 +183,13 @@ | ||
| 173 | 183 | ); |
| 174 | 184 | |
| 175 | 185 | // Validate if processor has validation |
| 176 | - if (processor.validateContent && !processor.validateContent(parsedContent)) { | |
| 177 | - throw new Error(`Invalid ${processor.type} content in ${matchingFiles[0]}`); | |
| 186 | + if ( | |
| 187 | + processor.validateContent && | |
| 188 | + !processor.validateContent(parsedContent) | |
| 189 | + ) { | |
| 190 | + throw new Error( | |
| 191 | + `Invalid ${processor.type} content in ${matchingFiles[0]}`, | |
| 192 | + ); | |
| 178 | 193 | } |
| 179 | 194 | |
| 180 | 195 | return parsedContent; |
| @@ -222,7 +237,7 @@ | ||
| 222 | 237 | ) { |
| 223 | 238 | const fs = await import("fs"); |
| 224 | 239 | const path = await import("path"); |
| 225 | - | |
| 240 | + | |
| 226 | 241 | try { |
| 227 | 242 | const absolutePath = path.resolve(ruleSpec); |
| 228 | 243 | if (!fs.existsSync(absolutePath)) { |
| @@ -230,7 +245,9 @@ | ||
| 230 | 245 | } |
| 231 | 246 | return fs.readFileSync(absolutePath, "utf-8"); |
| 232 | 247 | } catch (error: any) { |
| 233 | - throw new Error(`Failed to read rule file "${ruleSpec}": ${error.message}`); | |
| 248 | + throw new Error( | |
| 249 | + `Failed to read rule file "${ruleSpec}": ${error.message}`, | |
| 250 | + ); | |
| 234 | 251 | } |
| 235 | 252 | } |
| 236 | 253 | |
| @@ -252,9 +269,11 @@ | ||
| 252 | 269 | const content = await loadPackageFromHub(slug, processor); |
| 253 | 270 | results.push(content); |
| 254 | 271 | } catch (error: any) { |
| 255 | - logger.warn(`Failed to load ${processor.type} "${slug}": ${error.message}`); | |
| 272 | + logger.warn( | |
| 273 | + `Failed to load ${processor.type} "${slug}": ${error.message}`, | |
| 274 | + ); | |
| 256 | 275 | } |
| 257 | 276 | } |
| 258 | 277 | |
| 259 | 278 | return results; |
| 260 | -} | |
| No newline at the end of the file | ||
| 279 | +} |
modified extensions/cli/src/integration/rule-duplication.test.ts +6 −6
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { AssistantUnrolled } from "@continuedev/config-yaml"; |
| 2 | -import { beforeEach, describe, expect, it, vi } from "vitest"; | |
| 2 | +import { describe, expect, it, vi } from "vitest"; | |
| 3 | 3 | |
| 4 | 4 | import { BaseCommandOptions } from "../commands/BaseCommandOptions.js"; |
| 5 | 5 | import { ConfigEnhancer } from "../configEnhancer.js"; |
| @@ -22,7 +22,7 @@ | ||
| 22 | 22 | describe("Rule duplication integration test", () => { |
| 23 | 23 | it("should not duplicate rules when using --rule flag", async () => { |
| 24 | 24 | const enhancer = new ConfigEnhancer(); |
| 25 | - | |
| 25 | + | |
| 26 | 26 | // Initial config without any rules |
| 27 | 27 | const initialConfig: AssistantUnrolled = { |
| 28 | 28 | name: "Test Assistant", |
| @@ -40,13 +40,13 @@ | ||
| 40 | 40 | // Verify the rule was added exactly once as a RuleObject |
| 41 | 41 | expect(enhancedConfig.rules).toHaveLength(1); |
| 42 | 42 | expect(enhancedConfig.rules).toEqual([ |
| 43 | - { name: "nate/spanish", rule: "Content for nate/spanish" } | |
| 43 | + { name: "nate/spanish", rule: "Content for nate/spanish" }, | |
| 44 | 44 | ]); |
| 45 | 45 | }); |
| 46 | 46 | |
| 47 | 47 | it("should merge command-line rules with existing config rules", async () => { |
| 48 | 48 | const enhancer = new ConfigEnhancer(); |
| 49 | - | |
| 49 | + | |
| 50 | 50 | // Initial config with existing rules |
| 51 | 51 | const initialConfig: AssistantUnrolled = { |
| 52 | 52 | name: "Test Assistant", |
| @@ -72,7 +72,7 @@ | ||
| 72 | 72 | |
| 73 | 73 | it("should handle rule content with frontmatter", async () => { |
| 74 | 74 | const enhancer = new ConfigEnhancer(); |
| 75 | - | |
| 75 | + | |
| 76 | 76 | // Mock processRule to return content with frontmatter |
| 77 | 77 | const { processRule } = await import("../hubLoader.js"); |
| 78 | 78 | (processRule as any).mockImplementation((rule: string) => { |
| @@ -104,4 +104,4 @@ | ||
| 104 | 104 | expect(ruleObj.rule).toContain("Always respond in Spanish."); |
| 105 | 105 | expect(ruleObj.rule).toContain("alwaysApply: true"); |
| 106 | 106 | }); |
| 107 | -}); | |
| No newline at the end of the file | ||
| 107 | +}); |
modified extensions/cli/src/loadMcpFromHub.test.ts +8 −7
| @@ -18,7 +18,7 @@ | ||
| 18 | 18 | |
| 19 | 19 | beforeEach(async () => { |
| 20 | 20 | vi.clearAllMocks(); |
| 21 | - | |
| 21 | + | |
| 22 | 22 | // Get the mocked JSZip constructor |
| 23 | 23 | const JSZip = (await import("jszip")).default; |
| 24 | 24 | mockJSZip = new JSZip(); |
| @@ -52,13 +52,14 @@ | ||
| 52 | 52 | |
| 53 | 53 | it("should parse JSON configuration files", async () => { |
| 54 | 54 | const mockConfig = { |
| 55 | - content: "name: test-mcp\ncommand: npx test-mcp\nargs:\n - --port\n - 3000" | |
| 55 | + content: | |
| 56 | + "name: test-mcp\ncommand: npx test-mcp\nargs:\n - --port\n - 3000", | |
| 56 | 57 | }; |
| 57 | 58 | |
| 58 | 59 | // MCP packages now use the registry endpoint and return JSON directly |
| 59 | 60 | mockFetch.mockResolvedValueOnce({ |
| 60 | 61 | ok: true, |
| 61 | - json: vi.fn().mockResolvedValue(mockConfig) | |
| 62 | + json: vi.fn().mockResolvedValue(mockConfig), | |
| 62 | 63 | }); |
| 63 | 64 | |
| 64 | 65 | const result = await loadMcpFromHub("continuedev/test-mcp"); |
| @@ -66,7 +67,7 @@ | ||
| 66 | 67 | expect(result).toEqual({ |
| 67 | 68 | name: "test-mcp", |
| 68 | 69 | command: "npx test-mcp", |
| 69 | - args: ["--port", 3000] | |
| 70 | + args: ["--port", 3000], | |
| 70 | 71 | }); |
| 71 | 72 | }); |
| 72 | 73 | |
| @@ -75,7 +76,7 @@ | ||
| 75 | 76 | // If the content field is missing or empty, it should fail |
| 76 | 77 | mockFetch.mockResolvedValueOnce({ |
| 77 | 78 | ok: true, |
| 78 | - json: vi.fn().mockResolvedValue({}) | |
| 79 | + json: vi.fn().mockResolvedValue({}), | |
| 79 | 80 | }); |
| 80 | 81 | |
| 81 | 82 | const result = await loadMcpFromHub("continuedev/no-config"); |
| @@ -88,11 +89,11 @@ | ||
| 88 | 89 | // If the registry returns invalid JSON, it should fail at the fetch level |
| 89 | 90 | mockFetch.mockResolvedValueOnce({ |
| 90 | 91 | ok: true, |
| 91 | - json: vi.fn().mockRejectedValue(new Error("Invalid JSON")) | |
| 92 | + json: vi.fn().mockRejectedValue(new Error("Invalid JSON")), | |
| 92 | 93 | }); |
| 93 | 94 | |
| 94 | 95 | await expect(loadMcpFromHub("continuedev/invalid-json")).rejects.toThrow( |
| 95 | 96 | 'Failed to load mcp from hub "continuedev/invalid-json": Invalid JSON', |
| 96 | 97 | ); |
| 97 | 98 | }); |
| 98 | -}); | |
| No newline at the end of the file | ||
| 99 | +}); |
modified extensions/cli/src/mcp-flag.integration.test.ts +29 −21
| @@ -13,23 +13,26 @@ | ||
| 13 | 13 | it("should demonstrate the complete --mcp flag functionality", () => { |
| 14 | 14 | // Test command line parsing |
| 15 | 15 | process.argv = [ |
| 16 | - "node", | |
| 16 | + "node", | |
| 17 | 17 | "test", |
| 18 | - "--config", "my-config.yaml", | |
| 19 | - "--mcp", "continuedev/filesystem", | |
| 20 | - "--mcp", "continuedev/github", | |
| 21 | - "hello world" | |
| 18 | + "--config", | |
| 19 | + "my-config.yaml", | |
| 20 | + "--mcp", | |
| 21 | + "continuedev/filesystem", | |
| 22 | + "--mcp", | |
| 23 | + "continuedev/github", | |
| 24 | + "hello world", | |
| 22 | 25 | ]; |
| 23 | 26 | |
| 24 | 27 | const args = parseArgs(); |
| 25 | - | |
| 28 | + | |
| 26 | 29 | // Verify the parsed command line arguments |
| 27 | 30 | expect(args).toEqual({ |
| 28 | 31 | configPath: "my-config.yaml", |
| 29 | 32 | mcps: ["continuedev/filesystem", "continuedev/github"], |
| 30 | - prompt: "hello world" | |
| 33 | + prompt: "hello world", | |
| 31 | 34 | }); |
| 32 | - | |
| 35 | + | |
| 33 | 36 | // Verify MCP slugs are properly captured |
| 34 | 37 | expect(args.mcps).toHaveLength(2); |
| 35 | 38 | expect(args.mcps).toContain("continuedev/filesystem"); |
| @@ -39,34 +42,39 @@ | ||
| 39 | 42 | it("should validate MCP slug format", async () => { |
| 40 | 43 | // Test that invalid slug formats are rejected |
| 41 | 44 | await expect(loadMcpFromHub("invalid-slug")).rejects.toThrow( |
| 42 | - 'Invalid hub slug format. Expected "owner/package", got: invalid-slug' | |
| 45 | + 'Invalid hub slug format. Expected "owner/package", got: invalid-slug', | |
| 43 | 46 | ); |
| 44 | 47 | |
| 45 | 48 | // Valid format should not throw during validation step |
| 46 | 49 | const mockFetch = vi.fn().mockResolvedValue({ |
| 47 | 50 | ok: false, |
| 48 | 51 | status: 404, |
| 49 | - statusText: "Not Found" | |
| 52 | + statusText: "Not Found", | |
| 50 | 53 | }); |
| 51 | 54 | global.fetch = mockFetch; |
| 52 | 55 | |
| 53 | 56 | // This will fail at the HTTP level, not validation level |
| 54 | 57 | await expect(loadMcpFromHub("valid/slug")).rejects.toThrow( |
| 55 | - 'Failed to load mcp from hub "valid/slug": HTTP 404: Not Found' | |
| 58 | + 'Failed to load mcp from hub "valid/slug": HTTP 404: Not Found', | |
| 56 | 59 | ); |
| 57 | 60 | }); |
| 58 | 61 | |
| 59 | 62 | it("should demonstrate MCP flag with other command line options", () => { |
| 60 | 63 | process.argv = [ |
| 61 | 64 | "node", |
| 62 | - "test", | |
| 63 | - "--config", "test-config.yaml", | |
| 64 | - "--org", "my-org", | |
| 65 | - "--rule", "my-rule", | |
| 66 | - "--mcp", "continuedev/filesystem", | |
| 67 | - "--allow", "readFile", | |
| 65 | + "test", | |
| 66 | + "--config", | |
| 67 | + "test-config.yaml", | |
| 68 | + "--org", | |
| 69 | + "my-org", | |
| 70 | + "--rule", | |
| 71 | + "my-rule", | |
| 72 | + "--mcp", | |
| 73 | + "continuedev/filesystem", | |
| 74 | + "--allow", | |
| 75 | + "readFile", | |
| 68 | 76 | "--readonly", |
| 69 | - "analyze this code" | |
| 77 | + "analyze this code", | |
| 70 | 78 | ]; |
| 71 | 79 | |
| 72 | 80 | const args = parseArgs(); |
| @@ -74,11 +82,11 @@ | ||
| 74 | 82 | // Verify all flags are parsed correctly together |
| 75 | 83 | expect(args).toEqual({ |
| 76 | 84 | configPath: "test-config.yaml", |
| 77 | - organizationSlug: "my-org", | |
| 85 | + organizationSlug: "my-org", | |
| 78 | 86 | rules: ["my-rule"], |
| 79 | 87 | mcps: ["continuedev/filesystem"], |
| 80 | 88 | readonly: true, |
| 81 | - prompt: "analyze this code" | |
| 89 | + prompt: "analyze this code", | |
| 82 | 90 | }); |
| 83 | 91 | }); |
| 84 | -}); | |
| No newline at the end of the file | ||
| 92 | +}); |
modified extensions/cli/src/services/BaseService.ts +8 −2
| @@ -27,14 +27,20 @@ | ||
| 27 | 27 | * Public initialization method that wraps the implementation |
| 28 | 28 | */ |
| 29 | 29 | async initialize(...args: any[]): Promise<TState> { |
| 30 | - logger.debug(`Initializing ${this.serviceName}`); | |
| 30 | + logger.debug(`Initializing ${this.serviceName}`, { | |
| 31 | + wasInitialized: this.isInitialized, | |
| 32 | + hadPreviousState: !!this.currentState, | |
| 33 | + }); | |
| 31 | 34 | this.emit("initializing"); |
| 32 | 35 | |
| 33 | 36 | try { |
| 34 | 37 | const state = await this.doInitialize(...args); |
| 35 | 38 | this.currentState = state; |
| 36 | 39 | this.isInitialized = true; |
| 37 | - logger.debug(`${this.serviceName} initialized successfully`); | |
| 40 | + logger.debug(`${this.serviceName} initialized successfully`, { | |
| 41 | + stateKeys: state ? Object.keys(state as any) : [], | |
| 42 | + isNowInitialized: this.isInitialized, | |
| 43 | + }); | |
| 38 | 44 | this.emit("initialized", state); |
| 39 | 45 | return state; |
| 40 | 46 | } catch (error: any) { |
modified extensions/cli/src/services/ConfigService.test.ts +2 −2
| @@ -74,13 +74,13 @@ | ||
| 74 | 74 | config: mockConfig as any, |
| 75 | 75 | source: { type: "cli-flag", path: "/config.yaml" } as any, |
| 76 | 76 | }); |
| 77 | - | |
| 77 | + | |
| 78 | 78 | const expectedConfig = { |
| 79 | 79 | ...mockConfig, |
| 80 | 80 | systemMessage: |
| 81 | 81 | "Test system message\n\nProcessed: rule1\n\nProcessed: rule2", |
| 82 | 82 | }; |
| 83 | - | |
| 83 | + | |
| 84 | 84 | vi.mocked(configEnhancer.enhanceConfig).mockResolvedValue(expectedConfig); |
| 85 | 85 | |
| 86 | 86 | const state = await service.initialize( |
modified extensions/cli/src/services/ConfigService.ts +8 −3
| @@ -1,4 +1,3 @@ | ||
| 1 | - | |
| 2 | 1 | import { DefaultApiInterface } from "@continuedev/sdk/dist/api/dist/index.js"; |
| 3 | 2 | |
| 4 | 3 | import { AuthConfig, loadAuthConfig } from "../auth/workos.js"; |
| @@ -53,7 +52,10 @@ | ||
| 53 | 52 | let config = result.config; |
| 54 | 53 | |
| 55 | 54 | // Apply injected config if provided |
| 56 | - if (injectedConfigOptions && this.hasInjectedConfig(injectedConfigOptions)) { | |
| 55 | + if ( | |
| 56 | + injectedConfigOptions && | |
| 57 | + this.hasInjectedConfig(injectedConfigOptions) | |
| 58 | + ) { | |
| 57 | 59 | config = await configEnhancer.enhanceConfig( |
| 58 | 60 | config, |
| 59 | 61 | injectedConfigOptions, |
| @@ -99,7 +101,10 @@ | ||
| 99 | 101 | let config = result.config; |
| 100 | 102 | |
| 101 | 103 | // Apply injected config if provided |
| 102 | - if (injectedConfigOptions && this.hasInjectedConfig(injectedConfigOptions)) { | |
| 104 | + if ( | |
| 105 | + injectedConfigOptions && | |
| 106 | + this.hasInjectedConfig(injectedConfigOptions) | |
| 107 | + ) { | |
| 103 | 108 | config = await configEnhancer.enhanceConfig( |
| 104 | 109 | config, |
| 105 | 110 | injectedConfigOptions, |
added extensions/cli/src/services/ModelService.direct.test.ts +207 −0
| @@ -0,0 +1,207 @@ | ||
| 1 | +import { describe, it, expect, beforeEach } from "vitest"; | |
| 2 | + | |
| 3 | +import { ModelService } from "./ModelService.js"; | |
| 4 | + | |
| 5 | +describe("ModelService - Direct Testing", () => { | |
| 6 | + let modelService: ModelService; | |
| 7 | + | |
| 8 | + beforeEach(() => { | |
| 9 | + modelService = new ModelService(); | |
| 10 | + }); | |
| 11 | + | |
| 12 | + it("should throw 'ModelService not initialized' when switching without initialization", async () => { | |
| 13 | + // The service is created but not initialized (no assistant or authConfig) | |
| 14 | + expect(modelService.getCurrentModelIndex()).toBe(-1); | |
| 15 | + | |
| 16 | + // Try to switch model without initialization | |
| 17 | + try { | |
| 18 | + await modelService.switchModel(0); | |
| 19 | + // Should not reach here | |
| 20 | + expect(true).toBe(false); | |
| 21 | + } catch (error: any) { | |
| 22 | + // Should throw the specific error | |
| 23 | + expect(error.message).toBe( | |
| 24 | + "ModelService not initialized - assistant data missing", | |
| 25 | + ); | |
| 26 | + } | |
| 27 | + }); | |
| 28 | + | |
| 29 | + it("should handle model switching with null authConfig for non-proxy models", async () => { | |
| 30 | + // Initialize the service properly | |
| 31 | + const mockAssistant = { | |
| 32 | + name: "Test Assistant", | |
| 33 | + version: "1.0.0", | |
| 34 | + models: [ | |
| 35 | + { | |
| 36 | + name: "claude-3.5-sonnet", | |
| 37 | + provider: "anthropic", | |
| 38 | + model: "claude-3.5-sonnet", | |
| 39 | + apiKey: "test-anthropic-key", | |
| 40 | + roles: ["chat"], | |
| 41 | + }, | |
| 42 | + { | |
| 43 | + name: "GPT-4", | |
| 44 | + provider: "openai", | |
| 45 | + model: "gpt-4", | |
| 46 | + apiKey: "test-openai-key", | |
| 47 | + roles: ["chat"], | |
| 48 | + }, | |
| 49 | + ], | |
| 50 | + }; | |
| 51 | + | |
| 52 | + // Initialize with null authConfig - valid for non-proxy models | |
| 53 | + const initialState = await modelService.initialize( | |
| 54 | + mockAssistant as any, | |
| 55 | + null as any, | |
| 56 | + ); | |
| 57 | + | |
| 58 | + // Verify initialization succeeded with null authConfig | |
| 59 | + expect(initialState.assistant).toBe(mockAssistant); | |
| 60 | + expect(initialState.authConfig).toBe(null); | |
| 61 | + expect(initialState.model).toBeDefined(); | |
| 62 | + expect((initialState.model as any).name).toBe("claude-3.5-sonnet"); | |
| 63 | + | |
| 64 | + // Should be able to switch models even with null authConfig | |
| 65 | + const switchedState = await modelService.switchModel(1); | |
| 66 | + expect(switchedState.model).toBeDefined(); | |
| 67 | + expect((switchedState.model as any).name).toBe("GPT-4"); | |
| 68 | + expect(switchedState.assistant).toBe(mockAssistant); | |
| 69 | + expect(switchedState.authConfig).toBe(null); | |
| 70 | + | |
| 71 | + // Verify the model info | |
| 72 | + const modelInfo = modelService.getModelInfo(); | |
| 73 | + expect(modelInfo?.name).toBe("GPT-4"); | |
| 74 | + expect(modelInfo?.provider).toBe("openai"); | |
| 75 | + }); | |
| 76 | + | |
| 77 | + it("should successfully switch model after proper initialization", async () => { | |
| 78 | + // Initialize the service properly | |
| 79 | + const mockAssistant = { | |
| 80 | + name: "Test Assistant", | |
| 81 | + version: "1.0.0", | |
| 82 | + models: [ | |
| 83 | + { | |
| 84 | + name: "GPT-4", | |
| 85 | + provider: "openai", | |
| 86 | + model: "gpt-4", | |
| 87 | + apiKey: "test-key-1", | |
| 88 | + roles: ["chat"], | |
| 89 | + }, | |
| 90 | + { | |
| 91 | + name: "GPT-3.5", | |
| 92 | + provider: "openai", | |
| 93 | + model: "gpt-3.5-turbo", | |
| 94 | + apiKey: "test-key-2", | |
| 95 | + roles: ["chat"], | |
| 96 | + }, | |
| 97 | + ], | |
| 98 | + }; | |
| 99 | + | |
| 100 | + const mockAuthConfig = { | |
| 101 | + token: "test-token", | |
| 102 | + }; | |
| 103 | + | |
| 104 | + // Initialize the service (note: initialize calls doInitialize and sets the state) | |
| 105 | + const initialState = await modelService.initialize( | |
| 106 | + mockAssistant as any, | |
| 107 | + mockAuthConfig as any, | |
| 108 | + ); | |
| 109 | + | |
| 110 | + // Verify we have models and the first one is selected | |
| 111 | + const availableModels = modelService.getAvailableChatModels(); | |
| 112 | + expect(availableModels.length).toBe(2); | |
| 113 | + expect(initialState.model).toBeDefined(); | |
| 114 | + expect((initialState.model as any).name).toBe("GPT-4"); | |
| 115 | + | |
| 116 | + // Now switch should work | |
| 117 | + await modelService.switchModel(1); | |
| 118 | + expect(modelService.getCurrentModelIndex()).toBe(1); | |
| 119 | + | |
| 120 | + const modelInfo = modelService.getModelInfo(); | |
| 121 | + expect(modelInfo?.name).toBe("GPT-3.5"); | |
| 122 | + }); | |
| 123 | + | |
| 124 | + it("should handle switching to invalid model index", async () => { | |
| 125 | + // Initialize properly first | |
| 126 | + const mockAssistant = { | |
| 127 | + name: "Test Assistant", | |
| 128 | + version: "1.0.0", | |
| 129 | + models: [ | |
| 130 | + { | |
| 131 | + name: "GPT-4", | |
| 132 | + provider: "openai", | |
| 133 | + model: "gpt-4", | |
| 134 | + apiKey: "test-key", | |
| 135 | + }, | |
| 136 | + ], | |
| 137 | + }; | |
| 138 | + | |
| 139 | + const mockAuthConfig = { | |
| 140 | + token: "test-token", | |
| 141 | + }; | |
| 142 | + | |
| 143 | + await modelService.initialize(mockAssistant as any, mockAuthConfig as any); | |
| 144 | + | |
| 145 | + // Try to switch to invalid index | |
| 146 | + try { | |
| 147 | + await modelService.switchModel(5); // Only have 1 model at index 0 | |
| 148 | + expect(true).toBe(false); | |
| 149 | + } catch (error: any) { | |
| 150 | + expect(error.message).toContain("Invalid model index"); | |
| 151 | + } | |
| 152 | + }); | |
| 153 | + | |
| 154 | + it("should successfully switch model when service is properly initialized via container", async () => { | |
| 155 | + // This test simulates the fix where we ensure service is initialized before switching | |
| 156 | + const mockAssistant = { | |
| 157 | + name: "Test Assistant", | |
| 158 | + version: "1.0.0", | |
| 159 | + models: [ | |
| 160 | + { | |
| 161 | + name: "GPT-4", | |
| 162 | + provider: "openai", | |
| 163 | + model: "gpt-4", | |
| 164 | + apiKey: "test-key-1", | |
| 165 | + roles: ["chat"], | |
| 166 | + }, | |
| 167 | + { | |
| 168 | + name: "GPT-3.5", | |
| 169 | + provider: "openai", | |
| 170 | + model: "gpt-3.5-turbo", | |
| 171 | + apiKey: "test-key-2", | |
| 172 | + roles: ["chat"], | |
| 173 | + }, | |
| 174 | + ], | |
| 175 | + }; | |
| 176 | + | |
| 177 | + const mockAuthConfig = { | |
| 178 | + token: "test-token", | |
| 179 | + }; | |
| 180 | + | |
| 181 | + // Initialize the service properly through initialize method (which sets isInitialized) | |
| 182 | + await modelService.initialize(mockAssistant as any, mockAuthConfig as any); | |
| 183 | + | |
| 184 | + // Verify initialization | |
| 185 | + expect(modelService.isReady()).toBe(true); | |
| 186 | + expect(modelService.getCurrentModelIndex()).toBe(0); | |
| 187 | + | |
| 188 | + // Now switch should work and return success | |
| 189 | + const newState = await modelService.switchModel(1); | |
| 190 | + | |
| 191 | + // Verify switch was successful | |
| 192 | + expect(modelService.getCurrentModelIndex()).toBe(1); | |
| 193 | + expect(newState.model).toBeDefined(); | |
| 194 | + expect((newState.model as any).name).toBe("GPT-3.5"); | |
| 195 | + | |
| 196 | + // Get model info to verify success message data | |
| 197 | + const modelInfo = modelService.getModelInfo(); | |
| 198 | + expect(modelInfo).toEqual({ | |
| 199 | + provider: "openai", | |
| 200 | + name: "GPT-3.5", | |
| 201 | + }); | |
| 202 | + | |
| 203 | + // This is what the success message would use | |
| 204 | + const successMessage = `Switched to model: ${modelInfo?.provider}/${modelInfo?.name}`; | |
| 205 | + expect(successMessage).toBe("Switched to model: openai/GPT-3.5"); | |
| 206 | + }); | |
| 207 | +}); |
deleted extensions/cli/src/services/ModelService.refactored.ts +0 −260
| @@ -1,260 +0,0 @@ | ||
| 1 | -import { AssistantUnrolled, ModelConfig } from "@continuedev/config-yaml"; | |
| 2 | -import { constructLlmApi, LLMConfig } from "@continuedev/openai-adapters"; | |
| 3 | - | |
| 4 | -import { | |
| 5 | - AuthConfig, | |
| 6 | - getAccessToken, | |
| 7 | - getOrganizationId, | |
| 8 | - getModelName, | |
| 9 | -} from "../auth/workos.js"; | |
| 10 | -import { getLlmApi } from "../config.js"; | |
| 11 | -import { logger } from "../util/logger.js"; | |
| 12 | - | |
| 13 | -import { BaseService, ServiceWithDependencies } from "./BaseService.js"; | |
| 14 | -import { ModelServiceState } from "./types.js"; | |
| 15 | - | |
| 16 | -/** | |
| 17 | - * Service for managing LLM and model state | |
| 18 | - * Extends BaseService and implements ServiceWithDependencies | |
| 19 | - */ | |
| 20 | -export class ModelService | |
| 21 | - extends BaseService<ModelServiceState> | |
| 22 | - implements ServiceWithDependencies | |
| 23 | -{ | |
| 24 | - private availableModels: ModelConfig[] = []; | |
| 25 | - private assistant: AssistantUnrolled | null = null; | |
| 26 | - private authConfig: AuthConfig | null = null; | |
| 27 | - | |
| 28 | - constructor() { | |
| 29 | - super("ModelService", { | |
| 30 | - llmApi: null, | |
| 31 | - model: null, | |
| 32 | - }); | |
| 33 | - } | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * Declare dependencies on other services | |
| 37 | - */ | |
| 38 | - getDependencies(): string[] { | |
| 39 | - return ["auth", "config"]; | |
| 40 | - } | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Initialize the model service | |
| 44 | - */ | |
| 45 | - async doInitialize( | |
| 46 | - assistant: AssistantUnrolled, | |
| 47 | - authConfig: AuthConfig, | |
| 48 | - ): Promise<ModelServiceState> { | |
| 49 | - this.assistant = assistant; | |
| 50 | - this.authConfig = authConfig; | |
| 51 | - this.availableModels = (assistant.models?.filter( | |
| 52 | - (model) => | |
| 53 | - model && (model.roles?.includes("chat") || model.roles === undefined), | |
| 54 | - ) || []) as ModelConfig[]; | |
| 55 | - | |
| 56 | - // Check if we have a persisted model name and use it if valid | |
| 57 | - const persistedModelName = getModelName(authConfig); | |
| 58 | - if (persistedModelName) { | |
| 59 | - const modelIndex = this.getModelIndexByName(persistedModelName); | |
| 60 | - if (modelIndex === -1) { | |
| 61 | - // Model name not found, use default model selection | |
| 62 | - const [llmApi, model] = getLlmApi(assistant, authConfig); | |
| 63 | - return { | |
| 64 | - llmApi, | |
| 65 | - model, | |
| 66 | - }; | |
| 67 | - } else { | |
| 68 | - // Use the persisted model | |
| 69 | - const state = await this.switchModel(modelIndex); | |
| 70 | - return state; | |
| 71 | - } | |
| 72 | - } else { | |
| 73 | - // Use default model selection | |
| 74 | - const [llmApi, model] = getLlmApi(assistant, authConfig); | |
| 75 | - return { | |
| 76 | - llmApi, | |
| 77 | - model, | |
| 78 | - }; | |
| 79 | - } | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Update the model based on new config or auth changes | |
| 84 | - */ | |
| 85 | - async update( | |
| 86 | - assistant: AssistantUnrolled, | |
| 87 | - authConfig: AuthConfig, | |
| 88 | - ): Promise<ModelServiceState> { | |
| 89 | - logger.debug("Updating ModelService"); | |
| 90 | - | |
| 91 | - try { | |
| 92 | - this.assistant = assistant; | |
| 93 | - this.authConfig = authConfig; | |
| 94 | - this.availableModels = (assistant.models?.filter( | |
| 95 | - (model) => | |
| 96 | - model && (model.roles?.includes("chat") || model.roles === undefined), | |
| 97 | - ) || []) as ModelConfig[]; | |
| 98 | - | |
| 99 | - const [llmApi, model] = getLlmApi(assistant, authConfig); | |
| 100 | - | |
| 101 | - this.setState({ | |
| 102 | - llmApi, | |
| 103 | - model, | |
| 104 | - }); | |
| 105 | - | |
| 106 | - logger.debug("ModelService updated successfully", { | |
| 107 | - modelProvider: model.provider, | |
| 108 | - modelName: (model as any).name || "unnamed", | |
| 109 | - availableModels: this.availableModels.length, | |
| 110 | - }); | |
| 111 | - | |
| 112 | - return this.getState(); | |
| 113 | - } catch (error: any) { | |
| 114 | - logger.error("Failed to update ModelService:", error); | |
| 115 | - this.emit("error", error); | |
| 116 | - throw error; | |
| 117 | - } | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * Override isReady to check for required state | |
| 122 | - */ | |
| 123 | - override isReady(): boolean { | |
| 124 | - return ( | |
| 125 | - super.isReady() && | |
| 126 | - this.currentState.llmApi !== null && | |
| 127 | - this.currentState.model !== null | |
| 128 | - ); | |
| 129 | - } | |
| 130 | - | |
| 131 | - /** | |
| 132 | - * Get model information for display | |
| 133 | - */ | |
| 134 | - getModelInfo(): { provider: string; name: string } | null { | |
| 135 | - if (!this.currentState.model) { | |
| 136 | - return null; | |
| 137 | - } | |
| 138 | - | |
| 139 | - return { | |
| 140 | - provider: this.currentState.model.provider, | |
| 141 | - name: (this.currentState.model as any).name || "unnamed", | |
| 142 | - }; | |
| 143 | - } | |
| 144 | - | |
| 145 | - /** | |
| 146 | - * Get list of available chat models | |
| 147 | - */ | |
| 148 | - getAvailableChatModels(): Array<{ | |
| 149 | - provider: string; | |
| 150 | - name: string; | |
| 151 | - index: number; | |
| 152 | - }> { | |
| 153 | - return this.availableModels.map((model, index) => ({ | |
| 154 | - provider: model.provider, | |
| 155 | - name: (model as any).name || (model as any).model || "unnamed", | |
| 156 | - index, | |
| 157 | - })); | |
| 158 | - } | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * Switch to a different chat model by index | |
| 162 | - */ | |
| 163 | - async switchModel(modelIndex: number): Promise<ModelServiceState> { | |
| 164 | - if (!this.assistant || !this.authConfig) { | |
| 165 | - throw new Error("ModelService not initialized"); | |
| 166 | - } | |
| 167 | - | |
| 168 | - if (modelIndex < 0 || modelIndex >= this.availableModels.length) { | |
| 169 | - throw new Error( | |
| 170 | - `Invalid model index: ${modelIndex}. Available models: 0-${this.availableModels.length - 1}`, | |
| 171 | - ); | |
| 172 | - } | |
| 173 | - | |
| 174 | - const selectedModel = this.availableModels[modelIndex]; | |
| 175 | - logger.debug("Switching to model", { | |
| 176 | - modelIndex, | |
| 177 | - provider: selectedModel.provider, | |
| 178 | - name: (selectedModel as any).name || "unnamed", | |
| 179 | - }); | |
| 180 | - | |
| 181 | - try { | |
| 182 | - const accessToken = getAccessToken(this.authConfig); | |
| 183 | - const organizationId = getOrganizationId(this.authConfig); | |
| 184 | - | |
| 185 | - const config: LLMConfig = | |
| 186 | - selectedModel.provider === "continue-proxy" | |
| 187 | - ? { | |
| 188 | - provider: selectedModel.provider, | |
| 189 | - requestOptions: selectedModel.requestOptions, | |
| 190 | - apiBase: selectedModel.apiBase, | |
| 191 | - apiKey: accessToken ?? undefined, | |
| 192 | - env: { | |
| 193 | - apiKeyLocation: (selectedModel as any).apiKeyLocation, | |
| 194 | - orgScopeId: organizationId, | |
| 195 | - proxyUrl: undefined, | |
| 196 | - }, | |
| 197 | - } | |
| 198 | - : { | |
| 199 | - provider: selectedModel.provider as any, | |
| 200 | - apiKey: selectedModel.apiKey, | |
| 201 | - apiBase: selectedModel.apiBase, | |
| 202 | - requestOptions: selectedModel.requestOptions, | |
| 203 | - env: selectedModel.env, | |
| 204 | - }; | |
| 205 | - | |
| 206 | - const llmApi = constructLlmApi(config); | |
| 207 | - | |
| 208 | - if (!llmApi) { | |
| 209 | - throw new Error("Failed to initialize LLM with selected model"); | |
| 210 | - } | |
| 211 | - | |
| 212 | - this.setState({ | |
| 213 | - llmApi, | |
| 214 | - model: selectedModel, | |
| 215 | - }); | |
| 216 | - | |
| 217 | - logger.debug("Model switched successfully", { | |
| 218 | - modelProvider: selectedModel.provider, | |
| 219 | - modelName: (selectedModel as any).name || "unnamed", | |
| 220 | - }); | |
| 221 | - | |
| 222 | - return this.getState(); | |
| 223 | - } catch (error: any) { | |
| 224 | - logger.error("Failed to switch model:", error); | |
| 225 | - this.emit("error", error); | |
| 226 | - throw error; | |
| 227 | - } | |
| 228 | - } | |
| 229 | - | |
| 230 | - /** | |
| 231 | - * Get current model index | |
| 232 | - */ | |
| 233 | - getCurrentModelIndex(): number { | |
| 234 | - if (!this.currentState.model) { | |
| 235 | - return -1; | |
| 236 | - } | |
| 237 | - | |
| 238 | - return this.availableModels.findIndex( | |
| 239 | - (model) => | |
| 240 | - model.provider === this.currentState.model?.provider && | |
| 241 | - (model as any).name === (this.currentState.model as any).name, | |
| 242 | - ); | |
| 243 | - } | |
| 244 | - | |
| 245 | - /** | |
| 246 | - * Get model index by name and provider | |
| 247 | - */ | |
| 248 | - getModelIndexByName(modelName: string, provider?: string): number { | |
| 249 | - return this.availableModels.findIndex((model) => { | |
| 250 | - const name = (model as any).name || (model as any).model; | |
| 251 | - const nameMatches = name === modelName; | |
| 252 | - | |
| 253 | - if (provider) { | |
| 254 | - return nameMatches && model.provider === provider; | |
| 255 | - } | |
| 256 | - | |
| 257 | - return nameMatches; | |
| 258 | - }); | |
| 259 | - } | |
| 260 | -} |
modified extensions/cli/src/services/ModelService.test.ts +6 −0
| @@ -74,6 +74,8 @@ | ||
| 74 | 74 | expect(state).toEqual({ |
| 75 | 75 | llmApi: mockLlmApi, |
| 76 | 76 | model: mockAssistant.models![0], |
| 77 | + assistant: mockAssistant, | |
| 78 | + authConfig: mockAuthConfig, | |
| 77 | 79 | }); |
| 78 | 80 | }); |
| 79 | 81 | |
| @@ -135,6 +137,8 @@ | ||
| 135 | 137 | expect(state).toEqual({ |
| 136 | 138 | llmApi: newLlmApi, |
| 137 | 139 | model: newAssistant.models![0], |
| 140 | + assistant: newAssistant, | |
| 141 | + authConfig: mockAuthConfig, | |
| 138 | 142 | }); |
| 139 | 143 | }); |
| 140 | 144 | }); |
| @@ -157,6 +161,8 @@ | ||
| 157 | 161 | expect(state).toEqual({ |
| 158 | 162 | llmApi: newLlmApi, |
| 159 | 163 | model: mockAssistant.models![1], |
| 164 | + assistant: mockAssistant, | |
| 165 | + authConfig: mockAuthConfig, | |
| 160 | 166 | }); |
| 161 | 167 | expect(service.getState()).toEqual(state); |
| 162 | 168 | }); |
modified extensions/cli/src/services/ModelService.ts +146 −17
| @@ -29,6 +29,8 @@ | ||
| 29 | 29 | super("ModelService", { |
| 30 | 30 | llmApi: null, |
| 31 | 31 | model: null, |
| 32 | + assistant: null, | |
| 33 | + authConfig: null, | |
| 32 | 34 | }); |
| 33 | 35 | } |
| 34 | 36 | |
| @@ -46,6 +48,12 @@ | ||
| 46 | 48 | assistant: AssistantUnrolled, |
| 47 | 49 | authConfig: AuthConfig, |
| 48 | 50 | ): Promise<ModelServiceState> { |
| 51 | + logger.debug("ModelService.doInitialize called", { | |
| 52 | + hasAssistant: !!assistant, | |
| 53 | + hasAuthConfig: !!authConfig, | |
| 54 | + assistantModelsCount: assistant?.models?.length || 0, | |
| 55 | + }); | |
| 56 | + | |
| 49 | 57 | this.assistant = assistant; |
| 50 | 58 | this.authConfig = authConfig; |
| 51 | 59 | this.availableModels = (assistant.models?.filter( |
| @@ -56,18 +64,67 @@ | ||
| 56 | 64 | // Check if we have a persisted model name and use it if valid |
| 57 | 65 | const persistedModelName = getModelName(authConfig); |
| 58 | 66 | if (persistedModelName) { |
| 59 | - const modelIndex = this.getModelIndexByName(persistedModelName); | |
| 67 | + // During initialization, we need to check against availableModels directly | |
| 68 | + const modelIndex = this.availableModels.findIndex((model) => { | |
| 69 | + const name = (model as any).name || (model as any).model; | |
| 70 | + return name === persistedModelName; | |
| 71 | + }); | |
| 60 | 72 | if (modelIndex === -1) { |
| 61 | 73 | // Model name not found, use default model selection |
| 62 | 74 | const [llmApi, model] = getLlmApi(assistant, authConfig); |
| 63 | 75 | return { |
| 64 | 76 | llmApi, |
| 65 | 77 | model, |
| 78 | + assistant, | |
| 79 | + authConfig, | |
| 66 | 80 | }; |
| 67 | 81 | } else { |
| 68 | - // Use the persisted model | |
| 69 | - const state = await this.switchModel(modelIndex); | |
| 70 | - return state; | |
| 82 | + // Use the persisted model - but we need to handle initialization specially | |
| 83 | + // During init, currentState isn't set yet, so switchModel would fail | |
| 84 | + // Instead, we'll manually switch here | |
| 85 | + const selectedModel = this.availableModels[modelIndex]; | |
| 86 | + logger.debug("Using persisted model during initialization", { | |
| 87 | + modelIndex, | |
| 88 | + provider: selectedModel.provider, | |
| 89 | + name: (selectedModel as any).name || "unnamed", | |
| 90 | + }); | |
| 91 | + | |
| 92 | + const accessToken = getAccessToken(authConfig); | |
| 93 | + const organizationId = getOrganizationId(authConfig); | |
| 94 | + | |
| 95 | + const config: LLMConfig = | |
| 96 | + selectedModel.provider === "continue-proxy" | |
| 97 | + ? { | |
| 98 | + provider: selectedModel.provider, | |
| 99 | + requestOptions: selectedModel.requestOptions, | |
| 100 | + apiBase: selectedModel.apiBase, | |
| 101 | + apiKey: accessToken ?? undefined, | |
| 102 | + env: { | |
| 103 | + apiKeyLocation: (selectedModel as any).apiKeyLocation, | |
| 104 | + orgScopeId: organizationId, | |
| 105 | + proxyUrl: undefined, | |
| 106 | + }, | |
| 107 | + } | |
| 108 | + : { | |
| 109 | + provider: selectedModel.provider as any, | |
| 110 | + apiKey: selectedModel.apiKey, | |
| 111 | + apiBase: selectedModel.apiBase, | |
| 112 | + requestOptions: selectedModel.requestOptions, | |
| 113 | + env: selectedModel.env, | |
| 114 | + }; | |
| 115 | + | |
| 116 | + const llmApi = constructLlmApi(config); | |
| 117 | + | |
| 118 | + if (!llmApi) { | |
| 119 | + throw new Error("Failed to initialize LLM with persisted model"); | |
| 120 | + } | |
| 121 | + | |
| 122 | + return { | |
| 123 | + llmApi, | |
| 124 | + model: selectedModel, | |
| 125 | + assistant, | |
| 126 | + authConfig, | |
| 127 | + }; | |
| 71 | 128 | } |
| 72 | 129 | } else { |
| 73 | 130 | // Use default model selection |
| @@ -75,6 +132,8 @@ | ||
| 75 | 132 | return { |
| 76 | 133 | llmApi, |
| 77 | 134 | model, |
| 135 | + assistant, | |
| 136 | + authConfig, | |
| 78 | 137 | }; |
| 79 | 138 | } |
| 80 | 139 | } |
| @@ -89,6 +148,7 @@ | ||
| 89 | 148 | logger.debug("Updating ModelService"); |
| 90 | 149 | |
| 91 | 150 | try { |
| 151 | + // Update instance properties for backward compatibility | |
| 92 | 152 | this.assistant = assistant; |
| 93 | 153 | this.authConfig = authConfig; |
| 94 | 154 | this.availableModels = (assistant.models?.filter( |
| @@ -98,9 +158,12 @@ | ||
| 98 | 158 | |
| 99 | 159 | const [llmApi, model] = getLlmApi(assistant, authConfig); |
| 100 | 160 | |
| 161 | + // Ensure state has all necessary data | |
| 101 | 162 | this.setState({ |
| 102 | 163 | llmApi, |
| 103 | 164 | model, |
| 165 | + assistant, | |
| 166 | + authConfig, | |
| 104 | 167 | }); |
| 105 | 168 | |
| 106 | 169 | logger.debug("ModelService updated successfully", { |
| @@ -150,7 +213,19 @@ | ||
| 150 | 213 | name: string; |
| 151 | 214 | index: number; |
| 152 | 215 | }> { |
| 153 | - return this.availableModels.map((model, index) => ({ | |
| 216 | + // Get assistant from state to ensure we have the latest data | |
| 217 | + const { assistant } = this.getState(); | |
| 218 | + if (!assistant || !assistant.models) { | |
| 219 | + return []; | |
| 220 | + } | |
| 221 | + | |
| 222 | + // Filter for chat models | |
| 223 | + const chatModels = (assistant.models.filter( | |
| 224 | + (model) => | |
| 225 | + model && (model.roles?.includes("chat") || model.roles === undefined), | |
| 226 | + ) || []) as ModelConfig[]; | |
| 227 | + | |
| 228 | + return chatModels.map((model, index) => ({ | |
| 154 | 229 | provider: model.provider, |
| 155 | 230 | name: (model as any).name || (model as any).model || "unnamed", |
| 156 | 231 | index, |
| @@ -161,17 +236,51 @@ | ||
| 161 | 236 | * Switch to a different chat model by index |
| 162 | 237 | */ |
| 163 | 238 | async switchModel(modelIndex: number): Promise<ModelServiceState> { |
| 164 | - if (!this.assistant || !this.authConfig) { | |
| 165 | - throw new Error("ModelService not initialized"); | |
| 239 | + // Get assistant and authConfig from state, but fall back to instance properties | |
| 240 | + // This is needed during initialization when state isn't set yet | |
| 241 | + const stateValues = this.getState(); | |
| 242 | + const assistant = stateValues.assistant || this.assistant; | |
| 243 | + const authConfig = stateValues.authConfig || this.authConfig; | |
| 244 | + | |
| 245 | + // Debug logging to understand the state | |
| 246 | + logger.debug("switchModel: Checking state", { | |
| 247 | + hasStateAssistant: !!stateValues.assistant, | |
| 248 | + hasStateAuthConfig: !!stateValues.authConfig, | |
| 249 | + hasInstanceAssistant: !!this.assistant, | |
| 250 | + hasInstanceAuthConfig: !!this.authConfig, | |
| 251 | + isInitialized: this.isReady(), | |
| 252 | + isReady: this.isReady(), | |
| 253 | + modelIndex, | |
| 254 | + }); | |
| 255 | + | |
| 256 | + if (!assistant) { | |
| 257 | + logger.error("switchModel: Missing assistant data", { | |
| 258 | + assistant: !!assistant, | |
| 259 | + authConfig: !!authConfig, | |
| 260 | + stateKeys: Object.keys(stateValues), | |
| 261 | + currentState: { | |
| 262 | + hasLlmApi: !!stateValues.llmApi, | |
| 263 | + hasModel: !!stateValues.model, | |
| 264 | + hasAssistant: !!stateValues.assistant, | |
| 265 | + hasAuthConfig: !!stateValues.authConfig, | |
| 266 | + }, | |
| 267 | + }); | |
| 268 | + throw new Error("ModelService not initialized - assistant data missing"); | |
| 166 | 269 | } |
| 167 | 270 | |
| 168 | - if (modelIndex < 0 || modelIndex >= this.availableModels.length) { | |
| 271 | + // Get available models from assistant in state | |
| 272 | + const availableModels = (assistant.models?.filter( | |
| 273 | + (model) => | |
| 274 | + model && (model.roles?.includes("chat") || model.roles === undefined), | |
| 275 | + ) || []) as ModelConfig[]; | |
| 276 | + | |
| 277 | + if (modelIndex < 0 || modelIndex >= availableModels.length) { | |
| 169 | 278 | throw new Error( |
| 170 | - `Invalid model index: ${modelIndex}. Available models: 0-${this.availableModels.length - 1}`, | |
| 279 | + `Invalid model index: ${modelIndex}. Available models: 0-${availableModels.length - 1}`, | |
| 171 | 280 | ); |
| 172 | 281 | } |
| 173 | 282 | |
| 174 | - const selectedModel = this.availableModels[modelIndex]; | |
| 283 | + const selectedModel = availableModels[modelIndex]; | |
| 175 | 284 | logger.debug("Switching to model", { |
| 176 | 285 | modelIndex, |
| 177 | 286 | provider: selectedModel.provider, |
| @@ -179,8 +288,8 @@ | ||
| 179 | 288 | }); |
| 180 | 289 | |
| 181 | 290 | try { |
| 182 | - const accessToken = getAccessToken(this.authConfig); | |
| 183 | - const organizationId = getOrganizationId(this.authConfig); | |
| 291 | + const accessToken = getAccessToken(authConfig); | |
| 292 | + const organizationId = getOrganizationId(authConfig); | |
| 184 | 293 | |
| 185 | 294 | const config: LLMConfig = |
| 186 | 295 | selectedModel.provider === "continue-proxy" |
| @@ -212,6 +321,8 @@ | ||
| 212 | 321 | this.setState({ |
| 213 | 322 | llmApi, |
| 214 | 323 | model: selectedModel, |
| 324 | + assistant, | |
| 325 | + authConfig, | |
| 215 | 326 | }); |
| 216 | 327 | |
| 217 | 328 | logger.debug("Model switched successfully", { |
| @@ -231,14 +342,21 @@ | ||
| 231 | 342 | * Get current model index |
| 232 | 343 | */ |
| 233 | 344 | getCurrentModelIndex(): number { |
| 234 | - if (!this.currentState.model) { | |
| 345 | + const state = this.getState(); | |
| 346 | + if (!state.model || !state.assistant) { | |
| 235 | 347 | return -1; |
| 236 | 348 | } |
| 237 | 349 | |
| 238 | - return this.availableModels.findIndex( | |
| 350 | + // Get available models from state | |
| 351 | + const availableModels = (state.assistant.models?.filter( | |
| 352 | + (model) => | |
| 353 | + model && (model.roles?.includes("chat") || model.roles === undefined), | |
| 354 | + ) || []) as ModelConfig[]; | |
| 355 | + | |
| 356 | + return availableModels.findIndex( | |
| 239 | 357 | (model) => |
| 240 | - model.provider === this.currentState.model?.provider && | |
| 241 | - (model as any).name === (this.currentState.model as any).name, | |
| 358 | + model.provider === state.model?.provider && | |
| 359 | + (model as any).name === (state.model as any).name, | |
| 242 | 360 | ); |
| 243 | 361 | } |
| 244 | 362 | |
| @@ -246,7 +364,18 @@ | ||
| 246 | 364 | * Get model index by name and provider |
| 247 | 365 | */ |
| 248 | 366 | getModelIndexByName(modelName: string, provider?: string): number { |
| 249 | - return this.availableModels.findIndex((model) => { | |
| 367 | + const state = this.getState(); | |
| 368 | + if (!state.assistant) { | |
| 369 | + return -1; | |
| 370 | + } | |
| 371 | + | |
| 372 | + // Get available models from state | |
| 373 | + const availableModels = (state.assistant.models?.filter( | |
| 374 | + (model) => | |
| 375 | + model && (model.roles?.includes("chat") || model.roles === undefined), | |
| 376 | + ) || []) as ModelConfig[]; | |
| 377 | + | |
| 378 | + return availableModels.findIndex((model) => { | |
| 250 | 379 | const name = (model as any).name || (model as any).model; |
| 251 | 380 | const nameMatches = name === modelName; |
| 252 | 381 |
modified extensions/cli/src/services/types.ts +2 −0
| @@ -52,6 +52,8 @@ | ||
| 52 | 52 | export interface ModelServiceState { |
| 53 | 53 | llmApi: BaseLlmApi | null; |
| 54 | 54 | model: ModelConfig | null; |
| 55 | + assistant: AssistantUnrolled | null; | |
| 56 | + authConfig: AuthConfig | null; | |
| 55 | 57 | } |
| 56 | 58 | |
| 57 | 59 | export type MCPServerStatus = "idle" | "connecting" | "connected" | "error"; |
modified extensions/cli/src/ui/IntroMessage.test.tsx +9 −13
| @@ -51,14 +51,14 @@ | ||
| 51 | 51 | |
| 52 | 52 | // Count occurrences of "nate/spanish" in the output |
| 53 | 53 | const matches = output?.match(/nate\/spanish/g) || []; |
| 54 | - | |
| 54 | + | |
| 55 | 55 | // Should appear exactly once, not twice |
| 56 | 56 | expect(matches.length).toBe(1); |
| 57 | - | |
| 57 | + | |
| 58 | 58 | // Verify the slug is shown, not the content |
| 59 | 59 | expect(output).toContain("nate/spanish"); |
| 60 | 60 | expect(output).not.toContain("Always respond in Spanish"); |
| 61 | - | |
| 61 | + | |
| 62 | 62 | // Verify plain string rule is shown as-is |
| 63 | 63 | expect(output).toContain("Always be helpful"); |
| 64 | 64 | }); |
| @@ -66,11 +66,7 @@ | ||
| 66 | 66 | it("should display rules from config correctly", () => { |
| 67 | 67 | const config: AssistantUnrolled = { |
| 68 | 68 | name: "Test Assistant", |
| 69 | - rules: [ | |
| 70 | - "Rule 1", | |
| 71 | - { name: "Rule 2" }, | |
| 72 | - "Rule 3", | |
| 73 | - ], | |
| 69 | + rules: ["Rule 1", { name: "Rule 2" }, "Rule 3"], | |
| 74 | 70 | } as any; |
| 75 | 71 | |
| 76 | 72 | const { lastFrame } = render( |
| @@ -134,15 +130,15 @@ | ||
| 134 | 130 | const config: AssistantUnrolled = { |
| 135 | 131 | name: "Test Assistant", |
| 136 | 132 | rules: [ |
| 137 | - { | |
| 138 | - name: "org/complex-rule", | |
| 133 | + { | |
| 134 | + name: "org/complex-rule", | |
| 139 | 135 | rule: `--- |
| 140 | 136 | alwaysApply: true |
| 141 | 137 | --- |
| 142 | 138 | |
| 143 | 139 | This is a very long rule content that should not be displayed. |
| 144 | 140 | It contains multiple lines and complex formatting. |
| 145 | -The UI should only show the slug 'org/complex-rule'.` | |
| 141 | +The UI should only show the slug 'org/complex-rule'.`, | |
| 146 | 142 | }, |
| 147 | 143 | { name: "another/rule", rule: "Short content" }, |
| 148 | 144 | ], |
| @@ -161,10 +157,10 @@ | ||
| 161 | 157 | // Should show slugs only |
| 162 | 158 | expect(output).toContain("org/complex-rule"); |
| 163 | 159 | expect(output).toContain("another/rule"); |
| 164 | - | |
| 160 | + | |
| 165 | 161 | // Should NOT show the rule content |
| 166 | 162 | expect(output).not.toContain("This is a very long rule"); |
| 167 | 163 | expect(output).not.toContain("alwaysApply"); |
| 168 | 164 | expect(output).not.toContain("Short content"); |
| 169 | 165 | }); |
| 170 | -}); | |
| No newline at the end of the file | ||
| 166 | +}); |
added extensions/cli/test-fixtures/model-switch-test-config.yaml +18 −0
| @@ -0,0 +1,18 @@ | ||
| 1 | +name: Model Switch Test Config | |
| 2 | +version: 1.0.0 | |
| 3 | + | |
| 4 | +models: | |
| 5 | + - name: GPT-4 | |
| 6 | + provider: openai | |
| 7 | + model: gpt-4 | |
| 8 | + apiKey: test-api-key-1 | |
| 9 | + | |
| 10 | + - name: GPT-3.5 | |
| 11 | + provider: openai | |
| 12 | + model: gpt-3.5-turbo | |
| 13 | + apiKey: test-api-key-2 | |
| 14 | + | |
| 15 | + - name: Claude 3 | |
| 16 | + provider: anthropic | |
| 17 | + model: claude-3-opus | |
| 18 | + apiKey: test-api-key-3 | |
| No newline at the end of the file |
modified package-lock.json +1 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | { |
| 2 | - "name": "wt-nate-4", | |
| 2 | + "name": "wt-mcp-flag", | |
| 3 | 3 | "lockfileVersion": 3, |
| 4 | 4 | "requires": true, |
| 5 | 5 | "packages": { |