import { PROVIDER_DEFINITIONS, getProviderDefinition, } from '../../shared/providers/registry'; import type { ProviderAccount, ProviderConfig, ProviderDefinition, } from '../../shared/providers/types'; import { ensureProviderStoreMigrated } from './provider-migration'; import { getDefaultProviderAccountId, getProviderAccount, listProviderAccounts, providerAccountToConfig, providerConfigToAccount, saveProviderAccount, setDefaultProviderAccount, } from './provider-store'; import { deleteApiKey, deleteProvider, getAllProviders, getAllProvidersWithKeyInfo, getApiKey, getDefaultProvider, getProvider, hasApiKey, saveProvider, setDefaultProvider, storeApiKey, } from '../../utils/secure-storage'; import type { ProviderWithKeyInfo } from '../../shared/providers/types'; export class ProviderService { async listVendors(): Promise { return PROVIDER_DEFINITIONS; } async listAccounts(): Promise { await ensureProviderStoreMigrated(); return listProviderAccounts(); } async getAccount(accountId: string): Promise { await ensureProviderStoreMigrated(); return getProviderAccount(accountId); } async getDefaultAccountId(): Promise { await ensureProviderStoreMigrated(); return (await getDefaultProvider()) ?? getDefaultProviderAccountId(); } async createAccount(account: ProviderAccount, apiKey?: string): Promise { await ensureProviderStoreMigrated(); await saveProvider(providerAccountToConfig(account)); await saveProviderAccount(account); if (apiKey !== undefined && apiKey.trim()) { await storeApiKey(account.id, apiKey.trim()); } return (await getProviderAccount(account.id)) ?? account; } async updateAccount( accountId: string, patch: Partial, apiKey?: string, ): Promise { await ensureProviderStoreMigrated(); const existing = await getProviderAccount(accountId); if (!existing) { throw new Error('Provider account not found'); } const nextAccount: ProviderAccount = { ...existing, ...patch, id: accountId, updatedAt: patch.updatedAt ?? new Date().toISOString(), }; await saveProvider(providerAccountToConfig(nextAccount)); await saveProviderAccount(nextAccount); if (apiKey !== undefined) { const trimmedKey = apiKey.trim(); if (trimmedKey) { await storeApiKey(accountId, trimmedKey); } else { await deleteApiKey(accountId); } } return (await getProviderAccount(accountId)) ?? nextAccount; } async deleteAccount(accountId: string): Promise { await ensureProviderStoreMigrated(); return deleteProvider(accountId); } async syncLegacyProvider(config: ProviderConfig, options?: { isDefault?: boolean }): Promise { await ensureProviderStoreMigrated(); const account = providerConfigToAccount(config, options); await saveProviderAccount(account); return account; } async listLegacyProviders(): Promise { return getAllProviders(); } async listLegacyProvidersWithKeyInfo(): Promise { return getAllProvidersWithKeyInfo(); } async getLegacyProvider(providerId: string): Promise { return getProvider(providerId); } async saveLegacyProvider(config: ProviderConfig): Promise { await saveProvider(config); } async deleteLegacyProvider(providerId: string): Promise { return deleteProvider(providerId); } async setDefaultLegacyProvider(providerId: string): Promise { await setDefaultProvider(providerId); } async getDefaultLegacyProvider(): Promise { return getDefaultProvider(); } async setLegacyProviderApiKey(providerId: string, apiKey: string): Promise { return storeApiKey(providerId, apiKey); } async getLegacyProviderApiKey(providerId: string): Promise { return getApiKey(providerId); } async deleteLegacyProviderApiKey(providerId: string): Promise { return deleteApiKey(providerId); } async hasLegacyProviderApiKey(providerId: string): Promise { return hasApiKey(providerId); } async setDefaultAccount(accountId: string): Promise { await ensureProviderStoreMigrated(); await setDefaultProviderAccount(accountId); await setDefaultProvider(accountId); } getVendorDefinition(vendorId: string): ProviderDefinition | undefined { return getProviderDefinition(vendorId); } } const providerService = new ProviderService(); export function getProviderService(): ProviderService { return providerService; }