/** * TypeSafe System One judgments: `POST /v1/systemone` (TypeSafe's path) and * `POST /alpha/decisions` (OpenRouter's Decisions path, same wire). * * Same bearer auth, model resolution, broker credential lookup, and a/b/c * credential rotation as the chat routes, dispatched through * {@link NativeJudge} — pi-ai's client for every judgment API, which * translates to the resolved model's own wire (OpenAI Decisions, Workers AI) * — so 401 rotation and 429/529 backoff match omp's own judgments. A judgment * is one JSON round-trip: no stream, no provider session state. Most judgment * APIs report tokens only, so the cost is priced from the catalog model for the * response header and the broker's observed-usage ledger. */ import { calculateCost } from "@oh-my-pi/pi-catalog/models"; import { logger } from "@oh-my-pi/pi-utils"; import * as AIError from "../../error"; import { classifyGatewayError } from "../../error/gateway"; import { checkNativeJudgmentImages, createNativeJudge, isJudgmentApi } from "../../judgment/native"; import * as systemOne from "../../providers/systemone-server"; import { deterministicUuid } from "../../utils/deterministic-id"; import { type AuthGatewayRouteOptions, buildGatewayApiKeyResolver, gatewayRequestLogFields, mirrorRequestAbort, recordGatewayUsage, resolveGatewayApiKey, } from "../dispatch"; import { gatewayResponseHeaders, json, noStoreErrorResponse, resolveClientIdentity } from "../http"; export async function handleSystemOne( bootOpts: AuthGatewayRouteOptions, req: Request, peer: string, ): Promise { const startedAt = performance.now(); const requestId = crypto.randomUUID(); const controller = mirrorRequestAbort(req); const aborted = (): Response => noStoreErrorResponse(499, "request_aborted", "client closed request"); if (controller.signal.aborted) return aborted(); let body: unknown; try { body = await req.json(); } catch (error) { if (controller.signal.aborted) return aborted(); return noStoreErrorResponse(400, "invalid_request_error", `Invalid JSON body: ${String(error)}`); } if (controller.signal.aborted) return aborted(); let parsed: systemOne.SystemOneParsedRequest; try { parsed = systemOne.parseRequest(body); } catch (error) { const message = error instanceof Error ? error.message : String(error); // TypeSafe answers body validation failures with 422. return noStoreErrorResponse(422, "invalid_request_error", message); } const model = bootOpts.resolveModel(parsed.modelId); if (!model) { return noStoreErrorResponse(404, "invalid_request_error", `Unknown model: ${parsed.modelId}`); } if (!isJudgmentApi(model.api)) { return noStoreErrorResponse( 422, "invalid_request_error", `Model ${parsed.modelId} does not answer System One judgments`, ); } // Model capability and dialect caps are known locally: reject before credential lookup or refresh. try { checkNativeJudgmentImages(model, parsed.request, parsed.imageAssets); } catch (error) { return noStoreErrorResponse(422, "invalid_request_error", error instanceof Error ? error.message : String(error)); } const client = resolveClientIdentity(req.headers); // Judgments carry no conversation to derive a session from. One sticky // session per model keeps credential selection stable across calls and // still lets `markUsageLimitReached` hand off to a sibling account. const sessionId = deterministicUuid(`systemone\u0000${model.provider}/${model.id}`); const apiKey = await resolveGatewayApiKey(bootOpts, model, sessionId, controller.signal, peer); if (controller.signal.aborted) return aborted(); if ("status" in apiKey) return noStoreErrorResponse(apiKey.status, apiKey.type, apiKey.message); const judge = createNativeJudge(model, { apiKey: buildGatewayApiKeyResolver(bootOpts, model, sessionId, apiKey, controller.signal, "systemone", peer), fetch: bootOpts.fetch, }); logger.info("auth-gateway request", { requestId, format: "systemone", model: parsed.modelId, resolvedProvider: model.provider, resolvedModel: model.id, stream: false, peer, ...gatewayRequestLogFields(bootOpts, apiKey), }); try { const result = await judge.judge(parsed.request, { signal: controller.signal, imageAssets: parsed.imageAssets, // A split judgment that fails still billed the chunks that succeeded. onPartialUsage: usage => { if (usage.cost.total === 0) calculateCost(model, usage); recordGatewayUsage(bootOpts.storage, model, client, usage); }, }); // Encode before pricing: the body's `cost` means "upstream billed this" // (OpenRouter), while the header and ledger carry the catalog estimate // when the upstream reported tokens only (TypeSafe). const body = systemOne.encodeResponse(result); if (result.usage.cost.total === 0) calculateCost(model, result.usage); recordGatewayUsage(bootOpts.storage, model, client, result.usage); return json(200, body, gatewayResponseHeaders(model, { requestId, costUsd: result.usage.cost.total, startedAt })); } catch (error) { if (controller.signal.aborted) return aborted(); const message = error instanceof Error ? error.message : String(error); const classified = error instanceof AIError.ValidationError ? { status: 422, type: "invalid_request_error", message } : classifyGatewayError(error); logger.warn("auth-gateway judgment failed", { format: "systemone", error: classified.message, peer }); return noStoreErrorResponse(classified.status, classified.type, classified.message); } }