Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions apps/api/src/handlers/discord/__tests__/index.test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions apps/api/src/handlers/discord/__tests__/task-launch.test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 68 additions & 2 deletions apps/api/src/handlers/discord/callback-actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { activeRunStatuses } from '@roomote/types';
import {
activeRunStatuses,
type QueuedCommunicationMessage,
} from '@roomote/types';
import {
and,
db,
Expand All @@ -14,7 +17,6 @@ import {
import type { DiscordInteraction } from '@roomote/communication/discord-event';
import type { DiscordCommunicationProvider } from '@roomote/communication/discord-provider';
import { findDiscordMappedUserId } from '@roomote/sdk/server';
import type { QueuedCommunicationMessage } from '@roomote/types';

import { apiLogger } from '../../logging.js';
import { cancelOrphanedWorkItemRunBestEffort } from '../tasks/orphaned-work-item-run.js';
Expand All @@ -32,6 +34,9 @@ import {
import { claimDiscordSuggestionLaunch } from './setup-suggestions.js';
import { startNewDiscordTask } from './task-orchestration.js';

/** Match Slack cancel reaction (`DEFAULT_SLACK_CANCEL_EMOJI`). */
const DISCORD_CANCEL_REACTION_EMOJI = 'x';

function parseCancelCallbackData(value: string | undefined): number | null {
const match = /^discord:cancel:(\d+)$/u.exec(value ?? '');
if (!match) return null;
Expand Down Expand Up @@ -60,13 +65,68 @@ async function findCancelableDiscordRun(runId: number, channelId: string) {
status: true,
sandboxServerUrl: true,
actingUserId: true,
payload: true,
},
with: {
task: { columns: { initiatorUserId: true } },
},
});
}

function getDiscordCancelReactionTarget(payload: unknown): {
channelId: string;
messageId: string;
} | null {
if (!payload || typeof payload !== 'object') {
return null;
}

const channelId =
typeof (payload as { discordReactionChannelId?: unknown })
.discordReactionChannelId === 'string'
? (
payload as { discordReactionChannelId: string }
).discordReactionChannelId.trim()
: '';
const messageId =
typeof (payload as { discordReactionMessageId?: unknown })
.discordReactionMessageId === 'string'
? (
payload as { discordReactionMessageId: string }
).discordReactionMessageId.trim()
: '';

if (!channelId || !messageId) {
return null;
}

return { channelId, messageId };
}

async function addDiscordCancelReactionBestEffort(input: {
provider: DiscordCommunicationProvider;
payload: unknown;
}): Promise<void> {
const target = getDiscordCancelReactionTarget(input.payload);
if (!target) {
return;
}

try {
await input.provider.addReaction({
channelId: target.channelId,
messageId: target.messageId,
name: DISCORD_CANCEL_REACTION_EMOJI,
});
} catch (error) {
apiLogger.warn(
`[discord] Failed to add cancel reaction for channel ${target.channelId} message ${target.messageId}: ${
error instanceof Error ? error.message : String(error)
}`,
);
}
}

async function handleCancelCallback(input: {
provider: DiscordCommunicationProvider;
applicationId: string;
Expand Down Expand Up @@ -138,6 +198,12 @@ async function handleCancelCallback(input: {
});
const canceled =
result.success || result.statusCode === 404 || result.statusCode === 409;
if (canceled) {
await addDiscordCancelReactionBestEffort({
provider: input.provider,
payload: run.payload,
});
}
await replyToDiscordEvent({
provider: input.provider,
applicationId: input.applicationId,
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/handlers/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,21 @@ async function processDiscordGatewayEvent(event: DiscordGatewayEvent) {
return { ok: true, resumed: true, runId: resumed.id };
}

// Match Slack intake: ack the origin message with πŸ‘€ before launch work.
// Only real MESSAGE_CREATE messages can receive Discord reactions; slash-
// command interaction ids are not message targets and would 404.
if (message?.id) {
try {
await resolved.provider.addReaction({
channelId: channel.channelId,
messageId: message.id,
name: 'πŸ‘€',
});
} catch {
// Soft ack only.
}
}

const started = await startNewDiscordTask({
provider: resolved.provider,
applicationId: resolved.applicationId,
Expand Down
Loading
Loading