openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
189 lines (188 loc) • 8.66 kB
JavaScript
import { t as isPromiseLike } from "./promise-like-aMoWEh5w.mjs";
import { r as collectErrorGraphCandidates } from "./error-coercion-hDrHOSGy.mjs";
import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs";
import "./session-key-CqAyZeVk.mjs";
import { t as DEFAULT_ACCOUNT_ID } from "./account-id-BqKT3GuY.mjs";
import { i as getCommandOwnerAuthority, n as bindCommandOwnerAuthority, r as captureCommandOwnerAssertion } from "./command-owner-authority-BIUd20IP.mjs";
import { f as takeChannelParticipantInput, s as prepareHostChannelContextAdmissionEvidence, t as bindHostChannelContextAdmissionEvidence } from "./admission-evidence-q4_t-NFq.mjs";
import { a as prepareSessionParticipantInput, t as bindRequesterProfile } from "./requester-profile-DgtOmQ0f.mjs";
import { t as captureChannelOperatorRunAuthority } from "./operator-run-authority-3Pm3MQDA.mjs";
//#region src/channels/message-access/run-authorization-error.ts
/** A channel rejected source authorization before the model could execute. */
var ChannelRunAuthorizationError = class extends Error {
constructor(e, t = "denied") {
super("Channel authorization could not be renewed before run admission", { cause: e });
this.disposition = t;
this.name = "ChannelRunAuthorizationError";
}
};
function isChannelRunAuthorizationError(e, t) {
return collectErrorGraphCandidates(e).some((e) => e instanceof ChannelRunAuthorizationError && (t === void 0 || e.disposition === t));
}
//#endregion
//#region src/channels/message-access/child-session-publication.ts
const PUBLICATION = Symbol("openclaw.childSessionPublication");
const runs = /* @__PURE__ */ new WeakMap();
/** Host-only, single-run intent for fresh immediate children; never serialized into a session. */
var ChildSessionPublication = class {
#e;
#t;
#n;
constructor(e, t, n) {
this.requesterSessionKey = e;
this.assertSourceCurrent = t;
this.prepareSource = n;
}
prepare() {
return this.#n ??= this.prepareSourceAuthorization();
}
async prepareSourceAuthorization() {
try {
if (this.#e) throw new Error("An admitted publication cannot renew its source authority");
if (this.prepareSource) {
let e;
try {
e = await this.prepareSource();
} catch (e) {
throw new ChannelRunAuthorizationError(e, "unavailable");
}
if (!e) throw new ChannelRunAuthorizationError(/* @__PURE__ */ new Error("Channel source authorization was rejected"));
this.assertSourceCurrent = e;
}
this.assertSourceCurrent();
} catch (e) {
throw e instanceof ChannelRunAuthorizationError ? e : new ChannelRunAuthorizationError(e);
}
}
bind(e, t) {
if (this.#e && this.#e !== e) return;
this.assertSourceCurrent();
this.#e = e;
this.#t = t;
runs.set(e, this);
}
assertCurrent() {
if (!this.#e || !this.#t) throw new Error("Child publication requires an admitted source run.");
this.assertSourceCurrent();
this.#t();
}
claim(e) {
this.assertCurrent();
if (!e.isNew || e.fork || e.parentSessionKey !== this.requesterSessionKey || e.sessionKey === this.requesterSessionKey || !e.parent || e.parent.incognito || e.parent.visibility === "draft" || isIncognitoSessionKey(this.requesterSessionKey) || e.entry.incognito || e.entry.visibility === "draft" || isIncognitoSessionKey(e.sessionKey)) throw new Error("Public ingress can publish only its fresh isolated, non-private child.");
}
};
function bindChildSessionPublication(e, t, n, r) {
Object.assign(e, { [PUBLICATION]: new ChildSessionPublication(t, n, r) });
}
async function prepareChildSessionPublication(e) {
const t = Reflect.get(e, PUBLICATION);
if (t instanceof ChildSessionPublication) await t.prepare();
}
function copyChildSessionPublication(e, t) {
const n = Reflect.get(e, PUBLICATION);
if (n instanceof ChildSessionPublication) Object.assign(t, { [PUBLICATION]: n });
}
function admitChildSessionPublication(e, t, n) {
const r = Reflect.get(e, PUBLICATION);
if (r instanceof ChildSessionPublication) r.bind(t, n);
}
function readChildSessionPublication(e) {
return e ? runs.get(e) : void 0;
}
//#endregion
//#region src/channels/inbound-event/host-context-builder.ts
/** Wrap the ordinary builder with the private bundled-channel evidence binding. */
function createHostChannelInboundEventContextBuilder(e, t) {
return (n) => {
const r = prepareHostChannelContextAdmissionEvidence({
owner: t,
channelId: n.channel,
accountId: n.accountId,
ingress: n.channelIngress,
rawPrincipalRef: n.sender.id,
contextParams: n
});
const i = e(n);
const bindEvidence = (e) => {
if (t?.channelId === n.channel) {
const r = {
agentId: n.route.agentId,
sessionKey: n.route.dispatchSessionKey ?? n.route.routeSessionKey,
nativeChannelId: n.reply.nativeChannelId ?? n.conversation.nativeChannelId,
messageId: n.messageId,
inboundEventKind: n.message.inboundEventKind ?? "user_request"
};
const bindParticipants = () => {
if (!n.channelIngress || n.channelIngress === "unsupported") return;
const i = (Array.isArray(n.channelIngress) ? n.channelIngress : [n.channelIngress]).map(takeChannelParticipantInput);
if (i.at(-1)?.binding.messageId !== r.messageId || !t.isLive() || i.some((e) => !e || e.owner !== t || e.gatewayContext !== t.resolveGatewayContext?.() || e.identity.pluginId !== n.channel || e.binding.agentId !== r.agentId || e.binding.sessionKey !== r.sessionKey || e.binding.nativeChannelId !== r.nativeChannelId || e.binding.inboundEventKind !== r.inboundEventKind)) return;
for (const t of i) if (t) prepareSessionParticipantInput(e, t.identity, t.promptedAt);
const a = i.length === 1 ? i[0]?.childSessionPublication : void 0;
if (a?.audience === "public" && r.inboundEventKind === "user_request") {
const n = t.resolveGatewayContext?.();
const ownerIsCurrent = () => t.isLive() && Boolean(n) && t.resolveGatewayContext?.() === n;
const assertOwnerCurrent = () => {
if (!ownerIsCurrent()) throw new Error("Public ingress owner is no longer current.");
};
const i = a.prepare;
bindChildSessionPublication(e, r.sessionKey, () => {
assertOwnerCurrent();
a.assertCurrent();
}, i ? async () => {
if (!ownerIsCurrent()) return null;
const e = await i();
if (!ownerIsCurrent() || !e) return null;
return () => {
assertOwnerCurrent();
e();
};
} : void 0);
}
const o = i.at(-1)?.verifiedPrincipal;
const s = o && JSON.stringify(o);
const c = t.resolveGatewayContext?.();
if (!o || !c || i.some((e) => JSON.stringify(e?.verifiedPrincipal) !== s)) return;
const l = i.at(-1)?.requesterProfile;
if (l && e.SenderId === o.senderId && (e.AccountId ?? "default") === o.accountId && e.OriginatingChannel === o.channelId && i.every((e) => e?.requesterProfile?.id === l.id && e.requesterProfile.isCurrent())) {
for (const t of i) if (t) prepareSessionParticipantInput(e, {
type: "profile",
id: l.id
}, t.promptedAt);
bindRequesterProfile(e, {
...l,
isCurrent: () => t.isLive() && t.resolveGatewayContext?.() === c && i.every((e) => e?.requesterProfile?.isCurrent())
});
}
const u = i.at(-1)?.commandOwnerAuthority;
if (!u?.source || e.SenderId !== o.senderId || (e.AccountId ?? "default") !== o.accountId || e.OriginatingChannel !== o.channelId || !u.isCurrent(c.getRuntimeConfig())) return;
bindCommandOwnerAuthority(e, {
channelPrincipal: o,
recoveryReference: u.recoveryReference,
isCurrent: () => t.isLive() && t.resolveGatewayContext?.() === c && u.isCurrent(c.getRuntimeConfig())
});
const d = captureCommandOwnerAssertion(e);
const f = getCommandOwnerAuthority(e);
if (u.operatorProfile && d && f) bindCommandOwnerAuthority(e, {
...f,
operatorAuthority: captureChannelOperatorRunAuthority({
...u.operatorProfile,
recoveryReference: u.recoveryReference?.version === 1 ? u.recoveryReference : void 0,
getRuntimeConfig: () => c.getRuntimeConfig(),
assertCurrent: d,
signal: u.signal
})
});
};
bindParticipants();
}
bindHostChannelContextAdmissionEvidence({
context: e,
preparation: r
});
return e;
};
return isPromiseLike(i) ? i.then(bindEvidence) : bindEvidence(i);
};
}
//#endregion
export { readChildSessionPublication as a, prepareChildSessionPublication as i, admitChildSessionPublication as n, ChannelRunAuthorizationError as o, copyChildSessionPublication as r, isChannelRunAuthorizationError as s, createHostChannelInboundEventContextBuilder as t };