pycrdt icon indicating copy to clipboard operation
pycrdt copied to clipboard

Positioning cursors using awareness (`StickyIndex` support)

Open krassowski opened this issue 7 months ago • 5 comments

While the awareness is exposed by pycrdt, it does not expose the StickyIndex often needed to place collaborators cursors at a relative position.

krassowski avatar May 07 '25 11:05 krassowski

It looks like it would be sufficient to expose python binding to sticky_index on Text and Array, do I see it right?

https://github.com/y-crdt/y-crdt/blob/0968e1dd3889df369d3adff60e771632bd754af7/yrs/src/moving.rs#L892-L906

krassowski avatar May 07 '25 12:05 krassowski

cc @brichet who contributed to the awareness feature in pycrdt.

davidbrochart avatar May 07 '25 12:05 davidbrochart

Just to share my research into how we could possibly use it downstream (in jupyterlab):

ycell = document.ycells[0]
text: Text = ycell["source"]
document.awareness.set_local_state_field(
    "user",
    {
        "username": "krassowski",
        "initials": "MK",
        "display_name": "M. Krassowski",
        "name": "Krassowski",
        "color": "#ffb61e",
    },
)
# set the cursor in the first cell
cursor = text.sticky_index(index=0, assoc=0)
document.awareness.set_local_state_field(
    "cursors",
    [
        {
            "anchor": cursor,
            "head": cursor,
            "primary": True,
            "empty": True,
        }
    ],
)

Some questions here are:

  • should the stick_index object be natively json-serializable when passed to set_local_state_field or have to_py() method?
  • do we need to pass doc.transact_mut() as in y-crdt or can it be accessed only at the bindings level (as implied by my pseudocode above)

krassowski avatar May 07 '25 12:05 krassowski

I wonder what would be the use case for cursors position from the server side document awareness. Is it intended for LLM use?

brichet avatar May 09 '25 13:05 brichet

Is it intended for LLM use?

Yes, spot on. Having a collaborator agent on the backend being able to work and continue working even if you refresh the browser, and their cursor position being visible when they type.

krassowski avatar May 09 '25 13:05 krassowski