Skip to content
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

Please explain how to update Yjs document from the backend with streaming AND keeping the formatting. #1590

Open
mikealche opened this issue Apr 24, 2024 · 0 comments
Labels
documentation Related to the public documentation enhancement New feature or request triage needed The issue needs to be reviewed by the team

Comments

@mikealche
Copy link

mikealche commented Apr 24, 2024

What is the improvement or update you wish to see?

I'm creating a collaborative editor that's also assisted by a LLM.
My backend is calling the LLM and pushing the results to the Yjs document.
So far so good.
The only issue is that Markdown doesn't get translated correctly. Instead I just see the markdown symbols.
image

Is there any context that might help us understand?

I'm currently doing the following. Which returns a function that I can call with each new piece of text the my LLM outputs.

export const streamUpdateToLiveblocksYjsDoc = async ({
  roomId,
  paragraphInsertIndexAfter,
}: {
  paragraphInsertIndexAfter: number;
  roomId: string;
}) => {
  // Create a Yjs document
  const yDoc = new Y.Doc();

  const ydocUpdate = await liveblocks.getYjsDocumentAsBinaryUpdate(roomId);

  Y.applyUpdate(yDoc, new Uint8Array(ydocUpdate));

  const yXmlFragment = yDoc.getXmlFragment("default");
  const yxmlParagraph = yXmlFragment.get(paragraphInsertIndexAfter);

  return async (textChunk: string) => {
    yxmlParagraph.insert(yxmlParagraph.length, [new Y.XmlText(textChunk)]);

    const yUpdate = Y.encodeStateAsUpdate(yDoc);

    await liveblocks.sendYjsBinaryUpdate(roomId, yUpdate);
  };
};

which I KNOW it's wrong (I'm always doing Y.XmlText(textChunk) when I should be doing ... another thing?

Using a LLM in the backend seems in my opinion to be the table stakes in 2024 and most products will start adopting it I believe.
So I really wish we can figure out a way to do stream + formatting

Does the docs page already exist? Please link to it.
https://liveblocks.io/docs/guides/modifying-yjs-document-data-with-the-rest-api

@mikealche mikealche added documentation Related to the public documentation enhancement New feature or request triage needed The issue needs to be reviewed by the team labels Apr 24, 2024
@mikealche mikealche changed the title Explain how to update Yjs document from the backend with streaming AND keeping the formatting. Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Related to the public documentation enhancement New feature or request triage needed The issue needs to be reviewed by the team
1 participant