/** * Ollama (local models). Chat goes through Ollama's OpenAI-compatible endpoint * (`{base}/v1/chat/completions`), which handles tools and streaming; discovery * uses the native API because it reports what each model can do: * GET /api/tags -> installed models * POST /api/show -> { capabilities: ["completion","tools","vision","thinking"], model_info } * https://docs.ollama.com/api ยท https://docs.ollama.com/openai */ import { AIError, type DiscoveredModel, type ModelCapabilities, type ProviderAdapter, type ProviderConfig, type TestResult } from '../types.ts'; import { num, runTest, str, TEST_TIMEOUT_MS } from './common.ts'; import { isRecord, requestJson, trimBaseUrl, type ErrorContext } from './http.ts'; import { compatEmbed, streamChatCompletions, type CompatOptions } from './openaiCompatible.ts'; const LABEL = 'Ollama'; const DEFAULT_BASE_URL = 'http://localhost:11434'; function root(cfg: ProviderConfig): string { return trimBaseUrl(cfg.baseUrl || DEFAULT_BASE_URL).replace(/\/(v1|api)$/, ''); } function ctx(cfg: ProviderConfig): ErrorContext { return { provider: LABEL, secrets: [cfg.apiKey, ...Object.values(cfg.headers ?? {})] }; } function headers(cfg: ProviderConfig): Record { const h: Record = { ...cfg.headers }; if (cfg.apiKey) h.Authorization = `Bearer ${cfg.apiKey}`; // only needed behind an authenticating proxy return h; } function compat(cfg: ProviderConfig): CompatOptions { return { kind: 'ollama', label: LABEL, baseUrl: `${root(cfg)}/v1`, apiKey: cfg.apiKey, headers: cfg.headers }; } async function listModels(cfg: ProviderConfig, signal?: AbortSignal, timeoutMs?: number): Promise { let tags: unknown; try { tags = await requestJson(`${root(cfg)}/api/tags`, { ...ctx(cfg), headers: headers(cfg), signal, firstByteTimeoutMs: timeoutMs ?? 15_000, totalTimeoutMs: timeoutMs ?? 30_000 }); } catch (err) { if (err instanceof AIError && err.code === 'network') { throw new AIError('network', `Could not reach Ollama at ${root(cfg)}. Is it running, and reachable from the Study Desk container?`, { retryable: true }); } throw err; } const list = isRecord(tags) && Array.isArray(tags.models) ? tags.models.filter(isRecord) : []; const models: DiscoveredModel[] = []; // /api/show is one cheap local call per model; cap it so a huge library cannot stall the settings page. await Promise.all( list.slice(0, 60).map(async (m) => { const id = str(m.model) || str(m.name); if (!id) return; const model: DiscoveredModel = { id, label: str(m.name) || id, priceIn: 0, priceOut: 0 }; try { const show = await requestJson>(`${root(cfg)}/api/show`, { ...ctx(cfg), headers: headers(cfg), body: { model: id }, signal, firstByteTimeoutMs: 10_000, totalTimeoutMs: 10_000, }); if (Array.isArray(show.capabilities)) { const caps = show.capabilities.map(String); if (!caps.includes('completion')) return; // embedding-only model const c: Partial = { tools: caps.includes('tools'), vision: caps.includes('vision'), reasoning: caps.includes('thinking'), pdf: false, temperature: true }; model.capabilities = c; } if (isRecord(show.model_info)) { const key = Object.keys(show.model_info).find((k) => k.endsWith('.context_length')); if (key && num(show.model_info[key])) model.contextWindow = num(show.model_info[key]); } } catch { /* older Ollama versions: keep the model with unknown capabilities */ } models.push(model); }), ); return models.sort((a, b) => a.id.localeCompare(b.id)); } async function test(cfg: ProviderConfig): Promise { return runTest(LABEL, async () => { const count = (await listModels(cfg, undefined, TEST_TIMEOUT_MS)).length; return count === 0 ? { modelCount: 0, note: 'Connected to Ollama, but no models are installed. Run `ollama pull ` first.' } : { modelCount: count }; }); } export const ollamaAdapter: ProviderAdapter = { kind: 'ollama', meta: { label: LABEL, defaultBaseUrl: DEFAULT_BASE_URL, requiresApiKey: false, requiresBaseUrl: false, supportsDiscovery: true, docsUrl: 'https://docs.ollama.com', keyHint: 'Not needed', }, stream: (cfg, req) => streamChatCompletions(compat(cfg), req), listModels: (cfg, signal) => listModels(cfg, signal), // Ollama serves OpenAI-style embeddings at /v1/embeddings (e.g. nomic-embed-text). embed: (cfg, req) => compatEmbed(compat(cfg), req), test, };