@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,533 @@
|
|
|
1
|
+
// The geometry construction model (src/panes/geometry.js): points, lines, circles, polygons and angles, and the transforms that make
|
|
2
|
+
// new objects from old (reflect, rotate, translate, scale). Pure: no DOM, no clock, no imports, so the page (to draw and hit-test while
|
|
3
|
+
// a point is dragged) and the sandbox Worker (to publish the scene and the measurements, with wired numbers bound to named parameters;
|
|
4
|
+
// it is the sandbox module `mallory:geometry`, src/runtime/worker/98-geometry.js) run the very same code.
|
|
5
|
+
//
|
|
6
|
+
// A construction is a list of objects, each `{ id, type, ...fields }` that names the objects it is made from by id. Those references are a
|
|
7
|
+
// dependency DAG: an object is computed after everything it names, in whatever order the list holds them, and a cycle is refused.
|
|
8
|
+
// Moving a free point recomputes everything downstream. Deleting an object deletes everything that depends on it (removeObject): a
|
|
9
|
+
// dependent with no input has nothing to be, so it is never left behind as an orphan.
|
|
10
|
+
//
|
|
11
|
+
// point { x, y } a free point
|
|
12
|
+
// { on, t } anchored on a line (t: 0 at its first point, 1 at its second) or a circle (t: degrees)
|
|
13
|
+
// line { from, to, segment? } through two points; a segment when `segment` is true (it has a length)
|
|
14
|
+
// circle { center, through } through a point, or { center, radius } (a number or a parameter name)
|
|
15
|
+
// polygon { points: [ids] } its area, perimeter, centroid and whether it crosses itself
|
|
16
|
+
// angle { points: [a, b, c], unit?, mode? } at b: mode "shorter" (default, 0 to 180 degrees), "clickOrder" (the counterclockwise sweep
|
|
17
|
+
// from ray ba to ray bc, 0 to 360) or "reflex" (the other candidate, 180 to 360); unit "deg" or "rad"
|
|
18
|
+
// reflect { of, in } the image of a point, line, circle or polygon in a line
|
|
19
|
+
// rotate { of, about, angle, unit? } about a point; angle is a number or a parameter name; unit "deg" (default) or "rad"
|
|
20
|
+
//
|
|
21
|
+
// The construction also has `unit` ("deg" by default or "rad"): the unit of every angle and rotation that doesn't say its own.
|
|
22
|
+
// A line, circle, polygon or transform may have `color`, one of COLORS (theme tokens, never a raw colour).
|
|
23
|
+
// translate { of, dx, dy } numbers or parameter names
|
|
24
|
+
// scale { of, about, factor } number or parameter name (a circle's radius scales by |factor|)
|
|
25
|
+
//
|
|
26
|
+
// A parameter name is bound by a wired input of that name, else by `params[name]`; with neither, the object that reads it has an `error`
|
|
27
|
+
// (and what depends on it says so). One broken object never stops the others: each shape is `{ kind, ...}` or `{ error }`.
|
|
28
|
+
|
|
29
|
+
export const LIMITS = { objects: 120, polygonPoints: 64, id: 24, params: 24 };
|
|
30
|
+
export const TYPES = ["point", "line", "circle", "polygon", "angle", "reflect", "rotate", "translate", "scale"];
|
|
31
|
+
export const TRANSFORMS = ["reflect", "rotate", "translate", "scale"];
|
|
32
|
+
export const ANGLE_MODES = ["shorter", "clickOrder", "reflex"];
|
|
33
|
+
/** The palette an object can be recoloured with: names of the theme's tokens (the page maps them to `var(--…)`, an export resolves them). */
|
|
34
|
+
export const COLORS = ["accent", "ok", "warn", "err", "series1", "series2", "muted"];
|
|
35
|
+
const UNCOLOURED = ["point", "angle"];
|
|
36
|
+
/** A pointer's hit radius in pixels, and how many times wider it is for a coarse pointer (a finger, not a cursor). */
|
|
37
|
+
export const GRAB_PX = 9;
|
|
38
|
+
export const COARSE_FACTOR = 2.5;
|
|
39
|
+
export const hitRadius = (coarse) => (coarse ? GRAB_PX * COARSE_FACTOR : GRAB_PX);
|
|
40
|
+
/** The unit an angle or a rotation uses: its own, else the construction's, else degrees. */
|
|
41
|
+
export const resolveUnit = (o, g) => (o?.unit === "deg" || o?.unit === "rad" ? o.unit : g?.unit === "rad" ? "rad" : "deg");
|
|
42
|
+
const ID = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
43
|
+
const isNum = (v) => typeof v === "number" && Number.isFinite(v);
|
|
44
|
+
const isNumOrName = (v) => isNum(v) || (typeof v === "string" && ID.test(v) && v.length <= LIMITS.id);
|
|
45
|
+
const EPS = 1e-9;
|
|
46
|
+
|
|
47
|
+
/** The ids an object is made from. */
|
|
48
|
+
export function refsOf(o) {
|
|
49
|
+
switch (o?.type) {
|
|
50
|
+
case "point": return o.on === undefined ? [] : [o.on];
|
|
51
|
+
case "line": return [o.from, o.to];
|
|
52
|
+
case "circle": return [o.center, ...(o.through === undefined ? [] : [o.through])];
|
|
53
|
+
case "polygon": case "angle": return Array.isArray(o.points) ? [...o.points] : [];
|
|
54
|
+
case "reflect": return [o.of, o.in];
|
|
55
|
+
case "rotate": case "scale": return [o.of, o.about];
|
|
56
|
+
case "translate": return [o.of];
|
|
57
|
+
default: return [];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The parameter names an object reads. */
|
|
62
|
+
export function paramsOf(o) {
|
|
63
|
+
const names = [];
|
|
64
|
+
const add = (v) => typeof v === "string" && names.push(v);
|
|
65
|
+
if (o?.type === "circle") add(o.radius);
|
|
66
|
+
if (o?.type === "rotate") add(o.angle);
|
|
67
|
+
if (o?.type === "translate") (add(o.dx), add(o.dy));
|
|
68
|
+
if (o?.type === "scale") add(o.factor);
|
|
69
|
+
return names;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Why a geometry setting can't be saved or run, or null. */
|
|
73
|
+
export function geometryProblem(g) {
|
|
74
|
+
if (!g || typeof g !== "object" || Array.isArray(g)) return "geometry is an object";
|
|
75
|
+
const { objects, params } = g;
|
|
76
|
+
if (g.unit !== undefined && g.unit !== "deg" && g.unit !== "rad") return 'geometry.unit is "deg" or "rad"';
|
|
77
|
+
if (!Array.isArray(objects)) return "geometry.objects is a list";
|
|
78
|
+
if (objects.length > LIMITS.objects) return `a geometry pane has at most ${LIMITS.objects} objects`;
|
|
79
|
+
if (params !== undefined) {
|
|
80
|
+
if (!params || typeof params !== "object" || Array.isArray(params)) return "geometry.params is an object of numbers";
|
|
81
|
+
const keys = Object.keys(params);
|
|
82
|
+
if (keys.length > LIMITS.params) return `geometry.params has at most ${LIMITS.params} names`;
|
|
83
|
+
for (const k of keys) if (!ID.test(k) || k.length > LIMITS.id || !isNum(params[k])) return `geometry.params.${k} is a number under a name of letters, digits and _`;
|
|
84
|
+
}
|
|
85
|
+
const ids = new Set();
|
|
86
|
+
for (const [i, o] of objects.entries()) {
|
|
87
|
+
const at = `object ${i + 1}`;
|
|
88
|
+
if (!o || typeof o !== "object" || Array.isArray(o)) return `${at} is an object`;
|
|
89
|
+
if (typeof o.id !== "string" || !ID.test(o.id) || o.id.length > LIMITS.id) return `${at}: id is letters, digits and _ (not starting with a digit), at most ${LIMITS.id} characters`;
|
|
90
|
+
if (ids.has(o.id)) return `${at}: the id ${o.id} is used twice (ids are unique)`;
|
|
91
|
+
ids.add(o.id);
|
|
92
|
+
if (!TYPES.includes(o.type)) return `${o.id}: type is one of ${TYPES.join(", ")}`;
|
|
93
|
+
const problem = fieldsProblem(o);
|
|
94
|
+
if (problem) return `${o.id}: ${problem}`;
|
|
95
|
+
if (o.color !== undefined && (!COLORS.includes(o.color) || UNCOLOURED.includes(o.type))) return `${o.id}: color is one of ${COLORS.join(", ")}, on a line, circle, polygon or transform (not a ${UNCOLOURED.join(" or an ")})`;
|
|
96
|
+
}
|
|
97
|
+
for (const o of objects) for (const r of refsOf(o)) if (!ids.has(r)) return `${o.id} names ${r}, which is not an object`;
|
|
98
|
+
const cycle = findCycle(objects);
|
|
99
|
+
if (cycle) return `a cycle: ${cycle.join(" -> ")} (an object can't depend on itself)`;
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function fieldsProblem(o) {
|
|
104
|
+
const id = (k) => typeof o[k] === "string" && ID.test(o[k]);
|
|
105
|
+
const ids = (k, min, max) => Array.isArray(o[k]) && o[k].length >= min && o[k].length <= max && o[k].every((x) => typeof x === "string" && ID.test(x));
|
|
106
|
+
const num = (k) => isNumOrName(o[k]);
|
|
107
|
+
switch (o.type) {
|
|
108
|
+
case "point":
|
|
109
|
+
if (o.on !== undefined) return id("on") && isNum(o.t) ? null : "an anchored point has `on` (an object id) and `t` (a number)";
|
|
110
|
+
return isNum(o.x) && isNum(o.y) ? null : "x and y are numbers";
|
|
111
|
+
case "line":
|
|
112
|
+
if (!id("from") || !id("to")) return "from and to are point ids";
|
|
113
|
+
return o.segment === undefined || typeof o.segment === "boolean" ? null : "segment is true or false";
|
|
114
|
+
case "circle":
|
|
115
|
+
if (!id("center")) return "center is a point id";
|
|
116
|
+
if ((o.through === undefined) === (o.radius === undefined)) return "give through (a point id) or radius, not both";
|
|
117
|
+
return o.through !== undefined ? (id("through") ? null : "through is a point id") : num("radius") ? null : "radius is a number or a parameter name";
|
|
118
|
+
case "polygon":
|
|
119
|
+
return ids("points", 3, LIMITS.polygonPoints) ? null : `points is a list of 3 to ${LIMITS.polygonPoints} point ids`;
|
|
120
|
+
case "angle":
|
|
121
|
+
if (!ids("points", 3, 3)) return "points is three point ids (the vertex in the middle)";
|
|
122
|
+
if (o.mode !== undefined && !ANGLE_MODES.includes(o.mode)) return `mode is one of ${ANGLE_MODES.join(", ")}`;
|
|
123
|
+
return o.unit === undefined || o.unit === "deg" || o.unit === "rad" ? null : 'unit is "deg" or "rad"';
|
|
124
|
+
case "reflect": return id("of") && id("in") ? null : "of and in are object ids";
|
|
125
|
+
case "rotate":
|
|
126
|
+
if (!id("of") || !id("about")) return "of and about are object ids";
|
|
127
|
+
if (!num("angle")) return "angle is a number or a parameter name";
|
|
128
|
+
return o.unit === undefined || o.unit === "deg" || o.unit === "rad" ? null : 'unit is "deg" or "rad"';
|
|
129
|
+
case "translate": return id("of") && num("dx") && num("dy") ? null : "of is an object id; dx and dy are numbers or parameter names";
|
|
130
|
+
case "scale": return id("of") && id("about") && num("factor") ? null : "of and about are object ids; factor is a number or a parameter name";
|
|
131
|
+
default: return "unknown type";
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function findCycle(objects) {
|
|
136
|
+
const by = new Map(objects.map((o) => [o.id, o]));
|
|
137
|
+
const state = new Map();
|
|
138
|
+
const stack = [];
|
|
139
|
+
const visit = (id) => {
|
|
140
|
+
if (state.get(id) === 2) return null;
|
|
141
|
+
if (state.get(id) === 1) return [...stack.slice(stack.indexOf(id)), id];
|
|
142
|
+
state.set(id, 1);
|
|
143
|
+
stack.push(id);
|
|
144
|
+
for (const r of refsOf(by.get(id))) {
|
|
145
|
+
const found = by.has(r) ? visit(r) : null;
|
|
146
|
+
if (found) return found;
|
|
147
|
+
}
|
|
148
|
+
stack.pop();
|
|
149
|
+
state.set(id, 2);
|
|
150
|
+
return null;
|
|
151
|
+
};
|
|
152
|
+
for (const o of objects) {
|
|
153
|
+
const found = visit(o.id);
|
|
154
|
+
if (found) return found;
|
|
155
|
+
}
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Every object that depends on `id`, directly or not. */
|
|
160
|
+
export function dependents(objects, id) {
|
|
161
|
+
const out = new Set();
|
|
162
|
+
let grew = true;
|
|
163
|
+
while (grew) {
|
|
164
|
+
grew = false;
|
|
165
|
+
for (const o of objects) if (!out.has(o.id) && o.id !== id && refsOf(o).some((r) => r === id || out.has(r))) (out.add(o.id), (grew = true));
|
|
166
|
+
}
|
|
167
|
+
return [...out];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The objects without `id` and everything that depends on it, and the ids that went (the deletion rule: a cascade). */
|
|
171
|
+
export function removeObject(objects, id) {
|
|
172
|
+
const removed = objects.some((o) => o.id === id) ? [id, ...dependents(objects, id)] : [];
|
|
173
|
+
return { objects: objects.filter((o) => !removed.includes(o.id)), removed };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** An unused id: A, B, C… for points, a prefix and a number for the rest. */
|
|
177
|
+
export function nextId(objects, type) {
|
|
178
|
+
const used = new Set(objects.map((o) => o.id));
|
|
179
|
+
if (type === "point") {
|
|
180
|
+
for (let i = 0; i < 26; i++) if (!used.has(String.fromCharCode(65 + i))) return String.fromCharCode(65 + i);
|
|
181
|
+
}
|
|
182
|
+
const prefix = { point: "P", line: "l", circle: "c", polygon: "poly", angle: "ang", reflect: "refl", rotate: "rot", translate: "tr", scale: "sc" }[type] ?? "o";
|
|
183
|
+
for (let n = 1; ; n++) if (!used.has(`${prefix}${n}`)) return `${prefix}${n}`;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const tidy = (n) => Math.round(n * 1e6) / 1e6;
|
|
187
|
+
const dist = (p, q) => Math.hypot(q[0] - p[0], q[1] - p[1]);
|
|
188
|
+
|
|
189
|
+
/** True when the segments p1-p2 and p3-p4 share a point (touching and overlapping count). */
|
|
190
|
+
export function segmentsIntersect(p1, p2, p3, p4) {
|
|
191
|
+
const cross = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
|
192
|
+
const on = (a, b, c) => Math.min(a[0], b[0]) - EPS <= c[0] && c[0] <= Math.max(a[0], b[0]) + EPS && Math.min(a[1], b[1]) - EPS <= c[1] && c[1] <= Math.max(a[1], b[1]) + EPS;
|
|
193
|
+
const scale = Math.max(1, ...[p1, p2, p3, p4].flat().map(Math.abs));
|
|
194
|
+
const tol = EPS * scale * scale;
|
|
195
|
+
const sgn = (v) => (Math.abs(v) <= tol ? 0 : v > 0 ? 1 : -1);
|
|
196
|
+
const d1 = sgn(cross(p3, p4, p1)), d2 = sgn(cross(p3, p4, p2)), d3 = sgn(cross(p1, p2, p3)), d4 = sgn(cross(p1, p2, p4));
|
|
197
|
+
if (d1 * d2 < 0 && d3 * d4 < 0) return true;
|
|
198
|
+
return (d1 === 0 && on(p3, p4, p1)) || (d2 === 0 && on(p3, p4, p2)) || (d3 === 0 && on(p1, p2, p3)) || (d4 === 0 && on(p1, p2, p4));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** A polygon's area (the shoelace sum, absolute), perimeter, centroid and whether two of its sides that are not neighbours meet. */
|
|
202
|
+
export function polygonInfo(pts) {
|
|
203
|
+
const n = pts.length;
|
|
204
|
+
let a2 = 0, cx = 0, cy = 0, perimeter = 0;
|
|
205
|
+
for (let i = 0; i < n; i++) {
|
|
206
|
+
const p = pts[i], q = pts[(i + 1) % n];
|
|
207
|
+
const w = p[0] * q[1] - q[0] * p[1];
|
|
208
|
+
a2 += w;
|
|
209
|
+
cx += (p[0] + q[0]) * w;
|
|
210
|
+
cy += (p[1] + q[1]) * w;
|
|
211
|
+
perimeter += dist(p, q);
|
|
212
|
+
}
|
|
213
|
+
const area = Math.abs(a2) / 2;
|
|
214
|
+
const centroid = Math.abs(a2) > EPS ? [cx / (3 * a2), cy / (3 * a2)] : [pts.reduce((s, p) => s + p[0], 0) / n, pts.reduce((s, p) => s + p[1], 0) / n];
|
|
215
|
+
let selfIntersecting = false;
|
|
216
|
+
for (let i = 0; i < n && !selfIntersecting; i++) {
|
|
217
|
+
for (let j = i + 1; j < n; j++) {
|
|
218
|
+
if (j === i + 1 || (i === 0 && j === n - 1)) continue;
|
|
219
|
+
if (segmentsIntersect(pts[i], pts[(i + 1) % n], pts[j], pts[(j + 1) % n])) {
|
|
220
|
+
selfIntersecting = true;
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return { area, perimeter, centroid, selfIntersecting };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const polygonShape = (pts) => ({ kind: "polygon", pts, ...polygonInfo(pts) });
|
|
229
|
+
const RAD = Math.PI / 180;
|
|
230
|
+
|
|
231
|
+
/** The static kind an object will have and whether a line is a segment, from its settings alone (no numbers needed). */
|
|
232
|
+
function staticKind(by, id, seen = new Set()) {
|
|
233
|
+
const o = by.get(id);
|
|
234
|
+
if (!o || seen.has(id)) return {};
|
|
235
|
+
seen.add(id);
|
|
236
|
+
if (TRANSFORMS.includes(o.type)) return staticKind(by, o.of, seen);
|
|
237
|
+
return { kind: o.type, segment: o.type === "line" && o.segment === true };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** The names of the measurements the objects publish, from their settings alone (the pane's output ports). */
|
|
241
|
+
export function measurementNames(objects) {
|
|
242
|
+
const by = new Map(objects.map((o) => [o.id, o]));
|
|
243
|
+
const names = [];
|
|
244
|
+
for (const o of objects) {
|
|
245
|
+
const { kind, segment } = staticKind(by, o.id);
|
|
246
|
+
if (kind === "line" && segment) names.push(`${o.id}_length`);
|
|
247
|
+
else if (kind === "circle") names.push(`${o.id}_radius`);
|
|
248
|
+
else if (kind === "polygon") names.push(`${o.id}_area`, `${o.id}_perimeter`);
|
|
249
|
+
else if (kind === "angle") names.push(`${o.id}_angle`);
|
|
250
|
+
}
|
|
251
|
+
return names;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** The names a construction reads as parameters, in order of first use. */
|
|
255
|
+
export function parameterNames(objects) {
|
|
256
|
+
const out = [];
|
|
257
|
+
for (const o of objects) for (const n of paramsOf(o)) if (!out.includes(n)) out.push(n);
|
|
258
|
+
return out;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const mapShape = (s, f, radiusScale = 1) => {
|
|
262
|
+
switch (s.kind) {
|
|
263
|
+
case "point": { const [x, y] = f([s.x, s.y]); return { ...s, x, y }; }
|
|
264
|
+
case "line": return { ...s, a: f(s.a), b: f(s.b) };
|
|
265
|
+
case "circle": { const [cx, cy] = f([s.cx, s.cy]); return { ...s, cx, cy, r: s.r * radiusScale }; }
|
|
266
|
+
case "polygon": return polygonShape(s.pts.map(f));
|
|
267
|
+
default: throw new Error(`a${/^[aeiou]/.test(s.kind) ? "n" : ""} ${s.kind} can't be transformed`);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/** The points a shape stands for when it is used as a point. */
|
|
272
|
+
const pointOf = (s) => (s?.kind === "point" ? [s.x, s.y] : null);
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Work out a construction. `params` are the wired numbers (they beat `g.params`). The answer:
|
|
276
|
+
* shapes { id: shape } (an object that can't be worked out is `{ error }`)
|
|
277
|
+
* scene [{ id, type, kind, ...coordinates, error? }] in the order of the objects, plain JSON
|
|
278
|
+
* measurements { <id>_length | _radius | _area | _perimeter | _angle: number | null }
|
|
279
|
+
* params the parameter values in use
|
|
280
|
+
*/
|
|
281
|
+
export function evaluate(g, params = {}) {
|
|
282
|
+
const objects = g?.objects ?? [];
|
|
283
|
+
const by = new Map(objects.map((o) => [o.id, o]));
|
|
284
|
+
// Names come from the file and the wires: kept in objects with no prototype, and looked up as own keys, so `constructor` or `__proto__` is just a name.
|
|
285
|
+
const values = Object.create(null);
|
|
286
|
+
for (const [k, v] of Object.entries(g?.params ?? {})) Object.defineProperty(values, k, { value: v, enumerable: true, writable: true, configurable: true });
|
|
287
|
+
for (const [k, v] of Object.entries(params)) if (isNum(v)) Object.defineProperty(values, k, { value: v, enumerable: true, writable: true, configurable: true });
|
|
288
|
+
const shapes = Object.create(null);
|
|
289
|
+
const busy = new Set();
|
|
290
|
+
const get = (id) => {
|
|
291
|
+
if (Object.hasOwn(shapes, id)) return shapes[id];
|
|
292
|
+
const o = by.get(id);
|
|
293
|
+
if (!o || busy.has(id)) return (shapes[id] = { error: `${id} is not an object` });
|
|
294
|
+
busy.add(id);
|
|
295
|
+
try {
|
|
296
|
+
shapes[id] = build(o);
|
|
297
|
+
} catch (e) {
|
|
298
|
+
shapes[id] = { error: e.message };
|
|
299
|
+
}
|
|
300
|
+
busy.delete(id);
|
|
301
|
+
return shapes[id];
|
|
302
|
+
};
|
|
303
|
+
const input = (id, from) => {
|
|
304
|
+
const s = get(id);
|
|
305
|
+
if (s.error) throw new Error(`${id} has a problem (${s.error})`);
|
|
306
|
+
return s;
|
|
307
|
+
};
|
|
308
|
+
const point = (id) => {
|
|
309
|
+
const s = input(id);
|
|
310
|
+
if (!pointOf(s)) throw new Error(`${id} is a ${s.kind}, not a point`);
|
|
311
|
+
return [s.x, s.y];
|
|
312
|
+
};
|
|
313
|
+
const line = (id) => {
|
|
314
|
+
const s = input(id);
|
|
315
|
+
if (s.kind !== "line") throw new Error(`${id} is a ${s.kind}, not a line`);
|
|
316
|
+
if (dist(s.a, s.b) < EPS) throw new Error(`${id} has no direction`);
|
|
317
|
+
return s;
|
|
318
|
+
};
|
|
319
|
+
const num = (v, what) => {
|
|
320
|
+
if (isNum(v)) return v;
|
|
321
|
+
if (typeof v === "string") {
|
|
322
|
+
if (Object.hasOwn(values, v)) return values[v];
|
|
323
|
+
throw new Error(`${what} reads the parameter ${v}, which has no value (wire an input named ${v} or give it one in params)`);
|
|
324
|
+
}
|
|
325
|
+
throw new Error(`${what} is not a number`);
|
|
326
|
+
};
|
|
327
|
+
const build = (o) => {
|
|
328
|
+
switch (o.type) {
|
|
329
|
+
case "point": {
|
|
330
|
+
if (o.on === undefined) return { kind: "point", x: o.x, y: o.y };
|
|
331
|
+
const host = input(o.on);
|
|
332
|
+
if (host.kind === "line") {
|
|
333
|
+
if (dist(host.a, host.b) < EPS) throw new Error(`${o.on} has no direction`);
|
|
334
|
+
return { kind: "point", x: host.a[0] + o.t * (host.b[0] - host.a[0]), y: host.a[1] + o.t * (host.b[1] - host.a[1]), on: o.on };
|
|
335
|
+
}
|
|
336
|
+
if (host.kind === "circle") return { kind: "point", x: host.cx + host.r * Math.cos(o.t * RAD), y: host.cy + host.r * Math.sin(o.t * RAD), on: o.on };
|
|
337
|
+
throw new Error(`${o.on} is a ${host.kind}: a point can be anchored on a line or a circle`);
|
|
338
|
+
}
|
|
339
|
+
case "line": {
|
|
340
|
+
const a = point(o.from), b = point(o.to);
|
|
341
|
+
if (dist(a, b) < EPS) throw new Error("a line goes through two distinct points");
|
|
342
|
+
return { kind: "line", a, b, segment: o.segment === true, length: dist(a, b) };
|
|
343
|
+
}
|
|
344
|
+
case "circle": {
|
|
345
|
+
const c = point(o.center);
|
|
346
|
+
const r = o.through !== undefined ? dist(c, point(o.through)) : num(o.radius, "the radius");
|
|
347
|
+
if (!(r >= 0)) throw new Error("the radius is not below zero");
|
|
348
|
+
return { kind: "circle", cx: c[0], cy: c[1], r };
|
|
349
|
+
}
|
|
350
|
+
case "polygon": return polygonShape(o.points.map(point));
|
|
351
|
+
case "angle": {
|
|
352
|
+
const [a, b, c] = o.points.map(point);
|
|
353
|
+
const u = [a[0] - b[0], a[1] - b[1]], v = [c[0] - b[0], c[1] - b[1]];
|
|
354
|
+
if (Math.hypot(...u) < EPS || Math.hypot(...v) < EPS) throw new Error("an angle needs three distinct points (the vertex is the middle one)");
|
|
355
|
+
const shorter = Math.atan2(Math.abs(u[0] * v[1] - u[1] * v[0]), u[0] * v[0] + u[1] * v[1]);
|
|
356
|
+
const mode = o.mode ?? "shorter";
|
|
357
|
+
// The counterclockwise sweep from ray ba to ray bc, in [0, 2pi); the shorter angle is it or its complement, the reflex one the other.
|
|
358
|
+
let sweep = (Math.atan2(v[1], v[0]) - Math.atan2(u[1], u[0])) % (2 * Math.PI);
|
|
359
|
+
if (sweep < 0) sweep += 2 * Math.PI;
|
|
360
|
+
const rad = mode === "clickOrder" ? sweep : mode === "reflex" ? (sweep <= Math.PI ? 2 * Math.PI - sweep : sweep) : shorter;
|
|
361
|
+
const unit = resolveUnit(o, g);
|
|
362
|
+
return { kind: "angle", a, b, c, unit, mode, rad, value: unit === "rad" ? rad : rad / RAD, dir: mode === "shorter" ? (sweep <= Math.PI ? 1 : -1) : mode === "clickOrder" ? 1 : (sweep <= Math.PI ? -1 : 1) };
|
|
363
|
+
}
|
|
364
|
+
case "reflect": {
|
|
365
|
+
const src = input(o.of);
|
|
366
|
+
const l = line(o.in);
|
|
367
|
+
const [dx, dy] = [l.b[0] - l.a[0], l.b[1] - l.a[1]];
|
|
368
|
+
const len2 = dx * dx + dy * dy;
|
|
369
|
+
return mapShape(src, ([x, y]) => {
|
|
370
|
+
const t = ((x - l.a[0]) * dx + (y - l.a[1]) * dy) / len2;
|
|
371
|
+
return [2 * (l.a[0] + t * dx) - x, 2 * (l.a[1] + t * dy) - y];
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
case "rotate": {
|
|
375
|
+
const src = input(o.of);
|
|
376
|
+
const c = point(o.about);
|
|
377
|
+
const raw = num(o.angle, "the angle");
|
|
378
|
+
const th = resolveUnit(o, g) === "rad" ? raw : raw * RAD;
|
|
379
|
+
const [cos, sin] = [Math.cos(th), Math.sin(th)];
|
|
380
|
+
return mapShape(src, ([x, y]) => [c[0] + (x - c[0]) * cos - (y - c[1]) * sin, c[1] + (x - c[0]) * sin + (y - c[1]) * cos]);
|
|
381
|
+
}
|
|
382
|
+
case "translate": {
|
|
383
|
+
const src = input(o.of);
|
|
384
|
+
const dx = num(o.dx, "dx"), dy = num(o.dy, "dy");
|
|
385
|
+
return mapShape(src, ([x, y]) => [x + dx, y + dy]);
|
|
386
|
+
}
|
|
387
|
+
case "scale": {
|
|
388
|
+
const src = input(o.of);
|
|
389
|
+
const c = point(o.about);
|
|
390
|
+
const k = num(o.factor, "the factor");
|
|
391
|
+
return mapShape(src, ([x, y]) => [c[0] + (x - c[0]) * k, c[1] + (y - c[1]) * k], Math.abs(k));
|
|
392
|
+
}
|
|
393
|
+
default: throw new Error("unknown type");
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
for (const o of objects) get(o.id);
|
|
397
|
+
|
|
398
|
+
const scene = [];
|
|
399
|
+
const measurements = {};
|
|
400
|
+
for (const name of measurementNames(objects)) measurements[name] = null;
|
|
401
|
+
for (const o of objects) {
|
|
402
|
+
const s = shapes[o.id];
|
|
403
|
+
const base = { id: o.id, type: o.type };
|
|
404
|
+
if (s.error) {
|
|
405
|
+
scene.push({ ...base, error: s.error });
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
const out = { ...base, kind: s.kind };
|
|
409
|
+
if (s.kind === "point") Object.assign(out, { x: tidy(s.x), y: tidy(s.y) });
|
|
410
|
+
else if (s.kind === "line") {
|
|
411
|
+
Object.assign(out, { from: s.a.map(tidy), to: s.b.map(tidy), segment: s.segment });
|
|
412
|
+
if (s.segment) measurements[`${o.id}_length`] = tidy(s.length ?? dist(s.a, s.b));
|
|
413
|
+
} else if (s.kind === "circle") {
|
|
414
|
+
Object.assign(out, { center: [tidy(s.cx), tidy(s.cy)], radius: tidy(s.r) });
|
|
415
|
+
measurements[`${o.id}_radius`] = tidy(s.r);
|
|
416
|
+
} else if (s.kind === "polygon") {
|
|
417
|
+
Object.assign(out, { points: s.pts.map((p) => p.map(tidy)), area: tidy(s.area), perimeter: tidy(s.perimeter), centroid: s.centroid.map(tidy), selfIntersecting: s.selfIntersecting });
|
|
418
|
+
measurements[`${o.id}_area`] = tidy(s.area);
|
|
419
|
+
measurements[`${o.id}_perimeter`] = tidy(s.perimeter);
|
|
420
|
+
} else if (s.kind === "angle") {
|
|
421
|
+
Object.assign(out, { vertex: s.b.map(tidy), value: tidy(s.value), unit: s.unit, mode: s.mode });
|
|
422
|
+
measurements[`${o.id}_angle`] = tidy(s.value);
|
|
423
|
+
}
|
|
424
|
+
if (o.color !== undefined) out.color = o.color;
|
|
425
|
+
scene.push(out);
|
|
426
|
+
}
|
|
427
|
+
return { shapes, scene, measurements, params: values };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const round3 = (n) => Math.round(n * 1000) / 1000;
|
|
431
|
+
const show = (n) => (Number.isFinite(n) ? String(round3(n)) : "?");
|
|
432
|
+
|
|
433
|
+
/** What an object is, in a few words and numbers (the algebra view's value column). */
|
|
434
|
+
function valueText(s) {
|
|
435
|
+
if (!s) return "";
|
|
436
|
+
if (s.error) return "error";
|
|
437
|
+
switch (s.kind) {
|
|
438
|
+
case "point": return `(${show(s.x)}, ${show(s.y)})`;
|
|
439
|
+
case "line": return s.segment ? `segment, length ${show(s.length ?? dist(s.a, s.b))}` : "line";
|
|
440
|
+
case "circle": return `centre (${show(s.cx)}, ${show(s.cy)}), radius ${show(s.r)}`;
|
|
441
|
+
case "polygon": return `area ${show(s.area)}, perimeter ${show(s.perimeter)}${s.selfIntersecting ? ", crosses itself" : ""}`;
|
|
442
|
+
case "angle": return `${show(s.value)}${s.unit === "rad" ? " rad" : "°"}${s.mode !== "shorter" ? ` (${s.mode === "reflex" ? "reflex" : "click order"})` : ""}`;
|
|
443
|
+
default: return "";
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* The algebra view: every object with its tag, free (nothing it is made from: a point you can set) or dependent (built on other objects,
|
|
449
|
+
* so it can't be set, only moved by moving what it is built on), the reason in words, the ids it is built on, and its value as text.
|
|
450
|
+
*/
|
|
451
|
+
export function algebraRows(objects, shapes = {}) {
|
|
452
|
+
return objects.map((o) => {
|
|
453
|
+
const from = refsOf(o);
|
|
454
|
+
const names = paramsOf(o);
|
|
455
|
+
const bits = [];
|
|
456
|
+
if (o.type === "point" && o.on !== undefined) bits.push(`anchored on ${o.on} (it slides along it)`);
|
|
457
|
+
else if (from.length) bits.push(`built on ${from.join(", ")}`);
|
|
458
|
+
if (names.length) bits.push(`${names.join(", ")} ${names.length === 1 ? "is a parameter" : "are parameters"}`);
|
|
459
|
+
return { id: o.id, type: o.type, tag: from.length ? "dependent" : "free", why: from.length ? `dependent: ${bits.join("; ")}` : "free: a point you can drag or set", from, color: o.color, value: valueText(shapes[o.id]) };
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** The projection of (x, y) on the line through a and b, as t (0 at a, 1 at b). */
|
|
464
|
+
export function lineParameter(a, b, x, y, segment = false) {
|
|
465
|
+
const [dx, dy] = [b[0] - a[0], b[1] - a[1]];
|
|
466
|
+
const t = ((x - a[0]) * dx + (y - a[1]) * dy) / (dx * dx + dy * dy || 1);
|
|
467
|
+
return segment ? Math.min(1, Math.max(0, t)) : t;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* The objects with point `id` moved to (x, y): a free point goes there; an anchored one slides to the nearest place on its line (a
|
|
472
|
+
* segment keeps it between its ends) or circle. Anything else, or an unknown id, answers the same list. Everything downstream follows
|
|
473
|
+
* on the next evaluate.
|
|
474
|
+
*/
|
|
475
|
+
export function movePoint(objects, shapes, id, x, y) {
|
|
476
|
+
const o = objects.find((q) => q.id === id);
|
|
477
|
+
if (!o || o.type !== "point") return objects;
|
|
478
|
+
let patch;
|
|
479
|
+
if (o.on === undefined) patch = { x: tidy(x), y: tidy(y) };
|
|
480
|
+
else {
|
|
481
|
+
const host = shapes[o.on];
|
|
482
|
+
if (host?.kind === "line") patch = { t: tidy(lineParameter(host.a, host.b, x, y, host.segment)) };
|
|
483
|
+
else if (host?.kind === "circle") patch = { t: tidy(Math.atan2(y - host.cy, x - host.cx) / RAD) };
|
|
484
|
+
else return objects;
|
|
485
|
+
}
|
|
486
|
+
return objects.map((q) => (q.id === id ? { ...q, ...patch } : q));
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** The nearest point of a shape to (x, y), and how far it is: for hit-testing. */
|
|
490
|
+
export function distanceTo(s, x, y) {
|
|
491
|
+
if (!s || s.error) return Infinity;
|
|
492
|
+
switch (s.kind) {
|
|
493
|
+
case "point": return Math.hypot(s.x - x, s.y - y);
|
|
494
|
+
case "line": {
|
|
495
|
+
const t = lineParameter(s.a, s.b, x, y, s.segment);
|
|
496
|
+
return Math.hypot(s.a[0] + t * (s.b[0] - s.a[0]) - x, s.a[1] + t * (s.b[1] - s.a[1]) - y);
|
|
497
|
+
}
|
|
498
|
+
case "circle": return Math.abs(Math.hypot(x - s.cx, y - s.cy) - s.r);
|
|
499
|
+
case "polygon": return Math.min(...s.pts.map((p, i) => distanceTo({ kind: "line", a: p, b: s.pts[(i + 1) % s.pts.length], segment: true }, x, y)));
|
|
500
|
+
case "angle": return Math.hypot(s.b[0] - x, s.b[1] - y);
|
|
501
|
+
default: return Infinity;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** The run's answer: what the pane publishes (the scene and every measurement) and the parameters in use. */
|
|
506
|
+
export function run({ geometry, wired = {} }) {
|
|
507
|
+
const e = evaluate(geometry, wired);
|
|
508
|
+
return { value: { scene: e.scene, ...e.measurements }, params: e.params, errors: e.scene.filter((o) => o.error).map((o) => ({ id: o.id, error: o.error })) };
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* The pane's angle unit switched to `unit` without changing what any angle means: a rotation that follows the pane's unit has its number
|
|
513
|
+
* converted (and so has a parameter only such rotations read); a parameter that something else also reads cannot be converted for one
|
|
514
|
+
* reader and not the other, so the rotations that read it are pinned to the unit they were in (their own `unit`), and it keeps its value.
|
|
515
|
+
* Returns { objects, params } to save with `unit`.
|
|
516
|
+
*/
|
|
517
|
+
export function switchUnit(g, unit) {
|
|
518
|
+
const from = resolveUnit({}, g);
|
|
519
|
+
if (unit === from) return { objects: g.objects, params: g.params ?? {} };
|
|
520
|
+
const k = unit === "rad" ? Math.PI / 180 : 180 / Math.PI;
|
|
521
|
+
const r6 = (n) => Math.round(n * 1e6) / 1e6;
|
|
522
|
+
const follows = (o) => o.type === "rotate" && o.unit === undefined;
|
|
523
|
+
const shared = new Set(g.objects.filter((o) => !follows(o)).flatMap((o) => paramsOf(o)));
|
|
524
|
+
const names = new Set(g.objects.filter(follows).map((o) => o.angle).filter((a) => typeof a === "string" && !shared.has(a)));
|
|
525
|
+
const objects = g.objects.map((o) => {
|
|
526
|
+
if (!follows(o)) return o;
|
|
527
|
+
if (typeof o.angle === "number") return { ...o, angle: r6(o.angle * k) };
|
|
528
|
+
if (typeof o.angle === "string" && shared.has(o.angle)) return { ...o, unit: from };
|
|
529
|
+
return o;
|
|
530
|
+
});
|
|
531
|
+
const params = Object.fromEntries(Object.entries(g.params ?? {}).map(([n, v]) => [n, names.has(n) && typeof v === "number" ? r6(v * k) : v]));
|
|
532
|
+
return { objects, params };
|
|
533
|
+
}
|