Skip to content

Commit 21db50e

Browse files
committed
refactor(whatsapp): keep inbound boundary transport-private
1 parent 5c296b5 commit 21db50e

18 files changed

Lines changed: 220 additions & 314 deletions

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ c0f910ebfa3dbf283145fb1e3b9c016d03e853ef13e70402b09f3b9d9c2f4ab0 module/channel
2929
9a5aaf650f9242523bb57bdc2556c323ab64e55aa11e25e2685b73b23ee12534 module/channel-dm-policy
3030
fbf353eb38ae68d8ded3f2a60b432c7bb2c245d2ec7e7c9f53c6da19a0db0938 module/channel-entry-contract
3131
982f29a18e07228e3da82cae67d06ff38249592a29c2fd28f01f0d2016ff80d9 module/channel-feedback
32-
6cf2da836db49a267aed470ce6f5f55e68d8d86e1efe980cdc71349cfce30bc2 module/channel-inbound
32+
d645d24bcb7a5f68cc46c692ad0d1fbd19be0a99996f31e9479ce9cffce301c1 module/channel-inbound
3333
76bb7f531f3702c801e8fe7479e9e499f601fb361a4303afdcb45fc0da440e4b module/channel-inbound-debounce
3434
4a7ada095f0f483525dcbd848fbccab26473749eba87e6a6c6e5074fd04ee1d1 module/channel-ingress-runtime
3535
c97dd36cdf8f83c2893c33e9430a93cd131a03d855725783ca5b545de0cf84f8 module/channel-lifecycle

