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): persist topic status when run completes after agent switch (#15084)

Arvin Xu16b932278ef3
diff --git a/src/store/chat/slices/aiChat/actions/conversationControl.ts b/src/store/chat/slices/aiChat/actions/conversationControl.ts
index b5e6d99a92a..40f1b5283fa 100644
--- a/src/store/chat/slices/aiChat/actions/conversationControl.ts
+++ b/src/store/chat/slices/aiChat/actions/conversationControl.ts
@@ -820,7 +820,12 @@ export class ConversationControlActionImpl {
// CC stream resumes so flip it back to `running`. The natural completion
// (`runtime_end` β†’ `writeTopicStatus('active')`) takes over from there.
if (effectiveContext.topicId) {
- void this.#get().updateTopicStatus?.(effectiveContext.topicId, 'running');
+ void this.#get().updateTopicStatus?.({
+ agentId: effectiveContext.agentId,
+ groupId: effectiveContext.groupId,
+ status: 'running',
+ topicId: effectiveContext.topicId,
+ });
}
};
diff --git a/src/store/chat/slices/aiChat/actions/gateway.ts b/src/store/chat/slices/aiChat/actions/gateway.ts
index 2348d303877..2be45b73349 100644
--- a/src/store/chat/slices/aiChat/actions/gateway.ts
+++ b/src/store/chat/slices/aiChat/actions/gateway.ts
@@ -390,7 +390,12 @@ export class GatewayActionImpl {
if (result.topicId) {
this.#get().internal_updateTopicLoading(result.topicId, true);
- void this.#get().updateTopicStatus?.(result.topicId, 'running');
+ void this.#get().updateTopicStatus?.({
+ agentId: context.agentId,
+ groupId: context.groupId,
+ status: 'running',
+ topicId: result.topicId,
+ });
}
// Create a dedicated operation for gateway execution with correct context.
@@ -438,7 +443,12 @@ export class GatewayActionImpl {
this.#get().completeOperation(gatewayOpId);
if (result.topicId) {
this.#get().internal_updateTopicLoading(result.topicId, false);
- void this.#get().updateTopicStatus?.(result.topicId, 'active');
+ void this.#get().updateTopicStatus?.({
+ agentId: execContext.agentId,
+ groupId: execContext.groupId,
+ status: 'active',
+ topicId: result.topicId,
+ });
// Clear running operation from topic metadata (best-effort from frontend;
// if browser was closed, reconnect logic will handle stale entries)
topicService
@@ -528,7 +538,11 @@ export class GatewayActionImpl {
onSessionComplete: () => {
this.#get().completeOperation(gatewayOpId);
this.#get().internal_updateTopicLoading(topicId, false);
- void this.#get().updateTopicStatus?.(topicId, 'active');
+ void this.#get().updateTopicStatus?.({
+ agentId: context.agentId,
+ status: 'active',
+ topicId,
+ });
topicService.updateTopicMetadata(topicId, { runningOperation: null }).catch(() => {});
},
operationId,
diff --git a/src/store/chat/slices/aiChat/actions/gatewayEventHandler.ts b/src/store/chat/slices/aiChat/actions/gatewayEventHandler.ts
index 3bdefb1cf7e..d8291d02472 100644
--- a/src/store/chat/slices/aiChat/actions/gatewayEventHandler.ts
+++ b/src/store/chat/slices/aiChat/actions/gatewayEventHandler.ts
@@ -413,7 +413,13 @@ export const createGatewayEventHandler = (
// Persist a paused marker so the sidebar reflects "waiting on user" across reload.
// Resume back to 'running' is free: approve / reject both spawn a new op via the
// executor entries, which already write 'running'.
- if (context.topicId) void get().updateTopicStatus?.(context.topicId, 'paused');
+ if (context.topicId)
+ void get().updateTopicStatus?.({
+ agentId: context.agentId,
+ groupId: context.groupId,
+ status: 'paused',
+ topicId: context.topicId,
+ });
}
break;
diff --git a/src/store/chat/slices/aiChat/actions/heterogeneousAgentExecutor.ts b/src/store/chat/slices/aiChat/actions/heterogeneousAgentExecutor.ts
index 957106452a2..b2ab0e600b0 100644
--- a/src/store/chat/slices/aiChat/actions/heterogeneousAgentExecutor.ts
+++ b/src/store/chat/slices/aiChat/actions/heterogeneousAgentExecutor.ts
@@ -1196,7 +1196,12 @@ export const executeHeterogeneousAgent = async (
};
const writeTopicStatus = (status: ChatTopicStatus): void => {
if (!context.topicId) return;
- void get().updateTopicStatus?.(context.topicId, status);
+ void get().updateTopicStatus?.({
+ agentId: context.agentId,
+ groupId: context.groupId,
+ status,
+ topicId: context.topicId,
+ });
};
const retryWithoutResume = (error: unknown): boolean => {
if (
diff --git a/src/store/chat/slices/topic/action.ts b/src/store/chat/slices/topic/action.ts
index 55d95a82638..d18d977a4d3 100644
--- a/src/store/chat/slices/topic/action.ts
+++ b/src/store/chat/slices/topic/action.ts
@@ -310,17 +310,47 @@ export class ChatTopicActionImpl {
};
/**
- * Persist the topic's status. Optimistically updates the in-memory map so the
- * sidebar reflects the change immediately; persistence runs fire-and-forget so
- * a transient network blip never tears down the agent run that owns the write.
+ * Persist the topic's status. Optimistically patches the in-memory map so
+ * the sidebar reflects the change immediately; persistence runs
+ * fire-and-forget so a transient network blip never tears down the agent
+ * run that owns the write.
+ *
+ * Pass `agentId`/`groupId` when the call originates from an agent run
+ * rather than the active UI β€” without them, the lookup falls back to the
+ * currently active agent, and a status write arriving after the user has
+ * switched agents lands in the wrong bucket. The DB write is unconditional
+ * so even if no bucket is loaded for this topic, the next refetch picks
+ * up the persisted status.
*/
- updateTopicStatus = async (id: string, status: ChatTopicStatus): Promise<void> => {
- const topic = topicSelectors.getTopicById(id)(this.#get());
- if (!topic || topic.status === status) return;
+ updateTopicStatus = async (params: {
+ agentId?: string;
+ groupId?: string;
+ status: ChatTopicStatus;
+ topicId: string;
+ }): Promise<void> => {
+ const { topicId, status, agentId, groupId } = params;
+ const state = this.#get();
+ const key = topicMapKey({
+ agentId: agentId ?? state.activeAgentId,
+ groupId: groupId ?? state.activeGroupId,
+ });
+ const topic = state.topicDataMap[key]?.items?.find((t) => t.id === topicId);
- this.#get().internal_dispatchTopic({ type: 'updateTopic', id, value: { status } });
+ // Already at the target status β€” both the in-memory and DB writes are no-ops.
+ if (topic?.status === status) return;
- await topicService.updateTopic(id, { status }).catch((err) => {
+ // Scope on the payload routes the write to the owning bucket inside
+ // `internal_dispatchTopic`. A no-op if the bucket isn't loaded; the DB
+ // write below still ensures the status sticks across the next refetch.
+ state.internal_dispatchTopic({
+ type: 'updateTopic',
+ id: topicId,
+ value: { status },
+ agentId,
+ groupId,
+ });
+
+ await topicService.updateTopic(topicId, { status }).catch((err) => {
console.error('[updateTopicStatus] persist failed:', err);
});
};
@@ -728,9 +758,19 @@ export class ChatTopicActionImpl {
return topicId;
};
+ /**
+ * Apply a topic reducer to a bucket in `topicDataMap`. Scope on the payload
+ * (`agentId`/`groupId`) wins; otherwise falls back to the currently active
+ * agent/group bucket. Pass scope on the payload when the write originates
+ * outside the active UI context β€” e.g. an agent run finishing after the
+ * user switched agents (see `updateTopicStatus`).
+ */
internal_dispatchTopic = (payload: ChatTopicDispatch, action?: any): void => {
const { activeAgentId, activeGroupId } = this.#get();
- const key = topicMapKey({ agentId: activeAgentId, groupId: activeGroupId });
+ const key = topicMapKey({
+ agentId: payload.agentId ?? activeAgentId,
+ groupId: payload.groupId ?? activeGroupId,
+ });
const currentData = this.#get().topicDataMap[key];
const nextItems = topicReducer(currentData?.items, payload);
diff --git a/src/store/chat/slices/topic/reducer.ts b/src/store/chat/slices/topic/reducer.ts
index 299661544e3..012ad952888 100644
--- a/src/store/chat/slices/topic/reducer.ts
+++ b/src/store/chat/slices/topic/reducer.ts
@@ -3,18 +3,30 @@ import { produce } from 'immer';
import { type ChatTopic, type CreateTopicParams } from '@/types/topic';
-type AddChatTopicAction = {
+/**
+ * Optional scope on every topic dispatch. When set, `internal_dispatchTopic`
+ * routes the write to the matching bucket in `topicDataMap` regardless of
+ * `activeAgentId` β€” used when an agent run completes after the user has
+ * switched agents and the write must land in the run's *owning* bucket, not
+ * whichever bucket happens to be active.
+ */
+interface ChatTopicScope {
+ agentId?: string;
+ groupId?: string;
+}
+
+type AddChatTopicAction = ChatTopicScope & {
type: 'addTopic';
value: CreateTopicParams & { id?: string };
};
-type UpdateChatTopicAction = {
+type UpdateChatTopicAction = ChatTopicScope & {
id: string;
type: 'updateTopic';
value: Partial<ChatTopic>;
};
-type DeleteChatTopicAction = {
+type DeleteChatTopicAction = ChatTopicScope & {
id: string;
type: 'deleteTopic';
};