UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

101 lines (100 loc) • 4.55 kB
import { r as normalizeTlsFingerprint } from "./client-address-utils-Clpzy8Fr.mjs"; import { r as parseInboundMediaUri } from "./inbound-media-uri-DpK4_BLW.mjs"; import { r as applyGatewayWebSocketTlsPin } from "./websocket-transport-CF478NbJ.mjs"; import { t as normalizeControlUiBasePath } from "./control-ui-shared-_tUmz6W8.mjs"; import { o as resolveAssistantMediaRoutePath } from "./control-ui-resource-routes-DgXN5Axb.mjs"; import { t as readByteStreamWithLimit } from "./read-byte-stream-with-limit-BjmaH2Xs.mjs"; import "./media-reference-DDon-yG0.mjs"; import { s as parseManagedOutgoingRoute } from "./managed-image-attachments-DSFNBym_.mjs"; import { n as MANAGED_OUTGOING_IMAGE_ARTIFACT_ID_PREFIX } from "./managed-outgoing-artifact-id-CdPx-kme.mjs"; import { n as decodeTuiImageData, r as prepareTuiImage, t as TUI_IMAGE_MAX_BYTES } from "./tui-image-data-D1b0y70Q.mjs"; import http from "node:http"; import https from "node:https"; //#region src/tui/gateway-image-loader.ts async function loadGatewayImage(e) { const { request: t } = e; const n = AbortSignal.any([t.signal, AbortSignal.timeout(3e4)]); n.throwIfAborted(); const r = decodeTuiImageData(t.source); if (r) return await prepareTuiImage(r, n); const i = parseInboundMediaUri(t.source); let a; let o = ""; let s = e.credentials; if (i) { o = normalizeControlUiBasePath(await e.readMediaBasePath(n)); a = new URL(resolveAssistantMediaRoutePath(o), "http://localhost"); a.searchParams.set("source", i.normalizedSource); a.searchParams.set("sessionKey", t.sessionKey); if (t.agentId) a.searchParams.set("agentId", t.agentId); } else { const r = t.source.startsWith("/api/chat/media/outgoing/") ? parseManagedOutgoingRoute(t.source) : null; if (!r) throw new Error("Image source is not managed by this Gateway"); const i = `${MANAGED_OUTGOING_IMAGE_ARTIFACT_ID_PREFIX}${r.attachmentId}`; if (t.artifactId && t.artifactId !== i) throw new Error("Image artifact does not match its source"); const o = await e.downloadArtifact(i, n); const c = o.url; const l = c ? parseManagedOutgoingRoute(c) : null; if (!c?.startsWith("/api/chat/media/outgoing/") || o.artifact.id !== i || o.artifact.type !== "image" || l?.attachmentId !== r.attachmentId || l.sessionKey !== r.sessionKey || o.artifact.sessionKey !== l.sessionKey) throw new Error("Image artifact is unavailable"); a = new URL(c, "http://localhost"); a.pathname = a.pathname.replace(/\/full$/, "/thumbnail"); s = []; } const c = e.connection; async function requestGatewayImage() { const e = new URL(c.url); if (e.protocol !== "ws:" && e.protocol !== "wss:") throw new Error("Invalid Gateway image transport"); const t = new URL(e); t.protocol = e.protocol === "wss:" ? "https:" : "http:"; const r = normalizeControlUiBasePath(e.pathname); t.pathname = `${o && r.endsWith(o) ? r.slice(0, -o.length) : r}${a.pathname}`; t.search = a.search; t.hash = ""; t.username = ""; t.password = ""; const i = normalizeTlsFingerprint(c.tlsFingerprint); if (c.tlsFingerprint && (!i || t.protocol !== "https:")) throw new Error("Invalid Gateway image TLS fingerprint"); for (const [e, r] of (s.length ? s : [""]).entries()) { n.throwIfAborted(); const a = { headers: { ...c.edgeAuthHeaders, accept: "image/png,image/jpeg,image/gif,image/webp", ...r ? { authorization: `Bearer ${r}` } : {} } }; if (i) applyGatewayWebSocketTlsPin(a, i); const o = (t.protocol === "https:" ? https : http).request(t, { method: "GET", headers: a.headers, signal: n, agent: false, ...i ? { rejectUnauthorized: a.rejectUnauthorized } : {} }); const l = new Promise((e, t) => { o.once("response", e); o.once("error", t); }); if (a.finishRequest) a.finishRequest(o); else o.end(); const u = await l; if (u.statusCode === 401 && e + 1 < s.length) { u.destroy(); continue; } try { if (u.statusCode !== 200) throw new Error(`Image preview request failed (${u.statusCode ?? "unknown"})`); if (Number(u.headers["content-length"]) > 12582912) throw new Error("Image exceeds the preview byte limit"); return await readByteStreamWithLimit(u, { maxBytes: TUI_IMAGE_MAX_BYTES, onOverflow: () => /* @__PURE__ */ new Error("Image exceeds the preview byte limit") }); } finally { u.destroy(); } } throw new Error("Image preview authentication failed"); } const l = await requestGatewayImage(); return await prepareTuiImage(l, n); } //#endregion export { loadGatewayImage };