Skip to content

Commit 6f018ae

Browse files
committed
refactor(slack): reuse canonical interactive block renderer
1 parent b8d6e79 commit 6f018ae

3 files changed

Lines changed: 52 additions & 115 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ extensions/slack/src/approval-native-gates.ts 1
11641164
extensions/slack/src/approval-native.ts 2
11651165
extensions/slack/src/blocks-fallback.ts 2
11661166
extensions/slack/src/blocks-input.ts 2
1167-
extensions/slack/src/blocks-render.ts 2
1167+
extensions/slack/src/blocks-render.ts 1
11681168
extensions/slack/src/channel-actions.ts 1
11691169
extensions/slack/src/channel-type.ts 1
11701170
extensions/slack/src/channel.ts 7

‎extensions/slack/src/blocks-render.ts‎

Lines changed: 5 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { Block, KnownBlock } from "@slack/web-api";
33
import { parseExecApprovalCommandText } from "openclaw/plugin-sdk/approval-reply-runtime";
44
import {
5-
reduceLegacyInteractiveReply,
5+
legacyInteractiveReplyToPresentation,
66
resolveMessagePresentationButtonAction,
77
resolveMessagePresentationOptionAction,
88
} from "openclaw/plugin-sdk/interactive-runtime";
@@ -248,119 +248,10 @@ export function buildSlackInteractiveBlocks(
248248
interactive?: LegacyInteractiveReply,
249249
options: SlackBlockRenderOptions = {},
250250
): SlackBlock[] {
251-
const initialState = {
252-
blocks: [] as SlackBlock[],
253-
buttonIndex: options.buttonIndexOffset ?? 0,
254-
selectIndex: options.selectIndexOffset ?? 0,
255-
};
256-
return reduceLegacyInteractiveReply(interactive, initialState, (state, block) => {
257-
if (block.type === "text") {
258-
const trimmed = block.text.trim();
259-
if (!trimmed) {
260-
return state;
261-
}
262-
state.blocks.push({
263-
type: "section",
264-
text: {
265-
type: "mrkdwn",
266-
text: truncateSlackText(trimmed, SLACK_SECTION_TEXT_MAX),
267-
},
268-
});
269-
return state;
270-
}
271-
if (block.type === "buttons") {
272-
// Index is position in the question's options; core emits one buttons block in option order.
273-
const elements = block.buttons
274-
.flatMap((button, choiceIndex) => {
275-
const target = resolveSlackButtonTarget(button, choiceIndex);
276-
if (
277-
!target ||
278-
(target.kind === "link"
279-
? !isWithinSlackLimit(target.url, SLACK_BUTTON_URL_MAX)
280-
: !isWithinSlackLimit(target.value, SLACK_BUTTON_VALUE_MAX))
281-
) {
282-
return [];
283-
}
284-
const style = resolveSlackButtonStyle(button.style);
285-
return [
286-
{
287-
type: "button" as const,
288-
// Slack emits block_actions even for URL buttons; link-only actions must be ignored.
289-
action_id:
290-
target.kind === "link"
291-
? buildSlackReplyLinkActionId(state.buttonIndex + 1, choiceIndex)
292-
: target.kind === "approval"
293-
? buildSlackApprovalButtonActionId(state.buttonIndex + 1, choiceIndex)
294-
: target.kind === "callback"
295-
? buildSlackCallbackButtonActionId(state.buttonIndex + 1, choiceIndex)
296-
: target.kind === "question"
297-
? buildSlackQuestionButtonActionId(state.buttonIndex + 1, choiceIndex)
298-
: buildSlackReplyButtonActionId(state.buttonIndex + 1, choiceIndex),
299-
text: {
300-
type: "plain_text" as const,
301-
text: truncateSlackText(button.label, SLACK_ACTION_LABEL_MAX),
302-
emoji: true,
303-
},
304-
...(target.kind === "link" ? { url: target.url } : { value: target.value }),
305-
...(style ? { style } : {}),
306-
},
307-
];
308-
})
309-
.slice(0, SLACK_ACTION_BLOCK_ELEMENTS_MAX);
310-
if (elements.length === 0) {
311-
return state;
312-
}
313-
state.blocks.push({
314-
type: "actions",
315-
block_id: `openclaw_reply_buttons_${++state.buttonIndex}`,
316-
elements,
317-
});
318-
return state;
319-
}
320-
const optionsLocal = block.options
321-
.flatMap((option) => {
322-
const target = resolveSlackOptionTarget(option);
323-
return target ? [{ label: option.label, ...target }] : [];
324-
})
325-
.filter(isRenderableSlackOption)
326-
.slice(0, SLACK_STATIC_SELECT_OPTIONS_MAX);
327-
const optionKinds = new Set(optionsLocal.map((option) => option.kind));
328-
if (optionsLocal.length === 0 || optionKinds.size !== 1) {
329-
return state;
330-
}
331-
state.blocks.push({
332-
type: "actions",
333-
block_id: `openclaw_reply_select_${++state.selectIndex}`,
334-
elements: [
335-
{
336-
type: "static_select",
337-
action_id:
338-
optionsLocal[0]?.kind === "approval"
339-
? buildSlackApprovalSelectActionId(state.selectIndex)
340-
: optionsLocal[0]?.kind === "callback"
341-
? buildSlackCallbackSelectActionId(state.selectIndex)
342-
: buildSlackReplySelectActionId(state.selectIndex),
343-
placeholder: {
344-
type: "plain_text",
345-
text: truncateSlackText(
346-
normalizeOptionalString(block.placeholder) ?? "Choose an option",
347-
SLACK_ACTION_LABEL_MAX,
348-
),
349-
emoji: true,
350-
},
351-
options: optionsLocal.map((option, _choiceIndex) => ({
352-
text: {
353-
type: "plain_text",
354-
text: truncateSlackText(option.label, SLACK_ACTION_LABEL_MAX),
355-
emoji: true,
356-
},
357-
value: option.value,
358-
})),
359-
},
360-
],
361-
});
362-
return state;
363-
}).blocks;
251+
return buildSlackPresentationBlocks(
252+
interactive ? legacyInteractiveReplyToPresentation(interactive) : undefined,
253+
options,
254+
);
364255
}
365256