‎docs/plugins/sdk-channel-inbound.md‎

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -38,78 +38,6 @@ import {
3838
- `dispatchChannelInboundReply(...)`: records and dispatches an already
3939
assembled inbound reply with a delivery adapter.
4040

41-
## Prepared inbound envelope
42-
43-
Channels that finish transport admission and normalization before entering the
44-
shared runner can use `PreparedChannelInbound` as the boundary:
45-
46-
```ts
47-
import {
48-
projectPreparedChannelInbound,
49-
resolveChannelInboundReplyPolicy,
50-
runChannelInboundEvent,
51-
type PreparedChannelInbound,
52-
} from "openclaw/plugin-sdk/channel-inbound";
53-
54-
const inbound = {
55-
channel: "demo",
56-
event: { id: nativeEvent.id, timestamp: nativeEvent.timestamp },
57-
from: conversation.id,
58-
sender: { id: sender.id, name: sender.name },
59-
conversation: { kind: "group", id: conversation.id },
60-
route: { agentId, accountId, routeSessionKey },
61-
reply: { to: conversation.id, replyToId: quote?.id },
62-
message: {
63-
rawBody,
64-
body: formattedBody,
65-
bodyForAgent,
66-
commandBody,
67-
},
68-
command: {
69-
kind: "text-slash",
70-
body: commandBody,
71-
authorization: { kind: "authorized" },
72-
},
73-
media,
74-
} satisfies PreparedChannelInbound;
75-
76-
const prepared = projectPreparedChannelInbound({
77-
inbound,
78-
control: { messageReceivedHooks: "core" },
79-
});
80-
const replyPolicy = resolveChannelInboundReplyPolicy({
81-
cfg,
82-
ctx: prepared.context,
83-
blockStreamingEnabled,
84-
});
85-
86-
await runChannelInboundEvent({
87-
channel: inbound.channel,
88-
accountId,
89-
raw: inbound,
90-
adapter: {
91-
ingest: () => prepared.input,
92-
resolveTurn: () => buildTurnPlan(prepared.context, replyPolicy),
93-
},
94-
});
95-
```
96-
97-
The envelope contains prepared product facts only. Keep native callbacks,
98-
socket objects, provider message records, and transport lifecycle symbols in a
99-
channel-private context beside it. Represent command authorization explicitly
100-
as `not_checked`, `authorized`, or `denied`; do not make core infer it from a
101-
transport payload.
102-
103-
Use `resolveChannelInboundReplyPolicy(...)` after building the portable inbound
104-
context. Core resolves product policy for source reply delivery, block
105-
streaming, and typing suppression. The channel supplies prepared context facts,
106-
including mention state, plus native capabilities such as whether block
107-
streaming is enabled.
108-
109-
Keep quote encoding, reply callbacks, native target lookup, and other
110-
transport mechanics in the channel. Do not duplicate visible-reply policy in a
111-
channel adapter.
112-
11341
For media-only inbound events, keep the message body and command text empty and
11442
pass one `ChannelInboundMediaInput` fact per native attachment. When an ambient
11543
history line or another text-only carrier must describe those facts, use

‎extensions/whatsapp/src/auto-reply/monitor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ import {
2727
type ManagedWhatsAppListener,
2828
} from "../connection-controller.js";
2929
import { resolveWhatsAppInboundPolicy } from "../inbound-policy.js";
30-
import { normalizeAdmittedWebInboundMessage } from "../inbound/message-aliases.js";
3130
import {
3231
readWhatsAppBaileysCacheEntry,
3332
type WhatsAppBaileysGroupMetadataCache,
3433
type WhatsAppBaileysMessageCache,
3534
} from "../inbound/baileys-cache.js";
3635
import type { WhatsAppGroupMetadataCache } from "../inbound/group-metadata-cache.js";
36+
import { normalizeAdmittedWebInboundMessage } from "../inbound/message-aliases.js";
3737
import { attachWebInboxToSocket } from "../inbound/monitor.js";
3838
import type { WebInboundMessageInput } from "../inbound/types.js";
3939
import {

‎extensions/whatsapp/src/auto-reply/monitor/inbound-dispatch.message-sent.test.ts‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import {
1717
import { clearInternalHooks, registerInternalHook } from "openclaw/plugin-sdk/hook-runtime";
1818
import { afterEach, describe, expect, it, vi } from "vitest";
1919
import { createTestWebInboundMessage } from "../../inbound/test-message.test-helper.js";
20-
import { createWhatsAppReplyPlan } from "./inbound-dispatch.js";
20+
import {
21+
buildWhatsAppInboundTransportContext,
22+
createWhatsAppReplyPlan,
23+
} from "./inbound-dispatch.js";
2124

2225
const sessionKey = "agent:main:whatsapp:direct:+1000";
2326
type InternalHookEvent = Parameters<Parameters<typeof registerInternalHook>[1]>[0];
@@ -68,7 +71,25 @@ function createPlan(deliverReply: Parameters<typeof createWhatsAppReplyPlan>[0][
6871
groupHistories: new Map(),
6972
groupHistoryKey: sessionKey,
7073
maxMediaBytes: 1024,
71-
msg,
74+
inbound: {
75+
channel: "whatsapp",
76+
event: { id: "wa-inbound-1" },
77+
from: "+1000",
78+
sender: { id: "[email protected]" },
79+
conversation: { kind: "direct", id: "+1000" },
80+
route: {
81+
agentId: "main",
82+
accountId: "default",
83+
routeSessionKey: sessionKey,
84+
},
85+
reply: { to: "+2000", originatingTo: "+1000" },
86+
message: {
87+
body: "show me the result",
88+
bodyForAgent: "show me the result",
89+
rawBody: "show me the result",
90+
commandBody: "show me the result",
91+
},
92+
},
7293
rememberSentText: vi.fn(),
7394
replyLogger: {
7495
info: vi.fn(),
@@ -88,6 +109,7 @@ function createPlan(deliverReply: Parameters<typeof createWhatsAppReplyPlan>[0][
88109
matchedBy: "default",
89110
},
90111
shouldClearGroupHistory: false,
112+
transport: buildWhatsAppInboundTransportContext(msg),
91113
});
92114
return { context, plan };
93115
}

‎extensions/whatsapp/src/auto-reply/monitor/inbound-dispatch.test.ts‎

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,7 @@ vi.mock("openclaw/plugin-sdk/channel-outbound", async (importOriginal) => {
5151
return {
5252
...actual,
5353
deliverInboundReplyWithMessageSendContext: deliverInboundReplyWithMessageSendContextMock,
54-
};
55-
});
56-
57-
vi.mock("openclaw/plugin-sdk/channel-inbound", async (importOriginal) => {
58-
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/channel-inbound")>();
59-
return {
60-
...actual,
61-
resolveChannelInboundReplyPolicy: (params: {
54+
resolveChannelMessageSourceReplyDeliveryMode: (params: {
6255
cfg: {
6356
messages?: {
6457
visibleReplies?: "automatic" | "message_tool";
@@ -69,34 +62,16 @@ vi.mock("openclaw/plugin-sdk/channel-inbound", async (importOriginal) => {
6962
ChatType?: string;
7063
CommandSource?: "native" | "text";
7164
CommandAuthorized?: boolean;
72-
WasMentioned?: boolean;
7365
};
74-
blockStreamingEnabled?: boolean;
7566
}) => {
7667
sourceReplyDeliveryModeContexts.push(params.ctx);
77-
const isRoom = params.ctx.ChatType === "group" || params.ctx.ChatType === "channel";
78-
const sourceReplyDeliveryMode = !isRoom
79-
? undefined
80-
: params.ctx.CommandSource === "native" ||
81-
(params.ctx.CommandSource === "text" && params.ctx.CommandAuthorized === true)
68+
return params.ctx.CommandSource === "native" ||
69+
(params.ctx.CommandSource === "text" && params.ctx.CommandAuthorized === true)
70+
? "automatic"
71+
: (params.cfg.messages?.groupChat?.visibleReplies ??
72+
params.cfg.messages?.visibleReplies) === "automatic"
8273
? "automatic"
83-
: (params.cfg.messages?.groupChat?.visibleReplies ??
84-
params.cfg.messages?.visibleReplies) === "automatic"
85-
? "automatic"
86-
: "message_tool_only";
87-
const sourceRepliesAreToolOnly = sourceReplyDeliveryMode === "message_tool_only";
88-
return {
89-
sourceReplyDeliveryMode,
90-
disableBlockStreaming: sourceRepliesAreToolOnly
91-
? true
92-
: typeof params.blockStreamingEnabled === "boolean"
93-
? !params.blockStreamingEnabled
94-
: undefined,
95-
suppressTyping:
96-
sourceRepliesAreToolOnly &&
97-
params.ctx.ChatType === "group" &&
98-
params.ctx.WasMentioned !== true,
99-
};
74+
: "message_tool_only";
10075
},
10176
};
10277
});

‎extensions/whatsapp/src/auto-reply/monitor/inbound-dispatch.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
// Whatsapp plugin module implements inbound dispatch behavior.
22
import type { StatusReactionController } from "openclaw/plugin-sdk/channel-feedback";
33
import {
4-
buildChannelInboundEventContext,
54
createChannelPartialDeliveryError,
65
isChannelPartialDeliveryError,
7-
projectPreparedChannelInbound,
8-
resolveChannelInboundReplyPolicy,
96
type ChannelInboundTurnPlan,
10-
type PreparedChannelInbound,
117
toInboundMediaFactsWithMetadata,
128
} from "openclaw/plugin-sdk/channel-inbound";
139
import { hasVisibleInboundReplyDispatch } from "openclaw/plugin-sdk/channel-inbound";
1410
import {
15-
bindIngressLifecycleToReplyOptions,
1611
listMessageReceiptPlatformIds,
1712
resolveChannelStreamingBlockEnabled,
1813
} from "openclaw/plugin-sdk/channel-outbound";
@@ -54,6 +49,11 @@ import {
5449
type ReplyPayload,
5550
type resolveAgentRoute,
5651
} from "./inbound-dispatch.runtime.js";
52+
import {
53+
projectPreparedChannelInbound,
54+
resolveWhatsAppInboundReplyPolicy,
55+
type PreparedChannelInbound,
56+
} from "./prepared-inbound.js";
5757

5858
type ReplyLifecycleKind = "tool" | "block" | "final";
5959
type ChannelReplyOnModelSelected = NonNullable<
@@ -641,7 +641,7 @@ export function createWhatsAppReplyPlan(params: {
641641
accountId: params.route.accountId,
642642
});
643643
const mediaLocalRoots = getAgentScopedMediaLocalRoots(params.cfg, params.route.agentId);
644-
const replyPolicy = resolveChannelInboundReplyPolicy({
644+
const replyPolicy = resolveWhatsAppInboundReplyPolicy({
645645
cfg: params.cfg,
646646
ctx: params.context,
647647
blockStreamingEnabled: resolveChannelStreamingBlockEnabled(params.cfg.channels?.whatsapp),

‎extensions/whatsapp/src/auto-reply/monitor/on-message.audio-preflight.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ vi.mock("openclaw/plugin-sdk/routing", () => ({
9595
}),
9696
}));
9797

98-
import { requireAdmittedWhatsAppInboundMessage } from "../../inbound/admission.js";
99-
import { normalizeWebInboundMessage } from "../../inbound/message-aliases.js";
98+
import { normalizeAdmittedWebInboundMessage } from "../../inbound/message-aliases.js";
10099
import {
101100
createTestLegacyFlatWebInboundMessage,
102101
createTestWebAudioInboundMessage,
@@ -105,7 +104,7 @@ import type { AdmittedWebInboundMessage, WebInboundMessageInput } from "../../in
105104
import { createWebOnMessageHandler } from "./on-message.js";
106105

107106
function admitTestMessage(msg: WebInboundMessageInput): AdmittedWebInboundMessage {
108-
return requireAdmittedWhatsAppInboundMessage(normalizeWebInboundMessage(msg));
107+
return normalizeAdmittedWebInboundMessage(msg);
109108
}
110109

111110
function makeAudioMsg(): AdmittedWebInboundMessage {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { describe, expect, it } from "vitest";
2+
import { projectPreparedChannelInbound, type PreparedChannelInbound } from "./prepared-inbound.js";
3+
4+
describe("WhatsApp prepared inbound", () => {
5+
it("projects portable facts without WhatsApp transport state", () => {
6+
const inbound = {
7+
channel: "whatsapp",
8+
accountId: "work",
9+
event: {
10+
id: "event-1",
11+
fullId: "whatsapp:event-1",
12+
timestamp: 1_710_000_000,
13+
},
14+
from: "whatsapp:user:u1",
15+
sender: {
16+
id: "u1",
17+
name: "Alice",
18+
},
19+
conversation: {
20+
kind: "group",
21+
id: "room-1",
22+
label: "Example Room",
23+
},
24+
route: {
25+
agentId: "main",
26+
accountId: "work",
27+
routeSessionKey: "agent:main:whatsapp:group:room-1",
28+
},
29+
reply: {
30+
to: "whatsapp:room:room-1",
31+
replyToId: "quoted-1",
32+
},
33+
message: {
34+
body: "agent body",
35+
bodyForAgent: "agent body",
36+
rawBody: "raw body",
37+
commandBody: "/status",
38+
},
39+
command: {
40+
kind: "text-slash",
41+
body: "/status",
42+
authorization: {
43+
kind: "denied",
44+
reason: "sender_not_allowed",
45+
},
46+
},
47+
media: [
48+
{
49+
path: "/tmp/example.jpg",
50+
contentType: "image/jpeg",
51+
kind: "image",
52+
},
53+
],
54+
context: {
55+
senderE164: "+15550001111",
56+
},
57+
} satisfies PreparedChannelInbound;
58+
59+
const projected = projectPreparedChannelInbound({
60+
inbound,
61+
control: { messageReceivedHooks: "core" },
62+
});
63+
64+
expect(projected.input).toEqual({
65+
id: "event-1",
66+
timestamp: 1_710_000_000,
67+
rawText: "raw body",
68+
textForAgent: "agent body",
69+
textForCommands: "/status",
70+
raw: inbound,
71+
});
72+
expect(projected.context).toMatchObject({
73+
MessageSid: "event-1",
74+
MessageSidFull: "whatsapp:event-1",
75+
BodyForAgent: "agent body",
76+
RawBody: "raw body",
77+
CommandBody: "/status",
78+
ReplyToId: "quoted-1",
79+
CommandAuthorized: false,
80+
ConversationLabel: "Example Room",
81+
GroupSubject: "Example Room",
82+
SenderE164: "+15550001111",
83+
media: [
84+
{
85+
path: "/tmp/example.jpg",
86+
contentType: "image/jpeg",
87+
kind: "image",
88+
},
89+
],
90+
});
91+
});
92+
});

0 commit comments

Comments
 (0)