Wake agents on @mention in chat messages

Agents were deaf to @mentions — only /agent slash commands triggered a
task_run. Now ChatService.sendMessage auto-wakes every mentioned agent
(skipping self-mentions and /agent which already handles it) by creating
a queued task_run through ChatAgentWakeService.wakeFromMention.
This commit is contained in:
ZacharyZhang-NY committed 2026-06-07 17:20:58 -04:00
1 parent abe578b1a0
commit 44f31b49fb
2 files changed
+48

No files matched your search

@@ -35,6 +35,27 @@ export class ChatAgentWakeService {
this.organization = new OrganizationService(db);
}
wakeFromMention(input: SendMessageInput, agentId: string, messageId: string, threadId: string, now: string): AgentWakeResult {
const agent = this.requireRunnableAgent(input.orgId, agentId);
const workspace = this.workspaceForAgent(input.orgId, agent.workspace_id);
const taskId = this.organization.createTask({
orgId: input.orgId,
title: `Chat reply: ${agent.name}`,
description: this.mentionTaskDescription(input, messageId, threadId),
status: TASK_STATUS.BACKLOG,
assigneeAgentId: agent.id,
risk: RISK_LEVEL.LOW,
projectId: workspace?.project_id ?? null,
goalId: workspace?.goal_id ?? null,
workspaceId: workspace?.id ?? null,
priority: TASK_PRIORITY.HIGH,
requiresFilesystem: false,
});
const runId = this.createQueuedRun({ orgId: input.orgId, taskId, agent, workspace, messageId, threadId, now });
this.recordPresence(agent.id, { command: "mention", args: "" }, now);
return { targetId: agent.id, taskId, runId };
}
wakeFromSlash(input: SendMessageInput, mentions: ResolvedMention[], slash: SlashInvocation, messageId: string, threadId: string, now: string): AgentWakeResult {
const targetId = this.requireAgentMention(mentions);
const agent = this.requireRunnableAgent(input.orgId, targetId);
@@ -170,6 +191,17 @@ export class ChatAgentWakeService {
return `Chat mention: ${title.length > 64 ? `${title.slice(0, 61)}...` : title}`;
}
private mentionTaskDescription(input: SendMessageInput, messageId: string, threadId: string): string {
return [
`Source message: ${messageId}`,
`Source thread: ${threadId}`,
`Source space: ${input.spaceId}`,
`Sender: ${input.senderType}${input.senderId ? `:${input.senderId}` : ""}`,
"",
"You were @mentioned in a chat message. Read the thread context, then reply in the source thread with send_message and pass the thread_id above.",
].join("\n");
}
private taskDescription(input: SendMessageInput, slash: SlashInvocation, messageId: string, threadId: string): string {
return [
`Chat command: /agent ${slash.args.trim()}`,