UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

135 lines (134 loc) • 5 kB
import { t as captureSecretRedactionRegistrySnapshot } from "./secret-redaction-registry-Cy0acctn.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { r as findConfiguredProviderModel } from "./model-provider-config-Bbah_2ON.mjs"; import { t as normalizeExactAllowedHost } from "./exact-hostname-DgSGIR8G.mjs"; import { a as resolveProviderModelRoutes, t as createProviderModelCatalogIdNormalizer } from "./provider-model-routes-CbP035Gi.mjs"; import { t as resolveSecretRefString } from "./resolve-BYfGhigB.mjs"; import { g as resolveProviderConfigSecretInput } from "./model-auth-provider-config-Bnc4upRS.mjs"; import { t as withJobSecretEgress } from "./job-egress-ByAPVGG3.mjs"; import { StringDecoder } from "node:string_decoder"; //#region src/logging/redacting-stream.ts function createRedactingStreamWriter(e, t) { const n = new StringDecoder("utf8"); const r = t.filter(Boolean).toSorted((e, t) => t.length - e.length); const i = new Set(r.map((e) => e.charAt(0))); let a = ""; const emit = (t) => { let n = ""; let o = 0; while (o < a.length) { const e = i.has(a.charAt(o)) ? r.find((e) => a.startsWith(e, o) || !t && a.length - o < e.length && e.startsWith(a.slice(o))) : void 0; if (!e) { n += a[o]; o += 1; } else if (o + e.length > a.length) break; else { n += "<redacted>"; o += e.length; } } a = a.slice(o); return !n || e.write(n); }; return { write: (e) => { a += n.write(e); return emit(false); }, flush: () => { a += n.end(); emit(true); } }; } //#endregion //#region src/secrets/model-egress.ts function hasEntries(e) { return e !== void 0 && Object.keys(e).length > 0; } function resolveModelEgressSelection(e) { const t = normalizeProviderId(e.provider); const n = e.model.trim(); if (!t || !n) throw new Error("Model egress requires an explicit provider and model"); const { providerConfig: r, ref: i } = resolveProviderConfigSecretInput(e.config, t); if (!r) throw new Error("Model egress requires a configured provider"); if (!i || r.auth !== void 0 && r.auth !== "api-key") throw new Error("Model egress requires a configured API-key SecretRef; auth profiles and OAuth are unsupported"); const a = findConfiguredProviderModel(r, t, n, createProviderModelCatalogIdNormalizer(t)); if (hasEntries(r.headers) || hasEntries(a?.headers) || hasEntries(r.request) || r.authHeader === false || r.localService !== void 0) throw new Error("Model egress does not support custom request headers, authentication, proxy, TLS, or local-service configuration"); const o = resolveProviderModelRoutes({ provider: t, modelId: n, config: e.config }); if (o?.kind === "incompatible") throw new Error(o.message); const s = o ? o.kind === "routes" ? o.routes.find((e) => e.authRequirement === "api-key") : void 0 : { api: a?.api ?? r.api, baseUrl: a?.baseUrl ?? r.baseUrl }; if (!s?.baseUrl || s.api !== "openai-responses" && s.api !== "openai-completions") throw new Error("Model egress requires an OpenAI-compatible API-key model route"); let c; try { c = new URL(s.baseUrl); } catch { throw new Error("Model egress requires a valid HTTPS model base URL"); } if (c.protocol !== "https:" || c.port || c.username || c.password || c.search || c.hash) throw new Error("Model egress requires an HTTPS model base URL on port 443 without credentials, query, or fragment"); return { provider: t, model: n, ref: i, baseUrl: c.href, allowedHosts: [normalizeExactAllowedHost(c.hostname)] }; } /** Owns one explicit CLI job's protected model credential and its revocable egress path. */ async function withConfiguredModelEgress(e, t) { e.signal?.throwIfAborted(); const n = resolveModelEgressSelection(e); const r = await resolveSecretRefString(n.ref, { config: e.config }); e.signal?.throwIfAborted(); return withJobSecretEgress({ secrets: [{ envName: "OPENAI_API_KEY", name: `${n.provider} model API key`, value: r, allowedHosts: n.allowedHosts }], signal: e.signal, allowedHosts: n.allowedHosts }, async (r) => { try { var i = _usingCtx(); const a = i.a(new AsyncDisposableStack()); const o = [...captureSecretRedactionRegistrySnapshot().values, r.hostEnv.HTTPS_PROXY]; const output = (t) => createRedactingStreamWriter({ write(n) { e.onOutput?.(n, t); return true; } }, o); const s = output("stdout"); const c = output("stderr"); a.defer(s.flush); a.defer(c.flush); const l = await t({ sentinel: r.remoteEnv.OPENAI_API_KEY, baseUrl: n.baseUrl, model: n.model, allowedHosts: n.allowedHosts, hostEnv: r.hostEnv, caBundle: r.caBundle, onOutputChunk: e.onOutput ? (e, t) => { (t === "stdout" ? s : c).write(e); } : void 0 }); e.signal?.throwIfAborted(); return l; } catch (e) { i.e = e; } finally { await i.d(); } }); } //#endregion export { withConfiguredModelEgress };