|
| 1 | +/** |
| 2 | + * Detects message-tool sends that delivered a visible reply to the current source. |
| 3 | + */ |
| 4 | +import type { SourceReplyDeliveryMode } from "../auto-reply/get-reply-options.types.js"; |
| 5 | +import { isMessageToolSendActionName } from "./embedded-agent-messaging.js"; |
| 6 | +import { isToolResultError } from "./embedded-agent-subscribe.tools.js"; |
| 7 | +import { normalizeToolName } from "./tool-policy.js"; |
| 8 | + |
| 9 | +const MESSAGE_TOOL_NAME = "message"; |
| 10 | +const EXPLICIT_MESSAGE_ROUTE_KEYS = ["channel", "target", "to", "channelId", "provider"]; |
| 11 | +const DRY_RUN_DELIVERY_STATUS = "dry_run"; |
| 12 | +const SENT_DELIVERY_STATUS = "sent"; |
| 13 | +const RESULT_ENVELOPE_KEYS = [ |
| 14 | + "details", |
| 15 | + "payload", |
| 16 | + "result", |
| 17 | + "results", |
| 18 | + "sendResult", |
| 19 | + "toolResult", |
| 20 | +]; |
| 21 | + |
| 22 | +function asRecord(value: unknown): Record<string, unknown> { |
| 23 | + return value && typeof value === "object" && !Array.isArray(value) |
| 24 | + ? (value as Record<string, unknown>) |
| 25 | + : {}; |
| 26 | +} |
| 27 | + |
| 28 | +function hasStringValue(value: unknown): boolean { |
| 29 | + return typeof value === "string" && value.trim().length > 0; |
| 30 | +} |
| 31 | + |
| 32 | +function hasExplicitMessageRoute(args: Record<string, unknown>): boolean { |
| 33 | + if (EXPLICIT_MESSAGE_ROUTE_KEYS.some((key) => hasStringValue(args[key]))) { |
| 34 | + return true; |
| 35 | + } |
| 36 | + return Array.isArray(args.targets) && args.targets.some((value) => hasStringValue(value)); |
| 37 | +} |
| 38 | + |
| 39 | +function normalizeStatus(value: unknown): string | undefined { |
| 40 | + return typeof value === "string" ? value.trim().toLowerCase() : undefined; |
| 41 | +} |
| 42 | + |
| 43 | +function parseJsonRecord(value: string): Record<string, unknown> | undefined { |
| 44 | + try { |
| 45 | + const parsed = JSON.parse(value); |
| 46 | + return parsed && typeof parsed === "object" && !Array.isArray(parsed) |
| 47 | + ? (parsed as Record<string, unknown>) |
| 48 | + : undefined; |
| 49 | + } catch { |
| 50 | + return undefined; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function recordHasDeliveredMessageId(record: Record<string, unknown>): boolean { |
| 55 | + if (hasStringValue(record.messageId)) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + const receipt = record.receipt; |
| 59 | + if (!receipt || typeof receipt !== "object" || Array.isArray(receipt)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + const receiptRecord = receipt as Record<string, unknown>; |
| 63 | + return ( |
| 64 | + hasStringValue(receiptRecord.primaryPlatformMessageId) || |
| 65 | + (Array.isArray(receiptRecord.platformMessageIds) && |
| 66 | + receiptRecord.platformMessageIds.some((value) => hasStringValue(value))) |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +function deliveryEnvelopeIndicatesDryRun(value: unknown, depth = 0): boolean { |
| 71 | + if (!value || typeof value !== "object" || depth > 4) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + if (Array.isArray(value)) { |
| 75 | + return value.some((item) => deliveryEnvelopeIndicatesDryRun(item, depth + 1)); |
| 76 | + } |
| 77 | + |
| 78 | + const record = value as Record<string, unknown>; |
| 79 | + if ( |
| 80 | + record.dryRun === true || |
| 81 | + normalizeStatus(record.deliveryStatus) === DRY_RUN_DELIVERY_STATUS |
| 82 | + ) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + const content = record.content; |
| 87 | + if (Array.isArray(content)) { |
| 88 | + for (const item of content) { |
| 89 | + if (deliveryEnvelopeIndicatesDryRun(item, depth + 1)) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + if (item && typeof item === "object" && !Array.isArray(item)) { |
| 93 | + const text = (item as Record<string, unknown>).text; |
| 94 | + if (typeof text === "string") { |
| 95 | + const parsed = parseJsonRecord(text); |
| 96 | + if (parsed && deliveryEnvelopeIndicatesDryRun(parsed, depth + 1)) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return RESULT_ENVELOPE_KEYS.some((key) => |
| 105 | + deliveryEnvelopeIndicatesDryRun(record[key], depth + 1), |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +function deliveryEnvelopeIndicatesDelivered(value: unknown, depth = 0): boolean { |
| 110 | + if (!value || typeof value !== "object" || depth > 4) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + if (Array.isArray(value)) { |
| 114 | + return value.some((item) => deliveryEnvelopeIndicatesDelivered(item, depth + 1)); |
| 115 | + } |
| 116 | + |
| 117 | + const record = value as Record<string, unknown>; |
| 118 | + if ( |
| 119 | + normalizeStatus(record.deliveryStatus) === SENT_DELIVERY_STATUS || |
| 120 | + recordHasDeliveredMessageId(record) |
| 121 | + ) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + const content = record.content; |
| 126 | + if (Array.isArray(content)) { |
| 127 | + for (const item of content) { |
| 128 | + if (deliveryEnvelopeIndicatesDelivered(item, depth + 1)) { |
| 129 | + return true; |
| 130 | + } |
| 131 | + if (item && typeof item === "object" && !Array.isArray(item)) { |
| 132 | + const text = (item as Record<string, unknown>).text; |
| 133 | + if (typeof text === "string") { |
| 134 | + const parsed = parseJsonRecord(text); |
| 135 | + if (parsed && deliveryEnvelopeIndicatesDelivered(parsed, depth + 1)) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return RESULT_ENVELOPE_KEYS.some((key) => |
| 144 | + deliveryEnvelopeIndicatesDelivered(record[key], depth + 1), |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * Only implicit-route, non-dry-run, delivered `message.send` calls qualify. |
| 150 | + * Explicit routes and other messaging tools are outbound side effects, not source replies. |
| 151 | + */ |
| 152 | +export function isDeliveredMessageToolOnlySourceReplyResult(params: { |
| 153 | + sourceReplyDeliveryMode?: SourceReplyDeliveryMode; |
| 154 | + toolName: string; |
| 155 | + args?: unknown; |
| 156 | + result?: unknown; |
| 157 | + hookResult?: unknown; |
| 158 | + isError?: boolean; |
| 159 | +}): boolean { |
| 160 | + if (params.sourceReplyDeliveryMode !== "message_tool_only") { |
| 161 | + return false; |
| 162 | + } |
| 163 | + if (normalizeToolName(params.toolName) !== MESSAGE_TOOL_NAME) { |
| 164 | + return false; |
| 165 | + } |
| 166 | + const args = asRecord(params.args); |
| 167 | + if (!isMessageToolSendActionName(args.action) || hasExplicitMessageRoute(args)) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + if (params.isError || isToolResultError(params.result) || isToolResultError(params.hookResult)) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + if ( |
| 174 | + args.dryRun === true || |
| 175 | + deliveryEnvelopeIndicatesDryRun(params.result) || |
| 176 | + deliveryEnvelopeIndicatesDryRun(params.hookResult) |
| 177 | + ) { |
| 178 | + return false; |
| 179 | + } |
| 180 | + return ( |
| 181 | + deliveryEnvelopeIndicatesDelivered(params.result) || |
| 182 | + deliveryEnvelopeIndicatesDelivered(params.hookResult) |
| 183 | + ); |
| 184 | +} |
0 commit comments