Files
zn-ai/electron/providers/index.ts
2026-04-14 17:02:20 +08:00

35 lines
1.1 KiB
TypeScript

import { BaseProvider } from "./BaseProvider";
import { OpenAIProvider } from "./OpenAIProvider";
import { providerApiService } from '@electron/service/provider-api-service';
import { getProviderTypeInfo } from '@lib/providers';
export function createProvider(accountId: string): BaseProvider {
const account = providerApiService.getAccounts().find((a) => a.id === accountId);
if (!account) {
throw new Error(`Provider account ${accountId} not found`);
}
const apiKeyResult = providerApiService.getApiKey(accountId);
const apiKey = apiKeyResult.apiKey;
if (!apiKey) {
throw new Error(`API key for account ${accountId} not found`);
}
const baseURL = account.baseUrl || getProviderTypeInfo(account.vendorId)?.defaultBaseUrl;
if (!baseURL) {
throw new Error(`Base URL for account ${accountId} not found`);
}
switch (account.apiProtocol) {
case 'anthropic-messages':
throw new Error('Anthropic provider not yet implemented');
case 'openai-completions':
case 'openai-responses':
default:
return new OpenAIProvider(apiKey, baseURL, account.headers);
}
}
export * from './BaseProvider';
export { OpenAIProvider };