openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.04 kB
JavaScript
import{o as getAsyncWorkSignal}from"./async-work-scope-DBcxYU4O.mjs";import{Fn as object,Jn as string,Nt as array,Pn as number}from"./schemas-DuCQi-Qb.mjs";import{a as getActiveSecretsRuntimeConfigSnapshot}from"./runtime-state-lO6JkIdi.mjs";import{r as BoardValidationError}from"./board-layout-DBxMLFoe.mjs";import{s as resolveGitHubActionsRequest}from"./github-actions-capability-4nQ4mhOS.mjs";import{i as logWarn}from"./logger-BHIcoxTH.mjs";import{n as gitHubPublicApi}from"./github-public-api-oVU11ON0.mjs";import{r as BoardGatewayUnavailableError}from"./board-view-ticket-XXxcT39F.mjs";import{t as createStaleWhileRevalidateCache}from"./stale-while-revalidate-cache-CaAugAC4.mjs";import{t as GitHubIdentityError}from"./github-read-identity-CqU7tjHS.mjs";import{s as prepareGitHubReadIdentity}from"./github-tool-identity-ni2dSNkO.mjs";import{r as requestCurrentGitHubOAuthRefresh}from"./github-oauth-lifecycle-B_rexKVW.mjs";
//!src/gateway/github-actions-read.ts
const CACHE_TTL_MS=3e4;const CACHE_LIMIT=32;const MAX_CONCURRENT_READS=32;const ACTIONS_MAX_RESPONSE_BYTES=1048576;const integer=number().int().nonnegative().max(Number.MAX_SAFE_INTEGER);const shortText=string().max(256);const timestamp=string().max(40).datetime({offset:true});const runsSchema=object({total_count:integer,workflow_runs:array(object({id:integer,name:shortText.nullable(),display_title:string().max(1024),head_branch:shortText.nullable(),status:shortText.nullable(),conclusion:shortText.nullable(),html_url:string().max(2048).url(),run_started_at:timestamp.nullable(),created_at:timestamp,updated_at:timestamp,event:shortText,workflow_id:integer,run_attempt:integer})).max(30)});const gatewayCaches=/* @__PURE__ */ new WeakMap;function actionsFailure(e){if(e instanceof BoardValidationError||e instanceof BoardGatewayUnavailableError||e instanceof GitHubIdentityError)return e;if(e instanceof gitHubPublicApi.ControlUiGitHubError){if(e.statusCode===429)return/* @__PURE__ */ new Error(`GitHub Actions is rate limited; wait and retry.`);if(e.statusCode===401)return/* @__PURE__ */ new Error(`GitHub Actions authentication expired; reconnect the agent's GitHub identity in Settings.`);if(e.statusCode===403||e.statusCode===404)return/* @__PURE__ */ new Error(`GitHub Actions access denied or repository unavailable; check the repository and the selected agent identity's Actions read permission.`)}return/* @__PURE__ */ new Error(`GitHub Actions request failed or exceeded its response limit; retry with a smaller perPage or check GitHub availability.`)}async function prepareBoardGitHubIdentity(e,t){try{const n=e.getRuntimeConfig();return await prepareGitHubReadIdentity({config:n,sourceConfig:getActiveSecretsRuntimeConfigSnapshot()?.sourceConfig??n,agentId:t.boardSession.agentId,issuer:`github.com`,getCurrentConfig:()=>e.getRuntimeConfig(),assertActive:t.assertActive,startActive:t.useCurrent,refresh:()=>requestCurrentGitHubOAuthRefresh(t.boardSession.agentId)})}catch(e){if(e instanceof GitHubIdentityError||e instanceof BoardValidationError||e instanceof BoardGatewayUnavailableError)throw e;throw new GitHubIdentityError(`unverified`)}}async function readBoardGitHubActions(e,t,n,r){const i=resolveGitHubActionsRequest(e);const a=await n.useCurrent(()=>{const e=gatewayCaches.get(t)??{active:0,results:createStaleWhileRevalidateCache({maxEntries:CACHE_LIMIT,maxPending:MAX_CONCURRENT_READS,ttlMs:CACHE_TTL_MS,onBackgroundError:e=>logWarn(`board: ${actionsFailure(e).message}`)})};gatewayCaches.set(t,e);if(e.active>=MAX_CONCURRENT_READS)throw new Error(`GitHub Actions reads are busy; retry shortly.`);e.active+=1;return e});try{const e=await prepareBoardGitHubIdentity(t,n);const o=JSON.stringify([e.cacheScope,i.url]);const s=await e.start(()=>{return a.results.read(o,()=>t.trackExecution(async()=>{const{value:t}=await gitHubPublicApi.fetchGitHubJsonPage(i.url,fetch,e.token,{beforeRedirect:async()=>{throw new BoardValidationError(`invalid_operation`,`GitHub Actions redirected the request; verify the repository/workflow, update the widget grant if needed, and retry.`)},signal:getAsyncWorkSignal(),apiBaseUrl:gitHubPublicApi.GITHUB_API_ORIGIN,maxBytes:ACTIONS_MAX_RESPONSE_BYTES});const n=runsSchema.safeParse(t);if(!n.success||n.data.workflow_runs.length>i.perPage||JSON.stringify(n.data).includes(e.token))throw new Error(`Invalid Actions response`);for(const e of n.data.workflow_runs){const t=new URL(e.html_url);if(t.origin!==`https://github.com`||t.username||t.password||t.search||t.hash||t.pathname.toLowerCase()!==`/${i.repository}/actions/runs/${e.id}`)throw new Error(`Invalid Actions run URL`)}return n.data}))});await e.start(()=>{const e=n.query&&new Set(n.query.fields);const t=e?{...e.has(`total_count`)?{total_count:s.value.total_count}:{},workflow_runs:s.value.workflow_runs.map(t=>Object.fromEntries(Object.entries(t).filter(([t])=>e.has(t))))}:s.value;r(true,structuredClone({...t,...s.stale?{stale:true}:{}}))})}catch(e){throw actionsFailure(e)}finally{a.active-=1}}export{prepareBoardGitHubIdentity,readBoardGitHubActions};