68 lines
1.8 KiB
TypeScript
68 lines
1.8 KiB
TypeScript
export interface AIProviderResult {
|
|
content: string;
|
|
title?: string;
|
|
request: Record<string, unknown>;
|
|
response: Record<string, unknown>;
|
|
}
|
|
|
|
export interface AIProvider {
|
|
provider: string;
|
|
generate(prompt: string, systemPrompt?: string, options?: { jsonMode?: boolean }): Promise<AIProviderResult>;
|
|
validate?(): Promise<boolean>;
|
|
}
|
|
|
|
export interface AIProviderConfig {
|
|
provider: string;
|
|
apiKey?: string;
|
|
model?: string;
|
|
baseUrl?: string;
|
|
providerSettings?: string;
|
|
}
|
|
|
|
function parseProviderSettings(settingsJson?: string): {
|
|
headers?: Record<string, string>;
|
|
parameters?: Record<string, unknown>;
|
|
} {
|
|
if (!settingsJson) return {};
|
|
try {
|
|
const parsed = JSON.parse(settingsJson);
|
|
return {
|
|
headers: parsed.headers,
|
|
parameters: parsed.parameters,
|
|
};
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
import { OpenAIProvider } from './openai';
|
|
import { AnthropicProvider } from './anthropic';
|
|
import { OllamaProvider } from './ollama';
|
|
import { LMStudioProvider } from './lmstudio';
|
|
import { GroqProvider } from './groq';
|
|
import { XAIProvider } from './xai';
|
|
import { CustomProvider } from './custom';
|
|
|
|
export function createAIProvider(config: AIProviderConfig): AIProvider {
|
|
const settings = parseProviderSettings(config.providerSettings);
|
|
|
|
switch (config.provider) {
|
|
case 'openai':
|
|
return new OpenAIProvider(config, settings);
|
|
case 'anthropic':
|
|
return new AnthropicProvider(config, settings);
|
|
case 'ollama':
|
|
return new OllamaProvider(config, settings);
|
|
case 'lmstudio':
|
|
return new LMStudioProvider(config, settings);
|
|
case 'groq':
|
|
return new GroqProvider(config, settings);
|
|
case 'xai':
|
|
return new XAIProvider(config, settings);
|
|
case 'custom':
|
|
return new CustomProvider(config, settings);
|
|
default:
|
|
throw new Error(`Unknown AI provider: ${config.provider}`);
|
|
}
|
|
}
|