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(goal): keep the goal panel on its bound supervision topic (#21824)

Arvin Xu258c65766c75
diff --git a/src/features/AgentGoals/GoalChat/GoalChatProvider.tsx b/src/features/AgentGoals/GoalChat/GoalChatProvider.tsx
index 7bd806dd56e..349868b27e4 100644
--- a/src/features/AgentGoals/GoalChat/GoalChatProvider.tsx
+++ b/src/features/AgentGoals/GoalChat/GoalChatProvider.tsx
@@ -2,6 +2,7 @@ import type { ConversationContext } from '@lobechat/types';
import type { ReactNode } from 'react';
import { memo, useEffect, useMemo, useState } from 'react';
+import { PinnedTopicContext } from '@/features/ChatInput/InputEditor/ReferTopic/pinnedTopic';
import { ConversationProvider } from '@/features/Conversation';
import { useOperationState } from '@/hooks/useOperationState';
import { useAgentStore } from '@/store/agent';
@@ -14,6 +15,11 @@ interface GoalChatProviderProps {
goalId: string;
/** Open a specific conversation when entering the panel (e.g. supervision). */
initialTopicId?: string;
+ /**
+ * The goal is bound to `initialTopicId`: nothing inside the panel may switch
+ * it to another topic (a topic reference opens in the portal instead).
+ */
+ pinned?: boolean;
}
/**
@@ -22,7 +28,7 @@ interface GoalChatProviderProps {
* progress overview into the request (see streamingExecutor).
*/
export const GoalChatProvider = memo<GoalChatProviderProps>(
- ({ agentId, children, goalId, initialTopicId }) => {
+ ({ agentId, children, goalId, initialTopicId, pinned }) => {
const [initialized, setInitialized] = useState(false);
const setActiveAgentId = useAgentStore((s) => s.setActiveAgentId);
const activeTopicId = useChatStore((s) => s.activeTopicId);
@@ -64,9 +70,11 @@ export const GoalChatProvider = memo<GoalChatProviderProps>(
if (!initialized) return null;
return (
- <ConversationProvider context={context} operationState={operationState}>
- {children}
- </ConversationProvider>
+ <PinnedTopicContext value={pinned ? initialTopicId : undefined}>
+ <ConversationProvider context={context} operationState={operationState}>
+ {children}
+ </ConversationProvider>
+ </PinnedTopicContext>
);
},
);
diff --git a/src/features/AgentGoals/GoalChat/Toolbar.tsx b/src/features/AgentGoals/GoalChat/Toolbar.tsx
index 00788539281..239a472dfd8 100644
--- a/src/features/AgentGoals/GoalChat/Toolbar.tsx
+++ b/src/features/AgentGoals/GoalChat/Toolbar.tsx
@@ -17,18 +17,20 @@ interface ToolbarProps {
/** Shown while the open conversation has no titled topic. @default the side chat's title */
fallbackTitle?: string;
onCollapse: () => void;
- /** Hand a new conversation to another panel, after the topic is cleared. */
- onNewTopic?: () => void;
- /** Open a topic picked from history. @default switch this panel to it */
- onSelectTopic?: (topicId: string) => void;
+ /**
+ * The conversation is bound to the goal (its supervision record): no new
+ * conversation and no history, so the panel can never leave that topic.
+ */
+ topicLocked?: boolean;
}
/**
* Topic title + topic menu, then new-conversation + history — laid out like the
* agent conversation header. Shared by the side chat and the supervision record
- * so both goal-page conversations carry the same header.
+ * so both goal-page conversations carry the same header; the record locks it to
+ * its topic.
*/
-const Toolbar = memo<ToolbarProps>(({ fallbackTitle, onCollapse, onNewTopic, onSelectTopic }) => {
+const Toolbar = memo<ToolbarProps>(({ fallbackTitle, onCollapse, topicLocked }) => {
const { t } = useTranslation('chat');
const [topicPopoverOpen, setTopicPopoverOpen] = useState(false);
const agentId = useConversationStore(conversationSelectors.agentId);
@@ -63,6 +65,7 @@ const Toolbar = memo<ToolbarProps>(({ fallbackTitle, onCollapse, onNewTopic, onS
fav={currentTopic.favorite}
status={currentTopic.status}
topicId={currentTopic.id}
+ topicLocked={topicLocked}
topicTitle={currentTopic.title}
/>
)}
@@ -70,67 +73,66 @@ const Toolbar = memo<ToolbarProps>(({ fallbackTitle, onCollapse, onNewTopic, onS
}
right={
<>
- <ActionIcon
- icon={PlusIcon}
- size={DESKTOP_HEADER_ICON_SMALL_SIZE}
- title={t('goalChat.newTopic')}
- onClick={() => {
- // Clear first: the side chat keeps the agent's open topic when it
- // mounts, so a hand-off would otherwise land back on this one.
- void switchTopic(null);
- onNewTopic?.();
- }}
- />
- <Popover
- open={isLoadingTopics ? false : topicPopoverOpen}
- placement="bottomRight"
- trigger="click"
- content={
- hasTopics ? (
- <Flexbox
- gap={4}
- padding={8}
- style={{
- maxHeight: '50vh',
- overflowY: 'auto',
- width: '100%',
- }}
- >
- {topics!.map((topic) => (
- <TopicItem
- active={topic.id === activeTopicId}
- agentId={agentId}
- fav={topic.favorite}
- key={topic.id}
- status={topic.status}
- topicId={topic.id}
- topicTitle={topic.title}
- onClose={() => setTopicPopoverOpen(false)}
- onTopicChange={(id) => (onSelectTopic ? onSelectTopic(id) : switchTopic(id))}
- />
- ))}
- </Flexbox>
- ) : (
- <Flexbox padding={16}>
- <Text type={'secondary'}>{t('goalChat.noTopics')}</Text>
- </Flexbox>
- )
- }
- styles={{
- content: {
- padding: 0,
- width: 240,
- },
- }}
- onOpenChange={setTopicPopoverOpen}
- >
- <ActionIcon
- disabled={isLoadingTopics}
- icon={Clock3Icon}
- loading={isLoadingTopics}
- size={DESKTOP_HEADER_ICON_SMALL_SIZE}
- />
- </Popover>
+ {!topicLocked && (
+ <>
+ <ActionIcon
+ icon={PlusIcon}
+ size={DESKTOP_HEADER_ICON_SMALL_SIZE}
+ title={t('goalChat.newTopic')}
+ onClick={() => switchTopic(null)}
+ />
+ <Popover
+ open={isLoadingTopics ? false : topicPopoverOpen}
+ placement="bottomRight"
+ trigger="click"
+ content={
+ hasTopics ? (
+ <Flexbox
+ gap={4}
+ padding={8}
+ style={{
+ maxHeight: '50vh',
+ overflowY: 'auto',
+ width: '100%',
+ }}
+ >
+ {topics!.map((topic) => (
+ <TopicItem
+ active={topic.id === activeTopicId}
+ agentId={agentId}
+ fav={topic.favorite}
+ key={topic.id}
+ status={topic.status}
+ topicId={topic.id}
+ topicTitle={topic.title}
+ onClose={() => setTopicPopoverOpen(false)}
+ onTopicChange={(id) => switchTopic(id)}
+ />
+ ))}
+ </Flexbox>
+ ) : (
+ <Flexbox padding={16}>
+ <Text type={'secondary'}>{t('goalChat.noTopics')}</Text>
+ </Flexbox>
+ )
+ }
+ styles={{
+ content: {
+ padding: 0,
+ width: 240,
+ },
+ }}
+ onOpenChange={setTopicPopoverOpen}
+ >
+ <ActionIcon
+ disabled={isLoadingTopics}
+ icon={Clock3Icon}
+ loading={isLoadingTopics}
+ size={DESKTOP_HEADER_ICON_SMALL_SIZE}
+ />
+ </Popover>
+ </>
+ )}
<ActionIcon
icon={PanelRightCloseIcon}
size={DESKTOP_HEADER_ICON_SMALL_SIZE}
diff --git a/src/features/AgentGoals/GoalChat/index.tsx b/src/features/AgentGoals/GoalChat/index.tsx
index 8d7468ce7c5..0c9c01fdf86 100644
--- a/src/features/AgentGoals/GoalChat/index.tsx
+++ b/src/features/AgentGoals/GoalChat/index.tsx
@@ -30,11 +30,10 @@ interface ConversationProps {
initialMessage?: string;
onCollapse: () => void;
onInitialMessageConsumed?: () => void;
- onSelectTopic?: (topicId: string) => void;
}
const Conversation = memo<ConversationProps>(
- ({ initialMessage, onCollapse, onInitialMessageConsumed, onSelectTopic }) => {
+ ({ initialMessage, onCollapse, onInitialMessageConsumed }) => {
const useFetchAgentConfig = useAgentStore((s) => s.useFetchAgentConfig);
const currentAgentId = useConversationStore(conversationSelectors.agentId);
@@ -49,7 +48,7 @@ const Conversation = memo<ConversationProps>(
return (
<DragUploadZone style={{ flex: 1, height: '100%' }} onUploadFiles={handleUploadFiles}>
<Flexbox flex={1} height={'100%'} style={{ overflow: 'hidden' }}>
- <Toolbar onCollapse={onCollapse} onSelectTopic={onSelectTopic} />
+ <Toolbar onCollapse={onCollapse} />
<Flexbox flex={1} style={{ overflow: 'hidden' }}>
<ChatList welcome={<Welcome />} />
</Flexbox>
@@ -73,8 +72,6 @@ interface GoalChatProps {
initialTopicId?: string;
onCollapse: () => void;
onInitialMessageConsumed?: () => void;
- /** Open a topic picked from history. @default switch the panel to it in place */
- onSelectTopic?: (topicId: string) => void;
}
/**
@@ -83,21 +80,12 @@ interface GoalChatProps {
* with the current goal overview injected — "how is this going?" just works.
*/
const GoalChat = memo<GoalChatProps>(
- ({
- agentId,
- goalId,
- initialMessage,
- initialTopicId,
- onCollapse,
- onInitialMessageConsumed,
- onSelectTopic,
- }) => (
+ ({ agentId, goalId, initialMessage, initialTopicId, onCollapse, onInitialMessageConsumed }) => (
<GoalChatProvider agentId={agentId} goalId={goalId} initialTopicId={initialTopicId}>
<Conversation
initialMessage={initialMessage}
onCollapse={onCollapse}
onInitialMessageConsumed={onInitialMessageConsumed}
- onSelectTopic={onSelectTopic}
/>
</GoalChatProvider>
),
diff --git a/src/features/AgentGoals/GoalChat/useGoalTopicMenu.test.ts b/src/features/AgentGoals/GoalChat/useGoalTopicMenu.test.ts
index 0ca87c0c0d7..4966955038b 100644
--- a/src/features/AgentGoals/GoalChat/useGoalTopicMenu.test.ts
+++ b/src/features/AgentGoals/GoalChat/useGoalTopicMenu.test.ts
@@ -9,7 +9,14 @@ import { useGoalTopicMenu } from './useGoalTopicMenu';
const navigateMock = vi.hoisted(() => vi.fn());
vi.mock('@/features/PageEditor/Copilot/TopicSelector/useDropdownMenu', () => ({
- useDropdownMenu: () => () => [{ key: 'rename' }, { key: 'delete' }],
+ useDropdownMenu: () => () => [
+ { key: 'markCompleted' },
+ { key: 'rename' },
+ { type: 'divider' },
+ { key: 'duplicate' },
+ { type: 'divider' },
+ { key: 'delete' },
+ ],
}));
vi.mock('@/features/Workspace/useWorkspaceAwareNavigate', () => ({
@@ -26,11 +33,31 @@ describe('useGoalTopicMenu', () => {
expect(items.map((item) => item.key ?? item.type)).toEqual([
'openTopic',
'divider',
+ 'markCompleted',
'rename',
+ 'divider',
+ 'duplicate',
+ 'divider',
'delete',
]);
items[0].onClick?.();
expect(navigateMock).toHaveBeenCalledWith('/agent/agt_1/tpc_1');
});
+
+ // Archive, duplicate and delete each switch the shared active topic, which
+ // would move a goal's locked panel off its supervision record.
+ it('drops the actions that leave the topic when the panel is locked', () => {
+ const { result } = renderHook(() =>
+ useGoalTopicMenu({
+ agentId: 'agt_1',
+ topicId: 'tpc_1',
+ topicLocked: true,
+ topicTitle: 'Progress',
+ }),
+ );
+
+ const items = result.current() as { key?: string; type?: string }[];
+ expect(items.map((item) => item.key ?? item.type)).toEqual(['openTopic', 'divider', 'rename']);
+ });
});
diff --git a/src/features/AgentGoals/GoalChat/useGoalTopicMenu.ts b/src/features/AgentGoals/GoalChat/useGoalTopicMenu.ts
index ddd1de648bb..666909f7d8f 100644
--- a/src/features/AgentGoals/GoalChat/useGoalTopicMenu.ts
+++ b/src/features/AgentGoals/GoalChat/useGoalTopicMenu.ts
@@ -15,15 +15,38 @@ export interface GoalTopicMenuParams {
fav?: boolean;
status?: ChatTopicStatus | null;
topicId: string;
+ /** The goal is bound to this topic: drop the actions that leave or end it. */
+ topicLocked?: boolean;
topicTitle: string;
}
+/**
+ * Archiving or deleting the topic switches the shared active topic to a blank
+ * one, and duplicating it switches to the copy: each would move a locked panel
+ * off the goal's topic (and archiving or deleting would end the record itself).
+ */
+const LOCKED_HIDDEN_KEYS = new Set(['delete', 'duplicate', 'markCompleted']);
+
+/** Drop leading, trailing and doubled dividers left behind by a filter. */
+const tidyDividers = (items: DropdownItem[]) => {
+ const isDivider = (item?: DropdownItem) =>
+ (item as { type?: string } | undefined)?.type === 'divider';
+ const kept: DropdownItem[] = [];
+ for (const item of items) {
+ if (isDivider(item) && (kept.length === 0 || isDivider(kept.at(-1)))) continue;
+ kept.push(item);
+ }
+ while (isDivider(kept.at(-1))) kept.pop();
+ return kept;
+};
+
/** The standard topic menu, led by a jump to the topic's own conversation page. */
export const useGoalTopicMenu = ({
agentId,
fav,
status,
topicId,
+ topicLocked,
topicTitle,
}: GoalTopicMenuParams): (() => DropdownItem[]) => {
const { t } = useTranslation('chat');
@@ -47,8 +70,10 @@ export const useGoalTopicMenu = ({
onClick: () => navigate(AGENT_CHAT_TOPIC_URL(agentId, topicId)),
},
{ type: 'divider' as const },
- ...topicMenu(),
+ ...(topicLocked
+ ? tidyDividers(topicMenu().filter((item) => !LOCKED_HIDDEN_KEYS.has(String(item?.key))))
+ : topicMenu()),
],
- [agentId, navigate, t, topicId, topicMenu],
+ [agentId, navigate, t, topicId, topicLocked, topicMenu],
);
};
diff --git a/src/features/AgentGoals/GoalDetailPage.test.tsx b/src/features/AgentGoals/GoalDetailPage.test.tsx
index 54182521071..93d2b7cf800 100644
--- a/src/features/AgentGoals/GoalDetailPage.test.tsx
+++ b/src/features/AgentGoals/GoalDetailPage.test.tsx
@@ -134,25 +134,17 @@ vi.mock('./GoalChat', () => ({
agentId,
initialMessage,
initialTopicId,
- onSelectTopic,
}: {
agentId: string;
initialMessage?: string;
initialTopicId?: string;
- onSelectTopic?: (topicId: string) => void;
}) => (
<div
data-agent-id={agentId}
data-initial-message={initialMessage}
data-initial-topic-id={initialTopicId}
data-testid="goal-chat"
- >
- {onSelectTopic && (
- <button data-testid="goal-chat-pick-record" onClick={() => onSelectTopic('tpc_manager')}>
- pick record
- </button>
- )}
- </div>
+ />
),
}));
vi.mock('./GoalDetailActions', () => ({ default: () => null }));
@@ -288,36 +280,17 @@ describe('GoalDetailPage', () => {
expect(screen.queryByTestId('goal-follow-up')).not.toBeInTheDocument();
});
- // The side chat stays one menu item away, for a question that should not land
- // in the manager's own record.
- it('hands a managed goal back to its editable conversation from the record', () => {
+ // A goal bound to its supervision record keeps the panel on that topic: the
+ // record offers no way out to a new or another conversation.
+ it('keeps a bound goal’s panel on its supervision record', () => {
render(<GoalDetailPage agentId={'agt_manager'} goalId={'goal_1'} />);
fireEvent.click(screen.getByTestId('goal-supervisor-toggle'));
- expect(screen.getByTestId('goal-supervision')).toBeInTheDocument();
-
- fireEvent.click(screen.getByTestId('goal-supervision-open-chat'));
-
- expect(screen.queryByTestId('goal-supervision')).not.toBeInTheDocument();
- expect(screen.getByTestId('goal-chat')).toHaveAttribute('data-agent-id', 'agt_manager');
- expect(screen.getByTestId('goal-right-panel')).toHaveAttribute('data-expand', 'true');
- });
-
- // Both panels share one header with a history list: another topic picked from
- // the record opens as the editable side conversation on that topic, and the
- // record picked from the side conversation opens as the record again.
- it('routes a topic picked from history to the panel that owns it', () => {
- render(<GoalDetailPage agentId={'agt_manager'} goalId={'goal_1'} />);
-
- fireEvent.click(screen.getByTestId('goal-supervisor-toggle'));
- fireEvent.click(screen.getByTestId('goal-supervision-pick-topic'));
-
- expect(screen.queryByTestId('goal-supervision')).not.toBeInTheDocument();
- expect(screen.getByTestId('goal-chat')).toHaveAttribute('data-initial-topic-id', 'tpc_other');
-
- fireEvent.click(screen.getByTestId('goal-chat-pick-record'));
expect(screen.getByTestId('goal-supervision')).toHaveTextContent('tpc_manager');
+ expect(screen.queryByTestId('goal-supervision-open-chat')).not.toBeInTheDocument();
+ expect(screen.queryByTestId('goal-supervision-pick-topic')).not.toBeInTheDocument();
+ expect(screen.queryByTestId('goal-chat')).not.toBeInTheDocument();
});
it('leaves drill-downs to the workspace portal host on the agent-less route', () => {
diff --git a/src/features/AgentGoals/GoalDetailPage.tsx b/src/features/AgentGoals/GoalDetailPage.tsx
index 1a96ef1c5af..12f76359544 100644
--- a/src/features/AgentGoals/GoalDetailPage.tsx
+++ b/src/features/AgentGoals/GoalDetailPage.tsx
@@ -222,18 +222,6 @@ const GoalDetailPage = memo<GoalDetailPageProps>(({ agentId, goalId }) => {
// message landing, so the reply is never invisible behind a drill-down. A
// managed goal whose main Agent has no supervision record yet gets one from
// this very turn: the panel opens now and moves there once it exists.
- // A topic picked from either panel's history: the supervision record opens as
- // the record, any other topic as the side conversation on that topic.
- const selectTopic = (topicId: string) => {
- if (!supervisingAgentId || topicId === chat.topicId) return;
- clearPortalStack();
- chat.openConversation(
- managerConversation?.topicId === topicId
- ? { agentId: managerConversation.agentId, topicId }
- : { agentId: supervisingAgentId, topicId },
- );
- };
-
const revealConversation = () => {
setGraphFullscreen(false);
clearPortalStack();
@@ -410,20 +398,17 @@ const GoalDetailPage = memo<GoalDetailPageProps>(({ agentId, goalId }) => {
hasWorkspaceSidePanel ? null : (
<PortalContent />
)
- ) : supervisingAgentId &&
- chat.topicId &&
- chat.topicId === managerConversation?.topicId ? (
+ ) : managerConversation ? (
+ // A goal bound to its supervision record keeps the panel on that
+ // topic: no side conversation, no other topic can replace it.
<GoalSupervision
- agentId={supervisingAgentId}
+ agentId={managerConversation.agentId}
goalId={goalId}
initialMessage={chat.initialMessage}
- key={`${goalId}:${supervisingAgentId}:${chat.request}`}
- topicId={chat.topicId}
+ key={`${goalId}:${managerConversation.agentId}:${managerConversation.topicId}:${chat.request}`}
+ topicId={managerConversation.topicId}
onCollapse={() => chat.setOpen(false)}
onInitialMessageConsumed={chat.consumeInitialMessage}
- // A question that should not land in the manager's own record.
- onOpenChat={() => chat.openConversation({ agentId: supervisingAgentId })}
- onSelectTopic={selectTopic}
/>
) : chat.awaitingRecord ? (
// The supervision record is on its way: show it loading, not the
@@ -440,7 +425,6 @@ const GoalDetailPage = memo<GoalDetailPageProps>(({ agentId, goalId }) => {
key={`${goalId}:${supervisingAgentId}:${chat.request}`}
onCollapse={() => chat.setOpen(false)}
onInitialMessageConsumed={chat.consumeInitialMessage}
- onSelectTopic={managerConversation ? selectTopic : undefined}
/>
) : null}
</RightPanel>
diff --git a/src/features/AgentGoals/GoalSupervision.tsx b/src/features/AgentGoals/GoalSupervision.tsx
index 45f9e0c5b80..872799cdd14 100644
--- a/src/features/AgentGoals/GoalSupervision.tsx
+++ b/src/features/AgentGoals/GoalSupervision.tsx
@@ -19,10 +19,6 @@ interface GoalSupervisionProps {
initialMessage?: string;
onCollapse: () => void;
onInitialMessageConsumed?: () => void;
- /** Hand the panel back to the agent's editable side conversation. */
- onOpenChat?: () => void;
- /** Open a topic picked from the conversation history. */
- onSelectTopic?: (topicId: string) => void;
topicId: string;
}
@@ -55,8 +51,6 @@ export const GoalSupervision = ({
initialMessage,
onCollapse,
onInitialMessageConsumed,
- onOpenChat,
- onSelectTopic,
topicId,
}: GoalSupervisionProps) => {
const { t } = useTranslation('chat');
@@ -71,15 +65,14 @@ export const GoalSupervision = ({
[],
);
return (
- <GoalChatProvider agentId={agentId} goalId={goalId} initialTopicId={topicId}>
+ <GoalChatProvider pinned agentId={agentId} goalId={goalId} initialTopicId={topicId}>
<Flexbox height={'100%'} style={{ overflow: 'hidden' }}>
- {/* The side chat's header: the record is a topic like any other, so it
- gets the same title, topic menu, new-conversation and history. */}
+ {/* The side chat's header, locked to the record: the goal is bound to
+ this topic, so the panel offers no new conversation and no history. */}
<Toolbar
+ topicLocked
fallbackTitle={agentTitle || t('goalProcess.manager.title')}
onCollapse={onCollapse}
- onNewTopic={onOpenChat}
- onSelectTopic={onSelectTopic}
/>
<Flexbox flex={1} style={{ minHeight: 0, overflow: 'hidden' }}>
<ChatList disableActionsBar itemContent={itemContent} welcome={<WaitingForFirstRun />} />
diff --git a/src/features/ChatInput/InputEditor/ReferTopic/ReferTopicView.tsx b/src/features/ChatInput/InputEditor/ReferTopic/ReferTopicView.tsx
index faa343f51c2..efca02ed54d 100644
--- a/src/features/ChatInput/InputEditor/ReferTopic/ReferTopicView.tsx
+++ b/src/features/ChatInput/InputEditor/ReferTopic/ReferTopicView.tsx
@@ -7,6 +7,7 @@ import { useChatStore } from '@/store/chat';
import { topicSelectors } from '@/store/chat/slices/topic/selectors';
import { TAG_MARGIN_INLINE_END } from '../constants';
+import { usePinnedTopic } from './pinnedTopic';
export interface ReferTopicViewProps {
fallbackTitle?: string;
@@ -16,16 +17,25 @@ export interface ReferTopicViewProps {
export const ReferTopicView = memo<ReferTopicViewProps>(({ topicId, fallbackTitle }) => {
const { t } = useTranslation('topic');
const title = useChatStore(topicSelectors.getTopicById(topicId))?.title || fallbackTitle;
- const switchTopic = useChatStore((s) => s.switchTopic);
+ const [switchTopic, openTopicInPortal] = useChatStore((s) => [
+ s.switchTopic,
+ s.openTopicInPortal,
+ ]);
+ const pinnedTopicId = usePinnedTopic();
const handleClick = useCallback(
(e: React.MouseEvent) => {
e.stopPropagation();
- if (topicId) {
- switchTopic(topicId);
+ if (!topicId) return;
+ // A surface bound to one topic must not be switched away from it: show
+ // the referenced topic in the portal, which closes back onto that topic.
+ if (pinnedTopicId) {
+ if (topicId !== pinnedTopicId) openTopicInPortal(topicId);
+ return;
}
+ switchTopic(topicId);
},
- [switchTopic, topicId],
+ [openTopicInPortal, pinnedTopicId, switchTopic, topicId],
);
return (
diff --git a/src/features/ChatInput/InputEditor/ReferTopic/pinnedTopic.ts b/src/features/ChatInput/InputEditor/ReferTopic/pinnedTopic.ts
new file mode 100644
index 00000000000..7f989df6007
--- /dev/null
+++ b/src/features/ChatInput/InputEditor/ReferTopic/pinnedTopic.ts
@@ -0,0 +1,12 @@
+'use client';
+
+import { createContext, use } from 'react';
+
+/**
+ * The topic a conversation surface is bound to. Inside such a surface (a goal's
+ * supervision record) switching the shared active topic would replace that
+ * conversation in place, so a topic reference opens beside it instead.
+ */
+export const PinnedTopicContext = createContext<string | undefined>(undefined);
+
+export const usePinnedTopic = () => use(PinnedTopicContext);
diff --git a/src/features/Conversation/Markdown/plugins/ReferTopic/Render.test.tsx b/src/features/Conversation/Markdown/plugins/ReferTopic/Render.test.tsx
index 3b676dc6289..ba88d242461 100644
--- a/src/features/Conversation/Markdown/plugins/ReferTopic/Render.test.tsx
+++ b/src/features/Conversation/Markdown/plugins/ReferTopic/Render.test.tsx
@@ -5,6 +5,7 @@ import { fireEvent, render, renderHook, screen } from '@testing-library/react';
import ReactMarkdown from 'react-markdown';
import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { PinnedTopicContext } from '@/features/ChatInput/InputEditor/ReferTopic/pinnedTopic';
import { useChatStore } from '@/store/chat';
import { useMarkdown } from '../../../Messages/User/useMarkdown';
@@ -21,6 +22,7 @@ vi.mock('react-i18next', () => ({
describe('ReferTopicRender', () => {
beforeEach(() => {
useChatStore.setState({
+ openTopicInPortal: vi.fn(),
switchTopic: vi.fn(),
topicDataMap: {},
});
@@ -43,6 +45,29 @@ describe('ReferTopicRender', () => {
expect(useChatStore.getState().switchTopic).toHaveBeenCalledWith('topic-id');
});
+ // In a goal's supervision record the shared active topic is the record
+ // itself: switching it replaced the record in place and stranded the panel
+ // on the referenced topic until a refresh.
+ it('opens the reference in the portal inside a surface pinned to its topic', () => {
+ render(
+ <PinnedTopicContext value="record-topic">
+ <Render
+ id="message-id"
+ node={{ properties: { id: 'topic-id', name: 'Referenced topic' } }}
+ tagName="refer_topic"
+ type="refer_topic"
+ >
+ {null}
+ </Render>
+ </PinnedTopicContext>,
+ );
+
+ fireEvent.click(screen.getByText('Referenced topic'));
+
+ expect(useChatStore.getState().switchTopic).not.toHaveBeenCalled();
+ expect(useChatStore.getState().openTopicInPortal).toHaveBeenCalledWith('topic-id');
+ });
+
it('falls back to plain text when the topic id is missing', () => {
render(
<Render