Connectors, the connector SDK and registry, setup-connectors, provider-env, the LiteLLM runtime config and the sidecar's `vault` decoration only read and write secrets, so they now take the SecretStore interface. VaultStore is still constructed in the sidecar and the service startup, and its file format is unchanged. AGENTS.md now points at the interface.
107 lines
3.3 KiB
TypeScript
107 lines
3.3 KiB
TypeScript
import { WaggleConfig, type ProviderEntry, type SecretStore } from '@waggle/core';
|
|
|
|
/** Provider credentials consumed by LiteLLM and provider SDKs. */
|
|
export const PROVIDER_ENV_NAMES: Record<string, readonly string[]> = {
|
|
anthropic: ['ANTHROPIC_API_KEY'],
|
|
openai: ['OPENAI_API_KEY'],
|
|
google: ['GEMINI_API_KEY', 'GOOGLE_API_KEY'],
|
|
xai: ['XAI_API_KEY'],
|
|
deepseek: ['DEEPSEEK_API_KEY'],
|
|
mistral: ['MISTRAL_API_KEY'],
|
|
alibaba: ['DASHSCOPE_API_KEY'],
|
|
minimax: ['MINIMAX_API_KEY'],
|
|
zhipu: ['ZHIPU_API_KEY'],
|
|
moonshot: ['MOONSHOT_API_KEY'],
|
|
perplexity: ['PERPLEXITY_API_KEY'],
|
|
openrouter: ['OPENROUTER_API_KEY'],
|
|
};
|
|
|
|
export function applyProviderKeyToEnv(
|
|
providerId: string,
|
|
apiKey: string,
|
|
overwrite = true,
|
|
): number {
|
|
const envNames = PROVIDER_ENV_NAMES[providerId] ?? [];
|
|
let updated = 0;
|
|
for (const envName of envNames) {
|
|
if (!overwrite && process.env[envName]) continue;
|
|
if (process.env[envName] === apiKey) continue;
|
|
process.env[envName] = apiKey;
|
|
updated += 1;
|
|
}
|
|
return updated;
|
|
}
|
|
|
|
export function getProviderApiKeys(
|
|
providerId: string,
|
|
vault: SecretStore | null | undefined,
|
|
): string[] {
|
|
const candidates: string[] = [];
|
|
try {
|
|
const vaultKey = vault?.get(providerId)?.value;
|
|
if (vaultKey) candidates.push(vaultKey);
|
|
} catch {
|
|
// A locked/unavailable vault must not hide explicit process credentials.
|
|
}
|
|
for (const envName of PROVIDER_ENV_NAMES[providerId] ?? []) {
|
|
const value = process.env[envName];
|
|
if (value) candidates.push(value);
|
|
}
|
|
return [...new Set(candidates)];
|
|
}
|
|
|
|
export function getProviderApiKey(
|
|
providerId: string,
|
|
vault: SecretStore | null | undefined,
|
|
): string | undefined {
|
|
return getProviderApiKeys(providerId, vault)[0];
|
|
}
|
|
|
|
/** Hydrate provider SDK/LiteLLM env before a child process snapshots it. */
|
|
export function hydrateProviderEnvFromVault(vault: SecretStore, overwrite = false): number {
|
|
let updated = 0;
|
|
for (const providerId of Object.keys(PROVIDER_ENV_NAMES)) {
|
|
const entry = vault.get(providerId);
|
|
if (!entry?.value) continue;
|
|
updated += applyProviderKeyToEnv(providerId, entry.value, overwrite);
|
|
}
|
|
return updated;
|
|
}
|
|
|
|
/**
|
|
* Copy plaintext provider keys from config.json into the vault, keeping any key
|
|
* the vault already holds. Returns the number of keys copied.
|
|
*/
|
|
export function copyProviderKeysToVault(
|
|
vault: SecretStore,
|
|
providers: Readonly<Record<string, ProviderEntry>>,
|
|
): number {
|
|
let copied = 0;
|
|
for (const [name, provider] of Object.entries(providers)) {
|
|
if (provider.apiKey && !vault.has(name)) {
|
|
vault.set(name, provider.apiKey, {
|
|
models: provider.models,
|
|
baseUrl: provider.baseUrl,
|
|
});
|
|
copied++;
|
|
}
|
|
}
|
|
return copied;
|
|
}
|
|
|
|
/** Move legacy config.json secrets before LiteLLM starts, then scrub plaintext. */
|
|
export function migrateLegacyProviderKeysToVault(dataDir: string, vault: SecretStore): number {
|
|
const config = new WaggleConfig(dataDir);
|
|
const providers = config.getProviders();
|
|
const migrated = copyProviderKeysToVault(vault, providers);
|
|
let scrubbed = 0;
|
|
|
|
for (const [providerId, provider] of Object.entries(providers)) {
|
|
if (!provider.apiKey) continue;
|
|
config.setProvider(providerId, { ...provider, apiKey: '' });
|
|
scrubbed += 1;
|
|
}
|
|
if (scrubbed > 0) config.save();
|
|
return migrated;
|
|
}
|