|
| 1 | +import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce"; |
| 2 | +import { |
| 3 | + createOutboundPayloadPlan, |
| 4 | + projectOutboundPayloadPlanForMirror, |
| 5 | +} from "../../infra/outbound/payloads.js"; |
| 6 | +import { parseAgentSessionKey } from "../../routing/session-key.js"; |
| 7 | +import { commitBackgroundResultToSession } from "../../sessions/background-session-result.js"; |
| 8 | +import { createCronExecutionId } from "../run-id.js"; |
| 9 | +import { |
| 10 | + buildDirectCronTranscriptMirrorPayloads, |
| 11 | + resolveDirectCronTranscriptMirrorText, |
| 12 | +} from "./delivery-dispatch-awareness.js"; |
| 13 | +import type { DispatchCronDeliveryParams } from "./delivery-dispatch-types.js"; |
| 14 | + |
| 15 | +type CurrentSessionCompletionResult = |
| 16 | + | { ok: false; reason: string } |
| 17 | + | { ok: true; requiresExternalDelivery: boolean }; |
| 18 | + |
| 19 | +export async function commitCurrentSessionCronCompletion( |
| 20 | + params: DispatchCronDeliveryParams, |
| 21 | + text?: string, |
| 22 | +): Promise<CurrentSessionCompletionResult> { |
| 23 | + const sourceSessionKey = params.sourceSessionKey?.trim(); |
| 24 | + if (!sourceSessionKey) { |
| 25 | + return { ok: false, reason: "current cron delivery is missing its source session binding" }; |
| 26 | + } |
| 27 | + const completionText = |
| 28 | + resolveDirectCronTranscriptMirrorText( |
| 29 | + projectOutboundPayloadPlanForMirror( |
| 30 | + createOutboundPayloadPlan(buildDirectCronTranscriptMirrorPayloads(params.deliveryPayloads)), |
| 31 | + ), |
| 32 | + ) ?? normalizeOptionalString(text); |
| 33 | + if (!completionText) { |
| 34 | + return { ok: false, reason: "current cron completion has no durable transcript projection" }; |
| 35 | + } |
| 36 | + const runId = createCronExecutionId(params.job.id, params.runStartedAt); |
| 37 | + const committed = await commitBackgroundResultToSession({ |
| 38 | + agentId: params.agentId, |
| 39 | + sessionKey: sourceSessionKey, |
| 40 | + text: completionText, |
| 41 | + idempotencyKey: `cron-current-completion:${runId}`, |
| 42 | + provenance: { kind: "cron", jobId: params.job.id, runId }, |
| 43 | + config: params.cfgWithAgentDefaults, |
| 44 | + signal: params.abortSignal, |
| 45 | + }); |
| 46 | + if (!committed.ok) { |
| 47 | + return committed; |
| 48 | + } |
| 49 | + if (params.sourceDeliveryOutcome.satisfiesSourceDelivery) { |
| 50 | + return { ok: true, requiresExternalDelivery: false }; |
| 51 | + } |
| 52 | + if (params.resolvedDelivery.ok) { |
| 53 | + return { ok: true, requiresExternalDelivery: true }; |
| 54 | + } |
| 55 | + const sourceChannel = parseAgentSessionKey(sourceSessionKey)?.rest.split(":")[0]; |
| 56 | + if (params.resolvedDelivery.channel === "webchat" || sourceChannel === "webchat") { |
| 57 | + return { ok: true, requiresExternalDelivery: false }; |
| 58 | + } |
| 59 | + return { ok: false, reason: params.resolvedDelivery.error.message }; |
| 60 | +} |
0 commit comments