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:
@@ -181,7 +181,7 @@ import { EventEmitter } from 'node:events';
|
||||
import { LocalJobStore } from './job-store.js';
|
||||
import { AgentRunRegistry } from './agent-run-registry.js';
|
||||
import { ExecutorRegistry } from './executor-registry.js';
|
||||
import { hydrateProviderEnvFromVault } from './provider-env.js';
|
||||
import { copyProviderKeysToVault, hydrateProviderEnvFromVault } from './provider-env.js';
|
||||
|
||||
export interface LocalConfig {
|
||||
port: number;
|
||||
@@ -568,7 +568,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
const waggleConfig = new WaggleConfig(fullConfig.dataDir);
|
||||
const configProviders = waggleConfig.getProviders();
|
||||
if (Object.keys(configProviders).length > 0) {
|
||||
const migrated = vault.migrateFromConfig({ providers: configProviders });
|
||||
const migrated = copyProviderKeysToVault(vault, configProviders);
|
||||
let scrubbed = 0;
|
||||
for (const [name, provider] of Object.entries(configProviders)) {
|
||||
if (!provider.apiKey) continue;
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user