openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
357 lines (356 loc) • 13.3 kB
JavaScript
import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs";
import "./session-key-CqAyZeVk.mjs";
import { o as getRuntimeConfigSnapshotMetadata } from "./runtime-snapshot-WbwbY5b7.mjs";
import { n as operatorScopeSatisfied, o as roleScopesAllow, t as intersectOperatorScopes } from "./operator-scope-compat-CpFHu-FC.mjs";
import { t as captureResidentUserProfileAccess } from "./user-profile-list-D7DDbqt5.mjs";
import { g as resolveOperatorRolePolicyForProfile, h as resolveOperatorRolePolicyForAssignment, m as resolveOperatorRolePolicy, s as operatorSessionCap } from "./operator-role-policy-DhcqVpmG.mjs";
import { r as isSyntheticGatewayCaller, t as hasCurrentPersonalGatewaySource } from "./gateway-personal-caller-_qqOw6oy.mjs";
import { a as isGatewayClientProfilePending } from "./gateway-client-identity-BkzV3v8p.mjs";
import { s as prepareUserProfileRolePolicyAuthority } from "./user-channel-identity-operations-6wbdZymd.mjs";
import { r as createSessionListEntryFilter } from "./session-sharing-read-BqjuH16A.mjs";
import "./session-sharing-BIP3raq1.mjs";
import { n as prepareSessionSharingRead } from "./session-sharing-target-read-Gmjqaux8.mjs";
import { c as prepareGitHubPublicationSession, n as currentGitHubPublicationConfig } from "./github-publication-availability-BPmIEgKL.mjs";
import { u as GitHubPublicationSessionChangedError } from "./github-publication-git-index-DZcaQ_xd.mjs";
import { r as hasCurrentGatewayOperatorAccess } from "./operator-access-policy-BWvd6twL.mjs";
import { a as readGatewayRequestMutationAuthority } from "./session-mutation-guards-Bjnk_O0z.mjs";
import { n as resolveSessionMutationAuthorizationAsync } from "./session-sharing-authorization-async-eavQ3y-d.mjs";
import { n as prepareGitHubPublicationRequesterV2 } from "./github-publication-requester-C33d9FnS.mjs";
//#region src/gateway/server-methods/github-personal-authorization.ts
/** Intersect the live role ceiling with the socket grant, preserving scope implications. */
function currentGitHubClient(e, t, n) {
const { client: r, context: i } = e;
if (e.signal?.aborted || r?.invalidated || r?.connectionSignal?.aborted || !hasCurrentGatewayOperatorAccess(r?.internal?.operatorAccessAuthority) || r?.internal?.remoteIngressPrincipal && !hasCurrentPersonalGatewaySource(r, i) || r?.connId && !isSyntheticGatewayCaller(r) && !i.getClientConnIds?.((e) => e === r).has(r.connId)) throw new Error("GitHub request connection is no longer current; reconnect and try again.");
if (!r) return null;
if (isGatewayClientProfilePending(r)) throw new Error("Authenticated profile verification is unavailable; retry the request.");
const a = i.getRuntimeConfig();
const o = typeof n === "string" ? n : n?.profileId;
const s = typeof n === "object" ? resolveOperatorRolePolicyForAssignment(n.profileId, n.role, a, n.githubLogin ?? null) : o ? resolveOperatorRolePolicyForProfile(o, a) : resolveOperatorRolePolicy(r, a);
const c = r.connect.scopes ?? [];
const l = s ? intersectOperatorScopes(c, s.scopes) : c;
if (r.connect.role !== "operator" || !roleScopesAllow({
role: "operator",
requestedScopes: [t],
allowedScopes: l
})) throw new Error(`GitHub requires current ${t} permission.`);
return {
...r,
connect: {
...r.connect,
scopes: l
},
...o && r.authenticatedUserProfile ? { authenticatedUserProfile: {
...r.authenticatedUserProfile,
profileId: o
} } : {}
};
}
const optionsSessionFlights = /* @__PURE__ */ new WeakMap();
/** Share only pending durable preparation; every caller retains its own live authority. */
async function prepareOptionsSession(e, t, n) {
n();
const r = currentGitHubPublicationConfig();
if (isIncognitoSessionKey(e)) return prepareGitHubPublicationSession(e, {
agentId: t,
cfg: r,
assertCurrent: n
});
let i = optionsSessionFlights.get(r);
if (!i) {
i = /* @__PURE__ */ new Map();
optionsSessionFlights.set(r, i);
}
const a = JSON.stringify([
getRuntimeConfigSnapshotMetadata()?.revision,
t,
e
]);
let o = i.get(a);
if (!o) {
o = {
pending: prepareGitHubPublicationSession(e, {
agentId: t,
cfg: r
}).finally(() => {
i.delete(a);
}),
readers: 0
};
i.set(a, o);
}
const s = o;
s.readers++;
const c = await s.pending;
let l = true;
const release = () => {
if (!l) return;
l = false;
if (--s.readers === 0) c.release();
};
try {
n();
return {
readCurrent: () => {
if (!l) throw new GitHubPublicationSessionChangedError();
return c.readCurrent();
},
release
};
} catch (e) {
release();
throw e;
}
}
/** Shared reads do not require a person; absence never substitutes for failed authentication. */
async function prepareGitHubPublicationOptionsRead(e, { sessionKey: t, agentId: n }, r, i = "options") {
const a = readGatewayRequestMutationAuthority(e);
const o = e.client;
const s = o?.authenticatedUserProfile?.profileId;
const c = o?.authenticatedUserId;
const l = o?.internal?.operatorAccessAuthority;
const assertConnection = () => {
r?.throwIfAborted();
a.assertCurrent();
if (o?.authenticatedUserProfile?.profileId !== s || o?.authenticatedUserId !== c || o?.internal?.operatorAccessAuthority !== l) throw new Error("GitHub profile changed; retry publication options.");
};
assertConnection();
currentGitHubClient(e, "operator.sessions.read");
const u = s ? await prepareUserProfileRolePolicyAuthority(s) : void 0;
assertConnection();
if (s && !u) throw new Error("Authenticated profile verification is unavailable; retry the request.");
const currentClient = () => {
assertConnection();
if (u && !u.isCurrent()) throw new Error("GitHub profile changed; retry publication options.");
return currentGitHubClient(e, "operator.sessions.read", u);
};
const d = currentClient();
const f = i === "shared-observation" || !d || !hasCurrentPersonalGatewaySource(e.client, e.context) || !operatorScopeSatisfied("operator.read", d.connect.scopes ?? []) ? { kind: "ineligible" } : !u ? { kind: "absent" } : {
kind: "eligible",
action: preparePersonalGitHubAction(e, r)
};
const p = i === "shared-observation" ? await prepareGitHubPublicationSession(t, {
agentId: n,
assertCurrent: currentClient,
fields: [
"sessionId",
"lifecycleRevision",
"owner",
"createdActor",
"visibility",
"incognito"
]
}) : await prepareOptionsSession(t, n, currentClient);
let m;
const release = () => {
m?.release();
p.release();
};
try {
const n = e.context.getRuntimeConfig();
if (operatorSessionCap(currentClient(), n) === "invited") {
const e = p.readCurrent();
m = await prepareSessionSharingRead({
cfg: n,
sessionKey: t,
agentId: e.agentId
});
}
const readSession = () => {
const t = currentClient();
const n = p.readCurrent();
const r = e.context.getRuntimeConfig();
const i = m?.readCurrent(r);
const a = i?.target;
if (i && (!a || a.agentId !== n.agentId || a.canonicalKey !== n.canonicalKey || a.storePath !== n.storePath || a.entry.sessionId !== n.entry?.sessionId || a.entry.lifecycleRevision !== n.entry?.lifecycleRevision)) throw new GitHubPublicationSessionChangedError();
const o = createSessionListEntryFilter({
cfg: r,
client: t,
membership: i?.membership
});
return n.entry && o?.(n.canonicalKey, n.entry) !== false ? {
sessionId: n.entry.sessionId,
sessionKey: n.canonicalKey,
agentId: n.agentId,
lifecycleRevision: n.entry.lifecycleRevision ?? null,
archivedAt: n.entry.archivedAt ?? null
} : null;
};
const r = readSession();
if (!r) throw new Error("GitHub publication session was not found.");
const readCurrent = () => {
const e = readSession();
if (!e || e.sessionId !== r.sessionId || e.lifecycleRevision !== r.lifecycleRevision) throw new Error("GitHub publication session access changed; select the session again.");
return e;
};
return {
personal: f,
session: r,
release,
[Symbol.dispose]: release,
sessionScoped: a.sessionScope === "operator.sessions.read",
currentSession: readCurrent,
currentSessionRead: () => {
readCurrent();
return p.readCurrent();
},
assertSessionUnchanged: (e) => {
if (readCurrent().archivedAt !== e.archivedAt) throw new Error("GitHub publication session access changed; select the session again.");
}
};
} catch (e) {
release();
throw e;
}
}
/** Prepare canonical role facts off-thread; live checks consume the profile owner's revisions. */
async function preparePersonalGitHubActionV2(e, t = "operator.read", n) {
const { client: r, context: i } = e;
const a = r?.authenticatedUserProfile?.profileId;
const o = r?.authenticatedUserId;
const s = r?.internal?.operatorAccessAuthority;
const c = [
e.signal,
n,
r?.connectionSignal
].filter((e) => e !== void 0);
const l = AbortSignal.any(c);
const assertConnection = () => {
l.throwIfAborted();
if (!hasCurrentPersonalGatewaySource(r, i)) throw new Error("My GitHub requires a current authenticated human Gateway connection.");
if (r.authenticatedUserProfile?.profileId !== a || r.authenticatedUserId !== o || r.internal?.operatorAccessAuthority !== s) throw new Error("My GitHub owner changed; retry from your current profile.");
};
assertConnection();
const u = a ? await prepareUserProfileRolePolicyAuthority(a) : void 0;
assertConnection();
if (!u) throw new Error("My GitHub requires a verified durable user profile; sign in and try again.");
const assertCurrent = () => {
assertConnection();
if (!u.isCurrent()) throw new Error("My GitHub owner changed; retry from your current profile.");
currentGitHubClient(e, t, u);
};
assertCurrent();
return {
owner: u.profileId,
signal: l,
assertCurrent
};
}
/** Authority stays in this direct connection closure; a profile or request id alone grants nothing. */
function preparePersonalGitHubAction(e, t) {
const { client: n, context: r } = e;
const resolveOwner = () => {
t?.throwIfAborted();
if (!hasCurrentPersonalGatewaySource(n, r) || e.signal?.aborted) throw new Error("My GitHub requires a current authenticated human Gateway connection.");
const i = n.authenticatedUserProfile?.profileId;
if (!i) throw new Error("My GitHub requires a verified durable user profile; sign in and try again.");
const a = captureResidentUserProfileAccess(i).assertCurrent();
if (a.merged_into) throw new Error("My GitHub requires a verified durable user profile; sign in and try again.");
currentGitHubClient(e, "operator.read", {
profileId: a.id,
role: a.role ?? null,
githubLogin: a.githubLogin
});
return a.id;
};
const i = resolveOwner();
return {
owner: i,
assertCurrent: () => {
if (resolveOwner() !== i) throw new Error("My GitHub owner changed; retry from your current profile.");
}
};
}
async function bindPersonalGitHubSessionAction(e, t, n) {
const r = await prepareGitHubPublicationSession(n.sessionKey, {
agentId: n.agentId,
assertCurrent: t.assertCurrent,
fields: [
"sessionId",
"lifecycleRevision",
"archivedAt",
"owner",
"createdActor",
"visibility",
"incognito"
]
});
let releaseAuthorization = () => {};
try {
const n = r.readCurrent();
if (!n.entry?.sessionId) throw new Error("GitHub publication session was not found.");
const { authorization: i, error: a } = await resolveSessionMutationAuthorizationAsync({
client: currentGitHubClient(e, "operator.write", t.owner),
method: "sessions.github.publish",
requestParams: {
sessionKey: n.canonicalKey,
agentId: n.agentId
},
context: e.context,
assertInvocationCurrent: t.assertCurrent
});
releaseAuthorization = () => i?.release?.();
if (a) throw new Error(a.message);
const assertCurrent = () => {
t.assertCurrent();
currentGitHubClient(e, "operator.write", t.owner);
if (r.readCurrent().entry?.archivedAt !== void 0) throw new GitHubPublicationSessionChangedError();
i?.assertCurrent();
};
assertCurrent();
return {
action: {
...t,
assertCurrent,
sessionId: n.entry.sessionId,
lifecycleRevision: n.entry.lifecycleRevision ?? null,
sessionKey: n.canonicalKey,
agentId: n.agentId
},
release: () => {
releaseAuthorization();
r.release();
}
};
} catch (e) {
releaseAuthorization();
r.release();
throw e;
}
}
/** Retain the authenticated policy owner for worker commits as well as immediate effects. */
async function preparePersonalGitHubSessionActionV2(e, t) {
const n = await bindPersonalGitHubSessionAction(e, await preparePersonalGitHubActionV2(e, "operator.write"), t);
const { action: r } = n;
try {
const t = await prepareGitHubPublicationRequesterV2(e, r);
try {
r.assertCurrent();
return {
action: {
...r,
version: 2,
signal: t.requester.signal,
prepareSource: (e) => {
r.assertCurrent();
return t.requester.prepareSource({
...e,
personalOwnerProfileId: r.owner
});
}
},
release: () => {
t.release();
n.release();
}
};
} catch (e) {
t.release();
throw e;
}
} catch (e) {
n.release();
throw e;
}
}
//#endregion
export { preparePersonalGitHubActionV2 as n, preparePersonalGitHubSessionActionV2 as r, prepareGitHubPublicationOptionsRead as t };