import type { LLMProvider, ChatCompletionParams } from "./types.js"; export class OpenAIProvider implements LLMProvider { private apiKey: string; private baseUrl: string; constructor(apiKey: string, baseUrl: string = "https://api.openai.com/v1") { this.apiKey = apiKey; this.baseUrl = baseUrl.replace(/\/+$/, ""); } async chatCompletion(params: ChatCompletionParams): Promise { const response = await fetch(`${this.baseUrl}/chat/completions`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}`, }, body: JSON.stringify({ model: params.model, temperature: params.temperature, response_format: { type: "json_object" }, messages: [ { role: "system", content: params.systemPrompt }, { role: "user", content: params.userMessage }, ], }), }); if (!response.ok) { const err = await response.json().catch(() => ({})); throw new Error( (err as { error?: { message?: string } }).error?.message || `API error: ${response.status}` ); } const data = await response.json(); return data.choices[0].message.content; } }