import type { AIProvider, AIProviderConfig, AIProviderResult } from './provider'; export class OllamaProvider implements AIProvider { provider = 'ollama' as const; private baseUrl: string; private model: string; constructor(config: AIProviderConfig) { this.baseUrl = config.baseUrl || 'http://localhost:11434'; this.model = config.model || 'llama3.2'; } async generate(prompt: string, systemPrompt?: string, options?: { jsonMode?: boolean }): Promise { const requestBody = { model: this.model, stream: false, messages: [ ...(systemPrompt ? [{ role: 'system', content: systemPrompt }] : []), { role: 'user', content: prompt }, ], }; const response = await fetch(`${this.baseUrl}/api/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(requestBody), }); const responseData = await response.json(); if (!response.ok) { throw new Error(`Ollama API error: ${response.status} ${JSON.stringify(responseData)}`); } let content = responseData.message?.content || ''; let title: string | undefined; if (options?.jsonMode) { try { const parsed = JSON.parse(content); title = parsed.title; content = parsed.content || content; } catch { // If JSON parsing fails, use content as-is } } return { content, title, request: requestBody, response: responseData, }; } async validate(): Promise { try { const response = await fetch(`${this.baseUrl}/api/tags`); return response.ok; } catch { return false; } } }