UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

221 lines (220 loc) • 8.05 kB
import { i as resolveExplicitSessionStorePathForScope, l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs"; import { x as resolveSessionAgentId } from "./agent-scope-config-DmXgFGKP.mjs"; import { t as isDiagnosticFlagEnabled } from "./diagnostic-flags-D41PMN4c.mjs"; import "./agent-scope-BwB8YH6y.mjs"; import { t as assertAdmittedRunOperatorAuthority } from "./admitted-run-context-D3Jvk_f_.mjs"; import { X as captureSessionTarget } from "./session-controller.state-BApgK0mV.mjs"; import { a as readChannelSourceTurnId } from "./session-event-handoff-CC7Ui3SA.mjs"; import { i as getCommandOwnerAuthority } from "./command-owner-authority-BIUd20IP.mjs"; import { n as canonicalizeMainSessionAlias } from "./main-session-BNL3-mRV.mjs"; import { b as retireSessionControllerInput, m as bindSessionControllerSource } from "./session-controller.lifecycle-C_Z3sybN.mjs"; import { n as resolveSessionKey } from "./session-key-oH67puaB.mjs"; import { c as retargetSessionControllerSource, s as reserveSessionControllerSource } from "./session-controller.mailbox-BcK8UF_b.mjs"; import { n as resolveQueueSettingsCore } from "./settings-D_ru6nFL.mjs"; import { n as formatStageTimings, t as createStageTimingTracker } from "./stage-timing-By2RFvzf.mjs"; import { u as resolveCommandTurnTargetSessionKey } from "./command-turn-context-BaN4kAo-.mjs"; //#region src/auto-reply/reply/reply-source-binding.types.ts const replySource = Symbol.for("openclaw.replySourceInput"); //#endregion //#region src/auto-reply/reply/reply-source-binding.ts const sourcePreparationRetry = Symbol("openclaw.replySourcePreparationRetry"); /** Gateway-owned retry policy follows its exact source without expanding public option types. */ function bindReplySourcePreparationRetry(e, t) { Object.assign(e, { [sourcePreparationRetry]: t }); } function readReplySourcePreparationRetry(e) { return e?.[sourcePreparationRetry]; } function bindReplySourceInput(e, t) { return Object.assign(e, { [replySource]: t }); } /** Participants reserve their own physical source rather than inheriting the group root. */ function clearReplySourceInput(e) { delete e[replySource]; return e; } function readReplySourceInput(e) { if (!e) return; const t = e; const n = e.turnAdoptionLifecycle; return t[replySource] ?? n?.[replySource]; } /** Capture only an actual physical source scope, before asynchronous preparation. */ async function prepareReplySourceInput(e, t, n) { const r = { ...n }; const i = e.SessionKey?.trim() || e.CommandTargetSessionKey?.trim(); const a = readReplySourceInput(r); if (a) { const e = a.claim?.operation?.abortSignal ?? a.abortSignal; r.abortSignal = r.abortSignal && r.abortSignal !== e ? AbortSignal.any([r.abortSignal, e]) : e; return { options: bindReplySourceInput(r, a), input: a, created: false }; } if (!i) return { options: r, created: false }; const o = resolveSessionAgentId({ sessionKey: i, config: t, fallbackAgentId: e.AgentId }); const s = canonicalizeMainSessionAlias({ cfg: t, agentId: o, sessionKey: resolveSessionKey(t.session?.scope ?? "per-sender", { ...e, SessionKey: i }, t.session?.mainKey, o) }); const c = resolveExplicitSessionStorePathForScope({ agentId: o, sessionKey: i }) ?? resolveSessionStorePathCore(t.session?.store, { agentId: o }); const l = resolveQueueSettingsCore({ cfg: t, channel: e.Provider ?? e.Surface }); if (e.InboundEventKind === "room_event") l.mode = "followup"; const u = [n?.abortSignal, n?.turnAdoptionLifecycle?.abortSignal].filter((e) => e !== void 0); const d = u.length > 1 ? AbortSignal.any(u) : u[0]; if (d?.aborted) return { options: { ...r, abortSignal: d }, created: false }; d?.throwIfAborted(); n?.operatorAuthority?.assertCurrent(); const f = reserveSessionControllerSource(s, { sourceTurnId: readChannelSourceTurnId(e), protocolRunId: n?.runId, policy: l, target: captureSessionTarget({ storeScope: c, sessionKey: s, aliases: [i], agentId: o, incarnation: n?.expectedExistingSessionId }), adapter: { signal: d, authority: n?.operatorAuthority } }); r.abortSignal = f.abortSignal; return { options: bindReplySourceInput(r, f), input: f, created: true }; } function bindReplySourceToFollowup(e, t) { const n = readReplySourceInput(e) ?? readReplySourceInput({ turnAdoptionLifecycle: t.turnAdoptionLifecycle }); if (n) bindSessionControllerSource(n, t); return n; } /** Only a resolved native/explicit command continuation may leave its source. */ function retargetReplySourceForExecution(e) { const t = readReplySourceInput(e.options); if (!t || !e.sessionKey) return; if (t.mailbox.owner.target?.storeScope === e.storePath && t.mailbox.owner.aliases.has(e.sessionKey)) return; if (resolveCommandTurnTargetSessionKey(e.ctx) !== e.sessionKey) throw new Error("Reply execution target differs from its captured source"); retargetSessionControllerSource(t, captureSessionTarget({ storeScope: e.storePath, sessionKey: e.sessionKey, incarnation: e.sessionId, agentId: e.agentId }), "command-target"); } /** Non-turn/failed preparation retires only its source, never a queued or raw owner. */ function retireUnadoptedReplySource(e) { if (e && !e.claim && !e.custody.enqueued && !e.injection) retireSessionControllerInput(e); } //#endregion //#region src/auto-reply/reply/reply-timing-tracker.ts function isReplyProfilerEnabled(e) { const t = e?.config; return isDiagnosticFlagEnabled("profiler", t) || isDiagnosticFlagEnabled("reply.profiler", t); } /** Keeps slow replies diagnosable; profiling lowers the warning thresholds. */ function createReplyTimingTracker(e) { const t = createStageTimingTracker(); let n = false; const r = e.enabled ? 1e3 : 1e4; const i = e.enabled ? 500 : 5e3; return { measure: t.measure, measureSync: t.measureSync, logIfSlow(a, o) { if (n && !o?.repeat) return; const { totalMs: s, stages: c } = t.snapshot(); const l = { totalMs: s, spans: c }; if (l.totalMs < r && !l.spans.some((e) => e.durationMs >= i)) return; if (!o?.repeat) n = true; const u = formatStageTimings(l.spans); let d; let f; if (e.formatMessage) { const t = a; f = Object.fromEntries((e.detailKeys?.(a) ?? []).map((e) => [e, t[e]])); d = e.formatMessage(a, l, u); } else { const e = a; const t = [ `totalMs=${l.totalMs}`, `stages=${u}`, e.outcome ? `outcome=${e.outcome}` : void 0, e.reason ? `reason=${e.reason}` : void 0, e.error ? `error="${e.error}"` : void 0 ].filter(Boolean).join(" "); d = `${e.message} ${t}`; f = { ...e.details, outcome: e.outcome, reason: e.reason, error: e.error }; } e.log.warn(d, { ...f, ...l }); } }; } //#endregion //#region src/auto-reply/reply/get-reply.types.ts /** Pin the host-issued source before public options cross asynchronous preparation. */ function prepareInternalGetReplyOptions(e, t) { const n = t && getCommandOwnerAuthority(t)?.operatorAuthority; if (!e && !n) return; const { operatorAuthority: r = n, ...i } = e ?? {}; if (r !== void 0) { assertAdmittedRunOperatorAuthority(r); r.assertCurrent(); } return { ...i, operatorAuthority: r }; } function withExtractedFileImages(e, t) { if (!t || t.length === 0) return e; return { ...e, extractedFileImages: [...e?.extractedFileImages ?? [], ...t] }; } function shouldBridgeCliPreambleEvents(e) { return e?.commentaryProgressEnabled === true || e?.progressPreambleEnabled === true; } //#endregion export { isReplyProfilerEnabled as a, bindReplySourceToFollowup as c, readReplySourceInput as d, readReplySourcePreparationRetry as f, createReplyTimingTracker as i, clearReplySourceInput as l, retireUnadoptedReplySource as m, shouldBridgeCliPreambleEvents as n, bindReplySourceInput as o, retargetReplySourceForExecution as p, withExtractedFileImages as r, bindReplySourcePreparationRetry as s, prepareInternalGetReplyOptions as t, prepareReplySourceInput as u };