CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 5s
Security Scan (SIAX Cloud) / security (push) Successful in 14s
CI (SIAX Cloud) / security (push) Successful in 17s
masterplan-lock / check (push) Successful in 17s
CI (SIAX Cloud) / security (pull_request) Successful in 20s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 21s
CI (SIAX Cloud) / integration (pull_request) Successful in 54s
CI (SIAX Cloud) / quality (push) Successful in 1m24s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m29s
The 9B model spent its whole 4000-token budget on reasoning and came back model_incomplete. Call /api/chat with format=json, think=false and num_predict, and read prompt_eval_count/eval_count/done_reason. Structured extraction no longer needs chain-of-thought.
184 lines
6.3 KiB
TypeScript
184 lines
6.3 KiB
TypeScript
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<Inf0GatewayReceipt> {
|
|
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,
|
|
};
|
|
}
|
|
}
|