@openclaw/whatsapp 2026.5.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/action-runtime-api.ts +1 -0
- package/action-runtime.runtime.ts +1 -0
- package/api.ts +213 -0
- package/auth-presence.ts +80 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +3 -0
- package/config-api.ts +4 -0
- package/constants.ts +1 -0
- package/contract-api.ts +29 -0
- package/directory-contract-api.ts +4 -0
- package/dist/.boundary-tsc.stamp +1 -0
- package/dist/.boundary-tsc.tsbuildinfo +1 -0
- package/doctor-contract-api.ts +8 -0
- package/index.test.ts +13 -0
- package/index.ts +16 -0
- package/legacy-session-surface-api.ts +6 -0
- package/legacy-state-migrations-api.ts +1 -0
- package/light-runtime-api.ts +12 -0
- package/login-qr-api.ts +1 -0
- package/login-qr-runtime.ts +23 -0
- package/openclaw.plugin.json +742 -0
- package/outbound-payload-test-api.ts +1 -0
- package/package.json +74 -0
- package/runtime-api.ts +86 -0
- package/secret-contract-api.ts +4 -0
- package/security-contract-api.ts +4 -0
- package/setup-entry.test.ts +21 -0
- package/setup-entry.ts +21 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-config.ts +77 -0
- package/src/account-ids.ts +13 -0
- package/src/account-types.ts +5 -0
- package/src/accounts.test.ts +182 -0
- package/src/accounts.ts +176 -0
- package/src/accounts.whatsapp-auth.test.ts +59 -0
- package/src/action-runtime-target-auth.ts +27 -0
- package/src/action-runtime.test.ts +330 -0
- package/src/action-runtime.ts +76 -0
- package/src/active-listener.test.ts +65 -0
- package/src/active-listener.ts +17 -0
- package/src/agent-tools-login.test.ts +81 -0
- package/src/agent-tools-login.ts +113 -0
- package/src/approval-auth.test.ts +24 -0
- package/src/approval-auth.ts +27 -0
- package/src/auth-store.runtime.ts +1 -0
- package/src/auth-store.test.ts +311 -0
- package/src/auth-store.ts +502 -0
- package/src/auto-reply/config.runtime.ts +16 -0
- package/src/auto-reply/constants.ts +1 -0
- package/src/auto-reply/deliver-reply.test.ts +843 -0
- package/src/auto-reply/deliver-reply.ts +279 -0
- package/src/auto-reply/heartbeat-runner.runtime.ts +33 -0
- package/src/auto-reply/heartbeat-runner.test.ts +214 -0
- package/src/auto-reply/heartbeat-runner.ts +330 -0
- package/src/auto-reply/loggers.ts +6 -0
- package/src/auto-reply/mentions.ts +131 -0
- package/src/auto-reply/monitor/ack-reaction.test.ts +170 -0
- package/src/auto-reply/monitor/ack-reaction.ts +99 -0
- package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
- package/src/auto-reply/monitor/broadcast.ts +153 -0
- package/src/auto-reply/monitor/commands.ts +19 -0
- package/src/auto-reply/monitor/echo.ts +64 -0
- package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
- package/src/auto-reply/monitor/group-activation.test.ts +189 -0
- package/src/auto-reply/monitor/group-activation.ts +73 -0
- package/src/auto-reply/monitor/group-gating.audio-preflight.test.ts +103 -0
- package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
- package/src/auto-reply/monitor/group-gating.ts +217 -0
- package/src/auto-reply/monitor/group-members.test.ts +56 -0
- package/src/auto-reply/monitor/group-members.ts +65 -0
- package/src/auto-reply/monitor/inbound-context.test.ts +97 -0
- package/src/auto-reply/monitor/inbound-context.ts +92 -0
- package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +21 -0
- package/src/auto-reply/monitor/inbound-dispatch.test.ts +895 -0
- package/src/auto-reply/monitor/inbound-dispatch.ts +422 -0
- package/src/auto-reply/monitor/last-route.test.ts +37 -0
- package/src/auto-reply/monitor/last-route.ts +61 -0
- package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
- package/src/auto-reply/monitor/message-line.ts +54 -0
- package/src/auto-reply/monitor/on-message.audio-preflight.test.ts +354 -0
- package/src/auto-reply/monitor/on-message.ts +291 -0
- package/src/auto-reply/monitor/peer.ts +17 -0
- package/src/auto-reply/monitor/process-message.audio-preflight.test.ts +420 -0
- package/src/auto-reply/monitor/process-message.test.ts +355 -0
- package/src/auto-reply/monitor/process-message.ts +531 -0
- package/src/auto-reply/monitor/runtime-api.ts +30 -0
- package/src/auto-reply/monitor-state.test.ts +64 -0
- package/src/auto-reply/monitor-state.ts +106 -0
- package/src/auto-reply/monitor.ts +600 -0
- package/src/auto-reply/reply-resolver.runtime.ts +1 -0
- package/src/auto-reply/session-snapshot.ts +69 -0
- package/src/auto-reply/types.ts +48 -0
- package/src/auto-reply/util.ts +63 -0
- package/src/auto-reply/web-auto-reply-monitor.test.ts +687 -0
- package/src/auto-reply/web-auto-reply-utils.test.ts +327 -0
- package/src/auto-reply.broadcast-groups.combined.test.ts +246 -0
- package/src/auto-reply.broadcast-groups.test-harness.ts +47 -0
- package/src/auto-reply.impl.ts +7 -0
- package/src/auto-reply.test-harness.ts +421 -0
- package/src/auto-reply.ts +1 -0
- package/src/auto-reply.web-auto-reply.compresses-common-formats-jpeg-cap.test.ts +427 -0
- package/src/auto-reply.web-auto-reply.connection-and-logging.e2e.test.ts +908 -0
- package/src/auto-reply.web-auto-reply.last-route.test.ts +184 -0
- package/src/channel-actions.runtime.ts +7 -0
- package/src/channel-actions.test.ts +216 -0
- package/src/channel-actions.ts +84 -0
- package/src/channel-outbound.test.ts +131 -0
- package/src/channel-outbound.ts +36 -0
- package/src/channel-react-action.runtime.ts +7 -0
- package/src/channel-react-action.test.ts +294 -0
- package/src/channel-react-action.ts +84 -0
- package/src/channel.runtime.ts +117 -0
- package/src/channel.setup.test.ts +461 -0
- package/src/channel.setup.ts +28 -0
- package/src/channel.ts +336 -0
- package/src/command-policy.ts +7 -0
- package/src/config-accessors.test.ts +34 -0
- package/src/config-accessors.ts +22 -0
- package/src/config-schema.test.ts +121 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +24 -0
- package/src/connection-controller-registry.test.ts +27 -0
- package/src/connection-controller-registry.ts +49 -0
- package/src/connection-controller.test.ts +282 -0
- package/src/connection-controller.ts +679 -0
- package/src/creds-files.ts +19 -0
- package/src/creds-persistence.ts +102 -0
- package/src/directory-config.test.ts +60 -0
- package/src/directory-config.ts +40 -0
- package/src/directory-contract.test.ts +56 -0
- package/src/doctor-contract.ts +11 -0
- package/src/doctor.test.ts +74 -0
- package/src/doctor.ts +56 -0
- package/src/group-intro.ts +15 -0
- package/src/group-policy.test.ts +36 -0
- package/src/group-policy.ts +40 -0
- package/src/group-session-contract.ts +20 -0
- package/src/group-session-key.test.ts +53 -0
- package/src/group-session-key.ts +41 -0
- package/src/heartbeat-recipients.runtime.ts +6 -0
- package/src/heartbeat-recipients.test.ts +203 -0
- package/src/heartbeat-recipients.ts +104 -0
- package/src/heartbeat.ts +34 -0
- package/src/identity.ts +164 -0
- package/src/inbound/access-control.test-harness.ts +37 -0
- package/src/inbound/access-control.test.ts +375 -0
- package/src/inbound/access-control.ts +237 -0
- package/src/inbound/dedupe.ts +132 -0
- package/src/inbound/extract.test.ts +282 -0
- package/src/inbound/extract.ts +488 -0
- package/src/inbound/lifecycle.ts +39 -0
- package/src/inbound/media.node.test.ts +83 -0
- package/src/inbound/media.ts +102 -0
- package/src/inbound/monitor.ts +890 -0
- package/src/inbound/runtime-api.ts +7 -0
- package/src/inbound/save-media.runtime.ts +1 -0
- package/src/inbound/send-api.test.ts +307 -0
- package/src/inbound/send-api.ts +141 -0
- package/src/inbound/send-result.ts +61 -0
- package/src/inbound/types.ts +106 -0
- package/src/inbound-context.contract.test.ts +32 -0
- package/src/inbound-policy.ts +231 -0
- package/src/inbound.media.test.ts +320 -0
- package/src/inbound.test.ts +290 -0
- package/src/inbound.ts +9 -0
- package/src/login-qr.test.ts +483 -0
- package/src/login-qr.ts +542 -0
- package/src/login.coverage.test.ts +133 -0
- package/src/login.test.ts +89 -0
- package/src/login.ts +70 -0
- package/src/logout.test.ts +162 -0
- package/src/media.test.ts +480 -0
- package/src/media.ts +10 -0
- package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +400 -0
- package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
- package/src/monitor-inbox.behavior.test.ts +5 -0
- package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +362 -0
- package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +306 -0
- package/src/monitor-inbox.streams-inbound-messages.test-support.ts +729 -0
- package/src/monitor-inbox.test-harness.ts +304 -0
- package/src/normalize-target.ts +112 -0
- package/src/normalize.ts +6 -0
- package/src/outbound-adapter.poll.test.ts +62 -0
- package/src/outbound-adapter.sendpayload.test.ts +195 -0
- package/src/outbound-adapter.ts +36 -0
- package/src/outbound-base.test.ts +560 -0
- package/src/outbound-base.ts +244 -0
- package/src/outbound-media-contract.ts +278 -0
- package/src/outbound-media.runtime.ts +36 -0
- package/src/outbound-payload.contract.test.ts +61 -0
- package/src/outbound-send-deps.ts +1 -0
- package/src/outbound-test-support.ts +16 -0
- package/src/pairing-security.test-harness.ts +59 -0
- package/src/qr-image.ts +1 -0
- package/src/qr-terminal.ts +1 -0
- package/src/quoted-message.test.ts +83 -0
- package/src/quoted-message.ts +184 -0
- package/src/reaction-level.test.ts +94 -0
- package/src/reaction-level.ts +21 -0
- package/src/reconnect.test.ts +51 -0
- package/src/reconnect.ts +55 -0
- package/src/resolve-outbound-target.test.ts +265 -0
- package/src/resolve-outbound-target.ts +54 -0
- package/src/resolve-target.test.ts +97 -0
- package/src/runtime-api.ts +60 -0
- package/src/runtime-group-policy.ts +16 -0
- package/src/runtime.ts +9 -0
- package/src/security-contract.ts +47 -0
- package/src/security-fix.ts +71 -0
- package/src/send.test.ts +515 -0
- package/src/send.ts +287 -0
- package/src/session-contract.test.ts +31 -0
- package/src/session-contract.ts +43 -0
- package/src/session-errors.ts +125 -0
- package/src/session-route.ts +26 -0
- package/src/session.runtime.ts +8 -0
- package/src/session.test.ts +559 -0
- package/src/session.ts +330 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-finalize.ts +446 -0
- package/src/setup-surface.test.ts +367 -0
- package/src/setup-surface.ts +69 -0
- package/src/setup-test-helpers.ts +216 -0
- package/src/shared.ts +291 -0
- package/src/socket-timing.test.ts +49 -0
- package/src/socket-timing.ts +38 -0
- package/src/state-migrations.ts +54 -0
- package/src/status-issues.test.ts +137 -0
- package/src/status-issues.ts +185 -0
- package/src/system-prompt.test.ts +199 -0
- package/src/system-prompt.ts +31 -0
- package/src/targets-runtime.ts +180 -0
- package/src/test-helpers.ts +690 -0
- package/src/text-runtime.test.ts +162 -0
- package/src/text-runtime.ts +11 -0
- package/src/vcard.ts +84 -0
- package/targets.ts +5 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import { hasVisibleInboundReplyDispatch } from "openclaw/plugin-sdk/inbound-reply-dispatch";
|
|
2
|
+
import {
|
|
3
|
+
type DeliverableWhatsAppOutboundPayload,
|
|
4
|
+
normalizeWhatsAppOutboundPayload,
|
|
5
|
+
normalizeWhatsAppPayloadTextPreservingIndentation,
|
|
6
|
+
} from "../../outbound-media-contract.js";
|
|
7
|
+
import type { WhatsAppReplyDeliveryResult } from "../deliver-reply.js";
|
|
8
|
+
import type { WebInboundMsg } from "../types.js";
|
|
9
|
+
import { formatGroupMembers } from "./group-members.js";
|
|
10
|
+
import type { GroupHistoryEntry } from "./inbound-context.js";
|
|
11
|
+
import {
|
|
12
|
+
createChannelReplyPipeline,
|
|
13
|
+
dispatchReplyWithBufferedBlockDispatcher,
|
|
14
|
+
finalizeInboundContext,
|
|
15
|
+
getAgentScopedMediaLocalRoots,
|
|
16
|
+
jidToE164,
|
|
17
|
+
logVerbose,
|
|
18
|
+
resolveChunkMode,
|
|
19
|
+
resolveIdentityNamePrefix,
|
|
20
|
+
resolveInboundLastRouteSessionKey,
|
|
21
|
+
resolveMarkdownTableMode,
|
|
22
|
+
resolveSendableOutboundReplyParts,
|
|
23
|
+
resolveTextChunkLimit,
|
|
24
|
+
shouldLogVerbose,
|
|
25
|
+
toLocationContext,
|
|
26
|
+
type getChildLogger,
|
|
27
|
+
type getReplyFromConfig,
|
|
28
|
+
type LoadConfigFn,
|
|
29
|
+
type ReplyPayload,
|
|
30
|
+
type resolveAgentRoute,
|
|
31
|
+
} from "./inbound-dispatch.runtime.js";
|
|
32
|
+
|
|
33
|
+
type ReplyLifecycleKind = "tool" | "block" | "final";
|
|
34
|
+
type ChannelReplyOnModelSelected = NonNullable<
|
|
35
|
+
ReturnType<typeof createChannelReplyPipeline>["onModelSelected"]
|
|
36
|
+
>;
|
|
37
|
+
|
|
38
|
+
type WhatsAppDispatchPipeline = {
|
|
39
|
+
responsePrefix?: string;
|
|
40
|
+
} & Record<string, unknown>;
|
|
41
|
+
|
|
42
|
+
type VisibleReplyTarget = {
|
|
43
|
+
id?: string;
|
|
44
|
+
body?: string;
|
|
45
|
+
sender?: {
|
|
46
|
+
label?: string | null;
|
|
47
|
+
} | null;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
type ReplyThreadingContext = {
|
|
51
|
+
implicitCurrentMessage?: "default" | "allow" | "deny";
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type SenderContext = {
|
|
55
|
+
id?: string;
|
|
56
|
+
name?: string;
|
|
57
|
+
e164?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function logWhatsAppReplyDeliveryError(params: {
|
|
61
|
+
err: unknown;
|
|
62
|
+
info: { kind: ReplyLifecycleKind };
|
|
63
|
+
connectionId: string;
|
|
64
|
+
conversationId: string;
|
|
65
|
+
msg: WebInboundMsg;
|
|
66
|
+
replyLogger: ReturnType<typeof getChildLogger>;
|
|
67
|
+
}) {
|
|
68
|
+
params.replyLogger.error(
|
|
69
|
+
{
|
|
70
|
+
err: params.err,
|
|
71
|
+
replyKind: params.info.kind,
|
|
72
|
+
correlationId: params.msg.id ?? null,
|
|
73
|
+
connectionId: params.connectionId,
|
|
74
|
+
conversationId: params.conversationId,
|
|
75
|
+
chatId: params.msg.chatId ?? null,
|
|
76
|
+
to: params.msg.from ?? null,
|
|
77
|
+
from: params.msg.to ?? null,
|
|
78
|
+
},
|
|
79
|
+
"auto-reply delivery failed",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function resolveWhatsAppDisableBlockStreaming(cfg: ReturnType<LoadConfigFn>): boolean | undefined {
|
|
84
|
+
if (typeof cfg.channels?.whatsapp?.blockStreaming !== "boolean") {
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
return !cfg.channels.whatsapp.blockStreaming;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function resolveWhatsAppDeliverablePayload(
|
|
91
|
+
payload: ReplyPayload,
|
|
92
|
+
info: { kind: ReplyLifecycleKind },
|
|
93
|
+
): ReplyPayload | null {
|
|
94
|
+
if (payload.isReasoning === true || payload.isCompactionNotice === true) {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
if (payload.isError === true) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
if (info.kind === "tool") {
|
|
101
|
+
if (!resolveSendableOutboundReplyParts(payload).hasMedia) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
return { ...payload, text: undefined };
|
|
105
|
+
}
|
|
106
|
+
return payload;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function resolveWhatsAppResponsePrefix(params: {
|
|
110
|
+
cfg: ReturnType<LoadConfigFn>;
|
|
111
|
+
agentId: string;
|
|
112
|
+
isSelfChat: boolean;
|
|
113
|
+
pipelineResponsePrefix?: string;
|
|
114
|
+
}): string | undefined {
|
|
115
|
+
const configuredResponsePrefix = params.cfg.messages?.responsePrefix;
|
|
116
|
+
return (
|
|
117
|
+
params.pipelineResponsePrefix ??
|
|
118
|
+
(configuredResponsePrefix === undefined && params.isSelfChat
|
|
119
|
+
? resolveIdentityNamePrefix(params.cfg, params.agentId)
|
|
120
|
+
: undefined)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function buildWhatsAppInboundContext(params: {
|
|
125
|
+
bodyForAgent?: string;
|
|
126
|
+
combinedBody: string;
|
|
127
|
+
commandBody?: string;
|
|
128
|
+
commandAuthorized?: boolean;
|
|
129
|
+
conversationId: string;
|
|
130
|
+
groupHistory?: GroupHistoryEntry[];
|
|
131
|
+
groupMemberRoster?: Map<string, string>;
|
|
132
|
+
groupSystemPrompt?: string;
|
|
133
|
+
msg: WebInboundMsg;
|
|
134
|
+
rawBody?: string;
|
|
135
|
+
route: ReturnType<typeof resolveAgentRoute>;
|
|
136
|
+
sender: SenderContext;
|
|
137
|
+
transcript?: string;
|
|
138
|
+
mediaTranscribedIndexes?: number[];
|
|
139
|
+
replyThreading?: ReplyThreadingContext;
|
|
140
|
+
visibleReplyTo?: VisibleReplyTarget;
|
|
141
|
+
}) {
|
|
142
|
+
const inboundHistory =
|
|
143
|
+
params.msg.chatType === "group"
|
|
144
|
+
? (params.groupHistory ?? []).map((entry) => ({
|
|
145
|
+
sender: entry.sender,
|
|
146
|
+
body: entry.body,
|
|
147
|
+
timestamp: entry.timestamp,
|
|
148
|
+
}))
|
|
149
|
+
: undefined;
|
|
150
|
+
|
|
151
|
+
const result = finalizeInboundContext({
|
|
152
|
+
Body: params.combinedBody,
|
|
153
|
+
BodyForAgent: params.bodyForAgent ?? params.msg.body,
|
|
154
|
+
InboundHistory: inboundHistory,
|
|
155
|
+
RawBody: params.rawBody ?? params.msg.body,
|
|
156
|
+
CommandBody: params.commandBody ?? params.msg.body,
|
|
157
|
+
Transcript: params.transcript,
|
|
158
|
+
From: params.msg.from,
|
|
159
|
+
To: params.msg.to,
|
|
160
|
+
SessionKey: params.route.sessionKey,
|
|
161
|
+
AccountId: params.route.accountId,
|
|
162
|
+
MessageSid: params.msg.id,
|
|
163
|
+
ReplyToId: params.visibleReplyTo?.id,
|
|
164
|
+
ReplyToBody: params.visibleReplyTo?.body,
|
|
165
|
+
ReplyToSender: params.visibleReplyTo?.sender?.label,
|
|
166
|
+
MediaPath: params.msg.mediaPath,
|
|
167
|
+
MediaUrl: params.msg.mediaUrl,
|
|
168
|
+
MediaType: params.msg.mediaType,
|
|
169
|
+
MediaTranscribedIndexes: params.mediaTranscribedIndexes,
|
|
170
|
+
ChatType: params.msg.chatType,
|
|
171
|
+
Timestamp: params.msg.timestamp,
|
|
172
|
+
ConversationLabel: params.msg.chatType === "group" ? params.conversationId : params.msg.from,
|
|
173
|
+
GroupSubject: params.msg.groupSubject,
|
|
174
|
+
GroupMembers: formatGroupMembers({
|
|
175
|
+
participants: params.msg.groupParticipants,
|
|
176
|
+
roster: params.groupMemberRoster,
|
|
177
|
+
fallbackE164: params.sender.e164,
|
|
178
|
+
}),
|
|
179
|
+
SenderName: params.sender.name,
|
|
180
|
+
SenderId: params.sender.id ?? params.sender.e164,
|
|
181
|
+
SenderE164: params.sender.e164,
|
|
182
|
+
CommandAuthorized: params.commandAuthorized,
|
|
183
|
+
ReplyThreading: params.replyThreading,
|
|
184
|
+
WasMentioned: params.msg.wasMentioned,
|
|
185
|
+
GroupSystemPrompt: params.groupSystemPrompt,
|
|
186
|
+
UntrustedStructuredContext: params.msg.untrustedStructuredContext,
|
|
187
|
+
...(params.msg.location ? toLocationContext(params.msg.location) : {}),
|
|
188
|
+
Provider: "whatsapp",
|
|
189
|
+
Surface: "whatsapp",
|
|
190
|
+
OriginatingChannel: "whatsapp",
|
|
191
|
+
OriginatingTo: params.msg.from,
|
|
192
|
+
});
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function resolveWhatsAppDmRouteTarget(params: {
|
|
197
|
+
msg: WebInboundMsg;
|
|
198
|
+
senderE164?: string;
|
|
199
|
+
normalizeE164: (value: string) => string | null;
|
|
200
|
+
}): string | undefined {
|
|
201
|
+
if (params.msg.chatType === "group") {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
if (params.senderE164) {
|
|
205
|
+
return params.normalizeE164(params.senderE164) ?? undefined;
|
|
206
|
+
}
|
|
207
|
+
if (params.msg.from.includes("@")) {
|
|
208
|
+
return jidToE164(params.msg.from) ?? undefined;
|
|
209
|
+
}
|
|
210
|
+
return params.normalizeE164(params.msg.from) ?? undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function updateWhatsAppMainLastRoute(params: {
|
|
214
|
+
backgroundTasks: Set<Promise<unknown>>;
|
|
215
|
+
cfg: ReturnType<LoadConfigFn>;
|
|
216
|
+
ctx: Record<string, unknown>;
|
|
217
|
+
dmRouteTarget?: string;
|
|
218
|
+
pinnedMainDmRecipient: string | null;
|
|
219
|
+
route: ReturnType<typeof resolveAgentRoute>;
|
|
220
|
+
updateLastRoute: (params: {
|
|
221
|
+
cfg: ReturnType<LoadConfigFn>;
|
|
222
|
+
backgroundTasks: Set<Promise<unknown>>;
|
|
223
|
+
storeAgentId: string;
|
|
224
|
+
sessionKey: string;
|
|
225
|
+
channel: "whatsapp";
|
|
226
|
+
to: string;
|
|
227
|
+
accountId?: string;
|
|
228
|
+
ctx: Record<string, unknown>;
|
|
229
|
+
warn: ReturnType<typeof getChildLogger>["warn"];
|
|
230
|
+
}) => void;
|
|
231
|
+
warn: ReturnType<typeof getChildLogger>["warn"];
|
|
232
|
+
}) {
|
|
233
|
+
const shouldUpdateMainLastRoute =
|
|
234
|
+
!params.pinnedMainDmRecipient || params.pinnedMainDmRecipient === params.dmRouteTarget;
|
|
235
|
+
const inboundLastRouteSessionKey = resolveInboundLastRouteSessionKey({
|
|
236
|
+
route: params.route,
|
|
237
|
+
sessionKey: params.route.sessionKey,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
if (
|
|
241
|
+
params.dmRouteTarget &&
|
|
242
|
+
inboundLastRouteSessionKey === params.route.mainSessionKey &&
|
|
243
|
+
shouldUpdateMainLastRoute
|
|
244
|
+
) {
|
|
245
|
+
params.updateLastRoute({
|
|
246
|
+
cfg: params.cfg,
|
|
247
|
+
backgroundTasks: params.backgroundTasks,
|
|
248
|
+
storeAgentId: params.route.agentId,
|
|
249
|
+
sessionKey: params.route.mainSessionKey,
|
|
250
|
+
channel: "whatsapp",
|
|
251
|
+
to: params.dmRouteTarget,
|
|
252
|
+
accountId: params.route.accountId,
|
|
253
|
+
ctx: params.ctx,
|
|
254
|
+
warn: params.warn,
|
|
255
|
+
});
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (
|
|
260
|
+
params.dmRouteTarget &&
|
|
261
|
+
inboundLastRouteSessionKey === params.route.mainSessionKey &&
|
|
262
|
+
params.pinnedMainDmRecipient
|
|
263
|
+
) {
|
|
264
|
+
logVerbose(
|
|
265
|
+
`Skipping main-session last route update for ${params.dmRouteTarget} (pinned owner ${params.pinnedMainDmRecipient})`,
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export async function dispatchWhatsAppBufferedReply(params: {
|
|
271
|
+
cfg: ReturnType<LoadConfigFn>;
|
|
272
|
+
connectionId: string;
|
|
273
|
+
context: Record<string, unknown>;
|
|
274
|
+
conversationId: string;
|
|
275
|
+
deliverReply: (params: {
|
|
276
|
+
replyResult: ReplyPayload;
|
|
277
|
+
normalizedReplyResult?: DeliverableWhatsAppOutboundPayload<ReplyPayload>;
|
|
278
|
+
msg: WebInboundMsg;
|
|
279
|
+
mediaLocalRoots: readonly string[];
|
|
280
|
+
maxMediaBytes: number;
|
|
281
|
+
textLimit: number;
|
|
282
|
+
chunkMode?: ReturnType<typeof resolveChunkMode>;
|
|
283
|
+
replyLogger: ReturnType<typeof getChildLogger>;
|
|
284
|
+
connectionId?: string;
|
|
285
|
+
skipLog?: boolean;
|
|
286
|
+
tableMode?: ReturnType<typeof resolveMarkdownTableMode>;
|
|
287
|
+
}) => Promise<WhatsAppReplyDeliveryResult>;
|
|
288
|
+
groupHistories: Map<string, GroupHistoryEntry[]>;
|
|
289
|
+
groupHistoryKey: string;
|
|
290
|
+
maxMediaBytes: number;
|
|
291
|
+
maxMediaTextChunkLimit?: number;
|
|
292
|
+
msg: WebInboundMsg;
|
|
293
|
+
onModelSelected?: ChannelReplyOnModelSelected;
|
|
294
|
+
rememberSentText: (
|
|
295
|
+
text: string | undefined,
|
|
296
|
+
opts: {
|
|
297
|
+
combinedBody?: string;
|
|
298
|
+
combinedBodySessionKey?: string;
|
|
299
|
+
logVerboseMessage?: boolean;
|
|
300
|
+
},
|
|
301
|
+
) => void;
|
|
302
|
+
replyLogger: ReturnType<typeof getChildLogger>;
|
|
303
|
+
replyPipeline: WhatsAppDispatchPipeline;
|
|
304
|
+
replyResolver: typeof getReplyFromConfig;
|
|
305
|
+
route: ReturnType<typeof resolveAgentRoute>;
|
|
306
|
+
shouldClearGroupHistory: boolean;
|
|
307
|
+
}) {
|
|
308
|
+
const textLimit = params.maxMediaTextChunkLimit ?? resolveTextChunkLimit(params.cfg, "whatsapp");
|
|
309
|
+
const chunkMode = resolveChunkMode(params.cfg, "whatsapp", params.route.accountId);
|
|
310
|
+
const tableMode = resolveMarkdownTableMode({
|
|
311
|
+
cfg: params.cfg,
|
|
312
|
+
channel: "whatsapp",
|
|
313
|
+
accountId: params.route.accountId,
|
|
314
|
+
});
|
|
315
|
+
const mediaLocalRoots = getAgentScopedMediaLocalRoots(params.cfg, params.route.agentId);
|
|
316
|
+
const disableBlockStreaming = resolveWhatsAppDisableBlockStreaming(params.cfg);
|
|
317
|
+
let didSendReply = false;
|
|
318
|
+
let didLogHeartbeatStrip = false;
|
|
319
|
+
|
|
320
|
+
const { queuedFinal, counts } = await dispatchReplyWithBufferedBlockDispatcher({
|
|
321
|
+
ctx: params.context,
|
|
322
|
+
cfg: params.cfg,
|
|
323
|
+
replyResolver: params.replyResolver,
|
|
324
|
+
dispatcherOptions: {
|
|
325
|
+
...params.replyPipeline,
|
|
326
|
+
onHeartbeatStrip: () => {
|
|
327
|
+
if (!didLogHeartbeatStrip) {
|
|
328
|
+
didLogHeartbeatStrip = true;
|
|
329
|
+
logVerbose("Stripped stray HEARTBEAT_OK token from web reply");
|
|
330
|
+
}
|
|
331
|
+
},
|
|
332
|
+
deliver: async (payload: ReplyPayload, info: { kind: ReplyLifecycleKind }) => {
|
|
333
|
+
const deliveryPayload = resolveWhatsAppDeliverablePayload(payload, info);
|
|
334
|
+
if (!deliveryPayload) {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const normalizedOutboundPayload = normalizeWhatsAppOutboundPayload(deliveryPayload, {
|
|
338
|
+
normalizeText: normalizeWhatsAppPayloadTextPreservingIndentation,
|
|
339
|
+
});
|
|
340
|
+
const normalizedDeliveryPayload =
|
|
341
|
+
deliveryPayload.text === undefined
|
|
342
|
+
? { ...normalizedOutboundPayload, text: undefined }
|
|
343
|
+
: normalizedOutboundPayload;
|
|
344
|
+
const reply = resolveSendableOutboundReplyParts(normalizedDeliveryPayload);
|
|
345
|
+
if (!reply.hasMedia && !reply.text.trim()) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
const delivery = await params.deliverReply({
|
|
349
|
+
replyResult: normalizedDeliveryPayload,
|
|
350
|
+
normalizedReplyResult: normalizedDeliveryPayload,
|
|
351
|
+
msg: params.msg,
|
|
352
|
+
mediaLocalRoots,
|
|
353
|
+
maxMediaBytes: params.maxMediaBytes,
|
|
354
|
+
textLimit,
|
|
355
|
+
chunkMode,
|
|
356
|
+
replyLogger: params.replyLogger,
|
|
357
|
+
connectionId: params.connectionId,
|
|
358
|
+
skipLog: false,
|
|
359
|
+
tableMode,
|
|
360
|
+
});
|
|
361
|
+
if (!delivery.providerAccepted) {
|
|
362
|
+
params.replyLogger.warn(
|
|
363
|
+
{
|
|
364
|
+
correlationId: params.msg.id ?? null,
|
|
365
|
+
connectionId: params.connectionId,
|
|
366
|
+
conversationId: params.conversationId,
|
|
367
|
+
chatId: params.msg.chatId,
|
|
368
|
+
to: params.msg.from,
|
|
369
|
+
from: params.msg.to,
|
|
370
|
+
replyKind: info.kind,
|
|
371
|
+
},
|
|
372
|
+
"auto-reply was not accepted by WhatsApp provider",
|
|
373
|
+
);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
didSendReply = true;
|
|
377
|
+
const shouldLog = normalizedDeliveryPayload.text ? true : undefined;
|
|
378
|
+
params.rememberSentText(normalizedDeliveryPayload.text, {
|
|
379
|
+
combinedBody: params.context.Body as string | undefined,
|
|
380
|
+
combinedBodySessionKey: params.route.sessionKey,
|
|
381
|
+
logVerboseMessage: shouldLog,
|
|
382
|
+
});
|
|
383
|
+
const fromDisplay =
|
|
384
|
+
params.msg.chatType === "group" ? params.conversationId : (params.msg.from ?? "unknown");
|
|
385
|
+
if (shouldLogVerbose()) {
|
|
386
|
+
const preview = normalizedDeliveryPayload.text != null ? reply.text : "<media>";
|
|
387
|
+
logVerbose(`Reply body: ${preview}${reply.hasMedia ? " (media)" : ""} -> ${fromDisplay}`);
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
onReplyStart: params.msg.sendComposing,
|
|
391
|
+
onError: (err, info) => {
|
|
392
|
+
logWhatsAppReplyDeliveryError({
|
|
393
|
+
err,
|
|
394
|
+
info,
|
|
395
|
+
connectionId: params.connectionId,
|
|
396
|
+
conversationId: params.conversationId,
|
|
397
|
+
msg: params.msg,
|
|
398
|
+
replyLogger: params.replyLogger,
|
|
399
|
+
});
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
replyOptions: {
|
|
403
|
+
disableBlockStreaming,
|
|
404
|
+
onModelSelected: params.onModelSelected,
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
const didQueueVisibleReply = hasVisibleInboundReplyDispatch({ queuedFinal, counts });
|
|
409
|
+
if (!didQueueVisibleReply) {
|
|
410
|
+
if (params.shouldClearGroupHistory) {
|
|
411
|
+
params.groupHistories.set(params.groupHistoryKey, []);
|
|
412
|
+
}
|
|
413
|
+
logVerbose("Skipping auto-reply: silent token or no text/media returned from resolver");
|
|
414
|
+
return false;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (params.shouldClearGroupHistory) {
|
|
418
|
+
params.groupHistories.set(params.groupHistoryKey, []);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
return didSendReply;
|
|
422
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { trackBackgroundTask } from "./last-route.js";
|
|
3
|
+
|
|
4
|
+
const waitForAsyncCallbacks = async () => {
|
|
5
|
+
await Promise.resolve();
|
|
6
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
describe("trackBackgroundTask", () => {
|
|
10
|
+
const unhandledRejections: unknown[] = [];
|
|
11
|
+
const onUnhandledRejection = (reason: unknown) => {
|
|
12
|
+
unhandledRejections.push(reason);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
process.off("unhandledRejection", onUnhandledRejection);
|
|
17
|
+
unhandledRejections.length = 0;
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("does not leak unhandled rejections when a tracked task fails", async () => {
|
|
21
|
+
process.on("unhandledRejection", onUnhandledRejection);
|
|
22
|
+
const backgroundTasks = new Set<Promise<unknown>>();
|
|
23
|
+
let rejectTask!: (reason?: unknown) => void;
|
|
24
|
+
const task = new Promise<void>((_resolve, reject) => {
|
|
25
|
+
rejectTask = reject;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
trackBackgroundTask(backgroundTasks, task);
|
|
29
|
+
expect(backgroundTasks.size).toBe(1);
|
|
30
|
+
|
|
31
|
+
rejectTask(new Error("boom"));
|
|
32
|
+
await waitForAsyncCallbacks();
|
|
33
|
+
|
|
34
|
+
expect(backgroundTasks.size).toBe(0);
|
|
35
|
+
expect(unhandledRejections).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
|
|
2
|
+
import type { MsgContext } from "openclaw/plugin-sdk/reply-runtime";
|
|
3
|
+
import { formatError } from "../../session.js";
|
|
4
|
+
import { resolveStorePath, updateLastRoute } from "../config.runtime.js";
|
|
5
|
+
|
|
6
|
+
export function trackBackgroundTask(
|
|
7
|
+
backgroundTasks: Set<Promise<unknown>>,
|
|
8
|
+
task: Promise<unknown>,
|
|
9
|
+
) {
|
|
10
|
+
backgroundTasks.add(task);
|
|
11
|
+
const cleanup = () => {
|
|
12
|
+
backgroundTasks.delete(task);
|
|
13
|
+
};
|
|
14
|
+
task.then(cleanup, cleanup);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function updateLastRouteInBackground(params: {
|
|
18
|
+
cfg: OpenClawConfig;
|
|
19
|
+
backgroundTasks: Set<Promise<unknown>>;
|
|
20
|
+
storeAgentId: string;
|
|
21
|
+
sessionKey: string;
|
|
22
|
+
channel: "whatsapp";
|
|
23
|
+
to: string;
|
|
24
|
+
accountId?: string;
|
|
25
|
+
ctx?: MsgContext;
|
|
26
|
+
warn: (obj: unknown, msg: string) => void;
|
|
27
|
+
}) {
|
|
28
|
+
const storePath = resolveStorePath(params.cfg.session?.store, {
|
|
29
|
+
agentId: params.storeAgentId,
|
|
30
|
+
});
|
|
31
|
+
const task = updateLastRoute({
|
|
32
|
+
storePath,
|
|
33
|
+
sessionKey: params.sessionKey,
|
|
34
|
+
deliveryContext: {
|
|
35
|
+
channel: params.channel,
|
|
36
|
+
to: params.to,
|
|
37
|
+
accountId: params.accountId,
|
|
38
|
+
},
|
|
39
|
+
ctx: params.ctx,
|
|
40
|
+
}).catch((err) => {
|
|
41
|
+
params.warn(
|
|
42
|
+
{
|
|
43
|
+
error: formatError(err),
|
|
44
|
+
storePath,
|
|
45
|
+
sessionKey: params.sessionKey,
|
|
46
|
+
to: params.to,
|
|
47
|
+
},
|
|
48
|
+
"failed updating last route",
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
trackBackgroundTask(params.backgroundTasks, task);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function awaitBackgroundTasks(backgroundTasks: Set<Promise<unknown>>) {
|
|
55
|
+
if (backgroundTasks.size === 0) {
|
|
56
|
+
return Promise.resolve();
|
|
57
|
+
}
|
|
58
|
+
return Promise.allSettled(backgroundTasks).then(() => {
|
|
59
|
+
backgroundTasks.clear();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
formatInboundEnvelope,
|
|
5
|
+
type EnvelopeFormatOptions,
|
|
6
|
+
} from "openclaw/plugin-sdk/channel-envelope";
|
|
7
|
+
|
|
8
|
+
type WhatsAppMessagePrefixConfig = OpenClawConfig;
|
|
9
|
+
|
|
10
|
+
function normalizeAgentId(agentId: string): string {
|
|
11
|
+
return agentId.trim().toLowerCase() || "main";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function resolveIdentityNamePrefix(
|
|
15
|
+
cfg: WhatsAppMessagePrefixConfig,
|
|
16
|
+
agentId: string,
|
|
17
|
+
): string | undefined {
|
|
18
|
+
const normalizedAgentId = normalizeAgentId(agentId);
|
|
19
|
+
const identityName = cfg.agents?.list
|
|
20
|
+
?.find((agent) => normalizeAgentId(agent.id ?? "") === normalizedAgentId)
|
|
21
|
+
?.identity?.name?.trim();
|
|
22
|
+
return identityName ? `[${identityName}]` : undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveMessagePrefix(
|
|
26
|
+
cfg: WhatsAppMessagePrefixConfig,
|
|
27
|
+
agentId: string,
|
|
28
|
+
opts?: { configured?: string; hasAllowFrom?: boolean; fallback?: string },
|
|
29
|
+
): string {
|
|
30
|
+
const configured = opts?.configured ?? cfg.messages?.messagePrefix;
|
|
31
|
+
if (configured !== undefined) {
|
|
32
|
+
return configured;
|
|
33
|
+
}
|
|
34
|
+
if (opts?.hasAllowFrom === true) {
|
|
35
|
+
return "";
|
|
36
|
+
}
|
|
37
|
+
return resolveIdentityNamePrefix(cfg, agentId) ?? opts?.fallback ?? "[openclaw]";
|
|
38
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
|
|
2
|
+
import { getPrimaryIdentityId, getReplyContext, getSenderIdentity } from "../../identity.js";
|
|
3
|
+
import type { WebInboundMsg } from "../types.js";
|
|
4
|
+
import {
|
|
5
|
+
formatInboundEnvelope,
|
|
6
|
+
resolveMessagePrefix,
|
|
7
|
+
type EnvelopeFormatOptions,
|
|
8
|
+
} from "./message-line.runtime.js";
|
|
9
|
+
|
|
10
|
+
export function formatReplyContext(msg: WebInboundMsg) {
|
|
11
|
+
const replyTo = getReplyContext(msg);
|
|
12
|
+
if (!replyTo?.body) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const sender = replyTo.sender?.label ?? replyTo.sender?.e164 ?? "unknown sender";
|
|
16
|
+
const idPart = replyTo.id ? ` id:${replyTo.id}` : "";
|
|
17
|
+
return `[Replying to ${sender}${idPart}]\n${replyTo.body}\n[/Replying]`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function buildInboundLine(params: {
|
|
21
|
+
cfg: OpenClawConfig;
|
|
22
|
+
msg: WebInboundMsg;
|
|
23
|
+
agentId: string;
|
|
24
|
+
previousTimestamp?: number;
|
|
25
|
+
envelope?: EnvelopeFormatOptions;
|
|
26
|
+
}) {
|
|
27
|
+
const { cfg, msg, agentId, previousTimestamp, envelope } = params;
|
|
28
|
+
// WhatsApp inbound prefix: channels.whatsapp.messagePrefix > legacy messages.messagePrefix > identity/defaults
|
|
29
|
+
const messagePrefix = resolveMessagePrefix(cfg, agentId, {
|
|
30
|
+
configured: cfg.channels?.whatsapp?.messagePrefix,
|
|
31
|
+
hasAllowFrom: (cfg.channels?.whatsapp?.allowFrom?.length ?? 0) > 0,
|
|
32
|
+
});
|
|
33
|
+
const prefixStr = messagePrefix ? `${messagePrefix} ` : "";
|
|
34
|
+
const replyContext = formatReplyContext(msg);
|
|
35
|
+
const baseLine = `${prefixStr}${msg.body}${replyContext ? `\n\n${replyContext}` : ""}`;
|
|
36
|
+
const sender = getSenderIdentity(msg);
|
|
37
|
+
|
|
38
|
+
// Wrap with standardized envelope for the agent.
|
|
39
|
+
return formatInboundEnvelope({
|
|
40
|
+
channel: "WhatsApp",
|
|
41
|
+
from: msg.chatType === "group" ? msg.from : msg.from?.replace(/^whatsapp:/, ""),
|
|
42
|
+
timestamp: msg.timestamp,
|
|
43
|
+
body: baseLine,
|
|
44
|
+
chatType: msg.chatType,
|
|
45
|
+
sender: {
|
|
46
|
+
name: sender.name ?? undefined,
|
|
47
|
+
e164: sender.e164 ?? undefined,
|
|
48
|
+
id: getPrimaryIdentityId(sender) ?? undefined,
|
|
49
|
+
},
|
|
50
|
+
previousTimestamp,
|
|
51
|
+
envelope,
|
|
52
|
+
fromMe: msg.fromMe,
|
|
53
|
+
});
|
|
54
|
+
}
|