@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,600 @@
|
|
|
1
|
+
// Comparing two images: the pure part. Which values can be
|
|
2
|
+
// compared, what to say when they can't (a missing value, another kind of
|
|
3
|
+
// media, two sizes), the pixel difference itself, the shared zoom/pan of a
|
|
4
|
+
// compare view, and the bounded set of reference images a person keeps on
|
|
5
|
+
// purpose. No DOM, so it runs under node --test and in the diff Worker
|
|
6
|
+
// (src/media/compare-worker.js). Decoding is src/media/compare-job.js.
|
|
7
|
+
//
|
|
8
|
+
// Nothing here ever resamples: two images of different sizes are shown at
|
|
9
|
+
// their own pixels, and a difference needs equal sizes (or an explicit choice
|
|
10
|
+
// to compare their shared top-left region, which crops and never scales).
|
|
11
|
+
|
|
12
|
+
/** The most pixels a difference decodes (4096 × 2160). Bigger images still show side by side and as a wipe. */
|
|
13
|
+
export const MAX_COMPARE_PIXELS = 8_847_360;
|
|
14
|
+
/** The most bytes of one image the difference reads (the decoded copy is far bigger). */
|
|
15
|
+
export const MAX_COMPARE_BYTES = 64 * 1024 * 1024;
|
|
16
|
+
/** Reference images: one per pane, each up to `each` bytes, at most `count` of them and `total` bytes together, in memory only. */
|
|
17
|
+
export const REFERENCE_LIMITS = { each: 16 * 1024 * 1024, total: 48 * 1024 * 1024, count: 8 };
|
|
18
|
+
export const MODES = ["side", "wipe", "diff"];
|
|
19
|
+
export const ZOOM = { min: 0.05, max: 64 };
|
|
20
|
+
|
|
21
|
+
const kb = (n) => (n < 1024 ** 2 ? `${Math.round(n / 1024)} KB` : `${(n / 1024 ** 2).toFixed(1)} MB`);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Why `value` can't be one side of a comparison, or null when it can. `what`
|
|
25
|
+
* names it for the message ("A", "the reference").
|
|
26
|
+
*/
|
|
27
|
+
export function imageProblem(value, what = "it") {
|
|
28
|
+
if (value === undefined || value === null) return `${what} has no value`;
|
|
29
|
+
if (typeof value !== "object" || typeof value.$media !== "string") return `${what} is not media`;
|
|
30
|
+
if (value.$media !== "image") return `${what} is ${value.$media === "audio" ? "sound" : value.$media === "file" ? "a file" : "video"}, not an image (only images compare)`;
|
|
31
|
+
if (!value.blob || typeof value.blob.size !== "number") return `${what} has no image bytes`;
|
|
32
|
+
if (value.blob.size === 0) return `${what} is an empty image`;
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** "640×480". */
|
|
37
|
+
export const sizeText = (s) => (s?.w && s?.h ? `${s.w}×${s.h}` : "unknown size");
|
|
38
|
+
|
|
39
|
+
/** The two sizes and whether they match. `a` and `b` are `{ w, h }`, or undefined when not known. */
|
|
40
|
+
export function sizesOf(a, b) {
|
|
41
|
+
const known = Boolean(a?.w && a?.h && b?.w && b?.h);
|
|
42
|
+
const equal = known && a.w === b.w && a.h === b.h;
|
|
43
|
+
return { known, equal, a, b, common: known ? { w: Math.min(a.w, b.w), h: Math.min(a.h, b.h) } : undefined };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** One sentence about two sizes, or null when they match. */
|
|
47
|
+
export function sizeNote(a, b) {
|
|
48
|
+
const s = sizesOf(a, b);
|
|
49
|
+
if (!s.known || s.equal) return null;
|
|
50
|
+
return `A is ${sizeText(a)} and B is ${sizeText(b)}. They are shown at their own pixels from the top-left corner, never scaled.`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Crop RGBA pixels (`sw`×`sh`) to their top-left `w`×`h`. Never scales: a
|
|
55
|
+
* region outside the image is an error.
|
|
56
|
+
*/
|
|
57
|
+
export function cropRGBA(src, sw, sh, w, h) {
|
|
58
|
+
if (w > sw || h > sh || w < 1 || h < 1) throw new RangeError(`can't crop ${sw}×${sh} to ${w}×${h}`);
|
|
59
|
+
if (w === sw && h === sh) return src;
|
|
60
|
+
const out = new Uint8ClampedArray(w * h * 4);
|
|
61
|
+
for (let y = 0; y < h; y++) out.set(src.subarray(y * sw * 4, y * sw * 4 + w * 4), y * w * 4);
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The difference of two equal-sized RGBA images. A pixel differs when any of
|
|
67
|
+
* its four channels is more than `tolerance` (0 to 255) apart. The output
|
|
68
|
+
* shows equal pixels as a dark gray copy of A and differing ones from yellow
|
|
69
|
+
* (a small difference) to red (a large one).
|
|
70
|
+
* @returns {{ out: Uint8ClampedArray, width, height, total, differing, percent, maxDelta, meanDelta, bounds: ({x0,y0,x1,y1}|null), identical: boolean }}
|
|
71
|
+
*/
|
|
72
|
+
export function diffPixels(a, b, width, height, { tolerance = 0 } = {}) {
|
|
73
|
+
const total = width * height;
|
|
74
|
+
if (a.length !== total * 4 || b.length !== total * 4) throw new RangeError(`pixel arrays must be ${width}×${height}×4 bytes`);
|
|
75
|
+
const t = Math.max(0, Math.min(255, Math.floor(tolerance) || 0));
|
|
76
|
+
const out = new Uint8ClampedArray(total * 4);
|
|
77
|
+
let differing = 0;
|
|
78
|
+
let maxDelta = 0;
|
|
79
|
+
let sum = 0;
|
|
80
|
+
let x0 = width;
|
|
81
|
+
let y0 = height;
|
|
82
|
+
let x1 = -1;
|
|
83
|
+
let y1 = -1;
|
|
84
|
+
for (let p = 0, i = 0; p < total; p++, i += 4) {
|
|
85
|
+
const d = Math.max(Math.abs(a[i] - b[i]), Math.abs(a[i + 1] - b[i + 1]), Math.abs(a[i + 2] - b[i + 2]), Math.abs(a[i + 3] - b[i + 3]));
|
|
86
|
+
if (d > t) {
|
|
87
|
+
differing++;
|
|
88
|
+
sum += d;
|
|
89
|
+
if (d > maxDelta) maxDelta = d;
|
|
90
|
+
const x = p % width;
|
|
91
|
+
const y = (p - x) / width;
|
|
92
|
+
if (x < x0) x0 = x;
|
|
93
|
+
if (x > x1) x1 = x;
|
|
94
|
+
if (y < y0) y0 = y;
|
|
95
|
+
if (y > y1) y1 = y;
|
|
96
|
+
out[i] = 255;
|
|
97
|
+
out[i + 1] = 255 - d;
|
|
98
|
+
out[i + 2] = 0;
|
|
99
|
+
out[i + 3] = 255;
|
|
100
|
+
} else {
|
|
101
|
+
const g = Math.round((0.299 * a[i] + 0.587 * a[i + 1] + 0.114 * a[i + 2]) * 0.25);
|
|
102
|
+
out[i] = out[i + 1] = out[i + 2] = g;
|
|
103
|
+
out[i + 3] = 255;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
out,
|
|
108
|
+
width,
|
|
109
|
+
height,
|
|
110
|
+
total,
|
|
111
|
+
differing,
|
|
112
|
+
percent: total ? (differing / total) * 100 : 0,
|
|
113
|
+
maxDelta,
|
|
114
|
+
meanDelta: differing ? sum / differing : 0,
|
|
115
|
+
bounds: differing ? { x0, y0, x1, y1 } : null,
|
|
116
|
+
identical: differing === 0,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** A sentence for the stats of diffPixels (the numbers, not the picture). */
|
|
121
|
+
export function diffSummary(r, tolerance = 0) {
|
|
122
|
+
if (r.identical) return tolerance ? `No pixel differs by more than ${tolerance} (of 255), in ${r.total.toLocaleString("en-US")} pixels.` : `The images are identical: all ${r.total.toLocaleString("en-US")} pixels match.`;
|
|
123
|
+
const pct = r.percent >= 10 ? r.percent.toFixed(1) : r.percent >= 0.01 ? r.percent.toFixed(2) : "<0.01";
|
|
124
|
+
const { x0, y0, x1, y1 } = r.bounds;
|
|
125
|
+
return `${r.differing.toLocaleString("en-US")} of ${r.total.toLocaleString("en-US")} pixels differ (${pct}%), by up to ${r.maxDelta} (mean ${r.meanDelta.toFixed(1)}) of 255, within x ${x0}–${x1}, y ${y0}–${y1}.`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** What to tell the person when a difference was refused (the job's `reason`). */
|
|
129
|
+
export function refusalText(r) {
|
|
130
|
+
switch (r.reason) {
|
|
131
|
+
case "size": return `The sizes differ (A ${sizeText(r.a)}, B ${sizeText(r.b)}), and a difference never scales one to match the other. Compare their shared ${sizeText({ w: Math.min(r.a.w, r.b.w), h: Math.min(r.a.h, r.b.h) })} top-left region instead, or use side by side or the wipe.`;
|
|
132
|
+
case "too-large": return `${r.which} is ${sizeText(r.size)}: over the ${MAX_COMPARE_PIXELS.toLocaleString("en-US")}-pixel limit of a difference (4096×2160). Side by side and the wipe still work.`;
|
|
133
|
+
case "bytes": return `${r.which} is ${kb(r.bytes)}: over the ${kb(MAX_COMPARE_BYTES)} limit of a difference. Side by side and the wipe still work.`;
|
|
134
|
+
case "decode": return `${r.which} can't be decoded here${r.type ? ` (${r.type})` : ""}, so there is no difference. Side by side and the wipe still show it if this browser can display it.`;
|
|
135
|
+
case "stopped": return "The comparison was stopped.";
|
|
136
|
+
default: return r.message || "The difference failed.";
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ── nested values and source keys ────────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The images and videos in a pane's result (the Worker's `mediaFound`): the
|
|
144
|
+
* value itself, or the ones inside its arrays and objects, each with its path
|
|
145
|
+
* (`[0]`, `.photo`). `kinds` narrows them.
|
|
146
|
+
* @returns {{ path: string, value: object }[]}
|
|
147
|
+
*/
|
|
148
|
+
export function mediaChoices(found, kinds = ["image", "video"]) {
|
|
149
|
+
if (!found?.items) return [];
|
|
150
|
+
return found.items.filter((i) => kinds.includes(i.value?.$media) && i.value.blob && typeof i.value.blob.size === "number");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** A source key's tail for a pane and the path of a value inside its result: `c2` or `c2@[0].photo`. */
|
|
154
|
+
export const sourceKey = (id, path = "") => (path ? `${id}@${path}` : id);
|
|
155
|
+
|
|
156
|
+
/** The reverse. Only the first `@` splits: a path may hold one. */
|
|
157
|
+
export function splitSource(text) {
|
|
158
|
+
const at = text.indexOf("@");
|
|
159
|
+
return at < 0 ? { id: text, path: "" } : { id: text.slice(0, at), path: text.slice(at + 1) };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A pixel as the readout shows it: "rgba(12, 34, 56, 255) #0c2238" (the hex has the alpha only when it is not opaque). */
|
|
163
|
+
export function formatRGBA(p) {
|
|
164
|
+
if (!p) return "—";
|
|
165
|
+
const [r, g, b, a] = p;
|
|
166
|
+
const hex = (n) => n.toString(16).padStart(2, "0");
|
|
167
|
+
return `rgba(${r}, ${g}, ${b}, ${a}) #${hex(r)}${hex(g)}${hex(b)}${a === 255 ? "" : hex(a)}`;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A video time kept inside the video (0 to its length; an unknown length leaves it alone). */
|
|
171
|
+
export function frameTime(t, duration) {
|
|
172
|
+
const v = Number.isFinite(t) ? Math.max(0, t) : 0;
|
|
173
|
+
return Number.isFinite(duration) ? Math.min(v, Math.max(0, duration)) : v;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ── scopes ───────────────────────────────────────────────────────────────────
|
|
177
|
+
|
|
178
|
+
// ── playing two videos in step ───────────────────────────────────────────────
|
|
179
|
+
|
|
180
|
+
/** One frame, in seconds, when a video's own frame rate is not known (30 frames a second). */
|
|
181
|
+
export const FRAME_SECONDS = 1 / 30;
|
|
182
|
+
|
|
183
|
+
/** A time moved onto the frame grid (`fps` frames a second), never below 0. */
|
|
184
|
+
export function snapToFrame(t, fps = 30) {
|
|
185
|
+
if (!Number.isFinite(t) || t <= 0) return 0;
|
|
186
|
+
return Math.round(t * fps) / fps;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Where a shared clock stands: `from` seconds into the videos when playing
|
|
191
|
+
* began, `elapsedMs` later, at `rate` times normal speed. At the end of the
|
|
192
|
+
* longer video it stops (`ended`) or, with `loop`, starts over. An unknown
|
|
193
|
+
* length (`duration` 0) plays on. A rate of 0 is paused.
|
|
194
|
+
* @returns {{ time: number, ended: boolean }}
|
|
195
|
+
*/
|
|
196
|
+
export function playbackTime({ from = 0, elapsedMs = 0, rate = 1, duration = 0, loop = false }) {
|
|
197
|
+
const moved = rate > 0 && elapsedMs > 0 ? (elapsedMs / 1000) * rate : 0;
|
|
198
|
+
const time = Math.max(0, from) + moved;
|
|
199
|
+
if (!(duration > 0) || time < duration) return { time, ended: false };
|
|
200
|
+
return loop ? { time: time % duration, ended: false } : { time: duration, ended: true };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Are all `times` within one `frame` of each other? */
|
|
204
|
+
export function inLockstep(times, frame = FRAME_SECONDS) {
|
|
205
|
+
if (times.some((t) => !Number.isFinite(t))) return false;
|
|
206
|
+
if (times.length < 2) return true;
|
|
207
|
+
return Math.max(...times) - Math.min(...times) <= frame + 1e-9;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// ── scopes ───────────────────────────────────────────────────────────────────
|
|
211
|
+
|
|
212
|
+
/** The colour spaces a scope can read an image in. */
|
|
213
|
+
export const SCOPE_SPACES = ["srgb", "display-p3"];
|
|
214
|
+
const SPACE_LABEL = { srgb: "sRGB", "display-p3": "Display-P3", rec2020: "Rec. 2020", "adobe-rgb": "Adobe RGB" };
|
|
215
|
+
/** The name of a scope space (anything else is the default, sRGB). */
|
|
216
|
+
export const spaceLabel = (space) => (SCOPE_SPACES.includes(space) ? SPACE_LABEL[space] : SPACE_LABEL.srgb);
|
|
217
|
+
/** The name of any space a file can claim to be in. */
|
|
218
|
+
const hintLabel = (space) => SPACE_LABEL[space] ?? "an unknown space";
|
|
219
|
+
|
|
220
|
+
/** Luma weights per space, applied to the gamma-encoded values (Rec. 709 for sRGB; the Display-P3 primaries' own for P3). */
|
|
221
|
+
const LUMA = { srgb: [0.2126, 0.7152, 0.0722], "display-p3": [0.229, 0.6917, 0.0793] };
|
|
222
|
+
|
|
223
|
+
const EOTF = Float64Array.from({ length: 256 }, (_, v) => {
|
|
224
|
+
const c = v / 255;
|
|
225
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
/** Display-P3 values (0 to 255, the sRGB transfer curve) as linear-light sRGB: a channel below 0 or above 1 is a colour sRGB can't show. */
|
|
229
|
+
export function displayP3ToLinearSrgb(r, g, b) {
|
|
230
|
+
const lr = EOTF[r];
|
|
231
|
+
const lg = EOTF[g];
|
|
232
|
+
const lb = EOTF[b];
|
|
233
|
+
return [1.2249401 * lr - 0.2249404 * lg, -0.0420569 * lr + 1.0420571 * lg, -0.0196376 * lr - 0.0786361 * lg + 1.0982735 * lb];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Is a Display-P3 colour outside the sRGB gamut? */
|
|
237
|
+
export function outsideSrgb(r, g, b) {
|
|
238
|
+
if (r === g && g === b) return false; // greys are the same in both
|
|
239
|
+
return displayP3ToLinearSrgb(r, g, b).some((c) => c < -0.0005 || c > 1.0005);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Histograms of RGBA pixels as they were read, in colour space `space`
|
|
244
|
+
* ("srgb" or "display-p3"; nothing is converted here, the reader asked the
|
|
245
|
+
* browser for that space): 256 bins each for red, green, blue and luma
|
|
246
|
+
* (gamma-encoded values weighed with the space's own luma weights), with each
|
|
247
|
+
* channel's lowest, highest and mean value. Alpha is not binned and every
|
|
248
|
+
* pixel counts. In Display-P3, `outside` counts the pixels sRGB cannot show.
|
|
249
|
+
*/
|
|
250
|
+
export function histograms(rgba, space = "srgb") {
|
|
251
|
+
const bins = () => new Uint32Array(256);
|
|
252
|
+
const wide = space === "display-p3";
|
|
253
|
+
const [wr, wg, wb] = LUMA[wide ? "display-p3" : "srgb"];
|
|
254
|
+
const out = { space: wide ? "display-p3" : "srgb", r: bins(), g: bins(), b: bins(), luma: bins(), count: rgba.length >> 2, min: {}, max: {}, mean: {} };
|
|
255
|
+
const sum = { r: 0, g: 0, b: 0, luma: 0 };
|
|
256
|
+
const lo = { r: 255, g: 255, b: 255, luma: 255 };
|
|
257
|
+
const hi = { r: 0, g: 0, b: 0, luma: 0 };
|
|
258
|
+
let outside = 0;
|
|
259
|
+
for (let i = 0; i < rgba.length; i += 4) {
|
|
260
|
+
const v = { r: rgba[i], g: rgba[i + 1], b: rgba[i + 2], luma: Math.round(wr * rgba[i] + wg * rgba[i + 1] + wb * rgba[i + 2]) };
|
|
261
|
+
if (wide && outsideSrgb(v.r, v.g, v.b)) outside++;
|
|
262
|
+
for (const k in v) {
|
|
263
|
+
out[k][v[k]]++;
|
|
264
|
+
sum[k] += v[k];
|
|
265
|
+
if (v[k] < lo[k]) lo[k] = v[k];
|
|
266
|
+
if (v[k] > hi[k]) hi[k] = v[k];
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
for (const k in sum) {
|
|
270
|
+
out.min[k] = out.count ? lo[k] : 0;
|
|
271
|
+
out.max[k] = out.count ? hi[k] : 0;
|
|
272
|
+
out.mean[k] = out.count ? sum[k] / out.count : 0;
|
|
273
|
+
}
|
|
274
|
+
if (wide) {
|
|
275
|
+
out.outside = outside;
|
|
276
|
+
out.outsidePercent = out.count ? (outside / out.count) * 100 : 0;
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const latin1 = (u8) => {
|
|
282
|
+
let s = "";
|
|
283
|
+
for (let i = 0; i < u8.length; i += 0x8000) s += String.fromCharCode(...u8.subarray(i, i + 0x8000));
|
|
284
|
+
return s;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
// Where the red and green primaries sit (the X of each, in the profile's D50 connection space) for the spaces a scope knows about.
|
|
288
|
+
const PRIMARIES = { srgb: [0.4361, 0.3851], "display-p3": [0.5151, 0.292], "adobe-rgb": [0.6097, 0.2053], rec2020: [0.6734, 0.1656] };
|
|
289
|
+
|
|
290
|
+
/** The space an ICC profile's red and green primaries match (within a small distance), read from its rXYZ and gXYZ tags, or undefined. */
|
|
291
|
+
function primariesSpace(u8) {
|
|
292
|
+
if (u8.length < 132) return undefined;
|
|
293
|
+
const view = new DataView(u8.buffer, u8.byteOffset, u8.byteLength);
|
|
294
|
+
const count = view.getUint32(128);
|
|
295
|
+
const xyz = {};
|
|
296
|
+
for (let i = 0; i < Math.min(count, 200); i++) {
|
|
297
|
+
const at = 132 + i * 12;
|
|
298
|
+
if (at + 12 > u8.length) break;
|
|
299
|
+
const sig = latin1(u8.subarray(at, at + 4));
|
|
300
|
+
const offset = view.getUint32(at + 4);
|
|
301
|
+
if ((sig === "rXYZ" || sig === "gXYZ") && offset + 20 <= u8.length) xyz[sig] = view.getInt32(offset + 8) / 65536;
|
|
302
|
+
}
|
|
303
|
+
if (xyz.rXYZ === undefined || xyz.gXYZ === undefined) return undefined;
|
|
304
|
+
let best;
|
|
305
|
+
let bestDistance = 0.03;
|
|
306
|
+
for (const [space, [r, g]] of Object.entries(PRIMARIES)) {
|
|
307
|
+
const d = Math.hypot(xyz.rXYZ - r, xyz.gXYZ - g);
|
|
308
|
+
if (d < bestDistance) [best, bestDistance] = [space, d];
|
|
309
|
+
}
|
|
310
|
+
return best;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** A zlib stream inflated, or undefined when it can't be (a PNG's profile is compressed). */
|
|
314
|
+
async function inflate(u8) {
|
|
315
|
+
try {
|
|
316
|
+
return new Uint8Array(await new Response(new Blob([u8]).stream().pipeThrough(new DecompressionStream("deflate"))).arrayBuffer());
|
|
317
|
+
} catch {
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** The space a profile's text names (an ICC description is ASCII or UTF-16), else the one its primaries match, or undefined. */
|
|
323
|
+
function profileSpace(u8) {
|
|
324
|
+
const ascii = latin1(u8);
|
|
325
|
+
// UTF-16BE text is every second byte, from either alignment
|
|
326
|
+
const wideTexts = [0, 1].map((k) => latin1(u8.filter((_, i) => i % 2 === k)));
|
|
327
|
+
for (const text of [ascii, ...wideTexts]) {
|
|
328
|
+
if (/display\s?p3/i.test(text)) return "display-p3";
|
|
329
|
+
if (/rec\.?\s?2020|bt\.?2020/i.test(text)) return "rec2020";
|
|
330
|
+
if (/adobe rgb/i.test(text)) return "adobe-rgb";
|
|
331
|
+
if (/srgb/i.test(text)) return "srgb";
|
|
332
|
+
}
|
|
333
|
+
return primariesSpace(u8);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* What a picture file says about its colour, from its first bytes (a PNG's
|
|
338
|
+
* cICP chunk or its (compressed) profile, a JPEG's or WebP's embedded profile). Nothing is
|
|
339
|
+
* decoded: this is a hint for choosing a scope's space, not a conversion.
|
|
340
|
+
* @returns {{ space: 'srgb'|'display-p3'|'rec2020'|'adobe-rgb'|undefined, hdr: boolean, source: string|undefined }}
|
|
341
|
+
*/
|
|
342
|
+
export async function colorSpaceHint(u8) {
|
|
343
|
+
const none = { space: undefined, hdr: false, source: undefined };
|
|
344
|
+
if (!u8 || u8.length < 12) return none;
|
|
345
|
+
const ascii = (a, b) => latin1(u8.subarray(a, b));
|
|
346
|
+
const isPng = u8[0] === 137 && ascii(1, 4) === "PNG";
|
|
347
|
+
if (isPng) {
|
|
348
|
+
let found = none;
|
|
349
|
+
for (let p = 8; p + 8 <= u8.length; ) {
|
|
350
|
+
const len = ((u8[p] << 24) | (u8[p + 1] << 16) | (u8[p + 2] << 8) | u8[p + 3]) >>> 0;
|
|
351
|
+
const type = ascii(p + 4, p + 8);
|
|
352
|
+
if (type === "IDAT" || type === "IEND") break;
|
|
353
|
+
const data = u8.subarray(p + 8, Math.min(u8.length, p + 8 + len));
|
|
354
|
+
if (type === "cICP" && data.length >= 4) {
|
|
355
|
+
const primaries = { 1: "srgb", 12: "display-p3", 9: "rec2020" }[data[0]];
|
|
356
|
+
return { space: primaries, hdr: data[1] === 16 || data[1] === 18, source: "cICP" };
|
|
357
|
+
}
|
|
358
|
+
if (type === "iCCP") {
|
|
359
|
+
const nul = data.indexOf(0);
|
|
360
|
+
let space = profileSpace(data.subarray(0, nul < 0 ? data.length : nul));
|
|
361
|
+
if (!space && nul >= 0 && data.length < 1024 * 1024) {
|
|
362
|
+
const profile = await inflate(data.subarray(nul + 2)); // after the name, a NUL and the compression method
|
|
363
|
+
if (profile) space = profileSpace(profile);
|
|
364
|
+
}
|
|
365
|
+
if (space) found = { space, hdr: false, source: "ICC profile" };
|
|
366
|
+
}
|
|
367
|
+
p += 12 + len;
|
|
368
|
+
}
|
|
369
|
+
return found;
|
|
370
|
+
}
|
|
371
|
+
if (u8[0] === 0xff && u8[1] === 0xd8) {
|
|
372
|
+
for (let p = 2; p + 4 <= u8.length && u8[p] === 0xff; ) {
|
|
373
|
+
const marker = u8[p + 1];
|
|
374
|
+
if (marker === 0xda || marker === 0xd9) break;
|
|
375
|
+
const len = (u8[p + 2] << 8) | u8[p + 3];
|
|
376
|
+
if (marker === 0xe2 && ascii(p + 4, p + 15) === "ICC_PROFILE") {
|
|
377
|
+
const space = profileSpace(u8.subarray(p + 18, p + 2 + len));
|
|
378
|
+
if (space) return { space, hdr: false, source: "ICC profile" };
|
|
379
|
+
}
|
|
380
|
+
p += 2 + len;
|
|
381
|
+
}
|
|
382
|
+
return none;
|
|
383
|
+
}
|
|
384
|
+
if (ascii(0, 4) === "RIFF" && ascii(8, 12) === "WEBP") {
|
|
385
|
+
for (let p = 12; p + 8 <= u8.length; ) {
|
|
386
|
+
const len = u8[p + 4] | (u8[p + 5] << 8) | (u8[p + 6] << 16) | (u8[p + 7] << 24);
|
|
387
|
+
if (ascii(p, p + 4) === "ICCP") {
|
|
388
|
+
const space = profileSpace(u8.subarray(p + 8, p + 8 + len));
|
|
389
|
+
return space ? { space, hdr: false, source: "ICC profile" } : none;
|
|
390
|
+
}
|
|
391
|
+
p += 8 + len + (len & 1);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return none;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Which space a scope reads an image in. `choice` is "auto" (follow what the
|
|
399
|
+
* file says), or "srgb" / "display-p3" chosen; `supports` says which this
|
|
400
|
+
* browser's canvas can read. A space the browser can't read falls back to
|
|
401
|
+
* sRGB, with the reason.
|
|
402
|
+
* @returns {{ space: 'srgb'|'display-p3', reason: string }}
|
|
403
|
+
*/
|
|
404
|
+
export function scopeSpaceFor(choice, hint, supports) {
|
|
405
|
+
const p3 = Boolean(supports?.["display-p3"]);
|
|
406
|
+
const cantRead = { space: "srgb", reason: "this browser can't read Display-P3 from a canvas, so the scope reads sRGB" };
|
|
407
|
+
if (choice === "srgb") return { space: "srgb", reason: "chosen: sRGB" };
|
|
408
|
+
if (choice === "display-p3") return p3 ? { space: "display-p3", reason: "chosen: Display-P3" } : cantRead;
|
|
409
|
+
const named = hint?.space;
|
|
410
|
+
if (named === "display-p3") return p3 ? { space: "display-p3", reason: "the image carries a Display-P3 profile" } : cantRead;
|
|
411
|
+
if (named === "rec2020" || named === "adobe-rgb") return p3 ? { space: "display-p3", reason: `the image carries a ${hintLabel(named)} profile, wider than sRGB: read in Display-P3, the widest space a canvas gives here` } : cantRead;
|
|
412
|
+
if (named === "srgb") return { space: "srgb", reason: "the image carries an sRGB profile" };
|
|
413
|
+
return { space: "srgb", reason: "the image names no colour space, so it is read as sRGB" };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** The sentence under the scopes: the space they are in and what is not converted. */
|
|
417
|
+
export function scopeNote({ space, hint }) {
|
|
418
|
+
const wideHint = hint?.space && hint.space !== "srgb";
|
|
419
|
+
let text;
|
|
420
|
+
if (space === "display-p3") {
|
|
421
|
+
text = "Scopes are in Display-P3: the pixels are read in that space and are not converted to sRGB, so colours beyond sRGB keep their values. Luma uses the Display-P3 primaries' weights on the gamma-encoded values.";
|
|
422
|
+
} else {
|
|
423
|
+
text = "Scopes are in sRGB: the pixels as this browser decodes the image into sRGB (it converts an embedded profile), with luma from Rec. 709 weights on the gamma-encoded values.";
|
|
424
|
+
if (wideHint) text += ` This image carries ${hintLabel(hint.space)} colour, so what sRGB can't show is clipped here; read it as Display-P3 to see it.`;
|
|
425
|
+
}
|
|
426
|
+
if (hint?.hdr) text += " It is marked HDR (PQ or HLG): these scopes can't show HDR, they read the standard-range picture the browser draws.";
|
|
427
|
+
return text;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// ── saving and loading references ────────────────────────────────────────────
|
|
431
|
+
|
|
432
|
+
const FILE_FORMAT = "mallory-references";
|
|
433
|
+
const FILE_VERSION = 1;
|
|
434
|
+
|
|
435
|
+
async function toBase64(blob) {
|
|
436
|
+
const bytes = new Uint8Array(await blob.arrayBuffer());
|
|
437
|
+
let text = "";
|
|
438
|
+
for (let i = 0; i < bytes.length; i += 0x8000) text += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
439
|
+
return btoa(text);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* The text of a references file (JSON): every kept image with the pane it was
|
|
444
|
+
* kept for, its name and size. A file the person saves on purpose; it is the
|
|
445
|
+
* only way a reference leaves this tab.
|
|
446
|
+
*/
|
|
447
|
+
export async function encodeReferences(list) {
|
|
448
|
+
const items = [];
|
|
449
|
+
for (const r of list) {
|
|
450
|
+
items.push({ paneId: r.paneId, paneName: r.paneName, name: r.name, type: r.blob.type, width: r.width, height: r.height, at: r.at, data: await toBase64(r.blob) });
|
|
451
|
+
}
|
|
452
|
+
return JSON.stringify({ format: FILE_FORMAT, version: FILE_VERSION, items });
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Read a references file. Whatever is usable comes back in `items` (each with
|
|
457
|
+
* a Blob), and `problems` says in words what was left out or why nothing
|
|
458
|
+
* could be read. The limits are the tab's own (REFERENCE_LIMITS).
|
|
459
|
+
* @returns {Promise<{ items: { paneId, paneName?, name?, blob: Blob, width?, height?, at? }[], problems: string[] }>}
|
|
460
|
+
*/
|
|
461
|
+
export async function decodeReferences(text, limits = REFERENCE_LIMITS) {
|
|
462
|
+
let doc;
|
|
463
|
+
try {
|
|
464
|
+
doc = JSON.parse(text);
|
|
465
|
+
} catch {
|
|
466
|
+
return { items: [], problems: ["That is not a reference images file."] };
|
|
467
|
+
}
|
|
468
|
+
if ((doc?.format !== FILE_FORMAT && doc?.format !== "viaframe-references") || !Array.isArray(doc.items)) return { items: [], problems: ["That is not a reference images file."] };
|
|
469
|
+
if (!(doc.version <= FILE_VERSION)) return { items: [], problems: ["That file was saved by a newer version of the app and can't be read here."] };
|
|
470
|
+
const items = [];
|
|
471
|
+
const problems = [];
|
|
472
|
+
const pos = (n) => (Number.isInteger(n) && n > 0 ? n : undefined);
|
|
473
|
+
for (const [i, it] of doc.items.entries()) {
|
|
474
|
+
const name = typeof it?.name === "string" ? it.name : `image ${i + 1}`;
|
|
475
|
+
if (items.length >= limits.count) {
|
|
476
|
+
problems.push(`Only the first ${limits.count} images were read: a tab keeps up to ${limits.count} references.`);
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
if (typeof it?.paneId !== "string" || !it.paneId) {
|
|
480
|
+
problems.push(`${name}: it names no pane.`);
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
if (typeof it.type !== "string" || !it.type.startsWith("image/")) {
|
|
484
|
+
problems.push(`${name}: ${it.type || "this"} is not an image.`);
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
if (typeof it.data !== "string" || !it.data) {
|
|
488
|
+
problems.push(`${name}: it has no image bytes.`);
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
if (it.data.length * 0.75 > limits.each + 3) {
|
|
492
|
+
problems.push(`${name}: it is over the ${kb(limits.each)} a reference can be.`);
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
let bytes;
|
|
496
|
+
try {
|
|
497
|
+
const raw = atob(it.data);
|
|
498
|
+
bytes = new Uint8Array(raw.length);
|
|
499
|
+
for (let k = 0; k < raw.length; k++) bytes[k] = raw.charCodeAt(k);
|
|
500
|
+
} catch {
|
|
501
|
+
problems.push(`${name}: its image bytes are damaged.`);
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
items.push({ paneId: it.paneId, paneName: typeof it.paneName === "string" ? it.paneName : undefined, name: typeof it.name === "string" ? it.name : undefined, blob: new Blob([bytes], { type: it.type }), width: pos(it.width), height: pos(it.height), at: Number.isFinite(it.at) ? it.at : undefined });
|
|
505
|
+
}
|
|
506
|
+
return { items, problems };
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// ── the shared view: zoom and pan ────────────────────────────────────────────
|
|
510
|
+
|
|
511
|
+
const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
|
512
|
+
|
|
513
|
+
/** A view that fits content `{w,h}` in a box `{w,h}`, centered. Scales up small images at most to `max`. */
|
|
514
|
+
export function fitView(content, box, { max = 8 } = {}) {
|
|
515
|
+
if (!content?.w || !content?.h || !box?.w || !box?.h) return { scale: 1, x: 0, y: 0 };
|
|
516
|
+
const scale = clamp(Math.min(box.w / content.w, box.h / content.h), ZOOM.min, max);
|
|
517
|
+
return { scale, x: (box.w - content.w * scale) / 2, y: (box.h - content.h * scale) / 2 };
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/** Zoom by `factor` keeping the content under the box point (`px`,`py`) where it is. */
|
|
521
|
+
export function zoomAt(view, factor, px, py) {
|
|
522
|
+
const scale = clamp(view.scale * factor, ZOOM.min, ZOOM.max);
|
|
523
|
+
const k = scale / view.scale;
|
|
524
|
+
return { scale, x: px - (px - view.x) * k, y: py - (py - view.y) * k };
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export const panBy = (view, dx, dy) => ({ ...view, x: view.x + dx, y: view.y + dy });
|
|
528
|
+
|
|
529
|
+
/** The image pixel under a box point, `{ x, y }` (integers, may be outside the image). */
|
|
530
|
+
export function pixelAt(view, px, py) {
|
|
531
|
+
return { x: Math.floor((px - view.x) / view.scale), y: Math.floor((py - view.y) / view.scale) };
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** Is a pixel inside an image of size `s` (`{ w, h }`)? */
|
|
535
|
+
export const inside = (s, p) => Boolean(s) && p.x >= 0 && p.y >= 0 && p.x < s.w && p.y < s.h;
|
|
536
|
+
|
|
537
|
+
// ── reference images ─────────────────────────────────────────────────────────
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* The reference images a person keeps on purpose: one per pane, in memory
|
|
541
|
+
* only (never in the canvas, its file, its link, version history or the run
|
|
542
|
+
* log), and bounded so keeping one can't grow without limit. Keeping a
|
|
543
|
+
* reference that doesn't fit is refused with the reason, never by evicting
|
|
544
|
+
* another one silently.
|
|
545
|
+
*/
|
|
546
|
+
export class ReferenceSet {
|
|
547
|
+
constructor(limits = REFERENCE_LIMITS) {
|
|
548
|
+
this.limits = limits;
|
|
549
|
+
this.items = new Map(); // pane id -> { blob, name, paneName, width?, height?, at, size }
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
get bytes() {
|
|
553
|
+
let n = 0;
|
|
554
|
+
for (const r of this.items.values()) n += r.size;
|
|
555
|
+
return n;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
get size() {
|
|
559
|
+
return this.items.size;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** @returns {{ ok: true, replaced: boolean } | { ok: false, reason: string }} */
|
|
563
|
+
keep(paneId, { blob, name, paneName, width, height }, at = Date.now()) {
|
|
564
|
+
const { each, total, count } = this.limits;
|
|
565
|
+
if (!blob || !(blob.size > 0)) return { ok: false, reason: "There is no image to keep." };
|
|
566
|
+
if (blob.size > each) return { ok: false, reason: `That image is ${kb(blob.size)}: a reference can be up to ${kb(each)}.` };
|
|
567
|
+
const before = this.items.get(paneId);
|
|
568
|
+
if (!before && this.items.size >= count) return { ok: false, reason: `${count} references are kept already: forget one first.` };
|
|
569
|
+
if (this.bytes - (before?.size ?? 0) + blob.size > total) return { ok: false, reason: `References hold up to ${kb(total)} together (${kb(this.bytes)} are kept): forget one first.` };
|
|
570
|
+
this.items.set(paneId, { blob, name, paneName, width, height, at, size: blob.size });
|
|
571
|
+
return { ok: true, replaced: Boolean(before) };
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
get(paneId) {
|
|
575
|
+
return this.items.get(paneId);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
has(paneId) {
|
|
579
|
+
return this.items.has(paneId);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
forget(paneId) {
|
|
583
|
+
return this.items.delete(paneId);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/** Forget the references of panes `exists` no longer knows. Returns the ids dropped. */
|
|
587
|
+
prune(exists) {
|
|
588
|
+
const gone = [...this.items.keys()].filter((id) => !exists(id));
|
|
589
|
+
for (const id of gone) this.items.delete(id);
|
|
590
|
+
return gone;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
clear() {
|
|
594
|
+
this.items.clear();
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
list() {
|
|
598
|
+
return [...this.items].map(([paneId, r]) => ({ paneId, ...r }));
|
|
599
|
+
}
|
|
600
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// The media value: how images, sound, video and files travel between panes.
|
|
2
|
+
//
|
|
3
|
+
// One plain object shape, so it survives every boundary mallory has: it is stored
|
|
4
|
+
// in the sandbox Worker, structured-cloned into DOM/html pane frames, sent to
|
|
5
|
+
// the page for display and export, and returned by the headless runner. A Blob
|
|
6
|
+
// carries the bytes (Blobs clone across Workers, frames and postMessage without
|
|
7
|
+
// copying through JSON).
|
|
8
|
+
//
|
|
9
|
+
// {
|
|
10
|
+
// $media: "image" | "audio" | "video" | "file", // what it is
|
|
11
|
+
// blob: Blob, // the bytes; blob.type is the MIME type
|
|
12
|
+
// name?: string, // a file name, when there is one
|
|
13
|
+
// width?: number, height?: number, // image/video pixels
|
|
14
|
+
// duration?: number, // audio/video seconds
|
|
15
|
+
// sampleRate?: number, channels?: number, // audio
|
|
16
|
+
// provenance?: { pixels, metadata, removed?, kept?, note? }, // what a transform did to the pixels and the metadata
|
|
17
|
+
// }
|
|
18
|
+
//
|
|
19
|
+
// Anything with `$media` set to one of KINDS and a Blob in `blob` is a media
|
|
20
|
+
// value. Producers (the media input pane, the render and audio panes, effects,
|
|
21
|
+
// pane code through the helpers) build this shape; consumers (output display,
|
|
22
|
+
// export, effects) only rely on it. Pure: no DOM, safe in the Worker and Node.
|
|
23
|
+
|
|
24
|
+
export const KINDS = ["image", "audio", "video", "file"];
|
|
25
|
+
|
|
26
|
+
const isBlob = (b) => typeof Blob !== "undefined" && b instanceof Blob;
|
|
27
|
+
|
|
28
|
+
/** Is `value` a media value? */
|
|
29
|
+
export function isMedia(value) {
|
|
30
|
+
return Boolean(value) && typeof value === "object" && KINDS.includes(value.$media) && isBlob(value.blob);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const MIDI_TYPES = new Set(["audio/midi", "audio/mid", "audio/x-midi", "audio/x-mid"]);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The kind a MIME type suggests ("image/png" -> "image"); anything else is a "file". A MIDI file is a "file" though its type says audio:
|
|
37
|
+
* it holds notes, not sound, and no browser plays or decodes it (a media pane would wait on the audio element for nothing); the `smf`
|
|
38
|
+
* helper reads it.
|
|
39
|
+
*/
|
|
40
|
+
export function kindOf(type = "") {
|
|
41
|
+
const essence = String(type).split(";")[0].trim().toLowerCase();
|
|
42
|
+
if (MIDI_TYPES.has(essence)) return "file";
|
|
43
|
+
const top = essence.split("/")[0];
|
|
44
|
+
return KINDS.includes(top) ? top : "file";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Build a media value from a Blob (its kind defaults from blob.type). */
|
|
48
|
+
export function media(blob, { kind = kindOf(blob?.type), ...meta } = {}) {
|
|
49
|
+
if (!isBlob(blob)) throw new TypeError("media(): expected a Blob");
|
|
50
|
+
if (!KINDS.includes(kind)) throw new TypeError(`media(): kind must be one of ${KINDS.join(", ")}`);
|
|
51
|
+
return { $media: kind, blob, ...meta };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A short description for previews and logs: "image/png 640×480, 12.3 kB". */
|
|
55
|
+
export function describeMedia(value) {
|
|
56
|
+
if (!isMedia(value)) return "";
|
|
57
|
+
const size = value.blob.size;
|
|
58
|
+
const bytes = size < 1024 ? `${size} B` : size < 1024 ** 2 ? `${(size / 1024).toFixed(1)} kB` : `${(size / 1024 ** 2).toFixed(1)} MB`;
|
|
59
|
+
const dims = value.width && value.height ? ` ${value.width}×${value.height}` : "";
|
|
60
|
+
const time = value.duration ? ` ${value.duration.toFixed(2)} s` : "";
|
|
61
|
+
return `${value.blob.type || value.$media}${dims}${time}, ${bytes}`;
|
|
62
|
+
}
|