diff --git a/apps/api/src/router.ts b/apps/api/src/router.ts index dbdd0c5..4d5cb14 100644 --- a/apps/api/src/router.ts +++ b/apps/api/src/router.ts @@ -180,6 +180,7 @@ import { shouldForwardPeerThreadEvent, } from "./thread-message-pages.js"; import { + followUpGroupThreadMessage, reactToThreadMessage, resolveThreadTarget, sendThreadMessage, @@ -1543,79 +1544,8 @@ export function createRouter(deps: RouterDeps) { } return { ok: true as const }; } - const committed = await deps.prisma.$transaction(async (tx) => { - await lockOwnedGroup(tx, context.actor, target.groupId); - const group = await tx.chatGroup.findFirst({ - where: { - id: target.groupId, - archivedAt: null, - thread: { id: target.threadId }, - }, - include: { members: { orderBy: { createdAt: "asc" } } }, - }); - const botId = group?.members[0]?.botId; - if (!botId) throw new IsolationError(); - const blocks = [{ kind: "text" as const, text: input.text }]; - const message = await createThreadMessageInTransaction(tx, { - threadId: target.threadId, - role: "user", - blocks, - }); - const active = await tx.run.findFirst({ - where: { - threadId: target.threadId, - botId, - status: { in: [...ACTIVE_RUN_STATUSES] }, - }, - select: { id: true }, - }); - let run: { id: string } | null = null; - if (!active) { - const task = await tx.task.create({ - data: { - spaceId: context.actor.spaceId, - botId, - threadId: target.threadId, - userId: context.actor.userId, - prompt: input.text, - status: "queued", - }, - }); - run = await tx.run.create({ - data: { - spaceId: context.actor.spaceId, - botId, - threadId: target.threadId, - taskId: task.id, - userId: context.actor.userId, - status: "queued", - trigger: "follow_up", - sourceMessageId: message.id, - }, - select: { id: true }, - }); - await tx.message.update({ where: { id: message.id }, data: { runId: run.id } }); - } else { - await tx.steeringMessage.create({ - data: { - messageId: message.id, - botId, - userId: context.actor.userId, - runId: active.id, - }, - }); - await tx.message.update({ where: { id: message.id }, data: { runId: active.id } }); - } - const event = await appendEventInTransaction(tx, { - spaceId: context.actor.spaceId, - threadId: target.threadId, - botId, - type: "thread.message.created", - runId: run?.id ?? active?.id, - payload: { messageId: message.id, role: "user", blocks }, - }); - await touchGroupUpdatedAt(tx, target.groupId); - return { runId: run?.id, eventSeq: event.seq }; + const committed = await followUpGroupThreadMessage(deps, context.actor, target, { + text: input.text, }); await deps.events.notify(target.threadId, committed.eventSeq).catch((error) => { getLogger().error("group follow-up realtime notification", error);