Files
0pt/src/document-workbench/litellm-gateway-client.ts
T
admin e3c4bc28c3
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 6s
masterplan-lock / check (push) Successful in 6s
CI (SIAX Cloud) / security (push) Successful in 14s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 19s
CI (SIAX Cloud) / security (pull_request) Successful in 21s
Security Scan (SIAX Cloud) / security (push) Successful in 25s
CI (SIAX Cloud) / integration (pull_request) Successful in 29s
CI (SIAX Cloud) / quality (pull_request) Successful in 51s
CI (SIAX Cloud) / quality (push) Successful in 1m29s
feat(workbench): live LiteLLM gateway adapter + explicit project grants
- createInf0Gateway prefers the live OpenAI-compatible LiteLLM gateway and
  falls back to the native INF0 surface; admission stays local and fail-closed.
- Project authorization: OpenFGA when configured, otherwise a validated
  server-owned OPT_PROJECT_GRANTS list (deny by default).
- Tests for the gateway adapter, the factory and grants.
2026-11-02 14:50:50 +01:00

164 lines
5.4 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[];
}
/**
* INF0's live inference anchor today is the LiteLLM gateway (OpenAI-compatible,
* `ai-gateway.siax.io/v1`). The `/v1/inf0/*` SIAX surface is contract-only, so
* this adapter calls the live route while keeping the exact same admission
* semantics as the future native client: scope, allow-list, data class,
* external-transfer policy and token limits are all enforced locally before
* the request leaves the process.
*
* No provider key is placed in this repo: the caller passes a per-tenant
* LiteLLM virtual key resolved from the environment at runtime.
*/
export class LiteLlmGatewayClient {
private readonly baseUrl: string;
private readonly fetchImpl: typeof fetch;
constructor(
private readonly options: {
baseUrl: string;
apiKey: string;
providerKind?: 'local' | 'external';
fetchImpl?: typeof fetch;
timeoutMs?: number;
},
) {
let url: URL;
try {
url = new URL(options.baseUrl);
} catch {
throw new Error('Invalid INF0 gateway URL');
}
if (
url.protocol !== 'https:' ||
url.username ||
url.password ||
url.search ||
url.hash ||
!options.apiKey.trim()
) {
throw new Error('A trusted HTTPS INF0 gateway and key are required');
}
this.baseUrl = url.toString().replace(/\/+$/, '');
this.fetchImpl = options.fetchImpl ?? fetch;
this.options = { ...options };
}
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 (
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}/chat/completions`, {
method: 'POST',
redirect: 'error',
signal: AbortSignal.timeout(this.options.timeoutMs ?? 120_000),
headers: {
authorization: `Bearer ${this.options.apiKey}`,
'content-type': 'application/json',
'x-tenant-id': context.tenantId,
'x-siax-project-id': context.projectId ?? '',
'x-actor-id': context.actorId,
'x-siax-correlation-id': context.correlationId,
},
body: JSON.stringify({
model: admission.modelId,
messages,
max_tokens: admission.maxOutputTokens,
temperature: 0,
stream: false,
}),
});
} 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;
choices?: Array<{ message?: { content?: unknown }; finish_reason?: unknown }>;
usage?: { prompt_tokens?: unknown; completion_tokens?: unknown; total_tokens?: unknown };
} | null;
const content = result?.choices?.[0]?.message?.content;
const promptTokens = result?.usage?.prompt_tokens;
const completionTokens = result?.usage?.completion_tokens;
const totalTokens = result?.usage?.total_tokens;
if (
!result ||
typeof content !== 'string' ||
typeof result.model !== 'string' ||
!result.model.trim() ||
!Number.isSafeInteger(promptTokens) ||
!Number.isSafeInteger(completionTokens) ||
!Number.isSafeInteger(totalTokens) ||
(promptTokens as number) < 0 ||
(promptTokens as number) > admission.maxInputTokens ||
(completionTokens as number) > admission.maxOutputTokens ||
(promptTokens as number) + (completionTokens as number) !== totalTokens
) {
throw new Inf0OutcomeUnknownError();
}
const providerKind = this.options.providerKind ?? 'external';
if (policy.destination === 'internal' && providerKind !== 'local') {
throw new Inf0OutcomeUnknownError();
}
const finish = result.choices?.[0]?.finish_reason;
return {
status:
finish === 'stop' && (completionTokens as number) > 0 && content.trim()
? 'model_stopped'
: 'needs_review',
content,
modelExecuted: result.model,
providerKind,
promptTokens: promptTokens as number,
completionTokens: completionTokens as number,
totalTokens: totalTokens as number,
};
}
}