UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

143 lines (142 loc) • 5.11 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { u as redactToolPayloadText } from "./redact-Mj7pppxC.mjs"; import { r as fetchWithSsrFGuard } from "./fetch-guard-BLMw2n18.mjs"; import "./logging-core-DxpUImpb.mjs"; import "./ssrf-runtime-B6q6NM4E.mjs"; import { c as parseGitHubReadTarget } from "./targets-CFNRMYXS.mjs"; import { _ as readGitHubJsonResponse, a as GITHUB_REQUEST_TIMEOUT_MS, c as fetchGitHubApi, h as readBoundedResponse, r as GITHUB_API_ORIGIN, s as discardResponse, t as ControlUiGitHubError, y as requiredString } from "./github-api-C6HIcRTo.mjs"; //#region extensions/github/src/job-logs.ts const LOG_MAX_BYTES = 65536; const LOG_HOSTS = ["*.blob.core.windows.net", "*.actions.githubusercontent.com"]; function unavailable() { return new ControlUiGitHubError(502, "GitHub job logs are unavailable; open the job on GitHub"); } async function readLogTail(e, t, n) { const r = URL.parse(e); if (!r || r.protocol !== "https:" || r.username || r.password || r.port || !(r.hostname.endsWith(".blob.core.windows.net") || r.hostname.endsWith(".actions.githubusercontent.com"))) throw unavailable(); const request = async (e) => { await t?.revalidate(); t?.assertSelected(); return fetchWithSsrFGuard({ url: r.href, fetchImpl: n, requireHttps: true, maxRedirects: 0, timeoutMs: GITHUB_REQUEST_TIMEOUT_MS, signal: t?.signal, beforeRequest: () => t?.assertSelected(), capture: false, policy: { hostnameAllowlist: LOG_HOSTS }, init: { ...e, credentials: "omit", redirect: "error" } }); }; const i = await request({ method: "HEAD" }); let a; try { const e = i.response.headers.get("content-length"); a = e && /^\d+$/u.test(e) ? Number(e) : NaN; if (!i.response.ok || !Number.isSafeInteger(a) || a < 0) throw unavailable(); } finally { await discardResponse(i.response); await i.release(); } if (a === 0) return { text: "", totalBytes: a, start: 0 }; const o = Math.max(0, a - LOG_MAX_BYTES); const s = await request({ headers: { Range: `bytes=${o}-${a - 1}` } }); try { const e = s.response; const t = `bytes ${o}-${a - 1}/${a}`; if (!(e.status === 206 && e.headers.get("content-range") === t || e.status === 200 && o === 0)) throw unavailable(); const n = await readBoundedResponse(e, LOG_MAX_BYTES); if (n.length !== a - o) throw unavailable(); let r = n.toString("utf8"); if (o > 0) { const e = r.indexOf("\n"); r = e < 0 ? "" : r.slice(e + 1); } return { text: redactToolPayloadText(r), totalBytes: a, start: o }; } finally { await discardResponse(s.response); await s.release(); } } async function loadGitHubJobLog(e, t, n, r) { if (parseGitHubReadTarget(e)?.kind !== "job") throw new ControlUiGitHubError(400, "Invalid GitHub workflow job target"); const i = await r(); const a = `${GITHUB_API_ORIGIN}/repos/${e.owner}/${e.repo}`; const o = `${a}/actions/jobs/${e.jobId}`; const s = `https://github.com/${e.owner}/${e.repo}/actions/runs/${e.runId}/job/${e.jobId}`; const request = (e, r = "follow") => fetchGitHubApi(e, n, t?.token, async () => { throw unavailable(); }, t, void 0, void 0, void 0, GITHUB_API_ORIGIN, r); const c = await readGitHubJsonResponse(await request(o)); if (!isRecord(c) || c.id !== e.jobId || c.run_id !== e.runId || typeof c.url !== "string" || c.url.toLowerCase() !== o.toLowerCase() || typeof c.run_url !== "string" || c.run_url.toLowerCase() !== `${a}/actions/runs/${e.runId}`.toLowerCase() || typeof c.head_sha !== "string" || !/^[a-f\d]{40}$/iu.test(c.head_sha)) throw new ControlUiGitHubError(502, "GitHub returned a different or incomplete workflow job"); const l = requiredString(c, "name"); const u = requiredString(c, "status"); if (u !== "completed") throw new ControlUiGitHubError(502, "GitHub job is still running; read its logs after it completes"); const d = requiredString(c, "conclusion"); let f; try { const e = await request(o + "/logs", "manual"); try { if (e.status !== 302) { if (!e.ok) await readGitHubJsonResponse(e); throw unavailable(); } f = await readLogTail(e.headers.get("location") ?? "", t, n); } finally { await discardResponse(e); } } catch (e) { t?.signal?.throwIfAborted(); t?.assertSelected(); if (e instanceof ControlUiGitHubError) throw e; throw unavailable(); } await r(i); await t?.revalidate(); t?.assertSelected(); const p = f.start > 0; const m = p ? `Partial log: complete lines from the last ${LOG_MAX_BYTES} bytes of ${f.totalBytes} bytes. Earlier output is omitted.` : `Complete available job log (${f.totalBytes} bytes).`; return { url: s, title: l, subtitle: `${e.owner}/${e.repo} · job ${e.jobId}`, revision: c.head_sha, metadata: [ { label: "Run", value: String(e.runId) }, { label: "Job", value: String(e.jobId) }, { label: "Status", value: u }, { label: "Conclusion", value: d } ], body: `${m}\n\n${f.text.split("\n").map((e) => " " + e).join("\n")}`, bodyTruncated: p, partial: p }; } //#endregion export { loadGitHubJobLog };