UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

128 lines (127 loc) • 5.16 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { u as redactToolPayloadText } from "./redact-Mj7pppxC.mjs"; import { n as authorizeOperatorScopesForMethod } from "./method-scopes-Dpyb0Ck-.mjs"; import { n as SessionWorkStartError } from "./session-work-start-CFJs8WNp.mjs"; import { t as ToolAuthorizationError } from "./tool-input-error-B1hgavSw.mjs"; import { a as TLS_CERTIFICATE_ERROR_USER_TEXT } from "./user-copy-DFvWpx47.mjs"; import { i as describeFailoverError, p as resolveFailoverStatus } from "./failover-error-CbIiC30n.mjs"; import { d as sendMethodNotAllowed, f as sendMissingScopeForbidden, s as readJsonBodyOrError } from "./http-common-FfhIIwOW.mjs"; import { r as authorizeGatewayHttpRequestOrReply, u as resolveSharedSecretHttpOperatorScopes } from "./http-auth-utils-B0FI4CE4.mjs"; import "./http-utils-B5V9Okzh.mjs"; //#region src/gateway/http-endpoint-helpers.ts async function handleGatewayPostJsonEndpoint(e, t, n) { if (new URL(e.url ?? "/", "http://localhost").pathname !== n.pathname) return false; if (e.method !== "POST") { sendMethodNotAllowed(t); return; } const r = await authorizeGatewayHttpRequestOrReply({ ...n, req: e, res: t }); if (!r) return; const i = resolveSharedSecretHttpOperatorScopes(e, r); const a = authorizeOperatorScopesForMethod(n.requiredOperatorMethod, i); if (!a.allowed) { sendMissingScopeForbidden(t, a.missingScope); return; } const o = await readJsonBodyOrError(e, t, n.maxBodyBytes, 6e4); if (o === void 0) return; try { await r.revalidate(); } catch (e) { if (t.writableEnded || t.destroyed) return; throw e; } return { body: o, requestAuth: r, operatorScopes: i }; } //#endregion //#region src/gateway/openai-compat-errors.ts const ERROR_TYPE_BY_REASON = { auth: "authentication_error", auth_permanent: "permission_error", format: "invalid_request_error", rate_limit: "rate_limit_error", overloaded: "api_error", billing: "insufficient_quota", server_error: "api_error", timeout: "api_error", tls_certificate: "api_error", context_overflow: "invalid_request_error", model_not_found: "invalid_request_error", session_expired: "invalid_request_error", empty_response: void 0, no_error_details: void 0, unclassified: void 0, unknown: void 0 }; function resolveOpenAiCompatError(e) { if (e instanceof SessionWorkStartError && e.reason === "archived") return { status: 409, error: { message: "Session is archived. Restore it in the Control UI before retrying.", type: "invalid_request_error" } }; if (e instanceof ToolAuthorizationError) return { status: 403, error: { message: e.message, type: "permission_error" } }; const t = describeFailoverError(e); const n = t.reason; if (!n) return; const r = ERROR_TYPE_BY_REASON[n]; if (!r) return; let i = t.status ?? resolveFailoverStatus(n) ?? 500; let a; if (n === "tls_certificate") a = TLS_CERTIFICATE_ERROR_USER_TEXT; else if (n === "server_error" || n === "timeout") { i = t.status && t.status >= 400 && t.status < 500 ? t.status : n === "timeout" ? 504 : 502; a = n === "timeout" ? "upstream provider timeout" : "upstream provider error"; } else a = n === "overloaded" ? "upstream provider overloaded" : t.rawError?.trim() || t.message.trim() || "request failed"; return { status: i, error: { message: redactToolPayloadText(a), type: r, ...t.code ? { code: redactToolPayloadText(t.code) } : {} } }; } /** Validates sampling ranges after the HTTP request schema admits numeric fields. */ function validateOpenAiSamplingParams(e) { if (e.temperature != null && (e.temperature < 0 || e.temperature > 2)) return "`temperature` must be between 0 and 2."; if (e.topP != null && (e.topP < 0 || e.topP > 1)) return "`top_p` must be between 0 and 1."; if (e.frequencyPenalty != null && (e.frequencyPenalty < -2 || e.frequencyPenalty > 2)) return "`frequency_penalty` must be between -2.0 and 2.0."; if (e.presencePenalty != null && (e.presencePenalty < -2 || e.presencePenalty > 2)) return "`presence_penalty` must be between -2.0 and 2.0."; if (e.seed != null && !Number.isInteger(e.seed)) return "`seed` must be an integer."; } function resolveOpenAiStreamParams(e) { const t = Object.entries(e).filter(([, e]) => e !== void 0); return t.length ? Object.fromEntries(t) : void 0; } function resolveResponseFormat(e) { if (e == null) return; if (!isRecord(e)) throw new Error("response_format must be an object"); const t = e.type; if (t !== "text" && t !== "json_object" && t !== "json_schema") throw new Error("response_format.type must be text, json_object, or json_schema"); return e; } function resolveStopSequences(e) { if (e == null) return; const t = typeof e === "string" ? [e] : e; if (t.length > 4) throw new Error("stop supports at most 4 sequences"); if (t.some((e) => e.length === 0)) throw new Error("stop entries must be non-empty strings"); return t.length > 0 ? t : void 0; } //#endregion export { validateOpenAiSamplingParams as a, resolveStopSequences as i, resolveOpenAiStreamParams as n, handleGatewayPostJsonEndpoint as o, resolveResponseFormat as r, resolveOpenAiCompatError as t };