Skip to content
TypeScript

fix(web): preserve original mention text in the composer (#10100)

Julius Marminge761d4bac1c23
diff --git a/apps/web/src/components/ComposerPromptEditor.serialization.test.tsx b/apps/web/src/components/ComposerPromptEditor.serialization.test.tsx
new file mode 100644
index 00000000000..980521084f8
--- /dev/null
+++ b/apps/web/src/components/ComposerPromptEditor.serialization.test.tsx
@@ -0,0 +1,184 @@
+import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
+import { $copyNode, $getRoot, $isElementNode, PASTE_COMMAND, type LexicalEditor } from "lexical";
+import { act, createRef } from "react";
+import { create, type ReactTestRenderer } from "react-test-renderer";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
+
+import { collapseExpandedComposerCursor } from "../composer-logic";
+import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor";
+
+vi.mock("./chat/FileTagChip", () => ({
+ FILE_TAG_CHIP_CLASS_NAME: "",
+ FileTagChipContent: () => null,
+}));
+vi.mock("./chat/ComposerPendingTerminalContexts", () => ({
+ ComposerPendingTerminalContextChip: () => null,
+}));
+vi.mock("./chat/AssistantCitationChip", () => ({ AssistantCitationChip: () => null }));
+
+let lexicalEditor: LexicalEditor;
+// Keep the real composer, registered nodes, updates, and snapshot API. Only the
+// DOM view is omitted so Lexical runs headlessly in this component test.
+vi.mock("@lexical/react/LexicalPlainTextPlugin", () => ({
+ PlainTextPlugin: function HeadlessEditor() {
+ [lexicalEditor] = useLexicalComposerContext();
+ return null;
+ },
+}));
+
+let renderer: ReactTestRenderer | undefined;
+const editorRef = createRef<ComposerPromptEditorHandle>();
+
+function composer(value: string) {
+ return (
+ <ComposerPromptEditor
+ value={value}
+ cursor={collapseExpandedComposerCursor(value, value.length)}
+ terminalContexts={[]}
+ skills={[]}
+ disabled={false}
+ placeholder="Write a prompt"
+ onRemoveTerminalContext={() => {}}
+ onChange={() => {}}
+ onPaste={() => {}}
+ editorRef={editorRef}
+ />
+ );
+}
+
+async function renderPrompt(value: string) {
+ await act(() => {
+ if (renderer) renderer.update(composer(value));
+ else renderer = create(composer(value));
+ });
+}
+
+function $firstMention() {
+ const paragraph = $getRoot().getFirstChildOrThrow();
+ if (!$isElementNode(paragraph)) throw new Error("Expected a composer paragraph");
+ const mention = paragraph.getFirstChildOrThrow();
+ if (mention.getType() !== "composer-mention") throw new Error("Expected a mention");
+ return mention;
+}
+
+class TestClipboardEvent extends Event {
+ readonly clipboardData: DataTransfer;
+
+ constructor(text: string) {
+ super("paste", { cancelable: true });
+ this.clipboardData = {
+ files: [],
+ getData: (type: string) => (type === "text/plain" ? text : ""),
+ } as unknown as DataTransfer;
+ }
+}
+
+beforeEach(() => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ vi.stubGlobal("document", { activeElement: null });
+});
+
+afterEach(async () => {
+ await act(() => renderer?.unmount());
+ renderer = undefined;
+ vi.unstubAllGlobals();
+});
+
+describe("composer mention serialization", () => {
+ it.each([
+ "@README.md control",
+ "@terminal-1:3 Explain this output\n\n<terminal_context>\n- Terminal 1 line 3:\n 3 | output\n</terminal_context>",
+ '@"docs/My \\"File\\".md" please',
+ '@"docs/ι›ͺ πŸ‘‹.md" please',
+ "[README.md](README.md) control",
+ "[config#draft?.json](config%23draft%3f.json) control",
+ "Plain text\n Keep indentation πŸ‘‹",
+ ])("preserves the initial prompt %s", async (prompt) => {
+ await renderPrompt(prompt);
+ expect(editorRef.current?.readSnapshot().value).toBe(prompt);
+ });
+
+ it("preserves original source when replacing the controlled prompt", async () => {
+ for (const prompt of ["", "@README.md control", "Older plain control", "@README.md control"]) {
+ await renderPrompt(prompt);
+ expect(editorRef.current?.readSnapshot()).toMatchObject({
+ value: prompt,
+ expandedCursor: prompt.length,
+ });
+ if (prompt === "@README.md control") {
+ expect(lexicalEditor.getEditorState().read(() => $firstMention().isInline())).toBe(true);
+ }
+ }
+ });
+
+ it("preserves source when Lexical clones the mention and reloads exported state", async () => {
+ const prompt = '@"docs/ι›ͺ πŸ‘‹.md" remains a chip';
+ await renderPrompt(prompt);
+ const originalKey = lexicalEditor.getEditorState().read(() => $firstMention().getKey());
+
+ await act(() => {
+ lexicalEditor.update(
+ () => {
+ const mention = $firstMention();
+ mention.replace($copyNode(mention));
+ },
+ { discrete: true },
+ );
+ });
+ expect(lexicalEditor.getEditorState().read(() => $firstMention().getKey())).not.toBe(
+ originalKey,
+ );
+ expect(editorRef.current?.readSnapshot().value).toBe(prompt);
+ const exportedState = lexicalEditor.getEditorState().toJSON();
+
+ await renderPrompt("");
+ await act(() => {
+ lexicalEditor.setEditorState(lexicalEditor.parseEditorState(exportedState));
+ });
+ expect(editorRef.current?.readSnapshot().value).toBe(prompt);
+ expect(lexicalEditor.getEditorState().read(() => $firstMention().isInline())).toBe(true);
+ });
+
+ it("keeps canonical serialization when importing legacy mention JSON without source", async () => {
+ await renderPrompt("");
+ await act(() => {
+ lexicalEditor.setEditorState(
+ lexicalEditor.parseEditorState(
+ JSON.stringify({
+ root: {
+ type: "root",
+ version: 1,
+ children: [
+ {
+ type: "paragraph",
+ version: 1,
+ children: [{ type: "composer-mention", version: 1, path: "README.md" }],
+ },
+ ],
+ },
+ }),
+ ),
+ );
+ });
+ expect(editorRef.current?.readSnapshot().value).toBe("[README.md](README.md)");
+ expect(lexicalEditor.getEditorState().read(() => $firstMention().isInline())).toBe(true);
+ });
+
+ it("still serializes a newly inserted mention canonically", async () => {
+ vi.stubGlobal("ClipboardEvent", TestClipboardEvent);
+ await renderPrompt("");
+ const event = new TestClipboardEvent("@README.md ");
+ await act(() => {
+ lexicalEditor.update(
+ () => {
+ $getRoot().selectEnd();
+ lexicalEditor.dispatchCommand(PASTE_COMMAND, event as ClipboardEvent);
+ },
+ { discrete: true },
+ );
+ });
+ expect(event.defaultPrevented).toBe(true);
+ expect(editorRef.current?.readSnapshot().value).toBe("[README.md](README.md) ");
+ expect(lexicalEditor.getEditorState().read(() => $firstMention().isInline())).toBe(true);
+ });
+});
diff --git a/apps/web/src/components/ComposerPromptEditor.tsx b/apps/web/src/components/ComposerPromptEditor.tsx
index 2ecb4c873e8..6b49ef88473 100644
--- a/apps/web/src/components/ComposerPromptEditor.tsx
+++ b/apps/web/src/components/ComposerPromptEditor.tsx
@@ -117,6 +117,7 @@ const BACKTICK_SURROUND_CLOSE_SYMBOL = SURROUND_SYMBOLS_MAP.get("`") ?? null;
type SerializedComposerMentionNode = Spread<
{
path: string;
+ source?: string;
type: "composer-mention";
version: 1;
},
@@ -174,28 +175,33 @@ function ComposerMentionDecorator(props: { path: string }) {
class ComposerMentionNode extends DecoratorNode<React.ReactElement> {
__path: string;
+ __source: string;
static override getType(): string {
return "composer-mention";
}
static override clone(node: ComposerMentionNode): ComposerMentionNode {
- return new ComposerMentionNode(node.__path, node.__key);
+ return new ComposerMentionNode(node.__path, node.__source, node.__key);
}
static override importJSON(serializedNode: SerializedComposerMentionNode): ComposerMentionNode {
- return $createComposerMentionNode(serializedNode.path).updateFromJSON(serializedNode);
+ return $createComposerMentionNode(serializedNode.path, serializedNode.source).updateFromJSON(
+ serializedNode,
+ );
}
- constructor(path: string, key?: NodeKey) {
+ constructor(path: string, source = serializeComposerFileLink(path), key?: NodeKey) {
super(key);
this.__path = path;
+ this.__source = source;
}
override exportJSON(): SerializedComposerMentionNode {
return {
...super.exportJSON(),
path: this.__path,
+ source: this.__source,
type: "composer-mention",
version: 1,
};
@@ -212,7 +218,7 @@ class ComposerMentionNode extends DecoratorNode<React.ReactElement> {
}
override getTextContent(): string {
- return serializeComposerFileLink(this.__path);
+ return this.__source;
}
override isInline(): true {
@@ -224,8 +230,8 @@ class ComposerMentionNode extends DecoratorNode<React.ReactElement> {
}
}
-function $createComposerMentionNode(path: string): ComposerMentionNode {
- return $applyNodeReplacement(new ComposerMentionNode(path));
+function $createComposerMentionNode(path: string, source?: string): ComposerMentionNode {
+ return $applyNodeReplacement(new ComposerMentionNode(path, source));
}
function resolveSkillDescription(
@@ -851,7 +857,7 @@ function $setComposerEditorPrompt(
continue;
}
if (segment.type === "mention") {
- paragraph.append($createComposerMentionNode(segment.path));
+ paragraph.append($createComposerMentionNode(segment.path, segment.source));
continue;
}
if (segment.type === "skill") {