11/** Parses user-provided cron delivery fields into narrow runtime values. */
2+ import { isRecord } from "@openclaw/normalization-core/record-coerce" ;
23import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce" ;
34import { z , type ZodType } from "zod" ;
45
@@ -18,6 +19,19 @@ export const LowercaseNonEmptyStringFieldSchema = z.preprocess(
1819 z . string ( ) . min ( 1 ) ,
1920) ;
2021
22+ // Persisted/legacy cron jobs may carry `delivery.channel` as a `{ id }` channel
23+ // reference instead of a bare id. Recover the id so the explicit channel survives
24+ // normalization; otherwise it is dropped and routing degrades to implicit "last",
25+ // misrouting delivery to another configured channel (e.g. Feishu instead of Telegram).
26+ const channelReferenceToId = ( value : unknown ) : unknown =>
27+ isRecord ( value ) && typeof value . id === "string" ? value . id : value ;
28+
29+ /** Accepts a channel id string or a `{ id }` channel reference from persisted cron jobs. */
30+ export const DeliveryChannelFieldSchema = z . preprocess (
31+ channelReferenceToId ,
32+ LowercaseNonEmptyStringFieldSchema ,
33+ ) ;
34+
2135/** Accepts non-empty string fields after trimming delivery input without changing case. */
2236export const TrimmedNonEmptyStringFieldSchema = z . preprocess (
2337 trimStringPreprocess ,
@@ -45,7 +59,7 @@ type ParsedDeliveryInput = {
4559export function parseDeliveryInput ( input : Record < string , unknown > ) : ParsedDeliveryInput {
4660 return {
4761 mode : parseOptionalField ( DeliveryModeFieldSchema , input . mode ) ,
48- channel : parseOptionalField ( LowercaseNonEmptyStringFieldSchema , input . channel ) ,
62+ channel : parseOptionalField ( DeliveryChannelFieldSchema , input . channel ) ,
4963 to : parseOptionalField ( TrimmedNonEmptyStringFieldSchema , input . to ) ,
5064 threadId : parseOptionalField ( DeliveryThreadIdFieldSchema , input . threadId ) ,
5165 accountId : parseOptionalField ( TrimmedNonEmptyStringFieldSchema , input . accountId ) ,
0 commit comments