Skip to content

Commit 5e640b9

Browse files
committed
fix(discord): preserve outbound reply threading
1 parent 6d27176 commit 5e640b9

30 files changed

Lines changed: 1894 additions & 1609 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Docs: https://docs.openclaw.ai
9898
- Providers/Google: map `/think adaptive` to Gemini dynamic thinking instead of a fixed medium/high budget, using Gemini 3's provider default and Gemini 2.5's `thinkingBudget: -1`. Fixes #71316. Thanks @steipete.
9999
- Providers/MiniMax: keep M2.7 chat model metadata text-only so image tool requests route through `MiniMax-VL-01` instead of the Anthropic-compatible chat endpoint. Fixes #71296. Thanks @ilker-cevikkaya.
100100
- Discord/replies: run `message_sending` plugin hooks for Discord reply delivery, including DM targets, so plugins can transform or cancel outbound Discord replies consistently with other channels. Fixes #59350. (#71094) Thanks @wei840222.
101+
- Discord/replies: preserve single-use native reply semantics across shared payload fallback, component, voice, and queued delivery paths, so explicit reply tags no longer consume implicit reply slots and chunked fallback sends reply only once. Thanks @steipete.
101102
- Control UI/commands: carry provider-owned thinking option ids/labels in session rows and defaults so fresh sessions show and accept dynamic modes such as `adaptive`, `xhigh`, and `max`. Fixes #71269. Thanks @Young-Khalil.
102103
- Image generation: make explicit `model=` overrides exact-only so failed `openai/gpt-image-2` requests no longer fall through to Gemini or other configured providers, and update `image_generate list` to mention OpenAI Codex OAuth as valid auth for `openai/gpt-image-2`. Fixes #71290 and #71231. Thanks @Young-Khalil and @steipete.
103104
- Providers/GitHub Copilot: keep the plugin stream wrapper from claiming transport selection before OpenClaw picks a boundary-aware stream path, avoiding Pi's stale fallback Copilot headers on normal model turns. Thanks @steipete.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,14 @@ should use `resolveInboundMentionDecision({ facts, policy })`.
466466

467467
You can also pass raw adapter objects instead of the declarative options
468468
if you need full control.
469+
470+
Raw outbound adapters may define a `chunker(text, limit, ctx)` function.
471+
The optional `ctx.formatting` carries delivery-time formatting decisions
472+
such as `maxLinesPerMessage`; apply it before sending so reply threading
473+
and chunk boundaries are resolved once by shared outbound delivery.
474+
Send contexts also include `replyToIdSource` (`implicit` or `explicit`)
475+
when a native reply target was resolved, so payload helpers can preserve
476+
explicit reply tags without consuming an implicit single-use reply slot.
469477
</Accordion>
470478

471479
</Step>

‎docs/plugins/sdk-migration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ releases.
262262
| `plugin-sdk/inbound-reply-dispatch` | Inbound reply helpers | Shared record-and-dispatch helpers |
263263
| `plugin-sdk/messaging-targets` | Messaging target parsing | Target parsing/matching helpers |
264264
| `plugin-sdk/outbound-media` | Outbound media helpers | Shared outbound media loading |
265-
| `plugin-sdk/outbound-runtime` | Outbound runtime helpers | Outbound identity/send delegate and payload planning helpers |
265+
| `plugin-sdk/outbound-runtime` | Outbound runtime helpers | Outbound delivery, identity/send delegate, session, formatting, and payload planning helpers |
266266
| `plugin-sdk/thread-bindings-runtime` | Thread-binding helpers | Thread-binding lifecycle and adapter helpers |
267267
| `plugin-sdk/agent-media-payload` | Legacy media payload helpers | Agent media payload builder for legacy field layouts |
268268
| `plugin-sdk/channel-runtime` | Deprecated compatibility shim | Legacy channel runtime utilities only |

‎docs/plugins/sdk-subpaths.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ For the plugin authoring guide, see [Plugin SDK overview](/plugins/sdk-overview)
5050
| `plugin-sdk/inbound-reply-dispatch` | Shared inbound record-and-dispatch helpers |
5151
| `plugin-sdk/messaging-targets` | Target parsing/matching helpers |
5252
| `plugin-sdk/outbound-media` | Shared outbound media loading helpers |
53-
| `plugin-sdk/outbound-runtime` | Outbound identity, send delegate, and payload planning helpers |
53+
| `plugin-sdk/outbound-runtime` | Outbound delivery, identity, send delegate, session, formatting, and payload planning helpers |
5454
| `plugin-sdk/poll-runtime` | Narrow poll normalization helpers |
5555
| `plugin-sdk/thread-bindings-runtime` | Thread-binding lifecycle and adapter helpers |
5656
| `plugin-sdk/agent-media-payload` | Legacy agent media payload builder |

