/** * Resolve the live INF0 inference path. * * Preference order: * 1. DECISION_OLLAMA_URL + DECISION_OLLAMA_MODEL (self-hosted model on the * SIAX decision server; interim path while the external model API is on * hold — no provider key, no per-token cost, nothing leaves the fleet) * 2. INF0_GATEWAY_LITELLM_KEY + INF0_GATEWAY_URL (live LiteLLM) * 3. INF0_URL + INF0_API_TOKEN (native `/v1/inf0/*` surface, contract-only) * * Returns `undefined` when neither is configured, so the caller can fail * closed with an explicit "not configured" response. */ import { DecisionOllamaGatewayClient } from './decision-ollama-client'; import { Inf0GatewayClient } from './inf0-gateway-client'; import { LiteLlmGatewayClient } from './litellm-gateway-client'; import { isTrustedHttpsUrl } from './runtime-config'; export interface Inf0Gateway { complete(input: Parameters[0]): ReturnType; } export function createInf0Gateway(env: NodeJS.ProcessEnv = process.env): Inf0Gateway | undefined { const decisionUrl = env.DECISION_OLLAMA_URL?.trim(); const decisionModel = env.DECISION_OLLAMA_MODEL?.trim(); if (decisionUrl && decisionModel) { try { return new DecisionOllamaGatewayClient({ baseUrl: decisionUrl, model: decisionModel, }); } catch { return undefined; } } const litellmKey = env.INF0_GATEWAY_LITELLM_KEY?.trim(); const litellmUrl = env.INF0_GATEWAY_URL?.trim() || 'https://ai-gateway.siax.io/v1'; if (litellmKey && isTrustedHttpsUrl(litellmUrl)) { try { return new LiteLlmGatewayClient({ baseUrl: litellmUrl, apiKey: litellmKey, providerKind: env.INF0_GATEWAY_PROVIDER_KIND === 'local' ? 'local' : 'external', }); } catch { return undefined; } } const baseUrl = env.INF0_URL ?? env.INF0_API_URL; const token = env.INF0_API_TOKEN ?? env.INF0_SERVICE_TOKEN; if (isTrustedHttpsUrl(baseUrl) && token?.trim()) { try { return new Inf0GatewayClient({ baseUrl, serviceToken: token.trim() }); } catch { return undefined; } } return undefined; }