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): render topic references in user messages (#19679)

Arvin Xucdd2aeb49547
diff --git a/src/features/Conversation/Markdown/plugins/ReferTopic/Render.test.tsx b/src/features/Conversation/Markdown/plugins/ReferTopic/Render.test.tsx
new file mode 100644
index 00000000000..3b676dc6289
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/ReferTopic/Render.test.tsx
@@ -0,0 +1,75 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import { fireEvent, render, renderHook, screen } from '@testing-library/react';
+import ReactMarkdown from 'react-markdown';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { useChatStore } from '@/store/chat';
+
+import { useMarkdown } from '../../../Messages/User/useMarkdown';
+import Render from './Render';
+
+vi.mock('../../../Messages/User/components/ContentPreview', () => ({
+ default: () => null,
+}));
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+describe('ReferTopicRender', () => {
+ beforeEach(() => {
+ useChatStore.setState({
+ switchTopic: vi.fn(),
+ topicDataMap: {},
+ });
+ });
+
+ it('renders a persisted topic reference as an interactive tag', () => {
+ render(
+ <Render
+ id="message-id"
+ node={{ properties: { id: 'topic-id', name: 'Referenced topic' } }}
+ tagName="refer_topic"
+ type="refer_topic"
+ >
+ {null}
+ </Render>,
+ );
+
+ fireEvent.click(screen.getByText('Referenced topic'));
+
+ expect(useChatStore.getState().switchTopic).toHaveBeenCalledWith('topic-id');
+ });
+
+ it('falls back to plain text when the topic id is missing', () => {
+ render(
+ <Render
+ id="message-id"
+ node={{ properties: { name: 'Referenced topic' } }}
+ tagName="refer_topic"
+ type="refer_topic"
+ >
+ {null}
+ </Render>,
+ );
+
+ expect(screen.getByText('Referenced topic')).toBeInTheDocument();
+ });
+
+ it('renders serialized refer_topic markup through the user message pipeline', () => {
+ const { result } = renderHook(() => useMarkdown('message-id'));
+ const { components, remarkPlugins } = result.current;
+
+ render(
+ <ReactMarkdown components={components} remarkPlugins={remarkPlugins}>
+ {'See <refer_topic name="Referenced topic" id="topic-id" /> for context'}
+ </ReactMarkdown>,
+ );
+
+ fireEvent.click(screen.getByText('Referenced topic'));
+
+ expect(useChatStore.getState().switchTopic).toHaveBeenCalledWith('topic-id');
+ });
+});
diff --git a/src/features/Conversation/Markdown/plugins/ReferTopic/Render.tsx b/src/features/Conversation/Markdown/plugins/ReferTopic/Render.tsx
new file mode 100644
index 00000000000..1e920f1c5a2
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/ReferTopic/Render.tsx
@@ -0,0 +1,24 @@
+'use client';
+
+import { memo } from 'react';
+
+import { ReferTopicView } from '@/features/ChatInput/InputEditor/ReferTopic/ReferTopicView';
+
+import { type MarkdownElementProps } from '../type';
+
+interface ReferTopicNodeProps {
+ id?: string;
+ name?: string;
+}
+
+const Render = memo<MarkdownElementProps<ReferTopicNodeProps>>(({ node }) => {
+ const { id, name } = node?.properties || {};
+
+ if (!id) return name || null;
+
+ return <ReferTopicView fallbackTitle={name} topicId={id} />;
+});
+
+Render.displayName = 'ReferTopicRender';
+
+export default Render;
diff --git a/src/features/Conversation/Markdown/plugins/ReferTopic/index.ts b/src/features/Conversation/Markdown/plugins/ReferTopic/index.ts
new file mode 100644
index 00000000000..f1ff016c8c3
--- /dev/null
+++ b/src/features/Conversation/Markdown/plugins/ReferTopic/index.ts
@@ -0,0 +1,14 @@
+import { createRemarkSelfClosingTagPlugin } from '../remarkPlugins/createRemarkSelfClosingTagPlugin';
+import { type MarkdownElement } from '../type';
+import Render from './Render';
+
+export const REFER_TOPIC_TAG = 'refer_topic';
+
+const ReferTopic: MarkdownElement = {
+ Component: Render,
+ remarkPlugin: createRemarkSelfClosingTagPlugin(REFER_TOPIC_TAG),
+ scope: 'user',
+ tag: REFER_TOPIC_TAG,
+};
+
+export default ReferTopic;
diff --git a/src/features/Conversation/Markdown/plugins/index.ts b/src/features/Conversation/Markdown/plugins/index.ts
index b9bc0f1db24..93c7fdb7d69 100644
--- a/src/features/Conversation/Markdown/plugins/index.ts
+++ b/src/features/Conversation/Markdown/plugins/index.ts
@@ -7,6 +7,7 @@ import LobeThinking from './LobeThinking';
import LocalFile from './LocalFile';
import LocalFileLink from './LocalFileLink';
import Mention from './Mention';
+import ReferTopic from './ReferTopic';
import ScmEvent from './ScmEvent';
import Skill from './Skill';
import Task from './Task';
@@ -23,6 +24,7 @@ export const markdownElements: MarkdownElement[] = [
LobeThinking,
LocalFile,
Mention,
+ ReferTopic,
Skill,
Tool,
Task,