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(chat): preserve subAgentId/documentId in message bucket key context (#15865)

Arvin Xu8ad6c2180df4
diff --git a/packages/database/src/models/__tests__/topics/topic.create.test.ts b/packages/database/src/models/__tests__/topics/topic.create.test.ts
index 91c56c588c7..f0842dbfd3f 100644
--- a/packages/database/src/models/__tests__/topics/topic.create.test.ts
+++ b/packages/database/src/models/__tests__/topics/topic.create.test.ts
@@ -392,9 +392,26 @@ describe('TopicModel - Create', () => {
await serverDB.transaction(async (tx) => {
await tx.insert(topics).values({ id: topicId, sessionId, userId, title: 'Original Topic' });
+ // Distinct createdAt so `duplicate`'s `orderBy(createdAt)` is
+ // deterministic β€” inserting both in one tx would otherwise give them the
+ // same `now()` default, leaving the tied rows in arbitrary order.
await tx.insert(messages).values([
- { id: 'message1', role: 'user', topicId, userId, content: 'User message' },
- { id: 'message2', role: 'assistant', topicId, userId, content: 'Assistant message' },
+ {
+ id: 'message1',
+ role: 'user',
+ topicId,
+ userId,
+ content: 'User message',
+ createdAt: new Date('2024-01-01T00:00:00Z'),
+ },
+ {
+ id: 'message2',
+ role: 'assistant',
+ topicId,
+ userId,
+ content: 'Assistant message',
+ createdAt: new Date('2024-01-01T00:00:01Z'),
+ },
]);
});
diff --git a/src/store/chat/slices/message/actions/query.ts b/src/store/chat/slices/message/actions/query.ts
index 4574804449f..53e8f38e0fc 100644
--- a/src/store/chat/slices/message/actions/query.ts
+++ b/src/store/chat/slices/message/actions/query.ts
@@ -59,16 +59,14 @@ export class MessageQueryActionImpl {
// Priority 1: Use explicit context if provided (preserving scope)
if (params?.context) {
+ // Spread the whole context so every bucket-key field carries through β€”
+ // notably `documentId` (page scope: keeps writes in the
+ // `page_<agent>_<documentId>` bucket the editor reads from, instead of
+ // `page_<agent>_new`) and `subAgentId` (group_agent scope's subTopicId).
+ // Only agentId/topicId need a fallback to the active conversation.
ctx = {
+ ...params.context,
agentId: params.context.agentId ?? this.#get().activeAgentId,
- // Preserve groupId from context
- groupId: params.context.groupId,
- // Preserve scope from context
- isNew: params.context.isNew,
-
- scope: params.context.scope,
-
- threadId: params.context.threadId,
topicId:
params.context.topicId !== undefined ? params.context.topicId : this.#get().activeTopicId,
};
diff --git a/src/store/chat/slices/operation/__tests__/actions.test.ts b/src/store/chat/slices/operation/__tests__/actions.test.ts
index 1de6c59fff7..6a101ec1064 100644
--- a/src/store/chat/slices/operation/__tests__/actions.test.ts
+++ b/src/store/chat/slices/operation/__tests__/actions.test.ts
@@ -1121,6 +1121,31 @@ describe('Operation Actions', () => {
expect(context.isNew).toBe(true);
});
+ it('should preserve documentId from operation context (page scope)', () => {
+ const { result } = renderHook(() => useChatStore());
+
+ let operationId: string;
+
+ act(() => {
+ operationId = result.current.startOperation({
+ type: 'sendMessage',
+ context: {
+ agentId: 'page-agent',
+ documentId: 'doc-1',
+ scope: 'page',
+ },
+ }).operationId;
+ });
+
+ const context = result.current.internal_getConversationContext({ operationId: operationId! });
+
+ // Dropping documentId here sinks page-scoped optimistic writes into the
+ // `page_<agent>_new` bucket while the editor reads `page_<agent>_doc-1`,
+ // leaving the copilot stuck on the loading skeleton.
+ expect(context.documentId).toBe('doc-1');
+ expect(context.scope).toBe('page');
+ });
+
it('should fallback to global state when no operationId provided', () => {
const { result } = renderHook(() => useChatStore());
diff --git a/src/store/chat/slices/operation/actions.ts b/src/store/chat/slices/operation/actions.ts
index 994276d4a10..185b641a1e2 100644
--- a/src/store/chat/slices/operation/actions.ts
+++ b/src/store/chat/slices/operation/actions.ts
@@ -65,17 +65,23 @@ export class OperationActionsImpl {
context.operationId,
);
} else {
- const { agentId, topicId, threadId, scope, isNew, groupId } = operation.context;
+ const { agentId, topicId, threadId, scope, isNew, groupId, documentId } = operation.context;
log(
- '[internal_getConversationContext] get from operation %s: agentId=%s, topicId=%s, threadId=%s, scope=%s, groupId=%s',
+ '[internal_getConversationContext] get from operation %s: agentId=%s, topicId=%s, threadId=%s, scope=%s, groupId=%s, documentId=%s',
context.operationId,
agentId,
topicId,
threadId,
scope,
groupId,
+ documentId,
);
- return { agentId: agentId!, topicId, threadId, scope, isNew, groupId };
+ // Spread the whole operation context so every bucket-key field carries
+ // through β€” notably `documentId` (page-scoped optimistic writes resolve
+ // to the same `page_<agent>_<documentId>` bucket the editor reads from,
+ // not `page_<agent>_new`) and `subAgentId` (group_agent scope's
+ // subTopicId). Only agentId needs the non-null assertion.
+ return { ...operation.context, agentId: agentId! };
}
}