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 local-file-tag nodes in messages without crashing (#16687)

Arvin Xu4c2fd9607039
diff --git a/src/features/ChatInput/InputEditor/LocalFileTag/LocalFileTagNode.ts b/src/features/ChatInput/InputEditor/LocalFileTag/LocalFileTagNode.ts
index 29487e99eb1..f9377f46922 100644
--- a/src/features/ChatInput/InputEditor/LocalFileTag/LocalFileTagNode.ts
+++ b/src/features/ChatInput/InputEditor/LocalFileTag/LocalFileTagNode.ts
@@ -1,5 +1,6 @@
import { addClassNamesToElement } from '@lexical/utils';
import { getKernelFromEditor } from '@lobehub/editor';
+import type { HeadlessRenderableNode, HeadlessRenderContext } from '@lobehub/editor/renderer';
import {
$applyNodeReplacement,
DecoratorNode,
@@ -11,6 +12,9 @@ import {
type SerializedLexicalNode,
type Spread,
} from 'lexical';
+import { createElement } from 'react';
+
+import { LocalFile } from '@/features/LocalFile';
export type SerializedLocalFileTagNode = Spread<
{
@@ -28,7 +32,7 @@ export type SerializedLocalFileTagNode = Spread<
* `@`) and β€” crucially β€” own its `<localFile … />` markdown writer via a plugin
* that is always registered, independent of whether `mentionOption` is enabled.
*/
-export class LocalFileTagNode extends DecoratorNode<any> {
+export class LocalFileTagNode extends DecoratorNode<any> implements HeadlessRenderableNode {
__name: string;
__path: string;
__isDirectory: boolean;
@@ -116,6 +120,15 @@ export class LocalFileTagNode extends DecoratorNode<any> {
render: decorator.render(this, editor),
};
}
+
+ renderHeadless({ key }: HeadlessRenderContext) {
+ return createElement(LocalFile, {
+ isDirectory: this.__isDirectory,
+ key,
+ name: this.__name,
+ path: this.__path,
+ });
+ }
}
export function $createLocalFileTagNode(
diff --git a/src/features/Conversation/Messages/User/components/RichTextMessage.test.tsx b/src/features/Conversation/Messages/User/components/RichTextMessage.test.tsx
index d0e4431d6c3..e373534a56d 100644
--- a/src/features/Conversation/Messages/User/components/RichTextMessage.test.tsx
+++ b/src/features/Conversation/Messages/User/components/RichTextMessage.test.tsx
@@ -34,6 +34,34 @@ const mentionEditorState = {
},
};
+const localFileEditorState = {
+ root: {
+ children: [
+ {
+ children: [
+ {
+ isDirectory: false,
+ name: 'report.md',
+ path: '/Users/me/project/report.md',
+ type: 'local-file-tag',
+ version: 1,
+ },
+ ],
+ direction: null,
+ format: '',
+ indent: 0,
+ type: 'paragraph',
+ version: 1,
+ },
+ ],
+ direction: null,
+ format: '',
+ indent: 0,
+ type: 'root',
+ version: 1,
+ },
+};
+
afterEach(() => {
cleanup();
});
@@ -49,6 +77,19 @@ describe('RichTextMessage', () => {
expect(container.querySelector('.editor_mention')?.textContent).toBe('@Agent A');
});
+ // Regression: a local file dragged from the working sidebar serializes to a
+ // `local-file-tag` node. If that node isn't registered on the renderer,
+ // LexicalRenderer throws while parsing the state and the whole message crashes.
+ it('should render local-file-tag nodes without crashing', async () => {
+ const { container } = render(<RichTextMessage editorState={localFileEditorState} />);
+
+ await act(async () => {
+ await moment();
+ });
+
+ expect(container.textContent).toContain('report.md');
+ });
+
it('should render nothing for empty editor state', () => {
const { container } = render(<RichTextMessage editorState={{}} />);
diff --git a/src/features/Conversation/Messages/User/components/RichTextMessage.tsx b/src/features/Conversation/Messages/User/components/RichTextMessage.tsx
index 7f150617934..d14e28c25f5 100644
--- a/src/features/Conversation/Messages/User/components/RichTextMessage.tsx
+++ b/src/features/Conversation/Messages/User/components/RichTextMessage.tsx
@@ -4,6 +4,7 @@ import type { CSSProperties } from 'react';
import { memo, useMemo } from 'react';
import { ActionTagNode } from '@/features/ChatInput/InputEditor/ActionTag/ActionTagNode';
+import { LocalFileTagNode } from '@/features/ChatInput/InputEditor/LocalFileTag';
import { mentionFilledClassName } from '@/features/ChatInput/InputEditor/mentionStyle';
import { ReferTopicNode } from '@/features/ChatInput/InputEditor/ReferTopic/ReferTopicNode';
@@ -13,7 +14,7 @@ interface RichTextMessageProps {
const LINE_HEIGHT = 1.6;
const style: CSSProperties = { '--common-line-height': LINE_HEIGHT } as CSSProperties;
-const EXTRA_NODES = [ActionTagNode, ReferTopicNode];
+const EXTRA_NODES = [ActionTagNode, ReferTopicNode, LocalFileTagNode];
const RichTextMessage = memo<RichTextMessageProps>(({ editorState }) => {
const value = useMemo(() => {