From b8b5954c0d4b29ba94085b97c15e115210206e2b Mon Sep 17 00:00:00 2001 From: Marcus Chandra Date: Fri, 13 Nov 2026 14:11:21 -0800 Subject: [PATCH] fix(slack): stream a workflow reply into the thread of a clicked button A Slack trigger on block_actions could not stream its run's reply. Streaming was limited to message and app_mention, and interaction payloads never set user_team_id, which a streamed reply in a channel needs as its recipient team. Allow block_actions as a streaming event and normalize the clicking user's own team (user.team_id, falling back to the installation team) into user_team_id. The reply streams into the clicked message's thread. --- apps/sim/lib/webhooks/providers/slack.test.ts | 35 ++++++++++++++++ apps/sim/lib/webhooks/providers/slack.ts | 4 ++ .../webhooks/slack-execution-stream.test.ts | 40 +++++++++++++++++++ .../lib/webhooks/slack-stream-config.test.ts | 11 ++++- apps/sim/triggers/slack/oauth.ts | 2 +- apps/sim/triggers/slack/shared.ts | 6 ++- 6 files changed, 95 insertions(+), 3 deletions(-) diff --git a/apps/sim/lib/webhooks/providers/slack.test.ts b/apps/sim/lib/webhooks/providers/slack.test.ts index f38a5a0387c..567dcf02998 100644 --- a/apps/sim/lib/webhooks/providers/slack.test.ts +++ b/apps/sim/lib/webhooks/providers/slack.test.ts @@ -202,6 +202,41 @@ describe('slackHandler formatInput - interactivity (block_actions)', () => { expect(event.text).toBe('opt_b') expect(event.user_name).toBe('bob') }) + + it('maps a threaded button click to its thread and the clicking user team', async () => { + const { input } = await slackHandler.formatInput!( + ctx({ + type: 'block_actions', + user: { id: 'U3', username: 'carol', team_id: 'T-external' }, + team: { id: 'T-install' }, + channel: { id: 'C42', name: 'incidents' }, + container: { type: 'message', message_ts: '1700000002.000300' }, + message: { ts: '1700000002.000300', thread_ts: '1700000000.000100', text: '' }, + actions: [{ action_id: 'followup_0', type: 'button', value: 'Check the NAT gateway' }], + }) + ) + const event = eventOf(input) + expect(event.channel).toBe('C42') + expect(event.thread_ts).toBe('1700000000.000100') + expect(event.message_ts).toBe('1700000002.000300') + expect(event.team_id).toBe('T-install') + expect(event.user_team_id).toBe('T-external') + expect(event.action_id).toBe('followup_0') + expect(event.action_value).toBe('Check the NAT gateway') + }) + + it('falls back to the installation team when the user carries no team', async () => { + const { input } = await slackHandler.formatInput!( + ctx({ + type: 'block_actions', + user: { id: 'U4', username: 'dave' }, + team: { id: 'T-install' }, + channel: { id: 'C42' }, + actions: [{ action_id: 'followup_1', type: 'button', value: 'next' }], + }) + ) + expect(eventOf(input).user_team_id).toBe('T-install') + }) }) describe('slackHandler formatInput - block_suggestion', () => { diff --git a/apps/sim/lib/webhooks/providers/slack.ts b/apps/sim/lib/webhooks/providers/slack.ts index b103879edc0..35864f4b3ec 100644 --- a/apps/sim/lib/webhooks/providers/slack.ts +++ b/apps/sim/lib/webhooks/providers/slack.ts @@ -241,6 +241,9 @@ function formatSlackSlashCommand(b: Record): SlackTriggerEvent * `trigger_id` which are needed to respond to or follow up on the interaction. * `text` prefers the source message text, falling back to the triggering * action's value so a blocks-only message still surfaces something useful. + * `user_team_id` is the interacting user's own team (`user.team_id`), which a + * streamed reply needs as its recipient team; in Slack Connect it can differ + * from the installation team, and it is never used for routing. */ function formatSlackInteractive(b: Record): SlackTriggerEvent { const event = createSlackEvent() @@ -262,6 +265,7 @@ function formatSlackInteractive(b: Record): SlackTriggerEvent { const team = b.team as Record | undefined event.team_id = asString(team?.id) || asString(user?.team_id) + event.user_team_id = asString(user?.team_id) || event.team_id const container = b.container as Record | undefined const message = b.message as Record | undefined diff --git a/apps/sim/lib/webhooks/slack-execution-stream.test.ts b/apps/sim/lib/webhooks/slack-execution-stream.test.ts index 765d088719b..234418478cc 100644 --- a/apps/sim/lib/webhooks/slack-execution-stream.test.ts +++ b/apps/sim/lib/webhooks/slack-execution-stream.test.ts @@ -33,6 +33,7 @@ vi.mock('@/lib/webhooks/slack-stream-sessions', () => ({ unregisterSlackStreamSession: mockUnregisterSlackStreamSession, })) +import { slackHandler } from '@/lib/webhooks/providers/slack' import type { SlackStreamChunk } from '@/lib/webhooks/slack-agent-api' import { SlackDeliveryError } from '@/lib/webhooks/slack-delivery-error' import { SlackExecutionStreamController } from '@/lib/webhooks/slack-execution-stream' @@ -729,6 +730,45 @@ describe('SlackExecutionStreamController', () => { controller.assertSucceeded() }) + it('streams a button click into the thread of the clicked message for the clicking user', async () => { + const { input } = await slackHandler.formatInput!({ + webhook: {}, + workflow: { id: 'workflow-1', userId: 'user-1' }, + body: { + type: 'block_actions', + user: { id: 'U9', username: 'erin', team_id: 'T9' }, + team: { id: 'T-install' }, + channel: { id: 'C777' }, + message: { ts: '1700000005.000500', thread_ts: '1700000000.000100', text: '' }, + actions: [{ action_id: 'followup_0', type: 'button', value: 'Check the NAT gateway' }], + }, + headers: {}, + requestId: 'slack-click-test', + }) + + await createController(BASE_CONFIG, input as Record) + + const target = { + channel: 'C777', + threadTs: '1700000000.000100', + initiatorUserId: 'U9', + recipientUserId: 'U9', + recipientTeamId: 'T9', + } + expect(mockRegisterSlackStreamSession).toHaveBeenCalledWith('cred-1', target, { + executionId: 'execution-1', + workflowId: 'workflow-1', + userId: 'user-1', + workspaceId: 'workspace-1', + }) + expect(mockSetSlackAgentSessionStatus).toHaveBeenCalledWith( + 'xoxb-token', + target, + 'processing', + undefined + ) + }) + it('rejects credentials that do not belong to the workflow workspace', async () => { mockGetSlackBotCredential.mockResolvedValue({ botToken: 'xoxb-token', diff --git a/apps/sim/lib/webhooks/slack-stream-config.test.ts b/apps/sim/lib/webhooks/slack-stream-config.test.ts index 978fb43f8d9..6be78fb223c 100644 --- a/apps/sim/lib/webhooks/slack-stream-config.test.ts +++ b/apps/sim/lib/webhooks/slack-stream-config.test.ts @@ -66,7 +66,16 @@ describe('Slack stream response config', () => { ).toBe('Running') }) - it.each(['reaction_added', 'assistant_thread_started'])( + it('accepts button clicks as a reply-capable streaming event', () => { + expect( + normalizeSlackStreamResponseConfig( + { eventType: 'block_actions', streamResponse: true, streamOutputs: ['agent.content'] }, + { 'block-1': { id: 'block-1', name: 'Agent' } } + )?.outputConfigs + ).toEqual([{ blockId: 'block-1', path: 'content' }]) + }) + + it.each(['reaction_added', 'assistant_thread_started', 'view_submission'])( 'rejects unsupported streaming event %s and malformed output selectors', (eventType) => { expect(() => diff --git a/apps/sim/triggers/slack/oauth.ts b/apps/sim/triggers/slack/oauth.ts index d043d329a1f..d0babe87fcb 100644 --- a/apps/sim/triggers/slack/oauth.ts +++ b/apps/sim/triggers/slack/oauth.ts @@ -178,7 +178,7 @@ export const slackOAuthTrigger: TriggerConfig = { type: 'switch', defaultValue: false, description: - 'Create a Slack agent session and stream selected workflow outputs into the conversation that started this run. Custom bots only.', + 'Create a Slack agent session and stream selected workflow outputs into the conversation that started this run (for a button click, the thread of the clicked message). Custom bots only.', required: false, mode: 'trigger', condition: { field: 'eventType', value: SLACK_STREAM_RESPONSE_EVENTS }, diff --git a/apps/sim/triggers/slack/shared.ts b/apps/sim/triggers/slack/shared.ts index cf61c16c801..b8e428ec109 100644 --- a/apps/sim/triggers/slack/shared.ts +++ b/apps/sim/triggers/slack/shared.ts @@ -8,7 +8,11 @@ export const SLACK_AGENT_SCOPES = [ 'im:write', ] as const -export const SLACK_STREAM_RESPONSE_EVENTS = ['message', 'app_mention'] +/** + * Trigger events whose run can stream a reply into the conversation that + * started it. A `block_actions` click streams into the clicked message's thread. + */ +export const SLACK_STREAM_RESPONSE_EVENTS = ['message', 'app_mention', 'block_actions'] /** * Unified Slack trigger output shape, shared by the legacy bring-your-own-app