11import { hasAnyAuthProfileStoreSource } from "../../agents/auth-profiles/source-check.js" ;
2+ import type { MessagingToolSend } from "../../agents/pi-embedded-messaging.types.js" ;
23import type { SkillSnapshot } from "../../agents/skills.js" ;
34import type { ThinkLevel } from "../../auto-reply/thinking.js" ;
45import type { CliDeps } from "../../cli/outbound-send-deps.js" ;
56import type { AgentDefaultsConfig } from "../../config/types.agent-defaults.js" ;
67import type { OpenClawConfig } from "../../config/types.openclaw.js" ;
7- import { resolveCronDeliveryPlan } from "../delivery-plan.js" ;
8- import type { CronJob , CronRunTelemetry } from "../types.js" ;
8+ import { resolveCronDeliveryPlan , type CronDeliveryPlan } from "../delivery-plan.js" ;
9+ import type {
10+ CronDeliveryTrace ,
11+ CronDeliveryTraceMessageTarget ,
12+ CronDeliveryTraceTarget ,
13+ CronJob ,
14+ CronRunTelemetry ,
15+ } from "../types.js" ;
916import {
1017 isHeartbeatOnlyResponse ,
1118 resolveCronPayloadOutcome ,
@@ -120,6 +127,85 @@ type CronModelCatalogRuntime = typeof import("./run-model-catalog.runtime.js");
120127type CronDeliveryRuntime = typeof import ( "./run-delivery.runtime.js" ) ;
121128type ResolvedCronDeliveryTarget = Awaited < ReturnType < CronDeliveryRuntime [ "resolveDeliveryTarget" ] > > ;
122129
130+ function normalizeCronTraceTarget (
131+ target : CronDeliveryTraceTarget | undefined ,
132+ ) : CronDeliveryTraceTarget | undefined {
133+ if ( ! target ) {
134+ return undefined ;
135+ }
136+ return {
137+ ...( target . channel ? { channel : target . channel } : { } ) ,
138+ ...( target . to !== undefined ? { to : target . to } : { } ) ,
139+ ...( target . accountId ? { accountId : target . accountId } : { } ) ,
140+ ...( target . threadId !== undefined ? { threadId : target . threadId } : { } ) ,
141+ ...( target . source ? { source : target . source } : { } ) ,
142+ } ;
143+ }
144+
145+ function normalizeMessagingToolTarget (
146+ target : MessagingToolSend ,
147+ ) : CronDeliveryTraceMessageTarget | undefined {
148+ const channel = target . provider ?. trim ( ) ;
149+ if ( ! channel ) {
150+ return undefined ;
151+ }
152+ return {
153+ channel,
154+ ...( target . to ? { to : target . to } : { } ) ,
155+ ...( target . accountId ? { accountId : target . accountId } : { } ) ,
156+ ...( target . threadId ? { threadId : target . threadId } : { } ) ,
157+ } ;
158+ }
159+
160+ function buildCronDeliveryTrace ( params : {
161+ deliveryPlan : CronDeliveryPlan ;
162+ resolvedDelivery : ResolvedCronDeliveryTarget ;
163+ messagingToolSentTargets : MessagingToolSend [ ] ;
164+ fallbackUsed : boolean ;
165+ delivered : boolean ;
166+ } ) : CronDeliveryTrace {
167+ const intended = normalizeCronTraceTarget ( {
168+ channel : params . deliveryPlan . channel ?? "last" ,
169+ to : params . deliveryPlan . to ?? null ,
170+ accountId : params . deliveryPlan . accountId ,
171+ threadId : params . deliveryPlan . threadId ,
172+ source :
173+ params . deliveryPlan . channel === "last" || ! params . deliveryPlan . channel ? "last" : "explicit" ,
174+ } ) ;
175+ const resolved = params . resolvedDelivery . ok
176+ ? {
177+ ok : true ,
178+ ...normalizeCronTraceTarget ( {
179+ channel : params . resolvedDelivery . channel ,
180+ to : params . resolvedDelivery . to ,
181+ accountId : params . resolvedDelivery . accountId ,
182+ threadId : params . resolvedDelivery . threadId ,
183+ source : params . resolvedDelivery . mode === "implicit" ? "last" : "explicit" ,
184+ } ) ,
185+ }
186+ : {
187+ ok : false ,
188+ ...normalizeCronTraceTarget ( {
189+ channel : params . resolvedDelivery . channel ,
190+ to : params . resolvedDelivery . to ?? null ,
191+ accountId : params . resolvedDelivery . accountId ,
192+ threadId : params . resolvedDelivery . threadId ,
193+ source : params . resolvedDelivery . mode === "implicit" ? "last" : "explicit" ,
194+ } ) ,
195+ error : params . resolvedDelivery . error . message ,
196+ } ;
197+ const messageToolSentTo = params . messagingToolSentTargets
198+ . map ( ( target ) => normalizeMessagingToolTarget ( target ) )
199+ . filter ( ( target ) : target is CronDeliveryTraceMessageTarget => Boolean ( target ) ) ;
200+ return {
201+ ...( intended ? { intended } : { } ) ,
202+ resolved,
203+ ...( messageToolSentTo . length > 0 ? { messageToolSentTo } : { } ) ,
204+ fallbackUsed : params . fallbackUsed ,
205+ delivered : params . delivered ,
206+ } ;
207+ }
208+
123209function resolveCronToolPolicy ( params : { deliveryMode : "announce" | "webhook" | "none" } ) {
124210 const enableMessageTool = params . deliveryMode !== "webhook" ;
125211 return {
@@ -233,6 +319,7 @@ type PreparedCronRunContext = {
233319 persistSessionEntry : PersistCronSessionEntry ;
234320 withRunSession : WithRunSession ;
235321 agentPayload : Extract < CronJob [ "payload" ] , { kind : "agentTurn" } > | null ;
322+ deliveryPlan : CronDeliveryPlan ;
236323 resolvedDelivery : ResolvedCronDeliveryTarget ;
237324 deliveryRequested : boolean ;
238325 toolPolicy : ReturnType < typeof resolveCronToolPolicy > ;
@@ -403,11 +490,12 @@ async function prepareCronRunContext(params: {
403490 input . job . payload . kind === "agentTurn" ? input . job . payload . timeoutSeconds : undefined ,
404491 } ) ;
405492 const agentPayload = input . job . payload . kind === "agentTurn" ? input . job . payload : null ;
406- const { deliveryRequested, resolvedDelivery, toolPolicy } = await resolveCronDeliveryContext ( {
407- cfg : cfgWithAgentDefaults ,
408- job : input . job ,
409- agentId,
410- } ) ;
493+ const { deliveryPlan, deliveryRequested, resolvedDelivery, toolPolicy } =
494+ await resolveCronDeliveryContext ( {
495+ cfg : cfgWithAgentDefaults ,
496+ job : input . job ,
497+ agentId,
498+ } ) ;
411499
412500 const { formattedTime, timeLine } = resolveCronStyleNow ( input . cfg , now ) ;
413501 const base = `[cron:${ input . job . id } ${ input . job . name } ] ${ input . message } ` . trim ( ) ;
@@ -518,6 +606,7 @@ async function prepareCronRunContext(params: {
518606 persistSessionEntry,
519607 withRunSession,
520608 agentPayload,
609+ deliveryPlan,
521610 resolvedDelivery,
522611 deliveryRequested,
523612 toolPolicy,
@@ -641,7 +730,11 @@ async function finalizeCronRun(params: {
641730 finalAssistantVisibleText : finalRunResult . meta ?. finalAssistantVisibleText ,
642731 preferFinalAssistantVisibleText : prepared . resolvedDelivery . channel === "telegram" ,
643732 } ) ;
644- const resolveRunOutcome = ( result ?: { delivered ?: boolean ; deliveryAttempted ?: boolean } ) =>
733+ const resolveRunOutcome = ( result ?: {
734+ delivered ?: boolean ;
735+ deliveryAttempted ?: boolean ;
736+ delivery ?: CronDeliveryTrace ;
737+ } ) =>
645738 prepared . withRunSession ( {
646739 status : hasFatalErrorPayload ? "error" : "ok" ,
647740 ...( hasFatalErrorPayload
@@ -651,6 +744,7 @@ async function finalizeCronRun(params: {
651744 outputText,
652745 delivered : result ?. delivered ,
653746 deliveryAttempted : result ?. deliveryAttempted ,
747+ delivery : result ?. delivery ,
654748 ...telemetry ,
655749 } ) ;
656750
@@ -702,25 +796,35 @@ async function finalizeCronRun(params: {
702796 abortReason : params . abortReason ,
703797 withRunSession : prepared . withRunSession ,
704798 } ) ;
799+ const deliveryTrace = buildCronDeliveryTrace ( {
800+ deliveryPlan : prepared . deliveryPlan ,
801+ resolvedDelivery : prepared . resolvedDelivery ,
802+ messagingToolSentTargets,
803+ fallbackUsed : deliveryResult . deliveryAttempted && ! skipMessagingToolDelivery ,
804+ delivered : deliveryResult . delivered ,
805+ } ) ;
705806 if ( deliveryResult . result ) {
706807 const resultWithDeliveryMeta : RunCronAgentTurnResult = {
707808 ...deliveryResult . result ,
708809 deliveryAttempted :
709810 deliveryResult . result . deliveryAttempted ?? deliveryResult . deliveryAttempted ,
811+ delivery : deliveryTrace ,
710812 } ;
711813 if ( ! hasFatalErrorPayload || deliveryResult . result . status !== "ok" ) {
712814 return resultWithDeliveryMeta ;
713815 }
714816 return resolveRunOutcome ( {
715817 delivered : deliveryResult . result . delivered ,
716818 deliveryAttempted : resultWithDeliveryMeta . deliveryAttempted ,
819+ delivery : deliveryTrace ,
717820 } ) ;
718821 }
719822 summary = deliveryResult . summary ;
720823 outputText = deliveryResult . outputText ;
721824 return resolveRunOutcome ( {
722825 delivered : deliveryResult . delivered ,
723826 deliveryAttempted : deliveryResult . deliveryAttempted ,
827+ delivery : deliveryTrace ,
724828 } ) ;
725829}
726830
0 commit comments