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(hetero-agent): chain execAgent user turns (#16433)

Arvin Xub1aacd4d62e2
diff --git a/apps/server/src/routers/lambda/__tests__/integration/aiAgent/execAgent.integration.test.ts b/apps/server/src/routers/lambda/__tests__/integration/aiAgent/execAgent.integration.test.ts
index a4cc1322053..5730b226945 100644
--- a/apps/server/src/routers/lambda/__tests__/integration/aiAgent/execAgent.integration.test.ts
+++ b/apps/server/src/routers/lambda/__tests__/integration/aiAgent/execAgent.integration.test.ts
@@ -168,10 +168,32 @@ describe('execAgent', () => {
.insert(topics)
.values({ agentId: testAgentId, title: 'Existing Topic', userId })
.returning();
+ const [seedUserMessage] = (await serverDB
+ .insert(messages)
+ .values({
+ agentId: testAgentId,
+ content: 'Initial question',
+ role: 'user',
+ topicId: existingTopic.id,
+ userId,
+ })
+ .returning()) as any[];
+ const [latestAssistantMessage] = (await serverDB
+ .insert(messages)
+ .values({
+ agentId: testAgentId,
+ content: 'Initial answer',
+ parentId: seedUserMessage.id,
+ role: 'assistant',
+ topicId: existingTopic.id,
+ userId,
+ })
+ .returning()) as any[];
const result = await caller.execAgent({
agentId: testAgentId,
appContext: { topicId: existingTopic.id },
+ autoStart: false,
prompt: 'Follow up question',
});
@@ -180,6 +202,20 @@ describe('execAgent', () => {
const allTopics = await serverDB.select().from(topics).where(eq(topics.agentId, testAgentId));
expect(allTopics).toHaveLength(1);
expect(allTopics[0].id).toBe(existingTopic.id);
+
+ const allMessages = await serverDB
+ .select()
+ .from(messages)
+ .where(eq(messages.topicId, existingTopic.id));
+ const followUpUserMessage = allMessages.find(
+ (message) => message.role === 'user' && message.content === 'Follow up question',
+ );
+ expect(followUpUserMessage?.parentId).toBe(latestAssistantMessage.id);
+
+ const followUpAssistantMessage = allMessages.find(
+ (message) => message.role === 'assistant' && message.parentId === followUpUserMessage?.id,
+ );
+ expect(followUpAssistantMessage).toBeDefined();
});
});
diff --git a/apps/server/src/services/aiAgent/index.ts b/apps/server/src/services/aiAgent/index.ts
index 07938c03119..dc865b76ca4 100644
--- a/apps/server/src/services/aiAgent/index.ts
+++ b/apps/server/src/services/aiAgent/index.ts
@@ -1342,6 +1342,13 @@ export class AiAgentService {
// exclude this freshly-created turn — history must be the PRIOR turns only,
// otherwise the new prompt is double-counted in the LLM context.
const selfMessageIds = new Set<string>();
+ const userMessageParentId =
+ runFromHistory || parentMessageId
+ ? undefined
+ : await this.messageModel.getLatestSpineMessageId?.({
+ threadId: appContext?.threadId ?? null,
+ topicId,
+ });
const userMessageRecord = runFromHistory
? undefined
: await this.messageModel.create({
@@ -1353,6 +1360,7 @@ export class AiAgentService {
// shows when the topic is reopened (group topic sidebar + ownership fix).
groupId: appContext?.groupId ?? undefined,
metadata: requestTriggerMetadata,
+ parentId: userMessageParentId,
role: 'user',
threadId: appContext?.threadId ?? undefined,
topicId,