feat: enhance provider functionality with base URL normalization and API protocol support; add tests for provider creation
This commit is contained in:
@@ -1,8 +1,30 @@
|
||||
import { BaseProvider } from "./BaseProvider";
|
||||
import { OpenAIProvider } from "./OpenAIProvider";
|
||||
import { providerApiService } from '@electron/service/provider-api-service';
|
||||
import type { ProviderAccount } from '@runtime/lib/providers';
|
||||
import { getProviderTypeInfo } from '@runtime/lib/providers';
|
||||
|
||||
function normalizeBaseUrl(baseUrl?: string): string | undefined {
|
||||
const trimmed = baseUrl?.trim();
|
||||
if (!trimmed) {
|
||||
return undefined;
|
||||
}
|
||||
if (trimmed.startsWith('http://') || trimmed.startsWith('https://')) {
|
||||
return trimmed;
|
||||
}
|
||||
return `https://${trimmed}`;
|
||||
}
|
||||
|
||||
function resolveProviderBaseUrl(account: ProviderAccount): string | undefined {
|
||||
return normalizeBaseUrl(account.baseUrl)
|
||||
|| normalizeBaseUrl(account.metadata?.resourceUrl)
|
||||
|| getProviderTypeInfo(account.vendorId)?.defaultBaseUrl;
|
||||
}
|
||||
|
||||
function resolveProviderApiProtocol(account: ProviderAccount): ProviderAccount['apiProtocol'] | undefined {
|
||||
return account.apiProtocol || getProviderTypeInfo(account.vendorId)?.apiProtocol;
|
||||
}
|
||||
|
||||
export function createProvider(accountId: string): BaseProvider {
|
||||
const account = providerApiService.getAccounts().find((a) => a.id === accountId);
|
||||
if (!account) {
|
||||
@@ -15,12 +37,12 @@ export function createProvider(accountId: string): BaseProvider {
|
||||
throw new Error(`API key for account ${accountId} not found`);
|
||||
}
|
||||
|
||||
const baseURL = account.baseUrl || getProviderTypeInfo(account.vendorId)?.defaultBaseUrl;
|
||||
const baseURL = resolveProviderBaseUrl(account);
|
||||
if (!baseURL) {
|
||||
throw new Error(`Base URL for account ${accountId} not found`);
|
||||
}
|
||||
|
||||
switch (account.apiProtocol) {
|
||||
switch (resolveProviderApiProtocol(account)) {
|
||||
case 'anthropic-messages':
|
||||
throw new Error('Anthropic provider not yet implemented');
|
||||
case 'openai-completions':
|
||||
|
||||
Reference in New Issue
Block a user