Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat: ai editing documents (#4296)
Wolf Mermelsteinc38263e2f3d1
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 1244cc8310..7d83bb3101 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -65,6 +65,7 @@"macro_middleware","macro_queues","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mention_utils",@@ -206,6 +207,7 @@"macro_middleware","macro_queues","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mention_utils",@@ -347,6 +349,7 @@"macro_queues","macro_redis","macro_service_urls",+ "macro_sync_service_jwt","macro_tower_layers","macro_user_id","macro_uuid",@@ -631,6 +634,7 @@"macro_middleware","macro_queues","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mention_utils",@@ -1261,6 +1265,7 @@"macro_middleware","macro_queues","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mcp_client",@@ -1379,6 +1384,7 @@"macro_queues","macro_service_urls","macro_sha_count_client",+ "macro_sync_service_jwt","macro_tower_layers","macro_user_id","macro_uuid",@@ -1532,6 +1538,7 @@"macro_env_var","macro_middleware","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mention_utils",@@ -1616,6 +1623,7 @@"macro_env_var","macro_middleware","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mention_utils",@@ -2211,6 +2219,7 @@"macro_env_var","macro_middleware","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mention_utils",@@ -2710,6 +2719,9 @@"prompt","workspace-hack"],+ "macro_sync_service_jwt": [+ "macro_sync_service_jwt"+ ],"macro_tower_layers": ["macro_tower_layers","workspace-hack"@@ -2803,6 +2815,7 @@"macro_middleware","macro_queues","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mcp_auth_proxy",@@ -2902,6 +2915,7 @@"macro_middleware","macro_queues","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","memory",@@ -3706,6 +3720,7 @@"macro_middleware","macro_queues","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","memory",diff --git a/.gitignore b/.gitignoreindex 602d6b8067..f79c1e3df8 100644--- a/.gitignore+++ b/.gitignore@@ -1,6 +1,7 @@.direnv/*.envrc**/.env+**/.dev.vars.idea/**/.DS_Store.run/@@ -9,6 +10,7 @@ temp/.env.localrun-local.fish**/node_modules+**/.wrangler**/result**/result-*.pi/plan/diff --git a/docker-compose.yml b/docker-compose.ymlindex 1907c97810..6c4e853736 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -116,6 +116,10 @@ services:- static_file_service- sync_service- lexical_service+ - ai_editing_worker+ environment:+ - OVERRIDE_DOCUMENT_STORAGE_SERVICE_URL=http://document-storage-service:8080+ - OVERRIDE_AI_EDITING_WORKER_URL=http://ai-editing-worker:8933networks:databases:services:@@ -406,6 +410,27 @@ services:retries: 3start_period: 60s+ # ============================================================================+ # ai_editing_worker (Port 8933)+ # AI-powered document editing Cloudflare Worker (local wrangler dev)+ # ============================================================================+ ai_editing_worker:+ <<: *common-env+ build:+ context: ./js/ai-editing-worker+ dockerfile: Dockerfile+ ports:+ - "8933:8933"+ volumes:+ - ./js:/app+ - /app/ai-editing-worker/.wrangler+ depends_on:+ - sync_service+ networks:+ services:+ aliases:+ - ai-editing-worker+# ============================================================================# lexical_service (Port 8096)# Lexical document conversion service - converts Lexical JSON to various formatsdiff --git a/js/ai-editing-worker/.gitignore b/js/ai-editing-worker/.gitignorenew file mode 100644index 0000000000..77fc17cc22--- /dev/null+++ b/js/ai-editing-worker/.gitignore@@ -0,0 +1,4 @@+node_modules+.wrangler+.dev.vars*+worker-configuration.d.tsdiff --git a/js/ai-editing-worker/AI_EDITING.md b/js/ai-editing-worker/AI_EDITING.mdnew file mode 100644index 0000000000..693aa51bf8--- /dev/null+++ b/js/ai-editing-worker/AI_EDITING.md@@ -0,0 +1,109 @@+# AI editing++The general idea is that we have a worker that takes an english request for a+document, and dispatches agents to actually produce edits on the doucment.++We do this by having a Clouflare worker joins the document as a real Loro peer,+makes its edits through the same CRDT system everyone else uses, and streams them+out keystroke by keystroke.++# Flow++We have a simple hono Cloudflare worker `POST /edit`. The body carries a+user token, a document id, the prompt, and a few config options (`interpret`,+`typingAnimations`, `debug`, and optional per-role model overrides). The handler+swaps the user token for a short-lived document token against the document+service, builds the sync-service WebSocket URL from it, and hands everything to+`runEditSession` (`src/run-edit.ts`).++## Joining the document++`runEditSession` is the main entrypoint that our edit endpoint calls to start an+edit session. At a high level, it:++1. Opens a sync-service WebSocket, speaking the same Bebop wire protocol as the+ browser client.+2. Waits for the initial snapshot, then spins up a `LoroManager` + `SyncEngine`+ around the markdown Loro schema. It uses the same abstractions the frontend+ does.+3. Wires a `WALSyncer` so local commits get pushed back to the server, and+ flushes it before disconnecting. It's memory backed (not IDB).++## The agents++We're using three agents to carry out our edits:++- **Interpreter**: a first pass that figures out what the user "wants" and+ writes out some goals for the supervisor. Having a thinking step helps the+ supervisor make more "correct" edits for what the user wants.+- **Supervisor**: dispatches coding agents to make small mechanical edits, and+ iterates. Decides when we are done and happy.+- **Writer / coder**: generates code that edits the lexical tree. Uses a custom+ abstraction that we turn into edits for our animation framework, which then+ eventually turns into real lexical edits.++A diagram for the animation abstraction flow++```+ model writes JS → QuickJS sandbox → DocumentEditor → DocumentOp[]+ │+ ┌──────────────────────────────────┘+ ▼+ animate(op) → DocumentOpStep[] (cursor / pause / edit)+ │+ runQueue replays each step:+ pause → sleep awareness → cursor edit → Doc.apply+ │+ Lexical session ←────┘+ │+ propagate → snapshot → mirror → Loro → sync to everyone+```++### DocumentEditor and ops++`DocumentEditor` is a ergonomic chainable surface the model calls. It has a+million simple small methods like `bold`, `convertToHeading`,+`insertTableAfter`, `mentionUser`. As you use it it accumulates all the ops but+doesn't actually do any mutations on the document. As you create new nodes it+hands out "refs" from a pool of random IDs.++Once the AI writes code that acts on a document editor, we run the code with a+blank `DocumentEditor` in a quickjs sandbox in the worker. That code produces an+accumulated vector of all the document operations we want, which we can then run+on the document directly or propagate via an animator.++For animation, each `DocumentOp` kind has exactly one animator that maps to it,+in, and dispatches on the kind to produce a flat list of `DocumentOpStep`s.+There are only three kinds of steps:++- `awareness`: move the cursor or set a selection+- `edit`: apply a real `DocumentOp` to the document+- `pause`: sleep for some time++We have an abstraction called the "executor" that ties together the animation queue and the document editor. It is what actually "plays" everything.++### "Doc"s++`Doc{Writer/Reader}` is an abstraction over the real Lexical `Document`. This makes everything much more testable. It has a minimal-ish (okay not super minimal) interface that's easy to mock and run against real lexical.++We have a big "ai-toolkit" lexical library that has a bunch of utility functions for working with lexical documents that the real lexical impl of the `DocWriter` uses.++It's `apply` runs as a discrete Lexical update, delegating the actual mutation to+the existing `ai-toolkit` `$`-helpers usually resolved by our durable lexical+node ids. It is what turns DocumentOps into actual edits to the lexical document.++## Cursors and peer ids++**The live cursor** is ephemeral. While a writer works, `realAwarenessSource`+encodes its caret/selection as a Loro cursor and broadcasts it over the+ephemeral awareness channel, so the website renders a remote cursor walking+through the text. Each writer draws a name from a pool (`Sam (AI)`, `Alex (AI)`)+and one of a few accent colors; the cursor lingers ~700ms after the writer+finishes, then clears. We have some ugly code that manages resolving a block id+to the `LoroText` that actually owns the characters for this :/++**The peer id** Human peers get random ids from the whole 64b range, and we have+it so that AI commits use a small reserved range in+(`999999999999999000`–`999999999999999999`). This makes AI authorship easy to+detect (`isAiPeer`) which history will be able to use to group together all AI+edits.diff --git a/js/ai-editing-worker/Dockerfile b/js/ai-editing-worker/Dockerfilenew file mode 100644index 0000000000..9a9f0b362e--- /dev/null+++ b/js/ai-editing-worker/Dockerfile@@ -0,0 +1,11 @@+FROM node:22-slim+RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates \+ && rm -rf /var/lib/apt/lists/*+RUN npm install -g bun++ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt++WORKDIR /app/ai-editing-worker+EXPOSE 8933++CMD ["sh", "-c", "bun scripts/generate-sandbox.ts && npx wrangler dev --env local --ip 0.0.0.0 --var SYNC_WS_BASE:ws://sync-service:8787"]diff --git a/js/ai-editing-worker/biome.jsonc b/js/ai-editing-worker/biome.jsoncnew file mode 100644index 0000000000..c86a822080--- /dev/null+++ b/js/ai-editing-worker/biome.jsonc@@ -0,0 +1,15 @@+{+ "$schema": "https://biomejs.dev/schemas/2.4.12/schema.json",+ "extends": ["../app/biome.jsonc"],+ "vcs": {+ "root": "../.."+ },+ "files": {+ "includes": [+ "**/*.{ts,tsx,js,jsx}",+ "!**/*.d.ts",+ "!**/node_modules",+ "!src/editor-sandbox-code.ts"+ ]+ }+}diff --git a/js/ai-editing-worker/bun.lock b/js/ai-editing-worker/bun.locknew file mode 100644index 0000000000..86cdedecae--- /dev/null+++ b/js/ai-editing-worker/bun.lock@@ -0,0 +1,293 @@+{+ "lockfileVersion": 1,+ "configVersion": 1,+ "workspaces": {+ "": {+ "name": "ai-editing-worker",+ "dependencies": {+ "@ai-sdk/anthropic": "^3.0.85",+ "@ai-sdk/cerebras": "^2.0.57",+ "@ai-sdk/openai": "^3.0.73",+ "@hono/zod-validator": "^0.8.0",+ "ai": "^6.0.207",+ "ai-fallback": "2",+ "envsafe": "^2.0.3",+ "hono": "^4.6.20",+ "quickjs-emscripten": "^0.31.0",+ "yargs": "^18.0.0",+ "zod": "^4.4.3",+ },+ "devDependencies": {+ "@types/node": "^25.9.3",+ "@types/yargs": "^17.0.35",+ "bun-types": "^1.3.14",+ "wrangler": "^4.103.0",+ },+ },+ },+ "packages": {+ "@ai-sdk/anthropic": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-fNeDB644l5wbRNQU0FnI+F7UTtOenMnPtACfMPUJaS2zJfuBlseEa1TMg+otHkETZgaJB+6Na51NQEv0+m7czw=="],++ "@ai-sdk/cerebras": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/openai-compatible": "2.0.51", "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-sf7zWCAKBK0vU3+BuJd3qYNZmEZ5FQHh5swKMaYzJ2pRByjT3xUDa41vxQa4z06SnCdV6k+fTm4swN+kZWdtmQ=="],++ "@ai-sdk/gateway": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30", "@vercel/oidc": "3.2.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-Ebs+7iS9zUgJu5B0RlxM2JmDWzq79Cpd6YdiqcCzB5qFdpfQJPUDiXutqlQP89F2XGjOdDeidulBTXUdXWzOxw=="],++ "@ai-sdk/openai": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-+3x9oxHv9Xp33Iv2L8D+e5hqmZi64jofBKig/9611JKyfV59NdkaDDajtwc0CxOEfARgCVq1BW7dP+526gKOKw=="],++ "@ai-sdk/openai-compatible": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-A6qfyaVs4lxmRxRux6U3ViOa8mMbsSd0OaHghpei2MpiBT6791J4zFH5MN7kaW1tLfQ246rSC2DVTMOavsycjQ=="],++ "@ai-sdk/provider": ["@ai-sdk/[email protected]", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-Q3BZ27qfpYqnCYGvE3vt+Qi6LGOF9R5Nmzn+9JoM1lCRsD9mYaIhfJLkSunN48nfGXJ6n+XNV0J/XVpqGQl7Dw=="],++ "@ai-sdk/provider-utils": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@standard-schema/spec": "^1.1.0", "eventsource-parser": "^3.0.8" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-VO7I+vPffqI5sMnPoUq5DCSqKIgQIk/naJWRdQVpz2ma2zoprC/lqiJiUEl2s6DfvTD76TbhD3q39ROjlA6rGw=="],++ "@cloudflare/kv-asset-handler": ["@cloudflare/[email protected]", "", {}, "sha512-jxQYkj8dSIzc0cD6cMMNdOc1UVjqSqu8BZdor5s8cGjW2I8BjODt/kWPVdY+u9zj3ms75Q5qaZgnxUad83+eAg=="],++ "@cloudflare/unenv-preset": ["@cloudflare/[email protected]", "", { "peerDependencies": { "unenv": "2.0.0-rc.24", "workerd": ">1.20260305.0 <2.0.0-0" }, "optionalPeers": ["workerd"] }, "sha512-ECxObrMfyTl5bhQf/lZCXwo5G6xX9IAUo+nDMKK4SZ8m4Jvvxp52vilxyySSWh2YTZz8+HQ07qGH/2rEom1vDw=="],++ "@cloudflare/workerd-darwin-64": ["@cloudflare/[email protected]", "", { "os": "darwin", "cpu": "x64" }, "sha512-jWwmgEVVWbsHNrLSNXzwjJaH90VzRxq1cWkQFUidxyeUPnMxemeNE8I9qFAfrpzGgE11e9sKDcE3ettJW08swQ=="],++ "@cloudflare/workerd-darwin-arm64": ["@cloudflare/[email protected]", "", { "os": "darwin", "cpu": "arm64" }, "sha512-LHH7b565g9znfCUOkwbec6FG2rmRbsgCy6aJiU9KN662mNheWl5sw/iKleiFSiljPKQQP3HkjnC/NSkdgi/aSA=="],++ "@cloudflare/workerd-linux-64": ["@cloudflare/[email protected]", "", { "os": "linux", "cpu": "x64" }, "sha512-FMnaAKXe4Cfd8TQurCVd9fs2XQVBFRCsP+Id/SRdUv89MlwYu9zXfoyx6BxM+brPTIUK38SHbo8iaxiwzLi9JQ=="],++ "@cloudflare/workerd-linux-arm64": ["@cloudflare/[email protected]", "", { "os": "linux", "cpu": "arm64" }, "sha512-MRoifFYcqbxxIIQy7PqO5tFY/qPFSnjXzakWl0sO93l+HLyG35jRAgOi6jfqa4kBxc7gKKtH861DcewjxUfkjA=="],++ "@cloudflare/workerd-windows-64": ["@cloudflare/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-rgBV9wQrv0OSKgCTTbhFUFY3sLGNANZ88aqaLvtmEn2gmbFVb1J4PDGochVUdB7NSEp4D/ghHva6/8SZmbONpw=="],++ "@cspotcode/source-map-support": ["@cspotcode/[email protected]", "", { "dependencies": { "@jridgewell/trace-mapping": "0.3.9" } }, "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw=="],++ "@emnapi/runtime": ["@emnapi/[email protected]", "", { "dependencies": { "tslib": "^2.4.0" } }, "sha512-vgj7R3y3Wgx24IQaGPA/R6YFXLHVMOZ0uVEyIQPaWs+rd1AzfEMXlAC22FYwO1XkKR6NPsq7mUandH8oIRdZFw=="],++ "@esbuild/aix-ppc64": ["@esbuild/[email protected]", "", { "os": "aix", "cpu": "ppc64" }, "sha512-Svl7tq8k/08+p6CXPpRjQ1fKX+1odH/BQbb48fV6fj3CWHhsoIOoY87w1oHXm0qEpkIK3ZfVgp0hed3XBXzXMQ=="],++ "@esbuild/android-arm": ["@esbuild/[email protected]", "", { "os": "android", "cpu": "arm" }, "sha512-0k2F129Xdio1TdJfzJ8sy1Q47vUD2NnwdhiAf7drUN1EBTfPf4hsFCtmMgu/6m8JSzsBrlmVjudMBQqOfG8usQ=="],++ "@esbuild/android-arm64": ["@esbuild/[email protected]", "", { "os": "android", "cpu": "arm64" }, "sha512-34EGEbCIAgosYz6goLcopX6Mo7NyGv9tfwEM2/7Ce2VcVRk568iSvniGWcUXIy7wEDR1wzolcxcriFVrWYcwBg=="],++ "@esbuild/android-x64": ["@esbuild/[email protected]", "", { "os": "android", "cpu": "x64" }, "sha512-dbwY7ltSMDWsRatcRpCnES4F+im88OCUgGZjy52shC7GqHRE/cYlxNbB4Z4UpJswpcc4Qxd2oE/ufM0p61IKng=="],++ "@esbuild/darwin-arm64": ["@esbuild/[email protected]", "", { "os": "darwin", "cpu": "arm64" }, "sha512-TZbWkQY7kvTAXbXUT7uVACR5cMHsDiSz9z7ZKAX/RTq/WJEk3QyRr0wZpNhBDX+/0CtdqUIJlOiodQcta6tY3Q=="],++ "@esbuild/darwin-x64": ["@esbuild/[email protected]", "", { "os": "darwin", "cpu": "x64" }, "sha512-zfdzgK9ACBNZLI/CyHTOx81SyNbM6YXn7rxSgX97VjyiPl9W1i4Ka4fgKECEoFCKGpvBj5qArWIGgQjOwkgskQ=="],++ "@esbuild/freebsd-arm64": ["@esbuild/[email protected]", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-wG2EA8ENdEI0qhkSZMjfqrdY+ziCYCPMmtZjjIwOmXFjmyzEHn+UUxk5of+SYsjtfs3VpnlC7QLzSI5hY/rOAw=="],++ "@esbuild/freebsd-x64": ["@esbuild/[email protected]", "", { "os": "freebsd", "cpu": "x64" }, "sha512-i7dZ9vQgnvSCzi/rYCXNgtF/U+eKZNJBzu3eTQbRgHnM7tNSizLOkRFAl3qzVc/Op/u5YkHHa4pf/3DOYHthLQ=="],++ "@esbuild/linux-arm": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "arm" }, "sha512-qVXBOHQS+d5Y722GwJzJUtOLlX7km3CraOaGormF1pDtPd2C/l1SHRPgjLunLGe51Sh5YYWKMFDyV4SxgMQYTQ=="],++ "@esbuild/linux-arm64": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "arm64" }, "sha512-yHs+0uc8+nvEAfAfxrWQKK5peSNzBc4PegcMO0EJ2hT71uA7vB8Ihg2e77R2P7SG5uYjPbHlLLmve4LLLRCf0g=="],++ "@esbuild/linux-ia32": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "ia32" }, "sha512-d1z4ZuP0ajrfz/FhGT4vv278rX8KnPPJx8i5+AtK7TYbx9Le9F1hyzurZpkEyjkGa9dUGhQow4C1NmeGvqxN2w=="],++ "@esbuild/linux-loong64": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "none" }, "sha512-M5sRjUVZrkm1OAPR3dlOYzNmN+loZKGVi1VUQGrwuqLcbR6qeAz+famMhjASeH3YVKvZz+zT1jlh/keC3Rj/lg=="],++ "@esbuild/linux-mips64el": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "none" }, "sha512-mRObBZeHh2OxcBFPWE/FjylkRgZdYuiTR3vaTozquCGOH14iP9oN4x4Ge81CoIDYQrXmIxpFumJBu5MtZpnQJQ=="],++ "@esbuild/linux-ppc64": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "ppc64" }, "sha512-slScBsMAb3GFDcdrCgLwZtPYRoH2H/youv10QiZyRjmsP48fznoveWytSgCI/R0ZcUgpc0ZhIUEx6LHts8yrfQ=="],++ "@esbuild/linux-riscv64": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "none" }, "sha512-kw0owk1o0GFETUJyW0jc0G4Yzs0BHZn0JDZ8JRT088vjJYX777BAs1fDGxAC+q831qOs2DTC96mNsG2opdfyyQ=="],++ "@esbuild/linux-s390x": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "s390x" }, "sha512-/lAIjX8aYFRByhh6L5rYtPEDRqa9de/4V/juOXcta5frjvzXO4/sqEtyytse0g3zZFuWu5cDN0MkLz2qRDD2Ag=="],++ "@esbuild/linux-x64": ["@esbuild/[email protected]", "", { "os": "linux", "cpu": "x64" }, "sha512-u/anNYF2mmVOEDwLtnQ1wOr3EZ9sTNGLWrsYGYwHWzGA3Si84IOkHXlbWTD1NB+9/1lcnweYKO54uhxZydNzfA=="],++ "@esbuild/netbsd-arm64": ["@esbuild/[email protected]", "", { "os": "none", "cpu": "arm64" }, "sha512-oks0DYbLwWMmaakTsCb+zL4E+aHRVLom9IJZOAthMQEPiQmydXHkziYEsGYRx0uNV/IjEKGAV941JzH02pflqw=="],++ "@esbuild/netbsd-x64": ["@esbuild/[email protected]", "", { "os": "none", "cpu": "x64" }, "sha512-aeL6lAnN89Hz43Mlh1G8ARasbuoYvSITDEx0tHh5b7jJnHcssqgjy9Yx430GDpmCa6OyrKoS0aNRjKundRizGg=="],++ "@esbuild/openbsd-arm64": ["@esbuild/[email protected]", "", { "os": "openbsd", "cpu": "arm64" }, "sha512-MEFJe5C3R8pwXdZ5Y21oo6m7ePiS0d9pWucn99O/wvyJZChoIQKrQDxKrGeW8F5+T0okTHesAmDeiHDTIq0V/Q=="],++ "@esbuild/openbsd-x64": ["@esbuild/[email protected]", "", { "os": "openbsd", "cpu": "x64" }, "sha512-i/ZLIOafE0Z8cI/XANJAixoJL/uRAoS2xOA3rb0xN+KK0K177cMAsQYkzHtBrtMXAKuAc7HGgcWiZ/sRC1Nxgw=="],++ "@esbuild/openharmony-arm64": ["@esbuild/[email protected]", "", { "os": "none", "cpu": "arm64" }, "sha512-ge+Z7EXFNt2BO1oAMsVpiQ8EwndV9i1xXerAeTIK7AtPs3bKFXQM7nlRxDSIUIMeueR1CNXxqztLzdNeReKBJg=="],++ "@esbuild/sunos-x64": ["@esbuild/[email protected]", "", { "os": "sunos", "cpu": "x64" }, "sha512-BEjgtECkL3vY+SaSQ6nzVfiALUeFxpawyp8Jmf5PtYhf1Ug40N1h/hxlhts+f1FvSvarEigdxS3BlSMI2PJLcQ=="],++ "@esbuild/win32-arm64": ["@esbuild/[email protected]", "", { "os": "win32", "cpu": "arm64" }, "sha512-lCv9eK/H6ZJWbE7bh2nw54CZ9M2nupBxJcTsdk/QQnWkdSjKGuxmmH8/GWrlT1eMmZfn4dGcCjRte397WqfQXA=="],++ "@esbuild/win32-ia32": ["@esbuild/[email protected]", "", { "os": "win32", "cpu": "ia32" }, "sha512-zvb/mB2bSCoJOpoCBgYKKpX6YM6mJBlBUVUtVj41DlZJVEB6/0CKlRYxP5wWl1C1ILiCoAU5wZZ4q1P3qeS6Eg=="],++ "@esbuild/win32-x64": ["@esbuild/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-bm4Mowrv+GXMlpWX++EcXw/iLyd1o3+bJkC2DkWXYVvgZCqD/bSj9ctZeAMC3cIxgjRVR2Dufaiu4YPxr5gW1A=="],++ "@hono/zod-validator": ["@hono/[email protected]", "", { "peerDependencies": { "hono": ">=4.10.0", "zod": "^3.25.0 || ^4.0.0" } }, "sha512-5uS4S1/LKtZQYvD4BtpPUFkOv8d1wNxHHrChm26buMiEYc1FrHWvDUaKVBwkiVtvSExHSpLGDvcnpI2Copyj9w=="],++ "@img/colour": ["@img/[email protected]", "", {}, "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ=="],++ "@img/sharp-darwin-arm64": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-darwin-arm64": "1.2.4" }, "os": "darwin", "cpu": "arm64" }, "sha512-imtQ3WMJXbMY4fxb/Ndp6HBTNVtWCUI0WdobyheGf5+ad6xX8VIDO8u2xE4qc/fr08CKG/7dDseFtn6M6g/r3w=="],++ "@img/sharp-darwin-x64": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-darwin-x64": "1.2.4" }, "os": "darwin", "cpu": "x64" }, "sha512-YNEFAF/4KQ/PeW0N+r+aVVsoIY0/qxxikF2SWdp+NRkmMB7y9LBZAVqQ4yhGCm/H3H270OSykqmQMKLBhBJDEw=="],++ "@img/sharp-libvips-darwin-arm64": ["@img/[email protected]", "", { "os": "darwin", "cpu": "arm64" }, "sha512-zqjjo7RatFfFoP0MkQ51jfuFZBnVE2pRiaydKJ1G/rHZvnsrHAOcQALIi9sA5co5xenQdTugCvtb1cuf78Vf4g=="],++ "@img/sharp-libvips-darwin-x64": ["@img/[email protected]", "", { "os": "darwin", "cpu": "x64" }, "sha512-1IOd5xfVhlGwX+zXv2N93k0yMONvUlANylbJw1eTah8K/Jtpi15KC+WSiaX/nBmbm2HxRM1gZ0nSdjSsrZbGKg=="],++ "@img/sharp-libvips-linux-arm": ["@img/[email protected]", "", { "os": "linux", "cpu": "arm" }, "sha512-bFI7xcKFELdiNCVov8e44Ia4u2byA+l3XtsAj+Q8tfCwO6BQ8iDojYdvoPMqsKDkuoOo+X6HZA0s0q11ANMQ8A=="],++ "@img/sharp-libvips-linux-arm64": ["@img/[email protected]", "", { "os": "linux", "cpu": "arm64" }, "sha512-excjX8DfsIcJ10x1Kzr4RcWe1edC9PquDRRPx3YVCvQv+U5p7Yin2s32ftzikXojb1PIFc/9Mt28/y+iRklkrw=="],++ "@img/sharp-libvips-linux-ppc64": ["@img/[email protected]", "", { "os": "linux", "cpu": "ppc64" }, "sha512-FMuvGijLDYG6lW+b/UvyilUWu5Ayu+3r2d1S8notiGCIyYU/76eig1UfMmkZ7vwgOrzKzlQbFSuQfgm7GYUPpA=="],++ "@img/sharp-libvips-linux-riscv64": ["@img/[email protected]", "", { "os": "linux", "cpu": "none" }, "sha512-oVDbcR4zUC0ce82teubSm+x6ETixtKZBh/qbREIOcI3cULzDyb18Sr/Wcyx7NRQeQzOiHTNbZFF1UwPS2scyGA=="],++ "@img/sharp-libvips-linux-s390x": ["@img/[email protected]", "", { "os": "linux", "cpu": "s390x" }, "sha512-qmp9VrzgPgMoGZyPvrQHqk02uyjA0/QrTO26Tqk6l4ZV0MPWIW6LTkqOIov+J1yEu7MbFQaDpwdwJKhbJvuRxQ=="],++ "@img/sharp-libvips-linux-x64": ["@img/[email protected]", "", { "os": "linux", "cpu": "x64" }, "sha512-tJxiiLsmHc9Ax1bz3oaOYBURTXGIRDODBqhveVHonrHJ9/+k89qbLl0bcJns+e4t4rvaNBxaEZsFtSfAdquPrw=="],++ "@img/sharp-libvips-linuxmusl-arm64": ["@img/[email protected]", "", { "os": "linux", "cpu": "arm64" }, "sha512-FVQHuwx1IIuNow9QAbYUzJ+En8KcVm9Lk5+uGUQJHaZmMECZmOlix9HnH7n1TRkXMS0pGxIJokIVB9SuqZGGXw=="],++ "@img/sharp-libvips-linuxmusl-x64": ["@img/[email protected]", "", { "os": "linux", "cpu": "x64" }, "sha512-+LpyBk7L44ZIXwz/VYfglaX/okxezESc6UxDSoyo2Ks6Jxc4Y7sGjpgU9s4PMgqgjj1gZCylTieNamqA1MF7Dg=="],++ "@img/sharp-linux-arm": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-linux-arm": "1.2.4" }, "os": "linux", "cpu": "arm" }, "sha512-9dLqsvwtg1uuXBGZKsxem9595+ujv0sJ6Vi8wcTANSFpwV/GONat5eCkzQo/1O6zRIkh0m/8+5BjrRr7jDUSZw=="],++ "@img/sharp-linux-arm64": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-linux-arm64": "1.2.4" }, "os": "linux", "cpu": "arm64" }, "sha512-bKQzaJRY/bkPOXyKx5EVup7qkaojECG6NLYswgktOZjaXecSAeCWiZwwiFf3/Y+O1HrauiE3FVsGxFg8c24rZg=="],++ "@img/sharp-linux-ppc64": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-linux-ppc64": "1.2.4" }, "os": "linux", "cpu": "ppc64" }, "sha512-7zznwNaqW6YtsfrGGDA6BRkISKAAE1Jo0QdpNYXNMHu2+0dTrPflTLNkpc8l7MUP5M16ZJcUvysVWWrMefZquA=="],++ "@img/sharp-linux-riscv64": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-linux-riscv64": "1.2.4" }, "os": "linux", "cpu": "none" }, "sha512-51gJuLPTKa7piYPaVs8GmByo7/U7/7TZOq+cnXJIHZKavIRHAP77e3N2HEl3dgiqdD/w0yUfiJnII77PuDDFdw=="],++ "@img/sharp-linux-s390x": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-linux-s390x": "1.2.4" }, "os": "linux", "cpu": "s390x" }, "sha512-nQtCk0PdKfho3eC5MrbQoigJ2gd1CgddUMkabUj+rBevs8tZ2cULOx46E7oyX+04WGfABgIwmMC0VqieTiR4jg=="],++ "@img/sharp-linux-x64": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-linux-x64": "1.2.4" }, "os": "linux", "cpu": "x64" }, "sha512-MEzd8HPKxVxVenwAa+JRPwEC7QFjoPWuS5NZnBt6B3pu7EG2Ge0id1oLHZpPJdn3OQK+BQDiw9zStiHBTJQQQQ=="],++ "@img/sharp-linuxmusl-arm64": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-linuxmusl-arm64": "1.2.4" }, "os": "linux", "cpu": "arm64" }, "sha512-fprJR6GtRsMt6Kyfq44IsChVZeGN97gTD331weR1ex1c1rypDEABN6Tm2xa1wE6lYb5DdEnk03NZPqA7Id21yg=="],++ "@img/sharp-linuxmusl-x64": ["@img/[email protected]", "", { "optionalDependencies": { "@img/sharp-libvips-linuxmusl-x64": "1.2.4" }, "os": "linux", "cpu": "x64" }, "sha512-Jg8wNT1MUzIvhBFxViqrEhWDGzqymo3sV7z7ZsaWbZNDLXRJZoRGrjulp60YYtV4wfY8VIKcWidjojlLcWrd8Q=="],++ "@img/sharp-wasm32": ["@img/[email protected]", "", { "dependencies": { "@emnapi/runtime": "^1.7.0" }, "cpu": "none" }, "sha512-OdWTEiVkY2PHwqkbBI8frFxQQFekHaSSkUIJkwzclWZe64O1X4UlUjqqqLaPbUpMOQk6FBu/HtlGXNblIs0huw=="],++ "@img/sharp-win32-arm64": ["@img/[email protected]", "", { "os": "win32", "cpu": "arm64" }, "sha512-WQ3AgWCWYSb2yt+IG8mnC6Jdk9Whs7O0gxphblsLvdhSpSTtmu69ZG1Gkb6NuvxsNACwiPV6cNSZNzt0KPsw7g=="],++ "@img/sharp-win32-ia32": ["@img/[email protected]", "", { "os": "win32", "cpu": "ia32" }, "sha512-FV9m/7NmeCmSHDD5j4+4pNI8Cp3aW+JvLoXcTUo0IqyjSfAZJ8dIUmijx1qaJsIiU+Hosw6xM5KijAWRJCSgNg=="],++ "@img/sharp-win32-x64": ["@img/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-+29YMsqY2/9eFEiW93eqWnuLcWcufowXewwSNIT6UwZdUUCrM3oFjMWH/Z6/TMmb4hlFenmfAVbpWeup2jryCw=="],++ "@jitl/quickjs-ffi-types": ["@jitl/[email protected]", "", {}, "sha512-1yrgvXlmXH2oNj3eFTrkwacGJbmM0crwipA3ohCrjv52gBeDaD7PsTvFYinlAnqU8iPME3LGP437yk05a2oejw=="],++ "@jitl/quickjs-wasmfile-debug-asyncify": ["@jitl/[email protected]", "", { "dependencies": { "@jitl/quickjs-ffi-types": "0.31.0" } }, "sha512-YkdzQdr1uaftFhgEnTRjTTZHk2SFZdpWO7XhOmRVbi6CEVsH9g5oNF8Ta1q3OuSJHRwwT8YsuR1YzEiEIJEk6w=="],++ "@jitl/quickjs-wasmfile-debug-sync": ["@jitl/[email protected]", "", { "dependencies": { "@jitl/quickjs-ffi-types": "0.31.0" } }, "sha512-8XvloaaWBONqcHXYs5tWOjdhQVxzULilIfB2hvZfS6S+fI4m2+lFiwQy7xeP8ExHmiZ7D8gZGChNkdLgjGfknw=="],++ "@jitl/quickjs-wasmfile-release-asyncify": ["@jitl/[email protected]", "", { "dependencies": { "@jitl/quickjs-ffi-types": "0.31.0" } }, "sha512-uz0BbQYTxNsFkvkurd7vk2dOg57ElTBLCuvNtRl4rgrtbC++NIndD5qv2+AXb6yXDD3Uy1O2PCwmoaH0eXgEOg=="],++ "@jitl/quickjs-wasmfile-release-sync": ["@jitl/[email protected]", "", { "dependencies": { "@jitl/quickjs-ffi-types": "0.31.0" } }, "sha512-hYduecOByj9AsAfsJhZh5nA6exokmuFC8cls39+lYmTCGY51bgjJJJwReEu7Ff7vBWaQCL6TeDdVlnp2WYz0jw=="],++ "@jridgewell/resolve-uri": ["@jridgewell/[email protected]", "", {}, "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw=="],++ "@jridgewell/sourcemap-codec": ["@jridgewell/[email protected]", "", {}, "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og=="],++ "@jridgewell/trace-mapping": ["@jridgewell/[email protected]", "", { "dependencies": { "@jridgewell/resolve-uri": "^3.0.3", "@jridgewell/sourcemap-codec": "^1.4.10" } }, "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ=="],++ "@opentelemetry/api": ["@opentelemetry/[email protected]", "", {}, "sha512-gLyJlPHPZYdAk1JENA9LeHejZe1Ti77/pTeFm/nMXmQH/HFZlcS/O2XJB+L8fkbrNSqhdtlvjBVjxwUYanNH5Q=="],++ "@poppinss/colors": ["@poppinss/[email protected]", "", { "dependencies": { "kleur": "^4.1.5" } }, "sha512-H9xkIdFswbS8n1d6vmRd8+c10t2Qe+rZITbbDHHkQixH5+2x1FDGmi/0K+WgWiqQFKPSlIYB7jlH6Kpfn6Fleg=="],++ "@poppinss/dumper": ["@poppinss/[email protected]", "", { "dependencies": { "@poppinss/colors": "^4.1.5", "@sindresorhus/is": "^7.0.2", "supports-color": "^10.0.0" } }, "sha512-NBdYIb90J7LfOI32dOewKI1r7wnkiH6m920puQ3qHUeZkxNkQiFnXVWoE6YtFSv6QOiPPf7ys6i+HWWecDz7sw=="],++ "@poppinss/exception": ["@poppinss/[email protected]", "", {}, "sha512-dCED+QRChTVatE9ibtoaxc+WkdzOSjYTKi/+uacHWIsfodVfpsueo3+DKpgU5Px8qXjgmXkSvhXvSCz3fnP9lw=="],++ "@sindresorhus/is": ["@sindresorhus/[email protected]", "", {}, "sha512-P1Cz1dWaFfR4IR+U13mqqiGsLFf1KbayybWwdd2vfctdV6hDpUkgCY0nKOLLTMSoRd/jJNjtbqzf13K8DCCXQw=="],++ "@speed-highlight/core": ["@speed-highlight/[email protected]", "", {}, "sha512-Z92FwKpCtfaW1V0jTU/fh3QzYEZN8wDwrzRIBoADCJfn4mJCNcJN/XegifX7BDrQ8/h9Xh/JnbyMchL0FqXrkg=="],++ "@standard-schema/spec": ["@standard-schema/[email protected]", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="],++ "@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": ">=7.24.0 <7.24.7" } }, "sha512-dszCsrKb5U7ZsVZBWiHFklTloVl0mSEnWH/iZXfZUlI4rzCUnsvGmgqfuVRHL54ugE7/wRuxEIXRa2iMZ+BG6g=="],++ "@types/yargs": ["@types/[email protected]", "", { "dependencies": { "@types/yargs-parser": "*" } }, "sha512-qUHkeCyQFxMXg79wQfTtfndEC+N9ZZg76HJftDJp+qH2tV7Gj4OJi7l+PiWwJ+pWtW8GwSmqsDj/oymhrTWXjg=="],++ "@types/yargs-parser": ["@types/[email protected]", "", {}, "sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ=="],++ "@vercel/oidc": ["@vercel/[email protected]", "", {}, "sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug=="],++ "ai": ["[email protected]", "", { "dependencies": { "@ai-sdk/gateway": "3.0.133", "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30", "@opentelemetry/api": "^1.9.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-STz+AaZqJ4ZjH7UkpXkbHx+bjgIDOsE8fIUoZjkZ2whoZcfVmG9K/TqEKouJZ03SuZuD7lagntlU3zBhAEkRpQ=="],++ "ai-fallback": ["[email protected]", "", { "peerDependencies": { "@ai-sdk/provider": ">=3.0.0", "@ai-sdk/provider-utils": ">=4.0.0" } }, "sha512-uF/7dstZFT5Lu5DuHjJI6edCnbg8s8sxqxlsLbfcv9YvWnrYIh8aPAW7j9ij/L334Ad4v1rNjSJiL7gOB/iRfA=="],++ "ansi-regex": ["[email protected]", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="],++ "ansi-styles": ["[email protected]", "", {}, "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg=="],++ "blake3-wasm": ["[email protected]", "", {}, "sha512-F1+K8EbfOZE49dtoPtmxUQrpXaBIl3ICvasLh+nJta0xkz+9kF/7uet9fLnwKqhDrmj6g+6K3Tw9yQPUg2ka5g=="],++ "bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-4N0ig0fEomHt5R0KCFWjovxow98rIoRwKolrYdCcknNwMekCXRnWEUvgu5soYV8QXtVsrUD8B95MBOZGPvr6KQ=="],++ "cliui": ["[email protected]", "", { "dependencies": { "string-width": "^7.2.0", "strip-ansi": "^7.1.0", "wrap-ansi": "^9.0.0" } }, "sha512-k7ndgKhwoQveBL+/1tqGJYNz097I7WOvwbmmU2AR5+magtbjPWQTS1C5vzGkBC8Ym8UWRzfKUzUUqFLypY4Q+w=="],++ "cookie": ["[email protected]", "", {}, "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ=="],++ "detect-libc": ["[email protected]", "", {}, "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ=="],++ "emoji-regex": ["[email protected]", "", {}, "sha512-toUI84YS5YmxW219erniWD0CIVOo46xGKColeNQRgOzDorgBi1v4D71/OFzgD9GO2UGKIv1C3Sp8DAn0+j5w7A=="],++ "envsafe": ["[email protected]", "", {}, "sha512-51lek8h5btjXhXUDW//Pno7HPydM1WQzltOb1/ONRKdwB2QkfAURnN4Qn0cJ5LzGvX2Km1ReR2O3K3Bqq9sBMg=="],++ "error-stack-parser-es": ["[email protected]", "", {}, "sha512-5qucVt2XcuGMcEGgWI7i+yZpmpByQ8J1lHhcL7PwqCwu9FPP3VUXzT4ltHe5i2z9dePwEHcDVOAfSnHsOlCXRA=="],++ "esbuild": ["[email protected]", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.28.1", "@esbuild/android-arm": "0.28.1", "@esbuild/android-arm64": "0.28.1", "@esbuild/android-x64": "0.28.1", "@esbuild/darwin-arm64": "0.28.1", "@esbuild/darwin-x64": "0.28.1", "@esbuild/freebsd-arm64": "0.28.1", "@esbuild/freebsd-x64": "0.28.1", "@esbuild/linux-arm": "0.28.1", "@esbuild/linux-arm64": "0.28.1", "@esbuild/linux-ia32": "0.28.1", "@esbuild/linux-loong64": "0.28.1", "@esbuild/linux-mips64el": "0.28.1", "@esbuild/linux-ppc64": "0.28.1", "@esbuild/linux-riscv64": "0.28.1", "@esbuild/linux-s390x": "0.28.1", "@esbuild/linux-x64": "0.28.1", "@esbuild/netbsd-arm64": "0.28.1", "@esbuild/netbsd-x64": "0.28.1", "@esbuild/openbsd-arm64": "0.28.1", "@esbuild/openbsd-x64": "0.28.1", "@esbuild/openharmony-arm64": "0.28.1", "@esbuild/sunos-x64": "0.28.1", "@esbuild/win32-arm64": "0.28.1", "@esbuild/win32-ia32": "0.28.1", "@esbuild/win32-x64": "0.28.1" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-HrJrvZv5ayxBzPfwphOoNzkzOIIlifzk0KJrGK2c8R4+LKpMtpYLQeUdjnwjWv/LZlkH2laZk+4w78pi99D4Vw=="],++ "escalade": ["[email protected]", "", {}, "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA=="],++ "eventsource-parser": ["[email protected]", "", {}, "sha512-kJezFj9YFAMLeORyi7aCLxLbD5/qWMQnoMVlVPyHIll7lgRJCc3JVln9Vgl9nwQi0YkMnhdGTMNn7CkRRAptMg=="],++ "fsevents": ["[email protected]", "", { "os": "darwin" }, "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw=="],++ "get-caller-file": ["[email protected]", "", {}, "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg=="],++ "get-east-asian-width": ["[email protected]", "", {}, "sha512-QRbvDIbx6YklUe6RxeTeleMR0yv3cYH6PsPZHcnVn7xv7zO1BHN8r0XETu8n6Ye3Q+ahtSarc3WgtNWmehIBfA=="],++ "hono": ["[email protected]", "", {}, "sha512-uyZtpnYxM9CmQ7QsQknM4zN8EftNqhON1qYeIKM0Se67CCEe2c44xyGURwB0axX2fBDu1dqHrHAc1hmNT8ITkw=="],++ "json-schema": ["[email protected]", "", {}, "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA=="],++ "kleur": ["[email protected]", "", {}, "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ=="],++ "miniflare": ["[email protected]", "", { "dependencies": { "@cspotcode/source-map-support": "0.8.1", "sharp": "0.34.5", "undici": "7.28.0", "workerd": "1.20260617.1", "ws": "8.21.0", "youch": "4.1.0-beta.10" }, "bin": { "miniflare": "bootstrap.js" } }, "sha512-Go3/gzStm99QHptsSgU+q1S+xDfLoRgwjJNY80kaTVi0ENhTyqKq+sc4xZiWBSbM7uUcJwmzm8+QFKtcYLJ9nw=="],++ "path-to-regexp": ["[email protected]", "", {}, "sha512-Yhpw4T9C6hPpgPeA28us07OJeqZ5EzQTkbfwuhsUg0c237RomFoETJgmp2sa3F/41gfLE6G5cqcYwznmeEeOlQ=="],++ "pathe": ["[email protected]", "", {}, "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w=="],++ "quickjs-emscripten": ["[email protected]", "", { "dependencies": { "@jitl/quickjs-wasmfile-debug-asyncify": "0.31.0", "@jitl/quickjs-wasmfile-debug-sync": "0.31.0", "@jitl/quickjs-wasmfile-release-asyncify": "0.31.0", "@jitl/quickjs-wasmfile-release-sync": "0.31.0", "quickjs-emscripten-core": "0.31.0" } }, "sha512-K7Yt78aRPLjPcqv3fIuLW1jW3pvwO21B9pmFOolsjM/57ZhdVXBr51GqJpalgBlkPu9foAvhEAuuQPnvIGvLvQ=="],++ "quickjs-emscripten-core": ["[email protected]", "", { "dependencies": { "@jitl/quickjs-ffi-types": "0.31.0" } }, "sha512-oQz8p0SiKDBc1TC7ZBK2fr0GoSHZKA0jZIeXxsnCyCs4y32FStzCW4d1h6E1sE0uHDMbGITbk2zhNaytaoJwXQ=="],++ "semver": ["[email protected]", "", { "bin": { "semver": "bin/semver.js" } }, "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA=="],++ "sharp": ["[email protected]", "", { "dependencies": { "@img/colour": "^1.0.0", "detect-libc": "^2.1.2", "semver": "^7.7.3" }, "optionalDependencies": { "@img/sharp-darwin-arm64": "0.34.5", "@img/sharp-darwin-x64": "0.34.5", "@img/sharp-libvips-darwin-arm64": "1.2.4", "@img/sharp-libvips-darwin-x64": "1.2.4", "@img/sharp-libvips-linux-arm": "1.2.4", "@img/sharp-libvips-linux-arm64": "1.2.4", "@img/sharp-libvips-linux-ppc64": "1.2.4", "@img/sharp-libvips-linux-riscv64": "1.2.4", "@img/sharp-libvips-linux-s390x": "1.2.4", "@img/sharp-libvips-linux-x64": "1.2.4", "@img/sharp-libvips-linuxmusl-arm64": "1.2.4", "@img/sharp-libvips-linuxmusl-x64": "1.2.4", "@img/sharp-linux-arm": "0.34.5", "@img/sharp-linux-arm64": "0.34.5", "@img/sharp-linux-ppc64": "0.34.5", "@img/sharp-linux-riscv64": "0.34.5", "@img/sharp-linux-s390x": "0.34.5", "@img/sharp-linux-x64": "0.34.5", "@img/sharp-linuxmusl-arm64": "0.34.5", "@img/sharp-linuxmusl-x64": "0.34.5", "@img/sharp-wasm32": "0.34.5", "@img/sharp-win32-arm64": "0.34.5", "@img/sharp-win32-ia32": "0.34.5", "@img/sharp-win32-x64": "0.34.5" } }, "sha512-Ou9I5Ft9WNcCbXrU9cMgPBcCK8LiwLqcbywW3t4oDV37n1pzpuNLsYiAV8eODnjbtQlSDwZ2cUEeQz4E54Hltg=="],++ "string-width": ["[email protected]", "", { "dependencies": { "emoji-regex": "^10.3.0", "get-east-asian-width": "^1.0.0", "strip-ansi": "^7.1.0" } }, "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ=="],++ "strip-ansi": ["[email protected]", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="],++ "supports-color": ["[email protected]", "", {}, "sha512-SS+jx45GF1QjgEXQx4NJZV9ImqmO2NPz5FNsIHrsDjh2YsHnawpan7SNQ1o8NuhrbHZy9AZhIoCUiCeaW/C80g=="],++ "tslib": ["[email protected]", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],++ "undici": ["[email protected]", "", {}, "sha512-cRZYrTDwWznlnRiPjggAGxZXanty6M8RV1ff8Wm4LWXBp7/IG8v5DnOm74DtUBp9OONpK75YlPnIjQqX0dBDtA=="],++ "undici-types": ["[email protected]", "", {}, "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg=="],++ "unenv": ["[email protected]", "", { "dependencies": { "pathe": "^2.0.3" } }, "sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw=="],++ "workerd": ["[email protected]", "", { "optionalDependencies": { "@cloudflare/workerd-darwin-64": "1.20260617.1", "@cloudflare/workerd-darwin-arm64": "1.20260617.1", "@cloudflare/workerd-linux-64": "1.20260617.1", "@cloudflare/workerd-linux-arm64": "1.20260617.1", "@cloudflare/workerd-windows-64": "1.20260617.1" }, "bin": { "workerd": "bin/workerd" } }, "sha512-Re5pl6pdowt3ZmWUzGlOuB7jbRIIPetgKalmo4cYmucQnVhpo7/3e4MfpekbhLi2EhZZz5EY9NWRu8zFzuEZew=="],++ "wrangler": ["[email protected]", "", { "dependencies": { "@cloudflare/kv-asset-handler": "0.5.0", "@cloudflare/unenv-preset": "2.16.1", "blake3-wasm": "2.1.5", "esbuild": "0.28.1", "miniflare": "4.20260617.1", "path-to-regexp": "6.3.0", "unenv": "2.0.0-rc.24", "workerd": "1.20260617.1" }, "optionalDependencies": { "fsevents": "2.3.3" }, "peerDependencies": { "@cloudflare/workers-types": "^4.20260617.1" }, "optionalPeers": ["@cloudflare/workers-types"], "bin": { "cf-wrangler": "bin/cf-wrangler.js", "wrangler": "bin/wrangler.js", "wrangler2": "bin/wrangler.js" } }, "sha512-3Lv1P5t2xcSEkSTKtG+Lz+3JFryuU7YPLkaCUj7gNe+CJsjZJLtUwqsh1x595QBxkIbCE0GAvDx2DCJUU4+oqw=="],++ "wrap-ansi": ["[email protected]", "", { "dependencies": { "ansi-styles": "^6.2.1", "string-width": "^7.0.0", "strip-ansi": "^7.1.0" } }, "sha512-42AtmgqjV+X1VpdOfyTGOYRi0/zsoLqtXQckTmqTeybT+BDIbM/Guxo7x3pE2vtpr1ok6xRqM9OpBe+Jyoqyww=="],++ "ws": ["[email protected]", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g=="],++ "y18n": ["[email protected]", "", {}, "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA=="],++ "yargs": ["[email protected]", "", { "dependencies": { "cliui": "^9.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "string-width": "^7.2.0", "y18n": "^5.0.5", "yargs-parser": "^22.0.0" } }, "sha512-4UEqdc2RYGHZc7Doyqkrqiln3p9X2DZVxaGbwhn2pi7MrRagKaOcIKe8L3OxYcbhXLgLFUS3zAYuQjKBQgmuNg=="],++ "yargs-parser": ["[email protected]", "", {}, "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw=="],++ "youch": ["[email protected]", "", { "dependencies": { "@poppinss/colors": "^4.1.5", "@poppinss/dumper": "^0.6.4", "@speed-highlight/core": "^1.2.7", "cookie": "^1.0.2", "youch-core": "^0.3.3" } }, "sha512-rLfVLB4FgQneDr0dv1oddCVZmKjcJ6yX6mS4pU82Mq/Dt9a3cLZQ62pDBL4AUO+uVrCvtWz3ZFUL2HFAFJ/BXQ=="],++ "youch-core": ["[email protected]", "", { "dependencies": { "@poppinss/exception": "^1.2.2", "error-stack-parser-es": "^1.0.5" } }, "sha512-ho7XuGjLaJ2hWHoK8yFnsUGy2Y5uDpqSTq1FkHLK4/oqKtyUU1AFbOOxY4IpC9f0fTLjwYbslUz0Po5BpD1wrA=="],++ "zod": ["[email protected]", "", {}, "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ=="],+ }+}diff --git a/js/ai-editing-worker/justfile b/js/ai-editing-worker/justfilenew file mode 100644index 0000000000..146d65c399--- /dev/null+++ b/js/ai-editing-worker/justfile@@ -0,0 +1,26 @@+dev-local:+ bun run src/server.ts++dev:+ SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt npx wrangler dev --env local++dev-remote-dev:+ npx wrangler dev --env dev++dev-remote-prod:+ npx wrangler dev --env prod++generate-sandbox:+ bun scripts/generate-sandbox.ts++deploy-dev-dry-run: generate-sandbox+ npx wrangler deploy --env dev --dry-run++deploy-dev: generate-sandbox+ npx wrangler deploy --env dev++deploy-playground: generate-sandbox+ npx wrangler deploy --env playground++deploy-prod: generate-sandbox+ npx wrangler deploy --env proddiff --git a/js/ai-editing-worker/package.json b/js/ai-editing-worker/package.jsonnew file mode 100644index 0000000000..9cdda0bc3c--- /dev/null+++ b/js/ai-editing-worker/package.json@@ -0,0 +1,29 @@+{+ "name": "ai-editing-worker",+ "version": "0.0.1",+ "private": true,+ "type": "module",+ "scripts": {+ "prebuild": "bun scripts/generate-sandbox.ts",+ "type-check": "tsc --noEmit --project tsconfig.json"+ },+ "dependencies": {+ "@ai-sdk/anthropic": "^3.0.85",+ "@ai-sdk/cerebras": "^2.0.57",+ "@ai-sdk/openai": "^3.0.73",+ "@hono/zod-validator": "^0.8.0",+ "ai": "^6.0.207",+ "ai-fallback": "2",+ "envsafe": "^2.0.3",+ "hono": "^4.6.20",+ "quickjs-emscripten": "^0.31.0",+ "yargs": "^18.0.0",+ "zod": "^4.4.3"+ },+ "devDependencies": {+ "@types/node": "^25.9.3",+ "@types/yargs": "^17.0.35",+ "bun-types": "^1.3.14",+ "wrangler": "^4.103.0"+ }+}diff --git a/js/ai-editing-worker/prompts.d.ts b/js/ai-editing-worker/prompts.d.tsnew file mode 100644index 0000000000..a47bd629a6--- /dev/null+++ b/js/ai-editing-worker/prompts.d.ts@@ -0,0 +1,4 @@+declare module "*.md" {+ const content: string;+ export default content;+}diff --git a/js/ai-editing-worker/scripts/active-editors.ts b/js/ai-editing-worker/scripts/active-editors.tsnew file mode 100644index 0000000000..2bb0edd95a--- /dev/null+++ b/js/ai-editing-worker/scripts/active-editors.ts@@ -0,0 +1,49 @@+#!/usr/bin/env bun+import yargs from "yargs";+import { hideBin } from "yargs/helpers";++const {+ token,+ "sync-base": syncBase,+ "include-ai": includeAi,+ _,+} = await yargs(hideBin(process.argv))+ .usage("$0 <document-id> --token <doc-permission-token>")+ .help()+ .option("token", {+ type: "string",+ demandOption: true,+ describe: "document permission JWT (sent as the Bearer token)",+ })+ .option("sync-base", {+ type: "string",+ default: "http://localhost:8787",+ describe: "sync service base URL",+ })+ .option("include-ai", {+ type: "boolean",+ default: false,+ describe: "include AI editor peers in the result",+ })+ .parse();++const documentId = _[0] as string | undefined;+if (!documentId) {+ console.error(+ "Usage: bun run scripts/active-editors.ts <document-id> --token <doc-permission-token>",+ );+ process.exit(1);+}++const url = `${syncBase}/document/${documentId}/active_peers?include_ai=${includeAi}`;+const res = await fetch(url, {+ headers: { Authorization: `Bearer ${token}` },+});++if (!res.ok) {+ console.error(`${res.status} ${res.statusText}: ${await res.text()}`);+ process.exit(1);+}++const peerIds = (await res.json()) as string[];+console.log(JSON.stringify(peerIds, null, 2));diff --git a/js/ai-editing-worker/scripts/edit.ts b/js/ai-editing-worker/scripts/edit.tsnew file mode 100644index 0000000000..dce5c8bee7--- /dev/null+++ b/js/ai-editing-worker/scripts/edit.ts@@ -0,0 +1,101 @@+#!/usr/bin/env bun+import yargs from "yargs";+import { hideBin } from "yargs/helpers";++const {+ port,+ "worker-url": workerUrlOpt,+ "ws-url": wsUrl,+ "supervisor-model": supervisorModelFlag,+ "interpret-model": interpretModelFlag,+ "coding-model": codingModelFlag,+ debug,+ out,+ "prompt-file": promptFile,+ _,+} = await yargs(hideBin(process.argv))+ .usage("$0 [prompt] --ws-url <url>")+ .help()+ .option("ws-url", { type: "string", demandOption: true, describe: "sync service ws url with token, e.g. wss://…/document/<id>/connect?token=<doc-token>" })+ .option("port", { type: "number", default: 8933, describe: "worker port" })+ .option("worker-url", { type: "string", describe: "full worker base URL (overrides --port)" })+ .option("supervisor-model", { type: "string", demandOption: true, describe: "provider:model for the supervisor" })+ .option("interpret-model", { type: "string", demandOption: true, describe: "provider:model for the interpret pass" })+ .option("coding-model", { type: "string", demandOption: true, describe: "provider:model for the coding agents" })+ .option("debug", { type: "boolean", default: false, describe: "include the supervisor step trace + replay trace in the response" })+ .option("out", { type: "string", describe: "write the replay trace JSON to this file (implies --debug)" })+ .option("prompt-file", { type: "string", describe: "read the prompt from this file instead of the positional arg" })+ .parse();++const prompt = promptFile+ ? (await Bun.file(promptFile as string).text()).trim()+ : (_[0] as string | undefined);+if (!prompt) {+ console.error("Usage: bun run scripts/edit.ts [prompt] --ws-url <url> (or pass --prompt-file <path>)");+ process.exit(1);+}++const parsed = new URL(wsUrl as string);+const pathParts = parsed.pathname.split("/");+const documentId = pathParts[2];+const documentToken = parsed.searchParams.get("token");+if (!documentId || !documentToken) {+ console.error("--ws-url must be in the form wss://…/document/<id>/connect?token=<doc-token>");+ process.exit(1);+}++const parseModel = (flag: string) => {+ const [provider, model] = flag.split(":");+ return { provider: provider!, model: model! };+};++const workerUrl = workerUrlOpt ?? `http://localhost:${port}`;++const controller = new AbortController();+process.on("SIGINT", () => {+ console.error("\naborting request…");+ controller.abort();+});+const wantDebug = debug || Boolean(out);++const res = await fetch(`${workerUrl}/edit`, {+ method: "POST",+ signal: controller.signal,+ headers: { "content-type": "application/json" },+ body: JSON.stringify({+ documentToken,+ documentId,+ prompt,+ models: {+ supervisor: [parseModel(supervisorModelFlag)],+ interpret: [parseModel(interpretModelFlag)],+ coding: [parseModel(codingModelFlag)],+ },+ debug: wantDebug,+ }),+ timeout: false,+} as RequestInit & { timeout: boolean }).catch((err) => {+ if (controller.signal.aborted) process.exit(130);+ throw err;+});++const body = (await res.json()) as {+ trace?: string;+ replay?: unknown;+} & Record<string, unknown>;++if (out && body.replay) {+ await Bun.write(out, JSON.stringify(body.replay));+ const events = (body.replay as { events?: unknown[] }).events ?? [];+ console.log(`wrote replay trace to ${out} (${events.length} events)`);+}++if (wantDebug && body.trace) {+ console.log(body.trace);+ const { trace, replay, ...rest } = body;+ console.log("\n---\n");+ console.log(rest);+} else {+ const { replay, ...rest } = body;+ console.log(rest);+}diff --git a/js/ai-editing-worker/scripts/generate-sandbox.ts b/js/ai-editing-worker/scripts/generate-sandbox.tsnew file mode 100644index 0000000000..67d83d1548--- /dev/null+++ b/js/ai-editing-worker/scripts/generate-sandbox.ts@@ -0,0 +1,31 @@+/**+ * Transpiles DocumentEditor + EditError to plain JS and writes editor-sandbox-code.ts.+ * Run via `bun scripts/generate-sandbox.ts` (wired into prebuild).+ */+import { join, dirname } from 'node:path';+import { fileURLToPath } from 'node:url';++const root = join(dirname(fileURLToPath(import.meta.url)), '..');+const t = new Bun.Transpiler({ loader: 'ts' });++async function transpile(relPath: string): Promise<string> {+ const src = await Bun.file(join(root, relPath)).text();+ // Strip import/export — QuickJS evaluates a plain script, not an ES module.+ return t.transformSync(src)+ .replace(/^import\s.*$/gm, '')+ .replace(/^export\s+(?:default\s+)?/gm, '')+ .trim();+}++const code = [+ await transpile('src/ai-editing/editor/errors.ts'),+ await transpile('src/ai-editing/editor/document-editor.ts'),+].join('\n\n');++const out = `// Auto-generated by scripts/generate-sandbox.ts — do not edit.+// Source of truth: src/ai-editing/editor/document-editor.ts+export const SANDBOX_CODE = ${JSON.stringify(code)};+`;++await Bun.write(join(root, 'src/editor-sandbox-code.ts'), out);+console.log('generated src/editor-sandbox-code.ts');diff --git a/js/ai-editing-worker/scripts/to-xml.ts b/js/ai-editing-worker/scripts/to-xml.tsnew file mode 100644index 0000000000..73e64c198e--- /dev/null+++ b/js/ai-editing-worker/scripts/to-xml.ts@@ -0,0 +1,5 @@+import { toXml } from "../../lexical-core/transformers/xml";++const json = await Bun.stdin.text();+const state = JSON.parse(json);+process.stdout.write(toXml(state) + "\n");diff --git a/js/ai-editing-worker/scripts/typer.ts b/js/ai-editing-worker/scripts/typer.tsnew file mode 100644index 0000000000..81435ef29d--- /dev/null+++ b/js/ai-editing-worker/scripts/typer.ts@@ -0,0 +1,122 @@+// This is a small helper that uses the helpers of the ai worker to type random+// gibberish into a document++import "../src/globals";+import yargs from "yargs";+import { hideBin } from "yargs/helpers";+import { $getRoot } from "lexical";+import type { SerializedEditorState } from "lexical";+import { $getId, $updateAllNodeIds } from "../../lexical-core/plugins/nodeIdPlugin";+import {+ createEditingSession,+ loadSnapshot,+ toSnapshot,+} from "../src/ai-editing/ai-toolkit";+import { Doc } from "../src/ai-editing/doc/doc";+import { nextAiPeerId } from "../src/ai-editing/awareness/ai-peer";+import {+ AI_NAMES,+ COLORS,+ realAwarenessSource,+} from "../src/ai-editing/awareness/awareness-source";+import { DocumentEditor } from "../src/ai-editing/editor/document-editor";+import { type CodeRunner, runEditorCode } from "../src/ai-editing/runtime";+import { MARKDOWN_LORO_SCHEMA, type MarkdownLoroSchemaType } from "../../lexical-core/markdown-loro-schema";+import type { InferType } from "@loro-mirror/packages/core/src";+import { createWorkerSyncSource, createWorkerAwareness } from "../src/sources";+import { LoroManager } from "../../app/packages/core/collab/manager";+import { SyncEngine } from "../../app/packages/core/collab/engine";+import { InMemoryWALStore, WALSyncer } from "../../app/packages/core/collab/wal";+import type { RawUpdate } from "../../app/packages/core/collab/shared";++const argv = await yargs(hideBin(process.argv)).usage("$0 <wss-url>").help().parse();+const wssUrl = argv._[0] as string | undefined;+if (!wssUrl) { yargs().showHelp(); process.exit(1); }++const source = createWorkerSyncSource(wssUrl, "", undefined);+const initial = await source.doInitialSync();+if (initial.isErr()) throw new Error(`initial sync failed: ${initial.error.type}`);+const { snapshot } = initial.value;++const manager = new LoroManager(MARKDOWN_LORO_SCHEMA, { documentId: "" });+let engine: SyncEngine<typeof MARKDOWN_LORO_SCHEMA, unknown> | undefined;+manager.onStateChange((u) => queueMicrotask(() => engine?.onStateUpdate(u)));++const initResult = await manager.initializeFromSnapshot(snapshot);+if (initResult.isErr()) throw new Error(`failed to initialize: ${initResult.error[0]?.message}`);++const session = createEditingSession();+loadSnapshot(session, manager.mirror!.getState() as unknown as SerializedEditorState);++const workerAwareness = createWorkerAwareness(manager.peerIdStr);+const wal = new WALSyncer<RawUpdate>(new InMemoryWALStore<RawUpdate>(), (updates) => source.pushUpdate(updates));+engine = new SyncEngine({+ loroManager: manager,+ awareness: workerAwareness,+ syncs: { wal, live: source },+ bindings: { onRemoteState: () => {} },+});+engine.start();++let chain: Promise<void> = Promise.resolve();+const propagate = () => {+ chain = chain.then(async () => {+ const newPeer = nextAiPeerId();+ manager.doc.commit();+ manager.doc.setPeerId(newPeer);+ source.registerPeerId(newPeer);+ session.editor.update(() => $updateAllNodeIds(session.ids), { discrete: true });+ await engine!.syncStateToLoro(toSnapshot(session) as unknown as InferType<MarkdownLoroSchemaType>);+ });+};++const awareness = realAwarenessSource({+ mirror: manager.mirror!,+ doc: manager.doc,+ send: (bytes) => source.pushAwareness(bytes),+ name: AI_NAMES[0]!,+ color: COLORS[0]!,+});++const firstId = session.editor.getEditorState().read(() =>+ $getId($getRoot().getFirstChildOrThrow()),+);+if (!firstId) throw new Error("document has no nodes");++const paragraphs = [+ "The quick brown fox jumps over the lazy dog, and then, having completed that particular feat of athletic prowess, paused to reflect on the existential implications of being a fox in a world that seemingly only exists to test the typographic completeness of font families and keyboard layouts.",+ "It was a dark and stormy night, or at least that is what the weather forecast had suggested earlier in the week, though by the time the actual evening arrived the clouds had largely dispersed, leaving behind only a faint drizzle and the lingering sense that meteorology is, at its core, an exercise in optimistic uncertainty.",+ "The history of human civilization can be understood, if one squints sufficiently and ignores a great many inconvenient counterexamples, as a long and winding journey from sitting in caves wondering what that rustling noise was, all the way to sitting in offices wondering what that notification sound was, which is to say the fundamental anxieties have remained remarkably consistent.",+];++const inserts = paragraphs+ .map(+ (text, i) =>+ i === 0+ ? `const ref0 = editor.insertParagraphAfter('${firstId}', ${JSON.stringify(text)});`+ : `const ref${i} = editor.insertParagraphAfter(ref${i - 1}, ${JSON.stringify(text)});`,+ )+ .join("\n");++const runner: CodeRunner = (validIds, code) => {+ const refs = Array.from({ length: 128 }, (_, i) => `ref-${i + 1}`);+ const editor = new DocumentEditor({ validIds, refs });+ new Function("editor", code)(editor);+ return editor.drain();+};++await runEditorCode({+ session,+ doc: new Doc(session, propagate),+ code: inserts,+ awarenessSource: awareness,+ runner,+});++await chain;+await wal.flush();+awareness.clear();+engine.stop();+wal.destroy();+manager.dispose();+source.cleanup();diff --git a/js/ai-editing-worker/src/ai-editing/agents/coder.ts b/js/ai-editing-worker/src/ai-editing/agents/coder.tsnew file mode 100644index 0000000000..9482c4cebe--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/agents/coder.ts@@ -0,0 +1,69 @@+import { generateText, hasToolCall, type LanguageModel, stepCountIs } from 'ai';+import type { LexicalSession } from '../ai-toolkit';+import API_COMPLETE from '../prompts/API_COMPLETE.md';+import CODER from '../prompts/CODER.md';+import SHARED from '../prompts/SHARED.md';+import {+ createImBlockedTool,+ createReadDocumentTool,+ createRunCodeTool,+} from '../tools';+import type { RunTaskDeps } from './types';++export type { RunTaskDeps } from './types';++export const CHILD_SYSTEM = `${SHARED}\n${CODER}\n${API_COMPLETE}`;++/** One writer: carry out a single edit instruction via the `editor` surface. */+export async function coder(+ session: LexicalSession,+ task: string,+ model: LanguageModel,+ deps: RunTaskDeps+) {+ return generateText({+ model,+ // Stop on the step cap OR the moment the writer declares itself blocked.+ // Cap allows headroom for larger multi-part tasks and a few error retries.+ stopWhen: [stepCountIs(7), hasToolCall('reportBlocked')],+ system: CHILD_SYSTEM,+ prompt: buildPrompt(task, deps.context, deps.snippets),+ tools: {+ runCode: createRunCodeTool({+ session: session,+ doc: deps.doc,+ awarenessSource: deps.awarenessSource,+ snippets: deps.snippets,+ params: deps.params,+ typingAnimations: deps.typingAnimations,+ sleep: deps.sleep,+ runner: deps.runner,+ onOps: deps.onOps,+ }),+ readDocument: createReadDocumentTool({ session }),+ reportBlocked: createImBlockedTool(+ 'Call this instead of guessing when you cannot do the edit -- but only after `readDocument` failed to surface what you need.',+ false+ ),+ },+ abortSignal: deps.signal,+ });+}++function buildPrompt(+ task: string,+ context: string,+ snippets?: Record<string, string>+): string {+ const snippetBlock =+ snippets && Object.keys(snippets).length > 0+ ? [+ '\n\nSnippets (access as `snippets.KEY` in your code -- do NOT re-embed as string literals):',+ '```js',+ `const snippets = \n${JSON.stringify(snippets, null, 2)}`,+ '```',+ ].join('\n')+ : '';+ const contextBlock = `\n\nRelevant region of the document:\n<document>\n${context}\n</document>`;+ return `Carry out this edit task in full:\n${task}${snippetBlock}${contextBlock}`;+}diff --git a/js/ai-editing-worker/src/ai-editing/agents/index.ts b/js/ai-editing-worker/src/ai-editing/agents/index.tsnew file mode 100644index 0000000000..766898cef5--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/agents/index.ts@@ -0,0 +1,4 @@+export { CHILD_SYSTEM, coder } from './coder';+export { interpreter } from './interpreter';+export { supervisor } from './supervisor';+export type { RunAgentOptions, RunTaskDeps } from './types';diff --git a/js/ai-editing-worker/src/ai-editing/agents/interpreter.ts b/js/ai-editing-worker/src/ai-editing/agents/interpreter.tsnew file mode 100644index 0000000000..0b43d8defe--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/agents/interpreter.ts@@ -0,0 +1,20 @@+import { generateText, type LanguageModel, stepCountIs, type Tool } from 'ai';++export async function interpreter(+ documentContext: string,+ request: string,+ model: LanguageModel,+ system: string,+ tools?: Record<string, Tool>+) {+ const prompt = `User request: ${request}\n\n${documentContext}`;++ const result = await generateText({+ model,+ system,+ prompt,+ ...(tools ? { tools, stopWhen: stepCountIs(12) } : {}),+ });++ return { text: result.text, totalUsage: result.totalUsage };+}diff --git a/js/ai-editing-worker/src/ai-editing/agents/supervisor.ts b/js/ai-editing-worker/src/ai-editing/agents/supervisor.tsnew file mode 100644index 0000000000..43743036c2--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/agents/supervisor.ts@@ -0,0 +1,96 @@+import { generateText, hasToolCall, stepCountIs } from 'ai';+import type { ResolvedModels } from '../../run-edit';+import type { LexicalSession } from '../ai-toolkit';+import API_COMPACT from '../prompts/API_COMPACT.md';+import INTERPRET from '../prompts/INTERPRET.md';+import SHARED from '../prompts/SHARED.md';+import SUPERVISOR from '../prompts/SUPERVISOR.md';+import { TokenTracker } from '../token-tracker';+import { createDispatchTool, createImBlockedTool } from '../tools';+import { numberLines, serializeWithXml } from '../utils';+import { coder } from './coder';+import { interpreter } from './interpreter';+import type { RunAgentOptions } from './types';++export type { RunAgentOptions } from './types';++const MASTER_SYSTEM = `${SHARED}\n${SUPERVISOR}\n${API_COMPACT}`;+const INTERPRET_SYSTEM = `${SHARED}\n${INTERPRET}`;++export async function supervisor(+ session: LexicalSession,+ request: string,+ models: ResolvedModels,+ opts: RunAgentOptions+) {+ const serialize = (sess: LexicalSession) =>+ numberLines(serializeWithXml(sess));+ const tracker = new TokenTracker();++ const initialText = serialize(session);+ const docContext = `<document>\n${initialText}\n</document>`;++ let intent = '';+ if (opts.interpret) {+ const interpretation = await interpreter(+ docContext,+ request,+ models.interpret,+ INTERPRET_SYSTEM+ );+ tracker.add(+ models.interpret as { modelId: string },+ interpretation.totalUsage+ );+ intent = interpretation.text;+ }++ const tools = {+ reportBlocked: createImBlockedTool(+ 'Call this when you cannot proceed without more information or when the task is impossible.',+ true+ ),+ dispatch: createDispatchTool({+ session,+ childModel: models.coding,+ tracker,+ params: opts.params,+ typingAnimations: opts.typingAnimations,+ sleep: opts.sleep,+ signal: opts.signal,+ makeWriter: opts.borrowWriter,+ runTask: coder,+ serialize,+ runner: opts.runner,+ onOps: opts.onOps,+ onCoderResult: opts.onCoderResult,+ }),+ };++ const intentBlock = intent ? `<intent>\n${intent}\n</intent>\n\n` : '';+ const prompt = `Request: ${request}\n\n${intentBlock}${docContext}`;++ const result = await generateText({+ model: models.supervisor,+ stopWhen: [stepCountIs(6), hasToolCall('reportBlocked')],+ system: MASTER_SYSTEM,+ prompt,+ tools,+ abortSignal: opts.signal,+ });+ tracker.add(models.supervisor as { modelId: string }, result.totalUsage);++ const blocked = result.steps+ .flatMap((s) => s.toolCalls)+ .find((c) => c.toolName === 'reportBlocked');+ const clarification = (blocked?.input as { message: string } | undefined)+ ?.message;++ return {+ text: result.text || 'Applied edits.',+ totalUsage: tracker,+ steps: result.steps,+ intent,+ clarification,+ };+}diff --git a/js/ai-editing-worker/src/ai-editing/agents/types.ts b/js/ai-editing-worker/src/ai-editing/agents/types.tsnew file mode 100644index 0000000000..c1b5cfa37c--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/agents/types.ts@@ -0,0 +1,31 @@+import type { AwarenessSource } from '../awareness';+import type { Doc } from '../doc';+import type { DocumentOp } from '../editor';+import type { DocumentOpQueueParams } from '../queue';+import type { RunCodeToolOptions, Writer } from '../tools';++export type RunTaskDeps = {+ doc: Doc;+ awarenessSource: AwarenessSource;+ runner: RunCodeToolOptions['runner'];+ context: string;+ snippets?: Record<string, string>;+ params?: DocumentOpQueueParams;+ typingAnimations?: boolean;+ sleep?: (ms: number) => Promise<void>;+ signal?: AbortSignal;+ onOps?: RunCodeToolOptions['onOps'];+};++export type RunAgentOptions = {+ borrowWriter: () => Promise<Writer>;+ docFormat?: 'markdown' | 'xml';+ interpret?: boolean;+ runner: RunCodeToolOptions['runner'];+ params?: DocumentOpQueueParams;+ typingAnimations?: boolean;+ sleep?: (ms: number) => Promise<void>;+ signal?: AbortSignal;+ onOps?: (ops: DocumentOp[]) => void;+ onCoderResult?: (codes: string[][]) => void;+};diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/_test-helpers.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/_test-helpers.tsnew file mode 100644index 0000000000..ed811ec4ca--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/_test-helpers.ts@@ -0,0 +1,41 @@+import { $getRoot } from 'lexical';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import {+ createEditingSession,+ type LexicalSession,+ loadMarkdown,+} from './session';++/** Run a `$`-helper inside a discrete update so it commits before we assert. */+export function edit<T>(session: LexicalSession, fn: () => T): T {+ let result!: T;+ session.editor.update(+ () => {+ result = fn();+ },+ { discrete: true }+ );+ return result;+}++/** Read inside the editor state. */+export function read<T>(session: LexicalSession, fn: () => T): T {+ return session.editor.getEditorState().read(fn);+}++/** Durable ids of the top-level blocks, in document order. */+export function topLevelIds(session: LexicalSession): string[] {+ return read(session, () =>+ $getRoot()+ .getChildren()+ .map((c) => $getId(c) ?? '?')+ );+}++// this is just a test helper, and the markdown it creates isn't pretty but+// makes assertions easier+export function setup(md: string): { session: LexicalSession; ids: string[] } {+ const session = createEditingSession();+ loadMarkdown(session, md);+ return { session, ids: topLevelIds(session) };+}diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.test.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.test.tsnew file mode 100644index 0000000000..b756d33ed5--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.test.ts@@ -0,0 +1,123 @@+import { $createHeadingNode } from '@lexical/rich-text';+import { $createParagraphNode, $createTextNode } from 'lexical';+import { describe, expect, it } from 'vitest';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import { serializeWithXml } from '../utils';+import { edit, read, setup, topLevelIds } from './_test-helpers';+import {+ $appendBlock,+ $blockNode,+ $mergeBlocks,+ $moveBlock,+ $prependBlock,+ $setBlockType,+ $setText,+} from './blocks';+import { $blockById, $byId } from './locate';++describe('block ops', () => {+ it('$setBlockType changes type, mints a FRESH id, and forwards the old id to it', () => {+ const { session, ids } = setup('Notes');+ const id = ids[0]!;+ edit(session, () =>+ $setBlockType(session, $blockById(session, id), () =>+ $blockNode({ type: 'heading', level: 2 })+ )+ );+ const xml = serializeWithXml(session);+ expect(xml).toContain('<h2');+ expect(xml).toContain('Notes');+ // The replacement carries a fresh durable id; the old id is gone from the doc+ // (so a CRDT sync reads delete+insert, not an in-place reshape).+ const [newId] = topLevelIds(session);+ expect(newId).not.toBe(id);+ expect(xml).not.toContain(`id="${id}"`);+ // ...but the old id still resolves to the new node (forwarded in the id map).+ expect(read(session, () => $getId($blockById(session, id)))).toBe(newId);+ });++ it('$setText rewrites inline content but KEEPS type and id', () => {+ const { session, ids } = setup('ok so the launch is kinda behind');+ const id = ids[0];+ edit(session, () =>+ $setText($blockById(session, id), 'The launch is behind schedule.')+ );+ const xml = serializeWithXml(session);+ expect(xml).toContain('The launch is behind schedule.');+ expect(xml).toContain(`id="${id}"`);+ });++ it('$appendBlock adds blocks at the end with fresh ids', () => {+ const { session, ids } = setup('first');+ edit(session, () => {+ const h2 = $createHeadingNode('h2');+ h2.append($createTextNode('Notes'));+ const p = $createParagraphNode();+ p.append($createTextNode('Follow up Friday.'));+ $appendBlock(h2, p);+ });+ const after = topLevelIds(session);+ expect(after[0]).toBe(ids[0]);+ expect(after).toHaveLength(3);+ expect(new Set(after).size).toBe(3);+ const xml = serializeWithXml(session);+ expect(xml).toContain('<h2');+ expect(xml).toContain('Notes');+ expect(xml).toContain('Follow up Friday.');+ });++ it('$prependBlock adds blocks at the top with fresh ids', () => {+ const { session, ids } = setup('attendees: wolf, sara');+ edit(session, () => {+ const h1 = $createHeadingNode('h1');+ h1.append($createTextNode('Title'));+ $prependBlock(h1);+ });+ const after = topLevelIds(session);+ expect(after).toHaveLength(2);+ expect(after[1]).toBe(ids[0]);+ expect(after[0]).not.toBe(ids[0]);+ const xml = serializeWithXml(session);+ expect(xml).toContain('<h1');+ expect(xml).toContain('Title');+ });++ it('$moveBlock (2-arg) relocates a block after another', () => {+ const { session, ids } = setup(+ '## Decisions\n\nfiller\n\nneed to finalize pricing'+ );+ const [decisions, , pricing] = ids;+ edit(session, () =>+ $moveBlock($byId(session, pricing), { placement: 'after', id: decisions })+ );+ expect(topLevelIds(session)).toEqual([decisions, pricing, ids[1]]);+ const xml = serializeWithXml(session);+ expect(xml).toContain('Decisions');+ expect(xml).toContain('need to finalize pricing');+ });++ it('$moveBlock supports beforeId', () => {+ const { session, ids } = setup('a\n\nb\n\nc');+ const [idA, idB, idC] = ids;+ edit(session, () =>+ $moveBlock($byId(session, idC), { placement: 'before', id: idA })+ );+ expect(topLevelIds(session)).toEqual([idC, idA, idB]);+ });++ it('$mergeBlocks merges into the first block, KEEPING its id', () => {+ const { session, ids } = setup("We were behind.\n\nQA hadn't started.");+ const [idA, idB] = ids;+ const resultId = edit(session, () => {+ const merged = $mergeBlocks([idA, idB].map((id) => $byId(session, id)));+ return $getId(merged);+ });+ expect(resultId).toBe(idA); // first block's id preserved+ expect(serializeWithXml(session)).toContain(`id="${idA}"`);+ // both bodies land in the one surviving block, joined by the ' ' separator+ const mergedText = read(session, () =>+ $blockById(session, idA!).getTextContent()+ );+ expect(mergedText).toBe("We were behind. QA hadn't started.");+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.tsnew file mode 100644index 0000000000..f09b891fd1--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/blocks.ts@@ -0,0 +1,162 @@+import { $createCodeNode } from '@lexical/code';+import {+ $createHeadingNode,+ $createQuoteNode,+ type HeadingTagType,+} from '@lexical/rich-text';+import {+ $createParagraphNode,+ $createTextNode,+ $getNodeByKey,+ $getRoot,+ $isElementNode,+ $isTextNode,+ type ElementNode,+ type LexicalNode,+} from 'lexical';+import { match } from 'ts-pattern';+import { $isCustomCodeNode } from '../../../../lexical-core/nodes/CustomCodeNode';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import { $retypeContainer, type LexicalSession } from './session';++export type BlockData =+ | { type: 'paragraph' }+ | { type: 'heading'; level: number }+ | { type: 'quote' }+ | { type: 'code'; language?: string };++/** Build an (empty) block node to pass to `$setBlockType` / inserts. */+export function $blockNode(data: BlockData): ElementNode {+ return match(data)+ .returnType<ElementNode>()+ .with({ type: 'heading' }, (d) =>+ $createHeadingNode(`h${d.level}` as HeadingTagType)+ )+ .with({ type: 'quote' }, () => $createQuoteNode())+ .with({ type: 'code' }, (d) => $createCodeNode(d.language))+ .with({ type: 'paragraph' }, () => $createParagraphNode())+ .exhaustive();+}++/** Change a block's type, transplanting its content onto a fresh-id node. */+export function $setBlockType(+ session: LexicalSession,+ block: ElementNode,+ make: () => ElementNode+): ElementNode {+ return $retypeContainer(session, block, make());+}++/** Rewrite a block's inline content to plain text, keeping its type and id. Always strips any inline formatting (bold, italic, underline, etc.) on the kept node. */+export function $setText(block: ElementNode, text: string): void {+ // A code block's children are code-highlight nodes (re-tokenized from the+ // block's text by Prism), so we use `setCode`, which splices the whole+ // content in one shot, keeping the language.+ if ($isCustomCodeNode(block)) {+ block.setCode(block.getLanguage(), text);+ return;+ }+ const children = block.getChildren();+ const existing = children.find($isTextNode);+ if (existing) {+ existing.setTextContent(text);+ existing.setFormat(0);+ for (const child of children) {+ if (child !== existing) child.remove();+ }+ } else {+ block.clear();+ block.append($createTextNode(text));+ }+}++/** Append pre-built block node(s) at the end of the document. */+export function $appendBlock(...nodes: ElementNode[]): ElementNode[] {+ const root = $getRoot();+ for (const node of nodes) {+ root.append(node);+ }+ return nodes;+}++/** Prepend pre-built block node(s) at the top of the document. */+export function $prependBlock(...nodes: ElementNode[]): ElementNode[] {+ const root = $getRoot();+ const first = root.getFirstChild();+ if (!first) {+ for (const node of nodes) {+ root.append(node);+ }+ return nodes;+ }+ for (const node of nodes) {+ first.insertBefore(node);+ }+ return nodes;+}++/** Relocate a block to after/before another (by id). */+export function $moveBlock(+ block: LexicalNode,+ to: { placement: 'after' | 'before'; id: string },+ session?: LexicalSession+): void {+ // `to` id is resolved against the active editor state via key mappings if a+ // session is provided; otherwise the target is resolved by walking the root.+ const target = findTopLevelById(to.id, session);+ if (!target) {+ throw new Error(`No block with id "${to.id}"`);+ }+ block.remove();+ if (to.placement === 'after') {+ target.insertAfter(block);+ } else {+ target.insertBefore(block);+ }+}++function findTopLevelById(+ id: string,+ session?: LexicalSession+): ElementNode | null {+ if (session) {+ const key = session.ids.idToNodeKeyMap.get(id);+ if (key) {+ const node = $getNodeByKey(key);+ if (node && $isElementNode(node)) {+ return node;+ }+ }+ }+ for (const child of $getRoot().getChildren()) {+ if ($getId(child) === id && $isElementNode(child)) {+ return child;+ }+ }+ return null;+}++/**+ * Merge blocks into the first one (keeping its id). Inline content of later+ * blocks is appended, separated by `separator` (default `' '`). The later+ * blocks are removed.+ */+export function $mergeBlocks(+ blocks: LexicalNode[],+ separator = ' '+): ElementNode {+ const [first, ...rest] = blocks;+ if (!first || !$isElementNode(first)) {+ throw new Error('$mergeBlocks needs at least one block');+ }+ for (const node of rest) {+ if ($isElementNode(node)) {+ if (separator) {+ first.append($createTextNode(separator));+ }+ first.append(...node.getChildren());+ }+ node.remove();+ }+ return first;+}diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/index.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/index.tsnew file mode 100644index 0000000000..e3f2c444b9--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/index.ts@@ -0,0 +1,7 @@+export * from './blocks';+export * from './inline';+export * from './lists';+export * from './locate';+export * from './modify';+export * from './session';+export * from './tables';diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/inline.test.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/inline.test.tsnew file mode 100644index 0000000000..7c7f4fe704--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/inline.test.ts@@ -0,0 +1,275 @@+import { $createLinkNode } from '@lexical/link';+import { $createMarkNode } from '@lexical/mark';+import { $getRoot, $isTextNode, type ElementNode } from 'lexical';+import { describe, expect, it } from 'vitest';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import { serializeWithXml } from '../utils';+import { edit, read, setup } from './_test-helpers';+import {+ $appendText,+ $clearFormat,+ $formatTextInBlock,+ $prependText,+ $replaceString,+ $wrapInBlock,+} from './inline';+import { $blockById } from './locate';++describe('inline ops: scope + counts', () => {+ it('$formatTextInBlock — bold a substring (count), no-match returns 0', () => {+ const { session, ids } = setup('the Bluejay launch');+ const id = ids[0];+ const count = edit(session, () =>+ $formatTextInBlock($blockById(session, id), 'Bluejay', 'bold', {+ kind: 'all',+ })+ );+ expect(count).toBe(1);+ const bluejayNode = read(session, () => {+ const block = $getRoot().getFirstChild() as ElementNode;+ for (const c of block.getChildren()) {+ if ($isTextNode(c) && c.getTextContent() === 'Bluejay') return c;+ }+ return null;+ });+ expect(bluejayNode).not.toBeNull();+ expect(read(session, () => bluejayNode!.hasFormat('bold'))).toBe(true);+ expect(serializeWithXml(session)).toContain(`id="${id}"`);+ });++ it('$formatTextInBlock — strike maps to strikethrough', () => {+ const { session, ids } = setup('hello world');+ const id = ids[0];+ edit(session, () =>+ $formatTextInBlock($blockById(session, id), 'world', 'strike')+ );+ const xml = serializeWithXml(session);+ expect(xml).toContain('world');+ expect(xml).toContain(`id="${id}"`);+ // the text node with 'world' should have strikethrough format+ const worldNode = read(session, () => {+ const block = $getRoot().getFirstChild() as ElementNode;+ for (const c of block.getChildren()) {+ if ($isTextNode(c) && c.getTextContent() === 'world') return c;+ }+ return null;+ });+ expect(worldNode).not.toBeNull();+ expect(read(session, () => worldNode!.hasFormat('strikethrough'))).toBe(+ true+ );+ });++ it('$clearFormat — removes one format, leaving others', () => {+ // "Bluejay" is bold+italic; clearing bold should leave italic+ const { session, ids } = setup('the ***Bluejay*** launch');+ const id = ids[0];+ const count = edit(session, () =>+ $clearFormat($blockById(session, id), 'Bluejay', 'bold', { kind: 'all' })+ );+ expect(count).toBe(1);+ const bluejayNode = read(session, () => {+ const block = $getRoot().getFirstChild() as ElementNode;+ for (const c of block.getChildren()) {+ if ($isTextNode(c) && c.getTextContent() === 'Bluejay') return c;+ }+ return null;+ });+ expect(bluejayNode).not.toBeNull();+ expect(read(session, () => bluejayNode!.hasFormat('bold'))).toBe(false);+ expect(read(session, () => bluejayNode!.hasFormat('italic'))).toBe(true);+ });++ it('$clearFormat — without format clears all formatting', () => {+ const { session, ids } = setup('the ***Bluejay*** launch');+ const id = ids[0];+ edit(session, () => $clearFormat($blockById(session, id), 'Bluejay'));+ const xml = serializeWithXml(session);+ expect(xml).toContain('Bluejay');+ expect(xml).toContain(`id="${id}"`);+ const bluejayNode = read(session, () => {+ const block = $getRoot().getFirstChild() as ElementNode;+ for (const c of block.getChildren()) {+ if ($isTextNode(c) && c.getTextContent() === 'Bluejay') return c;+ }+ return null;+ });+ expect(bluejayNode).not.toBeNull();+ expect(read(session, () => bluejayNode!.hasFormat('bold'))).toBe(false);+ expect(read(session, () => bluejayNode!.hasFormat('italic'))).toBe(false);+ });++ it('$replaceString — literal replace, counts, default vs all', () => {+ const { session, ids } = setup('Q3 roadmap and Q3 budget');+ const id = ids[0];+ const count = edit(session, () =>+ $replaceString($blockById(session, id), 'Q3', 'Q4', { kind: 'all' })+ );+ expect(count).toBe(2);+ const xml = serializeWithXml(session);+ expect(xml).toContain('Q4');+ expect(xml).not.toContain('Q3');++ const miss = edit(session, () =>+ $replaceString($blockById(session, id), 'Q9', 'Q1')+ );+ expect(miss).toBe(0);+ });++ it('$replaceString mutates in place — text node ids survive (no churn)', () => {+ const { session, ids } = setup('Full control over rendering');+ const id = ids[0];+ const before = read(session, () =>+ $blockById(session, id)+ .getAllTextNodes()+ .map((n) => $getId(n))+ );+ edit(session, () => $replaceString($blockById(session, id), 'Full ', ''));+ const after = read(session, () =>+ $blockById(session, id)+ .getAllTextNodes()+ .map((n) => $getId(n))+ );+ expect(after).toEqual(before); // same leaf ids — the diff sees a clean setText+ expect(read(session, () => $blockById(session, id).getTextContent())).toBe(+ 'control over rendering'+ );+ });++ it('$appendText / $prependText extend an existing plain text node in place', () => {+ const { session, ids } = setup('Meeting Notes');+ const id = ids[0];+ const before = read(session, () =>+ $blockById(session, id)+ .getAllTextNodes()+ .map((n) => $getId(n))+ );+ edit(session, () => $appendText($blockById(session, id), ' (draft)'));+ edit(session, () => $prependText($blockById(session, id), 'DRAFT: '));+ const after = read(session, () =>+ $blockById(session, id)+ .getAllTextNodes()+ .map((n) => $getId(n))+ );+ expect(after).toEqual(before); // no new text nodes minted+ expect(read(session, () => $blockById(session, id).getTextContent())).toBe(+ 'DRAFT: Meeting Notes (draft)'+ );+ });++ it('formatting a substring preserves surrounding formats, scopes the span', () => {+ // whole "two three four" span is bold; italicize only "three"+ const { session, ids } = setup('one **two three four** five');+ const id = ids[0];+ edit(session, () =>+ $formatTextInBlock($blockById(session, id), 'three', 'italic')+ );+ const segs = read(session, () => {+ const block = $getRoot().getFirstChild() as ElementNode;+ const out: Array<{ text: string; bold: boolean; italic: boolean }> = [];+ for (const c of block.getChildren()) {+ if ($isTextNode(c)) {+ out.push({+ text: c.getTextContent(),+ bold: c.hasFormat('bold'),+ italic: c.hasFormat('italic'),+ });+ }+ }+ return out;+ });+ const three = segs.find((x) => x.text === 'three');+ expect(three).toBeDefined();+ expect(three!.bold).toBe(true); // surrounding bold preserved+ expect(three!.italic).toBe(true); // target span got italic+ // everything else keeps bold==their original and italic==false+ for (const seg of segs) {+ if (seg.text !== 'three') expect(seg.italic).toBe(false);+ }+ // the bold-but-not-three pieces are still bold+ expect(segs.find((x) => x.text.includes('two'))!.bold).toBe(true);+ expect(segs.find((x) => x.text.includes('four'))!.bold).toBe(true);+ });++ it('edge matches: needle at very start and very end both work', () => {+ const { session, ids } = setup('frog middle frog');+ const id = ids[0];+ const count = edit(session, () =>+ $formatTextInBlock($blockById(session, id), 'frog', 'bold', {+ kind: 'all',+ })+ );+ expect(count).toBe(2);+ const xml = serializeWithXml(session);+ expect(xml).toContain('frog');+ expect(xml).toContain(`id="${id}"`);+ // both frog occurrences are bold+ const boldFrogs = read(session, () => {+ const block = $getRoot().getFirstChild() as ElementNode;+ return block+ .getChildren()+ .filter(+ (c) =>+ $isTextNode(c) &&+ c.getTextContent() === 'frog' &&+ c.hasFormat('bold')+ );+ });+ expect(boldFrogs).toHaveLength(2);+ });++ it('scoped first-match does not bleed when needle repeats in the same text node', () => {+ // "XX" is one TextNode; formatting "X" with default scope (first only) must+ // not touch the second X even though both pieces equal the needle after splitting.+ const { session, ids } = setup('XX');+ const id = ids[0];+ const count = edit(session, () =>+ $formatTextInBlock($blockById(session, id), 'X', 'bold')+ );+ expect(count).toBe(1);+ const segs = read(session, () => {+ const block = $getRoot().getFirstChild() as ElementNode;+ const result: Array<{ text: string; bold: boolean }> = [];+ for (const c of block.getChildren()) {+ if ($isTextNode(c))+ result.push({ text: c.getTextContent(), bold: c.hasFormat('bold') });+ }+ return result;+ });+ // exactly one bold X and one non-bold X+ expect(segs.filter((x) => x.bold).map((x) => x.text)).toEqual(['X']);+ expect(segs.filter((x) => !x.bold).map((x) => x.text)).toEqual(['X']);+ });+});++describe('links / highlight', () => {+ it('$wrapInBlock wraps a substring in a LinkNode; count returned', () => {+ const { session, ids } = setup('see the docs');+ const id = ids[0];+ const count = edit(session, () =>+ $wrapInBlock($blockById(session, id), 'the docs', () =>+ $createLinkNode('https://docs.example.com')+ )+ );+ expect(count).toBe(1);+ const xml = serializeWithXml(session);+ expect(xml).toContain('href="https://docs.example.com"');+ // the link wraps 'the docs', leaving 'see ' as a sibling text node+ expect(xml).toMatch(/see /);+ expect(xml).toMatch(/<a[^>]*>[\s\S]*the docs[\s\S]*<\/a>/);+ });++ it('$wrapInBlock wraps matches in MarkNodes; all:true returns count', () => {+ const { session, ids } = setup('important here important');+ const id = ids[0];+ const count = edit(session, () =>+ $wrapInBlock($blockById(session, id), 'important', $createMarkNode, {+ kind: 'all',+ })+ );+ expect(count).toBe(2);+ const xml = serializeWithXml(session);+ expect(xml.match(/<mark/g)).toHaveLength(2);+ expect(xml).toMatch(/<mark[^>]*>[\s\S]*important[\s\S]*<\/mark>/);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/inline.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/inline.tsnew file mode 100644index 0000000000..f476538d10--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/inline.ts@@ -0,0 +1,259 @@+import {+ $createTextNode,+ $findMatchingParent,+ $isElementNode,+ $isTextNode,+ type ElementNode,+ type LexicalNode,+ type TextFormatType,+ type TextNode,+} from 'lexical';++export type Scope = { kind: 'nth'; n: number } | { kind: 'all' };++const FORMAT_MAP: Record<string, TextFormatType> = {+ bold: 'bold',+ italic: 'italic',+ underline: 'underline',+ strike: 'strikethrough',+ code: 'code',+};++export type InlineFormat = 'bold' | 'italic' | 'underline' | 'strike' | 'code';++/** Format a substring (split the text node and set the format bit). */+export function $formatTextInBlock(+ block: ElementNode,+ needle: string,+ format: InlineFormat,+ scope?: Scope+): number {+ const fmt = FORMAT_MAP[format];+ return mutateMatches(block, needle, scope, (matchNode) => {+ if (!matchNode.hasFormat(fmt)) {+ matchNode.toggleFormat(fmt);+ }+ });+}++/** Remove formatting from a substring (omit `format` to clear all). */+export function $clearFormat(+ block: ElementNode,+ needle: string,+ format?: InlineFormat,+ scope?: Scope+): number {+ return mutateMatches(block, needle, scope, (matchNode) => {+ if (format) {+ const fmt = FORMAT_MAP[format];+ if (matchNode.hasFormat(fmt)) {+ matchNode.toggleFormat(fmt);+ }+ } else {+ matchNode.setFormat(0);+ }+ });+}++/** Strip all inline formatting (bold, italic, underline, etc.) from every text node in a block. */+export function $stripFormat(block: ElementNode): void {+ for (const node of block.getAllTextNodes()) {+ node.setFormat(0);+ }+}++/**+ * Literal text replace. Mutates matched text nodes IN PLACE (via+ * `setTextContent`) so their durable ids survive — the diff then sees a clean+ * `setText{from,to}` instead of node churn, which is what lets the replay+ * highlight/animate the exact changed span. Like the formatting helpers it+ * works per text node, so a needle straddling two nodes isn't matched.+ */+// TODO(wolf): unreliable on code blocks -- their children are per-token+// code-highlight nodes, so a `find` can span a Prism token boundary+export function $replaceString(+ block: ElementNode,+ find: string,+ replace: string,+ scope?: Scope+): number {+ if (find.length === 0) return 0;+ const all = scope?.kind === 'all';+ const nth = scope?.kind === 'nth' ? scope.n : undefined;+ let occ = 0; // global, 1-based occurrence counter across the block's text nodes+ let count = 0;+ for (const tn of block.getAllTextNodes()) {+ const content = tn.getTextContent();+ if (!content.includes(find)) continue;+ let next = '';+ let i = 0;+ let changed = false;+ while (i < content.length) {+ if (content.startsWith(find, i)) {+ occ++;+ const take = all || (nth == null ? occ === 1 : occ === nth);+ if (take) {+ next += replace;+ changed = true;+ count++;+ } else {+ next += find;+ }+ i += find.length;+ } else {+ next += content[i];+ i++;+ }+ }+ if (!changed) continue;+ if (next.length === 0) tn.remove();+ else tn.setTextContent(next);+ }+ return count;+}++/** Append plain text to the end of a block. Extends the trailing plain text node+ * in place (preserving its id) when possible, else adds a new node. */+export function $appendText(block: ElementNode, text: string): void {+ const last = block.getLastChild();+ if ($isTextNode(last) && last.getFormat() === 0) {+ last.setTextContent(last.getTextContent() + text);+ } else {+ block.append($createTextNode(text));+ }+}++/** Prepend plain text to the start of a block. Extends the leading plain text+ * node in place (preserving its id) when possible, else adds a new node. */+export function $prependText(block: ElementNode, text: string): void {+ const first = block.getFirstChild();+ if ($isTextNode(first) && first.getFormat() === 0) {+ first.setTextContent(text + first.getTextContent());+ } else if (first) {+ first.insertBefore($createTextNode(text));+ } else {+ block.append($createTextNode(text));+ }+}++/**+ * Core inline-match engine. Finds occurrences of `needle` across the block's+ * text nodes, splits so each match is its own text node, then calls `apply` on+ * each matched node. Collects matches first, then mutates (mutating during the+ * walk would re-process freshly-created nodes). Returns the count changed.+ */+function mutateMatches(+ block: ElementNode,+ needle: string,+ scope: Scope | undefined,+ apply: (matchNode: TextNode) => void+): number {+ if (needle.length === 0) {+ return 0;+ }+ const all = scope?.kind === 'all';+ // `nth` is 1-based per occurrence; default targets the first occurrence.+ const nth = scope?.kind === 'nth' ? scope.n : undefined;++ // 1) Collect every (textNode, offset) occurrence in document order.+ const occurrences: Array<{ node: TextNode; offset: number }> = [];+ for (const tn of block.getAllTextNodes()) {+ const content = tn.getTextContent();+ let from = 0;+ let idx = content.indexOf(needle, from);+ while (idx !== -1) {+ occurrences.push({ node: tn, offset: idx });+ from = idx + needle.length;+ idx = content.indexOf(needle, from);+ }+ }+ if (occurrences.length === 0) {+ return 0;+ }++ // 2) Decide which occurrences to act on.+ let targets: Array<{ node: TextNode; offset: number }>;+ if (all) {+ targets = occurrences;+ } else if (nth != null) {+ const pick = occurrences[nth - 1];+ targets = pick ? [pick] : [];+ } else {+ targets = [occurrences[0]];+ }+ if (targets.length === 0) {+ return 0;+ }++ // 3) Group targets by their text node so we can split correctly. We process+ // one text node at a time, splitting out each match into its own node.+ const byNode = new Map<TextNode, number[]>();+ for (const t of targets) {+ const list = byNode.get(t.node) ?? [];+ list.push(t.offset);+ byNode.set(t.node, list);+ }++ let count = 0;+ for (const [node, offsets] of byNode) {+ // Split the node at all match boundaries (offset and offset+len). Then the+ // substrings equal to `needle` are isolated nodes we can act on.+ const boundaries = new Set<number>();+ for (const off of offsets) {+ boundaries.add(off);+ boundaries.add(off + needle.length);+ }+ const sorted = [...boundaries].sort((a, b) => a - b);+ const pieces = node.splitText(...sorted);+ // Walk pieces by cumulative character position so we can identify target+ // pieces by offset — content equality alone is ambiguous when needle repeats.+ const targetOffsets = new Set(offsets);+ let pos = 0;+ for (const piece of pieces) {+ const len = piece.getTextContent().length;+ if (targetOffsets.has(pos) && piece.getTextContent() === needle) {+ apply(piece);+ count++;+ }+ pos += len;+ }+ }+ return count;+}++/** Wrap a matched substring in a new element node (link, mark, …). Returns the count changed. */+export function $wrapInBlock(+ block: ElementNode,+ needle: string,+ createWrapper: () => ElementNode,+ scope?: Scope+): number {+ return mutateMatches(block, needle, scope, (matchNode) => {+ const wrapper = createWrapper();+ matchNode.replace(wrapper);+ wrapper.append(matchNode);+ });+}++function $unwrapWrapper(+ matchNode: TextNode,+ pred: (n: LexicalNode) => boolean+): void {+ const p = matchNode.getParent();+ const parent = p ? $findMatchingParent(p, pred) : null;+ if (!parent || !$isElementNode(parent)) return;+ for (const child of parent.getChildren()) parent.insertBefore(child);+ parent.remove();+}++/** Remove the nearest wrapper matching `pred` from a matched substring. Returns the count changed. */+export function $unwrapFromBlock(+ block: ElementNode,+ needle: string,+ pred: (n: LexicalNode) => boolean,+ scope?: Scope+): number {+ return mutateMatches(block, needle, scope, (matchNode) =>+ $unwrapWrapper(matchNode, pred)+ );+}diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/lists.test.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/lists.test.tsnew file mode 100644index 0000000000..0ffd7f2d76--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/lists.test.ts@@ -0,0 +1,95 @@+import {+ $isListItemNode,+ $isListNode,+ type ListItemNode,+ type ListNode,+} from '@lexical/list';+import { $getRoot, $isElementNode, type LexicalNode } from 'lexical';+import { describe, expect, it } from 'vitest';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import { serializeWithXml } from '../utils';+import { edit, read, setup, topLevelIds } from './_test-helpers';+import { $setListType, $toggleList } from './lists';+import { $byId } from './locate';+import {+ createEditingSession,+ type LexicalSession,+ loadMarkdown,+} from './session';++describe('lists', () => {+ it('$toggleList creates one list block (fresh id); items get fresh ids', () => {+ const { session, ids } = setup('todo a\n\ntodo b');+ const list = edit(session, () =>+ $toggleList(+ ids.map((id) => $byId(session, id)),+ 'check'+ )+ );+ // single top-level block now+ const top = topLevelIds(session);+ expect(top).toHaveLength(1);+ const listId = read(session, () => $getId(list));+ expect(listId).toBe(top[0]);+ expect(ids).not.toContain(listId); // fresh list id++ const items = read(session, () => {+ const ln = $getRoot().getFirstChild() as ListNode;+ expect($isListNode(ln)).toBe(true);+ return (ln.getChildren() as ListItemNode[]).map((it) => ({+ id: $getId(it),+ text: it.getTextContent(),+ checked: it.getChecked(),+ }));+ });+ expect(items.map((i: any) => i.text)).toEqual(['todo a', 'todo b']);+ // check list => items default unchecked+ expect(items.every((i: any) => i.checked === false)).toBe(true);+ // item ids are fresh, present, and distinct+ const itemIds = items.map((i: any) => i.id);+ expect(new Set(itemIds).size).toBe(2);+ for (const iid of itemIds) {+ expect(iid).toBeTruthy();+ expect(ids).not.toContain(iid);+ }+ // serialized as a checklist, each item carrying its own id in XML+ const xml = serializeWithXml(session);+ expect(xml).toContain('<ul');+ expect(xml).toContain(`id="${itemIds[0]}"`);+ expect(xml).toContain(`id="${itemIds[1]}"`);+ expect(xml).toContain('todo a');+ expect(xml).toContain('todo b');+ });+});++describe('$setListType / $toggleList separation', () => {+ // find the deepest list item with the given text+ const deepItemId = (session: LexicalSession, text: string) =>+ read(session, () => {+ let target: ListItemNode | undefined;+ const walk = (n: LexicalNode) => {+ if ($isListItemNode(n) && n.getTextContent() === text) target = n;+ if ($isElementNode(n)) n.getChildren().forEach(walk);+ };+ $getRoot().getChildren().forEach(walk);+ return target ? $getId(target) : null;+ });++ it('$setListType retypes a NESTED list, preserving indent + the item id (list id is fresh)', () => {+ const session = createEditingSession();+ loadMarkdown(session, '- deeply\n - nested\n - list\n - items');+ const itemId = deepItemId(session, 'items');+ edit(session, () =>+ $setListType($byId(session, itemId!), 'number', session)+ );+ read(session, () => {+ // the item is carried over by replace(…, true): its id still resolves+ const item = $byId(session, itemId!) as ListItemNode;+ expect(item.getType()).toBe('listitem');+ expect(item.getIndent()).toBe(3); // still deeply nested, not hoisted+ expect($isListNode(item.getParent())).toBe(true);+ expect((item.getParent() as ListNode).getListType()).toBe('number');+ expect($getRoot().getChildren()).toHaveLength(1);+ });+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/lists.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/lists.tsnew file mode 100644index 0000000000..4e76e15bc5--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/lists.ts@@ -0,0 +1,71 @@+import {+ $createListItemNode,+ $createListNode,+ $isListItemNode,+ $isListNode,+ type ListNode,+ type ListType,+} from '@lexical/list';+import {+ $findMatchingParent,+ $isElementNode,+ type ElementNode,+ type LexicalNode,+} from 'lexical';+import { $retypeContainer, type LexicalSession } from './session';++export type ListKind = 'bullet' | 'number' | 'check';++/**+ * Convert a set of blocks into a single list of the given kind. Each block+ * becomes a list item carrying that block's inline content. The list replaces+ * the first block; the rest are removed. Returns the new `ListNode`.+ */+export function $toggleList(blocks: LexicalNode[], type: ListKind): ListNode {+ const [first, ...rest] = blocks;+ if (!first || !$isElementNode(first)) {+ throw new Error('$toggleList needs at least one block');+ }+ // $toggleList WRAPS plain blocks into a new list. To change the type of a list+ // that already exists, use $setListType (which preserves nesting) — wrapping a+ // list item here would orphan it to the top level.+ if ($isListItemNode(first)) {+ throw new Error(+ '$toggleList wraps non-list blocks; to change an existing list type use $setListType'+ );+ }+ const list = $createListNode(type as ListType);+ const makeItem = (block: ElementNode) => {+ const item = $createListItemNode(type === 'check' ? false : undefined);+ item.append(...block.getChildren());+ return item;+ };+ list.append(makeItem(first));+ for (const node of rest) {+ if ($isElementNode(node)) {+ list.append(makeItem(node));+ }+ }+ first.replace(list);+ for (const node of rest) {+ node.remove();+ }+ return list;+}++/**+ * Change the type of the list enclosing `node` (a list, or any item in it) —+ * bullet ↔ number ↔ check — retyping the list node itself, preserving its+ * position, nesting, indentation, and the items' ids. Returns the retyped list.+ */+export function $setListType(+ node: LexicalNode,+ type: ListKind,+ session: LexicalSession+): ListNode {+ const list = $findMatchingParent(node, $isListNode);+ if (!$isListNode(list)) {+ throw new Error('$setListType: no enclosing list');+ }+ return $retypeContainer(session, list, $createListNode(type as ListType));+}diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/locate.test.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/locate.test.tsnew file mode 100644index 0000000000..5523c7ea33--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/locate.test.ts@@ -0,0 +1,37 @@+import { $getRoot, $isElementNode } from 'lexical';+import { describe, expect, it } from 'vitest';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import { edit, read, setup } from './_test-helpers';+import { $blockById, $byId } from './locate';++describe('lock-on + errors', () => {+ it('$byId throws EditError on a missing id', () => {+ const { session } = setup('hello');+ edit(session, () => {+ expect(() => $byId(session, 'nope-missing')).toThrowError(Error);+ });+ });++ it('$blockById throws EditError on a missing id', () => {+ const { session } = setup('hello');+ edit(session, () => {+ expect(() => $blockById(session, 'nope-missing')).toThrowError(Error);+ });+ });++ it('$blockById resolves an inline (text) id up to its containing block', () => {+ const { session } = setup('hello world');+ // the text node's id (a child of the block)+ const textId = read(session, () => {+ const block = $getRoot().getFirstChild();+ const text = $isElementNode(block) ? block.getFirstChild() : null;+ return text ? $getId(text) : null;+ });+ expect(textId).toBeTruthy();+ edit(session, () => {+ const block = $blockById(session, textId!);+ expect($isElementNode(block)).toBe(true);+ expect(block.getType()).toBe('paragraph');+ });+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/locate.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/locate.tsnew file mode 100644index 0000000000..5c4eddbfb7--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/locate.ts@@ -0,0 +1,77 @@+import {+ $findMatchingParent,+ $getNodeByKey,+ $isElementNode,+ $isTextNode,+ type ElementNode,+ type LexicalNode,+ type TextNode,+} from 'lexical';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import type { LexicalSession } from './session';++export function $byId(session: LexicalSession, id: string): LexicalNode {+ const key = session.ids.idToNodeKeyMap.get(id);+ const node = key != null ? $getNodeByKey(key) : null;+ if (!node) {+ throw new Error(`No node with id "${id}"`);+ }+ return node;+}++/**+ * Lock onto a block by id. Since every node carries an id (including inline+ * text/link spans), an id that points at an inline node resolves UP to its+ * nearest block-level element (the containing paragraph, heading, list item,+ * quote, …). Throws `Error` only if nothing block-level is found.+ */+export function $blockById(session: LexicalSession, id: string): ElementNode {+ const node = $findMatchingParent(+ $byId(session, id),+ (n) => $isElementNode(n) && !n.isInline()+ );+ if (!node || !$isElementNode(node)) {+ throw new Error(`No block-level node for id "${id}"`);+ }+ return node;+}++/**+ * Resolve a text node by its XML id (the `id` attr on `<t>` elements). Use+ * this in XML mode when you want to act on a specific text span without+ * knowing its parent block id. Throws if the id resolves to a non-text node.+ */+export function $textById(session: LexicalSession, id: string): TextNode {+ const node = $byId(session, id);+ if (!$isTextNode(node)) throw new Error(`Node "${id}" is not a TextNode`);+ return node;+}++export type TextMatch = { node: string; start: number; end: number };+export type LocateScope = { kind: 'nth'; n: number } | { kind: 'all' };++/** Find occurrences of `match` within `block`'s text nodes, filtered by scope. */+export function $locate(+ block: ElementNode,+ match: string,+ scope?: LocateScope+): TextMatch[] {+ const all = scope?.kind === 'all';+ const nth = scope?.kind === 'nth' ? scope.n : undefined;+ const out: TextMatch[] = [];+ let occurrences = 0;+ for (const textNode of block.getAllTextNodes()) {+ const content = textNode.getTextContent();+ const nodeId = $getId(textNode);+ let index = content.indexOf(match);+ while (index !== -1) {+ occurrences++;+ const take =+ all || (nth == null ? occurrences === 1 : occurrences === nth);+ if (take && nodeId)+ out.push({ node: nodeId, start: index, end: index + match.length });+ index = content.indexOf(match, index + match.length);+ }+ }+ return out;+}diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/modify.test.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/modify.test.tsnew file mode 100644index 0000000000..7505420e38--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/modify.test.ts@@ -0,0 +1,121 @@+import {+ $isListItemNode,+ type ListItemNode,+ type ListNode,+} from '@lexical/list';+import { $getRoot, $isElementNode, type LexicalNode } from 'lexical';+import { describe, expect, it } from 'vitest';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import { serializeWithXml } from '../utils';+import { edit, read, setup } from './_test-helpers';+import { $byId } from './locate';+import { $modifyNode } from './modify';+import { createEditingSession, loadMarkdown } from './session';++describe('$modifyNode', () => {+ it("op 'blockType' changes type + level, minting a fresh id the old id forwards to", () => {+ const { session, ids } = setup('Notes');+ const old = ids[0]!;+ edit(session, () =>+ $modifyNode(session, old, {+ op: 'blockType',+ block: { type: 'heading', level: 2 },+ })+ );+ const xml = serializeWithXml(session);+ expect(xml).toContain('<h2');+ expect(xml).toContain('Notes');+ // fresh id replaces the old one, but the old id still resolves to the node+ expect(xml).not.toContain(`id="${old}"`);+ expect(read(session, () => $getId($byId(session, old)))).not.toBe(old);+ });++ it("op 'text' rewrites a block's content, keeping type + id", () => {+ const { session, ids } = setup('# Title');+ edit(session, () =>+ $modifyNode(session, ids[0], { op: 'text', text: 'New title' })+ );+ const xml = serializeWithXml(session);+ expect(xml).toContain('<h1');+ expect(xml).toContain(`id="${ids[0]}"`);+ expect(xml).toContain('New title');+ });++ it("op 'listType' retypes the enclosing list from any item, preserving nesting", () => {+ const session = createEditingSession();+ loadMarkdown(session, '- a\n - b');+ const bId = read(session, () => {+ let target: ListItemNode | undefined;+ const walk = (n: LexicalNode) => {+ if ($isListItemNode(n) && n.getTextContent() === 'b') target = n;+ if ($isElementNode(n)) n.getChildren().forEach(walk);+ };+ $getRoot().getChildren().forEach(walk);+ return target ? $getId(target) : null;+ });+ edit(session, () =>+ $modifyNode(session, bId!, { op: 'listType', list: 'number' })+ );+ read(session, () => {+ const item = $byId(session, bId!) as ListItemNode;+ expect(item.getIndent()).toBe(1);+ expect((item.getParent() as ListNode).getListType()).toBe('number');+ });+ });++ it("op 'checked' checks an item (in a check list)", () => {+ const { session } = setup('- a\n- b');+ const aId = read(session, () =>+ $getId(($getRoot().getFirstChild() as ListNode).getChildren()[0])+ );+ // getChecked() only reflects a value inside a check list+ edit(session, () =>+ $modifyNode(session, aId!, { op: 'listType', list: 'check' })+ );+ edit(session, () =>+ $modifyNode(session, aId!, { op: 'checked', checked: true })+ );+ expect(+ read(session, () => ($byId(session, aId!) as ListItemNode).getChecked())+ ).toBe(true);+ });++ it("op 'indent' nests / un-nests a list item", () => {+ const { session } = setup('- a\n- b');+ const bId = read(session, () =>+ $getId(($getRoot().getFirstChild() as ListNode).getChildren()[1])+ );+ edit(session, () =>+ $modifyNode(session, bId!, { op: 'indent', indent: 'in' })+ );+ expect(+ read(session, () => ($byId(session, bId!) as ListItemNode).getIndent())+ ).toBe(1);+ edit(session, () =>+ $modifyNode(session, bId!, { op: 'indent', indent: 'out' })+ );+ expect(+ read(session, () => ($byId(session, bId!) as ListItemNode).getIndent())+ ).toBe(0);+ });++ it("op 'checked' on a non-list-item throws EditError", () => {+ const { session, ids } = setup('plain paragraph');+ edit(session, () => {+ expect(() =>+ $modifyNode(session, ids[0], { op: 'checked', checked: true })+ ).toThrowError(Error);+ });+ });++ it('accepts a node directly (composes with $byId)', () => {+ const { session, ids } = setup('# Frogs\n\nframe budget\n\nthe frog line');+ edit(session, () => {+ const node = $byId(session, ids[2]);+ $modifyNode(session, node, { op: 'blockType', block: { type: 'quote' } });+ });+ const xml = serializeWithXml(session);+ expect(xml).toContain('the frog line');+ expect(xml).toContain('<blockquote');+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/modify.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/modify.tsnew file mode 100644index 0000000000..cc9a2bf387--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/modify.ts@@ -0,0 +1,101 @@+import { $isListItemNode } from '@lexical/list';+import {+ $findMatchingParent,+ $isElementNode,+ type ElementNode,+ type LexicalNode,+} from 'lexical';+import { match } from 'ts-pattern';+import { $isEquationNode } from '../../../../lexical-core/nodes/EquationNode';+import { $blockNode, $setBlockType, $setText, type BlockData } from './blocks';+import { $setListType, type ListKind } from './lists';+import { $byId } from './locate';+import type { LexicalSession } from './session';++/**+ * A single in-place modification, discriminated by `op`. Each variant carries+ * only the fields relevant to that operation, and applies to the node kind that+ * operation makes sense for (resolved up from the given id):+ * - blockType/text → the enclosing block-level element+ * - listType → the enclosing list+ * - checked/indent → the enclosing list item+ * `text`/`checked`/`indent`/`listType` preserve durable ids. `blockType` mints a+ * FRESH id (it replaces the node — see `$setBlockType`), as do the explicit+ * fresh-id operations (`$insert`, `$replaceBlock`).+ */+export type NodeChange =+ | { op: 'blockType'; block: BlockData }+ | { op: 'text'; text: string }+ | { op: 'equation'; tex: string }+ | { op: 'listType'; list: ListKind }+ | { op: 'checked'; checked: boolean }+ | { op: 'indent'; indent: number | 'in' | 'out' };++function $asBlock(node: LexicalNode, id: string): ElementNode {+ const b = $findMatchingParent(+ node,+ (n) => $isElementNode(n) && !n.isInline()+ );+ if (!$isElementNode(b)) {+ throw new Error(`$modifyNode: no block-level node for "${id}"`);+ }+ return b;+}++function $asListItem(node: LexicalNode, id: string) {+ const item = $findMatchingParent(node, $isListItemNode);+ if (!$isListItemNode(item)) {+ throw new Error(`$modifyNode: no list item for "${id}"`);+ }+ return item;+}++/**+ * Modify a node in place, addressed by id OR a node (e.g. one from `$byId`).+ * `change` is a discriminated union — see `NodeChange`. Returns the (possibly+ * retyped) primary node.+ */+export function $modifyNode(+ session: LexicalSession,+ target: string | LexicalNode,+ change: NodeChange+): LexicalNode {+ const node = typeof target === 'string' ? $byId(session, target) : target;+ const label = typeof target === 'string' ? target : node.getType();+ return match(change)+ .returnType<LexicalNode>()+ .with({ op: 'blockType' }, (c) =>+ $setBlockType(session, $asBlock(node, label), () => $blockNode(c.block))+ )+ .with({ op: 'text' }, (c) => {+ const block = $asBlock(node, label);+ $setText(block, c.text);+ return block;+ })+ .with({ op: 'equation' }, (c) => {+ if (!$isEquationNode(node)) {+ throw new Error(`$modifyNode: "${label}" is not an equation node`);+ }+ node.setEquation(c.tex);+ return node;+ })+ .with({ op: 'listType' }, (c) => $setListType(node, c.list, session))+ .with({ op: 'checked' }, (c) => {+ const item = $asListItem(node, label);+ item.setChecked(c.checked);+ return item;+ })+ .with({ op: 'indent' }, (c) => {+ const item = $asListItem(node, label);+ const cur = item.getIndent();+ item.setIndent(+ c.indent === 'in'+ ? cur + 1+ : c.indent === 'out'+ ? Math.max(0, cur - 1)+ : c.indent+ );+ return item;+ })+ .exhaustive();+}diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/session.test.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/session.test.tsnew file mode 100644index 0000000000..b15c9c81be--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/session.test.ts@@ -0,0 +1,16 @@+import { describe, expect, it } from 'vitest';+import { serializeWithXml } from '../utils';+import { setup, topLevelIds } from './_test-helpers';+import { createEditingSession, loadSnapshot, toSnapshot } from './session';++describe('session / IO', () => {+ it('toSnapshot/loadSnapshot round-trips content and ids', () => {+ const { session, ids } = setup('# Title\n\nbody text');+ const snap = toSnapshot(session);++ const session2 = createEditingSession();+ loadSnapshot(session2, snap);+ expect(topLevelIds(session2)).toEqual(ids);+ expect(serializeWithXml(session2)).toBe(serializeWithXml(session));+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/session.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/session.tsnew file mode 100644index 0000000000..39711ae846--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/session.ts@@ -0,0 +1,122 @@+import { registerCodeHighlighting } from '@lexical/code';+import { $convertFromMarkdownString } from '@lexical/markdown';+import {+ $createParagraphNode,+ $getRoot,+ createEditor,+ type ElementNode,+ type LexicalEditor,+ type SerializedEditorState,+} from 'lexical';+import {+ NodeReplacements,+ SupportedNodeTypes,+} from '../../../../lexical-core/node-list';+import {+ $updateAllNodeIds,+ type NodeIdMappings,+ nodeIdPlugin,+} from '../../../../lexical-core/plugins/nodeIdPlugin';+import { INTERNAL_TRANSFORMERS } from '../../../../lexical-core/transformers';++export type LexicalSession = {+ editor: LexicalEditor;+ ids: NodeIdMappings;+};++export function createEditingSession(): LexicalSession {+ const ids: NodeIdMappings = {+ idToNodeKeyMap: new Map(),+ nodeKeyToIdMap: new Map(),+ };+ const editor = createEditor({+ nodes: [...SupportedNodeTypes, ...NodeReplacements], // code becomes custom code like how we do it on main frontend+ onError: (error) => {+ throw error;+ },+ });+ nodeIdPlugin({ nodes: SupportedNodeTypes, mappings: ids })(editor);+ // prism tokenizes code blocks into code-highlight nodes so highlighting is+ // baked into the doc even when no browser is connected (if a browser is+ // connected it will observe a lexical update and swap with tokenized).+ registerCodeHighlighting(editor);+ return { editor, ids };+}++/** Load a document from markdown, replacing any existing content. */+export function loadMarkdown(session: LexicalSession, md: string): void {+ session.editor.update(+ () => {+ $getRoot().clear();+ $convertFromMarkdownString(md, INTERNAL_TRANSFORMERS);+ },+ { discrete: true }+ );+ // Ensure every node has an id (and is in the mappings) even if a transform+ // somehow missed it.+ session.editor.update(+ () => {+ $updateAllNodeIds(session.ids);+ },+ { discrete: true }+ );+}++/** Load a document from a serialized editor-state snapshot. */+export function loadSnapshot(+ session: LexicalSession,+ raw: SerializedEditorState+): void {+ // Lexical forbids an empty root (setEditorState throws). A document can be+ // genuinely empty, so seed a single empty paragraph instead of crashing.+ if (raw.root.children.length === 0) {+ session.editor.update(+ () => {+ const root = $getRoot();+ root.clear();+ root.append($createParagraphNode());+ },+ { discrete: true }+ );+ } else {+ session.editor.setEditorState(session.editor.parseEditorState(raw));+ }+ session.editor.update(+ () => {+ $updateAllNodeIds(session.ids);+ },+ { discrete: true }+ );+}++/** Export the current document as a serialized editor-state snapshot. */+export function toSnapshot(session: LexicalSession): SerializedEditorState {+ return session.editor.getEditorState().toJSON();+}++/**+ * Retype a container node by swapping in a freshly-built replacement. The+ * replacement gets a FRESH durable id (Loro can't reshape a container in place —+ * reusing the id makes the change vanish on sync, a fresh id reads as a clean+ * delete + insert), and every id that pointed at the old node is forwarded to+ * the replacement. Children carry over via `replace(…, true)` with their own ids+ * intact. Backs `$setBlockType` and `$setListType`.+ *+ * This is specifically for retyping containers — NOT a generic node replace.+ * Inline wraps (`$wrapInBlock`) re-parent a node without minting a fresh id.+ */+export function $retypeContainer<T extends ElementNode>(+ session: LexicalSession,+ node: ElementNode,+ replacement: T+): T {+ const oldKey = node.getKey();+ node.replace(replacement, true);+ $updateAllNodeIds(session.ids, replacement);+ const { idToNodeKeyMap } = session.ids;+ const newKey = replacement.getKey();+ for (const [id, key] of idToNodeKeyMap) {+ if (key === oldKey) idToNodeKeyMap.set(id, newKey);+ }+ return replacement;+}diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/tables.test.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/tables.test.tsnew file mode 100644index 0000000000..a3c4288aa7--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/tables.test.ts@@ -0,0 +1,93 @@+import { $isTableNode, type TableNode } from '@lexical/table';+import {+ $createTextNode,+ $getRoot,+ $isTextNode,+ type ElementNode,+} from 'lexical';+import { describe, expect, it } from 'vitest';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import { serializeWithXml } from '../utils';+import { edit, read, setup } from './_test-helpers';+import { $blockNode } from './blocks';+import { $byId } from './locate';+import { $setCell, $table } from './tables';++describe('$table', () => {+ it('builds a real TableNode that serializes to XML with table structure', () => {+ const { session, ids } = setup('intro');+ edit(session, () =>+ $byId(session, ids[0]).insertAfter(+ $table([+ ['Fruit', 'Taste'],+ ['Apple', 'Sweet'],+ ['Lemon', 'Sour'],+ ])+ )+ );+ expect(+ read(session, () => $getRoot().getChildren().some($isTableNode))+ ).toBe(true);+ const xml = serializeWithXml(session);+ expect(xml).toContain('<table');+ expect(xml).toContain('Fruit');+ expect(xml).toContain('Taste');+ expect(xml).toContain('Apple');+ expect(xml).toContain('Sweet');+ expect(xml).toContain('Lemon');+ expect(xml).toContain('Sour');+ });+});++describe('$setCell', () => {+ it('edits a single cell in place (0-based, header is row 0)', () => {+ const { session, ids } = setup('intro');+ edit(session, () =>+ $byId(session, ids[0]).insertAfter(+ $table([+ ['Fruit', 'Taste'],+ ['Apple', 'Sweet'],+ ])+ )+ );+ const tableId = read(session, () => $getId($getRoot().getChildren()[1]!));+ edit(session, () => $setCell($byId(session, tableId!), 1, 0, 'Banana')); // Apple -> Banana+ const xml = serializeWithXml(session);+ expect(xml).toContain('Banana');+ expect(xml).not.toContain('Apple');+ });++ it('throws on an out-of-range cell', () => {+ const { session, ids } = setup('intro');+ edit(session, () =>+ $byId(session, ids[0]).insertAfter($table([['a', 'b']]))+ );+ const tableId = read(session, () => $getId($getRoot().getChildren()[1]!));+ edit(session, () => {+ expect(() => $setCell($byId(session, tableId!), 9, 9, 'x')).toThrowError(+ Error+ );+ });+ });+});++describe('$table / $setCell node flexibility', () => {+ it('a cell can be a node you build (e.g. bold text), not just a string', () => {+ const { session, ids } = setup('intro');+ edit(session, () => {+ const para = $blockNode({ type: 'paragraph' });+ para.append($createTextNode('Apple').toggleFormat('bold'));+ $byId(session, ids[0]).insertAfter($table([[para]]));+ });+ // the paragraph we built is used as-is — the cell's text node is bold+ const isBold = read(session, () => {+ const table = $getRoot().getChildren()[1] as TableNode;+ const row = table.getFirstChild() as ElementNode; // TableRowNode+ const cell = row.getFirstChild() as ElementNode; // TableCellNode+ const para = cell.getFirstChild() as ElementNode; // ParagraphNode+ const text = para.getFirstChild();+ return $isTextNode(text) && text.hasFormat('bold');+ });+ expect(isBold).toBe(true);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/ai-toolkit/tables.ts b/js/ai-editing-worker/src/ai-editing/ai-toolkit/tables.tsnew file mode 100644index 0000000000..9f095227b4--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/ai-toolkit/tables.ts@@ -0,0 +1,93 @@+import {+ $createTableCellNode,+ $createTableNode,+ $createTableRowNode,+ $isTableCellNode,+ $isTableNode,+ $isTableRowNode,+ TableCellHeaderStates,+ type TableNode,+} from '@lexical/table';+import {+ $createParagraphNode,+ $createTextNode,+ $findMatchingParent,+ $isElementNode,+ type LexicalNode,+} from 'lexical';++/** A cell's content: plain text, or a node you build (for rich/formatted cells). */+export type CellContent = string | LexicalNode;++/**+ * The block-level child for a cell. A string (or stray inline node) is wrapped+ * in a paragraph; a block node you built (paragraph, list, …) is used as-is —+ * so you have full control over what a cell contains.+ */+function $cellChild(content: CellContent): LexicalNode {+ if (typeof content === 'string') {+ const p = $createParagraphNode();+ p.append($createTextNode(content));+ return p;+ }+ if ($isElementNode(content) && !content.isInline()) {+ return content;+ }+ const p = $createParagraphNode();+ p.append(content);+ return p;+}++/**+ * Build a TableNode from a 2D array of cells (one inner array per row). Each+ * cell is a string OR a node you construct — e.g. a paragraph with formatted+ * text, a list, etc. The first row is treated as a header row. Place it with+ * the normal node ops:+ * $blockById(s, 'b7').insertAfter($table([['Fruit', 'Taste'], ['Apple', 'Sweet']]))+ * $byId(s, 'oldTableId').replace($table(rows)) // rebuild an existing table+ */+export function $table(rows: CellContent[][]): TableNode {+ const table = $createTableNode();+ for (let ri = 0; ri < rows.length; ri++) {+ const row = $createTableRowNode();+ const isHeader = ri === 0;+ for (const content of rows[ri]) {+ const cell = $createTableCellNode(+ isHeader ? TableCellHeaderStates.ROW : TableCellHeaderStates.NO_STATUS+ );+ cell.append($cellChild(content));+ row.append(cell);+ }+ table.append(row);+ }+ return table;+}++/**+ * Set one cell's content, in place. `node` is the table (or any node inside it);+ * `row`/`col` are 0-based over the table's ACTUAL rows — the header is row 0,+ * and the `---` line in the rendering is not a row. `content` is a string or a+ * node you build.+ * $setCell($byId(s, 'tableId'), 1, 0, 'Banana')+ */+export function $setCell(+ node: LexicalNode,+ row: number,+ col: number,+ content: CellContent+): void {+ const table = $findMatchingParent(node, $isTableNode);+ if (!$isTableNode(table)) {+ throw new Error('$setCell: no enclosing table');+ }+ const cell = table+ .getChildren()+ .filter($isTableRowNode)+ [row]?.getChildren()+ .filter($isTableCellNode)[col];+ if (!cell) {+ throw new Error(`$setCell: no cell at [${row}, ${col}]`);+ }+ cell.clear();+ cell.append($cellChild(content));+}diff --git a/js/ai-editing-worker/src/ai-editing/awareness/ai-peer.ts b/js/ai-editing-worker/src/ai-editing/awareness/ai-peer.tsnew file mode 100644index 0000000000..adccf79d7b--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/awareness/ai-peer.ts@@ -0,0 +1,30 @@+/**+ * AI editors draw their Loro peer id from a small reserved block whose decimal+ * form is 15 leading 9s followed by a 3-digit identity, i.e.+ * `999999999999999000 ... 999999999999999999`.+ *+ * A human's peer id is random across all 2^64 values, so the chance one lands in+ * this 1000-wide window is basically zero.+ */++/** How many distinct AI identities the block holds (the 3-digit suffix). */+export const AI_PEER_COUNT = 1000;++/** Start of the reserved block, `[BASE, BASE + AI_PEER_COUNT)`. */+export const AI_PEER_BASE = 999_999_999_999_999_000n;++/** Whether a peer id was minted by an AI editor (lives in the reserved block). */+export const isAiPeer = (peer: bigint): boolean =>+ peer >= AI_PEER_BASE && peer < AI_PEER_BASE + BigInt(AI_PEER_COUNT);++// the worker's lifecycle is naturally short-lived enough that this should be fine+let nextOffset = 0;++/** Hand out the next AI peer id, cycling through the block. Sequential, so+ * consecutive editors never share an id; with concurrency far below+ * AI_PEER_COUNT, two live editors can never collide. */+export const nextAiPeerId = (): bigint => {+ const id = AI_PEER_BASE + BigInt(nextOffset);+ nextOffset = (nextOffset + 1) % AI_PEER_COUNT;+ return id;+};diff --git a/js/ai-editing-worker/src/ai-editing/awareness/awareness-source.test.ts b/js/ai-editing-worker/src/ai-editing/awareness/awareness-source.test.tsnew file mode 100644index 0000000000..e04954a64f--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/awareness/awareness-source.test.ts@@ -0,0 +1,145 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { realAwarenessSource, resolveTextOwner } from './awareness-source';++// Duck-typed fake loro containers (kind/get), matching what resolveTextOwner reads.+function textC() {+ return {+ kind: () => 'Text',+ length: 10,+ getCursor: (offset: number) => ({+ encode: () => new Uint8Array([offset]),+ }),+ };+}+function mapC(id: string, fields: { text?: unknown; children?: unknown[] }) {+ return {+ kind: () => 'Map',+ get: (k: string) => {+ if (k === '$') return { getShallowValue: () => ({ id }) };+ if (k === 'text') return fields.text;+ if (k === 'children')+ return fields.children ? { toArray: () => fields.children } : undefined;+ return undefined;+ },+ };+}+function fakeLoro(containers: Record<string, unknown>) {+ const mirror = { getContainerIds: () => Object.keys(containers) } as any;+ const doc = { getContainerById: (cid: string) => containers[cid] } as any;+ return { mirror, doc };+}++describe('resolveTextOwner (cursor-walk fix)', () => {+ it('a text-node container resolves to itself', () => {+ const t = textC();+ const { mirror, doc } = fakeLoro({ c1: mapC('t1', { text: t }) });+ expect(resolveTextOwner(mirror, doc, 't1')).toEqual({+ text: t,+ nodeId: 't1',+ });+ });++ it('a block whose text lives in a child resolves to the CHILD text-node id', () => {+ const childText = textC();+ const block = mapC('b1', { children: [mapC('t2', { text: childText })] });+ const { mirror, doc } = fakeLoro({ c1: block });+ // block id in, but the owner is the child text node (so the caret can walk)+ expect(resolveTextOwner(mirror, doc, 'b1')).toEqual({+ text: childText,+ nodeId: 't2',+ });+ });++ it('finds text nested deeper than direct children (DFS): list item → paragraph → text', () => {+ const deepText = textC();+ const item = mapC('li1', {+ children: [mapC('p1', { children: [mapC('t9', { text: deepText })] })],+ });+ const { mirror, doc } = fakeLoro({ c1: item });+ expect(resolveTextOwner(mirror, doc, 'li1')).toEqual({+ text: deepText,+ nodeId: 't9',+ });+ });++ it('returns null for an unknown id or a block with no text anywhere', () => {+ const { mirror, doc } = fakeLoro({+ c1: mapC('b1', { children: [mapC('img', {})] }),+ });+ expect(resolveTextOwner(mirror, doc, 'missing')).toBeNull();+ expect(resolveTextOwner(mirror, doc, 'b1')).toBeNull();+ });+});++describe('realAwarenessSource (no live mirror)', () => {+ beforeEach(() => {+ vi.useFakeTimers();+ });+ afterEach(() => {+ vi.useRealTimers();+ });++ it('no-ops (no broadcast) when the node has no resolvable text container', () => {+ const send = vi.fn();+ const src = realAwarenessSource({+ mirror: { getContainerIds: () => [] } as any,+ doc: { getContainerById: () => null } as any,+ send,+ name: 'Sam (AI)',+ color: 'accent-30',+ });+ src.apply({ type: 'cursor', node: 'missing', at: 0 });+ expect(send).not.toHaveBeenCalled();+ src.clear(); // no peers → still no broadcast, and no throw+ expect(send).not.toHaveBeenCalled();+ });++ it('keeps live awareness alive until clear removes it', () => {+ const send = vi.fn();+ const { mirror, doc } = fakeLoro({ c1: mapC('t1', { text: textC() }) });+ const src = realAwarenessSource({+ mirror,+ doc,+ send,+ name: 'Sam (AI)',+ color: 'accent-30',+ });++ src.apply({ type: 'cursor', node: 't1', at: 3 });+ expect(send).toHaveBeenCalledTimes(1);++ vi.advanceTimersByTime(1_999);+ expect(send).toHaveBeenCalledTimes(1);++ vi.advanceTimersByTime(1);+ expect(send).toHaveBeenCalledTimes(2);++ src.clear();+ vi.advanceTimersByTime(2_000);+ expect(send).toHaveBeenCalledTimes(3);++ vi.advanceTimersByTime(2_000);+ expect(send).toHaveBeenCalledTimes(3);+ });++ it('clears live awareness when the latest target no longer resolves', () => {+ const send = vi.fn();+ const { mirror, doc } = fakeLoro({ c1: mapC('t1', { text: textC() }) });+ const src = realAwarenessSource({+ mirror,+ doc,+ send,+ name: 'Sam (AI)',+ color: 'accent-30',+ });++ src.apply({ type: 'cursor', node: 't1', at: 3 });+ expect(send).toHaveBeenCalledTimes(1);++ src.apply({ type: 'cursor', node: 'missing', at: 0 });+ expect(send).toHaveBeenCalledTimes(2);++ vi.advanceTimersByTime(2_000);+ expect(send).toHaveBeenCalledTimes(2);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/awareness/awareness-source.ts b/js/ai-editing-worker/src/ai-editing/awareness/awareness-source.tsnew file mode 100644index 0000000000..cd5e73204e--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/awareness/awareness-source.ts@@ -0,0 +1,276 @@+/**+ * Broadcasts a writer's cursor/selection over Loro awareness so it shows up as a+ * live remote cursor in the website. The executor pumps each `Awareness` step+ * into `apply()`; unlike the old `presence.ts`, there's no internal timing here —+ * the drag-select and typing motion are decomposed into discrete awareness/pause+ * steps upstream, so this just resolves a node id → its `LoroText`, encodes a+ * cursor at the offset, and sends. Each writer gets its own source (distinct+ * name + color → distinct peer → distinct cursor).+ */++import type { Mirror } from '@loro-mirror/packages/core/src';+import {+ type Container,+ EphemeralStore,+ type LoroDoc,+ type LoroMap,+ type LoroText,+} from 'loro-crdt';+import { match } from 'ts-pattern';+import type { MarkdownLoroSchemaType } from '../../../../lexical-core/markdown-loro-schema';+import type { Awareness } from '../queue';++export const AI_NAMES = [+ 'Priya',+ 'Wolf',+ 'Nora',+ 'Finn',+ 'Teo',+ 'Maya',+ 'Hutch',+ 'Omar',+ 'Julia',+ 'Ravi',+ 'Lena',+ 'Seamus',+ 'Zoe',+ 'Daniel',+ 'Leo',+ 'Evan',+ 'Soren',+ 'Cleo',+ 'Gab',+ 'Iris',+ 'Austin',+ 'Theo',+ 'Peter',+ 'Nadia',+ 'Milo',+ 'Sean',+ 'Mara',+ 'Ethan',+ 'Kai',+ 'Luca',+ 'Ava',+ 'Jess',+ 'Eric',+ 'Quinn',+ 'Amara',+ 'Juno',+ 'Silas',+ 'Noemi',+].map((n) => `${n} (AI)`);+/** Palette names must resolve to `--color-<name>` (see collaboration/color.ts). */+export const COLORS = [+ 'accent-210',+ 'accent-30',+ 'accent-90',+ 'accent-150',+ 'accent-270',+ 'accent-330',+];++/** How long the cursor lingers after a writer finishes before it disappears. */+const LINGER_MS = 700;+const KEEPALIVE_MS = 2_000;++export interface AwarenessSource {+ apply(x: Awareness): void;+ clear(): void;+}++/** Records every applied `Awareness` and never touches Loro — for tests. */+export function mockAwarenessSource(): AwarenessSource & { seen: Awareness[] } {+ const seen: Awareness[] = [];+ return {+ seen,+ apply: (x) => void seen.push(x),+ clear: () => {+ seen.length = 0;+ },+ };+}++type EncodedCursorPoint = { nodeId: string; cursor: Uint8Array };+type AwarenessPayload = {+ user: { userId: string; color: string; peerId: string };+ selection: { anchor: EncodedCursorPoint; focus: EncodedCursorPoint };+};++export type RealAwarenessOptions = {+ mirror: Mirror<MarkdownLoroSchemaType>;+ doc: LoroDoc;+ send: (bytes: Uint8Array) => void;+ name: string;+ color: string;+};++export function realAwarenessSource(+ opts: RealAwarenessOptions+): AwarenessSource {+ const { mirror, doc, send, name, color } = opts;+ const store = new EphemeralStore<Record<string, AwarenessPayload>>(30_000);+ const peerKey = crypto.randomUUID();+ let lingerTimer: ReturnType<typeof setTimeout> | null = null;+ let keepaliveTimer: ReturnType<typeof setInterval> | null = null;+ let lastAwareness: Awareness | null = null;+ let shown = false;++ function stopKeepalive(): void {+ if (!keepaliveTimer) return;+ clearInterval(keepaliveTimer);+ keepaliveTimer = null;+ }++ function deletePresence(): void {+ stopKeepalive();+ store.delete(peerKey);+ send(store.encodeAll());+ shown = false;+ lastAwareness = null;+ }++ function startKeepalive(): void {+ if (keepaliveTimer) return;+ keepaliveTimer = setInterval(() => {+ if (!lastAwareness || !shown) {+ stopKeepalive();+ return;+ }+ if (!publish(lastAwareness, false)) deletePresence();+ }, KEEPALIVE_MS);+ }++ function setRange(+ nodeId: string,+ text: LoroText,+ a: number,+ b: number+ ): boolean {+ if (lingerTimer) {+ clearTimeout(lingerTimer);+ lingerTimer = null;+ }+ const len = text.length;+ const anchor = text.getCursor(Math.max(0, Math.min(a, len)));+ const focus = text.getCursor(Math.max(0, Math.min(b, len)));+ if (!anchor || !focus) return false;+ store.set(peerKey, {+ user: { userId: name, color, peerId: peerKey },+ selection: {+ anchor: { nodeId, cursor: anchor.encode() },+ focus: { nodeId, cursor: focus.encode() },+ },+ });+ shown = true;+ send(store.encodeAll());+ return true;+ }++ function publish(x: Awareness, keepalive: boolean): boolean {+ // Resolve to the node that actually OWNS the text (a block's text lives in a+ // child text-node container). The cursor blob must be tagged with that id, or+ // the receiver can't map the Loro cursor to a caret and it never walks.+ const owner = resolveTextOwner(mirror, doc, x.node);+ if (!owner) return false;+ const { text, nodeId: ownerNodeId } = owner;+ const didPublish = match(x)+ .with({ type: 'cursor' }, ({ at = 0 }) =>+ setRange(ownerNodeId, text, at, at)+ )+ .with({ type: 'highlight' }, ({ span }) =>+ setRange(ownerNodeId, text, span?.start ?? 0, span?.end ?? text.length)+ )+ .exhaustive();+ if (didPublish && keepalive) startKeepalive();+ return didPublish;+ }++ function apply(x: Awareness): void {+ lastAwareness = x;+ if (!publish(x, true)) {+ if (shown) deletePresence();+ lastAwareness = null;+ }+ }++ function clear(): void {+ if (!shown) return;+ stopKeepalive();+ if (lingerTimer) return;+ lingerTimer = setTimeout(() => {+ lingerTimer = null;+ deletePresence();+ }, LINGER_MS);+ }++ return { apply, clear };+}++// the following functions smell; their role is just to help us find a loro node+// given a stable id and then get the actual lorotext that we can use for+// awareness. there's probably a better way to do this++function isLoroMap(c: Container | undefined): c is LoroMap {+ return c?.kind() === 'Map';+}++/** The `$.id` of a loro container. */+function containerId(c: LoroMap): string | undefined {+ const dollar = c.get('$') as+ | { getShallowValue?: () => { id?: string } | undefined; id?: string }+ | undefined;+ return dollar && typeof dollar.getShallowValue === 'function'+ ? dollar.getShallowValue()?.id+ : dollar?.id;+}++/**+ * Resolve a node id to its `LoroText` AND the id of the container that owns that+ * text. A block's text lives in a child text-node container, so for a block id we+ * return the child text node's id — the awareness blob must be tagged with the+ * text-owning node's id, otherwise the receiver can't turn the Loro cursor into a+ * caret and it never walks with the typed text.+ */+export function resolveTextOwner(+ mirror: Mirror<MarkdownLoroSchemaType>,+ doc: LoroDoc,+ nodeId: string+): { text: LoroText; nodeId: string } | null {+ for (const cid of mirror.getContainerIds()) {+ const c = doc.getContainerById(cid);+ if (!isLoroMap(c)) continue;+ if (containerId(c) !== nodeId) continue;+ const own = ownText(c);+ if (own) return own;+ return firstDescendantText(c);+ }+ return null;+}++/** The container's own `{ text, nodeId }` if it directly holds a LoroText. */+function ownText(c: LoroMap): { text: LoroText; nodeId: string } | null {+ const text = c.get('text') as { kind?: () => string } | undefined;+ const nodeId = containerId(c);+ return text?.kind?.() === 'Text' && nodeId+ ? { text: text as unknown as LoroText, nodeId }+ : null;+}++/** First descendant (DFS) that directly owns a LoroText. */+function firstDescendantText(+ c: LoroMap+): { text: LoroText; nodeId: string } | null {+ const children = c.get('children') as+ | { toArray?: () => Array<Container | undefined> }+ | undefined;+ if (!children || typeof children.toArray !== 'function') return null;+ for (const child of children.toArray()) {+ if (!isLoroMap(child)) continue;+ const own = ownText(child);+ if (own) return own;+ const deep = firstDescendantText(child);+ if (deep) return deep;+ }+ return null;+}diff --git a/js/ai-editing-worker/src/ai-editing/awareness/index.ts b/js/ai-editing-worker/src/ai-editing/awareness/index.tsnew file mode 100644index 0000000000..c46feb38d5--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/awareness/index.ts@@ -0,0 +1,3 @@+export * from './ai-peer';+export * from './awareness-source';+export * from './peer-pool';diff --git a/js/ai-editing-worker/src/ai-editing/awareness/peer-pool.test.ts b/js/ai-editing-worker/src/ai-editing/awareness/peer-pool.test.tsnew file mode 100644index 0000000000..3dee1500ba--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/awareness/peer-pool.test.ts@@ -0,0 +1,115 @@+import { describe, expect, it } from 'vitest';+import { PeerPool } from './peer-pool';++const names = ['A', 'B', 'C'];+const colors = ['red', 'green'];++describe('PeerPool', () => {+ it('borrowing N peers yields N distinct names', async () => {+ const pool = new PeerPool({ names, colors });+ const peers = await Promise.all([+ pool.borrow(),+ pool.borrow(),+ pool.borrow(),+ ]);+ expect(new Set(peers.map((p) => p.name)).size).toBe(3);+ expect(peers.map((p) => p.name).sort()).toEqual(['A', 'B', 'C']);+ expect(pool.outstanding).toBe(3);+ });++ it('releasing then borrowing reuses a freed name', async () => {+ const pool = new PeerPool({ names, colors });+ const a = await pool.borrow();+ const b = await pool.borrow();+ pool.release(a);+ const c = await pool.borrow();+ expect(c.name).toBe(a.name);+ expect(c.name).not.toBe(b.name);+ expect(pool.outstanding).toBe(2);+ });++ it('grows with still-unique names when the base list is exhausted', async () => {+ const pool = new PeerPool({ names, colors, max: 5 });+ const peers = await Promise.all([+ pool.borrow(),+ pool.borrow(),+ pool.borrow(),+ pool.borrow(),+ pool.borrow(),+ ]);+ expect(new Set(peers.map((p) => p.name)).size).toBe(5);+ expect(pool.outstanding).toBe(5);+ });++ it('release of an unknown peer is a no-op', async () => {+ const pool = new PeerPool({ names, colors });+ const a = await pool.borrow();+ pool.release({ name: 'X', color: 'blue', peerId: 0n }); // never borrowed+ pool.release(a);+ pool.release(a); // double release+ expect(pool.outstanding).toBe(0);+ });++ it('outstanding tracks borrows and releases', async () => {+ const pool = new PeerPool({ names, colors });+ expect(pool.outstanding).toBe(0);+ const a = await pool.borrow();+ const b = await pool.borrow();+ expect(pool.outstanding).toBe(2);+ pool.release(b);+ expect(pool.outstanding).toBe(1);+ pool.release(a);+ expect(pool.outstanding).toBe(0);+ });++ it('prefers distinct colors while the palette lasts', async () => {+ const pool = new PeerPool({ names, colors });+ const a = await pool.borrow();+ const b = await pool.borrow();+ expect(a.color).not.toBe(b.color);+ });++ it('caps concurrent borrows at max, releasing a slot for the next waiter', async () => {+ const pool = new PeerPool({ names, colors, max: 2 });+ const a = await pool.borrow();+ const b = await pool.borrow();+ expect(pool.outstanding).toBe(2);+ expect(b.name).not.toBe(a.name);++ // A third borrow must wait — the pool is full.+ let third: { name: string } | undefined;+ const pending = pool.borrow().then((p) => {+ third = p;+ return p;+ });+ await Promise.resolve(); // give the queue a tick; still blocked+ expect(third).toBeUndefined();+ expect(pool.outstanding).toBe(2);++ // Releasing one opens the slot; the waiter resolves and reuses the name.+ pool.release(a);+ const c = await pending;+ expect(third).toBe(c);+ expect(c.name).toBe(a.name);+ expect(pool.outstanding).toBe(2);+ });++ it('blocks borrows past max until a slot frees, reusing the freed name', async () => {+ const pool = new PeerPool({ names: ['A', 'B', 'C', 'D'], colors, max: 3 });+ const three = await Promise.all([+ pool.borrow(),+ pool.borrow(),+ pool.borrow(),+ ]);+ expect(pool.outstanding).toBe(3);++ const fourth = pool.borrow(); // blocks: no slot is free+ await Promise.resolve();+ expect(pool.outstanding).toBe(3); // never acquires while full++ pool.release(three[0]!);+ const p = await fourth; // slot opened → resolves+ expect(p.name).toBe(three[0]!.name);+ expect(pool.outstanding).toBe(3);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/awareness/peer-pool.ts b/js/ai-editing-worker/src/ai-editing/awareness/peer-pool.tsnew file mode 100644index 0000000000..034ebea8b4--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/awareness/peer-pool.ts@@ -0,0 +1,66 @@+import PQueue from 'p-queue';+import { nextAiPeerId } from './ai-peer';+import { AI_NAMES, COLORS } from './awareness-source';++export type Peer = { name: string; color: string; peerId: bigint };++export class PeerPool {+ private readonly names: string[];+ private readonly colors: string[];+ private readonly free: Peer[] = []; // released, ready to reuse+ private readonly out = new Set<Peer>();+ private readonly releasers = new Map<Peer, () => void>(); // peer → free its slot+ private readonly gate: PQueue;++ constructor(opts?: { names?: string[]; colors?: string[]; max?: number }) {+ this.names = [...(opts?.names ?? AI_NAMES)];+ this.colors = [...(opts?.colors ?? COLORS)];+ this.gate = new PQueue({ concurrency: opts?.max ?? 6 });+ }++ /** Acquire a unique identity, waiting if `max` are already out (semaphore). */+ public borrow(): Promise<Peer> {+ return new Promise<Peer>((handOut) => {+ // The queued task holds a concurrency slot until its promise resolves; we+ // keep it pending and resolve it from release(), so the slot is occupied+ // for exactly the borrow's lifetime.+ void this.gate.add(+ () =>+ new Promise<void>((freeSlot) => {+ const peer = this.free.pop() ?? this.mint();+ this.out.add(peer);+ this.releasers.set(peer, freeSlot); // this.releasers.get()... .release() will release us+ handOut(peer); // okay, now we can hand out the peer+ })+ );+ });+ }++ public release(p: Peer): void {+ if (!this.out.delete(p)) return; // unknown / double-release -> no-op+ this.free.push(p);+ const freeSlot = this.releasers.get(p);+ if (freeSlot) {+ this.releasers.delete(p);+ freeSlot(); // opens the slot for the next waiting borrow (resolves the promise)+ }+ }++ public get outstanding(): number {+ return this.out.size;+ }++ /** A never-before-issued identity: next unused base name, else a suffixed one. */+ private mint(): Peer {+ const used = new Set([...this.out, ...this.free].map((p) => p.name));+ const name = this.names.find((n) => !used.has(n));+ if (!name) throw new Error('PeerPool: exhausted all available names');++ const color = this.colors[this.issued() % this.colors.length] ?? '';+ return { name, color, peerId: nextAiPeerId() };+ }++ private issued(): number {+ return this.out.size + this.free.length;+ }+}diff --git a/js/ai-editing-worker/src/ai-editing/doc/doc.test.ts b/js/ai-editing-worker/src/ai-editing/doc/doc.test.tsnew file mode 100644index 0000000000..db65017fa1--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/doc/doc.test.ts@@ -0,0 +1,691 @@+import { $isTableCellNode, $isTableRowNode } from '@lexical/table';+import {+ $getRoot,+ $isElementNode,+ $isTextNode,+ type ElementNode,+ type LexicalNode,+} from 'lexical';+import { describe, expect, it } from 'vitest';+import { $getId } from '../../../../lexical-core/plugins/nodeIdPlugin';+import { read, setup } from '../ai-toolkit/_test-helpers';+import type { LexicalSession } from '../ai-toolkit/session';+import { serializeWithXml } from '../utils';+import { Doc } from './doc';++function textRuns(session: LexicalSession, blockIdx = 0) {+ return read(session, () => {+ const block = $getRoot().getChildren()[blockIdx] as ElementNode;+ const runs: Array<{ text: string; bold: boolean; italic: boolean }> = [];+ const walk = (n: LexicalNode) => {+ if ($isTextNode(n))+ runs.push({+ text: n.getTextContent(),+ bold: n.hasFormat('bold'),+ italic: n.hasFormat('italic'),+ });+ if ($isElementNode(n)) for (const c of n.getChildren()) walk(c);+ };+ if (block) for (const c of block.getChildren()) walk(c);+ return runs;+ });+}++function childIds(session: LexicalSession, topLevelIndex = 0): string[] {+ return read(session, () => {+ const block = $getRoot().getChildren()[topLevelIndex] as ElementNode;+ return block.getChildren().map((c) => $getId(c) ?? '?');+ });+}++describe('Doc — text content writes', () => {+ it('insertText at the end of a formatted run stays plain (no format inheritance)', () => {+ const { session, ids } = setup('a **b**');+ const len = new Doc(session).textLength(ids[0]!);+ new Doc(session).apply({+ kind: 'insertText',+ node: ids[0]!,+ at: len,+ text: ' c',+ });+ const runs = textRuns(session);+ expect(runs.find((r) => r.text === 'b')?.bold).toBe(true);+ expect(runs.find((r) => r.text.includes('c'))?.bold).toBe(false);+ });++ it('insertText at offset 0 before a formatted run stays plain', () => {+ const { session, ids } = setup('**b** c');+ new Doc(session).apply({+ kind: 'insertText',+ node: ids[0]!,+ at: 0,+ text: 'a ',+ });+ expect(textRuns(session).find((r) => r.text.includes('a'))?.bold).toBe(+ false+ );+ });+});++describe('Doc — block type & lists', () => {+ it('setBlockType mints a fresh durable id (old id leaves the doc)', () => {+ const { session, ids } = setup('Title');+ const before = ids[0]!;+ new Doc(session).apply({+ kind: 'setBlockType',+ node: before,+ block: 'heading',+ level: 2,+ });+ const xml = serializeWithXml(session);+ expect(xml).toContain('<h2');+ expect(xml).not.toContain(`id="${before}"`);+ });++ it('a follow-up edit still resolves the old id after a type swap', () => {+ const { session, ids } = setup('Title');+ const doc = new Doc(session);+ doc.apply({+ kind: 'setBlockType',+ node: ids[0]!,+ block: 'heading',+ level: 2,+ });+ doc.apply({ kind: 'appendText', node: ids[0]!, text: '!' });+ expect(serializeWithXml(session)).toContain('Title!');+ });+});++describe('Doc — structure & refs', () => {+ it('inserts a block anchored to a decorator (hr) landing as its sibling', () => {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 'hr',+ spec: { block: 'divider' },+ at: { after: ids[0]! },+ });+ doc.apply({+ kind: 'insertNode',+ ref: 'h',+ spec: { block: 'heading', level: 2, text: 'Section' },+ at: { after: 'hr' },+ });+ const out = serializeWithXml(session);+ expect(out.indexOf('<hr')).toBeLessThan(out.indexOf('<h2'));+ });++ it('appendListItem rejects a non-list target', () => {+ const { session, ids } = setup('para');+ expect(() =>+ new Doc(session).apply({+ kind: 'appendListItem',+ ref: 'x',+ node: ids[0]!,+ text: 'x',+ })+ ).toThrow(/not a list/);+ });++ it('appendListItem / prependListItem populate a list by its container id (even when empty)', () => {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 'L',+ spec: { block: 'list', list: 'number', items: [] },+ at: { after: ids[0]! },+ });+ // empty list: no <li> to anchor on — must work off the <ol> id+ doc.apply({ kind: 'appendListItem', ref: 'a', node: 'L', text: 'middle' });+ doc.apply({ kind: 'appendListItem', ref: 'b', node: 'L', text: 'last' });+ doc.apply({ kind: 'prependListItem', ref: 'c', node: 'L', text: 'first' });+ const xml = serializeWithXml(session);+ expect((xml.match(/<ol/g) ?? []).length).toBe(1);+ expect(xml.indexOf('first')).toBeLessThan(xml.indexOf('middle'));+ expect(xml.indexOf('middle')).toBeLessThan(xml.indexOf('last'));+ });++ it('appendListItem on a check list creates an (unchecked) checkbox item', () => {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 'L',+ spec: { block: 'list', list: 'check', items: [] },+ at: { after: ids[0]! },+ });+ doc.apply({ kind: 'appendListItem', ref: 'a', node: 'L', text: 'task' });+ // the appended item is a real checkbox item: toggling it renders checked.+ doc.apply({ kind: 'setChecked', node: 'a', checked: true });+ expect(serializeWithXml(session)).toMatch(/checked="true"/);+ });++ it('insertListItemAfter/Before add same-kind siblings into an existing list', () => {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 'L',+ spec: { block: 'list', list: 'bullet', items: [] },+ at: { after: ids[0]! },+ });+ doc.apply({+ kind: 'appendListItem',+ ref: 'L~li-0',+ node: 'L',+ text: 'middle',+ });+ doc.apply({+ kind: 'insertListItemAfter',+ ref: 'after',+ node: 'L~li-0',+ text: 'last',+ list: 'bullet',+ });+ doc.apply({+ kind: 'insertListItemBefore',+ ref: 'before',+ node: 'L~li-0',+ text: 'first',+ list: 'bullet',+ });+ const xml = serializeWithXml(session);+ expect((xml.match(/<ul/g) ?? []).length).toBe(1);+ expect(xml.indexOf('first')).toBeLessThan(xml.indexOf('middle'));+ expect(xml.indexOf('middle')).toBeLessThan(xml.indexOf('last'));+ });++ it('insertListItemAfter with a differing kind nests a sublist', () => {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 'L',+ spec: { block: 'list', list: 'bullet', items: [] },+ at: { after: ids[0]! },+ });+ doc.apply({+ kind: 'appendListItem',+ ref: 'L~li-0',+ node: 'L',+ text: 'bullet item',+ });+ doc.apply({+ kind: 'insertListItemAfter',+ ref: 'nested',+ node: 'L~li-0',+ text: 'numbered item',+ list: 'number',+ });+ expect(serializeWithXml(session)).toMatch(+ /<ul[\s\S]*<ol[\s\S]*numbered item[\s\S]*<\/ol>[\s\S]*<\/ul>/+ );+ });++ it('removeListItem rejects a non-list-item target', () => {+ const { session, ids } = setup('para');+ expect(() =>+ new Doc(session).apply({ kind: 'removeListItem', node: ids[0]! })+ ).toThrow(/not a list item/);+ });++ it('setCell rejects a <tr> id (must be the table)', () => {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 't',+ spec: {+ block: 'table',+ rows: [+ ['A', 'B'],+ ['c', 'd'],+ ],+ },+ at: { after: ids[0]! },+ });+ const rowId = serializeWithXml(session).match(/<tr id="([^"]+)"/)?.[1];+ expect(rowId).toBeTruthy();+ expect(() =>+ doc.apply({ kind: 'setCell', table: rowId!, row: 0, col: 0, text: 'X' })+ ).toThrow(/not a table/);+ });+});++describe('Doc — readers', () => {+ it('cellNode resolves a cell content id whose text we can measure', () => {+ const { session, ids } = setup('x');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 't',+ spec: { block: 'table', rows: [['Head'], ['body']] },+ at: { after: ids[0]! },+ });+ expect(doc.textLength(doc.cellNode('t', 1, 0))).toBe(4);+ });+});++describe('Doc — error surfacing', () => {+ it('an unknown id throws EditError', () => {+ const { session } = setup('hi');+ expect(() =>+ new Doc(session).apply({ kind: 'setText', node: 'nope', text: 'x' })+ ).toThrow(/No node with id|nope/);+ });++ it('a failed edit leaves the document untouched', () => {+ const { session, ids } = setup('safe');+ expect(() =>+ new Doc(session).apply({ kind: 'removeNode', node: 'ghost' })+ ).toThrow();+ expect(serializeWithXml(session)).toContain('safe');+ expect(serializeWithXml(session)).toContain(`id="${ids[0]}"`);+ });+});++describe('Doc.locate — within and across text nodes', () => {+ it('occurrences across two text runs report per-node (within-node) offsets', () => {+ const { session, ids } = setup('a **b** a');+ const matches = new Doc(session).locate(ids[0]!, 'a', { kind: 'all' });+ expect(matches).toHaveLength(2);+ expect(matches[0]).toMatchObject({ start: 0, end: 1 });+ expect(matches[1]).toMatchObject({ start: 1, end: 2 });+ expect(matches[0]!.node).not.toBe(matches[1]!.node);+ });++ it('node id is the text-node id (not the block id)', () => {+ const { session, ids } = setup('hello');+ const matches = new Doc(session).locate(ids[0]!, 'ell');+ expect(matches[0]!.node).not.toBe(ids[0]);+ expect(matches[0]).toMatchObject({ start: 1, end: 4 });+ });++ it("scope { kind: 'nth', n: 2 } returns only the 2nd occurrence", () => {+ const { session, ids } = setup('a a a');+ const m = new Doc(session).locate(ids[0]!, 'a', { kind: 'nth', n: 2 });+ expect(m).toHaveLength(1);+ expect(m[0]).toMatchObject({ start: 2, end: 3 });+ });++ it('nth counts across text-node boundaries', () => {+ const { session, ids } = setup('a **b** a');+ const m = new Doc(session).locate(ids[0]!, 'a', { kind: 'nth', n: 2 });+ expect(m).toHaveLength(1);+ expect(m[0]).toMatchObject({ start: 1, end: 2 });+ });+});++describe('Doc.insertText — multi-run formatting preservation', () => {+ it('inserting inside a plain run keeps adjacent bold run bold', () => {+ const { session, ids } = setup('**bold** plain');+ new Doc(session).apply({+ kind: 'insertText',+ node: ids[0]!,+ at: 6,+ text: 'Z',+ });+ expect(textRuns(session).find((r) => r.text === 'bold')?.bold).toBe(true);+ expect(serializeWithXml(session)).toContain('pZlain');+ });++ it('inserting inside a bold run stays bold', () => {+ const { session, ids } = setup('a **b** c');+ new Doc(session).apply({+ kind: 'insertText',+ node: ids[0]!,+ at: 3,+ text: 'X',+ });+ expect(textRuns(session).find((r) => r.text === 'bX')?.bold).toBe(true);+ });++ it('inserting at a run boundary lands in the earlier plain run', () => {+ const { session, ids } = setup('a **b** c');+ new Doc(session).apply({+ kind: 'insertText',+ node: ids[0]!,+ at: 2,+ text: 'X',+ });+ expect(textRuns(session).find((r) => r.text === 'b')?.bold).toBe(true);+ expect(serializeWithXml(session)).toContain('X');+ });+});++describe('Doc.removeText — slices and spans', () => {+ it('removes a range spanning two text nodes', () => {+ const { session, ids } = setup('a **b** c');+ new Doc(session).apply({+ kind: 'removeText',+ node: ids[0]!,+ at: 1,+ len: 3,+ });+ expect(serializeWithXml(session)).toContain('>a<');+ expect(serializeWithXml(session)).toContain('>c<');+ expect(serializeWithXml(session)).not.toContain('>b<');+ });+});++describe('Doc.insertInline — offset placement', () => {+ it('offset 0 inserts before the first run', () => {+ const { session, ids } = setup('hello');+ new Doc(session).apply({+ kind: 'insertInline',+ ref: 'r',+ node: ids[0]!,+ at: 0,+ spec: { inline: 'date', date: '2026-01-01' },+ });+ const out = serializeWithXml(session);+ expect(out.indexOf('2026-01-01')).toBeLessThan(out.indexOf('hello'));+ });++ it('a middle offset splits the run', () => {+ const { session, ids } = setup('hello');+ new Doc(session).apply({+ kind: 'insertInline',+ ref: 'r',+ node: ids[0]!,+ at: 2,+ spec: { inline: 'linebreak' },+ });+ expect(serializeWithXml(session)).toContain('<br');+ expect(serializeWithXml(session)).toContain('he');+ expect(serializeWithXml(session)).toContain('llo');+ });++ it('offset at the end appends after the last run', () => {+ const { session, ids } = setup('hello');+ new Doc(session).apply({+ kind: 'insertInline',+ ref: 'r',+ node: ids[0]!,+ at: 5,+ spec: { inline: 'date', date: '2026-01-01' },+ });+ const out = serializeWithXml(session);+ expect(out.indexOf('hello')).toBeLessThan(out.indexOf('2026-01-01'));+ });+});++describe('Doc — chained block-type swaps stay addressable by the original id', () => {+ it('paragraph → heading → quote → paragraph: original id still resolves throughout', () => {+ const { session, ids } = setup('Title');+ const id = ids[0]!;+ const doc = new Doc(session);+ doc.apply({ kind: 'setBlockType', node: id, block: 'heading', level: 2 });+ doc.apply({ kind: 'setBlockType', node: id, block: 'quote' });+ doc.apply({ kind: 'setBlockType', node: id, block: 'paragraph' });+ doc.apply({ kind: 'appendText', node: id, text: '!' });+ expect(serializeWithXml(session)).toContain('Title!');+ expect(serializeWithXml(session)).not.toContain(`id="${id}"`);+ });++ it('a child node stays addressable by its own id after the parent is swapped', () => {+ const { session, ids } = setup('hello world');+ const doc = new Doc(session);+ const childId = read(session, () =>+ $getId(($getRoot().getFirstChild() as ElementNode).getFirstChild()!)+ )!;+ doc.apply({+ kind: 'setBlockType',+ node: ids[0]!,+ block: 'heading',+ level: 2,+ });+ doc.apply({ kind: 'formatNode', node: childId, format: 'bold', on: true });+ const xml = serializeWithXml(session);+ expect(xml).toContain('<h2');+ expect(xml).toContain('bold="true"');+ });+});++describe('Doc.setListType — preserves item ids', () => {+ it('toggling a paragraph into a list produces an addressable item', () => {+ const { session, ids } = setup('one');+ const doc = new Doc(session);+ doc.apply({ kind: 'setListType', nodes: [ids[0]!], list: 'bullet' });+ const itemId = childIds(session)[0]!;+ doc.apply({ kind: 'appendText', node: itemId, text: '!' });+ expect(serializeWithXml(session)).toContain('one!');+ });++ it('switching bullet → numbered keeps every item id', () => {+ const { session } = setup('- one\n- two');+ const before = childIds(session);+ new Doc(session).apply({+ kind: 'setListType',+ nodes: [before[0]!],+ list: 'number',+ });+ expect(childIds(session)).toEqual(before);+ expect(serializeWithXml(session)).toContain('<ol');+ });++ it('retypes a list addressed by its container id (the <ul>/<ol>)', () => {+ const { session, ids } = setup('- one\n- two');+ new Doc(session).apply({+ kind: 'setListType',+ nodes: [ids[0]!],+ list: 'number',+ });+ const xml = serializeWithXml(session);+ expect(xml).toContain('<ol');+ expect(xml).not.toContain('<ul');+ });+});++describe('Doc — ref resolution', () => {+ it('inserting two blocks mints two distinct addressable ids', () => {+ const { session, ids } = setup('first');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 'a',+ spec: { block: 'paragraph', text: 'AA' },+ at: { after: ids[0]! },+ });+ doc.apply({+ kind: 'insertNode',+ ref: 'b',+ spec: { block: 'paragraph', text: 'BB' },+ at: { after: ids[0]! },+ });+ doc.apply({ kind: 'appendText', node: 'a', text: '1' });+ doc.apply({ kind: 'appendText', node: 'b', text: '2' });+ const out = serializeWithXml(session);+ expect(out).toContain('AA1');+ expect(out).toContain('BB2');+ });+});++describe('Doc — tables', () => {+ function makeTable(rows: string[][]) {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 't',+ spec: { block: 'table', rows },+ at: { after: ids[0]! },+ });+ return { session, doc };+ }++ it('setCell each cell of a 2x2 table', () => {+ const { session, doc } = makeTable([+ ['H1', 'H2'],+ ['a', 'b'],+ ]);+ doc.apply({ kind: 'setCell', table: 't', row: 0, col: 1, text: 'HX' });+ doc.apply({ kind: 'setCell', table: 't', row: 1, col: 0, text: 'AA' });+ doc.apply({ kind: 'setCell', table: 't', row: 1, col: 1, text: 'BB' });+ const out = serializeWithXml(session);+ expect(out).toContain('H1');+ expect(out).toContain('HX');+ expect(out).toContain('AA');+ expect(out).toContain('BB');+ });++ it('cellNode resolves and textLength matches the cell content', () => {+ const { doc } = makeTable([['Head'], ['body']]);+ expect(doc.textLength(doc.cellNode('t', 0, 0))).toBe(4);+ expect(doc.textLength(doc.cellNode('t', 1, 0))).toBe(4);+ });++ it('addRow appends a row with the right column count', () => {+ const { session, doc } = makeTable([+ ['H1', 'H2'],+ ['a', 'b'],+ ]);+ doc.apply({ kind: 'addRow', table: 't' });+ expect(rowCellCounts(session)).toEqual([2, 2, 2]);+ });++ it('addRow at an index inserts before that row', () => {+ const { session, doc } = makeTable([+ ['H1', 'H2'],+ ['a', 'b'],+ ]);+ doc.apply({ kind: 'addRow', table: 't', at: 1 });+ doc.apply({ kind: 'setCell', table: 't', row: 1, col: 0, text: 'mid' });+ expect(serializeWithXml(session)).toContain('mid');+ expect(rowCellCounts(session)).toEqual([2, 2, 2]);+ });++ it('addColumn gives every row a new cell; header-row cell is a header', () => {+ const { session, doc } = makeTable([+ ['H1', 'H2'],+ ['a', 'b'],+ ]);+ doc.apply({ kind: 'addColumn', table: 't' });+ expect(rowCellCounts(session)).toEqual([3, 3]);+ const headerStates = read(session, () =>+ tableRows(session)[0]!+ .getChildren()+ .filter($isTableCellNode)+ .map((c) => c.getHeaderStyles())+ );+ expect(headerStates.at(-1)).not.toBe(0);+ });++ it('removeRow drops a row', () => {+ const { session, doc } = makeTable([+ ['H1', 'H2'],+ ['a', 'b'],+ ['c', 'd'],+ ]);+ doc.apply({ kind: 'removeRow', table: 't', row: 2 });+ expect(rowCellCounts(session)).toHaveLength(2);+ expect(serializeWithXml(session)).not.toContain('>c<');+ expect(serializeWithXml(session)).not.toContain('>d<');+ });++ it('removeColumn drops the column from every row', () => {+ const { session, doc } = makeTable([+ ['H1', 'H2', 'H3'],+ ['aa', 'bb', 'cc'],+ ]);+ doc.apply({ kind: 'removeColumn', table: 't', col: 1 });+ expect(rowCellCounts(session)).toEqual([2, 2]);+ // assert on actual cell text, not raw XML: random node ids can contain the+ // substring 'H2', which would make a `not.toContain('H2')` check flaky.+ expect(cellTexts(session)).toEqual([+ ['H1', 'H3'],+ ['aa', 'cc'],+ ]);+ });+});++function cellTexts(session: LexicalSession): string[][] {+ return read(session, () =>+ tableRows(session).map((row) =>+ row+ .getChildren()+ .filter($isTableCellNode)+ .map((cell) => cell.getTextContent())+ )+ );+}++function tableRows(session: LexicalSession) {+ return read(session, () => {+ let table: LexicalNode | undefined;+ const walk = (n: LexicalNode) => {+ if (n.getType() === 'table') table = n;+ if ($isElementNode(n)) for (const c of n.getChildren()) walk(c);+ };+ for (const c of $getRoot().getChildren()) walk(c);+ return (table as ElementNode).getChildren().filter($isTableRowNode);+ });+}++function rowCellCounts(session: LexicalSession): number[] {+ return read(session, () => {+ let table: LexicalNode | undefined;+ const walk = (n: LexicalNode) => {+ if (n.getType() === 'table') table = n;+ if ($isElementNode(n)) for (const c of n.getChildren()) walk(c);+ };+ for (const c of $getRoot().getChildren()) walk(c);+ return (table as ElementNode)+ .getChildren()+ .filter($isTableRowNode)+ .map((r) => r.getChildren().filter($isTableCellNode).length);+ });+}++describe('Doc — error surfacing & atomicity', () => {+ it('a failed insert (unknown anchor) leaves the doc untouched', () => {+ const { session, ids } = setup('safe');+ const doc = new Doc(session);+ expect(() =>+ doc.apply({+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'paragraph', text: 'x' },+ at: { after: 'ghost' },+ })+ ).toThrow();+ expect(serializeWithXml(session)).toContain('safe');+ expect(serializeWithXml(session)).toContain(`id="${ids[0]}"`);+ });++ it('cellNode on a missing cell throws EditError', () => {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 't',+ spec: { block: 'table', rows: [['A']] },+ at: { after: ids[0]! },+ });+ expect(() => doc.cellNode('t', 9, 9)).toThrow();+ });++ it('a failed setCell leaves the table untouched', () => {+ const { session, ids } = setup('intro');+ const doc = new Doc(session);+ doc.apply({+ kind: 'insertNode',+ ref: 't',+ spec: {+ block: 'table',+ rows: [+ ['A', 'B'],+ ['c', 'd'],+ ],+ },+ at: { after: ids[0]! },+ });+ const before = serializeWithXml(session);+ expect(() =>+ doc.apply({ kind: 'setCell', table: 't', row: 9, col: 9, text: 'x' })+ ).toThrow();+ expect(serializeWithXml(session)).toBe(before);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/doc/doc.ts b/js/ai-editing-worker/src/ai-editing/doc/doc.tsnew file mode 100644index 0000000000..9e5fbe54b7--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/doc/doc.ts@@ -0,0 +1,942 @@+/**+ * The one place this system touches real Lexical (and, via `propagate`, Loro).+ * `Doc` implements both `DocReader` (reads to plan) and `DocWriter` (atomic+ * edits), resolving every `NodeRef` through the durable id ↔ node-key map and+ * delegating the actual mutations to the existing `ai-toolkit` `$`-helpers. A+ * minted ref IS the inserted node's id — `assignRef` stamps it on directly — so+ * later reads/edits in the same run resolve it with no extra mapping.+ */++import { $createCodeNode } from '@lexical/code';+import { $createLinkNode, $isLinkNode } from '@lexical/link';+import {+ $createListItemNode,+ $createListNode,+ $isListItemNode,+ $isListNode,+ type ListType,+} from '@lexical/list';+import { $createMarkNode, $isMarkNode } from '@lexical/mark';+import {+ $createHeadingNode,+ $createQuoteNode,+ type HeadingTagType,+} from '@lexical/rich-text';+import {+ $createTableCellNode,+ $createTableRowNode,+ $isTableCellNode,+ $isTableNode,+ $isTableRowNode,+ TableCellHeaderStates,+ type TableNode,+} from '@lexical/table';+import {+ $createLineBreakNode,+ $createParagraphNode,+ $createTextNode,+ $findMatchingParent,+ $getRoot,+ $isElementNode,+ $isTextNode,+ type ElementNode,+ type LexicalNode,+} from 'lexical';+import { match } from 'ts-pattern';+import { $createContactMentionNode } from '../../../../lexical-core/nodes/ContactMentionNode';+import {+ $createDateMentionNode,+ $isDateMentionNode,+} from '../../../../lexical-core/nodes/DateMentionNode';+import { $createDocumentCardNode } from '../../../../lexical-core/nodes/DocumentCardNode';+import { $createDocumentMentionNode } from '../../../../lexical-core/nodes/DocumentMentionNode';+import { $createEquationNode } from '../../../../lexical-core/nodes/EquationNode';+import { $createGroupMentionNode } from '../../../../lexical-core/nodes/GroupMentionNode';+import { $createHorizontalRuleNode } from '../../../../lexical-core/nodes/HorizontalRuleNode';+import { $createHtmlRenderNode } from '../../../../lexical-core/nodes/HtmlRenderNode';+import {+ $createImageNode,+ ImageNode,+} from '../../../../lexical-core/nodes/ImageNode';+import { $createUserMentionNode } from '../../../../lexical-core/nodes/UserMentionNode';+import {+ $createVideoNode,+ VideoNode,+} from '../../../../lexical-core/nodes/VideoNode';+import {+ $getId,+ $setId,+ $updateAllNodeIds,+} from '../../../../lexical-core/plugins/nodeIdPlugin';+import * as blocks from '../ai-toolkit/blocks';+import * as inline from '../ai-toolkit/inline';+import * as lists from '../ai-toolkit/lists';+import * as locate from '../ai-toolkit/locate';+import * as modify from '../ai-toolkit/modify';+import type { LexicalSession } from '../ai-toolkit/session';+import * as tables from '../ai-toolkit/tables';+import type {+ DocumentOp,+ Format,+ ListKind,+ NodeRef,+ NodeSpec,+ Offset,+ Position,+ Scope,+} from '../editor';+import { EditError } from '../editor';+import type { DocReader, DocWriter, Match } from './interfaces';++const FORMAT_BIT: Record<+ Format,+ 'bold' | 'italic' | 'underline' | 'strikethrough' | 'code'+> = {+ bold: 'bold',+ italic: 'italic',+ underline: 'underline',+ strike: 'strikethrough',+ code: 'code',+};++export class Doc implements DocReader, DocWriter {+ constructor(+ private readonly session: LexicalSession,+ /** Push the new state out (snapshot to mirror to Loro). Noop in unit tests. */+ private readonly propagate: () => void = () => {}+ ) {}++ /** A ref IS the node's id (we stamp it on at insert), so resolution is identity. */+ public resolveRef(node: NodeRef): NodeRef {+ return node;+ }++ public apply(op: DocumentOp): void {+ match(op)+ .with({ kind: 'insertText' }, (e) =>+ this.insertText(e.node, e.at, e.text)+ )+ .with({ kind: 'removeText' }, (e) => this.removeText(e.node, e.at, e.len))+ .with({ kind: 'setText' }, (e) => this.setText(e.node, e.text))+ .with({ kind: 'setEquation' }, (e) => this.setEquation(e.node, e.tex))+ .with({ kind: 'appendText' }, (e) => this.appendText(e.node, e.text))+ .with({ kind: 'prependText' }, (e) => this.prependText(e.node, e.text))+ .with({ kind: 'insertTextAfterInline' }, (e) =>+ this.insertTextAfterInline(e.inline, e.text, e.ref)+ )+ .with({ kind: 'replaceText' }, (e) =>+ this.replaceText(e.node, e.find, e.to, e.scope)+ )+ .with({ kind: 'formatText' }, (e) =>+ this.formatText(e.node, e.match, e.format, e.on, e.scope)+ )+ .with({ kind: 'clearFormat' }, (e) =>+ this.clearFormat(e.node, e.match, e.scope)+ )+ .with({ kind: 'markText' }, (e) =>+ this.markText(e.node, e.match, e.on, e.scope)+ )+ .with({ kind: 'linkText' }, (e) =>+ this.linkText(e.node, e.match, e.url, e.scope)+ )+ .with({ kind: 'formatNode' }, (e) =>+ this.formatNode(e.node, e.format, e.on)+ )+ .with({ kind: 'clearNodeFormat' }, (e) => this.clearNodeFormat(e.node))+ .with({ kind: 'setBlockType' }, (e) =>+ this.setBlockType(e.node, e.block, {+ level: e.level,+ language: e.language,+ })+ )+ .with({ kind: 'setListType' }, (e) => this.setListType(e.nodes, e.list))+ .with({ kind: 'appendListItem' }, (e) =>+ this.appendListItem(e.ref, e.node, e.text, e.checked)+ )+ .with({ kind: 'prependListItem' }, (e) =>+ this.prependListItem(e.ref, e.node, e.text, e.checked)+ )+ .with({ kind: 'setChecked' }, (e) => this.setChecked(e.node, e.checked))+ .with({ kind: 'setIndent' }, (e) => this.setIndent(e.node, e.indent))+ .with({ kind: 'insertNode' }, (e) => this.insertNode(e.ref, e.spec, e.at))+ .with({ kind: 'insertInline' }, (e) =>+ this.insertInline(e.ref, e.node, e.at, e.spec)+ )+ .with({ kind: 'moveNode' }, (e) => this.moveNode(e.node, e.at))+ .with({ kind: 'removeNode' }, (e) => this.removeNode(e.node))+ .with({ kind: 'mergeBlocks' }, (e) =>+ this.mergeBlocks(e.nodes, e.separator)+ )+ .with({ kind: 'insertListItemAfter' }, (e) =>+ this.insertListItemAfter(e.ref, e.node, e.text, e.list)+ )+ .with({ kind: 'insertListItemBefore' }, (e) =>+ this.insertListItemBefore(e.ref, e.node, e.text, e.list)+ )+ .with({ kind: 'removeListItem' }, (e) => this.removeListItem(e.node))+ .with({ kind: 'setCell' }, (e) =>+ this.setCell(e.table, e.row, e.col, e.text)+ )+ .with({ kind: 'addRow' }, (e) => this.addRow(e.table, e.at))+ .with({ kind: 'addColumn' }, (e) => this.addColumn(e.table, e.at))+ .with({ kind: 'removeRow' }, (e) => this.removeRow(e.table, e.row))+ .with({ kind: 'removeColumn' }, (e) => this.removeColumn(e.table, e.col))+ .with({ kind: 'setImageAlt' }, (e) => this.setImageAlt(e.node, e.alt))+ .with({ kind: 'setImageUrl' }, (e) => this.setImageUrl(e.node, e.url))+ .with({ kind: 'setVideoUrl' }, (e) => this.setVideoUrl(e.node, e.url))+ .with({ kind: 'setVideoControls' }, (e) =>+ this.setVideoControls(e.node, e.controls)+ )+ .with({ kind: 'setDate' }, (e) =>+ this.setDate(e.node, e.date, e.displayFormat)+ )+ .exhaustive();+ }++ private read<T>(fn: () => T): T {+ return this.session.editor.getEditorState().read(fn);+ }++ /** One discrete update; partial work is discarded on throw, errors surface as+ * EditError, and the edit propagates to the live doc immediately so changes+ * (and typing) stream in as they happen. */+ private tx(fn: () => void): void {+ const before = this.session.editor.getEditorState();+ try {+ this.session.editor.update(fn, { discrete: true });+ } catch (e) {+ this.session.editor.setEditorState(before);+ this.session.editor.update(() => $updateAllNodeIds(this.session.ids), {+ discrete: true,+ });+ if (!(e instanceof Error)) throw new Error(String(e));+ if (!(e instanceof EditError)) throw new EditError(e.message);+ throw e;+ }+ this.propagate();+ }++ private block(node: NodeRef): ElementNode {+ return locate.$blockById(this.session, node);+ }++ /** Resolve a table by id, rejecting non-table ids. `setCell`/cell resolution+ * otherwise walk up to the enclosing table, so a <tr>/<td> id "works" but+ * reindexes row/col against the table — silently clobbering the wrong cell+ * (e.g. the header). Require the table id so that mistake is a loud error. */+ private requireTable(id: NodeRef): TableNode {+ const node = locate.$byId(this.session, id);+ if ($isTableNode(node)) return node;+ const what = $isTableRowNode(node)+ ? 'a <tr>'+ : $isTableCellNode(node)+ ? 'a <td>'+ : `a <${node.getType()}>`;+ throw new EditError(+ `id "${id}" is ${what}, not a table — pass the table id with the cell's row/column (both shown in the XML).`+ );+ }++ public textLength(node: NodeRef): number {+ return this.read(+ () => locate.$byId(this.session, node).getTextContent().length+ );+ }++ public locate(id: string, match: string, scope?: Scope): Match[] {+ return this.read(() => locate.$locate(this.block(id), match, scope));+ }++ public cellNode(table: string, row: number, col: number): NodeRef {+ return this.read(() => {+ const cell = this.cell(table, row, col);+ const content = cell.getChildren().find($isElementNode) ?? cell;+ const cid = $getId(content as LexicalNode);+ if (!cid)+ throw new EditError(`cell [${row}, ${col}] has no addressable content`);+ return cid;+ });+ }++ private cell(tableRef: string, row: number, col: number) {+ const table = this.requireTable(tableRef);+ const cell = table+ .getChildren()+ .filter($isTableRowNode)+ [row]?.getChildren()+ .filter($isTableCellNode)[col];+ if (!cell) throw new EditError(`no cell at [${row}, ${col}]`);+ return cell;+ }++ private insertText(node: NodeRef, at: Offset, text: string): void {+ this.tx(() => {+ const target = locate.$byId(this.session, node);+ if ($isTextNode(target)) {+ const c = target.getTextContent();+ target.setTextContent(c.slice(0, at) + text + c.slice(at));+ } else {+ insertTextAt(this.block(node), at, text);+ }+ });+ }++ private removeText(node: NodeRef, at: Offset, len: number): void {+ this.tx(() => removeTextAt(this.block(node), at, len));+ }++ private setText(node: NodeRef, text: string): void {+ this.tx(() => blocks.$setText(this.block(node), text));+ }++ private appendText(node: NodeRef, text: string): void {+ this.tx(() => inline.$appendText(this.block(node), text));+ }++ private prependText(node: NodeRef, text: string): void {+ this.tx(() => inline.$prependText(this.block(node), text));+ }++ private insertTextAfterInline(+ inlineRef: NodeRef,+ text: string,+ ref?: string+ ): void {+ this.tx(() => {+ const node = locate.$byId(this.session, inlineRef);+ const tn = $createTextNode(text);+ node.insertAfter(tn);+ if (ref !== undefined) this.assignRef(ref, tn);+ });+ }++ private replaceText(+ node: NodeRef,+ find: string,+ to: string,+ scope: Scope+ ): void {+ this.tx(() => inline.$replaceString(this.block(node), find, to, scope));+ }++ private formatText(+ node: NodeRef,+ match: string,+ format: Format,+ on: boolean,+ scope: Scope+ ): void {+ this.tx(() => {+ const block = this.block(node);+ if (on) inline.$formatTextInBlock(block, match, format, scope);+ else inline.$clearFormat(block, match, format, scope);+ });+ }++ private clearFormat(+ node: NodeRef,+ match: string | undefined,+ scope: Scope+ ): void {+ this.tx(() => {+ const block = this.block(node);+ if (match === undefined) inline.$stripFormat(block);+ else inline.$clearFormat(block, match, undefined, scope);+ });+ }++ private markText(+ node: NodeRef,+ match: string,+ on: boolean,+ scope: Scope+ ): void {+ this.tx(() => {+ if (on)+ inline.$wrapInBlock(this.block(node), match, $createMarkNode, scope);+ else inline.$unwrapFromBlock(this.block(node), match, $isMarkNode, scope);+ });+ }++ private linkText(+ node: NodeRef,+ match: string,+ url: string | null,+ scope: Scope+ ): void {+ this.tx(() => {+ if (url !== null)+ inline.$wrapInBlock(+ this.block(node),+ match,+ () => $createLinkNode(url),+ scope+ );+ else inline.$unwrapFromBlock(this.block(node), match, $isLinkNode, scope);+ });+ }++ private formatNode(node: NodeRef, format: Format, on: boolean): void {+ this.tx(() => {+ const tn = locate.$textById(this.session, node);+ const bit = FORMAT_BIT[format];+ if (tn.hasFormat(bit) !== on) tn.toggleFormat(bit);+ });+ }++ private clearNodeFormat(node: NodeRef): void {+ this.tx(() => locate.$textById(this.session, node).setFormat(0));+ }++ private setEquation(node: NodeRef, tex: string): void {+ this.tx(() =>+ modify.$modifyNode(this.session, node, { op: 'equation', tex })+ );+ }++ // yes it'session a bit of a gross type but it'session for AI and prefers it flat+ private setBlockType(+ node: NodeRef,+ block: 'paragraph' | 'heading' | 'quote' | 'code',+ opts: { level?: number; language?: string }+ ): void {+ const data: blocks.BlockData =+ block === 'heading'+ ? { type: 'heading', level: opts.level ?? 1 }+ : block === 'code'+ ? { type: 'code', language: opts.language }+ : { type: block };+ this.tx(() =>+ modify.$modifyNode(this.session, node, {+ op: 'blockType',+ block: data,+ })+ );+ }++ private setListType(nodes: NodeRef[], list: ListKind): void {+ this.tx(() => {+ const resolved = nodes.map((n) => locate.$byId(this.session, n));+ const first = resolved[0];+ // Already a list (addressed by its <ul>/<ol> id) or an item in one →+ // retype in place. $toggleList only WRAPS plain blocks into a new list;+ // handed an existing list it mangles structure or no-ops, so reserve it+ // for genuine non-list blocks (e.g. paragraphs being turned into a list).+ if (first && ($isListNode(first) || $isListItemNode(first)))+ lists.$setListType(first, list, this.session);+ else lists.$toggleList(resolved, list);+ });+ }++ private setChecked(node: NodeRef, checked: boolean): void {+ this.tx(() =>+ modify.$modifyNode(this.session, node, {+ op: 'checked',+ checked,+ })+ );+ }++ private setIndent(node: NodeRef, indent: number | 'in' | 'out'): void {+ this.tx(() =>+ modify.$modifyNode(this.session, node, { op: 'indent', indent })+ );+ }++ private appendListItem(+ ref: string,+ node: NodeRef,+ text: string,+ checked?: boolean+ ): void {+ this.tx(() => {+ const list = locate.$byId(this.session, node);+ if (!$isListNode(list)) throw new EditError(`{${node}} is not a list`);+ const li = $createListItemNode(checked ?? listItemChecked(list));+ if (text) li.append($createTextNode(text));+ list.append(li);+ this.assignRef(ref, li);+ });+ }++ private prependListItem(+ ref: string,+ node: NodeRef,+ text: string,+ checked?: boolean+ ): void {+ this.tx(() => {+ const list = locate.$byId(this.session, node);+ if (!$isListNode(list)) throw new EditError(`{${node}} is not a list`);+ const li = $createListItemNode(checked ?? listItemChecked(list));+ if (text) li.append($createTextNode(text));+ const first = list.getFirstChild();+ if (first) first.insertBefore(li);+ else list.append(li);+ this.assignRef(ref, li);+ });+ }++ private insertNode(ref: string, spec: NodeSpec, at: Position): void {+ this.tx(() => {+ // `insertNode` is for block specs; inline specs go through `insertInline`.+ if ('inline' in spec)+ throw new EditError('insertNode requires a block spec');+ let node: LexicalNode = buildNode(spec);+ // Most blocks are ElementNodes or block decorators (divider/image/video).+ // A few block specs build a structurally-inline decorator (equation) — wrap+ // it in a paragraph so it stands as its own block.+ if (node.isInline()) {+ const p = $createParagraphNode();+ p.append(node);+ node = p;+ }+ this.place(node, at);+ this.assignRef(ref, node);+ });+ }++ private insertInline(+ ref: string,+ node: NodeRef,+ at: Offset,+ spec: NodeSpec+ ): void {+ this.tx(() => {+ const block = this.block(node);+ const inline = buildNode(spec);+ insertInlineAt(block, at, inline);+ this.assignRef(ref, inline);+ });+ }++ private moveNode(node: NodeRef, at: Position): void {+ this.tx(() => {+ const block = this.block(node);+ if ('after' in at)+ blocks.$moveBlock(+ block,+ { placement: 'after', id: at.after },+ this.session+ );+ else if ('before' in at)+ blocks.$moveBlock(+ block,+ { placement: 'before', id: at.before },+ this.session+ );+ else {+ block.remove();+ if ('appendToRoot' in at) blocks.$appendBlock(block);+ else blocks.$prependBlock(block);+ }+ });+ }++ private removeNode(node: NodeRef): void {+ this.tx(() => locate.$byId(this.session, node).remove());+ }++ private mergeBlocks(nodes: NodeRef[], separator: string): void {+ this.tx(() =>+ blocks.$mergeBlocks(+ nodes.map((n) => locate.$byId(this.session, n)),+ separator+ )+ );+ }++ private insertListItemAfter(+ ref: string,+ node: NodeRef,+ text: string,+ list: ListKind+ ): void {+ this.tx(() => this.insertListItem(ref, node, text, list, 'after'));+ }++ private insertListItemBefore(+ ref: string,+ node: NodeRef,+ text: string,+ list: ListKind+ ): void {+ this.tx(() => this.insertListItem(ref, node, text, list, 'before'));+ }++ private removeListItem(node: NodeRef): void {+ this.tx(() => {+ const li = locate.$byId(this.session, node);+ if (!$isListItemNode(li))+ throw new EditError(`{${node}} is not a list item`);+ li.remove();+ });+ }++ /** Insert a new list item adjacent to an existing one. When `list` matches the+ * surrounding list it lands as a plain sibling; when it differs, the new item+ * is wrapped in a sublist of that kind (a numbered list inside a bullet list,+ * etc.) — Lexical nests via a ListItemNode that holds the child ListNode. */+ private insertListItem(+ ref: string,+ node: NodeRef,+ text: string,+ list: ListKind,+ where: 'after' | 'before'+ ): void {+ const target = locate.$byId(this.session, node);+ if (!$isListItemNode(target))+ throw new EditError(`{${node}} is not a list item`);+ const newLi = $createListItemNode(list === 'check' ? false : undefined);+ newLi.append($createTextNode(text));++ const parent = target.getParent();+ const sameKind =+ $isListNode(parent) && parent.getListType() === (list as ListType);+ if (sameKind) {+ if (where === 'after') target.insertAfter(newLi);+ else target.insertBefore(newLi);+ } else {+ const sublist = $createListNode(list as ListType);+ sublist.append(newLi);+ const host = $createListItemNode();+ host.append(sublist);+ if (where === 'after') target.insertAfter(host);+ else target.insertBefore(host);+ }+ this.assignRef(ref, newLi);+ }++ private setCell(+ table: NodeRef,+ row: number,+ col: number,+ text: string+ ): void {+ this.tx(() => tables.$setCell(this.requireTable(table), row, col, text));+ }++ private addRow(table: NodeRef, at?: number): void {+ this.tx(() => {+ const t = resolveTable(locate.$byId(this.session, table));+ const rows = t.getChildren().filter($isTableRowNode);+ const cols = rows[0]?.getChildren().filter($isTableCellNode).length ?? 1;+ const row = $createTableRowNode();+ for (let c = 0; c < cols; c++) row.append(emptyCell(false));+ if (at == null || at >= rows.length) t.append(row);+ else rows[at]!.insertBefore(row);+ });+ }++ private addColumn(table: NodeRef, at?: number): void {+ this.tx(() => {+ const t = resolveTable(locate.$byId(this.session, table));+ t.getChildren()+ .filter($isTableRowNode)+ .forEach((row, ri) => {+ const cells = row.getChildren().filter($isTableCellNode);+ const cell = emptyCell(ri === 0);+ if (at == null || at >= cells.length) row.append(cell);+ else cells[at]!.insertBefore(cell);+ });+ });+ }++ private removeRow(table: NodeRef, row: number): void {+ this.tx(() => {+ const t = resolveTable(locate.$byId(this.session, table));+ t.getChildren().filter($isTableRowNode)[row]?.remove();+ });+ }++ private removeColumn(table: NodeRef, col: number): void {+ this.tx(() => {+ const t = resolveTable(locate.$byId(this.session, table));+ for (const row of t.getChildren().filter($isTableRowNode)) {+ row.getChildren().filter($isTableCellNode)[col]?.remove();+ }+ });+ }++ private setImageAlt(node: NodeRef, alt: string): void {+ this.tx(() => {+ const n = locate.$byId(this.session, node);+ if (!(n instanceof ImageNode))+ throw new EditError(`{${node}} is not an image`);+ n.setAlt(alt);+ });+ }++ private setImageUrl(node: NodeRef, url: string): void {+ this.tx(() => {+ const n = locate.$byId(this.session, node);+ if (!(n instanceof ImageNode))+ throw new EditError(`{${node}} is not an image`);+ n.setUrl(url);+ });+ }++ private setVideoUrl(node: NodeRef, url: string): void {+ this.tx(() => {+ const n = locate.$byId(this.session, node);+ if (!(n instanceof VideoNode))+ throw new EditError(`{${node}} is not a video`);+ n.setUrl(url);+ });+ }++ private setVideoControls(node: NodeRef, controls: boolean): void {+ this.tx(() => {+ const n = locate.$byId(this.session, node);+ if (!(n instanceof VideoNode))+ throw new EditError(`{${node}} is not a video`);+ n.setControls(controls);+ });+ }++ private setDate(node: NodeRef, date: string, displayFormat?: string): void {+ this.tx(() => {+ const n = locate.$byId(this.session, node);+ if (!$isDateMentionNode(n))+ throw new EditError(`{${node}} is not a date mention`);+ n.setDate(date);+ n.setDisplayFormat(displayFormat ?? date);+ });+ }++ /** Resolve an insert anchor to a sibling node. Climbs past inline nodes (so a+ * text id resolves to its block) but stops at the first block-level node —+ * including a decorator block like a divider/image/video. `$blockById` can't+ * be used here: it only accepts ElementNodes, so an `<hr>` anchor would skip+ * past to the root and `root.insertAfter` throws. */+ private anchor(id: NodeRef): LexicalNode {+ let node = locate.$byId(this.session, id);+ while (node.isInline() && node.getParent()) {+ node = node.getParent() as LexicalNode;+ }+ return node;+ }++ private place(node: LexicalNode, at: Position): void {+ // Node-level insertion (not the ElementNode-typed $-helpers) so decorator+ // blocks — divider/image/video/equation — place correctly too.+ if ('after' in at) this.anchor(at.after).insertAfter(node);+ else if ('before' in at) this.anchor(at.before).insertBefore(node);+ else if ('appendToRoot' in at) $getRoot().append(node);+ else {+ const first = $getRoot().getFirstChild();+ if (first) first.insertBefore(node);+ else $getRoot().append(node);+ }+ }++ /** Stamp the ref onto the node as its real id (refs ARE ids), then id the rest+ * of the inserted subtree so later reads/edits resolve. */+ private assignRef(ref: string, node: LexicalNode): void {+ $setId(node, ref);+ $updateAllNodeIds(this.session.ids);+ }+}++/** A check-list item carries an (unchecked) checkbox; other list kinds don't. */+function listItemChecked(list: LexicalNode): boolean | undefined {+ return $isListNode(list) && list.getListType() === 'check'+ ? false+ : undefined;+}++function resolveTable(node: LexicalNode): TableNode {+ const t = $findMatchingParent(node, $isTableNode);+ if (!$isTableNode(t)) throw new EditError('no enclosing table');+ return t;+}++function emptyCell(header: boolean) {+ const cell = $createTableCellNode(+ header ? TableCellHeaderStates.ROW : TableCellHeaderStates.NO_STATUS+ );+ cell.append($createParagraphNode());+ return cell;+}++/** Insert plain `text` at char offset `at` within a block. Plain text inserted at+ * the edge of a FORMATTED run goes into a fresh unformatted node (so appending+ * after a bold word, or prepending before one, stays plain) rather than+ * inheriting that run'session format. Past the end appends; empty block creates a new node. */+function insertTextAt(block: ElementNode, at: Offset, text: string): void {+ const texts = block.getAllTextNodes();+ if (texts.length === 0) {+ block.append($createTextNode(text));+ return;+ }+ const total = texts.reduce((n, t) => n + t.getTextContent().length, 0);+ // prepend boundary+ if (at <= 0) {+ const first = texts[0]!;+ if (first.getFormat() !== 0) first.insertBefore($createTextNode(text));+ else first.setTextContent(text + first.getTextContent());+ return;+ }+ // append boundary+ if (at >= total) {+ const last = texts[texts.length - 1]!;+ if (last.getFormat() !== 0) last.insertAfter($createTextNode(text));+ else last.setTextContent(last.getTextContent() + text);+ return;+ }+ // interior: splice into the run that contains the offset+ let remaining = at;+ for (const tn of texts) {+ const content = tn.getTextContent();+ if (remaining <= content.length) {+ tn.setTextContent(+ content.slice(0, remaining) + text + content.slice(remaining)+ );+ return;+ }+ remaining -= content.length;+ }+}++/** Remove `len` chars starting at offset `at`, spanning text nodes as needed. */+function removeTextAt(block: ElementNode, at: Offset, len: number): void {+ let skip = at;+ let left = len;+ for (const tn of block.getAllTextNodes()) {+ if (left <= 0) break;+ const content = tn.getTextContent();+ if (skip >= content.length) {+ skip -= content.length;+ continue;+ }+ const start = skip;+ const end = Math.min(content.length, start + left);+ tn.setTextContent(content.slice(0, start) + content.slice(end));+ left -= end - start;+ skip = 0;+ }+}++/** Insert an inline node at char offset `at` within a block. */+function insertInlineAt(+ block: ElementNode,+ at: Offset,+ inline: LexicalNode+): void {+ let remaining = at;+ for (const tn of block.getAllTextNodes()) {+ const len = tn.getTextContent().length;+ if (remaining <= len) {+ if (remaining === 0) tn.insertBefore(inline);+ else if (remaining >= len) tn.insertAfter(inline);+ else {+ const [, tail] = tn.splitText(remaining);+ (tail ?? tn).insertBefore(inline);+ }+ return;+ }+ remaining -= len;+ }+ block.append(inline);+}++/** Turn a declarative spec into real Lexical nodes. */+export function buildNode(spec: NodeSpec): LexicalNode {+ return match(spec)+ .returnType<LexicalNode>()+ .with({ block: 'paragraph' }, (session) =>+ withText($createParagraphNode(), session.text)+ )+ .with({ block: 'heading' }, (session) =>+ withText(+ $createHeadingNode(`h${session.level}` as HeadingTagType),+ session.text+ )+ )+ .with({ block: 'quote' }, (session) =>+ withText($createQuoteNode(), session.text)+ )+ .with({ block: 'code' }, (session) =>+ withText($createCodeNode(session.language), session.text)+ )+ .with({ block: 'list' }, (session) => {+ const list = $createListNode(session.list as ListType);+ for (const item of session.items) {+ const li = $createListItemNode(+ session.list === 'check' ? false : undefined+ );+ li.append($createTextNode(item));+ list.append(li);+ }+ return list;+ })+ .with({ block: 'table' }, (session) => tables.$table(session.rows))+ .with({ block: 'divider' }, () => $createHorizontalRuleNode())+ .with({ block: 'document-card' }, (s) =>+ $createDocumentCardNode({+ documentId: s.documentId,+ documentName: s.documentName,+ blockName: s.blockName,+ blockParams: s.blockParams,+ })+ )+ .with({ block: 'html-render' }, (s) =>+ $createHtmlRenderNode({ html: s.html })+ )+ .with({ block: 'image' }, (session) =>+ $createImageNode({+ srcType: session.srcType,+ url: session.url,+ alt: session.alt,+ width: session.width,+ height: session.height,+ })+ )+ .with({ block: 'video' }, (session) =>+ $createVideoNode({+ srcType: session.srcType,+ url: session.url,+ controls: session.controls,+ width: session.width,+ height: session.height,+ })+ )+ .with({ block: 'equation' }, (session) =>+ $createEquationNode(session.tex, session.inline ?? false)+ )+ .with({ inline: 'linebreak' }, () => $createLineBreakNode())+ .with({ inline: 'equation' }, (session) =>+ $createEquationNode(session.tex, true)+ )+ .with({ inline: 'date' }, (session) =>+ $createDateMentionNode({+ date: session.date,+ displayFormat: session.displayFormat ?? session.date,+ })+ )+ .with({ inline: 'mention' }, (session) => {+ const m = session.mention;+ if (m.kind === 'user')+ return $createUserMentionNode({ userId: m.userId, email: m.email });+ if (m.kind === 'contact')+ return $createContactMentionNode({+ contactId: m.contactId,+ name: m.name,+ emailOrDomain: m.emailOrDomain,+ isCompany: m.isCompany,+ });+ if (m.kind === 'group')+ return $createGroupMentionNode({ groupAlias: m.groupAlias });+ return $createDocumentMentionNode({+ documentId: m.documentId,+ documentName: m.documentName,+ blockName: m.blockName,+ });+ })+ .exhaustive();+}++function withText(block: ElementNode, text?: string): ElementNode {+ if (text) block.append($createTextNode(text));+ return block;+}diff --git a/js/ai-editing-worker/src/ai-editing/doc/index.ts b/js/ai-editing-worker/src/ai-editing/doc/index.tsnew file mode 100644index 0000000000..372b2c54db--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/doc/index.ts@@ -0,0 +1,2 @@+export * from './doc';+export * from './interfaces';diff --git a/js/ai-editing-worker/src/ai-editing/doc/interfaces.ts b/js/ai-editing-worker/src/ai-editing/doc/interfaces.tsnew file mode 100644index 0000000000..1736a114d4--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/doc/interfaces.ts@@ -0,0 +1,24 @@+/**+ * The two views onto the document. `Doc` (doc/doc.ts) implements both over a real+ * Lexical session; the planner (`DocumentOpQueue.step`) is handed only `DocReader`+ * so it can read to plan but never mutate, and the executor is handed `DocWriter`.+ * Mocks of these (with no Lexical) drive the pure tests.+ */+import type { DocumentOp, NodeId, NodeRef, Offset, Scope } from '../editor';++/** One located occurrence of a substring: the text node holding it and the span. */+export type Match = { node: NodeRef; start: Offset; end: Offset };++export interface DocReader {+ /** Every occurrence of `match` within block `id` (honoring `scope`), as text+ * node + offsets — used to animate per-occurrence selection. */+ locate(id: NodeId, match: string, scope?: Scope): Match[];+ /** Length of a node's plain-text content (block id or text-node id). */+ textLength(node: NodeRef): number;+ /** The content block node of a table cell (header is row 0). */+ cellNode(table: NodeId, row: number, col: number): NodeRef;+}++export interface DocWriter {+ apply(op: DocumentOp): void;+}diff --git a/js/ai-editing-worker/src/ai-editing/editor/document-editor.test.ts b/js/ai-editing-worker/src/ai-editing/editor/document-editor.test.tsnew file mode 100644index 0000000000..179b44e89c--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/editor/document-editor.test.ts@@ -0,0 +1,664 @@+import { describe, expect, it } from 'vitest';+import { DocumentEditor } from './document-editor';+import { EditError } from './errors';+import type { DocumentOp } from './ops';++/** Build an editor over a fixed set of valid ids. */+function ed(...ids: string[]): DocumentEditor {+ return new DocumentEditor({+ validIds: ids.length ? ids : ['b1', 'b2', 'b3', 't1'],+ refs: Array.from({ length: 32 }, (_, i) => `ref-${i + 1}`),+ });+}++describe('DocumentEditor — inline formatting → ops', () => {+ it('bold collapses to a formatText op (defaults to all + on)', () => {+ expect(ed().bold('b1', 'frog').drain()).toEqual<DocumentOp[]>([+ {+ kind: 'formatText',+ node: 'b1',+ match: 'frog',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ ]);+ });++ it('all six sugar methods map to their format with on=true', () => {+ const ops = ed()+ .italic('b1', 'a')+ .underline('b1', 'b')+ .strike('b1', 'c')+ .inlineCode('b1', 'd')+ .drain();+ expect(ops.map((o) => (o as any).format)).toEqual([+ 'italic',+ 'underline',+ 'strike',+ 'code',+ ]);+ expect(ops.every((o) => (o as any).on === true)).toBe(true);+ });++ it('un* methods set on=false', () => {+ expect(ed().unbold('b1', 'x').drain()[0]).toMatchObject({+ format: 'bold',+ on: false,+ });+ });++ it('passes through an explicit scope', () => {+ expect(+ ed().bold('b1', 'x', { kind: 'nth', n: 2 }).drain()[0]+ ).toMatchObject({+ scope: { kind: 'nth', n: 2 },+ });+ });++ it('highlight / unhighlight → markText', () => {+ expect(ed().highlight('b1', 'x').drain()[0]).toMatchObject({+ kind: 'markText',+ on: true,+ });+ expect(ed().unhighlight('b1', 'x').drain()[0]).toMatchObject({+ kind: 'markText',+ on: false,+ });+ });++ it('link / unlink → linkText (unlink uses url=null)', () => {+ expect(ed().link('b1', 'x', 'http://a').drain()[0]).toMatchObject({+ kind: 'linkText',+ url: 'http://a',+ });+ expect(ed().unlink('b1', 'x').drain()[0]).toMatchObject({+ kind: 'linkText',+ url: null,+ });+ });++ it('clearFormat with and without a match', () => {+ expect(ed().clearFormat('b1', 'x').drain()[0]).toEqual({+ kind: 'clearFormat',+ node: 'b1',+ match: 'x',+ scope: { kind: 'all' },+ });+ expect(ed().clearAllFormat('b1').drain()[0]).toEqual({+ kind: 'clearFormat',+ node: 'b1',+ match: undefined,+ scope: { kind: 'all' },+ });+ });++ it('formatNode / clearNodeFormat target a text-node id directly', () => {+ expect(ed().boldNode('t1').drain()[0]).toEqual({+ kind: 'formatNode',+ node: 't1',+ format: 'bold',+ on: true,+ });+ expect(ed().clearNodeFormat('t1').drain()[0]).toEqual({+ kind: 'clearNodeFormat',+ node: 't1',+ });+ });+});++describe('DocumentEditor — text / block / list → ops', () => {+ it('setText / replace / append / prepend', () => {+ expect(ed().setText('b1', 'hi').drain()[0]).toEqual({+ kind: 'setText',+ node: 'b1',+ text: 'hi',+ });+ expect(ed().replace('b1', 'a', 'b').drain()[0]).toEqual({+ kind: 'replaceText',+ node: 'b1',+ find: 'a',+ to: 'b',+ scope: { kind: 'all' },+ });+ expect(ed().appendText('b1', '!').drain()[0]).toEqual({+ kind: 'appendText',+ node: 'b1',+ text: '!',+ });+ expect(ed().prependText('b1', '>').drain()[0]).toEqual({+ kind: 'prependText',+ node: 'b1',+ text: '>',+ });+ });++ it('block type methods', () => {+ expect(ed().convertToParagraph('b1').drain()[0]).toEqual({+ kind: 'setBlockType',+ node: 'b1',+ block: 'paragraph',+ });+ expect(ed().convertToHeading('b1', 2).drain()[0]).toEqual({+ kind: 'setBlockType',+ node: 'b1',+ block: 'heading',+ level: 2,+ });+ expect(ed().convertToQuote('b1').drain()[0]).toEqual({+ kind: 'setBlockType',+ node: 'b1',+ block: 'quote',+ });+ expect(ed().convertToCodeBlock('b1', 'ts').drain()[0]).toEqual({+ kind: 'setBlockType',+ node: 'b1',+ block: 'code',+ language: 'ts',+ });+ expect(ed().setLanguage('b1', 'python').drain()[0]).toEqual({+ kind: 'setBlockType',+ node: 'b1',+ block: 'code',+ language: 'python',+ });+ });++ it('list toggles accept a single id or a list of ids', () => {+ expect(ed().bulletList('b1').drain()[0]).toEqual({+ kind: 'setListType',+ nodes: ['b1'],+ list: 'bullet',+ });+ expect(ed('b1', 'b2').numberedList(['b1', 'b2']).drain()[0]).toEqual({+ kind: 'setListType',+ nodes: ['b1', 'b2'],+ list: 'number',+ });+ expect(ed().checklist('b1').drain()[0]).toMatchObject({ list: 'check' });+ });++ it('check / uncheck → setChecked', () => {+ expect(ed().check('b1').drain()[0]).toEqual({+ kind: 'setChecked',+ node: 'b1',+ checked: true,+ });+ expect(ed().uncheck('b1').drain()[0]).toEqual({+ kind: 'setChecked',+ node: 'b1',+ checked: false,+ });+ });++ it('indent / outdent are relative, setIndent is absolute', () => {+ expect(ed().indent('b1').drain()[0]).toEqual({+ kind: 'setIndent',+ node: 'b1',+ indent: 'in',+ });+ expect(ed().outdent('b1').drain()[0]).toEqual({+ kind: 'setIndent',+ node: 'b1',+ indent: 'out',+ });+ expect(ed().setIndent('b1', 2).drain()[0]).toEqual({+ kind: 'setIndent',+ node: 'b1',+ indent: 2,+ });+ });++ it('insertListItemAfter defaults to a bullet item and returns a usable ref', () => {+ const e = ed();+ const ref = e.insertListItemAfter('b1', 'next');+ expect(ref).toBe('ref-1'); // first mint of a fresh editor's id pool+ e.setText(ref, 'NEXT'); // would throw if ref were not registered valid+ const ops = e.drain();+ expect(ops).toMatchObject([+ {+ kind: 'insertListItemAfter',+ ref: 'ref-1',+ node: 'b1',+ text: 'next',+ list: 'bullet',+ },+ { kind: 'setText', node: 'ref-1' },+ ]);+ });++ it('insertListItemBefore carries an explicit list kind for nesting', () => {+ const e = ed();+ const ref = e.insertListItemBefore('b1', 'sub', 'number');+ expect(ref).toBe('ref-1'); // first mint of a fresh editor's id pool+ expect(e.drain()[0]).toEqual({+ kind: 'insertListItemBefore',+ ref: 'ref-1',+ node: 'b1',+ text: 'sub',+ list: 'number',+ });+ });++ it('removeListItem targets a single item', () => {+ expect(ed().removeListItem('b1').drain()[0]).toEqual({+ kind: 'removeListItem',+ node: 'b1',+ });+ });+});++describe('DocumentEditor — structure & refs', () => {+ it('insertParagraphAfter returns a ref that is then a valid target', () => {+ const e = ed();+ const ref = e.insertParagraphAfter('b1', 'Intro');+ e.bold(ref, 'Intro'); // would throw if ref were not registered valid+ const ops = e.drain();+ expect(ops).toMatchObject([+ {+ kind: 'insertNode',+ ref,+ spec: { block: 'paragraph', text: 'Intro' },+ at: { after: 'b1' },+ },+ { kind: 'formatText', node: ref },+ ]);+ });++ it('append/prepend block go to root', () => {+ const a = ed();+ a.appendParagraph('x');+ expect(a.drain()[0]).toMatchObject({ at: { appendToRoot: true } });+ const p = ed();+ p.prependParagraph('x');+ expect(p.drain()[0]).toMatchObject({ at: { prependToRoot: true } });+ });++ it('move / remove / removeMany / merge', () => {+ expect(ed().move('b1', { before: 'b2' }).drain()[0]).toEqual({+ kind: 'moveNode',+ node: 'b1',+ at: { before: 'b2' },+ });+ expect(ed().remove('b1').drain()[0]).toEqual({+ kind: 'removeNode',+ node: 'b1',+ });+ expect(ed('b1', 'b2').removeMany(['b1', 'b2']).drain()).toHaveLength(2);+ expect(ed('b1', 'b2').merge(['b1', 'b2'], ' — ').drain()[0]).toEqual({+ kind: 'mergeBlocks',+ nodes: ['b1', 'b2'],+ separator: ' — ',+ });+ });++ it('tables: insert an EMPTY grid, then a setCell per non-empty cell (human-like fill)', () => {+ const e = ed();+ const t = e.appendTable([+ ['A', 'B'],+ ['c', ''],+ ]); // 2x2, one empty cell+ const ops = e.drain();+ // empty grid first (same shape, blank cells), then one setCell per NON-empty cell+ expect(ops).toEqual([+ expect.objectContaining({+ kind: 'insertNode',+ spec: {+ block: 'table',+ rows: [+ ['', ''],+ ['', ''],+ ],+ },+ }),+ { kind: 'setCell', table: t, row: 0, col: 0, text: 'A' },+ { kind: 'setCell', table: t, row: 0, col: 1, text: 'B' },+ { kind: 'setCell', table: t, row: 1, col: 0, text: 'c' },+ ]);+ expect(ed().addRow('b1').drain()[0]).toEqual({+ kind: 'addRow',+ table: 'b1',+ at: undefined,+ });+ expect(ed().removeColumn('b1', 2).drain()[0]).toEqual({+ kind: 'removeColumn',+ table: 'b1',+ col: 2,+ });+ });++ it('media / inline creators', () => {+ const e = ed();+ const img = e.insertImage('b1', {+ srcType: 'url',+ url: 'http://i',+ alt: 'a',+ });+ expect(typeof img).toBe('string');+ expect(e.drain()[0]).toMatchObject({+ kind: 'insertNode',+ spec: { block: 'image', srcType: 'url', url: 'http://i', alt: 'a' },+ });+ });++ it('insertInline creators produce insertInline ops at the offset', () => {+ const e = ed();+ e.insertLineBreak('b1', 4);+ expect(e.drain()[0]).toMatchObject({+ kind: 'insertInline',+ node: 'b1',+ at: 4,+ spec: { inline: 'linebreak' },+ });+ });+});++describe('DocumentEditor — eager validation (EditError)', () => {+ it('throws on an unknown id immediately', () => {+ expect(() => ed().bold('nope', 'x')).toThrow(EditError);+ expect(() => ed().setText('nope', 'x')).toThrow(/unknown id "nope"/);+ });++ it('throws when an insert position references an unknown id', () => {+ expect(() => ed().insertParagraphAfter('nope')).toThrow(EditError);+ });++ it('throws on empty match / find', () => {+ expect(() => ed().bold('b1', '')).toThrow(/match string is empty/);+ expect(() => ed().replace('b1', '', 'x')).toThrow(/find string is empty/);+ });++ it('throws on out-of-range heading level', () => {+ expect(() => ed().convertToHeading('b1', 0)).toThrow(/1-6/);+ expect(() => ed().convertToHeading('b1', 7)).toThrow(/1-6/);+ });++ it('throws on negative indent / cell indices / empty list & merge', () => {+ expect(() => ed().setIndent('b1', -1)).toThrow(/>= 0/);+ expect(() => ed().setCell('b1', -1, 0, 'x')).toThrow(/>= 0/);+ expect(() => ed().bulletList([])).toThrow(/at least one/);+ expect(() => ed().merge(['b1'])).toThrow(/at least two/);+ });++ it('does NOT throw partway — nothing is applied, ops just accumulate until the throw', () => {+ const e = ed();+ e.bold('b1', 'ok');+ expect(() => e.bold('nope', 'x')).toThrow();+ // the first op is still captured; the bad call added nothing+ expect(e.drain()).toHaveLength(1);+ });+});++describe('DocumentEditor — mention methods push insertInline ops', () => {+ it('every mention method pushes an insertInline op with the right mention kind', () => {+ const eu = ed();+ eu.mentionUser('b1', 0, { userId: 'u', email: 'e' });+ expect(eu.drain()[0]).toMatchObject({+ kind: 'insertInline',+ spec: {+ inline: 'mention',+ mention: { kind: 'user', userId: 'u', email: 'e' },+ },+ });+ const eg = ed();+ eg.mentionGroup('b1', 0, { groupAlias: 'g' });+ expect(eg.drain()[0]).toMatchObject({+ kind: 'insertInline',+ spec: { inline: 'mention', mention: { kind: 'group', groupAlias: 'g' } },+ });+ const ec = ed();+ ec.mentionContact('b1', 0, {+ contactId: 'c',+ name: 'n',+ emailOrDomain: 'd',+ isCompany: false,+ });+ expect(ec.drain()[0]).toMatchObject({+ kind: 'insertInline',+ spec: { inline: 'mention', mention: { kind: 'contact' } },+ });+ const ed2 = ed();+ ed2.mentionDocument('b1', 0, {+ documentId: 'd',+ documentName: 'n',+ blockName: 'b',+ });+ expect(ed2.drain()[0]).toMatchObject({+ kind: 'insertInline',+ spec: { inline: 'mention', mention: { kind: 'document' } },+ });+ });+});++describe('DocumentEditor — drain semantics', () => {+ it('chaining accumulates in order and drain resets', () => {+ const e = ed();+ e.bold('b1', 'a').italic('b2', 'b').remove('b3');+ const first = e.drain();+ expect(first.map((o) => o.kind)).toEqual([+ 'formatText',+ 'formatText',+ 'removeNode',+ ]);+ expect(e.drain()).toEqual([]); // drained+ });+});++describe('DocumentEditor — insert position validation', () => {+ it('insertParagraphBefore validates the anchor id', () => {+ expect(() => ed().insertParagraphBefore('nope')).toThrow(+ /unknown id "nope"/+ );+ });+ it('appendToRoot / prependToRoot need no anchor', () => {+ expect(() => ed().appendParagraph('x')).not.toThrow();+ expect(() => ed().prependParagraph('x')).not.toThrow();+ expect(() => ed().appendBlock({ block: 'divider' })).not.toThrow();+ expect(() => ed().prependBlock({ block: 'divider' })).not.toThrow();+ });+ it('move validates BOTH the moved id and the destination anchor', () => {+ expect(() => ed().move('nope', { after: 'b1' })).toThrow(/nope/);+ expect(() => ed().move('b1', { after: 'nope' })).toThrow(/nope/);+ expect(() => ed().move('b1', { appendToRoot: true })).not.toThrow();+ });+});++describe('DocumentEditor — match/find validation', () => {+ it('empty match throws for every match-taking method', () => {+ expect(() => ed().format('b1', '', 'bold')).toThrow(+ /match string is empty/+ );+ expect(() => ed().highlight('b1', '')).toThrow(/match string is empty/);+ expect(() => ed().unhighlight('b1', '')).toThrow(/match string is empty/);+ expect(() => ed().link('b1', '', 'http://a')).toThrow(+ /match string is empty/+ );+ expect(() => ed().unlink('b1', '')).toThrow(/match string is empty/);+ });+ it('clearFormat tolerates an omitted match but rejects an empty one', () => {+ expect(() => ed().clearFormat('b1')).not.toThrow();+ expect(() => ed().clearFormat('b1', '')).toThrow(/match string is empty/);+ });+ it('replace rejects an empty find', () => {+ expect(() => ed().replace('b1', '', 'x')).toThrow(/find string is empty/);+ });+});++describe('DocumentEditor — heading level bounds', () => {+ it('accepts 1..6 and rejects 0 and 7', () => {+ for (let lvl = 1; lvl <= 6; lvl++) {+ expect(ed().convertToHeading('b1', lvl).drain()[0]).toMatchObject({+ kind: 'setBlockType',+ block: 'heading',+ level: lvl,+ });+ }+ expect(() => ed().convertToHeading('b1', 0)).toThrow(/1-6/);+ expect(() => ed().convertToHeading('b1', 7)).toThrow(/1-6/);+ expect(() => ed().convertToHeading('b1', -1)).toThrow(/1-6/);+ });+});++describe('DocumentEditor — code block language validation', () => {+ it('requires a non-empty language for code block conversion and insertion', () => {+ expect(() => ed().convertToCodeBlock('b1', '')).toThrow(+ /language is required/+ );+ expect(() =>+ ed().convertToCodeBlock('b1', undefined as unknown as string)+ ).toThrow(/language is required/);+ expect(() => ed().setLanguage('b1', '')).toThrow(/language is required/);+ expect(() =>+ ed().setLanguage('b1', undefined as unknown as string)+ ).toThrow(/language is required/);+ expect(() => ed().insertCodeBlockAfter('b1', '')).toThrow(+ /language is required/+ );+ expect(() =>+ ed().insertCodeBlockAfter('b1', undefined as unknown as string)+ ).toThrow(/language is required/);+ });+});++describe('DocumentEditor — indent & cell bounds', () => {+ it('setIndent rejects negatives, allows 0 and positive', () => {+ expect(() => ed().setIndent('b1', -1)).toThrow(/>= 0/);+ expect(ed().setIndent('b1', 0).drain()[0]).toEqual({+ kind: 'setIndent',+ node: 'b1',+ indent: 0,+ });+ expect(ed().setIndent('b1', 3).drain()[0]).toEqual({+ kind: 'setIndent',+ node: 'b1',+ indent: 3,+ });+ });+ it('indent maps any nonneg "by" to "in" and any negative to "out"', () => {+ expect(ed().indent('b1', 5).drain()[0]).toMatchObject({ indent: 'in' });+ expect(ed().indent('b1', 0).drain()[0]).toMatchObject({ indent: 'in' }); // 0 >= 0+ expect(ed().indent('b1', -2).drain()[0]).toMatchObject({ indent: 'out' });+ expect(ed().outdent('b1', 3).drain()[0]).toMatchObject({ indent: 'out' });+ });+ it('setCell / removeRow / removeColumn reject negative indices', () => {+ expect(() => ed().setCell('b1', -1, 0, 'x')).toThrow(/>= 0/);+ expect(() => ed().setCell('b1', 0, -1, 'x')).toThrow(/>= 0/);+ expect(() => ed().removeRow('b1', -1)).toThrow(/>= 0/);+ expect(() => ed().removeColumn('b1', -1)).toThrow(/>= 0/);+ expect(ed().setCell('b1', 0, 0, 'x').drain()[0]).toMatchObject({+ kind: 'setCell',+ row: 0,+ col: 0,+ });+ });+});++describe('DocumentEditor — list & merge cardinality', () => {+ it('empty list throws', () => {+ expect(() => ed().bulletList([])).toThrow(/at least one/);+ expect(() => ed().numberedList([])).toThrow(/at least one/);+ expect(() => ed().checklist([])).toThrow(/at least one/);+ });+ it('merge requires two or more', () => {+ expect(() => ed().merge([])).toThrow(/at least two/);+ expect(() => ed('b1').merge(['b1'])).toThrow(/at least two/);+ expect(ed('b1', 'b2').merge(['b1', 'b2']).drain()[0]).toMatchObject({+ kind: 'mergeBlocks',+ });+ });+ it('merge forwards an explicit separator verbatim (even empty)', () => {+ expect(ed('b1', 'b2').merge(['b1', 'b2'], '\n').drain()[0]).toMatchObject({+ separator: '\n',+ });+ expect(ed('b1', 'b2').merge(['b1', 'b2'], '').drain()[0]).toMatchObject({+ separator: '',+ });+ });+});++describe('DocumentEditor — inline offset validation', () => {+ it('negative inline offset throws', () => {+ expect(() => ed().insertLineBreak('b1', -1)).toThrow(+ /inline offset must be >= 0/+ );+ expect(() => ed().insertInlineEquation('b1', -5, 'x')).toThrow(+ /inline offset must be >= 0/+ );+ });+ it('offset 0 is allowed', () => {+ const e = ed();+ e.insertLineBreak('b1', 0);+ expect(e.drain()[0]).toMatchObject({ kind: 'insertInline', at: 0 });+ });+});++describe('DocumentEditor — ref minted by a creator is a valid later target', () => {+ it('insertParagraphAfter ref accepted by format, append, list, table builders', () => {+ const e = ed();+ const ref = e.insertParagraphAfter('b1', 'Intro');+ e.bold(ref, 'Intro');+ e.appendText(ref, '!');+ e.convertToHeading(ref, 1);+ const ops = e.drain();+ expect(ops.map((o) => o.kind)).toEqual([+ 'insertNode',+ 'formatText',+ 'appendText',+ 'setBlockType',+ ]);+ });++ it('a table ref is a valid target for setCell/addRow/addColumn', () => {+ const e = ed();+ const t = e.appendTable([['', '']]); // empty grid → just an insertNode, no auto setCell+ e.setCell(t, 0, 0, 'x');+ e.addRow(t);+ e.addColumn(t);+ expect(e.drain().map((o) => o.kind)).toEqual([+ 'insertNode',+ 'setCell',+ 'addRow',+ 'addColumn',+ ]);+ });++ it('an inline ref (insertLineBreak) is registered valid too', () => {+ const e = ed();+ const r = e.insertLineBreak('b1', 0);+ expect(() => e.bold(r, 'x')).not.toThrow();+ });+});++describe('DocumentEditor — scope defaults', () => {+ it('an explicit scope overrides the default', () => {+ expect(+ ed().bold('b1', 'x', { kind: 'nth', n: 3 }).drain()[0]+ ).toMatchObject({+ scope: { kind: 'nth', n: 3 },+ });+ });+});++describe('DocumentEditor — mention methods require a valid block id', () => {+ it('each mention method throws EditError for an unknown id', () => {+ expect(() =>+ ed().mentionUser('nope', 0, { userId: 'u', email: 'e' })+ ).toThrow(/nope/);+ expect(() => ed().mentionGroup('nope', 0, { groupAlias: 'g' })).toThrow(+ /nope/+ );+ });+});++describe('DocumentEditor — removeMany & accumulation', () => {+ it('removeMany validates every id before pushing each', () => {+ expect(() => ed('b1', 'b2').removeMany(['b1', 'nope'])).toThrow(/nope/);+ });+ it('removeMany of all valid ids pushes one removeNode per id in order', () => {+ const ops = ed('b1', 'b2', 'b3').removeMany(['b3', 'b1']).drain();+ expect(ops).toEqual([+ { kind: 'removeNode', node: 'b3' },+ { kind: 'removeNode', node: 'b1' },+ ]);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/editor/document-editor.ts b/js/ai-editing-worker/src/ai-editing/editor/document-editor.tsnew file mode 100644index 0000000000..1f7737c35d--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/editor/document-editor.ts@@ -0,0 +1,657 @@+// IMPORTANT: this class runs inside the QuickJS sandbox via a generated bundle.+// If you change it, regenerate the sandbox (`bun scripts/generate-sandbox.ts`)+// or the AI's editor surface won't reflect your edits.+import { EditError } from './errors';+import type {+ DocumentOp,+ Format,+ ListKind,+ MentionSpec,+ NodeId,+ NodeRef,+ NodeSpec,+ Position,+ Ref,+ Scope,+} from './ops';++type SetBlockTypeArgs =+ | { block: 'paragraph' }+ | { block: 'heading'; level: number }+ | { block: 'quote' }+ | { block: 'code'; language: string };++export type DocumentEditorOptions = {+ /** Durable ids present in the document the model was shown. */+ validIds: Iterable<NodeId>;+ /** A pool of globally-unique ids the host pre-generated (the sandbox has no+ * good entropy of its own). Each insert pops one and stamps it as the new+ * node's durable id, so inserted ids never collide — across concurrent+ * writers, repeated runs, or sessions. */+ refs: string[];+};++export class DocumentEditor {+ private ops: DocumentOp[] = [];+ private valid: Set<string>;+ private refs: string[];+ private refIdx = 0;++ constructor(opts: DocumentEditorOptions) {+ this.valid = new Set(opts.validIds);+ this.refs = opts.refs;+ }++ private push(op: DocumentOp): this {+ this.ops.push(op);+ return this;+ }++ private requireId(id: NodeId): void {+ if (!this.valid.has(id)) {+ throw new EditError(`unknown id "${id}"`);+ }+ }++ private requireMatch(match: string): void {+ if (match.length === 0) throw new EditError('match string is empty');+ }++ private mintRef(): Ref {+ const ref = this.refs[this.refIdx++];+ if (!ref) {+ throw new EditError(+ 'too many inserts in one snippet (id pool exhausted)'+ );+ }+ this.valid.add(ref);+ return ref;+ }++ /** Hand the accumulated ops to the queue and reset. */+ public drain(): DocumentOp[] {+ const out = this.ops;+ this.ops = [];+ return out;+ }++ public format(+ id: NodeId,+ match: string,+ format: Format,+ on = true,+ scope: Scope = { kind: 'all' }+ ): this {+ this.requireId(id);+ this.requireMatch(match);+ return this.push({+ kind: 'formatText',+ node: id,+ match,+ format,+ on,+ scope,+ });+ }+ public bold(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'bold', true, scope);+ }++ public italic(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'italic', true, scope);+ }++ public underline(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'underline', true, scope);+ }++ public strike(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'strike', true, scope);+ }++ public inlineCode(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'code', true, scope);+ }++ public unbold(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'bold', false, scope);+ }++ public unitalic(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'italic', false, scope);+ }++ public ununderline(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'underline', false, scope);+ }++ public unstrike(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'strike', false, scope);+ }++ public uninlineCode(id: NodeId, match: string, scope?: Scope): this {+ return this.format(id, match, 'code', false, scope);+ }++ /** Strip all inline formatting from matched substrings (or the whole block if+ * `match` is omitted). */+ public clearFormat(+ id: NodeId,+ match?: string,+ scope: Scope = { kind: 'all' }+ ): this {+ this.requireId(id);+ if (match !== undefined) this.requireMatch(match);+ return this.push({ kind: 'clearFormat', node: id, match, scope });+ }+ public clearAllFormat(id: NodeId): this {+ return this.clearFormat(id);+ }++ public highlight(+ id: NodeId,+ match: string,+ scope: Scope = { kind: 'all' }+ ): this {+ this.requireId(id);+ this.requireMatch(match);+ return this.push({ kind: 'markText', node: id, match, on: true, scope });+ }+ public unhighlight(+ id: NodeId,+ match: string,+ scope: Scope = { kind: 'all' }+ ): this {+ this.requireId(id);+ this.requireMatch(match);+ return this.push({ kind: 'markText', node: id, match, on: false, scope });+ }++ public link(+ id: NodeId,+ match: string,+ url: string,+ scope: Scope = { kind: 'all' }+ ): this {+ this.requireId(id);+ this.requireMatch(match);+ return this.push({ kind: 'linkText', node: id, match, url, scope });+ }+ public unlink(+ id: NodeId,+ match: string,+ scope: Scope = { kind: 'all' }+ ): this {+ this.requireId(id);+ this.requireMatch(match);+ return this.push({ kind: 'linkText', node: id, match, url: null, scope });+ }++ public formatNode(node: NodeId, format: Format, on = true): this {+ this.requireId(node);+ return this.push({ kind: 'formatNode', node, format, on });+ }+ public boldNode(node: NodeId): this {+ return this.formatNode(node, 'bold');+ }++ public italicNode(node: NodeId): this {+ return this.formatNode(node, 'italic');+ }++ public underlineNode(node: NodeId): this {+ return this.formatNode(node, 'underline');+ }++ public strikeNode(node: NodeId): this {+ return this.formatNode(node, 'strike');+ }++ public codeNode(node: NodeId): this {+ return this.formatNode(node, 'code');+ }++ public clearNodeFormat(node: NodeId): this {+ this.requireId(node);+ return this.push({ kind: 'clearNodeFormat', node });+ }++ public setText(id: NodeId, text: string): this {+ this.requireId(id);+ return this.push({ kind: 'setText', node: id, text });+ }+ public setEquation(id: NodeId, tex: string): this {+ this.requireId(id);+ return this.push({ kind: 'setEquation', node: id, tex });+ }+ public replace(+ id: NodeId,+ find: string,+ to: string,+ scope: Scope = { kind: 'all' }+ ): this {+ this.requireId(id);+ if (find.length === 0) throw new EditError('find string is empty');+ return this.push({ kind: 'replaceText', node: id, find, to, scope });+ }+ public appendText(id: NodeId, text: string): this {+ this.requireId(id);+ return this.push({ kind: 'appendText', node: id, text });+ }+ public prependText(id: NodeId, text: string): this {+ this.requireId(id);+ return this.push({ kind: 'prependText', node: id, text });+ }++ private setBlockType(id: NodeId, args: SetBlockTypeArgs): this {+ this.requireId(id);+ return this.push({ kind: 'setBlockType', node: id, ...args });+ }++ private requireLanguage(language: string): void {+ if (typeof language !== 'string' || language.trim().length === 0)+ throw new EditError('code block language is required');+ }++ public convertToParagraph(id: NodeId): this {+ return this.setBlockType(id, { block: 'paragraph' });+ }++ public convertToHeading(id: NodeId, level: number): this {+ if (level < 1 || level > 6)+ throw new EditError(`heading level must be 1-6, got ${level}`);+ return this.setBlockType(id, { block: 'heading', level });+ }++ public convertToQuote(id: NodeId): this {+ return this.setBlockType(id, { block: 'quote' });+ }++ public convertToCodeBlock(id: NodeId, language: string): this {+ this.requireLanguage(language);+ return this.setBlockType(id, { block: 'code', language });+ }++ public setLanguage(id: NodeId, language: string): this {+ return this.convertToCodeBlock(id, language);+ }++ private toList(idOrIds: NodeId | NodeId[], list: ListKind): this {+ const ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds];+ if (ids.length === 0)+ throw new EditError('list requires at least one block');+ for (const id of ids) this.requireId(id);+ return this.push({ kind: 'setListType', nodes: ids, list });+ }+ public bulletList(idOrIds: NodeId | NodeId[]): this {+ return this.toList(idOrIds, 'bullet');+ }++ public numberedList(idOrIds: NodeId | NodeId[]): this {+ return this.toList(idOrIds, 'number');+ }++ public checklist(idOrIds: NodeId | NodeId[]): this {+ return this.toList(idOrIds, 'check');+ }++ public setListType(id: NodeId, list: ListKind): this {+ return this.toList(id, list);+ }++ public check(id: NodeId): this {+ return this.setChecked(id, true);+ }++ public uncheck(id: NodeId): this {+ return this.setChecked(id, false);+ }++ public setChecked(id: NodeId, checked: boolean): this {+ this.requireId(id);+ return this.push({ kind: 'setChecked', node: id, checked });+ }++ public indent(id: NodeId, by = 1): this {+ this.requireId(id);+ return this.push({+ kind: 'setIndent',+ node: id,+ indent: by >= 0 ? 'in' : 'out',+ });+ }+ public outdent(id: NodeId, by = 1): this {+ return this.indent(id, -Math.abs(by));+ }++ public setIndent(id: NodeId, level: number): this {+ this.requireId(id);+ if (level < 0)+ throw new EditError(`indent level must be >= 0, got ${level}`);+ return this.push({ kind: 'setIndent', node: id, indent: level });+ }+ public insertListItemAfter(+ id: NodeId,+ text: string,+ list: ListKind = 'bullet'+ ): Ref {+ this.requireId(id);+ const ref = this.mintRef();+ this.push({ kind: 'insertListItemAfter', ref, node: id, text, list });+ return ref;+ }++ public insertListItemBefore(+ id: NodeId,+ text: string,+ list: ListKind = 'bullet'+ ): Ref {+ this.requireId(id);+ const ref = this.mintRef();+ this.push({ kind: 'insertListItemBefore', ref, node: id, text, list });+ return ref;+ }++ /** Append a new item to the end of a list, addressed by the list's own+ * `<ul>`/`<ol>` id — works even when the list is empty (unlike+ * `insertListItemAfter`, which needs an existing sibling item). */+ public appendListItem(listId: NodeId, text: string): Ref {+ this.requireId(listId);+ const ref = this.mintRef();+ this.push({ kind: 'appendListItem', ref, node: listId, text });+ return ref;+ }++ /** Prepend a new item to the start of a list, addressed by the list's own+ * `<ul>`/`<ol>` id — works even when the list is empty. */+ public prependListItem(listId: NodeId, text: string): Ref {+ this.requireId(listId);+ const ref = this.mintRef();+ this.push({ kind: 'prependListItem', ref, node: listId, text });+ return ref;+ }++ public removeListItem(id: NodeId): this {+ this.requireId(id);+ return this.push({ kind: 'removeListItem', node: id });+ }++ private insert(spec: NodeSpec, at: Position): Ref {+ this.requireAt(at);+ const ref = this.mintRef();+ this.push({ kind: 'insertNode', ref, spec, at });+ return ref;+ }+ private requireAt(at: Position): void {+ if ('after' in at) this.requireId(at.after);+ else if ('before' in at) this.requireId(at.before);+ }++ public insertParagraphAfter(id: NodeId, text = ''): Ref {+ return this.insert({ block: 'paragraph', text }, { after: id });+ }++ public insertParagraphBefore(id: NodeId, text = ''): Ref {+ return this.insert({ block: 'paragraph', text }, { before: id });+ }++ public insertHeadingAfter(+ id: NodeId,+ level: 1 | 2 | 3 | 4 | 5 | 6,+ text = ''+ ): Ref {+ return this.insert({ block: 'heading', level, text }, { after: id });+ }++ public insertHeadingBefore(+ id: NodeId,+ level: 1 | 2 | 3 | 4 | 5 | 6,+ text = ''+ ): Ref {+ return this.insert({ block: 'heading', level, text }, { before: id });+ }++ public insertQuoteAfter(id: NodeId, text = ''): Ref {+ return this.insert({ block: 'quote', text }, { after: id });+ }++ public insertCodeBlockAfter(id: NodeId, language: string, text = ''): Ref {+ this.requireLanguage(language);+ return this.insert({ block: 'code', language, text }, { after: id });+ }++ public insertBlockAfter(id: NodeId, spec: NodeSpec): Ref {+ return this.insert(spec, { after: id });+ }++ public insertBlockBefore(id: NodeId, spec: NodeSpec): Ref {+ return this.insert(spec, { before: id });+ }++ public appendParagraph(text = ''): Ref {+ return this.insert({ block: 'paragraph', text }, { appendToRoot: true });+ }++ public prependParagraph(text = ''): Ref {+ return this.insert({ block: 'paragraph', text }, { prependToRoot: true });+ }++ public appendBlock(spec: NodeSpec): Ref {+ return this.insert(spec, { appendToRoot: true });+ }++ public prependBlock(spec: NodeSpec): Ref {+ return this.insert(spec, { prependToRoot: true });+ }++ public move(id: NodeId, at: Position): this {+ this.requireId(id);+ this.requireAt(at);+ return this.push({ kind: 'moveNode', node: id, at });+ }+ public remove(id: NodeId): this {+ this.requireId(id);+ return this.push({ kind: 'removeNode', node: id });+ }+ public removeMany(ids: NodeId[]): this {+ for (const id of ids) this.remove(id);+ return this;+ }+ public merge(ids: NodeId[], separator = ' '): this {+ if (ids.length < 2)+ throw new EditError('merge requires at least two blocks');+ for (const id of ids) this.requireId(id);+ return this.push({ kind: 'mergeBlocks', nodes: ids, separator });+ }+ public insertTableAfter(id: NodeId, rows: string[][]): Ref {+ return this.buildTable(rows, { after: id });+ }++ public insertTableBefore(id: NodeId, rows: string[][]): Ref {+ return this.buildTable(rows, { before: id });+ }++ public appendTable(rows: string[][]): Ref {+ return this.buildTable(rows, { appendToRoot: true });+ }++ private buildTable(rows: string[][], at: Position): Ref {+ if (rows.length === 0)+ throw new EditError('table requires at least one row');+ const empty = rows.map((row) => row.map(() => ''));+ const ref = this.insert({ block: 'table', rows: empty }, at);+ rows.forEach(+ (row, r) =>+ void row.forEach((cell, c) => {+ if (cell) this.setCell(ref, r, c, cell);+ })+ );+ return ref;+ }++ public setCell(table: NodeId, row: number, col: number, text: string): this {+ this.requireId(table);+ this.requireCell(row, col);+ return this.push({ kind: 'setCell', table, row, col, text });+ }+ public addRow(table: NodeId, at?: number): this {+ this.requireId(table);+ return this.push({ kind: 'addRow', table, at });+ }+ public addColumn(table: NodeId, at?: number): this {+ this.requireId(table);+ return this.push({ kind: 'addColumn', table, at });+ }+ public removeRow(table: NodeId, row: number): this {+ this.requireId(table);+ this.requireCell(row, 0);+ return this.push({ kind: 'removeRow', table, row });+ }+ public removeColumn(table: NodeId, col: number): this {+ this.requireId(table);+ this.requireCell(0, col);+ return this.push({ kind: 'removeColumn', table, col });+ }+ private requireCell(row: number, col: number): void {+ if (row < 0 || col < 0)+ throw new EditError(`cell indices must be >= 0, got (${row}, ${col})`);+ }++ public insertDivider(afterId: NodeId): Ref {+ return this.insert({ block: 'divider' }, { after: afterId });+ }++ public insertImage(+ afterId: NodeId,+ img: {+ srcType: string;+ url: string;+ alt?: string;+ width?: number;+ height?: number;+ }+ ): Ref {+ return this.insert({ block: 'image', ...img }, { after: afterId });+ }++ public insertVideo(+ afterId: NodeId,+ vid: {+ srcType: string;+ url: string;+ controls?: boolean;+ width?: number;+ height?: number;+ }+ ): Ref {+ return this.insert({ block: 'video', ...vid }, { after: afterId });+ }++ public insertEquation(afterId: NodeId, tex: string): Ref {+ return this.insert({ block: 'equation', tex }, { after: afterId });+ }++ public insertInlineEquation(blockId: NodeId, at: number, tex: string): Ref {+ return this.insertInline(blockId, at, { inline: 'equation', tex });+ }++ public insertTextAfterInline(inlineRef: NodeRef, text: string): this {+ return this.push({+ kind: 'insertTextAfterInline',+ inline: inlineRef,+ text,+ });+ }++ public insertLineBreak(blockId: NodeId, at: number): Ref {+ return this.insertInline(blockId, at, { inline: 'linebreak' });+ }++ public insertDate(+ blockId: NodeId,+ at: number,+ isoDate: string,+ displayFormat?: string+ ): Ref {+ return this.insertInline(blockId, at, {+ inline: 'date',+ date: isoDate,+ displayFormat,+ });+ }+ private insertInline(id: NodeId, at: number, spec: NodeSpec): Ref {+ this.requireId(id);+ if (at < 0) throw new EditError(`inline offset must be >= 0, got ${at}`);+ const ref = this.mintRef();+ this.push({ kind: 'insertInline', ref, node: id, at, spec });+ return ref;+ }++ public insertMention(blockId: NodeId, at: number, mention: MentionSpec): Ref {+ return this.insertInline(blockId, at, { inline: 'mention', mention });+ }++ public mentionUser(+ blockId: NodeId,+ at: number,+ entity: { userId: string; email: string }+ ): Ref {+ return this.insertMention(blockId, at, { kind: 'user', ...entity });+ }++ public mentionContact(+ blockId: NodeId,+ at: number,+ entity: {+ contactId: string;+ name: string;+ emailOrDomain: string;+ isCompany: boolean;+ }+ ): Ref {+ return this.insertMention(blockId, at, { kind: 'contact', ...entity });+ }++ public mentionGroup(+ blockId: NodeId,+ at: number,+ entity: { groupAlias: string }+ ): Ref {+ return this.insertMention(blockId, at, { kind: 'group', ...entity });+ }++ public mentionDocument(+ blockId: NodeId,+ at: number,+ entity: { documentId: string; documentName: string; blockName: string }+ ): Ref {+ return this.insertMention(blockId, at, { kind: 'document', ...entity });+ }++ public setImageAlt(id: NodeId, alt: string): this {+ this.requireId(id);+ return this.push({ kind: 'setImageAlt', node: id, alt });+ }++ public setImageUrl(id: NodeId, url: string): this {+ this.requireId(id);+ return this.push({ kind: 'setImageUrl', node: id, url });+ }++ public setVideoUrl(id: NodeId, url: string): this {+ this.requireId(id);+ return this.push({ kind: 'setVideoUrl', node: id, url });+ }++ public setVideoControls(id: NodeId, controls: boolean): this {+ this.requireId(id);+ return this.push({ kind: 'setVideoControls', node: id, controls });+ }++ public setDate(id: NodeId, date: string, displayFormat?: string): this {+ this.requireId(id);+ return this.push({ kind: 'setDate', node: id, date, displayFormat });+ }+}++export type { MentionSpec };diff --git a/js/ai-editing-worker/src/ai-editing/editor/errors.ts b/js/ai-editing-worker/src/ai-editing/editor/errors.tsnew file mode 100644index 0000000000..025daf4b35--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/editor/errors.ts@@ -0,0 +1,11 @@+/**+ * Raised by `DocumentEditor` when the AI references a node id that doesn't exist+ * or violates a sanity check, and by `Doc` when an edit fails to resolve at apply+ * time. The tool catches it and reports the message straight back to the model.+ */+export class EditError extends Error {+ constructor(message: string) {+ super(message);+ this.name = 'EditError';+ }+}diff --git a/js/ai-editing-worker/src/ai-editing/editor/index.ts b/js/ai-editing-worker/src/ai-editing/editor/index.tsnew file mode 100644index 0000000000..1cdc7578b7--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/editor/index.ts@@ -0,0 +1,3 @@+export * from './document-editor';+export * from './errors';+export * from './ops';diff --git a/js/ai-editing-worker/src/ai-editing/editor/ops.ts b/js/ai-editing-worker/src/ai-editing/editor/ops.tsnew file mode 100644index 0000000000..383d727951--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/editor/ops.ts@@ -0,0 +1,214 @@+/**+ * The unified op vocabulary. `DocumentEditor`'s ergonomic methods collapse onto+ * this set; animators also emit these when simulating keystrokes. Each op kind+ * has exactly one animator (queue/animators) and one writer path (doc/doc.ts).+ * These are plain data — nothing here imports Lexical.+ */++/** A durable node id (the `{id}` / `<t id>` the model sees). */+export type NodeId = string;++/** Placeholder for a node that doesn't exist yet; minted by a creator method and+ * resolved to a real id by `Doc` when its `insertNode`/`insertInline` runs. */+export type Ref = string;++/** Either an existing node's id or a ref to a not-yet-created one. */+export type NodeRef = NodeId | Ref;++/** A character offset from the start of a node's text content. */+export type Offset = number;++/** A range within a node's text; an omitted bound means the node's edge. */+export type Span = { start?: Offset; end?: Offset };++/** Inline format toggles. */+export type Format = 'bold' | 'italic' | 'underline' | 'strike' | 'code';++/** Which occurrences of a substring match to act on (mirrors ai-toolkit's Scope). */+export type Scope = { kind: 'nth'; n: number } | { kind: 'all' };++/** Where to place a new or moved block. */+export type Position =+ | { after: NodeRef }+ | { before: NodeRef }+ | { appendToRoot: true }+ | { prependToRoot: true };++/** Declarative description of a node to build. `buildNode(spec)` (doc/doc.ts)+ * turns it into real Lexical nodes; the queue uses `text` to plan typing. */+export type NodeSpec =+ | { block: 'paragraph'; text?: string }+ | { block: 'heading'; level: 1 | 2 | 3 | 4 | 5 | 6; text?: string }+ | { block: 'quote'; text?: string }+ | { block: 'code'; language: string; text?: string }+ | { block: 'list'; list: ListKind; items: string[] }+ | { block: 'table'; rows: string[][] } // row 0 is the header+ | { block: 'divider' }+ | {+ block: 'document-card';+ documentId: string;+ documentName: string;+ blockName: string;+ blockParams?: Record<string, string>;+ }+ | { block: 'html-render'; html: string }+ | {+ block: 'image';+ srcType: string;+ url: string;+ alt?: string;+ width?: number;+ height?: number;+ }+ | {+ block: 'video';+ srcType: string;+ url: string;+ controls?: boolean;+ width?: number;+ height?: number;+ }+ | { block: 'equation'; tex: string; inline?: boolean }+ | { inline: 'linebreak' }+ | { inline: 'equation'; tex: string }+ | { inline: 'date'; date: string; displayFormat?: string }+ | { inline: 'mention'; mention: MentionSpec };++export type ListKind = 'bullet' | 'number' | 'check';++/** Entity-mention specs. Ids are supplied with the edit request, not looked up. */+export type MentionSpec =+ | { kind: 'user'; userId: string; email: string }+ | {+ kind: 'contact';+ contactId: string;+ name: string;+ emailOrDomain: string;+ isCompany: boolean;+ }+ | { kind: 'group'; groupAlias: string }+ | {+ kind: 'document';+ documentId: string;+ documentName: string;+ blockName: string;+ };++/** Block-type targets for `setBlockType`. */+export type BlockType = 'paragraph' | 'heading' | 'quote' | 'code';++/**+ * The canonical op set. `DocumentEditor` methods produce the high-level ops;+ * animators also emit `insertText`, `removeText`, and `appendListItem` to+ * simulate keystrokes. `DocWriter.apply` handles every variant.+ */+export type DocumentOp =+ // animation primitives (emitted by animators, not DocumentEditor)+ | { kind: 'insertText'; node: NodeRef; at: Offset; text: string }+ | { kind: 'removeText'; node: NodeRef; at: Offset; len: number }+ | {+ kind: 'appendListItem';+ ref: string;+ node: NodeRef;+ text: string;+ checked?: boolean;+ }+ | {+ kind: 'prependListItem';+ ref: string;+ node: NodeRef;+ text: string;+ checked?: boolean;+ }+ // text content+ | { kind: 'setText'; node: NodeRef; text: string }+ | { kind: 'setEquation'; node: NodeRef; tex: string }+ | { kind: 'appendText'; node: NodeRef; text: string }+ | { kind: 'prependText'; node: NodeRef; text: string }+ | { kind: 'insertTextAfterInline'; inline: NodeRef; text: string; ref?: Ref }+ | {+ kind: 'replaceText';+ node: NodeRef;+ find: string;+ to: string;+ scope: Scope;+ }+ // inline formatting+ | {+ kind: 'formatText';+ node: NodeRef;+ match: string;+ format: Format;+ on: boolean;+ scope: Scope;+ }+ | { kind: 'clearFormat'; node: NodeRef; match?: string; scope: Scope }+ | {+ kind: 'markText';+ node: NodeRef;+ match: string;+ on: boolean;+ scope: Scope;+ }+ | {+ kind: 'linkText';+ node: NodeRef;+ match: string;+ url: string | null;+ scope: Scope;+ }+ | { kind: 'formatNode'; node: NodeRef; format: Format; on: boolean }+ | { kind: 'clearNodeFormat'; node: NodeRef }+ // block type / list+ | {+ kind: 'setBlockType';+ node: NodeRef;+ block: BlockType;+ level?: number;+ language?: string;+ }+ | { kind: 'setListType'; nodes: NodeRef[]; list: ListKind }+ | { kind: 'setChecked'; node: NodeRef; checked: boolean }+ | { kind: 'setIndent'; node: NodeRef; indent: number | 'in' | 'out' }+ // structure+ | { kind: 'insertNode'; ref: Ref; spec: NodeSpec; at: Position }+ | {+ kind: 'insertInline';+ ref: Ref;+ node: NodeRef;+ at: Offset;+ spec: NodeSpec;+ }+ | { kind: 'moveNode'; node: NodeRef; at: Position }+ | { kind: 'removeNode'; node: NodeRef }+ | { kind: 'mergeBlocks'; nodes: NodeRef[]; separator: string }+ // list items+ | {+ kind: 'insertListItemAfter';+ ref: Ref;+ node: NodeRef;+ text: string;+ list: ListKind;+ }+ | {+ kind: 'insertListItemBefore';+ ref: Ref;+ node: NodeRef;+ text: string;+ list: ListKind;+ }+ | { kind: 'removeListItem'; node: NodeRef }+ // tables+ | { kind: 'setCell'; table: NodeRef; row: number; col: number; text: string }+ | { kind: 'addRow'; table: NodeRef; at?: number }+ | { kind: 'addColumn'; table: NodeRef; at?: number }+ | { kind: 'removeRow'; table: NodeRef; row: number }+ | { kind: 'removeColumn'; table: NodeRef; col: number }+ // media / date+ | { kind: 'setImageAlt'; node: NodeRef; alt: string }+ | { kind: 'setImageUrl'; node: NodeRef; url: string }+ | { kind: 'setVideoUrl'; node: NodeRef; url: string }+ | { kind: 'setVideoControls'; node: NodeRef; controls: boolean }+ | { kind: 'setDate'; node: NodeRef; date: string; displayFormat?: string };++export type DocumentOpKind = DocumentOp['kind'];diff --git a/js/ai-editing-worker/src/ai-editing/prompts/API_COMPACT.md b/js/ai-editing-worker/src/ai-editing/prompts/API_COMPACT.mdnew file mode 100644index 0000000000..5d12876763--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/prompts/API_COMPACT.md@@ -0,0 +1,42 @@+# Summary of Editor API++The writer has the complete API reference. Use this compact list to help yourself understand what our editing team is capable of.++## Inline Formatting++- Match-based: `bold`, `italic`, `underline`, `strike`, `inlineCode`, `unbold`, `unitalic`, `ununderline`, `unstrike`, `uninlineCode`, `format`, `clearFormat`, `clearAllFormat`, `highlight`, `unhighlight`, `link`, `unlink`+- Text-node based: `boldNode`, `italicNode`, `underlineNode`, `strikeNode`, `codeNode`, `formatNode`, `clearNodeFormat`++## Text Content++- `setText`, `replace`, `appendText`, `prependText`, `setEquation`++## Block Type Conversion++- `convertToParagraph`, `convertToHeading`, `convertToQuote`, `convertToCodeBlock(id, language)`, `setLanguage(id, language)`+- Code block language is required. Use `setLanguage(id, language)` to change an existing code block's language.++## Lists++- `bulletList`, `numberedList`, `checklist`, `setListType`, `check`, `uncheck`, `setChecked`, `indent`, `outdent`, `setIndent`+- Add/remove items next to an existing item: `insertListItemAfter(liId, text, list?)`, `insertListItemBefore(liId, text, list?)`, `removeListItem(liId)`. `list` is `'bullet'`|`'number'`|`'check'` (default `'bullet'`); a differing kind nests a sublist. Pass an existing `<li>` id.+- Add an item to a list as a whole (incl. an **empty** list): `appendListItem(listId, text)`, `prependListItem(listId, text)`. Pass the `<ul>`/`<ol>` id; the item inherits the list's kind. All item inserts return the new `<li>` id.++## Structure++- Insert relative to existing blocks: `insertParagraphAfter`, `insertParagraphBefore`, `insertHeadingAfter`, `insertHeadingBefore`, `insertQuoteAfter`, `insertCodeBlockAfter(id, language, text?)`, `insertBlockAfter`, `insertBlockBefore`+- Insert at document edges: `appendParagraph`, `prependParagraph`, `appendBlock`, `prependBlock`+- Rearrange/remove: `move`, `remove`, `removeMany`, `merge`++## Tables++- `insertTableAfter`, `insertTableBefore`, `appendTable`, `setCell`, `addRow`, `addColumn`, `removeRow`, `removeColumn`++## Native Objects++- Block objects: `insertDivider`, `insertImage`, `insertVideo`, `insertEquation`; or via `insertBlockAfter`/`appendBlock` with specs `document-card` (documentId, documentName, blockName, blockParams?) and `html-render` (html)+- Inline objects: `insertInlineEquation`, `insertLineBreak`, `insertDate`, `insertMention`, `mentionUser`, `mentionContact`, `mentionGroup`, `mentionDocument`+- To place text after an inline object: `insertTextAfterInline(inlineRef, text)`. Do **not** use `appendText` for this — it targets the block's last text node and misplaces the text when an inline object is the last child.+- Updates: `setImageAlt`, `setImageUrl`, `setVideoUrl`, `setVideoControls`, `setDate`++Our editing team is very well versed in how to use the library and you should never tell them how to get the job done. But this is to help you understand what they are capable of.diff --git a/js/ai-editing-worker/src/ai-editing/prompts/API_COMPLETE.md b/js/ai-editing-worker/src/ai-editing/prompts/API_COMPLETE.mdnew file mode 100644index 0000000000..e1b0c6448e--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/prompts/API_COMPLETE.md@@ -0,0 +1,450 @@+# `editor` API++All edits go through the `editor` object. `id` is a node id from the XML. Methods that create a node return a handle you can pass to later calls. Pass plain text only. `scope` is optional: `{ kind: 'all' }` for every match, `{ kind: 'nth', n: N }` for the 1-based Nth match, or omitted (defaults to `{ kind: 'all' }`).++## Inline Formatting By Match++- `bold(id, match, scope?)` · `italic(id, match, scope?)` · `underline(id, match, scope?)` · `strike(id, match, scope?)` · `inlineCode(id, match, scope?)`+- `unbold(id, match, scope?)` · `unitalic(id, match, scope?)` · `ununderline(id, match, scope?)` · `unstrike(id, match, scope?)` · `uninlineCode(id, match, scope?)`+- `format(id, match, format, on?, scope?)` where `format` is `bold`, `italic`, `underline`, `strike`, or `code`+- `clearFormat(id, match?, scope?)` · `clearAllFormat(id)`+- `highlight(id, match, scope?)` · `unhighlight(id, match, scope?)`+- `link(id, match, url, scope?)` · `unlink(id, match, scope?)`++```ts+editor.bold('b1', 'quick');+editor.italic('b1', 'brown');+editor.highlight('b1', 'the');+editor.link('b1', 'fox', 'https://x');+```++Before:++```xml+<doc>+ <p id="b1">+ <t id="t1">the quick brown fox</t>+ </p>+</doc>+```++After:++```xml+<doc>+ <p id="b1">+ <mark id="b2" direction="null" format="" indent="0" version="1" ids="[]">+ <t id="t1">the</t>+ </mark>+ <t id="t2"> </t>+ <t id="t3" bold="true">quick</t>+ <t id="t4"> </t>+ <t id="t5" italic="true">brown</t>+ <t id="t6"> </t>+ <a id="b3" href="https://x">+ <t id="t7">fox</t>+ </a>+ </p>+</doc>+```++## Inline Formatting By Text Node++- `boldNode(textId)` · `italicNode(textId)` · `underlineNode(textId)` · `strikeNode(textId)` · `codeNode(textId)`+- `formatNode(textId, format, on?)` · `clearNodeFormat(textId)`++```ts+editor.boldNode('t1');+editor.underlineNode('t1');+```++Before:++```xml+<doc>+ <p id="b1">+ <t id="t1">Hello world</t>+ </p>+</doc>+```++After:++```xml+<doc>+ <p id="b1">+ <t id="t1" bold="true" underline="true">Hello world</t>+ </p>+</doc>+```++## Text Content++- `setText(id, text)`+- `replace(id, find, to, scope?)`+- `appendText(id, text)` · `prependText(id, text)`+- `setEquation(id, tex)`++```ts+editor.setText('b1', 'replaced');+editor.appendText('b2', '!!!');+editor.prependText('b3', '> ');+editor.replace('b3', 'third', 'THIRD');+editor.setEquation('eq1', 'y^2');+```++Before:++```xml+<doc>+ <p id="b1"><t id="t1">first</t></p>+ <p id="b2"><t id="t2">second</t></p>+ <p id="b3"><t id="t3">third</t></p>+ <equation id="eq1" version="1" equation="x^2" inline="false"/>+</doc>+```++After:++```xml+<doc>+ <p id="b1"><t id="t1">replaced</t></p>+ <p id="b2"><t id="t2">second!!!</t></p>+ <p id="b3"><t id="t3">> THIRD</t></p>+ <equation id="eq1" version="1" equation="y^2" inline="false"/>+</doc>+```++## Block Type Conversion++- `convertToParagraph(id)` · `convertToHeading(id, level)` · `convertToQuote(id)` · `convertToCodeBlock(id, language)` · `setLanguage(id, language)`++```ts+editor.convertToHeading('b1', 2);+editor.convertToQuote('b2');+editor.convertToCodeBlock('b3', 'ts');+editor.convertToParagraph('b4');+editor.setLanguage('b3', 'python');+```++Before:++```xml+<doc>+ <p id="b1"><t id="t1">Title</t></p>+ <p id="b2"><t id="t2">A quote</t></p>+ <p id="b3"><t id="t3">code here</t></p>+ <h3 id="b4"><t id="t4">Back to paragraph</t></h3>+</doc>+```++After:++```xml+<doc>+ <h2 id="b1"><t id="t1">Title</t></h2>+ <blockquote id="b2"><t id="t2">A quote</t></blockquote>+ <custom-code id="b3" direction="null" format="" indent="0" version="1" language="python"><t id="t3">code here</t></custom-code>+ <p id="b4"><t id="t4">Back to paragraph</t></p>+</doc>+```++## Lists++- `bulletList(idOrIds)` · `numberedList(idOrIds)` · `checklist(idOrIds)` · `setListType(id, kind)`+- `check(id)` · `uncheck(id)` · `setChecked(id, bool)`+- `indent(id, by?)` · `outdent(id, by?)` · `setIndent(id, level)`+- `insertListItemAfter(liId, text, list?)` · `insertListItemBefore(liId, text, list?)` · `removeListItem(liId)`+- `appendListItem(listId, text)` · `prependListItem(listId, text)`++To add an item relative to an *existing* sibling, use `insertListItemAfter`/`Before` with that `<li>`'s id. `list` (`'bullet'`|`'number'`|`'check'`, default `'bullet'`) sets the new item's kind; when it differs from the surrounding list, the item is wrapped in a nested sublist of that kind.++To add an item to a list as a whole -- including a list that is currently **empty** (no `<li>` to anchor on) -- use `appendListItem`/`prependListItem` with the `<ul>`/`<ol>` id; the item inherits the list's kind. All four methods return a handle to the new `<li>` you can use in later calls.++```ts+editor.checklist(['b1', 'b2']);+editor.check('b1');+editor.indent('b2');+editor.outdent('b2');+editor.setChecked('b2', false);+const li = editor.insertListItemAfter('li1', 'second item');+editor.insertListItemAfter(li, 'nested', 'number');+editor.removeListItem('li2');+const first = editor.appendListItem('ol3', 'first step'); // populate an empty list+editor.appendListItem('ol3', 'second step');+```++Before:++```xml+<doc>+ <ul id="list1"><li id="li1"><t id="t1">first item</t></li><li id="li2" value="2"><t id="t2">old item</t></li></ul>+</doc>+```++After:++```xml+<doc>+ <ul id="list1">+ <li id="li1"><t id="t1">first item</t></li>+ <li id="b1" value="2"><t id="t3">second item</t></li>+ <li id="b2" value="3">+ <ol id="b3"><li id="b4"><t id="t4">nested</t></li></ol>+ </li>+ </ul>+</doc>+```++Before:++```xml+<doc>+ <p id="b1"><t id="t1">task b</t></p>+ <p id="b2"><t id="t2">task a</t></p>+ <ul id="list1"><li id="li1"><t id="t3">z</t></li><li id="li2" value="2"><t id="t4">a</t></li></ul>+</doc>+```++After:++```xml+<doc>+ <ul id="b3" listType="check">+ <li id="b4" checked="true"><t id="t1">task b</t></li>+ <li id="b5" value="2" checked="false"><t id="t2">task a</t></li>+ </ul>+ <ul id="list1"><li id="li2"><t id="t4">a</t></li><li id="li1" value="2"><t id="t3">z</t></li></ul>+</doc>+```++## Structure++- `insertParagraphAfter(id, text?)` · `insertParagraphBefore(id, text?)`+- `insertHeadingAfter(id, level, text?)` · `insertHeadingBefore(id, level, text?)`+- `insertQuoteAfter(id, text?)` · `insertCodeBlockAfter(id, language, text?)`+- `insertBlockAfter(id, spec)` · `insertBlockBefore(id, spec)`+- `appendParagraph(text?)` · `prependParagraph(text?)` · `appendBlock(spec)` · `prependBlock(spec)`+- `move(id, position)` where `position` is `{ after: id }`, `{ before: id }`, `{ appendToRoot: true }`, or `{ prependToRoot: true }`+- `remove(id)` · `removeMany(ids)` · `merge(ids, separator?)`++```ts+editor.insertHeadingBefore('b1', 1, 'Title');+const c = editor.insertParagraphAfter('b2', 'Conclusion');+editor.appendText(c, '!');+editor.appendParagraph('Footer');+editor.move('b2', { after: c });+editor.remove('b4');+```++Before:++```xml+<doc>+ <p id="b1"><t id="t1">Intro</t></p>+ <p id="b2"><t id="t2">Body</t></p>+ <p id="b4"><t id="t3">Remove me</t></p>+</doc>+```++After:++```xml+<doc>+ <h1 id="b3"><t id="t4">Title</t></h1>+ <p id="b1"><t id="t1">Intro</t></p>+ <p id="b5"><t id="t5">Conclusion!</t></p>+ <p id="b2"><t id="t2">Body</t></p>+ <p id="b6"><t id="t6">Footer</t></p>+</doc>+```++## Tables++- `insertTableAfter(id, rows)` · `insertTableBefore(id, rows)` · `appendTable(rows)` where `rows` is `string[][]` and row 0 is the header+- `setCell(tableId, row, col, text)`+- `addRow(tableId, at?)` · `addColumn(tableId, at?)` · `removeRow(tableId, row)` · `removeColumn(tableId, col)`++```ts+const t = editor.appendTable([['Item', 'Done']]);+editor.addRow(t);+editor.setCell(t, 1, 1, 'yes');+```++Before:++```xml+<doc>+ <p id="b1"><t id="t1">Tasks</t></p>+</doc>+```++After:++```xml+<doc>+ <p id="b1"><t id="t1">Tasks</t></p>+ <table id="b2">+ <tr id="b3"><td id="b4" headerState="1"><p id="b5"><t id="t2">Item</t></p></td><td id="b6" headerState="1"><p id="b7"><t id="t3">Done</t></p></td></tr>+ <tr id="b8"><td id="b9"><p id="b10"/></td><td id="b11"><p id="b12"><t id="t4">yes</t></p></td></tr>+ </table>+</doc>+```++## Media, Math, And Inline Objects++- `insertDivider(afterId)`+- `insertImage(afterId, { srcType, url, alt?, width?, height? })`+- `insertVideo(afterId, { srcType, url, controls?, width?, height? })`+- `insertEquation(afterId, tex)` · `insertInlineEquation(blockId, at, tex)`+- `insertLineBreak(blockId, at)` — inserts a **soft line break** (Shift+Enter) within a block. Use only when multiple lines must live in a single semantic block (poetry, addresses, signature blocks). For ordinary multi-line content, insert separate paragraph nodes instead.+- `insertDate(blockId, at, isoDate, displayFormat?)`+- `insertTextAfterInline(inlineRef, text)` — inserts a plain text node immediately after an inline object. Use this (not `appendText`) when you need text to follow an inline equation, date, line break, or other inline node.++The `at` in `insertInlineEquation`/`insertLineBreak`/`insertDate` is a **character offset into the block's plain text** — inline objects don't count toward it. Compute it (`text.length`, `text.indexOf('word')`) rather than guessing. When you do use `insertLineBreak` for soft-break content, set the first line then add each break at the running text length and `insertTextAfterInline` the next line:++```ts+editor.setText('b1', 'Lily pad hoppers');+const br1 = editor.insertLineBreak('b1', 'Lily pad hoppers'.length);+editor.insertTextAfterInline(br1, 'Croaking through the night');+const br2 = editor.insertLineBreak('b1', 'Lily pad hoppers'.length + 'Croaking through the night'.length);+editor.insertTextAfterInline(br2, 'Splash, then silence');+```++```ts+editor.insertDivider('b1');+editor.insertImage('b1', { srcType: 'url', url: 'https://x/cat.png', alt: 'cat' });+editor.insertVideo('b1', { srcType: 'url', url: 'https://x/movie.mp4', controls: true });+editor.insertEquation('b1', 'E=mc^2');+const eq = editor.insertInlineEquation('b2', 0, 'x^2');+editor.insertTextAfterInline(eq, ' is x squared');+editor.insertLineBreak('b2', 3);+editor.insertDate('b2', 4, '2026-06-23', 'MMM d, yyyy');+```++Before:++```xml+<doc>+ <p id="b1"><t id="t1">Above</t></p>+ <p id="b2"><t id="t2">Below</t></p>+</doc>+```++After:++```xml+<doc>+ <p id="b1"><t id="t1">Above</t></p>+ <equation id="b3" version="1" equation="E=mc^2" inline="false"/>+ <video id="b4" version="1" srcType="url" _id="" url="https://x/movie.mp4" width="0" height="0" scale="1" constrainedWidth="null" constrainedHeight="null" controls="true"/>+ <image id="b5" version="1" srcType="url" _id="" url="https://x/cat.png" width="0" height="0" scale="1" constrainedWidth="null" constrainedHeight="null" alt="cat"/>+ <hr id="b6"/>+ <p id="b2"><equation id="b7" version="1" equation="x^2" inline="true"/><t id="t2">Bel</t><br id="b8"/><date id="b9" date="2026-06-23" displayFormat="MMM d, yyyy"/><t id="t3">ow</t></p>+</doc>+```++## Update Existing Media And Dates++- `setImageAlt(id, alt)` · `setImageUrl(id, url)`+- `setVideoUrl(id, url)` · `setVideoControls(id, controls)`+- `setDate(id, isoDate, displayFormat?)`++```ts+editor.setImageAlt('img1', 'dog');+editor.setImageUrl('img1', 'https://x/dog.png');+editor.setVideoUrl('vid1', 'https://x/new.mp4');+editor.setVideoControls('vid1', false);+editor.setDate('date1', '2026-07-01', 'yyyy-MM-dd');+```++Before:++```xml+<doc>+ <image id="img1" version="1" srcType="url" _id="" url="https://x/cat.png" alt="cat"/>+ <video id="vid1" version="1" srcType="url" _id="" url="https://x/old.mp4" controls="true"/>+ <p id="b1"><date id="date1" date="2026-06-23" displayFormat="MMM d"/></p>+</doc>+```++After:++```xml+<doc>+ <image id="img1" version="1" srcType="url" _id="" url="https://x/dog.png" alt="dog"/>+ <video id="vid1" version="1" srcType="url" _id="" url="https://x/new.mp4" controls="false"/>+ <p id="b1"><date id="date1" date="2026-07-01" displayFormat="yyyy-MM-dd"/></p>+</doc>+```++## Mentions++Use the ids and details supplied in the task -- do not invent them.++- `insertMention(blockId, at, mention)`+- `mentionUser(blockId, at, { userId, email })`+- `mentionContact(blockId, at, { contactId, name, emailOrDomain, isCompany })`+- `mentionGroup(blockId, at, { groupAlias })`+- `mentionDocument(blockId, at, { documentId, documentName, blockName })`++```ts+editor.mentionUser('b1', 6, { userId: 'u1', email: '[email protected]' });+editor.mentionContact('b1', 7, { contactId: 'c1', name: 'Acme', emailOrDomain: 'acme.com', isCompany: true });+```++Before:++```xml+<doc>+ <p id="b1"><t id="t1">Email </t></p>+</doc>+```++After:++```xml+<doc>+ <p id="b1"><t id="t1">Email </t><mention id="m1" kind="user" userId="u1" email="[email protected]"/><mention id="m2" kind="contact" contactId="c1" name="Acme" emailOrDomain="acme.com" isCompany="true"/></p>+</doc>+```++## Node Specs++- `{ block: 'paragraph', text? }`+- `{ block: 'heading', level, text? }`+- `{ block: 'quote', text? }`+- `{ block: 'code', language, text? }`+- `{ block: 'list', list: 'bullet'|'number'|'check', items: string[] }`+- `{ block: 'table', rows }`+- `{ block: 'divider' }`+- `{ block: 'document-card', documentId, documentName, blockName, blockParams? }` where `blockParams` is `Record<string, string>`. Use the ids supplied in the task -- do not invent them.+- `{ block: 'html-render', html }`+- `{ block: 'image', srcType, url, alt?, width?, height? }`+- `{ block: 'video', srcType, url, controls?, width?, height? }`+- `{ block: 'equation', tex, inline? }`++```ts+editor.appendBlock({ block: 'heading', level: 2, text: 'Section' });+editor.appendBlock({ block: 'list', list: 'check', items: ['todo a', 'todo b'] });+editor.appendBlock({ block: 'code', language: 'ts', text: 'const x = 1' });+```++Before:++```xml+<doc>+ <p id="b1"><t id="t1">Doc</t></p>+</doc>+```++After:++```xml+<doc>+ <p id="b1"><t id="t1">Doc</t></p>+ <h2 id="b2"><t id="t2">Section</t></h2>+ <ul id="b3" listType="check"><li id="b4"><t id="t3">todo a</t></li><li id="b5" value="2"><t id="t4">todo b</t></li></ul>+ <custom-code id="b6" direction="null" format="" indent="0" version="1" language="ts"><t id="t5">const x = 1</t></custom-code>+</doc>+```diff --git a/js/ai-editing-worker/src/ai-editing/prompts/CODER.md b/js/ai-editing-worker/src/ai-editing/prompts/CODER.mdnew file mode 100644index 0000000000..8e046b323e--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/prompts/CODER.md@@ -0,0 +1,46 @@+# Role: Coder++You are the coder.++You carry out one task from a supervisor by writing plain JavaScript against a single object, `editor`, then replying with a one-line summary. Your job is mechanical: translate the instruction into `editor` calls exactly.++The task may be a single small change or a larger set of related changes confined to one region of the document -- either way, you MUST carry out the **whole** instruction before you summarize. Drive as many `editor` calls as the task needs (a loop, or several calls in one `runCode`); you don't have to stop after one change.++You do not see the whole document by default -- only a line-numbered XML window around the ids mentioned in the supervisor's instruction. Everything you need is usually in that window. If you need an id or region that isn't shown, call `readDocument` to get the ENTIRE document as line-numbered XML, then proceed -- do this before ever reporting yourself blocked.++## How you write edits++You call `runCode(code)` with plain JS statements. The only things in scope are `editor` and (when provided) `snippets`. No `$`-helpers, no imports, no `s`, no `$getRoot`. For example:++```js+editor.convertToHeading('b14', 2);+editor.bold('b5', 'Bluejay');+```++- Each method produces an edit(s); the system then animates and applies them.+- **Address nodes by their `id`** from the XML. Block edits take a block id; a few+ inline methods (`boldNode`, `formatNode`, …) take a `<t id>` text-node id.+- **Creators return a handle** to the new node you can use in later calls:+ ```js+ const p = editor.insertParagraphAfter('b14', 'Intro');+ editor.bold(p, 'Intro');+ ```+- **Offsets (`at`) are character positions in the block's plain text** — `0` is the start, `text.length` is the end. Inline objects (line breaks, equations, dates) do NOT count as characters. **Never guess an offset.** You MUST compute it from the actual text: `snippets.line1.length` for the end of a line, `text.indexOf('word')` for a position inside it. Guessing small integers like `1, 2, 3` will split words apart.+- **Multi-line content → separate paragraphs, not line breaks:** if you need to write multiple lines of content, you should insert separate paragraph nodes. `insertLineBreak` produces a *soft* break (Shift+Enter) within a single block - only use it when the content is semantically one unit split across visual lines (poetry, addresses, signature blocks). For ordinary multi-line content:+ ```js+ editor.setText(id, snippets.line1);+ const p2 = editor.insertParagraphAfter(id, snippets.line2);+ editor.insertParagraphAfter(p2, snippets.line3);+ ```+ When you do need `insertLineBreak`, the `at` offset counts only characters (inline objects don't count). Append each line with `insertTextAfterInline(brRef, text)` and accumulate the running text length manually.+- Pass **plain text only** -- you MUST NOT ever use XML/markdown syntax. `editor.setText(id, '# x')` inserts the literal characters `# x`, it does not make a heading. We do not support or understand Markdown or XML in our editor.+- **`setText(id, text)` fully replaces a node's content and clears all inline formatting** -- use it whenever you want to overwrite a text node entirely with plain text. `replace(id, find, to)` is only for partial substitutions where `find` is a known substring and you want to preserve surrounding formatting. **For a code block, always rewrite its whole body with `setText` -- never use `replace` on a code block.**+- When the task provides a **`snippets` object**, use `snippets.KEY` directly rather than re-embedding its value as a string literal -- `editor.setText(id, snippets.code)`. This avoids escaping errors on special characters.+- If a call references an id that doesn't exist, you get an error back naming it -- re-read the regions shown, pick the right id, and try again. Don't repeat a failing call.+- If the instruction refers to a node you **cannot see** in your window, do NOT guess or invent an id. First call `readDocument` to view the whole document and locate the real id. Only if it is genuinely absent should you call `reportBlocked({ message })` to hand the problem back to the supervisor -- that ends your task, so don't also call `runCode`.+- You may use ordinary JS (loops, arrays) to drive many calls. You have the full power of plain JavaScript at your disposal.+- It is **not** your job to try to reason about *why* we are editing the document.+- We prefer replacements of text over entire node swaps+- Don't riff off the instruction, do what you are told++Apply the whole instruction, then reply with a one-line summary (no tool call).diff --git a/js/ai-editing-worker/src/ai-editing/prompts/INTERPRET.md b/js/ai-editing-worker/src/ai-editing/prompts/INTERPRET.mdnew file mode 100644index 0000000000..a6e9d719b1--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/prompts/INTERPRET.md@@ -0,0 +1,19 @@+# Role: Interpreter++You are the interpreter.++Your job is to help understand the user's request. Before any edits happen, read the user's request together with the document and produce a short bulleted list with answers to the following items.++You are not editing anything and you are not planning the mechanics -- you are a third-party establishing *intent* for the supervisor who acts next.++State:++1. **Literal ask** -- what the user literally said, in your own words.+2. **Underlying goal.** -- think about what is the user actually trying to accomplish, and what *purpose* must the result serve? Do not just rephrase the literal ask. Reason about the function of the thing being added or changed. If a literal reading would produce something that does not actually serve that purpose, prefer the reading that does. Verbs like "fix", "fill", "complete", "finish", or "clean up" ask for the content the thing is *for* -- produce the real, useful content the slot was meant to hold, not a token that merely satisfies the literal description. A blank that sits in a structure is defined by its place in that structure: infer what belongs there from its row and column, its sibling items, or its heading, the way a human reader would know what the empty cell "should" say. An absence-marker (e.g. "N/A", "TBD", "--", "lorem ipsum") restates the gap instead of closing it; emit one only when the document offers genuinely nothing to infer from, and say so.+3. **End state** -- the concrete result as **literal text that satisfies the underlying goal** (point 2), not just the literal words of the request. If you are adding or changing a block, write out the exact final text it should contain, including any decoration. Anchor each target by **what it is and where it sits** (e.g. "the intro paragraph", "the second heading", "each header cell of the threats table") -- describe the content, not a per-node `id` table. Node ids change the moment a node is edited, so an id-keyed list of final text would be stale before the edits even finish; describing the end state by content keeps it valid regardless of id churn.+4. **Ambiguities** -- if the target block, text, or position is unclear (e.g. "the dots", "the +s below X", "the bottom lines"), name the candidate nodes by their XML `id` attribute to pin down *which* node you mean, then commit to your single best interpretation and say why. (Using an id to disambiguate is fine; just don't build the whole end state around ids -- see point 3.)+5. **Scope** -- the general scope of the changes. Whether we should think about whole-document style changes and aim for sweeping changes to organize things, or focus on specific parts to satisfy a very-specific request.+6. **Style** -- think about the most correct way to implement the request, that fits with the way the document is structured and the user's intention. We want to by default conform and flow with the document's existing style unless the user is asking or implying in their **underlying goal** that they want to reformat or change the shape of the document. When you generate new content, match the voice, register, and specificity of its nearest siblings -- the other cells in the column, the other items in the list, the surrounding prose. Content that is correct but tonally flat (a dry "N/A" among playful, vivid entries) does not flow with the document and is therefore wrong.+7. **Success** -- a brief list of things to check for when verifying the result. Always include: before finishing, is each deliverable real content, or did you emit an absence-marker where the document had something to infer from? If so, revise the end state before finishing.++Be concise. Write in TLDR bullets. Do not write code, do not list edits, do not call any tools -- just the interpretation.diff --git a/js/ai-editing-worker/src/ai-editing/prompts/SHARED.md b/js/ai-editing-worker/src/ai-editing/prompts/SHARED.mdnew file mode 100644index 0000000000..10c897cb6b--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/prompts/SHARED.md@@ -0,0 +1,15 @@+# How this system works++You are one role in a two-role editing system for handling user requests to edit a document:++- **Supervisor** -- turns the user's request into small, mechanical edit instructions and dispatches them to writers in rounds, reviewing the resulting diff after each round until the request is done.+- **Writer** -- receives ONE mechanical edit instruction and carries it out by running JavaScript node-manipulation snippets against the document, then reports back.++Shared ground rules for both roles:++- The document is shown to you as **XML, which is a read-only structural *view*** -- the user sees the actual rendered rich text in their editor. The XML is never written back. All changes happen exclusively through node-manipulation API calls (`$`-helpers) addressed by node id.+- The XML shows the document's typed AST: block nodes (`<p>`, `<h1>`–`<h6>`, `<blockquote>`, `<custom-code>`, `<ul>`, `<li>`, `<table>`, etc.) each with an `id` attribute; inline text runs as `<t id="...">` with format attributes (`bold="true"`, `italic="true"`, `underline="true"`, `strikethrough="true"`, `code="true"`).+- Address any node by its `id` attribute from the XML.+- Edits within a single dispatched batch run **in parallel** against one shared document, so only independent, non-conflicting edits may be batched together.++Your role-specific instructions follow.diff --git a/js/ai-editing-worker/src/ai-editing/prompts/SUPERVISOR.md b/js/ai-editing-worker/src/ai-editing/prompts/SUPERVISOR.mdnew file mode 100644index 0000000000..48662368e4--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/prompts/SUPERVISOR.md@@ -0,0 +1,44 @@+# Role: Supervisor++You are the supervisor.++Use the `<intent>` block as the resolved task. Apply it by calling `dispatch`. Stop once the latest dispatch result is good enough and give a one-line summary. Aim to complete the task in as few dispatch rounds as possible — ideally one or two. Plan thoroughly before dispatching so you don't need correction rounds.++## Dispatch Rules++- Each edit is one coherent writer task.+- One region = one writer. Do NOT split a paragraph, list, table, or section across parallel writers.+- Batch only clearly disjoint regions. If unsure, dispatch sequentially.+- A writer can perform many related changes in one instruction.+- **Never batch sequentially-dependent insertions in a single dispatch.** If edit B inserts after a node created by edit A, you don't yet know that node's id -- dispatch A first, get the result, then dispatch B using the id from the result. Batching them will BLOCK edit B every time. Use concurrency where you can, but be VERY careful and THINK about this.++## Instructions++- Mention existing XML ids for the target region; dispatch expands context from those ids.+- Use `snippets` for all exact/verbatim text, including replacement strings, table cell contents, long text, code, and special-character text. In `editing_instruction`, refer to `snippets.KEY` instead of embedding the text. It is important you include all the necessary snippets since the writers will otherwise have to painstakingly type it out by hand.+- When creating new content from scratch (not editing existing text), generate the full text yourself and put it in `snippets`. Never ask the writer to invent or compose content -- writers are mechanical appliers, not content generators.+- Use native editor objects when appropriate: divider, table, heading, quote, code block, image, video, equation, date, mention, document-card. Do not simulate native objects with plain text, like `======` for a divider.+- To mention a person, use the `userId`/`email` (or `contactId`/`name`/`emailOrDomain`) from the request. To add a document-card, use the `documentId`/`documentName`/`blockName` from the request. Pass these to the writer via `snippets.KEY`. Never invent or look up these ids -- they come from the request.+- Do not write literal XML/Markdown unless the user wants those characters.+- Do not invent or preserve ids. New ids are assigned automatically and existing ids may change.+- Do not write code yourself; describe the change mechanically.+- Do not try to inject formatting like bullet unicode into your snippets. Let the writer handle the formatting.+- Think about what the document will look like when rendered to HTML, visualize it. We have a custom HTML variant, but the general aesthetic will be similar.++## After Dispatch++- If the document feels right, meets the interpreter criteria, isn't weird, and you're happy, you can finish.+- If blocked, failed, or wrong, dispatch a clearer correction using current ids from the latest result.+- Prefer in-place corrections. Don't remove-and-recreate a structure you already built -- patch the existing nodes. Recreating churns ids and rarely converges.+- If the same region is still wrong after 2 correction rounds, try a fundamentally different approach — different op, different structure — rather than repeating variations of the same dispatch.+- If it is still wrong after a third attempt, **give up on that region**: leave it as-is, note it in your final summary, and move on. Do not spend more rounds on something the writer cannot resolve.+- Judge by content and structure, not id stability.+- It is up to you to determine when we are "done" and the result is satisfactory; don't go on forever. Also don't make changes that undo all of your hard work if it's mostly done.+- Ensure that the formatting of the final document is good, doesn't overload formatting or use weird whitespace.++## When You Cannot Proceed++- You MUST call `reportBlocked` if you need more information -- never write clarification text directly as a response.+- Your message must be a directive to invoke you again with what is missing.+- Always call it before attempting a mention or document-card if the required ids were not provided in the request. Do not invent or guess ids.+- Do not use it just because an edit is complex -- attempt those directly.diff --git a/js/ai-editing-worker/src/ai-editing/queue/_testUtils.ts b/js/ai-editing-worker/src/ai-editing/queue/_testUtils.tsnew file mode 100644index 0000000000..99086eacbd--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/_testUtils.ts@@ -0,0 +1,30 @@+import type { DocWriter } from '../doc/interfaces';+import type { DocumentOp } from '../editor/ops';+import type { Awareness } from './types';++/** Records every DocWriter apply call; can be told to throw on a given kind (or several). */+export function recordingWriter(+ throwOn?:+ | { kind: string; error: string }+ | Array<{ kind: string; error: string }>+) {+ const throwers = throwOn+ ? Array.isArray(throwOn)+ ? throwOn+ : [throwOn]+ : [];+ const edits: DocumentOp[] = [];+ const w: DocWriter = {+ apply(op: DocumentOp) {+ const t = throwers.find((x) => x.kind === op.kind);+ if (t) throw new Error(t.error);+ edits.push(op);+ },+ };+ return { w, edits };+}++export function recordingAwareness() {+ const seen: Awareness[] = [];+ return { source: { apply: (x: Awareness) => void seen.push(x) }, seen };+}diff --git a/js/ai-editing-worker/src/ai-editing/queue/animators.test.ts b/js/ai-editing-worker/src/ai-editing/queue/animators.test.tsnew file mode 100644index 0000000000..7613f0cc18--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/animators.test.ts@@ -0,0 +1,1149 @@+import { describe, expect, it } from 'vitest';+import type { DocReader, Match } from '../doc/interfaces';+import type { DocumentOp } from '../editor/ops';+import { animate } from './animators';+import { mockRandomSource, type RandomSource } from './random-source';+import { DEFAULT_RANGES, type DocumentOpStep } from './types';++/** Mock reader: canned answers, no Lexical. */+function reader(over: Partial<DocReader> = {}): DocReader {+ return {+ locate: () => [],+ textLength: () => 0,+ cellNode: () => 'cell',+ ...over,+ };+}++/** Run a single op through the animator and return its steps. */+function run(+ op: DocumentOp,+ opts: { randomSource?: RandomSource; docReader?: DocReader } = {}+) {+ // pin speed 400 wpm → msPerChar 30 for exact-ms assertions+ const msPerChar = 60_000 / (400 * 5);+ const steps = animate(op, {+ randomSource: opts.randomSource ?? mockRandomSource(),+ docReader: opts.docReader ?? reader(),+ msPerChar,+ ranges: DEFAULT_RANGES,+ });+ return { done: true, steps };+}++const onlyEdits = (steps: DocumentOpStep[]) =>+ steps.filter((s) => s.kind === 'edit').map((s) => (s as any).y);++const highlights = (steps: DocumentOpStep[]) =>+ steps.filter(+ (s) => s.kind === 'awareness' && (s.x as any).type === 'highlight'+ ) as Array<{+ kind: 'awareness';+ x: {+ type: 'highlight';+ node: string;+ span: { start: number; end: number };+ };+ }>;++const cursors = (steps: DocumentOpStep[]) =>+ steps.filter(+ (s) => s.kind === 'awareness' && (s.x as any).type === 'cursor'+ ) as Array<{+ kind: 'awareness';+ x: { type: 'cursor'; node: string; at: number };+ }>;++const pauses = (steps: DocumentOpStep[]) =>+ steps.filter((s) => s.kind === 'pause').map((s) => (s as any).ms as number);++describe('formatText animator — full flow', () => {+ it('sweeps the matched span (3 sweeps, left-anchored) then one match-based edit', () => {+ // integer draws: preSelect(30), sweepCount=3, then 3 sweep pauses (75), then settle (145).+ const action = run(+ {+ kind: 'formatText',+ node: 'b5',+ match: 'Bluejay',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({ integer: [30, 3, 75, 75, 75, 145] }),+ docReader: reader({+ locate: () => [{ node: 't1', start: 4, end: 11 }],+ }),+ }+ );+ expect(action.done).toBe(true);+ expect(action.steps).toEqual<DocumentOpStep[]>([+ { kind: 'awareness', x: { type: 'cursor', node: 't1', at: 11 } },+ { kind: 'pause', ms: 30 }, // preSelect rest on anchor+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 't1', span: { start: 9, end: 11 } },+ },+ { kind: 'pause', ms: 75 },+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 't1', span: { start: 7, end: 11 } },+ },+ { kind: 'pause', ms: 75 },+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 't1', span: { start: 6, end: 11 } },+ },+ { kind: 'pause', ms: 75 },+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 't1', span: { start: 4, end: 11 } },+ },+ { kind: 'pause', ms: 145 },+ {+ kind: 'edit',+ y: {+ kind: 'formatText',+ node: 'b5',+ match: 'Bluejay',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ },+ ]);+ });++ it('right-anchors the sweep when direction=right', () => {+ const action = run(+ {+ kind: 'formatText',+ node: 'b5',+ match: 'x',+ format: 'italic',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ direction: 'right',+ integer: [30, 3, 75, 75, 75, 145],+ }),+ docReader: reader({+ locate: () => [{ node: 't1', start: 4, end: 11 }],+ }),+ }+ );+ // first awareness is a cursor at the START (right-anchored), grows rightward+ expect(action.steps[0]).toEqual({+ kind: 'awareness',+ x: { type: 'cursor', node: 't1', at: 4 },+ });+ expect(action.steps[1]).toEqual({ kind: 'pause', ms: 30 }); // preSelect rest on anchor+ expect(action.steps[2]).toEqual({+ kind: 'awareness',+ x: { type: 'highlight', node: 't1', span: { start: 4, end: 6 } },+ });+ });+});++describe('setText animator — select-all, delete, type (full flow)', () => {+ it('selects the existing text, deletes it, then types in 3-char chunks', () => {+ // integer draws: preSelect(30), sweepCount=3, 3 sweep pauses(75), settle(145), preDelete(190), typeText lead-in settle(150).+ // real draws: typeJitter 1.05; chunk len 2 → round(30*2*1.05)=63.+ const action = run(+ { kind: 'setText', node: 'b1', text: 'Hi' },+ {+ randomSource: mockRandomSource({+ integer: [30, 3, 75, 75, 75, 145, 190, 150],+ real: 1.05,+ }),+ docReader: reader({ textLength: () => 5 }),+ }+ );+ expect(action.steps).toEqual<DocumentOpStep[]>([+ { kind: 'awareness', x: { type: 'cursor', node: 'b1', at: 5 } },+ { kind: 'pause', ms: 30 }, // preSelect rest on anchor+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 'b1', span: { start: 4, end: 5 } },+ },+ { kind: 'pause', ms: 75 },+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 'b1', span: { start: 2, end: 5 } },+ },+ { kind: 'pause', ms: 75 },+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 'b1', span: { start: 1, end: 5 } },+ },+ { kind: 'pause', ms: 75 },+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 'b1', span: { start: 0, end: 5 } },+ },+ { kind: 'pause', ms: 145 },+ { kind: 'pause', ms: 190 }, // preDelete+ { kind: 'edit', y: { kind: 'removeText', node: 'b1', at: 0, len: 5 } },+ { kind: 'awareness', x: { type: 'cursor', node: 'b1', at: 0 } },+ { kind: 'pause', ms: 150 }, // typeText lead-in+ {+ kind: 'edit',+ y: { kind: 'insertText', node: 'b1', at: 0, text: 'Hi' },+ },+ { kind: 'awareness', x: { type: 'cursor', node: 'b1', at: 2 } },+ { kind: 'pause', ms: 63 },+ // canonicalizing setText: purges any stray inline nodes, text unchanged+ { kind: 'edit', y: { kind: 'setText', node: 'b1', text: 'Hi' } },+ ]);+ });++ it('skips the delete when the block is empty', () => {+ const action = run(+ { kind: 'setText', node: 'b1', text: 'A' },+ { docReader: reader({ textLength: () => 0 }) }+ );+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'insertText', node: 'b1', at: 0, text: 'A' },+ { kind: 'setText', node: 'b1', text: 'A' },+ ]);+ });+});++describe('insertNode animator', () => {+ it('inserts an EMPTY typed block, then types its text into the ref', () => {+ const action = run({+ kind: 'insertNode',+ ref: 'ref-1',+ spec: { block: 'paragraph', text: 'Hi' },+ at: { after: 'b1' },+ });+ expect(action.steps[0]).toEqual({+ kind: 'edit',+ y: {+ kind: 'insertNode',+ ref: 'ref-1',+ spec: { block: 'paragraph', text: '' },+ at: { after: 'b1' },+ },+ });+ expect(onlyEdits(action.steps)).toEqual([+ {+ kind: 'insertNode',+ ref: 'ref-1',+ spec: { block: 'paragraph', text: '' },+ at: { after: 'b1' },+ },+ { kind: 'insertText', node: 'ref-1', at: 0, text: 'Hi' },+ ]);+ });+ it('atomic block (image): caret to the insertion point, pause, it appears, caret moves in', () => {+ const action = run(+ {+ kind: 'insertNode',+ ref: 'ref-2',+ spec: { block: 'image', srcType: 'url', url: 'http://i' },+ at: { after: 'b1' },+ },+ {+ randomSource: mockRandomSource({ integer: 145 }),+ docReader: reader({ textLength: () => 4 }),+ }+ );+ expect(action.steps).toEqual<DocumentOpStep[]>([+ { kind: 'awareness', x: { type: 'cursor', node: 'b1', at: 4 } }, // caret at end of anchor+ { kind: 'pause', ms: 145 },+ {+ kind: 'edit',+ y: {+ kind: 'insertNode',+ ref: 'ref-2',+ spec: { block: 'image', srcType: 'url', url: 'http://i' },+ at: { after: 'b1' },+ },+ },+ { kind: 'awareness', x: { type: 'cursor', node: 'ref-2', at: 0 } },+ ]);+ });++ it('divider: inserts the rule directly (no draft swap)', () => {+ const action = run({+ kind: 'insertNode',+ ref: 'ref-3',+ spec: { block: 'divider' },+ at: { after: 'b1' },+ });+ expect(onlyEdits(action.steps)).toEqual([+ {+ kind: 'insertNode',+ ref: 'ref-3',+ spec: { block: 'divider' },+ at: { after: 'b1' },+ },+ ]);+ });+});++describe('insertInline animator', () => {+ it('caret to the offset, pause, the inline node appears', () => {+ const action = run(+ {+ kind: 'insertInline',+ ref: 'r1',+ node: 'b1',+ at: 3,+ spec: { inline: 'date', date: '2026-01-01' },+ },+ { randomSource: mockRandomSource({ integer: 145 }) }+ );+ expect(action.steps).toEqual<DocumentOpStep[]>([+ { kind: 'awareness', x: { type: 'cursor', node: 'b1', at: 3 } },+ { kind: 'pause', ms: 145 },+ {+ kind: 'edit',+ y: {+ kind: 'insertInline',+ ref: 'r1',+ node: 'b1',+ at: 3,+ spec: { inline: 'date', date: '2026-01-01' },+ },+ },+ ]);+ });+});++describe('insertTextAfterInline animator', () => {+ it('types the run out: first chunk creates a ref-stamped node, rest grow it by ref', () => {+ const action = run({+ kind: 'insertTextAfterInline',+ inline: 'br1',+ text: 'Ripple',+ });+ const edits = onlyEdits(action.steps);+ // first chunk creates + stamps the run after the inline; later chunks append+ expect(edits).toEqual([+ {+ kind: 'insertTextAfterInline',+ ref: 'br1~after-text',+ inline: 'br1',+ text: 'Rip',+ },+ { kind: 'insertText', node: 'br1~after-text', at: 3, text: 'ple' },+ ]);+ });++ it('empty text → a single one-shot edit (no typing)', () => {+ const action = run({+ kind: 'insertTextAfterInline',+ inline: 'br1',+ text: '',+ });+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'insertTextAfterInline', inline: 'br1', text: '' },+ ]);+ });+});++describe('setCell animator', () => {+ it('resolves the cell node and retypes it', () => {+ const action = run(+ { kind: 'setCell', table: 'tbl', row: 1, col: 0, text: 'Hi' },+ { docReader: reader({ cellNode: () => 'cellX', textLength: () => 3 }) }+ );+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'removeText', node: 'cellX', at: 0, len: 3 },+ { kind: 'insertText', node: 'cellX', at: 0, text: 'Hi' },+ { kind: 'setText', node: 'cellX', text: 'Hi' },+ ]);+ });++ it('filling a freshly-built (empty) cell just types — cursor walks, no delete', () => {+ // this is the table-fill path: empty grid inserted, then each cell typed.+ const action = run(+ { kind: 'setCell', table: 'tbl', row: 0, col: 0, text: 'Hi' },+ { docReader: reader({ cellNode: () => 'cellX', textLength: () => 0 }) }+ );+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'insertText', node: 'cellX', at: 0, text: 'Hi' },+ { kind: 'setText', node: 'cellX', text: 'Hi' },+ ]);+ // the cursor lands at the end of the typed chunk (offset 2)+ const cursors = action.steps+ .filter((s) => s.kind === 'awareness' && (s as any).x.type === 'cursor')+ .map((s) => (s as any).x.at);+ expect(cursors).toContain(2);+ });+});++describe('every op kind has an animation that ends in the right edit', () => {+ // one representative op per kind → (non-empty steps, terminal edit kind).+ const cases: Array<[DocumentOp, string]> = [+ [+ {+ kind: 'formatText',+ node: 'b1',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ 'formatText',+ ],+ [+ { kind: 'clearFormat', node: 'b1', match: 'x', scope: { kind: 'all' } },+ 'clearFormat',+ ],+ [+ { kind: 'clearFormat', node: 'b1', scope: { kind: 'all' } },+ 'clearFormat',+ ],+ [+ { kind: 'formatNode', node: 't1', format: 'italic', on: true },+ 'formatNode',+ ],+ [{ kind: 'clearNodeFormat', node: 't1' }, 'clearNodeFormat'],+ [+ {+ kind: 'markText',+ node: 'b1',+ match: 'x',+ on: true,+ scope: { kind: 'all' },+ },+ 'markText',+ ],+ [+ {+ kind: 'linkText',+ node: 'b1',+ match: 'x',+ url: 'u',+ scope: { kind: 'all' },+ },+ 'linkText',+ ],+ [{ kind: 'setText', node: 'b1', text: 'a' }, 'setText'],+ [+ {+ kind: 'replaceText',+ node: 'b1',+ find: 'a',+ to: 'b',+ scope: { kind: 'all' },+ },+ 'replaceText',+ ],+ [{ kind: 'appendText', node: 'b1', text: 'a' }, 'insertText'],+ [{ kind: 'prependText', node: 'b1', text: 'a' }, 'insertText'],+ [+ { kind: 'setBlockType', node: 'b1', block: 'heading', level: 1 },+ 'setBlockType',+ ],+ [{ kind: 'setListType', nodes: ['b1'], list: 'bullet' }, 'setListType'],+ [{ kind: 'setChecked', node: 'b1', checked: true }, 'setChecked'],+ [{ kind: 'setIndent', node: 'b1', indent: 'in' }, 'setIndent'],+ [+ {+ kind: 'insertNode',+ ref: 'r1',+ spec: { block: 'divider' },+ at: { after: 'b1' },+ },+ 'insertNode',+ ],+ [+ {+ kind: 'insertInline',+ ref: 'r1',+ node: 'b1',+ at: 0,+ spec: { inline: 'linebreak' },+ },+ 'insertInline',+ ],+ [{ kind: 'moveNode', node: 'b1', at: { before: 'b2' } }, 'moveNode'],+ [{ kind: 'removeNode', node: 'b1' }, 'removeNode'],+ [+ { kind: 'mergeBlocks', nodes: ['b1', 'b2'], separator: ' ' },+ 'mergeBlocks',+ ],+ [{ kind: 'setCell', table: 't', row: 0, col: 0, text: 'a' }, 'setText'],+ [{ kind: 'addRow', table: 't' }, 'addRow'],+ [{ kind: 'addColumn', table: 't' }, 'addColumn'],+ [{ kind: 'removeRow', table: 't', row: 1 }, 'removeRow'],+ [{ kind: 'removeColumn', table: 't', col: 1 }, 'removeColumn'],+ ];++ it.each(cases)('%o animates and ends in an edit', (op, terminalKind) => {+ const docReader = reader({+ locate: () => [{ node: 't1', start: 0, end: 1 }],+ textLength: () => 3,+ cellNode: () => 'cellX',+ });+ const steps = run(op, { docReader }).steps;+ expect(steps.length).toBeGreaterThan(0);+ const edits = onlyEdits(steps);+ expect(edits.length).toBeGreaterThan(0);+ expect(edits.at(-1)!.kind).toBe(terminalKind);+ });+});++// ── sweepSelect via formatText (the simplest animator that uses it on one match) ──++describe('sweepSelect — anchoring & grow offsets', () => {+ it('left-anchored: cursor at END, each highlight grows the left edge toward start', () => {+ // sweeps=3. len=10, start=0,end=10.+ // grow(i) = round(i/4 * 10): i=1→3 (round(2.5)=3), i=2→5, i=3→8 (round(7.5)=8)+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ integer: [30, 3, 75, 75, 75, 145],+ direction: 'left',+ }),+ docReader: reader({ locate: () => [{ node: 't', start: 0, end: 10 }] }),+ }+ );+ const hl = highlights(action.steps);+ expect(cursors(action.steps)[0]!.x.at).toBe(10); // anchored at end+ expect(hl.map((h) => [h.x.span.start, h.x.span.end])).toEqual([+ [7, 10], // end - 3+ [5, 10], // end - 5+ [2, 10], // end - 8+ [0, 10], // final+ ]);+ });++ it('right-anchored: cursor at START, each highlight grows the right edge toward end', () => {+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ integer: [30, 3, 75, 75, 75, 145],+ direction: 'right',+ }),+ docReader: reader({ locate: () => [{ node: 't', start: 0, end: 10 }] }),+ }+ );+ const hl = highlights(action.steps);+ expect(cursors(action.steps)[0]!.x.at).toBe(0); // anchored at start+ expect(hl.map((h) => [h.x.span.start, h.x.span.end])).toEqual([+ [0, 3],+ [0, 5],+ [0, 8],+ [0, 10], // final+ ]);+ });++ it('right-anchored with a non-zero start offsets grow from start', () => {+ // start=4,end=11 (len 7), sweeps=3. grow i=1→round(7/4)=2, i=2→round(14/4=3.5)=4, i=3→round(21/4=5.25)=5+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ integer: [30, 3, 75, 75, 75, 145],+ direction: 'right',+ }),+ docReader: reader({ locate: () => [{ node: 't', start: 4, end: 11 }] }),+ }+ );+ const hl = highlights(action.steps);+ expect(hl.map((h) => [h.x.span.start, h.x.span.end])).toEqual([+ [4, 6],+ [4, 8],+ [4, 9],+ [4, 11], // final span = (start,end)+ ]);+ });++ it('sweeps=0 → exactly one final highlight, no incremental sweeps', () => {+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ integer: [30, 0, 90],+ direction: 'left',+ }),+ docReader: reader({ locate: () => [{ node: 't', start: 2, end: 8 }] }),+ }+ );+ const hl = highlights(action.steps);+ expect(hl).toHaveLength(1);+ expect([hl[0]!.x.span.start, hl[0]!.x.span.end]).toEqual([2, 8]);+ // two pauses (preSelect + settle); sweepPause loop ran 0 times+ expect(pauses(action.steps)).toEqual([30, 90]);+ });++ it('sweeps=5 → six highlights with monotonically growing spans (left)', () => {+ // len=12, sweeps=5: grow i = round(i/6*12) = round(2i) = 2,4,6,8,10+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ integer: [30, 5, 50, 50, 50, 50, 50, 90],+ direction: 'left',+ }),+ docReader: reader({ locate: () => [{ node: 't', start: 0, end: 12 }] }),+ }+ );+ const hl = highlights(action.steps);+ expect(hl).toHaveLength(6);+ expect(hl.map((h) => h.x.span.start)).toEqual([10, 8, 6, 4, 2, 0]);+ expect(hl.every((h) => h.x.span.end === 12)).toBe(true);+ });++ it('len=0 (degenerate, start===end): all sweeps collapse to a zero-width span', () => {+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ integer: [30, 5, 50, 50, 50, 50, 50, 90],+ direction: 'left',+ }),+ docReader: reader({ locate: () => [{ node: 't', start: 5, end: 5 }] }),+ }+ );+ const hl = highlights(action.steps);+ expect(hl).toHaveLength(6);+ expect(hl.every((h) => h.x.span.start === 5 && h.x.span.end === 5)).toBe(+ true+ );+ expect(cursors(action.steps)[0]!.x.at).toBe(5);+ });+});++describe('sweepSelect — pause ms come straight from the integer draws', () => {+ it('0 sweeps → only the settle pause', () => {+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({ integer: [30, 0, 90] }),+ docReader: reader({ locate: () => [{ node: 't', start: 0, end: 4 }] }),+ }+ );+ expect(pauses(action.steps)).toEqual([30, 90]); // preSelect + settle+ });++ it('3 sweeps → 3 sweep pauses then the settle pause', () => {+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({ integer: [30, 3, 75, 75, 75, 145] }),+ docReader: reader({ locate: () => [{ node: 't', start: 0, end: 4 }] }),+ }+ );+ expect(pauses(action.steps)).toEqual([30, 75, 75, 75, 145]);+ });++ it('5 sweeps → 5 sweep pauses then the settle pause', () => {+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ integer: [30, 5, 109, 109, 109, 109, 109, 199],+ }),+ docReader: reader({ locate: () => [{ node: 't', start: 0, end: 4 }] }),+ }+ );+ expect(pauses(action.steps)).toEqual([30, 109, 109, 109, 109, 109, 199]);+ });+});++describe('typeText — per-char pause = round(msPerChar * typeJitter draw)', () => {+ it('pause = round(30 * real draw): real=0.6 → 18', () => {+ const action = run(+ { kind: 'appendText', node: 'b', text: 'a' },+ {+ randomSource: mockRandomSource({ real: 0.6 }),+ docReader: reader({ textLength: () => 0 }),+ }+ );+ expect(pauses(action.steps)).toEqual([0, 18]); // lead-in (integer 0) then per-char+ });+ it('pause = round(30 * real draw): real=1.5 → 45', () => {+ const action = run(+ { kind: 'appendText', node: 'b', text: 'a' },+ {+ randomSource: mockRandomSource({ real: 1.5 }),+ docReader: reader({ textLength: () => 0 }),+ }+ );+ expect(pauses(action.steps)).toEqual([0, 45]); // lead-in (integer 0) then per-char+ });++ it('chunk offset progression: insert/cursor/pause per 3-char chunk from the start offset', () => {+ // prependText types from offset 0; "abc" is one 3-char chunk → pause round(30*3*1.05)=95+ const action = run(+ { kind: 'prependText', node: 'b', text: 'abc' },+ { randomSource: mockRandomSource({ real: 1.05 }) }+ );+ expect(action.steps).toEqual<DocumentOpStep[]>([+ { kind: 'awareness', x: { type: 'cursor', node: 'b', at: 0 } },+ { kind: 'pause', ms: 0 }, // typeText lead-in (default integer 0)+ {+ kind: 'edit',+ y: { kind: 'insertText', node: 'b', at: 0, text: 'abc' },+ },+ { kind: 'awareness', x: { type: 'cursor', node: 'b', at: 3 } },+ { kind: 'pause', ms: 95 },+ ]);+ });++ it('appendText offsets start at the existing length', () => {+ const action = run(+ { kind: 'appendText', node: 'b', text: 'XY' },+ {+ randomSource: mockRandomSource({ real: 1.05 }),+ docReader: reader({ textLength: () => 7 }),+ }+ );+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'insertText', node: 'b', at: 7, text: 'XY' },+ ]);+ expect(cursors(action.steps).map((c) => c.x.at)).toEqual([7, 9]); // initial + after the chunk+ });++ it('empty append/prepend string → no edits and no type pauses', () => {+ const appendAction = run(+ { kind: 'appendText', node: 'b', text: '' },+ { docReader: reader({ textLength: () => 3 }) }+ );+ // appendText still emits the initial cursor, but no insert/pause+ expect(onlyEdits(appendAction.steps)).toEqual([]);+ expect(pauses(appendAction.steps)).toEqual([]);+ expect(cursors(appendAction.steps)).toHaveLength(1);++ const prependAction = run({ kind: 'prependText', node: 'b', text: '' });+ expect(onlyEdits(prependAction.steps)).toEqual([]);+ expect(pauses(prependAction.steps)).toEqual([]);+ });+});++describe('retype (setText) — delete branch and ordering', () => {+ it('non-empty target: selectAll → preDelete pause → removeText → cursor 0 → typeText', () => {+ // integer draws: preSelect=30, sweeps=0 (single highlight), settle=90, preDelete=120, typeText lead-in=150. real=0.6 → type pause 18.+ const action = run(+ { kind: 'setText', node: 'b', text: 'Z' },+ {+ randomSource: mockRandomSource({+ integer: [30, 0, 90, 120, 150],+ real: 0.6,+ }),+ docReader: reader({ textLength: () => 3 }),+ }+ );+ expect(action.steps).toEqual<DocumentOpStep[]>([+ { kind: 'awareness', x: { type: 'cursor', node: 'b', at: 3 } }, // selectAll cursor (left-anchored at end)+ { kind: 'pause', ms: 30 }, // preSelect rest on anchor+ {+ kind: 'awareness',+ x: { type: 'highlight', node: 'b', span: { start: 0, end: 3 } },+ },+ { kind: 'pause', ms: 90 }, // settle+ { kind: 'pause', ms: 120 }, // preDelete+ { kind: 'edit', y: { kind: 'removeText', node: 'b', at: 0, len: 3 } },+ { kind: 'awareness', x: { type: 'cursor', node: 'b', at: 0 } },+ { kind: 'pause', ms: 150 }, // typeText lead-in+ { kind: 'edit', y: { kind: 'insertText', node: 'b', at: 0, text: 'Z' } },+ { kind: 'awareness', x: { type: 'cursor', node: 'b', at: 1 } },+ { kind: 'pause', ms: 18 }, // typeJitter real 0.6 → round(30*0.6)+ { kind: 'edit', y: { kind: 'setText', node: 'b', text: 'Z' } }, // canonicalize+ ]);+ });++ it('empty target (textLength 0): no preDelete pause, no removeText, no cursor-0 reset', () => {+ const action = run(+ { kind: 'setText', node: 'b', text: 'Hi' },+ {+ randomSource: mockRandomSource({+ integer: [30, 0, 90, 150],+ real: 0.6,+ }),+ docReader: reader({ textLength: () => 0 }),+ }+ );+ // selectAll over a 0-length node still emits cursor + one final highlight + preSelect + settle pause+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'insertText', node: 'b', at: 0, text: 'Hi' },+ { kind: 'setText', node: 'b', text: 'Hi' },+ ]);+ // removeText must NOT appear (delete branch skipped for empty target)+ expect(+ action.steps.filter(+ (s) => s.kind === 'edit' && (s as any).y.kind === 'removeText'+ )+ ).toHaveLength(0);+ // no preDelete pause — preSelect(30) + settle(90) + typeText lead-in(150) + 1 chunk pause round(30*2*0.6)=36+ expect(pauses(action.steps)).toEqual([30, 90, 150, 36]);+ });++ it('empty target with empty text → selectAll then a canonicalizing setText (clears stray inlines)', () => {+ const action = run(+ { kind: 'setText', node: 'b', text: '' },+ {+ randomSource: mockRandomSource({ integer: [30, 0, 90] }),+ docReader: reader({ textLength: () => 0 }),+ }+ );+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'setText', node: 'b', text: '' },+ ]);+ });+});++describe('sweepEachThen — one selection group per match, betweenNodes pause, single edit', () => {+ it('zero matches → just the single edit, no selection steps', () => {+ const action = run(+ {+ kind: 'formatText',+ node: 'b',+ match: 'nope',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ { docReader: reader({ locate: () => [] }) }+ );+ expect(action.steps).toEqual<DocumentOpStep[]>([+ {+ kind: 'edit',+ y: {+ kind: 'formatText',+ node: 'b',+ match: 'nope',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ },+ ]);+ });++ it('three matches → a betweenNodes pause precedes the 2nd and 3rd group (not the 1st)', () => {+ const matches: Match[] = [+ { node: 't1', start: 0, end: 1 },+ { node: 't2', start: 0, end: 1 },+ { node: 't3', start: 0, end: 1 },+ ];+ // 0 sweeps per group; each group = cursor + preSelect pause + final highlight + settle(90).+ // integer draws per group: preSelect(30), sweeps(0), settle(90); betweenNodes(180) before groups 2 & 3.+ const action = run(+ {+ kind: 'markText',+ node: 'b',+ match: 'x',+ on: true,+ scope: { kind: 'all' },+ },+ {+ randomSource: mockRandomSource({+ integer: [30, 0, 90, 180, 30, 0, 90, 180, 30, 0, 90],+ direction: 'left',+ }),+ docReader: reader({ locate: () => matches }),+ }+ );+ // betweenNodes pause (180) appears exactly twice+ expect(pauses(action.steps).filter((ms) => ms === 180)).toHaveLength(2);+ expect(highlights(action.steps).map((h) => h.x.node)).toEqual([+ 't1',+ 't2',+ 't3',+ ]);+ expect(onlyEdits(action.steps)).toEqual([+ {+ kind: 'markText',+ node: 'b',+ match: 'x',+ on: true,+ scope: { kind: 'all' },+ },+ ]);+ });+});++describe('clearFormat animator — match vs whole-block branches', () => {+ it('with a match → sweepEachThen (per-occurrence selection)', () => {+ const action = run(+ { kind: 'clearFormat', node: 'b', match: 'x', scope: { kind: 'all' } },+ {+ docReader: reader({ locate: () => [{ node: 't1', start: 0, end: 1 }] }),+ }+ );+ expect(highlights(action.steps).length).toBeGreaterThan(0);+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'clearFormat', node: 'b', match: 'x', scope: { kind: 'all' } },+ ]);+ });+ it('without a match → selectAll over the whole block then one clear', () => {+ const action = run(+ { kind: 'clearFormat', node: 'b', scope: { kind: 'all' } },+ {+ randomSource: mockRandomSource({ integer: [30, 0, 90] }),+ docReader: reader({ textLength: () => 6 }),+ }+ );+ const hl = highlights(action.steps);+ expect(hl).toHaveLength(1);+ expect([hl[0]!.x.span.start, hl[0]!.x.span.end]).toEqual([0, 6]);+ expect(onlyEdits(action.steps)).toEqual([+ {+ kind: 'clearFormat',+ node: 'b',+ match: undefined,+ scope: { kind: 'all' },+ },+ ]);+ });+});++describe('formatNode / clearNodeFormat animators — sweep over node length', () => {+ it('formatNode selects the whole text-node then formats', () => {+ const action = run(+ { kind: 'formatNode', node: 't1', format: 'italic', on: true },+ {+ randomSource: mockRandomSource({ integer: [30, 0, 90] }),+ docReader: reader({ textLength: () => 4 }),+ }+ );+ const hl = highlights(action.steps);+ expect([hl[0]!.x.span.start, hl[0]!.x.span.end]).toEqual([0, 4]);+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'formatNode', node: 't1', format: 'italic', on: true },+ ]);+ });+ it('clearNodeFormat selects the whole text-node then clears', () => {+ const action = run(+ { kind: 'clearNodeFormat', node: 't1' },+ {+ randomSource: mockRandomSource({ integer: [30, 0, 90] }),+ docReader: reader({ textLength: () => 4 }),+ }+ );+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'clearNodeFormat', node: 't1' },+ ]);+ });+});++describe('insertNode animator — typed vs whole', () => {+ it('heading is a TYPED_BLOCK: inserted empty then typed', () => {+ const action = run({+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'heading', level: 2, text: 'Hi' },+ at: { after: 'b1' },+ });+ expect(onlyEdits(action.steps)).toEqual([+ {+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'heading', level: 2, text: '' },+ at: { after: 'b1' },+ },+ { kind: 'insertText', node: 'r', at: 0, text: 'Hi' },+ ]);+ });+ it('quote/code are TYPED_BLOCKS too', () => {+ const q = run({+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'quote', text: 'X' },+ at: { appendToRoot: true },+ });+ expect(onlyEdits(q.steps).map((e: any) => e.kind)).toEqual([+ 'insertNode',+ 'insertText',+ ]);+ });+ it('a typed block with empty text inserts empty and types nothing', () => {+ const action = run({+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'paragraph', text: '' },+ at: { appendToRoot: true },+ });+ expect(onlyEdits(action.steps)).toEqual([+ {+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'paragraph', text: '' },+ at: { appendToRoot: true },+ },+ ]);+ });+ it('a typed block with NO text key inserts empty and types nothing', () => {+ const action = run({+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'paragraph' },+ at: { appendToRoot: true },+ });+ expect(onlyEdits(action.steps)).toEqual([+ {+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'paragraph', text: '' },+ at: { appendToRoot: true },+ },+ ]);+ });+ it('list builds item by item: empty list, then append + type each item', () => {+ const action = run({+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'list', list: 'bullet', items: ['a', 'b'] },+ at: { appendToRoot: true },+ });+ expect(onlyEdits(action.steps)).toEqual([+ {+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'list', list: 'bullet', items: [] },+ at: { appendToRoot: true },+ },+ {+ kind: 'appendListItem',+ ref: 'r~li-0',+ node: 'r',+ text: '',+ checked: undefined,+ },+ { kind: 'insertText', node: 'r~li-0', at: 0, text: 'a' },+ {+ kind: 'appendListItem',+ ref: 'r~li-1',+ node: 'r',+ text: '',+ checked: undefined,+ },+ { kind: 'insertText', node: 'r~li-1', at: 0, text: 'b' },+ ]);+ // each item's caret drops into its own fresh node before typing+ expect(cursors(action.steps).map((c) => c.x.node)).toContain('r~li-0');+ expect(cursors(action.steps).map((c) => c.x.node)).toContain('r~li-1');+ });+ it('check list seeds each appended item as unchecked', () => {+ const action = run({+ kind: 'insertNode',+ ref: 'r',+ spec: { block: 'list', list: 'check', items: ['x'] },+ at: { appendToRoot: true },+ });+ expect(onlyEdits(action.steps)).toContainEqual({+ kind: 'appendListItem',+ ref: 'r~li-0',+ node: 'r',+ text: '',+ checked: false,+ });+ });+});++describe('mergeBlocks animator — between-node pauses', () => {+ it('three nodes → two betweenNodes pauses and one merge edit', () => {+ const action = run(+ { kind: 'mergeBlocks', nodes: ['b1', 'b2', 'b3'], separator: ' ' },+ {+ randomSource: mockRandomSource({+ integer: [30, 0, 90, 180, 30, 0, 90, 180, 30, 0, 90],+ }),+ docReader: reader({ textLength: () => 2 }),+ }+ );+ expect(pauses(action.steps).filter((ms) => ms === 180)).toHaveLength(2);+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'mergeBlocks', nodes: ['b1', 'b2', 'b3'], separator: ' ' },+ ]);+ expect(highlights(action.steps).map((h) => h.x.node)).toEqual([+ 'b1',+ 'b2',+ 'b3',+ ]);+ });+});++describe('addRow/addColumn/removeRow/removeColumn — focus then table edit', () => {+ it('addRow at index', () => {+ const action = run(+ { kind: 'addRow', table: 't', at: 2 },+ { randomSource: mockRandomSource({ integer: [90] }) }+ );+ expect(action.steps).toEqual<DocumentOpStep[]>([+ { kind: 'awareness', x: { type: 'cursor', node: 't', at: 0 } },+ { kind: 'pause', ms: 90 },+ { kind: 'edit', y: { kind: 'addRow', table: 't', at: 2 } },+ ]);+ });+ it('removeColumn', () => {+ const action = run({ kind: 'removeColumn', table: 't', col: 1 });+ expect(onlyEdits(action.steps)).toEqual([+ { kind: 'removeColumn', table: 't', col: 1 },+ ]);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/queue/animators.ts b/js/ai-editing-worker/src/ai-editing/queue/animators.tsnew file mode 100644index 0000000000..bfe3875403--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/animators.ts@@ -0,0 +1,573 @@+/**+ * Per-op animators: each turns ONE `DocumentOp` into the flat list of steps that+ * play it out. This is where animation intent lives — declared by the op, not+ * inferred from a diff. Primitives (`sweepSelect`, `typeText`, `focus`) compose+ * the common motions; `animate()` dispatches by op kind.+ *+ * Pure: an animator reads `docReader` to plan (lengths, match offsets) and emits+ * data. Nondeterminism comes only from `randomSource` (`integer`/`real`/+ * `direction`), so a fixed mock makes the whole action reproducible.+ */+import { match } from 'ts-pattern';+import type { DocReader } from '../doc';+import type {+ DocumentOp,+ NodeRef,+ NodeSpec,+ Offset,+ Position,+ Scope,+} from '../editor';+import type { RandomSource } from './random-source';+import type { DocumentOpStep, RandomRanges } from './types';++export type AnimatorCtx = {+ randomSource: RandomSource;+ docReader: DocReader;+ msPerChar: number;+ ranges: RandomRanges;+};++const cursor = (node: NodeRef, at: Offset): DocumentOpStep => ({+ kind: 'awareness',+ x: { type: 'cursor', node, at },+});+const highlight = (+ node: NodeRef,+ start: Offset,+ end: Offset+): DocumentOpStep => ({+ kind: 'awareness',+ x: { type: 'highlight', node, span: { start, end } },+});+const edit = (y: DocumentOp): DocumentOpStep => ({ kind: 'edit', y });++/**+ * Drag-select [start, end] on a node: rest the caret on one end (direction-biased)+ * for a short beat, then grow the selection to the full span in `highlightSweeps`+ * (0–5) increments, then settle. Selection is awareness-only — it reads against+ * the unmutated text, so offsets stay valid.+ */+function sweepSelect(+ node: NodeRef,+ start: Offset,+ end: Offset,+ ctx: AnimatorCtx+): DocumentOpStep[] {+ const { randomSource, ranges } = ctx;+ const leftward = randomSource.direction() === 'left';+ const len = Math.max(0, end - start);+ // Land the caret on the anchor and let it rest before the drag begins.+ const steps: DocumentOpStep[] = [+ cursor(node, leftward ? end : start),+ { kind: 'pause', ms: randomSource.integer(ranges.preSelectPauseMs) },+ ];+ const sweeps = randomSource.integer(ranges.highlightSweeps);+ for (let i = 1; i <= sweeps; i++) {+ const grow = Math.round((i / (sweeps + 1)) * len);+ steps.push(+ leftward+ ? highlight(node, end - grow, end)+ : highlight(node, start, start + grow)+ );+ steps.push({+ kind: 'pause',+ ms: randomSource.integer(ranges.sweepPauseMs),+ });+ }+ steps.push(highlight(node, start, end));+ steps.push({ kind: 'pause', ms: randomSource.integer(ranges.settlePauseMs) });+ return steps;+}++/** How many characters each typing keystroke emits. */+const TYPE_CHUNK = 3;++/** Type `text` into a node `TYPE_CHUNK` chars at a time from `from`, cursor+ * following. A lead-in beat separates positioning (caret placement) from typing. */+function typeText(+ node: NodeRef,+ text: string,+ from: Offset,+ ctx: AnimatorCtx+): DocumentOpStep[] {+ const { randomSource, ranges, msPerChar } = ctx;+ if (text.length === 0) return [];+ const steps: DocumentOpStep[] = [+ { kind: 'pause', ms: randomSource.integer(ranges.settlePauseMs) },+ ];+ for (let k = 0; k < text.length; k += TYPE_CHUNK) {+ const chunk = text.slice(k, k + TYPE_CHUNK);+ steps.push(edit({ kind: 'insertText', node, at: from + k, text: chunk }));+ steps.push(cursor(node, from + k + chunk.length));+ steps.push({+ kind: 'pause',+ ms: Math.round(+ msPerChar * chunk.length * randomSource.real(ranges.typeJitter)+ ),+ });+ }+ return steps;+}++/** Brief focus on a node (cursor + settle pause), for structural ops. */+function focus(node: NodeRef, ctx: AnimatorCtx): DocumentOpStep[] {+ return [+ cursor(node, 0),+ { kind: 'pause', ms: ctx.randomSource.integer(ctx.ranges.settlePauseMs) },+ ];+}++/** Select the whole of a node's current text, then settle. */+function selectAll(node: NodeRef, ctx: AnimatorCtx): DocumentOpStep[] {+ return sweepSelect(node, 0, ctx.docReader.textLength(node), ctx);+}++/** Sweep each occurrence of a match (with a pause between), then one edit. */+function sweepEachThen(+ id: string,+ match: string,+ scope: Scope,+ finalEdit: DocumentOp,+ ctx: AnimatorCtx+): DocumentOpStep[] {+ const matches = ctx.docReader.locate(id, match, scope);+ const steps: DocumentOpStep[] = [];+ matches.forEach((m, i) => {+ if (i > 0)+ steps.push({+ kind: 'pause',+ ms: ctx.randomSource.integer(ctx.ranges.betweenNodesPauseMs),+ });+ steps.push(...sweepSelect(m.node, m.start, m.end, ctx));+ });+ steps.push(edit(finalEdit));+ return steps;+}++/* Replace a node's text by selecting all, deleting, and typing the new content.*/+function retype(+ node: NodeRef,+ text: string,+ ctx: AnimatorCtx+): DocumentOpStep[] {+ const len = ctx.docReader.textLength(node);+ const steps: DocumentOpStep[] = [...selectAll(node, ctx)];+ if (len > 0) {+ steps.push({+ kind: 'pause',+ ms: ctx.randomSource.integer(ctx.ranges.preDeletePauseMs),+ });+ steps.push(edit({ kind: 'removeText', node, at: 0, len }));+ steps.push(cursor(node, 0));+ }+ steps.push(...typeText(node, text, 0, ctx));+ steps.push(edit({ kind: 'setText', node, text }));+ return steps;+}++/** Block specs whose text we type out (vs. build whole). */+const TYPED_BLOCKS = new Set(['paragraph', 'heading', 'quote', 'code']);++/** Move the caret to the insertion point (end of the anchor block) and pause —+ * so an atomic block reads as "place the caret here, then it appears". */+function insertLead(at: Position, ctx: AnimatorCtx): DocumentOpStep[] {+ const settle: DocumentOpStep = {+ kind: 'pause',+ ms: ctx.randomSource.integer(ctx.ranges.settlePauseMs),+ };+ const anchor =+ 'after' in at ? at.after : 'before' in at ? at.before : undefined;+ return anchor === undefined+ ? [settle]+ : [cursor(anchor, ctx.docReader.textLength(anchor)), settle];+}++/** Insert a block, animated by what it is:+ * - typed text block (paragraph/heading/quote/code): insert empty, then type it.+ * - atomic blocks (divider/image/video/equation/list/table): caret to the spot,+ * brief pause, it appears, caret moves into it. */+function animateInsertNode(+ o: Extract<DocumentOp, { kind: 'insertNode' }>,+ ctx: AnimatorCtx+): DocumentOpStep[] {+ const spec = o.spec;+ if ('block' in spec && TYPED_BLOCKS.has(spec.block)) {+ const text = (spec as { text?: string }).text ?? '';+ const steps: DocumentOpStep[] = [+ edit({+ kind: 'insertNode',+ ref: o.ref,+ spec: { ...spec, text: '' } as NodeSpec,+ at: o.at,+ }),+ ];+ if (text) {+ steps.push(cursor(o.ref, 0));+ steps.push(...typeText(o.ref, text, 0, ctx));+ }+ return steps;+ }+ if ('block' in spec && spec.block === 'list') {+ // Insert an empty list, then build it item by item: each item is a fresh+ // empty list item (the simulated Enter) the caret drops into and types out —+ // so a list reads as someone writing one bullet, hitting Enter, writing the next.+ const checked = spec.list === 'check' ? false : undefined;+ const steps: DocumentOpStep[] = [+ ...insertLead(o.at, ctx),+ edit({+ kind: 'insertNode',+ ref: o.ref,+ spec: { ...spec, items: [] } as NodeSpec,+ at: o.at,+ }),+ ];+ spec.items.forEach((text, i) => {+ const itemRef = `${o.ref}~li-${i}`;+ steps.push(+ edit({+ kind: 'appendListItem',+ ref: itemRef,+ node: o.ref,+ text: '',+ checked,+ })+ );+ steps.push(cursor(itemRef, 0));+ steps.push(...typeText(itemRef, text, 0, ctx));+ });+ return steps;+ }+ return [+ ...insertLead(o.at, ctx),+ edit({ kind: 'insertNode', ref: o.ref, spec, at: o.at }),+ cursor(o.ref, 0),+ ];+}++export function animate(op: DocumentOp, ctx: AnimatorCtx): DocumentOpStep[] {+ return (+ match(op)+ .returnType<DocumentOpStep[]>()+ // inline: sweep each occurrence, then one match-based edit+ .with({ kind: 'formatText' }, (o) =>+ sweepEachThen(+ o.node,+ o.match,+ o.scope,+ {+ kind: 'formatText',+ node: o.node,+ match: o.match,+ format: o.format,+ on: o.on,+ scope: o.scope,+ },+ ctx+ )+ )+ .with({ kind: 'markText' }, (o) =>+ sweepEachThen(+ o.node,+ o.match,+ o.scope,+ {+ kind: 'markText',+ node: o.node,+ match: o.match,+ on: o.on,+ scope: o.scope,+ },+ ctx+ )+ )+ .with({ kind: 'linkText' }, (o) =>+ sweepEachThen(+ o.node,+ o.match,+ o.scope,+ {+ kind: 'linkText',+ node: o.node,+ match: o.match,+ url: o.url,+ scope: o.scope,+ },+ ctx+ )+ )+ .with({ kind: 'replaceText' }, (o) =>+ sweepEachThen(+ o.node,+ o.find,+ o.scope,+ {+ kind: 'replaceText',+ node: o.node,+ find: o.find,+ to: o.to,+ scope: o.scope,+ },+ ctx+ )+ )+ .with({ kind: 'clearFormat' }, (o) =>+ o.match === undefined+ ? [+ ...selectAll(o.node, ctx),+ edit({+ kind: 'clearFormat',+ node: o.node,+ match: undefined,+ scope: o.scope,+ }),+ ]+ : sweepEachThen(+ o.node,+ o.match,+ o.scope,+ {+ kind: 'clearFormat',+ node: o.node,+ match: o.match,+ scope: o.scope,+ },+ ctx+ )+ )+ .with({ kind: 'formatNode' }, (o) => [+ ...sweepSelect(o.node, 0, ctx.docReader.textLength(o.node), ctx),+ edit({ kind: 'formatNode', node: o.node, format: o.format, on: o.on }),+ ])+ .with({ kind: 'clearNodeFormat' }, (o) => [+ ...sweepSelect(o.node, 0, ctx.docReader.textLength(o.node), ctx),+ edit({ kind: 'clearNodeFormat', node: o.node }),+ ])+ // text content: type+ .with({ kind: 'setText' }, (o) => retype(o.node, o.text, ctx))+ .with({ kind: 'setEquation' }, (o) => [+ ...selectAll(o.node, ctx),+ edit({ kind: 'setEquation', node: o.node, tex: o.tex }),+ ])+ .with({ kind: 'appendText' }, (o) => {+ const len = ctx.docReader.textLength(o.node);+ return [cursor(o.node, len), ...typeText(o.node, o.text, len, ctx)];+ })+ .with({ kind: 'prependText' }, (o) => [+ cursor(o.node, 0),+ ...typeText(o.node, o.text, 0, ctx),+ ])+ // block type / list+ // select the line, then transform it — like a person selecting and restyling.+ .with({ kind: 'setBlockType' }, (o) => [+ ...selectAll(o.node, ctx),+ edit({+ kind: 'setBlockType',+ node: o.node,+ block: o.block,+ level: o.level,+ language: o.language,+ }),+ ])+ .with({ kind: 'setListType' }, (o) => [+ ...focus(o.nodes[0]!, ctx),+ edit({ kind: 'setListType', nodes: o.nodes, list: o.list }),+ ])+ .with({ kind: 'setChecked' }, (o) => [+ ...focus(o.node, ctx),+ edit({ kind: 'setChecked', node: o.node, checked: o.checked }),+ ])+ .with({ kind: 'setIndent' }, (o) => [+ ...focus(o.node, ctx),+ edit({ kind: 'setIndent', node: o.node, indent: o.indent }),+ ])+ // structure+ .with({ kind: 'insertNode' }, (o) => animateInsertNode(o, ctx))+ // caret to the offset, brief pause, the inline node appears.+ .with({ kind: 'insertInline' }, (o) => [+ cursor(o.node, o.at),+ {+ kind: 'pause',+ ms: ctx.randomSource.integer(ctx.ranges.settlePauseMs),+ },+ edit({+ kind: 'insertInline',+ ref: o.ref,+ node: o.node,+ at: o.at,+ spec: o.spec,+ }),+ ])+ // select the whole block first, so the user sees what is about to move.+ .with({ kind: 'moveNode' }, (o) => [+ ...selectAll(o.node, ctx),+ edit({ kind: 'moveNode', node: o.node, at: o.at }),+ ])+ // select the whole block, hesitate, then delete — destroying content is deliberate.+ .with({ kind: 'removeNode' }, (o) => [+ ...selectAll(o.node, ctx),+ {+ kind: 'pause',+ ms: ctx.randomSource.integer(ctx.ranges.preDeletePauseMs),+ },+ edit({ kind: 'removeNode', node: o.node }),+ ])+ // highlight each block being combined, in turn, then merge.+ .with({ kind: 'mergeBlocks' }, (o) => {+ const steps: DocumentOpStep[] = [];+ o.nodes.forEach((node, i) => {+ if (i > 0)+ steps.push({+ kind: 'pause',+ ms: ctx.randomSource.integer(ctx.ranges.betweenNodesPauseMs),+ });+ steps.push(...selectAll(node, ctx));+ });+ steps.push(+ edit({ kind: 'mergeBlocks', nodes: o.nodes, separator: o.separator })+ );+ return steps;+ })+ .with({ kind: 'insertListItemAfter' }, (o) => [+ ...insertLead({ after: o.node }, ctx),+ edit({+ kind: 'insertListItemAfter',+ ref: o.ref,+ node: o.node,+ text: '',+ list: o.list,+ }),+ cursor(o.ref, 0),+ ...typeText(o.ref, o.text, 0, ctx),+ ])+ .with({ kind: 'insertListItemBefore' }, (o) => [+ ...insertLead({ before: o.node }, ctx),+ edit({+ kind: 'insertListItemBefore',+ ref: o.ref,+ node: o.node,+ text: '',+ list: o.list,+ }),+ cursor(o.ref, 0),+ ...typeText(o.ref, o.text, 0, ctx),+ ])+ .with({ kind: 'appendListItem' }, (o) => [+ ...focus(o.node, ctx),+ edit({+ kind: 'appendListItem',+ ref: o.ref,+ node: o.node,+ text: '',+ checked: o.checked,+ }),+ cursor(o.ref, 0),+ ...typeText(o.ref, o.text, 0, ctx),+ ])+ .with({ kind: 'prependListItem' }, (o) => [+ ...focus(o.node, ctx),+ edit({+ kind: 'prependListItem',+ ref: o.ref,+ node: o.node,+ text: '',+ checked: o.checked,+ }),+ cursor(o.ref, 0),+ ...typeText(o.ref, o.text, 0, ctx),+ ])+ .with({ kind: 'removeListItem' }, (o) => [+ ...selectAll(o.node, ctx),+ {+ kind: 'pause',+ ms: ctx.randomSource.integer(ctx.ranges.preDeletePauseMs),+ },+ edit({ kind: 'removeListItem', node: o.node }),+ ])+ // tables+ .with({ kind: 'setCell' }, (o) =>+ retype(ctx.docReader.cellNode(o.table, o.row, o.col), o.text, ctx)+ )+ .with({ kind: 'addRow' }, (o) => [+ ...focus(o.table, ctx),+ edit({ kind: 'addRow', table: o.table, at: o.at }),+ ])+ .with({ kind: 'addColumn' }, (o) => [+ ...focus(o.table, ctx),+ edit({ kind: 'addColumn', table: o.table, at: o.at }),+ ])+ .with({ kind: 'removeRow' }, (o) => [+ ...focus(o.table, ctx),+ edit({ kind: 'removeRow', table: o.table, row: o.row }),+ ])+ .with({ kind: 'removeColumn' }, (o) => [+ ...focus(o.table, ctx),+ edit({ kind: 'removeColumn', table: o.table, col: o.col }),+ ])+ // media / date — focus the node, apply the property change+ .with({ kind: 'setImageAlt' }, (o) => [+ ...focus(o.node, ctx),+ edit({ kind: 'setImageAlt', node: o.node, alt: o.alt }),+ ])+ .with({ kind: 'setImageUrl' }, (o) => [+ ...focus(o.node, ctx),+ edit({ kind: 'setImageUrl', node: o.node, url: o.url }),+ ])+ .with({ kind: 'setVideoUrl' }, (o) => [+ ...focus(o.node, ctx),+ edit({ kind: 'setVideoUrl', node: o.node, url: o.url }),+ ])+ .with({ kind: 'setVideoControls' }, (o) => [+ ...focus(o.node, ctx),+ edit({ kind: 'setVideoControls', node: o.node, controls: o.controls }),+ ])+ .with({ kind: 'setDate' }, (o) => [+ ...focus(o.node, ctx),+ edit({+ kind: 'setDate',+ node: o.node,+ date: o.date,+ displayFormat: o.displayFormat,+ }),+ ])+ // animation primitives — pass through as-is (animators call these directly,+ // but they should never be dispatched through animate() in normal use)+ .with({ kind: 'insertText' }, (o) => [edit(o)])+ .with({ kind: 'removeText' }, (o) => [edit(o)])+ // inline text insert: caret to the inline node, then type the run out. The+ // first chunk creates the text node (stamped with a derived ref so it's+ // addressable); later chunks grow it via `insertText` on that ref.+ .with({ kind: 'insertTextAfterInline' }, (o) => {+ if (o.text.length === 0) return [edit(o)];+ const ref = `${o.inline}~after-text`;+ const { randomSource, ranges, msPerChar } = ctx;+ const typePause = (n: number): DocumentOpStep => ({+ kind: 'pause',+ ms: Math.round(msPerChar * n * randomSource.real(ranges.typeJitter)),+ });+ const first = o.text.slice(0, TYPE_CHUNK);+ const steps: DocumentOpStep[] = [+ cursor(o.inline, 0),+ { kind: 'pause', ms: randomSource.integer(ranges.settlePauseMs) },+ edit({+ kind: 'insertTextAfterInline',+ ref,+ inline: o.inline,+ text: first,+ }),+ cursor(ref, first.length),+ typePause(first.length),+ ];+ for (let k = TYPE_CHUNK; k < o.text.length; k += TYPE_CHUNK) {+ const chunk = o.text.slice(k, k + TYPE_CHUNK);+ steps.push(+ edit({ kind: 'insertText', node: ref, at: k, text: chunk })+ );+ steps.push(cursor(ref, k + chunk.length));+ steps.push(typePause(chunk.length));+ }+ return steps;+ })+ .exhaustive()+ );+}diff --git a/js/ai-editing-worker/src/ai-editing/queue/index.ts b/js/ai-editing-worker/src/ai-editing/queue/index.tsnew file mode 100644index 0000000000..8cdd79d30c--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/index.ts@@ -0,0 +1,4 @@+export * from './animators';+export * from './random-source';+export * from './runner';+export * from './types';diff --git a/js/ai-editing-worker/src/ai-editing/queue/random-source.test.ts b/js/ai-editing-worker/src/ai-editing/queue/random-source.test.tsnew file mode 100644index 0000000000..16ef8e710d--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/random-source.test.ts@@ -0,0 +1,86 @@+import { describe, expect, it } from 'vitest';+import { mockRandomSource, realRandomSource } from './random-source';++describe('realRandomSource', () => {+ it('integer() stays within the inclusive range', () => {+ const r = realRandomSource();+ for (let i = 0; i < 500; i++) {+ const n = r.integer([2, 5]);+ expect(Number.isInteger(n)).toBe(true);+ expect(n).toBeGreaterThanOrEqual(2);+ expect(n).toBeLessThanOrEqual(5);+ }+ });+ it('integer() covers both ends of the range over many draws', () => {+ const r = realRandomSource();+ const seen = new Set<number>();+ for (let i = 0; i < 500; i++) seen.add(r.integer([0, 3]));+ expect(seen).toEqual(new Set([0, 1, 2, 3]));+ });+ it('real() stays in [min, max)', () => {+ const r = realRandomSource();+ for (let i = 0; i < 500; i++) {+ const n = r.real([0.6, 1.5]);+ expect(n).toBeGreaterThanOrEqual(0.6);+ expect(n).toBeLessThan(1.5);+ }+ });+});++describe('mockRandomSource — defaults', () => {+ it('returns defaults when nothing is specified (ranges are ignored)', () => {+ const r = mockRandomSource();+ expect(r.integer([10, 20])).toBe(0);+ expect(r.real([10, 20])).toBe(0);+ expect(r.direction()).toBe('left');+ });+});++describe('mockRandomSource — scalar constants repeat', () => {+ it('a scalar value is returned on every draw', () => {+ const r = mockRandomSource({ integer: 3, real: 0.8, direction: 'right' });+ for (let i = 0; i < 5; i++) {+ expect(r.integer([0, 0])).toBe(3);+ expect(r.real([0, 0])).toBe(0.8);+ expect(r.direction()).toBe('right');+ }+ });+});++describe('mockRandomSource — sequences', () => {+ it('replays an array in order', () => {+ const r = mockRandomSource({ integer: [1, 2, 3] });+ expect([r.integer([0, 0]), r.integer([0, 0]), r.integer([0, 0])]).toEqual([+ 1, 2, 3,+ ]);+ });+ it('drawers are independent: integer does not consume direction', () => {+ const r = mockRandomSource({+ integer: [1, 2],+ direction: ['right', 'left'],+ });+ expect(r.integer([0, 0])).toBe(1);+ expect(r.direction()).toBe('right');+ expect(r.integer([0, 0])).toBe(2);+ expect(r.direction()).toBe('left');+ });+});++describe('mockRandomSource — sequence overflow', () => {+ it('throws with the label and draw index', () => {+ const r = mockRandomSource({ integer: [7] });+ expect(r.integer([0, 0])).toBe(7);+ expect(() => r.integer([0, 0])).toThrow(+ /integer sequence exhausted at draw 2/+ );+ });+ it('throws for direction overflow', () => {+ const r = mockRandomSource({ direction: ['left'] });+ r.direction();+ expect(() => r.direction()).toThrow(/direction sequence exhausted/);+ });+ it('an empty array sequence throws on the very first draw', () => {+ const r = mockRandomSource({ real: [] });+ expect(() => r.real([0, 0])).toThrow(/exhausted at draw 1/);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/queue/random-source.ts b/js/ai-editing-worker/src/ai-editing/queue/random-source.tsnew file mode 100644index 0000000000..e426e4fb06--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/random-source.ts@@ -0,0 +1,71 @@+/**+ * The single source of nondeterminism for animation: selection direction, pause+ * jitter, and sweep counts. Animators consume it through `integer`/`real` so the+ * ranges (queue params) stay declarative. The real source is backed by random-js+ * (unbiased rejection-sampled integers); the mock replays chosen values, making a+ * full `DocumentOpAction` deterministic and assertable.+ */+import { nativeMath, Random } from 'random-js';++export type Direction = 'left' | 'right';+export type Range = [min: number, max: number];++export interface RandomSource {+ /** Inclusive integer in [min, max], uniform. */+ integer(range: Range): number;+ /** Float in [min, max). */+ real(range: Range): number;+ /** Which end a drag-select anchors to. Biased 60/40 toward 'left'. */+ direction(): Direction;+}++/** Real source backed by random-js + Math.random. Tests use `mockRandomSource`. */+export function realRandomSource(): RandomSource {+ const random = new Random(nativeMath);+ return {+ integer: ([min, max]) => random.integer(min, max),+ real: ([min, max]) => random.real(min, max, false),+ direction: () => (random.bool(0.6) ? 'left' : 'right'),+ };+}++export type MockRandomOptions = {+ /** Constant, or a sequence consumed in order (throws when exhausted). */+ integer?: number | number[];+ real?: number | number[];+ direction?: Direction | Direction[];+};++/**+ * Replaying source. Each method ignores its range and returns chosen values:+ * pass a scalar for a constant (every call returns it) or an array for an exact+ * sequence (overflow throws, to catch under-specified tests). Defaults: integer+ * 0, real 0, direction 'left'.+ */+export function mockRandomSource(opts: MockRandomOptions = {}): RandomSource {+ const integer = drawer('integer', opts.integer, 0);+ const real = drawer('real', opts.real, 0);+ const direction = drawer<Direction>('direction', opts.direction, 'left');+ return {+ integer: () => integer(),+ real: () => real(),+ direction: () => direction(),+ };+}++function drawer<T>(+ label: string,+ value: T | T[] | undefined,+ fallback: T+): () => T {+ if (value === undefined) return () => fallback;+ if (!Array.isArray(value)) return () => value;+ let i = 0;+ return () => {+ if (i >= value.length)+ throw new Error(+ `mockRandomSource: ${label} sequence exhausted at draw ${i + 1}`+ );+ return value[i++]!;+ };+}diff --git a/js/ai-editing-worker/src/ai-editing/queue/runner.test.ts b/js/ai-editing-worker/src/ai-editing/queue/runner.test.tsnew file mode 100644index 0000000000..ed7d8a96a3--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/runner.test.ts@@ -0,0 +1,373 @@+import { describe, expect, it, vi } from 'vitest';+import type { DocReader, DocWriter } from '../doc/interfaces';+import type { DocumentOp } from '../editor/ops';+import { recordingAwareness, recordingWriter } from './_testUtils';+import { mockRandomSource } from './random-source';+import { type OpResult, runQueue, summarize } from './runner';+import type { Awareness } from './types';+import { DEFAULT_QUEUE_PARAMS } from './types';++const reader = (over: Partial<DocReader> = {}): DocReader => ({+ locate: () => [{ node: 't1', start: 0, end: 3 }],+ textLength: () => 4,+ cellNode: () => 'cell',+ ...over,+});++async function run(+ ops: DocumentOp[],+ writer: DocWriter,+ opts: { docReader?: DocReader; sleep?: (ms: number) => Promise<void> } = {}+) {+ const awareness = recordingAwareness();+ const results = await runQueue({+ ops,+ params: DEFAULT_QUEUE_PARAMS,+ randomSource: mockRandomSource(),+ docReader: opts.docReader ?? reader(),+ docWriter: writer,+ awarenessSource: awareness.source,+ sleep: opts.sleep ?? (() => Promise.resolve()),+ });+ return { results, awareness };+}++describe('runQueue -- happy path', () => {+ it('applies edits in order and pumps awareness, returning one ok result per op', async () => {+ const { w, edits } = recordingWriter();+ const { results, awareness } = await run(+ [+ { kind: 'setBlockType', node: 'b1', block: 'heading', level: 2 },+ { kind: 'removeNode', node: 'b2' },+ ],+ w+ );+ expect(results.map((r) => r.ok)).toEqual([true, true]);+ expect(edits.map((e) => e.kind)).toEqual(['setBlockType', 'removeNode']);+ expect(awareness.seen.length).toBeGreaterThan(0); // cursors/highlights were pumped+ });++ it('expands a setText op into remove + chunked insert edits', async () => {+ const { w, edits } = recordingWriter();+ // typeText emits TYPE_CHUNK (3) chars per keystroke, so "Hi" is one insert.+ await run([{ kind: 'setText', node: 'b1', text: 'Hi' }], w, {+ docReader: reader({ textLength: () => 4 }),+ });+ // trailing setText canonicalizes the committed state (purges stray inlines).+ expect(edits.map((e) => e.kind)).toEqual([+ 'removeText',+ 'insertText',+ 'setText',+ ]);+ expect(edits[1]).toMatchObject({+ kind: 'insertText',+ node: 'b1',+ at: 0,+ text: 'Hi',+ });+ });++ it('awaits pauses through the injected sleep', async () => {+ const sleep = vi.fn(() => Promise.resolve());+ await runQueue({+ ops: [{ kind: 'setBlockType', node: 'b1', block: 'quote' }],+ params: DEFAULT_QUEUE_PARAMS,+ randomSource: mockRandomSource(),+ docReader: reader(),+ docWriter: recordingWriter().w,+ awarenessSource: { apply: () => {} },+ sleep,+ });+ expect(sleep).toHaveBeenCalled();+ });+});++describe('runQueue -- error handling', () => {+ it('records a failed op but keeps applying independent ones', async () => {+ const { w } = recordingWriter({ kind: 'removeNode', error: 'boom' });+ const { results } = await run(+ [+ { kind: 'setBlockType', node: 'b1', block: 'quote' }, // ok+ { kind: 'removeNode', node: 'b2' }, // throws+ { kind: 'setChecked', node: 'b3', checked: true }, // still runs+ ],+ w+ );+ expect(results.map((r) => r.ok)).toEqual([true, false, true]);+ const failed = results[1]!;+ expect(failed.ok).toBe(false);+ if (failed.ok === false) expect(failed.error).toBe('boom');+ });++ it('attributes a planning-time read failure to the stepped op', async () => {+ const { w } = recordingWriter();+ const docReader = reader({+ textLength: () => {+ throw new Error('no node');+ },+ });+ // setText animator reads textLength during step → throws while planning+ const { results } = await run(+ [{ kind: 'setText', node: 'ghost', text: 'x' }],+ w,+ { docReader }+ );+ expect(results).toHaveLength(1);+ expect(results[0].ok).toBe(false);+ if (!results[0].ok) expect(results[0].op.kind).toBe('setText');+ });+});++describe('runQueue -- error in the middle keeps neighbors', () => {+ it('op N fails, op N-1 and N+1 succeed', async () => {+ const { w } = recordingWriter({ kind: 'moveNode', error: 'cannot move' });+ const { results } = await run(+ [+ { kind: 'setBlockType', node: 'b1', block: 'quote' }, // ok+ { kind: 'moveNode', node: 'b2', at: { before: 'b3' } }, // throws+ { kind: 'setChecked', node: 'b4', checked: false }, // ok+ ],+ w+ );+ expect(results.map((r) => r.ok)).toEqual([true, false, true]);+ const r1 = results[1]!;+ expect(r1.ok).toBe(false);+ if (r1.ok === false) {+ expect(r1.error).toBe('cannot move');+ expect(r1.op.kind).toBe('moveNode');+ }+ });++ it('two failures in a row are each recorded, surrounding ops survive', async () => {+ const { w } = recordingWriter([+ { kind: 'removeNode', error: 'gone' },+ { kind: 'setIndent', error: 'bad indent' },+ ]);+ const { results } = await run(+ [+ { kind: 'setChecked', node: 'b1', checked: true }, // ok+ { kind: 'removeNode', node: 'b2' }, // throw+ { kind: 'setIndent', node: 'b3', indent: 'in' }, // throw+ ],+ w+ );+ expect(results.map((r) => r.ok)).toEqual([true, false, false]);+ });+});++describe('runQueue -- planning-time read failures attributed to the right op kind', () => {+ it('locate throw on a formatText op attributes to formatText', async () => {+ const { w } = recordingWriter();+ const docReader = reader({+ locate: () => {+ throw new Error('locate failed');+ },+ });+ const { results } = await run(+ [+ {+ kind: 'formatText',+ node: 'g',+ match: 'x',+ format: 'bold',+ on: true,+ scope: { kind: 'all' },+ },+ ],+ w,+ { docReader }+ );+ expect(results).toHaveLength(1);+ const r0 = results[0]!;+ expect(r0.ok).toBe(false);+ if (r0.ok === false) {+ expect(r0.op.kind).toBe('formatText');+ expect(r0.error).toBe('locate failed');+ }+ });++ it('cellNode throw on a setCell op attributes to setCell', async () => {+ const { w } = recordingWriter();+ const docReader = reader({+ cellNode: () => {+ throw new Error('no cell');+ },+ });+ const { results } = await run(+ [{ kind: 'setCell', table: 't', row: 9, col: 9, text: 'x' }],+ w,+ { docReader }+ );+ expect(results[0].ok).toBe(false);+ if (!results[0].ok) expect(results[0].op.kind).toBe('setCell');+ });++ it('a planning failure mid-queue still lets later ops run', async () => {+ const { w, edits } = recordingWriter();+ let calledLocate = 0;+ const docReader = reader({+ locate: () => {+ calledLocate++;+ if (calledLocate === 1) throw new Error('first locate boom');+ return [{ node: 't1', start: 0, end: 1 }];+ },+ });+ const { results } = await run(+ [+ {+ kind: 'markText',+ node: 'b1',+ match: 'x',+ on: true,+ scope: { kind: 'all' },+ }, // plan throws+ { kind: 'setBlockType', node: 'b2', block: 'quote' }, // ok+ ],+ w,+ { docReader }+ );+ expect(results.map((r) => r.ok)).toEqual([false, true]);+ expect(edits.map((e) => e.kind)).toEqual(['setBlockType']);+ });+});++describe('runQueue -- awareness ordering & sleep', () => {+ it('awareness is pumped in the exact step order', async () => {+ const { w } = recordingWriter();+ // appendText: cursor(end) then one chunked cursor (TYPE_CHUNK 3 > "ab").+ const { awareness } = await run(+ [{ kind: 'appendText', node: 'b', text: 'ab' }],+ w,+ { docReader: reader({ textLength: () => 5 }) }+ );+ expect(awareness.seen).toEqual<Awareness[]>([+ { type: 'cursor', node: 'b', at: 5 },+ { type: 'cursor', node: 'b', at: 7 },+ ]);+ });++ it('every pause ms is passed to the injected sleep, in order', async () => {+ const slept: number[] = [];+ const sleep = (ms: number) => {+ slept.push(ms);+ return Promise.resolve();+ };+ const { w } = recordingWriter();+ // setChecked: focus = one settle pause. The mock returns its integer value+ // directly, so every pause ms is the chosen 145.+ await runQueue({+ ops: [{ kind: 'setChecked', node: 'b1', checked: true }],+ params: DEFAULT_QUEUE_PARAMS,+ randomSource: mockRandomSource({ integer: 145 }),+ docReader: reader(),+ docWriter: w,+ awarenessSource: recordingAwareness().source,+ sleep,+ });+ expect(slept).toEqual([145]);+ });++ it('edits and awareness interleave: a removeNode pumps highlights before the remove', async () => {+ const events: string[] = [];+ const w: DocWriter = {+ apply(op) {+ events.push(`edit:${op.kind}`);+ },+ };+ await runQueue({+ ops: [{ kind: 'removeNode', node: 'b1' }],+ params: DEFAULT_QUEUE_PARAMS,+ randomSource: mockRandomSource({ integer: 0 }),+ docReader: reader({ textLength: () => 3 }),+ docWriter: w,+ awarenessSource: { apply: (x) => events.push(`aware:${x.type}`) },+ sleep: () => Promise.resolve(),+ });+ expect(events.at(-1)).toBe('edit:removeNode');+ expect(events.indexOf('aware:highlight')).toBeLessThan(+ events.indexOf('edit:removeNode')+ );+ });+});++describe('runQueue -- ref-dependent failures', () => {+ it('insertNode fails → later op on the same ref also fails, independent ops still succeed', async () => {+ // insertNode throws so the ref never resolves. The dependent op is an+ // appendText, which the animator expands into insertText edits -- the writer+ // throws on insertText (the unresolved-ref failure surfaces at apply time).+ const { w } = recordingWriter([+ { kind: 'insertNode', error: 'cannot insert' },+ { kind: 'insertText', error: 'unknown ref' }, // dependent op fails (ref unresolved)+ ]);+ const { results } = await run(+ [+ {+ kind: 'insertNode',+ ref: 'ref-1',+ spec: { block: 'paragraph', text: 'x' },+ at: { appendToRoot: true },+ }, // fails+ { kind: 'appendText', node: 'ref-1', text: 'Y' }, // dependent → fails+ { kind: 'setBlockType', node: 'b2', block: 'quote' }, // independent → ok+ ],+ w,+ { docReader: reader({ textLength: () => 0 }) }+ );+ expect(results.map((r) => r.ok)).toEqual([false, false, true]);+ });++ it('insertNode succeeds → dependent setText on the ref succeeds', async () => {+ const { w, edits } = recordingWriter();+ const { results } = await run(+ [+ {+ kind: 'insertNode',+ ref: 'ref-9',+ spec: { block: 'paragraph', text: '' },+ at: { appendToRoot: true },+ },+ { kind: 'appendText', node: 'ref-9', text: 'Z' },+ ],+ w,+ { docReader: reader({ textLength: () => 0 }) }+ );+ expect(results.map((r) => r.ok)).toEqual([true, true]);+ expect(edits.map((e) => e.kind)).toContain('insertNode');+ expect(edits.map((e) => e.kind)).toContain('insertText');+ });+});++describe('summarize', () => {+ it('empty results → "ok"', () => {+ expect(summarize([])).toBe('ok');+ });+ it('a failed result includes the op kind prefix', () => {+ const results: OpResult[] = [+ {+ ok: false,+ op: { kind: 'removeNode', node: 'b1' },+ error: 'planning blew up',+ },+ ];+ expect(summarize(results)).toBe('error: removeNode: planning blew up');+ });+});++describe('runQueue -- empty queue short-circuit', () => {+ it('never touches the writer, reader, or awareness', async () => {+ const apply = vi.fn();+ const { w, edits } = recordingWriter();+ const results = await runQueue({+ ops: [],+ params: DEFAULT_QUEUE_PARAMS,+ randomSource: mockRandomSource(),+ docReader: reader(),+ docWriter: w,+ awarenessSource: { apply },+ sleep: () => Promise.resolve(),+ });+ expect(results).toEqual([]);+ expect(edits).toEqual([]);+ expect(apply).not.toHaveBeenCalled();+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/queue/runner.ts b/js/ai-editing-worker/src/ai-editing/queue/runner.tsnew file mode 100644index 0000000000..928b0cdf60--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/runner.ts@@ -0,0 +1,83 @@+import { match } from 'ts-pattern';+import type { DocReader, DocWriter } from '../doc';+import type { DocumentOp, NodeRef } from '../editor';+import { animate } from './animators';+import type { RandomSource } from './random-source';+import type { Awareness, DocumentOpStep } from './types';+import { DEFAULT_QUEUE_PARAMS, type DocumentOpQueueParams } from './types';++export type OpResult =+ | { ok: true; op: DocumentOp }+ | { ok: false; op: DocumentOp; error: string };++const defaultSleep = (ms: number) =>+ new Promise<void>((r) => setTimeout(r, ms));++export type RunQueueArgs = {+ ops: DocumentOp[];+ params?: DocumentOpQueueParams;+ randomSource: RandomSource;+ docReader: DocReader;+ docWriter: DocWriter;+ awarenessSource: { apply(x: Awareness): void };+ /** Resolve a placeholder ref to its real id, so cursors point at inserted+ * nodes once their insert edit has run. Defaults to identity. */+ resolveNode?: (node: NodeRef) => NodeRef;+ /** Injectable for tests (skip real timers). */+ sleep?: (ms: number) => Promise<void>;+};++export async function runQueue(args: RunQueueArgs): Promise<OpResult[]> {+ const { ops, randomSource, docReader, docWriter, awarenessSource } = args;+ const sleep = args.sleep ?? defaultSleep;+ const resolveNode = args.resolveNode ?? ((n) => n);+ const params = args.params ?? DEFAULT_QUEUE_PARAMS;+ const msPerChar = 60_000 / (params.speed * 5);+ const results: OpResult[] = [];++ for (const op of ops) {+ let steps: DocumentOpStep[];+ try {+ steps = animate(op, {+ randomSource,+ docReader,+ msPerChar,+ ranges: params.ranges,+ });+ } catch (e) {+ results.push({ ok: false, op, error: toMessage(e) });+ continue;+ }++ try {+ for (const step of steps) {+ await match(step)+ .with({ kind: 'pause' }, ({ ms }) => sleep(ms))+ .with({ kind: 'awareness' }, ({ x }) =>+ awarenessSource.apply({ ...x, node: resolveNode(x.node) })+ )+ .with({ kind: 'edit' }, ({ y }) => docWriter.apply(y))+ .exhaustive();+ }+ results.push({ ok: true, op });+ } catch (e) {+ results.push({ ok: false, op, error: toMessage(e) });+ }+ }++ return results;+}++function toMessage(e: unknown): string {+ if (e instanceof Error) return e.message;+ throw e;+}++/** Render only failures; successful ops intentionally produce no detail. */+export function summarize(results: OpResult[]): string {+ const failures = results.filter(+ (r): r is Extract<OpResult, { ok: false }> => !r.ok+ );+ if (failures.length === 0) return 'ok';+ return failures.map((r) => `error: ${r.op.kind}: ${r.error}`).join('\n');+}diff --git a/js/ai-editing-worker/src/ai-editing/queue/types.ts b/js/ai-editing-worker/src/ai-editing/queue/types.tsnew file mode 100644index 0000000000..1749b359f6--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/queue/types.ts@@ -0,0 +1,54 @@+/**+ * The data the queue produces. An op expands into a `DocumentOpAction` — a flat+ * list of `DocumentOpStep`s the executor replays in order: pump an `Awareness`+ * into the AwarenessSource, apply a `DocumentOp` via the DocWriter, or sleep.+ */+import type { DocumentOp, NodeRef, Offset, Span } from '../editor';++/** Awareness payload. */+export type Awareness =+ | { type: 'cursor'; node: NodeRef; at?: Offset }+ | { type: 'highlight'; node: NodeRef; span?: Span };++export type DocumentOpStep =+ | { kind: 'awareness'; x: Awareness }+ | { kind: 'edit'; y: DocumentOp }+ | { kind: 'pause'; ms: number };++export type DocumentOpAction = { done: boolean; steps: DocumentOpStep[] };++/**+ * Tunable random ranges, threaded into every animator. Each is `[min, max]`;+ * the `RandomSource` picks within. `highlightSweeps` is the number of incremental+ * drag-select steps before a selection settles (0 = jump straight to the full span).+ */+export type RandomRanges = {+ highlightSweeps: [number, number];+ preSelectPauseMs: [number, number]; // caret rests on the anchor before sweeping+ sweepPauseMs: [number, number];+ settlePauseMs: [number, number];+ preDeletePauseMs: [number, number];+ typeJitter: [number, number]; // multiplier on msPerChar, per keystroke+ betweenNodesPauseMs: [number, number];+};++export type DocumentOpQueueParams = {+ speed: number;+ ranges: RandomRanges;+};++export const DEFAULT_RANGES: RandomRanges = {+ highlightSweeps: [0, 5],+ // Selecting and deleting read as deliberate, weighty motions — slower than typing.+ preSelectPauseMs: [60, 220],+ sweepPauseMs: [90, 240],+ settlePauseMs: [90, 200],+ preDeletePauseMs: [350, 700],+ typeJitter: [0.6, 1.5],+ betweenNodesPauseMs: [180, 360],+};++export const DEFAULT_QUEUE_PARAMS: DocumentOpQueueParams = {+ speed: 800,+ ranges: DEFAULT_RANGES,+};diff --git a/js/ai-editing-worker/src/ai-editing/runtime.test.ts b/js/ai-editing-worker/src/ai-editing/runtime.test.tsnew file mode 100644index 0000000000..f519f9f3cc--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/runtime.test.ts@@ -0,0 +1,120 @@+import { $getRoot } from 'lexical';+import { describe, expect, it } from 'vitest';+import { $getId } from '../../../lexical-core/plugins/nodeIdPlugin';+import { mockAwarenessSource } from './awareness/awareness-source';+import { Doc } from './doc/doc';+import { DocumentEditor } from './editor/document-editor';+import { type CodeRunner, docIds, runEditorCode } from './runtime';++// In-process executor for unit tests: the QuickJS sandbox's wasm import doesn't+// resolve under bun test, so tests run the snippet via `new Function`. Production+// always goes through `runInSandbox`. The id pool mirrors what the host injects.+const newFunctionRunner: CodeRunner = (validIds, code) => {+ const refs = Array.from({ length: 128 }, (_, i) => `ref-${i + 1}`);+ const editor = new DocumentEditor({ validIds, refs });+ // eslint-disable-next-line no-new-func+ new Function('editor', code)(editor);+ return editor.drain();+};++import type { LexicalSession } from './ai-toolkit/session';+import { createEditingSession, loadMarkdown } from './ai-toolkit/session';+import { serializeWithXml } from './utils';++function build(md: string): { session: LexicalSession; ids: string[] } {+ const session = createEditingSession();+ loadMarkdown(session, md);+ const ids = [...docIds(session)];+ return { session, ids };+}++/** Top-level block ids in document order. */+function topIds(session: LexicalSession): string[] {+ return session.editor.getEditorState().read(() =>+ $getRoot()+ .getChildren()+ .map((c) => $getId(c) ?? '?')+ );+}++/** Run a snippet end-to-end (editor → ops → queue → real Doc), no timers/awareness delay. */+async function runCode(session: LexicalSession, code: string): Promise<string> {+ return runEditorCode({+ session,+ doc: new Doc(session),+ code,+ runner: newFunctionRunner,+ awarenessSource: mockAwarenessSource(),+ sleep: () => Promise.resolve(),+ });+}++describe('runtime — end to end against real Lexical', () => {+ it('applies a multi-op snippet and returns compact success output', async () => {+ const { session } = build('Notes\n\nthe Bluejay launched');+ const [headingId, paragraphId] = topIds(session);+ const summary = await runCode(+ session,+ `editor.convertToHeading('${headingId}', 2); editor.bold('${paragraphId}', 'Bluejay');`+ );+ expect(summary).toBe('ok');+ const out = serializeWithXml(session);+ expect(out).toContain('<h2');+ expect(out).toContain('Notes');+ expect(out).toContain('Bluejay');+ });++ it('types new content char-by-char into a created block (ref resolves)', async () => {+ const { session, ids } = build('first');+ const summary = await runCode(+ session,+ `const p = editor.insertParagraphAfter('${ids[0]}', 'hello'); editor.bold(p, 'hello');`+ );+ expect(summary).toBe('ok');+ expect(serializeWithXml(session)).toContain('hello');+ });++ it('reports an eager EditError and applies nothing', async () => {+ const { session } = build('untouched');+ const summary = await runCode(+ session,+ `editor.bold('does-not-exist', 'x');`+ );+ expect(summary).toMatch(/error: unknown id/);+ expect(serializeWithXml(session)).toContain('untouched'); // unchanged+ });++ it('continues independent ops when one fails at apply time', async () => {+ const { session } = build('alpha\n\nbeta');+ const [alpha, beta] = topIds(session);+ // first op fails (setCell on a non-table block); the heading op still applies.+ const summary = await runCode(+ session,+ `editor.setCell('${alpha}', 9, 9, 'x'); editor.convertToHeading('${beta}', 3);`+ );+ expect(summary).toMatch(+ /error: setCell: id ".+" is a <paragraph>, not a table/+ );+ expect(serializeWithXml(session)).toContain('<h3');+ expect(serializeWithXml(session)).toContain('beta');+ });+});++describe('runtime — awareness ref resolution', () => {+ it('points cursors at the inserted node, whose ref is its durable id', async () => {+ const { session, ids } = build('first');+ const awareness = mockAwarenessSource();+ await runEditorCode({+ session,+ doc: new Doc(session),+ code: `editor.insertParagraphAfter('${ids[0]}', 'hi');`,+ runner: newFunctionRunner,+ awarenessSource: awareness,+ sleep: () => Promise.resolve(),+ });+ expect(awareness.seen.length).toBeGreaterThan(0);+ // refs ARE ids: every awareness node id resolves to a real node in the doc.+ const real = docIds(session);+ expect(awareness.seen.every((x) => real.has(x.node))).toBe(true);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/runtime.ts b/js/ai-editing-worker/src/ai-editing/runtime.tsnew file mode 100644index 0000000000..2d4fcca5a9--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/runtime.ts@@ -0,0 +1,91 @@+import { $getRoot, $isElementNode, type LexicalNode } from 'lexical';+import { $getId } from '../../../lexical-core/plugins/nodeIdPlugin';+import type { LexicalSession } from './ai-toolkit/session';+import type { AwarenessSource } from './awareness';+import type { Doc } from './doc';+import type { DocumentOp } from './editor';+import {+ type DocumentOpQueueParams,+ type OpResult,+ realRandomSource,+ runQueue,+ summarize,+} from './queue';++/** Every durable id currently in the document (what the model is allowed to reference). */+export function docIds(session: LexicalSession): Set<string> {+ return session.editor.getEditorState().read(() => {+ const ids = new Set<string>();+ const walk = (node: LexicalNode) => {+ const id = $getId(node);+ if (id) ids.add(id);+ if ($isElementNode(node)) for (const c of node.getChildren()) walk(c);+ };+ for (const c of $getRoot().getChildren()) walk(c);+ return ids;+ });+}++/** Takes the valid id set + AI snippet, returns the ops. */+export type CodeRunner = (+ validIds: Set<string>,+ code: string,+ snippets?: Record<string, string>+) => DocumentOp[] | Promise<DocumentOp[]>;++export type RunEditorCodeArgs = {+ session: LexicalSession;+ doc: Doc;+ code: string;+ awarenessSource: AwarenessSource;+ runner: CodeRunner;+ snippets?: Record<string, string>;+ params?: DocumentOpQueueParams;+ sleep?: (ms: number) => Promise<void>;+ /** Skip animation entirely -- apply each op directly with no typing, pauses, or cursor movement. */+ typingAnimations?: boolean;+ /** Called with the raw ops before they are queued/applied -- use to collect them. */+ onOps?: (ops: DocumentOp[]) => void;+};++/**+ * Build -> validate -> drain -> animate/apply, returning compact model-facing output.+ * An eager `EditError` (or any JS error) from the snippet is reported immediately+ * and nothing is applied.+ */+export async function runEditorCode(args: RunEditorCodeArgs): Promise<string> {+ let ops: DocumentOp[];+ try {+ ops = await args.runner(docIds(args.session), args.code, args.snippets);+ } catch (e) {+ if (!(e instanceof Error)) throw new Error(String(e));+ return `error: ${e.message}`;+ }+ args.onOps?.(ops);+ if (ops.length === 0) return 'ok';++ if (args.typingAnimations === false) {+ const results: OpResult[] = ops.map((op) => {+ try {+ args.doc.apply(op);+ return { ok: true, op };+ } catch (e) {+ if (!(e instanceof Error)) throw new Error(String(e));+ return { ok: false, op, error: e.message };+ }+ });+ return summarize(results);+ }++ const results = await runQueue({+ ops,+ params: args.params,+ randomSource: realRandomSource(),+ docReader: args.doc,+ docWriter: args.doc,+ awarenessSource: args.awarenessSource,+ resolveNode: (n) => args.doc.resolveRef(n), // point cursors at inserted nodes+ sleep: args.sleep,+ });+ return summarize(results);+}diff --git a/js/ai-editing-worker/src/ai-editing/token-tracker.ts b/js/ai-editing-worker/src/ai-editing/token-tracker.tsnew file mode 100644index 0000000000..ff8c17020a--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/token-tracker.ts@@ -0,0 +1,33 @@+export type UsageEntry = {+ model: string;+ inputTokens: number;+ outputTokens: number;+};++export class TokenTracker {+ private readonly map = new Map<+ string,+ { inputTokens: number; outputTokens: number }+ >();++ public add(+ model: { modelId: string },+ usage: { inputTokens?: number; outputTokens?: number }+ ): void {+ const prev = this.map.get(model.modelId) ?? {+ inputTokens: 0,+ outputTokens: 0,+ };+ this.map.set(model.modelId, {+ inputTokens: prev.inputTokens + (usage.inputTokens ?? 0),+ outputTokens: prev.outputTokens + (usage.outputTokens ?? 0),+ });+ }++ public toEntries(): UsageEntry[] {+ return Array.from(this.map.entries()).map(([model, u]) => ({+ model,+ ...u,+ }));+ }+}diff --git a/js/ai-editing-worker/src/ai-editing/tools/dispatch.test.ts b/js/ai-editing-worker/src/ai-editing/tools/dispatch.test.tsnew file mode 100644index 0000000000..728eed57a7--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/tools/dispatch.test.ts@@ -0,0 +1,102 @@+import { describe, expect, it } from 'vitest';+import { computeContextRange, indexXmlRanges, mergeRanges } from './dispatch';++describe('mergeRanges', () => {+ it('leaves disjoint ranges alone', () => {+ expect(+ mergeRanges([+ [1, 3],+ [5, 7],+ ])+ ).toEqual([+ [1, 3],+ [5, 7],+ ]);+ });++ it('merges overlapping ranges', () => {+ expect(+ mergeRanges([+ [1, 5],+ [3, 8],+ ])+ ).toEqual([[1, 8]]);+ });++ it('merges adjacent ranges', () => {+ expect(+ mergeRanges([+ [1, 3],+ [4, 6],+ ])+ ).toEqual([[1, 6]]);+ });++ it('handles unsorted input', () => {+ expect(+ mergeRanges([+ [5, 7],+ [1, 3],+ ])+ ).toEqual([+ [1, 3],+ [5, 7],+ ]);+ });+});++describe('indexXmlRanges', () => {+ it('indexes a single node', () => {+ const xml = '<p id="abc123">hello</p>';+ const { byId } = indexXmlRanges(xml);+ expect(byId.get('abc123')).toMatchObject({+ tag: 'p',+ id: 'abc123',+ startLine: 1,+ endLine: 1,+ });+ });++ it('tracks start/end lines for a multiline node', () => {+ const xml = '<ul id="list01">\n<li id="item01">one</li>\n</ul>';+ const { byId } = indexXmlRanges(xml);+ expect(byId.get('list01')).toMatchObject({ startLine: 1, endLine: 3 });+ expect(byId.get('item01')).toMatchObject({ startLine: 2, endLine: 2 });+ });++ it('records ancestors', () => {+ const xml = '<ul id="list01">\n<li id="item01">one</li>\n</ul>';+ const { byId } = indexXmlRanges(xml);+ const item = byId.get('item01')!;+ expect(item.ancestors.map((a) => a.id)).toEqual(['list01']);+ });++ it('ignores nodes without ids', () => {+ const xml = '<p>no id here</p>\n<p id="abc123">has id</p>';+ const { byId } = indexXmlRanges(xml);+ expect(byId.size).toBe(1);+ expect(byId.has('abc123')).toBe(true);+ });+});++describe('computeContextRange', () => {+ const xml =+ '<p id="abc123">first</p>\n<p id="def456">second</p>\n<p id="ghi789">third</p>';++ it('falls back to full document when no ids match', () => {+ const range = computeContextRange(xml, 'rewrite the intro');+ expect(range.source).toBe('full-document');+ });++ it('narrows to the matching node when an id is present', () => {+ const range = computeContextRange(xml, `edit node abc123`);+ expect(range.source).toBe('ids');+ expect(range.ids).toContain('abc123');+ });++ it('spans multiple matched nodes', () => {+ const range = computeContextRange(xml, `abc123 and ghi789`);+ expect(range.startLine).toBe(1);+ expect(range.endLine).toBe(3);+ });+});diff --git a/js/ai-editing-worker/src/ai-editing/tools/dispatch.ts b/js/ai-editing-worker/src/ai-editing/tools/dispatch.tsnew file mode 100644index 0000000000..8fb7dc2009--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/tools/dispatch.ts@@ -0,0 +1,296 @@+import { type LanguageModel, tool } from 'ai';+import { z } from 'zod';+import type { coder } from '../agents';+import type { LexicalSession } from '../ai-toolkit';+import type { AwarenessSource } from '../awareness';+import type { Doc } from '../doc';+import type { DocumentOpQueueParams } from '../queue';+import type { TokenTracker } from '../token-tracker';+import { numberLines, serializeWithXml } from '../utils';+import type { RunCodeToolOptions } from './run-code';++export type { UsageEntry } from '../token-tracker';+export { TokenTracker } from '../token-tracker';++type BlockedReport = {+ message: string;+};+type ContextRange = {+ startLine: number;+ endLine: number;+ rootIds: string[];+ ids: string[];+ source: 'ids' | 'full-document';+};++const ID_PATTERN = /[A-Za-z0-9_~-]{6,}/g;+const BLOCK_TAGS = new Set([+ 'h1',+ 'h2',+ 'h3',+ 'h4',+ 'h5',+ 'h6',+ 'p',+ 'blockquote',+ 'li',+ 'table',+ 'ul',+ 'ol',+]);+const CONTAINER_TAGS = new Set(['table', 'ul', 'ol']);++type XmlNodeRange = {+ tag: string;+ id: string;+ startLine: number;+ endLine: number;+ ancestors: XmlNodeRange[];+};++/** Pull a writer'session `reportBlocked` payload out of its run, if it bailed. */+function findBlocked(+ res: Awaited<ReturnType<typeof coder>>+): BlockedReport | null {+ for (const step of res.steps) {+ for (const call of step.toolCalls) {+ if (call.toolName === 'reportBlocked') return call.input as BlockedReport;+ }+ }+ return null;+}++export function mergeRanges(+ ranges: Array<[number, number]>+): Array<[number, number]> {+ const sorted = [...ranges].sort((a, b) => a[0] - b[0]);+ const merged: Array<[number, number]> = [];+ for (const range of sorted) {+ const last = merged[merged.length - 1];+ if (last && range[0] <= last[1] + 1) last[1] = Math.max(last[1], range[1]);+ else merged.push([range[0], range[1]]);+ }+ return merged;+}++export function indexXmlRanges(xml: string): {+ lines: string[];+ byId: Map<string, XmlNodeRange>;+} {+ const lines = xml.split('\n');+ const byId = new Map<string, XmlNodeRange>();+ const stack: XmlNodeRange[] = [];+ const tagPattern = /<\/?([A-Za-z][A-Za-z0-9_-]*)([^>]*)>/g;++ for (let i = 0; i < lines.length; i++) {+ const line = lines[i]!;+ tagPattern.lastIndex = 0;+ for (+ let match = tagPattern.exec(line);+ match !== null;+ match = tagPattern.exec(line)+ ) {+ const [raw, tag, attrs] = match;+ if (raw.startsWith('</')) {+ for (let j = stack.length - 1; j >= 0; j--) {+ const node = stack[j]!;+ stack.pop();+ node.endLine = i + 1;+ if (node.tag === tag) break;+ }+ continue;+ }++ const id = attrs.match(/\bid="([^"]+)"/)?.[1];+ const selfClosing = raw.endsWith('/>');+ if (!id) continue;++ const node: XmlNodeRange = {+ tag,+ id,+ startLine: i + 1,+ endLine: i + 1,+ ancestors: [...stack],+ };+ byId.set(id, node);+ if (!selfClosing) stack.push(node);+ }+ }++ for (const node of stack) node.endLine = lines.length;+ return { lines, byId };+}++function containingRoot(node: XmlNodeRange): XmlNodeRange {+ const lineage = [...node.ancestors, node];+ for (let i = lineage.length - 1; i >= 0; i--) {+ const current = lineage[i]!;+ if (CONTAINER_TAGS.has(current.tag)) return current;+ }+ for (let i = lineage.length - 1; i >= 0; i--) {+ const current = lineage[i]!;+ if (BLOCK_TAGS.has(current.tag)) return current;+ }+ return node;+}++export function computeContextRange(+ xml: string,+ instruction: string+): ContextRange {+ const { lines, byId } = indexXmlRanges(xml);+ const ids = [...new Set(instruction.match(ID_PATTERN) ?? [])].filter((id) =>+ byId.has(id)+ );+ if (ids.length > 0) {+ const roots = ids.map((id) => containingRoot(byId.get(id)!));+ const ranges = mergeRanges(+ roots.map((root) => [root.startLine, root.endLine])+ );+ return {+ startLine: ranges[0]![0],+ endLine: ranges[ranges.length - 1]![1],+ rootIds: [...new Set(roots.map((root) => root.id))],+ ids,+ source: 'ids',+ };+ }+ return {+ startLine: 1,+ endLine: lines.length,+ rootIds: [],+ ids,+ source: 'full-document',+ };+}++/** Lines of context to show a writer around its target region. Generous on+ * purpose: a roomy window means the writer rarely has to call `readDocument`. */+const MIN_WINDOW_LINES = 20;++function xmlWindow(+ xml: string,+ range: ContextRange,+ padding = 2,+ minLines = MIN_WINDOW_LINES+): string {+ const numbered = numberLines(xml).split('\n');+ let lo = Math.max(0, range.startLine - 1 - padding);+ let hi = Math.min(numbered.length - 1, range.endLine - 1 + padding);+ // Grow symmetrically until we show at least `minLines` (or hit the doc edges).+ while (hi - lo + 1 < minLines && (lo > 0 || hi < numbered.length - 1)) {+ if (lo > 0) lo--;+ if (hi < numbered.length - 1) hi++;+ }+ return numbered.slice(lo, hi + 1).join('\n');+}++export type Writer = {+ doc: Doc;+ awarenessSource: AwarenessSource;+ release: () => void;+};++export type DispatchToolOptions = {+ session: LexicalSession;+ childModel: LanguageModel;+ tracker: TokenTracker;+ runner: RunCodeToolOptions['runner'];+ params?: DocumentOpQueueParams;+ typingAnimations?: boolean;+ sleep?: (ms: number) => Promise<void>;+ signal?: AbortSignal;+ makeWriter: () => Promise<Writer>;+ runTask: typeof coder;+ serialize?: (session: LexicalSession) => string;+ onOps?: RunCodeToolOptions['onOps'];+ /** Called after each dispatch batch with the JS code blocks run by each coder. */+ onCoderResult?: (codes: string[][]) => void;+};++export function createDispatchTool(opts: DispatchToolOptions) {+ const {+ session,+ childModel,+ tracker,+ params,+ typingAnimations,+ sleep,+ signal,+ makeWriter,+ runTask,+ runner,+ onOps,+ onCoderResult,+ } = opts;+ const serialize = opts.serialize ?? serializeWithXml;+ return tool({+ description:+ 'spawn a writer to carry out an edit instruction on the document',+ inputSchema: z.object({+ edits: z+ .array(+ z.object({+ editing_instruction: z+ .string()+ .describe('mechanical changes you want applied to the document'),+ snippets: z+ .record(z.string(), z.string())+ .optional()+ .describe(+ 'verbatim text values injected as a `snippets` JS object the coder can use directly. all verbatim text goes here, so that the writer can paste it in'+ ),+ })+ )+ .describe('edit instructions to run as one parallel batch'),+ }),+ execute: async ({ edits }) => {+ const xml = serializeWithXml(session);+ const contexts = edits.map((e) =>+ computeContextRange(xml, e.editing_instruction)+ );+ // Writers run concurrently (distinct cursors); each applies its own ops+ // serially via editor.update, so the shared session never tears.+ const results = await Promise.all(+ edits.map(async ({ editing_instruction, snippets }, i) => {+ const writer = await makeWriter();+ const { doc, awarenessSource } = writer;+ try {+ return await runTask(session, editing_instruction, childModel, {+ doc,+ awarenessSource,+ context: xmlWindow(xml, contexts[i]!),+ snippets,+ params,+ typingAnimations,+ sleep,+ signal,+ runner,+ onOps,+ });+ } finally {+ writer.release();+ }+ })+ );+ const summaries = results.map((res, i) => {+ tracker.add(childModel as { modelId: string }, res.totalUsage);+ const blocked = findBlocked(res);+ if (blocked) {+ return `${i + 1}. ⚠ BLOCKED -- ${blocked.message}.`;+ }+ return `${i + 1}. ✓ APPLIED`;+ });+ if (onCoderResult) {+ const codes = results.map((res) =>+ res.steps+ .flatMap((step) => step.toolCalls)+ .filter((call) => call.toolName === 'runCode')+ .map((call) => (call.input as { code: string }).code)+ );+ onCoderResult(codes);+ }+ return `${summaries.join('\n')}\n\n<document>\n${serialize(session)}\n</document>`;+ },+ });+}diff --git a/js/ai-editing-worker/src/ai-editing/tools/im-blocked.ts b/js/ai-editing-worker/src/ai-editing/tools/im-blocked.tsnew file mode 100644index 0000000000..038d4042dd--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/tools/im-blocked.ts@@ -0,0 +1,29 @@+import { tool } from 'ai';+import { z } from 'zod';++/**+ * A tool the agent calls to bail out when it cannot proceed. Pass only the+ * trigger condition in `whenToUse`; this factory owns the shared framing.+ *+ * @param whenToUse - the situation that should trigger the call+ * @param callMeAgain - if the block is resolvable by re-invoking with more info,+ * the message is framed as a directive to the caller; otherwise it is a one-line reason+ */+export function createImBlockedTool(whenToUse: string, callMeAgain: boolean) {+ const guidance = callMeAgain+ ? ' Your message must be a directive telling the caller to invoke you again with the missing information.'+ : ' State what stopped you in one line.';+ return tool({+ description: `${whenToUse}${guidance} This ends your turn.`,+ inputSchema: z.object({+ message: z+ .string()+ .describe(+ callMeAgain+ ? 'directive to the caller: what to invoke you again with'+ : 'what stopped you, in one line'+ ),+ }),+ execute: async () => 'acknowledged',+ });+}diff --git a/js/ai-editing-worker/src/ai-editing/tools/index.ts b/js/ai-editing-worker/src/ai-editing/tools/index.tsnew file mode 100644index 0000000000..507522befb--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/tools/index.ts@@ -0,0 +1,4 @@+export * from './dispatch';+export * from './im-blocked';+export * from './read-document';+export * from './run-code';diff --git a/js/ai-editing-worker/src/ai-editing/tools/read-document.ts b/js/ai-editing-worker/src/ai-editing/tools/read-document.tsnew file mode 100644index 0000000000..9efb484a91--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/tools/read-document.ts@@ -0,0 +1,17 @@+import { tool } from 'ai';+import { z } from 'zod';+import type { LexicalSession } from '../ai-toolkit';+import { numberLines, serializeWithXml } from '../utils';++/** Escape hatch for a writer whose context window is too narrow: returns the+ * WHOLE document as line-numbered XML so it can find an id/region it can't see.+ * Cheaper than handing the problem back to the supervisor — the writer should+ * reach for this before `reportBlocked`, not instead of doing the edit. */+export function createReadDocumentTool(opts: { session: LexicalSession }) {+ return tool({+ description:+ 'Return the ENTIRE document as line-numbered XML. Call this ONLY when the id or region you need is not in your context window and you would otherwise have to reportBlocked.',+ inputSchema: z.object({}),+ execute: async () => numberLines(serializeWithXml(opts.session)),+ });+}diff --git a/js/ai-editing-worker/src/ai-editing/tools/run-code.ts b/js/ai-editing-worker/src/ai-editing/tools/run-code.tsnew file mode 100644index 0000000000..6b9ed87406--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/tools/run-code.ts@@ -0,0 +1,46 @@+import { tool } from 'ai';+import { z } from 'zod';+import type { LexicalSession } from '../ai-toolkit';+import type { AwarenessSource } from '../awareness';+import type { Doc } from '../doc';+import type { DocumentOp } from '../editor';+import type { DocumentOpQueueParams } from '../queue';+import { type CodeRunner, runEditorCode } from '../runtime';++export type RunCodeToolOptions = {+ session: LexicalSession;+ doc: Doc;+ awarenessSource: AwarenessSource;+ runner: CodeRunner;+ snippets?: Record<string, string>;+ params?: DocumentOpQueueParams;+ typingAnimations?: boolean;+ sleep?: (ms: number) => Promise<void>;+ onOps?: (ops: DocumentOp[]) => void;+};++/** The writer's one tool: run a JS snippet against `editor`, returning compact+ * success or error output. The snippet's only scope is+ * `editor`; the system animates + applies the resulting ops live. */+export function createRunCodeTool(opts: RunCodeToolOptions) {+ return tool({+ description:+ "Run JS statements against `editor` (the ONLY in-scope value) to edit the document — e.g. `editor.convertToHeading('b3', 2); editor.bold('b5', 'word')`. Returns `ok`, or an error naming a bad id so you can retry.",+ inputSchema: z.object({ code: z.string() }),+ execute: async ({ code }) => {+ const result = await runEditorCode({+ session: opts.session,+ doc: opts.doc,+ code,+ awarenessSource: opts.awarenessSource,+ snippets: opts.snippets,+ params: opts.params,+ typingAnimations: opts.typingAnimations,+ sleep: opts.sleep,+ runner: opts.runner,+ onOps: opts.onOps,+ });+ return result;+ },+ });+}diff --git a/js/ai-editing-worker/src/ai-editing/utils.ts b/js/ai-editing-worker/src/ai-editing/utils.tsnew file mode 100644index 0000000000..b61c81fe55--- /dev/null+++ b/js/ai-editing-worker/src/ai-editing/utils.ts@@ -0,0 +1,14 @@+import { toXml } from '../../../lexical-core/transformers/xml';+import type { LexicalSession } from './ai-toolkit';++/** Prefix each line with a 1-indexed `N | ` gutter -- the line addressing the agents read. */+export function numberLines(text: string): string {+ return text+ .split('\n')+ .map((line, i) => `${i + 1} | ${line}`)+ .join('\n');+}++export function serializeWithXml(session: LexicalSession): string {+ return toXml(session.editor.getEditorState().toJSON());+}diff --git a/js/ai-editing-worker/src/editing-workspace.ts b/js/ai-editing-worker/src/editing-workspace.tsnew file mode 100644index 0000000000..01d8891ad9--- /dev/null+++ b/js/ai-editing-worker/src/editing-workspace.ts@@ -0,0 +1,134 @@+import type { InferType } from '@loro-mirror/packages/core/src';+import type { SerializedEditorState } from 'lexical';+import { SyncEngine } from '../../app/packages/core/collab/engine';+import type { LoroManager } from '../../app/packages/core/collab/manager';+import type { RawUpdate } from '../../app/packages/core/collab/shared';+import type { WALSyncer } from '../../app/packages/core/collab/wal';+import type { SyncServiceSource } from '../../app/packages/service-clients/service-sync/source/source';+import type {+ MARKDOWN_LORO_SCHEMA,+ MarkdownLoroSchemaType,+} from '../../lexical-core/markdown-loro-schema';+import { $updateAllNodeIds } from '../../lexical-core/plugins/nodeIdPlugin';+import {+ createEditingSession,+ type LexicalSession,+ loadSnapshot,+ toSnapshot,+} from './ai-editing/ai-toolkit';+import {+ type AwarenessSource,+ nextAiPeerId,+ type Peer,+ PeerPool,+ realAwarenessSource,+} from './ai-editing/awareness';+import { Doc } from './ai-editing/doc';+import type { Writer } from './ai-editing/tools';+import { createWorkerAwareness } from './sources';++export type EditingWorkspaceOptions = {+ pool?: PeerPool;+};++export class EditingWorkspace {+ readonly session: LexicalSession;+ private readonly engine: SyncEngine<typeof MARKDOWN_LORO_SCHEMA, unknown>;+ private chain: Promise<void> = Promise.resolve();+ private readonly outstanding = new Map<Peer, AwarenessSource>();+ private readonly pool: PeerPool;++ constructor(+ private readonly manager: LoroManager<typeof MARKDOWN_LORO_SCHEMA>,+ private readonly source: SyncServiceSource,+ wal: WALSyncer<RawUpdate>,+ opts: EditingWorkspaceOptions = {}+ ) {+ this.pool = opts.pool ?? new PeerPool();+ // Seed the editing surface from the merged state.+ this.session = createEditingSession();+ loadSnapshot(+ this.session,+ manager.mirror!.getState() as unknown as SerializedEditorState+ );++ this.engine = new SyncEngine({+ loroManager: manager,+ awareness: createWorkerAwareness(manager.peerIdStr),+ syncs: { wal, live: source },+ bindings: {+ // Inbound: a remote (human) edit landed — fold it into the AI's session+ // so its next diff is a clean delta and the user's text is preserved.+ onRemoteState: (state) =>+ loadSnapshot(this.session, state as unknown as SerializedEditorState),+ },+ });++ // Feed the manager's state changes into the engine, deferred to a microtask:+ // loro fires the mirror subscriber synchronously during `importUpdate`, and+ // the engine guards remote handling with a mutex — calling `onStateUpdate`+ // inline would re-enter that lock. (The browser gets this deferral for free+ // via a Solid effect; here we do it by hand.)+ manager.onStateChange((u) =>+ queueMicrotask(() => this.engine.onStateUpdate(u))+ );+ this.engine.start();+ }++ /** Borrow a writer: its own `Doc` (authoring under a pooled peer) + cursor.+ * `release` (on the returned writer) clears the cursor and returns the peer. */+ async borrowWriter(): Promise<Writer> {+ const peer = await this.pool.borrow();+ const doc = new Doc(this.session, () => this.propagate(peer.peerId));+ const awarenessSource = realAwarenessSource({+ mirror: this.manager.mirror!,+ doc: this.manager.doc,+ send: (bytes) => this.source.pushAwareness(bytes),+ name: peer.name,+ color: peer.color,+ });+ this.outstanding.set(peer, awarenessSource);+ const release = () => {+ if (!this.outstanding.delete(peer)) return;+ awarenessSource.clear();+ this.pool.release(peer);+ };+ return { doc, awarenessSource, release };+ }++ /** Perform one final catch-all sync and wait for all queued propagation to finish. */+ flush(): Promise<void> {+ this.propagate(nextAiPeerId());+ return this.chain;+ }++ /** Stop syncing and release any writers still outstanding. */+ dispose(): void {+ this.engine.stop();+ for (const [peer, awarenessSource] of this.outstanding) {+ awarenessSource.clear();+ this.pool.release(peer);+ }+ this.outstanding.clear();+ }++ private propagate(peerId: bigint): void {+ this.chain = this.chain.then(async () => {+ this.manager.doc.commit();+ this.manager.doc.setPeerId(peerId);+ this.source.registerPeerId(peerId);++ // Prism creates code-highlight nodes without ids (skipTransforms). Stamp+ // ids before snapshotting so the Loro mirror matches them by id instead of+ // re-inserting duplicates on every sync. (code block thing :/)+ this.session.editor.update(() => $updateAllNodeIds(this.session.ids), {+ discrete: true,+ });++ const snapshot = toSnapshot(this.session);+ await this.engine.syncStateToLoro(+ snapshot as unknown as InferType<MarkdownLoroSchemaType>+ );+ });+ }+}diff --git a/js/ai-editing-worker/src/editor-sandbox-code.ts b/js/ai-editing-worker/src/editor-sandbox-code.tsnew file mode 100644index 0000000000..6edcdb8889--- /dev/null+++ b/js/ai-editing-worker/src/editor-sandbox-code.ts@@ -0,0 +1,3 @@+// Auto-generated by scripts/generate-sandbox.ts — do not edit.+// Source of truth: src/ai-editing/editor/document-editor.ts+export const SANDBOX_CODE = "class EditError extends Error {\n constructor(message) {\n super(message);\n this.name = \"EditError\";\n }\n}\n\nclass DocumentEditor {\n ops = [];\n valid;\n refs;\n refIdx = 0;\n constructor(opts) {\n this.valid = new Set(opts.validIds);\n this.refs = opts.refs;\n }\n push(op) {\n this.ops.push(op);\n return this;\n }\n requireId(id) {\n if (!this.valid.has(id)) {\n throw new EditError(`unknown id \"${id}\"`);\n }\n }\n requireMatch(match) {\n if (match.length === 0)\n throw new EditError(\"match string is empty\");\n }\n mintRef() {\n const ref = this.refs[this.refIdx++];\n if (!ref) {\n throw new EditError(\"too many inserts in one snippet (id pool exhausted)\");\n }\n this.valid.add(ref);\n return ref;\n }\n drain() {\n const out = this.ops;\n this.ops = [];\n return out;\n }\n format(id, match, format, on = true, scope = { kind: \"all\" }) {\n this.requireId(id);\n this.requireMatch(match);\n return this.push({\n kind: \"formatText\",\n node: id,\n match,\n format,\n on,\n scope\n });\n }\n bold(id, match, scope) {\n return this.format(id, match, \"bold\", true, scope);\n }\n italic(id, match, scope) {\n return this.format(id, match, \"italic\", true, scope);\n }\n underline(id, match, scope) {\n return this.format(id, match, \"underline\", true, scope);\n }\n strike(id, match, scope) {\n return this.format(id, match, \"strike\", true, scope);\n }\n inlineCode(id, match, scope) {\n return this.format(id, match, \"code\", true, scope);\n }\n unbold(id, match, scope) {\n return this.format(id, match, \"bold\", false, scope);\n }\n unitalic(id, match, scope) {\n return this.format(id, match, \"italic\", false, scope);\n }\n ununderline(id, match, scope) {\n return this.format(id, match, \"underline\", false, scope);\n }\n unstrike(id, match, scope) {\n return this.format(id, match, \"strike\", false, scope);\n }\n uninlineCode(id, match, scope) {\n return this.format(id, match, \"code\", false, scope);\n }\n clearFormat(id, match, scope = { kind: \"all\" }) {\n this.requireId(id);\n if (match !== undefined)\n this.requireMatch(match);\n return this.push({ kind: \"clearFormat\", node: id, match, scope });\n }\n clearAllFormat(id) {\n return this.clearFormat(id);\n }\n highlight(id, match, scope = { kind: \"all\" }) {\n this.requireId(id);\n this.requireMatch(match);\n return this.push({ kind: \"markText\", node: id, match, on: true, scope });\n }\n unhighlight(id, match, scope = { kind: \"all\" }) {\n this.requireId(id);\n this.requireMatch(match);\n return this.push({ kind: \"markText\", node: id, match, on: false, scope });\n }\n link(id, match, url, scope = { kind: \"all\" }) {\n this.requireId(id);\n this.requireMatch(match);\n return this.push({ kind: \"linkText\", node: id, match, url, scope });\n }\n unlink(id, match, scope = { kind: \"all\" }) {\n this.requireId(id);\n this.requireMatch(match);\n return this.push({ kind: \"linkText\", node: id, match, url: null, scope });\n }\n formatNode(node, format, on = true) {\n this.requireId(node);\n return this.push({ kind: \"formatNode\", node, format, on });\n }\n boldNode(node) {\n return this.formatNode(node, \"bold\");\n }\n italicNode(node) {\n return this.formatNode(node, \"italic\");\n }\n underlineNode(node) {\n return this.formatNode(node, \"underline\");\n }\n strikeNode(node) {\n return this.formatNode(node, \"strike\");\n }\n codeNode(node) {\n return this.formatNode(node, \"code\");\n }\n clearNodeFormat(node) {\n this.requireId(node);\n return this.push({ kind: \"clearNodeFormat\", node });\n }\n setText(id, text) {\n this.requireId(id);\n return this.push({ kind: \"setText\", node: id, text });\n }\n setEquation(id, tex) {\n this.requireId(id);\n return this.push({ kind: \"setEquation\", node: id, tex });\n }\n replace(id, find, to, scope = { kind: \"all\" }) {\n this.requireId(id);\n if (find.length === 0)\n throw new EditError(\"find string is empty\");\n return this.push({ kind: \"replaceText\", node: id, find, to, scope });\n }\n appendText(id, text) {\n this.requireId(id);\n return this.push({ kind: \"appendText\", node: id, text });\n }\n prependText(id, text) {\n this.requireId(id);\n return this.push({ kind: \"prependText\", node: id, text });\n }\n setBlockType(id, args) {\n this.requireId(id);\n return this.push({ kind: \"setBlockType\", node: id, ...args });\n }\n requireLanguage(language) {\n if (typeof language !== \"string\" || language.trim().length === 0)\n throw new EditError(\"code block language is required\");\n }\n convertToParagraph(id) {\n return this.setBlockType(id, { block: \"paragraph\" });\n }\n convertToHeading(id, level) {\n if (level < 1 || level > 6)\n throw new EditError(`heading level must be 1-6, got ${level}`);\n return this.setBlockType(id, { block: \"heading\", level });\n }\n convertToQuote(id) {\n return this.setBlockType(id, { block: \"quote\" });\n }\n convertToCodeBlock(id, language) {\n this.requireLanguage(language);\n return this.setBlockType(id, { block: \"code\", language });\n }\n setLanguage(id, language) {\n return this.convertToCodeBlock(id, language);\n }\n toList(idOrIds, list) {\n const ids = Array.isArray(idOrIds) ? idOrIds : [idOrIds];\n if (ids.length === 0)\n throw new EditError(\"list requires at least one block\");\n for (const id of ids)\n this.requireId(id);\n return this.push({ kind: \"setListType\", nodes: ids, list });\n }\n bulletList(idOrIds) {\n return this.toList(idOrIds, \"bullet\");\n }\n numberedList(idOrIds) {\n return this.toList(idOrIds, \"number\");\n }\n checklist(idOrIds) {\n return this.toList(idOrIds, \"check\");\n }\n setListType(id, list) {\n return this.toList(id, list);\n }\n check(id) {\n return this.setChecked(id, true);\n }\n uncheck(id) {\n return this.setChecked(id, false);\n }\n setChecked(id, checked) {\n this.requireId(id);\n return this.push({ kind: \"setChecked\", node: id, checked });\n }\n indent(id, by = 1) {\n this.requireId(id);\n return this.push({\n kind: \"setIndent\",\n node: id,\n indent: by >= 0 ? \"in\" : \"out\"\n });\n }\n outdent(id, by = 1) {\n return this.indent(id, -Math.abs(by));\n }\n setIndent(id, level) {\n this.requireId(id);\n if (level < 0)\n throw new EditError(`indent level must be >= 0, got ${level}`);\n return this.push({ kind: \"setIndent\", node: id, indent: level });\n }\n insertListItemAfter(id, text, list = \"bullet\") {\n this.requireId(id);\n const ref = this.mintRef();\n this.push({ kind: \"insertListItemAfter\", ref, node: id, text, list });\n return ref;\n }\n insertListItemBefore(id, text, list = \"bullet\") {\n this.requireId(id);\n const ref = this.mintRef();\n this.push({ kind: \"insertListItemBefore\", ref, node: id, text, list });\n return ref;\n }\n appendListItem(listId, text) {\n this.requireId(listId);\n const ref = this.mintRef();\n this.push({ kind: \"appendListItem\", ref, node: listId, text });\n return ref;\n }\n prependListItem(listId, text) {\n this.requireId(listId);\n const ref = this.mintRef();\n this.push({ kind: \"prependListItem\", ref, node: listId, text });\n return ref;\n }\n removeListItem(id) {\n this.requireId(id);\n return this.push({ kind: \"removeListItem\", node: id });\n }\n insert(spec, at) {\n this.requireAt(at);\n const ref = this.mintRef();\n this.push({ kind: \"insertNode\", ref, spec, at });\n return ref;\n }\n requireAt(at) {\n if (\"after\" in at)\n this.requireId(at.after);\n else if (\"before\" in at)\n this.requireId(at.before);\n }\n insertParagraphAfter(id, text = \"\") {\n return this.insert({ block: \"paragraph\", text }, { after: id });\n }\n insertParagraphBefore(id, text = \"\") {\n return this.insert({ block: \"paragraph\", text }, { before: id });\n }\n insertHeadingAfter(id, level, text = \"\") {\n return this.insert({ block: \"heading\", level, text }, { after: id });\n }\n insertHeadingBefore(id, level, text = \"\") {\n return this.insert({ block: \"heading\", level, text }, { before: id });\n }\n insertQuoteAfter(id, text = \"\") {\n return this.insert({ block: \"quote\", text }, { after: id });\n }\n insertCodeBlockAfter(id, language, text = \"\") {\n this.requireLanguage(language);\n return this.insert({ block: \"code\", language, text }, { after: id });\n }\n insertBlockAfter(id, spec) {\n return this.insert(spec, { after: id });\n }\n insertBlockBefore(id, spec) {\n return this.insert(spec, { before: id });\n }\n appendParagraph(text = \"\") {\n return this.insert({ block: \"paragraph\", text }, { appendToRoot: true });\n }\n prependParagraph(text = \"\") {\n return this.insert({ block: \"paragraph\", text }, { prependToRoot: true });\n }\n appendBlock(spec) {\n return this.insert(spec, { appendToRoot: true });\n }\n prependBlock(spec) {\n return this.insert(spec, { prependToRoot: true });\n }\n move(id, at) {\n this.requireId(id);\n this.requireAt(at);\n return this.push({ kind: \"moveNode\", node: id, at });\n }\n remove(id) {\n this.requireId(id);\n return this.push({ kind: \"removeNode\", node: id });\n }\n removeMany(ids) {\n for (const id of ids)\n this.remove(id);\n return this;\n }\n merge(ids, separator = \" \") {\n if (ids.length < 2)\n throw new EditError(\"merge requires at least two blocks\");\n for (const id of ids)\n this.requireId(id);\n return this.push({ kind: \"mergeBlocks\", nodes: ids, separator });\n }\n insertTableAfter(id, rows) {\n return this.buildTable(rows, { after: id });\n }\n insertTableBefore(id, rows) {\n return this.buildTable(rows, { before: id });\n }\n appendTable(rows) {\n return this.buildTable(rows, { appendToRoot: true });\n }\n buildTable(rows, at) {\n if (rows.length === 0)\n throw new EditError(\"table requires at least one row\");\n const empty = rows.map((row) => row.map(() => \"\"));\n const ref = this.insert({ block: \"table\", rows: empty }, at);\n rows.forEach((row, r) => void row.forEach((cell, c) => {\n if (cell)\n this.setCell(ref, r, c, cell);\n }));\n return ref;\n }\n setCell(table, row, col, text) {\n this.requireId(table);\n this.requireCell(row, col);\n return this.push({ kind: \"setCell\", table, row, col, text });\n }\n addRow(table, at) {\n this.requireId(table);\n return this.push({ kind: \"addRow\", table, at });\n }\n addColumn(table, at) {\n this.requireId(table);\n return this.push({ kind: \"addColumn\", table, at });\n }\n removeRow(table, row) {\n this.requireId(table);\n this.requireCell(row, 0);\n return this.push({ kind: \"removeRow\", table, row });\n }\n removeColumn(table, col) {\n this.requireId(table);\n this.requireCell(0, col);\n return this.push({ kind: \"removeColumn\", table, col });\n }\n requireCell(row, col) {\n if (row < 0 || col < 0)\n throw new EditError(`cell indices must be >= 0, got (${row}, ${col})`);\n }\n insertDivider(afterId) {\n return this.insert({ block: \"divider\" }, { after: afterId });\n }\n insertImage(afterId, img) {\n return this.insert({ block: \"image\", ...img }, { after: afterId });\n }\n insertVideo(afterId, vid) {\n return this.insert({ block: \"video\", ...vid }, { after: afterId });\n }\n insertEquation(afterId, tex) {\n return this.insert({ block: \"equation\", tex }, { after: afterId });\n }\n insertInlineEquation(blockId, at, tex) {\n return this.insertInline(blockId, at, { inline: \"equation\", tex });\n }\n insertTextAfterInline(inlineRef, text) {\n return this.push({\n kind: \"insertTextAfterInline\",\n inline: inlineRef,\n text\n });\n }\n insertLineBreak(blockId, at) {\n return this.insertInline(blockId, at, { inline: \"linebreak\" });\n }\n insertDate(blockId, at, isoDate, displayFormat) {\n return this.insertInline(blockId, at, {\n inline: \"date\",\n date: isoDate,\n displayFormat\n });\n }\n insertInline(id, at, spec) {\n this.requireId(id);\n if (at < 0)\n throw new EditError(`inline offset must be >= 0, got ${at}`);\n const ref = this.mintRef();\n this.push({ kind: \"insertInline\", ref, node: id, at, spec });\n return ref;\n }\n insertMention(blockId, at, mention) {\n return this.insertInline(blockId, at, { inline: \"mention\", mention });\n }\n mentionUser(blockId, at, entity) {\n return this.insertMention(blockId, at, { kind: \"user\", ...entity });\n }\n mentionContact(blockId, at, entity) {\n return this.insertMention(blockId, at, { kind: \"contact\", ...entity });\n }\n mentionGroup(blockId, at, entity) {\n return this.insertMention(blockId, at, { kind: \"group\", ...entity });\n }\n mentionDocument(blockId, at, entity) {\n return this.insertMention(blockId, at, { kind: \"document\", ...entity });\n }\n setImageAlt(id, alt) {\n this.requireId(id);\n return this.push({ kind: \"setImageAlt\", node: id, alt });\n }\n setImageUrl(id, url) {\n this.requireId(id);\n return this.push({ kind: \"setImageUrl\", node: id, url });\n }\n setVideoUrl(id, url) {\n this.requireId(id);\n return this.push({ kind: \"setVideoUrl\", node: id, url });\n }\n setVideoControls(id, controls) {\n this.requireId(id);\n return this.push({ kind: \"setVideoControls\", node: id, controls });\n }\n setDate(id, date, displayFormat) {\n this.requireId(id);\n return this.push({ kind: \"setDate\", node: id, date, displayFormat });\n }\n}";diff --git a/js/ai-editing-worker/src/endpoints/edit.ts b/js/ai-editing-worker/src/endpoints/edit.tsnew file mode 100644index 0000000000..5f46caeca8--- /dev/null+++ b/js/ai-editing-worker/src/endpoints/edit.ts@@ -0,0 +1,138 @@+import { createAnthropic } from '@ai-sdk/anthropic';+import { createCerebras } from '@ai-sdk/cerebras';+import { createOpenAI } from '@ai-sdk/openai';+import { zValidator } from '@hono/zod-validator';+import type { LanguageModel } from 'ai';+import { createFallback } from 'ai-fallback';+import { Hono } from 'hono';+import * as z from 'zod';+import { type Bindings, getEnv } from '../env';+import { type Model, runEditSession } from '../run-edit';+import { runInSandbox } from '../sandbox';+import { watchPresenceSpeed } from '../service-clients';++type Provider = 'anthropic' | 'cerebras' | 'openai';++const PROVIDERS = {+ anthropic: { key: 'ANTHROPIC_API_KEY', create: createAnthropic },+ cerebras: { key: 'CEREBRAS_API_KEY', create: createCerebras },+ openai: { key: 'OPENAI_API_KEY', create: createOpenAI },+} satisfies Record<+ Provider,+ {+ key: keyof Bindings;+ create: (opts: { apiKey: string }) => (modelId: string) => LanguageModel;+ }+>;++const ModelSchema: z.ZodType<Model> = z.object({+ provider: z.enum(['anthropic', 'cerebras', 'openai']),+ model: z.string(),+});++// Each role takes a non-empty list of models tried in order: the first is+// primary, the rest are fallbacks used in order, only when a provider errors or+// rate-limits.+const ModelListSchema = z.array(ModelSchema).min(1);++const EditBody = z.object({+ documentToken: z.string(),+ documentId: z.string(),+ prompt: z.string(),+ models: z.object({+ supervisor: ModelListSchema,+ interpret: ModelListSchema,+ coding: ModelListSchema,+ }),+ typingAnimations: z.boolean().optional(),+ /** Animation speed multiplier applied while nobody is watching the doc. */+ unwatchedSpeed: z.number().min(1).default(2.0),+ interpret: z.boolean().default(true),+ debug: z.boolean().default(false),+});++const edit = new Hono<{ Bindings: Bindings }>();++edit.post('/', zValidator('json', EditBody), async (c) => {+ const env = getEnv(c.env);+ const {+ documentToken,+ documentId,+ prompt,+ models,+ typingAnimations,+ unwatchedSpeed,+ interpret,+ debug,+ } = c.req.valid('json');++ const resolveOne = ({ provider, model }: Model) => {+ const apiKey = env[PROVIDERS[provider].key];+ return PROVIDERS[provider].create({ apiKey })(model);+ };++ // A single model resolves directly; multiple wrap in a fallback that advances+ // to the next model on provider errors/rate limits.+ const resolveModel = (specs: Model[]): LanguageModel => {+ const resolved = specs.map(resolveOne);+ if (resolved.length === 1) return resolved[0];+ return createFallback({+ models: resolved,+ onError: (error, modelId) =>+ console.error(`edit model ${modelId} failed, falling back:`, error),+ });+ };++ // FYI cancellation only works on live cloudflare not workerd. And it requires enable_request_signal.+ const signal = c.req.raw.signal;+ signal.addEventListener('abort', () => {+ console.log('edit session cancelled by client:', documentId);+ });++ try {+ const wsUrl = `${env.SYNC_WS_BASE}/document/${documentId}/connect?token=${documentToken}`;++ // Animations play at 1x while a human is watching and speed up to+ // `unwatchedSpeed` when nobody is, so unseen edits finish faster without+ // being skipped. Presence is re-polled throughout, so a viewer who joins+ // mid-edit slows it back to 1x.+ const presence = watchPresenceSpeed({+ syncWsBase: env.SYNC_WS_BASE,+ documentId,+ docToken: documentToken,+ unwatchedSpeed,+ signal,+ });+ const sleep = (ms: number) =>+ new Promise<void>((resolve) =>+ setTimeout(resolve, ms / presence.multiplier())+ );++ const { usage, ops, trace, clarification } = await runEditSession({+ wsUrl,+ documentId,+ prompt,+ models: {+ supervisor: resolveModel(models.supervisor),+ interpret: resolveModel(models.interpret),+ coding: resolveModel(models.coding),+ },+ typingAnimations,+ sleep,+ interpret,+ debug,+ runner: runInSandbox,+ signal,+ }).finally(presence.stop);+ return c.json({ ok: true, usage, ops, trace, clarification });+ } catch (err) {+ if (!(err instanceof Error)) throw new Error(String(err));+ if (!signal.aborted) {+ console.error('edit session failed:', err.message, err.stack);+ }+ const status = (signal.aborted ? 499 : 502) as 502;+ return c.json({ error: err.message }, status);+ }+});++export default edit;diff --git a/js/ai-editing-worker/src/endpoints/index.ts b/js/ai-editing-worker/src/endpoints/index.tsnew file mode 100644index 0000000000..a71aa42147--- /dev/null+++ b/js/ai-editing-worker/src/endpoints/index.ts@@ -0,0 +1,9 @@+import { Hono } from 'hono';+import type { Bindings } from '../env';+import edit from './edit';++const endpoints = new Hono<{ Bindings: Bindings }>();++endpoints.route('/edit', edit);++export default endpoints;diff --git a/js/ai-editing-worker/src/env.ts b/js/ai-editing-worker/src/env.tsnew file mode 100644index 0000000000..d8cca29d38--- /dev/null+++ b/js/ai-editing-worker/src/env.ts@@ -0,0 +1,31 @@+import { envsafe, str } from 'envsafe';++export type Bindings = {+ ANTHROPIC_API_KEY: string | undefined;+ CEREBRAS_API_KEY: string | undefined;+ OPENAI_API_KEY: string | undefined;+ SYNC_WS_BASE: string;+};++export type Env = ReturnType<typeof validateEnv>;++function validateEnv(rawEnv: Bindings) {+ return envsafe(+ {+ ANTHROPIC_API_KEY: str({ allowEmpty: false }),+ CEREBRAS_API_KEY: str({ allowEmpty: false }),+ OPENAI_API_KEY: str({ allowEmpty: false }),+ SYNC_WS_BASE: str({ allowEmpty: false }),+ },+ { env: rawEnv as Record<string, string | undefined> }+ );+}++let cachedEnv: Env | undefined;+export function getEnv(rawEnv: Bindings): Env {+ if (cachedEnv === undefined) {+ cachedEnv = validateEnv(rawEnv);+ }++ return cachedEnv;+}diff --git a/js/ai-editing-worker/src/globals.ts b/js/ai-editing-worker/src/globals.tsnew file mode 100644index 0000000000..e65542868f--- /dev/null+++ b/js/ai-editing-worker/src/globals.ts@@ -0,0 +1,39 @@+// Stub browser globals that lexical-core pulls in transitively but the worker doesn't need.+// One universal sink that is both callable and has all property accesses absorbed.+// Using `let` + assignment so the closure captures the final value.+// structural any — noopProxy absorbs all shapes+let noopProxy: any;+// structural any — Proxy target must match callable+noopProxy = new Proxy((() => noopProxy) as any, {+ get(_t, k) {+ if (k === Symbol.toPrimitive || k === 'valueOf') return () => 0;+ if (k === 'toString' || k === 'toLocaleString') return () => '';+ if (k === Symbol.iterator) return function* () {};+ if (k === 'length' || k === 'size') return 0;+ return noopProxy;+ },+ set() {+ return true;+ },+ defineProperty() {+ return true;+ },+ has() {+ return true;+ },+ apply() {+ return noopProxy;+ },+ construct() {+ return noopProxy;+ },+});++// globalThis assignment for worker polyfill+(globalThis as any).Prism = {+ highlight: (code: string) => code,+ languages: noopProxy,+ hooks: noopProxy,+ extend: noopProxy,+ util: noopProxy,+};diff --git a/js/ai-editing-worker/src/index.ts b/js/ai-editing-worker/src/index.tsnew file mode 100644index 0000000000..5bbe5bcdac--- /dev/null+++ b/js/ai-editing-worker/src/index.ts@@ -0,0 +1,10 @@+import './globals';+import { Hono } from 'hono';+import endpoints from './endpoints';+import type { Bindings } from './env';++const app = new Hono<{ Bindings: Bindings }>();++app.route('/', endpoints);++export default app;diff --git a/js/ai-editing-worker/src/run-edit.ts b/js/ai-editing-worker/src/run-edit.tsnew file mode 100644index 0000000000..cfca91d704--- /dev/null+++ b/js/ai-editing-worker/src/run-edit.ts@@ -0,0 +1,155 @@+import type { LanguageModel } from 'ai';+import { LoroManager } from '../../app/packages/core/collab/manager';+import type { RawUpdate } from '../../app/packages/core/collab/shared';+import {+ InMemoryWALStore,+ WALSyncer,+} from '../../app/packages/core/collab/wal';+import { MARKDOWN_LORO_SCHEMA } from '../../lexical-core/markdown-loro-schema';+import { supervisor } from './ai-editing/agents';+import type { DocumentOp } from './ai-editing/editor';+import type { CodeRunner } from './ai-editing/runtime';+import type { UsageEntry } from './ai-editing/token-tracker';+import { serializeWithXml } from './ai-editing/utils';+import { EditingWorkspace } from './editing-workspace';+import { createWorkerSyncSource } from './sources';+import { buildTraceLog } from './trace-log';++export type Model = {+ provider: 'anthropic' | 'cerebras' | 'openai';+ model: string;+};++export type Models = {+ supervisor: Model;+ interpret: Model;+ coding: Model;+};++export type ResolvedModels = {+ supervisor: LanguageModel;+ interpret: LanguageModel;+ coding: LanguageModel;+};++export type RunEditArgs = {+ wsUrl: string;+ documentId: string;+ prompt: string;+ models: ResolvedModels;+ /** Snippet runner — QuickJS sandbox in prod, `new Function` in local dev. */+ runner?: CodeRunner;+ typingAnimations?: boolean;+ /** Scales every animation pause; injected so unwatched edits can play faster. */+ sleep?: (ms: number) => Promise<void>;+ signal?: AbortSignal;+ /** Run an intent-interpretation pass before dispatching edits. */+ interpret?: boolean;+ /** Include a markdown trace of all supervisor steps in the result. */+ debug?: boolean;+};++export type { UsageEntry };++export type RunEditResult = {+ usage: UsageEntry[];+ ops: DocumentOp[];+ trace?: string;+ clarification?: string;+};++export async function runEditSession(+ args: RunEditArgs+): Promise<RunEditResult> {+ const source = createWorkerSyncSource(+ args.wsUrl,+ args.documentId,+ args.signal+ );+ const initialResult = await source.doInitialSync();+ if (initialResult.isErr()) {+ source.cleanup();+ const e = initialResult.error;+ throw new Error(`initial sync failed: ${e.type} (${e.duration}ms)`);+ }+ const initial = initialResult.value;++ // The manager owns the one true (merged) doc + mirror.+ const manager = new LoroManager(MARKDOWN_LORO_SCHEMA, {+ documentId: args.documentId,+ });++ const initResult = await manager.initializeFromSnapshot(initial.snapshot);+ if (initResult.isErr()) {+ source.cleanup();+ throw new Error(+ `failed to initialize from snapshot: ${initResult.error[0]?.message}`+ );+ }++ const wal = new WALSyncer<RawUpdate>(+ new InMemoryWALStore<RawUpdate>(),+ (updates) => source.pushUpdate(updates)+ );++ // The workspace owns the editing surface + its two-way sync with Loro, and+ // hands out per-coder writers. Under debug it also records a replay trace.+ const workspace = new EditingWorkspace(manager, source, wal);++ const allOps: DocumentOp[] = [];+ // code, per coder, per batch+ const coderCodeBlocks: string[][][] = [];+ const startedAt = new Date();+ const initialDocument = serializeWithXml(workspace.session);+ try {+ const { totalUsage, steps, intent, clarification } = await supervisor(+ workspace.session,+ args.prompt,+ args.models,+ {+ borrowWriter: () => workspace.borrowWriter(),+ typingAnimations: args.typingAnimations,+ sleep: args.sleep,+ signal: args.signal,+ interpret: args.interpret,+ runner: args.runner,+ onOps: (ops) => allOps.push(...ops),+ onCoderResult: (codes) => coderCodeBlocks.push(codes),+ }+ );++ // Drain the queued propagates (plus a final catch-all sync) and ensure every+ // commit reached the server before we disconnect.+ await workspace.flush();+ await wal.flush();++ const usage = totalUsage.toEntries();++ const trace = buildTraceLog(+ {+ documentId: args.documentId,+ prompt: args.prompt,+ startedAt,+ initialDocument,+ intent,+ coderCodeBlocks,+ },+ steps as any,+ usage+ );++ console.log(JSON.stringify({ documentId: args.documentId, debug: trace }));++ return {+ usage,+ ops: allOps,+ trace: args.debug ? trace : undefined,+ clarification,+ };+ } finally {+ workspace.dispose();+ wal.destroy();+ manager.dispose();+ source.cleanup();+ }+}diff --git a/js/ai-editing-worker/src/sandbox.ts b/js/ai-editing-worker/src/sandbox.tsnew file mode 100644index 0000000000..f9f374d4e4--- /dev/null+++ b/js/ai-editing-worker/src/sandbox.ts@@ -0,0 +1,58 @@+import releaseSync from '@jitl/quickjs-wasmfile-release-sync';+import wasmModule from '@jitl/quickjs-wasmfile-release-sync/dist/emscripten-module.wasm';+import { nanoid } from 'nanoid';+import {+ newQuickJSWASMModuleFromVariant,+ newVariant,+} from 'quickjs-emscripten-core';+import type { DocumentOp } from './ai-editing/editor';+import { SANDBOX_CODE } from './editor-sandbox-code';++/** Upper bound on inserts per snippet. The host mints the ids (QuickJS has no+ * good entropy), pre-generating this many globally-unique ones per run. */+const REF_POOL_SIZE = 128;++// Verbatim from their docs:+// Create a new variant by overriding how Emscripten obtains the WebAssembly+// module. This may be necessary in Cloudflare Workers, which can’t compile+// WebAssembly modules from binary data.+// Magic that makes cloudflare happy :)+const variant = newVariant(releaseSync, { wasmModule });++let _qjs: Awaited<ReturnType<typeof newQuickJSWASMModuleFromVariant>> | null =+ null;++async function qjs() {+ if (!_qjs) _qjs = await newQuickJSWASMModuleFromVariant(variant);+ return _qjs;+}++export async function runInSandbox(+ validIds: Set<string>,+ code: string,+ snippets?: Record<string, string>+): Promise<DocumentOp[]> {+ const QuickJS = await qjs();+ const ctx = QuickJS.newContext();+ try {+ const refs = Array.from({ length: REF_POOL_SIZE }, () => nanoid());+ const init = ctx.unwrapResult(+ ctx.evalCode(+ `${SANDBOX_CODE}\nconst editor = new DocumentEditor({ validIds: ${JSON.stringify([...validIds])}, refs: ${JSON.stringify(refs)} });\nconst snippets = ${JSON.stringify(snippets ?? {})};`+ )+ );+ init.dispose();++ const run = ctx.unwrapResult(ctx.evalCode(code));+ run.dispose();++ const out = ctx.unwrapResult(+ ctx.evalCode('JSON.stringify(editor.drain())')+ );+ const json = ctx.dump(out) as string;+ out.dispose();+ return JSON.parse(json) as DocumentOp[];+ } finally {+ ctx.dispose();+ }+}diff --git a/js/ai-editing-worker/src/service-clients.ts b/js/ai-editing-worker/src/service-clients.tsnew file mode 100644index 0000000000..53152e09d8--- /dev/null+++ b/js/ai-editing-worker/src/service-clients.ts@@ -0,0 +1,60 @@+/**+ * Whether a human collaborator is currently connected to the document, per the+ * sync service's `active_peers` (AI editor peers excluded). Fails open (returns+ * `true`) so a flaky check never silently disables animations.+ */+export async function hasHumanEditors(+ syncWsBase: string,+ documentId: string,+ docToken: string,+ signal?: AbortSignal+): Promise<boolean> {+ try {+ const httpBase = syncWsBase.replace(/^ws/, 'http');+ const resp = await fetch(+ `${httpBase}/document/${documentId}/active_peers?include_ai=false`,+ { headers: { Authorization: `Bearer ${docToken}` }, signal }+ );+ if (!resp.ok) return true;+ const peers = (await resp.json()) as string[];+ return peers.length > 0;+ } catch (err) {+ console.error('hasHumanEditors check failed:', err);+ // we fallback to showing animations+ return true;+ }+}++/** How often presence is re-checked while an edit is animating. */+const PRESENCE_POLL_MS = 2_500;++/**+ * Polls human presence and exposes a live animation speed multiplier: `1` while+ * a human is watching, `unwatchedSpeed` when nobody is — so unseen edits play+ * faster instead of being skipped, and a viewer who joins mid-edit slows it back+ * to 1x. Fails open to watched (1x) on any check error. Caller must `stop()` when+ * the edit finishes; aborting the signal also stops it.+ */+export function watchPresenceSpeed(opts: {+ syncWsBase: string;+ documentId: string;+ docToken: string;+ unwatchedSpeed: number;+ signal?: AbortSignal;+}): { multiplier: () => number; stop: () => void } {+ let multiplier = 1;+ const poll = async () => {+ const watched = await hasHumanEditors(+ opts.syncWsBase,+ opts.documentId,+ opts.docToken,+ opts.signal+ );+ multiplier = watched ? 1 : opts.unwatchedSpeed;+ };+ void poll();+ const timer = setInterval(() => void poll(), PRESENCE_POLL_MS);+ const stop = () => clearInterval(timer);+ opts.signal?.addEventListener('abort', stop);+ return { multiplier: () => multiplier, stop };+}diff --git a/js/ai-editing-worker/src/sources.ts b/js/ai-editing-worker/src/sources.tsnew file mode 100644index 0000000000..31d2ee1d7c--- /dev/null+++ b/js/ai-editing-worker/src/sources.ts@@ -0,0 +1,50 @@+/**+ * Glue for headless callers (the AI editing worker): builds the shared+ * `SyncServiceSource` over a static-URL socket, plus a minimal `Awareness` for+ * AI peer attribution. The wire protocol itself lives in `service-sync` and is+ * shared with the browser — nothing is reimplemented here.+ */+import { EphemeralStore, type PeerID } from 'loro-crdt';+import type { Awareness } from '../../app/packages/core/collab/awareness';+import { createSyncSocket } from '../../app/packages/service-clients/service-sync/source/socket';+import { SyncServiceSource } from '../../app/packages/service-clients/service-sync/source/source';++/**+ * A {@link SyncServiceSource} bound to a fixed, token-bearing URL. A worker+ * request is short-lived and online for its whole life, so every (re)connect+ * resolves to the same URL. Closes the socket on `signal` abort.+ */+export function createWorkerSyncSource(+ wsUrl: string,+ documentId: string,+ signal?: AbortSignal+): SyncServiceSource {+ const ws = createSyncSocket(() => wsUrl);+ const source = new SyncServiceSource(ws, documentId);+ signal?.addEventListener('abort', () => source.cleanup());+ return source;+}++export function createWorkerAwareness(peerId: PeerID): Awareness<unknown> {+ const store = new EphemeralStore(30_000);+ return {+ local: () => ({+ user: { userId: undefined, color: '', peerId },+ selection: undefined,+ }),+ remote: () => [],+ updateLocalAwareness: (selection) => {+ if (selection === undefined) store.delete(peerId);+ else store.set(peerId, selection as never);+ },+ updateRemoteAwareness: () => {},+ importRemoteAwareness: (update) => {+ try {+ store.apply(update);+ } catch {+ /* drop malformed awareness */+ }+ },+ getEncodedLocalAwareness: () => store.encodeAll(),+ };+}diff --git a/js/ai-editing-worker/src/trace-log.ts b/js/ai-editing-worker/src/trace-log.tsnew file mode 100644index 0000000000..b05e4ba9c8--- /dev/null+++ b/js/ai-editing-worker/src/trace-log.ts@@ -0,0 +1,214 @@+import type { StepResult, ToolSet } from 'ai';+import type { UsageEntry } from './ai-editing/token-tracker';++export type TraceMeta = {+ documentId: string;+ prompt: string;+ startedAt: Date;+ initialDocument?: string;+ intent?: string;+ /** JS code blocks run by each coder, indexed by dispatch round then edit index. */+ coderCodeBlocks?: string[][][];+};++type Usage = UsageEntry[];++function formatDispatchArgs(+ args: {+ edits: Array<{+ editing_instruction: string;+ context?: { start_line: number; end_line: number };+ snippets?: Record<string, string>;+ }>;+ },+ codesPerEdit?: string[][]+): string {+ return args.edits+ .map((e, i) => {+ const range = e.context+ ? ` [lines ${e.context.start_line}-${e.context.end_line}]`+ : '';+ let out = `${i + 1}. ${e.editing_instruction}${range}`;+ if (e.snippets && Object.keys(e.snippets).length > 0) {+ for (const [key, value] of Object.entries(e.snippets)) {+ out += `\n snippets.${key}:\n \`\`\`\n${value+ .split('\n')+ .map((l) => ` ${l}`)+ .join('\n')}\n \`\`\``;+ }+ }+ const codes = codesPerEdit?.[i];+ if (codes && codes.length > 0) {+ for (const code of codes) {+ out += `\n\n \`\`\`js\n${code+ .split('\n')+ .map((l) => ` ${l}`)+ .join('\n')}\n \`\`\``;+ }+ }+ return out;+ })+ .join('\n');+}++function formatToolCall(+ call: { toolName: string; input: unknown },+ output: unknown,+ codesPerEdit?: string[][]+): string {+ const lines: string[] = [];++ if (call.toolName === 'dispatch') {+ const { edits } = call.input as {+ edits: Array<{+ editing_instruction: string;+ context?: { start_line: number; end_line: number };+ snippets?: Record<string, string>;+ }>;+ };+ lines.push(`**dispatch** — ${edits.length} edit(s)`);+ lines.push('');+ lines.push(formatDispatchArgs({ edits }, codesPerEdit));+ if (output != null) {+ const res = String(output);+ const docStart = res.indexOf('<document>');+ const docEnd = res.indexOf('</document>');+ const summary =+ docStart !== -1 ? res.slice(0, docStart).trim() : res.trim();+ if (summary) {+ lines.push('');+ lines.push('**result:**');+ lines.push(summary);+ }+ if (docStart !== -1 && docEnd !== -1) {+ lines.push('');+ lines.push('**document after:**');+ lines.push('```xml');+ lines.push(res.slice(docStart + '<document>'.length, docEnd).trim());+ lines.push('```');+ }+ }+ } else if (call.toolName === 'readDocument') {+ lines.push(`**readDocument**`);+ if (output != null) {+ lines.push('');+ lines.push('```xml');+ lines.push(String(output).trim());+ lines.push('```');+ }+ } else {+ lines.push(`**${call.toolName}**`);+ lines.push('```json');+ lines.push(JSON.stringify(call.input, null, 2));+ lines.push('```');+ if (output != null) {+ lines.push('');+ lines.push('**result:**');+ lines.push(String(output).slice(0, 400));+ }+ }++ return lines.join('\n');+}++function formatStep(+ step: StepResult<ToolSet>,+ i: number,+ dispatchRoundRef: { current: number },+ coderCodeBlocks?: string[][][]+): string {+ const lines: string[] = [`### Step ${i + 1}`];++ if (step.text) {+ lines.push('');+ lines.push(step.text.trim());+ }++ for (let j = 0; j < step.toolCalls.length; j++) {+ const call = step.toolCalls[j]!;+ const result = step.toolResults?.[j];+ const codesPerEdit =+ call.toolName === 'dispatch'+ ? coderCodeBlocks?.[dispatchRoundRef.current++]+ : undefined;+ lines.push('');+ lines.push(+ formatToolCall(+ call as unknown as { toolName: string; input: unknown },+ result != null+ ? (result as unknown as { output: unknown }).output+ : undefined,+ codesPerEdit+ )+ );+ }++ const { inputTokens, outputTokens } = step.usage;+ lines.push('');+ lines.push(+ `*${inputTokens.toLocaleString()} in / ${outputTokens.toLocaleString()} out*`+ );++ return lines.join('\n');+}++function formatTrace(+ meta: TraceMeta,+ steps: StepResult<ToolSet>[],+ usage: Usage+): string {+ const sections: string[] = [+ '# AI Edit Trace',+ `- **document:** ${meta.documentId}`,+ `- **timestamp:** ${meta.startedAt.toISOString()}`,+ `- **prompt:** ${meta.prompt}`,+ ];++ if (meta.initialDocument) {+ sections.push(+ '',+ '**document before:**',+ '```xml',+ meta.initialDocument,+ '```'+ );+ }++ if (meta.intent) {+ sections.push('', '---', '', '## Interpreter', '', meta.intent);+ }++ sections.push('', '---', '', '## Supervisor');++ const dispatchRoundRef = { current: 0 };+ for (let i = 0; i < steps.length; i++) {+ sections.push('');+ sections.push(+ formatStep(steps[i]!, i, dispatchRoundRef, meta.coderCodeBlocks)+ );+ }++ sections.push(+ '',+ '---',+ '',+ '## Usage',+ '| model | input | output |',+ '|---|---|---|'+ );+ for (const u of usage) {+ sections.push(+ `| ${u.model} | ${u.inputTokens.toLocaleString()} | ${u.outputTokens.toLocaleString()} |`+ );+ }++ return sections.join('\n');+}++export function buildTraceLog(+ meta: TraceMeta,+ steps: StepResult<ToolSet>[],+ usage: Usage+): string {+ return formatTrace(meta, steps, usage);+}diff --git a/js/ai-editing-worker/tsconfig.json b/js/ai-editing-worker/tsconfig.jsonnew file mode 100644index 0000000000..b3dceaa825--- /dev/null+++ b/js/ai-editing-worker/tsconfig.json@@ -0,0 +1,54 @@+{+ "compilerOptions": {+ "esModuleInterop": true,+ "skipLibCheck": true,+ "target": "es2022",+ "verbatimModuleSyntax": false,+ "allowJs": true,+ "resolveJsonModule": true,+ "moduleDetection": "force",+ "downlevelIteration": true,+ "noImplicitAny": false,+ "strictNullChecks": false,+ "strict": true,+ "noUncheckedIndexedAccess": true,+ "moduleResolution": "Bundler",+ "module": "es2022",+ "noEmit": true,+ "lib": [+ "es2023",+ "dom"+ ],+ "types": [+ "@types/node",+ "bun-types"+ ],+ "baseUrl": ".",+ "paths": {+ "@loro-mirror/*": [+ "../loro-mirror/*"+ ],+ "@core/*": [+ "../app/packages/core/*"+ ],+ "@websocket": [+ "../app/packages/websocket"+ ],+ "@websocket/*": [+ "../app/packages/websocket/*"+ ]+ }+ },+ "exclude": [+ "node_modules",+ "../lexical-core/nodes",+ "../lexical-core/transformers",+ "../loro-mirror"+ ],+ "include": [+ "src",+ "scripts",+ "wasm.d.ts",+ "prompts.d.ts"+ ]+}diff --git a/js/ai-editing-worker/wasm.d.ts b/js/ai-editing-worker/wasm.d.tsnew file mode 100644index 0000000000..006b56a5dd--- /dev/null+++ b/js/ai-editing-worker/wasm.d.ts@@ -0,0 +1,4 @@+declare module "*.wasm" {+ const value: WebAssembly.Module;+ export default value;+}diff --git a/js/ai-editing-worker/wrangler.toml b/js/ai-editing-worker/wrangler.tomlnew file mode 100644index 0000000000..5f4c17ea95--- /dev/null+++ b/js/ai-editing-worker/wrangler.toml@@ -0,0 +1,65 @@+name = "ai-editing-worker"+main = "src/index.ts"+compatibility_date = "2025-07-22"+compatibility_flags = ["nodejs_compat", "enable_request_signal"]++[alias]+"loro-crdt" = "loro-crdt/bundler"+"@observability/logger" = "./src/logger-shim.ts"+"@core" = "../app/packages/core"+"@loro-mirror" = "../loro-mirror"+"@websocket" = "../app/packages/websocket/index.ts"++[[rules]]+type = "Text"+globs = ["**/*.md"]+fallthrough = true++[[rules]]+type = "CompiledWasm"+globs = ["**/*.wasm"]+fallthrough = true++[observability]+enabled = true++[vars]+SYNC_WS_BASE = "wss://sync-service-prod2.macroverse.workers.dev"+DSS_BASE = "https://cloud-storage.macro.com"+SEARCH_SERVICE_BASE = "https://cloud-storage.macro.com"+CONTACTS_SERVICE_BASE = "https://contacts.macro.com"+AUTH_SERVICE_BASE = "https://auth-service.macro.com"++[dev]+port = 8933++[env.local.vars]+SYNC_WS_BASE = "ws://sync-service:8787"+DSS_BASE = "http://document-storage-service:8080"+SEARCH_SERVICE_BASE = "http://search-processing-service:8080"+CONTACTS_SERVICE_BASE = "http://contacts-service:8080"+AUTH_SERVICE_BASE = "http://connection-gateway:8080"++[env.dev.vars]+SYNC_WS_BASE = "wss://sync-service-dev3.macroverse.workers.dev"+DSS_BASE = "https://cloud-storage-dev.macro.com"+SEARCH_SERVICE_BASE = "https://cloud-storage-dev.macro.com"+CONTACTS_SERVICE_BASE = "https://contacts-dev.macro.com"+AUTH_SERVICE_BASE = "https://auth-service-dev.macro.com"++[env.playground]+name = "ai-editing-worker-playground"++[env.playground.vars]+SYNC_WS_BASE = "wss://sync-service-dev3.macroverse.workers.dev"+DSS_BASE = "https://cloud-storage-dev.macro.com"+SEARCH_SERVICE_BASE = "https://cloud-storage-dev.macro.com"+CONTACTS_SERVICE_BASE = "https://contacts-dev.macro.com"+AUTH_SERVICE_BASE = "https://auth-service-dev.macro.com"++[env.prod.vars]+SYNC_WS_BASE = "wss://sync-service-prod2.macroverse.workers.dev"+DSS_BASE = "https://cloud-storage.macro.com"+SEARCH_SERVICE_BASE = "https://cloud-storage.macro.com"+CONTACTS_SERVICE_BASE = "https://contacts.macro.com"+AUTH_SERVICE_BASE = "https://auth-service.macro.com"diff --git a/js/app/packages/block-md/comments/CommentsProvider.tsx b/js/app/packages/block-md/comments/CommentsProvider.tsxindex 5aa0694732..056e828c70 100644--- a/js/app/packages/block-md/comments/CommentsProvider.tsx+++ b/js/app/packages/block-md/comments/CommentsProvider.tsx@@ -87,7 +87,7 @@ export const CommentsProvider: VoidComponent<{}const { plugins, editor } = wrapper;- const currentPeerId = () => props.loroManager.getPeerIdStr();+ const currentPeerId = () => props.loroManager.peerIdStr;const [marks, setMarks] = markStore;const [commentThreadsData] = commentThreadsResource;diff --git a/js/app/packages/block-md/component/Block.tsx b/js/app/packages/block-md/component/Block.tsxindex 92dc4aaa0a..45e40f686f 100644--- a/js/app/packages/block-md/component/Block.tsx+++ b/js/app/packages/block-md/component/Block.tsx@@ -64,7 +64,7 @@ async function ingestLocalSnapshot(// we replay, and then reload, and now we are in a state where we have an old// document until the new one loads inif (walEntries.length >= 1) {- const doc = loroManager.getDoc();+ const doc = loroManager.doc;const snapshot = doc.export({mode: 'shallow-snapshot',frontiers: doc.oplogFrontiers(),@@ -80,7 +80,7 @@ async function ingestRemoteSnapshot(const sync = await doInitialSync();if (sync.isErr()) {console.error('Failed to receive initial sync', sync.error);- return loroManager.isInitialized();+ return loroManager.initialized;}await loroManager.ingest({kind: 'dss',@@ -112,11 +112,10 @@ function BlockMarkdownContent({ optimisticSnapshot }: BlockMarkdownProps) {const [scrollRef, setScrollRef] = createSignal<HTMLDivElement>();const blockId = useBlockId();- const getSyncSource = blockSyncSourceSignal.get;+ const _getSyncSource = blockSyncSourceSignal.get;const setBlockError = blockErrorSignal.set;const loroManager = createLoroManager(MARKDOWN_LORO_SCHEMA, {- liveSyncSource: () => getSyncSource()!,documentId: blockId,});diff --git a/js/app/packages/block-md/component/InstructionsEditor.tsx b/js/app/packages/block-md/component/InstructionsEditor.tsxindex d8741b47cc..c4129b2f26 100644--- a/js/app/packages/block-md/component/InstructionsEditor.tsx+++ b/js/app/packages/block-md/component/InstructionsEditor.tsx@@ -160,7 +160,7 @@ export function InstructionsEditor(props: {if (!IS_SYNC()) {return createPeerIdValidator(() => undefined, false);}- const peerId = () => props.loroManager.getPeerIdStr();+ const peerId = () => props.loroManager.peerIdStr;return createPeerIdValidator(peerId, true);};@@ -213,7 +213,7 @@ export function InstructionsEditor(props: {);if (ENABLE_MARKDOWN_LIVE_COLLABORATION) {- const peerId = () => props.loroManager.getPeerIdStr();+ const peerId = () => props.loroManager.peerIdStr;plugins.use(peerIdPlugin({peerId,@@ -452,7 +452,10 @@ export function InstructionsEditor(props: {title="Lexical state debugger"onClose={props.onLexicalStateDebuggerClose}>- <LexicalStateDebugger state={state()}></LexicalStateDebugger>+ <LexicalStateDebugger+ state={state()}+ editor={editor}+ ></LexicalStateDebugger></SplitBottomPanel>)}</Show>diff --git a/js/app/packages/block-md/component/MarkdownCollabProvider.tsx b/js/app/packages/block-md/component/MarkdownCollabProvider.tsxindex a25cbe0634..be578f630f 100644--- a/js/app/packages/block-md/component/MarkdownCollabProvider.tsx+++ b/js/app/packages/block-md/component/MarkdownCollabProvider.tsx@@ -68,7 +68,6 @@ import {onCleanup,type Setter,} from 'solid-js';-import { untrack } from 'solid-js/web';import { CollabStatus } from './CollabStatus';type MutatedNodes = UpdateListenerPayload['mutatedNodes'];@@ -103,7 +102,7 @@ export function MarkdownCollabProvider(props: MarkdownCollabProviderProps) {if (!syncSource()) return null;const awareness = createAwareness(- loroManager.getPeerIdStr(),+ loroManager.peerIdStr,userId(),lexicalSelectionCodec,{@@ -176,7 +175,7 @@ export function MarkdownCollabProvider(props: MarkdownCollabProviderProps) {props.editor.getEditorState().toJSON(),state,props.mappings,- () => loroManager.getPeerIdStr()+ () => loroManager.peerIdStr);// Queue microtask after this `editor.update` to ensure that all the nodeIds are updated@@ -419,20 +418,19 @@ export function MarkdownCollabProvider(props: MarkdownCollabProviderProps) {}function startSync() {- const started = syncEngine.start();- logSyncService({- documentId: syncSource()!.documentId,- level: 'debug',- context: {},- message: `engine.start() → ${started}`,- });+ syncEngine.start();props.pluginManager.use(lexicalStateSyncPlugin);}+ const [managerInitialized, setManagerInitialized] = createSignal(+ loroManager.initialized+ );+ onCleanup(loroManager.onInitializedChange(setManagerInitialized));+/** Initializes the loroManager and starts the sync engine */createEffect(on(- () => loroManager.isInitialized() ?? false,+ () => managerInitialized() ?? false,(isInitialized) => {if (!isInitialized) {logSyncService({@@ -458,7 +456,7 @@ export function MarkdownCollabProvider(props: MarkdownCollabProviderProps) {// Get the current state from the loroManager// At this point, the loroManager should be initialized and should// have the initial state from the sync service- const state = untrack(loroManager.state);+ const state = loroManager.state;const empty = state? isStateEmpty(state.state as unknown as SerializedEditorState): null;@@ -505,7 +503,7 @@ export function MarkdownCollabProvider(props: MarkdownCollabProviderProps) {const initError = initializeEditorWithVersionedState(props.editor,state.state as unknown as SerializedEditorState,- loroManager.getPeerIdStr+ () => loroManager.peerIdStr);if (initError !== null) {logSyncService({diff --git a/js/app/packages/block-md/component/MarkdownEditor.tsx b/js/app/packages/block-md/component/MarkdownEditor.tsxindex aaa0bfae73..2bce868117 100644--- a/js/app/packages/block-md/component/MarkdownEditor.tsx+++ b/js/app/packages/block-md/component/MarkdownEditor.tsx@@ -510,7 +510,7 @@ export function MarkdownEditor(props: {if (!IS_SYNC()) {return createPeerIdValidator(() => undefined, false);}- const peerId = () => props.loroManager.getPeerIdStr();+ const peerId = () => props.loroManager.peerIdStr;return createPeerIdValidator(peerId, true);};@@ -629,7 +629,7 @@ export function MarkdownEditor(props: {}if (ENABLE_MARKDOWN_LIVE_COLLABORATION) {- const peerId = () => props.loroManager.getPeerIdStr();+ const peerId = () => props.loroManager.peerIdStr;plugins.use(peerIdPlugin({peerId,@@ -1129,7 +1129,10 @@ export function MarkdownEditor(props: {title="Lexical state debugger"onClose={props.onLexicalStateDebuggerClose}>- <LexicalStateDebugger state={state()}></LexicalStateDebugger>+ <LexicalStateDebugger+ state={state()}+ editor={editor}+ ></LexicalStateDebugger></SplitBottomPanel>)}</Show>diff --git a/js/app/packages/core/collab/chatter.ts b/js/app/packages/core/collab/chatter.tsnew file mode 100644index 0000000000..254307d36b--- /dev/null+++ b/js/app/packages/core/collab/chatter.ts@@ -0,0 +1,51 @@+import type { RawUpdate } from './shared';++/** What co-located replicas gossip to each other, out-of-band from the live+ * transport: a loro update or an awareness blob. */+export type ChatterMessage =+ | { type: 'update'; data: RawUpdate }+ | { type: 'awareness'; data: RawUpdate };++/**+ * Generic interface for "other people" that should also receive updates from+ * the local peer.+ *+ * Usually this is just a wrapper around a broadcast channel.+ */+export interface Chatter {+ /** Broadcast a message to the other replicas. */+ post(message: ChatterMessage): void;+ /** Subscribe to messages from other replicas. Returns an unsubscribe fn. */+ subscribe(handler: (message: ChatterMessage) => void): () => void;+ /** Tear down the channel. */+ close(): void;+}++export function noopChatter(): Chatter {+ return { post: () => {}, subscribe: () => () => {}, close: () => {} };+}++const CHANNEL_PREFIX = 'macro-loro-';++/** Cross-tab gossip over the browser {@link BroadcastChannel}. */+export class BroadcastChannelChatter implements Chatter {+ private readonly channel: BroadcastChannel;++ constructor(documentId: string) {+ this.channel = new BroadcastChannel(`${CHANNEL_PREFIX}${documentId}`);+ }++ public post(message: ChatterMessage): void {+ this.channel.postMessage(message);+ }++ public subscribe(handler: (message: ChatterMessage) => void): () => void {+ const listener = (e: MessageEvent<ChatterMessage>) => handler(e.data);+ this.channel.addEventListener('message', listener);+ return () => this.channel.removeEventListener('message', listener);+ }++ public close(): void {+ this.channel.close();+ }+}diff --git a/js/app/packages/core/collab/engine.test.ts b/js/app/packages/core/collab/engine.test.tsindex 7fb2ae326b..cae92914bb 100644--- a/js/app/packages/core/collab/engine.test.ts+++ b/js/app/packages/core/collab/engine.test.ts@@ -1,7 +1,12 @@import { LoroDoc } from 'loro-crdt';import { describe, expect, it, vi } from 'vitest';import { SyncEngine } from './engine';-import { MockLiveSyncSource, MockLoroManager, makeTestWAL } from './testing';+import {+ MockChatter,+ MockLiveSyncSource,+ MockLoroManager,+ makeTestWAL,+} from './testing';const emptySnapshot = () => new LoroDoc().export({ mode: 'snapshot' });@@ -113,6 +118,73 @@ describe('SyncEngine', () => {});});+ describe('cross-replica chatter', () => {+ it('broadcasts local updates to the chatter', async () => {+ const source = new MockLiveSyncSource();+ const { wal } = makeTestWAL(source);+ const manager = new MockLoroManager();+ const chatter = new MockChatter();+ const engine = new SyncEngine({+ loroManager: manager,+ awareness: makeAwareness(),+ syncs: { wal, live: source },+ bindings: { onRemoteState: vi.fn() },+ makeChatter: () => chatter,+ });++ engine.start();+ const update = new Uint8Array([1, 2, 3]);+ manager.triggerLocalUpdate(update);++ await vi.waitFor(() =>+ expect(chatter.posted).toContainEqual({ type: 'update', data: update })+ );+ });++ it('applies updates received from another replica', async () => {+ const source = new MockLiveSyncSource();+ const { wal } = makeTestWAL(source);+ const manager = new MockLoroManager();+ const chatter = new MockChatter();+ const engine = new SyncEngine({+ loroManager: manager,+ awareness: makeAwareness(),+ syncs: { wal, live: source },+ bindings: { onRemoteState: vi.fn() },+ makeChatter: () => chatter,+ });++ engine.start();+ const update = new Uint8Array([4, 5, 6]);+ chatter.receive({ type: 'update', data: update });++ await vi.waitFor(() =>+ expect(manager.importUpdate).toHaveBeenCalledWith(update)+ );+ });++ it('stops listening to the chatter on stop', () => {+ const source = new MockLiveSyncSource();+ const { wal } = makeTestWAL(source);+ const manager = new MockLoroManager();+ const chatter = new MockChatter();+ const engine = new SyncEngine({+ loroManager: manager,+ awareness: makeAwareness(),+ syncs: { wal, live: source },+ bindings: { onRemoteState: vi.fn() },+ makeChatter: () => chatter,+ });++ engine.start();+ engine.stop();+ chatter.receive({ type: 'update', data: new Uint8Array([7]) });++ expect(chatter.closed).toBe(true);+ expect(manager.importUpdate).not.toHaveBeenCalled();+ });+ });+it('does not start when manager is not initialized', () => {const source = new MockLiveSyncSource();const { wal } = makeTestWAL(source);diff --git a/js/app/packages/core/collab/engine.ts b/js/app/packages/core/collab/engine.tsindex 99e65dcf59..8394ad855f 100644--- a/js/app/packages/core/collab/engine.ts+++ b/js/app/packages/core/collab/engine.ts@@ -2,11 +2,22 @@ import { type InferType, SyncDirection } from '@loro-mirror/packages/core/src';import { Mutex } from 'async-mutex';import type { VersionVector } from 'loro-crdt';import type { ResultAsync } from 'neverthrow';-import { type Accessor, createEffect, createSignal, on } from 'solid-js';+import {+ type Accessor,+ createEffect,+ createSignal,+ on,+ onCleanup,+} from 'solid-js';import { match } from 'ts-pattern';import type { Awareness } from './awareness';+import { BroadcastChannelChatter, type Chatter, noopChatter } from './chatter';import { logSyncService } from './logger';-import { type LoroManager, LoroStateTag, type StateUpdate } from './manager';+import {+ LoroStateTag,+ type StateUpdate,+ type SyncEngineManager,+} from './manager';import type { GenericRootSchema, LoroRawUpdate, RawUpdate } from './shared';import type { SnapshotStore } from './snapshot-store';@@ -31,23 +42,18 @@ export type SyncSources = {};export type SyncEngineParams<S extends GenericRootSchema, D> = {- loroManager: LoroManager<S>;+ loroManager: SyncEngineManager<S>;awareness: Awareness<D>;syncs: SyncSources;bindings: EngineBindings<S>;readonly?: () => boolean;onRunningChange?: (v: boolean) => void;snapshotStore?: LoroSnapshotStore;+ makeChatter?: (documentId: string) => Chatter;};type SnapshotThunk = () => ResultAsync<Uint8Array, TimeoutError>;-const CROSS_TAB_CHANNEL_PREFIX = 'macro-loro-';--type CrossTabMessage =- | { type: 'update'; data: RawUpdate }- | { type: 'awareness'; data: RawUpdate };-export class SyncEngine<S extends GenericRootSchema, D> {private _isRunning = false;@@ -55,7 +61,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {return this._isRunning;}- private readonly loroManager: LoroManager<S>;+ private readonly loroManager: SyncEngineManager<S>;private readonly awareness: Awareness<D>;private readonly syncs: SyncSources;private readonly bindings: EngineBindings<S>;@@ -66,7 +72,9 @@ export class SyncEngine<S extends GenericRootSchema, D> {private readonly snapshotStore?: LoroSnapshotStore;private readonly defaultSnapshotThunk: SnapshotThunk;private readonly onRunningChange: (v: boolean) => void;- private crossTabChannel?: BroadcastChannel;+ private readonly makeChatter: (documentId: string) => Chatter;+ private chatter?: Chatter;+ private chatterUnsub?: () => void;constructor({loroManager,@@ -76,6 +84,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {readonly = () => false,onRunningChange = () => {},snapshotStore,+ makeChatter = () => noopChatter(),}: SyncEngineParams<S, D>) {this.loroManager = loroManager;this.awareness = awareness;@@ -85,6 +94,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {this.defaultSnapshotThunk = syncs.live.requestSnapshot;this.onRunningChange = onRunningChange;this.snapshotStore = snapshotStore;+ this.makeChatter = makeChatter;}private log(@@ -103,35 +113,28 @@ export class SyncEngine<S extends GenericRootSchema, D> {public start(): boolean {if (this._isRunning) return true; // already running — idempotent- if (!this.loroManager.isInitialized()) {+ if (!this.loroManager.initialized) {this.log('warn', 'engine.start: manager not initialized, aborting');return false;}this.unsubscribe?.();- this.unsubscribe = this.loroManager- .getDoc()- .subscribeLocalUpdates((update) => {- this.handleLocalUpdates(update);- });+ this.unsubscribe = this.loroManager.doc.subscribeLocalUpdates((update) => {+ this.handleLocalUpdates(update);+ });- this.crossTabChannel = new BroadcastChannel(- `${CROSS_TAB_CHANNEL_PREFIX}${this.syncs.live.documentId}`- );- this.crossTabChannel.onmessage = (e: MessageEvent<CrossTabMessage>) => {- match(e.data)- .with(- { type: 'update' },- (msg) => void this.handleRemoteUpdate(msg.data)- )- .with({ type: 'awareness' }, (msg) =>- this.awareness.importRemoteAwareness(msg.data)+ this.chatter = this.makeChatter(this.syncs.live.documentId);+ this.chatterUnsub = this.chatter.subscribe((msg) =>+ match(msg)+ .with({ type: 'update' }, (m) => void this.handleRemoteUpdate(m.data))+ .with({ type: 'awareness' }, (m) =>+ this.awareness.importRemoteAwareness(m.data))- .exhaustive();- };+ .exhaustive()+ );this.syncs.live.listen((event) => this.handleSourceEvent(event));- this.syncs.live.registerPeerId(this.loroManager.getPeerId());+ this.syncs.live.registerPeerId(this.loroManager.peerId);if (this.snapshotStore && this.snapshotInterval === undefined) {this.snapshotInterval = setInterval(@@ -150,8 +153,10 @@ export class SyncEngine<S extends GenericRootSchema, D> {this.unsubscribe?.();this.unsubscribe = undefined;- this.crossTabChannel?.close();- this.crossTabChannel = undefined;+ this.chatterUnsub?.();+ this.chatter?.close();+ this.chatterUnsub = undefined;+ this.chatter = undefined;if (this.snapshotInterval !== undefined) {clearInterval(this.snapshotInterval);@@ -173,7 +178,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {if (syncResult.isErr()) {this.log('error', 'syncStateToLoro: failed, resetting engine', {- err: syncResult.error,+ err: syncResult,});this.reset();}@@ -207,7 +212,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {const resetResult = await this.loroManager.reset(snapshot.value);if (resetResult.isErr()) {this.log('error', 'engine.reset: loro manager reset failed', {- err: resetResult.error,+ err: resetResult,});return;}@@ -234,17 +239,14 @@ export class SyncEngine<S extends GenericRootSchema, D> {const awarenessUpdate = this.awareness.getEncodedLocalAwareness();if (!awarenessUpdate) return;this.syncs.live.pushAwareness(awarenessUpdate);- this.crossTabChannel?.postMessage({- type: 'awareness',- data: awarenessUpdate,- });+ this.chatter?.post({ type: 'awareness', data: awarenessUpdate });}private async handleLocalUpdates(update: LoroRawUpdate) {if (this.readonly()) return;this.log('debug', 'engine: local update, appending to WAL');void this.syncs.wal.append(update);- this.crossTabChannel?.postMessage({ type: 'update', data: update });+ this.chatter?.post({ type: 'update', data: update });}private async persistSnapshot() {@@ -253,7 +255,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {void this.syncs.wal.flush(); // unawaitedtry {- const doc = this.loroManager.getDoc();+ const doc = this.loroManager.doc;this.log('debug', 'engine: persisting snapshot', { doc: doc.toJSON() });const snapshot = doc.export({mode: 'shallow-snapshot',@@ -279,7 +281,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {await Promise.resolve();if (importResult.isErr()) {this.log('error', 'engine: failed to import remote update, resetting', {- err: importResult.error,+ err: importResult,});this.reset();return;@@ -306,7 +308,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {'info','engine: reconnect, requesting updates since current version');- this.requestAndHandleUpdatesSince(this.loroManager.getDoc().version());+ this.requestAndHandleUpdatesSince(this.loroManager.doc.version());break;}}@@ -319,8 +321,7 @@ export class SyncEngine<S extends GenericRootSchema, D> {const updates = await this.syncs.live.requestUpdatesSince(since);if (updates.isErr() || !updates.value) {this.log('error', 'engine: requestUpdatesSince failed', {- err: updates.isErr() ? updates.error : 'update is undefined',- attempt,+ err: 'error' in updates ? updates.error : 'update is undefined',});if (updates.isErr() && attempt < REQUEST_UPDATES_MAX_ATTEMPTS) {await new Promise((resolve) =>@@ -357,10 +358,18 @@ export function createSyncEngine<): ReactiveSyncEngine<S, D> {const [isRunning, setIsRunning] = createSignal(false);- const engine = new SyncEngine({ ...params, onRunningChange: setIsRunning });+ const engine = new SyncEngine({+ ...params,+ onRunningChange: setIsRunning,+ // In the browser, gossip local edits to other tabs of the same doc.+ makeChatter:+ params.makeChatter ?? ((id) => new BroadcastChannelChatter(id)),+ });const { loroManager, awareness } = params;- createEffect(on(loroManager.state, (update) => engine.onStateUpdate(update)));+ onCleanup(+ loroManager.onStateChange((update) => engine.onStateUpdate(update))+ );createEffect(on(awareness.local, () => engine.onLocalAwarenessChange()));return {diff --git a/js/app/packages/core/collab/logger.ts b/js/app/packages/core/collab/logger.tsindex a5b937f088..bcd1679891 100644--- a/js/app/packages/core/collab/logger.ts+++ b/js/app/packages/core/collab/logger.ts@@ -20,7 +20,9 @@ export function logSyncService({context: SyncLogContext;message: string;}): void {- if (level === 'debug' && !(window as any).debugSyncServiceLog) return;+ const debugEnabled =+ typeof window !== 'undefined' && (window as any).debugSyncServiceLog;+ if (level === 'debug' && !debugEnabled) return;console[level === 'info' ? 'log' : level]({ documentId, t: Date.now(), ...context },messagediff --git a/js/app/packages/core/collab/manager-integration.test.ts b/js/app/packages/core/collab/manager-integration.test.tsindex 5b8ae05061..2cdfefeb39 100644--- a/js/app/packages/core/collab/manager-integration.test.ts+++ b/js/app/packages/core/collab/manager-integration.test.ts@@ -1,5 +1,5 @@import { Mirror, schema } from '@loro-mirror/packages/core/src';-import { LoroDoc, type VersionVector } from 'loro-crdt';+import { LoroDoc } from 'loro-crdt';import { createRoot } from 'solid-js';import { describe, expect, it } from 'vitest';import { createLoroManager, type LoroManager } from './manager';@@ -17,7 +17,7 @@ const TEST_SCHEMA = schema({});function paragraphTexts(manager: LoroManager<typeof TEST_SCHEMA>): string[] {- return manager.state()?.state.paragraphs.map((p) => p.text) ?? [];+ return manager.state?.state.paragraphs.map((p) => p.text) ?? [];}async function buildSnapshot(@@ -39,86 +39,47 @@ function pushToServer(manager: LoroManager<typeof TEST_SCHEMA>,server: TestServer) {- server.applyUpdate(manager.getDoc().export({ mode: 'update' }));+ server.applyUpdate(manager.doc.export({ mode: 'update' }));}describe('LoroManager seed + converge — two-client merge', () => {- it("client B reconnects dirty: requestUpdatesSince merges its offline edits with A's concurrent edits", async () => {+ it('two clients seeded from the same snapshot converge after concurrent edits', async () => {await createRoot(async (dispose) => {- // ── X: shared initial snapshot ────────────────────────────────────const initialSnapshotX = await buildSnapshot([{ id: 'p1', text: 'X ' }]);const server = new TestServer();server.applyUpdate(initialSnapshotX);- // ── Client A — always online ──────────────────────────────────────const clientA = createLoroManager(TEST_SCHEMA, {- liveSyncSource: () => server.asLiveSyncSource(),documentId: 'test-doc-a',});await clientA.ingest({ kind: 'dss', snapshot: initialSnapshotX });- // ── Client B — will go offline ────────────────────────────────────const clientB = createLoroManager(TEST_SCHEMA, {- liveSyncSource: () => server.asLiveSyncSource(),documentId: 'test-doc-b',});await clientB.ingest({ kind: 'dss', snapshot: initialSnapshotX });- // ── B makes an online edit, pushed to the server ──────────────────- await clientB.syncToLoro({- paragraphs: [{ id: 'p1', text: 'X online-B ' }],- });- pushToServer(clientB, server);-- // ── B goes offline; this edit never reaches the server ────────────- await clientB.syncToLoro({- paragraphs: [{ id: 'p1', text: 'X online-B offline-B ' }],- });- const offlineSnapshotFromB = clientB- .getDoc()- .export({ mode: 'snapshot' });- const bVvAfterOffline = clientB.getDoc().version();-- // ── A receives B's online-B from server (via full update import) ─- clientA.getDoc().import(server.doc.export({ mode: 'update' }));- // ── A makes its own edit and pushes ───────────────────────────────await clientA.syncToLoro({- paragraphs: [{ id: 'p1', text: 'X online-B online-A ' }],+ paragraphs: [{ id: 'p1', text: 'X edit-A ' }],});pushToServer(clientA, server);- // ── B reconnects: fresh manager seeds from its local snapshot ─────- const reconnectSource = server.asLiveSyncSource();- const reconnectedB = createLoroManager(TEST_SCHEMA, {- liveSyncSource: () => reconnectSource,- documentId: 'test-doc-b-reconnect',- });-- await reconnectedB.ingest({- kind: 'local',- snapshot: offlineSnapshotFromB,+ await clientB.syncToLoro({+ paragraphs: [{ id: 'p1', text: 'X edit-B ' }],});+ pushToServer(clientB, server);- // ── Assertions ────────────────────────────────────────────────────-- // 1. Seeding triggered exactly one requestUpdatesSince to converge.- expect(reconnectSource.requestUpdatesSince).toHaveBeenCalledTimes(1);+ // Both clients pull the full server state+ clientA.doc.import(server.doc.export({ mode: 'update' }));+ clientB.doc.import(server.doc.export({ mode: 'update' }));- // 2. It was called with B's post-offline version vector (so the- // server's delta excludes anything B already has).- const requestedVv = (- reconnectSource.requestUpdatesSince as unknown as {- mock: { calls: unknown[][] };- }- ).mock.calls[0]?.[0] as VersionVector;- expect(requestedVv.toJSON()).toEqual(bVvAfterOffline.toJSON());+ const textA = paragraphTexts(clientA)[0]!;+ const textB = paragraphTexts(clientB)[0]!;- // 3. Final state contains substrings contributed by every side.- const finalText = paragraphTexts(reconnectedB)[0]!;- expect(finalText).toContain('X'); // shared base- expect(finalText).toContain('online-B'); // B's online edit- expect(finalText).toContain('offline-B'); // B's local-only offline edit- expect(finalText).toContain('online-A'); // A's edit B missed+ expect(textA).toContain('X');+ expect(textA).toContain('edit-A');+ expect(textA).toContain('edit-B');+ expect(textA).toEqual(textB);dispose();});diff --git a/js/app/packages/core/collab/manager.ts b/js/app/packages/core/collab/manager.tsindex fdc7fe44ef..8a18d704fb 100644--- a/js/app/packages/core/collab/manager.ts+++ b/js/app/packages/core/collab/manager.ts@@ -10,16 +10,16 @@ import {type Container,type ContainerID,type Cursor,+ type ImportStatus,LoroDoc,type PeerID,type Side,type VersionVector,} from 'loro-crdt';import { err, ok, type Result } from 'neverthrow';-import { type Accessor, createEffect, createSignal, onCleanup } from 'solid-js';+import { onCleanup } from 'solid-js';import { logSyncService } from './logger';import type { GenericRootSchema, LoroRawUpdate, RawUpdate } from './shared';-import type { LiveSyncSource } from './source';export enum LoroManagerError {ImportFailed = 'IMPORT_FAILED',@@ -37,57 +37,98 @@ export enum LoroStateTag {FromManager = 'FROM_MANAGER',}+export type StateUpdate<S extends GenericRootSchema> = {+ state: InferType<S>;+ metadata: UpdateMetadata;+};++export type SnapshotIngest =+ | { kind: 'optimistic'; snapshot: RawUpdate }+ | {+ kind: 'local';+ snapshot: RawUpdate;+ /** Optional WAL update entries to replay on top of the snapshot. */+ walUpdates?: RawUpdate[];+ }+ | { kind: 's3'; snapshot: RawUpdate }+ | { kind: 'dss'; snapshot: RawUpdate };++export type LoroManagerOptions = {+ documentId: string;+};++export interface SyncEngineManager<+ S extends GenericRootSchema = GenericRootSchema,+> {+ readonly doc: LoroDoc;+ readonly initialized: boolean;+ readonly peerId: bigint;+ importUpdate(+ update: LoroRawUpdate+ ): Result<boolean, ResultError<LoroManagerError>[]>;+ syncToLoro(+ state: InferType<S>+ ): Promise<Result<void, ResultError<LoroManagerError>[]>>;+ reset(+ snapshot: LoroRawUpdate+ ): Promise<Result<void, ResultError<LoroManagerError>[]>>;+ /** Subscribe to state changes; `createSyncEngine` uses this to wire the+ * manager's updates into the engine. Returns an unsubscribe fn. */+ onStateChange(listener: (update: StateUpdate<S>) => void): () => void;+}+/**- * The LoroManager is responsible for managing the state of the LoroDoc- * It does this by syncing arbitrary json state to and from the LoroDoc- * via the Mirror. The mirror will incrementally diff the incoming json state- * and correctly apply it to the LoroDoc.+ * The LoroManager manages the state of a LoroDoc by syncing arbitrary JSON+ * state to and from it via the {@link Mirror}, which incrementally diffs the+ * incoming JSON state and applies it to the LoroDoc.** ┌────────────┬──────────────────────────────────────┐* │ Manager │ │* ├────────────┘ │- * │ │* │ .───────. │* │ ╱ ╲ │* │ ( State )◀─┐ │* │ `. ,' │ │* │ │─────' │ │* │ ┌───┬────────┴───────────┘ │- * │ │ │ │* │ │ ▼ │* │ ┌──────────────┐ ┌──────────────┐ │- * │ │ │ │ │ │* │ │ │◀─────────┤ │ │* │ │ Mirror │ │ LoroDoc │ │* │ │ ├─────────▶│ │ │- * │ │ │ │ │ │* │ └──────────────┘ └──────────────┘ │- * │ │- * │ │* └───────────────────────────────────────────────────┘*/-export type LoroManager<S extends GenericRootSchema = GenericRootSchema> = {- /**- * Accessor to the inner LoroDoc- * Only use this if you know what you're doing- * Most operations should be done through the manager directly- *- * @returns The LoroDoc- **/- getDoc: Accessor<LoroDoc>;-- /**- * Accessor to the inner Mirror- * Only use this if you know what you're doing- * Most operations should be done through the manager directly- *- * @returns The Mirror- **/- getMirror: Accessor<Mirror<S> | undefined>;-- /** The current schema of the manager */- schema: S;-+export class LoroManager<S extends GenericRootSchema = GenericRootSchema>+ implements SyncEngineManager<S>+{+ /** The current schema of the manager. */+ readonly schema: S;++ private _initialized = false;+ private _doc: LoroDoc = createLoroDoc();+ private _mirror?: Mirror<S>;+ private _state?: StateUpdate<S>;++ private mirrorUnsub?: () => void;+ private readonly options: LoroManagerOptions;++ private readonly stateListeners = new Set<(u: StateUpdate<S>) => void>();+ private readonly initListeners = new Set<(v: boolean) => void>();++ constructor(schema: S, options: LoroManagerOptions) {+ this.schema = schema;+ this.options = options;+ }++ /** The inner LoroDoc. Only touch this if you know what you're doing. */+ get doc(): LoroDoc {+ return this._doc;+ }+ /** The inner Mirror, once initialized. */+ get mirror(): Mirror<S> | undefined {+ return this._mirror;+ }/** The current mirrored state of the loro doc** ┌─────────────┐@@ -107,206 +148,78 @@ export type LoroManager<S extends GenericRootSchema = GenericRootSchema> = {* │ LoroDoc │ │ State │* └───────────┘ └───────────┘* */- state: Accessor<StateUpdate<S> | undefined>;-- /* Signal containing the errors */- error: Accessor<LoroManagerError[]>;-- /** Signal containing the initialized state of the manager */- isInitialized: Accessor<boolean>;-- /** Syncs the infered state from the schema to the loro doc via the Mirror- *- *- * ┌─────────────┐- * │ Local State │- * │ Update │- * └─────────────┘- * │- * │- * ▼- * ┌──────────┐ ┌ ─ ─ ─ ─ ┐- * │ Mirror │─────▶ Diff- * └──────────┘ └ ─ ─ ─ ─ ┘- * │- * │- * ▼- * ┌───────────┐- * │ LoroDoc │- * └───────────┘- *- * @param state - The state to sync to loro- * @returns result - The error if syncing to loro failed- *- * */- syncToLoro(- state: InferType<S>- ): Promise<Result<void, ResultError<LoroManagerError>[]>>;-- /** Retrieve all loro container ids */- getAllContainerIds(): Result<ContainerID[], ResultError<LoroManagerError>[]>;-- /** Retrieve a LoroUpdate with all relavent events/data since the given version vector- *- * @param lastVersionVector - The last version vector to sync from- * @returns result - The update if it exists, or an error if it doesn't- * */- getUpdateSince(- lastVersionVector: VersionVector- ): Result<Uint8Array | undefined, ResultError<LoroManagerError>[]>;-- /** Initializes the manager from a snapshot- *- * If this is successful, it will set isInitialized to true- *- * @param snapshot - The snapshot to initialize from- * @returns result - The error if initializing from the snapshot failed- * */- initializeFromSnapshot(- snapshot: LoroRawUpdate- ): Promise<Result<void, ResultError<LoroManagerError>[]>>;-- /** Imports a single loro update- *- * @param update - The update to import- * @returns result - The error if importing the update failed- * */- importUpdate(- update: LoroRawUpdate- ): Result<boolean, ResultError<LoroManagerError>[]>;-- /** Imports multiple loro updates at once- *- * @param updates - The updates to import- * @returns result - The error if importing the updates failed- * */- importBatchUpdates(- updates: LoroRawUpdate[]- ): Result<boolean, ResultError<LoroManagerError>[]>;-- /** Resets the manager to a new state- *- * @param snapshot - The snapshot to reset to- * @returns result - The error if resetting the manager failed- * */- reset(- snapshot: LoroRawUpdate- ): Promise<Result<void, ResultError<LoroManagerError>[]>>;-- /** Returns the current version of the manager */- getVersion(): VersionVector;-- /** Returns the current state of the manager */- getPeerId(): bigint;-- getPeerIdStr(): PeerID;-- /** Returns the container with the given id if it exists */- getContainerById(- id: ContainerID- ): Result<Container | undefined, ResultError<LoroManagerError>[]>;-- /** Feed a snapshot from any source. The init state machine internally- * decides whether to apply, ignore, or chain a `requestUpdatesSince`- * follow-up against the live sync source. */- ingest(input: SnapshotIngest): Promise<void>;-- /** Returns the current cursor position for the given LoroCursor within its container */- getCursorPos(cursor: Cursor): Result<- {- update?: Cursor;- offset: number;- side: Side;- },- ResultError<LoroManagerError>[]- >;-};--export type StateUpdate<S extends GenericRootSchema> = {- state: InferType<S>;- metadata: UpdateMetadata;-};--export type SnapshotIngest =- | { kind: 'optimistic'; snapshot: RawUpdate }- | {- kind: 'local';- snapshot: RawUpdate;- /** Optional WAL update entries to replay on top of the snapshot. */- walUpdates?: RawUpdate[];- }- | { kind: 's3'; snapshot: RawUpdate }- | { kind: 'dss'; snapshot: RawUpdate };--export type LoroManagerOptions = {- /** Accessor returning the live sync source. Required because ingest converges- * to server truth via `requestUpdatesSince` after seeding. */- liveSyncSource: () => LiveSyncSource;- documentId: string;-};--/** Creates a new [LoroManager] instance- *- * @param schema - The schema to use for the manager- * @param options - Optional dependencies (e.g. live sync source for catch-up)- * @returns The new manager [LoroManager]- * */-export function createLoroManager<S extends GenericRootSchema>(- schema: S,- options: LoroManagerOptions-): LoroManager<S> {- const [initialized, setInitialized] = createSignal<boolean>(false);- const [loroDoc, setLoroDoc] = createSignal<LoroDoc>(createLoroDoc());- const [mirror, setMirror] = createSignal<Mirror<S>>();- const [error, setError] = createSignal<LoroManagerError[]>([]);- const [state, setState] = createSignal<StateUpdate<S>>();-- const { documentId } = options;- /**- * True once the doc has been seeded from any snapshot.- * We ignore any subsequent snapshots provided after.- */- let seeded = false;- const docJson = (): unknown => {- try {- return loroDoc().toJSON();- } catch (e) {- return `<toJSON failed: ${e}>`;- }- };- logSyncService({- documentId,- level: 'debug',- context: {},- message: 'manager: created',- });+ get state(): StateUpdate<S> | undefined {+ return this._state;+ }+ /** Whether the manager has been initialized from a snapshot. */+ get initialized(): boolean {+ return this._initialized;+ }+ get peerId(): bigint {+ return this._doc.peerId;+ }+ get peerIdStr(): PeerID {+ return this._doc.peerIdStr;+ }+ get version(): VersionVector {+ return this._doc.version();+ }++ /** Subscribe to state changes (both directions, with metadata). Returns an+ * unsubscribe fn. */+ onStateChange(listener: (update: StateUpdate<S>) => void): () => void {+ this.stateListeners.add(listener);+ return () => this.stateListeners.delete(listener);+ }++ /** Subscribe to `initialized` flips. Returns an unsubscribe fn. */+ onInitializedChange(listener: (initialized: boolean) => void): () => void {+ this.initListeners.add(listener);+ return () => this.initListeners.delete(listener);+ }++ private setInitialized(value: boolean) {+ if (this._initialized === value) return;+ this._initialized = value;+ for (const listener of this.initListeners) listener(value);+ }++ private emitState(update: StateUpdate<S>) {+ this._state = update;+ for (const listener of this.stateListeners) listener(update);+ }++ /** Subscribe to the given mirror, dropping any prior subscription. The mirror+ * fires for every state change (both directions, with metadata). */+ private subscribeMirror(mirror: Mirror<S>) {+ this.mirrorUnsub?.();+ this._mirror = mirror;+ this.mirrorUnsub = mirror.subscribe((update, metadata) => {+ this.emitState({ state: update, metadata });+ });+ }- /** Util for awaiting the sync of the mirror to finish */- const awaitMirrorSync = async () => {+ /** Util for awaiting the sync of the mirror to finish. */+ private async awaitMirrorSync() {await Promise.resolve();await Promise.resolve();await Promise.resolve();- };+ }- const pushError = (error: LoroManagerError) => {- setError((prev) => [...prev, error]);- };-- const importUpdate = (+ importUpdate(update: LoroRawUpdate- ): Result<boolean, ResultError<LoroManagerError>[]> => {- let importStatus;+ ): Result<boolean, ResultError<LoroManagerError>[]> {+ let importStatus: ImportStatus;try {- importStatus = loroDoc().import(update);+ importStatus = this._doc.import(update);} catch (e) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `importUpdate failed: ${e}`,});- pushError(LoroManagerError.ImportFailed);return err([{code: LoroManagerError.ImportFailed,@@ -318,42 +231,28 @@ export function createLoroManager<S extends GenericRootSchema>(const didChange = Object.keys(importStatus.success).length > 0;if (Object.keys(importStatus.pending ?? {}).length > 0) {- logSyncService({- documentId: documentId,- level: 'error',- context: {},- message: 'importUpdate: pending updates after import',- });- pushError(LoroManagerError.ImportFailed);return err([{ code: LoroManagerError.ImportFailed, message: 'Import failed' },]);}- logSyncService({- documentId: documentId,- level: 'info',- context: { misc: { loroSuccess: importStatus.success, doc: docJson() } },- message: `importUpdate: ok (didChange=${didChange})`,- });return ok(didChange);- };+ }- const importBatchUpdates = (+ importBatchUpdates(updates: LoroRawUpdate[]- ): Result<boolean, ResultError<LoroManagerError>[]> => {- let importStatus;+ ): Result<boolean, ResultError<LoroManagerError>[]> {+ let importStatus: ImportStatus;try {- importStatus = loroDoc().importBatch(updates);+ importStatus = this._doc.importBatch(updates);} catch (e) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `importBatchUpdates failed: ${e}`,});- pushError(LoroManagerError.ImportFailed);return err([{code: LoroManagerError.ImportFailed,@@ -365,57 +264,43 @@ export function createLoroManager<S extends GenericRootSchema>(const didChange = Object.keys(importStatus.success).length > 0;if (Object.keys(importStatus.pending ?? {}).length > 0) {- logSyncService({- documentId: documentId,- level: 'error',- context: {},- message: 'importBatchUpdates: pending updates after import',- });- pushError(LoroManagerError.ImportFailed);return err([{ code: LoroManagerError.ImportFailed, message: 'Import failed' },]);}- logSyncService({- documentId: documentId,- level: 'info',- context: { misc: { loroSuccess: importStatus.success } },- message: `importBatchUpdates: ok (${updates.length} updates, didChange=${didChange})`,- });return ok(didChange);- };+ }- const initializeFromSnapshot = async (+ async initializeFromSnapshot(snapshot: LoroRawUpdate- ): Promise<Result<void, ResultError<LoroManagerError>[]>> => {- const importResult = importUpdate(snapshot);+ ): Promise<Result<void, ResultError<LoroManagerError>[]>> {+ const importResult = this.importUpdate(snapshot);if (importResult.isErr()) {const [error] = importResult.error;return err([{ code: error.code, message: error.message }]);}- const mirror_ = createMirror(loroDoc(), schema);+ const mirror = createMirror(this._doc, this.schema);try {- await awaitMirrorSync();- const mirrorState = mirror_.getState();- setState(() => ({+ await this.awaitMirrorSync();+ const mirrorState = mirror.getState();+ this.emitState({state: mirrorState,metadata: {direction: SyncDirection.TO_LORO,tags: ['INITIALIZE'],},- }));+ });} catch (e) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `initializeFromSnapshot: mirror sync failed: ${e}`,});- pushError(LoroManagerError.InitializeFailed);return err([{code: LoroManagerError.InitializeFailed,@@ -424,29 +309,21 @@ export function createLoroManager<S extends GenericRootSchema>(]);}- setInitialized(true);- setMirror(mirror_);+ this.setInitialized(true);+ this.subscribeMirror(mirror);- logSyncService({- documentId: documentId,- level: 'info',- context: { misc: { doc: docJson() } },- message: 'initializeFromSnapshot: ok, manager initialized',- });return ok(undefined);- };+ }- const getUpdateSince = (+ getUpdateSince(lastVersionVector: VersionVector- ): Result<Uint8Array | undefined, ResultError<LoroManagerError>[]> => {- const mirror_ = mirror();-- if (!initialized() || !mirror_) {+ ): Result<Uint8Array | undefined, ResultError<LoroManagerError>[]> {+ if (!this._initialized || !this._mirror) {return err([{ code: LoroManagerError.NotInitialized, message: 'Not initialized' },]);}- const currentVersionVector = loroDoc().version();+ const currentVersionVector = this._doc.version();/** Comparison between the current state, and the last synced state */const vvDiff = lastVersionVector.compare(currentVersionVector);@@ -454,19 +331,19 @@ export function createLoroManager<S extends GenericRootSchema>(return ok(undefined);}- const spans = loroDoc().findIdSpansBetween(- loroDoc().vvToFrontiers(lastVersionVector),- loroDoc().vvToFrontiers(currentVersionVector)+ const spans = this._doc.findIdSpansBetween(+ this._doc.vvToFrontiers(lastVersionVector),+ this._doc.vvToFrontiers(currentVersionVector));const localSpans = spans.forward.filter(- (span) => span.peer === loroDoc().peerIdStr+ (span) => span.peer === this._doc.peerIdStr);- let update;+ let update: RawUpdate;try {- update = loroDoc().export({+ update = this._doc.export({mode: 'updates-in-range',spans: localSpans.map((span) => ({id: { peer: span.peer, counter: span.counter },@@ -475,12 +352,11 @@ export function createLoroManager<S extends GenericRootSchema>(});} catch (e) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `getUpdateSince: export failed: ${e}`,});- pushError(LoroManagerError.ExportFailed);return err([{code: LoroManagerError.ExportFailed,@@ -490,40 +366,36 @@ export function createLoroManager<S extends GenericRootSchema>(}return ok(update);- };+ }- const getAllContainerIds = (): Result<- ContainerID[],- ResultError<LoroManagerError>[]- > => {- if (!initialized() || !mirror()) {+ getAllContainerIds(): Result<ContainerID[], ResultError<LoroManagerError>[]> {+ if (!this._initialized || !this._mirror) {return err([{ code: LoroManagerError.NotInitialized, message: 'Not initialized' },]);}- return ok(mirror()!.getContainerIds());- };+ return ok(this._mirror.getContainerIds());+ }- const syncToLoro = async (+ async syncToLoro(state: InferType<S>- ): Promise<Result<void, ResultError<LoroManagerError>[]>> => {- const mirror_ = mirror();- if (!initialized() || !mirror_) {+ ): Promise<Result<void, ResultError<LoroManagerError>[]>> {+ if (!this._initialized || !this._mirror) {return err([{ code: LoroManagerError.NotInitialized, message: 'Not initialized' },]);}try {- mirror_.setState(state, {+ this._mirror.setState(state, {tags: LoroStateTag.FromManager,});- await awaitMirrorSync();+ await this.awaitMirrorSync();} catch (e) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `syncToLoro failed: ${e}`,@@ -536,35 +408,30 @@ export function createLoroManager<S extends GenericRootSchema>(]);}- logSyncService({- documentId: documentId,- level: 'debug',- context: {},- message: 'syncToLoro: ok',- });return ok(undefined);- };+ }- const reset = async (+ async reset(snapshot: LoroRawUpdate- ): Promise<Result<void, ResultError<LoroManagerError>[]>> => {- mirror()?.dispose();- loroDoc().free();+ ): Promise<Result<void, ResultError<LoroManagerError>[]>> {+ this.mirrorUnsub?.();+ this.mirrorUnsub = undefined;+ this._mirror?.dispose();+ this._doc.free();const newDoc = createLoroDoc();- setLoroDoc(newDoc);+ this._doc = newDoc;- let importStatus;+ let importStatus: ImportStatus;try {importStatus = newDoc.import(snapshot);} catch (e) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `reset: snapshot import failed: ${e}`,});- pushError(LoroManagerError.ImportFailed);return err([{code: LoroManagerError.ImportFailed,@@ -574,7 +441,6 @@ export function createLoroManager<S extends GenericRootSchema>(}if (Object.keys(importStatus.pending ?? {}).length > 0) {- pushError(LoroManagerError.ImportFailed);return err([{code: LoroManagerError.ImportFailed,@@ -583,13 +449,13 @@ export function createLoroManager<S extends GenericRootSchema>(]);}- const newMirror = createMirror(newDoc, schema);- setMirror(newMirror);+ const newMirror = createMirror(newDoc, this.schema);+ this.subscribeMirror(newMirror);- await awaitMirrorSync();+ await this.awaitMirrorSync();const state = newMirror.getState();- setState({+ this.emitState({state,metadata: {direction: SyncDirection.TO_LORO,@@ -597,53 +463,44 @@ export function createLoroManager<S extends GenericRootSchema>(},});- setInitialized(true);+ this.setInitialized(true);- logSyncService({- documentId: documentId,- level: 'info',- context: { misc: { loroSuccess: importStatus.success } },- message: 'reset: ok, manager re-initialized',- });return ok(undefined);- };+ }- const getContainerById = (+ getContainerById(id: ContainerID- ): Result<Container | undefined, ResultError<LoroManagerError>[]> => {+ ): Result<Container | undefined, ResultError<LoroManagerError>[]> {let container: Container | undefined;try {- container = loroDoc().getContainerById(id);+ container = this._doc.getContainerById(id);} catch (e) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `getContainerById failed: ${e}`,});- pushError(LoroManagerError.GetContainerByIdFailed);return err([- { code: LoroManagerError.GetContainerByIdFailed, message: e },+ { code: LoroManagerError.GetContainerByIdFailed, message: String(e) },]);}return ok(container);- };+ }- const getCursorPos = (- cursor: Cursor- ): Result<+ getCursorPos(cursor: Cursor): Result<{update?: Cursor;offset: number;side: Side;},ResultError<LoroManagerError>[]- > => {+ > {let pos: { update?: Cursor; offset: number; side: Side } | undefined;try {- pos = loroDoc().getCursorPos(cursor);+ pos = this._doc.getCursorPos(cursor);if (!pos) {return err([{@@ -654,147 +511,44 @@ export function createLoroManager<S extends GenericRootSchema>(}} catch (e) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `getCursorPos failed: ${e}`,});- pushError(LoroManagerError.GetCursorPosFailed);- return err([{ code: LoroManagerError.GetCursorPosFailed, message: e }]);+ return err([+ { code: LoroManagerError.GetCursorPosFailed, message: String(e) },+ ]);}return ok(pos);- };-- createEffect(() => {- if (mirror()) {- mirror()?.subscribe((update, metadata) => {- setState(() => ({- state: update,- metadata,- }));- });- }- });+ }- onCleanup(() => {- mirror()?.dispose();- loroDoc().free();- });+ /**+ * First snapshot received is the one ingested, all others are automatically+ * ignored. Returns whether this snapshot became the seed.+ */+ async ingest(input: SnapshotIngest): Promise<boolean> {+ if (this._initialized) return false;- /** Apply a snapshot to the loro doc — initialize on first touch, otherwise- * merge as an update. Errors are logged and surfaced via the return. */- const applySnapshot = async (- snapshot: RawUpdate,- context: string- ): Promise<boolean> => {- if (!initialized()) {- const initResult = await initializeFromSnapshot(snapshot);- if (initResult.isErr()) {- logSyncService({- documentId: documentId,- level: 'error',- context: {},- message: `applySnapshot(${context}): initializeFromSnapshot failed`,- });- return false;- }- return true;- }- const importResult = importUpdate(snapshot);- if (importResult.isErr()) {+ const initResult = await this.initializeFromSnapshot(input.snapshot);+ if (initResult.isErr()) {logSyncService({- documentId: documentId,+ documentId: this.options.documentId,level: 'error',context: {},- message: `applySnapshot(${context}): importUpdate failed`,+ message: `ingest(${input.kind}): seed failed`,});return false;}- return true;- };-- /**- * Pull every server op since our current version and merge it in.- **/- const convergeFromServer = async (): Promise<void> => {- const deltaResult = await options- .liveSyncSource()- .requestUpdatesSince(loroDoc().version());- if (deltaResult.isErr()) {- logSyncService({- documentId,- level: 'warn',- context: {},- message: 'converge: requestUpdatesSince failed',- });- return;- }- const deltaImport = importUpdate(deltaResult.value);- if (deltaImport.isErr()) {- logSyncService({- documentId,- level: 'error',- context: {},- message: 'converge: failed to apply server delta',- });- return;- }- logSyncService({- documentId,- level: 'info',- context: { misc: { doc: docJson() } },- message: 'converge: applied server delta',- });- };-- /**- * Feed a snapshot from any source. The first snapshot to arrive seeds the- * (empty) doc; later snapshots are ignored because loro can't merge a snapshot- * onto a non-empty doc. After seeding we converge to server truth via- * `requestUpdatesSince`. Offline edits carried as `walUpdates` are replayed as- * updates (those *do* merge), regardless of seed order.- */- const ingest = async (input: SnapshotIngest): Promise<void> => {- if (seeded) {- // Already seeded — a second snapshot can't merge. The only thing worth- // taking from a later `local` is its WAL updates (real ops → mergeable).- if (input.kind === 'local' && input.walUpdates?.length) {- const replayResult = importBatchUpdates(input.walUpdates);- if (replayResult.isErr()) {- logSyncService({- documentId,- level: 'error',- context: {},- message: `ingest(${input.kind}): WAL replay failed`,- });- }- } else {- logSyncService({- documentId,- level: 'debug',- context: {},- message: `ingest(${input.kind}): ignored (already seeded)`,- });- }- return;- }-- const applied = await applySnapshot(input.snapshot, input.kind);- if (!applied) return;- seeded = true;- logSyncService({- documentId,- level: 'info',- context: { misc: { doc: docJson() } },- message: `ingest: seeded from ${input.kind}`,- });+ // WAL entries are causally anchored to the local snapshot, so they're only+ // safe to replay on top of the local seed we just applied.if (input.kind === 'local' && input.walUpdates?.length) {- const replayResult = importBatchUpdates(input.walUpdates);+ const replayResult = this.importBatchUpdates(input.walUpdates);if (replayResult.isErr()) {logSyncService({- documentId,+ documentId: this.options.documentId,level: 'error',context: {},message: `ingest(${input.kind}): WAL replay failed`,@@ -802,31 +556,27 @@ export function createLoroManager<S extends GenericRootSchema>(}}- // dss is already server truth so no reason to request updates in that case- if (input.kind !== 'dss') await convergeFromServer();- };+ return true;+ }++ /** Tear down subscriptions and free the underlying doc. */+ dispose() {+ this.mirrorUnsub?.();+ this.mirrorUnsub = undefined;+ this.stateListeners.clear();+ this.initListeners.clear();+ this._mirror?.dispose();+ this._doc.free();+ }+}- return {- getDoc: loroDoc,- getMirror: mirror,- schema,- state,- error,- isInitialized: initialized,- getAllContainerIds,- getUpdateSince,- initializeFromSnapshot,- importUpdate,- importBatchUpdates,- syncToLoro,- reset,- ingest,- getVersion: () => loroDoc().version(),- getPeerId: () => loroDoc().peerId,- getPeerIdStr: () => loroDoc().peerIdStr,- getContainerById,- getCursorPos,- };+export function createLoroManager<S extends GenericRootSchema>(+ schema: S,+ options: LoroManagerOptions+): LoroManager<S> {+ const manager = new LoroManager(schema, options);+ onCleanup(() => manager.dispose());+ return manager;}export function createLoroDoc(): LoroDoc {diff --git a/js/app/packages/core/collab/testing.ts b/js/app/packages/core/collab/testing.tsindex dc5bbff77f..405baf7691 100644--- a/js/app/packages/core/collab/testing.ts+++ b/js/app/packages/core/collab/testing.ts@@ -1,6 +1,7 @@import { ok, okAsync } from 'neverthrow';import { vi } from 'vitest';-import type { LoroManager } from './manager';+import type { Chatter, ChatterMessage } from './chatter';+import type { StateUpdate, SyncEngineManager } from './manager';import type { GenericRootSchema, LoroRawUpdate, RawUpdate } from './shared';import type { SnapshotStore } from './snapshot-store';import type { SyncSourceEvent } from './source';@@ -97,6 +98,36 @@ export function makeTestWAL(return { wal, walStore: store };}+/**+ * In-memory {@link Chatter} for tests — no real BroadcastChannel, so the engine+ * tests don't depend on an ambient browser global. Records what the engine+ * broadcasts (`posted`) and lets a test inject inbound messages (`receive`).+ */+export class MockChatter implements Chatter {+ public posted: ChatterMessage[] = [];+ public closed = false;+ private handlers = new Set<(message: ChatterMessage) => void>();++ public post = vi.fn((message: ChatterMessage) => {+ this.posted.push(message);+ });++ public subscribe(handler: (message: ChatterMessage) => void) {+ this.handlers.add(handler);+ return () => this.handlers.delete(handler);+ }++ public close() {+ this.closed = true;+ this.handlers.clear();+ }++ /** Test helper: simulate a message arriving from another replica. */+ public receive(message: ChatterMessage) {+ this.handlers.forEach((h) => h(message));+ }+}+export class MockLiveSyncSource implements LiveSyncSource {private listeners = new Set<(event: SyncSourceEvent) => void>();private pushResult = true;@@ -142,35 +173,27 @@ export class MockLiveSyncSource implements LiveSyncSource {}}-export class MockLoroManager implements LoroManager<GenericRootSchema> {+export class MockLoroManager implements SyncEngineManager<GenericRootSchema> {private updateCallbacks = new Set<(update: LoroRawUpdate) => void>();+ private stateCallbacks = new Set<+ (update: StateUpdate<GenericRootSchema>) => void+ >();private _initialized: boolean;- public schema = {} as GenericRootSchema;- public state = vi.fn(() => undefined as any);- public error = vi.fn(() => []);- public isInitialized: () => boolean;- public getPeerId = vi.fn(() => BigInt(1));- public getPeerIdStr = vi.fn(() => '1' as any);+ public peerId = BigInt(1);public importUpdate = vi.fn(() => ok(true));- public importBatchUpdates = vi.fn(() => ok(true));public syncToLoro = vi.fn(async () => ok(undefined as void));public reset = vi.fn(async () => ok(undefined as void));- public initializeFromSnapshot = vi.fn(async () => ok(undefined as void));- public ingest = vi.fn(async () => {});- public getVersion = vi.fn(() => ({}) as any);- public getUpdateSince = vi.fn(() => ok(undefined as any));- public getAllContainerIds = vi.fn(() => ok([]));- public getContainerById = vi.fn(() => ok(undefined as any));- public getCursorPos = vi.fn(() => ok({ offset: 0, side: 0 as any }));- public getMirror = vi.fn(() => undefined as any);constructor(initialized = true) {this._initialized = initialized;- this.isInitialized = () => this._initialized;}- public getDoc() {+ public get initialized(): boolean {+ return this._initialized;+ }++ public get doc() {return {subscribeLocalUpdates: (cb: (update: LoroRawUpdate) => void) => {this.updateCallbacks.add(cb);@@ -181,7 +204,16 @@ export class MockLoroManager implements LoroManager<GenericRootSchema> {} as any;}+ public onStateChange(cb: (update: StateUpdate<GenericRootSchema>) => void) {+ this.stateCallbacks.add(cb);+ return () => this.stateCallbacks.delete(cb);+ }+public triggerLocalUpdate(update: LoroRawUpdate) {this.updateCallbacks.forEach((cb) => void cb(update));}++ public triggerStateChange(update: StateUpdate<GenericRootSchema>) {+ this.stateCallbacks.forEach((cb) => void cb(update));+ }}diff --git a/js/app/packages/core/collab/wal.ts b/js/app/packages/core/collab/wal.tsindex f0776ded6d..a70408d8d4 100644--- a/js/app/packages/core/collab/wal.ts+++ b/js/app/packages/core/collab/wal.ts@@ -177,6 +177,44 @@ export class BrowserWALStore<T> implements WALStore<T> {}}+export class InMemoryWALStore<T> implements WALStore<T> {+ private entries: WALEntry<T>[] = [];+ private seq = 0;++ async append(update: T): Promise<void> {+ this.entries.push({+ id: ++this.seq,+ update,+ delivered: false,+ createdAt: Date.now(),+ });+ }++ async getAll(): Promise<WALEntry<T>[]> {+ return [...this.entries];+ }++ async markDelivered(ids: number[]): Promise<void> {+ const set = new Set(ids);+ for (const e of this.entries) if (set.has(e.id)) e.delivered = true;+ }++ async pruneDelivered(): Promise<void> {+ this.entries = this.entries.filter((e) => !e.delivered);+ }++ async pruneExpired(ttlMs: number): Promise<number> {+ const cutoff = Date.now() - ttlMs;+ const before = this.entries.length;+ this.entries = this.entries.filter((e) => !hasExpired(e, cutoff));+ return before - this.entries.length;+ }++ async count(): Promise<number> {+ return this.entries.length;+ }+}+/*** Append-only queue with retry semantics. Caller-supplied `push` does the* actual transport; the syncer handles persistence, batching, and dedupe.diff --git a/js/app/packages/core/component/AI/component/tool/EditDocument.tsx b/js/app/packages/core/component/AI/component/tool/EditDocument.tsxnew file mode 100644index 0000000000..e8a280a428--- /dev/null+++ b/js/app/packages/core/component/AI/component/tool/EditDocument.tsx@@ -0,0 +1,25 @@+import { ItemPreview } from '@core/component/ItemPreview';+import PencilSimple from '@phosphor-icons/core/regular/pencil-simple.svg';+import { Suspense } from 'solid-js';+import { BaseTool } from './BaseTool';+import { createToolRenderer } from './ToolRenderer';++const handler = createToolRenderer({+ name: 'EditDocument',+ render: (ctx) => (+ <BaseTool icon={PencilSimple} renderContext={ctx.renderContext} type="call">+ <div class="min-w-0 flex-1">+ Edit{' '}+ <Suspense>+ <ItemPreview+ class="inline-flex align-middle ring-0"+ id={ctx.tool.data.document_id}+ type="document"+ />+ </Suspense>+ </div>+ </BaseTool>+ ),+});++export const editDocumentHandler = handler;diff --git a/js/app/packages/core/component/AI/component/tool/handler.tsx b/js/app/packages/core/component/AI/component/tool/handler.tsxindex 4c8343c52c..edb105b34f 100644--- a/js/app/packages/core/component/AI/component/tool/handler.tsx+++ b/js/app/packages/core/component/AI/component/tool/handler.tsx@@ -7,6 +7,7 @@ import { Dynamic } from 'solid-js/web';import { bashCodeExecutionHandler } from './BashCodeExecution';import { createDocumentHandler } from './CreateDocument';import { displayResultsHandler } from './DisplayResults';+import { editDocumentHandler } from './EditDocument';import { getThreadHandler } from './GetThread';import { listCallRecordsHandler } from './ListCallRecords';import { listEntitiesHandler } from './ListEntities';@@ -67,6 +68,7 @@ const toolHandlers: ToolHandlerMap<RenderContext> = {DisplayResults: displayResultsHandler,ContentSearch: contentSearchHandler,CreateDocument: createDocumentHandler,+ EditDocument: editDocumentHandler,GetThread: getThreadHandler,NameSearch: nameSearchHandler,ReadCallRecord: readCallRecordHandler,diff --git a/js/app/packages/core/component/LexicalMarkdown/collaboration/remote-cursor.tsx b/js/app/packages/core/component/LexicalMarkdown/collaboration/remote-cursor.tsxindex 5c5ab2c9e5..b7e1c1f1a1 100644--- a/js/app/packages/core/component/LexicalMarkdown/collaboration/remote-cursor.tsx+++ b/js/app/packages/core/component/LexicalMarkdown/collaboration/remote-cursor.tsx@@ -263,6 +263,8 @@ type RemoteCursorsOverlayProps = {};function RemoteCursorsOverlay(props: RemoteCursorsOverlayProps) {+ const tagHideAt = new Map<string, number>();+createEffect(on(() => props.awareness.remote(),@@ -270,10 +272,21 @@ function RemoteCursorsOverlay(props: RemoteCursorsOverlayProps) {));+ // Purge entries for cursors that have left so the tag resets on next appearance.+ createEffect(() => {+ const active = new Set(+ props.remoteCursors().map((c) => c.user ?? 'Anonymous')+ );+ for (const user of [...tagHideAt.keys()]) {+ if (!active.has(user)) tagHideAt.delete(user);+ }+ });+return (<Portal mount={props.anchorElem}><For each={props.remoteCursors()}>{(cursor) => {+ const userKey = cursor.user ?? 'Anonymous';const userName = cursor.user? idToEmail(cursor.user).split('@')[0]: 'Anonymous';@@ -286,10 +299,19 @@ function RemoteCursorsOverlay(props: RemoteCursorsOverlayProps) {const userTagHeight = 20;- const [shouldShow, setShouldShow] = createSignal<boolean>(true);- setTimeout(() => {- setShouldShow(false);- }, 1500);+ // HACK: for now we ensure AI names stick around by ensuring they conform to the name pattern. We prob want to take a param instead.+ const isAI = userKey.endsWith('(AI)');++ if (!tagHideAt.has(userKey)) {+ tagHideAt.set(userKey, Date.now() + 1500);+ }+ const remaining = tagHideAt.get(userKey)! - Date.now();+ const [shouldShow, setShouldShow] = createSignal<boolean>(+ isAI || remaining > 0+ );+ if (!isAI && remaining > 0) {+ setTimeout(() => setShouldShow(false), remaining);+ }return (<>diff --git a/js/app/packages/core/component/LexicalMarkdown/component/debug/LexicalStateDebugger.tsx b/js/app/packages/core/component/LexicalMarkdown/component/debug/LexicalStateDebugger.tsxindex 6e6cb809d2..ea0987ca84 100644--- a/js/app/packages/core/component/LexicalMarkdown/component/debug/LexicalStateDebugger.tsx+++ b/js/app/packages/core/component/LexicalMarkdown/component/debug/LexicalStateDebugger.tsx@@ -18,12 +18,13 @@ import {$isRangeSelection,type EditorState,ElementNode,+ type LexicalEditor,type LexicalNode,RootNode,type TextFormatType,TextNode,} from 'lexical';-import { createMemo, For, Match, Show, Switch } from 'solid-js';+import { createMemo, createSignal, For, Match, Show, Switch } from 'solid-js';import { nodeType } from '../../plugins';import { markNodeKeysToIDs } from '../../plugins/comments/commentPlugin';@@ -309,7 +310,10 @@ function Selection(props: { selection?: SelectionRenderable; class?: string }) {);}-export function LexicalStateDebugger(props: { state: EditorState }) {+export function LexicalStateDebugger(props: {+ state: EditorState;+ editor: LexicalEditor;+}) {const state = createMemo(() => {let nodes = EditorStateToNodeList(props.state);const selection = EditorStateToSelection(props.state);@@ -317,108 +321,152 @@ export function LexicalStateDebugger(props: { state: EditorState }) {return { nodeList: selectableNodes, selection: selection };});+ const [lexicalJson, setLexicalJson] = createSignal('');+ const jsonState = createMemo(() =>+ JSON.stringify(props.state.toJSON(), null, 2)+ );return (- <div class="font-mono text-ink bg-surface text-xs size-full min-h-0 flex flex-col overflow-hidden">- <Layer depth={0}>- <div class="bg-surface m-2 min-h-0 flex-1 overflow-y-auto select-children rounded-md p-1 border border-edge">- <div class="px-1">- <For each={state().nodeList}>- {(node) => {- return (- <div- style={{ 'margin-left': `${node.depth * 24}px` }}- class="flex relative"- >- <span>- {' '}- {node.depth > 0 ? '↳' : ''}[{node.key}]- </span>- <Show when={node.id}>- <span class="px-1 text-ink-extra-muted">{node.id}</span>- </Show>- <Show when={node.peerId}>- <span class="bg-accent-30/15 border border-accent-30/30 text-accent-30 mx-0.5">- Peer ID: {node.peerId}- </span>- </Show>- <Show- when={node.sharedPeers && node.sharedPeers.length > 0}+ <div class="font-mono text-ink bg-surface text-xs size-full min-h-0 flex flex-row overflow-hidden">+ <div class="flex flex-col flex-1 min-w-0 overflow-hidden">+ <Layer depth={0}>+ <div class="bg-surface m-2 min-h-0 flex-1 overflow-y-auto select-children rounded-md p-1 border border-edge">+ <div class="px-1">+ <For each={state().nodeList}>+ {(node) => {+ return (+ <div+ style={{ 'margin-left': `${node.depth * 24}px` }}+ class="flex relative">- <span class="bg-accent-30/15 border border-accent-30/30 text-accent-30 mx-0.5">- <For each={node.sharedPeers}>- {(id) => <span>{id}</span>}- </For>+ <span>+ {' '}+ {node.depth > 0 ? '↳' : ''}[{node.key}]</span>- </Show>- <span- class={cn('inline-block px-1 mx-1', colors[node.type])}- >- {node.type}- </span>- <For each={node.styles}>- {(style) => (- <span class="bg-accent-60/15 border border-accent-60/30 text-accent-60 mx-0.5">- {style}+ <Show when={node.id}>+ <span class="px-1 text-ink-extra-muted">{node.id}</span>+ </Show>+ <Show when={node.peerId}>+ <span class="bg-accent-30/15 border border-accent-30/30 text-accent-30 mx-0.5">+ Peer ID: {node.peerId}</span>- )}- </For>- <span class="inline-block">{node.text}</span>- <SelectionIndicator- anchor={node.isAnchor}- focus={node.isFocus}- selected={node.selected}- class=""- />- <Show- when={- node.type === 'mark' || node.type === 'comment-mark'- }- >- <span class="bg-accent-90/15 border border-accent-90/30 text-accent-90 mx-0.5">- {markNodeKeysToIDs.get(node.key)?.join(', ') ?? ''}+ </Show>+ <Show+ when={node.sharedPeers && node.sharedPeers.length > 0}+ >+ <span class="bg-accent-30/15 border border-accent-30/30 text-accent-30 mx-0.5">+ <For each={node.sharedPeers}>+ {(id) => <span>{id}</span>}+ </For>+ </span>+ </Show>+ <span+ class={cn('inline-block px-1 mx-1', colors[node.type])}+ >+ {node.type}</span>- <Show when={node.type === 'comment-mark'}>- {(_) => {- const commentNode = () =>- $getNodeByKey(- node.key,- props.state- ) as CommentNode | null;- return (- <Show when={commentNode()}>- {(commentNode) => (- <span- class="bg-accent-90/15 border border-accent-90/30 text-accent-90 mx-0.5"- classList={{- 'bg-accent-30/30':- commentNode().getIsDraft(),- 'bg-accent-30/5':- !commentNode().getIsDraft(),- }}- >- {commentNode().getThreadId() ?? 'NO # ID'}- </span>- )}- </Show>- );- }}+ <For each={node.styles}>+ {(style) => (+ <span class="bg-accent-60/15 border border-accent-60/30 text-accent-60 mx-0.5">+ {style}+ </span>+ )}+ </For>+ <span class="inline-block">{node.text}</span>+ <SelectionIndicator+ anchor={node.isAnchor}+ focus={node.isFocus}+ selected={node.selected}+ class=""+ />+ <Show+ when={+ node.type === 'mark' || node.type === 'comment-mark'+ }+ >+ <span class="bg-accent-90/15 border border-accent-90/30 text-accent-90 mx-0.5">+ {markNodeKeysToIDs.get(node.key)?.join(', ') ?? ''}+ </span>+ <Show when={node.type === 'comment-mark'}>+ {(_) => {+ const commentNode = () =>+ $getNodeByKey(+ node.key,+ props.state+ ) as CommentNode | null;+ return (+ <Show when={commentNode()}>+ {(commentNode) => (+ <span+ class="bg-accent-90/15 border border-accent-90/30 text-accent-90 mx-0.5"+ classList={{+ 'bg-accent-30/30':+ commentNode().getIsDraft(),+ 'bg-accent-30/5':+ !commentNode().getIsDraft(),+ }}+ >+ {commentNode().getThreadId() ?? 'NO # ID'}+ </span>+ )}+ </Show>+ );+ }}+ </Show></Show>- </Show>- </div>- );- }}- </For>+ </div>+ );+ }}+ </For>+ </div>+ </div>+ </Layer>+ <Layer depth={0}>+ <div class="bg-surface m-2 shrink-0">+ <Selection+ selection={state().selection}+ class="bg-surface p-1 border border-edge rounded-md"+ /></div>+ </Layer>+ </div>+ <div class="flex flex-col w-1/2 border-l border-edge overflow-hidden">+ <div class="flex items-center justify-between px-2 py-1 border-b border-edge shrink-0">+ <span class="text-ink-extra-muted">JSON state</span>+ <button+ type="button"+ class="border border-edge rounded-sm px-2 py-0.5 hover:bg-edge active:brightness-75"+ onClick={() => navigator.clipboard.writeText(jsonState())}+ >+ Copy+ </button></div>- </Layer>- <Layer depth={0}>- <div class="bg-surface m-2 shrink-0">- <Selection- selection={state().selection}- class="bg-surface p-1 border border-edge rounded-md"+ <pre class="flex-1 overflow-auto p-2 text-xs select-all">+ {jsonState()}+ </pre>+ <div class="flex flex-col space-y-1 border-t border-edge p-2 shrink-0">+ <span class="text-ink-extra-muted">Import JSON</span>+ <textarea+ class="bg-surface border border-edge rounded-sm p-1 text-xs h-16 resize-none"+ placeholder='{"root":{"children":[...]}}'+ value={lexicalJson()}+ onInput={(e) => setLexicalJson(e.currentTarget.value)}/>+ <button+ type="button"+ class="border border-edge rounded-sm px-2 py-0.5 text-xs hover:bg-edge"+ onClick={() => {+ try {+ const state = props.editor.parseEditorState(lexicalJson());+ props.editor.setEditorState(state);+ } catch (e) {+ console.error('Failed to parse editor state JSON:', e);+ }+ }}+ >+ Import+ </button></div>- </Layer>+ </div></div>);}diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.tsindex 940fa39828..9ef702d406 100644--- a/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/pluginManager.ts@@ -34,7 +34,7 @@ export function createPluginManager(editor: LexicalEditor, type: EditorType) {history(timeGap = 400, loroManager?: LoroManager) {if (type === 'markdown-sync' && loroManager) {cleanupFunctions.push(- registerLoroHistory(editor, loroManager.getDoc(), timeGap)+ registerLoroHistory(editor, loroManager.doc, timeGap));} else {cleanupFunctions.push(diff --git a/js/app/packages/service-clients/service-cognition/generated/schemas/aiFeature.ts b/js/app/packages/service-clients/service-cognition/generated/schemas/aiFeature.tsindex 0ba11787e5..3be82b6bd9 100644--- a/js/app/packages/service-clients/service-cognition/generated/schemas/aiFeature.ts+++ b/js/app/packages/service-clients/service-cognition/generated/schemas/aiFeature.ts@@ -21,4 +21,5 @@ export const AiFeature = {call_summary: 'call_summary',channel_bot: 'channel_bot',ai_projection: 'ai_projection',+ ai_editing: 'ai_editing',} as const;diff --git a/js/app/packages/service-clients/service-cognition/generated/tools/schemas.ts b/js/app/packages/service-clients/service-cognition/generated/tools/schemas.tsindex 5f9835181a..38cf80d0a9 100644--- a/js/app/packages/service-clients/service-cognition/generated/tools/schemas.ts+++ b/js/app/packages/service-clients/service-cognition/generated/tools/schemas.ts@@ -497,6 +497,16 @@ export const DisplayResults = z.object({ view: z.any() });export const DisplayResultsResponse = z.object({ message: z.string() });+export const EditDocument = z.object({+ document_id: z.string(),+ instructions: z.string(),+});++export const EditDocumentResponse = z.object({+ clarification: z.union([z.string(), z.null()]).optional(),+ summary: z.string(),+});+export const GetEntityProperties = z.object({entity_id: z.string(),entity_type: z.enum([diff --git a/js/app/packages/service-clients/service-cognition/generated/tools/tool.ts b/js/app/packages/service-clients/service-cognition/generated/tools/tool.tsindex d95ac90742..ad7fdff409 100644--- a/js/app/packages/service-clients/service-cognition/generated/tools/tool.ts+++ b/js/app/packages/service-clients/service-cognition/generated/tools/tool.ts@@ -24,6 +24,10 @@ type ToolParserMap = {call: types.DisplayResults;response: types.DisplayResultsResponse;};+ EditDocument: {+ call: types.EditDocument;+ response: types.EditDocumentResponse;+ };GetEntityProperties: {call: types.GetEntityProperties;response: types.GetEntityPropertiesResponse;@@ -128,6 +132,10 @@ const toolParserMap = {call: schemas.DisplayResults,response: schemas.DisplayResultsResponse,},+ EditDocument: {+ call: schemas.EditDocument,+ response: schemas.EditDocumentResponse,+ },GetEntityProperties: {call: schemas.GetEntityProperties,response: schemas.GetEntityPropertiesResponse,@@ -255,6 +263,10 @@ type ToolDataMap = {call: types.DisplayResults;response: types.DisplayResultsResponse;};+ EditDocument: {+ call: types.EditDocument;+ response: types.EditDocumentResponse;+ };GetEntityProperties: {call: types.GetEntityProperties;response: types.GetEntityPropertiesResponse;diff --git a/js/app/packages/service-clients/service-cognition/generated/tools/types.ts b/js/app/packages/service-clients/service-cognition/generated/tools/types.tsindex a3cefd76e4..78adac1293 100644--- a/js/app/packages/service-clients/service-cognition/generated/tools/types.ts+++ b/js/app/packages/service-clients/service-cognition/generated/tools/types.ts@@ -1068,6 +1068,30 @@ export interface DocumentSearchResult {*/score?: number | null;}+/**+ * Apply AI-driven edits to a Macro document in place -- rewriting, inserting, formatting, or restructuring. If the response contains a `clarification` field, invoke again with the requested info appended to `instructions`. To insert mention(s), include each person's userId and email. To insert document-card(s), include each document's documentId and documentName.+ */+export interface EditDocument {+ /**+ * The ID of the document to edit.+ */+ document_id: string;+ /**+ * Natural language instructions. For mention(s), include userId and email per person. For document-card(s), include documentId and documentName per document. You may need to look these up.+ */+ instructions: string;+}+export interface EditDocumentResponse {+ /**+ * If present, invoke this tool again with this information appended to `instructions`.+ */+ clarification?: string | null;+ /**+ * A short outcome for the model -- whether the edit was applied or+ * interrupted -- never the underlying list of edit operations.+ */+ summary: string;+}/*** A recipient for an email.*/diff --git a/js/app/packages/service-clients/service-cognition/openapi.json b/js/app/packages/service-clients/service-cognition/openapi.jsonindex cf0cf0c108..32e6ebbcf3 100644--- a/js/app/packages/service-clients/service-cognition/openapi.json+++ b/js/app/packages/service-clients/service-cognition/openapi.json@@ -1581,7 +1581,8 @@"chat_rename","call_summary","channel_bot",- "ai_projection"+ "ai_projection",+ "ai_editing"]},"AssistantMessagePart": {diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/documentPermissionsToken.ts b/js/app/packages/service-clients/service-storage/generated/schemas/documentPermissionsToken.tsindex 2d864395fc..592189a9cc 100644--- a/js/app/packages/service-clients/service-storage/generated/schemas/documentPermissionsToken.ts+++ b/js/app/packages/service-clients/service-storage/generated/schemas/documentPermissionsToken.ts@@ -19,6 +19,5 @@ export interface DocumentPermissionsToken {exp: number;/** The issuer of the token */iss: string;- /** The users id if present */user_id?: DocumentPermissionsTokenUserId;}diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/documentPermissionsTokenUserId.ts b/js/app/packages/service-clients/service-storage/generated/schemas/documentPermissionsTokenUserId.tsindex 108decab0b..318f7a3f21 100644--- a/js/app/packages/service-clients/service-storage/generated/schemas/documentPermissionsTokenUserId.ts+++ b/js/app/packages/service-clients/service-storage/generated/schemas/documentPermissionsTokenUserId.ts@@ -4,8 +4,6 @@* document_storage_service* OpenAPI spec version: 0.1.0*/+import type { MacroUserIdStr } from './macroUserIdStr';-/**- * The users id if present- */-export type DocumentPermissionsTokenUserId = string | null;+export type DocumentPermissionsTokenUserId = null | MacroUserIdStr;diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/index.ts b/js/app/packages/service-clients/service-storage/generated/schemas/index.tsindex 59593d6854..1bb6a959c9 100644--- a/js/app/packages/service-clients/service-storage/generated/schemas/index.ts+++ b/js/app/packages/service-clients/service-storage/generated/schemas/index.ts@@ -637,6 +637,7 @@ export * from './locationResponseV3OneOfFiveType';export * from './locationResponseV3OneOfThree';export * from './locationResponseV3OneOfThreeType';export * from './locationResponseV3OneOfType';+export * from './macroUserIdStr';export * from './mentions';export * from './newChannelAttachment';export * from './newChannelAttachmentHeight';diff --git a/js/app/packages/service-clients/service-storage/generated/schemas/macroUserIdStr.ts b/js/app/packages/service-clients/service-storage/generated/schemas/macroUserIdStr.tsnew file mode 100644index 0000000000..c4bba29bc4--- /dev/null+++ b/js/app/packages/service-clients/service-storage/generated/schemas/macroUserIdStr.ts@@ -0,0 +1,8 @@+/**+ * Generated by orval v7.21.0 🍺+ * Do not edit manually.+ * document_storage_service+ * OpenAPI spec version: 0.1.0+ */++export type MacroUserIdStr = string;diff --git a/js/app/packages/service-clients/service-storage/generated/zod.ts b/js/app/packages/service-clients/service-storage/generated/zod.tsindex fcce18e4cd..c08e2f19e8 100644--- a/js/app/packages/service-clients/service-storage/generated/zod.ts+++ b/js/app/packages/service-clients/service-storage/generated/zod.ts@@ -4215,7 +4215,7 @@ export const validateDocumentPermissionsTokenResponse = zod.object({.min(validateDocumentPermissionsTokenResponseExpMin).describe('The expiration time of the token'),iss: zod.string().describe('The issuer of the token'),- user_id: zod.string().nullish().describe('The users id if present'),+ user_id: zod.union([zod.null(), zod.string()]).optional(),});/**diff --git a/js/app/packages/service-clients/service-storage/openapi.json b/js/app/packages/service-clients/service-storage/openapi.jsonindex 315c6e5b88..940b5608c6 100644--- a/js/app/packages/service-clients/service-storage/openapi.json+++ b/js/app/packages/service-clients/service-storage/openapi.json@@ -13154,8 +13154,15 @@"description": "The issuer of the token"},"user_id": {- "type": ["string", "null"],- "description": "The users id if present"+ "oneOf": [+ {+ "type": "null"+ },+ {+ "$ref": "#/components/schemas/MacroUserIdStr",+ "description": "The users id if present"+ }+ ]}}},@@ -15750,6 +15757,9 @@],"description": "Location response with content lifecycle metadata."},+ "MacroUserIdStr": {+ "type": "string"+ },"Mentions": {"type": "object","required": ["users", "mentionId"],diff --git a/js/app/packages/service-clients/service-sync/source.ts b/js/app/packages/service-clients/service-sync/source.tsdeleted file mode 100644index 991035a624..0000000000--- a/js/app/packages/service-clients/service-sync/source.ts+++ /dev/null@@ -1,384 +0,0 @@-import { logSyncService } from '@core/collab/logger';-import type { RawUpdate } from '@core/collab/shared';-import {- type InitialSync,- type LiveSyncSource,- SyncError,- type SyncSourceEvent,- SyncSourceStatus,- type TimeoutError,-} from '@core/collab/source';-import { SYNC_SERVICE_HOSTS } from '@core/constant/servers';-import { arrayEquals } from '@core/util/compareUtils';--import { storageServiceClient } from '@service-storage/client';-import { createEventBus } from '@solid-primitives/event-bus';-import { raceTimeout } from '@solid-primitives/promise';-import {- ArrayQueue,- BebopSerializer,- ExponentialBackoff,- type UrlResolver,- untilMessage,- WebsocketBuilder,- WebsocketConnectionState,-} from '@websocket';-import {- createReconnectEffect,- createSocketEffect,-} from '@websocket/solid/socket-effect';-import { createWebsocketStateSignal } from '@websocket/solid/state-signal';-import type { VersionVector } from 'loro-crdt';-import { type Result, ResultAsync } from 'neverthrow';-import {- FromPeer,- FromRemote,- type RemoteSnapshot,- type RemoteUpdateSince,-} from './generated/schema';--const SYNC_SERVICE_WS_URL = `${SYNC_SERVICE_HOSTS['ws']}/document`;--function mapToSyncStatus(status: WebsocketConnectionState): SyncSourceStatus {- switch (status) {- case WebsocketConnectionState.Connecting:- return SyncSourceStatus.Connecting;- case WebsocketConnectionState.Open:- return SyncSourceStatus.Connected;- case WebsocketConnectionState.Closed:- case WebsocketConnectionState.Closing:- case WebsocketConnectionState.Reconnecting:- return SyncSourceStatus.Disconnected;- }-}--function createSyncServiceSocket(documentId: string, initialToken: string) {- const connectUrl = (token: string) =>- `${SYNC_SERVICE_WS_URL}/${documentId}/connect?token=${token}`;- let initialUrl: string | undefined = connectUrl(initialToken);- let fallbackUrl = initialUrl;-- /**- * Uses the already-fetched token for the initial connect, then refetches on reconnect.- */- const getUrl: UrlResolver = async () => {- if (initialUrl) {- const url = initialUrl;- initialUrl = undefined;- return url;- }-- const response =- await storageServiceClient.permissionsTokens.createPermissionToken({- document_id: documentId,- });-- if (response.isErr()) {- console.error('failed to fetch permission token', response);- return fallbackUrl;- }-- const refreshedUrl = connectUrl(response.value.token);- fallbackUrl = refreshedUrl;- return refreshedUrl;- };-- return (- new WebsocketBuilder(getUrl)- .withSerializer(new BebopSerializer(FromPeer, FromRemote))- // Capped exponential backoff. The scheduler calls next() before the first- // retry, so the delays are 250*2^1 = 500ms doubling to a 250*2^5 = 8s- // cap; 20 retries ≈ 2 minutes of automatic attempts, after which- // something is very wrong and we stop hammering. A given-up socket is- // revived by 'online' / 'visibilitychange' signals or by the user editing- // (see pushUpdate) — unlike before, exhausting the budget no longer- // strands the socket permanently.- .withBackoff(new ExponentialBackoff(250, 5))- .withMaxRetries(20)- // Queue messages sent while disconnected; they are flushed in order once- // the connection is re-established, so edits made during a reconnect- // aren't dropped. Unbounded on purpose: dropping the oldest updates would- // leave dependency gaps the server can never fill, and CRDT updates are- // tiny relative to a session's lifetime.- .withBuffer(new ArrayQueue())- .withHeartbeat({- interval: 10_000,- timeout: 5_000,- pingMessage: 'ping',- pongMessage: 'pong',- maxMissedHeartbeats: 2,- autoStart: false, // Start heartbeat manually after initial sync completes- })- .build()- );-}--const TIMEOUTS = {- INITIAL_SYNC: 10_000,- // The server only acks after durably storing the update, and its internal- // budget for a storage operation is 4.5s — so a busy-but-healthy server can- // legitimately ack late. Waiting 5s keeps "server is busy" from being- // misread as "update was lost".- ACK: 5_000,- SNAPSHOT: 10_000,- REQUEST_UPDATES_SINCE: 10_000,-} as const;--export const createSyncServiceSource = (- documentId: string,- token: string-): {- source: LiveSyncSource;- doInitialSync: () => ResultAsync<InitialSync, TimeoutError>;-} => {- const ws = createSyncServiceSocket(documentId, token);-- // Register the initial-sync listener eagerly so it's in place before the- // server's RemoteInitialSync message arrives (~50ms after WS opens).- // `doInitialSync()` just returns the cached promise; if it's called late,- // it still resolves because the listener captured the message.- logSyncService({- documentId,- level: 'debug',- context: {},- message: 'sync source: created, waiting for WS open',- });-- let initialSyncReceived = false;- const initialSyncPromise = ResultAsync.fromPromise(- raceTimeout(- untilMessage(ws, (message) => message.isRemoteInitialSync()),- TIMEOUTS.INITIAL_SYNC,- true- ),- () => SyncError.timeout(TIMEOUTS.INITIAL_SYNC)- ).map((message) => {- initialSyncReceived = true;- logSyncService({- documentId,- level: 'debug',- context: {},- message: 'sync source: initial sync received',- });- // Start heartbeat only after initial sync completes successfully- // This prevents the heartbeat from closing the connection during slow initial syncs- ws.startHeartbeat();- return message.value as InitialSync;- });-- const doInitialSync = () => initialSyncPromise;-- const eventBus = createEventBus<SyncSourceEvent>();-- const status = createWebsocketStateSignal(ws);-- const syncEventForMessage = (message: FromRemote): SyncSourceEvent | null => {- if (message.isRemoteUpdate()) {- return {- type: 'update',- update: message.value.update,- };- } else if (message.isRemoteAwareness()) {- return {- type: 'awareness',- awareness: message.value.awareness,- };- } else if (message.isRemoteSnapshot()) {- return {- type: 'incremental_snapshot',- snapshot: message.value.snapshot,- };- }-- return null;- };-- createSocketEffect(ws, async (message) => {- const syncEvent = syncEventForMessage(message);- if (syncEvent) {- eventBus.emit(syncEvent);- }- });-- createReconnectEffect(ws, async () => {- logSyncService({- documentId,- level: 'debug',- context: {},- message: 'sync source: WS reconnected, waiting for initial sync',- });- // Always restart heartbeat after reconnect, regardless of sync success/failure.- // This ensures the connection is monitored even if sync fails.- // startHeartbeat() is safe to call - it will no-op if connection closed.- ws.startHeartbeat();-- const reconnectSyncResult: Result<InitialSync, TimeoutError> =- await ResultAsync.fromPromise(- raceTimeout(- untilMessage(ws, (message) => message.isRemoteInitialSync()),- TIMEOUTS.INITIAL_SYNC,- true- ),- () => SyncError.timeout(TIMEOUTS.INITIAL_SYNC)- ).map((message) => message.value as InitialSync);-- if (reconnectSyncResult.isErr()) {- logSyncService({- documentId,- level: 'warn',- context: { misc: { error: reconnectSyncResult.error } },- message: 'sync source: reconnect initial sync timed out',- });- // Heartbeat is already running from above, so connection remains monitored- // even though sync failed. The connection will eventually timeout and retry.- return;- }-- logSyncService({- documentId,- level: 'debug',- context: {},- message:- 'sync source: reconnect initial sync received, emitting reconnect event',- });- eventBus.emit({- type: 'reconnect',- snapshot: reconnectSyncResult.value.snapshot,- awareness: reconnectSyncResult.value.awareness,- });- });-- const requestSnapshot = (): ResultAsync<RawUpdate, TimeoutError> => {- const message = FromPeer.fromPeerRequestSnapshot({});- ws.send(message);-- return ResultAsync.fromPromise(- raceTimeout(- untilMessage(ws, (message) => message.isRemoteSnapshot()),- TIMEOUTS.SNAPSHOT,- true- ),- () => SyncError.timeout(TIMEOUTS.SNAPSHOT)- ).map((message) => (message.value as RemoteSnapshot).snapshot);- };-- const registerPeerId = (peerId: bigint) => {- const message = FromPeer.fromPeerRegisterId({ peerid: peerId });- ws.send(message);- };-- const pushUpdate = async (updates: RawUpdate[]): Promise<boolean> => {- if (!initialSyncReceived) return false;- if (updates.length === 0) return true;-- const id = crypto.randomUUID();-- const ack = (async () => {- try {- await raceTimeout(- untilMessage(- ws,- (msg) => msg.isRemoteUpdateAck() && msg.value.id === id- ),- TIMEOUTS.ACK,- true- );- return true;- } catch {- return false;- }- })();-- ws.send(FromPeer.fromPeerUpdate({ updates, id }));-- return ack;- };-- const pushAwareness = (awareness: RawUpdate) => {- // Awareness is ephemeral (cursor positions with a short server-side TTL),- // so never let it sit in the send buffer: replaying a backlog of stale- // cursor moves after a reconnect would flood the room for no benefit.- // Drop it unless the socket is open right now — the next local cursor- // move re-publishes fresh state anyway.- if (ws.underlyingWebsocket?.readyState !== WebSocket.OPEN) {- return;- }- const message = FromPeer.fromPeerAwareness({ awareness });- ws.send(message);- };-- const requestUpdatesSince = (- vv: VersionVector- ): ResultAsync<RawUpdate, TimeoutError> => {- const encodedVv = vv.encode();- const message = FromPeer.fromPeerRequestSince({ vv: encodedVv });- ws.send(message);-- return ResultAsync.fromPromise(- raceTimeout(- untilMessage(ws, (message) => {- return (- message.isRemoteUpdateSince() &&- arrayEquals(message.value.vv, encodedVv)- );- }),- TIMEOUTS.REQUEST_UPDATES_SINCE,- true- ),- () => SyncError.timeout(TIMEOUTS.REQUEST_UPDATES_SINCE)- ).map((message) => (message.value as RemoteUpdateSince).update);- };-- const reconnect = () => {- // Only force a new connection when the socket is actually down. Tearing- // down an OPEN socket (e.g. on a missed ack) caused reconnect storms, and- // tearing down a CONNECTING one aborts an attempt that may be about to- // succeed. Liveness of open sockets is owned by the heartbeat.- ws.reconnectIfDisconnected();- };-- // When the browser regains connectivity or the tab becomes visible again,- // kick the connection immediately instead of waiting out the current- // backoff timer (which may also have been throttled in background tabs).- const handleOnline = reconnect;- const handleVisibilityChange = () => {- if (document.visibilityState === 'visible') {- reconnect();- }- };-- if (typeof window !== 'undefined') {- window.addEventListener('online', handleOnline);- document.addEventListener('visibilitychange', handleVisibilityChange);- }-- const cleanup = () => {- logSyncService({- documentId,- level: 'debug',- context: {},- message: 'sync source: cleanup called, closing WS',- });- if (typeof window !== 'undefined') {- window.removeEventListener('online', handleOnline);- document.removeEventListener('visibilitychange', handleVisibilityChange);- }- ws.close();- };-- return {- source: {- documentId,- listen: eventBus.listen,- status: () => mapToSyncStatus(status()),- pushUpdate,- registerPeerId,- pushAwareness,- requestSnapshot,- requestUpdatesSince,- reconnect,- cleanup,- },- doInitialSync,- };-};diff --git a/js/app/packages/service-clients/service-sync/source/helpers.ts b/js/app/packages/service-clients/service-sync/source/helpers.tsnew file mode 100644index 0000000000..3e6c4a2308--- /dev/null+++ b/js/app/packages/service-clients/service-sync/source/helpers.ts@@ -0,0 +1,67 @@+import type {+ InitialSync,+ LiveSyncSource,+ TimeoutError,+} from '@core/collab/source';+import { SYNC_SERVICE_HOSTS } from '@core/constant/servers';+import { storageServiceClient } from '@service-storage/client';+import type { UrlResolver } from '@websocket';+import { createWebsocketStateSignal } from '@websocket/solid/state-signal';+import type { ResultAsync } from 'neverthrow';+import { createSyncSocket, type SyncWebsocket } from './socket';+import { mapToSyncStatus, SyncServiceSource } from './source';++const SYNC_SERVICE_WS_URL = `${SYNC_SERVICE_HOSTS['ws']}/document`;++/**+ * Browser sync websocket: uses the already-fetched token for the initial+ * connect, then refetches a fresh permission token on every reconnect.+ */+export function createSyncServiceSocket(+ documentId: string,+ initialToken: string+): SyncWebsocket {+ const connectUrl = (token: string) =>+ `${SYNC_SERVICE_WS_URL}/${documentId}/connect?token=${token}`;+ let initialUrl: string | undefined = connectUrl(initialToken);+ let fallbackUrl = initialUrl;++ const getUrl: UrlResolver = async () => {+ if (initialUrl) {+ const url = initialUrl;+ initialUrl = undefined;+ return url;+ }++ const response =+ await storageServiceClient.permissionsTokens.createPermissionToken({+ document_id: documentId,+ });++ if (response.isErr()) {+ console.error('failed to fetch permission token', response);+ return fallbackUrl;+ }++ const refreshedUrl = connectUrl(response.value.token);+ fallbackUrl = refreshedUrl;+ return refreshedUrl;+ };++ return createSyncSocket(getUrl);+}++export const createSyncServiceSource = (+ documentId: string,+ token: string+): {+ source: LiveSyncSource;+ doInitialSync: () => ResultAsync<InitialSync, TimeoutError>;+} => {+ const ws = createSyncServiceSocket(documentId, token);+ const state = createWebsocketStateSignal(ws);+ const source = new SyncServiceSource(ws, documentId, {+ status: () => mapToSyncStatus(state()),+ });+ return { source, doInitialSync: source.doInitialSync };+};diff --git a/js/app/packages/service-clients/service-sync/source/index.ts b/js/app/packages/service-clients/service-sync/source/index.tsnew file mode 100644index 0000000000..93d8afe296--- /dev/null+++ b/js/app/packages/service-clients/service-sync/source/index.ts@@ -0,0 +1,3 @@+export * from './helpers';+export * from './socket';+export * from './source';diff --git a/js/app/packages/service-clients/service-sync/source/socket.ts b/js/app/packages/service-clients/service-sync/source/socket.tsnew file mode 100644index 0000000000..20c95194ec--- /dev/null+++ b/js/app/packages/service-clients/service-sync/source/socket.ts@@ -0,0 +1,72 @@+import {+ ArrayQueue,+ BebopSerializer,+ ExponentialBackoff,+ type UrlResolver,+ type Websocket,+ WebsocketBuilder,+ type WebsocketConnectionState,+ type WebsocketEvent,+ type WebsocketEventListener,+} from '@websocket';+import { FromPeer, FromRemote, type IFromPeer } from '../generated/schema';++/** The concrete sync websocket: a {@link Websocket} speaking the Bebop protocol. */+export type SyncWebsocket = Websocket<IFromPeer, FromRemote>;++/**+ * Simple websocket interface that we use for our syncing abstractions.+ */+export interface SyncSocket {+ connectionState: WebsocketConnectionState;+ send(message: IFromPeer): boolean;+ startHeartbeat(): void;+ reconnectIfDisconnected(): void;+ close(code?: number, reason?: string): void;+ addEventListener<K extends WebsocketEvent>(+ type: K,+ listener: WebsocketEventListener<K, IFromPeer, FromRemote>+ ): void;+ removeEventListener<K extends WebsocketEvent>(+ type: K,+ listener: WebsocketEventListener<K, IFromPeer, FromRemote>+ ): void;+}++/**+ * Builds a sync websocket configured the way every caller wants it: Bebop+ * serializer, capped exponential backoff, send buffer, and a manually-started+ * heartbeat. The `getUrl` resolver is the only environment-specific bit — the+ * browser refreshes a permission token on each (re)connect, the worker returns+ * a static URL.+ */+export function createSyncSocket(getUrl: UrlResolver): SyncWebsocket {+ return (+ new WebsocketBuilder(getUrl)+ .withSerializer(new BebopSerializer(FromPeer, FromRemote))+ // Capped exponential backoff. The scheduler calls next() before the first+ // retry, so the delays are 250*2^1 = 500ms doubling to a 250*2^5 = 8s+ // cap; 20 retries ≈ 2 minutes of automatic attempts, after which+ // something is very wrong and we stop hammering. A given-up socket is+ // revived by 'online' / 'visibilitychange' signals or by the user editing+ // (see pushUpdate) — unlike before, exhausting the budget no longer+ // strands the socket permanently.+ .withBackoff(new ExponentialBackoff(250, 5))+ .withMaxRetries(20)+ // Queue messages sent while disconnected; they are flushed in order once+ // the connection is re-established, so edits made during a reconnect+ // aren't dropped. Unbounded on purpose: dropping the oldest updates would+ // leave dependency gaps the server can never fill, and CRDT updates are+ // tiny relative to a session's lifetime.+ .withBuffer(new ArrayQueue())+ .withHeartbeat({+ interval: 10_000,+ timeout: 5_000,+ pingMessage: 'ping',+ pongMessage: 'pong',+ maxMissedHeartbeats: 2,+ autoStart: false, // Start heartbeat manually after initial sync completes+ })+ .build()+ );+}diff --git a/js/app/packages/service-clients/service-sync/source/source.test.ts b/js/app/packages/service-clients/service-sync/source/source.test.tsnew file mode 100644index 0000000000..c0be402202--- /dev/null+++ b/js/app/packages/service-clients/service-sync/source/source.test.ts@@ -0,0 +1,236 @@+import type { SyncSourceEvent } from '@core/collab/source';+import {+ WebsocketConnectionState,+ WebsocketEvent,+ type WebsocketEventListener,+} from '@websocket';+import { describe, expect, it, vi } from 'vitest';+import type { FromRemote, IFromPeer } from '../generated/schema';+import type { SyncSocket } from './socket';+import { SyncServiceSource, TIMEOUTS } from './source';++type AnyListener = WebsocketEventListener<+ WebsocketEvent,+ IFromPeer,+ FromRemote+>;++// Minimal in-memory SyncSocket. Drives the class through deliver()/fireReconnect().+class FakeSocket implements SyncSocket {+ connectionState = WebsocketConnectionState.Connecting;+ sent: IFromPeer[] = [];+ heartbeats = 0;+ reconnects = 0;+ closed = false;+ private msg = new Set<AnyListener>();+ private recon = new Set<AnyListener>();++ send(message: IFromPeer) {+ this.sent.push(message);+ return true;+ }+ startHeartbeat() {+ this.heartbeats++;+ }+ reconnectIfDisconnected() {+ this.reconnects++;+ }+ close() {+ this.closed = true;+ }+ addEventListener<K extends WebsocketEvent>(+ type: K,+ listener: WebsocketEventListener<K, IFromPeer, FromRemote>+ ) {+ if (type === WebsocketEvent.Message) this.msg.add(listener as AnyListener);+ else if (type === WebsocketEvent.Reconnect)+ this.recon.add(listener as AnyListener);+ }+ removeEventListener<K extends WebsocketEvent>(+ _type: K,+ listener: WebsocketEventListener<K, IFromPeer, FromRemote>+ ) {+ this.msg.delete(listener as AnyListener);+ this.recon.delete(listener as AnyListener);+ }++ // test drivers+ deliver(data: FromRemote) {+ const event = { data } as Parameters<AnyListener>[1];+ for (const listener of [...this.msg]) listener(this as never, event);+ }+ fireReconnect() {+ this.connectionState = WebsocketConnectionState.Open;+ const event = {} as Parameters<AnyListener>[1];+ for (const listener of [...this.recon]) listener(this as never, event);+ }+}++const GUARDS = {+ isRemoteInitialSync: () => false,+ isRemoteUpdate: () => false,+ isRemoteAwareness: () => false,+ isRemoteSnapshot: () => false,+ isRemoteUpdateAck: () => false,+ isRemoteUpdateSince: () => false,+};++const remote = {+ initialSync: (snapshot: Uint8Array, awareness: Uint8Array) =>+ ({+ ...GUARDS,+ isRemoteInitialSync: () => true,+ value: { snapshot, awareness },+ }) as unknown as FromRemote,+ update: (update: Uint8Array) =>+ ({+ ...GUARDS,+ isRemoteUpdate: () => true,+ value: { update },+ }) as unknown as FromRemote,+ awareness: (awareness: Uint8Array) =>+ ({+ ...GUARDS,+ isRemoteAwareness: () => true,+ value: { awareness },+ }) as unknown as FromRemote,+ snapshot: (snapshot: Uint8Array) =>+ ({+ ...GUARDS,+ isRemoteSnapshot: () => true,+ value: { snapshot },+ }) as unknown as FromRemote,+ ack: (id: string) =>+ ({+ ...GUARDS,+ isRemoteUpdateAck: () => true,+ value: { id },+ }) as unknown as FromRemote,+};++const snap = new Uint8Array([1, 2, 3]);+const aw = new Uint8Array([4, 5]);+const flush = () => new Promise((resolve) => setTimeout(resolve));++describe('SyncServiceSource', () => {+ it('resolves doInitialSync and starts heartbeat on RemoteInitialSync', async () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1');+ const pending = src.doInitialSync();+ ws.deliver(remote.initialSync(snap, aw));+ expect((await pending)._unsafeUnwrap()).toEqual({+ snapshot: snap,+ awareness: aw,+ });+ expect(ws.heartbeats).toBe(1);+ });++ it('maps RemoteUpdate to an update event', () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1');+ const events: SyncSourceEvent[] = [];+ src.listen((e) => events.push(e));+ const bytes = new Uint8Array([9]);+ ws.deliver(remote.update(bytes));+ expect(events).toEqual([{ type: 'update', update: bytes }]);+ });++ it('buffers events that arrive before the first listener attaches', () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1');+ const bytes = new Uint8Array([7]);+ ws.deliver(remote.update(bytes)); // no listener yet+ const events: SyncSourceEvent[] = [];+ src.listen((e) => events.push(e));+ expect(events).toEqual([{ type: 'update', update: bytes }]);+ });++ it('pushUpdate resolves false before initial sync', async () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1');+ expect(await src.pushUpdate([new Uint8Array([1])])).toBe(false);+ });++ it('pushUpdate resolves true when a matching ack arrives', async () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1', { newId: () => 'id-1' });+ ws.deliver(remote.initialSync(snap, aw));+ await src.doInitialSync();++ const acked = src.pushUpdate([new Uint8Array([1])]);+ expect(ws.sent).toHaveLength(1); // the FromPeer update went out+ ws.deliver(remote.ack('id-1'));+ expect(await acked).toBe(true);+ });++ it('pushUpdate resolves false when the ack times out', async () => {+ vi.useFakeTimers();+ try {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1', { newId: () => 'id-1' });+ ws.deliver(remote.initialSync(snap, aw));+ await src.doInitialSync();++ const acked = src.pushUpdate([new Uint8Array([1])]);+ await vi.advanceTimersByTimeAsync(TIMEOUTS.ACK + 1);+ expect(await acked).toBe(false);+ } finally {+ vi.useRealTimers();+ }+ });++ it('matches concurrent acks to the right pushUpdate, even out of order', async () => {+ let n = 0;+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1', { newId: () => `id-${++n}` });+ ws.deliver(remote.initialSync(snap, aw));+ await src.doInitialSync();++ const first = src.pushUpdate([new Uint8Array([1])]); // id-1+ const second = src.pushUpdate([new Uint8Array([2])]); // id-2+ ws.deliver(remote.ack('id-2')); // ack the second one first+ ws.deliver(remote.ack('id-1'));+ expect(await first).toBe(true);+ expect(await second).toBe(true);+ });++ it('pushAwareness sends only while the socket is open', () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1');++ ws.connectionState = WebsocketConnectionState.Reconnecting;+ src.pushAwareness(new Uint8Array([1]));+ expect(ws.sent).toHaveLength(0); // dropped while not open++ ws.connectionState = WebsocketConnectionState.Open;+ src.pushAwareness(new Uint8Array([2]));+ expect(ws.sent).toHaveLength(1); // sent once open+ });++ it('requestSnapshot resolves with the snapshot bytes', async () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1');+ const pending = src.requestSnapshot();+ const bytes = new Uint8Array([42]);+ ws.deliver(remote.snapshot(bytes));+ expect((await pending)._unsafeUnwrap()).toEqual(bytes);+ });++ it('re-syncs and emits reconnect after a Reconnect event', async () => {+ const ws = new FakeSocket();+ const src = new SyncServiceSource(ws, 'doc1');+ const events: SyncSourceEvent[] = [];+ src.listen((e) => events.push(e));++ ws.fireReconnect();+ expect(ws.heartbeats).toBe(1); // heartbeat restarted on reconnect+ ws.deliver(remote.initialSync(snap, aw));+ await flush();++ expect(events).toContainEqual({+ type: 'reconnect',+ snapshot: snap,+ awareness: aw,+ });+ });+});diff --git a/js/app/packages/service-clients/service-sync/source/source.ts b/js/app/packages/service-clients/service-sync/source/source.tsnew file mode 100644index 0000000000..9d66a5c9a0--- /dev/null+++ b/js/app/packages/service-clients/service-sync/source/source.ts@@ -0,0 +1,356 @@+import { logSyncService } from '@core/collab/logger';+import type { RawUpdate } from '@core/collab/shared';+import {+ type InitialSync,+ type LiveSyncSource,+ SyncError,+ type SyncSourceEvent,+ SyncSourceStatus,+ type TimeoutError,+} from '@core/collab/source';+import { arrayEquals } from '@core/util/compareUtils';+import {+ WebsocketConnectionState,+ WebsocketEvent,+ type WebsocketEventListener,+} from '@websocket';+import type { VersionVector } from 'loro-crdt';+import { ResultAsync } from 'neverthrow';+import {+ FromPeer,+ type FromRemote,+ type IFromPeer,+ type RemoteSnapshot,+ type RemoteUpdateSince,+} from '../generated/schema';+import type { SyncSocket } from './socket';++export const TIMEOUTS = {+ INITIAL_SYNC: 10_000,+ // The server only acks after durably storing the update, and its internal+ // budget for a storage operation is 4.5s — so a busy-but-healthy server can+ // legitimately ack late. Waiting 5s keeps "server is busy" from being+ // misread as "update was lost".+ ACK: 5_000,+ SNAPSHOT: 10_000,+ REQUEST_UPDATES_SINCE: 10_000,+} as const;++export function mapToSyncStatus(+ status: WebsocketConnectionState+): SyncSourceStatus {+ switch (status) {+ case WebsocketConnectionState.Connecting:+ return SyncSourceStatus.Connecting;+ case WebsocketConnectionState.Open:+ return SyncSourceStatus.Connected;+ case WebsocketConnectionState.Closed:+ case WebsocketConnectionState.Closing:+ case WebsocketConnectionState.Reconnecting:+ return SyncSourceStatus.Disconnected;+ }+}++function syncEventForMessage(message: FromRemote): SyncSourceEvent | null {+ if (message.isRemoteUpdate()) {+ return { type: 'update', update: message.value.update };+ } else if (message.isRemoteAwareness()) {+ return { type: 'awareness', awareness: message.value.awareness };+ } else if (message.isRemoteSnapshot()) {+ return { type: 'incremental_snapshot', snapshot: message.value.snapshot };+ }+ return null;+}++/** A pending request: resolves with the first {@link FromRemote} that matches. */+interface MessageWaiter {+ match: (message: FromRemote) => boolean;+ resolve: (message: FromRemote) => void;+}++export interface SyncServiceSourceOptions {+ /** ID generator for update messages; overridable so tests can match acks. */+ newId?: () => string;+ /**+ * Connection status accessor. Defaults to a plain read of the socket state;+ * the browser injects a reactive Solid signal so UI tracks status changes.+ */+ status?: LiveSyncSource['status'];+}++/**+ * Transport-agnostic sync-service source.+ */+export class SyncServiceSource implements LiveSyncSource {+ public readonly documentId: string;++ private readonly ws: SyncSocket;+ private readonly newId: () => string;+ private readonly listeners = new Set<(event: SyncSourceEvent) => void>();+ /** Events that arrive before any listener attaches; flushed on `listen()`. */+ private readonly buffered: SyncSourceEvent[] = [];+ /** Pending request/response waiters, matched against each incoming message. */+ private readonly waiters = new Set<MessageWaiter>();+ private initialSyncReceived = false;+ private readonly initialSyncPromise: ResultAsync<InitialSync, TimeoutError>;++ public constructor(+ ws: SyncSocket,+ documentId: string,+ options: SyncServiceSourceOptions = {}+ ) {+ this.ws = ws;+ this.documentId = documentId;+ this.newId = options.newId ?? (() => crypto.randomUUID());+ if (options.status) this.status = options.status;++ logSyncService({+ documentId,+ level: 'debug',+ context: {},+ message: 'sync source: created, waiting for WS open',+ });++ // Register the initial-sync listener eagerly so it's in place before the+ // server's RemoteInitialSync message arrives (~50ms after WS opens).+ // `doInitialSync()` just returns the cached promise; if it's called late,+ // it still resolves because the listener captured the message.+ this.initialSyncPromise = this.awaitInitialSync().map((message) => {+ this.initialSyncReceived = true;+ logSyncService({+ documentId,+ level: 'debug',+ context: {},+ message: 'sync source: initial sync received',+ });+ // Start heartbeat only after initial sync completes successfully — this+ // prevents the heartbeat from closing the connection during slow syncs.+ this.ws.startHeartbeat();+ return message.value as InitialSync;+ });++ this.ws.addEventListener(WebsocketEvent.Message, this.onMessage);+ this.ws.addEventListener(WebsocketEvent.Reconnect, this.onReconnect);++ this.registerEnvironmentListeners();+ }++ /**+ * When the browser regains connectivity or the tab becomes visible again,+ * kick the connection immediately instead of waiting out the current backoff+ * timer. No-ops in non-DOM environments (e.g. the worker).+ */+ private registerEnvironmentListeners(): void {+ if (typeof window === 'undefined') return;+ window.addEventListener('online', this.onOnline);+ document.addEventListener('visibilitychange', this.onVisibilityChange);+ }++ public doInitialSync = (): ResultAsync<InitialSync, TimeoutError> =>+ this.initialSyncPromise;++ public listen: LiveSyncSource['listen'] = (listener) => {+ this.listeners.add(listener);+ // Flush anything that arrived before the first listener attached.+ if (this.buffered.length > 0) {+ const pending = this.buffered.splice(0);+ for (const event of pending) listener(event);+ }+ return () => this.listeners.delete(listener);+ };++ public status: LiveSyncSource['status'] = () =>+ mapToSyncStatus(this.ws.connectionState);++ public pushUpdate = async (updates: RawUpdate[]): Promise<boolean> => {+ if (!this.initialSyncReceived) return false;+ if (updates.length === 0) return true;++ const id = this.newId();+ // Attach the ack listener before sending so an ack that races back early+ // can't be missed.+ const acked = this.awaitMessage(+ (message) => message.isRemoteUpdateAck() && message.value.id === id,+ TIMEOUTS.ACK+ ).then(+ () => true,+ () => false+ );++ this.ws.send(FromPeer.fromPeerUpdate({ updates, id }));+ return acked;+ };++ public pushAwareness = (awareness: RawUpdate): void => {+ // Awareness is ephemeral (cursor positions with a short server-side TTL),+ // so never let it sit in the send buffer+ if (this.ws.connectionState !== WebsocketConnectionState.Open) return;+ this.ws.send(FromPeer.fromPeerAwareness({ awareness }));+ };++ public registerPeerId = (peerId: bigint): void => {+ this.ws.send(FromPeer.fromPeerRegisterId({ peerid: peerId }));+ };++ public requestSnapshot = (): ResultAsync<RawUpdate, TimeoutError> => {+ this.ws.send(FromPeer.fromPeerRequestSnapshot({}));+ return ResultAsync.fromPromise(+ this.awaitMessage(+ (message) => message.isRemoteSnapshot(),+ TIMEOUTS.SNAPSHOT+ ),+ (error) => error as TimeoutError+ ).map((message) => (message.value as RemoteSnapshot).snapshot);+ };++ public requestUpdatesSince = (+ vv: VersionVector+ ): ResultAsync<RawUpdate, TimeoutError> => {+ const encodedVv = vv.encode();+ this.ws.send(FromPeer.fromPeerRequestSince({ vv: encodedVv }));+ return ResultAsync.fromPromise(+ this.awaitMessage(+ (message) =>+ message.isRemoteUpdateSince() &&+ arrayEquals(message.value.vv, encodedVv),+ TIMEOUTS.REQUEST_UPDATES_SINCE+ ),+ (error) => error as TimeoutError+ ).map((message) => (message.value as RemoteUpdateSince).update);+ };++ public reconnect = (): void => {+ // Only force a new connection when the socket is actually down. Tearing+ // down an OPEN socket caused reconnect storms, and tearing down a+ // CONNECTING one aborts an attempt that may be about to succeed. Liveness+ // of open sockets is owned by the heartbeat.+ this.ws.reconnectIfDisconnected();+ };++ public cleanup = (): void => {+ logSyncService({+ documentId: this.documentId,+ level: 'debug',+ context: {},+ message: 'sync source: cleanup called, closing WS',+ });+ if (typeof window !== 'undefined') {+ window.removeEventListener('online', this.onOnline);+ document.removeEventListener('visibilitychange', this.onVisibilityChange);+ }+ this.ws.removeEventListener(WebsocketEvent.Message, this.onMessage);+ this.ws.removeEventListener(WebsocketEvent.Reconnect, this.onReconnect);+ this.ws.close();+ };++ private emit(event: SyncSourceEvent) {+ if (this.listeners.size === 0) {+ this.buffered.push(event);+ return;+ }+ for (const listener of this.listeners) listener(event);+ }++ private awaitInitialSync(): ResultAsync<FromRemote, TimeoutError> {+ return ResultAsync.fromPromise(+ this.awaitMessage(+ (message) => message.isRemoteInitialSync(),+ TIMEOUTS.INITIAL_SYNC+ ),+ (error) => error as TimeoutError+ );+ }++ /**+ * Resolves with the next message matching `match`, or rejects with a+ * {@link TimeoutError} after `timeoutMs`. The waiter removes itself on both+ * paths, so a timed-out request leaves nothing behind.+ */+ private awaitMessage(+ match: (message: FromRemote) => boolean,+ timeoutMs: number+ ): Promise<FromRemote> {+ const { promise, resolve, reject } = Promise.withResolvers<FromRemote>();+ const waiter: MessageWaiter = {+ match,+ resolve: (message) => {+ remove();+ resolve(message);+ },+ };+ const timer = setTimeout(() => {+ remove();+ reject(SyncError.timeout(timeoutMs));+ }, timeoutMs);+ const remove = () => {+ clearTimeout(timer);+ this.waiters.delete(waiter);+ };+ this.waiters.add(waiter);+ return promise;+ }++ private onMessage: WebsocketEventListener<+ WebsocketEvent.Message,+ IFromPeer,+ FromRemote+ > = (_ws, event) => {+ const message = event.data;+ const syncEvent = syncEventForMessage(message);+ if (syncEvent) this.emit(syncEvent);+ // Resolve every pending request whose predicate matches this message.+ for (const waiter of [...this.waiters]) {+ if (waiter.match(message)) waiter.resolve(message);+ }+ };++ private onReconnect: WebsocketEventListener<+ WebsocketEvent.Reconnect,+ IFromPeer,+ FromRemote+ > = () => {+ logSyncService({+ documentId: this.documentId,+ level: 'debug',+ context: {},+ message: 'sync source: WS reconnected, waiting for initial sync',+ });+ // Always restart heartbeat after reconnect, regardless of sync+ // success/failure, so the connection stays monitored even if sync fails.+ this.ws.startHeartbeat();++ void this.awaitInitialSync().match(+ (message) => {+ const sync = message.value as InitialSync;+ logSyncService({+ documentId: this.documentId,+ level: 'debug',+ context: {},+ message:+ 'sync source: reconnect initial sync received, emitting reconnect event',+ });+ this.emit({+ type: 'reconnect',+ snapshot: sync.snapshot,+ awareness: sync.awareness,+ });+ },+ (error) => {+ logSyncService({+ documentId: this.documentId,+ level: 'warn',+ context: { misc: { error } },+ message: 'sync source: reconnect initial sync timed out',+ });+ // Heartbeat is already running, so the connection remains monitored+ // even though sync failed; it will eventually timeout and retry.+ }+ );+ };++ private onOnline = (): void => this.reconnect();++ private onVisibilityChange = (): void => {+ if (document.visibilityState === 'visible') this.reconnect();+ };+}diff --git a/js/app/packages/websocket/core/websocket-builder.ts b/js/app/packages/websocket/core/websocket-builder.tsindex 609f2c830c..a22c70bbb0 100644--- a/js/app/packages/websocket/core/websocket-builder.ts+++ b/js/app/packages/websocket/core/websocket-builder.ts@@ -192,7 +192,7 @@ export class WebsocketBuilder<Send = WebsocketData, Receive = WebsocketData> {listeners: this._options?.listeners,serializer,buffer: this._options?.buffer,- } as WebsocketOptions<NewSend, NewReceive>;+ } as unknown as WebsocketOptions<NewSend, NewReceive>;if (serializer.binaryType !== undefined) {return newBuilder.withBinaryType(serializer.binaryType);diff --git a/js/app/packages/websocket/core/websocket.ts b/js/app/packages/websocket/core/websocket.tsindex f54c610a1a..a8f7428719 100644--- a/js/app/packages/websocket/core/websocket.ts+++ b/js/app/packages/websocket/core/websocket.ts@@ -1,5 +1,6 @@import { platformWebSocketFactory } from '@websocket/platform/factory';import { match, P } from 'ts-pattern';+import type { MinimalWebSocket } from '../platform/minimal-websocket';import type { Backoff } from './backoff/backoff';import type { WebsocketBuffer } from './websocket-buffer';import { WebsocketConnectionState } from './websocket-connection-state';@@ -41,7 +42,7 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {private _closedByUser: boolean = false; // whether the websocket was closed by the userprivate _lastConnection?: Date; // timestamp of the last connection- private _underlyingWebsocket: WebSocket; // the underlying websocket, e.g. native browser websocket+ private _underlyingWebsocket: MinimalWebSocket; // the underlying websocket, e.g. native browser websocketprivate connectPending: boolean = false; // whether tryConnect is resolving its url / creating a socketprivate retryTimeout?: ReturnType<typeof globalThis.setTimeout>; // timeout for the next retry, if any@@ -192,7 +193,7 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {* @see https://developer.mozilla.org/en-US/docs/Web/API/WebSocket* @return the underlying websocket.*/- get underlyingWebsocket(): WebSocket {+ get underlyingWebsocket(): MinimalWebSocket {return this._underlyingWebsocket;}@@ -288,7 +289,10 @@ export class Websocket<Send = WebsocketData, Receive = WebsocketData> {// The underlying websocket is unassigned while the first connect attempt// is still resolving its url; tryConnect observes _closedByUser and// won't open a socket after this point.- (this._underlyingWebsocket as WebSocket | undefined)?.close(code, reason);+ (this._underlyingWebsocket as MinimalWebSocket | undefined)?.close(+ code,+ reason+ );this.stopHeartbeat();}diff --git a/js/app/vitest.config.ts b/js/app/vitest.config.tsindex e1af975076..6953e8215f 100644--- a/js/app/vitest.config.ts+++ b/js/app/vitest.config.ts@@ -95,6 +95,13 @@ export default defineConfig({name: 'block-email',},},+ {+ extends: './packages/core/vitest.config.ts',+ test: {+ include: ['packages/service-clients/**/*.{test,spec}.{ts,tsx}'],+ name: 'service-clients',+ },+ },],},});diff --git a/js/bun.lock b/js/bun.lockindex a30172b63b..384ee4e52f 100644--- a/js/bun.lock+++ b/js/bun.lock@@ -314,6 +314,10 @@"name": "@macro-inc/lexical-core","version": "0.2.2","dependencies": {+ "@ai-sdk/anthropic": "^3.0.85",+ "@ai-sdk/cerebras": "^2.0.57",+ "@ai-sdk/google": "^3.0.83",+ "@ai-sdk/openai": "^3.0.73","@lexical/code": "0.45.0","@lexical/headless": "0.45.0","@lexical/link": "0.45.0",@@ -323,12 +327,26 @@"@lexical/rich-text": "0.45.0","@lexical/table": "0.45.0","@lexical/utils": "0.45.0",+ "@types/node": "^25.9.3",+ "ai": "^6.0.207",+ "bitf": "^1.0.2",+ "diff": "^9.0.0",+ "envsafe": "^2.0.3",+ "fast-xml-parser": "^4.5.6",+ "flat": "^6.0.1","lexical": "0.45.0","loro-crdt": "1.13.3","nanoid": "^5.1.5",+ "p-queue": "^9.3.0",+ "random-js": "^2.1.0",+ "ts-pattern": "^5.9.0",+ "zod": "^4.4.3",},"devDependencies": {"@types/bun": "^1.3.14",+ "@types/diff": "^8.0.0",+ "@types/flat": "^5.0.5",+ "@types/yargs": "^17.0.35",},},"lexical-service": {@@ -427,6 +445,22 @@"@adobe/css-tools": ["@adobe/[email protected]", "", {}, "sha512-Elp+iwUx5rN5+Y8xLt5/GRoG20WGoDCQ/1Fb+1LiGtvwbDavuSk0jhD/eZdckHAuzcDzccnkv+rEjyWfRx18gg=="],+ "@ai-sdk/anthropic": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-fNeDB644l5wbRNQU0FnI+F7UTtOenMnPtACfMPUJaS2zJfuBlseEa1TMg+otHkETZgaJB+6Na51NQEv0+m7czw=="],++ "@ai-sdk/cerebras": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/openai-compatible": "2.0.51", "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-sf7zWCAKBK0vU3+BuJd3qYNZmEZ5FQHh5swKMaYzJ2pRByjT3xUDa41vxQa4z06SnCdV6k+fTm4swN+kZWdtmQ=="],++ "@ai-sdk/gateway": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30", "@vercel/oidc": "3.2.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-Ebs+7iS9zUgJu5B0RlxM2JmDWzq79Cpd6YdiqcCzB5qFdpfQJPUDiXutqlQP89F2XGjOdDeidulBTXUdXWzOxw=="],++ "@ai-sdk/google": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-Pz7aCX0dy+5x+r4K/37HbLZNaPtPL4q2NduzJW64VffLv5sI9Nb478wAd7PlH2r2asiypJsz/Jerf9draTciUA=="],++ "@ai-sdk/openai": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-+3x9oxHv9Xp33Iv2L8D+e5hqmZi64jofBKig/9611JKyfV59NdkaDDajtwc0CxOEfARgCVq1BW7dP+526gKOKw=="],++ "@ai-sdk/openai-compatible": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-A6qfyaVs4lxmRxRux6U3ViOa8mMbsSd0OaHghpei2MpiBT6791J4zFH5MN7kaW1tLfQ246rSC2DVTMOavsycjQ=="],++ "@ai-sdk/provider": ["@ai-sdk/[email protected]", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-Q3BZ27qfpYqnCYGvE3vt+Qi6LGOF9R5Nmzn+9JoM1lCRsD9mYaIhfJLkSunN48nfGXJ6n+XNV0J/XVpqGQl7Dw=="],++ "@ai-sdk/provider-utils": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "3.0.10", "@standard-schema/spec": "^1.1.0", "eventsource-parser": "^3.0.8" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-VO7I+vPffqI5sMnPoUq5DCSqKIgQIk/naJWRdQVpz2ma2zoprC/lqiJiUEl2s6DfvTD76TbhD3q39ROjlA6rGw=="],+"@antfu/install-pkg": ["@antfu/[email protected]", "", { "dependencies": { "package-manager-detector": "^1.3.0", "tinyexec": "^1.0.1" } }, "sha512-MGQsmw10ZyI+EJo45CdSER4zEb+p31LpDAFp2Z3gkSd1yqVZGi0Ebx++YTEMonJy4oChEMLsxZ64j8FH6sSqtQ=="],"@apidevtools/json-schema-ref-parser": ["@apidevtools/[email protected]", "", { "dependencies": { "js-yaml": "^4.1.1" }, "peerDependencies": { "@types/json-schema": "^7.0.15" } }, "sha512-orNOYXw3hYXxxisXMldjzjBzqqTLBPbwOtHg7ovBPvfBHDue1qM9YJENZ3W2BQuS+7z4ThogMbEzEsov57Itkg=="],@@ -1503,6 +1537,8 @@"@speed-highlight/core": ["@speed-highlight/[email protected]", "", {}, "sha512-BMq1K3DsElxDWawkX6eLg9+CKJrTVGCBAWVuHXVUV2u0s2711qiChLSId6ikYPfxhdYocLNt3wWwSvDiTvFabw=="],+ "@standard-schema/spec": ["@standard-schema/[email protected]", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="],+"@stoplight/better-ajv-errors": ["@stoplight/[email protected]", "", { "dependencies": { "jsonpointer": "^5.0.0", "leven": "^3.1.0" }, "peerDependencies": { "ajv": ">=8" } }, "sha512-0p9uXkuB22qGdNfy3VeEhxkU5uwvp/KrBTAbrLBURv6ilxIVwanKwjMc41lQfIVgPGcOkmLbTolfFrSsueu7zA=="],"@stoplight/json": ["@stoplight/[email protected]", "", { "dependencies": { "@stoplight/ordered-object-literal": "^1.0.3", "@stoplight/path": "^1.3.2", "@stoplight/types": "^13.6.0", "jsonc-parser": "~2.2.1", "lodash": "^4.17.21", "safe-stable-stringify": "^1.1" } }, "sha512-xcJXgKFqv/uCEgtGlPxy3tPA+4I+ZI4vAuMJ885+ThkTHFVkC+0Fm58lA9NlsyjnkpxFh4YiQWpH+KefHdbA0A=="],@@ -1629,6 +1665,8 @@"@types/deep-eql": ["@types/[email protected]", "", {}, "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw=="],+ "@types/diff": ["@types/[email protected]", "", { "dependencies": { "diff": "*" } }, "sha512-o7jqJM04gfaYrdCecCVMbZhNdG6T1MHg/oQoRFdERLV+4d+V7FijhiEAbFu0Usww84Yijk9yH58U4Jk4HbtzZw=="],+"@types/dom-mediacapture-record": ["@types/[email protected]", "", {}, "sha512-mUMZLK3NvwRLcAAT9qmcK+9p7tpU2FHdDsntR3YI4+GY88XrgG4XiE7u1Q2LAN2/FZOz/tdMDC3GQCR4T8nFuw=="],"@types/dom-mediacapture-transform": ["@types/[email protected]", "", { "dependencies": { "@types/dom-webcodecs": "*" } }, "sha512-Y2p+nGf1bF2XMttBnsVPHUWzRRZzqUoJAKmiP10b5umnO6DDrWI0BrGDJy1pOHoOULVmGSfFNkQrAlC5dcj6nQ=="],@@ -1641,6 +1679,8 @@"@types/facebook-pixel": ["@types/[email protected]", "", {}, "sha512-NyfutSDY9Cabk7IUsEMy2pcVPGVVQjsFI3caZkeZvJ9lffz/CZjJA84mUhluTRM/iHjZ96VwBMPORpDSioNeyg=="],+ "@types/flat": ["@types/[email protected]", "", {}, "sha512-nPLljZQKSnac53KDUDzuzdRfGI0TDb5qPrb+SrQyN3MtdQrOnGsKniHN1iYZsJEBIVQve94Y6gNz22sgISZq+Q=="],+"@types/gtag.js": ["@types/[email protected]", "", {}, "sha512-wwAbk3SA2QeU67unN7zPxjEHmPmlXwZXZvQEpbEUQuMCRGgKyE1m6XDuTUA9b6pCGb/GqJmdfMOY5LuDjJSbbg=="],"@types/hast": ["@types/[email protected]", "", { "dependencies": { "@types/unist": "*" } }, "sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ=="],@@ -1679,6 +1719,10 @@"@types/ws": ["@types/[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg=="],+ "@types/yargs": ["@types/[email protected]", "", { "dependencies": { "@types/yargs-parser": "*" } }, "sha512-qUHkeCyQFxMXg79wQfTtfndEC+N9ZZg76HJftDJp+qH2tV7Gj4OJi7l+PiWwJ+pWtW8GwSmqsDj/oymhrTWXjg=="],++ "@types/yargs-parser": ["@types/[email protected]", "", {}, "sha512-I4q9QU9MQv4oEOz4tAHJtNz1cwuLxn2F3xcc2iV5WdqLPpUnj30aUuxt1mAxYTG+oe8CZMV/+6rU4S4gRDzqtQ=="],+"@typescript-eslint/eslint-plugin": ["@typescript-eslint/[email protected]", "", { "dependencies": { "@eslint-community/regexpp": "^4.4.0", "@typescript-eslint/scope-manager": "5.62.0", "@typescript-eslint/type-utils": "5.62.0", "@typescript-eslint/utils": "5.62.0", "debug": "^4.3.4", "graphemer": "^1.4.0", "ignore": "^5.2.0", "natural-compare-lite": "^1.4.0", "semver": "^7.3.7", "tsutils": "^3.21.0" }, "peerDependencies": { "@typescript-eslint/parser": "^5.0.0", "eslint": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, "sha512-TiZzBSJja/LbhNPvk6yc0JrX9XqhQ0hdh6M2svYfsHGejaKFIAGd9MQ+ERIMzLGlN/kZoYIgdxFV0PuljTKXag=="],"@typescript-eslint/parser": ["@typescript-eslint/[email protected]", "", { "dependencies": { "@typescript-eslint/scope-manager": "5.62.0", "@typescript-eslint/types": "5.62.0", "@typescript-eslint/typescript-estree": "5.62.0", "debug": "^4.3.4" }, "peerDependencies": { "eslint": "^6.0.0 || ^7.0.0 || ^8.0.0" } }, "sha512-VlJEV0fOQ7BExOsHYAGrgbEiZoi8D+Bl2+f6V2RrXerRSylnp+ZBHmPvaIa8cz0Ajx7WO7Z5RqfgYg7ED1nRhA=="],@@ -1701,6 +1745,8 @@"@use-gesture/core": ["@use-gesture/[email protected]", "", {}, "sha512-WcINiDt8WjqBdUXye25anHiNxPc0VOrlT8F6LLkU6cycrOGUDyY/yyFmsg3k8i5OLvv25llc0QC45GhR/C8llw=="],+ "@vercel/oidc": ["@vercel/[email protected]", "", {}, "sha512-UycprH3T6n3jH0k44NHMa7pnFHGu/N05MjojYr+Mc6I7obkoLIJujSWwin1pCvdy/eOxrI/l3uDLQsmcrOb4ug=="],+"@vitest/expect": ["@vitest/[email protected]", "", { "dependencies": { "@types/chai": "^5.2.2", "@vitest/spy": "3.2.4", "@vitest/utils": "3.2.4", "chai": "^5.2.0", "tinyrainbow": "^2.0.0" } }, "sha512-Io0yyORnB6sikFlt8QW5K7slY4OjqNX9jmJQ02QDda8lyM6B5oNgVWoSoKPac8/kgnCUzuHQKrSLtu/uOqqrig=="],"@vitest/mocker": ["@vitest/[email protected]", "", { "dependencies": { "@vitest/spy": "3.2.4", "estree-walker": "^3.0.3", "magic-string": "^0.30.17" }, "peerDependencies": { "msw": "^2.4.9", "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0" }, "optionalPeers": ["msw", "vite"] }, "sha512-46ryTE9RZO/rfDd7pEqFl7etuyzekzEhUbTW3BvmeO/BcCMEgq59BKhek3dXDWgAj4oMK6OZi+vRr1wPW6qjEQ=="],@@ -1739,6 +1785,8 @@"agent-base": ["[email protected]", "", {}, "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ=="],+ "ai": ["[email protected]", "", { "dependencies": { "@ai-sdk/gateway": "3.0.133", "@ai-sdk/provider": "3.0.10", "@ai-sdk/provider-utils": "4.0.30", "@opentelemetry/api": "^1.9.0" }, "peerDependencies": { "zod": "^3.25.76 || ^4.1.8" } }, "sha512-9rAHnqU+AvxyqO6WgiWj7hQENX6AprHXZWZEdBWwgnA854D2Mje/PiTmbcFqO+2Cck1lII0NLRQJY9lmdSorMw=="],+"ajv": ["[email protected]", "", { "dependencies": { "fast-deep-equal": "^3.1.3", "fast-uri": "^3.0.1", "json-schema-traverse": "^1.0.0", "require-from-string": "^2.0.2" } }, "sha512-PlXPeEWMXMZ7sPYOHqmDyCJzcfNrUr3fGNKtezX14ykXOEIvyK81d+qydx89KY5O71FKMPaQ2vBfBFI5NHR63A=="],"ajv-draft-04": ["[email protected]", "", { "peerDependencies": { "ajv": "^8.5.0" }, "optionalPeers": ["ajv"] }, "sha512-mv00Te6nmYbRp5DCwclxtt7yV/joXJPGS7nM+97GdxvuttCOfgI3K4U25zboyeX0O+myI8ERluxQe5wljMmVIw=="],@@ -1819,6 +1867,8 @@"bignumber.js": ["[email protected]", "", {}, "sha512-Ko0uX15oIUS7wJ3Rb30Fs6SkVbLmPBAKdlm7q9+ak9bbIeFf0MwuBsQV6z7+X768/cHsfg+WlysDWJcmthjsjQ=="],+ "bitf": ["[email protected]", "", {}, "sha512-l3PytekTpJOq4W8QmXQQtDRI29Fp3ds2F9SfK+RTc9rvfDk0RFtFRjTuVSpYOyh0qITJP6walQq9TkxjY7gAFw=="],+"bl": ["[email protected]", "", { "dependencies": { "buffer": "^5.5.0", "inherits": "^2.0.4", "readable-stream": "^3.4.0" } }, "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w=="],"blake3-wasm": ["[email protected]", "", {}, "sha512-F1+K8EbfOZE49dtoPtmxUQrpXaBIl3ICvasLh+nJta0xkz+9kF/7uet9fLnwKqhDrmj6g+6K3Tw9yQPUg2ka5g=="],@@ -2045,6 +2095,8 @@"dezalgo": ["[email protected]", "", { "dependencies": { "asap": "^2.0.0", "wrappy": "1" } }, "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig=="],+ "diff": ["[email protected]", "", {}, "sha512-svtcdpS8CgJyqAjEQIXdb3OjhFVVYjzGAPO8WGCmRbrml64SPw/jJD4GoE98aR7r25A0XcgrK3F02yw9R/vhQw=="],+"dinero.js": ["[email protected]", "", { "dependencies": { "@dinero.js/calculator-number": "2.0.0-alpha.8", "@dinero.js/core": "2.0.0-alpha.8", "@dinero.js/currencies": "2.0.0-alpha.8" } }, "sha512-6bl+g6oh6iQ6vPR5Pd4qr7D+P5e51GYRUT3jl8HYqYeejYC5sd9OVTTbXC3WU7L25mAIbOm+diiTVz1rL4QLwg=="],"dir-glob": ["[email protected]", "", { "dependencies": { "path-type": "^4.0.0" } }, "sha512-WkrWp9GR4KXfKGYzOLmTuGVi1UWFfws377n9cc55/tb6DuqyF6pcQ5AbiHEshaDpY9v6oaSr2XCDidGmMwdzIA=="],@@ -2101,6 +2153,8 @@"environment": ["[email protected]", "", {}, "sha512-xUtoPkMggbz0MPyPiIWr1Kp4aeWJjDZ6SMvURhimjdZgsRuDplF5/s9hcgGhyXMhs+6vpnuoiZ2kFiu3FMnS8Q=="],+ "envsafe": ["[email protected]", "", {}, "sha512-51lek8h5btjXhXUDW//Pno7HPydM1WQzltOb1/ONRKdwB2QkfAURnN4Qn0cJ5LzGvX2Km1ReR2O3K3Bqq9sBMg=="],+"error-ex": ["[email protected]", "", { "dependencies": { "is-arrayish": "^0.2.1" } }, "sha512-sqQamAnR14VgCr1A618A3sGrygcpK+HEbenA/HiEAkkUwcZIIB/tgWqHFxWgOyDh4nB4JCRimh79dR5Ywc9MDQ=="],"error-stack-parser-es": ["[email protected]", "", {}, "sha512-5qucVt2XcuGMcEGgWI7i+yZpmpByQ8J1lHhcL7PwqCwu9FPP3VUXzT4ltHe5i2z9dePwEHcDVOAfSnHsOlCXRA=="],@@ -2159,6 +2213,8 @@"events": ["[email protected]", "", {}, "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q=="],+ "eventsource-parser": ["[email protected]", "", {}, "sha512-kJezFj9YFAMLeORyi7aCLxLbD5/qWMQnoMVlVPyHIll7lgRJCc3JVln9Vgl9nwQi0YkMnhdGTMNn7CkRRAptMg=="],+"execa": ["[email protected]", "", { "dependencies": { "cross-spawn": "^7.0.3", "get-stream": "^6.0.0", "human-signals": "^2.1.0", "is-stream": "^2.0.0", "merge-stream": "^2.0.0", "npm-run-path": "^4.0.1", "onetime": "^5.1.2", "signal-exit": "^3.0.3", "strip-final-newline": "^2.0.0" } }, "sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg=="],"expect-type": ["[email protected]", "", {}, "sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA=="],@@ -2213,6 +2269,8 @@"find-up": ["[email protected]", "", { "dependencies": { "locate-path": "^6.0.0", "path-exists": "^4.0.0" } }, "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng=="],+ "flat": ["[email protected]", "", { "bin": { "flat": "cli.js" } }, "sha512-/3FfIa8mbrg3xE7+wAhWeV+bd7L2Mof+xtZb5dRDKZ+wDvYJK4WDYeIOuOhre5Yv5aQObZrlbRmk3RTSiuQBtw=="],+"flat-cache": ["[email protected]", "", { "dependencies": { "flatted": "^3.2.9", "keyv": "^4.5.4" } }, "sha512-f7ccFPK3SXFHpx15UIGyRJ/FJQctuKZ0zVuN3frBo4HnK3cay9VEW0R6yPYFHC0AgqhukPzKjq22t5DmAyqGyw=="],"flatted": ["[email protected]", "", {}, "sha512-PjDse7RzhcPkIJwy5t7KPWQSZ9cAbzQXcafsetQoD7sOJRQlGikNbx7yZp2OotDnJyrDcbyRq3Ttb18iYOqkxA=="],@@ -2511,6 +2569,8 @@"json-refs": ["[email protected]", "", { "dependencies": { "commander": "~4.1.1", "graphlib": "^2.1.8", "js-yaml": "^3.13.1", "lodash": "^4.17.15", "native-promise-only": "^0.8.1", "path-loader": "^1.0.10", "slash": "^3.0.0", "uri-js": "^4.2.2" }, "bin": { "json-refs": "./bin/json-refs" } }, "sha512-0vOQd9eLNBL18EGl5yYaO44GhixmImes2wiYn9Z3sag3QnehWrYWlB9AFtMxCL2Bj3fyxgDYkxGFEU/chlYssw=="],+ "json-schema": ["[email protected]", "", {}, "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA=="],+"json-schema-to-typescript": ["[email protected]", "", { "dependencies": { "@apidevtools/json-schema-ref-parser": "^11.5.5", "@types/json-schema": "^7.0.15", "@types/lodash": "^4.17.7", "is-glob": "^4.0.3", "js-yaml": "^4.1.0", "lodash": "^4.17.21", "minimist": "^1.2.8", "prettier": "^3.2.5", "tinyglobby": "^0.2.9" }, "bin": { "json2ts": "dist/src/cli.js" } }, "sha512-Su9oK8DR4xCmDsLlyvadkXzX6+GGXJpbhwoLtOGArAG61dvbW4YQmSEno2y66ahpIdmLMg6YUf/QHLgiwvkrHQ=="],"json-schema-to-zod": ["[email protected]", "", { "bin": { "json-schema-to-zod": "dist/cjs/cli.js" } }, "sha512-uiHmWH21h9FjKJkRBntfVGTLpYlCZ1n98D0izIlByqQLqpmkQpNTBtfbdP04Na6+43lgsvrShFh2uWLkQDKJuQ=="],@@ -2801,6 +2861,10 @@"p-locate": ["[email protected]", "", { "dependencies": { "p-limit": "^3.0.2" } }, "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw=="],+ "p-queue": ["[email protected]", "", { "dependencies": { "eventemitter3": "^5.0.4", "p-timeout": "^7.0.0" } }, "sha512-7NED7xhQ74Ngp4JP/2e0VZHp7vSWfJfqeiR92jPgxsz6m0Se4P03YoTKa9dDXyZ3r6P616gUXttrB6nnHYKang=="],++ "p-timeout": ["[email protected]", "", {}, "sha512-AxTM2wDGORHGEkPCt8yqxOTMgpfbEHqF51f/5fJCmwFC3C/zNcGT63SymH2ttOAaiIws2zVg4+izQCjrakcwHg=="],+"p-try": ["[email protected]", "", {}, "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ=="],"pac-proxy-agent": ["[email protected]", "", { "dependencies": { "@tootallnate/quickjs-emscripten": "^0.23.0", "agent-base": "^7.1.2", "debug": "^4.3.4", "get-uri": "^6.0.1", "http-proxy-agent": "^7.0.0", "https-proxy-agent": "^7.0.6", "pac-resolver": "^7.0.1", "socks-proxy-agent": "^8.0.5" } }, "sha512-TEB8ESquiLMc0lV8vcd5Ql/JAKAoyzHFXaStwjkzpOpC5Yv+pIzLfHvjTSdf3vpa2bMiUQrg9i6276yn8666aA=="],@@ -2915,6 +2979,8 @@"raf": ["[email protected]", "", { "dependencies": { "performance-now": "^2.1.0" } }, "sha512-Sq4CW4QhwOHE8ucn6J34MqtZCeWFP2aQSmrlroYgqAV1PjStIhJXxYuTgUIfkEk7zTLjmIjLmU5q+fbD1NnOJA=="],+ "random-js": ["[email protected]", "", {}, "sha512-CRUyWmnzmZBA7RZSVGq0xMqmgCyPPxbiKNLFA5ud7KenojVX2s7Gv+V7eB52beKTPGxWRnVZ7D/tCIgYJJ8vNQ=="],+"randombytes": ["[email protected]", "", { "dependencies": { "safe-buffer": "^5.1.0" } }, "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ=="],"randomfill": ["[email protected]", "", { "dependencies": { "randombytes": "^2.0.5", "safe-buffer": "^5.1.0" } }, "sha512-87lcbR8+MhcWcUiQ+9e+Rwx8MyR2P7qnt15ynUlbm3TU/fjbgz4GsvfSUDTemtCCtVCqb4ZcEFlyPNTh9bBTLw=="],@@ -3411,7 +3477,7 @@"youch-core": ["[email protected]", "", { "dependencies": { "@poppinss/exception": "^1.2.2", "error-stack-parser-es": "^1.0.5" } }, "sha512-ho7XuGjLaJ2hWHoK8yFnsUGy2Y5uDpqSTq1FkHLK4/oqKtyUU1AFbOOxY4IpC9f0fTLjwYbslUz0Po5BpD1wrA=="],- "zod": ["[email protected]", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],+ "zod": ["[email protected]", "", {}, "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ=="],"zod-validation-error": ["[email protected]", "", { "peerDependencies": { "zod": "^3.25.0 || ^4.0.0" } }, "sha512-hmk+pkyKq7Q71PiWVSDUc3VfpzpvcRHZ3QPw9yEMVvmtCekaMeOHnbr3WbxfrgEnQTv6haGP4cmv0Ojmihzsxw=="],@@ -3575,6 +3641,8 @@"@kobalte/core/solid-presence": ["[email protected]", "", { "dependencies": { "@corvu/utils": "~0.4.0" }, "peerDependencies": { "solid-js": "^1.8" } }, "sha512-pWGtXUFWYYUZNbg5YpG5vkQJyOtzn2KXhxYaMx/4I+lylTLYkITOLevaCwMRN+liCVk0pqB6EayLWojNqBFECA=="],+ "@macro-inc/lexical-core/@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": ">=7.24.0 <7.24.7" } }, "sha512-603BddQMv3pUcr4U2dhujk83N2tTDVr/34wII2B6bJy6g+8WD6yUb11jszNs0gdi4PesVWl7ABt8nYMVpnLUcg=="],+"@macro-inc/lexical-core/nanoid": ["[email protected]", "", { "bin": { "nanoid": "bin/nanoid.js" } }, "sha512-ZUvP7KeBLe3OZ1ypw6dI/TzYJuvHP77IM4Ry73waSQTLn8/g8rpdjfyVAh7t1/+FjBtG4lCP42MEbDxOsRpBMw=="],"@macro/tauri/@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-orrrD74MBUyK8jOAD/r0+lfa1I2MO6I+vAkmAWzMYbCcgrN4lCrmK52gRFQq/JRxfYPfonkr4b0jcY7Olqdqbw=="],@@ -3683,6 +3751,8 @@"concurrently/yargs": ["[email protected]", "", { "dependencies": { "cliui": "^8.0.1", "escalade": "^3.1.1", "get-caller-file": "^2.0.5", "require-directory": "^2.1.1", "string-width": "^4.2.3", "y18n": "^5.0.5", "yargs-parser": "^21.1.1" } }, "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w=="],+ "chanfana/zod": ["[email protected]", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],+"csso/css-tree": ["[email protected]", "", { "dependencies": { "mdn-data": "2.0.28", "source-map-js": "^1.0.1" } }, "sha512-OA0mILzGc1kCOCSJerOeqDxDQ4HOh+G8NbOJFOTgOCzpw7fCBubk0fEyxp8AgOL/jvLgYA/uV0cMbe43ElF1JA=="],"datadog-metrics/debug": ["[email protected]", "", { "dependencies": { "ms": "2.0.0" } }, "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g=="],@@ -3753,6 +3823,8 @@"katex/commander": ["[email protected]", "", {}, "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww=="],+ "knip/zod": ["[email protected]", "", {}, "sha512-rftlrkhHZOcjDwkGlnUtZZkvaPHCsDATp4pGpuOOMDaTdDDXF91wuVDJoWoPsKX/3YPQ5fHuF3STjcYyKr+Qhg=="],+"lexical-service/@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-ClIbNe36lawluuvq3+YYhnIN2CELi+6q8NpnM7PYp4hBn/TatfboPgVSm2rwKRfnV2M+Ty9GWDFI64KEe+kysA=="],"lexical-service/nanoid": ["[email protected]", "", { "bin": { "nanoid": "bin/nanoid.js" } }, "sha512-ZUvP7KeBLe3OZ1ypw6dI/TzYJuvHP77IM4Ry73waSQTLn8/g8rpdjfyVAh7t1/+FjBtG4lCP42MEbDxOsRpBMw=="],@@ -3993,6 +4065,8 @@"@isaacs/cliui/wrap-ansi/ansi-styles": ["[email protected]", "", {}, "sha512-4Dj6M28JB+oAH8kFkTLUo+a2jwOFkuqb3yucU0CANcRRUbxS0cP0nZYCGjcc3BNXwRIsUVmDGgzawme7zvJHvg=="],+ "@macro-inc/lexical-core/@types/node/undici-types": ["[email protected]", "", {}, "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg=="],+"@macro/tauri/@types/node/undici-types": ["[email protected]", "", {}, "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ=="],"@orval/core/chalk/supports-color": ["[email protected]", "", { "dependencies": { "has-flag": "^4.0.0" } }, "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw=="],diff --git a/js/bunfig.toml b/js/bunfig.tomlindex 57f75baf9a..c1d403ac40 100644--- a/js/bunfig.toml+++ b/js/bunfig.toml@@ -1,2 +1,5 @@[install]linker = "hoisted"++[loader]+".md" = "text"diff --git a/js/lexical-core/biome.jsonc b/js/lexical-core/biome.jsoncnew file mode 100644index 0000000000..23bd71e72a--- /dev/null+++ b/js/lexical-core/biome.jsonc@@ -0,0 +1,15 @@+{+ "$schema": "https://biomejs.dev/schemas/2.4.12/schema.json",+ "extends": ["../app/biome.jsonc"],+ "vcs": {+ "root": "../.."+ },+ "files": {+ "includes": [+ "**/*.{ts,tsx,js,jsx}",+ "!**/*.d.ts",+ "!**/node_modules",+ "!markdown-golden*.ts"+ ]+ }+}diff --git a/js/lexical-core/index.ts b/js/lexical-core/index.tsindex 584377d152..6bbc4569b4 100644--- a/js/lexical-core/index.ts+++ b/js/lexical-core/index.ts@@ -36,5 +36,6 @@ export * from './plugins/nodeIdPlugin';export * from './plugins/peerIdPlugin';export * from './transformers';+export { toXml } from './transformers/xml';export * from './utils/markdown-state';export * from './utils/mentions';diff --git a/js/lexical-core/markdown-loro-snapshot.ts b/js/lexical-core/markdown-loro-snapshot.tsindex 4faa639b70..b96834b413 100644--- a/js/lexical-core/markdown-loro-snapshot.ts+++ b/js/lexical-core/markdown-loro-snapshot.ts@@ -1,6 +1,6 @@-import { Mirror, type InferType } from '../loro-mirror/packages/core/src';-import { LoroDoc } from 'loro-crdt';import type { SerializedEditorState } from 'lexical';+import { LoroDoc } from 'loro-crdt';+import { type InferType, Mirror } from '../loro-mirror/packages/core/src';import { MARKDOWN_GOLDEN } from './markdown-golden.1';import { MARKDOWN_LORO_SCHEMA } from './markdown-loro-schema';import { markdownToSerializedEditorStateWithIds } from './utils/markdown-state';diff --git a/js/lexical-core/nodes/CustomCodeNode.ts b/js/lexical-core/nodes/CustomCodeNode.tsindex 1b3c3ab86a..17dc63287e 100644--- a/js/lexical-core/nodes/CustomCodeNode.ts+++ b/js/lexical-core/nodes/CustomCodeNode.ts@@ -68,15 +68,15 @@ export type SerializedCustomCodeNode = Spread<// Re-export for backward compatibilityexport {- SupportedLanguages,- type SupportedLanguage,DEFAULT_LANGUAGE,- LanguageDefinitions,- type LanguageDefinition,+ getSupportedLanguageFromFileExtension,+ isNormalizedLanguage,isSupportedLanguage,+ type LanguageDefinition,+ LanguageDefinitions,normalizedLanguage,- isNormalizedLanguage,- getSupportedLanguageFromFileExtension,+ type SupportedLanguage,+ SupportedLanguages,};PrismTokenizer.defaultLanguage = DEFAULT_LANGUAGE;diff --git a/js/lexical-core/nodes/ImageNode.ts b/js/lexical-core/nodes/ImageNode.tsindex 73471558f0..83e5175027 100644--- a/js/lexical-core/nodes/ImageNode.ts+++ b/js/lexical-core/nodes/ImageNode.ts@@ -138,7 +138,7 @@ export class ImageNode extends MediaNode<{ alt: string }> {static importDOM(): DOMConversionMap<HTMLElement> | null {return {- img: (domNode: HTMLImageElement) => {+ img: (domNode: HTMLElement) => {const src = domNode.getAttribute('src');const alt = domNode.getAttribute('alt');const width = domNode.getAttribute('width');@@ -176,7 +176,7 @@ export class ImageNode extends MediaNode<{ alt: string }> {}return null;},- div: (domNode: HTMLDivElement) => {+ div: (domNode: HTMLElement) => {const img = domNode.querySelector('img');if (!img) return null;diff --git a/js/lexical-core/nodes/MediaNode.ts b/js/lexical-core/nodes/MediaNode.tsindex a7b7fd9296..f924559ad6 100644--- a/js/lexical-core/nodes/MediaNode.ts+++ b/js/lexical-core/nodes/MediaNode.ts@@ -201,7 +201,8 @@ export abstract class MediaNode<return { element: wrapper };}- decorate(): DecoratorComponent<MediaInfo & T> | undefined {+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ decorate(): DecoratorComponent<any> | undefined {return undefined;}@@ -236,7 +237,7 @@ export abstract class MediaNode<}}-export function createMediaTypeGuard<T extends MediaNode>(+export function createMediaTypeGuard<T extends MediaNode<any>>(nodeClass: new (...args: any[]) => T) {return (node: LexicalNode): node is T => node instanceof nodeClass;diff --git a/js/lexical-core/nodes/VideoNode.ts b/js/lexical-core/nodes/VideoNode.tsindex d4d36a4291..68abd0f7c1 100644--- a/js/lexical-core/nodes/VideoNode.ts+++ b/js/lexical-core/nodes/VideoNode.ts@@ -142,7 +142,7 @@ export class VideoNode extends MediaNode<{ controls: boolean }> {static importDOM(): DOMConversionMap<HTMLElement> | null {return {- video: (domNode: HTMLVideoElement) => {+ video: (domNode: HTMLElement) => {const src = domNode.getAttribute('src');const controls = domNode.hasAttribute('controls');const width = domNode.getAttribute('width');@@ -180,7 +180,7 @@ export class VideoNode extends MediaNode<{ controls: boolean }> {}return null;},- div: (domNode: HTMLDivElement) => {+ div: (domNode: HTMLElement) => {const video = domNode.querySelector('video');if (!video) return null;diff --git a/js/lexical-core/package.json b/js/lexical-core/package.jsonindex 094d8cbbf3..710ae78baf 100644--- a/js/lexical-core/package.json+++ b/js/lexical-core/package.json@@ -10,9 +10,10 @@"keywords": [],"author": "seamus","dependencies": {- "nanoid": "^5.1.5",- "lexical": "0.45.0",- "loro-crdt": "1.13.3",+ "@ai-sdk/anthropic": "^3.0.85",+ "@ai-sdk/cerebras": "^2.0.57",+ "@ai-sdk/google": "^3.0.83",+ "@ai-sdk/openai": "^3.0.73","@lexical/code": "0.45.0","@lexical/headless": "0.45.0","@lexical/link": "0.45.0",@@ -21,9 +22,26 @@"@lexical/markdown": "0.45.0","@lexical/rich-text": "0.45.0","@lexical/table": "0.45.0",- "@lexical/utils": "0.45.0"+ "@lexical/utils": "0.45.0",+ "@types/node": "^25.9.3",+ "ai": "^6.0.207",+ "bitf": "^1.0.2",+ "diff": "^9.0.0",+ "envsafe": "^2.0.3",+ "fast-xml-parser": "^4.5.6",+ "flat": "^6.0.1",+ "lexical": "0.45.0",+ "loro-crdt": "1.13.3",+ "nanoid": "^5.1.5",+ "p-queue": "^9.3.0",+ "random-js": "^2.1.0",+ "ts-pattern": "^5.9.0",+ "zod": "^4.4.3"},"devDependencies": {- "@types/bun": "^1.3.14"+ "@types/bun": "^1.3.14",+ "@types/diff": "^8.0.0",+ "@types/flat": "^5.0.5",+ "@types/yargs": "^17.0.35"}}diff --git a/js/lexical-core/tests/xml-serialize.test.ts b/js/lexical-core/tests/xml-serialize.test.tsnew file mode 100644index 0000000000..e51ff7f2f6--- /dev/null+++ b/js/lexical-core/tests/xml-serialize.test.ts@@ -0,0 +1,333 @@+import { createHeadlessEditor } from '@lexical/headless';+import { $createListItemNode, $createListNode } from '@lexical/list';+import { $createMarkNode } from '@lexical/mark';+import { $createHeadingNode, $createQuoteNode } from '@lexical/rich-text';+import {+ $createLineBreakNode,+ $createParagraphNode,+ $createTabNode,+ $createTextNode,+ $getRoot,+} from 'lexical';+import { describe, expect, it } from 'vitest';+import { NodeReplacements, SupportedNodeTypes } from '../node-list';+import { $createCustomCodeNode } from '../nodes/CustomCodeNode';+import { $createEquationNode } from '../nodes/EquationNode';+import { $createHtmlRenderNode } from '../nodes/HtmlRenderNode';+import { $createImageNode } from '../nodes/ImageNode';+import { $createUserMentionNode } from '../nodes/UserMentionNode';+import { toXml } from '../transformers/xml';++function makeEditor() {+ return createHeadlessEditor({+ nodes: [...SupportedNodeTypes, ...NodeReplacements],+ });+}++function serialize(build: () => void): string {+ const editor = makeEditor();+ editor.update(+ () => {+ $getRoot().clear();+ build();+ },+ { discrete: true }+ );+ return toXml(editor.getEditorState().toJSON());+}++describe('xml serialization', () => {+ it('empty paragraph', () => {+ expect(+ serialize(() => {+ $getRoot().append($createParagraphNode());+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p/>+ </doc>"+ `);+ });++ it('paragraph with plain text', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append($createTextNode('hello world'));+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <t>hello world</t>+ </p>+ </doc>"+ `);+ });++ it('paragraph with bold and italic text', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append(+ $createTextNode('plain '),+ $createTextNode('bold').toggleFormat('bold'),+ $createTextNode(' '),+ $createTextNode('italic').toggleFormat('italic')+ );+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <t>plain </t>+ <t bold="true">bold</t>+ <t> </t>+ <t italic="true">italic</t>+ </p>+ </doc>"+ `);+ });++ it('heading', () => {+ expect(+ serialize(() => {+ const h = $createHeadingNode('h1');+ h.append($createTextNode('My Title'));+ $getRoot().append(h);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <h1>+ <t>My Title</t>+ </h1>+ </doc>"+ `);+ });++ it('bullet list', () => {+ expect(+ serialize(() => {+ const list = $createListNode('bullet');+ for (const text of ['one', 'two', 'three']) {+ const li = $createListItemNode();+ li.append($createTextNode(text));+ list.append(li);+ }+ $getRoot().append(list);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <ul>+ <li>+ <t>one</t>+ </li>+ <li value="2">+ <t>two</t>+ </li>+ <li value="3">+ <t>three</t>+ </li>+ </ul>+ </doc>"+ `);+ });++ it('paragraph with line break', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append(+ $createTextNode('line one'),+ $createLineBreakNode(),+ $createTextNode('line two')+ );+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <t>line one</t>+ <br/>+ <t>line two</t>+ </p>+ </doc>"+ `);+ });++ it('blockquote', () => {+ expect(+ serialize(() => {+ const q = $createQuoteNode();+ q.append($createTextNode('a wise thing'));+ $getRoot().append(q);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <blockquote>+ <t>a wise thing</t>+ </blockquote>+ </doc>"+ `);+ });++ it('highlight (mark) drops the comment-thread ids', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ const m = $createMarkNode(['thread-1']);+ m.append($createTextNode('important'));+ p.append(m);+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <mark>+ <t>important</t>+ </mark>+ </p>+ </doc>"+ `);+ });++ it('code block flattens prism tokens back into raw source', () => {+ expect(+ serialize(() => {+ const code = $createCustomCodeNode('typescript');+ code.setCode('typescript', 'const x = 1 < 2;\nfoo(x);');+ $getRoot().append(code);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <code language="typescript">const x = 1 < 2;+ foo(x);</code>+ </doc>"+ `);+ });++ it('equation carries TeX as text content (special chars escaped)', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append($createEquationNode('a < b & c > "d"', true));+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <equation inline="true">a < b & c > "d"</equation>+ </p>+ </doc>"+ `);+ });++ it('image redacts a data: URI payload but keeps the prefix', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append(+ $createImageNode({+ srcType: 'url',+ url: 'data:image/png;base64,AAAA',+ alt: 'a duck',+ })+ );+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <image alt="a duck" src="data:image/png;base64,..."/>+ </p>+ </doc>"+ `);+ });++ it('image keeps a real url as src', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append(+ $createImageNode({+ srcType: 'url',+ url: 'https://ex.com/duck.png',+ alt: 'a duck',+ })+ );+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <image alt="a duck" src="https://ex.com/duck.png"/>+ </p>+ </doc>"+ `);+ });++ it('user mention keeps userId + email', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append($createUserMentionNode({ userId: 'u_1', email: '[email protected]' }));+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <user-mention userId="u_1" email="[email protected]"/>+ </p>+ </doc>"+ `);+ });++ it('tab serializes minimally', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append($createTextNode('a'), $createTabNode(), $createTextNode('b'));+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <t>a</t>+ <tab/>+ <t>b</t>+ </p>+ </doc>"+ `);+ });++ it('html-render carries its html as escaped text content', () => {+ expect(+ serialize(() => {+ const p = $createParagraphNode();+ p.append($createHtmlRenderNode({ html: '<b>x</b>' }));+ $getRoot().append(p);+ })+ ).toMatchInlineSnapshot(`+ "+ <doc>+ <p>+ <html-render><b>x</b></html-render>+ </p>+ </doc>"+ `);+ });+});diff --git a/js/lexical-core/tests/xml.test.ts b/js/lexical-core/tests/xml.test.tsnew file mode 100644index 0000000000..6c61b34283--- /dev/null+++ b/js/lexical-core/tests/xml.test.ts@@ -0,0 +1,173 @@+import { describe, expect, it } from 'vitest';+import { type SerializedEditorState, toXml } from '../transformers/xml';++function root(children: any[]): SerializedEditorState {+ return {+ root: {+ children,+ direction: 'ltr',+ format: '',+ indent: 0,+ type: 'root',+ version: 1,+ $: {+ documentMetadata: { version: 1.4, environmentTags: null },+ id: 'root',+ },+ },+ } as SerializedEditorState;+}++function text(value: string, format = 0, id = 't') {+ return {+ detail: 0,+ format,+ mode: 'normal',+ style: '',+ text: value,+ type: 'text',+ version: 1,+ $: { id },+ };+}++function paragraph(children: any[], id = 'p') {+ return {+ children,+ direction: 'ltr',+ format: '',+ indent: 0,+ type: 'paragraph',+ version: 1,+ $: { id },+ textFormat: 0,+ textStyle: '',+ };+}++const FORMAT = {+ bold: 1,+ italic: 2,+ strikethrough: 4,+ underline: 8,+ code: 16,+} as const;++describe('xml', () => {+ it('serializes an empty paragraph as self-closing', () => {+ const state = root([paragraph([], 'p1')]);+ const xml = toXml(state);+ expect(xml).toContain('<p id="p1"/>');+ });++ it('serializes table colWidths', () => {+ const cell = (txt: string, id: string) => ({+ children: [paragraph([text(txt, 0, `t-${id}`)], `p-${id}`)],+ direction: 'ltr',+ format: '',+ indent: 0,+ type: 'tablecell',+ version: 1,+ $: { id },+ backgroundColor: null,+ colSpan: 1,+ headerState: 0,+ rowSpan: 1,+ });+ const row = (cells: any[], id: string) => ({+ children: cells,+ direction: 'ltr',+ format: '',+ indent: 0,+ type: 'tablerow',+ version: 1,+ $: { id },+ });+ const state = root([+ {+ children: [+ row([cell('a', 'c1'), cell('b', 'c2')], 'r1'),+ row([cell('c', 'c3'), cell('d', 'c4')], 'r2'),+ ],+ direction: 'ltr',+ format: '',+ indent: 0,+ type: 'table',+ version: 1,+ $: { id: 't1' },+ colWidths: [120, 120],+ },+ ]);+ expect(toXml(state)).toContain('colWidths="120,120"');+ });++ it('stamps each table cell with its 0-based row/column', () => {+ const cell = (txt: string, id: string) => ({+ children: [paragraph([text(txt, 0, `t-${id}`)], `p-${id}`)],+ direction: 'ltr',+ format: '',+ indent: 0,+ type: 'tablecell',+ version: 1,+ $: { id },+ backgroundColor: null,+ colSpan: 1,+ headerState: 0,+ rowSpan: 1,+ });+ const row = (cells: ReturnType<typeof cell>[], id: string) => ({+ children: cells,+ direction: 'ltr',+ format: '',+ indent: 0,+ type: 'tablerow',+ version: 1,+ $: { id },+ });+ const state = root([+ {+ children: [+ row([cell('a', 'c1'), cell('b', 'c2')], 'r1'),+ row([cell('c', 'c3'), cell('d', 'c4')], 'r2'),+ ],+ direction: 'ltr',+ format: '',+ indent: 0,+ type: 'table',+ version: 1,+ $: { id: 't1' },+ },+ ]);+ const xml = toXml(state);+ expect(xml).toContain('id="c1" row="0" column="0"');+ expect(xml).toContain('id="c2" row="0" column="1"');+ expect(xml).toContain('id="c3" row="1" column="0"');+ expect(xml).toContain('id="c4" row="1" column="1"');+ });++ it('serializes a horizontal rule as self-closing', () => {+ const state = root([+ { type: 'horizontalrule', version: 1, $: { id: 'hr1' } },+ ]);+ expect(toXml(state)).toContain('<hr id="hr1"/>');+ });++ it('emits the expected XML for a simple known input (snapshot)', () => {+ const state = root([+ paragraph([text('hi ', 0, 'ta'), text('bold', FORMAT.bold, 'tb')], 'p1'),+ ]);+ const xml = toXml(state);+ expect(xml).toContain('<doc>');+ expect(xml).toContain('<p id="p1">');+ expect(xml).toContain('<t id="ta">hi </t>');+ expect(xml).toContain('<t id="tb" bold="true">bold</t>');+ });++ it('escapes special characters in text and attributes', () => {+ const state = root([paragraph([text('a < b & c > d "q"', 0, 't1')], 'p1')]);+ const xml = toXml(state);+ expect(xml).toContain('<');+ expect(xml).toContain('&');+ expect(xml).toContain('>');+ });+});diff --git a/js/lexical-core/transformers/image.ts b/js/lexical-core/transformers/image.tsindex f1e9865d07..cc5b7dadb0 100644--- a/js/lexical-core/transformers/image.ts+++ b/js/lexical-core/transformers/image.ts@@ -36,9 +36,9 @@ export const I_IMAGE_CONSTRAINED: ElementTransformer = {return `<m-image>${data}</m-image>`;},- replace: (parent: ElementNode, _, match: RegExpMatchArray) => {+ replace: (parent: ElementNode, _, match: string[]) => {try {- const data = JSON.parse(match[1]);+ const data = JSON.parse(match[1] ?? '');if (!data.url) throw new Error('Missing url field');const imageNode = $createImageNode({diff --git a/js/lexical-core/transformers/video.ts b/js/lexical-core/transformers/video.tsindex 19bbcd0908..fef81cc509 100644--- a/js/lexical-core/transformers/video.ts+++ b/js/lexical-core/transformers/video.ts@@ -30,9 +30,9 @@ export const I_VIDEO: ElementTransformer = {return `<m-video>${data}</m-video>`;},- replace: (parent: ElementNode, _, match: RegExpMatchArray) => {+ replace: (parent: ElementNode, _, match: string[]) => {try {- const data = JSON.parse(match[1]);+ const data = JSON.parse(match[1] ?? '');if (typeof data.url !== 'string' || !data.url) {throw new Error('Missing or invalid url field');}diff --git a/js/lexical-core/transformers/watermark.ts b/js/lexical-core/transformers/watermark.tsindex 097d32cfb5..71609f4785 100644--- a/js/lexical-core/transformers/watermark.ts+++ b/js/lexical-core/transformers/watermark.ts@@ -24,9 +24,9 @@ export const I_WATERMARK: ElementTransformer = {return `<m-watermark>${data}</m-watermark>`;},- replace: (parent: ElementNode, _, match: RegExpMatchArray) => {+ replace: (parent: ElementNode, _, match: string[]) => {try {- const data = JSON.parse(match[1]);+ const data = JSON.parse(match[1] ?? '');for (const field of ['content']) {if (!(field in data)) throw new Error(`Missing field ${field}`);}diff --git a/js/lexical-core/transformers/xml/codecs.ts b/js/lexical-core/transformers/xml/codecs.tsnew file mode 100644index 0000000000..c137abdfcc--- /dev/null+++ b/js/lexical-core/transformers/xml/codecs.ts@@ -0,0 +1,372 @@+import { bitflag, defineBitflags } from 'bitf';+import { flatten } from 'flat';+import { match } from 'ts-pattern';+import type {+ CustomCodeNode,+ KnownNode,+ SerNode,+ TableCellNode,+ TableNode,+ TableRowNode,+ TextNode,+ UnknownNode,+} from './nodes';++export interface FxpElementNode {+ ':@'?: Record<string, string>;+ [key: string]: FxpNode[] | Record<string, string> | undefined;+}+export type FxpNode = FxpElementNode | { '#text': string };++const FORMAT_FLAGS = defineBitflags({+ bold: 1 << 0,+ italic: 1 << 1,+ strikethrough: 1 << 2,+ underline: 1 << 3,+ code: 1 << 4,+ subscript: 1 << 5,+ superscript: 1 << 6,+});++function encodeFormat(format: number): Record<string, string> {+ return Object.fromEntries(+ Object.entries(FORMAT_FLAGS)+ .filter(([, bit]) => bitflag(format).has(bit))+ .map(([name]) => [name, 'true'])+ );+}++function el(+ tag: string,+ kids: FxpNode[],+ attrs: Record<string, string> = {}+): FxpNode {+ return { [tag]: kids, ...(Object.keys(attrs).length && { ':@': attrs }) };+}++function nodeAttrs(+ node: { $?: { id: string } },+ extra: Record<string, string> = {}+): Record<string, string> {+ return node.$?.id ? { id: node.$.id, ...extra } : extra;+}++function container(+ tag: string,+ node: { $?: { id: string }; children: SerNode[] },+ extra: Record<string, string> = {}+): FxpNode {+ return el(tag, node.children.map(serializeNode), nodeAttrs(node, extra));+}++const KNOWN_TYPES: Record<KnownNode['type'], 1> = {+ text: 1,+ linebreak: 1,+ paragraph: 1,+ heading: 1,+ quote: 1,+ list: 1,+ listitem: 1,+ table: 1,+ tablerow: 1,+ tablecell: 1,+ horizontalrule: 1,+ 'date-mention': 1,+ link: 1,+ autolink: 1,+ mark: 1,+ tab: 1,+ 'classed-block': 1,+ 'custom-code': 1,+ equation: 1,+ image: 1,+ video: 1,+ 'html-render': 1,+ 'document-card': 1,+ 'user-mention': 1,+ 'document-mention': 1,+ 'contact-mention': 1,+ 'group-mention': 1,+ 'pr-mention': 1,+ 'theme-mention': 1,+ 'unknown-mention': 1,+};++function isKnownNode(node: SerNode): node is KnownNode {+ return node.type in KNOWN_TYPES;+}++// Lexical bookkeeping fields that carry no meaning for a reader of the doc.+const NOISE_KEYS = new Set(['version', 'direction', 'indent']);++function serUnknown(n: UnknownNode): FxpNode {+ const { type, children, ...rest } = n;+ // this makes an object of attributes into dotted notation+ // e.g. { $: { id: 'foo' } } -> { '$.id': 'foo' }+ // since we are unidirectional rn this is fine+ const flat = flatten(rest, { safe: true }) as Record<string, unknown>;+ const attrs: Record<string, string> = {};+ if (n.$?.id) attrs.id = n.$.id;+ for (const [k, v] of Object.entries(flat)) {+ if (k.startsWith('$') || NOISE_KEYS.has(k)) continue;+ if (v === null || v === undefined) {+ attrs[k] = 'null';+ continue;+ }+ attrs[k === 'id' ? '_id' : k] =+ typeof v === 'object' ? JSON.stringify(v) : String(v);+ }+ return el(type, (children ?? []).map(serializeNode), attrs);+}++/** Flatten a code block's per-token `code-highlight` / `linebreak` children back+ * into one raw source string. Raw code is better for the LLMM than prism. */+function serializeCode(n: CustomCodeNode): FxpNode {+ const code = (n.children ?? [])+ .map((c) => (c.type === 'linebreak' ? '\n' : ((c as TextNode).text ?? '')))+ .join('');+ return el(+ 'code',+ code ? [{ '#text': code }] : [],+ nodeAttrs(n, { ...(n.language && { language: n.language }) })+ );+}++/** A leaf node carrying only text content (equation TeX, raw html). */+function textLeaf(+ tag: string,+ n: { $?: { id: string } },+ text: string,+ extra: Record<string, string> = {}+): FxpNode {+ return el(tag, text ? [{ '#text': text }] : [], nodeAttrs(n, extra));+}++/** Keep an image/video src but redact a `data:` URI's payload to `...` so the+ * model knows media is present without ingesting a base64 blob. */+function redactedSrc(url: string): string {+ if (!url.startsWith('data:')) return url;+ const comma = url.indexOf(',');+ const prefix = comma === -1 ? 'data:' : url.slice(0, comma + 1);+ return `${prefix}...`;+}++/** Serialize a table, stamping each cell with its 0-based `row`/`column` so the+ * model can address `setCell` by coordinate without counting `<tr>`/`<td>`. */+function serializeTable(n: TableNode): FxpNode {+ // Rows/cells nest structurally inside a table, so the casts are safe; the+ // discriminated union can't narrow them (UnknownNode.type widens to string).+ let r = 0;+ const rows = n.children.map((child) => {+ if (child.type !== 'tablerow') return serializeNode(child);+ const row = child as TableRowNode;+ const rowIdx = r++;+ let c = 0;+ const cells = row.children.map((child) => {+ if (child.type !== 'tablecell') return serializeNode(child);+ const cell = child as TableCellNode;+ const colIdx = c++;+ return el(+ 'td',+ cell.children.map(serializeNode),+ nodeAttrs(cell, {+ row: String(rowIdx),+ column: String(colIdx),+ ...(cell.headerState !== 0 && {+ headerState: String(cell.headerState),+ }),+ ...(cell.colSpan !== 1 && { colSpan: String(cell.colSpan) }),+ ...(cell.rowSpan !== 1 && { rowSpan: String(cell.rowSpan) }),+ ...(cell.backgroundColor && {+ backgroundColor: cell.backgroundColor,+ }),+ })+ );+ });+ return el(+ 'tr',+ cells,+ nodeAttrs(+ row,+ row.height !== undefined ? { height: String(row.height) } : {}+ )+ );+ });+ return el(+ 'table',+ rows,+ nodeAttrs(n, {+ ...(n.colWidths && { colWidths: n.colWidths.join(',') }),+ ...(n.rowStriping && { rowStriping: 'true' }),+ })+ );+}++export function serializeNode(node: SerNode): FxpNode {+ if (!isKnownNode(node)) return serUnknown(node as UnknownNode);+ return match(node)+ .with({ type: 'text' }, (n) => {+ const extra: Record<string, string> = { ...encodeFormat(n.format) };+ if (n.style) extra.style = n.style;+ if (n.detail) extra.detail = String(n.detail);+ if (n.mode && n.mode !== 'normal') extra.mode = n.mode;+ return el('t', n.text ? [{ '#text': n.text }] : [], nodeAttrs(n, extra));+ })+ .with({ type: 'linebreak' }, () => el('br', []))+ .with({ type: 'paragraph' }, (n) => container('p', n))+ .with({ type: 'heading' }, (n) => container(n.tag, n))+ .with({ type: 'quote' }, (n) => container('blockquote', n))+ .with({ type: 'listitem' }, (n) =>+ container('li', n, {+ ...(n.value !== 1 && { value: String(n.value) }),+ ...(n.checked && { checked: 'true' }),+ })+ )+ .with({ type: 'list' }, (n) =>+ container(n.listType === 'number' ? 'ol' : 'ul', n, {+ ...(n.listType === 'check' && { listType: 'check' }),+ ...(n.start !== 1 && { start: String(n.start) }),+ })+ )+ .with({ type: 'table' }, (n) => serializeTable(n))+ .with({ type: 'tablerow' }, (n) =>+ container(+ 'tr',+ n,+ n.height !== undefined ? { height: String(n.height) } : {}+ )+ )+ .with({ type: 'tablecell' }, (n) =>+ container('td', n, {+ ...(n.headerState !== 0 && { headerState: String(n.headerState) }),+ ...(n.colSpan !== 1 && { colSpan: String(n.colSpan) }),+ ...(n.rowSpan !== 1 && { rowSpan: String(n.rowSpan) }),+ ...(n.backgroundColor && { backgroundColor: n.backgroundColor }),+ })+ )+ .with({ type: 'horizontalrule' }, (n) => el('hr', [], nodeAttrs(n)))+ .with({ type: 'date-mention' }, (n) =>+ el(+ 'date-mention',+ [],+ nodeAttrs(n, {+ date: n.date,+ displayFormat: n.displayFormat,+ ...(n.mentionUuid !== undefined && { mentionUuid: n.mentionUuid }),+ })+ )+ )+ .with({ type: 'link' }, { type: 'autolink' }, (n) =>+ container('a', n, {+ href: n.url ?? '',+ ...(n.rel && { rel: n.rel }),+ ...(n.target && { target: n.target }),+ ...(n.title && { title: n.title }),+ })+ )+ .with({ type: 'mark' }, (n) => container('mark', n))+ .with({ type: 'tab' }, (n) => el('tab', [], nodeAttrs(n)))+ .with({ type: 'classed-block' }, (n) =>+ container('classed-block', n, {+ tag: n.tag,+ ...(n.classes?.length && { classes: n.classes.join(' ') }),+ })+ )+ .with({ type: 'custom-code' }, (n) => serializeCode(n))+ .with({ type: 'equation' }, (n) =>+ textLeaf('equation', n, n.equation, {+ ...(n.inline && { inline: 'true' }),+ })+ )+ .with({ type: 'image' }, (n) =>+ el(+ 'image',+ [],+ nodeAttrs(n, {+ ...(n.alt && { alt: n.alt }),+ ...(n.url && { src: redactedSrc(n.url) }),+ })+ )+ )+ .with({ type: 'video' }, (n) =>+ el(+ 'video',+ [],+ nodeAttrs(n, {+ ...(n.url && { src: redactedSrc(n.url) }),+ ...(n.controls && { controls: 'true' }),+ })+ )+ )+ .with({ type: 'html-render' }, (n) => textLeaf('html-render', n, n.html))+ .with({ type: 'document-card' }, (n) =>+ el(+ 'document-card',+ [],+ nodeAttrs(n, {+ ...(n.documentId && { documentId: n.documentId }),+ ...(n.documentName && { name: n.documentName }),+ })+ )+ )+ .with({ type: 'user-mention' }, (n) =>+ el(+ 'user-mention',+ [],+ nodeAttrs(n, {+ ...(n.userId && { userId: n.userId }),+ ...(n.email && { email: n.email }),+ })+ )+ )+ .with({ type: 'document-mention' }, (n) =>+ el(+ 'document-mention',+ [],+ nodeAttrs(n, {+ ...(n.documentId && { documentId: n.documentId }),+ ...(n.documentName && { name: n.documentName }),+ })+ )+ )+ .with({ type: 'contact-mention' }, (n) =>+ el(+ 'contact-mention',+ [],+ nodeAttrs(n, {+ ...(n.contactId && { contactId: n.contactId }),+ ...(n.name && { name: n.name }),+ ...(n.emailOrDomain && { email: n.emailOrDomain }),+ ...(n.isCompany && { isCompany: 'true' }),+ })+ )+ )+ .with({ type: 'group-mention' }, (n) =>+ el(+ 'group-mention',+ [],+ nodeAttrs(n, { ...(n.groupAlias && { alias: n.groupAlias }) })+ )+ )+ .with({ type: 'pr-mention' }, (n) =>+ el(+ 'pr-mention',+ [],+ nodeAttrs(n, {+ ...(n.id && { prId: n.id }),+ ...(n.label && { label: n.label }),+ })+ )+ )+ .with({ type: 'theme-mention' }, (n) =>+ el('theme-mention', [], nodeAttrs(n, { ...(n.name && { name: n.name }) }))+ )+ .with({ type: 'unknown-mention' }, (n) =>+ el(+ 'unknown-mention',+ [],+ nodeAttrs(n, { ...(n.name && { name: n.name }) })+ )+ )+ .exhaustive();+}diff --git a/js/lexical-core/transformers/xml/index.ts b/js/lexical-core/transformers/xml/index.tsnew file mode 100644index 0000000000..8d87f7ee7f--- /dev/null+++ b/js/lexical-core/transformers/xml/index.ts@@ -0,0 +1,21 @@+import { XMLBuilder } from 'fast-xml-parser';+import type { SerializedEditorState } from 'lexical';+import { serializeNode } from './codecs';+import type { SerNode } from './nodes';++export type { SerializedEditorState } from 'lexical';++const builder = new XMLBuilder({+ ignoreAttributes: false,+ attributeNamePrefix: '',+ preserveOrder: true,+ textNodeName: '#text',+ suppressEmptyNode: true,+ format: true,+ indentBy: ' ',+});++export function toXml(state: SerializedEditorState): string {+ const root = state.root as unknown as { children: SerNode[] };+ return builder.build([{ doc: root.children.map(serializeNode) }]);+}diff --git a/js/lexical-core/transformers/xml/nodes.ts b/js/lexical-core/transformers/xml/nodes.tsnew file mode 100644index 0000000000..d90e94d754--- /dev/null+++ b/js/lexical-core/transformers/xml/nodes.ts@@ -0,0 +1,248 @@+export type Id = { $?: { id: string } };++type EleBase = Id & {+ version: number;+ direction: string | null;+ format: string;+ indent: number;+};++export type TextNode = Id & {+ type: 'text';+ text: string;+ format: number;+ detail: number;+ mode: string;+ style: string;+ version: number;+};++export type LineBreakNode = Id & {+ type: 'linebreak';+ version: number;+};++export type ParagraphNode = EleBase & {+ type: 'paragraph';+ children: SerNode[];+ textFormat: number;+ textStyle: string;+};++export type HeadingNode = EleBase & {+ type: 'heading';+ children: SerNode[];+ tag: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';+};++export type QuoteNode = EleBase & {+ type: 'quote';+ children: SerNode[];+};++export type ListNode = EleBase & {+ type: 'list';+ children: SerNode[];+ listType: 'bullet' | 'number' | 'check';+ start: number;+ tag: 'ul' | 'ol';+};++export type ListItemNode = EleBase & {+ type: 'listitem';+ children: SerNode[];+ value: number;+ checked?: boolean;+};++export type TableNode = EleBase & {+ type: 'table';+ children: SerNode[];+ colWidths?: number[];+ rowStriping?: boolean;+};++export type TableRowNode = EleBase & {+ type: 'tablerow';+ children: SerNode[];+ height?: number;+};++export type TableCellNode = EleBase & {+ type: 'tablecell';+ children: SerNode[];+ headerState: number;+ colSpan: number;+ rowSpan: number;+ backgroundColor: string | null;+};++export type HrNode = Id & {+ type: 'horizontalrule';+ version: number;+};++export type DateMentionNode = Id & {+ type: 'date-mention';+ version: number;+ date: string;+ displayFormat: string;+ mentionUuid?: string;+};++export type LinkNode = EleBase & {+ type: 'link' | 'autolink';+ children: SerNode[];+ url: string;+ rel?: string | null;+ target?: string | null;+ title?: string | null;+};++export type MarkNode = EleBase & {+ type: 'mark';+ children: SerNode[];+ ids: string[];+};++export type TabNode = Id & {+ type: 'tab';+ version: number;+};++export type ClassedBlockNode = EleBase & {+ type: 'classed-block';+ children: SerNode[];+ tag: string;+ classes: string[];+ attributes?: Record<string, string>;+};++/** A code block whose children are per-token `code-highlight` leaves (flattened+ * back into raw source on serialize). `language` comes from the `CodeNode` base. */+export type CustomCodeNode = EleBase & {+ type: 'custom-code';+ children: SerNode[];+ language: string | null;+};++export type EquationNode = Id & {+ type: 'equation';+ version: number;+ equation: string;+ inline: boolean;+};++export type ImageNode = Id & {+ type: 'image';+ version: number;+ alt: string;+ url: string;+};++export type VideoNode = Id & {+ type: 'video';+ version: number;+ url: string;+ controls: boolean;+};++export type HtmlRenderNode = Id & {+ type: 'html-render';+ version: number;+ html: string;+};++export type DocumentCardNode = Id & {+ type: 'document-card';+ version: number;+ documentId: string;+ documentName: string;+};++export type UserMentionNode = Id & {+ type: 'user-mention';+ version: number;+ userId: string;+ email: string;+};++export type DocumentMentionNode = Id & {+ type: 'document-mention';+ version: number;+ documentId: string;+ documentName: string;+};++export type ContactMentionNode = Id & {+ type: 'contact-mention';+ version: number;+ contactId: string;+ name: string;+ emailOrDomain: string;+ isCompany: boolean;+};++export type GroupMentionNode = Id & {+ type: 'group-mention';+ version: number;+ groupAlias: string;+};++export type PullRequestMentionNode = Id & {+ type: 'pr-mention';+ version: number;+ id: string;+ label: string;+};++export type ThemeMentionNode = Id & {+ type: 'theme-mention';+ version: number;+ name: string;+};++export type UnknownMentionNode = Id & {+ type: 'unknown-mention';+ version: number;+ name: string;+};++export type KnownNode =+ | TextNode+ | LineBreakNode+ | ParagraphNode+ | HeadingNode+ | QuoteNode+ | ListNode+ | ListItemNode+ | TableNode+ | TableRowNode+ | TableCellNode+ | HrNode+ | DateMentionNode+ | LinkNode+ | MarkNode+ | TabNode+ | ClassedBlockNode+ | CustomCodeNode+ | EquationNode+ | ImageNode+ | VideoNode+ | HtmlRenderNode+ | DocumentCardNode+ | UserMentionNode+ | DocumentMentionNode+ | ContactMentionNode+ | GroupMentionNode+ | PullRequestMentionNode+ | ThemeMentionNode+ | UnknownMentionNode;++export type UnknownNode = Id & {+ type: Exclude<string, KnownNode['type']>;+ version?: number;+ children?: SerNode[];+};++export type SerNode = KnownNode | UnknownNode;diff --git a/js/lexical-core/tsconfig.json b/js/lexical-core/tsconfig.jsonnew file mode 100644index 0000000000..5191ba56dd--- /dev/null+++ b/js/lexical-core/tsconfig.json@@ -0,0 +1,16 @@+{+ "compilerOptions": {+ "target": "ESNext",+ "module": "ESNext",+ "moduleResolution": "Bundler",+ "lib": ["ESNext", "DOM"],+ "types": ["@types/node", "@types/bun"],+ "strict": true,+ "skipLibCheck": true,+ "resolveJsonModule": true,+ "verbatimModuleSyntax": true,+ "noEmit": true+ },+ "include": ["./**/*"],+ "exclude": ["node_modules"]+}diff --git a/js/lexical-core/utils/mentions.ts b/js/lexical-core/utils/mentions.tsindex c353aab66b..b0cd5f6f2d 100644--- a/js/lexical-core/utils/mentions.ts+++ b/js/lexical-core/utils/mentions.ts@@ -1,4 +1,5 @@import { $dfsIterator } from '@lexical/utils';+import { $getRoot, type LexicalNode } from 'lexical';import {$isContactMentionNode,type ContactMentionInfo,@@ -29,7 +30,6 @@ import {type UserMentionNode,} from '../nodes/UserMentionNode';import { wrapXml } from '../transformers/transformers';-import { $getRoot, type LexicalNode } from 'lexical';function dropKey<T extends object, K extends keyof T>(obj: T,diff --git a/js/lexical-service/src/endpoints/xml.ts b/js/lexical-service/src/endpoints/xml.tsnew file mode 100644index 0000000000..29a5a66265--- /dev/null+++ b/js/lexical-service/src/endpoints/xml.ts@@ -0,0 +1,69 @@+import { toXml } from '@macro-inc/lexical-core';+import { OpenAPIRoute } from 'chanfana';+import type { Context } from 'hono';+import { z } from 'zod';+import {+ ConversionError,+ createSyncError,+ handleEndpointError,+ validateEnvironment,+} from '../lib/error-handler';+import { docIdParam, standardErrorResponses } from '../lib/schemas';+import { createSyncClient } from '../lib/sync-service';++export class XmlEndpoint extends OpenAPIRoute {+ schema = {+ summary: 'Convert document snapshot to XML',+ description:+ 'Fetches a document snapshot from the sync service and converts it to XML',+ request: {+ params: docIdParam,+ },+ responses: {+ 200: {+ description: 'Successfully converted document to XML',+ content: {+ 'application/json': {+ schema: z.object({+ data: z.string(),+ }),+ },+ },+ },+ ...standardErrorResponses,+ },+ };++ async handle(c: Context) {+ let docId = 'unknown';+ try {+ const { params } = await this.getValidatedData<typeof this.schema>();+ docId = params.docId;++ validateEnvironment(c, ['SYNC_SERVICE_AUTH_KEY', 'SYNC_SERVICE_URL']);++ const syncClient = createSyncClient({+ baseUrl: c.env.SYNC_SERVICE_URL,+ internalAuthKey: c.env.SYNC_SERVICE_AUTH_KEY,+ serviceFetcher: c.env.SYNC_SERVICE,+ });++ const rawDocument = await syncClient.raw(docId);++ if (rawDocument.success) {+ try {+ const xml = toXml(rawDocument.data);+ return c.json({ data: xml });+ } catch {+ throw new ConversionError('Failed to parse document snapshot');+ }+ } else {+ throw createSyncError(+ rawDocument as { success: false; error: Error; status?: number }+ );+ }+ } catch (error) {+ return handleEndpointError(error, c, docId);+ }+ }+}diff --git a/js/lexical-service/src/index.ts b/js/lexical-service/src/index.tsindex cb141f34e6..f899b93728 100644--- a/js/lexical-service/src/index.ts+++ b/js/lexical-service/src/index.ts@@ -7,6 +7,7 @@ import { CognitionTextEndpoint } from './endpoints/cognition-text';import { CognitionV2Endpoint } from './endpoints/cognition-v2';import { MarkdownEndpoint } from './endpoints/markdown';import { MarkdownSnapshotEndpoint } from './endpoints/markdown-snapshot';+import { XmlEndpoint } from './endpoints/xml';import { PlaintextEndpoint } from './endpoints/plaintext';import { SearchTextEndpoint } from './endpoints/search-text';@@ -52,6 +53,14 @@ app.use('/search/*', async (c, next) => {await next();});+app.use('/xml/*', async (c, next) => {+ const authKey = c.req.header('x-internal-auth-key');+ if (!authKey || authKey !== c.env.INTERNAL_AUTH_KEY) {+ return c.json({ error: 'Unauthorized' }, 401);+ }+ await next();+});+app.use('/markdown/*', async (c, next) => {const authKey = c.req.header('x-internal-auth-key');if (!authKey || authKey !== c.env.INTERNAL_AUTH_KEY) {@@ -94,6 +103,7 @@ openapi.get('/cognition/:docId', CognitionTextEndpoint);openapi.get('/cognitionv2/:docId', CognitionV2Endpoint);openapi.get('/search/:docId', SearchTextEndpoint);openapi.get('/markdown/:docId', MarkdownEndpoint);+openapi.get('/xml/:docId', XmlEndpoint);openapi.post('/snapshot/markdown', MarkdownSnapshotEndpoint);openapi.get('/internal/health', (c) => c.json({ status: 'healthy' }));diff --git a/js/lexical-service/src/lib/convsersions.ts b/js/lexical-service/src/lib/convsersions.tsindex 836458d2c5..65482ba8bf 100644--- a/js/lexical-service/src/lib/convsersions.ts+++ b/js/lexical-service/src/lib/convsersions.ts@@ -143,7 +143,6 @@ export function toCognitionV2(raw: SerializedEditorState): NewMdNode[] {throw new Error('Error converting snapshot to cognition textv2');}}-export type MarkdownTarget = 'internal' | 'external' | 'embedding';export function toMarkdownText(diff --git a/js/loro-mirror/packages/core/src/core/mirror.ts b/js/loro-mirror/packages/core/src/core/mirror.tsindex 6a6e91a4d8..4af43937a0 100644--- a/js/loro-mirror/packages/core/src/core/mirror.ts+++ b/js/loro-mirror/packages/core/src/core/mirror.ts@@ -237,10 +237,11 @@ export class Mirror<S extends SchemaType> {this.state = newState;// Register root containers- if (this.schema) {- for (const key in this.schema.definition) {- if (Object.prototype.hasOwnProperty.call(this.schema.definition, key)) {- const fieldSchema = this.schema.definition[key];+ if (this.schema && this.schema.type === 'schema') {+ const rootSchema = this.schema as RootSchemaType<Record<string, ContainerSchemaType>>;+ for (const key in rootSchema.definition) {+ if (Object.prototype.hasOwnProperty.call(rootSchema.definition, key)) {+ const fieldSchema = rootSchema.definition[key];if ([@@ -556,7 +557,7 @@ export class Mirror<S extends SchemaType> {this.applyContainerChanges(container, containerChanges);} else {throw new Error(- `Container not found for ID: ${containerId}.+ `Container not found for ID: ${containerId}.This is likely due to a stale reference or a synchronization issue.`);}@@ -727,7 +728,7 @@ export class Mirror<S extends SchemaType> {break;default:throw new Error(- `Unknown container kind for top-level update: ${kind}.+ `Unknown container kind for top-level update: ${kind}.This is likely a programming error or unsupported container type.`);}@@ -766,8 +767,9 @@ export class Mirror<S extends SchemaType> {}// Get the idSelector function from the schema- const idSelector = schema?.idSelector;- const itemSchema = schema?.itemSchema;+ const listSchema = (schema != null && (isLoroListSchema(schema) || isLoroMovableListSchema(schema))) ? schema : undefined;+ const idSelector = listSchema?.idSelector;+ const itemSchema = listSchema?.itemSchema;if (this.options.debug) {console.log(diff --git a/nix-support/node_modules-hashes.json b/nix-support/node_modules-hashes.jsonindex 5455d0d748..6f71d01c6b 100644--- a/nix-support/node_modules-hashes.json+++ b/nix-support/node_modules-hashes.json@@ -1,6 +1,6 @@{"nodeModules": {- "aarch64-darwin": "sha256-MlD7jeJ2Ieg7ZVRVzNX5TjRJAifU4SyG1OeFUbbqo58=",- "x86_64-linux": "sha256-OFK7Wi3l3QbuRLLOiGyS6WXpYFrg4hb0jtrxPoiRLfQ="+ "aarch64-darwin": "sha256-NixmiWMFLKhy6zarDYnmS4N7GYthokmpBL3lM64vZzk=",+ "x86_64-linux": "sha256-psPPOB4jSR61J5Tsuiqb1F5k2okJetKF4l72l0nuKa4="}}\ No newline at end of filediff --git a/rust/cloud-storage/.config/hakari.toml b/rust/cloud-storage/.config/hakari.tomlindex c398cec579..0d6de1c49a 100644--- a/rust/cloud-storage/.config/hakari.toml+++ b/rust/cloud-storage/.config/hakari.toml@@ -36,4 +36,8 @@ platforms = [# the deploy dependency layers that every service build warms. Excluding it# also means manage-deps does not add the workspace-hack dep to it.[traversal-excludes]-workspace-members = ["xtask"]+# xtask: dev-only automation (see above).+# macro_sync_service_jwt: must stay wasm-safe (the sync service compiles it to+# wasm32), so it must not gain the workspace-hack dep, which pulls native-only+# features.+workspace-members = ["xtask", "macro_sync_service_jwt"]diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lockindex efe17ed68d..f3e3687791 100644--- a/rust/cloud-storage/Cargo.lock+++ b/rust/cloud-storage/Cargo.lock@@ -221,6 +221,7 @@ dependencies = ["rand 0.9.4","readonly_pool","remote_env_var",+ "reqwest 0.13.4","rootcause","schemars 1.2.1","search_service_client",@@ -2928,6 +2929,15 @@ dependencies = ["workspace-hack",]+[[package]]+name = "convert_case"+version = "0.10.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "633458d4ef8c78b72454de2d54fd6ab2e60f9e02be22f3c6104cdc8a4e0fceb9"+dependencies = [+ "unicode-segmentation",+]+[[package]]name = "convert_case"version = "0.11.0"@@ -3714,10 +3724,12 @@ version = "2.1.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb"dependencies = [+ "convert_case 0.10.0","proc-macro2","quote","rustc_version","syn 2.0.117",+ "unicode-xid",][[package]]@@ -3995,6 +4007,7 @@ dependencies = ["macro_queues","macro_service_urls","macro_sha_count_client",+ "macro_sync_service_jwt","macro_tower_layers","macro_user_id","macro_uuid",@@ -4141,6 +4154,7 @@ name = "documents"version = "0.1.0"dependencies = ["ai_toolset",+ "ai_usage","anyhow","async-trait","attachment",@@ -4168,6 +4182,7 @@ dependencies = ["macro_entrypoint","macro_env_var","macro_service_urls",+ "macro_sync_service_jwt","macro_user_id","macro_uuid","mockall",@@ -7632,6 +7647,15 @@ dependencies = ["workspace-hack",]+[[package]]+name = "macro_sync_service_jwt"+version = "0.1.0"+dependencies = [+ "derive_more 2.1.1",+ "jsonwebtoken 10.3.0",+ "serde",+]+[[package]]name = "macro_tower_layers"version = "0.1.0"@@ -10917,7 +10941,7 @@ name = "rig-derive"version = "0.38.2"source = "git+https://github.com/macro-inc/rig?branch=feat%2Fresponses-api-non-strict-tools#6deadfc6f9b432c849ea79733a549f46407530b7"dependencies = [- "convert_case",+ "convert_case 0.11.0","deluxe","indoc","proc-macro-crate 3.5.0",diff --git a/rust/cloud-storage/Cargo.toml b/rust/cloud-storage/Cargo.tomlindex 131b5e9a0c..7c6420a258 100644--- a/rust/cloud-storage/Cargo.toml+++ b/rust/cloud-storage/Cargo.toml@@ -41,6 +41,7 @@ members = ["macro_event_broker","macro_event_topics","macro_queues",+ "macro_sync_service_jwt","mcp_service","memory","notification_sandbox",diff --git a/rust/cloud-storage/ai_tools/Cargo.toml b/rust/cloud-storage/ai_tools/Cargo.tomlindex 59351d0315..d7addaf828 100644--- a/rust/cloud-storage/ai_tools/Cargo.toml+++ b/rust/cloud-storage/ai_tools/Cargo.toml@@ -55,6 +55,7 @@ prompt = { path = "../prompt" }models_properties = { path = "../models_properties" }models_search = { path = "../models_search" }rand.workspace = true+reqwest.workspace = truereadonly_pool = { path = "../readonly_pool" }rootcause = { workspace = true }schemars.workspace = truediff --git a/rust/cloud-storage/ai_tools/src/build_context.rs b/rust/cloud-storage/ai_tools/src/build_context.rsindex 504dc4a792..57fa258a2e 100644--- a/rust/cloud-storage/ai_tools/src/build_context.rs+++ b/rust/cloud-storage/ai_tools/src/build_context.rs@@ -14,6 +14,7 @@ use channels::domain::list_service::ChannelListServiceImpl;use channels::outbound::pg_channels_repo::PgChannelsRepo;use documents::domain::models::CloudFrontConfig;use documents::inbound::toolset::DocumentToolContext;+use documents::outbound::editing_worker_client::ReqwestEditingWorkerClient;use documents::outbound::pg_document_repo::PgDocumentRepo;use documents::outbound::s3_upload_url::S3UploadUrlAdapter;use email::domain::ports::ReadonlyEmailPreviewAdapter;@@ -32,7 +33,8 @@ use lexical_client::LexicalClient;use macro_env::Environment;use macro_env_var::{env_var, maybe_env_var};use macro_service_urls::{- DocumentStorageServiceUrl, EmailServiceUrl, LexicalServiceUrl, SyncServiceUrl,+ AiEditingWorkerUrl, DocumentStorageServiceUrl, EmailServiceUrl, LexicalServiceUrl,+ SyncServiceUrl,};use notification::domain::service::{NotificationReaderService, PlatformArnConfig};use notification::outbound::queue::SqsQueue;@@ -54,6 +56,7 @@ env_var! {DocumentStorageServiceCloudfrontDistributionUrl,DocumentStorageServiceCloudfrontSignerPublicKeyId,DocumentStorageServiceCloudfrontSignerPrivateKeySecretName,+ DocumentPermissionJwtSecretKey,}}@@ -100,6 +103,7 @@ pub async fn build_tool_service_context_from_env(let sync_service_url = SyncServiceUrl::new()?.to_string();let email_service_url = EmailServiceUrl::new()?.to_string();let lexical_service_url = LexicalServiceUrl::new()?.to_string();+ let ai_editing_worker_url = AiEditingWorkerUrl::new()?.to_string();let aws_config = macro_aws_config::get_macro_aws_config().await;let aws_sqs_client = aws_sdk_sqs::Client::new(&aws_config);@@ -164,7 +168,7 @@ pub async fn build_tool_service_context_from_env(let search_client = Arc::new(SearchServiceClient::new(env.document_storage_service_auth_key.to_string(),- document_storage_service_url,+ document_storage_service_url.clone(),));let sync_client = Arc::new(SyncServiceClient::new(sync_service_auth_key.clone(),@@ -257,6 +261,8 @@ pub async fn build_tool_service_context_from_env((*entity_access_service).clone(),lexical_client,sync_client.as_ref().clone(),+ ReqwestEditingWorkerClient::new(ai_editing_worker_url, Arc::new(reqwest::Client::new())),+ env.document_permission_jwt_secret_key.to_string(),);let properties_tool_context =diff --git a/rust/cloud-storage/ai_tools/src/tool_context.rs b/rust/cloud-storage/ai_tools/src/tool_context.rsindex 8f0f41ea23..a1521f0fb6 100644--- a/rust/cloud-storage/ai_tools/src/tool_context.rs+++ b/rust/cloud-storage/ai_tools/src/tool_context.rs@@ -464,8 +464,11 @@ pub type ToolEntityAccessService = entity_access::domain::service::EntityAccessS>;/// Type alias for the document tool context-pub type ToolDocumentToolContext =- DocumentToolContext<ToolDocumentService, ToolEntityAccessService>;+pub type ToolDocumentToolContext = DocumentToolContext<+ ToolDocumentService,+ ToolEntityAccessService,+ documents::outbound::editing_worker_client::ReqwestEditingWorkerClient,+>;/// Type alias for the foreign entity service implementation used by AI tools.pub type ToolForeignEntityService = ForeignEntityServiceImpl<PgForeignEntityRepo>;diff --git a/rust/cloud-storage/ai_usage/src/domain/mod.rs b/rust/cloud-storage/ai_usage/src/domain/mod.rsindex 340df61c37..102bddaebb 100644--- a/rust/cloud-storage/ai_usage/src/domain/mod.rs+++ b/rust/cloud-storage/ai_usage/src/domain/mod.rs@@ -4,7 +4,8 @@ pub mod ports;pub mod service;pub use ports::{- AiFeature, CompletionUsage, FeatureUsage, Price, Result, SYSTEM_USER_ID, Usage, UsageApiParams,- UsageContext, UsageError, UsageEvent, UsageRecorder, UsageRepo, UsageService, UsageSummary,+ AiFeature, CompletionUsage, FeatureUsage, NoOpUsageRecorder, Price, Result, SYSTEM_USER_ID,+ Usage, UsageApiParams, UsageContext, UsageError, UsageEvent, UsageRecorder, UsageRepo,+ UsageService, UsageSummary,};pub use service::UsageServiceImpl;diff --git a/rust/cloud-storage/ai_usage/src/domain/ports.rs b/rust/cloud-storage/ai_usage/src/domain/ports.rsindex 02b3c5761a..d0ad35ec5c 100644--- a/rust/cloud-storage/ai_usage/src/domain/ports.rs+++ b/rust/cloud-storage/ai_usage/src/domain/ports.rs@@ -56,6 +56,8 @@ pub enum AiFeature {ChannelBot,/// AI projection materialization.AiProjection,+ /// In-document AI editing (the ai-editing-worker).+ AiEditing,}/// Resolved price for one completion.@@ -266,11 +268,9 @@ pub trait UsageRecorder: Send + Sync {/// A [`UsageRecorder`] that drops every event. Used at call sites that cannot/// (or should not) record, mirroring the `NoOp*` adapters used elsewhere.-#[cfg(test)]#[derive(Debug, Clone, Copy, Default)]pub struct NoOpUsageRecorder;-#[cfg(test)]impl UsageRecorder for NoOpUsageRecorder {fn record(&self, _event: UsageEvent) {}}diff --git a/rust/cloud-storage/ai_usage/src/lib.rs b/rust/cloud-storage/ai_usage/src/lib.rsindex 82d25ad3ba..9f7c54dd58 100644--- a/rust/cloud-storage/ai_usage/src/lib.rs+++ b/rust/cloud-storage/ai_usage/src/lib.rs@@ -17,8 +17,8 @@ pub mod inbound;pub mod outbound;pub use domain::{- AiFeature, CompletionUsage, FeatureUsage, Price, SYSTEM_USER_ID, Usage, UsageApiParams,- UsageContext, UsageEvent, UsageRecorder, UsageRepo, UsageService, UsageSummary,+ AiFeature, CompletionUsage, FeatureUsage, NoOpUsageRecorder, Price, SYSTEM_USER_ID, Usage,+ UsageApiParams, UsageContext, UsageEvent, UsageRecorder, UsageRepo, UsageService, UsageSummary,};use std::sync::Arc;diff --git a/rust/cloud-storage/deny.toml b/rust/cloud-storage/deny.tomlindex 5aa1fbdf32..19936f9045 100644--- a/rust/cloud-storage/deny.toml+++ b/rust/cloud-storage/deny.toml@@ -75,14 +75,9 @@ ignore = [{ id = "RUSTSEC-2024-0436", reason = "Paste is a 'finished' crate which is heavily depended on" },{ id = "RUSTSEC-2025-0012", reason = "Cannot remove dep due to 3rd party openai dependency" },{ id = "RUSTSEC-2025-0057", reason = "We should switch to rustc-hash, but haven't got to that yet" },- { id = "RUSTSEC-2026-0118", reason = "hickory-proto 0.26.1 fix not yet available via reqwest 0.13 constraints" },- { id = "RUSTSEC-2026-0119", reason = "hickory-proto 0.26.1 fix not yet available via reqwest 0.13 constraints" },- { id = "RUSTSEC-2026-0118", reason = "hickory-proto 0.26.1 fix blocked by reqwest 0.13 pinning hickory-resolver ^0.25; revisit on next reqwest release" },- { id = "RUSTSEC-2026-0119", reason = "hickory-proto 0.26.1 fix blocked by reqwest 0.13 pinning hickory-resolver ^0.25; revisit on next reqwest release" },{ id = "RUSTSEC-2026-0098", reason = "rustls-webpki 0.101.7 pinned by AWS SDK via rustls 0.21" },{ id = "RUSTSEC-2026-0099", reason = "rustls-webpki 0.101.7 pinned by AWS SDK via rustls 0.21" },{ id = "RUSTSEC-2026-0104", reason = "rustls-webpki 0.101.7 pinned by AWS SDK via rustls 0.21" },- { id = "RUSTSEC-2026-0105", reason = "core2 yanked/unmaintained, transitive via pdfium-render deps" },{ id = "RUSTSEC-2026-0173", reason = "proc-macro-error2 is unmaintained; transitive dependency pending upstream migration" },{ id = "RUSTSEC-2026-0174", reason = "http-types unmaintained, transitive via async-stripe; no safe upgrade available" },{ id = "RUSTSEC-2026-0179", reason = "postgres-protocol 0.6.11 is transitive via pgvector; database connections are to trusted infrastructure" },diff --git a/rust/cloud-storage/document_cognition_service/src/api/context/test.rs b/rust/cloud-storage/document_cognition_service/src/api/context/test.rsindex 6998152fc3..c19f3b9986 100644--- a/rust/cloud-storage/document_cognition_service/src/api/context/test.rs+++ b/rust/cloud-storage/document_cognition_service/src/api/context/test.rs@@ -256,11 +256,17 @@ pub async fn test_api_context(pool: sqlx::Pool<sqlx::Postgres>) -> std::sync::ArForeignEntityServiceImpl::new(PgForeignEntityRepo::new(pool.clone())),);let test_lexical_client = LexicalClient::new("test".into(), "http://nofileshere".into());+ let test_editing_client =+ documents::outbound::editing_worker_client::ReqwestEditingWorkerClient::from_url(+ "http://nofileshere".into(),+ );let document_tool_context = documents::inbound::toolset::DocumentToolContext::new(document_service,(*entity_access_service).clone(),test_lexical_client,sync_service_client.as_ref().clone(),+ test_editing_client,+ "test-jwt-secret".to_string(),);let search_service_client = Arc::new(search_service_client);diff --git a/rust/cloud-storage/document_cognition_service/src/api/stream/chat_message/mod.rs b/rust/cloud-storage/document_cognition_service/src/api/stream/chat_message/mod.rsindex 312a590258..520276b83f 100644--- a/rust/cloud-storage/document_cognition_service/src/api/stream/chat_message/mod.rs+++ b/rust/cloud-storage/document_cognition_service/src/api/stream/chat_message/mod.rs@@ -524,6 +524,7 @@ fn stream_and_save_message(// Carry the feature on the context so tool-spawned subagents attribute to it.let mut tool_context = tool_context;tool_context.usage_context = usage_ctx.clone();+ tool_context.document_tool_context.recorder = tool_context.recorder.clone();let (mut session, cancel) = agent_loop.session(toolset, Arc::new(tool_context), &system_prompt, usage_ctx).awaitdiff --git a/rust/cloud-storage/document_cognition_service/src/config.rs b/rust/cloud-storage/document_cognition_service/src/config.rsindex d1ff511723..8968360ece 100644--- a/rust/cloud-storage/document_cognition_service/src/config.rs+++ b/rust/cloud-storage/document_cognition_service/src/config.rs@@ -2,6 +2,7 @@ use anyhow::Context;pub use macro_env::Environment;use macro_env_var::{env_vars, maybe_env_vars};use macro_middleware::auth::internal_access::InternalApiKey;+use macro_service_urls::AiEditingWorkerUrl;use secretsmanager_client::LocalOrRemoteSecret;use crate::core::constants::DEFAULT_DOCUMENT_BATCH_LIMIT;@@ -19,6 +20,7 @@ env_vars!(pub struct DocumentStorageServiceCloudfrontSignerPublicKeyId;pub struct DocumentStorageServiceCloudfrontSignerPrivateKey;pub struct McpCredentialsKeySecretName;+ pub struct DocumentPermissionJwtSecretKey;);maybe_env_vars!(@@ -64,6 +66,11 @@ pub struct Config {pub mcp_credentials_key_secret_name: LocalOrRemoteSecret<McpCredentialsKeySecretName>,/// The internal api keypub internal_api_key: InternalApiKey,+ /// AI editing worker URL+ #[macro_config_default(AiEditingWorkerUrl::unwrap_new().to_string())]+ pub ai_editing_worker_url: String,+ /// JWT secret for minting document permission tokens for the editing worker.+ pub document_permission_jwt_secret_key: DocumentPermissionJwtSecretKey,}impl Config {@@ -110,6 +117,10 @@ impl Config {McpCredentialsKeySecretName::Comptime("MCP_CREDENTIALS_KEY_SECRET_NAME"),),internal_api_key: InternalApiKey::Comptime(""),+ ai_editing_worker_url: AiEditingWorkerUrl::unwrap_new().to_string(),+ document_permission_jwt_secret_key: DocumentPermissionJwtSecretKey::Comptime(+ "DOCUMENT_PERMISSION_JWT_SECRET_KEY",+ ),}}}diff --git a/rust/cloud-storage/document_cognition_service/src/main.rs b/rust/cloud-storage/document_cognition_service/src/main.rsindex e005306660..03519798a7 100644--- a/rust/cloud-storage/document_cognition_service/src/main.rs+++ b/rust/cloud-storage/document_cognition_service/src/main.rs@@ -290,6 +290,14 @@ async fn main() -> anyhow::Result<()> {(*entity_access_service).clone(),lexical_client_for_tools,sync_service_client.clone(),+ documents::outbound::editing_worker_client::ReqwestEditingWorkerClient::new(+ config.ai_editing_worker_url.clone(),+ std::sync::Arc::new(reqwest::Client::new()),+ ),+ config+ .document_permission_jwt_secret_key+ .as_ref()+ .to_string(),);tracing::info!("initialized document tool context");diff --git a/rust/cloud-storage/document_storage_service/Cargo.toml b/rust/cloud-storage/document_storage_service/Cargo.tomlindex e53a1d7ebd..b3de089a17 100644--- a/rust/cloud-storage/document_storage_service/Cargo.toml+++ b/rust/cloud-storage/document_storage_service/Cargo.toml@@ -94,6 +94,7 @@ macro_db_client = { path = "../macro_db_client" }macro_entrypoint = { path = "../macro_entrypoint" }macro_env = { path = "../macro_env" }macro_service_urls = { path = "../macro_service_urls" }+macro_sync_service_jwt = { path = "../macro_sync_service_jwt" }macro_env_var = { path = "../macro_env_var" }macro_queues = { path = "../macro_queues" }macro_middleware = { path = "../macro_middleware", default-features = false, features = [diff --git a/rust/cloud-storage/document_storage_service/src/api/documents/permissions_token/create_permission_token.rs b/rust/cloud-storage/document_storage_service/src/api/documents/permissions_token/create_permission_token.rsindex 03070f673f..c3fcec5ed2 100644--- a/rust/cloud-storage/document_storage_service/src/api/documents/permissions_token/create_permission_token.rs+++ b/rust/cloud-storage/document_storage_service/src/api/documents/permissions_token/create_permission_token.rs@@ -76,7 +76,8 @@ pub async fn handler(}),).into_response()- })?;+ })?+ .into_inner();Ok((StatusCode::OK,diff --git a/rust/cloud-storage/document_storage_service/src/api/documents/permissions_token/validate_permissions_token.rs b/rust/cloud-storage/document_storage_service/src/api/documents/permissions_token/validate_permissions_token.rsindex 31df87722e..4a722ffcee 100644--- a/rust/cloud-storage/document_storage_service/src/api/documents/permissions_token/validate_permissions_token.rs+++ b/rust/cloud-storage/document_storage_service/src/api/documents/permissions_token/validate_permissions_token.rs@@ -6,8 +6,8 @@ use axum::{http::StatusCode,response::{IntoResponse, Response},};-use documents_hex::domain::permission_token::ISSUER;use jsonwebtoken::{Algorithm, DecodingKey, Validation};+use macro_sync_service_jwt::ISSUER;use model::{document::DocumentPermissionsToken, response::ErrorResponse, user::UserContext};use utoipa::ToSchema;@@ -85,7 +85,7 @@ pub async fn handler(},};- if decoded_jwt.user_id != user_id {+ if decoded_jwt.user_id.as_ref().map(|id| id.to_string()) != user_id {return Err((StatusCode::UNAUTHORIZED,Json(ErrorResponse {diff --git a/rust/cloud-storage/documents/Cargo.toml b/rust/cloud-storage/documents/Cargo.tomlindex c21d85cc04..96d08ac889 100644--- a/rust/cloud-storage/documents/Cargo.toml+++ b/rust/cloud-storage/documents/Cargo.toml@@ -20,6 +20,7 @@ attachment = []ai_tools = ["dep:ai_toolset",+ "dep:ai_usage","dep:async-trait","dep:model_file_type","dep:schemars",@@ -99,6 +100,7 @@ foreign_entity = { path = "../foreign_entity", default-features = false, featuremacro_aws_config = { path = "../macro_aws_config", features = ["s3"] }macro_entrypoint = { path = "../macro_entrypoint", optional = true }macro_env_var = { path = "../macro_env_var" }+macro_sync_service_jwt = { path = "../macro_sync_service_jwt" }macro_user_id = { path = "../macro_user_id" }macro_uuid = { path = "../macro_uuid" }model = { path = "../model" }@@ -133,6 +135,7 @@ urlencoding = { workspace = true }# AI Tool dependenciesai_toolset = { path = "../ai_toolset", optional = true }+ai_usage = { path = "../ai_usage", optional = true }async-trait = { workspace = true, optional = true }lexical_client = { path = "../lexical_client", optional = true }model_file_type = { path = "../model_file_type", optional = true }diff --git a/rust/cloud-storage/documents/src/domain/permission_token.rs b/rust/cloud-storage/documents/src/domain/permission_token.rsindex d76170ba8d..78980f4fd8 100644--- a/rust/cloud-storage/documents/src/domain/permission_token.rs+++ b/rust/cloud-storage/documents/src/domain/permission_token.rs@@ -2,30 +2,30 @@//! sync service to authorize document access.use crate::domain::models::DocumentError;+use macro_sync_service_jwt::{DocumentPermissionToken, ISSUER, TOKEN_TTL_SECS};+use macro_user_id::user_id::MacroUserIdStr;use model::document::DocumentPermissionsToken;use models_permissions::share_permission::access_level::AccessLevel;use std::time::{SystemTime, UNIX_EPOCH};-/// JWT issuer claim value-pub const ISSUER: &str = "document_storage_service";--/// Token lifetime in seconds (1 hour).-const TOKEN_TTL_SECS: usize = 3600;-/// Sign a document permission token for the given user and document.pub fn encode_permission_token(user_id: Option<String>,document_id: String,access_level: AccessLevel,jwt_secret: &str,-) -> Result<String, DocumentError> {+) -> Result<DocumentPermissionToken, DocumentError> {+ let user_id = user_id+ .map(MacroUserIdStr::try_from)+ .transpose()+ .map_err(|e| DocumentError::Internal(anyhow::anyhow!("invalid user id: {e}")))?;+let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as usize;- Ok(jsonwebtoken::encode(- &jsonwebtoken::Header::new(jsonwebtoken::Algorithm::HS256),+ Ok(macro_sync_service_jwt::encode(&DocumentPermissionsToken {user_id,document_id,@@ -33,6 +33,6 @@ pub fn encode_permission_token(exp: now + TOKEN_TTL_SECS,iss: ISSUER.to_string(),},- &jsonwebtoken::EncodingKey::from_secret(jwt_secret.as_bytes()),+ jwt_secret,)?)}diff --git a/rust/cloud-storage/documents/src/domain/ports/editing.rs b/rust/cloud-storage/documents/src/domain/ports/editing.rsnew file mode 100644index 0000000000..f46022f6cd--- /dev/null+++ b/rust/cloud-storage/documents/src/domain/ports/editing.rs@@ -0,0 +1,38 @@+//! Port definition for the AI editing worker.++use macro_sync_service_jwt::DocumentPermissionToken;+use std::future::Future;++/// Result returned by a successful edit operation.+pub struct EditResult {+ /// Number of individual edit operations applied to the document.+ pub edits_applied: usize,+ /// Per-model token usage reported by the editing worker (the worker runs+ /// several models, supervisor, interpret, coder; this is one entry each).+ pub usage: Vec<EditUsage>,+ /// If set, the worker needs more information. Invoke again with the+ /// requested details appended to the instructions.+ pub clarification: Option<String>,+}++/// Token usage for one model used by the editing worker.+pub struct EditUsage {+ /// The model api id (e.g. `claude-haiku-4-5-xxxx`).+ pub model: String,+ /// Input tokens consumed.+ pub input_tokens: u32,+ /// Output tokens produced.+ pub output_tokens: u32,+}++/// Port for applying AI-driven edits to a document via the editing worker.+#[cfg_attr(test, mockall::automock)]+pub trait EditingWorkerService: Send + Sync + 'static {+ /// Apply AI-driven edits to `document_id` using a pre-minted `document_token`.+ fn edit(+ &self,+ document_id: &str,+ document_token: &DocumentPermissionToken,+ instructions: &str,+ ) -> impl Future<Output = anyhow::Result<EditResult>> + Send;+}diff --git a/rust/cloud-storage/documents/src/domain/ports/mod.rs b/rust/cloud-storage/documents/src/domain/ports/mod.rsindex cb5276c3a9..bec80f534b 100644--- a/rust/cloud-storage/documents/src/domain/ports/mod.rs+++ b/rust/cloud-storage/documents/src/domain/ports/mod.rs@@ -4,6 +4,7 @@#[cfg(feature = "document_create")]pub mod create;+pub mod editing;pub mod markdown;use std::future::Future;diff --git a/rust/cloud-storage/documents/src/inbound/axum_router/create_markdown.rs b/rust/cloud-storage/documents/src/inbound/axum_router/create_markdown.rsindex 078f4eff7f..c04f75496d 100644--- a/rust/cloud-storage/documents/src/inbound/axum_router/create_markdown.rs+++ b/rust/cloud-storage/documents/src/inbound/axum_router/create_markdown.rs@@ -77,7 +77,8 @@ pub async fn create_markdown_handler<.map_err(|e| {tracing::error!(error=?e, "failed to encode permission token");DocumentError::Internal(e.into())- })?;+ })?+ .into_inner();Ok(Json(CreateMarkdownDocumentResponse {document_id,diff --git a/rust/cloud-storage/documents/src/inbound/axum_router/create_task.rs b/rust/cloud-storage/documents/src/inbound/axum_router/create_task.rsindex 65ddb5c3e1..cc71a656ad 100644--- a/rust/cloud-storage/documents/src/inbound/axum_router/create_task.rs+++ b/rust/cloud-storage/documents/src/inbound/axum_router/create_task.rs@@ -102,7 +102,8 @@ pub async fn create_task_handler<.map_err(|e| {tracing::error!(error=?e, "failed to encode permission token");DocumentError::Internal(e.into())- })?;+ })?+ .into_inner();Ok(Json(CreateTaskResponse {document_id,diff --git a/rust/cloud-storage/documents/src/inbound/toolset/create_document.rs b/rust/cloud-storage/documents/src/inbound/toolset/create_document.rsindex f971e0d961..ee97863623 100644--- a/rust/cloud-storage/documents/src/inbound/toolset/create_document.rs+++ b/rust/cloud-storage/documents/src/inbound/toolset/create_document.rs@@ -6,6 +6,7 @@ use crate::domain::create::{NewDocumentMetadata, NewPlainTextDocument};use crate::domain::models::DocumentError;use crate::domain::ports::DocumentService;use crate::domain::ports::create::DocumentCreationService;+use crate::domain::ports::editing::EditingWorkerService;use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};use anyhow::Context;use async_trait::async_trait;@@ -59,16 +60,17 @@ pub struct CreateDocument {}#[async_trait]-impl<DSvc, ESvc> AsyncTool<DocumentToolContext<DSvc, ESvc>> for CreateDocument+impl<DSvc, ESvc, EDSvc> AsyncTool<DocumentToolContext<DSvc, ESvc, EDSvc>> for CreateDocumentwhereDSvc: DocumentService + DocumentCreationService,ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,{type Output = CreateDocumentResponse;async fn call(&self,- service_context: ServiceContext<DocumentToolContext<DSvc, ESvc>>,+ service_context: ServiceContext<DocumentToolContext<DSvc, ESvc, EDSvc>>,request_context: RequestContext,) -> ToolResult<Self::Output> {tracing::info!(params=?self, "Create content");diff --git a/rust/cloud-storage/documents/src/inbound/toolset/edit_document.rs b/rust/cloud-storage/documents/src/inbound/toolset/edit_document.rsnew file mode 100644index 0000000000..8f3ce10043--- /dev/null+++ b/rust/cloud-storage/documents/src/inbound/toolset/edit_document.rs@@ -0,0 +1,126 @@+//! EditDocument tool — thin wrapper over [`EditingWorkerPort`].++use crate::domain::permission_token::encode_permission_token;+use crate::domain::ports::{+ DocumentService, create::DocumentCreationService, editing::EditingWorkerService,+};+use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};+use async_trait::async_trait;+use entity_access::domain::{+ models::{EditAccessLevel, EntityType},+ ports::EntityAccessService,+};+use models_permissions::share_permission::access_level::AccessLevel;+use schemars::JsonSchema;+use serde::{Deserialize, Serialize};++use super::DocumentToolContext;++#[derive(Debug, Deserialize, JsonSchema)]+#[schemars(+ title = "EditDocument",+ description = "Apply AI-driven edits to a Macro document in place -- rewriting, inserting, formatting, or restructuring. If the response contains a `clarification` field, invoke again with the requested info appended to `instructions`. To insert mention(s), include each person's userId and email. To insert document-card(s), include each document's documentId and documentName."+)]+pub struct EditDocument {+ #[schemars(description = "The ID of the document to edit.")]+ pub document_id: String,+ #[schemars(+ description = "Natural language instructions. For mention(s), include userId and email per person. For document-card(s), include documentId and documentName per document. You may need to look these up."+ )]+ pub instructions: String,+}++#[derive(Debug, Serialize, JsonSchema)]+pub struct EditDocumentResponse {+ /// A short outcome for the model -- whether the edit was applied or+ /// interrupted -- never the underlying list of edit operations.+ pub summary: String,+ /// If present, invoke this tool again with this information appended to `instructions`.+ pub clarification: Option<String>,+}++#[async_trait]+impl<DSvc, ESvc, EDSvc> AsyncTool<DocumentToolContext<DSvc, ESvc, EDSvc>> for EditDocument+where+ DSvc: DocumentService + DocumentCreationService,+ ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,+{+ type Output = EditDocumentResponse;++ #[tracing::instrument(skip_all, fields(document_id = %self.document_id), err)]+ async fn call(+ &self,+ ctx: ServiceContext<DocumentToolContext<DSvc, ESvc, EDSvc>>,+ request_context: RequestContext,+ ) -> ToolResult<Self::Output> {+ ctx.entity_access_service+ .generate_entity_access_receipt::<EditAccessLevel>(+ &request_context.user_id,+ None,+ &self.document_id,+ EntityType::Document,+ )+ .await+ .map_err(|e| ToolCallError {+ description: "you do not have edit access to this document".to_string(),+ internal_error: e.into(),+ })?;++ let document_token = encode_permission_token(+ Some(request_context.user_id.to_string()),+ self.document_id.clone(),+ AccessLevel::Edit,+ &ctx.document_permission_jwt_secret,+ )+ .map_err(|e| ToolCallError {+ description: "failed to mint document token".to_string(),+ internal_error: e.into(),+ })?;++ // Honor user cancellation: if the request is cancelled mid-edit, drop the+ // in-flight worker call (closing the HTTP connection so the worker aborts+ // its own LLM work) and surface a `cancelled` tool error -- matching how+ // the chat stream renders cancellation for tool calls that never returned.+ let result = tokio::select! {+ _ = request_context.cancel.cancelled() => {+ return Err(ToolCallError {+ description: "cancelled".to_string(),+ internal_error: anyhow::anyhow!("edit cancelled by user. document might be left in a partially edited state."),+ });+ }+ r = ctx.editing.edit(&self.document_id, &document_token, &self.instructions) => r,+ }+ .map_err(|e| ToolCallError {+ description: e.to_string(),+ internal_error: e,+ })?;++ // The worker runs several models on the caller's behalf; record each so+ // their tokens land on the usage ledger (attributed to this user).+ let entity = macro_uuid::string_to_uuid(&self.document_id).ok();+ for u in &result.usage {+ let cx = ai_usage::UsageContext::new(+ ai_usage::AiFeature::AiEditing,+ request_context.user_id.clone(),+ )+ .with_entity(entity);+ ctx.recorder.record(cx.into_event(+ u.model.clone(),+ u.input_tokens as u64,+ u.output_tokens as u64,+ ));+ }++ let summary = if result.clarification.is_some() {+ "Paused for clarification; no edits applied.".to_string()+ } else {+ format!("Applied {} edit(s) to the document.", result.edits_applied)+ };++ Ok(EditDocumentResponse {+ summary,+ clarification: result.clarification,+ })+ }+}diff --git a/rust/cloud-storage/documents/src/inbound/toolset/mod.rs b/rust/cloud-storage/documents/src/inbound/toolset/mod.rsindex cb9c3ce2de..6bcbce68ff 100644--- a/rust/cloud-storage/documents/src/inbound/toolset/mod.rs+++ b/rust/cloud-storage/documents/src/inbound/toolset/mod.rs@@ -1,6 +1,7 @@//! Toolset inbound adapter for Documents.mod create_document;+mod edit_document;mod read_content;mod read_metadata;mod rename_document;@@ -12,9 +13,10 @@ use crate::{domain::create::DocumentCreator,domain::ports::DocumentService,domain::ports::create::DocumentCreationService,+ domain::ports::editing::EditingWorkerService,inbound::toolset::{- create_document::CreateDocument, read_content::ReadContent, read_metadata::ReadMetadata,- rename_document::RenameDocument,+ create_document::CreateDocument, edit_document::EditDocument, read_content::ReadContent,+ read_metadata::ReadMetadata, rename_document::RenameDocument,},outbound::{document_bytes_upload::ReqwestDocumentBytesUploader,@@ -35,6 +37,7 @@ pub type DefaultDocumentToolCreator<DSvc> =pub struct DocumentToolContext<DSvc: DocumentService + DocumentCreationService,ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,> {/// The document service instancepub service: Arc<DSvc>,@@ -49,10 +52,23 @@ pub struct DocumentToolContext</// Backend-owned document creation use case.pub creator: DefaultDocumentToolCreator<DSvc>,++ /// Editing worker service for the EditDocument tool.+ pub editing: Arc<EDSvc>,++ /// JWT secret used to mint document permission tokens for the editing worker.+ pub document_permission_jwt_secret: String,++ /// Records the token usage the editing worker reports. Defaults to a no-op;+ /// the chat path injects the real (Postgres-backed) recorder per request.+ pub recorder: Arc<dyn ai_usage::UsageRecorder>,}-impl<DSvc: DocumentService + DocumentCreationService, ESvc: EntityAccessService> Clone- for DocumentToolContext<DSvc, ESvc>+impl<+ DSvc: DocumentService + DocumentCreationService,+ ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,+> Clone for DocumentToolContext<DSvc, ESvc, EDSvc>{fn clone(&self) -> Self {Self {@@ -61,12 +77,18 @@ impl<DSvc: DocumentService + DocumentCreationService, ESvc: EntityAccessService>lexical_client: self.lexical_client.clone(),sync_service_client: self.sync_service_client.clone(),creator: self.creator.clone(),+ editing: self.editing.clone(),+ document_permission_jwt_secret: self.document_permission_jwt_secret.clone(),+ recorder: self.recorder.clone(),}}}-impl<DSvc: DocumentService + DocumentCreationService, ESvc: EntityAccessService>- DocumentToolContext<DSvc, ESvc>+impl<+ DSvc: DocumentService + DocumentCreationService,+ ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,+> DocumentToolContext<DSvc, ESvc, EDSvc>{/// Create a new document tool contextpub fn new(@@ -74,6 +96,8 @@ impl<DSvc: DocumentService + DocumentCreationService, ESvc: EntityAccessService>entity_access_service: ESvc,lexical_client: LexicalClient,sync_service_client: SyncServiceClient,+ editing: EDSvc,+ document_permission_jwt_secret: String,) -> Self {let service = Arc::new(service);let lexical_client = Arc::new(lexical_client);@@ -93,19 +117,31 @@ impl<DSvc: DocumentService + DocumentCreationService, ESvc: EntityAccessService>lexical_client,sync_service_client,creator,+ editing: Arc::new(editing),+ document_permission_jwt_secret,+ recorder: Arc::new(ai_usage::NoOpUsageRecorder),}}++ /// Set the usage recorder the EditDocument tool logs worker token usage to.+ pub fn with_recorder(mut self, recorder: Arc<dyn ai_usage::UsageRecorder>) -> Self {+ self.recorder = recorder;+ self+ }}/// Create a document toolset-pub fn document_toolset<DSvc, ESvc>() -> AsyncToolCollection<DocumentToolContext<DSvc, ESvc>>+pub fn document_toolset<DSvc, ESvc, EDSvc>()+-> AsyncToolCollection<DocumentToolContext<DSvc, ESvc, EDSvc>>whereDSvc: DocumentService + DocumentCreationService,ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,{AsyncToolCollection::new()- .add_tool::<ReadMetadata, DocumentToolContext<DSvc, ESvc>>()- .add_tool::<ReadContent, DocumentToolContext<DSvc, ESvc>>()- .add_tool::<CreateDocument, DocumentToolContext<DSvc, ESvc>>()- .add_tool::<RenameDocument, DocumentToolContext<DSvc, ESvc>>()+ .add_tool::<ReadMetadata, DocumentToolContext<DSvc, ESvc, EDSvc>>()+ .add_tool::<ReadContent, DocumentToolContext<DSvc, ESvc, EDSvc>>()+ .add_tool::<CreateDocument, DocumentToolContext<DSvc, ESvc, EDSvc>>()+ .add_tool::<RenameDocument, DocumentToolContext<DSvc, ESvc, EDSvc>>()+ .add_tool::<EditDocument, DocumentToolContext<DSvc, ESvc, EDSvc>>()}diff --git a/rust/cloud-storage/documents/src/inbound/toolset/read_content.rs b/rust/cloud-storage/documents/src/inbound/toolset/read_content.rsindex 0c01214475..ea4e3db774 100644--- a/rust/cloud-storage/documents/src/inbound/toolset/read_content.rs+++ b/rust/cloud-storage/documents/src/inbound/toolset/read_content.rs@@ -4,7 +4,7 @@ use std::str::FromStr;use crate::domain::{models::{CommentThread, LocationQueryParams},- ports::{DocumentService, create::DocumentCreationService},+ ports::{DocumentService, create::DocumentCreationService, editing::EditingWorkerService},response::LocationResponseV3,};use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};@@ -90,17 +90,18 @@ pub struct ReadContent {}#[async_trait]-impl<DSvc, ESvc> AsyncTool<DocumentToolContext<DSvc, ESvc>> for ReadContent+impl<DSvc, ESvc, EDSvc> AsyncTool<DocumentToolContext<DSvc, ESvc, EDSvc>> for ReadContentwhereDSvc: DocumentService + DocumentCreationService,ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,{type Output = ReadContentResponse;#[tracing::instrument(skip_all, fields(user_id=?request_context.user_id, document_id=?self.document_id), err)]async fn call(&self,- service_context: ServiceContext<DocumentToolContext<DSvc, ESvc>>,+ service_context: ServiceContext<DocumentToolContext<DSvc, ESvc, EDSvc>>,request_context: RequestContext,) -> ToolResult<Self::Output> {tracing::info!(params=?self, "Read metadata");@@ -207,8 +208,9 @@ whereasync fn get_document_content_from_location<DSvc: DocumentService + DocumentCreationService,ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,>(- service_context: ServiceContext<DocumentToolContext<DSvc, ESvc>>,+ service_context: ServiceContext<DocumentToolContext<DSvc, ESvc, EDSvc>>,document_context: &DocumentBasic,entity_access_receipt: EntityAccessReceipt<ViewAccessLevel>,) -> anyhow::Result<String> {diff --git a/rust/cloud-storage/documents/src/inbound/toolset/read_metadata.rs b/rust/cloud-storage/documents/src/inbound/toolset/read_metadata.rsindex 932500008d..0ad7dd7195 100644--- a/rust/cloud-storage/documents/src/inbound/toolset/read_metadata.rs+++ b/rust/cloud-storage/documents/src/inbound/toolset/read_metadata.rs@@ -1,6 +1,8 @@//! ReadMetadata tool for reading document metadata.-use crate::domain::ports::{DocumentService, create::DocumentCreationService};+use crate::domain::ports::{+ DocumentService, create::DocumentCreationService, editing::EditingWorkerService,+};use crate::domain::response::DocumentMetadataWithContent;use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};use async_trait::async_trait;@@ -43,17 +45,18 @@ pub struct ReadMetadata {}#[async_trait]-impl<DSvc, ESvc> AsyncTool<DocumentToolContext<DSvc, ESvc>> for ReadMetadata+impl<DSvc, ESvc, EDSvc> AsyncTool<DocumentToolContext<DSvc, ESvc, EDSvc>> for ReadMetadatawhereDSvc: DocumentService + DocumentCreationService,ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,{type Output = ReadMetadataResponse;#[tracing::instrument(skip_all, fields(user_id=?request_context.user_id), err)]async fn call(&self,- service_context: ServiceContext<DocumentToolContext<DSvc, ESvc>>,+ service_context: ServiceContext<DocumentToolContext<DSvc, ESvc, EDSvc>>,request_context: RequestContext,) -> ToolResult<Self::Output> {tracing::info!(params=?self, "Read metadata");diff --git a/rust/cloud-storage/documents/src/inbound/toolset/rename_document.rs b/rust/cloud-storage/documents/src/inbound/toolset/rename_document.rsindex c307c8b108..3fdcbb4869 100644--- a/rust/cloud-storage/documents/src/inbound/toolset/rename_document.rs+++ b/rust/cloud-storage/documents/src/inbound/toolset/rename_document.rs@@ -2,7 +2,7 @@use crate::domain::{models::{DocumentError, EditDocumentServiceArgs},- ports::{DocumentService, create::DocumentCreationService},+ ports::{DocumentService, create::DocumentCreationService, editing::EditingWorkerService},};use ai_toolset::{AsyncTool, RequestContext, ServiceContext, ToolCallError, ToolResult};use async_trait::async_trait;@@ -59,17 +59,18 @@ pub struct RenameDocument {}#[async_trait]-impl<DSvc, ESvc> AsyncTool<DocumentToolContext<DSvc, ESvc>> for RenameDocument+impl<DSvc, ESvc, EDSvc> AsyncTool<DocumentToolContext<DSvc, ESvc, EDSvc>> for RenameDocumentwhereDSvc: DocumentService + DocumentCreationService,ESvc: EntityAccessService,+ EDSvc: EditingWorkerService,{type Output = RenameDocumentResponse;#[tracing::instrument(skip_all, fields(user_id=?request_context.user_id, document_id=?self.document_id), err)]async fn call(&self,- service_context: ServiceContext<DocumentToolContext<DSvc, ESvc>>,+ service_context: ServiceContext<DocumentToolContext<DSvc, ESvc, EDSvc>>,request_context: RequestContext,) -> ToolResult<Self::Output> {tracing::info!(params=?self, "Rename document");diff --git a/rust/cloud-storage/documents/src/outbound.rs b/rust/cloud-storage/documents/src/outbound.rsindex 29b2243e03..f765325446 100644--- a/rust/cloud-storage/documents/src/outbound.rs+++ b/rust/cloud-storage/documents/src/outbound.rs@@ -2,6 +2,8 @@#[cfg(feature = "document_create_adapters")]pub mod document_bytes_upload;+#[cfg(feature = "ai_tools")]+pub mod editing_worker_client;#[cfg(feature = "markdown_init")]pub mod markdown_init;#[cfg(feature = "outbound")]diff --git a/rust/cloud-storage/documents/src/outbound/editing_worker_client.rs b/rust/cloud-storage/documents/src/outbound/editing_worker_client.rsnew file mode 100644index 0000000000..8351b1bd20--- /dev/null+++ b/rust/cloud-storage/documents/src/outbound/editing_worker_client.rs@@ -0,0 +1,99 @@+//! Outbound adapter for the AI editing worker.++use crate::domain::ports::editing::{EditResult, EditUsage, EditingWorkerService};+use macro_sync_service_jwt::DocumentPermissionToken;+use reqwest::Client;+use std::sync::Arc;++/// Reqwest-backed client for the AI editing worker.+#[derive(Clone)]+pub struct ReqwestEditingWorkerClient {+ worker_url: String,+ client: Arc<Client>,+}++impl ReqwestEditingWorkerClient {+ /// Construct a new client.+ pub fn new(worker_url: String, client: Arc<Client>) -> Self {+ Self { worker_url, client }+ }++ /// Construct a client backed by a fresh default reqwest client.+ pub fn from_url(worker_url: String) -> Self {+ Self::new(worker_url, Arc::new(Client::new()))+ }+}++impl EditingWorkerService for ReqwestEditingWorkerClient {+ #[tracing::instrument(skip_all, fields(document_id), err)]+ async fn edit(+ &self,+ document_id: &str,+ document_token: &DocumentPermissionToken,+ instructions: &str,+ ) -> anyhow::Result<EditResult> {+ let request_body = serde_json::json!({+ "documentToken": document_token.as_str(),+ "documentId": document_id,+ "prompt": instructions,+ // Each role lists models tried in order: Cerebras primary, with an+ // Anthropic fallback so edits keep working if Cerebras is rate-limited+ // or down.+ "models": {+ "supervisor": [+ { "provider": "anthropic", "model": "claude-sonnet-4-6" },+ { "provider": "openai", "model": "gpt-5.5" },+ ],+ "interpret": [+ { "provider": "cerebras", "model": "zai-glm-4.7" },+ { "provider": "anthropic", "model": "claude-sonnet-4-6" },+ ],+ "coding": [+ { "provider": "cerebras", "model": "gpt-oss-120b" },+ { "provider": "anthropic", "model": "claude-haiku-4-5" },+ ],+ },+ "interpret": true,+ });++ let edit_resp = self+ .client+ .post(format!("{}/edit", self.worker_url))+ .json(&request_body)+ .send()+ .await?;++ let status = edit_resp.status();+ if !status.is_success() {+ // The worker returns `{ "error": "..." }` with the real cause; surface+ // it instead of just the status so failures are debuggable.+ let body = edit_resp.text().await.unwrap_or_default();+ anyhow::bail!("editing worker returned {}: {}", status, body);+ }++ let body = edit_resp.json::<serde_json::Value>().await?;++ // The worker reports `usage` as an array of per-model entries+ // (`{ model, inputTokens, outputTokens }`, camelCase) — one per model it+ // ran (supervisor, interpret, coder).+ let usage = body["usage"]+ .as_array()+ .map(|entries| {+ entries+ .iter()+ .map(|e| EditUsage {+ model: e["model"].as_str().unwrap_or_default().to_owned(),+ input_tokens: e["inputTokens"].as_u64().unwrap_or(0) as u32,+ output_tokens: e["outputTokens"].as_u64().unwrap_or(0) as u32,+ })+ .collect()+ })+ .unwrap_or_default();++ Ok(EditResult {+ edits_applied: body["ops"].as_array().map(|a| a.len()).unwrap_or(0),+ usage,+ clarification: body["clarification"].as_str().map(str::to_owned),+ })+ }+}diff --git a/rust/cloud-storage/macro_db_client/migrations/20260623000000_add_cerebras_pricing.sql b/rust/cloud-storage/macro_db_client/migrations/20260623000000_add_cerebras_pricing.sqlnew file mode 100644index 0000000000..98048188bf--- /dev/null+++ b/rust/cloud-storage/macro_db_client/migrations/20260623000000_add_cerebras_pricing.sql@@ -0,0 +1,5 @@+-- Seed Cerebras model prices (USD per million tokens).+INSERT INTO ai_pricing (model, price_per_million_in, price_per_million_out) VALUES+ ('gpt-oss-120b', 0.35, 0.75),+ ('zai-glm-4.7', 2.25, 2.75)+ON CONFLICT DO NOTHING;diff --git a/rust/cloud-storage/macro_service_urls/src/lib.rs b/rust/cloud-storage/macro_service_urls/src/lib.rsindex 290e519b17..05308db14a 100644--- a/rust/cloud-storage/macro_service_urls/src/lib.rs+++ b/rust/cloud-storage/macro_service_urls/src/lib.rs@@ -590,5 +590,11 @@ service_url! {dev: "https://sync-service-dev3.macroverse.workers.dev",prod: "https://sync-service-prod2.macroverse.workers.dev",},+ /// AI editing worker API URL.+ pub AiEditingWorkerUrl {+ local: "http://localhost:8933",+ dev: "https://ai-editing-worker-dev.macroverse.workers.dev",+ prod: "https://ai-editing-worker.macroverse.workers.dev",+ },}}diff --git a/rust/cloud-storage/macro_sync_service_jwt/Cargo.toml b/rust/cloud-storage/macro_sync_service_jwt/Cargo.tomlnew file mode 100644index 0000000000..927d38b761--- /dev/null+++ b/rust/cloud-storage/macro_sync_service_jwt/Cargo.toml@@ -0,0 +1,12 @@+[package]+edition = "2024"+name = "macro_sync_service_jwt"+publish = false+version = "0.1.0"++# Excluded from workspace-hack in .config/hakari.toml since it needs to compile+# to wasm too for sync service+[dependencies]+derive_more = { version = "2", features = ["display", "from"] }+jsonwebtoken = { workspace = true }+serde = { workspace = true }diff --git a/rust/cloud-storage/macro_sync_service_jwt/src/lib.rs b/rust/cloud-storage/macro_sync_service_jwt/src/lib.rsnew file mode 100644index 0000000000..193443f4d5--- /dev/null+++ b/rust/cloud-storage/macro_sync_service_jwt/src/lib.rs@@ -0,0 +1,65 @@+//! Minting and validation for the document-permission JWTs that authorize the+//! sync service to grant document access.++use jsonwebtoken::{+ Algorithm, DecodingKey, EncodingKey, Header, Validation, decode as jwt_decode,+ encode as jwt_encode,+};+use serde::de::DeserializeOwned;+use serde::{Deserialize, Serialize};++/// JWT `iss` claim value stamped on every minted token.+pub const ISSUER: &str = "document_storage_service";++/// Lifetime of a minted token in seconds (1 hour).+pub const TOKEN_TTL_SECS: usize = 3600;++/// A signed, opaque document-permission JWT: minted by [`encode`] and consumed+/// by the sync service. Wrapping it in a distinct type keeps it from being+/// confused with a raw user bearer (e.g. a FusionAuth token) at call sites that+/// otherwise just pass `&str` around.+#[derive(+ Debug, Clone, PartialEq, Eq, Serialize, Deserialize, derive_more::Display, derive_more::From,+)]+#[serde(transparent)]+#[display("{_0}")]+pub struct DocumentPermissionToken(String);++impl DocumentPermissionToken {+ /// Borrow the token+ pub fn as_str(&self) -> &str {+ &self.0+ }++ /// Consume the wrapper+ pub fn into_inner(self) -> String {+ self.0+ }+}++/// Sign `claims` with `secret` (HS256), returning the minted token.+pub fn encode<C: Serialize>(+ claims: &C,+ secret: &str,+) -> Result<DocumentPermissionToken, jsonwebtoken::errors::Error> {+ let token = jwt_encode(+ &Header::new(Algorithm::HS256),+ claims,+ &EncodingKey::from_secret(secret.as_bytes()),+ )?;+ Ok(DocumentPermissionToken(token))+}++/// Validate `token` against `secret` (HS256) and deserialize its claims into `C`.+/// Uses the default validation, which enforces expiry.+pub fn decode<C: DeserializeOwned>(+ token: &str,+ secret: &str,+) -> Result<C, jsonwebtoken::errors::Error> {+ let data = jwt_decode::<C>(+ token,+ &DecodingKey::from_secret(secret.as_bytes()),+ &Validation::new(Algorithm::HS256),+ )?;+ Ok(data.claims)+}diff --git a/rust/cloud-storage/mcp_auth_proxy/src/inbound/middleware.rs b/rust/cloud-storage/mcp_auth_proxy/src/inbound/middleware.rsindex deea0fafed..f006e54fc9 100644--- a/rust/cloud-storage/mcp_auth_proxy/src/inbound/middleware.rs+++ b/rust/cloud-storage/mcp_auth_proxy/src/inbound/middleware.rs@@ -10,6 +10,10 @@ use axum::{use macro_auth::middleware::decode_jwt::{JwtToken, JwtValidationArgs, handler};use macro_user_id::user_id::MacroUserIdStr;+/// Validated Macro JWT from the `Authorization: Bearer` header.+#[derive(Clone)]+pub struct JwtAccessToken(pub String);+const RESOURCE_METADATA_PATH: &str = "/.well-known/oauth-protected-resource/mcp";fn absolute_resource_metadata_url(request: &Request<Body>) -> String {@@ -104,6 +108,7 @@ pub async fn validate_bearer(};request.extensions_mut().insert(user_id);+ request.extensions_mut().insert(JwtAccessToken(token));next.run(request).await}diff --git a/rust/cloud-storage/mcp_service/src/config.rs b/rust/cloud-storage/mcp_service/src/config.rsindex 5ccc155d78..50de2d26a0 100644--- a/rust/cloud-storage/mcp_service/src/config.rs+++ b/rust/cloud-storage/mcp_service/src/config.rs@@ -48,6 +48,8 @@ env_vars! {/// Base URL of the Macro web app (e.g. `https://macro.com`), used to build/// links to Macro items in MCP responses.pub struct AppBaseUrl;+ /// JWT secret for minting document permission tokens for the editing worker.+ pub struct DocumentPermissionJwtSecretKey;}/// The configuration parameters for the MCP server.@@ -84,6 +86,7 @@ pub struct Config {pub app_base_url: AppBaseUrl,/// The internal api keypub internal_api_key: InternalApiKey,+ pub document_permission_jwt_secret_key: DocumentPermissionJwtSecretKey,}impl Config {diff --git a/rust/cloud-storage/mcp_service/src/context.rs b/rust/cloud-storage/mcp_service/src/context.rsindex 731bf20d7a..3f07df28c3 100644--- a/rust/cloud-storage/mcp_service/src/context.rs+++ b/rust/cloud-storage/mcp_service/src/context.rs@@ -11,7 +11,10 @@ use channels::{use documents::{domain::models::CloudFrontConfig,inbound::toolset::DocumentToolContext,- outbound::{pg_document_repo::PgDocumentRepo, s3_upload_url::S3UploadUrlAdapter},+ outbound::{+ editing_worker_client::ReqwestEditingWorkerClient, pg_document_repo::PgDocumentRepo,+ s3_upload_url::S3UploadUrlAdapter,+ },};use email::domain::ports::ReadonlyEmailPreviewAdapter;use email::domain::service::EmailServiceImpl;@@ -26,7 +29,8 @@ use frecency::domain::services::FrecencyQueryServiceImpl;use frecency::outbound::postgres::FrecencyPgStorage;use macro_auth::middleware::decode_jwt::JwtValidationArgs;use macro_service_urls::{- DocumentStorageServiceUrl, EmailServiceUrl, LexicalServiceUrl, SyncServiceUrl,+ AiEditingWorkerUrl, DocumentStorageServiceUrl, EmailServiceUrl, LexicalServiceUrl,+ SyncServiceUrl,};use mcp_auth_proxy::{domain::service::McpAuthProxyServiceImpl,@@ -125,6 +129,7 @@ async fn build_tool_context(let sync_service_url = SyncServiceUrl::new()?.to_string();let lexical_service_url = LexicalServiceUrl::new()?.to_string();let email_service_url = EmailServiceUrl::new()?.to_string();+ let ai_editing_worker_url = AiEditingWorkerUrl::new()?.to_string();let search_service_client =SearchServiceClient::new(document_storage_service_auth_key, dss_url);@@ -227,6 +232,8 @@ async fn build_tool_context((*entity_access_service).clone(),lexical_client_for_tools,sync_service_client.clone(),+ ReqwestEditingWorkerClient::from_url(ai_editing_worker_url),+ config.document_permission_jwt_secret_key.to_string(),);let properties_tool_context = ai_tools::build_properties_tool_context(properties_service);diff --git a/rust/cloud-storage/mcp_service/src/tool_service.rs b/rust/cloud-storage/mcp_service/src/tool_service.rsindex a93e0a5d34..308cb4e698 100644--- a/rust/cloud-storage/mcp_service/src/tool_service.rs+++ b/rust/cloud-storage/mcp_service/src/tool_service.rs@@ -118,6 +118,7 @@ whereUse ContentSearch and NameSearch to find entities. \Use ReadContent, ReadMetadata, and ReadThread to read them. \Use CreateDocument to create new documents. \+ Use EditDocument to edit existing documents. \Use ListEntities to browse recent items.\n\n{}",prompt::mcp_instructions(base_url),));diff --git a/rust/cloud-storage/model/Cargo.toml b/rust/cloud-storage/model/Cargo.tomlindex 7227f09ba6..1fceb7b8df 100644--- a/rust/cloud-storage/model/Cargo.toml+++ b/rust/cloud-storage/model/Cargo.toml@@ -12,7 +12,7 @@ base64 = { workspace = true }chrono = { workspace = true }document_sub_type = { path = "../document_sub_type" }doppleganger = { workspace = true, features = ["chrono", "uuid"] }-macro_user_id = { path = "../macro_user_id" }+macro_user_id = { path = "../macro_user_id", features = ["schema"] }model-error-response = { path = "../model-error-response" }model_file_type = { path = "../model_file_type", features = ["utoipa"] }model_user = { path = "../model_user", features = ["axum"] }diff --git a/rust/cloud-storage/model/src/document/mod.rs b/rust/cloud-storage/model/src/document/mod.rsindex d583734355..710563b55c 100644--- a/rust/cloud-storage/model/src/document/mod.rs+++ b/rust/cloud-storage/model/src/document/mod.rs@@ -26,7 +26,7 @@ use models_permissions::share_permission::access_level::AccessLevel;pub struct DocumentPermissionsToken {/// The users id if present#[serde(skip_serializing_if = "Option::is_none")]- pub user_id: Option<String>,+ pub user_id: Option<MacroUserIdStr<'static>>,/// The document idpub document_id: String,/// The access level of the user for the documentdiff --git a/rust/sync-service/Cargo.lock b/rust/sync-service/Cargo.lockindex aa077b2d44..286239c682 100644--- a/rust/sync-service/Cargo.lock+++ b/rust/sync-service/Cargo.lock@@ -194,6 +194,15 @@ version = "0.9.6"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"+[[package]]+name = "convert_case"+version = "0.10.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "633458d4ef8c78b72454de2d54fd6ab2e60f9e02be22f3c6104cdc8a4e0fceb9"+dependencies = [+ "unicode-segmentation",+]+[[package]]name = "cpufeatures"version = "0.2.17"@@ -324,6 +333,29 @@ dependencies = ["syn 2.0.114",]+[[package]]+name = "derive_more"+version = "2.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134"+dependencies = [+ "derive_more-impl",+]++[[package]]+name = "derive_more-impl"+version = "2.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb"+dependencies = [+ "convert_case",+ "proc-macro2",+ "quote",+ "rustc_version",+ "syn 2.0.114",+ "unicode-xid",+]+[[package]]name = "diff"version = "0.1.13"@@ -1151,6 +1183,15 @@ dependencies = ["twox-hash",]+[[package]]+name = "macro_sync_service_jwt"+version = "0.1.0"+dependencies = [+ "derive_more",+ "jsonwebtoken",+ "serde",+]+[[package]]name = "matchers"version = "0.2.0"@@ -1964,8 +2005,8 @@ name = "sync_service"version = "0.1.0"dependencies = ["bebop",- "jsonwebtoken","loro",+ "macro_sync_service_jwt","matchit 0.8.6","serde","serde_json",@@ -2210,6 +2251,18 @@ version = "1.0.22"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"+[[package]]+name = "unicode-segmentation"+version = "1.13.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8"++[[package]]+name = "unicode-xid"+version = "0.2.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"+[[package]]name = "url"version = "2.5.8"diff --git a/rust/sync-service/Cargo.toml b/rust/sync-service/Cargo.tomlindex fc6df9dc05..ca4f86bf15 100644--- a/rust/sync-service/Cargo.toml+++ b/rust/sync-service/Cargo.toml@@ -52,6 +52,6 @@ serde = { version = "1.0.219", features = ["derive"] }wasm-bindgen = "0.2.100"uuid = { version = "1.16.0", features = ["v4", "js"] }loro = "1.13"-jsonwebtoken = { git = "https://github.com/whutchinson98/jsonwebtoken", features = ["rust_crypto"] }+macro_sync_service_jwt = { path = "../cloud-storage/macro_sync_service_jwt" }matchit = "0.8.4"diff --git a/rust/sync-service/package-lock.json b/rust/sync-service/package-lock.jsonindex 75f8e16240..fdb501b5d5 100644--- a/rust/sync-service/package-lock.json+++ b/rust/sync-service/package-lock.json@@ -14,7 +14,7 @@"@types/jsonwebtoken": "^9.0.9","bebop-tools": "^3.1.3","miniflare": "^4.20260617.0",- "vitest": "^3.0.8",+ "vitest": "^4.1.0","wrangler": "^4.102.0"}},@@ -641,372 +641,38 @@"node": ">=12"}},- "node_modules/@emnapi/runtime": {+ "node_modules/@emnapi/core": {"version": "1.11.1",- "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.11.1.tgz",- "integrity": "sha512-vgj7R3y3Wgx24IQaGPA/R6YFXLHVMOZ0uVEyIQPaWs+rd1AzfEMXlAC22FYwO1XkKR6NPsq7mUandH8oIRdZFw==",+ "resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.11.1.tgz",+ "integrity": "sha512-RSvbQmHzdKzNsLYa/wHrbc3KN4sYLKAdPZxqiM2HATqv/SBk2/ENSHpvXGaLOMcsAyz0poEGqkmmKYG3OWiJEQ==","dev": true,"license": "MIT","optional": true,"dependencies": {+ "@emnapi/wasi-threads": "1.2.2","tslib": "^2.4.0"}},- "node_modules/@esbuild/aix-ppc64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.25.4.tgz",- "integrity": "sha512-1VCICWypeQKhVbE9oW/sJaAmjLxhVqacdkvPLEjwlttjfwENRSClS8EjBz0KzRyFSCPDIkuXW34Je/vk7zdB7Q==",- "cpu": [- "ppc64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "aix"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/android-arm": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.25.4.tgz",- "integrity": "sha512-QNdQEps7DfFwE3hXiU4BZeOV68HHzYwGd0Nthhd3uCkkEKK7/R6MTgM0P7H7FAs5pU/DIWsviMmEGxEoxIZ+ZQ==",- "cpu": [- "arm"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "android"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/android-arm64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.25.4.tgz",- "integrity": "sha512-bBy69pgfhMGtCnwpC/x5QhfxAz/cBgQ9enbtwjf6V9lnPI/hMyT9iWpR1arm0l3kttTr4L0KSLpKmLp/ilKS9A==",- "cpu": [- "arm64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "android"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/android-x64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.25.4.tgz",- "integrity": "sha512-TVhdVtQIFuVpIIR282btcGC2oGQoSfZfmBdTip2anCaVYcqWlZXGcdcKIUklfX2wj0JklNYgz39OBqh2cqXvcQ==",- "cpu": [- "x64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "android"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/darwin-arm64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.25.4.tgz",- "integrity": "sha512-Y1giCfM4nlHDWEfSckMzeWNdQS31BQGs9/rouw6Ub91tkK79aIMTH3q9xHvzH8d0wDru5Ci0kWB8b3up/nl16g==",- "cpu": [- "arm64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "darwin"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/darwin-x64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.25.4.tgz",- "integrity": "sha512-CJsry8ZGM5VFVeyUYB3cdKpd/H69PYez4eJh1W/t38vzutdjEjtP7hB6eLKBoOdxcAlCtEYHzQ/PJ/oU9I4u0A==",- "cpu": [- "x64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "darwin"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/freebsd-arm64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.25.4.tgz",- "integrity": "sha512-yYq+39NlTRzU2XmoPW4l5Ifpl9fqSk0nAJYM/V/WUGPEFfek1epLHJIkTQM6bBs1swApjO5nWgvr843g6TjxuQ==",- "cpu": [- "arm64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "freebsd"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/freebsd-x64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.25.4.tgz",- "integrity": "sha512-0FgvOJ6UUMflsHSPLzdfDnnBBVoCDtBTVyn/MrWloUNvq/5SFmh13l3dvgRPkDihRxb77Y17MbqbCAa2strMQQ==",- "cpu": [- "x64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "freebsd"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-arm": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.25.4.tgz",- "integrity": "sha512-kro4c0P85GMfFYqW4TWOpvmF8rFShbWGnrLqlzp4X1TNWjRY3JMYUfDCtOxPKOIY8B0WC8HN51hGP4I4hz4AaQ==",- "cpu": [- "arm"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-arm64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.25.4.tgz",- "integrity": "sha512-+89UsQTfXdmjIvZS6nUnOOLoXnkUTB9hR5QAeLrQdzOSWZvNSAXAtcRDHWtqAUtAmv7ZM1WPOOeSxDzzzMogiQ==",- "cpu": [- "arm64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-ia32": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.25.4.tgz",- "integrity": "sha512-yTEjoapy8UP3rv8dB0ip3AfMpRbyhSN3+hY8mo/i4QXFeDxmiYbEKp3ZRjBKcOP862Ua4b1PDfwlvbuwY7hIGQ==",- "cpu": [- "ia32"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-loong64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.25.4.tgz",- "integrity": "sha512-NeqqYkrcGzFwi6CGRGNMOjWGGSYOpqwCjS9fvaUlX5s3zwOtn1qwg1s2iE2svBe4Q/YOG1q6875lcAoQK/F4VA==",- "cpu": [- "loong64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-mips64el": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.25.4.tgz",- "integrity": "sha512-IcvTlF9dtLrfL/M8WgNI/qJYBENP3ekgsHbYUIzEzq5XJzzVEV/fXY9WFPfEEXmu3ck2qJP8LG/p3Q8f7Zc2Xg==",- "cpu": [- "mips64el"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-ppc64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.25.4.tgz",- "integrity": "sha512-HOy0aLTJTVtoTeGZh4HSXaO6M95qu4k5lJcH4gxv56iaycfz1S8GO/5Jh6X4Y1YiI0h7cRyLi+HixMR+88swag==",- "cpu": [- "ppc64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-riscv64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.25.4.tgz",- "integrity": "sha512-i8JUDAufpz9jOzo4yIShCTcXzS07vEgWzyX3NH2G7LEFVgrLEhjwL3ajFE4fZI3I4ZgiM7JH3GQ7ReObROvSUA==",- "cpu": [- "riscv64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-s390x": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.25.4.tgz",- "integrity": "sha512-jFnu+6UbLlzIjPQpWCNh5QtrcNfMLjgIavnwPQAfoGx4q17ocOU9MsQ2QVvFxwQoWpZT8DvTLooTvmOQXkO51g==",- "cpu": [- "s390x"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/linux-x64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.25.4.tgz",- "integrity": "sha512-6e0cvXwzOnVWJHq+mskP8DNSrKBr1bULBvnFLpc1KY+d+irZSgZ02TGse5FsafKS5jg2e4pbvK6TPXaF/A6+CA==",- "cpu": [- "x64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/netbsd-arm64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.25.4.tgz",- "integrity": "sha512-vUnkBYxZW4hL/ie91hSqaSNjulOnYXE1VSLusnvHg2u3jewJBz3YzB9+oCw8DABeVqZGg94t9tyZFoHma8gWZQ==",- "cpu": [- "arm64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "netbsd"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/netbsd-x64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.25.4.tgz",- "integrity": "sha512-XAg8pIQn5CzhOB8odIcAm42QsOfa98SBeKUdo4xa8OvX8LbMZqEtgeWE9P/Wxt7MlG2QqvjGths+nq48TrUiKw==",- "cpu": [- "x64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "netbsd"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/openbsd-arm64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.25.4.tgz",- "integrity": "sha512-Ct2WcFEANlFDtp1nVAXSNBPDxyU+j7+tId//iHXU2f/lN5AmO4zLyhDcpR5Cz1r08mVxzt3Jpyt4PmXQ1O6+7A==",- "cpu": [- "arm64"- ],+ "node_modules/@emnapi/runtime": {+ "version": "1.11.1",+ "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.11.1.tgz",+ "integrity": "sha512-vgj7R3y3Wgx24IQaGPA/R6YFXLHVMOZ0uVEyIQPaWs+rd1AzfEMXlAC22FYwO1XkKR6NPsq7mUandH8oIRdZFw==","dev": true,"license": "MIT","optional": true,- "os": [- "openbsd"- ],- "engines": {- "node": ">=18"+ "dependencies": {+ "tslib": "^2.4.0"}},- "node_modules/@esbuild/openbsd-x64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.25.4.tgz",- "integrity": "sha512-xAGGhyOQ9Otm1Xu8NT1ifGLnA6M3sJxZ6ixylb+vIUVzvvd6GOALpwQrYrtlPouMqd/vSbgehz6HaVk4+7Afhw==",- "cpu": [- "x64"- ],+ "node_modules/@emnapi/wasi-threads": {+ "version": "1.2.2",+ "resolved": "https://registry.npmjs.org/@emnapi/wasi-threads/-/wasi-threads-1.2.2.tgz",+ "integrity": "sha512-c95qOXkHdydNKhscBTebqEC1CVAZpyqOfVfBzQ1qgzyl3gfeldUjIggDbIZgDKsHLgnsM+igH7TJ/eAasaVuMA==","dev": true,"license": "MIT","optional": true,- "os": [- "openbsd"- ],- "engines": {- "node": ">=18"+ "dependencies": {+ "tslib": "^2.4.0"}},"node_modules/@esbuild/openharmony-arm64": {@@ -1026,74 +692,6 @@"node": ">=18"}},- "node_modules/@esbuild/sunos-x64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.25.4.tgz",- "integrity": "sha512-Mw+tzy4pp6wZEK0+Lwr76pWLjrtjmJyUB23tHKqEDP74R3q95luY/bXqXZeYl4NYlvwOqoRKlInQialgCKy67Q==",- "cpu": [- "x64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "sunos"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/win32-arm64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.25.4.tgz",- "integrity": "sha512-AVUP428VQTSddguz9dO9ngb+E5aScyg7nOeJDrF1HPYu555gmza3bDGMPhmVXL8svDSoqPCsCPjb265yG/kLKQ==",- "cpu": [- "arm64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "win32"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/win32-ia32": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.25.4.tgz",- "integrity": "sha512-i1sW+1i+oWvQzSgfRcxxG2k4I9n3O9NRqy8U+uugaT2Dy7kLO9Y7wI72haOahxceMX8hZAzgGou1FhndRldxRg==",- "cpu": [- "ia32"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "win32"- ],- "engines": {- "node": ">=18"- }- },- "node_modules/@esbuild/win32-x64": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.25.4.tgz",- "integrity": "sha512-nOT2vZNw6hJ+z43oP1SPea/G/6AbN6X+bGNhNuq8NtRHy4wsMhw765IKLNmnjek7GvjWBYQ8Q5VBoYTFg9y1UQ==",- "cpu": [- "x64"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "win32"- ],- "engines": {- "node": ">=18"- }- },"node_modules/@img/colour": {"version": "1.1.0","resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz",@@ -1655,9 +1253,38 @@"integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==","dev": true,"license": "MIT",- "dependencies": {- "@jridgewell/resolve-uri": "^3.0.3",- "@jridgewell/sourcemap-codec": "^1.4.10"+ "dependencies": {+ "@jridgewell/resolve-uri": "^3.0.3",+ "@jridgewell/sourcemap-codec": "^1.4.10"+ }+ },+ "node_modules/@napi-rs/wasm-runtime": {+ "version": "1.1.6",+ "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-1.1.6.tgz",+ "integrity": "sha512-ZLv/JdUfkvOy9eCnnBaGfiO+XimbjebAeO+MRQqD/B+FR1tnRN0tpKSJHRbE8sFfS6aqsXZ67TQjfwfsxULVbg==",+ "dev": true,+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "@tybys/wasm-util": "^0.10.3"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ },+ "peerDependencies": {+ "@emnapi/core": "^1.7.1",+ "@emnapi/runtime": "^1.7.1"+ }+ },+ "node_modules/@oxc-project/types": {+ "version": "0.138.0",+ "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.138.0.tgz",+ "integrity": "sha512-1a7ZKmrRTCoN1XMZ4L0PyyqrMnrNlLyPuOkdSX2MZg7IiIGRUyurNhAm73ptDOraoBcIordsIGKNPKUzy3ZmfA==",+ "dev": true,+ "license": "MIT",+ "funding": {+ "url": "https://github.com/sponsors/Boshen"}},"node_modules/@poppinss/colors": {@@ -1689,24 +1316,10 @@"dev": true,"license": "MIT"},- "node_modules/@rollup/rollup-android-arm-eabi": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm-eabi/-/rollup-android-arm-eabi-4.59.0.tgz",- "integrity": "sha512-upnNBkA6ZH2VKGcBj9Fyl9IGNPULcjXRlg0LLeaioQWueH30p6IXtJEbKAgvyv+mJaMxSm1l6xwDXYjpEMiLMg==",- "cpu": [- "arm"- ],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "android"- ]- },- "node_modules/@rollup/rollup-android-arm64": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-android-arm64/-/rollup-android-arm64-4.59.0.tgz",- "integrity": "sha512-hZ+Zxj3SySm4A/DylsDKZAeVg0mvi++0PYVceVyX7hemkw7OreKdCvW2oQ3T1FMZvCaQXqOTHb8qmBShoqk69Q==",+ "node_modules/@rolldown/binding-android-arm64": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.1.4.tgz",+ "integrity": "sha512-EZLpf/8y7GXkkra90ML47kzik/GMP3EMcE9bPyHmRfxLC6z9+aW5A8poCsoxjrT5GfEcNAAvWwUHjvP1pUQkfw==","cpu": ["arm64"],@@ -1715,12 +1328,15 @@"optional": true,"os": ["android"- ]+ ],+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-darwin-arm64": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-arm64/-/rollup-darwin-arm64-4.59.0.tgz",- "integrity": "sha512-W2Psnbh1J8ZJw0xKAd8zdNgF9HRLkdWwwdWqubSVk0pUuQkoHnv7rx4GiF9rT4t5DIZGAsConRE3AxCdJ4m8rg==",+ "node_modules/@rolldown/binding-darwin-arm64": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.1.4.tgz",+ "integrity": "sha512-aUi+HBvmYb7j8krl1+qJgkG8C17fO79gk3c+jPw4S8glRFc1DTija9S3EyaTSQUm5GJXYKDAsugBEhFHH2vYiQ==","cpu": ["arm64"],@@ -1729,12 +1345,15 @@"optional": true,"os": ["darwin"- ]+ ],+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-darwin-x64": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-darwin-x64/-/rollup-darwin-x64-4.59.0.tgz",- "integrity": "sha512-ZW2KkwlS4lwTv7ZVsYDiARfFCnSGhzYPdiOU4IM2fDbL+QGlyAbjgSFuqNRbSthybLbIJ915UtZBtmuLrQAT/w==",+ "node_modules/@rolldown/binding-darwin-x64": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.1.4.tgz",+ "integrity": "sha512-F7hHC3gwY11+vByKPRWqwGbeXWVgKmL+pTGCinaEhdihzBV2aQ0fvZOch9cXYUOKuKKq429HeYXOqQLc7wFCEg==","cpu": ["x64"],@@ -1743,26 +1362,15 @@"optional": true,"os": ["darwin"- ]- },- "node_modules/@rollup/rollup-freebsd-arm64": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-arm64/-/rollup-freebsd-arm64-4.59.0.tgz",- "integrity": "sha512-EsKaJ5ytAu9jI3lonzn3BgG8iRBjV4LxZexygcQbpiU0wU0ATxhNVEpXKfUa0pS05gTcSDMKpn3Sx+QB9RlTTA==",- "cpu": [- "arm64"],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "freebsd"- ]+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-freebsd-x64": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-freebsd-x64/-/rollup-freebsd-x64-4.59.0.tgz",- "integrity": "sha512-d3DuZi2KzTMjImrxoHIAODUZYoUUMsuUiY4SRRcJy6NJoZ6iIqWnJu9IScV9jXysyGMVuW+KNzZvBLOcpdl3Vg==",+ "node_modules/@rolldown/binding-freebsd-x64": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.1.4.tgz",+ "integrity": "sha512-sI5yw+7s92SK6odiEhD5lKCBlWcpjHS5qyqpVQbZAJ0fIzEUXrmbl3DH2ybR3PZogulNJF+COLtmA8hUfvkCCQ==","cpu": ["x64"],@@ -1771,26 +1379,15 @@"optional": true,"os": ["freebsd"- ]- },- "node_modules/@rollup/rollup-linux-arm-gnueabihf": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-gnueabihf/-/rollup-linux-arm-gnueabihf-4.59.0.tgz",- "integrity": "sha512-t4ONHboXi/3E0rT6OZl1pKbl2Vgxf9vJfWgmUoCEVQVxhW6Cw/c8I6hbbu7DAvgp82RKiH7TpLwxnJeKv2pbsw==",- "cpu": [- "arm"],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ]+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-linux-arm-musleabihf": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm-musleabihf/-/rollup-linux-arm-musleabihf-4.59.0.tgz",- "integrity": "sha512-CikFT7aYPA2ufMD086cVORBYGHffBo4K8MQ4uPS/ZnY54GKj36i196u8U+aDVT2LX4eSMbyHtyOh7D7Zvk2VvA==",+ "node_modules/@rolldown/binding-linux-arm-gnueabihf": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.1.4.tgz",+ "integrity": "sha512-mCi0OKgEieFircrtVYmQAFGszRtMnZ6fpZAXrxanXAu7lqZcsK1E1RAaZNG0uKAnxox3B1f4EyQNnoyMfN1vAA==","cpu": ["arm"],@@ -1799,180 +1396,135 @@"optional": true,"os": ["linux"- ]- },- "node_modules/@rollup/rollup-linux-arm64-gnu": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-gnu/-/rollup-linux-arm64-gnu-4.59.0.tgz",- "integrity": "sha512-jYgUGk5aLd1nUb1CtQ8E+t5JhLc9x5WdBKew9ZgAXg7DBk0ZHErLHdXM24rfX+bKrFe+Xp5YuJo54I5HFjGDAA==",- "cpu": [- "arm64"],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ]+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-linux-arm64-musl": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-arm64-musl/-/rollup-linux-arm64-musl-4.59.0.tgz",- "integrity": "sha512-peZRVEdnFWZ5Bh2KeumKG9ty7aCXzzEsHShOZEFiCQlDEepP1dpUl/SrUNXNg13UmZl+gzVDPsiCwnV1uI0RUA==",+ "node_modules/@rolldown/binding-linux-arm64-gnu": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.1.4.tgz",+ "integrity": "sha512-B9Ial3Kv5sh0SHnB1g/QWcUQCEvCF6QKGAl4zXypYj65mVI+B4AhFBwPtSN7pDrJeIx8Z7zdy4ntx+wQABom7w==","cpu": ["arm64"],"dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ]- },- "node_modules/@rollup/rollup-linux-loong64-gnu": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-gnu/-/rollup-linux-loong64-gnu-4.59.0.tgz",- "integrity": "sha512-gbUSW/97f7+r4gHy3Jlup8zDG190AuodsWnNiXErp9mT90iCy9NKKU0Xwx5k8VlRAIV2uU9CsMnEFg/xXaOfXg==",- "cpu": [- "loong64"+ "libc": [+ "glibc"],- "dev": true,"license": "MIT","optional": true,"os": ["linux"- ]- },- "node_modules/@rollup/rollup-linux-loong64-musl": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-loong64-musl/-/rollup-linux-loong64-musl-4.59.0.tgz",- "integrity": "sha512-yTRONe79E+o0FWFijasoTjtzG9EBedFXJMl888NBEDCDV9I2wGbFFfJQQe63OijbFCUZqxpHz1GzpbtSFikJ4Q==",- "cpu": [- "loong64"],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ]+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-linux-ppc64-gnu": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-gnu/-/rollup-linux-ppc64-gnu-4.59.0.tgz",- "integrity": "sha512-sw1o3tfyk12k3OEpRddF68a1unZ5VCN7zoTNtSn2KndUE+ea3m3ROOKRCZxEpmT9nsGnogpFP9x6mnLTCaoLkA==",+ "node_modules/@rolldown/binding-linux-arm64-musl": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.1.4.tgz",+ "integrity": "sha512-lZVym0PuHE1KZ22gmFTC15lAkrg9iTszR617oYRB/iPY1A56ywoJzVKOJBKaot5RiikCObmur6pogpse3gRcng==","cpu": [- "ppc64"+ "arm64"],"dev": true,+ "libc": [+ "musl"+ ],"license": "MIT","optional": true,"os": ["linux"- ]+ ],+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-linux-ppc64-musl": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-ppc64-musl/-/rollup-linux-ppc64-musl-4.59.0.tgz",- "integrity": "sha512-+2kLtQ4xT3AiIxkzFVFXfsmlZiG5FXYW7ZyIIvGA7Bdeuh9Z0aN4hVyXS/G1E9bTP/vqszNIN/pUKCk/BTHsKA==",+ "node_modules/@rolldown/binding-linux-ppc64-gnu": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.1.4.tgz",+ "integrity": "sha512-t2DNiLJWNTbnEHyUzTumldML6ET4/g16467LZoDDJ3tSxGvguL5/NyC2lCsNKuyRycg9XeDQF5SSv+TNOhQEXg==","cpu": ["ppc64"],"dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ]- },- "node_modules/@rollup/rollup-linux-riscv64-gnu": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-gnu/-/rollup-linux-riscv64-gnu-4.59.0.tgz",- "integrity": "sha512-NDYMpsXYJJaj+I7UdwIuHHNxXZ/b/N2hR15NyH3m2qAtb/hHPA4g4SuuvrdxetTdndfj9b1WOmy73kcPRoERUg==",- "cpu": [- "riscv64"+ "libc": [+ "glibc"],- "dev": true,"license": "MIT","optional": true,"os": ["linux"- ]- },- "node_modules/@rollup/rollup-linux-riscv64-musl": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-riscv64-musl/-/rollup-linux-riscv64-musl-4.59.0.tgz",- "integrity": "sha512-nLckB8WOqHIf1bhymk+oHxvM9D3tyPndZH8i8+35p/1YiVoVswPid2yLzgX7ZJP0KQvnkhM4H6QZ5m0LzbyIAg==",- "cpu": [- "riscv64"],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "linux"- ]+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-linux-s390x-gnu": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-s390x-gnu/-/rollup-linux-s390x-gnu-4.59.0.tgz",- "integrity": "sha512-oF87Ie3uAIvORFBpwnCvUzdeYUqi2wY6jRFWJAy1qus/udHFYIkplYRW+wo+GRUP4sKzYdmE1Y3+rY5Gc4ZO+w==",+ "node_modules/@rolldown/binding-linux-s390x-gnu": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.1.4.tgz",+ "integrity": "sha512-0WIRnL1Uw4BvTZRLQt+PVgo6ZKTJadlC2btP+/EOXv2f/DWbY0rEgl+y834mIVwP1FkTlWVTrGGJXf12lru7EQ==","cpu": ["s390x"],"dev": true,+ "libc": [+ "glibc"+ ],"license": "MIT","optional": true,"os": ["linux"- ]+ ],+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-linux-x64-gnu": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-gnu/-/rollup-linux-x64-gnu-4.59.0.tgz",- "integrity": "sha512-3AHmtQq/ppNuUspKAlvA8HtLybkDflkMuLK4DPo77DfthRb71V84/c4MlWJXixZz4uruIH4uaa07IqoAkG64fg==",+ "node_modules/@rolldown/binding-linux-x64-gnu": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.1.4.tgz",+ "integrity": "sha512-JWtGshGfX+oENAKonoNkqEJX+7hC8yfhi9GUyPX1VX4mdh1y5r+ZiJLR5XzAB0aoP6s/PcILsGjKq8O0mm24bw==","cpu": ["x64"],"dev": true,+ "libc": [+ "glibc"+ ],"license": "MIT","optional": true,"os": ["linux"- ]+ ],+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-linux-x64-musl": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-linux-x64-musl/-/rollup-linux-x64-musl-4.59.0.tgz",- "integrity": "sha512-2UdiwS/9cTAx7qIUZB/fWtToJwvt0Vbo0zmnYt7ED35KPg13Q0ym1g442THLC7VyI6JfYTP4PiSOWyoMdV2/xg==",+ "node_modules/@rolldown/binding-linux-x64-musl": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.1.4.tgz",+ "integrity": "sha512-rT6yQcxUuXs4CnbofqwHRRV0iem349rLMYpTjkgQGLjrY4ado/eDzwPZPTCgTOlF6Nkp8NEv70yLMTn6qkWxsQ==","cpu": ["x64"],"dev": true,+ "libc": [+ "musl"+ ],"license": "MIT","optional": true,"os": ["linux"- ]- },- "node_modules/@rollup/rollup-openbsd-x64": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-openbsd-x64/-/rollup-openbsd-x64-4.59.0.tgz",- "integrity": "sha512-M3bLRAVk6GOwFlPTIxVBSYKUaqfLrn8l0psKinkCFxl4lQvOSz8ZrKDz2gxcBwHFpci0B6rttydI4IpS4IS/jQ==",- "cpu": [- "x64"],- "dev": true,- "license": "MIT",- "optional": true,- "os": [- "openbsd"- ]+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-openharmony-arm64": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-openharmony-arm64/-/rollup-openharmony-arm64-4.59.0.tgz",- "integrity": "sha512-tt9KBJqaqp5i5HUZzoafHZX8b5Q2Fe7UjYERADll83O4fGqJ49O1FsL6LpdzVFQcpwvnyd0i+K/VSwu/o/nWlA==",+ "node_modules/@rolldown/binding-openharmony-arm64": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.1.4.tgz",+ "integrity": "sha512-KXMGoboq5cyaCQjDA4GLuRiOwBQ0EyFnJoVViLeZ45/3rFItRODEr+NdsBcVpll40hhNArlm/speWGRvj08LzA==","cpu": ["arm64"],@@ -1981,40 +1533,51 @@"optional": true,"os": ["openharmony"- ]+ ],+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-win32-arm64-msvc": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-arm64-msvc/-/rollup-win32-arm64-msvc-4.59.0.tgz",- "integrity": "sha512-V5B6mG7OrGTwnxaNUzZTDTjDS7F75PO1ae6MJYdiMu60sq0CqN5CVeVsbhPxalupvTX8gXVSU9gq+Rx1/hvu6A==",+ "node_modules/@rolldown/binding-wasm32-wasi": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-wasm32-wasi/-/binding-wasm32-wasi-1.1.4.tgz",+ "integrity": "sha512-5K83rb36oJiY7BCyE9zLZtGcPV4g5wvq+xwdO0XPIwDVZI8cyB/AUjkNXGb92/rnmezEkjMOpgY61rtwjQtFwg==","cpu": [- "arm64"+ "wasm32"],"dev": true,"license": "MIT","optional": true,- "os": [- "win32"- ]+ "dependencies": {+ "@emnapi/core": "1.11.1",+ "@emnapi/runtime": "1.11.1",+ "@napi-rs/wasm-runtime": "^1.1.6"+ },+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-win32-ia32-msvc": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-ia32-msvc/-/rollup-win32-ia32-msvc-4.59.0.tgz",- "integrity": "sha512-UKFMHPuM9R0iBegwzKF4y0C4J9u8C6MEJgFuXTBerMk7EJ92GFVFYBfOZaSGLu6COf7FxpQNqhNS4c4icUPqxA==",+ "node_modules/@rolldown/binding-win32-arm64-msvc": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.1.4.tgz",+ "integrity": "sha512-PnWBtw3TV5KOg69HQQDR0mnQuyCmSGR2pAB4DC1rPF808fgKeTUMj2EOEyKATpgiuxuR5APQmiDO7PDgEjTFSA==","cpu": [- "ia32"+ "arm64"],"dev": true,"license": "MIT","optional": true,"os": ["win32"- ]+ ],+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }},- "node_modules/@rollup/rollup-win32-x64-gnu": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-gnu/-/rollup-win32-x64-gnu-4.59.0.tgz",- "integrity": "sha512-laBkYlSS1n2L8fSo1thDNGrCTQMmxjYY5G0WFWjFFYZkKPjsMBsgJfGf4TLxXrF6RyhI60L8TMOjBMvXiTcxeA==",+ "node_modules/@rolldown/binding-win32-x64-msvc": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.1.4.tgz",+ "integrity": "sha512-M1lpniBePobTfsa7Ks9a199e1akxsXn+GYBUKsEzv3YFzOm1HJAMNwKI3qr0Zq+mxwx9gOZoTdP1yXRYsZUocQ==","cpu": ["x64"],@@ -2023,21 +1586,17 @@"optional": true,"os": ["win32"- ]- },- "node_modules/@rollup/rollup-win32-x64-msvc": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/@rollup/rollup-win32-x64-msvc/-/rollup-win32-x64-msvc-4.59.0.tgz",- "integrity": "sha512-2HRCml6OztYXyJXAvdDXPKcawukWY2GpR5/nxKp4iBgiO3wcoEGkAaqctIbZcNB6KlUQBIqt8VYkNSj2397EfA==",- "cpu": [- "x64"],+ "engines": {+ "node": "^20.19.0 || >=22.12.0"+ }+ },+ "node_modules/@rolldown/pluginutils": {+ "version": "1.0.1",+ "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.1.tgz",+ "integrity": "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw==","dev": true,- "license": "MIT",- "optional": true,- "os": [- "win32"- ]+ "license": "MIT"},"node_modules/@sindresorhus/is": {"version": "7.2.0",@@ -2059,10 +1618,46 @@"dev": true,"license": "CC0-1.0"},+ "node_modules/@standard-schema/spec": {+ "version": "1.1.0",+ "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz",+ "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/@tybys/wasm-util": {+ "version": "0.10.3",+ "resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.10.3.tgz",+ "integrity": "sha512-F3fo1MYrRJYL3zER0OUOmkutjr1Vp23m7OsSgp7nq4SP6OqX6C/56XFIPAl5bt3zaBRjmW7SGz3u/6LwFpYcOg==",+ "dev": true,+ "license": "MIT",+ "optional": true,+ "dependencies": {+ "tslib": "^2.4.0"+ }+ },+ "node_modules/@types/chai": {+ "version": "5.2.3",+ "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz",+ "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@types/deep-eql": "*",+ "assertion-error": "^2.0.1"+ }+ },+ "node_modules/@types/deep-eql": {+ "version": "4.0.2",+ "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz",+ "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==",+ "dev": true,+ "license": "MIT"+ },"node_modules/@types/estree": {- "version": "1.0.8",- "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz",- "integrity": "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==",+ "version": "1.0.9",+ "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.9.tgz",+ "integrity": "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg==","dev": true,"license": "MIT"},@@ -2095,141 +1690,56 @@}},"node_modules/@vitest/expect": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-3.0.8.tgz",- "integrity": "sha512-Xu6TTIavTvSSS6LZaA3EebWFr6tsoXPetOWNMOlc7LO88QVVBwq2oQWBoDiLCN6YTvNYsGSjqOO8CAdjom5DCQ==",+ "version": "4.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-4.1.9.tgz",+ "integrity": "sha512-vl/rYsUKcBr3SnQn166+XR5ZQcgMx3DQhFWdfli/cWpLnLUmbxZvyrJZotLFUryib+LtArYMSTJ5RbQ57ZqrlA==","dev": true,"license": "MIT","dependencies": {- "@vitest/spy": "3.0.8",- "@vitest/utils": "3.0.8",- "chai": "^5.2.0",- "tinyrainbow": "^2.0.0"+ "@standard-schema/spec": "^1.1.0",+ "@types/chai": "^5.2.2",+ "@vitest/spy": "4.1.9",+ "@vitest/utils": "4.1.9",+ "chai": "^6.2.2",+ "tinyrainbow": "^3.1.0"},"funding": {"url": "https://opencollective.com/vitest"}},"node_modules/@vitest/mocker": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-3.0.8.tgz",- "integrity": "sha512-n3LjS7fcW1BCoF+zWZxG7/5XvuYH+lsFg+BDwwAz0arIwHQJFUEsKBQ0BLU49fCxuM/2HSeBPHQD8WjgrxMfow==",+ "version": "4.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-4.1.9.tgz",+ "integrity": "sha512-EVkXzBjrPGM+cK8/ANWgBrkUCfJfb38/EfTSO8h7pWvKkyPkpWxvR7BkD2MyItMF62C97zAEoqdpUixwR/e+Rw==","dev": true,"license": "MIT","dependencies": {- "@vitest/spy": "3.0.8",+ "@vitest/spy": "4.1.9","estree-walker": "^3.0.3",- "magic-string": "^0.30.17"+ "magic-string": "^0.30.21"},"funding": {"url": "https://opencollective.com/vitest"},"peerDependencies": {"msw": "^2.4.9",- "vite": "^5.0.0 || ^6.0.0"- },- "peerDependenciesMeta": {- "msw": {- "optional": true- },- "vite": {- "optional": true- }- }- },- "node_modules/@vitest/pretty-format": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-3.0.8.tgz",- "integrity": "sha512-BNqwbEyitFhzYMYHUVbIvepOyeQOSFA/NeJMIP9enMntkkxLgOcgABH6fjyXG85ipTgvero6noreavGIqfJcIg==",- "dev": true,- "license": "MIT",- "dependencies": {- "tinyrainbow": "^2.0.0"- },- "funding": {- "url": "https://opencollective.com/vitest"- }- },- "node_modules/@vitest/runner": {- "version": "4.1.9",- "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-4.1.9.tgz",- "integrity": "sha512-KXLMDtc7oe70+3mJfGrPUWPesswH+3sTxAMAMl8DG7I8IUQT4XW718dY5ID3vPUcmlu27CcKfY4P3h3I29SLJg==",- "dev": true,- "license": "MIT",- "peer": true,- "dependencies": {- "@vitest/utils": "4.1.9",- "pathe": "^2.0.3"- },- "funding": {- "url": "https://opencollective.com/vitest"- }- },- "node_modules/@vitest/runner/node_modules/@vitest/pretty-format": {- "version": "4.1.9",- "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-4.1.9.tgz",- "integrity": "sha512-s0iufns3iIFitdgm+YR7g1whCAaGtXz459VS9/PqyKDEEFgYIhsHOQmXgIgDuYCt7DeQmiZT0Qe2OA2p4ZPu5A==",- "dev": true,- "license": "MIT",- "peer": true,- "dependencies": {- "tinyrainbow": "^3.1.0"- },- "funding": {- "url": "https://opencollective.com/vitest"- }- },- "node_modules/@vitest/runner/node_modules/@vitest/utils": {- "version": "4.1.9",- "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-4.1.9.tgz",- "integrity": "sha512-A51o8ymO5PpqlWNnBP9ZHPXDIpuMtTLlGSjN7la4US+LJzoUMyhwjA5QXlm39JexgwHKW4Xjs8Z2d3dLCXOeuA==",- "dev": true,- "license": "MIT",- "peer": true,- "dependencies": {- "@vitest/pretty-format": "4.1.9",- "convert-source-map": "^2.0.0",- "tinyrainbow": "^3.1.0"- },- "funding": {- "url": "https://opencollective.com/vitest"- }- },- "node_modules/@vitest/runner/node_modules/tinyrainbow": {- "version": "3.1.0",- "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.1.0.tgz",- "integrity": "sha512-Bf+ILmBgretUrdJxzXM0SgXLZ3XfiaUuOj/IKQHuTXip+05Xn+uyEYdVg0kYDipTBcLrCVyUzAPz7QmArb0mmw==",- "dev": true,- "license": "MIT",- "peer": true,- "engines": {- "node": ">=14.0.0"- }- },- "node_modules/@vitest/snapshot": {- "version": "4.1.9",- "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-4.1.9.tgz",- "integrity": "sha512-Jc7RKGNBo8Z28WYIm0Niej4xdSPByRf6mU58VpHQkd6Zh05rlnA+twjbK5HyeIGHxrzsc3mJgS43uM0CZKzaIA==",- "dev": true,- "license": "MIT",- "peer": true,- "dependencies": {- "@vitest/pretty-format": "4.1.9",- "@vitest/utils": "4.1.9",- "magic-string": "^0.30.21",- "pathe": "^2.0.3"+ "vite": "^6.0.0 || ^7.0.0 || ^8.0.0"},- "funding": {- "url": "https://opencollective.com/vitest"+ "peerDependenciesMeta": {+ "msw": {+ "optional": true+ },+ "vite": {+ "optional": true+ }}},- "node_modules/@vitest/snapshot/node_modules/@vitest/pretty-format": {+ "node_modules/@vitest/pretty-format": {"version": "4.1.9","resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-4.1.9.tgz","integrity": "sha512-s0iufns3iIFitdgm+YR7g1whCAaGtXz459VS9/PqyKDEEFgYIhsHOQmXgIgDuYCt7DeQmiZT0Qe2OA2p4ZPu5A==","dev": true,"license": "MIT",- "peer": true,"dependencies": {"tinyrainbow": "^3.1.0"},@@ -2237,56 +1747,56 @@"url": "https://opencollective.com/vitest"}},- "node_modules/@vitest/snapshot/node_modules/@vitest/utils": {+ "node_modules/@vitest/runner": {"version": "4.1.9",- "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-4.1.9.tgz",- "integrity": "sha512-A51o8ymO5PpqlWNnBP9ZHPXDIpuMtTLlGSjN7la4US+LJzoUMyhwjA5QXlm39JexgwHKW4Xjs8Z2d3dLCXOeuA==",+ "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-4.1.9.tgz",+ "integrity": "sha512-KXLMDtc7oe70+3mJfGrPUWPesswH+3sTxAMAMl8DG7I8IUQT4XW718dY5ID3vPUcmlu27CcKfY4P3h3I29SLJg==","dev": true,"license": "MIT",- "peer": true,"dependencies": {- "@vitest/pretty-format": "4.1.9",- "convert-source-map": "^2.0.0",- "tinyrainbow": "^3.1.0"+ "@vitest/utils": "4.1.9",+ "pathe": "^2.0.3"},"funding": {"url": "https://opencollective.com/vitest"}},- "node_modules/@vitest/snapshot/node_modules/tinyrainbow": {- "version": "3.1.0",- "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.1.0.tgz",- "integrity": "sha512-Bf+ILmBgretUrdJxzXM0SgXLZ3XfiaUuOj/IKQHuTXip+05Xn+uyEYdVg0kYDipTBcLrCVyUzAPz7QmArb0mmw==",+ "node_modules/@vitest/snapshot": {+ "version": "4.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-4.1.9.tgz",+ "integrity": "sha512-Jc7RKGNBo8Z28WYIm0Niej4xdSPByRf6mU58VpHQkd6Zh05rlnA+twjbK5HyeIGHxrzsc3mJgS43uM0CZKzaIA==","dev": true,"license": "MIT",- "peer": true,- "engines": {- "node": ">=14.0.0"+ "dependencies": {+ "@vitest/pretty-format": "4.1.9",+ "@vitest/utils": "4.1.9",+ "magic-string": "^0.30.21",+ "pathe": "^2.0.3"+ },+ "funding": {+ "url": "https://opencollective.com/vitest"}},"node_modules/@vitest/spy": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-3.0.8.tgz",- "integrity": "sha512-MR+PzJa+22vFKYb934CejhR4BeRpMSoxkvNoDit68GQxRLSf11aT6CTj3XaqUU9rxgWJFnqicN/wxw6yBRkI1Q==",+ "version": "4.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-4.1.9.tgz",+ "integrity": "sha512-fHpsS6mIi+PiEW+vcRVOMkX1oSaPKne3VOclSFICPcGOmfKgXPU5iAah+wcNcj2xPrCCmfq99IDGf+EojhhvhA==","dev": true,"license": "MIT",- "dependencies": {- "tinyspy": "^3.0.2"- },"funding": {"url": "https://opencollective.com/vitest"}},"node_modules/@vitest/utils": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-3.0.8.tgz",- "integrity": "sha512-nkBC3aEhfX2PdtQI/QwAWp8qZWwzASsU4Npbcd5RdMPBSSLCpkZp52P3xku3s3uA0HIEhGvEcF8rNkBsz9dQ4Q==",+ "version": "4.1.9",+ "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-4.1.9.tgz",+ "integrity": "sha512-A51o8ymO5PpqlWNnBP9ZHPXDIpuMtTLlGSjN7la4US+LJzoUMyhwjA5QXlm39JexgwHKW4Xjs8Z2d3dLCXOeuA==","dev": true,"license": "MIT","dependencies": {- "@vitest/pretty-format": "3.0.8",- "loupe": "^3.1.3",- "tinyrainbow": "^2.0.0"+ "@vitest/pretty-format": "4.1.9",+ "convert-source-map": "^2.0.0",+ "tinyrainbow": "^3.1.0"},"funding": {"url": "https://opencollective.com/vitest"@@ -2344,41 +1854,14 @@"integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==","license": "BSD-3-Clause"},- "node_modules/cac": {- "version": "6.7.14",- "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz",- "integrity": "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==",- "dev": true,- "license": "MIT",- "engines": {- "node": ">=8"- }- },"node_modules/chai": {- "version": "5.2.0",- "resolved": "https://registry.npmjs.org/chai/-/chai-5.2.0.tgz",- "integrity": "sha512-mCuXncKXk5iCLhfhwTc0izo0gtEmpz5CtG2y8GiOINBlMVS6v8TMRc5TaLWKS6692m9+dVVfzgeVxR5UxWHTYw==",- "dev": true,- "license": "MIT",- "dependencies": {- "assertion-error": "^2.0.1",- "check-error": "^2.1.1",- "deep-eql": "^5.0.1",- "loupe": "^3.1.0",- "pathval": "^2.0.0"- },- "engines": {- "node": ">=12"- }- },- "node_modules/check-error": {- "version": "2.1.1",- "resolved": "https://registry.npmjs.org/check-error/-/check-error-2.1.1.tgz",- "integrity": "sha512-OAlb+T7V4Op9OwdkjmguYRqncdlx5JiofwOAUkmTF+jNdHwzTaTs4sRAGpzLF3oOz5xAyDGrPgeIDFQmDOTiJw==",+ "version": "6.2.2",+ "resolved": "https://registry.npmjs.org/chai/-/chai-6.2.2.tgz",+ "integrity": "sha512-NUPRluOfOiTKBKvWPtSD4PhFvWCqOi0BGStNWs57X9js7XGTprSmFoz5F0tWhR4WPjNeR9jXqdC7/UpSJTnlRg==","dev": true,"license": "MIT","engines": {- "node": ">= 16"+ "node": ">=18"}},"node_modules/cjs-module-lexer": {@@ -2393,8 +1876,7 @@"resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz","integrity": "sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==","dev": true,- "license": "MIT",- "peer": true+ "license": "MIT"},"node_modules/cookie": {"version": "1.1.1",@@ -2428,16 +1910,6 @@}}},- "node_modules/deep-eql": {- "version": "5.0.2",- "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-5.0.2.tgz",- "integrity": "sha512-h5k/5U50IJJFpzfL6nO9jaaumfjO/f2NjK/oYB2Djzm4p9L+3T9qWpZqZ2hAbLPuuYq9wrU08WQyBTL5GbPk5Q==",- "dev": true,- "license": "MIT",- "engines": {- "node": ">=6"- }- },"node_modules/detect-libc": {"version": "2.1.2","resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz",@@ -2468,171 +1940,403 @@}},"node_modules/es-module-lexer": {- "version": "1.6.0",- "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.6.0.tgz",- "integrity": "sha512-qqnD1yMU6tk/jnaMosogGySTZP8YtUgAffA9nMN+E/rjxcfRQ6IEk7IiozUjgxKoFHBGjTLnrHB/YC45r/59EQ==",+ "version": "2.3.0",+ "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.3.0.tgz",+ "integrity": "sha512-KLdwQm2NvGLDkQDCGvmiQrhkd0JbMzXthwQAUgWjQuQdBLFa3eiBP5arXZyA+f8x+x7OXgud6bq2rxjGtHV2tw==",+ "dev": true,+ "license": "MIT"+ },+ "node_modules/estree-walker": {+ "version": "3.0.3",+ "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz",+ "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==",+ "dev": true,+ "license": "MIT",+ "dependencies": {+ "@types/estree": "^1.0.0"+ }+ },+ "node_modules/expect-type": {+ "version": "1.4.0",+ "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.4.0.tgz",+ "integrity": "sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA==",+ "dev": true,+ "license": "Apache-2.0",+ "engines": {+ "node": ">=12.0.0"+ }+ },+ "node_modules/fdir": {+ "version": "6.5.0",+ "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz",+ "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">=12.0.0"+ },+ "peerDependencies": {+ "picomatch": "^3 || ^4"+ },+ "peerDependenciesMeta": {+ "picomatch": {+ "optional": true+ }+ }+ },+ "node_modules/fflate": {+ "version": "0.7.4",+ "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.7.4.tgz",+ "integrity": "sha512-5u2V/CDW15QM1XbbgS+0DfPxVB+jUKhWEKuuFuHncbk3tEEqzmoXL+2KyOFuKGqOnmdIy0/davWF1CkuwtibCw==","dev": true,"license": "MIT"},- "node_modules/esbuild": {- "version": "0.25.4",- "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.4.tgz",- "integrity": "sha512-8pgjLUcUjcgDg+2Q4NYXnPbo/vncAY4UmyaCm0jZevERqCHZIaWwdJHkf8XQtu4AxSKCdvrUbT0XUr1IdZzI8Q==",+ "node_modules/fs-monkey": {+ "version": "1.0.6",+ "resolved": "https://registry.npmjs.org/fs-monkey/-/fs-monkey-1.0.6.tgz",+ "integrity": "sha512-b1FMfwetIKymC0eioW7mTywihSQE4oLzQn1dB6rZB5fx/3NpNEdAWeCSMB+60/AeT0TCXsxzAlcYVEFCTAksWg==",+ "dev": true,+ "license": "Unlicense"+ },+ "node_modules/fsevents": {+ "version": "2.3.3",+ "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",+ "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==","dev": true,"hasInstallScript": true,"license": "MIT",- "bin": {- "esbuild": "bin/esbuild"+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": "^8.16.0 || ^10.6.0 || >=11.0.0"+ }+ },+ "node_modules/jsonwebtoken": {+ "version": "9.0.2",+ "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz",+ "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==",+ "license": "MIT",+ "dependencies": {+ "jws": "^3.2.2",+ "lodash.includes": "^4.3.0",+ "lodash.isboolean": "^3.0.3",+ "lodash.isinteger": "^4.0.4",+ "lodash.isnumber": "^3.0.3",+ "lodash.isplainobject": "^4.0.6",+ "lodash.isstring": "^4.0.1",+ "lodash.once": "^4.0.0",+ "ms": "^2.1.1",+ "semver": "^7.5.4"+ },+ "engines": {+ "node": ">=12",+ "npm": ">=6"+ }+ },+ "node_modules/jwa": {+ "version": "1.4.2",+ "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.2.tgz",+ "integrity": "sha512-eeH5JO+21J78qMvTIDdBXidBd6nG2kZjg5Ohz/1fpa28Z4CcsWUzJ1ZZyFq/3z3N17aZy+ZuBoHljASbL1WfOw==",+ "license": "MIT",+ "dependencies": {+ "buffer-equal-constant-time": "^1.0.1",+ "ecdsa-sig-formatter": "1.0.11",+ "safe-buffer": "^5.0.1"+ }+ },+ "node_modules/jws": {+ "version": "3.2.3",+ "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.3.tgz",+ "integrity": "sha512-byiJ0FLRdLdSVSReO/U4E7RoEyOCKnEnEPMjq3HxWtvzLsV08/i5RQKsFVNkCldrCaPr2vDNAOMsfs8T/Hze7g==",+ "license": "MIT",+ "dependencies": {+ "jwa": "^1.4.2",+ "safe-buffer": "^5.0.1"+ }+ },+ "node_modules/kleur": {+ "version": "4.1.5",+ "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz",+ "integrity": "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==",+ "dev": true,+ "license": "MIT",+ "engines": {+ "node": ">=6"+ }+ },+ "node_modules/lightningcss": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.32.0.tgz",+ "integrity": "sha512-NXYBzinNrblfraPGyrbPoD19C1h9lfI/1mzgWYvXUTe414Gz/X1FD2XBZSZM7rRTrMA8JL3OtAaGifrIKhQ5yQ==",+ "dev": true,+ "license": "MPL-2.0",+ "dependencies": {+ "detect-libc": "^2.0.3"+ },+ "engines": {+ "node": ">= 12.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"+ },+ "optionalDependencies": {+ "lightningcss-android-arm64": "1.32.0",+ "lightningcss-darwin-arm64": "1.32.0",+ "lightningcss-darwin-x64": "1.32.0",+ "lightningcss-freebsd-x64": "1.32.0",+ "lightningcss-linux-arm-gnueabihf": "1.32.0",+ "lightningcss-linux-arm64-gnu": "1.32.0",+ "lightningcss-linux-arm64-musl": "1.32.0",+ "lightningcss-linux-x64-gnu": "1.32.0",+ "lightningcss-linux-x64-musl": "1.32.0",+ "lightningcss-win32-arm64-msvc": "1.32.0",+ "lightningcss-win32-x64-msvc": "1.32.0"+ }+ },+ "node_modules/lightningcss-android-arm64": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-android-arm64/-/lightningcss-android-arm64-1.32.0.tgz",+ "integrity": "sha512-YK7/ClTt4kAK0vo6w3X+Pnm0D2cf2vPHbhOXdoNti1Ga0al1P4TBZhwjATvjNwLEBCnKvjJc2jQgHXH0NEwlAg==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "android"+ ],+ "engines": {+ "node": ">= 12.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"+ }+ },+ "node_modules/lightningcss-darwin-arm64": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-darwin-arm64/-/lightningcss-darwin-arm64-1.32.0.tgz",+ "integrity": "sha512-RzeG9Ju5bag2Bv1/lwlVJvBE3q6TtXskdZLLCyfg5pt+HLz9BqlICO7LZM7VHNTTn/5PRhHFBSjk5lc4cmscPQ==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">= 12.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"+ }+ },+ "node_modules/lightningcss-darwin-x64": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-darwin-x64/-/lightningcss-darwin-x64-1.32.0.tgz",+ "integrity": "sha512-U+QsBp2m/s2wqpUYT/6wnlagdZbtZdndSmut/NJqlCcMLTWp5muCrID+K5UJ6jqD2BFshejCYXniPDbNh73V8w==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">= 12.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"+ }+ },+ "node_modules/lightningcss-freebsd-x64": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-freebsd-x64/-/lightningcss-freebsd-x64-1.32.0.tgz",+ "integrity": "sha512-JCTigedEksZk3tHTTthnMdVfGf61Fky8Ji2E4YjUTEQX14xiy/lTzXnu1vwiZe3bYe0q+SpsSH/CTeDXK6WHig==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "freebsd"+ ],+ "engines": {+ "node": ">= 12.0.0"},+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"+ }+ },+ "node_modules/lightningcss-linux-arm-gnueabihf": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-linux-arm-gnueabihf/-/lightningcss-linux-arm-gnueabihf-1.32.0.tgz",+ "integrity": "sha512-x6rnnpRa2GL0zQOkt6rts3YDPzduLpWvwAF6EMhXFVZXD4tPrBkEFqzGowzCsIWsPjqSK+tyNEODUBXeeVHSkw==",+ "cpu": [+ "arm"+ ],+ "dev": true,+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "linux"+ ],"engines": {- "node": ">=18"+ "node": ">= 12.0.0"},- "optionalDependencies": {- "@esbuild/aix-ppc64": "0.25.4",- "@esbuild/android-arm": "0.25.4",- "@esbuild/android-arm64": "0.25.4",- "@esbuild/android-x64": "0.25.4",- "@esbuild/darwin-arm64": "0.25.4",- "@esbuild/darwin-x64": "0.25.4",- "@esbuild/freebsd-arm64": "0.25.4",- "@esbuild/freebsd-x64": "0.25.4",- "@esbuild/linux-arm": "0.25.4",- "@esbuild/linux-arm64": "0.25.4",- "@esbuild/linux-ia32": "0.25.4",- "@esbuild/linux-loong64": "0.25.4",- "@esbuild/linux-mips64el": "0.25.4",- "@esbuild/linux-ppc64": "0.25.4",- "@esbuild/linux-riscv64": "0.25.4",- "@esbuild/linux-s390x": "0.25.4",- "@esbuild/linux-x64": "0.25.4",- "@esbuild/netbsd-arm64": "0.25.4",- "@esbuild/netbsd-x64": "0.25.4",- "@esbuild/openbsd-arm64": "0.25.4",- "@esbuild/openbsd-x64": "0.25.4",- "@esbuild/sunos-x64": "0.25.4",- "@esbuild/win32-arm64": "0.25.4",- "@esbuild/win32-ia32": "0.25.4",- "@esbuild/win32-x64": "0.25.4"+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"}},- "node_modules/estree-walker": {- "version": "3.0.3",- "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz",- "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==",+ "node_modules/lightningcss-linux-arm64-gnu": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-gnu/-/lightningcss-linux-arm64-gnu-1.32.0.tgz",+ "integrity": "sha512-0nnMyoyOLRJXfbMOilaSRcLH3Jw5z9HDNGfT/gwCPgaDjnx0i8w7vBzFLFR1f6CMLKF8gVbebmkUN3fa/kQJpQ==",+ "cpu": [+ "arm64"+ ],"dev": true,- "license": "MIT",- "dependencies": {- "@types/estree": "^1.0.0"+ "libc": [+ "glibc"+ ],+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 12.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"}},- "node_modules/expect-type": {- "version": "1.2.0",- "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.2.0.tgz",- "integrity": "sha512-80F22aiJ3GLyVnS/B3HzgR6RelZVumzj9jkL0Rhz4h0xYbNW9PjlQz5h3J/SShErbXBc295vseR4/MIbVmUbeA==",+ "node_modules/lightningcss-linux-arm64-musl": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-musl/-/lightningcss-linux-arm64-musl-1.32.0.tgz",+ "integrity": "sha512-UpQkoenr4UJEzgVIYpI80lDFvRmPVg6oqboNHfoH4CQIfNA+HOrZ7Mo7KZP02dC6LjghPQJeBsvXhJod/wnIBg==",+ "cpu": [+ "arm64"+ ],"dev": true,- "license": "Apache-2.0",+ "libc": [+ "musl"+ ],+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "linux"+ ],"engines": {- "node": ">=12.0.0"+ "node": ">= 12.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"}},- "node_modules/fdir": {- "version": "6.5.0",- "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz",- "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==",+ "node_modules/lightningcss-linux-x64-gnu": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-gnu/-/lightningcss-linux-x64-gnu-1.32.0.tgz",+ "integrity": "sha512-V7Qr52IhZmdKPVr+Vtw8o+WLsQJYCTd8loIfpDaMRWGUZfBOYEJeyJIkqGIDMZPwPx24pUMfwSxxI8phr/MbOA==",+ "cpu": [+ "x64"+ ],"dev": true,- "license": "MIT",+ "libc": [+ "glibc"+ ],+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "linux"+ ],"engines": {- "node": ">=12.0.0"- },- "peerDependencies": {- "picomatch": "^3 || ^4"+ "node": ">= 12.0.0"},- "peerDependenciesMeta": {- "picomatch": {- "optional": true- }+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"}},- "node_modules/fflate": {- "version": "0.7.4",- "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.7.4.tgz",- "integrity": "sha512-5u2V/CDW15QM1XbbgS+0DfPxVB+jUKhWEKuuFuHncbk3tEEqzmoXL+2KyOFuKGqOnmdIy0/davWF1CkuwtibCw==",- "dev": true,- "license": "MIT"- },- "node_modules/fs-monkey": {- "version": "1.0.6",- "resolved": "https://registry.npmjs.org/fs-monkey/-/fs-monkey-1.0.6.tgz",- "integrity": "sha512-b1FMfwetIKymC0eioW7mTywihSQE4oLzQn1dB6rZB5fx/3NpNEdAWeCSMB+60/AeT0TCXsxzAlcYVEFCTAksWg==",- "dev": true,- "license": "Unlicense"- },- "node_modules/fsevents": {- "version": "2.3.3",- "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",- "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",+ "node_modules/lightningcss-linux-x64-musl": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-musl/-/lightningcss-linux-x64-musl-1.32.0.tgz",+ "integrity": "sha512-bYcLp+Vb0awsiXg/80uCRezCYHNg1/l3mt0gzHnWV9XP1W5sKa5/TCdGWaR/zBM2PeF/HbsQv/j2URNOiVuxWg==",+ "cpu": [+ "x64"+ ],"dev": true,- "hasInstallScript": true,- "license": "MIT",+ "libc": [+ "musl"+ ],+ "license": "MPL-2.0","optional": true,"os": [- "darwin"+ "linux"],"engines": {- "node": "^8.16.0 || ^10.6.0 || >=11.0.0"- }- },- "node_modules/jsonwebtoken": {- "version": "9.0.2",- "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz",- "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==",- "license": "MIT",- "dependencies": {- "jws": "^3.2.2",- "lodash.includes": "^4.3.0",- "lodash.isboolean": "^3.0.3",- "lodash.isinteger": "^4.0.4",- "lodash.isnumber": "^3.0.3",- "lodash.isplainobject": "^4.0.6",- "lodash.isstring": "^4.0.1",- "lodash.once": "^4.0.0",- "ms": "^2.1.1",- "semver": "^7.5.4"+ "node": ">= 12.0.0"},- "engines": {- "node": ">=12",- "npm": ">=6"- }- },- "node_modules/jwa": {- "version": "1.4.2",- "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.2.tgz",- "integrity": "sha512-eeH5JO+21J78qMvTIDdBXidBd6nG2kZjg5Ohz/1fpa28Z4CcsWUzJ1ZZyFq/3z3N17aZy+ZuBoHljASbL1WfOw==",- "license": "MIT",- "dependencies": {- "buffer-equal-constant-time": "^1.0.1",- "ecdsa-sig-formatter": "1.0.11",- "safe-buffer": "^5.0.1"+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"}},- "node_modules/jws": {- "version": "3.2.3",- "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.3.tgz",- "integrity": "sha512-byiJ0FLRdLdSVSReO/U4E7RoEyOCKnEnEPMjq3HxWtvzLsV08/i5RQKsFVNkCldrCaPr2vDNAOMsfs8T/Hze7g==",- "license": "MIT",- "dependencies": {- "jwa": "^1.4.2",- "safe-buffer": "^5.0.1"+ "node_modules/lightningcss-win32-arm64-msvc": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-win32-arm64-msvc/-/lightningcss-win32-arm64-msvc-1.32.0.tgz",+ "integrity": "sha512-8SbC8BR40pS6baCM8sbtYDSwEVQd4JlFTOlaD3gWGHfThTcABnNDBda6eTZeqbofalIJhFx0qKzgHJmcPTnGdw==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 12.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"}},- "node_modules/kleur": {- "version": "4.1.5",- "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz",- "integrity": "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==",+ "node_modules/lightningcss-win32-x64-msvc": {+ "version": "1.32.0",+ "resolved": "https://registry.npmjs.org/lightningcss-win32-x64-msvc/-/lightningcss-win32-x64-msvc-1.32.0.tgz",+ "integrity": "sha512-Amq9B/SoZYdDi1kFrojnoqPLxYhQ4Wo5XiL8EVJrVsB8ARoC1PWW6VGtT0WKCemjy8aC+louJnjS7U18x3b06Q==",+ "cpu": [+ "x64"+ ],"dev": true,- "license": "MIT",+ "license": "MPL-2.0",+ "optional": true,+ "os": [+ "win32"+ ],"engines": {- "node": ">=6"+ "node": ">= 12.0.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/parcel"}},"node_modules/lodash.includes": {@@ -2683,13 +2387,6 @@"integrity": "sha512-vh+hYxvsGL9MrsUaquPpawbrL97R4Qb84nc41xTh0zEEz7tJsdioH4mhbsLi0R659LFj3/xII2ML2ScZYJTw9Q==","license": "MIT"},- "node_modules/loupe": {- "version": "3.1.3",- "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.1.3.tgz",- "integrity": "sha512-kkIp7XSkP78ZxJEsSxW3712C6teJVoeHHwgo9zJ380de7IYyJ2ISlxojcH2pC5OFLewESmnRi/+XCDIEEVyoug==",- "dev": true,- "license": "MIT"- },"node_modules/magic-string": {"version": "0.30.21","resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz",@@ -2728,9 +2425,9 @@"license": "MIT"},"node_modules/nanoid": {- "version": "3.3.11",- "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.11.tgz",- "integrity": "sha512-N8SpfPUnUp1bK+PMYW8qSWdl9U+wwNWI4QKxOYDy9JAro3WMX7p2OeVRF9v+347pnakNevPmiHhNmZ2HbFA76w==",+ "version": "3.3.15",+ "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.15.tgz",+ "integrity": "sha512-y7Wygv/7mEOvxTuEQDB8StXdMRBWf1kR/tlhAzBRUFkB2jfcLOAxO/SHmOO2zgz1pVgK29/kyupn059/bCHdjA==","dev": true,"funding": [{@@ -2746,6 +2443,20 @@"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"}},+ "node_modules/obug": {+ "version": "2.1.3",+ "resolved": "https://registry.npmjs.org/obug/-/obug-2.1.3.tgz",+ "integrity": "sha512-9miFgM2OFba7hB+pRgvtV84pYTBaoTHohvmIgiRt6dRIzbwEOIaNaP+dIlGs2fNFoB0SeISs0Jz5WFVRid6Xyg==",+ "dev": true,+ "funding": [+ "https://github.com/sponsors/sxzz",+ "https://opencollective.com/debug"+ ],+ "license": "MIT",+ "engines": {+ "node": ">=12.20.0"+ }+ },"node_modules/path-browserify": {"version": "1.0.1","resolved": "https://registry.npmjs.org/path-browserify/-/path-browserify-1.0.1.tgz",@@ -2767,16 +2478,6 @@"dev": true,"license": "MIT"},- "node_modules/pathval": {- "version": "2.0.0",- "resolved": "https://registry.npmjs.org/pathval/-/pathval-2.0.0.tgz",- "integrity": "sha512-vE7JKRyES09KiunauX7nd2Q9/L7lhok4smP9RZTDeD4MVs72Dp2qNFVz39Nz5a0FVEW0BJR6C0DYrq6unoziZA==",- "dev": true,- "license": "MIT",- "engines": {- "node": ">= 14.16"- }- },"node_modules/picocolors": {"version": "1.1.1","resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",@@ -2785,9 +2486,9 @@"license": "ISC"},"node_modules/picomatch": {- "version": "4.0.4",- "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz",- "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==",+ "version": "4.0.5",+ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.5.tgz",+ "integrity": "sha512-RvwwcruNjI1ncT5xRakeyS9Lf8lcItv34KD+aif+VH9kduAyfYBipGh12274xtenIPZ119/R9BdTBa8gAwSh0A==","dev": true,"license": "MIT","engines": {@@ -2798,9 +2499,9 @@}},"node_modules/postcss": {- "version": "8.5.10",- "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.10.tgz",- "integrity": "sha512-pMMHxBOZKFU6HgAZ4eyGnwXF/EvPGGqUr0MnZ5+99485wwW41kW91A4LOGxSHhgugZmSChL5AlElNdwlNgcnLQ==",+ "version": "8.5.16",+ "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.16.tgz",+ "integrity": "sha512-vuwillviilfKZsg0VGj5R/YwwcHx4SLsIOI/7K6mQkWx+l5cUHTjj5g0AasTBcyXsbfTgrwsUNmVUb5xVwyPwg==","dev": true,"funding": [{@@ -2818,7 +2519,7 @@],"license": "MIT","dependencies": {- "nanoid": "^3.3.11",+ "nanoid": "^3.3.12","picocolors": "^1.1.1","source-map-js": "^1.2.1"},@@ -2847,49 +2548,38 @@"safe-buffer": "^5.1.0"}},- "node_modules/rollup": {- "version": "4.59.0",- "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.59.0.tgz",- "integrity": "sha512-2oMpl67a3zCH9H79LeMcbDhXW/UmWG/y2zuqnF2jQq5uq9TbM9TVyXvA4+t+ne2IIkBdrLpAaRQAvo7YI/Yyeg==",+ "node_modules/rolldown": {+ "version": "1.1.4",+ "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.1.4.tgz",+ "integrity": "sha512-IjZYiLxZwpnhwhdBH2ugdTGVSdhCQUmLxLoqyjiL0JxYjyRst+5a0P3xfrTxJ5F638j4Mvvw5FAX5XE6eHpXbA==","dev": true,"license": "MIT","dependencies": {- "@types/estree": "1.0.8"+ "@oxc-project/types": "=0.138.0",+ "@rolldown/pluginutils": "^1.0.0"},"bin": {- "rollup": "dist/bin/rollup"+ "rolldown": "bin/cli.mjs"},"engines": {- "node": ">=18.0.0",- "npm": ">=8.0.0"+ "node": "^20.19.0 || >=22.12.0"},"optionalDependencies": {- "@rollup/rollup-android-arm-eabi": "4.59.0",- "@rollup/rollup-android-arm64": "4.59.0",- "@rollup/rollup-darwin-arm64": "4.59.0",- "@rollup/rollup-darwin-x64": "4.59.0",- "@rollup/rollup-freebsd-arm64": "4.59.0",- "@rollup/rollup-freebsd-x64": "4.59.0",- "@rollup/rollup-linux-arm-gnueabihf": "4.59.0",- "@rollup/rollup-linux-arm-musleabihf": "4.59.0",- "@rollup/rollup-linux-arm64-gnu": "4.59.0",- "@rollup/rollup-linux-arm64-musl": "4.59.0",- "@rollup/rollup-linux-loong64-gnu": "4.59.0",- "@rollup/rollup-linux-loong64-musl": "4.59.0",- "@rollup/rollup-linux-ppc64-gnu": "4.59.0",- "@rollup/rollup-linux-ppc64-musl": "4.59.0",- "@rollup/rollup-linux-riscv64-gnu": "4.59.0",- "@rollup/rollup-linux-riscv64-musl": "4.59.0",- "@rollup/rollup-linux-s390x-gnu": "4.59.0",- "@rollup/rollup-linux-x64-gnu": "4.59.0",- "@rollup/rollup-linux-x64-musl": "4.59.0",- "@rollup/rollup-openbsd-x64": "4.59.0",- "@rollup/rollup-openharmony-arm64": "4.59.0",- "@rollup/rollup-win32-arm64-msvc": "4.59.0",- "@rollup/rollup-win32-ia32-msvc": "4.59.0",- "@rollup/rollup-win32-x64-gnu": "4.59.0",- "@rollup/rollup-win32-x64-msvc": "4.59.0",- "fsevents": "~2.3.2"+ "@rolldown/binding-android-arm64": "1.1.4",+ "@rolldown/binding-darwin-arm64": "1.1.4",+ "@rolldown/binding-darwin-x64": "1.1.4",+ "@rolldown/binding-freebsd-x64": "1.1.4",+ "@rolldown/binding-linux-arm-gnueabihf": "1.1.4",+ "@rolldown/binding-linux-arm64-gnu": "1.1.4",+ "@rolldown/binding-linux-arm64-musl": "1.1.4",+ "@rolldown/binding-linux-ppc64-gnu": "1.1.4",+ "@rolldown/binding-linux-s390x-gnu": "1.1.4",+ "@rolldown/binding-linux-x64-gnu": "1.1.4",+ "@rolldown/binding-linux-x64-musl": "1.1.4",+ "@rolldown/binding-openharmony-arm64": "1.1.4",+ "@rolldown/binding-wasm32-wasi": "1.1.4",+ "@rolldown/binding-win32-arm64-msvc": "1.1.4",+ "@rolldown/binding-win32-x64-msvc": "1.1.4"}},"node_modules/safe-buffer": {@@ -2994,9 +2684,9 @@"license": "MIT"},"node_modules/std-env": {- "version": "3.8.1",- "resolved": "https://registry.npmjs.org/std-env/-/std-env-3.8.1.tgz",- "integrity": "sha512-vj5lIj3Mwf9D79hBkltk5qmkFI+biIKWS2IBxEyEU3AX1tUf7AoL8nSazCOiiqQsGKIq01SClsKEzweu34uwvA==",+ "version": "4.1.0",+ "resolved": "https://registry.npmjs.org/std-env/-/std-env-4.1.0.tgz",+ "integrity": "sha512-Rq7ybcX2RuC55r9oaPVEW7/xu3tj8u4GeBYHBWCychFtzMIr86A7e3PPEBPT37sHStKX3+TiX/Fr/ACmJLVlLQ==","dev": true,"license": "MIT"},@@ -3021,21 +2711,24 @@"license": "MIT"},"node_modules/tinyexec": {- "version": "0.3.2",- "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-0.3.2.tgz",- "integrity": "sha512-KQQR9yN7R5+OSwaK0XQoj22pwHoTlgYqmUscPYoknOoWCWfj/5/ABTMRi69FrKU5ffPVh5QcFikpWJI/P1ocHA==",+ "version": "1.2.4",+ "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-1.2.4.tgz",+ "integrity": "sha512-SHf/r48b7vOrjve9PxJo3MN5v5yuyjHvdUcrQffT3WXMUfnGmHDVbC4k3sHJaJTgZCwpUplIaAo5ANtMyp3YHg==","dev": true,- "license": "MIT"+ "license": "MIT",+ "engines": {+ "node": ">=18"+ }},"node_modules/tinyglobby": {- "version": "0.2.15",- "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.15.tgz",- "integrity": "sha512-j2Zq4NyQYG5XMST4cbs02Ak8iJUdxRM0XI5QyxXuZOzKOINmWurp3smXu3y5wDcJrptwpSjgXHzIQxR0omXljQ==",+ "version": "0.2.17",+ "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz",+ "integrity": "sha512-wXR/dYpcqKmfWpEdZjiKJOwCNFndD0DMnrW/cYjVGttEkBfVgcLFHoNrlj47mjOVic9yyNu65alsgF4NQyTa2g==","dev": true,"license": "MIT","dependencies": {"fdir": "^6.5.0",- "picomatch": "^4.0.3"+ "picomatch": "^4.0.4"},"engines": {"node": ">=12.0.0"@@ -3044,30 +2737,10 @@"url": "https://github.com/sponsors/SuperchupuDev"}},- "node_modules/tinypool": {- "version": "1.0.2",- "resolved": "https://registry.npmjs.org/tinypool/-/tinypool-1.0.2.tgz",- "integrity": "sha512-al6n+QEANGFOMf/dmUMsuS5/r9B06uwlyNjZZql/zv8J7ybHCgoihBNORZCY2mzUuAnomQa2JdhyHKzZxPCrFA==",- "dev": true,- "license": "MIT",- "engines": {- "node": "^18.0.0 || >=20.0.0"- }- },"node_modules/tinyrainbow": {- "version": "2.0.0",- "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-2.0.0.tgz",- "integrity": "sha512-op4nsTR47R6p0vMUUoYl/a+ljLFVtlfaXkLQmqfLR1qHma1h/ysYk4hEXZ880bf2CYgTskvTa/e196Vd5dDQXw==",- "dev": true,- "license": "MIT",- "engines": {- "node": ">=14.0.0"- }- },- "node_modules/tinyspy": {- "version": "3.0.2",- "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-3.0.2.tgz",- "integrity": "sha512-n1cw8k1k0x4pgA2+9XrOkFydTerNcJ1zWCO5Nn9scWHTD+5tp8dghT2x1uduQePZTZgd3Tupf+x9BxJjeJi77Q==",+ "version": "3.1.0",+ "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.1.0.tgz",+ "integrity": "sha512-Bf+ILmBgretUrdJxzXM0SgXLZ3XfiaUuOj/IKQHuTXip+05Xn+uyEYdVg0kYDipTBcLrCVyUzAPz7QmArb0mmw==","dev": true,"license": "MIT","engines": {@@ -3131,24 +2804,23 @@}},"node_modules/vite": {- "version": "6.4.2",- "resolved": "https://registry.npmjs.org/vite/-/vite-6.4.2.tgz",- "integrity": "sha512-2N/55r4JDJ4gdrCvGgINMy+HH3iRpNIz8K6SFwVsA+JbQScLiC+clmAxBgwiSPgcG9U15QmvqCGWzMbqda5zGQ==",+ "version": "8.1.3",+ "resolved": "https://registry.npmjs.org/vite/-/vite-8.1.3.tgz",+ "integrity": "sha512-Ds+gBRbj0lwRO2Y5hwnUBdxSwlAve9LeRyU4sNnAr0ewW0gWF0n5bgXgUzbgZ49MV9BVUAQUFYVcDUcilUExMA==","dev": true,"license": "MIT","dependencies": {- "esbuild": "^0.25.0",- "fdir": "^6.4.4",- "picomatch": "^4.0.2",- "postcss": "^8.5.3",- "rollup": "^4.34.9",- "tinyglobby": "^0.2.13"+ "lightningcss": "^1.32.0",+ "picomatch": "^4.0.4",+ "postcss": "^8.5.16",+ "rolldown": "~1.1.3",+ "tinyglobby": "^0.2.17"},"bin": {"vite": "bin/vite.js"},"engines": {- "node": "^18.0.0 || ^20.0.0 || >=22.0.0"+ "node": "^20.19.0 || >=22.12.0"},"funding": {"url": "https://github.com/vitejs/vite?sponsor=1"@@ -3157,14 +2829,15 @@"fsevents": "~2.3.3"},"peerDependencies": {- "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0",+ "@types/node": "^20.19.0 || >=22.12.0",+ "@vitejs/devtools": "^0.3.0",+ "esbuild": "^0.27.0 || ^0.28.0","jiti": ">=1.21.0",- "less": "*",- "lightningcss": "^1.21.0",- "sass": "*",- "sass-embedded": "*",- "stylus": "*",- "sugarss": "*",+ "less": "^4.0.0",+ "sass": "^1.70.0",+ "sass-embedded": "^1.70.0",+ "stylus": ">=0.54.8",+ "sugarss": "^5.0.0","terser": "^5.16.0","tsx": "^4.8.1","yaml": "^2.4.2"@@ -3173,13 +2846,16 @@"@types/node": {"optional": true},- "jiti": {+ "@vitejs/devtools": {"optional": true},- "less": {+ "esbuild": {+ "optional": true+ },+ "jiti": {"optional": true},- "lightningcss": {+ "less": {"optional": true},"sass": {@@ -3205,86 +2881,80 @@}}},- "node_modules/vite-node": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-3.0.8.tgz",- "integrity": "sha512-6PhR4H9VGlcwXZ+KWCdMqbtG649xCPZqfI9j2PsK1FcXgEzro5bGHcVKFCTqPLaNKZES8Evqv4LwvZARsq5qlg==",- "dev": true,- "license": "MIT",- "dependencies": {- "cac": "^6.7.14",- "debug": "^4.4.0",- "es-module-lexer": "^1.6.0",- "pathe": "^2.0.3",- "vite": "^5.0.0 || ^6.0.0"- },- "bin": {- "vite-node": "vite-node.mjs"- },- "engines": {- "node": "^18.0.0 || ^20.0.0 || >=22.0.0"- },- "funding": {- "url": "https://opencollective.com/vitest"- }- },"node_modules/vitest": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/vitest/-/vitest-3.0.8.tgz",- "integrity": "sha512-dfqAsNqRGUc8hB9OVR2P0w8PZPEckti2+5rdZip0WIz9WW0MnImJ8XiR61QhqLa92EQzKP2uPkzenKOAHyEIbA==",+ "version": "4.1.9",+ "resolved": "https://registry.npmjs.org/vitest/-/vitest-4.1.9.tgz",+ "integrity": "sha512-nE3/LEyc0z87uHYLZebqCUOaJr2hdtuPp7BQ4BosVFnfltxgAvMG08NyrSGlPpOUWvR27c5flSmYFTNr78L9GQ==","dev": true,"license": "MIT","dependencies": {- "@vitest/expect": "3.0.8",- "@vitest/mocker": "3.0.8",- "@vitest/pretty-format": "^3.0.8",- "@vitest/runner": "3.0.8",- "@vitest/snapshot": "3.0.8",- "@vitest/spy": "3.0.8",- "@vitest/utils": "3.0.8",- "chai": "^5.2.0",- "debug": "^4.4.0",- "expect-type": "^1.1.0",- "magic-string": "^0.30.17",+ "@vitest/expect": "4.1.9",+ "@vitest/mocker": "4.1.9",+ "@vitest/pretty-format": "4.1.9",+ "@vitest/runner": "4.1.9",+ "@vitest/snapshot": "4.1.9",+ "@vitest/spy": "4.1.9",+ "@vitest/utils": "4.1.9",+ "es-module-lexer": "^2.0.0",+ "expect-type": "^1.3.0",+ "magic-string": "^0.30.21",+ "obug": "^2.1.1","pathe": "^2.0.3",- "std-env": "^3.8.0",+ "picomatch": "^4.0.3",+ "std-env": "^4.0.0-rc.1","tinybench": "^2.9.0",- "tinyexec": "^0.3.2",- "tinypool": "^1.0.2",- "tinyrainbow": "^2.0.0",- "vite": "^5.0.0 || ^6.0.0",- "vite-node": "3.0.8",+ "tinyexec": "^1.0.2",+ "tinyglobby": "^0.2.15",+ "tinyrainbow": "^3.1.0",+ "vite": "^6.0.0 || ^7.0.0 || ^8.0.0","why-is-node-running": "^2.3.0"},"bin": {"vitest": "vitest.mjs"},"engines": {- "node": "^18.0.0 || ^20.0.0 || >=22.0.0"+ "node": "^20.0.0 || ^22.0.0 || >=24.0.0"},"funding": {"url": "https://opencollective.com/vitest"},"peerDependencies": {"@edge-runtime/vm": "*",- "@types/debug": "^4.1.12",- "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0",- "@vitest/browser": "3.0.8",- "@vitest/ui": "3.0.8",+ "@opentelemetry/api": "^1.9.0",+ "@types/node": "^20.0.0 || ^22.0.0 || >=24.0.0",+ "@vitest/browser-playwright": "4.1.9",+ "@vitest/browser-preview": "4.1.9",+ "@vitest/browser-webdriverio": "4.1.9",+ "@vitest/coverage-istanbul": "4.1.9",+ "@vitest/coverage-v8": "4.1.9",+ "@vitest/ui": "4.1.9","happy-dom": "*",- "jsdom": "*"+ "jsdom": "*",+ "vite": "^6.0.0 || ^7.0.0 || ^8.0.0"},"peerDependenciesMeta": {"@edge-runtime/vm": {"optional": true},- "@types/debug": {+ "@opentelemetry/api": {"optional": true},"@types/node": {"optional": true},- "@vitest/browser": {+ "@vitest/browser-playwright": {+ "optional": true+ },+ "@vitest/browser-preview": {+ "optional": true+ },+ "@vitest/browser-webdriverio": {+ "optional": true+ },+ "@vitest/coverage-istanbul": {+ "optional": true+ },+ "@vitest/coverage-v8": {"optional": true},"@vitest/ui": {@@ -3295,38 +2965,12 @@},"jsdom": {"optional": true+ },+ "vite": {+ "optional": false}}},- "node_modules/vitest/node_modules/@vitest/runner": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-3.0.8.tgz",- "integrity": "sha512-c7UUw6gEcOzI8fih+uaAXS5DwjlBaCJUo7KJ4VvJcjL95+DSR1kova2hFuRt3w41KZEFcOEiq098KkyrjXeM5w==",- "dev": true,- "license": "MIT",- "dependencies": {- "@vitest/utils": "3.0.8",- "pathe": "^2.0.3"- },- "funding": {- "url": "https://opencollective.com/vitest"- }- },- "node_modules/vitest/node_modules/@vitest/snapshot": {- "version": "3.0.8",- "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-3.0.8.tgz",- "integrity": "sha512-x8IlMGSEMugakInj44nUrLSILh/zy1f2/BgH0UeHpNyOocG18M9CWVIFBaXPt8TrqVZWmcPjwfG/ht5tnpba8A==",- "dev": true,- "license": "MIT",- "dependencies": {- "@vitest/pretty-format": "3.0.8",- "magic-string": "^0.30.17",- "pathe": "^2.0.3"- },- "funding": {- "url": "https://opencollective.com/vitest"- }- },"node_modules/wasi-js": {"version": "1.7.3","resolved": "https://registry.npmjs.org/wasi-js/-/wasi-js-1.7.3.tgz",diff --git a/rust/sync-service/package.json b/rust/sync-service/package.jsonindex 4733cef9cd..c05e8b06f8 100644--- a/rust/sync-service/package.json+++ b/rust/sync-service/package.json@@ -4,7 +4,7 @@"@types/jsonwebtoken": "^9.0.9","bebop-tools": "^3.1.3","miniflare": "^4.20260617.0",- "vitest": "^3.0.8",+ "vitest": "^4.1.0","wrangler": "^4.102.0"},"dependencies": {diff --git a/rust/sync-service/src/ai_peer.rs b/rust/sync-service/src/ai_peer.rsnew file mode 100644index 0000000000..f7c1da8162--- /dev/null+++ b/rust/sync-service/src/ai_peer.rs@@ -0,0 +1,11 @@+//! AI editors draw their Loro peer id from a reserved block: 15 leading 9s+//! followed by a 3-digit identity (`999_999_999_999_999_000 ..= ...999`). A+//! human's peer id is random across all of u64, so a collision is ~nil.+//! Keep in sync with the TS definition (`isAiPeer` / `AI_PEER_BASE`).++pub const AI_PEER_COUNT: u64 = 1000;+pub const AI_PEER_BASE: u64 = 999_999_999_999_999_000;++pub fn is_ai_peer(peer: u64) -> bool {+ (AI_PEER_BASE..AI_PEER_BASE + AI_PEER_COUNT).contains(&peer)+}diff --git a/rust/sync-service/src/auth.rs b/rust/sync-service/src/auth.rsindex d9fb30b7ca..225a795783 100644--- a/rust/sync-service/src/auth.rs+++ b/rust/sync-service/src/auth.rs@@ -1,4 +1,3 @@-use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode};use serde::{Deserialize, Serialize};use tracing::error;use worker::Error;@@ -122,13 +121,10 @@ did not match expected value: {}",TokenFrom::QueryParams => req.query::<WebsocketQueryParams>()?.token,};- let validation = Validation::new(Algorithm::HS256);let secret = secrets.document_permissions_secret;- let key = DecodingKey::from_secret(secret.to_string().as_bytes());- let claims = decode::<AuthToken>(&token, &key, &validation)- .context("failed to decode `AuthToken`")?- .claims;+ let claims = macro_sync_service_jwt::decode::<AuthToken>(&token, &secret)+ .context("failed to decode `AuthToken`")?;Ok(claims)}diff --git a/rust/sync-service/src/durable_object.rs b/rust/sync-service/src/durable_object.rsindex acb1d201ec..5593f3c9d2 100644--- a/rust/sync-service/src/durable_object.rs+++ b/rust/sync-service/src/durable_object.rs@@ -17,6 +17,7 @@ use worker::{};use crate::{+ ai_peer::is_ai_peer,auth::{AccessLevel, TokenFrom, decode_jwt},constants::USER_PEER_D1_BINDING,d1::{PeerWithUserId, get_user_id_from_peer_id, insert_user_mapping},@@ -371,7 +372,15 @@ impl DocumentSyncSession {}path::RAW => return self.raw_handler(document_id).await,path::SNAPSHOT => return self.snapshot_handler(req, document_id).await,- path::ACTIVE_PEERS_MARKER => return self.active_peer_ids_handler().await,+ path::ACTIVE_PEERS_MARKER => {+ // `?include_ai=false` filters out AI editors; default keeps them.+ let include_ai = req+ .url()?+ .query_pairs()+ .find(|(k, _)| k == "include_ai")+ .map_or(true, |(_, v)| !matches!(v.as_ref(), "false" | "0"));+ return self.active_peer_ids_handler(include_ai).await;+ }path::INITIALIZE => {or_unauth!(claims.has_permission(&AccessLevel::Edit).then_some(()));return self.initialize_handler(req, document_id).await;@@ -465,11 +474,17 @@ impl DocumentSyncSession {Response::empty()}- async fn active_peer_ids_handler(&self) -> Result<Response> {+ /// Active peer ids. With `include_ai = false`, AI editors (peer ids from the+ /// reserved AI block) are filtered out so callers see only human collaborators.+ async fn active_peer_ids_handler(&self, include_ai: bool) -> Result<Response> {let mut peer_ids: BTreeSet<u64> = BTreeSet::new();for ws in self.state.get_websockets() {let new_peer_ids = Wsm::new(self, &ws).get_peer_ids().await?;- peer_ids.extend(new_peer_ids);+ peer_ids.extend(+ new_peer_ids+ .into_iter()+ .filter(|&p| include_ai || !is_ai_peer(p)),+ );}let str_ids = peer_ids.into_iter()diff --git a/rust/sync-service/src/lib.rs b/rust/sync-service/src/lib.rsindex 990a5bc382..6285506a80 100644--- a/rust/sync-service/src/lib.rs+++ b/rust/sync-service/src/lib.rs@@ -1,3 +1,4 @@+mod ai_peer;mod auth;mod cf_worker;mod constants;