‎extensions/discord/src/channel.ts‎

Lines changed: 8 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from "openclaw/plugin-sdk/directory-runtime";
1616
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
1717
import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
18-
import { resolveOutboundSendDep } from "openclaw/plugin-sdk/outbound-runtime";
1918
import { sleepWithAbort } from "openclaw/plugin-sdk/runtime-env";
2019
import {
2120
createComputedAccountStatusAdapter,
@@ -49,16 +48,12 @@ import {
4948
resolveDiscordGroupRequireMention,
5049
resolveDiscordGroupToolPolicy,
5150
} from "./group-policy.js";
52-
import { isLikelyDiscordVideoMedia } from "./media-detection.js";
5351
import {
5452
setThreadBindingIdleTimeoutBySessionKey,
5553
setThreadBindingMaxAgeBySessionKey,
5654
} from "./monitor/thread-bindings.session-updates.js";
57-
import {
58-
looksLikeDiscordTargetId,
59-
normalizeDiscordMessagingTarget,
60-
normalizeDiscordOutboundTarget,
61-
} from "./normalize.js";
55+
import { looksLikeDiscordTargetId, normalizeDiscordMessagingTarget } from "./normalize.js";
56+
import { discordOutbound } from "./outbound-adapter.js";
6257
import { resolveDiscordOutboundSessionRoute } from "./outbound-session-route.js";
6358
import type { DiscordProbe } from "./probe.js";
6459
import { getDiscordRuntime } from "./runtime.js";
@@ -69,8 +64,6 @@ import { createDiscordPluginBase, discordConfigAdapter } from "./shared.js";
6964
import { collectDiscordStatusIssues } from "./status-issues.js";
7065
import { parseDiscordTarget } from "./target-parsing.js";
7166

72-
type DiscordSendFn = typeof import("./send.js").sendMessageDiscord;
73-
7467
let discordProviderRuntimePromise:
7568
| Promise<typeof import("./monitor/provider.runtime.js")>
7669
| undefined;
@@ -145,22 +138,6 @@ function resolveRuntimeDiscordMessageActions() {
145138
}
146139
}
147140

