UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.99 kB
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{N as parseAgentSessionKey}from"./session-key-BPgKfU2m.mjs";import{o as roleScopesAllow}from"./operator-scope-compat-TmgAKmD2.mjs";import{a as prepareCurrentGitHubPublicationIdentity,c as prepareGitHubPublicationSession,i as matchesCurrentGitHubPublicationIdentity}from"./github-publication-availability-LlQ3BS1p.mjs";import{n as createGitHubPublicationJsonReader}from"./github-publication-json-read-BYKGjBSc.mjs";import{t as inspectOwnGitHubPullRequest}from"./github-publication-inspect-BlrLFa2O.mjs"; //!src/gateway/github-publication-action-target.ts async function preparePublishingSession(e,t,n){const r=n.use(await prepareGitHubPublicationSession(e.sessionKey,{agentId:e.agentId,assertCurrent:t}));const i=e.publishingSessionKey&&e.publishingSessionKey!==r.loaded.canonicalKey?n.use(await prepareGitHubPublicationSession(e.publishingSessionKey,{assertCurrent:t})):r;const a=r===i?[r]:[r,i];const assertSessions=()=>{for(const e of a)if(!e.readCurrent().entry?.sessionId)throw new Error(`The GitHub publication session is unavailable.`)};let o=i.loaded.canonicalKey;let s=i.loaded.agentId;const c=/* @__PURE__ */ new Set;while(o!==r.loaded.canonicalKey||s!==r.loaded.agentId){const e=`${s}\0${o}`;if(c.has(e)||c.size>=64)throw new Error(`The publishing session is not a child of this conversation.`);c.add(e);const i=n.use(await prepareGitHubPublicationSession(o,{agentId:parseAgentSessionKey(o)?void 0:s,assertCurrent:t,fields:[`sessionId`,`lifecycleRevision`,`parentSessionKey`,`spawnedBy`]}));a.push(i);assertSessions();s=i.loaded.agentId;if(i.loaded.canonicalKey===r.loaded.canonicalKey&&s===r.loaded.agentId)break;const l=i.readCurrent().entry;const u=l?.parentSessionKey??l?.spawnedBy;if(!u)throw new Error(`The publishing session is not a child of this conversation.`);o=u}assertSessions();return{selected:i,assertCurrent:assertSessions}}function pullRequestState(e,t,n,r){if(!isRecord(e)||!isRecord(e.head)||!isRecord(e.base)||!isRecord(e.base.repo)||typeof e.node_id!==`string`||!e.node_id||e.html_url!==t.url||e.head.ref!==t.branch||typeof e.base.repo.full_name!==`string`||e.base.repo.full_name.toLowerCase()!==t.repository.toLowerCase()||typeof e.head.sha!==`string`||!/^[a-f0-9]{40}$/u.test(e.head.sha)||e.state!==`open`&&e.state!==`closed`||typeof e.draft!==`boolean`||typeof e.merged!==`boolean`||e.mergeable!==null&&typeof e.mergeable!==`boolean`||typeof e.mergeable_state!==`string`||typeof e.title!==`string`||e.body!==null&&typeof e.body!==`string`||e.merge_commit_sha!==null&&typeof e.merge_commit_sha!==`string`)throw new Error(`GitHub returned an invalid pull request state; inspect the published PR before retrying.`);if(n&&(e.number!==n.number||e.base.repo.id!==n.base.repositoryId||e.base.ref!==n.base.branch||(n.head?!isRecord(e.head.repo)||e.head.repo.id!==n.head.repositoryId||e.head.repo.full_name!==n.head.repository:e.head.repo!==null)||r!==void 0&&(!isRecord(e.user)||e.user.id!==r)))throw new Error(`The original PR target or author changed; inspect it again before continuing.`);return{nodeId:e.node_id,state:e.merged?`merged`:e.state===`closed`?`closed`:e.draft?`draft`:`open`,headCommit:e.head.sha,mergeCommit:e.merged?e.merge_commit_sha:null,mergeable:e.mergeable,mergeState:e.mergeable_state,title:e.title,body:e.body??``}} //!src/gateway/github-publication-selected-read.ts async function prepareSelectedGitHubPullRequestReader(e){const{session:t,pullRequest:n,binding:r,requester:i}=e;const assertRequester=()=>{e.assertCurrent();i.assertCurrent()};const a=await prepareCurrentGitHubPublicationIdentity(t.agentId,assertRequester);const assertCurrent=()=>{assertRequester();if(!matchesCurrentGitHubPublicationIdentity({agentId:t.agentId,identity:a}))throw new Error(`The GitHub publisher changed; inspect the current identity before retrying.`)};assertCurrent();const o=URL.parse(n.url);const s=a.host??`github.com`;const c=`/`+n.repository+`/pull/`;const l=o?.pathname.slice(c.length);if(!o||o.protocol!==`https:`||o.host!==s||r&&r.host!==s||o.username||o.password||!o.pathname.startsWith(c)||!l||!/^[1-9][0-9]*$/.test(l)||o.search||o.hash)throw new Error(`The publication's GitHub target is invalid.`);const u=createGitHubPublicationJsonReader({host:s,env:a.env,assertCurrent,requireSuccess:true,unavailable:()=>/* @__PURE__ */ new Error(`GitHub PR state is unavailable; check the publisher's repository access and retry inspect.`)});const d=roleScopesAllow({role:`operator`,requestedScopes:[`operator.write`],allowedScopes:i.snapshot.scopes});let f;const readJson=async(e,n)=>{assertCurrent();if(r&&!d)f=await inspectOwnGitHubPullRequest({pullRequestUrl:r.url,session:t,requester:i,assertCurrent});const a=await u(e,n);f?.assertCurrent();assertCurrent();return a};return{identity:a,assertCurrent,readJson,get inspection(){return f},endpoint:`repos/`+n.repository+`/pulls/`+l}}export{preparePublishingSession as n,pullRequestState as r,prepareSelectedGitHubPullRequestReader as t};