@johnhenry/mallory-cli 0.0.0-stage → 0.1.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 +60 -2
- package/bin/mallory.mjs +51 -0
- package/package.json +51 -4
- package/src/a2a/client.js +511 -0
- package/src/a2a/demo-agent.js +396 -0
- package/src/a2a/hub.js +578 -0
- package/src/a2a/settings.js +88 -0
- package/src/a2a/task.js +358 -0
- package/src/acp/client.js +573 -0
- package/src/acp/demo-agent.js +332 -0
- package/src/acp/hub.js +632 -0
- package/src/acp/settings.js +78 -0
- package/src/actions/map.js +524 -0
- package/src/agent/cloud.js +77 -0
- package/src/agent/compact.js +84 -0
- package/src/agent/constants.js +39 -0
- package/src/agent/diff.js +101 -0
- package/src/agent/fork.js +159 -0
- package/src/agent/grounding.js +73 -0
- package/src/agent/guards.js +129 -0
- package/src/agent/local-transport.js +152 -0
- package/src/agent/loop.js +479 -0
- package/src/agent/park-store.js +77 -0
- package/src/agent/park.js +76 -0
- package/src/agent/ports.js +28 -0
- package/src/agent/providers.js +591 -0
- package/src/agent/restart.js +51 -0
- package/src/agent/router.js +380 -0
- package/src/agent/run.js +861 -0
- package/src/agent/tools.js +247 -0
- package/src/agent-library/engine.js +43 -0
- package/src/ai/bridge.js +155 -0
- package/src/ai/policy.js +131 -0
- package/src/aimatey/vendor.js +18 -0
- package/src/audio/container-duration.js +35 -0
- package/src/audio/instruments.js +776 -0
- package/src/audio/lanes.js +230 -0
- package/src/audio/patch.js +1410 -0
- package/src/audio/plan.js +276 -0
- package/src/audio/rack.js +1006 -0
- package/src/audio/sample-trim.js +293 -0
- package/src/audio/soundfont.js +243 -0
- package/src/audio/take-store.js +87 -0
- package/src/audio/takes.js +129 -0
- package/src/audio/wav.js +170 -0
- package/src/audio/zzfx.js +333 -0
- package/src/autonomy/bundle.js +207 -0
- package/src/autonomy/confine.js +61 -0
- package/src/autonomy/enforce.js +155 -0
- package/src/autonomy/file-kv.js +88 -0
- package/src/autonomy/ledger.js +203 -0
- package/src/autonomy/pins.js +50 -0
- package/src/autonomy/psl/index.js +32 -0
- package/src/autonomy/psl/suffixes.json +1 -0
- package/src/autonomy/record.js +28 -0
- package/src/autonomy/routines.js +107 -0
- package/src/autonomy/server.js +175 -0
- package/src/autonomy/signer-node.js +167 -0
- package/src/autonomy/store.js +43 -0
- package/src/autonomy/unattended.js +13 -0
- package/src/bytes/bytes.js +378 -0
- package/src/calc/syntax.js +125 -0
- package/src/calc/tex.js +28 -0
- package/src/calendar/vendor.js +2 -0
- package/src/clipboard.js +130 -0
- package/src/clsh/clsh.js +3417 -0
- package/src/codec/capnp.js +1804 -0
- package/src/codec/core.js +2043 -0
- package/src/codec/vendor/avro.js +136 -0
- package/src/codec/vendor/cbor.js +25 -0
- package/src/codec/vendor/flatbuffers.js +206 -0
- package/src/codec/vendor/msgpack.js +9 -0
- package/src/codec/vendor/protobuf.js +53 -0
- package/src/delegation/caps.js +60 -0
- package/src/delegation/config.js +32 -0
- package/src/device/model.js +246 -0
- package/src/device/recording.js +198 -0
- package/src/device/stream-model.js +151 -0
- package/src/documents/documents.js +1224 -0
- package/src/effects/catalog.js +63 -0
- package/src/effects/extras.js +133 -0
- package/src/effects/parse.js +339 -0
- package/src/embed/protocol.js +131 -0
- package/src/env/env.js +159 -0
- package/src/exact/vendor.js +45 -0
- package/src/example-meta.js +164 -0
- package/src/examples.js +94 -0
- package/src/export/canvas-image.js +217 -0
- package/src/export/expr-latex-lazy.js +8 -0
- package/src/export/formats.js +322 -0
- package/src/export/latex.js +183 -0
- package/src/export/pane.js +184 -0
- package/src/export/value.js +452 -0
- package/src/export/zip.js +214 -0
- package/src/ext/access.js +58 -0
- package/src/ext/btasks.js +225 -0
- package/src/ext/client.js +209 -0
- package/src/ext/tools.js +69 -0
- package/src/ext/view.js +105 -0
- package/src/ext/webmcp-tabs.js +299 -0
- package/src/files/archive.js +232 -0
- package/src/files/files.js +317 -0
- package/src/formats/json.js +24 -0
- package/src/formats/jupyter.js +188 -0
- package/src/formats/markdown.js +254 -0
- package/src/formats/n8n.js +276 -0
- package/src/formats/node-red.js +363 -0
- package/src/formats/registry.js +177 -0
- package/src/formats/rivet.js +259 -0
- package/src/formats/shared.js +98 -0
- package/src/formats/structure.js +182 -0
- package/src/formats/yaml.js +127 -0
- package/src/frame/frame.js +962 -0
- package/src/frame/vendor.js +1484 -0
- package/src/gate/audit.js +116 -0
- package/src/gate/gate.js +365 -0
- package/src/gate/policy.js +295 -0
- package/src/gate/scripted.js +110 -0
- package/src/gate/seams.js +32 -0
- package/src/gate/store.js +66 -0
- package/src/geometry/model.js +533 -0
- package/src/grant-key.js +149 -0
- package/src/graph/model.js +381 -0
- package/src/headless/a2a-push.js +123 -0
- package/src/headless/channels/config.js +142 -0
- package/src/headless/channels/connectors.js +283 -0
- package/src/headless/channels/index.js +257 -0
- package/src/headless/channels/mail.js +197 -0
- package/src/headless/channels/mailnet.js +223 -0
- package/src/headless/channels/platforms.js +194 -0
- package/src/headless/channels/replay.js +49 -0
- package/src/headless/cli.js +1397 -0
- package/src/headless/companion-client.js +182 -0
- package/src/headless/cost-cli.js +177 -0
- package/src/headless/deprecations-cli.js +102 -0
- package/src/headless/examples.js +48 -0
- package/src/headless/ffmpeg-exec.js +217 -0
- package/src/headless/gateways/config.js +213 -0
- package/src/headless/gateways/homeassistant.js +159 -0
- package/src/headless/gateways/index.js +121 -0
- package/src/headless/gateways/matter.js +150 -0
- package/src/headless/gateways/mqtt-client.js +177 -0
- package/src/headless/gateways/mqtt.js +70 -0
- package/src/headless/gateways/netway.js +226 -0
- package/src/headless/gateways/serverpod-worker.mjs +16 -0
- package/src/headless/gateways/serverpod.js +71 -0
- package/src/headless/gateways/util.js +27 -0
- package/src/headless/grant-cli.js +208 -0
- package/src/headless/guard.js +125 -0
- package/src/headless/guards.js +124 -0
- package/src/headless/machine-cli.js +853 -0
- package/src/headless/mcp-endpoint.js +287 -0
- package/src/headless/mcp-roots.js +38 -0
- package/src/headless/model-endpoint.js +88 -0
- package/src/headless/net.js +151 -0
- package/src/headless/node.js +263 -0
- package/src/headless/npm-tarball.js +29 -0
- package/src/headless/operator-page.js +112 -0
- package/src/headless/operator.js +242 -0
- package/src/headless/people.js +34 -0
- package/src/headless/pkg-cli.js +1103 -0
- package/src/headless/pkg-serve.js +138 -0
- package/src/headless/process-worker.js +88 -0
- package/src/headless/runner-remote.js +477 -0
- package/src/headless/sandbox-child.mjs +21 -0
- package/src/headless/scheduled.js +557 -0
- package/src/headless/serve-stream.js +97 -0
- package/src/headless/serve.js +2238 -0
- package/src/headless/short-links.js +158 -0
- package/src/headless/static-dirs.js +207 -0
- package/src/headless/trust-file.js +51 -0
- package/src/headless/watch.js +291 -0
- package/src/headless/ws-connect.js +65 -0
- package/src/headless/ws-connections.js +44 -0
- package/src/headless/ws-echo.js +150 -0
- package/src/headless/ws-proxy.js +354 -0
- package/src/headless/ws-ticket.js +48 -0
- package/src/hooks/config.js +50 -0
- package/src/hooks/engine.js +189 -0
- package/src/input/log.js +613 -0
- package/src/input/player.js +217 -0
- package/src/input/replay.js +209 -0
- package/src/jth/vendor.js +28 -0
- package/src/ledger/tx.js +94 -0
- package/src/machine/apply.js +193 -0
- package/src/machine/diff.js +168 -0
- package/src/machine/drive.js +296 -0
- package/src/machine/engine.js +478 -0
- package/src/machine/files-tool.js +76 -0
- package/src/machine/lint.js +139 -0
- package/src/machine/llmtm-trace.js +117 -0
- package/src/machine/llmtm.js +475 -0
- package/src/machine/otel.js +80 -0
- package/src/machine/pane-provider.js +51 -0
- package/src/machine/propose.js +159 -0
- package/src/machine/random.js +52 -0
- package/src/machine/risk.js +107 -0
- package/src/machine/session.js +1017 -0
- package/src/machine/shell-tool.js +45 -0
- package/src/machine/tape.js +349 -0
- package/src/machine/templates.js +104 -0
- package/src/machine/trace.js +664 -0
- package/src/machine/view.js +73 -0
- package/src/machine/vm-tool.js +47 -0
- package/src/machine/watchdog.js +46 -0
- package/src/mcp/app-args.js +51 -0
- package/src/mcp/app-run.js +56 -0
- package/src/mcp/apps.js +468 -0
- package/src/mcp/cache.js +232 -0
- package/src/mcp/client.js +507 -0
- package/src/mcp/demo-app.js +183 -0
- package/src/mcp/demo-server.js +602 -0
- package/src/mcp/elicitation.js +116 -0
- package/src/mcp/hub.js +1326 -0
- package/src/mcp/roots.js +139 -0
- package/src/mcp/sampling.js +105 -0
- package/src/mcp/settings.js +125 -0
- package/src/mcp/tape.js +292 -0
- package/src/mcp/uri-template.js +156 -0
- package/src/mcp/wsh-client.js +164 -0
- package/src/media/animated-codec.js +7 -0
- package/src/media/animated.js +623 -0
- package/src/media/compare-agent.js +70 -0
- package/src/media/compare-job.js +63 -0
- package/src/media/compare-page.js +296 -0
- package/src/media/compare-worker.js +14 -0
- package/src/media/compare.js +600 -0
- package/src/media/contract.js +62 -0
- package/src/media/decode-frame.js +57 -0
- package/src/media/decode-pool.js +188 -0
- package/src/media/ffmpeg-edit.js +344 -0
- package/src/media/ffmpeg-wasm-client.js +121 -0
- package/src/media/ffmpeg-wasm-session.js +79 -0
- package/src/media/ffmpeg-wasm.js +139 -0
- package/src/media/ffmpeg-worker.js +23 -0
- package/src/media/ffmpeg.js +903 -0
- package/src/media/helpers.js +419 -0
- package/src/media/keep.js +108 -0
- package/src/media/metadata-worker.js +16 -0
- package/src/media/metadata.js +2518 -0
- package/src/media/mime.js +102 -0
- package/src/media/page-decode.js +74 -0
- package/src/media/persist.js +114 -0
- package/src/media/png.js +226 -0
- package/src/media/sample-input.js +67 -0
- package/src/media/store.js +86 -0
- package/src/media/wav.js +109 -0
- package/src/midi/mappings.js +250 -0
- package/src/midi/message.js +125 -0
- package/src/model/action-hub.js +143 -0
- package/src/model/adapters.js +284 -0
- package/src/model/agent-runs.js +12 -0
- package/src/model/arrange.js +308 -0
- package/src/model/automation-clock.js +494 -0
- package/src/model/automation.js +653 -0
- package/src/model/batches.js +78 -0
- package/src/model/binary-view.js +195 -0
- package/src/model/breakpoints.js +392 -0
- package/src/model/calc-settings.js +65 -0
- package/src/model/calendar.js +254 -0
- package/src/model/canvas-id.js +5 -0
- package/src/model/canvas.js +5048 -0
- package/src/model/channel-names.js +29 -0
- package/src/model/channels.js +692 -0
- package/src/model/chart-clock.js +631 -0
- package/src/model/chart-code.js +69 -0
- package/src/model/codecs.js +60 -0
- package/src/model/companion-access.js +271 -0
- package/src/model/companion-pane.js +50 -0
- package/src/model/composite.js +324 -0
- package/src/model/context.js +380 -0
- package/src/model/controls.js +231 -0
- package/src/model/converter-presets.js +368 -0
- package/src/model/cost.js +580 -0
- package/src/model/curve-transform.js +122 -0
- package/src/model/cycles.js +176 -0
- package/src/model/debug-log.js +168 -0
- package/src/model/decision-clock.js +100 -0
- package/src/model/decisions.js +265 -0
- package/src/model/desk.js +87 -0
- package/src/model/device-hub.js +400 -0
- package/src/model/diff.js +445 -0
- package/src/model/emit-events.js +179 -0
- package/src/model/errors.js +238 -0
- package/src/model/event-presets.js +297 -0
- package/src/model/events.js +1303 -0
- package/src/model/explicit-setters.js +139 -0
- package/src/model/field-mapping.js +623 -0
- package/src/model/files-pane.js +263 -0
- package/src/model/flow-idle.js +89 -0
- package/src/model/foreach.js +549 -0
- package/src/model/frame-loop.js +71 -0
- package/src/model/frame-pressure.js +180 -0
- package/src/model/function-scaffold.js +1000 -0
- package/src/model/geometry-settings.js +54 -0
- package/src/model/goals.js +176 -0
- package/src/model/graph-settings.js +76 -0
- package/src/model/grid-widget.js +175 -0
- package/src/model/groups.js +332 -0
- package/src/model/hash.js +21 -0
- package/src/model/history.js +259 -0
- package/src/model/http-copy.js +88 -0
- package/src/model/http-send-companion.js +110 -0
- package/src/model/http-send.js +163 -0
- package/src/model/http-tap.js +521 -0
- package/src/model/http-tools.js +1138 -0
- package/src/model/import-lock.js +78 -0
- package/src/model/imports.js +258 -0
- package/src/model/infer-outputs.js +822 -0
- package/src/model/ink-connect.js +150 -0
- package/src/model/ink-diff.js +75 -0
- package/src/model/ink-gone.js +71 -0
- package/src/model/ink-route.js +120 -0
- package/src/model/ink-svg.js +206 -0
- package/src/model/ink.js +739 -0
- package/src/model/input-log-hub.js +456 -0
- package/src/model/input-split.js +69 -0
- package/src/model/json-schema-host.js +9 -0
- package/src/model/json-schema.js +368 -0
- package/src/model/jth.js +78 -0
- package/src/model/latex-expr.js +528 -0
- package/src/model/layout-history.js +120 -0
- package/src/model/lazy-map.js +88 -0
- package/src/model/ledger-pane.js +101 -0
- package/src/model/legacy-viaframe.js +90 -0
- package/src/model/lessons.js +448 -0
- package/src/model/links.js +981 -0
- package/src/model/live.js +411 -0
- package/src/model/math-widget.js +53 -0
- package/src/model/memory-settings.js +33 -0
- package/src/model/midi-controls.js +350 -0
- package/src/model/nl-time.js +408 -0
- package/src/model/notebook.js +137 -0
- package/src/model/pane-view-store.js +15 -0
- package/src/model/partial.js +115 -0
- package/src/model/pause-clock.js +53 -0
- package/src/model/peer-pane.js +32 -0
- package/src/model/persist-state.js +290 -0
- package/src/model/person-only.js +122 -0
- package/src/model/pin.js +209 -0
- package/src/model/plot-3d.js +213 -0
- package/src/model/plot-series.js +157 -0
- package/src/model/plot-settings.js +531 -0
- package/src/model/ports.js +312 -0
- package/src/model/presentation.js +222 -0
- package/src/model/preset-apply.js +316 -0
- package/src/model/preset-banks.js +335 -0
- package/src/model/preset-morph.js +263 -0
- package/src/model/preset-ops.js +205 -0
- package/src/model/preset-player.js +283 -0
- package/src/model/preset-states.js +172 -0
- package/src/model/profile-settings.js +109 -0
- package/src/model/promote.js +103 -0
- package/src/model/protected.js +10 -0
- package/src/model/proxy-log.js +230 -0
- package/src/model/proxy.js +367 -0
- package/src/model/quantity-widget.js +61 -0
- package/src/model/queue-book.js +359 -0
- package/src/model/quick-add.js +224 -0
- package/src/model/rate-limit.js +313 -0
- package/src/model/rate.js +95 -0
- package/src/model/recovery.js +216 -0
- package/src/model/rename-follow.js +113 -0
- package/src/model/repeater.js +1126 -0
- package/src/model/retention.js +664 -0
- package/src/model/route-edit.js +132 -0
- package/src/model/router.js +155 -0
- package/src/model/runaway.js +232 -0
- package/src/model/runlog.js +1033 -0
- package/src/model/runners/a2a.js +2 -0
- package/src/model/runners/acp.js +2 -0
- package/src/model/runners/actions.js +4 -0
- package/src/model/runners/agent.js +4 -0
- package/src/model/runners/audio.js +21 -0
- package/src/model/runners/automation.js +2 -0
- package/src/model/runners/calc.js +23 -0
- package/src/model/runners/catch.js +10 -0
- package/src/model/runners/codec.js +24 -0
- package/src/model/runners/companion-run.js +2 -0
- package/src/model/runners/device.js +7 -0
- package/src/model/runners/dom.js +13 -0
- package/src/model/runners/effects.js +2 -0
- package/src/model/runners/embed.js +11 -0
- package/src/model/runners/eval.js +2 -0
- package/src/model/runners/files.js +19 -0
- package/src/model/runners/geometry.js +23 -0
- package/src/model/runners/goals.js +18 -0
- package/src/model/runners/graph.js +23 -0
- package/src/model/runners/html.js +2 -0
- package/src/model/runners/http-proxy.js +185 -0
- package/src/model/runners/http-send.js +61 -0
- package/src/model/runners/http-static.js +83 -0
- package/src/model/runners/http-tap.js +26 -0
- package/src/model/runners/http.js +40 -0
- package/src/model/runners/import.js +2 -0
- package/src/model/runners/index.js +74 -0
- package/src/model/runners/input.js +14 -0
- package/src/model/runners/inputlog.js +7 -0
- package/src/model/runners/ledger.js +15 -0
- package/src/model/runners/machine.js +4 -0
- package/src/model/runners/mcp.js +2 -0
- package/src/model/runners/mcpapp.js +2 -0
- package/src/model/runners/media.js +18 -0
- package/src/model/runners/memory.js +28 -0
- package/src/model/runners/mesh-inspect.js +13 -0
- package/src/model/runners/peer.js +14 -0
- package/src/model/runners/plot.js +22 -0
- package/src/model/runners/render.js +14 -0
- package/src/model/runners/respond.js +2 -0
- package/src/model/runners/sample-soundfont.js +85 -0
- package/src/model/runners/sample.js +159 -0
- package/src/model/runners/scene.js +8 -0
- package/src/model/runners/score.js +24 -0
- package/src/model/runners/secret.js +10 -0
- package/src/model/runners/select.js +2 -0
- package/src/model/runners/shared.js +4 -0
- package/src/model/runners/shares.js +15 -0
- package/src/model/runners/shell.js +7 -0
- package/src/model/runners/skill.js +26 -0
- package/src/model/runners/state.js +2 -0
- package/src/model/runners/statechart.js +4 -0
- package/src/model/runners/subgraph.js +9 -0
- package/src/model/runners/tabs.js +25 -0
- package/src/model/runners/terminal.js +5 -0
- package/src/model/runners/text.js +4 -0
- package/src/model/runners/timer.js +7 -0
- package/src/model/runners/traffic.js +65 -0
- package/src/model/runners/trigger.js +2 -0
- package/src/model/runners/verdict.js +29 -0
- package/src/model/runners/vm.js +7 -0
- package/src/model/runners/wait.js +4 -0
- package/src/model/runners/ws-client.js +57 -0
- package/src/model/safe-regex.js +719 -0
- package/src/model/sample-settings.js +43 -0
- package/src/model/sample-source.js +47 -0
- package/src/model/saved-mapping.js +153 -0
- package/src/model/score-controllers.js +87 -0
- package/src/model/score-edit.js +399 -0
- package/src/model/score-instruments.js +300 -0
- package/src/model/score-library.js +407 -0
- package/src/model/score-markup.js +235 -0
- package/src/model/score-mix.js +248 -0
- package/src/model/score-patterns.js +276 -0
- package/src/model/score-selection.js +34 -0
- package/src/model/score-time.js +139 -0
- package/src/model/score-transforms.js +336 -0
- package/src/model/score-transport.js +192 -0
- package/src/model/selection.js +351 -0
- package/src/model/serial-controls.js +328 -0
- package/src/model/shares-pane.js +17 -0
- package/src/model/shell-pane.js +106 -0
- package/src/model/signature.js +1122 -0
- package/src/model/size-hints.js +106 -0
- package/src/model/size-ratio.js +12 -0
- package/src/model/skill-settings.js +23 -0
- package/src/model/snapping.js +466 -0
- package/src/model/split.js +375 -0
- package/src/model/spring-player.js +222 -0
- package/src/model/statecharts.js +1369 -0
- package/src/model/states-preset.js +208 -0
- package/src/model/static-route.js +100 -0
- package/src/model/step-words.js +45 -0
- package/src/model/subgraph.js +1042 -0
- package/src/model/switch.js +98 -0
- package/src/model/tabs-store.js +10 -0
- package/src/model/tensor-view.js +146 -0
- package/src/model/timer-clock.js +361 -0
- package/src/model/timers.js +422 -0
- package/src/model/timing.js +212 -0
- package/src/model/traffic-agent.js +111 -0
- package/src/model/traffic-store.js +20 -0
- package/src/model/traffic.js +665 -0
- package/src/model/transport.js +364 -0
- package/src/model/turns.js +86 -0
- package/src/model/types.js +425 -0
- package/src/model/value-path.js +138 -0
- package/src/model/versions.js +535 -0
- package/src/model/view-outputs.js +144 -0
- package/src/model/viewport-bookmarks.js +194 -0
- package/src/model/virtual-clock.js +85 -0
- package/src/model/vm-pane.js +258 -0
- package/src/model/wait-clock.js +715 -0
- package/src/model/wait-persist.js +248 -0
- package/src/model/waits.js +431 -0
- package/src/model/watch.js +80 -0
- package/src/model/webhook.js +496 -0
- package/src/model/when.js +48 -0
- package/src/model/widget-values.js +392 -0
- package/src/model/window-commands.js +278 -0
- package/src/model/windows.js +348 -0
- package/src/model/wire-inspect.js +343 -0
- package/src/model/wire-ops.js +324 -0
- package/src/model/wire-pipeline.js +378 -0
- package/src/model/wire-route.js +637 -0
- package/src/model/workspaces.js +122 -0
- package/src/model/ws-client.js +155 -0
- package/src/model/ws-demo.js +31 -0
- package/src/model/ws-frames.js +141 -0
- package/src/model/ws-hub.js +219 -0
- package/src/model/ws-served.js +13 -0
- package/src/model/xstate.js +374 -0
- package/src/narrative/narrative.js +1557 -0
- package/src/network/auth.js +224 -0
- package/src/network/csp-frame.js +197 -0
- package/src/network/declarative-shadow.js +27 -0
- package/src/network/frame-net-shim.js +962 -0
- package/src/network/hosts.js +157 -0
- package/src/network/policy.js +520 -0
- package/src/network/rtc-shim.js +418 -0
- package/src/network/secret-gate.js +78 -0
- package/src/network/secrets.js +261 -0
- package/src/network/sockets.js +287 -0
- package/src/network/store.js +186 -0
- package/src/network/streams.js +99 -0
- package/src/packages/asset-values.js +50 -0
- package/src/packages/assets.js +99 -0
- package/src/packages/deprecation.js +203 -0
- package/src/packages/fetch.js +152 -0
- package/src/packages/function-canvas.js +73 -0
- package/src/packages/module.js +120 -0
- package/src/packages/package.js +474 -0
- package/src/packages/published.js +27 -0
- package/src/packages/requires.js +83 -0
- package/src/packages/signing.js +145 -0
- package/src/packages/trust.js +107 -0
- package/src/panes/a2a.js +51 -0
- package/src/panes/acp.js +60 -0
- package/src/panes/actions.js +54 -0
- package/src/panes/agent.js +83 -0
- package/src/panes/audio.js +103 -0
- package/src/panes/automation.js +112 -0
- package/src/panes/calc.js +67 -0
- package/src/panes/catch.js +52 -0
- package/src/panes/codec.js +59 -0
- package/src/panes/colors.js +12 -0
- package/src/panes/companion.js +54 -0
- package/src/panes/decision.js +65 -0
- package/src/panes/device.js +56 -0
- package/src/panes/dom.js +37 -0
- package/src/panes/effects.js +118 -0
- package/src/panes/embed.js +30 -0
- package/src/panes/eval.js +186 -0
- package/src/panes/fields.js +299 -0
- package/src/panes/files.js +45 -0
- package/src/panes/gate.js +70 -0
- package/src/panes/geometry.js +64 -0
- package/src/panes/goals.js +75 -0
- package/src/panes/graph.js +66 -0
- package/src/panes/html.js +54 -0
- package/src/panes/http.js +82 -0
- package/src/panes/import.js +38 -0
- package/src/panes/input.js +124 -0
- package/src/panes/inputlog.js +66 -0
- package/src/panes/ledger.js +56 -0
- package/src/panes/machine.js +138 -0
- package/src/panes/mcp.js +85 -0
- package/src/panes/mcpapp.js +64 -0
- package/src/panes/media.js +38 -0
- package/src/panes/memory.js +46 -0
- package/src/panes/mesh-inspect.js +49 -0
- package/src/panes/note.js +38 -0
- package/src/panes/peer.js +44 -0
- package/src/panes/plot.js +196 -0
- package/src/panes/registry.js +475 -0
- package/src/panes/render.js +87 -0
- package/src/panes/respond.js +44 -0
- package/src/panes/router.js +84 -0
- package/src/panes/sample.js +75 -0
- package/src/panes/scene.js +55 -0
- package/src/panes/score.js +87 -0
- package/src/panes/secret.js +38 -0
- package/src/panes/select.js +67 -0
- package/src/panes/shader.js +166 -0
- package/src/panes/shared.js +33 -0
- package/src/panes/shares.js +43 -0
- package/src/panes/shell.js +47 -0
- package/src/panes/skill.js +44 -0
- package/src/panes/state.js +91 -0
- package/src/panes/statechart.js +63 -0
- package/src/panes/story.js +33 -0
- package/src/panes/subgraph.js +94 -0
- package/src/panes/tabs.js +52 -0
- package/src/panes/terminal.js +57 -0
- package/src/panes/text.js +36 -0
- package/src/panes/timer.js +55 -0
- package/src/panes/traffic.js +59 -0
- package/src/panes/verdict.js +62 -0
- package/src/panes/vm.js +46 -0
- package/src/panes/wait.js +61 -0
- package/src/panes/watch.js +57 -0
- package/src/panes/webhook.js +67 -0
- package/src/panes/ws-client.js +67 -0
- package/src/persist.js +281 -0
- package/src/plot/tex.js +19 -0
- package/src/plot/vendor.js +59 -0
- package/src/plot/view-vendor.js +5 -0
- package/src/privacy/levels.js +108 -0
- package/src/protocol/devtools.js +116 -0
- package/src/protocol/inspector.js +368 -0
- package/src/render/time.js +381 -0
- package/src/rooms/mesh-metrics.js +162 -0
- package/src/run/companion.js +355 -0
- package/src/run/describe.js +114 -0
- package/src/run/local-model.js +150 -0
- package/src/run/monitor.js +208 -0
- package/src/run/publish.js +360 -0
- package/src/run/respond-outcome.js +12 -0
- package/src/run/run.js +820 -0
- package/src/run/runner-pairing.js +243 -0
- package/src/run/values.js +122 -0
- package/src/runtime/compile.js +122 -0
- package/src/runtime/console-tag.js +64 -0
- package/src/runtime/frame-signals.js +126 -0
- package/src/runtime/iteration-push.js +19 -0
- package/src/runtime/keys.js +474 -0
- package/src/runtime/midi-input.js +170 -0
- package/src/runtime/person-waits.js +140 -0
- package/src/runtime/resource-timing.js +56 -0
- package/src/runtime/runtime.js +2150 -0
- package/src/runtime/sandbox-modules.js +163 -0
- package/src/runtime/spring.js +246 -0
- package/src/runtime/strict.js +215 -0
- package/src/runtime/transfer.js +75 -0
- package/src/runtime/tween.js +158 -0
- package/src/runtime/wasm-fuel.js +596 -0
- package/src/runtime/worker/00-prelude.js +88 -0
- package/src/runtime/worker/10-helpers.js +295 -0
- package/src/runtime/worker/20-io.js +645 -0
- package/src/runtime/worker/30-previews.js +210 -0
- package/src/runtime/worker/40-profile-lineage.js +278 -0
- package/src/runtime/worker/50-own-worker.js +233 -0
- package/src/runtime/worker/60-timers.js +172 -0
- package/src/runtime/worker/70-state-keyed.js +193 -0
- package/src/runtime/worker/80-each-compile-generators.js +619 -0
- package/src/runtime/worker/90-events-operators.js +655 -0
- package/src/runtime/worker/95-agents-batches.js +1820 -0
- package/src/runtime/worker/96-plot.js +47 -0
- package/src/runtime/worker/97-calc.js +28 -0
- package/src/runtime/worker/98-geometry.js +24 -0
- package/src/runtime/worker/99-graph.js +30 -0
- package/src/runtime/worker/99-shell.js +30 -0
- package/src/safety/config.js +57 -0
- package/src/safety/engine.js +324 -0
- package/src/scene/pixi-scene.js +806 -0
- package/src/scene/scene-assets.js +241 -0
- package/src/scene/scene-edit.js +319 -0
- package/src/secret/settings.js +41 -0
- package/src/security/multipart.js +257 -0
- package/src/security/redact.js +261 -0
- package/src/serial/broker.js +473 -0
- package/src/serial/config.js +185 -0
- package/src/serial/framers.js +389 -0
- package/src/serial/packets.js +64 -0
- package/src/shader/audio-texture.js +87 -0
- package/src/shader/channels.js +496 -0
- package/src/shader/common.js +90 -0
- package/src/shader/model.js +867 -0
- package/src/shader/passes.js +150 -0
- package/src/shader/shadertoy.js +198 -0
- package/src/shell/hub.js +287 -0
- package/src/sim/checkpoints.js +850 -0
- package/src/sim/compile.js +37 -0
- package/src/sim/fixed.js +377 -0
- package/src/sim/headless.js +205 -0
- package/src/sim/physics.js +399 -0
- package/src/smf-helper/vendor.js +2 -0
- package/src/sources/config.js +435 -0
- package/src/storage.js +153 -0
- package/src/table/table-geo.js +2344 -0
- package/src/table/table-lineage.js +808 -0
- package/src/table/table-quality.js +355 -0
- package/src/table/table-schema.js +392 -0
- package/src/table/table-sql.js +1650 -0
- package/src/table/table-stats.js +480 -0
- package/src/table/table.js +2102 -0
- package/src/tensor/tensor.js +1153 -0
- package/src/terminal/hub.js +715 -0
- package/src/terminal/screen.js +516 -0
- package/src/tests/capture.js +88 -0
- package/src/tests/compare.js +480 -0
- package/src/tests/dataset.js +170 -0
- package/src/tests/evaluate.js +286 -0
- package/src/tests/from-runs.js +99 -0
- package/src/tests/judge.js +134 -0
- package/src/tests/matchers.js +263 -0
- package/src/tests/matrix-spec.js +87 -0
- package/src/tests/matrix.js +397 -0
- package/src/tests/metrics.js +369 -0
- package/src/tests/no-value.js +42 -0
- package/src/tests/pane-check.js +45 -0
- package/src/tests/report.js +124 -0
- package/src/tests/reporters.js +140 -0
- package/src/tests/run.js +155 -0
- package/src/tilemap/tilemap.js +1122 -0
- package/src/units/units.js +3 -0
- package/src/util/bytes-text.js +21 -0
- package/src/util/num.js +10 -0
- package/src/util/regexp.js +2 -0
- package/src/util/text.js +38 -0
- package/src/verdict/engine.js +154 -0
- package/src/vm/guest-wsh.js +108 -0
- package/src/vm/hub.js +457 -0
- package/src/vm/image-files.js +4 -0
- package/src/vm/image.js +33 -0
- package/src/vm/image.lock.json +13 -0
- package/src/vm/link.js +266 -0
- package/src/vm/mount.js +212 -0
- package/src/vm/net.js +85 -0
- package/src/vm/pipe-ws.js +187 -0
- package/src/vm/v86-loader.js +11 -0
- package/src/webmcp/args.js +212 -0
- package/src/webmcp/hidden.js +218 -0
- package/src/webmcp/text.js +250 -0
- package/src/webmcp/tools/bookmark-tools.js +106 -0
- package/src/webmcp/tools/breakpoint-tools.js +48 -0
- package/src/webmcp/tools/canvases.js +208 -0
- package/src/webmcp/tools/context-tools.js +84 -0
- package/src/webmcp/tools/core.js +170 -0
- package/src/webmcp/tools/devices.js +41 -0
- package/src/webmcp/tools/edit.js +357 -0
- package/src/webmcp/tools/event-tools.js +145 -0
- package/src/webmcp/tools/image-tools.js +173 -0
- package/src/webmcp/tools/ink-tools.js +247 -0
- package/src/webmcp/tools/input-log-tools.js +163 -0
- package/src/webmcp/tools/live.js +166 -0
- package/src/webmcp/tools/machine-tools.js +74 -0
- package/src/webmcp/tools/media.js +161 -0
- package/src/webmcp/tools/midi-tools.js +32 -0
- package/src/webmcp/tools/preset-tools.js +184 -0
- package/src/webmcp/tools/quick-add-tools.js +95 -0
- package/src/webmcp/tools/reshape.js +152 -0
- package/src/webmcp/tools/run-tools.js +309 -0
- package/src/webmcp/tools/runs.js +101 -0
- package/src/webmcp/tools/scaffold-tools.js +161 -0
- package/src/webmcp/tools/serial-tools.js +36 -0
- package/src/webmcp/tools/shared.js +338 -0
- package/src/webmcp/tools/subgraphs.js +360 -0
- package/src/webmcp/tools/tests.js +170 -0
- package/src/webmcp/tools/traffic-tools.js +70 -0
- package/src/webmcp/tools/transport-tools.js +65 -0
- package/src/webmcp/tools/version-tools.js +126 -0
- package/src/webmcp/tools/versions.js +11 -0
- package/src/webmcp/tools/wire-tools.js +90 -0
- package/src/webmcp/tools/workspace-tools.js +93 -0
- package/src/webmcp/tools.js +1323 -0
- package/src/wsh/connect.js +347 -0
- package/src/wsh/demo-host.js +361 -0
- package/src/wsh/e2e.js +27 -0
- package/src/wsh/keys.js +154 -0
- package/src/wsh/known-hosts.js +39 -0
- package/src/wsh/private-key.js +95 -0
- package/src/wsh/reverse.js +34 -0
- package/src/wsh/settings.js +118 -0
- package/src/wsh/transfer.js +62 -0
- package/src/wsh/vendor.js +5 -0
- package/src/x402/challenge.js +171 -0
- package/src/x402/demo-server.js +127 -0
- package/src/x402/pay.js +125 -0
- package/src/x402/retry.js +95 -0
- package/src/x402/store.js +76 -0
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
// An event (src/model/events.js) is stored here under a key of its own, as
|
|
2
|
+
// { name, payload, at, source } (frozen), with its payload as that key's
|
|
3
|
+
// value, so a reader that runs later still reads its own event. Each event
|
|
4
|
+
// output (and each event wire) keeps its last `keep` events, a ring; the
|
|
5
|
+
// node's own value is its latest event. Pane code sends one with
|
|
6
|
+
// emit.event(name, payload), or by making a value keyed by an event output's
|
|
7
|
+
// name; they go to the page in order, in batches, one message in flight per
|
|
8
|
+
// pane (eventOutbox). A run's reply (exec, liveNext) waits until the events it
|
|
9
|
+
// sent have all reached the page (eventsDelivered), so the page never sees the
|
|
10
|
+
// run finished while some of its events are still here: a headless run,
|
|
11
|
+
// an agent's wait or a test that waits for the canvas to settle gets them all.
|
|
12
|
+
// Operators on a wire are @johnhenry/iteration's push
|
|
13
|
+
// transducers, kept per wire and stage (wirePush); what comes out waits here
|
|
14
|
+
// (wireItems) until the page takes it or a debounce drops it.
|
|
15
|
+
const eventStore = new Map(); // key -> frozen event
|
|
16
|
+
const eventRings = new Map(); // event node -> [keys], oldest first
|
|
17
|
+
const eventOutputs = new Map(); // pane id -> { runId, names, keep, label }: its event outputs (set by exec)
|
|
18
|
+
const eventOutbox = new Map(); // pane id -> { list, sending, queued, done, waiters }: events queued and handed over (counts), runs waiting
|
|
19
|
+
let eventSeq = 0;
|
|
20
|
+
/** The wait before a pane's next batch goes, once the page took the last (ms). Tests stretch it (createRuntime's eventBatchGapMs). */
|
|
21
|
+
let eventBatchGapMs = 0;
|
|
22
|
+
|
|
23
|
+
/** Keep event `event` under `key` in node `node`'s ring; the node's value becomes it. Returns its preview. */
|
|
24
|
+
function keepEvent(node, key, event, keep = 1002) {
|
|
25
|
+
eventStore.set(key, event);
|
|
26
|
+
values.set(key, event.payload);
|
|
27
|
+
let ring = eventRings.get(node);
|
|
28
|
+
if (!ring) eventRings.set(node, (ring = []));
|
|
29
|
+
ring.push(key);
|
|
30
|
+
while (ring.length > Math.max(1, keep)) {
|
|
31
|
+
const old = ring.shift();
|
|
32
|
+
eventStore.delete(old);
|
|
33
|
+
values.delete(old);
|
|
34
|
+
}
|
|
35
|
+
values.set(node, event);
|
|
36
|
+
return present(node, event, "auto");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Payload objects of events a person removed: later sweeps of the wires' rings still recognise them. */
|
|
40
|
+
const removedPayloads = new WeakSet();
|
|
41
|
+
const isRemoved = (p) => p !== null && typeof p === "object" && removedPayloads.has(p);
|
|
42
|
+
|
|
43
|
+
/** Take event `key` out of the store, the values and `node`'s ring. The node's own value, when it was this event, becomes the one before it (or the same event without its payload). Returns whether it was held. */
|
|
44
|
+
function dropFromRing(node, key) {
|
|
45
|
+
const gone = eventStore.get(key);
|
|
46
|
+
const had = eventStore.delete(key);
|
|
47
|
+
values.delete(key);
|
|
48
|
+
const ring = eventRings.get(node);
|
|
49
|
+
const at = ring ? ring.indexOf(key) : -1;
|
|
50
|
+
if (at >= 0) ring.splice(at, 1);
|
|
51
|
+
if (gone && values.get(node) === gone) {
|
|
52
|
+
const before = ring?.length ? eventStore.get(ring[ring.length - 1]) : undefined;
|
|
53
|
+
values.set(node, before ?? Object.freeze({ ...gone, payload: null }));
|
|
54
|
+
}
|
|
55
|
+
return had;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Let go of event `key` of node `node` (its take was removed): out of the store, the values and the ring,
|
|
60
|
+
* so the Blob a take's payload holds is no longer referenced here. Then the same for the copies the wires of
|
|
61
|
+
* operators made of it: every other ring entry that carries that payload object goes (a wire's filter, map x, take, dedupe…
|
|
62
|
+
* pass the event on), and an entry whose payload is an array holding it (a group's or partition's output) is rewritten
|
|
63
|
+
* without it, or goes when nothing else is left. Then the operators' own state (`dropHeld`): a half-full group or
|
|
64
|
+
* partition, accumulate's last event, dedupe's last key. Then what the stages already emitted and is on its way to the
|
|
65
|
+
* next stage or the page (`wireItems`): tombstoned in place (`tombstone`), so no reader loses an event and no batch
|
|
66
|
+
* shrinks after it was sent. Returns whether the event itself was held.
|
|
67
|
+
*/
|
|
68
|
+
function discardEvent(node, key) {
|
|
69
|
+
const gone = eventStore.get(key);
|
|
70
|
+
const had = dropFromRing(node, key);
|
|
71
|
+
if (gone && gone.payload !== null && typeof gone.payload === "object") removedPayloads.add(gone.payload);
|
|
72
|
+
for (const [ringNode, ring] of eventRings) {
|
|
73
|
+
for (const k of [...ring]) {
|
|
74
|
+
const item = eventStore.get(k);
|
|
75
|
+
if (!item) continue;
|
|
76
|
+
const p = item.payload;
|
|
77
|
+
if (isRemoved(p)) dropFromRing(ringNode, k);
|
|
78
|
+
else if (Array.isArray(p) && p.some(isRemoved)) {
|
|
79
|
+
const rest = p.filter((x) => !isRemoved(x));
|
|
80
|
+
if (!rest.length) dropFromRing(ringNode, k);
|
|
81
|
+
else {
|
|
82
|
+
const next = Object.freeze({ ...item, payload: rest });
|
|
83
|
+
eventStore.set(k, next);
|
|
84
|
+
values.set(k, rest);
|
|
85
|
+
if (values.get(ringNode) === item) values.set(ringNode, next);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
dropHeld();
|
|
91
|
+
tombstoneItems();
|
|
92
|
+
return had;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The same thing without the removed take: an event carrying it becomes a copy whose payload is `null` (the shape
|
|
97
|
+
* `dropHeld` gives accumulate's last event); a value that is it becomes `null`; an array (a batch a group or partitionBy
|
|
98
|
+
* emitted) keeps its length, with the slot that held it tombstoned the same way. Returns `item` itself when it holds nothing.
|
|
99
|
+
*/
|
|
100
|
+
function tombstone(item) {
|
|
101
|
+
if (isRemoved(item)) return null;
|
|
102
|
+
if (item === null || typeof item !== "object") return item;
|
|
103
|
+
if (Array.isArray(item)) return item.some(holdsRemoved) ? item.map(tombstone) : item;
|
|
104
|
+
if (isRemoved(item.payload) || (Array.isArray(item.payload) && item.payload.some(holdsRemoved))) {
|
|
105
|
+
const copy = Object.freeze({ ...item, payload: tombstone(item.payload) });
|
|
106
|
+
if (copy.payload === null) tombstones.add(copy);
|
|
107
|
+
return copy;
|
|
108
|
+
}
|
|
109
|
+
return item;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Events tombstoned in `wireItems`: later stages pass them on without running an operator's expression on a payload that isn't there. */
|
|
113
|
+
const tombstones = new WeakSet();
|
|
114
|
+
|
|
115
|
+
/** What stages emitted and nothing has taken yet (`wireItems`), tombstoned in place: same keys, order and count. */
|
|
116
|
+
function tombstoneItems() {
|
|
117
|
+
for (const [key, item] of wireItems) {
|
|
118
|
+
const next = tombstone(item);
|
|
119
|
+
if (next !== item) wireItems.set(key, next);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Whether something a stage of operators holds carries a removed take's payload: the payload (an event's or a value wire's), an event holding it, a group's array of them, a dedupe key that is it. */
|
|
124
|
+
function holdsRemoved(h) {
|
|
125
|
+
if (isRemoved(h)) return true;
|
|
126
|
+
if (h === null || typeof h !== "object") return false;
|
|
127
|
+
if (Array.isArray(h)) return h.some(holdsRemoved);
|
|
128
|
+
return isRemoved(h.payload) || (Array.isArray(h.payload) && h.payload.some(isRemoved));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Let go of what wires' operators hold of removed takes (the push transducers' `held`/`drop`): a group or partitionBy
|
|
133
|
+
* leaves the item out of the batch it will send (the batch is not sent early, and holds the rest); dedupe forgets a last
|
|
134
|
+
* key that is the removed payload (an equal item then passes once). Accumulate's pair is `{ acc, item }`: its running
|
|
135
|
+
* value is the person's, so when that carries the take the fold starts again, and when only the last event does (the
|
|
136
|
+
* event is held to give the output its payload) the event is replaced by the same one without its payload and the
|
|
137
|
+
* running value goes on.
|
|
138
|
+
*/
|
|
139
|
+
function dropHeld() {
|
|
140
|
+
for (const stages of wireStates.values()) {
|
|
141
|
+
for (const st of stages.values()) {
|
|
142
|
+
st.push.drop((h) => {
|
|
143
|
+
if (isFoldPair(h)) {
|
|
144
|
+
if (holdsRemoved(h.acc)) return true;
|
|
145
|
+
if (holdsRemoved(h.item)) h.item = Object.freeze({ ...h.item, payload: null });
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
return holdsRemoved(h);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const isFoldPair = (h) => h !== null && typeof h === "object" && !Array.isArray(h) && "acc" in h && "item" in h;
|
|
155
|
+
/** How many things wires' operators hold, or have emitted and not yet handed on, that still carry a removed take (debug: 0 once a removal has been swept). */
|
|
156
|
+
function heldRemovedCount() {
|
|
157
|
+
let n = 0;
|
|
158
|
+
for (const stages of wireStates.values()) for (const st of stages.values()) n += st.push.held().filter((h) => (isFoldPair(h) ? holdsRemoved(h.acc) || holdsRemoved(h.item) : holdsRemoved(h))).length;
|
|
159
|
+
for (const item of wireItems.values()) if (holdsRemoved(item)) n++; // emitted, waiting for the next stage or the page
|
|
160
|
+
return n;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function dropRing(node) {
|
|
164
|
+
for (const key of eventRings.get(node) ?? []) {
|
|
165
|
+
eventStore.delete(key);
|
|
166
|
+
values.delete(key);
|
|
167
|
+
}
|
|
168
|
+
eventRings.delete(node);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The `event` a reader's run sees: the stored event, and `input`, the name of the input it came in on. */
|
|
172
|
+
function eventFor(key, input) {
|
|
173
|
+
const stored = eventStore.get(key);
|
|
174
|
+
return stored && typeof input === "string" ? Object.freeze({ ...stored, input }) : stored;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** How long emit.event(…, { ack: true }) waits for the page's answer before it says it got none (ms). The page answers in the same call that delivers the event, so this is only a ceiling. */
|
|
178
|
+
const ACK_WAIT_MS = 10000;
|
|
179
|
+
|
|
180
|
+
/** The key of a batch: src/model/batches.js's rule (this file can't import it). */
|
|
181
|
+
function batchKey(what, key) {
|
|
182
|
+
if (typeof key !== "string" || !key) throw new TypeError(`${what}: a batch key is a non-empty string (the same text on every event and marker of one batch)`);
|
|
183
|
+
if (key.length > 64) throw new TypeError(`${what}: a batch key is at most 64 characters`);
|
|
184
|
+
return key;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** `emit.event`'s third argument: `{ ack: true }` asks to hear whether the readers' queues took the event; `{ batch: key }` tags it as part of a finite batch. */
|
|
188
|
+
function eventOptions(name, options) {
|
|
189
|
+
if (options === undefined) return { ack: false };
|
|
190
|
+
const extra = options && typeof options === "object" ? Object.keys(options).filter((k) => k !== "ack" && k !== "batch") : ["?"];
|
|
191
|
+
if (extra.length) throw new TypeError(`emit.event("${name}", payload, options): options is { ack: true } (an acknowledged send resolves to { accepted, reason? }) or { batch: "key" } (the event is part of a finite batch), not ${extra.join(", ")}`);
|
|
192
|
+
return { ack: Boolean(options.ack), ...(options.batch === undefined ? {} : { batch: batchKey(`emit.event("${name}", …, { batch })`, options.batch) }) };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const ackAsked = (name, options) => eventOptions(name, options).ack;
|
|
196
|
+
|
|
197
|
+
/** Answer (or time out) the acknowledged event `key` of `box`. */
|
|
198
|
+
function settleAck(box, key, answer) {
|
|
199
|
+
const a = box.acks?.get(key);
|
|
200
|
+
if (!a) return;
|
|
201
|
+
clearTimeout(a.timer);
|
|
202
|
+
box.acks.delete(key);
|
|
203
|
+
a.resolve(answer);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Acknowledged events of `list` that have no answer yet get `why` as a refusal. */
|
|
207
|
+
function unanswered(box, list, why) {
|
|
208
|
+
for (const item of list) if (item.ack) settleAck(box, item.key, { accepted: false, reason: why, unanswered: true });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Pane `id` sends event `name` (emit.event, or a keyed value): kept here, then told to the page in order. With `options.ack`, resolves to the page's answer ({ accepted, reason? }). */
|
|
212
|
+
function queueEvent(id, name, payload, options) {
|
|
213
|
+
const info = eventOutputs.get(id);
|
|
214
|
+
if (!info?.names.includes(name)) {
|
|
215
|
+
throw new TypeError(`emit.event("${name}"): this pane has no event output "${name}"${info?.names.length ? ` (its event outputs: ${info.names.join(", ")})` : ""}; add one with Outputs…, type "event"`);
|
|
216
|
+
}
|
|
217
|
+
const { ack, batch } = eventOptions(name, options);
|
|
218
|
+
const node = `${id}#${name}`;
|
|
219
|
+
const key = `${node}@${ownMode ? "o" : "w"}${++eventSeq}`;
|
|
220
|
+
const event = Object.freeze({ name, payload, at: stamp(), source: info.label, ...(batch === undefined ? {} : { batch: Object.freeze({ key: batch }) }) });
|
|
221
|
+
let item;
|
|
222
|
+
if (ownMode) {
|
|
223
|
+
// Its own Worker: the event goes to the shared sandbox as a copy, which keeps it (forwardEvents on the page).
|
|
224
|
+
const problem = copyProblem(payload);
|
|
225
|
+
if (problem) throw ownCopyError(`emit.event("${name}", …)`, problem);
|
|
226
|
+
item = { name, key, payload: toCopy(payload), at: event.at, source: event.source, keep: info.keep, ...(batch === undefined ? {} : { batch }) };
|
|
227
|
+
} else item = { name, key, result: keepEvent(node, key, event, info.keep), at: event.at, source: event.source, bytes: approxBytes(payload), ...(batch === undefined ? {} : { batch }) }; // its size, for the readers' byte budgets
|
|
228
|
+
return handOver(id, name, key, item, ack);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Pane `id` sends a batch marker on event output `name`: no payload is kept here, only the boundary is told to the page, in order with the events. */
|
|
232
|
+
function queueMarker(id, name, kind, batch, reason, options) {
|
|
233
|
+
const what = `emit.batch.${kind}("${name}", …)`;
|
|
234
|
+
const info = eventOutputs.get(id);
|
|
235
|
+
if (!info?.names.includes(name)) throw new TypeError(`${what}: this pane has no event output "${name}"${info?.names.length ? ` (its event outputs: ${info.names.join(", ")})` : ""}; add one with Outputs…, type "event"`);
|
|
236
|
+
batchKey(what, batch);
|
|
237
|
+
if (reason !== undefined && (typeof reason !== "string" || reason.length > 500)) throw new TypeError(`${what}: a reason is text of at most 500 characters`);
|
|
238
|
+
const extra = options && typeof options === "object" ? Object.keys(options).filter((k) => !["ack", "onError", "onCancel"].includes(k)) : options === undefined ? [] : ["?"];
|
|
239
|
+
if (extra.length) throw new TypeError(`${what}: options is { ack: true }${kind === "start" ? ", onError and onCancel: \"discard\" or \"emit\"" : ""}, not ${extra.join(", ")}`);
|
|
240
|
+
const marker = { kind, batch };
|
|
241
|
+
for (const policy of ["onError", "onCancel"]) {
|
|
242
|
+
if (options?.[policy] === undefined) continue;
|
|
243
|
+
if (kind !== "start") throw new TypeError(`${what}: ${policy} is declared where a batch starts: only a start marker takes it`);
|
|
244
|
+
if (options[policy] !== "discard" && options[policy] !== "emit") throw new TypeError(`${what}: ${policy} is "discard" or "emit", not ${JSON.stringify(options[policy])}`);
|
|
245
|
+
marker[policy] = options[policy];
|
|
246
|
+
}
|
|
247
|
+
if (reason !== undefined) marker.reason = reason;
|
|
248
|
+
const ack = Boolean(options?.ack);
|
|
249
|
+
const key = `${id}#${name}@${ownMode ? "o" : "w"}${++eventSeq}`;
|
|
250
|
+
return handOver(id, name, key, { name, key, marker, at: stamp(), source: info.label, bytes: 0 }, ack);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Queue `item` (an event or a marker) of pane `id` for the page, in order; with `ack`, resolves to the page's answer. */
|
|
254
|
+
function handOver(id, name, key, item, ack) {
|
|
255
|
+
if (ack) item.ack = true;
|
|
256
|
+
let box = eventOutbox.get(id);
|
|
257
|
+
if (!box) eventOutbox.set(id, (box = { list: [], sending: false, queued: 0, done: 0, waiters: [] }));
|
|
258
|
+
box.list.push(item);
|
|
259
|
+
box.queued++;
|
|
260
|
+
let answer = true;
|
|
261
|
+
if (ack) {
|
|
262
|
+
answer = new Promise((resolve) => {
|
|
263
|
+
const timer = setTimeout(() => settleAck(box, key, { accepted: false, reason: `the page did not answer within ${ACK_WAIT_MS / 1000} s`, unanswered: true }), ACK_WAIT_MS);
|
|
264
|
+
(box.acks ??= new Map()).set(key, { resolve, timer });
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
flushEvents(id, box);
|
|
268
|
+
return answer;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function flushEvents(id, box) {
|
|
272
|
+
if (box.sending || !box.list.length) return;
|
|
273
|
+
if (eventOutbox.get(id) !== box) {
|
|
274
|
+
const gone = box.list.splice(0);
|
|
275
|
+
unanswered(box, gone, "the pane went before the event was delivered");
|
|
276
|
+
return handed(box, gone.length); // forgotten: they go nowhere
|
|
277
|
+
}
|
|
278
|
+
const list = box.list.splice(0);
|
|
279
|
+
box.sending = true;
|
|
280
|
+
hostRef
|
|
281
|
+
.call("paneEvents", id, ownMode ? viaLink(new Map([["list", list]])) : list) // its own Worker's carry payloads: a Map crosses whole, on the channel when there is one
|
|
282
|
+
.then(
|
|
283
|
+
(reply) => {
|
|
284
|
+
for (const [key, answer] of Object.entries(reply?.acks ?? {})) settleAck(box, key, answer); // the readers' queues' word on acknowledged events
|
|
285
|
+
const later = new Set(reply?.deferred ?? []); // paced channel sends: the page answers them when they go (settleSendAck)
|
|
286
|
+
unanswered(box, later.size ? list.filter((item) => !later.has(item.key)) : list, "the page gave no answer");
|
|
287
|
+
handed(box, list.length);
|
|
288
|
+
},
|
|
289
|
+
(error) => {
|
|
290
|
+
if (/concurrent/i.test(String(error?.message))) box.list.unshift(...list); // too many calls in flight: send them again
|
|
291
|
+
else {
|
|
292
|
+
unanswered(box, list, `the page refused the event (${String(error?.message ?? error)})`);
|
|
293
|
+
handed(box, list.length); // refused for good: nothing more to wait for
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
)
|
|
297
|
+
.finally(() => {
|
|
298
|
+
box.sending = false;
|
|
299
|
+
if (box.list.length) setTimeout(() => flushEvents(id, box), eventBatchGapMs);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** `n` more of `box`'s events are done with (on the page, or dropped): runs waiting for them may reply. */
|
|
304
|
+
function handed(box, n) {
|
|
305
|
+
box.done += n;
|
|
306
|
+
box.waiters = box.waiters.filter((w) => (box.done >= w.upTo ? (w.resolve(), false) : true));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Resolves once the events pane `id` has queued so far have reached the page
|
|
311
|
+
* (or were dropped with its outbox). Only those: a pane that keeps sending
|
|
312
|
+
* from a timer doesn't hold up the reply of the run that started it.
|
|
313
|
+
*/
|
|
314
|
+
function eventsDelivered(id) {
|
|
315
|
+
const box = eventOutbox.get(id);
|
|
316
|
+
if (!box || box.done >= box.queued) return Promise.resolve();
|
|
317
|
+
return new Promise((resolve) => box.waiters.push({ upTo: box.queued, resolve }));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Run `step` (a run of pane `id`); reply with its result, or its error, once the events it sent have reached the page. */
|
|
321
|
+
async function afterEvents(id, step) {
|
|
322
|
+
try {
|
|
323
|
+
return await step();
|
|
324
|
+
} finally {
|
|
325
|
+
await eventsDelivered(id);
|
|
326
|
+
await forwarded(); // a pane in its own Worker: its setters' changes have reached the shared sandbox too
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* The value run of pane `id` made in its own Worker, handed back (runOwn,
|
|
332
|
+
* eachFinishOwn): it isn't kept here (the shared sandbox keeps it, and makes
|
|
333
|
+
* its preview). `stateBytes`: its `state` too, as JSON.
|
|
334
|
+
*/
|
|
335
|
+
function ownValue(id, stateBytes) {
|
|
336
|
+
const value = values.get(id);
|
|
337
|
+
values.delete(id);
|
|
338
|
+
inspector.release(id);
|
|
339
|
+
const budget = { left: 200000 };
|
|
340
|
+
const problem = copyProblem(value, "", budget);
|
|
341
|
+
if (problem) throw new Error(`This pane runs in its own Worker, so its value is copied to the other panes, and it holds ${problem}, which can't be copied. Return plain data (objects, arrays, Maps, Dates, Blobs…), or ${OWN_OFF.replace(" to use it", "")}.`);
|
|
342
|
+
const state = stateBytes ? stateJSON(id, stateBytes) : { none: true };
|
|
343
|
+
const out = viaLink(new Map([["value", toCopy(value)], ...(budget.binary ? [["binary", true]] : [])]));
|
|
344
|
+
return wrapped({ out, effects: leftBehind(id) || Boolean(state.lost), ...(state.text !== undefined ? { state: state.text } : {}) });
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** A live value from a pane in its own Worker: its events (a value keyed by event outputs), then the copy the page stores in the shared sandbox (ownOut). */
|
|
348
|
+
function ownLive(id, value, what) {
|
|
349
|
+
if (!isSkip(value) && eventOutputs.has(id)) eventsFromValue(id, value);
|
|
350
|
+
return ownOut(value, what);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** A value keyed by event output names (outputs({ tick }), a "*" key for all): those events. */
|
|
354
|
+
function eventsFromValue(id, value) {
|
|
355
|
+
if (!value || typeof value !== "object" || isSkip(value)) return;
|
|
356
|
+
const has = (k) => Object.hasOwn(value, k) && value[k] !== undefined;
|
|
357
|
+
for (const name of eventOutputs.get(id).names) {
|
|
358
|
+
if (has(name)) queueEvent(id, name, value[name]);
|
|
359
|
+
else if (has("*")) queueEvent(id, name, value["*"]);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** The `emit` pane code gets: emit(value) in a live run, and emit.event(name, payload) in any run of the pane while it is the latest. */
|
|
364
|
+
function emitHelper(id, runId, live, forwardingConsole) {
|
|
365
|
+
const valueEmit = live ? emitFor(id, live, forwardingConsole) : null;
|
|
366
|
+
const emit = function emit(value) {
|
|
367
|
+
if (!valueEmit) throw new TypeError("emit(value) is for live panes (code that calls emit(…) itself); emit.event(name, payload) sends an event");
|
|
368
|
+
return valueEmit(value);
|
|
369
|
+
};
|
|
370
|
+
if (valueEmit) emit.close = valueEmit.close;
|
|
371
|
+
emit.event = (name, payload, options) => (latest.get(id) === runId ? queueEvent(id, String(name), payload, options) : ackAsked(String(name), options) ? Promise.resolve({ accepted: false, reason: "this run is no longer the pane's latest" }) : false);
|
|
372
|
+
// Batch boundaries for a finite stream (src/model/batches.js): tag events with { batch }, then end (or fail, or cancel) the batch.
|
|
373
|
+
const marker = (kind) => (name, batch, ...rest) => {
|
|
374
|
+
const withReason = kind === "error" || kind === "cancel";
|
|
375
|
+
const [reason, options] = withReason ? rest : [undefined, rest[0]];
|
|
376
|
+
if (latest.get(id) === runId) return queueMarker(id, String(name), kind, batch, reason, options);
|
|
377
|
+
return options?.ack ? Promise.resolve({ accepted: false, reason: "this run is no longer the pane's latest" }) : false;
|
|
378
|
+
};
|
|
379
|
+
emit.batch = Object.freeze({ start: marker("start"), end: marker("end"), error: marker("error"), cancel: marker("cancel") });
|
|
380
|
+
return Object.freeze(emit);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// Named event channels (src/model/channels.js): send(name, payload, { scope }) keeps the payload
|
|
384
|
+
// here under a key of its own (a ring per pane) and tells the page in order, through the same outbox as
|
|
385
|
+
// emit.event; the page finds the receivers, and each one's event copies the payload from that key.
|
|
386
|
+
// The names and scopes are src/model/channels.js's (this file can't import it).
|
|
387
|
+
const CHANNEL_NAME = /^[A-Za-z0-9_][A-Za-z0-9_.:-]{0,63}$/;
|
|
388
|
+
const CHANNEL_SCOPES = ["canvas", "parent", "children", "siblings"];
|
|
389
|
+
const CHANNEL_KEEP = 1002;
|
|
390
|
+
const channelRings = new Map(); // pane id -> [keys of the payloads it sent], oldest first
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Pane `id` sends `payload` on channel `channel`. `options`: `{ scope }`, and `{ ack: true }` to hear what the page did with it:
|
|
394
|
+
* a promise of `{ accepted, queued, refusedBy?, reason? }` (held while the canvas is paused: `held: true`; waiting for the runaway
|
|
395
|
+
* budget: when it goes, or why it was dropped), where without it the call is `true` and tells nothing.
|
|
396
|
+
*/
|
|
397
|
+
function queueChannel(id, channel, payload, options) {
|
|
398
|
+
const name = typeof channel === "string" ? channel : "";
|
|
399
|
+
if (!CHANNEL_NAME.test(name)) throw new TypeError(`send(${JSON.stringify(channel) ?? String(channel)}, …): a channel is a name of letters, digits and _ . : - (at most 64), like "reset"`);
|
|
400
|
+
if (options !== undefined && (options === null || typeof options !== "object")) throw new TypeError(`send("${name}", payload, options): options is { scope, ack }`);
|
|
401
|
+
const scope = options?.scope ?? "canvas";
|
|
402
|
+
if (!CHANNEL_SCOPES.includes(scope)) throw new TypeError(`send("${name}", …, { scope }): the scope is canvas, parent, children or siblings (not ${JSON.stringify(scope)})`);
|
|
403
|
+
const key = `${id}~${name}@${ownMode ? "o" : "c"}${++eventSeq}`;
|
|
404
|
+
let item = { channel: name, scope, key, at: stamp(), bytes: approxBytes(payload) }; // its size, for the receivers' readers' byte budgets
|
|
405
|
+
if (ownMode) {
|
|
406
|
+
// Its own Worker: the payload goes to the shared sandbox as a copy, which keeps it under `key`.
|
|
407
|
+
const problem = copyProblem(payload);
|
|
408
|
+
if (problem) throw ownCopyError(`send("${name}", …)`, problem);
|
|
409
|
+
item = { ...item, payload: toCopy(payload) };
|
|
410
|
+
} else {
|
|
411
|
+
values.set(key, payload);
|
|
412
|
+
let ring = channelRings.get(id);
|
|
413
|
+
if (!ring) channelRings.set(id, (ring = []));
|
|
414
|
+
ring.push(key);
|
|
415
|
+
while (ring.length > CHANNEL_KEEP) values.delete(ring.shift());
|
|
416
|
+
}
|
|
417
|
+
return handOver(id, name, key, item, Boolean(options?.ack));
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function dropChannelRing(id) {
|
|
421
|
+
for (const key of channelRings.get(id) ?? []) values.delete(key);
|
|
422
|
+
channelRings.delete(id);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** The `send` pane code gets: it sends while its run is the latest, like emit.event. */
|
|
426
|
+
function sendHelper(id, runId) {
|
|
427
|
+
return Object.freeze(function send(channel, payload, options) {
|
|
428
|
+
if (latest.get(id) === runId) return queueChannel(id, channel, payload, options);
|
|
429
|
+
return options?.ack ? Promise.resolve({ accepted: false, queued: 0, reason: "this run is no longer the pane's latest" }) : false;
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Wire operators: their expressions, compiled here (they never run on the page).
|
|
434
|
+
const wireStates = new Map(); // wire node -> Map(stage slot -> { sig, push }): a slot is a stage, or a stage of one finite batch
|
|
435
|
+
/** The slot of stage `stage` in a wire's states: its own, or (a finite batch) the batch's. */
|
|
436
|
+
const stageSlot = (stage, batch) => (batch === undefined || batch === null ? stage : `${batch.length}:${batch}:${stage}`);
|
|
437
|
+
/** Wire `wire`'s operators for one stage (and batch): made when first asked for, again when its operators changed. */
|
|
438
|
+
function wireStage(wire, stage, ops, at, kind, batch, make) {
|
|
439
|
+
if (!transducers) throw new Error("wire operators aren't available in this sandbox");
|
|
440
|
+
let stages = wireStates.get(wire);
|
|
441
|
+
if (!stages) wireStates.set(wire, (stages = new Map()));
|
|
442
|
+
const slot = stageSlot(stage, batch);
|
|
443
|
+
const sig = `${kind}|${JSON.stringify(ops)}`;
|
|
444
|
+
let st = stages.get(slot);
|
|
445
|
+
if (make && (!st || st.sig !== sig)) stages.set(slot, (st = { sig, push: WINDOW_OPS.has(ops[0]?.op) ? windowMachine(ops[0], at, kind) : transducers.transducePush(...ops.flatMap((o, i) => transducersFor(o, at + i, kind))) }));
|
|
446
|
+
return st;
|
|
447
|
+
}
|
|
448
|
+
const wireItems = new Map(); // key -> what came out of a stage, waiting for the next one (or the page)
|
|
449
|
+
let wireSeq = 0;
|
|
450
|
+
/** An event a batch's error or cancel flushed: marked `batch.partial` (with `reason`). */
|
|
451
|
+
function markPartial(next, batch, reason) {
|
|
452
|
+
if (!next || typeof next !== "object") return next;
|
|
453
|
+
return Object.freeze({ ...next, batch: Object.freeze({ ...next.batch, key: next.batch?.key ?? batch, partial: true, ...(reason ? { reason } : {}) }) });
|
|
454
|
+
}
|
|
455
|
+
/** What a window stage's call returns: the windows that closed, kept for the page to take, and the stage's state. */
|
|
456
|
+
function windowResult(wire, st, emitted) {
|
|
457
|
+
const keys = [];
|
|
458
|
+
const bounds = [];
|
|
459
|
+
for (const { item, bound } of emitted) {
|
|
460
|
+
const k = `${wire}@${++wireSeq}`;
|
|
461
|
+
wireItems.set(k, item);
|
|
462
|
+
keys.push(k);
|
|
463
|
+
bounds.push(bound);
|
|
464
|
+
}
|
|
465
|
+
return { keys, bounds, ...st.push.state(), next: st.push.deadline() };
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function operatorFn(text, at, op, what = "expr") {
|
|
469
|
+
try {
|
|
470
|
+
return new Function("x", "value", "event", "acc", `"use strict";\nreturn (${text}\n);`);
|
|
471
|
+
} catch (error) {
|
|
472
|
+
throw new SyntaxError(`operator ${at + 1} (${op}): its ${what === "expr" ? "expression" : what} doesn't parse: ${error.message}`);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** A dedupe key: a JSON-like value by its content, anything else by itself. */
|
|
477
|
+
function dedupeKey(v) {
|
|
478
|
+
if (v === null || typeof v !== "object") return v;
|
|
479
|
+
const fp = fingerprintOf(v);
|
|
480
|
+
return "hash" in fp ? `#${fp.hash}` : v;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** One operator as transducers (`kind` "event": items are events, and the operator works on their payloads). */
|
|
484
|
+
function transducersFor(o, at, kind) {
|
|
485
|
+
const T = transducers;
|
|
486
|
+
const lift = kind === "event";
|
|
487
|
+
const P = (item) => (lift ? item.payload : item);
|
|
488
|
+
const withPayload = (item, payload) => (lift ? Object.freeze({ ...item, payload }) : payload);
|
|
489
|
+
const fn = o.expr === undefined ? null : operatorFn(o.expr, at, o.op);
|
|
490
|
+
const gone = (item) => lift && tombstones.has(item);
|
|
491
|
+
const call = (item, acc) => {
|
|
492
|
+
try {
|
|
493
|
+
return fn(P(item), P(item), lift ? item : undefined, acc);
|
|
494
|
+
} catch (error) {
|
|
495
|
+
throw new Error(`operator ${at + 1} (${o.op}): ${error?.message ?? error}`);
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
const arrays = (list) => (lift ? withPayload(list.at(-1), list.map(P)) : list);
|
|
499
|
+
switch (o.op) {
|
|
500
|
+
case "filter":
|
|
501
|
+
return [T.filter((item) => gone(item) || Boolean(call(item)))]; // a tombstone keeps its place
|
|
502
|
+
case "reject":
|
|
503
|
+
return [T.reject((item) => !gone(item) && Boolean(call(item)))];
|
|
504
|
+
case "map":
|
|
505
|
+
return [T.map((item) => (gone(item) ? item : withPayload(item, call(item))))];
|
|
506
|
+
case "dedupe":
|
|
507
|
+
return [T.dedupe((item) => (gone(item) ? item : dedupeKey(fn ? call(item) : P(item))))]; // its own key: never an equal one
|
|
508
|
+
case "take":
|
|
509
|
+
return [T.take(o.n)];
|
|
510
|
+
case "drop":
|
|
511
|
+
return [T.drop(o.n)];
|
|
512
|
+
case "group":
|
|
513
|
+
return [T.group(o.n), T.map(arrays)];
|
|
514
|
+
case "partitionBy": {
|
|
515
|
+
let last; // a tombstone joins the run it arrived in
|
|
516
|
+
return [T.partitionBy((item) => (gone(item) ? last : (last = dedupeKey(call(item))))), T.map(arrays)];
|
|
517
|
+
}
|
|
518
|
+
case "accumulate": {
|
|
519
|
+
let initial = 0;
|
|
520
|
+
if (o.initial !== undefined) {
|
|
521
|
+
try {
|
|
522
|
+
initial = operatorFn(o.initial, at, o.op, "initial value")();
|
|
523
|
+
} catch (error) {
|
|
524
|
+
throw new Error(`operator ${at + 1} (accumulate): its initial value: ${error?.message ?? error}`);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return [T.accumulate((pair, item) => ({ acc: gone(item) ? pair.acc : call(item, pair.acc), item }), { acc: initial }), T.map((pair) => withPayload(pair.item, pair.acc))];
|
|
528
|
+
}
|
|
529
|
+
default:
|
|
530
|
+
throw new Error(`operator ${at + 1}: "${o.op}" doesn't run in the sandbox`);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Windows on wires (src/model/wire-ops.js `window`, `slide`, `last`, `session`): the values of a window are held here,
|
|
535
|
+
// as items (so they are swept like a group's when a take is removed: `drop` and `held` below), and the page, which owns
|
|
536
|
+
// the transport's clock, says what time it is with every call and sets the timer for the deadline it is given back.
|
|
537
|
+
// A machine is one wire stage (or one finite batch's): add(item, now) and tick(now) return what closed, as
|
|
538
|
+
// [{ item, bound: { opened, closed, count, reason } }]; complete(now) closes what is open when the batch ends.
|
|
539
|
+
const WINDOW_OPS = new Set(["window", "slide", "last", "session"]);
|
|
540
|
+
const WINDOW_MAX_ITEMS = 10000; // as src/model/wire-ops.js MAX_WINDOW_ITEMS: a window that fills up closes early
|
|
541
|
+
|
|
542
|
+
/** A window's aggregate (`sum(x)`) as a function of its items; the expression is compiled here, like any operator's. */
|
|
543
|
+
function windowAggregate(text, at, op, lift) {
|
|
544
|
+
const m = /^(sum|avg|min|max|count|first|last)\(([\s\S]*)\)$/.exec(String(text).trim());
|
|
545
|
+
if (!m) throw new Error(`operator ${at + 1} (${op}): its aggregate doesn't parse`);
|
|
546
|
+
const expr = m[2].trim();
|
|
547
|
+
const fn = operatorFn(expr || "x", at, op, "aggregate");
|
|
548
|
+
const read = (item) => {
|
|
549
|
+
try {
|
|
550
|
+
return fn(lift ? item.payload : item, lift ? item.payload : item, lift ? item : undefined, undefined);
|
|
551
|
+
} catch (error) {
|
|
552
|
+
throw new Error(`operator ${at + 1} (${op}): ${error?.message ?? error}`);
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
const numbers = (list) => list.map(read).map(Number).filter(Number.isFinite);
|
|
556
|
+
switch (m[1]) {
|
|
557
|
+
case "sum":
|
|
558
|
+
return (list) => numbers(list).reduce((a, b) => a + b, 0);
|
|
559
|
+
case "avg":
|
|
560
|
+
return (list) => {
|
|
561
|
+
const n = numbers(list);
|
|
562
|
+
return n.length ? n.reduce((a, b) => a + b, 0) / n.length : null;
|
|
563
|
+
};
|
|
564
|
+
case "min":
|
|
565
|
+
return (list) => {
|
|
566
|
+
const n = numbers(list);
|
|
567
|
+
return n.length ? Math.min(...n) : null;
|
|
568
|
+
};
|
|
569
|
+
case "max":
|
|
570
|
+
return (list) => {
|
|
571
|
+
const n = numbers(list);
|
|
572
|
+
return n.length ? Math.max(...n) : null;
|
|
573
|
+
};
|
|
574
|
+
case "count":
|
|
575
|
+
return (list) => (expr ? list.filter((item) => Boolean(read(item))).length : list.length);
|
|
576
|
+
case "first":
|
|
577
|
+
return (list) => (list.length ? read(list[0]) : null);
|
|
578
|
+
default:
|
|
579
|
+
return (list) => (list.length ? read(list.at(-1)) : null);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** The state of one window stage (and batch): see above. `push` of a wireStates slot is one of these (drop and held are what removals sweep). */
|
|
584
|
+
function windowMachine(o, at, kind) {
|
|
585
|
+
const lift = kind === "event";
|
|
586
|
+
const aggregate = o.aggregate === undefined ? null : windowAggregate(o.aggregate, at, o.op, lift);
|
|
587
|
+
const build = (items) => {
|
|
588
|
+
const last = items.at(-1);
|
|
589
|
+
if (aggregate) return lift ? Object.freeze({ ...last, payload: aggregate(items) }) : aggregate(items);
|
|
590
|
+
return lift ? Object.freeze({ ...last, payload: items.map((item) => item.payload) }) : items;
|
|
591
|
+
};
|
|
592
|
+
const gone = (item) => lift && tombstones.has(item);
|
|
593
|
+
let entries = []; // { item, at }: what the window holds, oldest first
|
|
594
|
+
let due = null; // when a time window or session closes
|
|
595
|
+
let sinceEmit = 0; // sliding windows: arrivals since the last one went
|
|
596
|
+
const close = (list, reason, closed) => ({ item: build(list.map((e) => e.item)), bound: { opened: list[0].at, closed, count: list.length, reason } });
|
|
597
|
+
/** A time window (or session) whose deadline has come closes at the deadline. */
|
|
598
|
+
const expire = (now, out) => {
|
|
599
|
+
if (due !== null && now >= due && entries.length) {
|
|
600
|
+
out.push(close(entries, o.op === "session" ? "idle" : "time", due));
|
|
601
|
+
entries = [];
|
|
602
|
+
}
|
|
603
|
+
if (!entries.length) due = null;
|
|
604
|
+
};
|
|
605
|
+
return {
|
|
606
|
+
add(item, now) {
|
|
607
|
+
const out = [];
|
|
608
|
+
if (gone(item)) return out; // a removed take's tombstone: nothing to hold
|
|
609
|
+
expire(now, out);
|
|
610
|
+
entries.push({ item, at: now });
|
|
611
|
+
if (o.op === "window" && o.n !== undefined) {
|
|
612
|
+
if (entries.length >= o.n) (out.push(close(entries, "count", now)), (entries = []));
|
|
613
|
+
} else if (o.op === "window") {
|
|
614
|
+
if (due === null) due = now + o.ms;
|
|
615
|
+
} else if (o.op === "session") {
|
|
616
|
+
due = now + o.ms;
|
|
617
|
+
} else if (o.op === "slide") {
|
|
618
|
+
if (entries.length > o.n) entries.shift();
|
|
619
|
+
sinceEmit++;
|
|
620
|
+
if (entries.length >= o.n && sinceEmit >= (o.every ?? 1)) (out.push(close(entries, "slide", now)), (sinceEmit = 0));
|
|
621
|
+
} else if (o.op === "last") {
|
|
622
|
+
entries = entries.filter((e) => e.at > now - o.ms);
|
|
623
|
+
out.push(close(entries, "slide", now));
|
|
624
|
+
}
|
|
625
|
+
if (entries.length >= WINDOW_MAX_ITEMS && (o.op === "window" || o.op === "session")) (out.push(close(entries, "full", now)), (entries = []), (due = null));
|
|
626
|
+
else if (entries.length > WINDOW_MAX_ITEMS) entries.shift();
|
|
627
|
+
return out;
|
|
628
|
+
},
|
|
629
|
+
tick(now) {
|
|
630
|
+
const out = [];
|
|
631
|
+
expire(now, out);
|
|
632
|
+
return out;
|
|
633
|
+
},
|
|
634
|
+
/** The batch ended (or the window is flushed): what is open goes. */
|
|
635
|
+
complete(now) {
|
|
636
|
+
const out = [];
|
|
637
|
+
if (o.op === "slide") {
|
|
638
|
+
if (sinceEmit > 0 && entries.length) out.push(close(entries, "end", now));
|
|
639
|
+
} else if (o.op !== "last" && entries.length) out.push(close(entries, "end", now));
|
|
640
|
+
entries = [];
|
|
641
|
+
due = null;
|
|
642
|
+
sinceEmit = 0;
|
|
643
|
+
return out;
|
|
644
|
+
},
|
|
645
|
+
deadline: () => due,
|
|
646
|
+
state: () => ({ held: entries.length, since: entries.length ? entries[0].at : null }),
|
|
647
|
+
drop(pred) {
|
|
648
|
+
entries = entries.filter((e) => !pred(e.item));
|
|
649
|
+
sinceEmit = Math.min(sinceEmit, entries.length);
|
|
650
|
+
if (!entries.length) due = null;
|
|
651
|
+
},
|
|
652
|
+
held: () => entries.map((e) => e.item),
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
|