diff --git a/packages/config-yaml/src/load/clientRender.ts b/packages/config-yaml/src/load/clientRender.ts index b58aca05f..a849be67c 100644 --- a/packages/config-yaml/src/load/clientRender.ts +++ b/packages/config-yaml/src/load/clientRender.ts @@ -1,7 +1,11 @@ import * as YAML from "yaml"; import { PlatformClient, SecretStore } from "../interfaces/index.js"; -import { encodeSecretLocation } from "../interfaces/SecretResult.js"; -import { FQSN, decodeFQSN, encodeFQSN } from "../interfaces/slugs.js"; +import { + decodeSecretLocation, + encodeSecretLocation, + SecretLocation, +} from "../interfaces/SecretResult.js"; +import { decodeFQSN, encodeFQSN, FQSN } from "../interfaces/slugs.js"; import { ConfigYaml } from "../schemas/index.js"; import { fillTemplateVariables, @@ -54,6 +58,44 @@ export async function clientRender( const renderedYaml = fillTemplateVariables(rawYaml, secretsTemplateData); // 6. The rendered YAML is parsed and validated again - const finalParsedYaml = parseConfigYaml(renderedYaml); - return finalParsedYaml; + const parsedYaml = parseConfigYaml(renderedYaml); + + // 7. We update any of the items with the proxy version if there are un-rendered secrets + const finalConfig = useProxyForUnrenderedSecrets(parsedYaml); + return finalConfig; +} + +function getUnrenderedSecretLocation( + value: string | undefined, +): SecretLocation | undefined { + if (!value) return undefined; + + const templateVars = getTemplateVariables(value); + if (templateVars.length === 1) { + const secretLocationEncoded = templateVars[0].split("secrets.")[1]; + const secretLocation = decodeSecretLocation(secretLocationEncoded); + return secretLocation; + } + + return undefined; +} + +function useProxyForUnrenderedSecrets(config: ConfigYaml): ConfigYaml { + if (config.models) { + for (let i = 0; i < config.models.length; i++) { + const apiKeyLocation = getUnrenderedSecretLocation( + config.models[i].apiKey, + ); + if (apiKeyLocation) { + config.models[i] = { + ...config.models[i], + provider: "continue-proxy", + apiKeyLocation: encodeSecretLocation(apiKeyLocation), + apiKey: undefined, + }; + } + } + } + + return config; } diff --git a/packages/config-yaml/src/schemas/models.ts b/packages/config-yaml/src/schemas/models.ts index 8a4198947..713d2cc2d 100644 --- a/packages/config-yaml/src/schemas/models.ts +++ b/packages/config-yaml/src/schemas/models.ts @@ -42,15 +42,26 @@ export const completionOptionsSchema = z.object({ }); export type CompletionOptions = z.infer; -export const modelSchema = z.object({ +const baseModelFields = { name: z.string(), - provider: z.string(), model: z.string(), apiKey: z.string().optional(), apiBase: z.string().optional(), roles: modelRolesSchema.array().optional(), defaultCompletionOptions: completionOptionsSchema.optional(), requestOptions: requestOptionsSchema.optional(), -}); +}; + +export const modelSchema = z.union([ + z.object({ + ...baseModelFields, + provider: z.literal("continue-proxy"), + apiKeyLocation: z.string(), + }), + z.object({ + ...baseModelFields, + provider: z.string().refine((val) => val !== "continue-proxy"), + }), +]); export type ModelConfig = z.infer; diff --git a/packages/config-yaml/test/index.test.ts b/packages/config-yaml/test/index.test.ts index 289052c5e..a17790d65 100644 --- a/packages/config-yaml/test/index.test.ts +++ b/packages/config-yaml/test/index.test.ts @@ -1,4 +1,5 @@ import * as fs from "fs"; +import { decodeSecretLocation, resolveSecretLocationInProxy } from "../dist"; import { clientRender, FQSN, @@ -13,6 +14,7 @@ import { SecretType, unrollAssistant, } from "../src"; +import exp = require("constants"); // Test e2e flows from raw yaml -> unroll -> client render -> resolve secrets on proxy describe("E2E Scenarios", () => { @@ -24,6 +26,10 @@ describe("E2E Scenarios", () => { ANTHROPIC_API_KEY: "sk-ant", }; + const proxyEnvSecrets: Record = { + ANTHROPIC_API_KEY: "sk-ant-env", + }; + const localUserSecretStore: SecretStore = { get: async function (secretName: string): Promise { return userSecrets[secretName]; @@ -45,6 +51,15 @@ describe("E2E Scenarios", () => { }, }; + const environmentSecretStore: SecretStore = { + get: async function (secretName: string): Promise { + return proxyEnvSecrets[secretName]; + }, + set: function (secretName: string, secretValue: string): Promise { + throw new Error("Function not implemented."); + }, + }; + const platformSecretStore: PlatformSecretStore = { getSecretFromSecretLocation: async function ( secretLocation: SecretLocation, @@ -96,11 +111,33 @@ describe("E2E Scenarios", () => { platformClient, ); - // Test that user secrets were injected and other secrets remain templated + // Test that user secrets were injected and others were changed to use proxy + const anthropicSecretLocation = "package:test-org/models/ANTHROPIC_API_KEY"; expect(clientRendered.models?.[0].apiKey).toBe("sk-123"); expect(clientRendered.models?.[1].apiKey).toBe("sk-456"); - expect(clientRendered.models?.[2].apiKey).toBe( - "${{ secrets.package:test-org/models/ANTHROPIC_API_KEY }}", + expect(clientRendered.models?.[2].provider).toBe("continue-proxy"); + expect((clientRendered.models?.[2] as any).apiKeyLocation).toBe( + anthropicSecretLocation, ); + expect(clientRendered.models?.[2].apiKey).toBeUndefined(); + + // Test that proxy can correctly resolve secrets + const secretLocation = decodeSecretLocation(anthropicSecretLocation); + + // With environment + const secretValue = await resolveSecretLocationInProxy( + secretLocation, + platformSecretStore, + environmentSecretStore, + ); + expect(secretValue).toBe("sk-ant-env"); + + // Without environment + const secretValue2 = await resolveSecretLocationInProxy( + secretLocation, + platformSecretStore, + undefined, + ); + expect(secretValue2).toBe("sk-ant"); }); });