148-
function resolveOptionalDiscordRuntime() {
149-
try {
150-
return getDiscordRuntime();
151-
} catch {
152-
return null;
153-
}
154-
}
155-
156-
async function resolveDiscordSend(deps?: { [channelId: string]: unknown }): Promise<DiscordSendFn> {
157-
return (
158-
resolveOutboundSendDep<DiscordSendFn>(deps, "discord") ??
159-
resolveOptionalDiscordRuntime()?.channel?.discord?.sendMessageDiscord ??
160-
(await loadDiscordSendModule()).sendMessageDiscord
161-
);
162-
}
163-
164141
const discordMessageActions = {
165142
describeMessageTool: (
166143
ctx: Parameters<NonNullable<ChannelMessageActionAdapter["describeMessageTool"]>>[0],
@@ -811,84 +788,13 @@ export const discordPlugin: ChannelPlugin<ResolvedDiscordAccount, DiscordProbe>
811788
},
812789
},
813790
outbound: {
814-
base: {
815-
deliveryMode: "direct",
816-
chunker: null,
817-
textChunkLimit: 2000,
818-
pollMaxOptions: 10,
819-
shouldTreatDeliveredTextAsVisible: shouldTreatDiscordDeliveredTextAsVisible,
820-
shouldSuppressLocalPayloadPrompt: ({ cfg, accountId, payload }) =>
821-
shouldSuppressLocalDiscordExecApprovalPrompt({
822-
cfg,
823-
accountId,
824-
payload,
825-
}),
826-
resolveTarget: ({ to }) => normalizeDiscordOutboundTarget(to),
827-
},
828-
attachedResults: {
829-
channel: "discord",
830-
sendText: async ({ cfg, to, text, accountId, deps, replyToId, threadId, silent }) => {
831-
const send = await resolveDiscordSend(deps);
832-
return await send(resolveDiscordAttachedOutboundTarget({ to, threadId }), text, {
833-
verbose: false,
834-
cfg,
835-
replyTo: replyToId ?? undefined,
836-
accountId: accountId ?? undefined,
837-
silent: silent ?? undefined,
838-
});
839-
},
840-
sendMedia: async ({
791+
...discordOutbound,
792+
shouldTreatDeliveredTextAsVisible: shouldTreatDiscordDeliveredTextAsVisible,
793+
shouldSuppressLocalPayloadPrompt: ({ cfg, accountId, payload }) =>
794+
shouldSuppressLocalDiscordExecApprovalPrompt({
841795
cfg,
842-
to,
843-
text,
844-
mediaUrl,
845-
mediaLocalRoots,
846-
mediaReadFile,
847796
accountId,
848-
deps,
849-
replyToId,
850-
threadId,
851-
silent,
852-
}) => {
853-
const send = await resolveDiscordSend(deps);
854-
const target = resolveDiscordAttachedOutboundTarget({ to, threadId });
855-
if (text.trim() && mediaUrl && isLikelyDiscordVideoMedia(mediaUrl)) {
856-
await send(target, text, {
857-
verbose: false,
858-
cfg,
859-
replyTo: replyToId ?? undefined,
860-
accountId: accountId ?? undefined,
861-
silent: silent ?? undefined,
862-
});
863-
return await send(target, "", {
864-
verbose: false,
865-
cfg,
866-
mediaUrl,
867-
mediaLocalRoots,
868-
mediaReadFile,
869-
accountId: accountId ?? undefined,
870-
silent: silent ?? undefined,
871-
});
872-
}
873-
return await send(target, text, {
874-
verbose: false,
875-
cfg,
876-
mediaUrl,
877-
mediaLocalRoots,
878-
mediaReadFile,
879-
replyTo: replyToId ?? undefined,
880-
accountId: accountId ?? undefined,
881-
silent: silent ?? undefined,
882-
});
883-
},
884-
sendPoll: async ({ cfg, to, poll, accountId, threadId, silent }) =>
885-
await (
886-
await loadDiscordSendModule()
887-
).sendPollDiscord(resolveDiscordAttachedOutboundTarget({ to, threadId }), poll, {
888-
cfg,
889-
accountId: accountId ?? undefined,
890-
silent: silent ?? undefined,
891-
}),
892-
},
797+
payload,
798+
}),
893799
},
894800
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
2+
import {
3+
resolveRetryConfig,
4+
retryAsync,
5+
type RetryConfig,
6+
} from "openclaw/plugin-sdk/retry-runtime";
7+
import { resolveDiscordAccount } from "./accounts.js";
8+
9+
const DISCORD_DELIVERY_RETRY_DEFAULTS = {
10+
attempts: 3,
11+
minDelayMs: 1000,
12+
maxDelayMs: 30_000,
13+
jitter: 0,
14+
} satisfies Required<RetryConfig>;
15+
16+
function isRetryableDiscordDeliveryError(err: unknown): boolean {
17+
const status = (err as { status?: number }).status ?? (err as { statusCode?: number }).statusCode;
18+
return status === 429 || (status !== undefined && status >= 500);
19+
}
20+
21+
function getDiscordDeliveryRetryAfterMs(err: unknown): number | undefined {
22+
if (!err || typeof err !== "object") {
23+
return undefined;
24+
}
25+
if (
26+
"retryAfter" in err &&
27+
typeof err.retryAfter === "number" &&
28+
Number.isFinite(err.retryAfter)
29+
) {
30+
return err.retryAfter * 1000;
31+
}
32+
const retryAfterRaw = (err as { headers?: Record<string, string> }).headers?.["retry-after"];
33+
if (!retryAfterRaw) {
34+
return undefined;
35+
}
36+
const retryAfterMs = Number(retryAfterRaw) * 1000;
37+
return Number.isFinite(retryAfterMs) ? retryAfterMs : undefined;
38+
}
39+
40+
export async function withDiscordDeliveryRetry<T>(params: {
41+
cfg: OpenClawConfig;
42+
accountId?: string | null;
43+
fn: () => Promise<T>;
44+
}): Promise<T> {
45+
const account = resolveDiscordAccount({ cfg: params.cfg, accountId: params.accountId });
46+
const retryConfig = resolveRetryConfig(DISCORD_DELIVERY_RETRY_DEFAULTS, account.config.retry);
47+
return await retryAsync(params.fn, {
48+
...retryConfig,
49+
shouldRetry: (err) => isRetryableDiscordDeliveryError(err),
50+
retryAfterMs: getDiscordDeliveryRetryAfterMs,
51+
});
52+
}

0 commit comments

Comments
 (0)