Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): preserve batched Slack HITL cards (#530)

Ruia3efd4b27bcd
diff --git a/.changeset/slack-hitl-batched-card-update.md b/.changeset/slack-hitl-batched-card-update.md
new file mode 100644
index 000000000..a10911dc2
--- /dev/null
+++ b/.changeset/slack-hitl-batched-card-update.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Render Slack HITL button prompts as card blocks, move approval tool input into collapsible containers, and keep answered-card updates scoped to the answered request so sibling batched approval buttons remain clickable.
diff --git a/packages/eve/src/public/channels/slack/hitl.test.ts b/packages/eve/src/public/channels/slack/hitl.test.ts
index d809b1953..1f5507fba 100644
--- a/packages/eve/src/public/channels/slack/hitl.test.ts
+++ b/packages/eve/src/public/channels/slack/hitl.test.ts
@@ -16,7 +16,10 @@ import {
isHitlAction,
renderInputRequestBlocks,
} from "#public/channels/slack/hitl.js";
-import { SLACK_SECTION_TEXT_MAX_LENGTH } from "#public/channels/slack/limits.js";
+import {
+ SLACK_CARD_BODY_TEXT_MAX_LENGTH,
+ SLACK_SECTION_TEXT_MAX_LENGTH,
+} from "#public/channels/slack/limits.js";
function makeRequest(overrides: Partial<InputRequest>): InputRequest {
return {
@@ -81,7 +84,7 @@ describe("isHitlAction", () => {
});
describe("renderInputRequestBlocks", () => {
- it("emits a section + buttons block for an option list with no display hint", () => {
+ it("emits a card block for an option list with no display hint", () => {
const blocks = renderInputRequestBlocks(
makeRequest({
options: [
@@ -91,32 +94,36 @@ describe("renderInputRequestBlocks", () => {
}),
);
- expect(blocks).toHaveLength(2);
- expect((blocks[0] as { type: string }).type).toBe("section");
-
- const actions = blocks[1] as {
+ expect(blocks).toHaveLength(1);
+ const card = blocks[0] as {
+ body: { text: string; type: string; verbatim: boolean };
+ actions: Array<Record<string, unknown>>;
type: string;
- elements: Array<Record<string, unknown>>;
};
- expect(actions.type).toBe("actions");
- expect(actions.elements).toHaveLength(2);
- const actionIds = actions.elements.map((element) => element.action_id);
+ expect(card).toMatchObject({
+ type: "card",
+ body: { type: "mrkdwn", text: "*Pick one*", verbatim: false },
+ });
+ expect(card.actions).toHaveLength(2);
+ const actionIds = card.actions.map((element) => element.action_id);
expect(new Set(actionIds).size).toBe(actionIds.length);
- expect(actions.elements[0]).toMatchObject({
+ expect(card.actions[0]).toMatchObject({
type: "button",
action_id: `${HITL_ACTION_PREFIX}call_abc123:button:0`,
value: "approve",
style: "primary",
+ text: { type: "plain_text", text: "Approve", emoji: false },
});
- expect(actions.elements[1]).toMatchObject({
+ expect(card.actions[1]).toMatchObject({
type: "button",
action_id: `${HITL_ACTION_PREFIX}call_abc123:button:1`,
value: "deny",
style: "danger",
+ text: { type: "plain_text", text: "Deny", emoji: false },
});
});
- it("shows tool input for confirmation approval requests", () => {
+ it("shows tool input for confirmation approval requests in a collapsible container", () => {
const request = makeRequest({
action: {
kind: "tool-call",
@@ -139,16 +146,41 @@ describe("renderInputRequestBlocks", () => {
const blocks = renderInputRequestBlocks(request);
- expect(blocks).toHaveLength(3);
- const details = blocks[1] as { type: string; text: { type: string; text: string } };
- expect(details).toMatchObject({ type: "section", text: { type: "mrkdwn" } });
- expect(details.text.text).toContain("*Tool input*");
- expect(details.text.text).toContain('"collection": "org_members"');
- expect(details.text.text).toContain('"_id": "qudw7ekkzulpgw3j"');
- expect(blocks[2]).toMatchObject({ type: "actions" });
+ expect(blocks).toHaveLength(2);
+ const card = blocks[0] as {
+ actions: Array<Record<string, unknown>>;
+ body: { text: string; type: string };
+ type: string;
+ };
+ expect(card).toMatchObject({
+ type: "card",
+ body: { type: "mrkdwn", text: "*Approve tool call: mongodb-mutate*" },
+ });
+ expect(card.actions).toMatchObject([
+ { text: { text: "Deny" }, value: "deny" },
+ { style: "primary", text: { text: "Allow" }, value: "approve" },
+ ]);
+
+ const details = blocks[1] as {
+ child_blocks: Array<{ text?: { text?: string }; type: string }>;
+ default_collapsed: boolean;
+ is_collapsible: boolean;
+ title: { text: string; type: string };
+ type: string;
+ };
+ expect(details).toMatchObject({
+ type: "container",
+ title: { type: "plain_text", text: "Tool input" },
+ is_collapsible: true,
+ default_collapsed: false,
+ });
+ expect(details.child_blocks).toHaveLength(1);
+ expect(details.child_blocks[0]).toMatchObject({ type: "section", text: { type: "mrkdwn" } });
+ expect(details.child_blocks[0]?.text?.text).toContain('"collection": "org_members"');
+ expect(details.child_blocks[0]?.text?.text).toContain('"_id": "qudw7ekkzulpgw3j"');
});
- it("keeps long approval input details within the section limit", () => {
+ it("keeps long approval input details within the collapsible container section limit", () => {
const blocks = renderInputRequestBlocks(
makeRequest({
action: {
@@ -165,9 +197,14 @@ describe("renderInputRequestBlocks", () => {
}),
);
- const details = blocks[1] as { text: { text: string } };
- expect(details.text.text.length).toBeLessThanOrEqual(SLACK_SECTION_TEXT_MAX_LENGTH);
- expect(details.text.text).toMatch(/\.\.\.\n```$/u);
+ const card = blocks[0] as { body: { text: string } };
+ expect(card.body.text.length).toBeLessThanOrEqual(SLACK_CARD_BODY_TEXT_MAX_LENGTH);
+ expect(card.body.text).toBe("*Pick one*");
+
+ const details = blocks[1] as { child_blocks: Array<{ text?: { text?: string } }> };
+ const text = details.child_blocks[0]?.text?.text;
+ expect(text?.length).toBeLessThanOrEqual(SLACK_SECTION_TEXT_MAX_LENGTH);
+ expect(text?.endsWith("...\n```")).toBe(true);
});
it("renders a radio_buttons widget for select-display requests with ≤6 options", () => {
@@ -243,7 +280,7 @@ describe("renderInputRequestBlocks", () => {
expect(button.style).toBe("primary");
});
- it("emits the freeform button alongside options when allowFreeform is set", () => {
+ it("uses option buttons when allowFreeform is set alongside options", () => {
const blocks = renderInputRequestBlocks(
makeRequest({
allowFreeform: true,
@@ -253,9 +290,9 @@ describe("renderInputRequestBlocks", () => {
// current behavior: options take precedence; freeform button is the
// fallback when no options are supplied. This documents the
// option-only path; freeform-with-options is left as future work.
- expect(blocks).toHaveLength(2);
- const actions = blocks[1] as { elements: Array<{ action_id: string }> };
- const ids = actions.elements.map((e) => e.action_id);
+ expect(blocks).toHaveLength(1);
+ const card = blocks[0] as { actions: Array<{ action_id: string }> };
+ const ids = card.actions.map((e) => e.action_id);
for (const id of ids) {
expect(id.startsWith(HITL_ACTION_PREFIX)).toBe(true);
}
@@ -268,8 +305,8 @@ describe("renderInputRequestBlocks", () => {
});
const blocks = renderInputRequestBlocks(request);
- const button = (blocks[1] as { elements: Array<{ action_id: string; value: string }> })
- .elements[0]!;
+ const button = (blocks[0] as { actions: Array<{ action_id: string; value: string }> })
+ .actions[0]!;
const response = deriveHitlResponse({ actionId: button.action_id, value: button.value });
expect(response).toEqual({
diff --git a/packages/eve/src/public/channels/slack/hitl.ts b/packages/eve/src/public/channels/slack/hitl.ts
index 175f58165..548b79812 100644
--- a/packages/eve/src/public/channels/slack/hitl.ts
+++ b/packages/eve/src/public/channels/slack/hitl.ts
@@ -13,6 +13,7 @@
*/
import {
+ truncateCardBodyText,
SLACK_SECTION_TEXT_MAX_LENGTH,
truncateModalTitle,
truncatePlainText,
@@ -61,10 +62,14 @@ export const HITL_FREEFORM_MODAL_ACTION_ID = "eve_freeform_text";
const RADIO_SELECT_OPTION_LIMIT = 6;
const BUTTON_ACTION_ID_RE = /^(?<requestId>.+):button:\d+$/u;
const TOOL_INPUT_PREFIX = "*Tool input*\n```\n";
+const TOOL_INPUT_CODE_PREFIX = "```\n";
const TOOL_INPUT_SUFFIX = "\n```";
const ANSWERED_TEXT_PREFIX = ":white_check_mark: *";
const ANSWERED_TEXT_SUFFIX = "*";
+type InputRequestOption = NonNullable<InputRequest["options"]>[number];
+type CardButtonOption = Pick<InputRequestOption, "id" | "label" | "style">;
+
/**
* Subset of one Slack interactivity action the HITL decoder reads.
* Mirrors the relevant fields of `SlackInteractionAction`.
@@ -128,8 +133,9 @@ export function isHitlAction(actionId: string): boolean {
* visible.
* - `display === "select"` with more options → `static_select`
* dropdown so the picker stays scrollable.
- * - Anything else with options → buttons. Best for visually distinct
- * choices (approve / deny / cancel).
+ * - Anything else with options → Slack `card` blocks with action
+ * buttons. Best for visually distinct choices (approve / deny /
+ * cancel).
* - No options (or `allowFreeform: true`) → a single "Type your answer"
* button that opens a Slack modal with a plain_text_input. The modal
* submission comes back as a `view_submission` webhook the channel
@@ -162,14 +168,9 @@ export function renderInputRequestBlocks(request: InputRequest): unknown[] {
}
if (options && options.length > 0) {
- return [
- prompt,
- ...details,
- {
- type: "actions",
- elements: options.map((opt, index) => buildButton(opt, actionId, index)),
- },
- ];
+ const card = renderInputRequestCardBlock(request, actionId);
+ const details = renderToolInputContainerBlock(request);
+ return details === undefined ? [card] : [card, details];
}
if (acceptsFreeform) {
@@ -274,15 +275,15 @@ export function freeformRequestIdFromActionId(actionId: string): string | undefi
return slice.length > 0 ? slice : undefined;
}
-function buildButton(
- opt: NonNullable<InputRequest["options"]>[number],
+function buildCardButton(
+ opt: CardButtonOption,
actionId: string,
index: number,
): Record<string, unknown> {
const button: Record<string, unknown> = {
- action_id: `${actionId}:button:${index}`,
- text: { text: truncatePlainText(opt.label), type: "plain_text" },
type: "button",
+ text: { type: "plain_text", text: truncatePlainText(opt.label), emoji: false },
+ action_id: `${actionId}:button:${index}`,
value: opt.id,
};
if (opt.style === "primary" || opt.style === "danger") {
@@ -291,7 +292,7 @@ function buildButton(
return button;
}
-function buildOption(opt: NonNullable<InputRequest["options"]>[number]): Record<string, unknown> {
+function buildOption(opt: InputRequestOption): Record<string, unknown> {
const option: Record<string, unknown> = {
text: { text: truncatePlainText(opt.label), type: "plain_text" },
value: opt.id,
@@ -303,6 +304,43 @@ function buildOption(opt: NonNullable<InputRequest["options"]>[number]): Record<
return option;
}
+function renderInputRequestCardBlock(
+ request: InputRequest,
+ actionId: string,
+): Record<string, unknown> {
+ return {
+ type: "card",
+ body: {
+ type: "mrkdwn",
+ text: truncateCardBodyText(`*${request.prompt}*`),
+ verbatim: false,
+ },
+ actions: cardButtonOptions(request).map((opt, index) => buildCardButton(opt, actionId, index)),
+ };
+}
+
+function cardButtonOptions(request: InputRequest): CardButtonOption[] {
+ const options = request.options ?? [];
+ if (!isApprovalRequest(request)) return options.map(toCardButtonOption);
+
+ const approve = options.find((option) => option.id === "approve");
+ const deny = options.find((option) => option.id === "deny");
+ if (!approve || !deny) return options.map(toCardButtonOption);
+
+ return [
+ { id: deny.id, label: "Deny" },
+ { id: approve.id, label: "Allow", style: "primary" },
+ ];
+}
+
+function toCardButtonOption(option: InputRequestOption): CardButtonOption {
+ const result: CardButtonOption = { id: option.id, label: option.label };
+ if (option.style === "primary" || option.style === "danger") {
+ return { ...result, style: option.style };
+ }
+ return result;
+}
+
/**
* Renders the "answered" replacement blocks for a previously-posted
* HITL card. Preserves the original prompt/detail blocks (so context
@@ -350,6 +388,31 @@ function renderInputRequestDetailBlocks(request: InputRequest): unknown[] {
: [{ type: "section", text: { type: "mrkdwn", text: details } }];
}
+function renderToolInputContainerBlock(request: InputRequest): Record<string, unknown> | undefined {
+ const details = formatToolInputContainerText(request);
+ if (details === undefined) return undefined;
+
+ return {
+ type: "container",
+ title: { type: "plain_text", text: "Tool input" },
+ is_collapsible: true,
+ default_collapsed: false,
+ child_blocks: [{ type: "section", text: { type: "mrkdwn", text: details } }],
+ };
+}
+
+function formatToolInputContainerText(request: InputRequest): string | undefined {
+ if (!isApprovalRequest(request)) return undefined;
+
+ const json = JSON.stringify(request.action.input, null, 2);
+ if (json === "{}") return undefined;
+
+ const bodyBudget =
+ SLACK_SECTION_TEXT_MAX_LENGTH - TOOL_INPUT_CODE_PREFIX.length - TOOL_INPUT_SUFFIX.length;
+ const body = truncateWithEllipsis(json, bodyBudget);
+ return `${TOOL_INPUT_CODE_PREFIX}${body}${TOOL_INPUT_SUFFIX}`;
+}
+
function formatToolInputDetails(request: InputRequest): string | undefined {
if (!isApprovalRequest(request)) return undefined;
diff --git a/packages/eve/src/public/channels/slack/interactions.ts b/packages/eve/src/public/channels/slack/interactions.ts
index 5d41f3ff8..78112793e 100644
--- a/packages/eve/src/public/channels/slack/interactions.ts
+++ b/packages/eve/src/public/channels/slack/interactions.ts
@@ -42,6 +42,10 @@ import {
isHitlAction,
type HitlFreeformModalMetadata,
} from "#public/channels/slack/hitl.js";
+import {
+ SLACK_CARD_SUBTEXT_MAX_LENGTH,
+ truncateCardSubtext,
+} from "#public/channels/slack/limits.js";
import type {
SlackChannelConfig,
SlackChannelState,
@@ -203,6 +207,117 @@ function readPromptTextFromBlocks(blocks: readonly unknown[]): string | undefine
return typeof text === "string" && text.length > 0 ? text : undefined;
}
+function buildAnsweredHitlMessageBlocks(input: {
+ readonly actionId: string;
+ readonly answerLabel: string;
+ readonly messageBlocks: readonly unknown[];
+ readonly userId: string;
+}): unknown[] {
+ const actionBlockIndex = findActionBlockIndex(input.messageBlocks, input.actionId);
+ if (actionBlockIndex === -1) {
+ return buildAnsweredBlocks({
+ promptBlocks: findPromptBlocks(input.messageBlocks),
+ answerLabel: input.answerLabel,
+ userId: input.userId,
+ });
+ }
+
+ const actionBlock = input.messageBlocks[actionBlockIndex];
+ const answeredBlocks =
+ answeredBlocksFromActionBlock({
+ answerLabel: input.answerLabel,
+ block: actionBlock,
+ userId: input.userId,
+ }) ??
+ buildAnsweredBlocks({
+ promptBlocks: promptBlocksFromActionBlock(actionBlock),
+ answerLabel: input.answerLabel,
+ userId: input.userId,
+ });
+ return [
+ ...input.messageBlocks.slice(0, actionBlockIndex),
+ ...answeredBlocks,
+ ...input.messageBlocks.slice(actionBlockIndex + 1),
+ ];
+}
+
+function findActionBlockIndex(blocks: readonly unknown[], actionId: string): number {
+ return blocks.findIndex((block) => blockContainsActionId(block, actionId));
+}
+
+function blockContainsActionId(block: unknown, actionId: string): boolean {
+ if (!isObjectRecord(block)) return false;
+ return (
+ actionsContainActionId(block.elements, actionId) ||
+ actionsContainActionId(block.actions, actionId)
+ );
+}
+
+function actionsContainActionId(actions: unknown, actionId: string): boolean {
+ if (!Array.isArray(actions)) return false;
+ return actions.some((element) => isObjectRecord(element) && element.action_id === actionId);
+}
+
+function isObjectRecord(value: unknown): value is Record<string, unknown> {
+ return typeof value === "object" && value !== null;
+}
+
+function answeredBlocksFromActionBlock(input: {
+ readonly answerLabel: string;
+ readonly block: unknown;
+ readonly userId: string;
+}): unknown[] | undefined {
+ if (!isObjectRecord(input.block) || input.block.type !== "card") return undefined;
+
+ const { actions: _actions, subtext: _subtext, ...blockWithoutActions } = input.block;
+ const answeredCard = {
+ ...blockWithoutActions,
+ subtext: {
+ type: "mrkdwn",
+ text: formatAnsweredCardSubtext(input),
+ verbatim: false,
+ },
+ };
+ return hasCardContent(answeredCard) ? [answeredCard] : undefined;
+}
+
+const ANSWERED_CARD_SUBTEXT_PREFIX = ":white_check_mark: *";
+const ANSWERED_CARD_SUBTEXT_SUFFIX = "*";
+
+function formatAnsweredCardSubtext(input: {
+ readonly answerLabel: string;
+ readonly userId: string;
+}): string {
+ const attribution = input.userId.length > 0 ? ` by <@${input.userId}>` : "";
+ const labelBudget =
+ SLACK_CARD_SUBTEXT_MAX_LENGTH -
+ ANSWERED_CARD_SUBTEXT_PREFIX.length -
+ ANSWERED_CARD_SUBTEXT_SUFFIX.length -
+ attribution.length;
+ const label = truncateWithEllipsis(input.answerLabel, labelBudget);
+ return truncateCardSubtext(
+ `${ANSWERED_CARD_SUBTEXT_PREFIX}${label}${ANSWERED_CARD_SUBTEXT_SUFFIX}${attribution}`,
+ );
+}
+
+function truncateWithEllipsis(value: string, maxLength: number): string {
+ if (maxLength <= 0) return "";
+ if (value.length <= maxLength) return value;
+ const sliceLength = Math.max(0, maxLength - 3);
+ return `${value.slice(0, sliceLength).trimEnd()}...`;
+}
+
+function promptBlocksFromActionBlock(block: unknown): unknown[] {
+ if (!isObjectRecord(block) || block.type !== "card") return [];
+
+ const { actions: _actions, ...blockWithoutActions } = block;
+ return hasCardContent(blockWithoutActions) ? [blockWithoutActions] : [];
+}
+
+function hasCardContent(block: Record<string, unknown>): boolean {
+ return block.body !== undefined || block.title !== undefined || block.hero_image !== undefined;
+}
+
/**
* Channel-supplied dependencies for {@link handleInteractionPost}.
*
@@ -469,9 +584,10 @@ async function updateAnsweredHitlCard(
const answerLabel = hitlAction.label ?? hitlAction.selectedOptionValue ?? hitlAction.value;
if (!answerLabel) return;
- const blocks = buildAnsweredBlocks({
- promptBlocks: findPromptBlocks(interaction.messageBlocks),
+ const blocks = buildAnsweredHitlMessageBlocks({
+ actionId: hitlAction.actionId,
answerLabel,
+ messageBlocks: interaction.messageBlocks,
userId: hitlAction.user.id,
});
diff --git a/packages/eve/src/public/channels/slack/limits.test.ts b/packages/eve/src/public/channels/slack/limits.test.ts
index 14109a8eb..64d4bc174 100644
--- a/packages/eve/src/public/channels/slack/limits.test.ts
+++ b/packages/eve/src/public/channels/slack/limits.test.ts
@@ -2,10 +2,14 @@ import { describe, expect, it } from "vitest";
import {
SLACK_BLOCK_KIT_PLAIN_TEXT_MAX_LENGTH,
+ SLACK_CARD_BODY_TEXT_MAX_LENGTH,
+ SLACK_CARD_SUBTEXT_MAX_LENGTH,
SLACK_MESSAGE_TEXT_MAX_LENGTH,
SLACK_MODAL_TITLE_MAX_LENGTH,
SLACK_SECTION_TEXT_MAX_LENGTH,
SLACK_TYPING_STATUS_MAX_LENGTH,
+ truncateCardBodyText,
+ truncateCardSubtext,
truncateMessageText,
truncateModalTitle,
truncatePlainText,
@@ -97,6 +101,32 @@ describe("truncateSectionText", () => {
});
});
+describe("truncateCardBodyText", () => {
+ it("returns short strings unchanged", () => {
+ expect(truncateCardBodyText("Approve deploy?")).toBe("Approve deploy?");
+ });
+
+ it("caps long strings at the card body limit with a trailing ellipsis", () => {
+ const long = "c".repeat(SLACK_CARD_BODY_TEXT_MAX_LENGTH + 50);
+ const result = truncateCardBodyText(long);
+ expect(result.length).toBeLessThanOrEqual(SLACK_CARD_BODY_TEXT_MAX_LENGTH);
+ expect(result.endsWith("...")).toBe(true);
+ });
+});
+
+describe("truncateCardSubtext", () => {
+ it("returns short strings unchanged", () => {
+ expect(truncateCardSubtext(":white_check_mark: *Allow*")).toBe(":white_check_mark: *Allow*");
+ });
+
+ it("caps long strings at the card subtext limit with a trailing ellipsis", () => {
+ const long = "s".repeat(SLACK_CARD_SUBTEXT_MAX_LENGTH + 50);
+ const result = truncateCardSubtext(long);
+ expect(result.length).toBeLessThanOrEqual(SLACK_CARD_SUBTEXT_MAX_LENGTH);
+ expect(result.endsWith("...")).toBe(true);
+ });
+});
+
describe("truncateMessageText", () => {
it("returns short strings unchanged", () => {
expect(truncateMessageText("Pick one")).toBe("Pick one");
diff --git a/packages/eve/src/public/channels/slack/limits.ts b/packages/eve/src/public/channels/slack/limits.ts
index 47e2d8c92..d8b6e3e6e 100644
--- a/packages/eve/src/public/channels/slack/limits.ts
+++ b/packages/eve/src/public/channels/slack/limits.ts
@@ -29,6 +29,16 @@ export const SLACK_BLOCK_KIT_PLAIN_TEXT_MAX_LENGTH = 75;
*/
export const SLACK_SECTION_TEXT_MAX_LENGTH = 3000;
+/**
+ * Block Kit `card` blocks cap `body.text` at 200 chars.
+ */
+export const SLACK_CARD_BODY_TEXT_MAX_LENGTH = 200;
+
+/**
+ * Block Kit `card` blocks cap `subtext.text` at 200 chars.
+ */
+export const SLACK_CARD_SUBTEXT_MAX_LENGTH = 200;
+
/**
* Top-level `text` field on `chat.postMessage` is capped at 40000 chars.
*/
@@ -77,6 +87,22 @@ export function truncateSectionText(value: string): string {
return truncateWithEllipsis(value, SLACK_SECTION_TEXT_MAX_LENGTH);
}
+/**
+ * Caps a card block's `body.text` at the Slack limit with a trailing
+ * ellipsis.
+ */
+export function truncateCardBodyText(value: string): string {
+ return truncateWithEllipsis(value, SLACK_CARD_BODY_TEXT_MAX_LENGTH);
+}
+
+/**
+ * Caps a card block's `subtext.text` at the Slack limit with a trailing
+ * ellipsis.
+ */
+export function truncateCardSubtext(value: string): string {
+ return truncateWithEllipsis(value, SLACK_CARD_SUBTEXT_MAX_LENGTH);
+}
+
/**
* Caps a `chat.postMessage` `text` field at the Slack limit with a
* trailing ellipsis.
diff --git a/packages/eve/src/public/channels/slack/slackChannel.test.ts b/packages/eve/src/public/channels/slack/slackChannel.test.ts
index 73e9e6562..68a30dd0c 100644
--- a/packages/eve/src/public/channels/slack/slackChannel.test.ts
+++ b/packages/eve/src/public/channels/slack/slackChannel.test.ts
@@ -17,6 +17,8 @@ import {
HITL_FREEFORM_MODAL_CALLBACK_ID,
} from "#public/channels/slack/hitl.js";
import {
+ SLACK_CARD_BODY_TEXT_MAX_LENGTH,
+ SLACK_CARD_SUBTEXT_MAX_LENGTH,
SLACK_MAX_BLOCKS_PER_MESSAGE,
SLACK_MESSAGE_TEXT_MAX_LENGTH,
SLACK_SECTION_TEXT_MAX_LENGTH,
@@ -426,7 +428,18 @@ describe("slackChannel() default event handlers", () => {
const [url, init] = fetchMock.mock.calls[0]!;
expect(String(url)).toBe("https://slack.com/api/chat.postMessage");
const body = parseSlackRequestBody(init as RequestInit) as {
- blocks: Array<{ elements?: Array<{ action_id: string; value: string }> }>;
+ blocks: Array<{
+ actions?: Array<{
+ action_id: string;
+ style?: string;
+ text: { emoji?: boolean; text: string; type: string };
+ value: string;
+ }>;
+ body?: { text: string; type: string; verbatim?: boolean };
+ child_blocks?: Array<{ text?: { text?: string; type?: string }; type: string }>;
+ title?: { text: string; type: string };
+ type: string;
+ }>;
channel: string;
text: string;
thread_ts: string;
@@ -437,13 +450,51 @@ describe("slackChannel() default event handlers", () => {
thread_ts: "1700000000.000001",
});
- const actions = body.blocks.find((block) => Array.isArray(block.elements));
- const actionIds = actions?.elements?.map((element) => element.action_id) ?? [];
+ expect(body.blocks).toHaveLength(2);
+ const [card, details] = body.blocks;
+ expect(card).toMatchObject({
+ type: "card",
+ body: {
+ type: "mrkdwn",
+ text: "*Approve tool call: mongodb-mutate*",
+ verbatim: false,
+ },
+ });
+ expect(card?.body?.text.length).toBeLessThanOrEqual(SLACK_CARD_BODY_TEXT_MAX_LENGTH);
+ expect(details).toMatchObject({
+ type: "container",
+ title: { type: "plain_text", text: "Tool input" },
+ is_collapsible: true,
+ default_collapsed: false,
+ child_blocks: [
+ {
+ type: "section",
+ text: {
+ type: "mrkdwn",
+ text: '```\n{\n "operation": "deleteMany"\n}\n```',
+ },
+ },
+ ],
+ });
+
+ const actions = card?.actions ?? [];
+ const actionIds = actions.map((element) => element.action_id);
expect(actionIds).toEqual([
`${HITL_ACTION_PREFIX}approval_abc123:button:0`,
`${HITL_ACTION_PREFIX}approval_abc123:button:1`,
]);
expect(new Set(actionIds).size).toBe(actionIds.length);
+ expect(actions).toMatchObject([
+ {
+ text: { type: "plain_text", text: "Deny", emoji: false },
+ value: "deny",
+ },
+ {
+ style: "primary",
+ text: { type: "plain_text", text: "Allow", emoji: false },
+ value: "approve",
+ },
+ ]);
});
it("input.requested caps section and fallback text so Slack does not reject the post", async () => {
@@ -496,9 +547,10 @@ describe("slackChannel() default event handlers", () => {
);
const ctx = buildAdapterContext(adapter, stubAccessor());
- // 20 approval requests render 3 blocks each (prompt, tool input,
- // actions) = 60 blocks, which must split across two posts.
- const requests = Array.from({ length: 20 }, (_, index) => ({
+ // Approval requests with tool input render as a card plus a tool-input
+ // container. 60 requests must split across three posts to stay below
+ // Slack's 50-block message cap.
+ const requests = Array.from({ length: 60 }, (_, index) => ({
action: {
callId: `call_${index}`,
input: { operation: "deleteMany" },
@@ -520,22 +572,26 @@ describe("slackChannel() default event handlers", () => {
ctx,
);
- expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(fetchMock).toHaveBeenCalledTimes(3);
const allRequestIds: string[] = [];
for (const [url, init] of fetchMock.mock.calls) {
expect(String(url)).toBe("https://slack.com/api/chat.postMessage");
const body = parseSlackRequestBody(init as RequestInit) as {
- blocks: Array<{ type: string; elements?: Array<{ action_id: string }> }>;
+ blocks: Array<{
+ type: string;
+ actions?: Array<{ action_id: string }>;
+ elements?: Array<{ action_id: string }>;
+ }>;
};
expect(body.blocks.length).toBeLessThanOrEqual(SLACK_MAX_BLOCKS_PER_MESSAGE);
for (const block of body.blocks) {
- for (const element of block.elements ?? []) {
+ for (const element of block.actions ?? block.elements ?? []) {
const requestId = element.action_id.replace(/^.*:(approval_\d+):button:\d+$/u, "$1");
if (!allRequestIds.includes(requestId)) allRequestIds.push(requestId);
}
}
}
- expect(allRequestIds).toHaveLength(20);
+ expect(allRequestIds).toHaveLength(60);
});
it("turn.started calls assistant.threads.setStatus", async () => {
@@ -1501,6 +1557,281 @@ describe("slackChannel() HITL interaction pipeline", () => {
});
});
+ it("updates one answered HITL card without removing sibling batched buttons", async () => {
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+
+ const firstDenyActionId = `${HITL_ACTION_PREFIX}approval_451:button:0`;
+ const firstApproveActionId = `${HITL_ACTION_PREFIX}approval_451:button:1`;
+ const secondDenyActionId = `${HITL_ACTION_PREFIX}approval_508:button:0`;
+ const secondApproveActionId = `${HITL_ACTION_PREFIX}approval_508:button:1`;
+
+ const { send } = await firePost(
+ channel,
+ buildSignedInteractionRequest({
+ type: "block_actions",
+ team: { id: "T01" },
+ user: {
+ id: "U_APPROVER",
+ username: "ada",
+ name: "ada",
+ team_id: "T01",
+ },
+ channel: { id: "C01" },
+ message: {
+ ts: "1700000000.000010",
+ thread_ts: "1700000000.000001",
+ blocks: [
+ {
+ type: "card",
+ body: {
+ type: "mrkdwn",
+ text: "*Approve issue 451?*",
+ verbatim: false,
+ },
+ actions: [
+ {
+ type: "button",
+ action_id: firstDenyActionId,
+ text: { type: "plain_text", text: "Deny" },
+ value: "deny",
+ },
+ {
+ type: "button",
+ action_id: firstApproveActionId,
+ text: { type: "plain_text", text: "Allow" },
+ value: "approve",
+ },
+ ],
+ },
+ {
+ type: "container",
+ title: { type: "plain_text", text: "Tool input" },
+ is_collapsible: true,
+ default_collapsed: false,
+ child_blocks: [
+ { type: "section", text: { type: "mrkdwn", text: '```\n{"issue":451}\n```' } },
+ ],
+ },
+ {
+ type: "card",
+ body: {
+ type: "mrkdwn",
+ text: "*Approve issue 508?*",
+ verbatim: false,
+ },
+ actions: [
+ {
+ type: "button",
+ action_id: secondDenyActionId,
+ text: { type: "plain_text", text: "Deny" },
+ value: "deny",
+ },
+ {
+ type: "button",
+ action_id: secondApproveActionId,
+ text: { type: "plain_text", text: "Allow" },
+ value: "approve",
+ },
+ ],
+ },
+ {
+ type: "container",
+ title: { type: "plain_text", text: "Tool input" },
+ is_collapsible: true,
+ default_collapsed: false,
+ child_blocks: [
+ { type: "section", text: { type: "mrkdwn", text: '```\n{"issue":508}\n```' } },
+ ],
+ },
+ ],
+ },
+ actions: [
+ {
+ action_id: firstApproveActionId,
+ text: { type: "plain_text", text: "Allow" },
+ value: "approve",
+ },
+ ],
+ }),
+ );
+
+ expect(send).toHaveBeenCalledTimes(1);
+ expect(send.mock.calls[0]?.[0]).toEqual({
+ inputResponses: [{ optionId: "approve", requestId: "approval_451" }],
+ });
+
+ const updateCall = fetchMock.mock.calls.find(
+ ([url]) => String(url) === "https://slack.com/api/chat.update",
+ );
+ expect(updateCall).toBeDefined();
+ const body = parseSlackRequestBody(updateCall?.[1] as RequestInit) as {
+ blocks: Array<{
+ actions?: Array<{ action_id?: string }>;
+ elements?: Array<{ action_id?: string }>;
+ subtext?: { text?: string };
+ text?: { text?: string };
+ }>;
+ channel: string;
+ text: string;
+ ts: string;
+ };
+
+ expect(body).toMatchObject({
+ channel: "C01",
+ text: "Answered: Allow",
+ ts: "1700000000.000010",
+ });
+ expect(JSON.stringify(body.blocks)).toContain("Approve issue 451?");
+ expect(JSON.stringify(body.blocks)).toContain("Allow");
+ expect(JSON.stringify(body.blocks)).toContain("Tool input");
+ expect(JSON.stringify(body.blocks)).toContain("Approve issue 508?");
+ expect(body.blocks[0]?.subtext?.text).toBe(":white_check_mark: *Allow* by <@U_APPROVER>");
+ expect(body.blocks[0]?.subtext?.text?.length).toBeLessThanOrEqual(
+ SLACK_CARD_SUBTEXT_MAX_LENGTH,
+ );
+
+ const remainingActionIds = body.blocks.flatMap(
+ (block) =>
+ (block.actions ?? block.elements)
+ ?.map((element) => element.action_id)
+ .filter((actionId): actionId is string => typeof actionId === "string") ?? [],
+ );
+ expect(remainingActionIds).toEqual([secondDenyActionId, secondApproveActionId]);
+ });
+
+ it("covers the observed e0 batched escalation approval run", async () => {
+ const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });
+ const adapter = withState(getAdapter(channel), THREAD_STATE);
+ const ctx = buildAdapterContext(adapter, stubAccessor());
+
+ await callEvent(
+ adapter,
+ makeEvent("input.requested", {
+ requests: [
+ {
+ action: {
+ callId: "toolu_escalate_451",
+ input: { issueNumber: 451, ownerSlackUserId: "U0AT7H56S90" },
+ kind: "tool-call",
+ toolName: "escalate_issue",
+ },
+ display: "confirmation",
+ options: [
+ { id: "approve", label: "Yes" },
+ { id: "deny", label: "No" },
+ ],
+ prompt: "Approve tool call: escalate_issue",
+ requestId: "approval_451",
+ },
+ {
+ action: {
+ callId: "toolu_escalate_508",
+ input: { issueNumber: 508 },
+ kind: "tool-call",
+ toolName: "escalate_issue",
+ },
+ display: "confirmation",
+ options: [
+ { id: "approve", label: "Yes" },
+ { id: "deny", label: "No" },
+ ],
+ prompt: "Approve tool call: escalate_issue",
+ requestId: "approval_508",
+ },
+ ],
+ sequence: 0,
+ stepIndex: 0,
+ turnId: "turn_0",
+ }),
+ ctx,
+ );
+
+ const postCall = fetchMock.mock.calls.find(
+ ([url]) => String(url) === "https://slack.com/api/chat.postMessage",
+ );
+ expect(postCall).toBeDefined();
+ const posted = parseSlackRequestBody(postCall?.[1] as RequestInit) as {
+ blocks: Array<{
+ actions?: Array<{ action_id?: string; text?: { text?: string }; value?: string }>;
+ child_blocks?: Array<{ text?: { text?: string } }>;
+ title?: { text?: string };
+ type?: string;
+ }>;
+ };
+ expect(posted.blocks).toHaveLength(4);
+ expect(posted.blocks[1]?.child_blocks?.[0]?.text?.text).toContain('"issueNumber": 451');
+ expect(posted.blocks[1]?.child_blocks?.[0]?.text?.text).toContain(
+ '"ownerSlackUserId": "U0AT7H56S90"',
+ );
+ expect(posted.blocks[3]?.child_blocks?.[0]?.text?.text).toContain('"issueNumber": 508');
+
+ const firstDenyAction = posted.blocks[0]?.actions?.find((action) => action.value === "deny");
+ expect(firstDenyAction).toMatchObject({
+ action_id: `${HITL_ACTION_PREFIX}approval_451:button:0`,
+ text: { text: "Deny" },
+ value: "deny",
+ });
+
+ const { send } = await firePost(
+ channel,
+ buildSignedInteractionRequest({
+ type: "block_actions",
+ team: { id: "T01" },
+ user: {
+ id: "U0AT7H56S90",
+ username: "rui",
+ name: "rui",
+ team_id: "T01",
+ },
+ channel: { id: "C01" },
+ message: {
+ ts: "1700000000.000010",
+ thread_ts: "1700000000.000001",
+ blocks: posted.blocks,
+ },
+ actions: [
+ {
+ action_id: firstDenyAction?.action_id,
+ text: { type: "plain_text", text: "Deny" },
+ value: "deny",
+ },
+ ],
+ }),
+ );
+
+ expect(send).toHaveBeenCalledTimes(1);
+ expect(send.mock.calls[0]?.[0]).toEqual({
+ inputResponses: [{ optionId: "deny", requestId: "approval_451" }],
+ });
+
+ const updateCall = fetchMock.mock.calls.find(
+ ([url]) => String(url) === "https://slack.com/api/chat.update",
+ );
+ expect(updateCall).toBeDefined();
+ const body = parseSlackRequestBody(updateCall?.[1] as RequestInit) as {
+ blocks: Array<{
+ actions?: Array<{ action_id?: string }>;
+ child_blocks?: Array<{ text?: { text?: string } }>;
+ elements?: Array<{ action_id?: string }>;
+ subtext?: { text?: string };
+ }>;
+ };
+
+ expect(body.blocks[0]?.subtext?.text).toBe(":white_check_mark: *Deny* by <@U0AT7H56S90>");
+ expect(body.blocks[1]?.child_blocks?.[0]?.text?.text).toContain('"issueNumber": 451');
+ expect(body.blocks[3]?.child_blocks?.[0]?.text?.text).toContain('"issueNumber": 508');
+ const remainingActionIds = body.blocks.flatMap(
+ (block) =>
+ (block.actions ?? block.elements)
+ ?.map((element) => element.action_id)
+ .filter((actionId): actionId is string => typeof actionId === "string") ?? [],
+ );
+ expect(remainingActionIds).toEqual([
+ `${HITL_ACTION_PREFIX}approval_508:button:0`,
+ `${HITL_ACTION_PREFIX}approval_508:button:1`,
+ ]);
+ });
+
it("resumes freeform modal answers with the submitting Slack user auth", async () => {
const channel = slackChannel({ credentials: { botToken: "xoxb-test" } });