diff --git a/packages/config-yaml/package.json b/packages/config-yaml/package.json index 101b25f06..b5fab55ae 100644 --- a/packages/config-yaml/package.json +++ b/packages/config-yaml/package.json @@ -1,6 +1,6 @@ { "name": "@continuedev/config-yaml", - "version": "1.0.8", + "version": "1.0.9", "description": "", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/config-yaml/src/converter.ts b/packages/config-yaml/src/converter.ts index 14a60eecd..485d469a9 100644 --- a/packages/config-yaml/src/converter.ts +++ b/packages/config-yaml/src/converter.ts @@ -132,7 +132,7 @@ export function convertJsonToYamlConfig(configJson: ConfigJson): ConfigYaml { version: "0.0.1", models, context, - rules: configJson.systemMessage, + rules: configJson.systemMessage ? [configJson.systemMessage] : undefined, prompts, mcpServers, docs, diff --git a/packages/config-yaml/src/index.ts b/packages/config-yaml/src/index.ts index c2ebe8254..c74d2d70a 100644 --- a/packages/config-yaml/src/index.ts +++ b/packages/config-yaml/src/index.ts @@ -92,6 +92,17 @@ export function fillTemplateVariables( } export { convertJsonToYamlConfig } from "./converter.js"; -export { ConfigYaml, configYamlSchema } from "./schemas/index.js"; +export { resolveSecretsOnClient } from "./resolveSecretsOnClient.js"; +export { + ClientConfigYaml, + clientConfigYamlSchema, + ConfigYaml, + configYamlSchema, +} from "./schemas/index.js"; export type { ModelConfig } from "./schemas/models.js"; -export { validateConfigYaml } from "./validation.js"; +export { + ConfigResult, + ConfigValidationError, + validateConfigYaml, + ValidationLevel, +} from "./validation.js"; diff --git a/packages/config-yaml/src/resolveSecretsOnClient.ts b/packages/config-yaml/src/resolveSecretsOnClient.ts new file mode 100644 index 000000000..c3029fbda --- /dev/null +++ b/packages/config-yaml/src/resolveSecretsOnClient.ts @@ -0,0 +1,64 @@ +import { ClientConfigYaml, ConfigYaml } from "./schemas/index.js"; +type SecretProvider = ( + secretNames: string[], +) => Promise<{ [key: string]: string }>; + +/** + * Take a ConfigYaml with apiKeySecrets, and look to fill in these secrets + * with whatever secret store exists in the client. + */ +export async function resolveSecretsOnClient( + configYaml: ClientConfigYaml, + getSecretsFromClientStore: SecretProvider, + getSecretsFromServer: SecretProvider, +): Promise { + const requiredSecrets = getRequiredSecretsInClientConfig(configYaml); + + const secretsFoundOnClient = await getSecretsFromClientStore(requiredSecrets); + + const secretsNotFoundOnClient = requiredSecrets.filter( + (secret) => !secretsFoundOnClient[secret], + ); + + let secretsFoundOnServer = {}; + if (secretsNotFoundOnClient.length > 0) { + secretsFoundOnServer = await getSecretsFromServer(secretsNotFoundOnClient); + } + + const clientSecrets = { + ...secretsFoundOnClient, + ...secretsFoundOnServer, + }; + + const finalConfigYaml = injectClientSecrets(configYaml, clientSecrets); + + // Anything with an apiKeySecret left over must use proxy + return finalConfigYaml; +} + +function getRequiredSecretsInClientConfig( + configYaml: ClientConfigYaml, +): string[] { + const secrets = new Set(); + for (const model of configYaml.models ?? []) { + if (model.apiKeySecret) { + secrets.add(model.apiKeySecret); + } + } + return Array.from(secrets); +} + +function injectClientSecrets( + configYaml: ClientConfigYaml, + clientSecrets: Record, +): ConfigYaml { + for (const model of configYaml.models ?? []) { + if (model.apiKeySecret && clientSecrets[model.apiKeySecret]) { + // Remove apiKeySecret and place the client secret in apiKey + model.apiKey = clientSecrets[model.apiKeySecret]; + delete model.apiKeySecret; + } + } + + return configYaml; +} diff --git a/packages/config-yaml/src/schemas/index.ts b/packages/config-yaml/src/schemas/index.ts index 75018bb25..f763c7be5 100644 --- a/packages/config-yaml/src/schemas/index.ts +++ b/packages/config-yaml/src/schemas/index.ts @@ -48,9 +48,13 @@ export const configYamlSchema = z.object({ data: z.array(dataSchema).optional(), tools: z.array(toolSchema).optional(), mcpServers: z.array(mcpServerSchema).optional(), - rules: z.string().optional(), + rules: z.array(z.string()).optional(), prompts: z.array(promptSchema).optional(), docs: z.array(docSchema).optional(), }); export type ConfigYaml = z.infer; + +export const clientConfigYamlSchema = configYamlSchema.omit({ packages: true }); + +export type ClientConfigYaml = z.infer; diff --git a/packages/config-yaml/src/validation.ts b/packages/config-yaml/src/validation.ts index 223764e71..581b5fe92 100644 --- a/packages/config-yaml/src/validation.ts +++ b/packages/config-yaml/src/validation.ts @@ -10,6 +10,12 @@ export interface ConfigValidationError { message: string; } +export interface ConfigResult { + config: T | undefined; + errors: ConfigValidationError[] | undefined; + configLoadInterrupted: boolean; +} + export function validateConfigYaml( config: ConfigYaml, ): ConfigValidationError[] {