refactor(server): move the config.json provider-key copy out of VaultStore (P1.2)

VaultStore.migrateFromConfig had two callers, both in the server. It is now
copyProviderKeysToVault in provider-env.ts, next to
migrateLegacyProviderKeysToVault, with the same rules: copy keys that have a
value, keep keys the vault already holds, store models and baseUrl as metadata.
Its tests moved from core to provider-env.test.ts.
This commit is contained in:
shumeiko.it
2026-10-24 16:40:39 +02:00
parent 3528a242f6
commit 2226ec1bd7
5 changed files with 65 additions and 61 deletions

View File

@@ -1,4 +1,4 @@
import { WaggleConfig, type VaultStore } from '@waggle/core';
import { WaggleConfig, type ProviderEntry, type VaultStore } from '@waggle/core';
/** Provider credentials consumed by LiteLLM and provider SDKs. */
export const PROVIDER_ENV_NAMES: Record<string, readonly string[]> = {
@@ -68,11 +68,32 @@ export function hydrateProviderEnvFromVault(vault: VaultStore, overwrite = false
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: VaultStore,
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: VaultStore): number {
const config = new WaggleConfig(dataDir);
const providers = config.getProviders();
const migrated = vault.migrateFromConfig({ providers });
const migrated = copyProviderKeysToVault(vault, providers);
let scrubbed = 0;
for (const [providerId, provider] of Object.entries(providers)) {