All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 33s
- Drag-to-reorder for class materials, folders, quiz questions and deck cards. Pointer-based so touch works, plus keyboard (arrows/Home/End) with screen-reader announcements. - Timed exams get a wall-clock deadline. The server refuses late answers, submits overdue exams with what was saved, and the countdown keeps running while the page is closed. - Shared quiz sessions keep a frozen copy of their questions and flag later owner edits. - Scanned PDFs are read in cached 8-page batches with real page numbers (pdf-lib), removing the 30 MB whole-file limit. Compatible providers now receive PDFs as file parts. - Semantic search: embeddings for OpenAI, Gemini, Ollama and compatible servers, background indexing into chunk_vectors (migration 2), hybrid keyword + vector ranking for search, the assistant and focused generation, and a Settings -> AI section. 181 tests pass locally and in the Docker test stage on Node 24. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
4.6 KiB
TypeScript
104 lines
4.6 KiB
TypeScript
/**
|
|
* 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<string, string> {
|
|
const h: Record<string, string> = { ...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<DiscoveredModel[]> {
|
|
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<Record<string, unknown>>(`${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<ModelCapabilities> = { 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<TestResult> {
|
|
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 <model>` 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,
|
|
};
|