openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
528 lines (527 loc) • 20.6 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import { n as normalizeAccountId } from "./account-id-BqKT3GuY.mjs";
import { i as normalizeChatChannelId } from "./ids-BRSWrLzE.mjs";
import { a as listLoadedChannelPlugins, i as getLoadedChannelPluginForRead, n as getLoadedChannelPluginById } from "./registry-loaded-NYWUyLLC.mjs";
import { t as INTERNAL_MESSAGE_CHANNEL } from "./message-channel-constants-CkbqRgGl.mjs";
import { t as warnPluginSdkDeprecation } from "./sdk-deprecation-D5N9acnu.mjs";
import { t as normalizeAnyChannelId } from "./registry-normalize-guW5_4yo.mjs";
import "./registry-BB86xKJa.mjs";
import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import { a as isInternalMessageChannel } from "./message-channel-DTYVLJT1.mjs";
import { _ as resolveOperatorRoleSelection, h as resolveOperatorRolePolicyForAssignment } from "./operator-role-policy-DhcqVpmG.mjs";
import { i as getCommandOwnerAuthority, r as captureCommandOwnerAssertion } from "./command-owner-authority-BIUd20IP.mjs";
import { t as getCommandSenderAuthority } from "./command-sender-authority-DS9M5Fsl.mjs";
import { r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs";
import { n as normalizeAllowFromEntry, r as resolveSenderCandidates, t as formatAllowFromList } from "./sender-identity-B18lzPv4.mjs";
import { n as parseUserChannelAuthorizationReference } from "./user-channel-authorization-reference-CAWzJwx-.mjs";
import { c as resolveUserChannelAuthorizationPolicy, l as resolveUserChannelIdentity } from "./user-channel-identities-CaFiegZy.mjs";
import { a as prepareUserChannelIdentityAuthority, i as prepareConfiguredCommandOwnerAuthority, t as authorizeCanonicalUserChannelIdentity } from "./user-channel-identity-operations-6wbdZymd.mjs";
import { i as resolveChannelAllowFrom } from "./account-resolution-D7u1J1M0.mjs";
import { n as GatewayOperatorAccessDeniedError, o as resolvePreparedGatewayOperatorAccessAuthority, r as hasCurrentGatewayOperatorAccess, s as resumeGatewayOperatorAccessGrant } from "./operator-access-policy-BWvd6twL.mjs";
import { t as resolveIdentityOperatorScopes } from "./operator-identity-scopes-CltjA6id.mjs";
//#region src/gateway/channel-operator-authority.ts
/** One-shot CLI owners retain the grant while checking their original installation's state. */
function resolveChannelOperatorAdminAuthority(e, t, n = {}) {
const r = resolveChannelOperatorIdentityFacts(e, t, n);
return r && captureLinkedOperatorAdmin(e, r.linked, r.isCurrent)?.authority;
}
/** The update owner must prove accepted native custody before using identity-only checks. */
function resolveUpdateChannelOperatorAdminIdentityAuthority(e, t, n = {}) {
const r = resolveChannelOperatorIdentityFacts(e, t, n);
return r && captureLinkedOperatorAdminIdentity(e, r.linked, r.isCurrent);
}
function resolveChannelOperatorIdentityFacts(e, t, n) {
if (!e.gateway?.roles && !e.gateway?.auth?.identityScopes) return;
const r = { ...t };
const i = resolveUserChannelIdentity(r, n);
if (!i) return;
return {
linked: i,
isCurrent: (t = e) => {
const a = resolveUserChannelIdentity(r, n);
const o = Object.keys(t.gateway?.roles?.assignments?.byGithubLogin ?? {}).length > 0 && resolveOperatorRoleSelection(i.profileId, i.role, t, i.githubLogin ?? null).roleSource !== "assigned";
return a !== void 0 && a.profileId === i.profileId && a.role === i.role && (!o || a.githubLogin === i.githubLogin) && i.emails.every((e) => a.emails.includes(e)) && i.loginIdentities.every((e) => a.loginIdentities.includes(e));
}
};
}
function resolveLinkedOperatorAdmin(e, t) {
const n = resolveOperatorRolePolicyForAssignment(t.profileId, t.role, e, t.githubLogin ?? null);
return (n ? n.scopes.includes("operator.admin") : t.loginIdentities.some((t) => resolveIdentityOperatorScopes(t, e.gateway?.auth?.identityScopes).includes("operator.admin"))) ? t.profileId : void 0;
}
function captureLinkedOperatorAdminIdentity(e, t, n) {
if (!resolveLinkedOperatorAdmin(e, t)) return;
const r = resolveOperatorRolePolicyForAssignment(t.profileId, t.role, e, t.githubLogin ?? null)?.accessPolicyPlugin;
let i = true;
const isCurrent = (e) => {
i &&= n(e) && resolveLinkedOperatorAdmin(e, t) === t.profileId && resolveOperatorRolePolicyForAssignment(t.profileId, t.role, e, t.githubLogin ?? null)?.accessPolicyPlugin === r;
return i;
};
return isCurrent(e) ? {
profileId: t.profileId,
isCurrent
} : void 0;
}
function captureLinkedOperatorAdmin(e, t, n, r) {
const i = captureLinkedOperatorAdminIdentity(e, t, n);
if (!i) return;
try {
const a = r ? null : resolvePreparedGatewayOperatorAccessAuthority({
...t,
githubLogin: t.githubLogin ?? null,
isCurrent: () => n(e)
}, e);
let o = true;
const isCurrent = (e) => {
o &&= i.isCurrent(e) && hasCurrentGatewayOperatorAccess(a);
if (o && r) {
resumeGatewayOperatorAccessGrant({
profileId: t.profileId,
emails: t.emails,
githubAccountIds: t.githubAccountIds,
assignedRole: t.role,
githubLogin: t.githubLogin ?? null
}, e, r.grant);
o &&= n(e);
}
return o;
};
return isCurrent(e) ? {
authority: {
profileId: t.profileId,
isCurrent,
...a ? { signal: a.signal } : {}
},
grant: a === null ? null : a.gatewayAccessGrant
} : void 0;
} catch (e) {
if (e instanceof GatewayOperatorAccessDeniedError) return;
throw e;
}
}
async function prepareChannelOperatorAdmin(e, t, n = {}) {
if (!e.gateway?.roles && !e.gateway?.auth?.identityScopes) return;
const r = "identity" in t ? t.identity : t;
const i = resolveUserChannelAuthorizationPolicy(e.gateway);
const a = "version" in r ? parseUserChannelAuthorizationReference(r) : void 0;
if ("version" in r && !a) return;
const o = "identity" in t ? t.prepared : await prepareUserChannelIdentityAuthority("version" in r ? {
authorizationId: r.id,
policy: i
} : r, n);
if (!o) return;
const s = captureLinkedOperatorAdmin(e, o.linked, o.isCurrent, o.linked.authorization);
if (!s) return;
const c = o.linked.authorization;
const assertCurrent = () => {
if (!s.authority.isCurrent(e)) throw new GatewayOperatorAccessDeniedError();
};
const l = c?.reference ?? (!("version" in r) && s.grant !== void 0 ? await authorizeCanonicalUserChannelIdentity({
action: "authorize",
identity: r,
profileId: o.linked.profileId,
policy: i,
grant: s.grant
}, {
...n,
assertCurrent
}) : void 0);
assertCurrent();
return {
...s.authority,
recoveryReference: l,
operatorProfile: {
profileId: o.linked.profileId,
assignedRole: o.linked.role,
githubLogin: o.linked.githubLogin ?? null,
scopes: resolveOperatorRolePolicyForAssignment(o.linked.profileId, o.linked.role, e, o.linked.githubLogin ?? null)?.scopes ?? ["operator.admin"],
gatewayAccessGrant: s.grant
}
};
}
//#endregion
//#region src/auto-reply/command-auth.ts
function resolveExplicitProviderFromContext(e) {
const t = [
e.Surface,
e.OriginatingChannel,
e.Provider
].map((e) => normalizeMessageChannel(e)).filter((e) => Boolean(e));
const n = t.find((e) => e !== INTERNAL_MESSAGE_CHANNEL);
if (!n && t.includes("webchat")) return {
providerId: void 0,
hadResolutionError: false
};
const r = normalizeAnyChannelId(n) ?? n ?? normalizeAnyChannelId(e.Provider) ?? normalizeAnyChannelId(e.Surface) ?? normalizeAnyChannelId(e.OriginatingChannel);
if (r) return {
providerId: r,
hadResolutionError: false
};
const i = [e.From, e.To].filter((e) => Boolean(e?.trim())).flatMap((e) => e.split(":").map((e) => e.trim()));
for (const e of i) {
const t = normalizeMessageChannel(e);
if (t === "webchat") return {
providerId: void 0,
hadResolutionError: false
};
const n = normalizeAnyChannelId(t) ?? t ?? normalizeAnyChannelId(e);
if (n) return {
providerId: n,
hadResolutionError: false
};
}
}
function resolveInferredProvider(e) {
let t = false;
const n = [];
for (const { providerId: r, resolved: i } of e) if (i.allowFromList.length > 0) n.push({
providerId: r,
hadResolutionError: i.hadResolutionError
});
else if (i.hadResolutionError) t = true;
const r = n[0];
if (n.length === 1 && r) return r;
return {
providerId: void 0,
hadResolutionError: t || n.some((e) => e.hadResolutionError)
};
}
function isWildcardAllowFromEntry(e) {
return e.trim() === "*";
}
function stripWildcardAllowFrom(e) {
return e.filter((e) => !isWildcardAllowFromEntry(e));
}
function resolveProviderAllowFromResult(e, t) {
const { plugin: n, cfg: r, accountId: i } = e;
const a = e.forceFallbackResolutionError ? e.providerId ?? n?.id : n?.id;
let o = Boolean(e.forceFallbackResolutionError);
let s;
if ("error" in t) {
console.warn(`[command-auth] resolveAllowFrom threw for provider "${a}", falling back to config allowFrom: ${describeAllowFromResolutionError(t.error)}`);
o = true;
} else if (t.value != null && !Array.isArray(t.value)) {
console.warn(`[command-auth] resolveAllowFrom returned an invalid allowFrom for provider "${a}", falling back to config allowFrom: invalid_result`);
o = true;
}
if (o) s = resolveFallbackAllowFrom({
cfg: r,
providerId: a,
accountId: i
});
else s = "value" in t && Array.isArray(t.value) ? t.value : [];
return {
allowFromList: formatAllowFromList({
plugin: n,
cfg: r,
accountId: i,
allowFrom: s
}),
hadResolutionError: o
};
}
function resolveProviderAllowFrom(e) {
const { plugin: t, cfg: n, accountId: r } = e;
let i;
if (e.forceFallbackResolutionError || !t?.config?.resolveAllowFrom) i = { value: resolveFallbackAllowFrom({
cfg: n,
providerId: e.forceFallbackResolutionError ? e.providerId ?? t?.id : t?.id,
accountId: r
}) };
else try {
i = { value: t.config.resolveAllowFrom({
cfg: n,
accountId: r
}) };
} catch (e) {
i = { error: e };
}
return resolveProviderAllowFromResult(e, i);
}
async function resolveProviderAllowFromAsync(e) {
const { plugin: t, cfg: n, accountId: r } = e;
if (e.forceFallbackResolutionError || !t) return resolveProviderAllowFrom(e);
let i;
try {
i = { value: t.config.resolveAllowFromAsync || t.config.resolveAllowFrom ? await resolveChannelAllowFrom({
plugin: t,
cfg: n,
accountId: r
}) : resolveFallbackAllowFrom({
cfg: n,
providerId: t.id,
accountId: r
}) };
} catch (e) {
i = { error: e };
}
return resolveProviderAllowFromResult(e, i);
}
function describeAllowFromResolutionError(e) {
return e instanceof Error ? normalizeOptionalString(e.name) ?? "Error" : "unknown_error";
}
function resolveOwnerAllowFromList(e) {
const t = e.allowFrom ?? e.cfg.commands?.ownerAllowFrom;
if (!Array.isArray(t) || t.length === 0) return [];
const n = [];
for (const r of normalizeStringEntries(t.map((e) => e ?? ""))) {
const t = r.indexOf(":");
const i = r.slice(0, t);
const a = t > 0 ? normalizeAnyChannelId(i) ?? normalizeChatChannelId(i) : void 0;
if (!a) {
n.push(r);
continue;
}
if (!e.providerId || a !== e.providerId || normalizeChatChannelId(i) && /^[^:]+:user:[^:\s*]+$/i.test(r) && e.plugin?.messaging?.directTargetStyle !== "user-prefixed") continue;
const o = r.slice(t + 1).trim();
if (o) n.push(o);
}
return formatAllowFromList({
...e,
allowFrom: n
});
}
function resolveExplicitOwnerList(e) {
return Array.from(new Set(stripWildcardAllowFrom(resolveOwnerAllowFromList(e))));
}
function resolveFallbackAllowFrom(e) {
const t = normalizeOptionalString(e.providerId);
if (!t) return [];
const n = e.cfg.channels?.[t];
let r = resolveFallbackAccountConfig(n?.accounts, t, e.accountId);
if (r == null) {
const e = n?.accounts;
if (e) {
r = resolveFallbackAccountConfig(e, t, n?.defaultAccount) ?? resolveFallbackAccountConfig(e, t, "default");
if (!r) {
const n = Object.keys(e).filter((t) => e[t]);
const i = n.length === 1 ? n[0] : void 0;
r = i === void 0 ? void 0 : resolveChannelAccountEntry(e, i, t);
}
}
}
const i = r?.allowFrom ?? r?.dm?.allowFrom ?? n?.allowFrom ?? n?.dm?.allowFrom;
return Array.isArray(i) ? i : [];
}
function resolveFallbackAccountConfig(e, t, n) {
const r = normalizeOptionalLowercaseString(n);
if (!e || !r) return;
return resolveChannelAccountEntry(e, r, t);
}
function resolveCommandAuthorizationState(e, t) {
const { ctx: n, cfg: r, commandAuthorized: i } = e;
const { providerId: a, hadResolutionError: o } = t?.provider ?? resolveExplicitProviderFromContext(n) ?? resolveInferredProvider(listLoadedChannelPlugins().map((e) => ({
providerId: e.id,
resolved: resolveProviderAllowFrom({
plugin: e,
cfg: r,
accountId: n.AccountId
})
})));
const s = a ? getLoadedChannelPluginById(a) : void 0;
const c = normalizeOptionalString(n.From) ?? "";
const l = normalizeOptionalString(n.To) ?? "";
const u = Boolean(r.commands?.allowFrom && typeof r.commands.allowFrom === "object");
const d = {
plugin: s,
cfg: r,
accountId: n.AccountId,
providerId: a
};
const f = r.commands?.allowFrom;
let p = null;
if (f && typeof f === "object") {
const e = f[a ?? ""];
const t = f["*"];
const n = Array.isArray(e) ? e : t;
if (Array.isArray(n)) p = formatAllowFromList({
...d,
allowFrom: n
});
}
const { allowFromList: m, hadResolutionError: h } = t?.allowFrom ?? resolveProviderAllowFrom({
...d,
forceFallbackResolutionError: o
});
const g = n.OwnerAllowFrom;
const _ = resolveOwnerAllowFromList({
...d,
allowFrom: g
});
const v = !h && (m.length === 0 || m.some(isWildcardAllowFromEntry));
const y = v ? [] : stripWildcardAllowFrom(m);
if (!v && y.length === 0 && l) y.push(...normalizeAllowFromEntry({
...d,
value: l
}));
const b = resolveExplicitOwnerList(d);
const x = stripWildcardAllowFrom(_);
const S = new Set(b.length > 0 ? b : x.length > 0 ? x : y);
const C = resolveSenderCandidates({
...d,
senderId: n.SenderId,
senderE164: n.SenderE164,
commandSenderId: getCommandSenderAuthority(n)?.(),
from: c,
chatType: n.ChatType
});
const w = C.find((e) => b.includes(e));
const T = C.find((e) => S.has(e));
const E = w ?? T ?? C[0];
const D = Boolean(s?.commands?.enforceOwnerForCommands);
const O = Boolean(w);
const k = isInternalMessageChannel(n.Provider) && Array.isArray(n.GatewayClientScopes) && n.GatewayClientScopes.includes("operator.admin");
const A = b.length > 0;
const j = captureCommandOwnerAssertion(n);
const M = O || k || getCommandOwnerAuthority(n)?.isCurrent() === true;
const N = !(D || A) ? true : A ? M : M || Boolean(T);
let P = "denied";
if (n.CommandInterpretationSuppressed !== true && (!D || N)) {
if (p !== null || o && u) {
if (!o) P = p?.some((e) => isWildcardAllowFromEntry(e) || C.includes(e)) ? "commands" : "denied";
} else if (i) P = N ? "commands" : "reset-only";
}
return {
authorization: {
providerId: a,
ownerList: b,
senderId: E || void 0,
senderIsOwner: M,
...j ? { assertOwnerCurrent: j } : {},
isAuthorizedSender: P === "commands",
from: c || void 0,
to: l || void 0
},
access: P
};
}
/** @deprecated Await resolveCommandAuthorizationAsync for worker-owned channel allowlists. Supported through 2026-12-31. Removal in the first January 2027 stable release. */
function resolveCommandAuthorization(e) {
warnPluginSdkDeprecation({
family: "command-authorization-sync",
method: "resolveCommandAuthorization",
replacement: "resolveCommandAuthorizationAsync"
});
return resolveCommandAuthorizationState(e).authorization;
}
async function prepareCommandAuthorization(e) {
const { ctx: t, cfg: n } = e;
const r = resolveExplicitProviderFromContext(t) ?? resolveInferredProvider(await Promise.all(listLoadedChannelPlugins().map(async (e) => ({
providerId: e.id,
resolved: await resolveProviderAllowFromAsync({
plugin: e,
cfg: n,
accountId: t.AccountId
})
}))));
return {
provider: r,
allowFrom: await resolveProviderAllowFromAsync({
plugin: r.providerId ? getLoadedChannelPluginById(r.providerId) : void 0,
cfg: n,
accountId: t.AccountId,
providerId: r.providerId,
forceFallbackResolutionError: r.hadResolutionError
})
};
}
/** Prepares channel allowlists through their worker-owned runtime before evaluating live authority. */
async function resolveCommandAuthorizationAsync(e) {
return resolveCommandAuthorizationState(e, await prepareCommandAuthorization(e)).authorization;
}
/** Configured owners that a turn on this channel account renders; a config fact, not authority. */
function resolveConfiguredCommandOwners(e, t) {
const n = normalizeAnyChannelId(t.channel) ?? t.channel;
return resolveExplicitOwnerList({
cfg: e,
plugin: n ? getLoadedChannelPluginForRead(n) : void 0,
providerId: n,
accountId: t.accountId
});
}
function isConfiguredCommandOwner(e, t) {
const n = normalizeAnyChannelId(t.channel) ?? t.channel;
const r = {
cfg: e,
plugin: n ? getLoadedChannelPluginForRead(n) : void 0,
providerId: n,
accountId: t.accountId
};
const i = stripWildcardAllowFrom(resolveOwnerAllowFromList(r));
return resolveSenderCandidates({
...r,
senderId: t.senderId,
senderE164: t.senderE164
}).some((e) => i.includes(e));
}
/** Synchronous CLI capture; deferred effects retain its original person-access grant. */
function resolveCommandOwnerAuthority(e, t, n = {}) {
return captureCommandOwnerIdentity(e, t, n, resolveChannelOperatorAdminAuthority);
}
/** Additional person policy stays with the original Gateway's accepted update operation. */
function resolveUpdateRequesterIdentityAuthority(e, t, n = {}) {
return captureCommandOwnerIdentity(e, t, n, resolveUpdateChannelOperatorAdminIdentityAuthority);
}
function resolveCommandOwnerIdentity(e) {
const t = normalizeAnyChannelId(e?.channel) ?? e?.channel;
return t && e?.senderId ? {
channelId: t,
accountId: normalizeAccountId(e.accountId),
senderId: e.senderId
} : void 0;
}
function captureCommandOwnerIdentity(e, t, n, r) {
const i = { ...t };
if (isConfiguredCommandOwner(e, i)) return Object.freeze({
source: "configured-owner",
isCurrent: (e) => isConfiguredCommandOwner(e, i)
});
const a = resolveCommandOwnerIdentity(i);
const o = a ? r(e, a, n) : void 0;
return Object.freeze({
source: o ? `profile:${o.profileId}` : void 0,
...o?.signal ? { signal: o.signal } : {},
isCurrent: (e) => o !== void 0 && !isConfiguredCommandOwner(e, i) && o.isCurrent(e)
});
}
/** Admission and recovery share the original authority owner; final checks never touch SQLite. */
async function prepareCommandOwnerAuthority(e, t, n = {}) {
const r = "identity" in t ? t : void 0;
const i = "identity" in t ? {
channel: t.identity.channelId,
accountId: t.identity.accountId,
senderId: t.identity.senderId
} : "version" in t ? void 0 : { ...t };
const a = "version" in t ? t : void 0;
if (a?.version === 2 || i && isConfiguredCommandOwner(e, i)) {
const t = await prepareConfiguredCommandOwnerAuthority(e.commands?.ownerAllowFrom, n);
const r = !a || t?.recoveryReference.id === a.id;
return Object.freeze({
source: r ? "configured-owner" : void 0,
recoveryReference: t?.recoveryReference,
isCurrent: (e) => r && (t ? t.isCurrent() : !a) && (!i || isConfiguredCommandOwner(e, i))
});
}
const o = a ?? resolveCommandOwnerIdentity(i);
const s = o && await prepareChannelOperatorAdmin(e, r ?? o, n);
return Object.freeze({
source: s ? `profile:${s.profileId}` : void 0,
operatorProfile: s?.operatorProfile,
recoveryReference: s?.recoveryReference,
...s?.signal ? { signal: s.signal } : {},
isCurrent: (e) => s !== void 0 && (!i || !isConfiguredCommandOwner(e, i)) && s.isCurrent(e)
});
}
/** Resolves reset admission without granting other command or owner authority. */
async function isResetAuthorizedForContext(e) {
return resolveCommandAuthorizationState(e, await prepareCommandAuthorization(e)).access !== "denied" && isResetAllowedForGatewayScopes(e.ctx);
}
function isResetAllowedForGatewayScopes(e) {
const t = e.Provider;
if (!(t ? isInternalMessageChannel(t) : isInternalMessageChannel(e.Surface))) return true;
const n = e.GatewayClientScopes;
if (!Array.isArray(n) || n.length === 0) return true;
return n.includes("operator.admin");
}
//#endregion
export { resolveCommandAuthorizationAsync as a, resolveUpdateRequesterIdentityAuthority as c, resolveCommandAuthorization as i, isResetAuthorizedForContext as n, resolveCommandOwnerAuthority as o, prepareCommandOwnerAuthority as r, resolveConfiguredCommandOwners as s, isConfiguredCommandOwner as t };