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.

🐛 fix(channel): keep replies to an Agent in their feed and let @mentions pick who answers (#23345)

Arvin Xub93ae157f20c
diff --git a/packages/database/src/repositories/channel/__tests__/reply.test.ts b/packages/database/src/repositories/channel/__tests__/reply.test.ts
index 1dd33ec93a7..e9d06a0cf16 100644
--- a/packages/database/src/repositories/channel/__tests__/reply.test.ts
+++ b/packages/database/src/repositories/channel/__tests__/reply.test.ts
@@ -110,99 +110,96 @@ describe('Channel replies', () => {
expect(await threads(channel.id)).toEqual([]);
});
- it('creates a thread with only the replied-to Agent and includes its answer in the claimed input', async () => {
+ it('asks the quoted Agent in the same feed and includes its answer in the claimed input', async () => {
const { asOwner, author, channel, executions, input } = await setup();
const message = await asOwner.send(channel.id, input);
- const [thread] = await threads(channel.id);
- expect(thread).toMatchObject({
- rootMessageId: input.replyToId,
- followerMemberIds: [author.id],
- });
expect(message).toMatchObject({
- threadId: thread.id,
+ replyToId: input.replyToId,
+ threadId: null,
routingStatus: 'directed',
+ routingReason: 'Reply to Agent',
recipientMemberIds: [author.id],
});
+ expect(await threads(channel.id)).toEqual([]);
const [job] = await jobsOf(db, message.id);
- expect(job).toMatchObject({ memberId: author.id, threadId: thread.id });
+ expect(job).toMatchObject({ memberId: author.id, threadId: null });
await executions.claim(channel.id, job.id, lease);
const context = await executions.input(channel.id, job.id);
expect(context.map((entry) => entry.id)).toEqual([input.replyToId, message.id]);
expect(context[1].replyToId).toBe(input.replyToId);
- const detail = await asOwner.detail(channel.id, { threadId: thread.id });
- expect(detail.messages[0].replyTo).toMatchObject({
- id: input.replyToId,
- author: { type: 'agent' },
+ const detail = await asOwner.detail(channel.id, { limit: 1 });
+ expect(detail.messages[0]).toMatchObject({
+ id: message.id,
+ replyTo: { id: input.replyToId, author: { type: 'agent' } },
});
- expect((await asOwner.detail(channel.id)).messages.map((entry) => entry.id)).not.toContain(
- message.id,
- );
});
- it('unions explicit Agent mentions with the author and retains them for subsequent conversation', async () => {
- const { asOwner, author, channel, input, other } = await setup();
- const message = await asOwner.send(channel.id, {
+ it('asks only the @mentioned members when quoting an Agent', async () => {
+ const { asOwner, author, channel, input, member, other } = await setup();
+ const handOff = await asOwner.send(channel.id, { ...input, mentions: [other.id] });
+ expect(handOff).toMatchObject({
+ threadId: null,
+ routingStatus: 'directed',
+ routingReason: 'Mentioned',
+ recipientMemberIds: [other.id],
+ });
+ expect(await jobsOf(db, handOff.id)).toMatchObject([{ memberId: other.id }]);
+ const both = await asOwner.send(channel.id, {
...input,
mentions: [other.id, author.id, other.id],
+ requestKey: 'both',
});
- expect(new Set(message.recipientMemberIds)).toEqual(new Set([author.id, other.id]));
- expect(await jobsOf(db, message.id)).toHaveLength(2);
- const [thread] = await threads(channel.id);
- expect(new Set(thread.followerMemberIds)).toEqual(new Set([author.id, other.id]));
- const next = await asOwner.send(channel.id, {
- content: 'Continue',
- requestKey: 'next',
- routing: 'fallback',
- threadId: thread.id,
- });
- expect(new Set(next.recipientMemberIds)).toEqual(new Set([author.id, other.id]));
- const humanQuote = await asOwner.send(channel.id, {
- content: 'Just a note',
- mentions: [other.id],
- requestKey: 'note',
- routing: 'decision',
- replyToId: next.id,
- threadId: thread.id,
+ expect(new Set(both.recipientMemberIds)).toEqual(new Set([author.id, other.id]));
+ const person = await asOwner.send(channel.id, {
+ ...input,
+ mentions: [member.id],
+ requestKey: 'person',
});
- // Quoting a person stays in the thread; only the mentioned Agent is asked.
- expect(humanQuote.threadId).toBe(thread.id);
- expect(humanQuote.recipientMemberIds).toEqual([other.id]);
- expect(await jobsOf(db, humanQuote.id)).toMatchObject([
- { memberId: other.id, threadId: thread.id },
- ]);
- expect(await threads(channel.id)).toHaveLength(1);
+ expect(person.recipientMemberIds).toEqual([]);
+ expect(await jobsOf(db, person.id)).toEqual([]);
+ expect(await threads(channel.id)).toEqual([]);
});
- it('reuses the thread and idempotent message without removing existing followers', async () => {
- const { asOwner, author, channel, input, other } = await setup();
- const firstInput = { ...input, mentions: [other.id] };
+ it('returns the original message for a retried reply', async () => {
+ const { asOwner, author, channel, input } = await setup();
const [first, retry] = await Promise.all([
- asOwner.send(channel.id, firstInput),
- asOwner.send(channel.id, firstInput),
+ asOwner.send(channel.id, input),
+ asOwner.send(channel.id, input),
]);
expect(retry.id).toBe(first.id);
- expect(await jobsOf(db, first.id)).toHaveLength(2);
- const next = await asOwner.send(channel.id, { ...input, requestKey: 'again' });
- expect(next.threadId).toBe(first.threadId);
- expect(next.recipientMemberIds).toEqual([author.id]);
- const all = await threads(channel.id);
- expect(all).toHaveLength(1);
- expect(new Set(all[0].followerMemberIds)).toEqual(new Set([author.id, other.id]));
+ expect(await jobsOf(db, first.id)).toMatchObject([{ memberId: author.id }]);
+ expect(await threads(channel.id)).toEqual([]);
});
- it('replies to an Agent within the current thread without nesting, adding only mentioned Agents', async () => {
- const { asOwner, author, channel, input, other, publish } = await setup();
- const first = await asOwner.send(channel.id, input);
+ it('replies to an Agent within the current thread without nesting', async () => {
+ const { asOwner, author, channel, input, other, publish, root } = await setup();
+ const first = await asOwner.send(channel.id, {
+ content: 'Let us discuss',
+ mentions: [author.id],
+ requestKey: 'branch',
+ rootMessageId: root.id,
+ routing: 'fallback',
+ });
const answerId = await publish(first.id, 'Three works');
- const next = await asOwner.send(channel.id, {
+ const quoted = await asOwner.send(channel.id, {
...input,
replyToId: answerId,
threadId: first.threadId,
- mentions: [other.id],
requestKey: 'in-thread',
});
- expect(next.threadId).toBe(first.threadId);
- expect(new Set(next.recipientMemberIds)).toEqual(new Set([author.id, other.id]));
+ expect(quoted.threadId).toBe(first.threadId);
+ expect(quoted.recipientMemberIds).toEqual([author.id]);
+ const handOff = await asOwner.send(channel.id, {
+ ...input,
+ replyToId: answerId,
+ threadId: first.threadId,
+ mentions: [other.id],
+ requestKey: 'hand-off',
+ });
+ expect(handOff.threadId).toBe(first.threadId);
+ expect(handOff.recipientMemberIds).toEqual([other.id]);
+ const [thread] = await threads(channel.id);
+ expect(new Set(thread.followerMemberIds)).toEqual(new Set([author.id, other.id]));
expect(await threads(channel.id)).toHaveLength(1);
await expect(
asOwner.send(channel.id, { ...input, replyToId: answerId, requestKey: 'wrong-scope' }),
@@ -231,8 +228,8 @@ describe('Channel replies', () => {
expect(await threads(foreign.channel.id)).toEqual([]);
});
- it('rejects a removed Agent and an archived thread without dispatch', async () => {
- const { asOwner, author, channel, input } = await setup();
+ it('rejects a removed Agent and a reply in an archived thread without dispatch', async () => {
+ const { asOwner, author, channel, input, root } = await setup();
await db.update(channelMembers).set({ status: 'left' }).where(eq(channelMembers.id, author.id));
await expect(asOwner.send(channel.id, input)).rejects.toMatchObject({ code: 'BAD_REQUEST' });
expect(await threads(channel.id)).toEqual([]);
@@ -240,17 +237,27 @@ describe('Channel replies', () => {
.update(channelMembers)
.set({ status: 'active' })
.where(eq(channelMembers.id, author.id));
- const first = await asOwner.send(channel.id, input);
+ const first = await asOwner.send(channel.id, {
+ content: 'Branch',
+ requestKey: 'branch',
+ rootMessageId: root.id,
+ routing: 'fallback',
+ });
await db
.update(channelThreads)
.set({ status: 'archived' })
.where(eq(channelThreads.id, first.threadId!));
await expect(
- asOwner.send(channel.id, { ...input, requestKey: 'archived' }),
+ asOwner.send(channel.id, {
+ ...input,
+ replyToId: first.id,
+ requestKey: 'archived',
+ threadId: first.threadId,
+ }),
).rejects.toMatchObject({ code: 'CONFLICT' });
});
- it('rejects a changed quote with the same retry key and rolls back thread creation', async () => {
+ it('rejects a changed quote with the same retry key', async () => {
const { asOwner, channel, input, root } = await setup();
await asOwner.send(channel.id, { ...input, replyToId: root.id });
await expect(asOwner.send(channel.id, input)).rejects.toMatchObject({ code: 'CONFLICT' });
diff --git a/packages/database/src/repositories/channel/send.ts b/packages/database/src/repositories/channel/send.ts
index df7bf7b43ca..fe7adf01f15 100644
--- a/packages/database/src/repositories/channel/send.ts
+++ b/packages/database/src/repositories/channel/send.ts
@@ -35,7 +35,7 @@ export interface ChannelSendInput {
mentions?: string[];
/** Stored with the message, e.g. `via` for a request an Agent relayed for the caller. */
metadata?: ChannelMessageMetadata;
- /** Quote a person (only mentions dispatch), or continue an Agent answer in its thread. */
+ /** Quote a message in its own feed; @mentions dispatch, else a quoted Agent answers. */
replyToId?: string;
/** Client retry key; a retry with the same key returns the original message. */
requestKey: string;
@@ -73,8 +73,9 @@ export const validateSendInput = (input: ChannelSendInput) => {
/**
* Posts a human message. The caller holds the channel lock and is `access.member`.
*
- * - Human quotes stay in their feed; without mentions they get no jobs. Agent replies continue
- * in a thread.
+ * - A reply stays in the feed of the message it quotes; it never opens a Thread.
+ * - Quoting a person without mentions gets no jobs. Quoting an Agent without mentions asks that
+ * Agent; with @mentions, only the mentioned Agents answer.
* - @mentioned Agents get jobs in this transaction (`directed`, source `explicit`), quoted or not.
* - @mentioning only people wakes no Agent.
* - Without mentions, `decision` leaves the message `pending` for the routing task, and
@@ -111,8 +112,9 @@ export const sendMessage = async (
const humanQuote = !!replyTo && replyTo.routingStatus !== 'reply';
// An explicit @mention still dispatches; the quote only adds context.
const quoteOnly = humanQuote && !mentions.length;
+ // Quoting an Agent asks it only when nobody is @mentioned: a mention names who should answer.
const [replyAgent] =
- replyTo && !humanQuote
+ replyTo && !humanQuote && !mentions.length
? await tx
.select()
.from(channelMembers)
@@ -125,20 +127,14 @@ export const sendMessage = async (
),
)
: [];
- if (replyTo && !humanQuote && !replyAgent)
+ if (replyTo && !humanQuote && !mentions.length && !replyAgent)
throw new ChannelError('BAD_REQUEST', 'The replying Agent is no longer available');
- // Replying to a muted Agent still opens its Thread, but only an @mention makes it answer.
+ // Replying to a muted Agent asks no one; only an @mention makes it answer.
const replyTarget = replyAgent?.config?.muted ? undefined : replyAgent;
- // A click is local. Create the Agent's thread atomically with the first sent reply.
const thread = assertThreadOpen(
- replyAgent && !sourceThread
- ? await createChannelThread(tx, channel.id, {
- rootMessageId: replyTo!.id,
- followerMemberIds: [replyAgent.id],
- })
- : input.rootMessageId
- ? await createChannelThread(tx, channel.id, { rootMessageId: input.rootMessageId })
- : sourceThread,
+ input.rootMessageId
+ ? await createChannelThread(tx, channel.id, { rootMessageId: input.rootMessageId })
+ : sourceThread,
);
const threadId = thread?.id ?? null;
@@ -223,7 +219,7 @@ export const sendMessage = async (
? []
: [
...mentions,
- ...(replyAgent ? [replyAgent.id] : []),
+ ...(replyTarget ? [replyTarget.id] : []),
...(thread && paired ? [paired.id] : []),
];
if (thread && followers.some((id) => !thread.followerMemberIds.includes(id))) {
@@ -253,7 +249,7 @@ export const sendMessage = async (
const routed = quoteOnly
? ({ reason: 'Quoted human message', source: 'explicit', status: 'skipped' } as const)
: replyAgent
- ? replyTarget || mentions.length
+ ? replyTarget
? ({ reason: 'Reply to Agent', source: 'explicit', status: 'directed' } as const)
: ({ reason: 'The Agent is muted', source: 'explicit', status: 'skipped' } as const)
: paired
@@ -284,14 +280,9 @@ export const sendMessage = async (
const recipients = quoteOnly
? []
: replyAgent
- ? [
- ...new Map(
- [...(replyTarget ? [replyTarget] : []), ...mentionedAgents].map((agent) => [
- agent.id,
- agent,
- ]),
- ).values(),
- ]
+ ? replyTarget
+ ? [replyTarget]
+ : []
: paired
? [paired]
: mentions.length