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): restore embedded conversation toolbar (#18381)

Arvin Xua621d0e3948e
diff --git a/src/features/FloatingChatPanel/index.test.tsx b/src/features/FloatingChatPanel/index.test.tsx
index 9c322e29466..ff151591900 100644
--- a/src/features/FloatingChatPanel/index.test.tsx
+++ b/src/features/FloatingChatPanel/index.test.tsx
@@ -82,7 +82,12 @@ vi.mock('@lobehub/ui', () => ({
}));
const mergedHooksCaptured = vi.hoisted(() => ({
- current: undefined as undefined | { onBeforeSendMessage?: () => Promise<void> },
+ current: undefined as
+ | undefined
+ | {
+ onBeforeSendMessage?: () => Promise<void>;
+ onTopicCreated?: (topicId: string) => Promise<void>;
+ },
}));
vi.mock('@/features/Conversation', () => ({
@@ -154,6 +159,29 @@ vi.mock('@/hooks/useOperationState', () => ({
useOperationState: () => undefined,
}));
+vi.mock('@/features/PageEditor/Copilot/Toolbar', () => ({
+ default: ({
+ onTopicChange,
+ topicId,
+ }: {
+ onTopicChange?: (topicId: string | null) => void;
+ topicId?: string | null;
+ }) => (
+ <header data-testid="copilot-toolbar" data-topic-id={topicId ?? 'new'}>
+ <button data-testid="toolbar-new-topic" onClick={() => onTopicChange?.(null)}>
+ New topic
+ </button>
+ <button data-testid="toolbar-history-topic" onClick={() => onTopicChange?.('topic-2')}>
+ History topic
+ </button>
+ </header>
+ ),
+}));
+
+vi.mock('@/features/PageEditor/RightPanel/OverrideContext', () => ({
+ PageAgentPanelOverrideProvider: ({ children }: { children?: ReactNode }) => children,
+}));
+
vi.mock('@/features/Conversation/hooks/useChatFollowUp', () => ({
useChatFollowUp: () => ({}),
}));
@@ -287,6 +315,50 @@ describe('FloatingChatPanel', () => {
expect(queryByTestId('floating-chat-panel-collapse-button')).toBeNull();
});
+ it('binds the embedded toolbar topic selection to the conversation context', () => {
+ const { getByTestId, queryByTestId } = render(
+ <FloatingChatPanel agentId="agent-1" mode="embedded" topicId="topic-1" />,
+ );
+
+ expect(getByTestId('copilot-toolbar')).toHaveAttribute('data-topic-id', 'topic-1');
+ expect(JSON.parse(getByTestId('provider').dataset.context!)).toMatchObject({
+ isolatedTopic: true,
+ topicId: 'topic-1',
+ });
+
+ fireEvent.click(getByTestId('toolbar-history-topic'));
+
+ expect(getByTestId('copilot-toolbar')).toHaveAttribute('data-topic-id', 'topic-2');
+ expect(JSON.parse(getByTestId('provider').dataset.context!)).toMatchObject({
+ isolatedTopic: true,
+ topicId: 'topic-2',
+ });
+ expect(queryByTestId('floating-panel-shell')).toBeNull();
+ });
+
+ it('keeps a newly created embedded topic bound to the toolbar and conversation', async () => {
+ const { getByTestId } = render(
+ <FloatingChatPanel agentId="agent-1" mode="embedded" topicId="topic-1" />,
+ );
+
+ fireEvent.click(getByTestId('toolbar-new-topic'));
+ expect(getByTestId('copilot-toolbar')).toHaveAttribute('data-topic-id', 'new');
+ expect(JSON.parse(getByTestId('provider').dataset.context!)).toMatchObject({
+ isolatedTopic: true,
+ topicId: null,
+ });
+
+ await act(async () => {
+ await mergedHooksCaptured.current?.onTopicCreated?.('topic-created');
+ });
+
+ expect(getByTestId('copilot-toolbar')).toHaveAttribute('data-topic-id', 'topic-created');
+ expect(JSON.parse(getByTestId('provider').dataset.context!)).toMatchObject({
+ isolatedTopic: true,
+ topicId: 'topic-created',
+ });
+ });
+
it('renders a minimal ChatInput while collapsed (no left/right actions)', () => {
const { getByTestId } = render(<FloatingChatPanel agentId="a" topicId="t" />);
const input = getByTestId('chat-input');
diff --git a/src/features/FloatingChatPanel/index.tsx b/src/features/FloatingChatPanel/index.tsx
index fc95168786f..0ee8e1c57e9 100644
--- a/src/features/FloatingChatPanel/index.tsx
+++ b/src/features/FloatingChatPanel/index.tsx
@@ -17,6 +17,8 @@ import {
import { useChatFollowUp } from '@/features/Conversation/hooks/useChatFollowUp';
import { type ConversationContext, type MessagesChangeMeta } from '@/features/Conversation/types';
import { mergeConversationHooks } from '@/features/Conversation/utils/mergeConversationHooks';
+import CopilotToolbar from '@/features/PageEditor/Copilot/Toolbar';
+import { PageAgentPanelOverrideProvider } from '@/features/PageEditor/RightPanel/OverrideContext';
import { useOperationState } from '@/hooks/useOperationState';
import { useActionsBarConfig } from '@/routes/(main)/agent/features/Conversation/useActionsBarConfig';
import { useAgentStore } from '@/store/agent';
@@ -147,17 +149,21 @@ const FloatingChatPanel = memo<FloatingChatPanelProps>(
}) => {
useSingleInstanceGuard();
const { t } = useTranslation('chat');
+ const isEmbedded = mode === 'embedded';
+ const [embeddedTopicId, setEmbeddedTopicId] = useState<string | null>(topicId);
+ const activeTopicId = isEmbedded ? embeddedTopicId : topicId;
const context = useMemo<ConversationContext>(
() => ({
agentId,
...(agentDocumentId ? { agentDocumentId } : {}),
...(documentId ? { documentId } : {}),
+ ...(isEmbedded ? { isolatedTopic: true } : {}),
scope: 'main',
threadId: null,
- topicId,
+ topicId: activeTopicId,
}),
- [agentId, agentDocumentId, documentId, topicId],
+ [activeTopicId, agentDocumentId, agentId, documentId, isEmbedded],
);
const chatKey = useMemo(() => messageMapKey(context), [context]);
@@ -180,8 +186,6 @@ const FloatingChatPanel = memo<FloatingChatPanelProps>(
const operationState = useOperationState(context);
const defaultActionsBar = useActionsBarConfig();
const resolvedActionsBar = actionsBar ?? defaultActionsBar;
- const isEmbedded = mode === 'embedded';
-
const [isCollapsed, setIsCollapsed] = useState(!defaultOpen);
const [activeSnapPoint, setActiveSnapPoint] = useState<number>(MID_SNAP_POINT);
@@ -217,10 +221,13 @@ const FloatingChatPanel = memo<FloatingChatPanelProps>(
onBeforeSendMessage: async () => {
expand();
},
+ onTopicCreated: (createdTopicId) => {
+ if (isEmbedded) setEmbeddedTopicId(createdTopicId);
+ },
},
chatFollowUpHooks,
),
- [hooks, chatFollowUpHooks, expand],
+ [chatFollowUpHooks, expand, hooks, isEmbedded],
);
const collapseAction = (
@@ -279,6 +286,9 @@ const FloatingChatPanel = memo<FloatingChatPanelProps>(
>
{isEmbedded ? (
<>
+ <PageAgentPanelOverrideProvider defaultExpand>
+ <CopilotToolbar topicId={activeTopicId} onTopicChange={setEmbeddedTopicId} />
+ </PageAgentPanelOverrideProvider>
<ChatBody />
<InputRow isCollapsed={false} showExpandBar={false} onExpand={expand} />
</>
diff --git a/src/features/PageEditor/Copilot/Toolbar.tsx b/src/features/PageEditor/Copilot/Toolbar.tsx
index 0a397430378..2e75bbdde5e 100644
--- a/src/features/PageEditor/Copilot/Toolbar.tsx
+++ b/src/features/PageEditor/Copilot/Toolbar.tsx
@@ -12,20 +12,26 @@ import { topicSelectors } from '@/store/chat/slices/topic/selectors';
import { usePageAgentPanelControl, usePageAgentPanelOverride } from '../RightPanel/OverrideContext';
import TopicItem from './TopicSelector/TopicItem';
-const CopilotToolbar = memo(() => {
+interface CopilotToolbarProps {
+ onTopicChange?: (topicId: string | null) => void;
+ topicId?: string | null;
+}
+
+const CopilotToolbar = memo<CopilotToolbarProps>(({ onTopicChange, topicId }) => {
const { t } = useTranslation('topic');
const [topicPopoverOpen, setTopicPopoverOpen] = useState(false);
const agentId = useConversationStore(conversationSelectors.agentId);
useChatStore((s) => s.useFetchTopics)(true, { agentId });
- const [activeTopicId, switchTopic, topics] = useChatStore((s) => [
+ const [globalActiveTopicId, switchTopic, topics] = useChatStore((s) => [
s.activeTopicId,
s.switchTopic,
- topicSelectors.currentTopics(s),
+ topicSelectors.getTopicsByAgentId(agentId)(s),
]);
- const currentTopic = useChatStore(topicSelectors.currentActiveTopic);
+ const activeTopicId = topicId === undefined ? globalActiveTopicId : topicId;
+ const currentTopic = topics?.find((topic) => topic.id === activeTopicId);
const { toggle: togglePageAgentPanel } = usePageAgentPanelControl();
const hasOverride = !!usePageAgentPanelOverride();
@@ -55,7 +61,9 @@ const CopilotToolbar = memo(() => {
icon={PlusIcon}
size={DESKTOP_HEADER_ICON_SMALL_SIZE}
title={t('actions.addNewTopic')}
- onClick={() => switchTopic(null, { scope: 'page' })}
+ onClick={() =>
+ onTopicChange ? onTopicChange(null) : switchTopic(null, { scope: 'page' })
+ }
/>
{!hideHistory && (
<Popover
@@ -82,7 +90,7 @@ const CopilotToolbar = memo(() => {
topicId={topic.id}
topicTitle={topic.title}
onClose={() => setTopicPopoverOpen(false)}
- onTopicChange={(id) => switchTopic(id)}
+ onTopicChange={(id) => (onTopicChange ? onTopicChange(id) : switchTopic(id))}
/>
))}
</Flexbox>