ollama.ts89 lines · main
1import { brivenError } from '@briven/shared';
2
3/**
4 * Minimal client for the flndrn Ollama gateway (ai.flndrn.com).
5 *
6 * Briven's in-product assistant runs on flndrn's OWN self-hosted Ollama —
7 * no per-token cost, fully independent (made in Flanders). We talk to it the
8 * same way ghostbot does: POST `{base}/chat` with a Bearer key, model
9 * qwen2.5-coder:14b (a coder model — excellent at emitting clean JSON).
10 *
11 * `format: 'json'` constrains the model to a single valid JSON document so
12 * the assistant's "build plan" parses deterministically with no scraping.
13 *
14 * Env (set on briven-france):
15 * OLLAMA_BASE_URL default https://ai.flndrn.com/api
16 * OLLAMA_MODEL default qwen2.5-coder:14b
17 * OLLAMA_API_KEY required — without it the assistant degrades gracefully
18 */
19
20const BASE = process.env.OLLAMA_BASE_URL ?? 'https://ai.flndrn.com/api';
21const MODEL = process.env.OLLAMA_MODEL ?? 'qwen2.5-coder:14b';
22const API_KEY = process.env.OLLAMA_API_KEY ?? '';
23
24/** True when an API key is present — UI uses this to fail gracefully. */
25export function assistantConfigured(): boolean {
26 return API_KEY.length > 0;
27}
28
29export interface ChatMessage {
30 readonly role: 'system' | 'user' | 'assistant';
31 readonly content: string;
32}
33
34/**
35 * One non-streaming chat completion. When `json` is true the model is
36 * constrained to a valid JSON document (Ollama `format: 'json'`). Returns
37 * the raw assistant text. Throws a brivenError (502/503) on misconfig or
38 * upstream failure so the route surfaces a clean, friendly error.
39 */
40export async function ollamaChat(
41 messages: readonly ChatMessage[],
42 opts: { json?: boolean; temperature?: number; timeoutMs?: number } = {},
43): Promise<string> {
44 if (!assistantConfigured()) {
45 throw new brivenError('assistant_unconfigured', 'the assistant is not configured yet', {
46 status: 503,
47 });
48 }
49 let res: Response;
50 try {
51 res = await fetch(`${BASE}/chat`, {
52 method: 'POST',
53 signal: AbortSignal.timeout(opts.timeoutMs ?? 90_000),
54 headers: {
55 'content-type': 'application/json',
56 authorization: `Bearer ${API_KEY}`,
57 },
58 body: JSON.stringify({
59 model: MODEL,
60 stream: false,
61 ...(opts.json ? { format: 'json' } : {}),
62 options: { temperature: opts.temperature ?? 0.2 },
63 messages,
64 }),
65 });
66 } catch (err) {
67 throw new brivenError('assistant_unreachable', 'could not reach the assistant — try again', {
68 status: 502,
69 cause: err instanceof Error ? err.message : String(err),
70 });
71 }
72 if (!res.ok) {
73 const detail = await res.text().catch(() => '');
74 throw new brivenError('assistant_upstream_error', `the assistant brain returned ${res.status}`, {
75 status: 502,
76 context: { detail: detail.slice(0, 300) },
77 });
78 }
79 const body = (await res.json().catch(() => null)) as { message?: { content?: string } } | null;
80 const content = body?.message?.content;
81 if (typeof content !== 'string' || content.trim() === '') {
82 throw new brivenError('assistant_empty', 'the assistant returned an empty answer', {
83 status: 502,
84 });
85 }
86 return content;
87}
88
89export { MODEL as ASSISTANT_MODEL };