@sixb/core 0.2.3 → 0.2.5
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/dist/actions/index.js +28 -28
- package/dist/agents/cancel.d.ts +1 -1
- package/dist/agents/cancel.d.ts.map +1 -1
- package/dist/agents/child-question.d.ts +35 -2
- package/dist/agents/child-question.d.ts.map +1 -1
- package/dist/agents/execution.d.ts +35 -3
- package/dist/agents/execution.d.ts.map +1 -1
- package/dist/agents/ids.d.ts +4 -0
- package/dist/agents/ids.d.ts.map +1 -1
- package/dist/agents/index.d.ts +9 -7
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/index.js +59 -33
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/model-loop.d.ts.map +1 -1
- package/dist/agents/queued-turn.d.ts +16 -0
- package/dist/agents/queued-turn.d.ts.map +1 -0
- package/dist/agents/request.d.ts +79 -1
- package/dist/agents/request.d.ts.map +1 -1
- package/dist/agents/run-after-finish.d.ts +10 -0
- package/dist/agents/run-after-finish.d.ts.map +1 -1
- package/dist/agents/run-artifacts.d.ts +0 -38
- package/dist/agents/run-artifacts.d.ts.map +1 -1
- package/dist/agents/run-events.d.ts +6 -1
- package/dist/agents/run-events.d.ts.map +1 -1
- package/dist/agents/run-session-children.d.ts +9 -0
- package/dist/agents/run-session-children.d.ts.map +1 -1
- package/dist/agents/run-session-http.d.ts.map +1 -1
- package/dist/agents/run-session-serve.d.ts.map +1 -1
- package/dist/agents/run-session.d.ts +14 -1
- package/dist/agents/run-session.d.ts.map +1 -1
- package/dist/agents/run-turn.d.ts.map +1 -1
- package/dist/agents/streams/index.d.ts +1 -1
- package/dist/agents/streams/index.d.ts.map +1 -1
- package/dist/agents/streams/index.js +1 -1
- package/dist/agents/streams/internal.js +1 -1
- package/dist/agents/streams/protocol.d.ts +16 -0
- package/dist/agents/streams/protocol.d.ts.map +1 -1
- package/dist/agents/tool-files.d.ts +1 -1
- package/dist/agents/tool-files.d.ts.map +1 -1
- package/dist/agents/tool-input-coercion.d.ts +20 -0
- package/dist/agents/tool-input-coercion.d.ts.map +1 -0
- package/dist/agents/turn-answered.d.ts +7 -0
- package/dist/agents/turn-answered.d.ts.map +1 -0
- package/dist/agents/validation.d.ts +1 -1
- package/dist/agents/validation.d.ts.map +1 -1
- package/dist/artifacts/canvas-display.d.ts +3 -5
- package/dist/artifacts/canvas-display.d.ts.map +1 -1
- package/dist/artifacts/comments.d.ts +14 -3
- package/dist/artifacts/comments.d.ts.map +1 -1
- package/dist/artifacts/document-fill.d.ts +18 -0
- package/dist/artifacts/document-fill.d.ts.map +1 -0
- package/dist/artifacts/errors.d.ts +1 -1
- package/dist/artifacts/errors.d.ts.map +1 -1
- package/dist/artifacts/events.d.ts +3 -2
- package/dist/artifacts/events.d.ts.map +1 -1
- package/dist/artifacts/execution.d.ts +40 -4
- package/dist/artifacts/execution.d.ts.map +1 -1
- package/dist/artifacts/frames/assets.d.ts +8 -1
- package/dist/artifacts/frames/assets.d.ts.map +1 -1
- package/dist/artifacts/frames/document-data.d.ts +98 -0
- package/dist/artifacts/frames/document-data.d.ts.map +1 -0
- package/dist/artifacts/frames/entries.d.ts +12 -7
- package/dist/artifacts/frames/entries.d.ts.map +1 -1
- package/dist/artifacts/frames/index.d.ts +2 -1
- package/dist/artifacts/frames/index.d.ts.map +1 -1
- package/dist/artifacts/frames/index.js +22 -13
- package/dist/artifacts/frames/index.js.map +4 -4
- package/dist/artifacts/frames/kit.d.ts +15 -0
- package/dist/artifacts/frames/kit.d.ts.map +1 -1
- package/dist/artifacts/frames/protocol.d.ts +65 -3
- package/dist/artifacts/frames/protocol.d.ts.map +1 -1
- package/dist/artifacts/frames/runtime.d.ts +32 -5
- package/dist/artifacts/frames/runtime.d.ts.map +1 -1
- package/dist/artifacts/index.d.ts +4 -2
- package/dist/artifacts/index.d.ts.map +1 -1
- package/dist/artifacts/index.js +35 -3
- package/dist/artifacts/index.js.map +3 -3
- package/dist/artifacts/live/blocks.d.ts +62 -0
- package/dist/artifacts/live/blocks.d.ts.map +1 -0
- package/dist/artifacts/live/board.d.ts +6 -0
- package/dist/artifacts/live/board.d.ts.map +1 -1
- package/dist/artifacts/live/document.d.ts +12 -0
- package/dist/artifacts/live/document.d.ts.map +1 -1
- package/dist/artifacts/live/index.d.ts +2 -0
- package/dist/artifacts/live/index.d.ts.map +1 -1
- package/dist/artifacts/live/index.js +148 -44
- package/dist/artifacts/live/index.js.map +3 -3
- package/dist/artifacts/live/page.d.ts +49 -0
- package/dist/artifacts/live/page.d.ts.map +1 -0
- package/dist/artifacts/live/protocol.d.ts +5 -0
- package/dist/artifacts/live/protocol.d.ts.map +1 -1
- package/dist/artifacts/live/server.d.ts +2 -2
- package/dist/artifacts/live/server.d.ts.map +1 -1
- package/dist/artifacts/live/server.js +23 -19
- package/dist/artifacts/live/server.js.map +1 -1
- package/dist/artifacts/live/state.d.ts +4 -1
- package/dist/artifacts/live/state.d.ts.map +1 -1
- package/dist/artifacts/outline.d.ts +16 -0
- package/dist/artifacts/outline.d.ts.map +1 -1
- package/dist/artifacts/paper.d.ts +22 -0
- package/dist/artifacts/paper.d.ts.map +1 -0
- package/dist/artifacts/pdf-job.d.ts +9 -0
- package/dist/artifacts/pdf-job.d.ts.map +1 -0
- package/dist/artifacts/publish/fields.d.ts +25 -0
- package/dist/artifacts/publish/fields.d.ts.map +1 -0
- package/dist/artifacts/publish/index.d.ts +2 -1
- package/dist/artifacts/publish/index.d.ts.map +1 -1
- package/dist/artifacts/publish/index.js +26 -18
- package/dist/artifacts/publish/index.js.map +1 -1
- package/dist/artifacts/publish/merge.d.ts +5 -5
- package/dist/artifacts/publish/merge.d.ts.map +1 -1
- package/dist/artifacts/publish/pdf.d.ts +28 -0
- package/dist/artifacts/publish/pdf.d.ts.map +1 -0
- package/dist/artifacts/publish/previews.d.ts +12 -20
- package/dist/artifacts/publish/previews.d.ts.map +1 -1
- package/dist/artifacts/publish/publish.d.ts.map +1 -1
- package/dist/artifacts/publish/queries.d.ts +6 -1
- package/dist/artifacts/publish/queries.d.ts.map +1 -1
- package/dist/artifacts/publish/query-answers.d.ts +5 -2
- package/dist/artifacts/publish/query-answers.d.ts.map +1 -1
- package/dist/artifacts/publish/validate/document-html.d.ts +24 -0
- package/dist/artifacts/publish/validate/document-html.d.ts.map +1 -0
- package/dist/artifacts/publish/validate/document.d.ts +5 -3
- package/dist/artifacts/publish/validate/document.d.ts.map +1 -1
- package/dist/artifacts/queries.d.ts +16 -2
- package/dist/artifacts/queries.d.ts.map +1 -1
- package/dist/auth/index.js +2 -2
- package/dist/authorization/event-visibility.d.ts.map +1 -1
- package/dist/authorization/index.js +1 -1
- package/dist/blob-storage/index.js +1 -1
- package/dist/bootstrap/index.js +38 -38
- package/dist/{chunk-tqa68kj7.js → chunk-0hkdtwqk.js} +5 -5
- package/dist/{chunk-s5n9617r.js → chunk-0mvjpgvm.js} +5 -5
- package/dist/{chunk-k66rp2hr.js → chunk-1yh6jgtz.js} +1 -1
- package/dist/{chunk-26qet7wh.js → chunk-5t9gm5tt.js} +4 -4
- package/dist/{chunk-a6fjkcne.js → chunk-60662fyv.js} +54 -28
- package/dist/{chunk-a6fjkcne.js.map → chunk-60662fyv.js.map} +7 -6
- package/dist/{chunk-e0hnbb1m.js → chunk-61zptves.js} +1 -1
- package/dist/{chunk-hkaa807g.js → chunk-68hm50qh.js} +3 -3
- package/dist/{chunk-9nsjjd44.js → chunk-8tv3qd65.js} +2 -1
- package/dist/chunk-8tv3qd65.js.map +12 -0
- package/dist/{chunk-fe6r7yhg.js → chunk-92wrka15.js} +1 -1
- package/dist/{chunk-m7b9q5dk.js → chunk-aw1z0gre.js} +4 -4
- package/dist/chunk-az95tsh3.js.map +2 -2
- package/dist/{chunk-4j0sagwv.js → chunk-b4escq4c.js} +62 -14
- package/dist/{chunk-4j0sagwv.js.map → chunk-b4escq4c.js.map} +7 -7
- package/dist/chunk-bqr703h4.js +1799 -0
- package/dist/chunk-bqr703h4.js.map +13 -0
- package/dist/{chunk-eptrvnby.js → chunk-c29729me.js} +42 -3
- package/dist/chunk-c29729me.js.map +12 -0
- package/dist/{chunk-t24yktqr.js → chunk-dvgc56n2.js} +20 -4
- package/dist/{chunk-t24yktqr.js.map → chunk-dvgc56n2.js.map} +5 -5
- package/dist/{chunk-3014rfjz.js → chunk-ewqn51gr.js} +25 -7
- package/dist/{chunk-3014rfjz.js.map → chunk-ewqn51gr.js.map} +3 -3
- package/dist/{chunk-7z5rkf0m.js → chunk-ey1p8w68.js} +175 -5
- package/dist/chunk-ey1p8w68.js.map +13 -0
- package/dist/{chunk-z0t4p2sk.js → chunk-fpf57gca.js} +350 -61
- package/dist/chunk-fpf57gca.js.map +47 -0
- package/dist/{chunk-kv0kvknv.js → chunk-fzae1jpq.js} +7 -7
- package/dist/{chunk-b9qfh2ya.js → chunk-fzbhybya.js} +29 -2
- package/dist/chunk-fzbhybya.js.map +11 -0
- package/dist/{chunk-2by73sm2.js → chunk-gjhrz66x.js} +225 -17
- package/dist/{chunk-2by73sm2.js.map → chunk-gjhrz66x.js.map} +5 -5
- package/dist/chunk-hn2r7z41.js.map +1 -1
- package/dist/{chunk-6gtxshcc.js → chunk-k29n4176.js} +6 -6
- package/dist/{chunk-48c1z8nj.js → chunk-k3jh8gsg.js} +10 -2
- package/dist/chunk-k3jh8gsg.js.map +10 -0
- package/dist/{chunk-w9s23r24.js → chunk-m3qx2pns.js} +751 -93
- package/dist/chunk-m3qx2pns.js.map +34 -0
- package/dist/{chunk-smnt672s.js → chunk-m9ketb51.js} +20 -4
- package/dist/chunk-m9ketb51.js.map +10 -0
- package/dist/chunk-mbwsgjn7.js +40 -0
- package/dist/chunk-mbwsgjn7.js.map +11 -0
- package/dist/{chunk-sysnvjce.js → chunk-naxjka9v.js} +2 -2
- package/dist/{chunk-n4zzaf6q.js → chunk-nxysqbt0.js} +4 -2
- package/dist/chunk-nxysqbt0.js.map +10 -0
- package/dist/{chunk-vzkxqf8h.js → chunk-paxabbqd.js} +49 -5
- package/dist/chunk-paxabbqd.js.map +23 -0
- package/dist/{chunk-8tqnsx3q.js → chunk-pc7kra3k.js} +5 -3
- package/dist/{chunk-8tqnsx3q.js.map → chunk-pc7kra3k.js.map} +3 -3
- package/dist/{chunk-qafng88v.js → chunk-pqvvqc65.js} +4 -4
- package/dist/chunk-pvpq483h.js.map +2 -2
- package/dist/{chunk-mzx71snp.js → chunk-q7g9cqwt.js} +7 -2
- package/dist/{chunk-mzx71snp.js.map → chunk-q7g9cqwt.js.map} +3 -3
- package/dist/{chunk-8t2ynwg0.js → chunk-q8d9p0tb.js} +24 -1
- package/dist/{chunk-8t2ynwg0.js.map → chunk-q8d9p0tb.js.map} +3 -3
- package/dist/{chunk-wa05txmk.js → chunk-qavwr628.js} +49 -5
- package/dist/chunk-qavwr628.js.map +12 -0
- package/dist/{chunk-bkmnd7zq.js → chunk-rg725hry.js} +174 -30
- package/dist/{chunk-bkmnd7zq.js.map → chunk-rg725hry.js.map} +9 -8
- package/dist/{chunk-8p7bze4c.js → chunk-s1mqyqmm.js} +3 -3
- package/dist/{chunk-p351afzm.js → chunk-s5pj68fa.js} +2 -2
- package/dist/{chunk-855w9nvw.js → chunk-vehcr9eb.js} +1 -1
- package/dist/{chunk-qds484t8.js → chunk-vs2s3p8n.js} +932 -21
- package/dist/chunk-vs2s3p8n.js.map +20 -0
- package/dist/chunk-vtjzwymr.js.map +2 -2
- package/dist/{chunk-n68vz95m.js → chunk-wqe9shx2.js} +1 -1
- package/dist/{chunk-0k3r5kb3.js → chunk-xcrnrpfh.js} +1 -1
- package/dist/{chunk-cwdzcv37.js → chunk-xrmbhap8.js} +2 -2
- package/dist/{chunk-cwdzcv37.js.map → chunk-xrmbhap8.js.map} +2 -2
- package/dist/{chunk-f6ahf5gf.js → chunk-xxfj0rr2.js} +20 -5
- package/dist/chunk-xxfj0rr2.js.map +13 -0
- package/dist/{chunk-3hzhcfsv.js → chunk-y8fwm3z2.js} +2 -2
- package/dist/{chunk-3hzhcfsv.js.map → chunk-y8fwm3z2.js.map} +3 -3
- package/dist/{chunk-e9w4mgdh.js → chunk-z1njfqqs.js} +589 -53
- package/dist/{chunk-e9w4mgdh.js.map → chunk-z1njfqqs.js.map} +14 -11
- package/dist/{chunk-yv8hv4wq.js → chunk-z4jqb44a.js} +3 -1
- package/dist/chunk-z4jqb44a.js.map +10 -0
- package/dist/{chunk-s7xqfxs9.js → chunk-zft9bzbc.js} +2 -1
- package/dist/{chunk-s7xqfxs9.js.map → chunk-zft9bzbc.js.map} +3 -3
- package/dist/{chunk-38yvdqzd.js → chunk-zxhym8tr.js} +1 -1
- package/dist/connectors/connection-handles.d.ts +9 -0
- package/dist/connectors/connection-handles.d.ts.map +1 -0
- package/dist/connectors/connections/authorizations.d.ts +9 -0
- package/dist/connectors/connections/authorizations.d.ts.map +1 -1
- package/dist/connectors/connections/events.d.ts +17 -0
- package/dist/connectors/connections/events.d.ts.map +1 -0
- package/dist/connectors/connections/runs.d.ts +3 -0
- package/dist/connectors/connections/runs.d.ts.map +1 -1
- package/dist/connectors/connections/service.d.ts +4 -0
- package/dist/connectors/connections/service.d.ts.map +1 -1
- package/dist/connectors/execution.d.ts +7 -1
- package/dist/connectors/execution.d.ts.map +1 -1
- package/dist/connectors/index.d.ts +1 -1
- package/dist/connectors/index.d.ts.map +1 -1
- package/dist/connectors/types.d.ts +22 -1
- package/dist/connectors/types.d.ts.map +1 -1
- package/dist/datasets/write.js +1 -1
- package/dist/deploy/index.d.ts +1 -1
- package/dist/deploy/index.d.ts.map +1 -1
- package/dist/deploy/internal.js +3 -2
- package/dist/deploy/internal.js.map +3 -3
- package/dist/deploy/services.d.ts.map +1 -1
- package/dist/deploy/types.d.ts +18 -1
- package/dist/deploy/types.d.ts.map +1 -1
- package/dist/events/definitions.d.ts +4 -0
- package/dist/events/definitions.d.ts.map +1 -1
- package/dist/events/execution.d.ts.map +1 -1
- package/dist/events/index.d.ts +2 -2
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +3 -3
- package/dist/events/outbox-dispatcher.d.ts +7 -0
- package/dist/events/outbox-dispatcher.d.ts.map +1 -1
- package/dist/events/scope.d.ts +1 -0
- package/dist/events/scope.d.ts.map +1 -1
- package/dist/events/scope.js +1 -1
- package/dist/events/selectors/builder.d.ts +9 -1
- package/dist/events/selectors/builder.d.ts.map +1 -1
- package/dist/events/selectors/index.d.ts +2 -2
- package/dist/events/selectors/index.d.ts.map +1 -1
- package/dist/events/selectors/index.js +2 -2
- package/dist/events/selectors/predicate.d.ts.map +1 -1
- package/dist/events/selectors/types.d.ts +18 -3
- package/dist/events/selectors/types.d.ts.map +1 -1
- package/dist/events/types/artifacts.d.ts +7 -1
- package/dist/events/types/artifacts.d.ts.map +1 -1
- package/dist/events/types/connectors.d.ts +32 -0
- package/dist/events/types/connectors.d.ts.map +1 -0
- package/dist/events/types/index.d.ts +3 -1
- package/dist/events/types/index.d.ts.map +1 -1
- package/dist/execution/agent.js +28 -28
- package/dist/execution/authorization.d.ts +5 -0
- package/dist/execution/authorization.d.ts.map +1 -1
- package/dist/execution/primitive.js +28 -28
- package/dist/execution/request.js +29 -29
- package/dist/execution/scopes.d.ts +8 -0
- package/dist/execution/scopes.d.ts.map +1 -1
- package/dist/execution/script.d.ts +7 -0
- package/dist/execution/script.d.ts.map +1 -0
- package/dist/execution/types.d.ts +16 -1
- package/dist/execution/types.d.ts.map +1 -1
- package/dist/http/api-routes.d.ts.map +1 -1
- package/dist/http/index.js +1 -1
- package/dist/index.d.ts +9 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +46 -44
- package/dist/index.js.map +1 -1
- package/dist/lake-storage/index.js +1 -1
- package/dist/lineage/executors.d.ts +1 -1
- package/dist/lineage/executors.d.ts.map +1 -1
- package/dist/maintenance/index.js +1 -1
- package/dist/materialization/event-envelopes.d.ts +3 -3
- package/dist/materialization/event-envelopes.d.ts.map +1 -1
- package/dist/materialization/events.d.ts +6 -0
- package/dist/materialization/events.d.ts.map +1 -1
- package/dist/materialization/index.js +1 -1
- package/dist/materialization/model.d.ts +6 -0
- package/dist/materialization/model.d.ts.map +1 -1
- package/dist/materializer/index.js +13 -13
- package/dist/materializer/projections/replace.d.ts.map +1 -1
- package/dist/materializer/projections/replacement-plan.d.ts +2 -0
- package/dist/materializer/projections/replacement-plan.d.ts.map +1 -1
- package/dist/materializer/shared/normalize.d.ts +1 -0
- package/dist/materializer/shared/normalize.d.ts.map +1 -1
- package/dist/models/execution/index.js +12 -12
- package/dist/models/language-model.d.ts +6 -0
- package/dist/models/language-model.d.ts.map +1 -1
- package/dist/models/tools.d.ts +2 -0
- package/dist/models/tools.d.ts.map +1 -1
- package/dist/objects/index.js +28 -28
- package/dist/objects/query/index.js +12 -12
- package/dist/pipelines/index.js +18 -18
- package/dist/projections/builders.d.ts +7 -1
- package/dist/projections/builders.d.ts.map +1 -1
- package/dist/projections/index.d.ts +1 -1
- package/dist/projections/index.d.ts.map +1 -1
- package/dist/projections/internal.js +13 -13
- package/dist/projections/types.d.ts +10 -0
- package/dist/projections/types.d.ts.map +1 -1
- package/dist/projections/validation.d.ts.map +1 -1
- package/dist/queues/in-memory.d.ts +2 -1
- package/dist/queues/in-memory.d.ts.map +1 -1
- package/dist/queues/index.d.ts +1 -1
- package/dist/queues/index.d.ts.map +1 -1
- package/dist/queues/index.js +1 -1
- package/dist/queues/types.d.ts +40 -13
- package/dist/queues/types.d.ts.map +1 -1
- package/dist/runtime/host.d.ts +13 -0
- package/dist/runtime/host.d.ts.map +1 -1
- package/dist/runtime/internal.d.ts +1 -0
- package/dist/runtime/internal.d.ts.map +1 -1
- package/dist/runtime/internal.js +39 -18
- package/dist/runtime/internal.js.map +4 -3
- package/dist/runtime/resolve-definitions.d.ts +1 -1
- package/dist/runtime/resolve-definitions.d.ts.map +1 -1
- package/dist/runtime/script.d.ts +39 -0
- package/dist/runtime/script.d.ts.map +1 -0
- package/dist/runtime/sixb.d.ts +2 -2
- package/dist/runtime/sixb.d.ts.map +1 -1
- package/dist/schedules/builders.d.ts.map +1 -1
- package/dist/schedules/index.js +3 -3
- package/dist/schedules/runtime.d.ts.map +1 -1
- package/dist/schedules/validation.d.ts +2 -0
- package/dist/schedules/validation.d.ts.map +1 -1
- package/dist/shares/internal.js +12 -12
- package/dist/storage/agents/errors.d.ts +1 -1
- package/dist/storage/agents/errors.d.ts.map +1 -1
- package/dist/storage/agents/in-memory.d.ts +26 -1
- package/dist/storage/agents/in-memory.d.ts.map +1 -1
- package/dist/storage/agents/index.d.ts +2 -2
- package/dist/storage/agents/index.d.ts.map +1 -1
- package/dist/storage/agents/provider.d.ts +12 -3
- package/dist/storage/agents/provider.d.ts.map +1 -1
- package/dist/storage/agents/provider.js +25 -5
- package/dist/storage/agents/provider.js.map +1 -1
- package/dist/storage/agents/types.d.ts +156 -1
- package/dist/storage/agents/types.d.ts.map +1 -1
- package/dist/storage/artifacts/in-memory.d.ts +7 -1
- package/dist/storage/artifacts/in-memory.d.ts.map +1 -1
- package/dist/storage/artifacts/index.d.ts +1 -1
- package/dist/storage/artifacts/index.d.ts.map +1 -1
- package/dist/storage/artifacts/record.d.ts +2 -1
- package/dist/storage/artifacts/record.d.ts.map +1 -1
- package/dist/storage/artifacts/types.d.ts +34 -0
- package/dist/storage/artifacts/types.d.ts.map +1 -1
- package/dist/storage/executions/provider.d.ts +3 -3
- package/dist/storage/executions/provider.d.ts.map +1 -1
- package/dist/storage/executions/provider.js +1 -1
- package/dist/storage/executions/types.d.ts +6 -0
- package/dist/storage/executions/types.d.ts.map +1 -1
- package/dist/storage/executions/validation.d.ts.map +1 -1
- package/dist/storage/file-upload-sessions/provider.js +3 -3
- package/dist/storage/index.d.ts +3 -3
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +18 -14
- package/dist/storage/index.js.map +1 -1
- package/dist/storage/internal.d.ts +1 -1
- package/dist/storage/internal.d.ts.map +1 -1
- package/dist/storage/internal.js +8 -6
- package/dist/storage/internal.js.map +2 -2
- package/dist/storage/ontology/in-memory/replacement-plans.d.ts +2 -0
- package/dist/storage/ontology/in-memory/replacement-plans.d.ts.map +1 -1
- package/dist/storage/ontology/provider.js +1 -1
- package/dist/storage/ontology/replacement-plans.d.ts +8 -0
- package/dist/storage/ontology/replacement-plans.d.ts.map +1 -1
- package/dist/storage/operation-scope.d.ts.map +1 -1
- package/dist/storage/operation-scope.js +1 -1
- package/dist/storage/workflow-runs/provider.js +3 -3
- package/dist/syncs/index.js +19 -19
- package/dist/testing/agent-execution.d.ts +3 -0
- package/dist/testing/agent-execution.d.ts.map +1 -1
- package/dist/testing/agent-inbox-storage-contract.d.ts +15 -0
- package/dist/testing/agent-inbox-storage-contract.d.ts.map +1 -0
- package/dist/testing/agent-storage-contract.d.ts.map +1 -1
- package/dist/testing/artifact-storage-contract.d.ts.map +1 -1
- package/dist/testing/execution-storage-contract.d.ts.map +1 -1
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +5524 -4555
- package/dist/testing/index.js.map +10 -9
- package/dist/testing/materializer-storage-contract.d.ts.map +1 -1
- package/dist/testing/queues-contract.d.ts.map +1 -1
- package/dist/webhooks/connections.d.ts +3 -4
- package/dist/webhooks/connections.d.ts.map +1 -1
- package/dist/webhooks/index.d.ts +1 -1
- package/dist/webhooks/index.d.ts.map +1 -1
- package/dist/webhooks/internal.js +16 -49
- package/dist/webhooks/internal.js.map +3 -3
- package/dist/webhooks/types.d.ts +2 -11
- package/dist/webhooks/types.d.ts.map +1 -1
- package/dist/workers/queued-run-dispatch.d.ts.map +1 -1
- package/dist/workflows/index.js +35 -35
- package/package.json +6 -1
- package/src/agents/README.md +6 -2
- package/src/agents/approval-outcome.ts +2 -2
- package/src/agents/cancel.ts +2 -2
- package/src/agents/child-question.ts +76 -8
- package/src/agents/execution.ts +168 -30
- package/src/agents/ids.ts +9 -1
- package/src/agents/index.ts +27 -6
- package/src/agents/model-loop.ts +3 -1
- package/src/agents/queued-turn.ts +46 -0
- package/src/agents/request.ts +517 -2
- package/src/agents/run-after-finish.ts +259 -2
- package/src/agents/run-artifacts.ts +3 -93
- package/src/agents/run-events.ts +26 -1
- package/src/agents/run-session-children.ts +33 -0
- package/src/agents/run-session-http.ts +10 -2
- package/src/agents/run-session-serve.ts +3 -8
- package/src/agents/run-session.ts +142 -3
- package/src/agents/run-turn.ts +8 -0
- package/src/agents/streams/index.ts +1 -0
- package/src/agents/streams/protocol.ts +41 -1
- package/src/agents/tool-files.ts +1 -1
- package/src/agents/tool-input-coercion.ts +140 -0
- package/src/agents/turn-answered.ts +31 -0
- package/src/agents/validation.ts +13 -3
- package/src/artifacts/canvas-display.ts +4 -6
- package/src/artifacts/comments.ts +70 -17
- package/src/artifacts/document-fill.ts +103 -0
- package/src/artifacts/errors.ts +2 -0
- package/src/artifacts/events.ts +4 -1
- package/src/artifacts/execution.ts +265 -31
- package/src/artifacts/frames/assets.ts +42 -7
- package/src/artifacts/frames/document-data.ts +353 -0
- package/src/artifacts/frames/entries.ts +22 -16
- package/src/artifacts/frames/grants.ts +2 -2
- package/src/artifacts/frames/index.ts +3 -0
- package/src/artifacts/frames/kit.ts +102 -0
- package/src/artifacts/frames/protocol.ts +96 -3
- package/src/artifacts/frames/runtime.ts +709 -19
- package/src/artifacts/index.ts +6 -0
- package/src/artifacts/live/blocks.ts +889 -0
- package/src/artifacts/live/board.ts +195 -42
- package/src/artifacts/live/document.ts +170 -5
- package/src/artifacts/live/index.ts +7 -0
- package/src/artifacts/live/page.ts +807 -0
- package/src/artifacts/live/protocol.ts +5 -0
- package/src/artifacts/live/server.ts +2 -2
- package/src/artifacts/live/state.ts +9 -4
- package/src/artifacts/outline.ts +17 -0
- package/src/artifacts/paper.ts +173 -0
- package/src/artifacts/pdf-job.ts +11 -0
- package/src/artifacts/publish/fields.ts +177 -0
- package/src/artifacts/publish/index.ts +6 -1
- package/src/artifacts/publish/merge.ts +14 -12
- package/src/artifacts/publish/pdf.ts +82 -0
- package/src/artifacts/publish/previews.ts +24 -34
- package/src/artifacts/publish/publish.ts +26 -3
- package/src/artifacts/publish/queries.ts +20 -4
- package/src/artifacts/publish/query-answers.ts +14 -3
- package/src/artifacts/publish/validate/document-html.ts +245 -0
- package/src/artifacts/publish/validate/document.ts +34 -3
- package/src/artifacts/queries.ts +59 -13
- package/src/authorization/event-visibility.ts +13 -0
- package/src/connectors/connection-handles.ts +71 -0
- package/src/connectors/connections/authorizations.ts +50 -2
- package/src/connectors/connections/events.ts +57 -0
- package/src/connectors/connections/runs.ts +5 -0
- package/src/connectors/connections/service.ts +19 -1
- package/src/connectors/execution.ts +30 -2
- package/src/connectors/index.ts +3 -0
- package/src/connectors/types.ts +29 -1
- package/src/deploy/index.ts +1 -0
- package/src/deploy/services.ts +1 -0
- package/src/deploy/types.ts +26 -1
- package/src/events/definitions.ts +14 -0
- package/src/events/execution.ts +9 -2
- package/src/events/index.ts +8 -0
- package/src/events/outbox-dispatcher.ts +12 -2
- package/src/events/scope.ts +3 -0
- package/src/events/selectors/builder.ts +49 -0
- package/src/events/selectors/index.ts +5 -0
- package/src/events/selectors/predicate.ts +2 -0
- package/src/events/selectors/types.ts +30 -1
- package/src/events/types/artifacts.ts +7 -1
- package/src/events/types/connectors.ts +36 -0
- package/src/events/types/index.ts +7 -0
- package/src/execution/authorization.ts +45 -0
- package/src/execution/durable.ts +2 -0
- package/src/execution/scopes.ts +24 -0
- package/src/execution/script.ts +27 -0
- package/src/execution/types.ts +7 -1
- package/src/http/api-routes.ts +26 -1
- package/src/index.ts +21 -2
- package/src/lineage/executors.ts +3 -1
- package/src/materialization/event-envelopes.ts +10 -5
- package/src/materialization/events.ts +2 -0
- package/src/materialization/model.ts +6 -0
- package/src/materializer/README.md +10 -0
- package/src/materializer/projections/replace.ts +42 -1
- package/src/materializer/projections/replacement-plan.ts +44 -4
- package/src/materializer/shared/normalize.ts +4 -0
- package/src/models/language-model.ts +6 -0
- package/src/models/tools.ts +2 -0
- package/src/projections/builders.ts +20 -0
- package/src/projections/index.ts +1 -0
- package/src/projections/registry.ts +1 -0
- package/src/projections/revision.ts +2 -0
- package/src/projections/types.ts +11 -0
- package/src/projections/validation.ts +6 -0
- package/src/queues/in-memory.ts +5 -0
- package/src/queues/index.ts +3 -1
- package/src/queues/types.ts +45 -14
- package/src/runtime/host.ts +112 -4
- package/src/runtime/internal.ts +1 -0
- package/src/runtime/resolve-definitions.ts +4 -1
- package/src/runtime/script.ts +62 -0
- package/src/runtime/sixb.ts +3 -3
- package/src/schedules/builders.ts +4 -1
- package/src/schedules/runtime.ts +3 -0
- package/src/schedules/validation.ts +20 -2
- package/src/storage/agents/errors.ts +1 -0
- package/src/storage/agents/in-memory.ts +229 -1
- package/src/storage/agents/index.ts +16 -0
- package/src/storage/agents/provider.ts +284 -2
- package/src/storage/agents/types.ts +168 -1
- package/src/storage/artifacts/in-memory.ts +32 -1
- package/src/storage/artifacts/index.ts +2 -0
- package/src/storage/artifacts/record.ts +25 -2
- package/src/storage/artifacts/types.ts +37 -0
- package/src/storage/executions/provider.ts +22 -2
- package/src/storage/executions/types.ts +2 -0
- package/src/storage/executions/validation.ts +27 -0
- package/src/storage/index.ts +18 -0
- package/src/storage/internal.ts +1 -0
- package/src/storage/ontology/in-memory/replacement-plans.ts +22 -0
- package/src/storage/ontology/replacement-plans.ts +6 -0
- package/src/storage/operation-scope.ts +1 -0
- package/src/testing/agent-execution.ts +45 -2
- package/src/testing/agent-inbox-storage-contract.ts +815 -0
- package/src/testing/agent-storage-contract.ts +103 -0
- package/src/testing/artifact-storage-contract.ts +72 -4
- package/src/testing/execution-storage-contract.ts +31 -0
- package/src/testing/index.ts +5 -0
- package/src/testing/materializer-storage-contract.ts +139 -1
- package/src/testing/queues-contract.ts +22 -0
- package/src/webhooks/connections.ts +9 -50
- package/src/webhooks/index.ts +0 -2
- package/src/webhooks/types.ts +2 -13
- package/src/workers/queued-run-dispatch.ts +8 -1
- package/dist/chunk-48c1z8nj.js.map +0 -10
- package/dist/chunk-58geavnf.js +0 -29
- package/dist/chunk-58geavnf.js.map +0 -10
- package/dist/chunk-7z5rkf0m.js.map +0 -13
- package/dist/chunk-9nsjjd44.js.map +0 -12
- package/dist/chunk-b9qfh2ya.js.map +0 -11
- package/dist/chunk-eptrvnby.js.map +0 -12
- package/dist/chunk-f6ahf5gf.js.map +0 -13
- package/dist/chunk-m2jerfef.js +0 -474
- package/dist/chunk-m2jerfef.js.map +0 -11
- package/dist/chunk-n4zzaf6q.js.map +0 -10
- package/dist/chunk-qds484t8.js.map +0 -18
- package/dist/chunk-smnt672s.js.map +0 -10
- package/dist/chunk-vzkxqf8h.js.map +0 -23
- package/dist/chunk-w9s23r24.js.map +0 -30
- package/dist/chunk-wa05txmk.js.map +0 -11
- package/dist/chunk-yv8hv4wq.js.map +0 -10
- package/dist/chunk-z0t4p2sk.js.map +0 -47
- /package/dist/{chunk-tqa68kj7.js.map → chunk-0hkdtwqk.js.map} +0 -0
- /package/dist/{chunk-s5n9617r.js.map → chunk-0mvjpgvm.js.map} +0 -0
- /package/dist/{chunk-k66rp2hr.js.map → chunk-1yh6jgtz.js.map} +0 -0
- /package/dist/{chunk-26qet7wh.js.map → chunk-5t9gm5tt.js.map} +0 -0
- /package/dist/{chunk-e0hnbb1m.js.map → chunk-61zptves.js.map} +0 -0
- /package/dist/{chunk-hkaa807g.js.map → chunk-68hm50qh.js.map} +0 -0
- /package/dist/{chunk-fe6r7yhg.js.map → chunk-92wrka15.js.map} +0 -0
- /package/dist/{chunk-m7b9q5dk.js.map → chunk-aw1z0gre.js.map} +0 -0
- /package/dist/{chunk-kv0kvknv.js.map → chunk-fzae1jpq.js.map} +0 -0
- /package/dist/{chunk-6gtxshcc.js.map → chunk-k29n4176.js.map} +0 -0
- /package/dist/{chunk-sysnvjce.js.map → chunk-naxjka9v.js.map} +0 -0
- /package/dist/{chunk-qafng88v.js.map → chunk-pqvvqc65.js.map} +0 -0
- /package/dist/{chunk-8p7bze4c.js.map → chunk-s1mqyqmm.js.map} +0 -0
- /package/dist/{chunk-p351afzm.js.map → chunk-s5pj68fa.js.map} +0 -0
- /package/dist/{chunk-855w9nvw.js.map → chunk-vehcr9eb.js.map} +0 -0
- /package/dist/{chunk-n68vz95m.js.map → chunk-wqe9shx2.js.map} +0 -0
- /package/dist/{chunk-0k3r5kb3.js.map → chunk-xcrnrpfh.js.map} +0 -0
- /package/dist/{chunk-38yvdqzd.js.map → chunk-zxhym8tr.js.map} +0 -0
|
@@ -0,0 +1,889 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Blockquote,
|
|
3
|
+
Code,
|
|
4
|
+
Heading,
|
|
5
|
+
Image,
|
|
6
|
+
List,
|
|
7
|
+
ListItem,
|
|
8
|
+
Nodes,
|
|
9
|
+
Paragraph,
|
|
10
|
+
PhrasingContent,
|
|
11
|
+
Root,
|
|
12
|
+
RootContent,
|
|
13
|
+
Table,
|
|
14
|
+
TableCell,
|
|
15
|
+
} from "mdast"
|
|
16
|
+
import { fromMarkdown } from "mdast-util-from-markdown"
|
|
17
|
+
import { gfmFromMarkdown, gfmToMarkdown } from "mdast-util-gfm"
|
|
18
|
+
import { toMarkdown } from "mdast-util-to-markdown"
|
|
19
|
+
import { gfm } from "micromark-extension-gfm"
|
|
20
|
+
import * as Y from "yjs"
|
|
21
|
+
import { diffSequences, interleave, mergeSequences, mergeText } from "./diff"
|
|
22
|
+
|
|
23
|
+
// A Markdown document as blocks, which its editor edits: the nodes and marks ProseMirror holds,
|
|
24
|
+
// named as the app's TipTap editor names them, so the live document's `Y.XmlFragment` is what the
|
|
25
|
+
// editor's collaboration reads and writes.
|
|
26
|
+
//
|
|
27
|
+
// doc ─ paragraph · heading{level} · blockquote · callout{kind} · bulletList · orderedList{start}
|
|
28
|
+
// · taskList · listItem · taskItem{checked} · codeBlock{language, meta} · horizontalRule
|
|
29
|
+
// · image{src, alt, title} · table · tableRow · tableHeader · tableCell{colspan, rowspan,
|
|
30
|
+
// align}
|
|
31
|
+
// · markdown{source}, any block written as it was
|
|
32
|
+
// inline ─ text · hardBreak · markdownInline{source}
|
|
33
|
+
// marks ─ bold · italic · strike · code · link{href, target, rel, class, title}
|
|
34
|
+
//
|
|
35
|
+
// A node is a `Y.XmlElement` named by its type, its attributes those of the node; text is a
|
|
36
|
+
// `Y.XmlText` whose formatting attributes are its marks, each named by its type with its
|
|
37
|
+
// attributes as value, as y-prosemirror encodes them; each with every attribute the editor's schema
|
|
38
|
+
// gives it, so what the server writes the editor leaves as it is. What it does not model, a footnote,
|
|
39
|
+
// a reference definition or raw HTML, is kept as its own Markdown, which it writes back verbatim.
|
|
40
|
+
|
|
41
|
+
/** A node as ProseMirror's JSON holds it. */
|
|
42
|
+
export interface BlockNode {
|
|
43
|
+
readonly type: string
|
|
44
|
+
readonly attrs?: Readonly<Record<string, unknown>>
|
|
45
|
+
readonly content?: readonly BlockNode[]
|
|
46
|
+
readonly text?: string
|
|
47
|
+
readonly marks?: readonly BlockMark[]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface BlockMark {
|
|
51
|
+
readonly type: string
|
|
52
|
+
readonly attrs?: Readonly<Record<string, unknown>>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A callout's marker, read as the document page reads it: words may follow it on its line. */
|
|
56
|
+
const CALLOUT = /^\s*\[!(note|tip|important|warning|caution)\][ \t]*\n?/i
|
|
57
|
+
|
|
58
|
+
/** Front matter, which Markdown has no node for: kept as it was written. */
|
|
59
|
+
const FRONT_MATTER = /^---[ \t]*\n[\s\S]*?\n(?:---|\.\.\.)[ \t]*(?:\n|$)/
|
|
60
|
+
|
|
61
|
+
/** The inline nodes the editor models: within a mark, anything else keeps the mark verbatim. */
|
|
62
|
+
const MODELLED_INLINE: ReadonlySet<string> = new Set([
|
|
63
|
+
"text",
|
|
64
|
+
"strong",
|
|
65
|
+
"emphasis",
|
|
66
|
+
"delete",
|
|
67
|
+
"inlineCode",
|
|
68
|
+
"link",
|
|
69
|
+
"break",
|
|
70
|
+
])
|
|
71
|
+
|
|
72
|
+
/** A line break in a table cell, where Markdown has no other. */
|
|
73
|
+
const CELL_BREAK = /^<br\s*\/?>$/i
|
|
74
|
+
|
|
75
|
+
const MARKS_OF: Readonly<Record<string, string>> = {
|
|
76
|
+
strong: "bold",
|
|
77
|
+
emphasis: "italic",
|
|
78
|
+
delete: "strike",
|
|
79
|
+
}
|
|
80
|
+
const PHRASING_OF: Readonly<Record<string, "strong" | "emphasis" | "delete">> = {
|
|
81
|
+
bold: "strong",
|
|
82
|
+
italic: "emphasis",
|
|
83
|
+
strike: "delete",
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ── Markdown → blocks ───────────────────────────────────────────────────
|
|
87
|
+
|
|
88
|
+
/** A document's Markdown as blocks. */
|
|
89
|
+
export function markdownToBlocks(markdown: string): BlockNode {
|
|
90
|
+
const frontMatter = FRONT_MATTER.exec(markdown)?.[0]
|
|
91
|
+
const body = frontMatter ? markdown.slice(frontMatter.length) : markdown
|
|
92
|
+
const tree = fromMarkdown(body, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] })
|
|
93
|
+
const source = (node: Nodes) =>
|
|
94
|
+
body.slice(node.position?.start.offset ?? 0, node.position?.end.offset ?? 0)
|
|
95
|
+
// Within a quote or a list, a block's source holds its container's markers on every line: it
|
|
96
|
+
// is kept as Markdown would write it on its own, so it can move out of the container.
|
|
97
|
+
const raw = (node: Nodes, nested = false): BlockNode => ({
|
|
98
|
+
type: "markdown",
|
|
99
|
+
attrs: {
|
|
100
|
+
source: !nested
|
|
101
|
+
? source(node)
|
|
102
|
+
: node.type === "html"
|
|
103
|
+
? node.value
|
|
104
|
+
: toMarkdown(node as Root, { extensions: [gfmToMarkdown()] }).trimEnd(),
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
const inlineRaw = (node: Nodes): BlockNode => ({
|
|
108
|
+
type: "markdownInline",
|
|
109
|
+
attrs: { source: source(node) },
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
const inline = (
|
|
113
|
+
nodes: readonly PhrasingContent[],
|
|
114
|
+
marks: readonly BlockMark[] = [],
|
|
115
|
+
inCell = false
|
|
116
|
+
): BlockNode[] =>
|
|
117
|
+
nodes.flatMap((node): BlockNode[] => {
|
|
118
|
+
switch (node.type) {
|
|
119
|
+
case "text":
|
|
120
|
+
return node.value ? [text(node.value, marks)] : []
|
|
121
|
+
case "strong":
|
|
122
|
+
case "emphasis":
|
|
123
|
+
case "delete":
|
|
124
|
+
// A mark around a picture or markup the editor does not model is kept whole, mark
|
|
125
|
+
// and all, or editing beside it would write the mark around the words alone.
|
|
126
|
+
if (!modelledThroughout(node)) return [inlineRaw(node)]
|
|
127
|
+
return inline(node.children, [...marks, { type: MARKS_OF[node.type] as string }], inCell)
|
|
128
|
+
case "inlineCode":
|
|
129
|
+
return [text(node.value, [...marks, { type: "code" }])]
|
|
130
|
+
case "link":
|
|
131
|
+
if (!modelledThroughout(node)) return [inlineRaw(node)]
|
|
132
|
+
return inline(node.children, [...marks, link(node.url, node.title ?? null)], inCell)
|
|
133
|
+
case "break":
|
|
134
|
+
return [{ type: "hardBreak" }]
|
|
135
|
+
case "html":
|
|
136
|
+
if (inCell && CELL_BREAK.test(node.value)) return [{ type: "hardBreak" }]
|
|
137
|
+
return [inlineRaw(node)]
|
|
138
|
+
default:
|
|
139
|
+
return [inlineRaw(node)]
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const paragraph = (node: Paragraph): BlockNode => {
|
|
144
|
+
const [only] = node.children
|
|
145
|
+
// An image alone in its paragraph is a figure; its title is its caption.
|
|
146
|
+
if (node.children.length === 1 && only?.type === "image") return image(only)
|
|
147
|
+
return block("paragraph", undefined, inline(node.children))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const list = (node: List, nested: boolean): BlockNode => {
|
|
151
|
+
const checked = (item: ListItem) => item.checked !== null && item.checked !== undefined
|
|
152
|
+
const task = node.children.some(checked)
|
|
153
|
+
// A list of to-dos and plain items, which the editor's lists cannot hold: kept as written.
|
|
154
|
+
if (task && !node.children.every(checked)) return raw(node, nested)
|
|
155
|
+
const items = node.children.map((item) => listItem(item, task))
|
|
156
|
+
if (task) return block("taskList", undefined, items)
|
|
157
|
+
if (node.ordered) return block("orderedList", { start: node.start ?? 1 }, items)
|
|
158
|
+
return block("bulletList", undefined, items)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const listItem = (node: ListItem, task: boolean): BlockNode => {
|
|
162
|
+
const children = blocks(node.children, true)
|
|
163
|
+
// An item opens with words: the editor's list items do.
|
|
164
|
+
const content = children[0]?.type === "paragraph" ? children : [block("paragraph"), ...children]
|
|
165
|
+
return task
|
|
166
|
+
? block("taskItem", { checked: node.checked === true }, content)
|
|
167
|
+
: block("listItem", undefined, content)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const blockquote = (node: Blockquote): BlockNode => {
|
|
171
|
+
const [first] = node.children
|
|
172
|
+
const marker = first?.type === "paragraph" ? first.children[0] : undefined
|
|
173
|
+
const match = marker?.type === "text" ? CALLOUT.exec(marker.value) : null
|
|
174
|
+
if (!match || first?.type !== "paragraph" || marker?.type !== "text") {
|
|
175
|
+
return block("blockquote", undefined, nonEmpty(blocks(node.children, true)))
|
|
176
|
+
}
|
|
177
|
+
const rest = marker.value.slice(match[0].length)
|
|
178
|
+
const opening: Paragraph = {
|
|
179
|
+
...first,
|
|
180
|
+
children: [...(rest ? [{ ...marker, value: rest }] : []), ...first.children.slice(1)],
|
|
181
|
+
}
|
|
182
|
+
const content = [
|
|
183
|
+
...(opening.children.length > 0 ? [paragraph(opening)] : []),
|
|
184
|
+
...blocks(node.children.slice(1), true),
|
|
185
|
+
]
|
|
186
|
+
return block("callout", { kind: (match[1] as string).toLowerCase() }, nonEmpty(content))
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const table = (node: Table): BlockNode =>
|
|
190
|
+
block(
|
|
191
|
+
"table",
|
|
192
|
+
undefined,
|
|
193
|
+
node.children.map((row, index) =>
|
|
194
|
+
block(
|
|
195
|
+
"tableRow",
|
|
196
|
+
undefined,
|
|
197
|
+
row.children.map((cell: TableCell, column) =>
|
|
198
|
+
block(
|
|
199
|
+
index === 0 ? "tableHeader" : "tableCell",
|
|
200
|
+
{ colspan: 1, rowspan: 1, align: node.align?.[column] ?? null },
|
|
201
|
+
[block("paragraph", undefined, inline(cell.children, [], true))]
|
|
202
|
+
)
|
|
203
|
+
)
|
|
204
|
+
)
|
|
205
|
+
)
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
const code = (node: Code): BlockNode =>
|
|
209
|
+
block(
|
|
210
|
+
"codeBlock",
|
|
211
|
+
{ language: node.lang ?? null, meta: node.meta ?? null },
|
|
212
|
+
node.value ? [text(node.value)] : []
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
const blocks = (nodes: readonly RootContent[], nested = false): BlockNode[] =>
|
|
216
|
+
nodes.flatMap((node): BlockNode[] => {
|
|
217
|
+
switch (node.type) {
|
|
218
|
+
case "paragraph":
|
|
219
|
+
return [paragraph(node)]
|
|
220
|
+
case "heading":
|
|
221
|
+
return [block("heading", { level: node.depth }, inline(node.children))]
|
|
222
|
+
case "thematicBreak":
|
|
223
|
+
return [{ type: "horizontalRule" }]
|
|
224
|
+
case "blockquote":
|
|
225
|
+
return [blockquote(node)]
|
|
226
|
+
case "list":
|
|
227
|
+
return [list(node, nested)]
|
|
228
|
+
case "code":
|
|
229
|
+
return [code(node)]
|
|
230
|
+
case "table":
|
|
231
|
+
return [table(node)]
|
|
232
|
+
default:
|
|
233
|
+
return [raw(node, nested)]
|
|
234
|
+
}
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
const content = blocks((tree as Root).children)
|
|
238
|
+
if (frontMatter) content.unshift(block("markdown", { source: frontMatter.trimEnd() }))
|
|
239
|
+
return block("doc", undefined, nonEmpty(content))
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Whether every inline node within a mark is one the editor models. */
|
|
243
|
+
function modelledThroughout(node: PhrasingContent): boolean {
|
|
244
|
+
if (!MODELLED_INLINE.has(node.type)) return false
|
|
245
|
+
return !("children" in node) || node.children.every(modelledThroughout)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* A link as the editor marks one: every attribute its schema gives a link, as a mark keeps them,
|
|
250
|
+
* nulls included, so a link the server wrote is one the editor leaves as it is.
|
|
251
|
+
*/
|
|
252
|
+
function link(href: string, title: string | null): BlockMark {
|
|
253
|
+
return { type: "link", attrs: { href, target: null, rel: null, class: null, title } }
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function image(node: Image): BlockNode {
|
|
257
|
+
return {
|
|
258
|
+
type: "image",
|
|
259
|
+
attrs: { src: node.url, alt: node.alt ?? null, title: node.title ?? null },
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function text(value: string, marks: readonly BlockMark[] = []): BlockNode {
|
|
264
|
+
return marks.length > 0 ? { type: "text", text: value, marks } : { type: "text", text: value }
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function block(
|
|
268
|
+
type: string,
|
|
269
|
+
attrs?: Record<string, unknown>,
|
|
270
|
+
content: readonly BlockNode[] = []
|
|
271
|
+
): BlockNode {
|
|
272
|
+
return {
|
|
273
|
+
type,
|
|
274
|
+
...(attrs ? { attrs } : {}),
|
|
275
|
+
...(content.length > 0 ? { content } : {}),
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Content that must hold a block: an empty paragraph when it holds none. */
|
|
280
|
+
function nonEmpty(content: readonly BlockNode[]): BlockNode[] {
|
|
281
|
+
return content.length > 0 ? [...content] : [block("paragraph")]
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// ── Blocks → Markdown ───────────────────────────────────────────────────
|
|
285
|
+
|
|
286
|
+
/** Blocks written as a document's Markdown. */
|
|
287
|
+
export function blocksToMarkdown(doc: BlockNode): string {
|
|
288
|
+
const sources: string[] = []
|
|
289
|
+
// What is kept verbatim goes out as a placeholder, put back once the rest is written: letters
|
|
290
|
+
// and digits, which Markdown never escapes, unlike any word the document holds.
|
|
291
|
+
const nonce = placeholderNonce(doc)
|
|
292
|
+
const verbatim = (source: string) => {
|
|
293
|
+
sources.push(source)
|
|
294
|
+
return `${nonce}${sources.length - 1}x`
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const phrasing = (nodes: readonly BlockNode[] = [], inCell = false): PhrasingContent[] => {
|
|
298
|
+
const out: PhrasingContent[] = []
|
|
299
|
+
for (const node of nodes) {
|
|
300
|
+
if (node.type === "hardBreak") {
|
|
301
|
+
// A cell is one line: its breaks are written as HTML, as GFM tables write them.
|
|
302
|
+
out.push(inCell ? { type: "html", value: "<br>" } : { type: "break" })
|
|
303
|
+
continue
|
|
304
|
+
}
|
|
305
|
+
if (node.type === "markdownInline") {
|
|
306
|
+
out.push({ type: "html", value: verbatim(String(node.attrs?.source ?? "")) })
|
|
307
|
+
continue
|
|
308
|
+
}
|
|
309
|
+
if (node.type !== "text" || !node.text) continue
|
|
310
|
+
const marks = node.marks ?? []
|
|
311
|
+
let leaf: PhrasingContent = marks.some((mark) => mark.type === "code")
|
|
312
|
+
? { type: "inlineCode", value: node.text }
|
|
313
|
+
: { type: "text", value: node.text }
|
|
314
|
+
// Marks wrap from the innermost, the order ProseMirror keeps them in reversed.
|
|
315
|
+
for (const mark of [...marks].reverse()) {
|
|
316
|
+
if (mark.type === "link") {
|
|
317
|
+
leaf = {
|
|
318
|
+
type: "link",
|
|
319
|
+
url: String(mark.attrs?.href ?? ""),
|
|
320
|
+
title: typeof mark.attrs?.title === "string" ? mark.attrs.title : null,
|
|
321
|
+
children: [leaf],
|
|
322
|
+
}
|
|
323
|
+
} else if (PHRASING_OF[mark.type]) {
|
|
324
|
+
leaf = { type: PHRASING_OF[mark.type] as "strong", children: [leaf] }
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
out.push(leaf)
|
|
328
|
+
}
|
|
329
|
+
return joinPhrasing(out)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const flow = (nodes: readonly BlockNode[] = []): RootContent[] =>
|
|
333
|
+
nodes.flatMap((node): RootContent[] => {
|
|
334
|
+
switch (node.type) {
|
|
335
|
+
case "paragraph":
|
|
336
|
+
return [{ type: "paragraph", children: phrasing(node.content) }]
|
|
337
|
+
case "heading":
|
|
338
|
+
return [
|
|
339
|
+
{
|
|
340
|
+
type: "heading",
|
|
341
|
+
depth: clampLevel(node.attrs?.level),
|
|
342
|
+
children: phrasing(node.content),
|
|
343
|
+
} as Heading,
|
|
344
|
+
]
|
|
345
|
+
case "horizontalRule":
|
|
346
|
+
return [{ type: "thematicBreak" }]
|
|
347
|
+
case "blockquote":
|
|
348
|
+
return [{ type: "blockquote", children: flow(node.content) as Blockquote["children"] }]
|
|
349
|
+
case "callout": {
|
|
350
|
+
const kind = String(node.attrs?.kind ?? "note").toUpperCase()
|
|
351
|
+
const [first, ...rest] = flow(node.content)
|
|
352
|
+
const marker: PhrasingContent = { type: "html", value: verbatim(`[!${kind}]`) }
|
|
353
|
+
const opening: Paragraph =
|
|
354
|
+
first?.type === "paragraph"
|
|
355
|
+
? {
|
|
356
|
+
type: "paragraph",
|
|
357
|
+
children: [marker, { type: "text", value: "\n" }, ...first.children],
|
|
358
|
+
}
|
|
359
|
+
: { type: "paragraph", children: [marker] }
|
|
360
|
+
const children = first?.type === "paragraph" ? rest : first ? [first, ...rest] : rest
|
|
361
|
+
return [
|
|
362
|
+
{ type: "blockquote", children: [opening, ...children] as Blockquote["children"] },
|
|
363
|
+
]
|
|
364
|
+
}
|
|
365
|
+
case "bulletList":
|
|
366
|
+
case "orderedList":
|
|
367
|
+
case "taskList":
|
|
368
|
+
return [
|
|
369
|
+
{
|
|
370
|
+
type: "list",
|
|
371
|
+
ordered: node.type === "orderedList",
|
|
372
|
+
...(node.type === "orderedList" ? { start: Number(node.attrs?.start ?? 1) } : {}),
|
|
373
|
+
spread: false,
|
|
374
|
+
children: (node.content ?? []).map(
|
|
375
|
+
(item): ListItem => ({
|
|
376
|
+
type: "listItem",
|
|
377
|
+
// Markdown kept as written, a definition or a footnote, needs a blank line
|
|
378
|
+
// before it, or it reads as the words before it.
|
|
379
|
+
spread: (item.content ?? []).some(
|
|
380
|
+
(child, index) => index > 0 && child.type === "markdown"
|
|
381
|
+
),
|
|
382
|
+
checked: node.type === "taskList" ? item.attrs?.checked === true : null,
|
|
383
|
+
children: flow(item.content) as ListItem["children"],
|
|
384
|
+
})
|
|
385
|
+
),
|
|
386
|
+
},
|
|
387
|
+
]
|
|
388
|
+
case "codeBlock":
|
|
389
|
+
return [
|
|
390
|
+
{
|
|
391
|
+
type: "code",
|
|
392
|
+
lang: typeof node.attrs?.language === "string" ? node.attrs.language : null,
|
|
393
|
+
meta: typeof node.attrs?.meta === "string" ? node.attrs.meta : null,
|
|
394
|
+
value: (node.content ?? []).map((each) => each.text ?? "").join(""),
|
|
395
|
+
},
|
|
396
|
+
]
|
|
397
|
+
case "image":
|
|
398
|
+
return [
|
|
399
|
+
{
|
|
400
|
+
type: "paragraph",
|
|
401
|
+
children: [
|
|
402
|
+
{
|
|
403
|
+
type: "image",
|
|
404
|
+
url: String(node.attrs?.src ?? ""),
|
|
405
|
+
alt: typeof node.attrs?.alt === "string" ? node.attrs.alt : null,
|
|
406
|
+
title: typeof node.attrs?.title === "string" ? node.attrs.title : null,
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
},
|
|
410
|
+
]
|
|
411
|
+
case "table": {
|
|
412
|
+
const rows = node.content ?? []
|
|
413
|
+
return [
|
|
414
|
+
{
|
|
415
|
+
type: "table",
|
|
416
|
+
align: (rows[0]?.content ?? []).map((cell) => alignOf(cell.attrs?.align)),
|
|
417
|
+
children: rows.map((row) => ({
|
|
418
|
+
type: "tableRow",
|
|
419
|
+
children: (row.content ?? []).map((cell) => ({
|
|
420
|
+
type: "tableCell",
|
|
421
|
+
children: phrasing(
|
|
422
|
+
(cell.content ?? []).flatMap((each, index) => [
|
|
423
|
+
...(index > 0 ? [{ type: "hardBreak" }] : []),
|
|
424
|
+
...(each.content ?? []),
|
|
425
|
+
]),
|
|
426
|
+
true
|
|
427
|
+
),
|
|
428
|
+
})),
|
|
429
|
+
})),
|
|
430
|
+
} as Table,
|
|
431
|
+
]
|
|
432
|
+
}
|
|
433
|
+
case "markdown":
|
|
434
|
+
return [{ type: "html", value: verbatim(String(node.attrs?.source ?? "")) }]
|
|
435
|
+
default:
|
|
436
|
+
return []
|
|
437
|
+
}
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
const written = toMarkdown(
|
|
441
|
+
{ type: "root", children: flow(doc.content) },
|
|
442
|
+
{
|
|
443
|
+
extensions: [gfmToMarkdown({ tablePipeAlign: false })],
|
|
444
|
+
bullet: "-",
|
|
445
|
+
emphasis: "*",
|
|
446
|
+
strong: "*",
|
|
447
|
+
rule: "-",
|
|
448
|
+
fences: true,
|
|
449
|
+
listItemIndent: "one",
|
|
450
|
+
}
|
|
451
|
+
)
|
|
452
|
+
return written
|
|
453
|
+
.replace(TABLE_DELIMITER_ROW, (row) => row.replace(/(:?)-+(:?)/g, "$1---$2"))
|
|
454
|
+
.replace(new RegExp(`${nonce}(\\d+)x`, "g"), (_, index: string) => sources[Number(index)] ?? "")
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** Letters no text of `doc` holds, to mark where what is kept verbatim goes. */
|
|
458
|
+
function placeholderNonce(doc: BlockNode): string {
|
|
459
|
+
const words: string[] = []
|
|
460
|
+
const collect = (node: BlockNode) => {
|
|
461
|
+
if (node.text) words.push(node.text)
|
|
462
|
+
for (const value of Object.values(node.attrs ?? {})) {
|
|
463
|
+
if (typeof value === "string") words.push(value)
|
|
464
|
+
}
|
|
465
|
+
for (const child of node.content ?? []) collect(child)
|
|
466
|
+
}
|
|
467
|
+
collect(doc)
|
|
468
|
+
const all = words.join("\n")
|
|
469
|
+
let nonce = "sixbverbatim"
|
|
470
|
+
while (all.includes(nonce)) nonce += "q"
|
|
471
|
+
return nonce
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** A table's delimiter row, which is written `| --- |` as people write it, not `| - |`. */
|
|
475
|
+
const TABLE_DELIMITER_ROW = /^\|(?: *:?-+:? *\|)+$/gm
|
|
476
|
+
|
|
477
|
+
/** Adjacent runs of the same mark joined, as Markdown writes them: `**a b**`, not `**a** **b**`. */
|
|
478
|
+
function joinPhrasing(nodes: PhrasingContent[]): PhrasingContent[] {
|
|
479
|
+
const joined: PhrasingContent[] = []
|
|
480
|
+
for (const node of nodes) {
|
|
481
|
+
const last = joined.at(-1)
|
|
482
|
+
if (last && node.type === last.type && "children" in node && "children" in last) {
|
|
483
|
+
const sameLink =
|
|
484
|
+
node.type !== "link" ||
|
|
485
|
+
(last.type === "link" && node.url === last.url && node.title === last.title)
|
|
486
|
+
if (sameLink) {
|
|
487
|
+
;(last.children as PhrasingContent[]).push(...(node.children as PhrasingContent[]))
|
|
488
|
+
last.children = joinPhrasing(last.children as PhrasingContent[]) as typeof last.children
|
|
489
|
+
continue
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (last?.type === "text" && node.type === "text") {
|
|
493
|
+
last.value += node.value
|
|
494
|
+
continue
|
|
495
|
+
}
|
|
496
|
+
joined.push(node)
|
|
497
|
+
}
|
|
498
|
+
return joined
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function clampLevel(level: unknown): 1 | 2 | 3 | 4 | 5 | 6 {
|
|
502
|
+
const value = Number(level)
|
|
503
|
+
return (Number.isInteger(value) ? Math.min(6, Math.max(1, value)) : 1) as 1 | 2 | 3 | 4 | 5 | 6
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function alignOf(align: unknown): "left" | "right" | "center" | null {
|
|
507
|
+
return align === "left" || align === "right" || align === "center" ? align : null
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// ── Blocks ⇄ the live document ──────────────────────────────────────────
|
|
511
|
+
|
|
512
|
+
/** Blocks as a `Y.XmlFragment`'s children: each an element, its text with its marks. */
|
|
513
|
+
export function blocksToY(nodes: readonly BlockNode[]): (Y.XmlElement | Y.XmlText)[] {
|
|
514
|
+
const children: (Y.XmlElement | Y.XmlText)[] = []
|
|
515
|
+
let run: BlockNode[] = []
|
|
516
|
+
const flush = () => {
|
|
517
|
+
if (run.length === 0) return
|
|
518
|
+
const text = new Y.XmlText()
|
|
519
|
+
let at = 0
|
|
520
|
+
for (const node of run) {
|
|
521
|
+
const value = node.text ?? ""
|
|
522
|
+
text.insert(at, value, marksAttributes(node.marks))
|
|
523
|
+
at += value.length
|
|
524
|
+
}
|
|
525
|
+
children.push(text)
|
|
526
|
+
run = []
|
|
527
|
+
}
|
|
528
|
+
for (const node of nodes) {
|
|
529
|
+
if (node.type === "text") {
|
|
530
|
+
run.push(node)
|
|
531
|
+
continue
|
|
532
|
+
}
|
|
533
|
+
flush()
|
|
534
|
+
const element = new Y.XmlElement(node.type)
|
|
535
|
+
for (const [key, value] of Object.entries(node.attrs ?? {})) {
|
|
536
|
+
if (value !== null && value !== undefined) element.setAttribute(key, value as string)
|
|
537
|
+
}
|
|
538
|
+
const content = blocksToY(node.content ?? [])
|
|
539
|
+
if (content.length > 0) element.insert(0, content)
|
|
540
|
+
children.push(element)
|
|
541
|
+
}
|
|
542
|
+
flush()
|
|
543
|
+
return children
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function marksAttributes(marks: readonly BlockMark[] = []): Record<string, unknown> {
|
|
547
|
+
return Object.fromEntries(marks.map((mark) => [mark.type, { ...mark.attrs }]))
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** A fragment's children read back as blocks. */
|
|
551
|
+
export function blocksFromY(
|
|
552
|
+
children: readonly (Y.XmlElement | Y.XmlText | Y.XmlHook)[]
|
|
553
|
+
): BlockNode[] {
|
|
554
|
+
return children.flatMap((child): BlockNode[] => {
|
|
555
|
+
if (child instanceof Y.XmlText) {
|
|
556
|
+
return (child.toDelta() as { insert?: unknown; attributes?: Record<string, unknown> }[])
|
|
557
|
+
.filter((op) => typeof op.insert === "string" && op.insert !== "")
|
|
558
|
+
.map((op) => {
|
|
559
|
+
const marks = Object.entries(op.attributes ?? {}).map(([name, attrs]) => {
|
|
560
|
+
// y-prosemirror names a second mark of a type that allows several `type--hash`.
|
|
561
|
+
const type = name.split("--")[0] as string
|
|
562
|
+
const values =
|
|
563
|
+
attrs && typeof attrs === "object" ? (attrs as Record<string, unknown>) : {}
|
|
564
|
+
return Object.keys(values).length > 0 ? { type, attrs: values } : { type }
|
|
565
|
+
})
|
|
566
|
+
return text(op.insert as string, marks)
|
|
567
|
+
})
|
|
568
|
+
}
|
|
569
|
+
if (!(child instanceof Y.XmlElement)) return []
|
|
570
|
+
const attrs = child.getAttributes() as Record<string, unknown>
|
|
571
|
+
const content = blocksFromY(child.toArray())
|
|
572
|
+
return [
|
|
573
|
+
{
|
|
574
|
+
type: child.nodeName,
|
|
575
|
+
...(Object.keys(attrs).length > 0 ? { attrs } : {}),
|
|
576
|
+
...(content.length > 0 ? { content } : {}),
|
|
577
|
+
},
|
|
578
|
+
]
|
|
579
|
+
})
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/** A new fragment holding the blocks of `markdown`. */
|
|
583
|
+
export function markdownFragment(markdown: string): Y.XmlFragment {
|
|
584
|
+
const fragment = new Y.XmlFragment()
|
|
585
|
+
fragment.insert(0, blocksToY(markdownToBlocks(markdown).content ?? []))
|
|
586
|
+
return fragment
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/** The Markdown a fragment holds now. */
|
|
590
|
+
export function fragmentMarkdown(fragment: Y.XmlFragment): string {
|
|
591
|
+
return blocksToMarkdown({ type: "doc", content: blocksFromY(fragment.toArray()) })
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Makes a fragment hold the blocks of `markdown` by the fewest changes to its blocks, as
|
|
596
|
+
* `patchBlocks` makes them.
|
|
597
|
+
*/
|
|
598
|
+
export function patchFragment(fragment: Y.XmlFragment, markdown: string): void {
|
|
599
|
+
patchBlocks(fragment, markdownToBlocks(markdown).content ?? [])
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Makes a fragment or an element hold `next` by the fewest changes to its blocks: one that reads
|
|
604
|
+
* the same stays as it is, with whatever its editors' carets and history hold. A block replaced by
|
|
605
|
+
* one of the same type and attributes that holds blocks too is patched inside the same way, so a
|
|
606
|
+
* change to one paragraph of a section leaves the rest of the section as it was.
|
|
607
|
+
*/
|
|
608
|
+
export function patchBlocks(
|
|
609
|
+
parent: Y.XmlFragment,
|
|
610
|
+
next: readonly BlockNode[],
|
|
611
|
+
options: BlockComparison = {}
|
|
612
|
+
): void {
|
|
613
|
+
const current = blocksFromY(parent.toArray())
|
|
614
|
+
const key = options.key ?? ((node: BlockNode) => JSON.stringify(normalized(node)))
|
|
615
|
+
const shell = options.shell ?? ((node: BlockNode) => key({ ...node, content: [] }))
|
|
616
|
+
const hunks = diffSequences(current.map(key), next.map(key))
|
|
617
|
+
for (const hunk of hunks.reverse()) {
|
|
618
|
+
const removed = hunk.aEnd - hunk.aStart
|
|
619
|
+
const added = hunk.bEnd - hunk.bStart
|
|
620
|
+
const pairs = Array.from({ length: removed === added ? removed : 0 }, (_, at) => ({
|
|
621
|
+
element: parent.get(hunk.aStart + at),
|
|
622
|
+
before: current[hunk.aStart + at] as BlockNode,
|
|
623
|
+
after: next[hunk.bStart + at] as BlockNode,
|
|
624
|
+
}))
|
|
625
|
+
if (
|
|
626
|
+
pairs.length > 0 &&
|
|
627
|
+
pairs.every(
|
|
628
|
+
({ element, before, after }) =>
|
|
629
|
+
element instanceof Y.XmlElement &&
|
|
630
|
+
holdsBlocks(before) &&
|
|
631
|
+
holdsBlocks(after) &&
|
|
632
|
+
shell(before) === shell(after)
|
|
633
|
+
)
|
|
634
|
+
) {
|
|
635
|
+
for (const { element, after } of pairs) {
|
|
636
|
+
patchBlocks(element as Y.XmlElement, after.content ?? [], options)
|
|
637
|
+
}
|
|
638
|
+
continue
|
|
639
|
+
}
|
|
640
|
+
// A block of words changed in its words alone: the words are patched where they changed, so
|
|
641
|
+
// what someone is typing elsewhere in it stays.
|
|
642
|
+
if (
|
|
643
|
+
pairs.length > 0 &&
|
|
644
|
+
pairs.every(
|
|
645
|
+
({ element, before, after }) =>
|
|
646
|
+
element instanceof Y.XmlElement &&
|
|
647
|
+
holdsWordsAlone(element, before) &&
|
|
648
|
+
holdsWordsAlone(null, after) &&
|
|
649
|
+
shell(before) === shell(after)
|
|
650
|
+
)
|
|
651
|
+
) {
|
|
652
|
+
for (const { element, after } of pairs) patchWords(element as Y.XmlElement, after)
|
|
653
|
+
continue
|
|
654
|
+
}
|
|
655
|
+
if (removed > 0) parent.delete(hunk.aStart, removed)
|
|
656
|
+
if (added > 0) parent.insert(hunk.aStart, blocksToY(next.slice(hunk.bStart, hunk.bEnd)))
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Whether a block holds words alone, with their marks: no line break, nothing kept verbatim. In
|
|
662
|
+
* the live document, its element holds one text at most.
|
|
663
|
+
*/
|
|
664
|
+
function holdsWordsAlone(element: Y.XmlElement | null, node: BlockNode): boolean {
|
|
665
|
+
if (!node.content?.every((each) => each.type === "text")) {
|
|
666
|
+
return node.content === undefined && (element === null || element.length <= 1)
|
|
667
|
+
}
|
|
668
|
+
if (element === null) return true
|
|
669
|
+
const children = element.toArray()
|
|
670
|
+
return children.length === 1 && children[0] instanceof Y.XmlText
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Makes an element holding words alone hold `next`'s, by replacing what lies between the words
|
|
675
|
+
* they start and end with alike, marks included, and takes `next`'s attributes.
|
|
676
|
+
*/
|
|
677
|
+
function patchWords(element: Y.XmlElement, next: BlockNode): void {
|
|
678
|
+
const attrs = Object.fromEntries(
|
|
679
|
+
Object.entries(next.attrs ?? {}).filter(([, value]) => value !== null && value !== undefined)
|
|
680
|
+
)
|
|
681
|
+
for (const [name, value] of Object.entries(element.getAttributes())) {
|
|
682
|
+
if (!(name in attrs)) element.removeAttribute(name)
|
|
683
|
+
else if (value !== attrs[name]) element.setAttribute(name, attrs[name] as string)
|
|
684
|
+
}
|
|
685
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
686
|
+
if (!element.hasAttribute(name)) element.setAttribute(name, value as string)
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
const existing = element.get(0)
|
|
690
|
+
if (!(existing instanceof Y.XmlText)) {
|
|
691
|
+
const fresh = blocksToY(next.content ?? [])
|
|
692
|
+
if (fresh.length > 0) element.insert(0, fresh)
|
|
693
|
+
return
|
|
694
|
+
}
|
|
695
|
+
const characters = (nodes: readonly BlockNode[]) =>
|
|
696
|
+
nodes.flatMap((node) => {
|
|
697
|
+
const marks = JSON.stringify(marksAttributes(node.marks))
|
|
698
|
+
return [...(node.text ?? "")].map((character) => ({ character, marks, node }))
|
|
699
|
+
})
|
|
700
|
+
const before = characters(blocksFromY([existing]))
|
|
701
|
+
const after = characters(next.content ?? [])
|
|
702
|
+
const same = (left: (typeof before)[number], right: (typeof after)[number]) =>
|
|
703
|
+
left.character === right.character && left.marks === right.marks
|
|
704
|
+
let start = 0
|
|
705
|
+
while (start < before.length && start < after.length && same(before[start], after[start]))
|
|
706
|
+
start += 1
|
|
707
|
+
let end = 0
|
|
708
|
+
while (
|
|
709
|
+
end < before.length - start &&
|
|
710
|
+
end < after.length - start &&
|
|
711
|
+
same(before[before.length - 1 - end], after[after.length - 1 - end])
|
|
712
|
+
)
|
|
713
|
+
end += 1
|
|
714
|
+
|
|
715
|
+
// Yjs counts UTF-16 code units, as JavaScript strings do.
|
|
716
|
+
const units = (run: readonly { character: string }[]) =>
|
|
717
|
+
run.reduce((sum, each) => sum + each.character.length, 0)
|
|
718
|
+
const from = units(before.slice(0, start))
|
|
719
|
+
const removed = units(before.slice(start, before.length - end))
|
|
720
|
+
if (removed > 0) existing.delete(from, removed)
|
|
721
|
+
let at = from
|
|
722
|
+
const inserted = after.slice(start, after.length - end)
|
|
723
|
+
for (let index = 0; index < inserted.length; ) {
|
|
724
|
+
const marks = inserted[index]?.marks
|
|
725
|
+
let run = ""
|
|
726
|
+
let node = inserted[index]?.node as BlockNode
|
|
727
|
+
while (index < inserted.length && inserted[index]?.marks === marks) {
|
|
728
|
+
run += inserted[index]?.character
|
|
729
|
+
node = inserted[index]?.node as BlockNode
|
|
730
|
+
index += 1
|
|
731
|
+
}
|
|
732
|
+
existing.insert(at, run, marksAttributes(node.marks))
|
|
733
|
+
at += run.length
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/** Whether a node holds blocks alone, each its own element of the live document. */
|
|
738
|
+
function holdsBlocks(node: BlockNode): boolean {
|
|
739
|
+
return (
|
|
740
|
+
(node.content?.length ?? 0) > 0 && node.content?.every((each) => each.type !== "text") === true
|
|
741
|
+
)
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Folds two changes of a list of blocks, `ours` and `theirs` of `base`, block by block: a block
|
|
746
|
+
* only one side changed is taken from it; one both changed is merged inside, a block of blocks by
|
|
747
|
+
* its blocks and anything else by `mergeLeaf`; what both added is kept, ours first, and what
|
|
748
|
+
* either took out goes. A block both hold is taken as `theirs` holds it.
|
|
749
|
+
*/
|
|
750
|
+
export function mergeBlockLists(
|
|
751
|
+
base: readonly BlockNode[],
|
|
752
|
+
ours: readonly BlockNode[],
|
|
753
|
+
theirs: readonly BlockNode[],
|
|
754
|
+
comparison: Required<BlockComparison> & {
|
|
755
|
+
/** One block both changed, merged inside; null to keep both sides' as two. */
|
|
756
|
+
readonly mergeLeaf: (base: BlockNode, ours: BlockNode, theirs: BlockNode) => BlockNode | null
|
|
757
|
+
}
|
|
758
|
+
): BlockNode[] {
|
|
759
|
+
const keys = new WeakMap<BlockNode, string>()
|
|
760
|
+
const key = (node: BlockNode) => {
|
|
761
|
+
let known = keys.get(node)
|
|
762
|
+
if (known === undefined) {
|
|
763
|
+
known = comparison.key(node)
|
|
764
|
+
keys.set(node, known)
|
|
765
|
+
}
|
|
766
|
+
return known
|
|
767
|
+
}
|
|
768
|
+
const merge = (
|
|
769
|
+
base: readonly BlockNode[],
|
|
770
|
+
ours: readonly BlockNode[],
|
|
771
|
+
theirs: readonly BlockNode[]
|
|
772
|
+
): BlockNode[] => {
|
|
773
|
+
const baseKeys = base.map(key)
|
|
774
|
+
const oursKeys = ours.map(key)
|
|
775
|
+
const theirsKeys = theirs.map(key)
|
|
776
|
+
if (sameKeys(oursKeys, baseKeys)) return [...theirs]
|
|
777
|
+
if (sameKeys(theirsKeys, baseKeys) || sameKeys(oursKeys, theirsKeys)) return [...ours]
|
|
778
|
+
|
|
779
|
+
// Each key's blocks, theirs first; the first of each side's, for blocks both changed.
|
|
780
|
+
const byKey = new Map<string, BlockNode[]>()
|
|
781
|
+
for (const node of [...theirs, ...ours, ...base]) {
|
|
782
|
+
const list = byKey.get(key(node))
|
|
783
|
+
if (list) list.push(node)
|
|
784
|
+
else byKey.set(key(node), [node])
|
|
785
|
+
}
|
|
786
|
+
const firstOf = (list: readonly BlockNode[]) => {
|
|
787
|
+
const map = new Map<string, BlockNode>()
|
|
788
|
+
for (const node of list) if (!map.has(key(node))) map.set(key(node), node)
|
|
789
|
+
return map
|
|
790
|
+
}
|
|
791
|
+
const [baseOf, oursOf, theirsOf] = [firstOf(base), firstOf(ours), firstOf(theirs)]
|
|
792
|
+
const one = (b: BlockNode, o: BlockNode, t: BlockNode): BlockNode | null => {
|
|
793
|
+
const shell = comparison.shell(t)
|
|
794
|
+
if (comparison.shell(b) !== shell || comparison.shell(o) !== shell) return null
|
|
795
|
+
if (holdsBlocks(b) && holdsBlocks(o) && holdsBlocks(t)) {
|
|
796
|
+
return { ...t, content: merge(b.content ?? [], o.content ?? [], t.content ?? []) }
|
|
797
|
+
}
|
|
798
|
+
return comparison.mergeLeaf(b, o, t)
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
const merged = mergeSequences(baseKeys, oursKeys, theirsKeys, (b, o, t) => {
|
|
802
|
+
// The same blocks changed on both sides: each merged inside, while it is the same
|
|
803
|
+
// block on each.
|
|
804
|
+
if (b.length === o.length && o.length === t.length) {
|
|
805
|
+
const each = b.map((k, at) =>
|
|
806
|
+
one(
|
|
807
|
+
baseOf.get(k) as BlockNode,
|
|
808
|
+
oursOf.get(o[at] as string) as BlockNode,
|
|
809
|
+
theirsOf.get(t[at] as string) as BlockNode
|
|
810
|
+
)
|
|
811
|
+
)
|
|
812
|
+
if (each.every((node) => node !== null)) {
|
|
813
|
+
return each.map((node) => {
|
|
814
|
+
const k = key(node as BlockNode)
|
|
815
|
+
byKey.set(k, [node as BlockNode, ...(byKey.get(k) ?? [])])
|
|
816
|
+
return k
|
|
817
|
+
})
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
return interleave(b, o, t)
|
|
821
|
+
})
|
|
822
|
+
const taken = new Map<string, number>()
|
|
823
|
+
return merged.map((k) => {
|
|
824
|
+
const at = taken.get(k) ?? 0
|
|
825
|
+
taken.set(k, at + 1)
|
|
826
|
+
const nodes = byKey.get(k) ?? []
|
|
827
|
+
return (nodes[at] ?? nodes[0]) as BlockNode
|
|
828
|
+
})
|
|
829
|
+
}
|
|
830
|
+
return merge(base, ours, theirs)
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
function sameKeys(left: readonly string[], right: readonly string[]): boolean {
|
|
834
|
+
return left.length === right.length && left.every((key, at) => key === right[at])
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* Folds two changes of a Markdown document, `ours` the blocks its editors hold now, block by
|
|
839
|
+
* block: compared as blocks, how each side writes its Markdown is no change. A block both changed
|
|
840
|
+
* is merged by its Markdown, written alone.
|
|
841
|
+
*/
|
|
842
|
+
export function mergeMarkdownBlocks(
|
|
843
|
+
base: string,
|
|
844
|
+
ours: readonly BlockNode[],
|
|
845
|
+
theirs: string
|
|
846
|
+
): BlockNode[] {
|
|
847
|
+
const key = (node: BlockNode) => JSON.stringify(normalized(node))
|
|
848
|
+
return mergeBlockLists(
|
|
849
|
+
markdownToBlocks(base).content ?? [],
|
|
850
|
+
ours,
|
|
851
|
+
markdownToBlocks(theirs).content ?? [],
|
|
852
|
+
{
|
|
853
|
+
key,
|
|
854
|
+
shell: (node) => key({ ...node, content: [] }),
|
|
855
|
+
mergeLeaf: (b, o, t) => {
|
|
856
|
+
const written = (node: BlockNode) => blocksToMarkdown({ type: "doc", content: [node] })
|
|
857
|
+
const [merged, ...more] =
|
|
858
|
+
markdownToBlocks(mergeText(written(b), written(o), written(t))).content ?? []
|
|
859
|
+
if (!merged || more.length > 0 || merged.type !== t.type) return null
|
|
860
|
+
return merged
|
|
861
|
+
},
|
|
862
|
+
}
|
|
863
|
+
)
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* How `patchBlocks` tells blocks apart: `key` is the same for two blocks that are the same, and
|
|
868
|
+
* `shell` for two that are the same but for what they hold. By default, by their nodes.
|
|
869
|
+
*/
|
|
870
|
+
export interface BlockComparison {
|
|
871
|
+
readonly key?: (node: BlockNode) => string
|
|
872
|
+
readonly shell?: (node: BlockNode) => string
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/** A block without what reading it back adds or drops: null attributes, empty marks. */
|
|
876
|
+
function normalized(node: BlockNode): unknown {
|
|
877
|
+
const attrs = Object.fromEntries(
|
|
878
|
+
Object.entries(node.attrs ?? {}).filter(([, value]) => value !== null && value !== undefined)
|
|
879
|
+
)
|
|
880
|
+
return {
|
|
881
|
+
type: node.type,
|
|
882
|
+
...(Object.keys(attrs).length > 0 ? { attrs } : {}),
|
|
883
|
+
...(node.text !== undefined ? { text: node.text } : {}),
|
|
884
|
+
...(node.marks?.length
|
|
885
|
+
? { marks: node.marks.map((mark) => normalized(mark as BlockNode)) }
|
|
886
|
+
: {}),
|
|
887
|
+
...(node.content?.length ? { content: node.content.map(normalized) } : {}),
|
|
888
|
+
}
|
|
889
|
+
}
|