openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
364 lines (363 loc) • 14 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 { g as readUserProfileVersion, m as readUserProfileAliasRevision } from "./user-profile-events-BhLFevt9.mjs";
import { a as READ_SCOPE, o as SESSION_READ_SCOPE } from "./operator-scopes-CULmYfyY.mjs";
import { a as captureAgentDatabaseAdmission } from "./agent-database-admission-CL6wHJ_N.mjs";
import { o as roleScopesAllow } from "./operator-scope-compat-CpFHu-FC.mjs";
import { m as readResidentUserProfileId } from "./user-profile-list-D7DDbqt5.mjs";
import { f as resolveGatewayOperatorRoleActor, n as authorizeCurrentOperatorRoleScopes, v as isSessionCreatorProfile, y as prepareSessionCreatorProfile } from "./operator-role-policy-DhcqVpmG.mjs";
import { r as resolveRequestedSessionAgentId } from "./session-request-agent-tfwWH53J.mjs";
import { a as resolveConfiguredGitHubHost } from "./github-host-CjR25cOO.mjs";
import { a as isGatewayClientProfilePending } from "./gateway-client-identity-BkzV3v8p.mjs";
import { y as resolveSessionVisibility } from "./session-sharing-policy-3TAaxC-R.mjs";
import { t as getOrCreateWeakOwnerMap } from "./weak-owner-map-BGsaUABm.mjs";
import { r as createSessionListEntryFilter } from "./session-sharing-read-BqjuH16A.mjs";
import "./session-sharing-BIP3raq1.mjs";
import { t as parseGitHubRemoteUrl } from "./github-remote-xiDM0pdC.mjs";
import { r as hasCurrentGatewayOperatorAccess } from "./operator-access-policy-BWvd6twL.mjs";
import { _ as hasEntry, s as withReadySessionRows, t as SessionRowFactsPending, x as isPreparedSessionRowDatabaseFacts } from "./session-row-prepared-read-B4U_T7SO.mjs";
import { t as prepareTaskPublicationSessions } from "./control-ui-session-task-prs-CqXfyl6N.mjs";
import { t as captureOwnGitHubPullRequestReader } from "./github-publication-inspect-reader-B7C2nwns.mjs";
import { t as resolveSessionWorkspaceRoots } from "./session-workspace-roots-BBOKk2gF.mjs";
//#region src/gateway/control-ui-session-pr-read.ts
const projectedTargets = /* @__PURE__ */ new WeakMap();
const boundProjectedTargets = /* @__PURE__ */ new WeakMap();
/** Git facts and cached snapshots belong to the recorded session and workspace source. */
function resolveControlUiSessionPrTarget(e, t, n = {}) {
const { cfg: r, agentId: i, canonicalKey: a, storePath: o, readSource: s, entry: c } = e;
if (!c?.sessionId || !o || !s) return;
let l;
const u = resolveConfiguredGitHubHost(r);
const d = n.includeBoundPullRequest ? c.worktree?.githubPullRequest : void 0;
if (c.repositoryWorkspaceId) {
const e = t;
const n = e ? parseGitHubRemoteUrl(e.url) : null;
const r = n ?? (e ? parseGitHubRemoteUrl(e.url, u) : null);
l = r && e ? {
...r,
...!n ? { host: u } : {},
branch: e.branch
} : null;
} else l = resolveSessionWorkspaceRoots(r, i, c).diffCwd ?? null;
return {
sessionId: c.sessionId,
lifecycleRevision: c.lifecycleRevision,
params: {
sessionKey: a,
agentId: i
},
githubHost: u,
readSource: s,
identity: JSON.stringify([
i,
a,
o,
s?.agentId,
s?.path,
c.sessionId,
c.lifecycleRevision,
c.repositoryWorkspaceId,
c.worktree?.id,
d,
l,
u
]),
source: l,
githubPullRequest: d,
refreshIndex: Boolean(c.worktree && !c.repositoryWorkspaceId)
};
}
function resolveProjectedControlUiSessionPrTarget(e, t, n = {}) {
const { storePath: r, agentId: i } = t.storeTarget;
return resolveControlUiSessionPrTarget({
cfg: e,
agentId: t.agentId,
canonicalKey: t.key,
storePath: r,
readSource: {
agentId: i,
path: r
},
entry: t.entry
}, t.materialized.row.repository ?? null, n);
}
/** Audience and lineage are part of snapshot identity, never inherited from another viewer. */
function withControlUiTaskPrTarget(e, t, n, r, i) {
if (!e || n.entry.incognito || isIncognitoSessionKey(n.key)) return e;
const a = {
agentId: n.agentId,
key: n.key,
storeTarget: n.storeTarget,
generation: n.generation,
entry: {
sessionId: n.entry.sessionId,
lifecycleRevision: n.entry.lifecycleRevision
}
};
const o = prepareTaskPublicationSessions(t, a, r);
const s = {
sessions: o.sessions,
...i ? { ownerProfileId: i } : {}
};
return {
...e,
branchIdentity: e.branchIdentity ?? e.identity,
taskReceipts: s,
identity: JSON.stringify([e.identity, JSON.stringify(s)]),
assertCurrent: () => {
e.assertCurrent?.();
o.assertCurrent();
}
};
}
/** Background facts use the Gateway's current row owner, never a completed caller's grant. */
async function prepareControlUiSessionPrServiceTarget(e, t) {
const n = e();
if (!n || isIncognitoSessionKey(t.sessionKey)) return;
const r = {
key: t.sessionKey,
agentId: t.agentId
};
return await withReadySessionRows(n, () => [r], (t) => {
const i = t.describe(r);
if (e() !== n || !i || i.entry.incognito || resolveSessionVisibility(i.entry) === "draft") return;
const a = resolveProjectedControlUiSessionPrTarget(t.state.cfg, i);
return a ? {
...a,
assertCurrent: () => {
a.assertCurrent?.();
if (e() !== n || n.capture(r) !== i || !n.isCurrent(i)) throw new Error("Session pull-request target changed");
}
} : void 0;
});
}
/** A watcher may follow a replaced target, but never a replacement person or access grant. */
function createControlUiSessionPrRead(e) {
const { client: t, sessionKey: n, agentId: r, getRuntimeConfig: i, getSessionRowProjection: a, isCurrentClient: o } = e;
const s = resolveGatewayOperatorRoleActor(t);
const c = s?.kind;
const l = s?.kind === "operator" ? s.profileId : void 0;
const u = t.authenticatedUserProfile?.profileId;
const d = t.authenticatedUserId;
const f = [...t.connect.scopes ?? []];
const p = f.toSorted().join("\0");
const hasCurrentScopes = () => {
const e = t.connect.scopes;
if ((e?.length ?? 0) !== f.length) return false;
for (let t = 0; t < f.length; t++) if (e[t] !== f[t]) return e.toSorted().join("\0") === p;
return true;
};
const m = t.internal?.operatorAccessAuthority;
const h = t.connectionSignal;
const g = a();
if (!g) return;
let _ = -1;
let v;
let y;
const captureCurrent = () => {
try {
const s = resolveGatewayOperatorRoleActor(t);
if (!o() || t.invalidated || (t.connect.role ?? "operator") !== "operator" || t.connectionSignal !== h || h?.aborted || isGatewayClientProfilePending(t) || t.authenticatedUserProfile?.profileId !== u || t.authenticatedUserId !== d || s?.kind !== c || (s?.kind === "operator" ? s.profileId : void 0) !== l || !hasCurrentScopes() || t.internal?.operatorAccessAuthority !== m || !hasCurrentGatewayOperatorAccess(m)) return;
const f = readUserProfileAliasRevision();
if (f !== _) {
y = void 0;
if (l && readResidentUserProfileId(l) !== l) return;
_ = f;
}
const p = i();
if (a() !== g) return;
g.state;
const v = g.sharingRevision;
const b = getRuntimeConfigSnapshotMetadata();
const x = readUserProfileVersion();
if (y && v && y.sharing === v && y.cfg === p && y.configRevision === b && y.profileVersion === x && y.profile === t.preparedSessionProfile && !t.internal?.operatorRunAuthority && (!p.gateway?.roles || c === "system" || l && t.preparedSessionProfile?.aliases.has(l))) {
y.assertAgentAdmitted();
return y;
}
if (authorizeCurrentOperatorRoleScopes(t, p) || !roleScopesAllow({
role: "operator",
requestedScopes: [e.allowOwnImportedPullRequest ? "operator.sessions.read" : "operator.read"],
allowedScopes: t.connect.scopes ?? []
})) return;
const S = resolveRequestedSessionAgentId(p, n, r);
if (!S.ok) return;
const C = {
key: n,
agentId: S.agentId
};
const w = g.capture(C);
if (!hasEntry(w) || !g.isCurrent(w) || (isIncognitoSessionKey(w.key) ? g.needsMembershipPreparation() : g.sharingTargetState(C).status !== "ready") || createSessionListEntryFilter({
cfg: p,
client: t,
membership: isIncognitoSessionKey(w.key) ? void 0 : g.readMembership({
...C,
storePath: w.storeTarget.storePath
})
})?.(w.key, w.entry) === false) return;
const T = {
cfg: p,
query: C,
selected: w,
sharing: v,
configRevision: b,
profileVersion: x,
profile: t.preparedSessionProfile,
assertAgentAdmitted: captureAgentDatabaseAdmission(S.agentId)
};
y = isIncognitoSessionKey(w.key) ? void 0 : T;
return T;
} catch {
return;
}
};
const readPreparedCurrent = (n, r) => {
try {
const a = n.describe(r.query, r.selected);
const o = a?.storeTarget.storePath;
if (!a || !o) return;
const s = a.materialized.row.repository ?? null;
const c = g.sharingRevision;
const d = c && !isIncognitoSessionKey(a.key) ? getOrCreateWeakOwnerMap(e.allowOwnImportedPullRequest ? boundProjectedTargets : projectedTargets, c) : void 0;
let f = d?.get(a);
if (!d?.has(a)) {
f = resolveProjectedControlUiSessionPrTarget(r.cfg, a, { includeBoundPullRequest: e.allowOwnImportedPullRequest });
d?.set(a, f);
}
if (!f) return;
if (!roleScopesAllow({
role: "operator",
requestedScopes: ["operator.read"],
allowedScopes: t.connect.scopes ?? []
})) {
if (!l || !f.githubPullRequest?.preparedHeadCommit || !isSessionCreatorProfile(a.entry.createdActor, l)) return;
const e = captureOwnGitHubPullRequestReader(l, () => resolveConfiguredGitHubHost(i()));
const t = JSON.stringify([f.identity, e.identity]);
return {
target: {
...f,
ownGitHubProfileId: l,
ownGitHubIdentity: e.identity,
identity: t,
branchIdentity: t,
assertCurrent: e.assertCurrent
},
repository: s,
projectedIdentity: f.identity
};
}
const p = withControlUiTaskPrTarget(f, g, a, () => {
const e = i();
const n = prepareSessionCreatorProfile(u ?? l);
return (r) => createSessionListEntryFilter({
cfg: e,
client: t,
membership: g.readMembership({
key: r.key,
agentId: r.agentId,
storePath: r.storeTarget.storePath
})
}, n)?.(r.key, r.entry) !== false;
}, l);
return p ? {
target: p,
repository: s,
projectedIdentity: f.identity
} : void 0;
} catch (e) {
if (e instanceof SessionRowFactsPending) throw e;
return;
}
};
return async () => {
try {
if (a() !== g) return;
if (v) try {
v.assertCurrent?.();
return v;
} catch {
v = void 0;
}
const t = await withReadySessionRows(g, (e) => {
const t = resolveRequestedSessionAgentId(e, n, r);
return t.ok ? [{
key: n,
agentId: t.agentId
}] : [];
}, (e) => {
const t = captureCurrent();
if (!t) return;
const n = readPreparedCurrent(e, t);
if (!n) return;
const r = isIncognitoSessionKey(t.selected.key);
const i = g.readPreparedRowContext();
if (r && t.selected.entry?.repositoryWorkspaceId && !i) return;
return {
...n,
cfg: t.cfg,
taskRevision: g.readTaskPublicationRevision(),
factsRevision: t.selected.databaseFactsRevision,
captured: r ? void 0 : t.selected,
privateSource: r ? {
generation: t.selected.generation,
sessionId: t.selected.entry?.sessionId,
lifecycleRevision: t.selected.entry?.lifecycleRevision,
rowContext: i
} : void 0
};
});
let i;
const o = t ? {
...t.target,
...e.taskReceiptsOnly ? {
taskReceiptsOnly: true,
identity: JSON.stringify([t.target.identity, "receipts-only"])
} : {},
assertCurrent: () => {
const n = captureCurrent();
const r = t.privateSource;
let a;
if (!n) throw new Error("Session pull-request target changed");
if (n === i && !r && !t.target.ownGitHubProfileId && n.selected.databaseFactsRevision === t.factsRevision && (n.selected.materialized?.row.repository ?? null) === t.repository && g.readTaskPublicationRevision() === t.taskRevision) return;
if (!r && (n.selected !== t.captured || n.selected.databaseFactsRevision !== t.factsRevision || n.cfg !== t.cfg || (n.selected.materialized?.row.repository ?? null) !== t.repository)) {
if (n.selected.generation !== t.captured?.generation) throw new Error("Session pull-request target changed");
if (!isPreparedSessionRowDatabaseFacts(n.selected.retainedDatabaseFacts)) throw new SessionRowFactsPending([n.query]);
const r = g.describe(n.query, n.selected);
if (!r || resolveProjectedControlUiSessionPrTarget(n.cfg, r, { includeBoundPullRequest: e.allowOwnImportedPullRequest })?.identity !== t.projectedIdentity) throw new Error("Session pull-request target changed");
a = r;
}
t.target.assertCurrent?.();
i = n;
if (!r) {
t.taskRevision = g.readTaskPublicationRevision();
if (a) {
t.captured = a;
t.factsRevision = a.databaseFactsRevision;
t.cfg = n.cfg;
t.repository = a.materialized.row.repository ?? null;
}
return;
}
const { selected: o } = n;
if (o.generation !== r.generation || o.entry?.sessionId !== r.sessionId || o.entry?.lifecycleRevision !== r.lifecycleRevision || o.entry?.repositoryWorkspaceId && (!r.rowContext || g.readPreparedRowContext() !== r.rowContext) || resolveControlUiSessionPrTarget({
cfg: n.cfg,
agentId: o.agentId,
canonicalKey: o.key,
storePath: o.storeTarget.storePath,
readSource: {
agentId: o.storeTarget.agentId,
path: o.storeTarget.storePath
},
entry: o.entry
}, t.repository, { includeBoundPullRequest: e.allowOwnImportedPullRequest })?.identity !== t.target.identity) throw new Error("Session pull-request target changed");
}
} : void 0;
v = t?.privateSource || t?.target.ownGitHubProfileId ? void 0 : o;
return o;
} catch {
return;
}
};
}
//#endregion
export { resolveProjectedControlUiSessionPrTarget as i, prepareControlUiSessionPrServiceTarget as n, resolveControlUiSessionPrTarget as r, createControlUiSessionPrRead as t };