|
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import { eq } from 'drizzle-orm'; |
| 3 | + |
| 4 | +import { db, cloudJobs } from '@roo-code-cloud/db/server'; |
| 5 | + |
| 6 | +import { SlackNotifier } from '@/lib/slack'; |
| 7 | + |
| 8 | +import { createAndEnqueueJob } from '../github/handlers/utils'; |
| 9 | + |
| 10 | +const mentionedThreads = new Set<string>(); |
| 11 | +const pendingWorkspaceSelections = new Map<string, SlackEvent>(); |
| 12 | +const slack = new SlackNotifier(); |
| 13 | + |
| 14 | +interface SlackEvent { |
| 15 | + type: string; |
| 16 | + channel: string; |
| 17 | + user: string; |
| 18 | + text: string; |
| 19 | + ts: string; |
| 20 | + thread_ts?: string; |
| 21 | + bot_id?: string; |
| 22 | + app_id?: string; |
| 23 | +} |
| 24 | + |
| 25 | +interface SlackInteractivePayload { |
| 26 | + type: string; |
| 27 | + user: { |
| 28 | + id: string; |
| 29 | + name: string; |
| 30 | + }; |
| 31 | + channel: { |
| 32 | + id: string; |
| 33 | + name: string; |
| 34 | + }; |
| 35 | + message: { |
| 36 | + ts: string; |
| 37 | + thread_ts?: string; |
| 38 | + }; |
| 39 | + actions: Array<{ |
| 40 | + action_id: string; |
| 41 | + value: string; |
| 42 | + text: { |
| 43 | + text: string; |
| 44 | + }; |
| 45 | + }>; |
| 46 | + response_url: string; |
| 47 | + trigger_id: string; |
| 48 | +} |
| 49 | + |
| 50 | +interface SlackWebhookBody { |
| 51 | + type: string; |
| 52 | + challenge?: string; |
| 53 | + event?: SlackEvent; |
| 54 | + team_id?: string; |
| 55 | + payload?: string; // For interactive payloads. |
| 56 | +} |
| 57 | + |
| 58 | +export async function POST(request: NextRequest) { |
| 59 | + const contentType = request.headers.get('content-type'); |
| 60 | + |
| 61 | + if (contentType?.includes('application/x-www-form-urlencoded')) { |
| 62 | + const formData = await request.formData(); |
| 63 | + const payload = formData.get('payload') as string; |
| 64 | + |
| 65 | + if (payload) { |
| 66 | + const interactivePayload: SlackInteractivePayload = JSON.parse(payload); |
| 67 | + await handleInteractivePayload(interactivePayload); |
| 68 | + return NextResponse.json({ ok: true }); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + const body: SlackWebhookBody = JSON.parse(await request.text()); |
| 73 | + |
| 74 | + if (body.type === 'url_verification') { |
| 75 | + console.log('🔐 Slack URL verification challenge received'); |
| 76 | + return NextResponse.json({ challenge: body.challenge }); |
| 77 | + } |
| 78 | + |
| 79 | + if (body.type === 'event_callback' && body.event) { |
| 80 | + const event = body.event; |
| 81 | + |
| 82 | + if (event.bot_id || event.app_id) { |
| 83 | + return NextResponse.json({ ok: true }); |
| 84 | + } |
| 85 | + |
| 86 | + console.log('🛎️ Slack Event ->', { |
| 87 | + type: event.type, |
| 88 | + channel: event.channel, |
| 89 | + user: event.user, |
| 90 | + text: event.text?.substring(0, 100), |
| 91 | + thread_ts: event.thread_ts, |
| 92 | + ts: event.ts, |
| 93 | + }); |
| 94 | + |
| 95 | + switch (event.type) { |
| 96 | + case 'app_mention': |
| 97 | + await handleAppMention(event); |
| 98 | + break; |
| 99 | + |
| 100 | + case 'message': |
| 101 | + await handleMessage(event); |
| 102 | + break; |
| 103 | + |
| 104 | + default: |
| 105 | + console.log(`Unhandled event type: ${event.type}`); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return NextResponse.json({ ok: true }); |
| 110 | +} |
| 111 | + |
| 112 | +async function handleInteractivePayload(payload: SlackInteractivePayload) { |
| 113 | + console.log('🎯 Interactive payload received:', { |
| 114 | + type: payload.type, |
| 115 | + user: payload.user.id, |
| 116 | + channel: payload.channel.id, |
| 117 | + action: payload.actions[0]?.action_id, |
| 118 | + value: payload.actions[0]?.value, |
| 119 | + }); |
| 120 | + |
| 121 | + if ( |
| 122 | + payload.actions[0]?.action_id === 'select_roo_code_cloud' || |
| 123 | + payload.actions[0]?.action_id === 'select_roo_code' |
| 124 | + ) { |
| 125 | + const workspace = payload.actions[0].value; |
| 126 | + const threadId = payload.message.thread_ts || payload.message.ts; |
| 127 | + |
| 128 | + try { |
| 129 | + const originalEvent = pendingWorkspaceSelections.get(threadId); |
| 130 | + |
| 131 | + if (!originalEvent) { |
| 132 | + throw new Error('Original mention event not found'); |
| 133 | + } |
| 134 | + |
| 135 | + pendingWorkspaceSelections.delete(threadId); |
| 136 | + |
| 137 | + const { jobId, enqueuedJobId } = await createAndEnqueueJob( |
| 138 | + 'slack.app.mention', |
| 139 | + { |
| 140 | + channel: originalEvent.channel, |
| 141 | + user: originalEvent.user, |
| 142 | + text: originalEvent.text, |
| 143 | + ts: originalEvent.ts, |
| 144 | + thread_ts: threadId, |
| 145 | + workspace, |
| 146 | + }, |
| 147 | + ); |
| 148 | + |
| 149 | + console.log( |
| 150 | + `🔗 Enqueued slack.app.mention job for workspace ${workspace} (id: ${jobId}, enqueued: ${enqueuedJobId})`, |
| 151 | + ); |
| 152 | + |
| 153 | + await slack.postMessage({ |
| 154 | + text: `✅ Enqueued job ${enqueuedJobId} for workspace ${workspace}.`, |
| 155 | + channel: payload.channel.id, |
| 156 | + thread_ts: threadId, |
| 157 | + }); |
| 158 | + |
| 159 | + await db |
| 160 | + .update(cloudJobs) |
| 161 | + .set({ slackThreadTs: threadId }) |
| 162 | + .where(eq(cloudJobs.id, jobId)); |
| 163 | + } catch (error) { |
| 164 | + console.error('❌ Failed to process workspace selection:', error); |
| 165 | + |
| 166 | + await slack.postMessage({ |
| 167 | + text: `❌ Sorry, something went wrong processing your request. Please try again.`, |
| 168 | + channel: payload.channel.id, |
| 169 | + thread_ts: threadId, |
| 170 | + }); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +async function handleAppMention(event: SlackEvent) { |
| 176 | + console.log('🤖 Bot mentioned in channel:', event.channel); |
| 177 | + const threadId = event.thread_ts || event.ts; |
| 178 | + mentionedThreads.add(threadId); |
| 179 | + console.log(`📌 Tracking thread: ${threadId}`); |
| 180 | + |
| 181 | + try { |
| 182 | + pendingWorkspaceSelections.set(threadId, event); |
| 183 | + |
| 184 | + const result = await slack.postMessage({ |
| 185 | + text: '👋 Which workspace would you like me to work in?', |
| 186 | + channel: event.channel, |
| 187 | + thread_ts: threadId, |
| 188 | + blocks: [ |
| 189 | + { |
| 190 | + type: 'section', |
| 191 | + text: { |
| 192 | + type: 'mrkdwn', |
| 193 | + text: '👋 Which workspace would you like me to work in?', |
| 194 | + }, |
| 195 | + }, |
| 196 | + { |
| 197 | + type: 'actions', |
| 198 | + elements: [ |
| 199 | + { |
| 200 | + type: 'button', |
| 201 | + text: { type: 'plain_text', text: 'Roo-Code-Cloud', emoji: true }, |
| 202 | + action_id: 'select_roo_code_cloud', |
| 203 | + value: 'roo-code-cloud', |
| 204 | + }, |
| 205 | + { |
| 206 | + type: 'button', |
| 207 | + text: { type: 'plain_text', text: 'Roo-Code', emoji: true }, |
| 208 | + action_id: 'select_roo_code', |
| 209 | + value: 'roo-code', |
| 210 | + }, |
| 211 | + ], |
| 212 | + }, |
| 213 | + ], |
| 214 | + }); |
| 215 | + |
| 216 | + console.log(`✅ Sent workspace selection to thread: ${threadId}`, result); |
| 217 | + } catch (error) { |
| 218 | + console.error('❌ Failed to process app mention:', error); |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +async function handleMessage(event: SlackEvent) { |
| 223 | + if (!event.thread_ts || !mentionedThreads.has(event.thread_ts)) { |
| 224 | + return; |
| 225 | + } |
| 226 | + |
| 227 | + console.log('💬 New message in tracked thread:', { |
| 228 | + thread: event.thread_ts, |
| 229 | + channel: event.channel, |
| 230 | + user: event.user, |
| 231 | + text: event.text?.substring(0, 100), |
| 232 | + }); |
| 233 | + |
| 234 | + // TODO: Process the thread message. |
| 235 | + // This is where you'd handle follow-up messages in the thread. |
| 236 | +} |
0 commit comments