UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

196 lines (195 loc) • 7.52 kB
import { t as isCoreCanvasHostEnabled } from "./config-7gJAwGAJ.mjs"; import { l as resolveSessionPublicShare } from "./session-accessor.sqlite-entry-store-DmqxH0lm.mjs"; import { t as resolveGatewayPublicOrigin } from "./gateway-public-origin-SLjx6A2a.mjs"; import { s as withReadySessionRows } from "./session-row-prepared-read-B4U_T7SO.mjs"; import { n as buildControlUiPublicSessionCardPath } from "./public-share-B-H7sO_M.mjs"; import { r as buildPublicSessionWidgetUrl, t as buildPublicSessionMediaBaseUrl } from "./control-ui-public-session-attachments-DauqtHb5.mjs"; import { t as isSecurePublicSessionIngress } from "./control-ui-public-session-ingress-BMb0UKvP.mjs"; import { i as parseControlUiSessionReturnPath, r as isControlUiSessionReturnPathQueryKey, t as buildControlUiSessionEntryUrl } from "./control-ui-session-entry-path-DeICmSyS.mjs"; import { n as resolveControlUiShareOrigin } from "./control-ui-share-V2WSYnX0.mjs"; import { a as projectPublicSessionItems, r as readPublicSessionShare, t as isPublicSessionShareActive } from "./control-ui-public-session-read-CDIuuINn.mjs"; import { r as renderPublicSessionDocument, t as PUBLIC_SESSION_CONTENT_SECURITY_POLICY } from "./control-ui-public-session-render-7lgjVbUW.mjs"; import { i as mintPublicSessionWidgetToken, r as mintExistingPublicSessionShareToken } from "./control-ui-public-session-token-BfoKw2lQ.mjs"; import { t as resolveControlUiSessionPath } from "./control-ui-session-path-resolve-BrUGcHIE.mjs"; //#region src/gateway/control-ui-public-session.runtime.ts /** Both public routes recheck publication in the synchronous response frame, including 304s. */ async function servePublicSessionRepresentation(e) { const { req: t, res: n, config: r, gate: i, locator: a, projection: o, offset: s, unavailable: c } = e; const l = await i.run({ kind: "transcript", publicationKey: a.shareId, sessionKey: a.sessionKey, config: r, requestKey: e.requestKey, work: async () => { const t = await readPublicSessionShare(r, a, { offset: s, projection: o }); if (!t) return null; const n = /* @__PURE__ */ new Map(); const i = isCoreCanvasHostEnabled(r) ? projectPublicSessionItems(t.messages) : []; for (const t of i) { if (t.kind !== "message") continue; for (const { docId: r } of t.embeds.filter((e) => e.kind === "widget")) { const t = await mintPublicSessionWidgetToken(a, r); if (t) n.set(r, buildPublicSessionWidgetUrl(e.document.assetBasePath ?? "", t)); } } return renderPublicSessionDocument({ ...t, ...e.document, widgetUrls: n, isLatest: s === 0, ...t.olderOffset !== void 0 ? { olderUrl: e.olderUrl(t.olderOffset) } : {} }); }, consume: (e) => withReadySessionRows(o, () => [{ key: a.sessionKey, agentId: a.agentId }], () => { if (!e || !isPublicSessionShareActive(r, a, o)) return 404; if (!e.isCurrent()) return false; const { body: i, etag: s } = e; if (typeof i !== "string") return 503; n.setHeader("ETag", s); if (t.headers["if-none-match"] === s) { n.statusCode = 304; n.end(); } else { n.statusCode = 200; n.setHeader("Content-Type", "text/html; charset=utf-8"); n.setHeader("Content-Length", Buffer.byteLength(i)); n.end(i); } }) }); if (l.kind !== "ok") { await c(l.kind === "rate-limited" ? 429 : 503, l.kind === "rate-limited" ? l.retryAfterSeconds : void 0); return true; } if (l.value !== void 0) await c(l.value); return true; } async function serveControlUiPublicChat(e) { const { req: t, res: n, basePath: r, config: i, projection: a, gate: o } = e; const s = new URL(t.url ?? "/", "http://localhost"); const c = new URLSearchParams([...s.searchParams].filter(([e]) => e === "offset" || isControlUiSessionReturnPathQueryKey(e))); if (c.size !== s.searchParams.size && (t.method === "GET" || t.method === "HEAD") && parseControlUiSessionReturnPath(s.pathname, r)) { n.statusCode = 302; n.setHeader("Cache-Control", "no-store"); n.setHeader("Location", s.pathname + (c.size ? `?${c}` : "")); n.end(); return true; } const l = new URLSearchParams(s.searchParams); l.delete("offset"); const u = s.pathname + (l.size ? `?${l}` : ""); const d = parseControlUiSessionReturnPath(u, r); const f = resolveGatewayPublicOrigin(i); const p = resolveControlUiShareOrigin(t, f); n.setHeader("Cache-Control", "no-store"); n.setHeader("X-Robots-Tag", "noindex, nofollow"); n.setHeader("Content-Security-Policy", PUBLIC_SESSION_CONTENT_SECURITY_POLICY); const end = (e, r) => { n.statusCode = e; n.setHeader("Content-Type", "text/html; charset=utf-8"); n.setHeader("Content-Length", Buffer.byteLength(r)); n.end(t.method === "HEAD" ? void 0 : r); return true; }; const m = s.searchParams.get("offset") ?? "0"; if (!d || !p || t.method !== "GET" && t.method !== "HEAD" || s.searchParams.getAll("offset").length > 1 || !/^(?:0|[1-9][0-9]{0,9})$/u.test(m)) return end(404, "Not found"); if (t.method === "HEAD") { n.setHeader("Allow", "GET"); return end(405, ""); } const h = buildControlUiSessionEntryUrl(u, r); const g = e.auth.mode === "token" || e.auth.mode === "password" ? r : void 0; const _ = isSecurePublicSessionIngress(t, e.ingress, f); if (!_ && g !== void 0) { await e.serveApp(u); return true; } const unavailable = (e = 404, t = 1) => { if (e !== 404) { n.setHeader("Retry-After", e === 429 ? t : "1"); return end(e, e === 429 ? "Too many public session requests. Please retry later." : "This conversation is temporarily unavailable. Please retry."); } return end(404, renderPublicSessionDocument({ title: "Conversation unavailable", messages: [], truncated: false, latestUrl: s.pathname, entryUrl: h, clientAuthBasePath: g, assetBasePath: r, cardUrl: `${p}${r}/share/card.png`, unavailable: true })); }; const v = o.admitClient(e.ingress.rateLimit.subject.key); if (v.kind === "rate-limited") return unavailable(429, v.retryAfterSeconds); if (!_) return unavailable(); if (!a) return unavailable(503); try { const t = await resolveControlUiSessionPath({ target: d, projection: a, client: null, publicOnly: true }); if (!t) return unavailable(); const n = { key: t.key, agentId: t.agentId }; const queries = () => [n]; const i = await withReadySessionRows(a, queries, () => { const e = a.sharingTargetState(n); return e.status === "ready" ? resolveSessionPublicShare(e.target.entry) : void 0; }); if (!i) return unavailable(); const o = { agentId: t.agentId, sessionKey: t.key, sessionId: i.sessionId, shareId: i.id }; const c = Number(m); const l = await mintExistingPublicSessionShareToken(o); return await servePublicSessionRepresentation({ ...e, locator: o, projection: a, offset: c, requestKey: JSON.stringify([ "canonical", i.id, s.pathname, c, p, h, g ]), document: { latestUrl: s.pathname, ...l ? { mediaBaseUrl: buildPublicSessionMediaBaseUrl(r, l) } : {}, entryUrl: h, clientAuthBasePath: g, assetBasePath: r, canonicalUrl: `${p}${s.pathname}`, cardUrl: l ? `${p}${buildControlUiPublicSessionCardPath({ basePath: r, token: l })}` : `${p}${r}/share/card.png` }, olderUrl: (e) => `${s.pathname}?offset=${e}`, unavailable }); } catch { return unavailable(503); } } //#endregion export { PUBLIC_SESSION_CONTENT_SECURITY_POLICY, serveControlUiPublicChat, servePublicSessionRepresentation };