openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
141 lines (140 loc) • 4.85 kB
JavaScript
import { n as gitHubPublicApi } from "./github-public-api-C47tI7vQ.mjs";
import { n as monotonicNow } from "./monotonic-time-Cc4T2fkU.mjs";
import { t as createRetainedCache } from "./retained-cache-Dwkf3ewM.mjs";
import { a as prepareSessionPullRequestGitHubRead, i as createGitHubReadGroup, t as fetchSessionPullRequestCheckDetails } from "./control-ui-session-prs-checks-CRddYp6P.mjs";
//#region src/gateway/control-ui-session-pr-check-details.ts
const checkDetailsCache = createRetainedCache();
let activeCheckDetails = 0;
const CHECK_DETAILS_CACHE_MS = 3e4;
const MAX_CHECK_DETAIL_REQUESTS = 4;
/** The only details admission path: clients select a PR already resolved for this session. */
async function loadControlUiSessionPullRequestChecks(e, t) {
const { owner: n, repo: r, number: i, headSha: a } = e;
const o = {
owner: n,
repo: r,
number: i,
headSha: a
};
const unavailable = (e) => ({
...o,
checks: [],
status: "unavailable",
rateLimited: false,
error: e
});
t.assertCurrent();
const matchesTarget = (e) => e.owner.toLowerCase() === n.toLowerCase() && e.repo.toLowerCase() === r.toLowerCase() && e.number === i && e.headSha === a;
const { loadPullRequests: s } = t;
const c = await s({
sessionKey: e.sessionKey,
agentId: e.agentId
});
t.assertCurrent();
const l = c.pullRequests.find(matchesTarget);
if (!l || l.state !== "open" && l.state !== "draft") return unavailable("The session pull request or head changed; reopen CI details");
const u = new URL(l.url).hostname;
const d = prepareSessionPullRequestGitHubRead(u, t.assertCurrent, { credential: t.credential?.() });
const f = d.assertCurrent;
const p = {
...o,
host: u,
apiBaseUrl: d.apiBaseUrl
};
const m = JSON.stringify([
t.sessionScope,
u,
d.apiBaseUrl,
n.toLowerCase(),
r.toLowerCase(),
i,
a,
d.cacheScope
]);
let h = checkDetailsCache.get(m);
let g;
if (!h || h.expiresAt <= Date.now() || h.access.signal.aborted) {
if (activeCheckDetails >= MAX_CHECK_DETAIL_REQUESTS) return {
...unavailable("CI details are busy; retry shortly"),
retryAfterMs: 5e3
};
const e = h?.lastGood;
const n = {
expiresAt: Infinity,
promise: Promise.resolve(unavailable("CI details are loading")),
lastGood: e,
access: createGitHubReadGroup()
};
g = n.access.add(t.assertCurrent);
const r = prepareSessionPullRequestGitHubRead(u, n.access.assertCurrent, {
signal: n.access.signal,
credential: d.credential
});
activeCheckDetails += 1;
const load = async () => {
const t = monotonicNow() + 25e3;
const i = AbortSignal.timeout(25e3);
let a = 0;
const request = async (e, n) => {
r.assertCurrent();
if (++a > 64 || monotonicNow() > t) throw new gitHubPublicApi.ControlUiGitHubError(502, "CI details exceeded the request budget; open the job on GitHub");
return r.request(e, n, i, async () => {
throw new gitHubPublicApi.ControlUiGitHubError(409, "GitHub repository changed; reopen CI details");
});
};
try {
const e = await fetchSessionPullRequestCheckDetails(p, request);
const t = {
...o,
checks: e.checks,
status: e.error ? "stale" : "ready",
rateLimited: false
};
if (Buffer.byteLength(JSON.stringify(t), "utf8") > 524288) throw new gitHubPublicApi.ControlUiGitHubError(502, "CI details exceeded the response limit; open the job on GitHub");
if (e.error) {
const n = gitHubPublicApi.formatControlUiGitHubPreviewError(e.error);
t.error = n.message;
t.rateLimited = e.error instanceof gitHubPublicApi.ControlUiGitHubError && e.error.statusCode === 429;
t.retryAfterMs = n.retryAfterMs;
}
n.lastGood = t;
return t;
} catch (t) {
const r = gitHubPublicApi.formatControlUiGitHubPreviewError(t);
const i = t instanceof gitHubPublicApi.ControlUiGitHubError && t.statusCode === 409;
const a = t instanceof gitHubPublicApi.ControlUiGitHubError && t.statusCode === 429;
const s = !i && e && t instanceof gitHubPublicApi.ControlUiGitHubError && (t.retryable || t.statusCode === 502);
if (!s) n.lastGood = void 0;
return {
...o,
checks: s ? e.checks : [],
status: s ? "stale" : "unavailable",
rateLimited: a,
error: i ? t.message : r.message,
retryAfterMs: r.retryAfterMs
};
}
};
n.promise = load().then((e) => {
n.expiresAt = Date.now() + Math.max(CHECK_DETAILS_CACHE_MS, e.retryAfterMs ?? 0);
return e;
}).finally(() => {
activeCheckDetails -= 1;
});
checkDetailsCache.set(m, n);
h = n;
}
const _ = g ?? h.access.add(t.assertCurrent);
try {
const e = await h.promise;
f();
return structuredClone({
...e,
...e.retryAfterMs === void 0 ? {} : { retryAfterMs: Math.max(0, h.expiresAt - Date.now()) }
});
} finally {
_();
}
}
//#endregion
export { loadControlUiSessionPullRequestChecks };