import { admitModelExecution, type ModelAdmissionRequest, type ModelBudgetReservation, type ModelExecutionPolicy, } from './model-admission'; import { Inf0OutcomeUnknownError, type Inf0GatewayReceipt } from './inf0-gateway-client'; import type { SiaxRequestContext } from '../platform/siax-context'; type Message = { role: 'system' | 'user' | 'assistant'; content: string }; export interface GatewayCompleteInput { context: SiaxRequestContext; admission: ModelAdmissionRequest; policy: ModelExecutionPolicy; reservation: ModelBudgetReservation | undefined; inputTokens: number; messages: readonly Message[]; } /** * Self-hosted Ollama on the SIAX decision server (server-decision-01). * * This is the interim AI path used while the external model API is on hold: * the same local model that serves the Jev/Laya plane, reached over the * tailnet. No provider key, no per-token cost and no customer document leaves * the fleet. Admission is enforced locally exactly like the INF0 paths. * * The endpoint must be an explicit, server-owned internal address: plain HTTP * is only accepted for loopback, RFC1918 or tailnet (100.64.0.0/10) hosts. */ function isInternalAddress(hostname: string): boolean { const ipv4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(hostname); if (!ipv4) return hostname === 'localhost'; const [a, b] = [Number(ipv4[1]), Number(ipv4[2])]; if (ipv4.slice(1).some((part) => Number(part) > 255)) return false; if (a === 127) return true; if (a === 10) return true; if (a === 192 && b === 168) return true; if (a === 172 && b >= 16 && b <= 31) return true; if (a === 100 && b >= 64 && b <= 127) return true; // tailnet CGNAT range return false; } export function assertTrustedDecisionOllamaUrl(value: string): URL { let url: URL; try { url = new URL(value); } catch { throw new Error('Invalid decision Ollama URL'); } const httpOk = url.protocol === 'http:' && isInternalAddress(url.hostname); const httpsOk = url.protocol === 'https:'; if ((!httpOk && !httpsOk) || url.username || url.password || url.search || url.hash || !url.hostname) { throw new Error('Decision Ollama URL must be a trusted internal endpoint'); } return url; } export class DecisionOllamaGatewayClient { private readonly baseUrl: string; private readonly fetchImpl: typeof fetch; constructor( private readonly options: { baseUrl: string; model: string; fetchImpl?: typeof fetch; timeoutMs?: number; }, ) { const url = assertTrustedDecisionOllamaUrl(options.baseUrl); if (!options.model.trim()) throw new Error('Decision Ollama model is required'); this.baseUrl = url.toString().replace(/\/+$/, ''); this.fetchImpl = options.fetchImpl ?? fetch; } async complete(input: GatewayCompleteInput): Promise { const { context, admission, policy, reservation, inputTokens, messages } = structuredClone(input); const decision = admitModelExecution(admission, policy, reservation); if (!decision.allowed) { throw new Error(`Model execution denied: ${decision.reason}`); } if (policy.destination !== 'internal') { throw new Error('Model execution denied: decision Ollama is an internal route'); } if ( context.tenantId !== admission.tenantId || context.projectId !== admission.projectId || !context.actorId?.trim() || !context.correlationId?.trim() || !Number.isSafeInteger(inputTokens) || inputTokens <= 0 || inputTokens > admission.maxInputTokens || !Array.isArray(messages) || messages.length === 0 ) { throw new Error('Invalid scoped model request'); } let response: Response; try { response = await this.fetchImpl(`${this.baseUrl}/api/chat`, { method: 'POST', redirect: 'error', signal: AbortSignal.timeout(this.options.timeoutMs ?? 120_000), headers: { 'content-type': 'application/json', 'x-tenant-id': context.tenantId, 'x-actor-id': context.actorId, 'x-siax-correlation-id': context.correlationId, }, body: JSON.stringify({ model: this.options.model, messages, stream: false, // Structured extraction: JSON mode, no chain-of-thought output, and a // hard output budget. A thinking model otherwise spends the whole // budget on reasoning and returns model_incomplete. format: 'json', think: false, options: { temperature: 0, num_predict: admission.maxOutputTokens, num_ctx: Math.max( 4096, Math.min(131072, admission.maxInputTokens + admission.maxOutputTokens + 1024), ), }, }), }); } catch { throw new Inf0OutcomeUnknownError(); } if (!response.ok) throw new Inf0OutcomeUnknownError(); let decoded: unknown; try { decoded = await response.json(); } catch { throw new Inf0OutcomeUnknownError(); } const result = decoded as { model?: unknown; message?: { content?: unknown }; done?: unknown; done_reason?: unknown; prompt_eval_count?: unknown; eval_count?: unknown; } | null; const content = result?.message?.content; const promptTokens = result?.prompt_eval_count; const completionTokens = result?.eval_count; if ( !result || typeof content !== 'string' || typeof result.model !== 'string' || !result.model.trim() || !Number.isSafeInteger(promptTokens) || !Number.isSafeInteger(completionTokens) || (promptTokens as number) < 0 || (promptTokens as number) > admission.maxInputTokens || (completionTokens as number) > admission.maxOutputTokens ) { throw new Inf0OutcomeUnknownError(); } const totalTokens = (promptTokens as number) + (completionTokens as number); const finish = result.done_reason; return { status: finish === 'stop' && (completionTokens as number) > 0 && content.trim() ? 'model_stopped' : 'needs_review', content, modelExecuted: result.model, providerKind: 'local', promptTokens: promptTokens as number, completionTokens: completionTokens as number, totalTokens: totalTokens as number, }; } }