UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

119 lines (118 loc) • 5.55 kB
import { o as getAsyncWorkSignal } from "./async-work-scope-BXagjUap.mjs"; import { Fn as object, Jn as string, Nt as array, Pn as number } from "./schemas-Cy6abvsu.mjs"; import { a as getActiveSecretsRuntimeConfigSnapshot } from "./runtime-state-Dq_fFDZp.mjs"; import { r as BoardValidationError } from "./board-layout-C0d9b7cg.mjs"; import { a as resolveGitHubActionsRequest } from "./github-actions-capability-ByBXQPgn.mjs"; import { n as gitHubPublicApi } from "./github-public-api-C47tI7vQ.mjs"; import { i as logWarn } from "./logger-CIwfuhOU.mjs"; import { r as BoardGatewayUnavailableError } from "./board-view-ticket-CRLjSnq1.mjs"; import { t as createStaleWhileRevalidateCache } from "./stale-while-revalidate-cache-BRZEfv_f.mjs"; import { n as GitHubIdentityError } from "./github-read-identity-DyhbpOtm.mjs"; import { s as prepareGitHubReadIdentity } from "./github-tool-identity-41SK6r8O.mjs"; import { r as requestCurrentGitHubOAuthRefresh } from "./github-oauth-lifecycle-6AnTmFkw.mjs"; //#region 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."); } /** Pinning and reads share the same source-config scrub, refresh, and current credential check. */ 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(() => r(true, structuredClone({ ...s.value, ...s.stale ? { stale: true } : {} }))); } catch (e) { throw actionsFailure(e); } finally { a.active -= 1; } } //#endregion export { prepareBoardGitHubIdentity, readBoardGitHubActions };