UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

194 lines (193 loc) • 6.27 kB
import { r as runWithMainThreadTask } from "./main-thread-stall-BhUfYJnA.mjs"; import { r as isIncognitoSessionKey } from "./session-key-DW1jcHbM.mjs"; import "./session-key-CqAyZeVk.mjs"; import { x as registerOpenClawStateDatabaseLifecycleListener } from "./openclaw-state-db-cache-CkwtTqzF.mjs"; import { t as captureOpenClawStateReadContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { i as githubPublicationReceipts } from "./github-publication-receipts-DrGMcoy5.mjs"; import { t as getOrCreateWeakOwnerMap } from "./weak-owner-map-BGsaUABm.mjs"; import { _ as hasEntry, t as SessionRowFactsPending } from "./session-row-prepared-read-B4U_T7SO.mjs"; import { t as createRetainedCache } from "./retained-cache-Dwkf3ewM.mjs"; //#region src/gateway/control-ui-session-task-prs.ts const nodesByRevision = /* @__PURE__ */ new WeakMap(); const rowsByProjection = /* @__PURE__ */ new WeakMap(); function publicationRows(e) { if (!e.sharingRevision) return; let t = rowsByProjection.get(e); if (!t) { const n = /* @__PURE__ */ new Map(); e.onFactsChange((e) => { if (e.kind === "reset") n.clear(); else n.delete(e.key); }); e.onSelectionChange((e) => { if (e.kind === "reset") n.clear(); }); rowsByProjection.set(e, n); t = n; } return t; } function readPublicationRow(e, t, n) { let r = n?.get(t.query.key)?.get(t); if (!r) { r = e.capture(t.query); if (r) { if (n) { let e = n.get(t.query.key); if (!e) { e = /* @__PURE__ */ new WeakMap(); n.set(t.query.key, e); } e.set(t, r); } } } if (r && (r.sharingEntry !== r.storedEntry || r.entryAcquisitionPending)) throw new SessionRowFactsPending([t.query]); return r; } function allowsPublicationRow(e, t) { return hasEntry(e) && !e.entry.incognito && !isIncognitoSessionKey(e.key) && t?.(e) !== false; } /** Traverse the row owner's indexed lineage; neither checkout nor transcript is evidence. */ function selectTaskPublicationSessions(e, t, n) { const r = e.readTaskPublicationRevision(); const i = getOrCreateWeakOwnerMap(nodesByRevision, r); const a = publicationRows(e); const prepare = (e) => { const t = i.get(e.generation); if (t) return t; const n = { query: { agentId: e.agentId, key: e.key, storePath: e.storeTarget.storePath }, identity: JSON.stringify([ e.agentId, e.key, e.entry.sessionId ]), session: { agentId: e.agentId, sessionKey: e.key, sessionId: e.entry.sessionId, lifecycleRevision: e.entry.lifecycleRevision ?? null } }; i.set(e.generation, n); return n; }; const o = /* @__PURE__ */ new Set(); const s = [prepare(t)]; const c = []; const l = []; for (const t of s) { const { identity: r } = t; const i = readPublicationRow(e, t, a); const u = o.has(r); const d = !u && allowsPublicationRow(i, n); l.push({ node: t, duplicate: u, included: d }); if (!d) continue; o.add(r); c.push(t.session); t.children ??= i.key.startsWith("agent:") && !e.hasLogicalChildEntries(i) ? [] : runWithMainThreadTask("gateway:task-pr-lineage", () => e.selectEntries({ parentSessionKey: i }).map(prepare)); s.push(...t.children); } c.sort((e, t) => e.sessionKey.localeCompare(t.sessionKey) || e.agentId.localeCompare(t.agentId)); return { sessions: c, visits: l, revision: r }; } /** Retain structural selection facts; every assertion still checks live disclosure authority. */ function prepareTaskPublicationSessions(e, t, n) { const r = n(); e.state; let i = selectTaskPublicationSessions(e, t, r); const a = i.sessions; return { sessions: a, assertCurrent: () => { const r = n(); e.state; if (i.revision === e.readTaskPublicationRevision()) { const t = publicationRows(e); if (i.visits.every(({ node: n, duplicate: i, included: a }) => { const o = readPublicationRow(e, n, t); return i || allowsPublicationRow(o, r) === a; }) && i.revision === e.readTaskPublicationRevision()) return; } const o = selectTaskPublicationSessions(e, t, r); if (o.sessions.length !== a.length || o.sessions.some((e, t) => { const n = a[t]; return e !== n && (e.agentId !== n?.agentId || e.sessionKey !== n.sessionKey || e.sessionId !== n.sessionId || e.lifecycleRevision !== n.lifecycleRevision); })) throw new Error("Task pull-request audience changed"); i = o; } }; } const cache = createRetainedCache(); githubPublicationReceipts.subscribeFacts((e) => { if (e.kind === "committed" || e.kind === "unknown") cache.clear(); }); registerOpenClawStateDatabaseLifecycleListener((e) => { if (e.kind !== "opened") cache.clear(); }); async function readTaskPullRequestReceipts(e, t, n) { t(); const r = captureOpenClawStateReadContext(); r.admission.assertCurrent(); const i = JSON.stringify([r.admission.identity.key, e]); let a = cache.get(i, n); if (!a) { a = (async () => { const { readGitHubPublicationInWorker: t } = await import("./github-publication-worker-yPEzBCWn.mjs"); const n = await t({ type: "githubPublications.taskReceipts", input: e }); r.admission.assertCurrent(); const i = n?.type === "githubPublications.taskReceipts" ? n.result : { receipts: [], count: 0, branchMatchCount: 0 }; const a = []; for (const e of i.receipts) { const t = e.pull_request_url ? URL.parse(e.pull_request_url) : null; const n = t?.pathname.split("/"); const r = Number(n?.[4]); if (!t || t.protocol !== "https:" || t.username || t.password || t.search || t.hash || n?.length !== 5 || n[3] !== "pull" || !n[1] || !n[2] || !Number.isSafeInteger(r) || r < 1) continue; a.push({ url: t.href, owner: n[1], repo: n[2], number: r, branch: e.branch, ...e.title ? { title: e.title } : {}, sourceSessionKey: e.session_key, sourceAgentId: e.agent_id }); } return { taskPullRequests: a, taskPullRequestCount: i.count, pullRequestCount: i.count + (e.branchUrls?.length ?? 0) - i.branchMatchCount }; })(); cache.set(i, a, n); const t = a; a.catch(() => cache.delete(i, t)); } const o = await a; r.admission.assertCurrent(); t(); return o; } //#endregion export { readTaskPullRequestReceipts as n, prepareTaskPublicationSessions as t };