Skip to content
Closed
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
47 changes: 47 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.

33 changes: 33 additions & 0 deletions apps/api/src/handlers/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {

import { apiLogger } from '../../logging.js';
import { continueSessionAttentionReply } from '../tasks/continue-session-attention-reply.js';
import { stopChatSessionTasks } from '../tasks/session-stop-command.js';
import { getCallRoomoteViaEmojiConfiguration } from '../call-roomote-via-emoji.js';
import { buildCommunicationTaskThreadName } from '../tasks/communication-task-thread.js';
import {
Expand Down Expand Up @@ -152,6 +153,7 @@ const DISCORD_HELP_MESSAGE = [
'**Available commands**',
'`/new request:<request>` — start a fresh task.',
'`/goal objective:<objective>` — keep this session working toward an objective across multiple turns.',
'`/stop` — resumably stop every active task in this session; send another message here to continue later.',
'`/link code:<code>` — link this Discord account in a DM with me.',
'`/help` — show this message.',
'',
Expand Down Expand Up @@ -582,6 +584,8 @@ async function processDiscordGatewayEvent(
if (command && command.name !== 'new') {
if (command.name === 'goal') {
// Handled after resolving the current conversation and linked user.
} else if (command.name === 'stop') {
// Handled after resolving the linked sender and conversation below.
} else if (command.name === 'skills') {
const registrations = [
resolved.provider.registerCommands({
Expand Down Expand Up @@ -884,6 +888,35 @@ async function processDiscordGatewayEvent(
userId: senderUserId,
});

if (command?.name === 'stop') {
const stopResult = await stopChatSessionTasks({
provider: 'discord',
workspaceId: channel.guildId ?? 'dm',
channelId: metadata.communicationChannelId,
conversationId: getDiscordFastConversationId(
channel,
interaction?.id ?? event.eventId,
),
...(metadata.communicationThreadId
? { threadId: metadata.communicationThreadId }
: {}),
...(message?.message_reference?.message_id
? { replyToMessageId: message.message_reference.message_id }
: {}),
userId: senderUserId,
displayName: sender.global_name ?? sender.username,
});
await replyToDiscordEvent({
provider: resolved.provider,
applicationId: resolved.applicationId,
channel,
interaction: interactionReplyContext(event),
text: stopResult.text,
ephemeral: true,
});
return { ok: true, stopRequested: true, outcome: stopResult.kind };
}

// Fast mode is unconditional for ordinary linked-human messages, including
// reaction summons: a configured emoji synthesizes a bot mention that enters
// the fast agent, matching Slack's call-roomote-via-emoji flow.
Expand Down

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

63 changes: 63 additions & 0 deletions apps/api/src/handlers/slack/events/message-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import {
type TaskInitiator,
isDeploymentReadOnlyError,
} from '@roomote/types';
import { isStopSessionCommandText } from '@roomote/communication';
import { findSessionAttentionNotificationReply } from '@roomote/sdk/server';

import { apiLogger } from '../../../logging.js';
import { continueSessionAttentionReply } from '../../tasks/continue-session-attention-reply.js';
import { stopChatSessionTasks } from '../../tasks/session-stop-command.js';
import {
ROUTING_LOCK_TTL_SECONDS,
SLACK_ROUTING_LOCK_PREFIX,
Expand Down Expand Up @@ -1162,6 +1164,8 @@ async function handleSlackEntryEvent(params: {
slackInstallation: SlackInstallation;
slack: SlackNotifier;
teamId: string;
stopSessionCommand?: boolean;
silentStopWhenUnavailable?: boolean;
skipThreadFollowupHandling?: boolean;
threadTaskId?: string;
peerConversationsEnabled?: boolean;
Expand All @@ -1177,6 +1181,8 @@ async function handleSlackEntryEvent(params: {
slackInstallation,
slack,
teamId,
stopSessionCommand = false,
silentStopWhenUnavailable = false,
skipThreadFollowupHandling = false,
threadTaskId,
peerConversationsEnabled,
Expand Down Expand Up @@ -1213,6 +1219,28 @@ async function handleSlackEntryEvent(params: {
return;
}

if (stopSessionCommand) {
const stopResult = await stopChatSessionTasks({
provider: 'slack',
workspaceId: teamId,
channelId: event.channel,
conversationId: event.thread_ts || event.ts,
threadId: event.thread_ts || event.ts,
...(event.thread_ts ? { replyToMessageId: event.thread_ts } : {}),
userId: userMapping.userId,
});
if (silentStopWhenUnavailable && stopResult.kind === 'unavailable') {
return;
}
await slack.postMessage({
channel: event.channel,
thread_ts: event.thread_ts || event.ts,
text: stopResult.text,
blocks: [{ type: 'markdown', text: stopResult.text }],
});
return;
}

const threadId = event.thread_ts || event.ts;
const attentionResolution = event.thread_ts
? await findSessionAttentionNotificationReply({
Expand Down Expand Up @@ -1342,6 +1370,41 @@ export async function handleMessageOrAppMentionEvent(params: {
}): Promise<void> {
const { event, context } = params;
enrichSlackMessageEvent(event);
const isStopCommand = isStopSessionCommandText(
event.authoredText ?? event.text,
);
const isHumanSlackMessage =
Boolean(event.user) &&
!event.bot_id &&
!event.subtype &&
event.user !== context.slackInstallation.botUserId;
const isStopCommandAddressedToRoomote =
isHumanSlackMessage &&
(event.type === 'app_mention' ||
event.channel_type === 'im' ||
event.channel_type === 'mpim' ||
mentionsSlackBot(event, context.slackInstallation.botUserId));
Comment thread
roomote-community[bot] marked this conversation as resolved.
const isUnmentionedStopInSessionThread =
isHumanSlackMessage &&
event.type === 'message' &&
event.channel_type !== 'im' &&
event.channel_type !== 'mpim' &&
Boolean(event.thread_ts) &&
!mentionsSlackBot(event, context.slackInstallation.botUserId);
if (
isStopCommand &&
(isStopCommandAddressedToRoomote || isUnmentionedStopInSessionThread)
) {
await handleSlackEntryEvent({
event,
slackInstallation: context.slackInstallation,
slack: context.slack,
teamId: context.teamId,
stopSessionCommand: true,
silentStopWhenUnavailable: isUnmentionedStopInSessionThread,
});
return;
}
const automatedAppMentionEvent = isRoutableAutomatedSlackAppMention(
event,
context.slackInstallation,
Expand Down
Loading
Loading