-
-
Notifications
You must be signed in to change notification settings - Fork 761
fix(ai): operations on collaborative documents #2952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+213
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
packages/xl-ai/src/api/formats/html-blocks/collabUpdate.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /** | ||
| * Regression test for https://github.com/TypeCellOS/BlockNote/issues/2946 | ||
| * | ||
| * Runs the `update` stream tool against a Yjs-collaborative editor, fully | ||
| * offline (no LLM call): the tool call is fed straight into the executor. | ||
| * | ||
| * `AIExtension.invokeAI` forks the Y.Doc before the request starts, so the | ||
| * fork is part of the setup here. | ||
| */ | ||
| import { | ||
| BlockNoteEditor, | ||
| expandPMRangeToWords, | ||
| getBlockInfo, | ||
| getNodeById, | ||
| } from "@blocknote/core"; | ||
| import type { ForkYDocExtension } from "@blocknote/core/yjs"; | ||
| import { withCollaboration } from "@blocknote/core/yjs"; | ||
| import { TextSelection } from "prosemirror-state"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
| import * as Y from "yjs"; | ||
|
|
||
| import { AIExtension } from "../../../AIExtension.js"; | ||
| import { StreamToolExecutor } from "../../../streamTool/StreamToolExecutor.js"; | ||
| import { StreamTool } from "../../../streamTool/streamTool.js"; | ||
| import { tools } from "./tools/index.js"; | ||
|
|
||
| function createLocalEditor(text: string) { | ||
| const editor = BlockNoteEditor.create({ | ||
| initialContent: [{ type: "paragraph", content: text }], | ||
| trailingBlock: false, | ||
| extensions: [AIExtension()], | ||
| }); | ||
| editor.mount(document.createElement("div")); | ||
|
|
||
| return { editor, fork: () => undefined }; | ||
| } | ||
|
|
||
| function createCollabEditor(text: string) { | ||
| const ydoc = new Y.Doc(); | ||
| const editor = BlockNoteEditor.create( | ||
| withCollaboration({ | ||
| collaboration: { | ||
| fragment: ydoc.getXmlFragment("doc"), | ||
| user: { color: "#ff0000", name: "Local User" }, | ||
| provider: undefined, | ||
| }, | ||
| trailingBlock: false, | ||
| extensions: [AIExtension()], | ||
| }), | ||
| ); | ||
| editor.mount(document.createElement("div")); | ||
|
|
||
| editor.replaceBlocks(editor.document, [{ type: "paragraph", content: text }]); | ||
|
|
||
| return { | ||
| editor, | ||
| fork: () => | ||
| editor.getExtension<typeof ForkYDocExtension>("yForkDoc")?.fork(), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Selects the full content of the first block, mirroring what the AI menu does | ||
| * (`buildAIRequest` -> `expandPMRangeToWords`) | ||
| */ | ||
| function selectWholeFirstBlock(editor: BlockNoteEditor<any, any, any>) { | ||
| const id = editor.document[0].id; | ||
| const info = getBlockInfo(getNodeById(id, editor.prosemirrorState.doc)!); | ||
| if (!info.isBlockContainer) { | ||
| throw new Error("not a block container"); | ||
| } | ||
| const from = info.blockContent.beforePos + 1; | ||
| const to = info.blockContent.afterPos - 1; | ||
|
|
||
| editor.transact((tr) => { | ||
| tr.setSelection(TextSelection.create(tr.doc, from, to)); | ||
| }); | ||
|
|
||
| return expandPMRangeToWords(editor.prosemirrorState.doc, { | ||
| $from: editor.prosemirrorState.doc.resolve(from), | ||
| $to: editor.prosemirrorState.doc.resolve(to), | ||
| }); | ||
| } | ||
|
|
||
| async function runUpdate( | ||
| editor: BlockNoteEditor<any, any, any>, | ||
| id: string, | ||
| html: string, | ||
| selection?: { from: number; to: number }, | ||
| ) { | ||
| const streamTools = [ | ||
| tools.update(editor, { | ||
| idsSuffixed: false, | ||
| withDelays: false, | ||
| updateSelection: selection, | ||
| }), | ||
| ] as StreamTool<any>[]; | ||
|
|
||
| await new StreamToolExecutor(streamTools).execute( | ||
| (async function* () { | ||
| yield { | ||
| operation: { type: "update" as const, id, block: html }, | ||
| isUpdateToPreviousOperation: false, | ||
| isPossiblyPartial: false, | ||
| metadata: undefined, | ||
| }; | ||
| })(), | ||
| ); | ||
| } | ||
|
|
||
| describe.each([ | ||
| ["local", createLocalEditor], | ||
| ["collaborative", createCollabEditor], | ||
| ])("update tool (%s)", (_name, createEditor) => { | ||
| it("updates a selected paragraph", async () => { | ||
| const { editor, fork } = createEditor("Bonjour le monde"); | ||
| fork(); | ||
| const id = editor.document[0].id; | ||
| const selection = selectWholeFirstBlock(editor); | ||
|
|
||
| await runUpdate(editor, id, "<p>Bonjour à tous</p>", selection); | ||
|
|
||
| editor.getExtension(AIExtension)?.acceptChanges(); | ||
| expect((editor.document[0] as any).content[0].text).toBe("Bonjour à tous"); | ||
| }); | ||
|
|
||
| it("updates a paragraph without a selection", async () => { | ||
| const { editor, fork } = createEditor("Bonjour le monde"); | ||
| fork(); | ||
| const id = editor.document[0].id; | ||
|
|
||
| await runUpdate(editor, id, "<p>Bonjour à tous</p>"); | ||
|
|
||
| editor.getExtension(AIExtension)?.acceptChanges(); | ||
| expect((editor.document[0] as any).content[0].text).toBe("Bonjour à tous"); | ||
| }); | ||
| }); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Fail when the fork extension is unavailable.
Optional chaining makes
fork()a no-op whenyForkDocis missing. The collaborative tests can then pass without testing the forked Y.Doc path. Throw if the extension is unavailable.Proposed fix
return { editor, - fork: () => - editor.getExtension<typeof ForkYDocExtension>("yForkDoc")?.fork(), + fork: () => { + const forkYDoc = editor.getExtension<typeof ForkYDocExtension>("yForkDoc"); + if (!forkYDoc) { + throw new Error("yForkDoc extension is not available"); + } + forkYDoc.fork(); + }, };📝 Committable suggestion
🤖 Prompt for AI Agents