openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 11.5 kB
JavaScript
import{r as isIncognitoSessionKey}from"./session-key-DYtwLLn1.mjs";import"./session-key-BPgKfU2m.mjs";import{t as getOrCreateWeakOwnerMap}from"./weak-owner-map-B5mTikxj.mjs";import{o as getRuntimeConfigSnapshotMetadata}from"./runtime-snapshot-Bg5K4JNu.mjs";import{g as readUserProfileVersion,m as readUserProfileAliasRevision}from"./user-profile-events-CkQlfQnE.mjs";import{o as READ_SCOPE,s as SESSION_READ_SCOPE}from"./operator-scopes-bEvB7E5X.mjs";import{a as captureAgentDatabaseAdmission}from"./agent-database-admission-DiJx9iIl.mjs";import{o as roleScopesAllow}from"./operator-scope-compat-TmgAKmD2.mjs";import{m as readResidentUserProfileId}from"./user-profile-list-DJsakwXy.mjs";import{f as resolveGatewayOperatorRoleActor,n as authorizeCurrentOperatorRoleScopes,v as isSessionCreatorProfile,y as prepareSessionCreatorProfile}from"./operator-role-policy-g-dxw0h4.mjs";import{t as isNamedOperatorRoleEnforcementEnabled}from"./operator-role-enforcement-FsaRvImR.mjs";import{r as hasCurrentGatewayOperatorAccess}from"./operator-access-policy-lm0chPjw.mjs";import{r as resolveRequestedSessionAgentId}from"./session-request-agent-DmeXmT1P.mjs";import{a as resolveConfiguredGitHubHost}from"./github-host-BIyVIYLv.mjs";import{a as isGatewayClientProfilePending}from"./gateway-client-identity-H5JO7081.mjs";import{b as resolveSessionVisibility,x as sharingIdentity}from"./session-sharing-policy-CQ0Zqotl.mjs";import{r as createSessionListEntryFilter}from"./session-sharing-read-Cm-EAfc6.mjs";import"./session-sharing-BmRcWi9h.mjs";import{t as parseGitHubRemoteUrl}from"./github-remote-Cv6zDIpK.mjs";import{S as isPreparedSessionRowDatabaseFacts,c as withReadySessionRows,n as SessionRowFactsPending,v as hasEntry}from"./session-row-prepared-read-B7-BN1nJ.mjs";import{n as resolveSessionWorkspaceRoots}from"./session-workspace-roots-DetXNmKU.mjs";import{t as prepareTaskPublicationSessions}from"./control-ui-session-task-prs-C3u7ukWd.mjs";import{t as captureOwnGitHubPullRequestReader}from"./github-publication-inspect-reader-Cz75wiX2.mjs";
//!src/gateway/control-ui-session-pr-read.ts
const projectedTargets=/* @__PURE__ */ new WeakMap;const boundProjectedTargets=/* @__PURE__ */ new WeakMap;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)}function withControlUiTaskPrTarget(e,t,n,r,i){if(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:o.assertCurrent}}const receiptsOnlyTargetIdentity=e=>JSON.stringify([e,`receipts-only`]);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:()=>{if(e()!==n||n.capture(r)!==i||!n.isCurrent(i))throw new Error(`Session pull-request target changed`)}}:void 0})}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&&(!isNamedOperatorRoleEnforcementEnabled(p)||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}}return{target:withControlUiTaskPrTarget(f,g,a,()=>{const e=i();const n=prepareSessionCreatorProfile(u??l);const r=createSessionListEntryFilter({cfg:e,client:t},n);const a=sharingIdentity(t,resolveGatewayOperatorRoleActor(t));return r?e=>r(e.key,e.entry,a!==void 0&&g.hasMembership(e.storeTarget.storePath,e.key,a.id)):void 0},l),repository:s,projectedIdentity:f.identity}}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:receiptsOnlyTargetIdentity(t.target.identity)}:{},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}}}export{resolveProjectedControlUiSessionPrTarget as a,resolveControlUiSessionPrTarget as i,prepareControlUiSessionPrServiceTarget as n,receiptsOnlyTargetIdentity as r,createControlUiSessionPrRead as t};