/** * Ollama REST API client (background only). * * Uses the browser Fetch API directly against the Ollama REST API. * - GET {host}/api/tags : list installed models * - POST {host}/api/chat : streaming chat completion (NDJSON) * * Security rules implemented here (Security.md ยง4): * - the host is re-validated immediately before each request; * - NDJSON is parsed with a buffering line reader that tolerates chunk * boundaries falling inside a JSON object; * - requests can be aborted via AbortController. */ import { isValidOllamaHost } from '../shared/settings-schema.js'; import { ERROR_CODES } from '../shared/constants.js'; /** * Translate a fetch/HTTP failure into a stable error code. * @param {Response|undefined} response * @returns {string} error code */ function statusCodeToError(status) { if (status === 404) return ERROR_CODES.MODEL_NOT_FOUND; if (status === 400 || status === 403) return ERROR_CODES.INVALID_SETTINGS; if (status >= 500) return ERROR_CODES.STREAM_ERROR; return ERROR_CODES.STREAM_ERROR; } /** * Assert that the host is allowed. Throws Error with code attached. * @param {string} host */ function assertAllowedHost(host) { if (!isValidOllamaHost(host)) { const error = new Error('Invalid Ollama host'); error.code = ERROR_CODES.INVALID_SETTINGS; throw error; } } /** * Normalize an error thrown by fetch into an extension error code. * @param {*} error * @param {{aborted?: boolean, code?: string}} [options] * @returns {Error} */ export function toExtensionError(error, { aborted = false, code = ERROR_CODES.OLLAMA_UNAVAILABLE } = {}) { if (error && error.name === 'AbortError') { const e = new Error('Request aborted'); e.code = ERROR_CODES.REQUEST_ABORTED; return e; } if (error && error.code) return error; // fetch() rejects on network failures (Ollama not running etc.) const e = new Error(error instanceof Error ? error.message : 'Unknown Ollama error'); e.code = aborted ? ERROR_CODES.REQUEST_ABORTED : code; return e; } /** * List models installed in Ollama. * @param {string} host validated host * @param {AbortSignal} [signal] * @returns {Promise} model names */ export async function listModels(host, signal) { assertAllowedHost(host); let response; try { response = await fetch(`${host}/api/tags`, { method: 'GET', signal }); } catch (error) { throw toExtensionError(error); } if (!response.ok) { const e = new Error(`Ollama /api/tags failed: ${response.status}`); e.code = response.status === 404 ? ERROR_CODES.MODEL_NOT_FOUND : ERROR_CODES.STREAM_ERROR; throw e; } const data = await response.json(); if (!data || !Array.isArray(data.models)) return []; return data.models.map((m) => (m && typeof m.name === 'string' ? m.name : '')).filter((name) => name.length > 0); } /** * Stream a chat completion. Calls handlers for each incremental text chunk. * * @param {object} params * @param {string} params.host validated Ollama host * @param {string} params.model * @param {Array<{role: string, content: string}>} params.messages * @param {boolean|string} [params.think] true | 'low' | 'medium' | 'high'; omit when off * @param {object} params.options Ollama options object * @param {(text: string, thinking: string) => void} params.onChunk * @param {AbortSignal} [params.signal] * @returns {Promise} resolves when the stream completes */ export async function chatStream({ host, model, messages, think, options, onChunk, signal }) { assertAllowedHost(host); const body = { model, messages, stream: true }; if (think !== undefined) { body.think = think; } if (options && Object.keys(options).length > 0) { body.options = options; } let response; try { response = await fetch(`${host}/api/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), signal }); } catch (error) { throw toExtensionError(error, { aborted: isAbortError(error) }); } if (!response.ok) { const e = new Error(`Ollama /api/chat failed: ${response.status}`); e.code = response.status === 404 ? ERROR_CODES.MODEL_NOT_FOUND : response.status === 400 || response.status === 403 ? ERROR_CODES.INVALID_SETTINGS : ERROR_CODES.STREAM_ERROR; throw e; } if (!response.body) { const e = new Error('Ollama response has no body'); e.code = ERROR_CODES.STREAM_ERROR; throw e; } const reader = response.body.getReader(); const decoder = new TextDecoder(); let buffer = ''; const processLine = (line) => { if (!line) return; let parsed; try { parsed = JSON.parse(line); } catch { // Malformed line: skip it rather than crashing the stream. return; } if (parsed === null || typeof parsed !== 'object') return; if (parsed.error) { const e = new Error(String(parsed.error)); e.code = typeof parsed.error === 'string' && /not found/i.test(parsed.error) ? ERROR_CODES.MODEL_NOT_FOUND : ERROR_CODES.STREAM_ERROR; throw e; } const content = parsed.message && typeof parsed.message.content === 'string' ? parsed.message.content : ''; const thinking = parsed.message && typeof parsed.message.thinking === 'string' ? parsed.message.thinking : ''; if (content || thinking) { onChunk(content, thinking); } }; try { for (;;) { const { done, value } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); let newlineIndex; while ((newlineIndex = buffer.indexOf('\n')) !== -1) { const line = buffer.slice(0, newlineIndex).replace(/\r$/, ''); buffer = buffer.slice(newlineIndex + 1); processLine(line); } } // Flush any remaining buffered line. const tail = buffer.replace(/\r$/, ''); if (tail) processLine(tail); } catch (error) { if (isAbortError(error)) { const e = new Error('Request aborted'); e.code = ERROR_CODES.REQUEST_ABORTED; throw e; } throw error && error.code ? error : toExtensionError(error, { code: ERROR_CODES.STREAM_ERROR }); } } /** * @param {*} error * @returns {boolean} */ function isAbortError(error) { return Boolean(error && (error.name === 'AbortError' || error.code === ERROR_CODES.REQUEST_ABORTED)); }