Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(channel): let a direct conversation move a long reply into a Thread (#23074)

Arvin Xu740171adef59
diff --git a/packages/database/src/repositories/channel/__tests__/replyOpening.test.ts b/packages/database/src/repositories/channel/__tests__/replyOpening.test.ts
index 02397d92ec0..ea89d43adf0 100644
--- a/packages/database/src/repositories/channel/__tests__/replyOpening.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/replyOpening.test.ts
@@ -243,7 +243,7 @@ describe('Channel reply opening', () => {
});
});
- it('acknowledges in a direct conversation but never moves it into a Thread', async () => {
+ it('moves a long reply in a direct conversation into a Thread under the acknowledgement', async () => {
const [agent] = await db
.insert(agents)
.values({ name: 'Assistant', userId: fixture.ownerId, workspaceId: fixture.workspaceId })
@@ -258,15 +258,20 @@ describe('Channel reply opening', () => {
const [job] = await jobsOf(db, message.id);
expect(await execution.openingCandidate(channel.id, job.id)).toMatchObject({
- canOpenThread: false,
+ canOpenThread: true,
});
await execution.open(channel.id, job.id, { ack: 'On it.', threadTitle: 'This week' });
- expect((await jobById(db, job.id)).threadId).toBeNull();
- expect(
- await db.select().from(channelThreads).where(eq(channelThreads.channelId, channel.id)),
- ).toEqual([]);
- expect(await execution.openingReply(channel.id, job.id)).toBe('On it.');
+ const threads = await db
+ .select()
+ .from(channelThreads)
+ .where(eq(channelThreads.channelId, channel.id));
+ expect(threads).toHaveLength(1);
+ const ack = (await messagesOf(channel.id)).find((row) => row.routingStatus === 'reply');
+ expect(threads[0]).toMatchObject({ rootMessageId: ack?.id });
+ expect(threads[0].metadata).toMatchObject({ title: 'This week' });
+ expect((await jobById(db, job.id)).threadId).toBe(threads[0].id);
+ expect(await execution.claim(channel.id, job.id, lease)).not.toBeNull();
});
it('never opens a wake that may stay silent', async () => {
diff --git a/packages/database/src/repositories/channel/opening.ts b/packages/database/src/repositories/channel/opening.ts
index 5cbfd40ebd3..8ba74977a59 100644
--- a/packages/database/src/repositories/channel/opening.ts
+++ b/packages/database/src/repositories/channel/opening.ts
@@ -33,8 +33,8 @@ export const openingRequestKey = (jobId: string) => `opening:${jobId}`;
export interface ChannelOpeningCandidate {
agent: { id: string; name: string | null; title: string | null };
/**
- * Whether the reply may move into a new Thread: only a request on a group's main timeline can.
- * A direct conversation is a DM with the Agent, where a bot never opens one either.
+ * Whether the reply may move into a new Thread: only a request on the main timeline can, in a
+ * group or a direct conversation alike, so a long run leaves that timeline free.
*/
canOpenThread: boolean;
/** Public messages just before the request, oldest first. */
@@ -115,7 +115,7 @@ export const findOpeningCandidate = async (
.where(eq(channelThreads.id, job.threadId))
: [],
db
- .select({ agentId: channels.agentId, workspaceId: channels.workspaceId })
+ .select({ workspaceId: channels.workspaceId })
.from(channels)
.where(eq(channels.id, channelId)),
]);
@@ -139,7 +139,7 @@ export const findOpeningCandidate = async (
return {
agent: { id: row.agentId, name: row.agentName, title: row.agentTitle },
- canOpenThread: !job.threadId && !channel.agentId,
+ canOpenThread: !job.threadId,
recent: messages.filter((message) => message.id !== job.messageId),
request: requestMessage,
requesterUserId: requester.userId,
@@ -159,7 +159,7 @@ export const applyOpening = async (
jobId: string,
decision: ChannelOpeningDecision,
): Promise<boolean> =>
- withChannelLock(db, channelId, async (tx, channel) => {
+ withChannelLock(db, channelId, async (tx) => {
const [job] = await tx
.select()
.from(channelJobs)
@@ -197,12 +197,7 @@ export const applyOpening = async (
// Without an acknowledgement the Thread opens under the request itself, unless an archived
// one already sits there: it takes no new work, so the reply stays where it was asked.
const rootMessageId = posted?.id ?? job.messageId;
- if (
- title &&
- !threadId &&
- !channel.agentId &&
- !(await hasArchivedThread(tx, channelId, rootMessageId))
- ) {
+ if (title && !threadId && !(await hasArchivedThread(tx, channelId, rootMessageId))) {
// Follow-ups deferred behind this answer move with it: they must read it to stay silent.
const deferred = await tx
.select({ id: channelJobs.id, memberId: channelJobs.memberId })