diff --git a/extensions/cli/src/CLIPlatformClient.test.ts b/extensions/cli/src/CLIPlatformClient.test.ts new file mode 100644 index 000000000..b54ab972b --- /dev/null +++ b/extensions/cli/src/CLIPlatformClient.test.ts @@ -0,0 +1,377 @@ +import * as fs from "node:fs"; + +import { FQSN, SecretResult, SecretType } from "@continuedev/config-yaml"; +import { beforeEach, describe, expect, it, vi } from "vitest"; + +import { CLIPlatformClient } from "./CLIPlatformClient.js"; + +// Mock fs module +vi.mock("node:fs", () => ({ + existsSync: vi.fn(), + readFileSync: vi.fn(), +})); + +// Mock env module +vi.mock("./env.js", () => ({ + env: { + continueHome: "/home/user/.continue", + }, +})); + +describe("CLIPlatformClient", () => { + let mockApiClient: { + syncSecrets: ReturnType; + }; + + beforeEach(() => { + vi.resetAllMocks(); + mockApiClient = { + syncSecrets: vi.fn(), + }; + // Reset process.env mocks + vi.unstubAllEnvs(); + }); + + describe("resolveFQSNs", () => { + it("returns empty array for empty input", async () => { + const client = new CLIPlatformClient(null, mockApiClient as any); + const results = await client.resolveFQSNs([]); + expect(results).toEqual([]); + expect(mockApiClient.syncSecrets).not.toHaveBeenCalled(); + }); + + it("prioritizes local env over API results", async () => { + const fqsn: FQSN = { + packageSlugs: [ + { ownerSlug: "anthropic", packageSlug: "claude-sonnet" }, + ], + secretName: "ANTHROPIC_API_KEY", + }; + + // Local env has the secret + vi.stubEnv("ANTHROPIC_API_KEY", "local-api-key"); + + const client = new CLIPlatformClient("org-123", mockApiClient as any); + const results = await client.resolveFQSNs([fqsn]); + + // Should not call API since local env has the secret + expect(mockApiClient.syncSecrets).not.toHaveBeenCalled(); + + expect(results).toHaveLength(1); + expect(results[0]).toMatchObject({ + found: true, + value: "local-api-key", + secretLocation: { + secretType: SecretType.LocalEnv, + secretName: "ANTHROPIC_API_KEY", + }, + }); + }); + + it("falls back to API when local env does not have the secret", async () => { + const fqsn: FQSN = { + packageSlugs: [ + { ownerSlug: "anthropic", packageSlug: "claude-sonnet" }, + ], + secretName: "ANTHROPIC_API_KEY", + }; + + // Ensure no local env secret exists + vi.stubEnv("ANTHROPIC_API_KEY", undefined as unknown as string); + vi.mocked(fs.existsSync).mockReturnValue(false); + + // API returns a user secret with value + const apiResult: SecretResult = { + found: true, + fqsn, + value: "api-secret-value", + secretLocation: { + secretType: SecretType.User, + userSlug: "testuser", + secretName: "ANTHROPIC_API_KEY", + }, + }; + mockApiClient.syncSecrets.mockResolvedValue([apiResult]); + + const client = new CLIPlatformClient("org-123", mockApiClient as any); + const results = await client.resolveFQSNs([fqsn]); + + expect(mockApiClient.syncSecrets).toHaveBeenCalledWith({ + syncSecretsRequest: { + fqsns: [fqsn], + orgScopeId: "org-123", + }, + }); + + expect(results).toHaveLength(1); + expect(results[0]).toEqual(apiResult); + }); + + it("keeps API results with secretLocation but no value (models_add_on)", async () => { + const fqsn: FQSN = { + packageSlugs: [ + { ownerSlug: "anthropic", packageSlug: "claude-sonnet" }, + ], + secretName: "ANTHROPIC_API_KEY", + }; + + // Ensure no local env secret exists + vi.stubEnv("ANTHROPIC_API_KEY", undefined as unknown as string); + vi.mocked(fs.existsSync).mockReturnValue(false); + + // API returns a models_add_on result (has secretLocation but no value) + const apiResult: SecretResult = { + found: true, + fqsn, + secretLocation: { + secretType: SecretType.ModelsAddOn, + blockSlug: { ownerSlug: "anthropic", packageSlug: "claude-sonnet" }, + secretName: "ANTHROPIC_API_KEY", + }, + }; + mockApiClient.syncSecrets.mockResolvedValue([apiResult]); + + const client = new CLIPlatformClient("org-123", mockApiClient as any); + const results = await client.resolveFQSNs([fqsn]); + + expect(results).toHaveLength(1); + // The result should be kept even though it has no value + expect(results[0]).toEqual(apiResult); + expect(results[0]).not.toHaveProperty("value"); + expect((results[0] as any).secretLocation.secretType).toBe( + SecretType.ModelsAddOn, + ); + }); + + it("keeps API results with secretLocation but no value (free_trial)", async () => { + const fqsn: FQSN = { + packageSlugs: [{ ownerSlug: "openai", packageSlug: "gpt-4" }], + secretName: "OPENAI_API_KEY", + }; + + // Ensure no local env secret exists + vi.stubEnv("OPENAI_API_KEY", undefined as unknown as string); + vi.mocked(fs.existsSync).mockReturnValue(false); + + // API returns a free_trial result + const apiResult: SecretResult = { + found: true, + fqsn, + secretLocation: { + secretType: SecretType.FreeTrial, + blockSlug: { ownerSlug: "openai", packageSlug: "gpt-4" }, + secretName: "OPENAI_API_KEY", + }, + }; + mockApiClient.syncSecrets.mockResolvedValue([apiResult]); + + const client = new CLIPlatformClient(null, mockApiClient as any); + const results = await client.resolveFQSNs([fqsn]); + + expect(results).toHaveLength(1); + expect(results[0]).toEqual(apiResult); + expect((results[0] as any).secretLocation.secretType).toBe( + SecretType.FreeTrial, + ); + }); + + it("handles mixed results - some local, some from API", async () => { + const fqsn1: FQSN = { + packageSlugs: [ + { ownerSlug: "anthropic", packageSlug: "claude-sonnet" }, + ], + secretName: "ANTHROPIC_API_KEY", + }; + const fqsn2: FQSN = { + packageSlugs: [{ ownerSlug: "openai", packageSlug: "gpt-4" }], + secretName: "OPENAI_API_KEY", + }; + + // First secret is in local env + vi.stubEnv("ANTHROPIC_API_KEY", "local-anthropic-key"); + // Ensure second secret is not in local env + vi.stubEnv("OPENAI_API_KEY", undefined as unknown as string); + vi.mocked(fs.existsSync).mockReturnValue(false); + + // API returns models_add_on for the second secret + const apiResult: SecretResult = { + found: true, + fqsn: fqsn2, + secretLocation: { + secretType: SecretType.ModelsAddOn, + blockSlug: { ownerSlug: "openai", packageSlug: "gpt-4" }, + secretName: "OPENAI_API_KEY", + }, + }; + mockApiClient.syncSecrets.mockResolvedValue([apiResult]); + + const client = new CLIPlatformClient("org-123", mockApiClient as any); + const results = await client.resolveFQSNs([fqsn1, fqsn2]); + + // API should only be called for the unresolved secret + expect(mockApiClient.syncSecrets).toHaveBeenCalledWith({ + syncSecretsRequest: { + fqsns: [fqsn2], // Only fqsn2 should be sent to API + orgScopeId: "org-123", + }, + }); + + expect(results).toHaveLength(2); + + // First result from local env + expect(results[0]).toMatchObject({ + found: true, + value: "local-anthropic-key", + secretLocation: { + secretType: SecretType.LocalEnv, + }, + }); + + // Second result from API (models_add_on, no value) + expect(results[1]).toEqual(apiResult); + }); + + it("handles API errors gracefully", async () => { + const fqsn: FQSN = { + packageSlugs: [{ ownerSlug: "test", packageSlug: "model" }], + secretName: "API_KEY", + }; + + // No local env secret + vi.mocked(fs.existsSync).mockReturnValue(false); + + // API throws an error + mockApiClient.syncSecrets.mockRejectedValue(new Error("Network error")); + + const client = new CLIPlatformClient(null, mockApiClient as any); + const results = await client.resolveFQSNs([fqsn]); + + // Should return undefined for the unresolved secret + expect(results).toHaveLength(1); + expect(results[0]).toBeUndefined(); + }); + + it("does not call API when all secrets are found locally", async () => { + const fqsn1: FQSN = { + packageSlugs: [], + secretName: "SECRET_1", + }; + const fqsn2: FQSN = { + packageSlugs: [], + secretName: "SECRET_2", + }; + + // Both secrets are in local env + vi.stubEnv("SECRET_1", "value1"); + vi.stubEnv("SECRET_2", "value2"); + + const client = new CLIPlatformClient(null, mockApiClient as any); + const results = await client.resolveFQSNs([fqsn1, fqsn2]); + + // API should not be called at all + expect(mockApiClient.syncSecrets).not.toHaveBeenCalled(); + + expect(results).toHaveLength(2); + expect(results[0]).toMatchObject({ found: true, value: "value1" }); + expect(results[1]).toMatchObject({ found: true, value: "value2" }); + }); + + it("returns undefined for secrets not found anywhere", async () => { + const fqsn: FQSN = { + packageSlugs: [{ ownerSlug: "unknown", packageSlug: "model" }], + secretName: "UNKNOWN_KEY", + }; + + // No local env secret + vi.mocked(fs.existsSync).mockReturnValue(false); + + // API returns not found + const apiResult: SecretResult = { + found: false, + fqsn, + secretLocation: { + secretType: SecretType.NotFound, + secretName: "UNKNOWN_KEY", + }, + }; + mockApiClient.syncSecrets.mockResolvedValue([apiResult]); + + const client = new CLIPlatformClient(null, mockApiClient as any); + const results = await client.resolveFQSNs([fqsn]); + + expect(results).toHaveLength(1); + // Not found results should not be kept (found: false) + expect(results[0]).toBeUndefined(); + }); + + it("resolves secrets from process.env with file-based FQSN format (empty package slugs)", async () => { + // This tests the FQSN format used for slug injected blocks + // e.g., ${{ secrets.//ANTHROPIC_API_KEY }} which decodes to: + // { packageSlugs: [{ ownerSlug: "", packageSlug: "" }], secretName: "ANTHROPIC_API_KEY" } + const fqsn: FQSN = { + packageSlugs: [{ ownerSlug: "", packageSlug: "" }], + secretName: "ANTHROPIC_API_KEY", + }; + + // Secret is in process.env + vi.stubEnv("ANTHROPIC_API_KEY", "my-local-api-key"); + + const client = new CLIPlatformClient(null, mockApiClient as any); + const results = await client.resolveFQSNs([fqsn]); + + // Should not call API since local env has the secret + expect(mockApiClient.syncSecrets).not.toHaveBeenCalled(); + + expect(results).toHaveLength(1); + expect(results[0]).toMatchObject({ + found: true, + value: "my-local-api-key", + fqsn: { + packageSlugs: [{ ownerSlug: "", packageSlug: "" }], + secretName: "ANTHROPIC_API_KEY", + }, + secretLocation: { + secretType: SecretType.LocalEnv, + secretName: "ANTHROPIC_API_KEY", + }, + }); + }); + + it("falls back to API for file-based FQSN when not in local env", async () => { + // File-based FQSN with empty package slugs + const fqsn: FQSN = { + packageSlugs: [{ ownerSlug: "", packageSlug: "" }], + secretName: "OPENAI_API_KEY", + }; + + // Ensure no local env secret exists + vi.stubEnv("OPENAI_API_KEY", undefined as unknown as string); + vi.mocked(fs.existsSync).mockReturnValue(false); + + // API returns models_add_on (the server can still resolve this) + const apiResult: SecretResult = { + found: true, + fqsn, + secretLocation: { + secretType: SecretType.ModelsAddOn, + blockSlug: { ownerSlug: "", packageSlug: "" }, + secretName: "OPENAI_API_KEY", + }, + }; + mockApiClient.syncSecrets.mockResolvedValue([apiResult]); + + const client = new CLIPlatformClient("org-123", mockApiClient as any); + const results = await client.resolveFQSNs([fqsn]); + + expect(mockApiClient.syncSecrets).toHaveBeenCalledWith({ + syncSecretsRequest: { + fqsns: [fqsn], + orgScopeId: "org-123", + }, + }); + + expect(results).toHaveLength(1); + expect(results[0]).toEqual(apiResult); + }); + }); +}); diff --git a/extensions/cli/src/CLIPlatformClient.ts b/extensions/cli/src/CLIPlatformClient.ts index 2f0046397..c81e89b57 100644 --- a/extensions/cli/src/CLIPlatformClient.ts +++ b/extensions/cli/src/CLIPlatformClient.ts @@ -96,38 +96,44 @@ export class CLIPlatformClient implements PlatformClient { undefined, ); - // Try to resolve secrets through the API client first - try { - const apiResults: any = await this.apiClient.syncSecrets({ - syncSecretsRequest: { - fqsns, - orgScopeId: this.orgScopeId, - }, - }); - - // Merge API results into our results array - but only if they have an actual value - // API can return found=true for secrets that exist but aren't accessible locally - for (let i = 0; i < apiResults.length; i++) { - if (apiResults[i]?.found && "value" in apiResults[i]) { - results[i] = apiResults[i]; - } + // First, check local env files for secrets (highest priority for CLI) + // This allows users to override any secret with a local environment variable + for (let i = 0; i < fqsns.length; i++) { + const secretFromEnv = this.findSecretInLocalEnvFiles(fqsns[i]); + if (secretFromEnv?.found) { + results[i] = secretFromEnv; } - } catch (error) { - console.warn( - `Error resolving FQSNs through API: ${ - error instanceof Error ? error.message : String(error) - }`, - ); } - // For any secret without a value (not just "not found"), look in local .env files - // This allows environment variables to override org/package secrets that aren't accessible - for (let i = 0; i < fqsns.length; i++) { - if (!results[i] || !("value" in results[i]!)) { - const secretFromEnv = this.findSecretInLocalEnvFiles(fqsns[i]); - if (secretFromEnv?.found) { - results[i] = secretFromEnv; + // For secrets not found locally, try to resolve through the API + const unresolvedIndices = results + .map((r, i) => (r === undefined ? i : -1)) + .filter((i) => i !== -1); + + if (unresolvedIndices.length > 0) { + try { + const unresolvedFqsns = unresolvedIndices.map((i) => fqsns[i]); + const apiResults: any = await this.apiClient.syncSecrets({ + syncSecretsRequest: { + fqsns: unresolvedFqsns, + orgScopeId: this.orgScopeId, + }, + }); + + // Merge API results - keep all found results (with or without value) + // Results with value = user secrets (rendered directly) + // Results with secretLocation but no value = org/package/models_add_on/free_trial (will be proxied) + for (let j = 0; j < apiResults.length; j++) { + if (apiResults[j]?.found) { + results[unresolvedIndices[j]] = apiResults[j]; + } } + } catch (error) { + console.warn( + `Error resolving FQSNs through API: ${ + error instanceof Error ? error.message : String(error) + }`, + ); } } diff --git a/packages/config-yaml/src/load/injectBlocks.test.ts b/packages/config-yaml/src/load/injectBlocks.test.ts index 14feb97dc..f8623be0f 100644 --- a/packages/config-yaml/src/load/injectBlocks.test.ts +++ b/packages/config-yaml/src/load/injectBlocks.test.ts @@ -75,10 +75,11 @@ models: expect(result.config!.models).toBeDefined(); expect(result.config!.models!.length).toBe(1); - // Check that inputs were converted to secrets in the model + // Check that inputs were converted to secrets and then to FQSNs + // For file-type blocks, the FQSN format is: ${{ secrets.//secretName }} const model = result.config!.models![0]!; - expect(model.apiKey).toBe("${{ secrets.openaiKey }}"); - expect(model.apiBase).toBe("${{ secrets.customEndpoint }}"); + expect(model.apiKey).toBe("${{ secrets.//openaiKey }}"); + expect(model.apiBase).toBe("${{ secrets.//customEndpoint }}"); expect(model.name).toBe("gpt-4"); expect(model.provider).toBe("openai"); expect(model.model).toBe("gpt-4"); @@ -126,14 +127,16 @@ rules: expect(result.config!.rules).toBeDefined(); expect(result.config!.rules!.length).toBe(1); - // Check that input was converted to secret in the rule + // Check that input was converted to secret and then to FQSN // Note: string rules get wrapped in objects with sourceFile when injected const rule = result.config!.rules![0]!; if (typeof rule === "string") { - expect(rule).toBe("Use ${{ secrets.codeStyle }} formatting consistently"); + expect(rule).toBe( + "Use ${{ secrets.//codeStyle }} formatting consistently", + ); } else { expect(rule.rule).toBe( - "Use ${{ secrets.codeStyle }} formatting consistently", + "Use ${{ secrets.//codeStyle }} formatting consistently", ); } @@ -182,11 +185,11 @@ docs: expect(result.config!.docs).toBeDefined(); expect(result.config!.docs!.length).toBe(1); - // Check that inputs were converted to secrets in the doc config + // Check that inputs were converted to secrets and then to FQSNs const doc = result.config!.docs![0]!; - expect(doc.startUrl).toBe("${{ secrets.docsBaseUrl }}/api"); - expect(doc.rootUrl).toBe("${{ secrets.docsBaseUrl }}"); - expect(doc.faviconUrl).toBe("${{ secrets.docsBaseUrl }}/favicon.ico"); + expect(doc.startUrl).toBe("${{ secrets.//docsBaseUrl }}/api"); + expect(doc.rootUrl).toBe("${{ secrets.//docsBaseUrl }}"); + expect(doc.faviconUrl).toBe("${{ secrets.//docsBaseUrl }}/favicon.ico"); expect(doc.name).toBe("project-docs"); // Check that no input variables remain @@ -233,10 +236,10 @@ prompts: expect(result.config!.prompts).toBeDefined(); expect(result.config!.prompts!.length).toBe(1); - // Check that inputs were converted to secrets in the prompt + // Check that inputs were converted to secrets and then to FQSNs const prompt = result.config!.prompts![0]!; expect(prompt.prompt).toBe( - "You are a ${{ secrets.roleType }} assistant. Use ${{ secrets.responseStyle }} responses.", + "You are a ${{ secrets.//roleType }} assistant. Use ${{ secrets.//responseStyle }} responses.", ); expect(prompt.name).toBe("custom-prompt"); expect(prompt.description).toBe("A customizable prompt"); @@ -298,22 +301,22 @@ rules: expect(result.config).toBeDefined(); - // Check model block conversion + // Check model block conversion (file-type blocks use // as FQSN prefix) expect(result.config!.models).toBeDefined(); expect(result.config!.models!.length).toBe(1); const model = result.config!.models![0]!; - expect(model.model).toBe("${{ secrets.modelName }}"); - expect(model.apiKey).toBe("${{ secrets.apiKey }}"); + expect(model.model).toBe("${{ secrets.//modelName }}"); + expect(model.apiKey).toBe("${{ secrets.//apiKey }}"); // Check rules block conversion expect(result.config!.rules).toBeDefined(); expect(result.config!.rules!.length).toBe(1); const rule = result.config!.rules![0]!; if (typeof rule === "string") { - expect(rule).toBe("Follow ${{ secrets.codingStandard }} conventions"); + expect(rule).toBe("Follow ${{ secrets.//codingStandard }} conventions"); } else { expect(rule.rule).toBe( - "Follow ${{ secrets.codingStandard }} conventions", + "Follow ${{ secrets.//codingStandard }} conventions", ); } @@ -417,15 +420,15 @@ rules: ruleText = rule.rule; } - // Input should be converted to secret, others should remain unchanged + // Input should be converted to secret and then to FQSN, others should also get FQSN treatment expect(ruleText).toBe( - "Input: ${{ secrets.userSetting }} | Secret: ${{ secrets.apiKey }} | Continue: ${{ continue.workspaceRoot }}", + "Input: ${{ secrets.//userSetting }} | Secret: ${{ secrets.//apiKey }} | Continue: ${{ continue.workspaceRoot }}", ); // Verify only inputs were converted const configStr = JSON.stringify(result.config); expect(configStr).not.toContain("inputs.userSetting"); - expect(configStr).toContain("secrets.apiKey"); + expect(configStr).toContain("secrets.//apiKey"); expect(configStr).toContain("continue.workspaceRoot"); }); @@ -469,7 +472,7 @@ rules: const rule = result.config!.rules![0]; expect(rule).toMatchObject({ name: "custom-rule", - rule: "Always add type hints with ${{ secrets.typeStyle }}", + rule: "Always add type hints with ${{ secrets.//typeStyle }}", description: "Enforce type hints for better code clarity", sourceFile: "file:///test/source-block.yaml", }); @@ -524,9 +527,9 @@ rules: ruleText = rule.rule; } - // All nested inputs should be converted to secrets + // All nested inputs should be converted to secrets and then to FQSNs expect(ruleText).toBe( - "Database config: host=${{ secrets.db.host }};port=${{ secrets.db.port }};user=${{ secrets.db.user }}", + "Database config: host=${{ secrets.//db.host }};port=${{ secrets.//db.port }};user=${{ secrets.//db.user }}", ); // Verify all nested inputs were converted @@ -535,4 +538,152 @@ rules: expect(configStr).not.toContain("inputs.db.port"); expect(configStr).not.toContain("inputs.db.user"); }); + + it("converts secrets in injected slug blocks to FQSNs using the block's package identifier", async () => { + const assistant: ConfigYaml = { + name: "FQSN Test Assistant", + version: "1.0.0", + }; + + // This simulates a model block from the hub (e.g., anthropic/claude-sonnet) + // that has a secret reference that needs to be converted to an FQSN + const modelBlockContent = ` +name: Anthropic Claude Model +version: 1.0.0 +schema: v1 +models: + - name: claude-sonnet + provider: anthropic + model: claude-sonnet-4-20250514 + apiKey: \${{ secrets.ANTHROPIC_API_KEY }} +`; + + // Using a slug-type identifier (like anthropic/claude-sonnet from the hub) + const injectedBlockId: PackageIdentifier = { + uriType: "slug", + fullSlug: { + ownerSlug: "anthropic", + packageSlug: "claude-sonnet", + versionSlug: "latest", + }, + }; + + mockRegistry.setContent("anthropic/claude-sonnet", modelBlockContent); + + const result = await unrollBlocks( + assistant, + mockRegistry, + [injectedBlockId], + undefined, + undefined, + undefined, + ); + + expect(result.config).toBeDefined(); + expect(result.config!.models).toBeDefined(); + expect(result.config!.models!.length).toBe(1); + + // The secret should be converted to an FQSN that includes the block's slug + // Format: ${{ secrets.ownerSlug/packageSlug/secretName }} + const model = result.config!.models![0]!; + expect(model.apiKey).toBe( + "${{ secrets.anthropic/claude-sonnet/ANTHROPIC_API_KEY }}", + ); + expect(model.name).toBe("claude-sonnet"); + expect(model.provider).toBe("anthropic"); + expect(model.model).toBe("claude-sonnet-4-20250514"); + }); + + it("converts secrets in injected file blocks to FQSNs using file path shorthand", async () => { + const assistant: ConfigYaml = { + name: "File FQSN Test Assistant", + version: "1.0.0", + }; + + const modelBlockContent = ` +name: Local Model +version: 1.0.0 +schema: v1 +models: + - name: local-model + provider: openai + model: gpt-4 + apiKey: \${{ secrets.OPENAI_API_KEY }} +`; + + const injectedBlockId: PackageIdentifier = { + uriType: "file", + fileUri: "/path/to/local-model.yaml", + }; + + mockRegistry.setContent("/path/to/local-model.yaml", modelBlockContent); + + const result = await unrollBlocks( + assistant, + mockRegistry, + [injectedBlockId], + undefined, + undefined, + undefined, + ); + + expect(result.config).toBeDefined(); + expect(result.config!.models).toBeDefined(); + expect(result.config!.models!.length).toBe(1); + + // For file-type blocks, the shorthand slug is "/" + // So the FQSN becomes: ${{ secrets.//secretName }} + const model = result.config!.models![0]!; + expect(model.apiKey).toBe("${{ secrets.//OPENAI_API_KEY }}"); + }); + + it("converts both inputs and secrets to proper FQSNs in injected blocks", async () => { + const assistant: ConfigYaml = { + name: "Mixed Template Test Assistant", + version: "1.0.0", + }; + + // Block has both inputs and secrets + const modelBlockContent = ` +name: Mixed Model +version: 1.0.0 +schema: v1 +models: + - name: mixed-model + provider: openai + model: \${{ inputs.modelName }} + apiKey: \${{ secrets.API_KEY }} + apiBase: \${{ inputs.apiBase }} +`; + + const injectedBlockId: PackageIdentifier = { + uriType: "slug", + fullSlug: { + ownerSlug: "myorg", + packageSlug: "mixed-model", + versionSlug: "1.0.0", + }, + }; + + mockRegistry.setContent("myorg/mixed-model", modelBlockContent); + + const result = await unrollBlocks( + assistant, + mockRegistry, + [injectedBlockId], + undefined, + undefined, + undefined, + ); + + expect(result.config).toBeDefined(); + expect(result.config!.models).toBeDefined(); + expect(result.config!.models!.length).toBe(1); + + const model = result.config!.models![0]!; + // Inputs get converted to secrets first, then both get FQSN treatment + expect(model.model).toBe("${{ secrets.myorg/mixed-model/modelName }}"); + expect(model.apiKey).toBe("${{ secrets.myorg/mixed-model/API_KEY }}"); + expect(model.apiBase).toBe("${{ secrets.myorg/mixed-model/apiBase }}"); + }); }); diff --git a/packages/config-yaml/src/load/unroll.ts b/packages/config-yaml/src/load/unroll.ts index 49476925e..9fae9d17b 100644 --- a/packages/config-yaml/src/load/unroll.ts +++ b/packages/config-yaml/src/load/unroll.ts @@ -550,10 +550,20 @@ export async function unrollBlocks( const injectedBlockPromises = injectBlocks.map(async (injectBlock) => { try { const blockConfigYaml = await registry.getContent(injectBlock); + // Convert inputs to secrets, then convert secrets to FQSNs using the injected block's identifier + // This ensures secrets are properly namespaced for proxy resolution (e.g., models add-on) const blockConfigYamlWithSecrets = replaceInputsWithSecrets(blockConfigYaml); - const resolvedBlock = parseMarkdownRuleOrConfigYaml( + const blockConfigYamlWithFQSNs = renderTemplateData( blockConfigYamlWithSecrets, + { + secrets: extractFQSNMap(blockConfigYamlWithSecrets, [ + injectBlock, + ]), + }, + ); + const resolvedBlock = parseMarkdownRuleOrConfigYaml( + blockConfigYamlWithFQSNs, injectBlock, ); const blockType = getBlockType(resolvedBlock);