366257
/** Render portable presentation blocks as Slack Block Kit blocks. */

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,52 @@ describe("buildSlackInteractiveBlocks", () => {
339339
expect(buttonBlock.elements?.[2]?.style).toBe("primary");
340340
expect(buttonBlock.elements?.[3]).not.toHaveProperty("style");
341341
});
342+
343+
it.each([
344+
{
345+
name: "approval",
346+
action: {
347+
type: "approval" as const,
348+
approvalId: "request-1",
349+
approvalKind: "exec" as const,
350+
decision: "allow-once" as const,
351+
},
352+
actionId: "openclaw:approval_button:5:1",
353+
value:
354+
'openclaw:approval:v1:{"approvalId":"request-1","approvalKind":"exec","decision":"allow-once"}',
355+
},
356+
{
357+
name: "callback",
358+
action: { type: "callback" as const, value: "plugin:opaque|value" },
359+
actionId: "openclaw:callback_button:5:1",
360+
value: "plugin:opaque|value",
361+
},
362+
{
363+
name: "question",
364+
action: {
365+
type: "question" as const,
366+
questionId: "ask_0123456789abcdef0123456789abcdef",
367+
optionValue: "Production",
368+
},
369+
actionId: "openclaw:question_button:5:1",
370+
value: "slq1:ask_0123456789abcdef0123456789abcdef:0",
371+
},
372+
])(
373+
"preserves typed $name authority through the legacy renderer",
374+
({ action, actionId, value }) => {
375+
expect(
376+
buildSlackInteractiveBlocks(
377+
{ blocks: [{ type: "buttons", buttons: [{ label: "Continue", action }] }] },
378+
{ buttonIndexOffset: 4 },
379+
),
380+
).toMatchObject([
381+
{
382+
block_id: "openclaw_reply_buttons_5",
383+
elements: [{ action_id: actionId, value }],
384+
},
385+
]);
386+
},
387+
);
342388
});
343389

344390
describe("buildSlackPresentationBlocks", () => {

0 commit comments

Comments
 (0)