Skip to content

Commit a4b17d6

Browse files
committed
refactor: consolidate message delivery API
1 parent 195db88 commit a4b17d6

74 files changed

Lines changed: 1560 additions & 339 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Docs: https://docs.openclaw.ai
3030
- Plugins/doctor: avoid full-array sorting while selecting ClawHub search/archive results and bounded dreaming doctor entries. Thanks @shakkernerd.
3131
- Agents/compaction: keep contributor diagnostics to a bounded top-three selection without sorting the full history. Thanks @shakkernerd.
3232
- Sessions/UI: avoid full-array sorting while selecting ACPX leases, Google Meet calendar events, and latest chat sessions. Thanks @shakkernerd.
33+
- Plugin SDK: mark direct `deliverOutboundPayloads` and legacy reply-dispatch bridges as deprecated compatibility substrate, enrich `sendDurableMessageBatch` with explicit durable send outcomes, migrate bundled send/turn paths off deprecated APIs, and enforce the split with `check:deprecated-api-usage`.
3334
- Telegram: preserve the channel-specific 10-option poll cap in the unified outbound adapter so over-limit polls are rejected before send. (#78762) Thanks @obviyus.
3435
- Telegram/streaming: continue over-limit draft previews in a new message instead of stopping when rendered preview text crosses Telegram's message limit. (#74508) Thanks @anagnorisis2peripeteia.
3536
- Slack: route handled top-level channel turns in implicit-conversation channels to thread-scoped sessions when Slack reply threading is enabled, keeping the root turn and later thread replies on one OpenClaw session. (#78522) Thanks @zeroth-blip.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
9f7ea91407a66fee6bcdebaf64bd15d0a6ae8b48cf100753c96f2ad29ad86390 plugin-sdk-api-baseline.json
2-
4d516f6ac681cf55e916f712601abb8f5a7ddcd92a7710e8947f89c38e4054e7 plugin-sdk-api-baseline.jsonl
1+
779d6cd6c35fb4685cb5a31c40748ab71184b89632982920d2aeee9d703cdb3a plugin-sdk-api-baseline.json
2+
ecb5ea5dc7b4573c61070b2cbdfdcbc3436032bbe39c3045dd8d5213d18ec858 plugin-sdk-api-baseline.jsonl

‎docs/plugins/hooks.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ Decision rules:
369369
- `message_sending` with `cancel: false` is treated as no decision.
370370
- Rewritten `content` continues to lower-priority hooks unless a later hook
371371
cancels delivery.
372+
- `message_sending` can return `cancelReason` and bounded `metadata` with a
373+
cancellation. New message lifecycle APIs expose this as a suppressed delivery
374+
outcome with reason `cancelled_by_message_sending_hook`; legacy direct
375+
delivery keeps returning an empty result array for compatibility.
376+
- `message_sent` is observation-only. Handler failures are logged and do not
377+
change the delivery result.
372378

373379
## Install hooks
374380

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

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ Runtime delivery helpers are available from
3131
`openclaw/plugin-sdk/channel-message-runtime` for monitor/send code paths that
3232
are already doing asynchronous message I/O.
3333

34+
New channel and plugin send code should use the message lifecycle helpers from
35+
`openclaw/plugin-sdk/channel-message-runtime`: `sendDurableMessageBatch`,
36+
`withDurableMessageSendContext`, or `deliverInboundReplyWithMessageSendContext`.
37+
The older
38+
`deliverOutboundPayloads(...)` helper in `openclaw/plugin-sdk/outbound-runtime`
39+
is deprecated compatibility/runtime substrate for outbound internals, recovery,
40+
and legacy adapters. Do not use it for new channel or plugin send paths.
41+
42+
`sendDurableMessageBatch(...)` returns an explicit lifecycle outcome:
43+
44+
- `sent` - at least one visible platform message was delivered.
45+
- `suppressed` - no platform message should be treated as missing. Stable
46+
reasons include `cancelled_by_message_sending_hook`,
47+
`empty_after_message_sending_hook`, `no_visible_payload`,
48+
`adapter_returned_no_identity`, and legacy `no_visible_result`.
49+
- `partial_failed` - at least one platform message was delivered before a later
50+
payload or side effect failed. The result includes the delivered receipt prefix
51+
plus the failure.
52+
- `failed` - no platform receipt was produced.
53+
54+
Use `payloadOutcomes` when a batch mixes sent, suppressed, and failed payloads.
55+
Do not infer hook cancellation by checking whether the old direct-delivery array
56+
is empty.
57+
58+
Compatibility dispatchers that still need the buffered reply dispatcher should
59+
build reply-prefix options with `createChannelMessageReplyPipeline(...)` from
60+
`openclaw/plugin-sdk/channel-message`, then call the runtime's
61+
`channel.turn.runPrepared(...)`. That keeps session recording and dispatch
62+
ordering on the shared turn lifecycle without adding another public turn wrapper.
63+
3464
## Minimal adapter
3565

3666
Most new channel plugins can start with a small adapter:
@@ -124,8 +154,10 @@ tool send, implement `actions.prepareSendPayload(...)` instead of sending from
124154
`prepareSendPayload(...)` receives the normalized core `ReplyPayload` plus the
125155
full action context. Return a payload with channel-specific data in
126156
`payload.channelData.<channel>` and let core call `sendMessage(...)`,
127-
`deliverOutboundPayloads(...)`, the write-ahead queue, message-sending hooks,
128-
retry, recovery, and ack cleanup.
157+
the message lifecycle runtime, the write-ahead queue, message-sending hooks,
158+
retry, recovery, and ack cleanup. The lifecycle runtime may call
159+
`deliverOutboundPayloads(...)` internally as compatibility substrate, but channel
160+
plugins should not call it directly for new send behavior.
129161

130162
Return `null` only when the send cannot be represented as a durable payload, for
131163
example because it contains a non-serializable component factory. Core will keep
@@ -390,17 +422,21 @@ surface.
390422
These APIs remain importable for third-party compatibility. Do not use them for
391423
new channel code.
392424

393-
| Deprecated API | Replacement |
394-
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
395-
| `openclaw/plugin-sdk/channel-reply-pipeline` | `openclaw/plugin-sdk/channel-message` |
396-
| `createChannelTurnReplyPipeline(...)` | `createChannelMessageReplyPipeline(...)` for compatibility dispatchers, or a `message` adapter for new channel code |
397-
| `deliverDurableInboundReplyPayload(...)` | `deliverInboundReplyWithMessageSendContext(...)` from `openclaw/plugin-sdk/channel-message-runtime` |
398-
| `dispatchInboundReplyWithBase(...)` | `dispatchChannelMessageReplyWithBase(...)` only for compatibility dispatchers |
399-
| `recordInboundSessionAndDispatchReply(...)` | `recordChannelMessageReplyDispatch(...)` only for compatibility dispatchers |
400-
| `resolveChannelSourceReplyDeliveryMode(...)` | `resolveChannelMessageSourceReplyDeliveryMode(...)` |
401-
| `deliverFinalizableDraftPreview(...)` | `defineFinalizableLivePreviewAdapter(...)` plus `deliverWithFinalizableLivePreviewAdapter(...)` |
402-
| `DraftPreviewFinalizerDraft` | `LivePreviewFinalizerDraft` |
403-
| `DraftPreviewFinalizerResult` | `LivePreviewFinalizerResult` |
425+
| Deprecated API | Replacement |
426+
| -------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
427+
| `openclaw/plugin-sdk/channel-reply-pipeline` | `openclaw/plugin-sdk/channel-message` |
428+
| `createChannelTurnReplyPipeline(...)` | `createChannelMessageReplyPipeline(...)` for compatibility dispatchers, or a `message` adapter for new channel code |
429+
| `buildChannelMessageReplyDispatchBase(...)` | `createChannelMessageReplyPipeline(...)` plus `channel.turn.runPrepared(...)`, or a `message` adapter for new channel code |
430+
| `dispatchChannelMessageReplyWithBase(...)` | `createChannelMessageReplyPipeline(...)` plus `channel.turn.runPrepared(...)`, or a `message` adapter for new channel code |
431+
| `recordChannelMessageReplyDispatch(...)` | `createChannelMessageReplyPipeline(...)` plus `channel.turn.runPrepared(...)`, or a `message` adapter for new channel code |
432+
| `deliverOutboundPayloads(...)` | `sendDurableMessageBatch(...)` or `deliverInboundReplyWithMessageSendContext(...)` from `channel-message-runtime` |
433+
| `deliverDurableInboundReplyPayload(...)` | `deliverInboundReplyWithMessageSendContext(...)` from `openclaw/plugin-sdk/channel-message-runtime` |
434+
| `dispatchInboundReplyWithBase(...)` | `createChannelMessageReplyPipeline(...)` plus `channel.turn.runPrepared(...)`, or a `message` adapter for new channel code |
435+
| `recordInboundSessionAndDispatchReply(...)` | `createChannelMessageReplyPipeline(...)` plus `channel.turn.runPrepared(...)`, or a `message` adapter for new channel code |
436+
| `resolveChannelSourceReplyDeliveryMode(...)` | `resolveChannelMessageSourceReplyDeliveryMode(...)` |
437+
| `deliverFinalizableDraftPreview(...)` | `defineFinalizableLivePreviewAdapter(...)` plus `deliverWithFinalizableLivePreviewAdapter(...)` |
438+
| `DraftPreviewFinalizerDraft` | `LivePreviewFinalizerDraft` |
439+
| `DraftPreviewFinalizerResult` | `LivePreviewFinalizerResult` |
404440

405441
Compatibility dispatchers can still use `createReplyPrefixContext(...)`,
406442
`createReplyPrefixOptions(...)`, and `createTypingCallbacks(...)` through the

‎docs/plugins/sdk-migration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ releases.
250250
helpers for external plugins during the migration window and warn once with
251251
the `runtime-config-load-write` compatibility code. Bundled plugins and repo
252252
runtime code are protected by scanner guardrails in
253-
`pnpm check:deprecated-internal-config-api` and
253+
`pnpm check:deprecated-api-usage` and
254254
`pnpm check:no-runtime-action-load-config`: new production plugin usage
255255
fails outright, direct config writes fail, gateway server methods must use
256256
the request runtime snapshot, runtime channel send/action/client helpers

‎docs/plugins/sdk-subpaths.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ For the plugin authoring guide, see [Plugin SDK overview](/plugins/sdk-overview)
6666
| `plugin-sdk/command-gating` | Narrow command authorization gate helpers |
6767
| `plugin-sdk/channel-policy` | `resolveChannelGroupRequireMention` |
6868
| `plugin-sdk/channel-lifecycle` | `createAccountStatusSink`, `createChannelRunQueue`, and legacy draft stream lifecycle helpers. New preview finalization code should use `plugin-sdk/channel-message`. |
69-
| `plugin-sdk/channel-message` | Cheap message lifecycle contract helpers such as `defineChannelMessageAdapter`, `createChannelMessageAdapterFromOutbound`, `createReplyPrefixContext`, `resolveChannelMessageSourceReplyDeliveryMode`, compatibility facades, durable-final capability derivation, capability proof helpers for send/receipt/side-effect capabilities, `MessageReceiveContext`, receive ack policy proofs, `defineFinalizableLivePreviewAdapter`, `deliverWithFinalizableLivePreviewAdapter`, live-preview and live-finalizer capability proofs, durable recovery state, `RenderedMessageBatch`, message receipt types, and receipt id helpers. See [Channel message API](/plugins/sdk-channel-message). Legacy `createChannelTurnReplyPipeline` remains only for compatibility dispatchers. |
70-
| `plugin-sdk/channel-message-runtime` | Runtime delivery helpers that may load outbound delivery, including `deliverInboundReplyWithMessageSendContext`, `sendDurableMessageBatch`, `withDurableMessageSendContext`, `dispatchChannelMessageReplyWithBase`, and `recordChannelMessageReplyDispatch`. Use from monitor/send runtime modules, not hot plugin bootstrap files. |
69+
| `plugin-sdk/channel-message` | Cheap message lifecycle contract helpers such as `defineChannelMessageAdapter`, `createChannelMessageAdapterFromOutbound`, `createChannelMessageReplyPipeline`, `createReplyPrefixContext`, `resolveChannelMessageSourceReplyDeliveryMode`, durable-final capability derivation, capability proof helpers for send/receipt/side-effect capabilities, `MessageReceiveContext`, receive ack policy proofs, `defineFinalizableLivePreviewAdapter`, `deliverWithFinalizableLivePreviewAdapter`, live-preview and live-finalizer capability proofs, durable recovery state, `RenderedMessageBatch`, message receipt types, and receipt id helpers. See [Channel message API](/plugins/sdk-channel-message). Legacy reply-dispatch facades are deprecated compatibility only. |
70+
| `plugin-sdk/channel-message-runtime` | Runtime delivery helpers that may load outbound delivery, including `deliverInboundReplyWithMessageSendContext`, `sendDurableMessageBatch`, and `withDurableMessageSendContext`. Deprecated reply-dispatch bridges remain importable for compatibility dispatchers only. Use from monitor/send runtime modules, not hot plugin bootstrap files. |
7171
| `plugin-sdk/inbound-envelope` | Shared inbound route + envelope builder helpers |
7272
| `plugin-sdk/inbound-reply-dispatch` | Legacy shared inbound record-and-dispatch helpers, visible/final dispatch predicates, and deprecated `deliverDurableInboundReplyPayload` compatibility for prepared channel dispatchers. New channel receive/dispatch code should import runtime lifecycle helpers from `plugin-sdk/channel-message-runtime`. |
7373
| `plugin-sdk/messaging-targets` | Target parsing/matching helpers |
7474
| `plugin-sdk/outbound-media` | Shared outbound media loading helpers |
7575
| `plugin-sdk/outbound-send-deps` | Lightweight outbound send dependency lookup for channel adapters |
76-
| `plugin-sdk/outbound-runtime` | Outbound delivery, identity, send delegate, session, formatting, and payload planning helpers |
76+
| `plugin-sdk/outbound-runtime` | Outbound identity, send delegate, session, formatting, and payload planning helpers. Direct delivery helpers such as `deliverOutboundPayloads` are deprecated compatibility substrate; use `plugin-sdk/channel-message-runtime` for new send paths. |
7777
| `plugin-sdk/poll-runtime` | Narrow poll normalization helpers |
7878
| `plugin-sdk/thread-bindings-runtime` | Thread-binding lifecycle and adapter helpers |
7979
| `plugin-sdk/agent-media-payload` | Legacy agent media payload builder |

‎extensions/discord/src/monitor/reply-delivery.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ vi.mock("openclaw/plugin-sdk/outbound-runtime", async () => {
1919
};
2020
});
2121

22+
vi.mock("../../../../src/infra/outbound/deliver.js", async () => {
23+
const actual = await vi.importActual<typeof import("../../../../src/infra/outbound/deliver.js")>(
24+
"../../../../src/infra/outbound/deliver.js",
25+
);
26+
return {
27+
...actual,
28+
deliverOutboundPayloads: deliverOutboundPayloadsMock,
29+
deliverOutboundPayloadsInternal: deliverOutboundPayloadsMock,
30+
};
31+
});
32+
2233
vi.mock("../send.js", async () => {
2334
const actual = await vi.importActual<typeof import("../send.js")>("../send.js");
2435
return {

‎extensions/discord/src/monitor/reply-delivery.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { resolveAgentAvatar } from "openclaw/plugin-sdk/agent-runtime";
2+
import { sendDurableMessageBatch } from "openclaw/plugin-sdk/channel-message";
23
import type {
34
MarkdownTableMode,
45
OpenClawConfig,
@@ -7,7 +8,6 @@ import type {
78
import type { OutboundMediaAccess } from "openclaw/plugin-sdk/media-runtime";
89
import {
910
buildOutboundSessionContext,
10-
deliverOutboundPayloads,
1111
type OutboundDeliveryFormattingOptions,
1212
type OutboundIdentity,
1313
type OutboundSendDeps,
@@ -181,7 +181,7 @@ export async function deliverDiscordReply(params: {
181181
return;
182182
}
183183

184-
const results = await deliverOutboundPayloads({
184+
const send = await sendDurableMessageBatch({
185185
cfg: params.cfg,
186186
channel: "discord",
187187
to: delivery.to,
@@ -205,6 +205,10 @@ export async function deliverDiscordReply(params: {
205205
requesterAccountId: params.accountId,
206206
}),
207207
});
208+
if (send.status === "failed" || send.status === "partial_failed") {
209+
throw send.error;
210+
}
211+
const results = send.status === "sent" ? send.results : [];
208212
if (results.length === 0) {
209213
throw new Error(`discord final reply produced no delivered message for ${delivery.to}`);
210214
}

‎extensions/irc/src/inbound.ts‎

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createChannelMessageReplyPipeline } from "openclaw/plugin-sdk/channel-message";
12
import { createChannelPairingController } from "openclaw/plugin-sdk/channel-pairing";
23
import {
34
readStoreAllowFromForDmPolicy,
@@ -333,38 +334,53 @@ export async function handleIrcInbound(params: {
333334
CommandAuthorized: commandAuthorized,
334335
});
335336

336-
const { dispatchChannelMessageReplyWithBase } =
337-
await import("openclaw/plugin-sdk/channel-message");
338-
await dispatchChannelMessageReplyWithBase({
337+
const { onModelSelected, ...replyPipeline } = createChannelMessageReplyPipeline({
339338
cfg: config as OpenClawConfig,
339+
agentId: route.agentId,
340+
channel: CHANNEL_ID,
341+
accountId: account.accountId,
342+
});
343+
344+
await core.channel.turn.runPrepared({
340345
channel: CHANNEL_ID,
341346
accountId: account.accountId,
342-
route,
347+
routeSessionKey: route.sessionKey,
343348
storePath,
344349
ctxPayload,
345-
core,
346-
deliver: async (payload) => {
347-
await deliverIrcReply({
348-
payload,
349-
cfg: config,
350-
target: peerId,
351-
accountId: account.accountId,
352-
sendReply: params.sendReply,
353-
statusSink,
354-
});
355-
},
356-
onRecordError: (err) => {
357-
runtime.error?.(`irc: failed updating session meta: ${String(err)}`);
358-
},
359-
onDispatchError: (err, info) => {
360-
runtime.error?.(`irc ${info.kind} reply failed: ${String(err)}`);
361-
},
362-
replyOptions: {
363-
skillFilter: groupMatch.groupConfig?.skills,
364-
disableBlockStreaming:
365-
typeof account.config.blockStreaming === "boolean"
366-
? !account.config.blockStreaming
367-
: undefined,
350+
recordInboundSession: core.channel.session.recordInboundSession,
351+
runDispatch: async () =>
352+
await core.channel.reply.dispatchReplyWithBufferedBlockDispatcher({
353+
ctx: ctxPayload,
354+
cfg: config as OpenClawConfig,
355+
dispatcherOptions: {
356+
...replyPipeline,
357+
deliver: async (payload) => {
358+
await deliverIrcReply({
359+
payload,
360+
cfg: config,
361+
target: peerId,
362+
accountId: account.accountId,
363+
sendReply: params.sendReply,
364+
statusSink,
365+
});
366+
},
367+
onError: (err, info) => {
368+
runtime.error?.(`irc ${info.kind} reply failed: ${String(err)}`);
369+
},
370+
},
371+
replyOptions: {
372+
onModelSelected,
373+
skillFilter: groupMatch.groupConfig?.skills,
374+
disableBlockStreaming:
375+
typeof account.config.blockStreaming === "boolean"
376+
? !account.config.blockStreaming
377+
: undefined,
378+
},
379+
}),
380+
record: {
381+
onRecordError: (err) => {
382+
runtime.error?.(`irc: failed updating session meta: ${String(err)}`);
383+
},
368384
},
369385
});
370386
}

‎extensions/irc/src/runtime-api.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export {
2929
resolveEffectiveAllowFromLists,
3030
} from "openclaw/plugin-sdk/channel-policy";
3131
export { resolveControlCommandGate } from "openclaw/plugin-sdk/command-auth";
32-
export { dispatchChannelMessageReplyWithBase } from "openclaw/plugin-sdk/channel-message";
32+
export { createChannelMessageReplyPipeline } from "openclaw/plugin-sdk/channel-message";
3333
export { chunkTextForOutbound } from "openclaw/plugin-sdk/text-chunking";
3434
export {
3535
deliverFormattedTextWithAttachments,

0 commit comments

Comments
 (0)