Skip to content

Commit 8aaf9fc

Browse files
davelutztxxena68steipete
authored
fix(discord): send fresh final messages after previews (#99711)
* fix(discord): send fresh final messages after previews * fix(discord): suppress broadcast mentions on fresh finals * docs(changelog): note Discord streamed final fix * fix(discord): stabilize targeted mention policy --------- Co-authored-by: Dave Lutz <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 6bc661c commit 8aaf9fc

11 files changed

Lines changed: 121 additions & 131 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Docs: https://docs.openclaw.ai
2222

2323
### Fixes
2424

25+
- **Discord streamed finals:** send completion replies as fresh messages so inactive channels become unread, while preserving targeted mentions without escalating `@everyone` or `@here`. (#99711, #99662) Thanks @davelutztx.
2526
- **Agent auth copy order:** preserve the source agent's portable auth-profile precedence when copying credentials to a new agent while excluding skipped profiles and transient auth state. (#100833) Thanks @machine3at.
2627
- **Cron edit delivery:** preserve each job's implicit delivery mode when applying partial delivery updates, so disabling best-effort delivery no longer turns detached job announcements off. (#100846) Thanks @machine3at.
2728
- **Control UI session creation:** keep newly created sessions at the front of the stable sidebar order after selecting another session. Thanks @shakkernerd.

‎extensions/discord/src/channel.message-adapter.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ describe("discord channel message adapter", () => {
242242
expect(adapter.live?.finalizer?.capabilities?.discardPending).toBe(true);
243243
},
244244
previewFinalization: () => {
245-
expect(adapter.live?.finalizer?.capabilities?.finalEdit).toBe(true);
245+
expect(adapter.live?.finalizer?.capabilities?.normalFallback).toBe(true);
246246
},
247247
progressUpdates: () => {
248248
expect(adapter.live?.capabilities?.draftPreview).toBe(true);
@@ -255,7 +255,7 @@ describe("discord channel message adapter", () => {
255255
adapter,
256256
proofs: {
257257
finalEdit: () => {
258-
expect(adapter.live?.capabilities?.previewFinalization).toBe(true);
258+
expect(adapter.live?.finalizer?.capabilities?.finalEdit).toBe(false);
259259
},
260260
normalFallback: () => {
261261
expect(sendText).toBeTypeOf("function");

‎extensions/discord/src/channel.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const discordMessageAdapter = createChannelMessageAdapterFromOutbound({
9595
},
9696
finalizer: {
9797
capabilities: {
98-
finalEdit: true,
98+
finalEdit: false,
9999
normalFallback: true,
100100
discardPending: true,
101101
},

‎extensions/discord/src/monitor/message-handler.process.test.ts‎

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// Discord tests cover message handler.process plugin behavior.
2-
import { MessageFlags } from "discord-api-types/v10";
32
import { DEFAULT_EMOJIS, DEFAULT_TIMING } from "openclaw/plugin-sdk/channel-feedback";
43
import {
54
recordChannelBotPairLoopAndCheckSuppression,
@@ -733,19 +732,6 @@ function createMockDraftStreamForTest() {
733732
return draftStream;
734733
}
735734

736-
function expectPreviewEditContent(content: string) {
737-
const call = firstMockCall(editMessageDiscord, "preview edit");
738-
expect(call[0]).toBe("c1");
739-
expect(call[1]).toBe("preview-1");
740-
expect(call[2]).toEqual({ content, flags: MessageFlags.SuppressEmbeds });
741-
requireRecord(requireRecord(call[3], "preview edit options").rest, "preview edit REST client");
742-
}
743-
744-
function expectSinglePreviewEdit() {
745-
expectPreviewEditContent("Hello\nWorld");
746-
expect(deliverDiscordReply).not.toHaveBeenCalled();
747-
}
748-
749735
function expectProgressSummaryEdit(...parts: string[]) {
750736
const call = firstMockCall(editMessageDiscord, "summary edit");
751737
expect(call[0]).toBe("c1");
@@ -2028,9 +2014,10 @@ describe("processDiscordMessage draft streaming", () => {
20282014
});
20292015
}
20302016

2031-
it("finalizes via preview edit when final fits one chunk", async () => {
2017+
it("sends a fresh final message when final fits one chunk", async () => {
20322018
await runSingleChunkFinalScenario({ streamMode: "partial", maxLinesPerMessage: 5 });
2033-
expectSinglePreviewEdit();
2019+
expect(editMessageDiscord).not.toHaveBeenCalled();
2020+
expectFreshFinalText("Hello\nWorld");
20342021
});
20352022

20362023
it("delivers a fresh message instead of a preview edit when the final reply resolves a mention alias", async () => {
@@ -2048,8 +2035,6 @@ describe("processDiscordMessage draft streaming", () => {
20482035

20492036
await runProcessDiscordMessage(ctx);
20502037

2051-
// Discord only fires mention notifications on create, never on edits, so the
2052-
// streamed preview must be abandoned and the mention delivered fresh.
20532038
expect(editMessageDiscord).not.toHaveBeenCalled();
20542039
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
20552040
});
@@ -2070,7 +2055,7 @@ describe("processDiscordMessage draft streaming", () => {
20702055
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
20712056
});
20722057

2073-
it("still finalizes via preview edit when an unaliased handle stays plain text", async () => {
2058+
it("sends a fresh final message when an unaliased handle stays plain text", async () => {
20742059
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
20752060
await params?.dispatcher.sendFinalReply({ text: "On it @Sentinel" });
20762061
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
@@ -2082,13 +2067,14 @@ describe("processDiscordMessage draft streaming", () => {
20822067

20832068
await runProcessDiscordMessage(ctx);
20842069

2085-
expect(editMessageDiscord).toHaveBeenCalledTimes(1);
2086-
expect(deliverDiscordReply).not.toHaveBeenCalled();
2070+
expect(editMessageDiscord).not.toHaveBeenCalled();
2071+
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
20872072
});
20882073

2089-
it("still finalizes via preview edit for broadcast mentions like @everyone", async () => {
2074+
it("sends a fresh final message for broadcast mentions like @everyone", async () => {
20902075
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
20912076
await params?.dispatcher.sendFinalReply({ text: "heads up @everyone" });
2077+
await params?.dispatcher.waitForIdle();
20922078
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
20932079
});
20942080

@@ -2098,13 +2084,16 @@ describe("processDiscordMessage draft streaming", () => {
20982084

20992085
await runProcessDiscordMessage(ctx);
21002086

2101-
expect(editMessageDiscord).toHaveBeenCalledTimes(1);
2102-
expect(deliverDiscordReply).not.toHaveBeenCalled();
2087+
expect(editMessageDiscord).not.toHaveBeenCalled();
2088+
expect(firstMockArg(deliverDiscordReply, "deliverDiscordReply")).toMatchObject({
2089+
allowedMentions: { parse: ["users", "roles"] },
2090+
});
21032091
});
21042092

2105-
it("still finalizes via preview edit when a targeted mention is mixed with @everyone", async () => {
2093+
it("sends a fresh final message when a targeted mention is mixed with @everyone", async () => {
21062094
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
21072095
await params?.dispatcher.sendFinalReply({ text: "heads up @Sentinel @everyone" });
2096+
await params?.dispatcher.waitForIdle();
21082097
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
21092098
});
21102099

@@ -2117,14 +2106,16 @@ describe("processDiscordMessage draft streaming", () => {
21172106

21182107
await runProcessDiscordMessage(ctx);
21192108

2120-
// Mixed targeted + broadcast must not escalate into a create that pings @everyone.
2121-
expect(editMessageDiscord).toHaveBeenCalledTimes(1);
2122-
expect(deliverDiscordReply).not.toHaveBeenCalled();
2109+
expect(editMessageDiscord).not.toHaveBeenCalled();
2110+
expect(firstMockArg(deliverDiscordReply, "deliverDiscordReply")).toMatchObject({
2111+
allowedMentions: { parse: ["users", "roles"] },
2112+
});
21232113
});
21242114

21252115
it("accepts streaming=true alias for partial preview mode", async () => {
21262116
await runSingleChunkFinalScenario({ streaming: true, maxLinesPerMessage: 5 });
2127-
expectSinglePreviewEdit();
2117+
expect(editMessageDiscord).not.toHaveBeenCalled();
2118+
expectFreshFinalText("Hello\nWorld");
21282119
});
21292120

21302121
it("defaults unset Discord preview streaming to progress mode without drafting text-only turns", async () => {
@@ -2418,8 +2409,9 @@ describe("processDiscordMessage draft streaming", () => {
24182409
expect(draftStream.clear).toHaveBeenCalledTimes(1);
24192410
});
24202411

2421-
it("uses root discord maxLinesPerMessage for preview finalization when runtime config omits it", async () => {
2412+
it("uses root discord maxLinesPerMessage for fresh final delivery when runtime config omits it", async () => {
24222413
const longReply = Array.from({ length: 20 }, (_value, index) => `Line ${index + 1}`).join("\n");
2414+
const draftStream = createMockDraftStreamForTest();
24232415
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
24242416
await params?.dispatcher.sendFinalReply({ text: longReply });
24252417
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
@@ -2440,8 +2432,10 @@ describe("processDiscordMessage draft streaming", () => {
24402432

24412433
await runProcessDiscordMessage(ctx);
24422434

2443-
expectPreviewEditContent(longReply);
2444-
expect(deliverDiscordReply).not.toHaveBeenCalled();
2435+
expect(editMessageDiscord).not.toHaveBeenCalled();
2436+
expectFreshFinalText(longReply);
2437+
expect(draftStream.discardPending).toHaveBeenCalledTimes(1);
2438+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
24452439
});
24462440

24472441
it("falls back to standard delivery for explicit reply-tag finals", async () => {
@@ -2488,7 +2482,7 @@ describe("processDiscordMessage draft streaming", () => {
24882482
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
24892483
});
24902484

2491-
it("keeps the preview and sends media-only for TTS supplement finals", async () => {
2485+
it("sends a fresh visible TTS supplement final and clears the preview", async () => {
24922486
const draftStream = createMockDraftStreamForTest();
24932487
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
24942488
await params?.dispatcher.sendFinalReply({
@@ -2507,15 +2501,16 @@ describe("processDiscordMessage draft streaming", () => {
25072501

25082502
await runProcessDiscordMessage(ctx);
25092503

2510-
expect(draftStream.flush).toHaveBeenCalledTimes(1);
2511-
expect(draftStream.discardPending).not.toHaveBeenCalled();
2512-
expect(draftStream.clear).not.toHaveBeenCalled();
2513-
expectPreviewEditContent("Spoken answer");
2504+
expect(draftStream.flush).not.toHaveBeenCalled();
2505+
expect(draftStream.discardPending).toHaveBeenCalledTimes(1);
2506+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
2507+
expect(editMessageDiscord).not.toHaveBeenCalled();
25142508
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
25152509
expect(firstMockArg(deliverDiscordReply, "deliverDiscordReply")).toMatchObject({
25162510
replyToId: "m1",
25172511
replies: [
25182512
{
2513+
text: "Spoken answer",
25192514
mediaUrl: "https://example.com/tts.mp3",
25202515
audioAsVoice: true,
25212516
spokenText: "Spoken answer",
@@ -2525,9 +2520,8 @@ describe("processDiscordMessage draft streaming", () => {
25252520
});
25262521
});
25272522

2528-
it("falls back with visible text when TTS supplement preview finalization fails", async () => {
2523+
it("sends fresh visible text for TTS supplement finals", async () => {
25292524
const draftStream = createMockDraftStreamForTest();
2530-
editMessageDiscord.mockRejectedValueOnce(new Error("edit failed"));
25312525
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
25322526
await params?.dispatcher.sendFinalReply({
25332527
mediaUrl: "https://example.com/tts.mp3",
@@ -2544,9 +2538,10 @@ describe("processDiscordMessage draft streaming", () => {
25442538

25452539
await runProcessDiscordMessage(ctx);
25462540

2547-
expect(draftStream.flush).toHaveBeenCalledTimes(1);
2541+
expect(draftStream.flush).not.toHaveBeenCalled();
25482542
expect(draftStream.discardPending).toHaveBeenCalled();
25492543
expect(draftStream.clear).toHaveBeenCalled();
2544+
expect(editMessageDiscord).not.toHaveBeenCalled();
25502545
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
25512546
expect(firstMockArg(deliverDiscordReply, "deliverDiscordReply")).toMatchObject({
25522547
replies: [
@@ -2636,10 +2631,11 @@ describe("processDiscordMessage draft streaming", () => {
26362631

26372632
await runProcessDiscordMessage(ctx);
26382633

2639-
expectPreviewEditContent("delivery survived");
2640-
expect(draftStream.clear).not.toHaveBeenCalled();
2641-
expect(draftStream.messageId()).toBe("preview-1");
2642-
expect(deliverDiscordReply).not.toHaveBeenCalled();
2634+
expect(editMessageDiscord).not.toHaveBeenCalled();
2635+
expectFreshFinalText("delivery survived");
2636+
expect(draftStream.discardPending).toHaveBeenCalledTimes(1);
2637+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
2638+
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
26432639
});
26442640

26452641
it("drops earlier tool warning finals when recovered replies arrive", async () => {
@@ -2657,10 +2653,11 @@ describe("processDiscordMessage draft streaming", () => {
26572653

26582654
await runProcessDiscordMessage(ctx);
26592655

2660-
expectPreviewEditContent("delivery recovered");
2661-
expect(draftStream.clear).not.toHaveBeenCalled();
2662-
expect(draftStream.messageId()).toBe("preview-1");
2663-
expect(deliverDiscordReply).not.toHaveBeenCalled();
2656+
expect(editMessageDiscord).not.toHaveBeenCalled();
2657+
expectFreshFinalText("delivery recovered");
2658+
expect(draftStream.discardPending).toHaveBeenCalledTimes(1);
2659+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
2660+
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
26642661
});
26652662

26662663
it("suppresses pure tool warning finals when no recovered reply is available", async () => {
@@ -2723,9 +2720,11 @@ describe("processDiscordMessage draft streaming", () => {
27232720

27242721
await runProcessDiscordMessage(ctx);
27252722

2726-
expectPreviewEditContent("Done.");
2727-
expect(draftStream.clear).not.toHaveBeenCalled();
2728-
expect(deliverDiscordReply).not.toHaveBeenCalled();
2723+
expect(editMessageDiscord).not.toHaveBeenCalled();
2724+
expectFreshFinalText("Done.");
2725+
expect(draftStream.discardPending).toHaveBeenCalledTimes(1);
2726+
expect(draftStream.clear).toHaveBeenCalledTimes(1);
2727+
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
27292728
});
27302729

27312730
it("renders reasoning block payloads as a 🧠 blockquote", async () => {

0 commit comments

Comments
 (0)