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:
@@ -327,20 +327,4 @@ export class VaultStore implements SecretStore {
|
||||
const vault = this.readVault();
|
||||
return name in vault;
|
||||
}
|
||||
|
||||
/** Migrate plaintext providers from config.json to vault. Returns count migrated. */
|
||||
migrateFromConfig(config: { providers?: Record<string, { apiKey: string; models?: string[]; baseUrl?: string }> }): number {
|
||||
if (!config.providers) return 0;
|
||||
let migrated = 0;
|
||||
for (const [name, provider] of Object.entries(config.providers)) {
|
||||
if (provider.apiKey && !this.has(name)) {
|
||||
this.set(name, provider.apiKey, {
|
||||
models: provider.models,
|
||||
baseUrl: provider.baseUrl,
|
||||
});
|
||||
migrated++;
|
||||
}
|
||||
}
|
||||
return migrated;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,47 +171,6 @@ describe('VaultStore', () => {
|
||||
expect(entry!.value).toBe('sk-ant-shared-secret');
|
||||
});
|
||||
|
||||
it('migration from config — providers migrated to vault', () => {
|
||||
const dir = makeTempDir();
|
||||
const vault = new VaultStore(dir);
|
||||
|
||||
const config = {
|
||||
providers: {
|
||||
anthropic: { apiKey: 'sk-ant-key-1', models: ['claude-sonnet-4-6'], baseUrl: undefined },
|
||||
openai: { apiKey: 'sk-openai-key-1', models: ['gpt-4o'] },
|
||||
},
|
||||
};
|
||||
|
||||
const migrated = vault.migrateFromConfig(config);
|
||||
expect(migrated).toBe(2);
|
||||
|
||||
const anthropic = vault.get('anthropic');
|
||||
expect(anthropic).not.toBeNull();
|
||||
expect(anthropic!.value).toBe('sk-ant-key-1');
|
||||
expect(anthropic!.metadata?.models).toEqual(['claude-sonnet-4-6']);
|
||||
|
||||
const openai = vault.get('openai');
|
||||
expect(openai).not.toBeNull();
|
||||
expect(openai!.value).toBe('sk-openai-key-1');
|
||||
});
|
||||
|
||||
it('migration skips existing — migrate twice, count stays same', () => {
|
||||
const dir = makeTempDir();
|
||||
const vault = new VaultStore(dir);
|
||||
|
||||
const config = {
|
||||
providers: {
|
||||
anthropic: { apiKey: 'sk-ant-key-1', models: ['claude-sonnet-4-6'] },
|
||||
},
|
||||
};
|
||||
|
||||
const first = vault.migrateFromConfig(config);
|
||||
expect(first).toBe(1);
|
||||
|
||||
const second = vault.migrateFromConfig(config);
|
||||
expect(second).toBe(0);
|
||||
});
|
||||
|
||||
it('key file is generated on first use and reused', () => {
|
||||
const dir = makeTempDir();
|
||||
new VaultStore(dir);
|
||||
|
||||
Reference in New Issue
Block a user