UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

120 lines (119 loc) • 4.41 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs"; import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs"; import { d as hasUnretiredRpcSource, r as getRpcSourceForGateway } from "./session-controller.rpc-sources-CTFbRW2t.mjs"; import { i as setGatewayDedupeEntry } from "./agent-job-mP_hfXWa.mjs"; //#region src/gateway/agent-turn/agent-dedupe.ts var AgentRequestReservationEndedError = class extends Error { constructor() { super("Agent request reservation is no longer active."); } }; function resolveAgentDedupeKeys(e) { const t = [`agent:${e.idempotencyKey}`]; const n = e.execApprovalFollowupApprovalId?.trim(); if (n) t.push(`agent:exec-approval-followup:${n}`); return uniqueStrings(t); } function readGatewayDedupeEntry(e) { for (const t of e.keys) { const n = e.dedupe.get(t); if (n) return n; } } function isAcceptedAgentDedupePayload(e) { return typeof e === "object" && e !== null && e.status === "accepted"; } function resolveAgentWaitSource(e, t) { const n = getRpcSourceForGateway(t, e.resolveGatewayContext); if (n) return n.adapter.kind === "agent" ? "agent" : "chat"; return isAcceptedAgentDedupePayload(e.dedupe.get(`agent:${t}`)?.payload) ? "agent" : void 0; } function isPreRegistrationAbortedAgentDedupePayload(e) { const t = e?.stopReason; return typeof e === "object" && e !== null && e.status === "timeout" && (t === "rpc" || t === "stop"); } function isPreRegistrationAbortedAgentDedupeEntryForSession(e) { if (!e.entry?.ok || !isPreRegistrationAbortedAgentDedupePayload(e.entry.payload)) return false; const t = e.entry.payload; const n = typeof t.runId === "string" ? t.runId : ""; if (n && n !== e.runId) return false; const r = normalizeOptionalString(t.sessionKey); const i = normalizeOptionalString(t.agentId); if (e.agentId && i !== e.agentId) return false; const a = new Set([e.sessionKey, ...e.alternateSessionKeys ?? []].filter((e) => Boolean(e?.trim()))); return !r || a.size === 0 || a.has(r); } function setGatewayDedupeEntries(e) { for (const t of e.keys) setGatewayDedupeEntry({ ...e, key: t }); } function buildAbortedAgentPayload(e, t, n) { return { runId: e, ...n?.agentId ? { agentId: n.agentId } : {}, ...n?.sessionKey ? { sessionKey: n.sessionKey } : {}, status: "timeout", summary: "aborted", stopReason: t, timeoutPhase: "queue", providerStarted: false }; } function setAbortedAgentDedupeEntries(e) { setGatewayDedupeEntries({ ...e, entry: { ts: Date.now(), ok: true, payload: buildAbortedAgentPayload(e.runId, e.stopReason, e) } }); } function replayAgentTurnIfCached(e) { const { agentDedupeKeys: t, runId: n } = e.preflight; const r = readGatewayDedupeEntry({ dedupe: e.context.dedupe, keys: t }); if (!r) return false; if (e.acceptedOnly && !(r.ok && isAcceptedAgentDedupePayload(r.payload))) return false; if (e.acceptedOnly && isAcceptedAgentDedupePayload(r.payload) && !r.payload.reservationId && !hasUnretiredRpcSource(n, { sessionKey: normalizeOptionalString(r.payload.sessionKey), agentId: normalizeOptionalString(r.payload.agentId) }, e.context.resolveGatewayContext)) return false; if (r.ok && isAcceptedAgentDedupePayload(r.payload)) { const t = normalizeOptionalString(r.payload.runId) ?? n; const i = normalizeOptionalString(r.payload.sessionKey); const a = normalizeOptionalString(r.payload.agentId); const o = asOptionalRecord(r.payload.runtime); const s = typeof r.payload.reservationId === "string"; e.io.emitAcceptance([ true, { runId: t, status: "in_flight", ...i ? { sessionKey: i } : {}, ...a ? { agentId: a } : {}, ...o ? { runtime: o } : {}, ...s ? { admissionPending: true } : {} }, void 0 ], { cached: true, runId: t }); } else e.io.emitAcceptance([ r.ok, r.payload, r.error ], { cached: true, ...r.incognito && r.error ? { errorMessage: "Incognito agent error." } : {} }); return true; } //#endregion export { readGatewayDedupeEntry as a, resolveAgentWaitSource as c, isPreRegistrationAbortedAgentDedupeEntryForSession as i, setAbortedAgentDedupeEntries as l, buildAbortedAgentPayload as n, replayAgentTurnIfCached as o, isAcceptedAgentDedupePayload as r, resolveAgentDedupeKeys as s, AgentRequestReservationEndedError as t, setGatewayDedupeEntries as u };