@desplega.ai/agent-swarm 1.143.0 → 1.145.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/{acp-adapter-qhzzpz4b.js → acp-adapter-580t41c0.js} +5 -5
- package/dist/{actions-6s03jvk8.js → actions-m662tjng.js} +20 -19
- package/dist/{anthropic-messages-jc89gynp.js → anthropic-messages-vs2rabzp.js} +5 -5
- package/dist/app-zb3rndm8.js +28 -0
- package/dist/{artifact-wpy087er.js → artifact-gj9arwrr.js} +5 -5
- package/dist/{assistant-42xa0m4e.js → assistant-0qt42nzg.js} +22 -21
- package/dist/{azure-openai-responses-5qwmjys3.js → azure-openai-responses-cqar64rc.js} +5 -5
- package/dist/{boot-reembed-vx4fkhgd.js → boot-reembed-072x00vn.js} +15 -14
- package/dist/{boot-reembed-we49a2kr.js → boot-reembed-65zz2smt.js} +16 -15
- package/dist/{boot-scrub-logs-zefgmq2b.js → boot-scrub-logs-yz0ams04.js} +12 -11
- package/dist/{claude-adapter-nm0essg1.js → claude-adapter-paj270y2.js} +36 -25
- package/dist/{claude-wqn3dtca.js → claude-k04qfvsv.js} +1 -1
- package/dist/{claude-managed-adapter-vtseey1x.js → claude-managed-adapter-sactwn31.js} +5 -5
- package/dist/{claude-managed-setup-3zqp51cr.js → claude-managed-setup-w619jncn.js} +2 -2
- package/dist/claude-sdk-session-swt80j9z.js +39101 -0
- package/dist/{cli-qh4sqfws.js → cli-07jd68a1.js} +1 -1
- package/dist/{cli-9zjf3ahs.js → cli-123q2get.js} +56 -21
- package/dist/{cli-dcn5dc6j.js → cli-1ebpar1q.js} +14 -14
- package/dist/{cli-s9y6zv37.js → cli-1fj0chdk.js} +5 -5
- package/dist/{cli-7jcc9y1r.js → cli-1m2e086m.js} +1 -1
- package/dist/{cli-ppwm8c6e.js → cli-2daksqps.js} +4 -1
- package/dist/{cli-f64gdfxc.js → cli-2djtr51b.js} +1 -1
- package/dist/{cli-a4ajmfvp.js → cli-3aqnbv8r.js} +2 -2
- package/dist/{cli-mez7x2z0.js → cli-4j8k1m4w.js} +4 -4
- package/dist/{cli-vjkzg867.js → cli-4xz3pmrq.js} +13 -13
- package/dist/{cli-nkn0ka5p.js → cli-51tf7ac3.js} +1 -1
- package/dist/{cli-gy07xk4s.js → cli-53rm0rrz.js} +1 -1
- package/dist/{cli-787ph852.js → cli-55qrx3q8.js} +3 -3
- package/dist/{cli-sshnjch3.js → cli-59ej1c07.js} +10 -448
- package/dist/{cli-f3s9dry0.js → cli-5bfdm1ah.js} +1 -1
- package/dist/{cli-zxrec1gh.js → cli-65vqzz3v.js} +64 -41
- package/dist/{cli-9w49enmh.js → cli-76yrvwsa.js} +9 -3
- package/dist/{cli-87zrjxdb.js → cli-7fnkj1hj.js} +2 -2
- package/dist/{cli-dwwz52tw.js → cli-7nxes1hs.js} +1 -1
- package/dist/{cli-w2jy8x2r.js → cli-81r683bv.js} +5 -5
- package/dist/{cli-rnyttbh0.js → cli-87rg4p7x.js} +1 -1
- package/dist/{cli-mnm7w46e.js → cli-8fc8de14.js} +1 -1
- package/dist/{cli-56anwfxj.js → cli-9xvdm3bx.js} +12 -12
- package/dist/{cli-63erbyda.js → cli-a0g1gbt5.js} +3 -3
- package/dist/{cli-vsap7a8d.js → cli-aehm1rbq.js} +1 -1
- package/dist/{cli-cppy3dz4.js → cli-anktweqr.js} +6 -6
- package/dist/{cli-jmkqrhpm.js → cli-axs8e6d4.js} +1 -1
- package/dist/{cli-a3pkfpqs.js → cli-b5b30yzk.js} +2 -2
- package/dist/{cli-pq2ee5ja.js → cli-bc05qbss.js} +38 -1
- package/dist/{cli-497b685t.js → cli-bd957dgb.js} +1 -1
- package/dist/{cli-00w4hbam.js → cli-btbzvq95.js} +1 -1
- package/dist/{cli-8e1r79yq.js → cli-bxdhw615.js} +6 -6
- package/dist/{cli-xbyjkasx.js → cli-c2mk1kgf.js} +1 -1
- package/dist/{cli-35h7kw0d.js → cli-c71mg51n.js} +1 -1
- package/dist/{cli-gpt9kf01.js → cli-cg4vnyh4.js} +13 -13
- package/dist/{cli-s9yg23fg.js → cli-cmp8g3sw.js} +8 -3
- package/dist/{cli-d62cd9qs.js → cli-cn6mmc7f.js} +3 -3
- package/dist/{cli-srjj750f.js → cli-ct0et58h.js} +9 -9
- package/dist/{cli-ewvk4ceg.js → cli-dc9k2ba9.js} +10 -4
- package/dist/{cli-rx5cxzn2.js → cli-dpvkp2et.js} +1 -1
- package/dist/{cli-tdtmw1r5.js → cli-dt553ktb.js} +1 -1
- package/dist/{cli-a1rrkrb4.js → cli-e29gmdmw.js} +1 -1
- package/dist/{cli-nanaxrz6.js → cli-ej6gbxfb.js} +1 -1
- package/dist/cli-eree7rnv.js +25 -0
- package/dist/{cli-wtyyc7ev.js → cli-f567d5c1.js} +60 -22
- package/dist/cli-f7kvq46p.js +32 -0
- package/dist/{cli-b4g3p5y2.js → cli-fs1kkqgr.js} +5 -5
- package/dist/{cli-2z3qhdg0.js → cli-gh8y8f36.js} +1 -1
- package/dist/{cli-vr2nfxtt.js → cli-gxnzc6wh.js} +11 -11
- package/dist/{cli-4kj5adw0.js → cli-gzky0gkr.js} +4 -4
- package/dist/{cli-jepmgn7r.js → cli-j490egb5.js} +1 -1
- package/dist/{cli-7pnwg2vx.js → cli-jmb7g47h.js} +918 -174
- package/dist/{cli-wrfhh2qz.js → cli-jnf5zghe.js} +2 -2
- package/dist/{cli-q6d0vvey.js → cli-jyetr64j.js} +3 -3
- package/dist/{cli-whrvct19.js → cli-knd276ed.js} +1 -1
- package/dist/{cli-r3bxcgrj.js → cli-kpev657q.js} +2 -2
- package/dist/{cli-6garengg.js → cli-m1xa8be7.js} +6 -6
- package/dist/{cli-mjf432kq.js → cli-m4j6trn4.js} +3 -3
- package/dist/{cli-rzhcrw2p.js → cli-n011tws7.js} +1 -1
- package/dist/{cli-tkf8n6cw.js → cli-pdz9qde4.js} +2 -2
- package/dist/{cli-9y5b02vr.js → cli-pp9ax9pa.js} +1 -1
- package/dist/{cli-pmz8qr82.js → cli-pw9rj5t9.js} +1 -1
- package/dist/{cli-qntkxysp.js → cli-pzxba7gw.js} +209 -165
- package/dist/{cli-m4dadbv4.js → cli-q34a0e10.js} +2 -2
- package/dist/{cli-qf9kpsve.js → cli-q51yprfq.js} +2 -2
- package/dist/{cli-c5ddbdfj.js → cli-qa66qaf0.js} +2 -2
- package/dist/{cli-g814jbst.js → cli-qghpvca2.js} +1 -1
- package/dist/{cli-wvdvxjep.js → cli-qgm2pfpq.js} +1 -1
- package/dist/{cli-dx3dz0ms.js → cli-rembwgt8.js} +2 -2
- package/dist/{cli-mvsztzgn.js → cli-rs6ec3b9.js} +13 -14
- package/dist/{cli-hgkw1x1k.js → cli-rvqz34y5.js} +6 -6
- package/dist/{cli-qe636hdh.js → cli-s2f20r5w.js} +4 -4
- package/dist/{cli-6t2fgxpv.js → cli-s2qjdd6z.js} +2 -2
- package/dist/{cli-s35981d0.js → cli-sd0mxa3c.js} +2 -2
- package/dist/{cli-1cwhyvta.js → cli-sxbwep7h.js} +1 -1
- package/dist/{cli-65d93ves.js → cli-tcvj7j2r.js} +3 -3
- package/dist/{cli-tpa83vxb.js → cli-tqtvj5pt.js} +1 -1
- package/dist/{cli-6hy7hej2.js → cli-txgcknmy.js} +1 -1
- package/dist/{cli-qjwdtyxn.js → cli-v0asr5yv.js} +279 -200
- package/dist/{cli-0v7jxbvz.js → cli-vj6h9kc2.js} +1 -1
- package/dist/{cli-3cgfyfzm.js → cli-w1d1bgkk.js} +3 -3
- package/dist/{cli-pqv5gvyf.js → cli-w5e1d1vv.js} +1 -1
- package/dist/{cli-vn8s7wtf.js → cli-wvp6bvy4.js} +2 -2
- package/dist/{cli-mkq1rk7b.js → cli-xqcq9y5e.js} +1 -1
- package/dist/{cli-8p37s71j.js → cli-xvkmedgn.js} +2 -2
- package/dist/{cli-jdtvfcyw.js → cli-xytwff7f.js} +2 -2
- package/dist/{cli-g2p9bd1z.js → cli-yatxqynm.js} +1 -1
- package/dist/{cli-z6nsjrh1.js → cli-ygbjp3z8.js} +41 -6
- package/dist/{cli-9bnajfes.js → cli-z96t0thj.js} +1 -1
- package/dist/{cli-bdyxycq6.js → cli-z9fprcrf.js} +7829 -1008
- package/dist/cli-zf8jjwex.js +34 -0
- package/dist/cli.js +44 -38
- package/dist/{codex-adapter-zvh2pgah.js → codex-adapter-k98vaw2r.js} +17 -17
- package/dist/{codex-hook-79p3xs7j.js → codex-hook-9pf19cks.js} +5 -5
- package/dist/{codex-login-x7603481.js → codex-login-vx7ghkzj.js} +1 -1
- package/dist/{codex-session-runner-bs8tsjx5.js → codex-session-runner-tngtphx7.js} +17 -17
- package/dist/{commands-c73m6m2j.js → commands-y8nbr3fv.js} +12 -11
- package/dist/{db-n2bgwkr3.js → db-30p4h2t1.js} +16 -11
- package/dist/{devin-adapter-8qmhqpy3.js → devin-adapter-8vgkwh29.js} +1 -1
- package/dist/{devtools-j2rnj8w7.js → devtools-qpc3j21w.js} +3 -3
- package/dist/{e2b-b7jt70fc.js → e2b-6hb103d8.js} +11 -11
- package/dist/{es5-fmqrjzy3.js → es5-tj3tn4jj.js} +1 -1
- package/dist/{event-bus-n8sb35pw.js → event-bus-sfx8dx3t.js} +3 -2
- package/dist/{getMachineId-bsd-wpzbhkm6.js → getMachineId-bsd-xj3bn2fv.js} +3 -3
- package/dist/{getMachineId-darwin-45zabjy0.js → getMachineId-darwin-07jzjcx6.js} +3 -3
- package/dist/{getMachineId-linux-warnp5pd.js → getMachineId-linux-ahngynxd.js} +2 -2
- package/dist/{getMachineId-unsupported-5spent2a.js → getMachineId-unsupported-vy43b44z.js} +2 -2
- package/dist/{getMachineId-win-v4bvcw7a.js → getMachineId-win-8x66cynz.js} +3 -3
- package/dist/{google-generative-ai-nnw57f3t.js → google-generative-ai-tsskx7te.js} +5 -5
- package/dist/{google-vertex-rxwnws1y.js → google-vertex-ppw8ydpa.js} +6 -6
- package/dist/{handlers-s768yzzt.js → handlers-yfp1qn81.js} +23 -22
- package/dist/{hook-m950r3r8.js → hook-21jexvfx.js} +15 -15
- package/dist/hook-dcw4jfbj.js +35 -0
- package/dist/{http-5rqpbf9g.js → http-r4kq5g8d.js} +1241 -431
- package/dist/{index-shne41sd.js → index-1kq5hw2p.js} +12 -12
- package/dist/index-1p8wdstt.js +7 -0
- package/dist/{index-e19s2m8k.js → index-2sp0384z.js} +8 -8
- package/dist/{index-qgvkk4mz.js → index-53zyht4z.js} +589 -29
- package/dist/{index-t36q2v4t.js → index-66xz5e9j.js} +2 -2
- package/dist/{index-emvxcy98.js → index-8bn5jm6d.js} +3 -3
- package/dist/{index-ns5kksc1.js → index-8fdjzb2f.js} +4 -4
- package/dist/{index-1nsdwvn5.js → index-aa3sa6s8.js} +8 -8
- package/dist/{index-yqweff5n.js → index-bxmwzbgw.js} +2 -2
- package/dist/{index-g9tttwkm.js → index-f6cryxwe.js} +2 -2
- package/dist/{index-d9f3yqc5.js → index-gay66b9w.js} +30 -29
- package/dist/{index-agykc6k2.js → index-n5t1gzfb.js} +4 -4
- package/dist/index-nqvf26za.js +53 -0
- package/dist/index-nzerxd5z.js +7 -0
- package/dist/{index-3e03k6e4.js → index-pyymnzat.js} +5 -5
- package/dist/index-q3qja6ta.js +54 -0
- package/dist/{index-pdxc8kfh.js → index-qv6atbab.js} +5 -5
- package/dist/{index-ptcqm8bc.js → index-rja32qa7.js} +5 -5
- package/dist/{index-gqnmr60d.js → index-tzg5srfp.js} +5 -5
- package/dist/{index-20xqmn3v.js → index-vhncw983.js} +3 -3
- package/dist/{index-7m8yxkw5.js → index-xk036zsk.js} +7 -7
- package/dist/index-zyazbzcb.js +6 -0
- package/dist/{keepalive-sc7pvszh.js → keepalive-xs8fa55y.js} +14 -13
- package/dist/lead-d9m2t4s0.js +95 -0
- package/dist/maintenance-3zh82g6z.js +30 -0
- package/dist/{mistral-conversations-gnnamk9c.js → mistral-conversations-4ryk7h83.js} +2 -2
- package/dist/{multipart-parser-6gftesw0.js → multipart-parser-c33er1yp.js} +2 -2
- package/dist/{node-0zgg64ps.js → node-agg3zs4s.js} +1 -1
- package/dist/{node-nxwyzp1j.js → node-x1g4v0pq.js} +3 -3
- package/dist/{oauth-refresh-sweep-xrydav2q.js → oauth-refresh-sweep-03snsye5.js} +13 -12
- package/dist/{onboard-972xcrsh.js → onboard-0cg1925x.js} +7 -7
- package/dist/{openai-codex-responses-y1qgyvmt.js → openai-codex-responses-g7w48y30.js} +5 -5
- package/dist/{openai-completions-ccrxm4b6.js → openai-completions-0bqmsajw.js} +4 -4
- package/dist/{openai-responses-6r5d4z8y.js → openai-responses-nmeyptke.js} +5 -5
- package/dist/{opencode-adapter-krfzs6pd.js → opencode-adapter-fef1mrn2.js} +9 -9
- package/dist/{openrouter-images-71hthwh9.js → openrouter-images-qx18fyp8.js} +2 -2
- package/dist/{otel-impl-q0mt4sy8.js → otel-impl-0ab95z27.js} +11 -11
- package/dist/{photon_rs-8dwt2pzy.js → photon_rs-q3vet37k.js} +1 -1
- package/dist/{pi-messages-kmr52r41.js → pi-messages-79jvzz1d.js} +3 -3
- package/dist/{pi-mono-adapter-9tb0c3jh.js → pi-mono-adapter-dtkz4e7j.js} +37 -37
- package/dist/pricing-refresh-e5jhzz4j.js +23 -0
- package/dist/{rbac-roles-vxy05wcn.js → rbac-roles-2w3cmrkc.js} +13 -12
- package/dist/rbac-roles-p1mf40z3.js +33 -0
- package/dist/{registry-6nvn6bj0.js → registry-9kdck5ye.js} +1 -1
- package/dist/render-v2-xhawry3k.js +40 -0
- package/dist/seed-pricing-t223sw91.js +20 -0
- package/dist/{setup-frgenxn8.js → setup-zcs0rce7.js} +7 -7
- package/dist/{token-xg5711x5.js → token-78fepns5.js} +3 -3
- package/dist/token-util-0rrc1ybd.js +6 -0
- package/dist/worker-qnp2ka62.js +95 -0
- package/dist/{x-xkfh0w0v.js → x-n2phr5vm.js} +3 -3
- package/openapi.json +962 -4
- package/package.json +9 -3
- package/src/artifact-sdk/browser-sdk.ts +7 -3
- package/src/be/db.ts +41 -3
- package/src/be/migrations/145_slack_render_v2_delegation.sql +12 -0
- package/src/be/scripts/typecheck.ts +35 -0
- package/src/be/seed-skills/bundled-files.generated.json +4 -0
- package/src/be/swarm-config-guard.ts +9 -0
- package/src/commands/runner.ts +54 -7
- package/src/http/agents.ts +267 -91
- package/src/http/all-routes.ts +2 -0
- package/src/http/auth.ts +25 -1
- package/src/http/db-query-shared.ts +43 -22
- package/src/http/index.ts +30 -6
- package/src/http/kv.ts +16 -1
- package/src/http/memory.ts +37 -3
- package/src/http/page-proxy.ts +8 -5
- package/src/http/pages-public.ts +7 -6
- package/src/http/pages.ts +16 -3
- package/src/http/realtime.ts +28 -0
- package/src/http/rooms.ts +314 -0
- package/src/http/route-def.ts +8 -2
- package/src/http/tasks.ts +1 -1
- package/src/http/utils.ts +104 -7
- package/src/kv-reserved-namespaces.ts +6 -0
- package/src/providers/claude-adapter.ts +120 -309
- package/src/providers/claude-sdk-session.ts +586 -0
- package/src/providers/claude-session-events.ts +247 -0
- package/src/rbac/legacy-policy.ts +1 -0
- package/src/rbac/permissions.ts +4 -0
- package/src/realtime/auth.ts +130 -0
- package/src/realtime/browser.generated.txt +69 -0
- package/src/realtime/browser.ts +382 -0
- package/src/realtime/bus.ts +33 -0
- package/src/realtime/document.ts +529 -0
- package/src/realtime/rooms.ts +575 -0
- package/src/realtime/transport.ts +406 -0
- package/src/realtime/tsconfig.browser.json +14 -0
- package/src/scripts-runtime/ctx.ts +1 -2
- package/src/scripts-runtime/sdk-allowlist.ts +6 -0
- package/src/scripts-runtime/swarm-sdk.ts +35 -7
- package/src/scripts-runtime/types/stdlib.d.ts +65 -0
- package/src/scripts-runtime/types/swarm-sdk.d.ts +65 -0
- package/src/server-user.ts +28 -16
- package/src/server.ts +10 -0
- package/src/slack/ack.ts +39 -19
- package/src/slack/closure.ts +66 -0
- package/src/slack/render-v2.ts +404 -36
- package/src/tasks/terminal-result-guard.ts +14 -1
- package/src/tests/agents-harness-provider.test.ts +254 -0
- package/src/tests/artifact-sdk.test.ts +12 -2
- package/src/tests/claude-hook-credentials.test.ts +84 -0
- package/src/tests/claude-sdk-lifecycle.test.ts +262 -0
- package/src/tests/claude-sdk-live-lifecycle.test.ts +272 -0
- package/src/tests/claude-sdk-process.fixture.ts +223 -0
- package/src/tests/claude-sdk-transport.test.ts +289 -0
- package/src/tests/claude-worker-parity.test.ts +574 -0
- package/src/tests/db-query.test.ts +73 -0
- package/src/tests/db-retention.test.ts +2 -1
- package/src/tests/docs-index-footprint.test.js +4 -3
- package/src/tests/e2e-claude-transport.test.ts +204 -0
- package/src/tests/e2e-nightly-report.test.ts +41 -4
- package/src/tests/fetch-resolved-env.test.ts +126 -0
- package/src/tests/harness-provider-resolution.test.ts +7 -0
- package/src/tests/internal-ai/complete-structured.test.ts +2 -0
- package/src/tests/memory-edit-authorization.test.ts +295 -0
- package/src/tests/page-proxy.test.ts +223 -27
- package/src/tests/page-session.test.ts +32 -0
- package/src/tests/page-viewer-auth.test.ts +182 -0
- package/src/tests/pages-public-html.test.ts +3 -0
- package/src/tests/rbac-engine.test.ts +1 -0
- package/src/tests/realtime-room-auth.test.ts +68 -0
- package/src/tests/realtime-room-surfaces.test.ts +329 -0
- package/src/tests/realtime-rooms.test.ts +343 -0
- package/src/tests/realtime-transport.test.ts +551 -0
- package/src/tests/runner-session-outcome.test.ts +28 -0
- package/src/tests/sandboxed-process.test.ts +20 -8
- package/src/tests/seed-skills-bundled-files.test.ts +1 -0
- package/src/tests/send-task-output-schema.test.ts +247 -0
- package/src/tests/slack-closure.test.ts +183 -0
- package/src/tests/slack-relay-obligations.test.ts +10 -0
- package/src/tests/slack-render-v2-delegation.test.ts +373 -0
- package/src/tests/slack-render-v2.test.ts +1107 -5
- package/src/tests/spec-base-url.test.ts +2 -1
- package/src/tests/structured-output.test.ts +31 -0
- package/src/tests/tool-annotations.test.ts +41 -6
- package/src/tools/kv/kv-delete.ts +5 -1
- package/src/tools/kv/kv-incr.ts +5 -1
- package/src/tools/kv/kv-set.ts +5 -1
- package/src/tools/kv/kv-write-auth.ts +6 -1
- package/src/tools/memory-edit.ts +37 -2
- package/src/tools/rooms.ts +171 -0
- package/src/tools/send-task.ts +29 -0
- package/src/tools/tool-config.ts +26 -22
- package/src/types.ts +3 -1
- package/src/utils/claude-transport.ts +36 -0
- package/src/utils/internal-ai/complete-structured.ts +2 -1
- package/src/utils/page-session.ts +21 -28
- package/src/utils/request-auth-context.ts +6 -2
- package/src/utils/sandboxed-process.ts +31 -22
- package/src/workflows/event-bus.ts +6 -10
- package/src/workflows/executors/swarm-script.ts +2 -0
- package/src/workflows/json-schema-validator.ts +52 -0
- package/templates/skills/pages/content.md +147 -8
- package/templates/skills/pages/files/examples/multiplayer-board.html +416 -0
- package/tsconfig.json +1 -0
- package/dist/app-zej2afh5.js +0 -27
- package/dist/cli-95m51ztz.js +0 -28
- package/dist/hook-xn3pevet.js +0 -35
- package/dist/index-1qthq7vc.js +0 -6
- package/dist/index-ag4bxqkp.js +0 -53
- package/dist/index-tcnvv8w6.js +0 -7
- package/dist/index-vb4b1nts.js +0 -52
- package/dist/index-w91qbfv1.js +0 -7
- package/dist/lead-5zcqts8e.js +0 -93
- package/dist/maintenance-45j327y6.js +0 -29
- package/dist/pricing-refresh-s7n1nec8.js +0 -22
- package/dist/rbac-roles-hpph0s5w.js +0 -32
- package/dist/render-v2-8vk0y1js.js +0 -35
- package/dist/seed-pricing-an8qdaxz.js +0 -19
- package/dist/token-util-zy908s1a.js +0 -6
- package/dist/worker-0w9fdwds.js +0 -93
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import * as Y from "yjs";
|
|
2
|
+
import { changeDocument, type JsonObject, materialize, type RoomOperation } from "./document";
|
|
3
|
+
|
|
4
|
+
type Handler = (value: unknown) => void;
|
|
5
|
+
type Viewer = { userId: string; name: string; kind: string };
|
|
6
|
+
type Peer = Viewer & { data: unknown };
|
|
7
|
+
type Frame = {
|
|
8
|
+
type: string;
|
|
9
|
+
id?: number;
|
|
10
|
+
seq?: number;
|
|
11
|
+
name?: string;
|
|
12
|
+
room?: View;
|
|
13
|
+
me?: Viewer;
|
|
14
|
+
peers?: Peer[];
|
|
15
|
+
data?: unknown;
|
|
16
|
+
error?: string;
|
|
17
|
+
};
|
|
18
|
+
type View = {
|
|
19
|
+
namespace: string;
|
|
20
|
+
name: string;
|
|
21
|
+
generation: string;
|
|
22
|
+
schemaVersion: number;
|
|
23
|
+
stale: boolean;
|
|
24
|
+
snapshot: string;
|
|
25
|
+
bytes: number;
|
|
26
|
+
};
|
|
27
|
+
type Options = { schemaVersion?: number };
|
|
28
|
+
|
|
29
|
+
function encode(bytes: Uint8Array): string {
|
|
30
|
+
let value = "";
|
|
31
|
+
for (let i = 0; i < bytes.length; i += 8192)
|
|
32
|
+
value += String.fromCharCode(...bytes.subarray(i, i + 8192));
|
|
33
|
+
return btoa(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function decode(value: string): Uint8Array {
|
|
37
|
+
return Uint8Array.from(atob(value), (c) => c.charCodeAt(0));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class Events {
|
|
41
|
+
private handlers = new Map<string, Set<Handler>>();
|
|
42
|
+
on(name: string, handler: Handler): () => void {
|
|
43
|
+
const set = this.handlers.get(name) ?? new Set<Handler>();
|
|
44
|
+
set.add(handler);
|
|
45
|
+
this.handlers.set(name, set);
|
|
46
|
+
return () => set.delete(handler);
|
|
47
|
+
}
|
|
48
|
+
emit(name: string, value: unknown): void {
|
|
49
|
+
for (const handler of this.handlers.get(name) ?? []) handler(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
class Connection extends Events {
|
|
54
|
+
socket?: WebSocket;
|
|
55
|
+
rooms = new Map<string, Room>();
|
|
56
|
+
channels = new Map<string, Channel>();
|
|
57
|
+
private pending = new Map<
|
|
58
|
+
number,
|
|
59
|
+
{
|
|
60
|
+
resolve: (value: Frame) => void;
|
|
61
|
+
reject: (error: Error) => void;
|
|
62
|
+
timer: ReturnType<typeof setTimeout>;
|
|
63
|
+
}
|
|
64
|
+
>();
|
|
65
|
+
private nextId = 0;
|
|
66
|
+
private opening?: Promise<void>;
|
|
67
|
+
private retry?: ReturnType<typeof setTimeout>;
|
|
68
|
+
private stopped = false;
|
|
69
|
+
me?: Viewer;
|
|
70
|
+
|
|
71
|
+
async open(): Promise<void> {
|
|
72
|
+
if (this.socket?.readyState === WebSocket.OPEN) return;
|
|
73
|
+
if (this.opening) return this.opening;
|
|
74
|
+
const match = location.pathname.match(/^\/p\/([^/.]+)/);
|
|
75
|
+
if (!match) throw new Error("Realtime rooms require a Swarm page");
|
|
76
|
+
const url = new URL("/@swarm/realtime", location.href);
|
|
77
|
+
url.protocol = location.protocol === "https:" ? "wss:" : "ws:";
|
|
78
|
+
url.searchParams.set("pageId", decodeURIComponent(match[1]!));
|
|
79
|
+
this.opening = new Promise<void>((resolve, reject) => {
|
|
80
|
+
const socket = new WebSocket(url);
|
|
81
|
+
this.socket = socket;
|
|
82
|
+
socket.onopen = () => {
|
|
83
|
+
this.opening = undefined;
|
|
84
|
+
resolve();
|
|
85
|
+
};
|
|
86
|
+
socket.onerror = () => reject(new Error("Realtime connection failed"));
|
|
87
|
+
socket.onmessage = (event) => {
|
|
88
|
+
const message = JSON.parse(event.data) as Frame;
|
|
89
|
+
if (message.seq) socket.send(JSON.stringify({ ack: message.seq }));
|
|
90
|
+
if (message.type === "hello") this.me = message.me;
|
|
91
|
+
if (message.type === "room")
|
|
92
|
+
this.rooms.get(message.name ?? "")?.receive(message as unknown as View);
|
|
93
|
+
if (message.type === "presence")
|
|
94
|
+
this.rooms.get(message.name ?? "")?.receivePresence(message.peers);
|
|
95
|
+
if (message.type === "channel")
|
|
96
|
+
this.channels.get(message.name ?? "")?.emit("message", message.data);
|
|
97
|
+
if (message.type === "deleted") this.rooms.get(message.name ?? "")?.deleted();
|
|
98
|
+
if (message.id !== undefined) {
|
|
99
|
+
const pending = this.pending.get(message.id);
|
|
100
|
+
if (pending) {
|
|
101
|
+
clearTimeout(pending.timer);
|
|
102
|
+
this.pending.delete(message.id);
|
|
103
|
+
if (message.type === "error") pending.reject(new Error(message.error));
|
|
104
|
+
else pending.resolve(message);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
socket.onclose = () => {
|
|
109
|
+
this.opening = undefined;
|
|
110
|
+
reject(new Error("Realtime connection closed"));
|
|
111
|
+
for (const pending of this.pending.values()) {
|
|
112
|
+
clearTimeout(pending.timer);
|
|
113
|
+
pending.reject(new Error("Realtime connection closed"));
|
|
114
|
+
}
|
|
115
|
+
this.pending.clear();
|
|
116
|
+
if (!this.stopped)
|
|
117
|
+
this.retry = setTimeout(() => {
|
|
118
|
+
void this.reconnect();
|
|
119
|
+
}, 1000);
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
return this.opening;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private async reconnect(): Promise<void> {
|
|
126
|
+
try {
|
|
127
|
+
await this.open();
|
|
128
|
+
for (const room of this.rooms.values()) await room.join(true);
|
|
129
|
+
for (const name of this.channels.keys()) await this.request({ op: "subscribe", name });
|
|
130
|
+
} catch (error) {
|
|
131
|
+
for (const room of this.rooms.values()) room.emit("error", error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async request(message: Record<string, unknown>): Promise<Frame> {
|
|
136
|
+
await this.open();
|
|
137
|
+
const id = ++this.nextId;
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
const timer = setTimeout(() => {
|
|
140
|
+
this.pending.delete(id);
|
|
141
|
+
reject(new Error("Realtime request timed out"));
|
|
142
|
+
}, 15_000);
|
|
143
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
144
|
+
this.socket!.send(JSON.stringify({ ...message, id }));
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
close(): void {
|
|
149
|
+
this.stopped = true;
|
|
150
|
+
clearTimeout(this.retry);
|
|
151
|
+
this.socket?.close();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
class Room extends Events {
|
|
156
|
+
ydoc = new Y.Doc({ gc: true });
|
|
157
|
+
generation = "";
|
|
158
|
+
stale = false;
|
|
159
|
+
bytes = 0;
|
|
160
|
+
me?: Viewer;
|
|
161
|
+
private ready?: Promise<void>;
|
|
162
|
+
private writes = Promise.resolve();
|
|
163
|
+
private lastWrite = Promise.resolve();
|
|
164
|
+
private peers: Peer[] = [];
|
|
165
|
+
private presenceData: unknown = {};
|
|
166
|
+
private presenceTimer?: ReturnType<typeof setTimeout>;
|
|
167
|
+
private closed = false;
|
|
168
|
+
readonly schemaVersion: number;
|
|
169
|
+
readonly presence: { set: (data: unknown) => void; readonly peers: Peer[] };
|
|
170
|
+
|
|
171
|
+
constructor(
|
|
172
|
+
private readonly connection: Connection,
|
|
173
|
+
readonly name: string,
|
|
174
|
+
options: Options,
|
|
175
|
+
) {
|
|
176
|
+
super();
|
|
177
|
+
this.schemaVersion = options.schemaVersion ?? 1;
|
|
178
|
+
const self = this;
|
|
179
|
+
this.presence = {
|
|
180
|
+
set(data) {
|
|
181
|
+
self.presenceData = structuredClone(data);
|
|
182
|
+
if (!self.presenceTimer)
|
|
183
|
+
self.presenceTimer = setTimeout(() => {
|
|
184
|
+
self.presenceTimer = undefined;
|
|
185
|
+
void connection
|
|
186
|
+
.request({ op: "presence", name, data: self.presenceData })
|
|
187
|
+
.catch((error) => self.emit("error", error));
|
|
188
|
+
}, 50);
|
|
189
|
+
},
|
|
190
|
+
get peers() {
|
|
191
|
+
return self.peers;
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
this.observe();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
get state(): JsonObject {
|
|
198
|
+
return materialize(this.ydoc);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private observe(): void {
|
|
202
|
+
this.ydoc.on("update", (_update: Uint8Array, origin: unknown) => {
|
|
203
|
+
this.emit("change", this.state);
|
|
204
|
+
if (origin === "remote" || !this.generation || this.closed) return;
|
|
205
|
+
if (this.connection.socket?.readyState !== WebSocket.OPEN) return;
|
|
206
|
+
const snapshot = encode(Y.encodeStateAsUpdate(this.ydoc));
|
|
207
|
+
const generation = this.generation;
|
|
208
|
+
const write = this.writes.then(async () => {
|
|
209
|
+
try {
|
|
210
|
+
const response = await this.connection.request({
|
|
211
|
+
op: "update",
|
|
212
|
+
name: this.name,
|
|
213
|
+
generation,
|
|
214
|
+
schemaVersion: this.schemaVersion,
|
|
215
|
+
snapshot,
|
|
216
|
+
});
|
|
217
|
+
if (response.room) this.receive(response.room);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
// A rejected write must not remain visible as accepted local state.
|
|
220
|
+
if (this.connection.socket?.readyState === WebSocket.OPEN) {
|
|
221
|
+
const response = await this.connection.request({
|
|
222
|
+
op: "join",
|
|
223
|
+
name: this.name,
|
|
224
|
+
schemaVersion: this.schemaVersion,
|
|
225
|
+
});
|
|
226
|
+
this.receive(response.room, true);
|
|
227
|
+
}
|
|
228
|
+
this.emit("error", error);
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
this.lastWrite = write;
|
|
233
|
+
this.writes = write.catch(() => {});
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async join(resync = false): Promise<void> {
|
|
238
|
+
if (this.ready && !resync) return this.ready;
|
|
239
|
+
this.ready = (async () => {
|
|
240
|
+
const generation = this.generation;
|
|
241
|
+
const response = await this.connection.request({
|
|
242
|
+
op: "join",
|
|
243
|
+
name: this.name,
|
|
244
|
+
schemaVersion: this.schemaVersion,
|
|
245
|
+
});
|
|
246
|
+
this.me = response.me;
|
|
247
|
+
this.receive(response.room);
|
|
248
|
+
this.receivePresence(response.peers);
|
|
249
|
+
if (resync && generation === this.generation && !this.stale) {
|
|
250
|
+
const result = await this.connection.request({
|
|
251
|
+
op: "update",
|
|
252
|
+
name: this.name,
|
|
253
|
+
schemaVersion: this.schemaVersion,
|
|
254
|
+
generation,
|
|
255
|
+
snapshot: encode(Y.encodeStateAsUpdate(this.ydoc)),
|
|
256
|
+
});
|
|
257
|
+
this.receive(result.room);
|
|
258
|
+
}
|
|
259
|
+
if (resync) this.presence.set(this.presenceData);
|
|
260
|
+
})();
|
|
261
|
+
return this.ready;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
receive(view: View | undefined, replace = false): void {
|
|
265
|
+
if (this.closed || !view) return;
|
|
266
|
+
const reset = replace || (this.generation !== "" && view.generation !== this.generation);
|
|
267
|
+
this.generation = view.generation;
|
|
268
|
+
this.stale = view.schemaVersion !== this.schemaVersion;
|
|
269
|
+
this.bytes = view.bytes;
|
|
270
|
+
if (reset) {
|
|
271
|
+
this.ydoc.destroy();
|
|
272
|
+
this.ydoc = new Y.Doc({ gc: true });
|
|
273
|
+
this.observe();
|
|
274
|
+
}
|
|
275
|
+
Y.applyUpdate(this.ydoc, decode(view.snapshot), "remote");
|
|
276
|
+
if (reset) this.emit("reset", this.state);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
receivePresence(peers: Peer[] = []): void {
|
|
280
|
+
this.peers = peers;
|
|
281
|
+
this.emit("presence", peers);
|
|
282
|
+
}
|
|
283
|
+
deleted(): void {
|
|
284
|
+
this.closed = true;
|
|
285
|
+
this.emit("error", new Error("Room owner was deleted"));
|
|
286
|
+
this.ydoc.destroy();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async change(fn: (state: JsonObject) => void): Promise<void> {
|
|
290
|
+
if (this.closed) throw new Error("Room is closed");
|
|
291
|
+
if (this.stale) throw new Error("Room schema is stale. Read the state and reset explicitly.");
|
|
292
|
+
if (changeDocument(this.ydoc, fn).length > 0) await this.lastWrite;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
async apply(operations: RoomOperation[]): Promise<void> {
|
|
296
|
+
if (this.closed || this.stale) throw new Error("Room is closed or stale");
|
|
297
|
+
await this.writes;
|
|
298
|
+
const response = await this.connection.request({
|
|
299
|
+
op: "change",
|
|
300
|
+
name: this.name,
|
|
301
|
+
generation: this.generation,
|
|
302
|
+
schemaVersion: this.schemaVersion,
|
|
303
|
+
operations,
|
|
304
|
+
});
|
|
305
|
+
this.receive(response.room);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async reset(state: Record<string, unknown> = {}): Promise<void> {
|
|
309
|
+
if (this.closed) throw new Error("Room is closed");
|
|
310
|
+
await this.writes;
|
|
311
|
+
const response = await this.connection.request({
|
|
312
|
+
op: "reset",
|
|
313
|
+
name: this.name,
|
|
314
|
+
schemaVersion: this.schemaVersion,
|
|
315
|
+
state,
|
|
316
|
+
});
|
|
317
|
+
this.receive(response.room);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async close(): Promise<void> {
|
|
321
|
+
this.closed = true;
|
|
322
|
+
clearTimeout(this.presenceTimer);
|
|
323
|
+
this.connection.rooms.delete(this.name);
|
|
324
|
+
try {
|
|
325
|
+
await this.connection.request({ op: "leave", name: this.name });
|
|
326
|
+
} finally {
|
|
327
|
+
this.ydoc.destroy();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
class Channel extends Events {
|
|
333
|
+
constructor(
|
|
334
|
+
private readonly connection: Connection,
|
|
335
|
+
readonly name: string,
|
|
336
|
+
) {
|
|
337
|
+
super();
|
|
338
|
+
}
|
|
339
|
+
async publish(data: unknown): Promise<void> {
|
|
340
|
+
await this.connection.request({ op: "publish", name: this.name, data });
|
|
341
|
+
}
|
|
342
|
+
async close(): Promise<void> {
|
|
343
|
+
this.connection.channels.delete(this.name);
|
|
344
|
+
await this.connection.request({ op: "unsubscribe", name: this.name });
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const connection = new Connection();
|
|
349
|
+
window.addEventListener("pagehide", () => connection.close());
|
|
350
|
+
|
|
351
|
+
export async function room(name = "default", options: Options = {}): Promise<Room> {
|
|
352
|
+
if (!/^[a-zA-Z0-9_-]{1,64}$/.test(name)) throw new Error("Invalid room name");
|
|
353
|
+
let result = connection.rooms.get(name);
|
|
354
|
+
if (result && result.schemaVersion !== (options.schemaVersion ?? 1))
|
|
355
|
+
throw new Error("Room already opened with a different schema version");
|
|
356
|
+
if (!result) {
|
|
357
|
+
result = new Room(connection, name, options);
|
|
358
|
+
connection.rooms.set(name, result);
|
|
359
|
+
}
|
|
360
|
+
try {
|
|
361
|
+
await result.join();
|
|
362
|
+
} catch (error) {
|
|
363
|
+
connection.rooms.delete(name);
|
|
364
|
+
throw error;
|
|
365
|
+
}
|
|
366
|
+
return result;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export async function channel(name: string): Promise<Channel> {
|
|
370
|
+
if (!/^[a-zA-Z0-9_-]{1,64}$/.test(name)) throw new Error("Invalid channel name");
|
|
371
|
+
const existing = connection.channels.get(name);
|
|
372
|
+
if (existing) return existing;
|
|
373
|
+
const result = new Channel(connection, name);
|
|
374
|
+
connection.channels.set(name, result);
|
|
375
|
+
try {
|
|
376
|
+
await connection.request({ op: "subscribe", name });
|
|
377
|
+
} catch (error) {
|
|
378
|
+
connection.channels.delete(name);
|
|
379
|
+
throw error;
|
|
380
|
+
}
|
|
381
|
+
return result;
|
|
382
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
|
|
3
|
+
type Handler = (payload: unknown) => void;
|
|
4
|
+
|
|
5
|
+
// Delivery is synchronous and ephemeral. Rooms recover through CRDT sync.
|
|
6
|
+
export class InProcessBus {
|
|
7
|
+
private readonly emitter = new EventEmitter();
|
|
8
|
+
|
|
9
|
+
constructor() {
|
|
10
|
+
this.emitter.setMaxListeners(0);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
publish(topic: string, payload: unknown): void {
|
|
14
|
+
this.emitter.emit(topic, payload);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
subscribe(topic: string, handler: Handler): () => void {
|
|
18
|
+
this.emitter.on(topic, handler);
|
|
19
|
+
return () => this.unsubscribe(topic, handler);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
unsubscribe(topic: string, handler: Handler): void {
|
|
23
|
+
this.emitter.off(topic, handler);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
subscriberCount(topic: string): number {
|
|
27
|
+
return this.emitter.listenerCount(topic);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const state = globalThis as typeof globalThis & { __realtimeBus?: InProcessBus };
|
|
32
|
+
state.__realtimeBus ??= new InProcessBus();
|
|
33
|
+
export const realtimeBus = state.__realtimeBus;
|