UNPKG

eve

Version:

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

5 lines • 4.34 kB
import{getGitHubPullRequest,listGitHubCompareFiles,listGitHubPullRequestFiles}from"#public/channels/github/api.js";const MAX_PATCH_BYTES=2e4,GITHUB_DEFAULT_EXCLUDED_DIFF_FILES=[`**/pnpm-lock.yaml`,`**/package-lock.json`,`**/npm-shrinkwrap.json`,`**/yarn.lock`,`**/bun.lockb`,`**/Cargo.lock`,`**/go.sum`,`**/poetry.lock`,`**/Pipfile.lock`,`**/uv.lock`,`**/composer.lock`,`**/Gemfile.lock`,`**/*.min.js`,`**/*.min.css`,`**/*.map`,`**/*.snap`];async function buildGitHubPullRequestContext(e){if(e.pullRequestNumber===null)return;let t=withPinnedPullRequestShas(await getGitHubPullRequest({api:e.api,credentials:e.credentials,installationId:e.installationId,owner:e.owner,pullRequestNumber:e.pullRequestNumber,repo:e.repo}),e),n=renderPullRequestMetadata(t),r=readNonEmptySha(e.baseSha)??readNonEmptySha(t.base.sha),i=readNonEmptySha(e.headSha)??readNonEmptySha(t.head.sha),a=readNonEmptySha(e.headSha)!==void 0&&i!==void 0&&r!==void 0?(await listGitHubCompareFiles({api:e.api,baseSha:r,credentials:e.credentials,headSha:i,installationId:e.installationId,owner:e.owner,repo:e.repo})).slice(0,50):await listGitHubPullRequestFiles({api:e.api,credentials:e.credentials,installationId:e.installationId,owner:e.owner,perPage:50,pullRequestNumber:e.pullRequestNumber,repo:e.repo}),o=[...GITHUB_DEFAULT_EXCLUDED_DIFF_FILES,...e.config?.excludedFiles??[]];return n.push(``,...renderPullRequestFiles(a,o)),[[`GitHub pull request context for the current turn. `,`Use this as background for the user's GitHub comment.`,``,n.join(` `)].join(` `)]}function mergeGitHubContext(e){let t=e.github??[],n=e.hook??[];if(t.length!==0||n.length!==0)return[...t,...n]}function withPinnedPullRequestShas(e,t){let n=readNonEmptySha(t.headSha),r=readNonEmptySha(t.baseSha);return n===void 0&&r===void 0?e:{...e,base:{...e.base,sha:r??e.base.sha},head:{...e.head,sha:n??e.head.sha}}}function readNonEmptySha(e){return typeof e==`string`&&e.trim().length>0?e.trim():void 0}function renderPullRequestMetadata(e){return[`<github_pull_request>`,`number: ${e.number}`,`title: ${e.title}`,`state: ${e.state??`unknown`}`,`draft: ${e.draft?`true`:`false`}`,...e.author?.login?[`author: ${e.author.login}`]:[],...e.htmlUrl?[`url: ${e.htmlUrl}`]:[],...e.base.ref?[`base_ref: ${e.base.ref}`]:[],...e.base.sha?[`base_sha: ${e.base.sha}`]:[],...e.head.ref?[`head_ref: ${e.head.ref}`]:[],...e.head.sha?[`head_sha: ${e.head.sha}`]:[],...e.mergeable===null||e.mergeable===void 0?[]:[`mergeable: ${e.mergeable?`true`:`false`}`],...e.changedFiles===void 0?[]:[`changed_files: ${e.changedFiles}`],...e.additions===void 0?[]:[`additions: ${e.additions}`],...e.deletions===void 0?[]:[`deletions: ${e.deletions}`],...e.body?[``,`body:`,indent(truncateText(e.body,4e3))]:[],`</github_pull_request>`]}function renderPullRequestFiles(e,t){if(e.length===0)return[`<github_pull_request_files>`,`none`,`</github_pull_request_files>`];let n=[`<github_pull_request_files>`],r=MAX_PATCH_BYTES,i=!1;for(let a of e){if(n.push(renderFileSummary(a)),fileMatchesAnyGlob(a.filename,t)){n.push(` patch omitted (excluded)`);continue}if(a.patch===void 0)continue;if(r<=0){i=!0;continue}let e=truncateByBytes(a.patch,r);n.push(`patch:`,indent(e.text)),r-=e.bytes,e.truncated&&(i=!0,r=0)}return i&&n.push(``,`patches_truncated: true`,`max_patch_bytes: ${MAX_PATCH_BYTES}`),n.push(`</github_pull_request_files>`),n}function renderFileSummary(e){let t=[e.status??`modified`,e.additions===void 0?void 0:`+${e.additions}`,e.deletions===void 0?void 0:`-${e.deletions}`].filter(e=>e!==void 0);return`- ${e.filename}${t.length>0?` (${t.join(`, `)})`:``}`}function fileMatchesAnyGlob(e,t){return t.some(t=>globToRegExp(t).test(e))}function globToRegExp(e){let t=``;for(let n=0;n<e.length;n+=1){let r=e.charAt(n);r===`*`?e[n+1]===`*`?(t+=`.*`,n+=1,e[n+1]===`/`&&(n+=1)):t+=`[^/]*`:`\\^$.|?+()[]{}`.includes(r)?t+=`\\${r}`:t+=r}return RegExp(`^${t}$`,`u`)}function truncateByBytes(e,t){let n=new TextEncoder().encode(e);return n.byteLength<=t?{bytes:n.byteLength,text:e,truncated:!1}:{bytes:t,text:`${new TextDecoder().decode(n.slice(0,t))}\n[truncated]`,truncated:!0}}function truncateText(e,t){return e.length<=t?e:`${e.slice(0,t)}\n[truncated]`}function indent(e){return e.split(` `).map(e=>` ${e}`).join(` `)}export{GITHUB_DEFAULT_EXCLUDED_DIFF_FILES,buildGitHubPullRequestContext,fileMatchesAnyGlob,mergeGitHubContext};