fix: use openclaw.json as single source of truth for provider list (#649)

This commit is contained in:
paisley
2026-03-24 15:21:37 +08:00
committed by GitHub
Unverified
parent 859e3fd6c5
commit 05ae404dee
5 changed files with 268 additions and 193 deletions

View File

@@ -6,7 +6,7 @@ import {
} from './provider-store';
import { getClawXProviderStore } from './store-instance';
const PROVIDER_STORE_SCHEMA_VERSION = 1;
const PROVIDER_STORE_SCHEMA_VERSION = 2;
export async function ensureProviderStoreMigrated(): Promise<void> {
const store = await getClawXProviderStore();
@@ -16,19 +16,31 @@ export async function ensureProviderStoreMigrated(): Promise<void> {
return;
}
const legacyProviders = (store.get('providers') ?? {}) as Record<string, ProviderConfig>;
const defaultProviderId = (store.get('defaultProvider') ?? null) as string | null;
const existingDefaultAccountId = await getDefaultProviderAccountId();
// v0 → v1: migrate legacy `providers` entries to `providerAccounts`.
if (schemaVersion < 1) {
const legacyProviders = (store.get('providers') ?? {}) as Record<string, ProviderConfig>;
const defaultProviderId = (store.get('defaultProvider') ?? null) as string | null;
const existingDefaultAccountId = await getDefaultProviderAccountId();
for (const provider of Object.values(legacyProviders)) {
const account = providerConfigToAccount(provider, {
isDefault: provider.id === defaultProviderId,
});
await saveProviderAccount(account);
for (const provider of Object.values(legacyProviders)) {
const account = providerConfigToAccount(provider, {
isDefault: provider.id === defaultProviderId,
});
await saveProviderAccount(account);
}
if (!existingDefaultAccountId && defaultProviderId) {
store.set('defaultProviderAccountId', defaultProviderId);
}
}
if (!existingDefaultAccountId && defaultProviderId) {
store.set('defaultProviderAccountId', defaultProviderId);
// v1 → v2: clear the legacy `providers` store.
// The old `saveProvider()` was duplicating entries into this store, causing
// phantom and duplicate accounts when the migration above re-runs.
// Now that createAccount/updateAccount no longer write to `providers`,
// we clear it to prevent stale entries from causing issues.
if (schemaVersion < 2) {
store.set('providers', {});
}
store.set('schemaVersion', PROVIDER_STORE_SCHEMA_VERSION);