Skip to content

Commit 21bc2c4

Browse files
authored
fix(discord): explain failed form openings instead of silently doing nothing (#119657)
* fix(discord): report failed modal presentation to the operator * refactor(discord): reuse canonical interaction failure recovery
1 parent 40a3bd2 commit 21bc2c4

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

‎extensions/discord/src/monitor/agent-components.handlers.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ async function handleDiscordModalTrigger(params: {
258258
);
259259
} catch (err) {
260260
logError(`${params.label}: failed to show modal: ${String(err)}`);
261+
await replyUnavailableComponentInteraction(
262+
params.interaction,
263+
"Could not open this form. Request a new form and try again.",
264+
);
261265
}
262266
}
263267

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { InteractionResponseType, MessageFlags } from "discord-api-types/v10";
2+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
3+
import { beforeEach, describe, expect, it, vi } from "vitest";
4+
import { buildDiscordComponentCustomId } from "../component-custom-id.js";
5+
import {
6+
registerDiscordComponentEntries,
7+
resolveDiscordComponentEntryWithPersistence,
8+
resolveDiscordModalEntryWithPersistence,
9+
} from "../components-registry.js";
10+
import { clearDiscordComponentEntriesForTest } from "../components-registry.test-support.js";
11+
import type { DiscordComponentEntry, DiscordModalEntry } from "../components.js";
12+
import { ButtonInteraction, createInteraction, type ComponentData } from "../internal/discord.js";
13+
import {
14+
attachRestMock,
15+
createInternalComponentInteractionPayload,
16+
createInternalTestClient,
17+
} from "../internal/test-builders.test-support.js";
18+
import { resetDiscordComponentRuntimeMocks } from "../test-support/component-runtime.js";
19+
import { createDiscordComponentControls } from "./agent-components.js";
20+
21+
describe("Discord modal presentation failures", () => {
22+
beforeEach(() => {
23+
clearDiscordComponentEntriesForTest();
24+
resetDiscordComponentRuntimeMocks();
25+
});
26+
27+
it.each([{ replyRejects: false }, { replyRejects: true }])(
28+
"handles a rejected modal callback with replyRejects=$replyRejects",
29+
async ({ replyRejects }) => {
30+
const sharedComponent = {
31+
messageId: "msg-1",
32+
sessionKey: "session-1",
33+
agentId: "agent-1",
34+
accountId: "default",
35+
consumptionGroupId: "group-1",
36+
consumptionGroupEntryIds: ["btn_1", "btn_cancel"],
37+
};
38+
const modalTrigger: DiscordComponentEntry = {
39+
...sharedComponent,
40+
id: "btn_1",
41+
kind: "modal-trigger",
42+
label: "Open form",
43+
modalId: "mdl_1",
44+
};
45+
const siblingButton: DiscordComponentEntry = {
46+
...sharedComponent,
47+
id: "btn_cancel",
48+
kind: "button",
49+
label: "Cancel",
50+
};
51+
const modal: DiscordModalEntry = {
52+
id: "mdl_1",
53+
title: "Details",
54+
messageId: "msg-1",
55+
sessionKey: "session-1",
56+
agentId: "agent-1",
57+
accountId: "default",
58+
fields: [{ id: "fld_1", name: "name", label: "Name", type: "text" }],
59+
};
60+
registerDiscordComponentEntries({ entries: [modalTrigger, siblingButton], modals: [modal] });
61+
62+
const createButton = createDiscordComponentControls[0];
63+
if (!createButton) {
64+
throw new Error("expected Discord component button factory");
65+
}
66+
const cfg: OpenClawConfig = {
67+
channels: { discord: { replyToMode: "first" } },
68+
};
69+
const button = createButton({
70+
cfg,
71+
accountId: "default",
72+
dmPolicy: "allowlist",
73+
allowFrom: ["123456789"],
74+
discordConfig: { replyToMode: "first" },
75+
token: "token",
76+
});
77+
const post = vi.fn().mockRejectedValueOnce(new Error("Discord rejected the modal"));
78+
if (replyRejects) {
79+
post.mockRejectedValueOnce(new Error("Discord rejected the recovery reply"));
80+
} else {
81+
post.mockResolvedValueOnce(undefined);
82+
}
83+
const client = createInternalTestClient();
84+
attachRestMock(client, { post });
85+
const interaction = createInteraction(
86+
client,
87+
createInternalComponentInteractionPayload({
88+
id: "interaction-1",
89+
token: "interaction-token",
90+
channel_id: "dm-channel",
91+
user: {
92+
id: "123456789",
93+
username: "AgentUser",
94+
discriminator: "0001",
95+
global_name: null,
96+
avatar: null,
97+
},
98+
data: {
99+
custom_id: buildDiscordComponentCustomId({ componentId: "btn_1", modalId: "mdl_1" }),
100+
},
101+
}),
102+
);
103+
if (!(interaction instanceof ButtonInteraction)) {
104+
throw new Error("expected a Discord button interaction");
105+
}
106+
107+
await button.run(interaction, { cid: "btn_1", mid: "mdl_1" } as ComponentData);
108+
109+
const callbackPath = "/interactions/interaction-1/interaction-token/callback";
110+
expect(post).toHaveBeenCalledTimes(2);
111+
expect(post).toHaveBeenNthCalledWith(
112+
1,
113+
callbackPath,
114+
expect.objectContaining({
115+
body: expect.objectContaining({ type: InteractionResponseType.Modal }),
116+
}),
117+
);
118+
expect(post).toHaveBeenNthCalledWith(2, callbackPath, {
119+
body: {
120+
type: InteractionResponseType.ChannelMessageWithSource,
121+
data: {
122+
content: "Could not open this form. Request a new form and try again.",
123+
flags: MessageFlags.Ephemeral,
124+
},
125+
},
126+
});
127+
await expect(
128+
resolveDiscordComponentEntryWithPersistence({ id: "btn_1", consume: false }),
129+
).resolves.toBeNull();
130+
await expect(
131+
resolveDiscordComponentEntryWithPersistence({ id: "btn_cancel", consume: false }),
132+
).resolves.toBeNull();
133+
await expect(
134+
resolveDiscordModalEntryWithPersistence({ id: "mdl_1", consume: false }),
135+
).resolves.toEqual(expect.objectContaining({ id: "mdl_1" }));
136+
},
137+
);
138+
});

0 commit comments

Comments
 (0)