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(conversation): anchor subtopic forks on the row tail (#20532)

Arvin Xuc6c7086eba25
diff --git a/src/features/Conversation/Messages/components/MessageActionBar/actions/branching.ts b/src/features/Conversation/Messages/components/MessageActionBar/actions/branching.ts
index 660280f84c2..5cfb9ddfc53 100644
--- a/src/features/Conversation/Messages/components/MessageActionBar/actions/branching.ts
+++ b/src/features/Conversation/Messages/components/MessageActionBar/actions/branching.ts
@@ -3,6 +3,7 @@ import { Split } from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
+import { useConversationStore } from '@/features/Conversation/store';
import { useChatStore } from '@/store/chat';
import { defineAction } from '../defineAction';
@@ -12,7 +13,11 @@ export const branchingAction = defineAction({
useBuild: (ctx) => {
const { t } = useTranslation('common');
- const [topic, openThreadCreator] = useChatStore((s) => [s.activeTopicId, s.openThreadCreator]);
+ const topic = useChatStore((s) => s.activeTopicId);
+ // Go through the Conversation store's `openThreadCreator` so the anchor is
+ // resolved against this conversation's rows (see `dataSelectors.rowTailId`);
+ // the raw ChatStore action would anchor on the row id β€” the chain head.
+ const openThreadCreator = useConversationStore((s) => s.openThreadCreator);
return useMemo(
() => ({
diff --git a/src/features/Conversation/store/slices/data/selectors.test.ts b/src/features/Conversation/store/slices/data/selectors.test.ts
index 124847503f7..69d4ec86d29 100644
--- a/src/features/Conversation/store/slices/data/selectors.test.ts
+++ b/src/features/Conversation/store/slices/data/selectors.test.ts
@@ -128,3 +128,27 @@ describe('getBlockMetadata', () => {
expect(dataSelectors.getBlockMetadata('missing')(stateWith([]))).toBeUndefined();
});
});
+
+describe('rowTailId', () => {
+ // A steered row keeps its host id (`g1`) while rendering the continuation (`g2`)
+ // underneath it β€” the row tail is what the user is actually looking at.
+ const steeredRow = stateWith([
+ { id: 'u1', role: 'user' },
+ { id: 'g1', role: 'assistantGroup' },
+ { id: 's1', metadata: { steer: true }, role: 'user' },
+ { id: 'g2', role: 'assistantGroup' },
+ { id: 'u2', role: 'user' },
+ { id: 'g3', role: 'assistantGroup' },
+ ]);
+
+ it('resolves the host and a steer message to the last continuation of that row', () => {
+ expect(dataSelectors.rowTailId('g1')(steeredRow)).toBe('g2');
+ expect(dataSelectors.rowTailId('s1')(steeredRow)).toBe('g2');
+ });
+
+ it('resolves the row tail and a plain row to themselves', () => {
+ expect(dataSelectors.rowTailId('g2')(steeredRow)).toBe('g2');
+ expect(dataSelectors.rowTailId('u2')(steeredRow)).toBe('u2');
+ expect(dataSelectors.rowTailId('g3')(steeredRow)).toBe('g3');
+ });
+});
diff --git a/src/features/Conversation/store/slices/data/selectors.ts b/src/features/Conversation/store/slices/data/selectors.ts
index 4e38ff177f4..d1429fce33d 100644
--- a/src/features/Conversation/store/slices/data/selectors.ts
+++ b/src/features/Conversation/store/slices/data/selectors.ts
@@ -173,6 +173,23 @@ const rowMemberIds = (id: string) => (s: State) =>
const hostRowOf = (id: string) => (s: State) =>
collectSteerChains(s.displayMessages).hostOf.get(id) ?? id;
+/**
+ * The last message of the row that contains `id`.
+ *
+ * A row's id is its host β€” the *first* turn of the row. Steered continuations
+ * (see `collectSteerChains`) render inside that same row, so the row's id and
+ * the message the user is actually looking at are two different things. Any
+ * action that must act on what the user sees has to resolve to the row tail;
+ * otherwise it silently acts on the chain head. A plain (non-chained) row
+ * resolves to itself, so this is a no-op there.
+ */
+const rowTailId = (id: string) => (s: State) => {
+ const { byHost, hostOf } = collectSteerChains(s.displayMessages);
+ const hostId = hostOf.get(id) ?? id;
+
+ return byHost.get(hostId)?.continuations.at(-1)?.groupId ?? hostId;
+};
+
const collectDeletableMessageIds = (message: UIChatMessage | undefined): string[] => {
if (!message) return [];
if ((message.role !== 'assistantGroup' && message.role !== 'supervisor') || !message.children) {
@@ -351,6 +368,7 @@ export const dataSelectors = {
messagesInit,
pendingInterventions,
rowMemberIds,
+ rowTailId,
skipFetch,
taskCallbackTaskIds,
workSummariesByRootOperationId,
diff --git a/src/features/Conversation/store/slices/generation/action.test.ts b/src/features/Conversation/store/slices/generation/action.test.ts
index 68e8522d66a..6c2c16bf69b 100644
--- a/src/features/Conversation/store/slices/generation/action.test.ts
+++ b/src/features/Conversation/store/slices/generation/action.test.ts
@@ -34,6 +34,7 @@ const mockIsGatewayModeEnabled = vi.fn(() => false);
const mockExecuteGatewayAgent = vi.fn();
const mockUpdateTopicMetadata = vi.fn();
const mockUpdateTopicStatus = vi.fn();
+const mockOpenThreadCreator = vi.fn();
const mockSourceTopic = {
status: 'scheduled',
metadata: { scheduledRun: { kind: 'resume_after_rate_limit' } },
@@ -69,6 +70,7 @@ vi.mock('@/store/chat', () => ({
isGatewayModeEnabled: mockIsGatewayModeEnabled,
executeGatewayAgent: mockExecuteGatewayAgent,
internal_dispatchTopic: vi.fn(),
+ openThreadCreator: mockOpenThreadCreator,
updateTopicMetadata: mockUpdateTopicMetadata,
updateTopicStatus: mockUpdateTopicStatus,
})),
@@ -86,6 +88,32 @@ describe('Generation Actions', () => {
vi.clearAllTimers();
});
+ describe('openThreadCreator', () => {
+ it('anchors a steered row on its tail instead of the row/host id', () => {
+ const store = createStore({
+ context: { agentId: 'session-1', threadId: null, topicId: 'topic-1' },
+ });
+
+ // Steered row: host `g1` + steer message `s1` + continuation `g2`. The row
+ // renders as one block whose id is `g1` (the chain head), so passing the raw
+ // id anchored the fork at the top of the row instead of where the user is.
+ store.setState({
+ displayMessages: [
+ { content: '', createdAt: 0, id: 'u1', role: 'user' },
+ { content: '', createdAt: 0, id: 'g1', role: 'assistantGroup' },
+ { content: '', createdAt: 0, id: 's1', metadata: { steer: true }, role: 'user' },
+ { content: '', createdAt: 0, id: 'g2', role: 'assistantGroup' },
+ ] as any,
+ });
+
+ act(() => {
+ store.getState().openThreadCreator('g1');
+ });
+
+ expect(mockOpenThreadCreator).toHaveBeenCalledWith('g2');
+ });
+ });
+
describe('cancelHeteroContinuation', () => {
it('does not overwrite a source topic that has already started running', async () => {
mockSourceTopic.status = 'running';
@@ -1875,7 +1903,10 @@ describe('Generation Actions', () => {
expect(executeHeterogeneousAgentSpy).toHaveBeenCalledWith(
expect.any(Function),
expect.objectContaining({
- heterogeneousProvider: expect.objectContaining({ model: pinnedModel, type: providerType }),
+ heterogeneousProvider: expect.objectContaining({
+ model: pinnedModel,
+ type: providerType,
+ }),
}),
);
});
diff --git a/src/features/Conversation/store/slices/generation/action.ts b/src/features/Conversation/store/slices/generation/action.ts
index e98a64c7623..ea39aab56da 100644
--- a/src/features/Conversation/store/slices/generation/action.ts
+++ b/src/features/Conversation/store/slices/generation/action.ts
@@ -56,6 +56,7 @@ import { getUserStoreState } from '@/store/user';
import { userProfileSelectors } from '@/store/user/selectors';
import { type Store as ConversationStore } from '../../action';
+import { dataSelectors } from '../data/selectors';
import { MAX_HETERO_AUTO_RETRIES } from './heteroRetryConfig';
const buildRetryInitialContext = (editorData: Record<string, any> | null | undefined) => {
@@ -1154,7 +1155,11 @@ export const generationSlice: StateCreator<
openThreadCreator: (messageId: string) => {
const chatStore = useChatStore.getState();
- chatStore.openThreadCreator(messageId);
+ // Anchor the subtopic on the row's tail. A steered row's id is its host (the
+ // chain's *first* turn) while the user is looking at the chain's last turn β€”
+ // passing the raw id anchors the fork at the top of the row instead of where
+ // the user is. See `dataSelectors.rowTailId`.
+ chatStore.openThreadCreator(dataSelectors.rowTailId(messageId)(get()));
},
internal_beginHeteroOverloadWait: (scopeId: string) => {