openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
94 lines (93 loc) • 3.32 kB
JavaScript
import { o as getAsyncWorkSignal } from "./async-work-scope-BXagjUap.mjs";
import { n as GATEWAY_CLIENT_IDS, r as GATEWAY_CLIENT_MODES } from "./client-info-Cm1BcrMm.mjs";
import { t as AgentDatabaseAdmissionError } from "./agent-database-admission-CL6wHJ_N.mjs";
import { i as PROTOCOL_VERSION } from "./version-CwNT1gaY.mjs";
import { d as prepareGatewayRecipientProfile } from "./session-sharing-read-BqjuH16A.mjs";
import { i as parseControlUiSessionReturnPath } from "./control-ui-session-entry-path-DeICmSyS.mjs";
import { t as authorizeControlUiReadRequestOrReply } from "./http-auth-utils-B0FI4CE4.mjs";
import { t as resolveControlUiSessionPath } from "./control-ui-session-path-resolve-BrUGcHIE.mjs";
//#region src/gateway/control-ui-session-entry.ts
/** This endpoint must remain behind the deployment's authenticated proxy, unlike /chat. */
async function serveControlUiSessionEntry(e) {
const { req: t, res: n, basePath: r, projection: i } = e;
n.setHeader("Cache-Control", "no-store");
n.setHeader("X-Robots-Tag", "noindex, nofollow");
const a = new URL(t.url ?? "/", "http://localhost");
const o = a.searchParams.get("path") ?? "";
const s = parseControlUiSessionReturnPath(o, r);
const c = a.searchParams.get("probe") === "1";
if (t.method !== "GET" || !s || a.searchParams.getAll("path").length !== 1 || a.searchParams.getAll("probe").length > 1 || [...a.searchParams.keys()].some((e) => e !== "path" && e !== "probe") || a.searchParams.has("probe") && !c) {
n.statusCode = 404;
n.end("Not found");
return true;
}
const l = !c && (e.auth.mode === "token" || e.auth.mode === "password");
const u = await authorizeControlUiReadRequestOrReply({
...e,
requiredOperatorMethod: "chat.history",
replyOnFailure: !l
});
if (!u) {
if (l) await e.serveApp(o);
return true;
}
const d = {
connect: {
minProtocol: 4,
maxProtocol: 4,
client: {
id: GATEWAY_CLIENT_IDS.CONTROL_UI,
version: "internal",
platform: "web",
mode: GATEWAY_CLIENT_MODES.UI
},
role: "operator",
scopes: u.operatorScopes
},
authenticatedUserProfile: u.authenticatedUserProfile,
internal: {
operatorRoleActor: u.operatorRoleActor,
operatorAccessAuthority: u.operatorAccessAuthority
}
};
if (i) prepareGatewayRecipientProfile(d);
let f = null;
if (i) for (;;) {
let t;
try {
f = await resolveControlUiSessionPath({
target: s,
projection: i,
client: d,
isCurrent: u.hasCurrentClientAuthority
});
break;
} catch (n) {
if (!(n instanceof AgentDatabaseAdmissionError) || n.refusal.code !== "agent-database-inspection-pending") throw n;
t = e.agentDatabaseStartup?.waitForAgentPreparation(n.refusal.agentId, {
forWrite: false,
signal: getAsyncWorkSignal()
});
if (!t) throw n;
}
await t;
await u.revalidate();
}
if (!f || !i || !u.hasCurrentClientAuthority()) {
n.statusCode = 403;
n.end(c ? void 0 : "Session unavailable. Reload the conversation or log in again.");
return true;
}
await u.revalidate();
const isCurrent = () => u.hasCurrentClientAuthority() && f.isCurrent();
if (c) {
n.statusCode = isCurrent() ? 204 : 403;
n.end();
} else {
n.setHeader("X-OpenClaw-Session-Entry", "1");
await e.serveApp(o, isCurrent);
}
return true;
}
//#endregion
export { serveControlUiSessionEntry };