diff --git a/core/index.d.ts b/core/index.d.ts index 518e301d9..2f096dc49 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -460,7 +460,7 @@ export interface LLMOptions { writeLog?: (str: string) => Promise; llmRequestHook?: (model: string, prompt: string) => any; apiKey?: string; - apiKeySecret?: string; + apiKeyLocation?: string; aiGatewaySlug?: string; apiBase?: string; cacheBehavior?: CacheBehavior; @@ -896,7 +896,7 @@ export interface ModelDescription { provider: string; model: string; apiKey?: string; - apiKeySecret?: string; + apiKeyLocation?: string; apiBase?: string; contextLength?: number; maxStopWords?: number; diff --git a/core/llm/index.ts b/core/llm/index.ts index 5ecfa33ff..70565b5ee 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -117,7 +117,7 @@ export abstract class BaseLLM implements ILLM { writeLog?: (str: string) => Promise; llmRequestHook?: (model: string, prompt: string) => any; apiKey?: string; - apiKeySecret?: string; + apiKeyLocation?: string; apiBase?: string; cacheBehavior?: CacheBehavior; capabilities?: ModelCapability; @@ -195,7 +195,7 @@ export abstract class BaseLLM implements ILLM { this.writeLog = options.writeLog; this.llmRequestHook = options.llmRequestHook; this.apiKey = options.apiKey; - this.apiKeySecret = options.apiKeySecret; + this.apiKeyLocation = options.apiKeyLocation; this.aiGatewaySlug = options.aiGatewaySlug; this.apiBase = options.apiBase; this.cacheBehavior = options.cacheBehavior; diff --git a/core/llm/llms/stubs/ContinueProxy.ts b/core/llm/llms/stubs/ContinueProxy.ts index 59d05b115..848b3fff7 100644 --- a/core/llm/llms/stubs/ContinueProxy.ts +++ b/core/llm/llms/stubs/ContinueProxy.ts @@ -14,12 +14,11 @@ class ContinueProxy extends OpenAI { // but we need to keep track of the actual values that the proxy will use // to call whatever LLM API is chosen private actualApiBase?: string; - private actualApiKey?: string; constructor(options: LLMOptions) { super(options); this.actualApiBase = options.apiBase; - this.actualApiKey = options.apiKey; + this.apiKeyLocation = options.apiKeyLocation; } static providerName = "continue-proxy"; @@ -30,9 +29,8 @@ class ContinueProxy extends OpenAI { protected extraBodyProperties(): Record { return { continueProperties: { - apiKey: this.actualApiKey, + apiKeyLocation: this.apiKeyLocation, apiBase: this.actualApiBase, - apiKeySecret: this.apiKeySecret, }, }; } diff --git a/packages/config-yaml/src/load/clientRender.ts b/packages/config-yaml/src/load/clientRender.ts index dfb1c4181..52e89cbfc 100644 --- a/packages/config-yaml/src/load/clientRender.ts +++ b/packages/config-yaml/src/load/clientRender.ts @@ -87,8 +87,11 @@ function getUnrenderedSecretLocation( const secretLocation = decodeSecretLocation(secretLocationEncoded); return secretLocation; } catch (e) { - // If it's templated but not a valid secret location, leave it be + // If it's a templated secret but not a valid secret location, leave it be // in case on-prem proxy has the secret in an env variable + if (templateVars[0].startsWith("secrets.")) { + return undefined; // TODO + } return undefined; } } diff --git a/packages/config-yaml/test/index.test.ts b/packages/config-yaml/test/index.test.ts index 20a1656a6..3f75ff092 100644 --- a/packages/config-yaml/test/index.test.ts +++ b/packages/config-yaml/test/index.test.ts @@ -1,6 +1,10 @@ import * as fs from "fs"; import * as YAML from "yaml"; -import { decodeSecretLocation, resolveSecretLocationInProxy } from "../dist"; +import { + decodeSecretLocation, + encodeSecretLocation, + resolveSecretLocationInProxy, +} from "../dist"; import { clientRender, FQSN, @@ -24,11 +28,13 @@ describe("E2E Scenarios", () => { }; const packageSecrets: Record = { - ANTHROPIC_API_KEY: "sk-ant", + "test-org/assistant/ANTHROPIC_API_KEY": "sk-ant", + "test-org/models/GEMINI_API_KEY": "gemini-api-key", }; const proxyEnvSecrets: Record = { ANTHROPIC_API_KEY: "sk-ant-env", + GEMINI_API_KEY: "gemini-api-key-env", }; const localUserSecretStore: SecretStore = { @@ -66,7 +72,9 @@ describe("E2E Scenarios", () => { secretLocation: SecretLocation, ): Promise { if (secretLocation.secretType === SecretType.Package) { - return packageSecrets[secretLocation.secretName]; + return packageSecrets[ + encodeSecretLocation(secretLocation).split(":")[1] + ]; } else if (secretLocation.secretType === SecretType.User) { return userSecrets[secretLocation.secretName]; } else { @@ -92,7 +100,7 @@ describe("E2E Scenarios", () => { ); // Test that packages were correctly unrolled and params replaced - expect(unrolledConfig.models?.length).toBe(3); + expect(unrolledConfig.models?.length).toBe(4); expect(unrolledConfig.models?.[0].apiKey).toBe( "${{ secrets.test-org/assistant/OPENAI_API_KEY }}", ); @@ -100,6 +108,9 @@ describe("E2E Scenarios", () => { expect(unrolledConfig.models?.[2].apiKey).toBe( "${{ secrets.test-org/assistant/test-org/models/ANTHROPIC_API_KEY }}", ); + expect(unrolledConfig.models?.[3].apiKey).toBe( + "${{ secrets.test-org/assistant/test-org/models/GEMINI_API_KEY }}", + ); expect(unrolledConfig.rules?.length).toBe(3); expect(unrolledConfig.docs?.[0].startUrl).toBe( @@ -113,7 +124,9 @@ describe("E2E Scenarios", () => { ); // Test that user secrets were injected and others were changed to use proxy - const anthropicSecretLocation = "package:test-org/models/ANTHROPIC_API_KEY"; + const anthropicSecretLocation = + "package:test-org/assistant/ANTHROPIC_API_KEY"; + const geminiSecretLocation = "package:test-org/models/GEMINI_API_KEY"; expect(clientRendered.models?.[0].apiKey).toBe("sk-123"); expect(clientRendered.models?.[1].apiKey).toBe("sk-456"); expect(clientRendered.models?.[2].provider).toBe("continue-proxy"); @@ -121,24 +134,45 @@ describe("E2E Scenarios", () => { anthropicSecretLocation, ); expect(clientRendered.models?.[2].apiKey).toBeUndefined(); + expect(clientRendered.models?.[3].provider).toBe("continue-proxy"); + expect((clientRendered.models?.[3] as any).apiKeyLocation).toBe( + geminiSecretLocation, + ); + expect(clientRendered.models?.[3].apiKey).toBeUndefined(); // Test that proxy can correctly resolve secrets - const secretLocation = decodeSecretLocation(anthropicSecretLocation); + const decodedAnthropicSecretLocation = decodeSecretLocation( + anthropicSecretLocation, + ); + const decodedGeminiSecretLocation = + decodeSecretLocation(geminiSecretLocation); // With environment - const secretValue = await resolveSecretLocationInProxy( - secretLocation, + const antSecretValue = await resolveSecretLocationInProxy( + decodedAnthropicSecretLocation, platformSecretStore, environmentSecretStore, ); - expect(secretValue).toBe("sk-ant-env"); + expect(antSecretValue).toBe("sk-ant-env"); + const geminiSecretValue = await resolveSecretLocationInProxy( + decodedGeminiSecretLocation, + platformSecretStore, + environmentSecretStore, + ); + expect(geminiSecretValue).toBe("gemini-api-key-env"); // Without environment - const secretValue2 = await resolveSecretLocationInProxy( - secretLocation, + const antSecretValue2 = await resolveSecretLocationInProxy( + decodedAnthropicSecretLocation, platformSecretStore, undefined, ); - expect(secretValue2).toBe("sk-ant"); + expect(antSecretValue2).toBe("sk-ant"); + const geminiSecretValue2 = await resolveSecretLocationInProxy( + decodedGeminiSecretLocation, + platformSecretStore, + undefined, + ); + expect(geminiSecretValue2).toBe("gemini-api-key"); }); }); diff --git a/packages/config-yaml/test/packages/test-org/models.yaml b/packages/config-yaml/test/packages/test-org/models.yaml index 26091aa4b..f47831662 100644 --- a/packages/config-yaml/test/packages/test-org/models.yaml +++ b/packages/config-yaml/test/packages/test-org/models.yaml @@ -11,3 +11,8 @@ models: provider: anthropic model: claude-3-5-sonnet-latest apiKey: ${{ secrets.ANTHROPIC_API_KEY }} + + - name: gemini + provider: gemini + model: gemini + apiKey: ${{ secrets.GEMINI_API_KEY }}