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(group): persist groupId on execAgent-created topics so group conversations stay in the group (#16228)

Arvin Xu719eb838e1d8
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 7c06b0a18d2..8184dd0916d 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
@@ -6,7 +6,7 @@
* InMemory implementations when Redis is not available (test environment).
*/
import { type LobeChatDatabase } from '@lobechat/database';
-import { agents, messages, threads, topics } from '@lobechat/database/schemas';
+import { agents, chatGroups, messages, threads, topics } from '@lobechat/database/schemas';
import { getTestDB } from '@lobechat/database/test-utils';
import { and, eq } from 'drizzle-orm';
import OpenAI from 'openai';
@@ -256,6 +256,57 @@ describe('execAgent', () => {
expect(result.success).toBe(true);
expect(result.operationId).toBeDefined();
});
+
+ // Regression: LOBE-10604 / LOBE-10627 β€” a group conversation that reaches
+ // execAgent without a pre-created topicId must persist groupId on BOTH the
+ // new topic AND the user/assistant messages. Otherwise:
+ // - the topic is group-less and never appears in the group sidebar
+ // (which queries `topics.groupId`), and
+ // - the messages are group-less, so reopening the topic returns an empty
+ // conversation (the group read filters on `messages.groupId`).
+ it('should persist groupId on the topic and messages when running in a group context', async () => {
+ mockResponsesCreate.mockResolvedValue(
+ createMockResponsesAPIStream('Hello from group context') as any,
+ );
+
+ const [group] = await serverDB
+ .insert(chatGroups)
+ .values({ title: 'Regression Group', userId })
+ .returning();
+
+ const caller = aiAgentRouter.createCaller(createTestContext());
+
+ // autoStart:false β€” we only assert topic/message *creation* carries
+ // groupId; no need to run the full agent loop (keeps the test fast and
+ // deterministic). The user message + assistant placeholder are still
+ // created before the run gate.
+ const result = await caller.execAgent({
+ agentId: testAgentId,
+ appContext: { groupId: group.id },
+ autoStart: false,
+ prompt: 'Hello, group via execAgent',
+ });
+
+ expect(result.success).toBe(true);
+
+ const createdTopics = await serverDB
+ .select()
+ .from(topics)
+ .where(eq(topics.agentId, testAgentId));
+
+ expect(createdTopics).toHaveLength(1);
+ expect(createdTopics[0].groupId).toBe(group.id);
+
+ // The user + assistant turn must also carry groupId, or the group read
+ // path (messageModel.query filters messages.groupId) returns nothing.
+ const createdMessages = await serverDB
+ .select()
+ .from(messages)
+ .where(eq(messages.topicId, createdTopics[0].id));
+
+ expect(createdMessages.length).toBeGreaterThanOrEqual(2);
+ expect(createdMessages.every((m) => m.groupId === group.id)).toBe(true);
+ });
});
describe('Full LLM Execution with Local Async Mode', () => {
diff --git a/apps/server/src/services/aiAgent/index.ts b/apps/server/src/services/aiAgent/index.ts
index 5687421874b..44f1724893e 100644
--- a/apps/server/src/services/aiAgent/index.ts
+++ b/apps/server/src/services/aiAgent/index.ts
@@ -1160,6 +1160,14 @@ export class AiAgentService {
const fallbackTitleSource = markdownToTxt(prompt);
const newTopic = await this.topicModel.create({
agentId: resolvedAgentId,
+ // Persist the group association when running inside a group conversation.
+ // Without it the topic is created group-less and only shows under the
+ // member agent's topic list β€” never in the group sidebar (which queries
+ // `topics.groupId`), so the conversation silently "disappears" from the
+ // group. execGroupAgent normally pre-creates the topic, but any path
+ // that reaches execAgent without a topicId (e.g. the async/queue run)
+ // must carry the groupId through too. (LOBE-10604 / LOBE-10627)
+ groupId: appContext?.groupId,
metadata,
title:
title !== undefined
@@ -1169,9 +1177,10 @@ export class AiAgentService {
});
topicId = newTopic.id;
log(
- 'execAgent: created new topic %s with trigger %s, cronJobId %s',
+ 'execAgent: created new topic %s with trigger %s, groupId %s, cronJobId %s',
topicId,
trigger || 'default',
+ appContext?.groupId || 'none',
cronJobId || 'none',
);
} else {
@@ -1249,6 +1258,10 @@ export class AiAgentService {
agentId: persistAgentId,
content: prompt,
files: runAttachments.fileIds,
+ // Group reads filter on messages.groupId (MessageModel.query group
+ // branch), so a group turn must stamp groupId or the message never
+ // shows when the topic is reopened. (LOBE-10604 / LOBE-10627)
+ groupId: appContext?.groupId ?? undefined,
metadata: requestTriggerMetadata,
role: 'user',
threadId: appContext?.threadId ?? undefined,
@@ -1267,6 +1280,9 @@ export class AiAgentService {
const assistantMessageRecord = await this.messageModel.create({
agentId: persistAgentId,
content: LOADING_FLAT,
+ // Stamp groupId so the assistant turn is visible in the group read path
+ // (MessageModel.query filters group chats by messages.groupId).
+ groupId: appContext?.groupId ?? undefined,
model: isHeteroAgent ? undefined : model,
parentId: parentMessageId ?? userMessageRecord?.id,
provider: isHeteroAgent ? heteroType : provider,