Skip to content

Commit 47072b8

Browse files
committed
refactor(discord): reuse canonical interactive component renderer
1 parent ebc70fa commit 47072b8

3 files changed

Lines changed: 56 additions & 23 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ extensions/discord/src/send.shared.ts 4
436436
extensions/discord/src/send.webhook.ts 1
437437
extensions/discord/src/setup-account-state.ts 1
438438
extensions/discord/src/setup-core.ts 1
439-
extensions/discord/src/shared-interactive.ts 1
440439
extensions/discord/src/shared.ts 1
441440
extensions/discord/src/status-issues.ts 1
442441
extensions/discord/src/target-parsing.ts 1

‎extensions/discord/src/shared-interactive.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,59 @@ describe("buildDiscordInteractiveComponents", () => {
9898
});
9999
});
100100

101+
it.each([
102+
{
103+
name: "approval",
104+
action: {
105+
type: "approval" as const,
106+
approvalId: "approval-1",
107+
approvalKind: "exec" as const,
108+
decision: "allow-once" as const,
109+
},
110+
expected: {
111+
internalCustomId: "execapproval:kind=exec;id=approval-1;action=allow-once",
112+
},
113+
},
114+
{
115+
name: "callback",
116+
action: { type: "callback" as const, value: "plugin:opaque|value" },
117+
expected: { callbackData: "plugin:opaque|value", callbackDataKind: "callback" },
118+
},
119+
{
120+
name: "command",
121+
action: { type: "command" as const, command: "/codex inspect" },
122+
expected: { callbackData: "/codex inspect", callbackDataKind: "command" },
123+
},
124+
{
125+
name: "question",
126+
action: {
127+
type: "question" as const,
128+
questionId: "ask_0123456789abcdef0123456789abcdef",
129+
optionValue: "Production",
130+
},
131+
expected: { internalCustomId: "ocq:id=ask_0123456789abcdef0123456789abcdef;i=1" },
132+
},
133+
])("preserves typed $name authority through the legacy renderer", ({ action, expected }) => {
134+
expect(
135+
buildDiscordInteractiveComponents({
136+
blocks: [
137+
{
138+
type: "buttons",
139+
buttons: [
140+
{
141+
label: "Unavailable",
142+
action: { type: "web-app", widgetId: "invalid" },
143+
},
144+
{ label: "Continue", action },
145+
],
146+
},
147+
],
148+
}),
149+
).toMatchObject({
150+
blocks: [{ type: "actions", buttons: [{ label: "Continue", ...expected }] }],
151+
});
152+
});
153+
101154
it.each(["url", "web-app"] as const)(
102155
"renders typed %s actions as Discord link buttons",
103156
(type) => {

‎extensions/discord/src/shared-interactive.ts‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Discord plugin module implements shared interactive behavior.
22
import {
3-
reduceLegacyInteractiveReply,
3+
legacyInteractiveReplyToPresentation,
44
resolveMessagePresentationActionValue,
55
resolveMessagePresentationButtonAction,
66
resolveMessagePresentationOptionAction,
@@ -180,28 +180,9 @@ function appendDiscordSelectBlock(
180180
export function buildDiscordInteractiveComponents(
181181
interactive?: LegacyInteractiveReply,
182182
): DiscordComponentMessageSpec | undefined {
183-
const blocks = reduceLegacyInteractiveReply(
184-
interactive,
185-
[] as NonNullable<DiscordComponentMessageSpec["blocks"]>,
186-
(state, block) => {
187-
if (block.type === "text") {
188-
const text = block.text.trim();
189-
if (text) {
190-
state.push({ type: "text", text });
191-
}
192-
return state;
193-
}
194-
if (block.type === "buttons") {
195-
appendDiscordButtonBlocks(state, block.buttons);
196-
return state;
197-
}
198-
if (block.type === "select") {
199-
appendDiscordSelectBlock(state, block);
200-
}
201-
return state;
202-
},
183+
return buildDiscordPresentationComponents(
184+
interactive ? legacyInteractiveReplyToPresentation(interactive) : undefined,
203185
);
204-
return blocks.length > 0 ? { blocks } : undefined;
205186
}
206187

207188
export function buildDiscordPresentationComponents(

0 commit comments

Comments
 (0)