Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
94 changes: 94 additions & 0 deletions apps/api/src/handlers/discord/__tests__/goal-command.test.ts

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

77 changes: 77 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.

69 changes: 69 additions & 0 deletions apps/api/src/handlers/discord/goal-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { sendMessageToTask } from '../tasks/sendMessageToTask.js';
import { prepareTaskGoalActivation } from '@roomote/db/server';
import {
DEFAULT_TASK_GOAL_MAX_CONTINUATIONS,
type TaskGoal,
} from '@roomote/types';

export async function startDiscordTaskGoal(input: {
taskId: string;
userId: string;
objective: string;
clientMessageId: string;
}): Promise<{ success: true } | { success: false; error: string }> {
const goal = {
objective: input.objective,
maxContinuations: DEFAULT_TASK_GOAL_MAX_CONTINUATIONS,
};
const activation = await prepareTaskGoalActivation({
taskId: input.taskId,
goal,
});
if (!activation) {
return {
success: false,
error: 'Goal Mode activation is already pending.',
};
}

const goalContext: TaskGoal = {
...goal,
generation: activation.generation,
status: 'active',
continuationsUsed: 0,
blockedReason: null,
completedAt: null,
};

try {
const delivered = await sendMessageToTask({
taskId: input.taskId,
userId: input.userId,
message: input.objective,
source: 'discord',
clientMessageId: input.clientMessageId,
goalContext,
});
if (!delivered.success) {
await activation.rollback();
return { success: false, error: delivered.error };
}
} catch (error) {
await activation.rollback().catch(() => undefined);
throw error;
}

let committed: TaskGoal | null;
try {
committed = await activation.commit();
} catch (error) {
await activation.rollback().catch(() => undefined);
throw error;
}
if (!committed) {
await activation.rollback();
return { success: false, error: 'Goal Mode activation was superseded.' };
}

return { success: true };
}
49 changes: 48 additions & 1 deletion apps/api/src/handlers/discord/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import {
resolveDiscordChannelContext,
} from './task-launch.js';
import { startNewDiscordTask } from './task-orchestration.js';
import { startDiscordTaskGoal } from './goal-command.js';
import {
buildDiscordContinuationPrompt,
fetchDiscordThreadHistoryBestEffort,
Expand Down Expand Up @@ -154,6 +155,7 @@ const DISCORD_HELP_MESSAGE = [
'',
'**Available commands**',
'`/new request:<request>` — start a fresh task.',
'`/goal objective:<objective>` — enable Goal Mode for the current task.',
'`/link code:<code>` — link this Discord account in a DM with me.',
'`/help` — show this message.',
'',
Expand Down Expand Up @@ -509,7 +511,11 @@ async function processDiscordGatewayEvent(
}

if (command && command.name !== 'new') {
return { ok: true, ignored: 'unsupported_command' };
if (command.name === 'goal') {
// Handled after resolving the current conversation and linked user.
} else {
return { ok: true, ignored: 'unsupported_command' };
}
}
if (command?.name === 'new' && !command.request) {
await replyToDiscordEvent({
Expand Down Expand Up @@ -683,6 +689,47 @@ async function processDiscordGatewayEvent(
userId: senderUserId,
});

if (command?.name === 'goal') {
if (!command.objective) {
await replyToDiscordEvent({
provider: resolved.provider,
applicationId: resolved.applicationId,
channel,
interaction: interactionReplyContext(event),
text: 'Add what you want Roomote to keep working toward in the `objective` field.',
ephemeral: true,
});
return { ok: true, goalStarted: false, reason: 'missing_objective' };
}
if (!activeRun) {
await replyToDiscordEvent({
provider: resolved.provider,
applicationId: resolved.applicationId,
channel,
interaction: interactionReplyContext(event),
text: 'Use `/goal` in an active Roomote task thread or DM. Start a task with `/new` or mention me first.',
ephemeral: true,
});
return { ok: true, goalStarted: false, reason: 'no_active_task' };
}

const result = await startDiscordTaskGoal({
taskId: activeRun.taskId,
userId: senderUserId,
objective: command.objective,
clientMessageId: interaction?.id ?? event.eventId,
});
await replyToDiscordEvent({
provider: resolved.provider,
applicationId: resolved.applicationId,
channel,
interaction: interactionReplyContext(event),
text: result.success ? 'Goal Mode enabled.' : result.error,
ephemeral: true,
});
return { ok: true, goalStarted: result.success, runId: activeRun.id };
}

const messageAttachments = message
? getDiscordMessageAttachments(message)
: [];
Expand Down
11 changes: 11 additions & 0 deletions apps/api/src/handlers/tasks/sendMessageToTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
TaskPayload,
RunTokenContext,
PullRequestStatus,
TaskGoal,
} from '@roomote/types';
import { trackLatestUserMessageForReplyQuote } from '@roomote/communication/messages';
import {
Expand Down Expand Up @@ -728,6 +729,7 @@ export async function sendMessageToTask({
clientMessageId,
senderMode,
workerQuoteUserName,
goalContext,
}: {
taskId: string;
userId: string;
Expand All @@ -746,6 +748,7 @@ export async function sendMessageToTask({
* commenter has no linked account.
*/
workerQuoteUserName?: string;
goalContext?: TaskGoal;
}): Promise<SendMessageToTaskResult> {
try {
const run = await findLatestTaskRun(taskId, {
Expand Down Expand Up @@ -787,6 +790,13 @@ export async function sendMessageToTask({
}

if (isExitedRunStatus(run.status)) {
if (goalContext) {
return {
success: false,
error: `Task is not active (status: ${run.status})`,
status: 409,
};
}
const resumeResult = await resumeTaskFromSnapshot({
taskId,
userId: linkedReviewHandoff.senderUserId,
Expand Down Expand Up @@ -878,6 +888,7 @@ export async function sendMessageToTask({
// credential identity changes. Native steering injects at the
// next step; fallback steering aborts and replays promptly.
...(requiresActorHandoff ? { autoSteerWhenQueued: true } : {}),
...(goalContext ? { autoSteerWhenQueued: true, goalContext } : {}),
...(images?.length ? { images } : {}),
}),
});
Expand Down
Loading
Loading