UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines • 6.69 kB
import{createLogger,logError}from"#internal/logging.js";import{extractGitHubCommentTrigger,formatGitHubContextBlock}from"#public/channels/github/inbound.js";import{buildGitHubBinding}from"#public/channels/github/binding.js";import{buildGitHubPullRequestContext,mergeGitHubContext}from"#public/channels/github/pr-context.js";import{continuationTokenFromState,stateFromCiEvent,stateFromIssueCommentEvent,stateFromIssueEvent,stateFromPullRequestEvent,stateFromPullRequestReviewCommentEvent}from"#public/channels/github/state.js";const log=createLogger(`github.dispatch`);async function dispatchIssueComment(e){let t=await e.botName();if(isIgnoredInboundComment(e.event.comment.body,e.event.comment.author,t))return;let n=buildInboundContext(e.config,e.event);await dispatchCommentTurn({body:e.event.comment.body,botName:t,commentUrl:e.event.comment.htmlUrl,event:e.event,handlerResult:()=>e.handler(n,toGitHubComment(e.event.comment)),config:e.config,from:e.from,state:stateFromIssueCommentEvent(e.event)})}async function dispatchPullRequestReviewComment(e){let t=await e.botName();if(isIgnoredInboundComment(e.event.comment.body,e.event.comment.author,t))return;let n=buildInboundContext(e.config,e.event);await dispatchCommentTurn({body:e.event.comment.body,botName:t,commentUrl:e.event.comment.htmlUrl,event:e.event,handlerResult:()=>e.handler(n,toGitHubComment(e.event.comment)),config:e.config,from:e.from,state:stateFromPullRequestReviewCommentEvent(e.event)})}async function dispatchIssue(e){let t=buildInboundContext(e.config,e.event);await dispatchWebhookEventTurn({botName:e.botName,config:e.config,event:e.event,handlerResult:()=>e.handler(t,e.event.issue),message:formatIssueEventMessage(e.event),from:e.from,state:stateFromIssueEvent(e.event)})}async function dispatchPullRequest(e){let t=buildInboundContext(e.config,e.event);await dispatchWebhookEventTurn({botName:e.botName,config:e.config,event:e.event,handlerResult:()=>e.handler(t,e.event.pullRequest),message:formatPullRequestEventMessage(e.event),from:e.from,state:stateFromPullRequestEvent(e.event)})}async function dispatchCheckSuite(e){await dispatchCiEvent({...e,ci:e.event.checkSuite,handlerResult:t=>e.handler(t,e.event.checkSuite),label:`Check suite`})}async function dispatchCheckRun(e){await dispatchCiEvent({...e,ci:e.event.checkRun,handlerResult:t=>e.handler(t,e.event.checkRun),label:`Check run`})}async function dispatchWorkflowRun(e){await dispatchCiEvent({...e,ci:e.event.workflowRun,handlerResult:t=>e.handler(t,e.event.workflowRun),label:`Workflow run`})}async function dispatchCiEvent(e){let t=stateFromCiEvent(e.event),n=buildInboundContext(e.config,e.event);if(t.pullRequestNumber===null){await runInboundHandler({event:e.event,handlerResult:()=>e.handlerResult(n)})!=null&&log.warn(`GitHub CI event cannot dispatch without an associated pull request`,{deliveryId:e.event.delivery.id,event:e.event.kind});return}await dispatchWebhookEventTurn({botName:e.botName,config:e.config,event:e.event,handlerResult:()=>e.handlerResult(n),message:formatCiEventMessage(e.label,e.ci),from:e.from,state:t})}async function dispatchWebhookEventTurn(e){let t=await runInboundHandler({event:e.event,handlerResult:e.handlerResult});if(t==null)return;let n=await e.botName();await sendGitHubTurn({auth:t.auth,botName:n,event:e.event,message:e.message,context:mergeGitHubContext({github:await buildPullRequestContext(e.config,e.state,e.event.delivery.id),hook:t.context}),from:e.from,state:e.state,title:t.title})}async function dispatchCommentTurn(e){let t=await runInboundHandler({event:e.event,handlerResult:e.handlerResult});if(t==null)return;let n=extractGitHubCommentTrigger({body:e.body,botName:e.botName}),r=n?.message??e.body.trim();await sendGitHubTurn({auth:t.auth,botName:e.botName,commentUrl:e.commentUrl,event:e.event,isMentioned:n!==null,message:r,context:mergeGitHubContext({github:await buildPullRequestContext(e.config,e.state,e.event.delivery.id),hook:t.context}),from:e.from,state:e.state,title:t.title})}async function runInboundHandler(e){try{return await e.handlerResult()}catch(t){logError(log,`GitHub inbound handler failed`,t,{deliveryId:e.event.delivery.id});return}}async function sendGitHubTurn(e){let t=formatGitHubContextBlock({botName:e.botName,deliveryId:e.event.delivery.id,commentUrl:e.commentUrl,headSha:e.state.headSha,issueNumber:e.state.issueNumber,isMentioned:e.isMentioned,pullRequestNumber:e.state.pullRequestNumber,repository:e.event.repository,sender:e.event.sender});try{await e.from(continuationTokenFromState(e.state)).send(e.message,{auth:e.auth,context:[t,...e.context??[]],state:e.state,title:e.title})}catch(t){logError(log,e.logMessage??`GitHub delivery failed`,t,{deliveryId:e.event.delivery.id})}}async function buildPullRequestContext(e,t,n){try{return await buildGitHubPullRequestContext({api:e.api,baseSha:t.baseSha,config:e.pullRequestContext,credentials:e.credentials,headSha:t.headSha,installationId:t.installationId??void 0,owner:t.owner,pullRequestNumber:t.pullRequestNumber,repo:t.repo})}catch(e){logError(log,`GitHub pull-request context failed — swallowed`,e,{deliveryId:n});return}}function buildInboundContext(e,t){let n=t.kind===`check_suite`||t.kind===`check_run`||t.kind===`workflow_run`?stateFromCiEvent(t):t.kind===`issue_comment`?stateFromIssueCommentEvent(t):t.kind===`issues`?stateFromIssueEvent(t):t.kind===`pull_request`?stateFromPullRequestEvent(t):stateFromPullRequestReviewCommentEvent(t),r=buildGitHubBinding({config:e,state:n});return{conversation:t.conversation,delivery:t.delivery,github:r.github,repository:t.repository,sender:t.sender,thread:r.thread}}function toGitHubComment(e){return{author:e.author,body:e.body,htmlUrl:e.htmlUrl,id:e.id,raw:e.raw,url:e.url}}function formatIssueEventMessage(e){let t=readString(e.issue.raw.title);return`Issue ${e.issue.action}: #${e.issue.issueNumber}${t?` ${t}`:``}`}function formatPullRequestEventMessage(e){let t=readString(e.pullRequest.raw.title);return`Pull request ${e.pullRequest.action}: #${e.pullRequest.pullRequestNumber}${t?` ${t}`:``}`}function formatCiEventMessage(e,t){let n=t.conclusion??t.status;return`${e} ${t.action}: ${ciEventId(t)}${n?` (${n})`:``}`}function ciEventId(e){return`checkSuiteId`in e?e.checkSuiteId:`checkRunId`in e?e.checkRunId:e.workflowRunId}function isIgnoredInboundComment(e,t,n){if(e.includes(`<!-- eve:github:`)||t?.type===`Bot`)return!0;let r=n?`${n}[bot]`.toLowerCase():``;return r.length>0&&t?.login.toLowerCase()===r}function readString(e){return typeof e==`string`&&e.trim().length>0?e.trim():void 0}export{dispatchCheckRun,dispatchCheckSuite,dispatchIssue,dispatchIssueComment,dispatchPullRequest,dispatchPullRequestReviewComment,dispatchWorkflowRun};