UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

136 lines (135 loc) • 4.74 kB
import { t as LruCache } from "./lru-cache-wquJPIr-.mjs"; import { n as ok, t as err } from "./result-CV1-31DB.mjs"; import { k as normalizeSessionKeyPreservingOpaquePeerIds } from "./session-key-CqAyZeVk.mjs"; import { r as createLazyRuntimeModule } from "./lazy-runtime-zGjszCtt.mjs"; import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs"; import "./io-4QsrsWTJ.mjs"; import { r as sessionDeliveryOrigin } from "./delivery-context.read-BrYAJ7RK.mjs"; import { t as captureSessionEntryMetadataRead } from "./session-entry-source-authority-Bxiv7qVT.mjs"; import { n as matchesChannelAvatarSender } from "./channel-avatar-reference-BDD5BKs4.mjs"; import { i as parseControlUiResourcePath } from "./control-ui-resource-routes-DgXN5Axb.mjs"; import "./control-ui-contract-DTmt_Jww.mjs"; import { r as respondNotFound } from "./control-ui-http-utils-DH5C3tE0.mjs"; import { d as sendMethodNotAllowed, m as sendSessionReadForbidden } from "./http-common-FfhIIwOW.mjs"; import { l as readMediaBuffer } from "./store-zsTAVy24.mjs"; import { n as resolveInboundMediaReference } from "./media-reference-DDon-yG0.mjs"; import { n as authorizeControlUiSessionOwnerReadRequestOrReply } from "./http-auth-utils-B0FI4CE4.mjs"; import "./http-utils-B5V9Okzh.mjs"; import { o as resolveHttpImageRepresentation, s as sendHttpImageResponse, t as HTTP_IMAGE_MAX_BYTES } from "./http-image-response-BMmTktPR.mjs"; //#region src/gateway/channel-avatar-http.ts const channelAvatarCache = new LruCache(128); const channelAvatarLoads = /* @__PURE__ */ new Map(); const getSessionStoreModule = createLazyRuntimeModule(() => import("./session-utils-store-worker-DXIMwV0W.mjs")); async function loadChannelAvatar(e, t) { const n = channelAvatarCache.get(e); if (n?.reference === t) return n.image; const r = JSON.stringify([e, t]); let i = channelAvatarLoads.get(r); if (!i) { i = (async () => { const n = await resolveInboundMediaReference(t); if (!n) return; const r = await readMediaBuffer(n.id, "inbound", HTTP_IMAGE_MAX_BYTES); const i = await resolveHttpImageRepresentation(n.id, r.buffer); if (i) channelAvatarCache.set(e, { reference: t, image: i }); return i; })().finally(() => { channelAvatarLoads.delete(r); }); channelAvatarLoads.set(r, i); } return i; } /** Serves the current channel-avatar snapshot for an owner-visible session. */ async function handleChannelAvatarHttpRequest(e, t, n) { const r = new URL(e.url ?? "/", "http://localhost"); const i = r.pathname; const a = parseControlUiResourcePath("channelAvatar", i, n.basePath); if (!a.matched) return false; if (e.method !== "GET" && e.method !== "HEAD") { sendMethodNotAllowed(t, "GET, HEAD"); return true; } const o = a.value; const s = o ? normalizeSessionKeyPreservingOpaquePeerIds(o) : void 0; let c; try { c = ok(s ? captureSessionEntryMetadataRead({ sessionKey: s }, () => {}) : void 0); } catch (e) { c = err(e); } const l = await authorizeControlUiSessionOwnerReadRequestOrReply({ ...n, req: e, res: t }); if (!l) return true; l.assertCurrent(); if (!c.ok) throw c.error; const u = c.value; if (!o || !s) { t.setHeader("cache-control", "no-store"); respondNotFound(t); return true; } const serve = async (n, r) => { r(); if (!n) { t.setHeader("cache-control", "no-store"); respondNotFound(t); return true; } let i; try { i = await loadChannelAvatar(s, n); } catch {} r(); if (!i) { t.setHeader("cache-control", "no-store"); respondNotFound(t); return true; } sendHttpImageResponse({ req: e, res: t, image: i, filename: "channel-avatar" }); return true; }; if (u) { const e = u.readCurrent(); if (e && !l.canReadSession(s, e)) { sendSessionReadForbidden(t); return true; } const n = sessionDeliveryOrigin(e); const i = n?.avatar; const assertCurrent = () => { l.assertCurrent(); const t = u.readCurrent(); if (t?.sessionId !== e?.sessionId || t?.lifecycleRevision !== e?.lifecycleRevision || sessionDeliveryOrigin(t)?.avatar !== i) throw new Error("Channel avatar session authority changed"); }; return serve(matchesChannelAvatarSender(r.searchParams, n) ? i : void 0, assertCurrent); } let d; try { const { entry: e } = await (await getSessionStoreModule()).loadGatewaySessionEntryReadOnlyInWorker({ cfg: readRuntimeConfig(), key: o, assertActive: l.assertCurrent }); if (e && !l.canReadSession(o, e)) { sendSessionReadForbidden(t); return true; } const n = sessionDeliveryOrigin(e); d = matchesChannelAvatarSender(r.searchParams, n) ? n?.avatar : void 0; } catch {} return serve(d, l.assertCurrent); } //#endregion export { handleChannelAvatarHttpRequest };