@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,480 @@
|
|
|
1
|
+
// Aligned candidate comparison: an evaluation's `compare` block lines two
|
|
2
|
+
// or more candidates up on the same dataset rows BY KEY (never by position) and
|
|
3
|
+
// reduces them to dataset-level confusion matrices and precision / recall / F1.
|
|
4
|
+
// Pure: no DOM, no sandbox, no network; candidates' outputs are already values.
|
|
5
|
+
//
|
|
6
|
+
// compare: {
|
|
7
|
+
// key: "id", // the row column (dotted path) that names a row; "$row.id" also works
|
|
8
|
+
// expected: "$row.label", // the label each row should get (a value or a $row reference)
|
|
9
|
+
// candidates: [
|
|
10
|
+
// { name: "rules", pane: "ruleRoute", path: "route" }, // a pane of this canvas, run on every row
|
|
11
|
+
// { name: "keyword", pane: "kwRoute" },
|
|
12
|
+
// { name: "last week", predictions: [{ key: "a1", actual: "bug" }, …] }, // saved results, by key
|
|
13
|
+
// ],
|
|
14
|
+
// positive: "bug", // optional: a binary headline for that class
|
|
15
|
+
// average: "macro" // "macro" (default) | "micro" | "binary" (needs positive)
|
|
16
|
+
// }
|
|
17
|
+
//
|
|
18
|
+
// Definitions (docs/guide/cli-and-serving.md's "Comparing candidates" states them for people):
|
|
19
|
+
// - A row is aligned when its key is present and unique in the dataset. Rows with no key, and every
|
|
20
|
+
// row whose key is shared (all of them: it is ambiguous which one a prediction means), are left out
|
|
21
|
+
// of every number and listed in `alignment`. Keys compare as keyText (src/runtime/keys.js): 1 and "1"
|
|
22
|
+
// are different keys.
|
|
23
|
+
// - A prediction is available when the candidate produced a value that isn't null or undefined. A
|
|
24
|
+
// pane that failed, was skipped or has no value, a null output, and (saved predictions) a key the
|
|
25
|
+
// candidate has no entry for are UNAVAILABLE: reported with their own count, never a valid
|
|
26
|
+
// prediction and never a hit. A saved prediction whose key isn't in the dataset is `unmatched`; two
|
|
27
|
+
// saved predictions for one key are `duplicate` and neither is used.
|
|
28
|
+
// - A row without an expected label (null or missing) is `unlabeled`: left out of the confusion
|
|
29
|
+
// numbers, still shown in the aligned table.
|
|
30
|
+
// - Labels compare as text for strings, numbers and booleans (so a CSV cell "1" and the number 1 are
|
|
31
|
+
// the same label) and as canonical JSON for objects and arrays. Case counts.
|
|
32
|
+
// - Per class c: TP (expected c, predicted c), FP (predicted c, expected other), FN (expected c,
|
|
33
|
+
// predicted other). precision = TP / (TP + FP), recall = TP / (TP + FN), F1 = 2PR / (P + R). A ratio
|
|
34
|
+
// with a zero denominator is undefined (null) and counts as 0 when averaged (zero_division = 0).
|
|
35
|
+
// - Classes are every label seen as expected or predicted. macro = unweighted mean over all of them;
|
|
36
|
+
// micro pools TP, FP and FN over all classes (for single-label data micro precision = recall = F1 =
|
|
37
|
+
// accuracy). accuracy = correct / scored. binary = the `positive` class alone.
|
|
38
|
+
// - Ties: a candidate gives one label per row, so there is no tie to break. Between two candidates a
|
|
39
|
+
// row where both are right (or both wrong) is a tie; `pairs` counts both-right, only-a, only-b and
|
|
40
|
+
// both-wrong on the rows both have an available prediction for.
|
|
41
|
+
// - Small samples: nothing here is a significance test. `sample` says n and flags n < 30; `interval`
|
|
42
|
+
// is the 95% Wilson interval of accuracy and is wide when n is small.
|
|
43
|
+
import { keyText } from "../runtime/keys.js";
|
|
44
|
+
import { pick, withRow, parseCsv } from "./dataset.js";
|
|
45
|
+
|
|
46
|
+
/** Below this many scored rows, a difference between candidates should not be read as real. */
|
|
47
|
+
export const SMALL_SAMPLE = 30;
|
|
48
|
+
/** The averaging a headline precision / recall / F1 uses. */
|
|
49
|
+
export const AVERAGES = ["macro", "micro", "binary"];
|
|
50
|
+
/** The figures a candidate contributes to `pass` thresholds, as `<candidate>.<figure>`. */
|
|
51
|
+
export const COMPARE_FIGURES = ["accuracy", "precision", "recall", "f1", "coverage"];
|
|
52
|
+
|
|
53
|
+
const round = (n) => Math.round(n * 1e6) / 1e6;
|
|
54
|
+
const isMissing = (v) => v === null || v === undefined;
|
|
55
|
+
|
|
56
|
+
/** The row column a `compare.key` names (`"$row.id"` and `"id"` are the same). */
|
|
57
|
+
export const keyPath = (key) => String(key).replace(/^\$row\.?/, "");
|
|
58
|
+
|
|
59
|
+
/** A label as comparable text (see the top of this file). */
|
|
60
|
+
export function labelText(v) {
|
|
61
|
+
if (typeof v === "string") return v;
|
|
62
|
+
if (typeof v === "number" || typeof v === "boolean" || typeof v === "bigint") return String(v);
|
|
63
|
+
const k = keyText(v);
|
|
64
|
+
return k === undefined ? String(v) : k.slice(1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The names of a compare block's candidates (a pane's name, plus its path, when it has none of its own). */
|
|
68
|
+
export function candidateName(c) {
|
|
69
|
+
return c.name ?? (c.pane ? (c.path ? `${c.pane}.${c.path}` : c.pane) : undefined);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The `pass` names a compare block adds: `rules.f1`, … */
|
|
73
|
+
export function compareFigureNames(compare) {
|
|
74
|
+
return (compare?.candidates ?? []).flatMap((c) => COMPARE_FIGURES.map((f) => `${candidateName(c)}.${f}`));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Throws a readable error unless a test's `compare` block is valid. */
|
|
78
|
+
export function validateCompare(compare, where) {
|
|
79
|
+
if (compare === undefined) return;
|
|
80
|
+
const bad = (m) => new Error(`${where}: compare: ${m}`);
|
|
81
|
+
if (!compare || typeof compare !== "object" || Array.isArray(compare)) throw bad('it must be { "key", "expected", "candidates" }.');
|
|
82
|
+
for (const k of Object.keys(compare)) if (!["key", "expected", "candidates", "positive", "average"].includes(k)) throw bad(`unknown field "${k}".`);
|
|
83
|
+
if (typeof compare.key !== "string" || !keyPath(compare.key)) throw bad('"key" must name the row column that identifies a row (like "id").');
|
|
84
|
+
if (!Array.isArray(compare.candidates) || compare.candidates.length < 2) throw bad('"candidates" must list at least two candidates.');
|
|
85
|
+
if (compare.candidates.length > 8) throw bad("at most 8 candidates.");
|
|
86
|
+
if (compare.average !== undefined && !AVERAGES.includes(compare.average)) throw bad(`"average" is ${AVERAGES.join(", ")}.`);
|
|
87
|
+
if (compare.average === "binary" && isMissing(compare.positive)) throw bad('"average": "binary" needs "positive" (the class it is about).');
|
|
88
|
+
if (compare.positive !== undefined && (typeof compare.positive === "object" || isMissing(compare.positive))) throw bad('"positive" must be a text, number or boolean label.');
|
|
89
|
+
if (compare.expected === undefined && compare.positive !== undefined) throw bad('"positive" needs "expected" labels.');
|
|
90
|
+
const names = new Set();
|
|
91
|
+
for (const [i, c] of compare.candidates.entries()) {
|
|
92
|
+
if (!c || typeof c !== "object" || Array.isArray(c)) throw bad(`candidate ${i + 1} is malformed.`);
|
|
93
|
+
const name = candidateName(c);
|
|
94
|
+
const which = `candidate ${i + 1}${name ? ` ("${name}")` : ""}`;
|
|
95
|
+
if (typeof name !== "string" || !name) throw bad(`${which} needs a "name" (or a "pane").`);
|
|
96
|
+
if (names.has(name)) throw bad(`two candidates are called "${name}".`);
|
|
97
|
+
names.add(name);
|
|
98
|
+
const hasPane = c.pane !== undefined;
|
|
99
|
+
const hasPred = c.predictions !== undefined;
|
|
100
|
+
if (hasPane === hasPred) throw bad(`${which} has either "pane" (a pane of this canvas) or "predictions" (saved results), not ${hasPane ? "both" : "neither"}.`);
|
|
101
|
+
if (hasPane && (typeof c.pane !== "string" || !c.pane)) throw bad(`${which}: "pane" must be a pane's name.`);
|
|
102
|
+
if (c.path !== undefined && (!hasPane || typeof c.path !== "string")) throw bad(`${which}: "path" is text, and only for a pane.`);
|
|
103
|
+
if (hasPred) {
|
|
104
|
+
if (!Array.isArray(c.predictions)) throw bad(`${which}: "predictions" must be a list of { key, actual }.`);
|
|
105
|
+
if (c.predictions.length > 5000) throw bad(`${which}: "predictions" is limited to 5,000 entries.`);
|
|
106
|
+
for (const p of c.predictions) if (!p || typeof p !== "object" || Array.isArray(p) || !Object.hasOwn(p, "key")) throw bad(`${which}: every prediction is { "key", "actual" }.`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* An evaluation with its `compare` block set from a plain description (the Tests dialog's form and
|
|
113
|
+
* mallory_save_test's `compare`): `{ key, expected?, candidates: [{ name?, pane?, path? }], positive?, average? }`,
|
|
114
|
+
* or `null` to remove the block. A candidate that names no pane but matches the name of a saved-predictions
|
|
115
|
+
* candidate already in the block keeps that one (saved predictions are never typed in or sent by an agent).
|
|
116
|
+
* `panes` (optional) is the list of pane names a candidate may read. Throws the same readable errors as
|
|
117
|
+
* validateCompare; returns a new test, leaving `test` alone.
|
|
118
|
+
*/
|
|
119
|
+
export function withCompare(test, spec, { panes } = {}) {
|
|
120
|
+
const where = `"${test.name}"`;
|
|
121
|
+
if (spec === null) {
|
|
122
|
+
const { compare: _drop, pass, ...rest } = test;
|
|
123
|
+
// Thresholds on a candidate's figures (`rules.f1`) mean nothing without the block: drop them rather than leave a test that can't validate.
|
|
124
|
+
const kept = Object.fromEntries(Object.entries(pass ?? {}).filter(([k]) => !compareFigureNames(test.compare).includes(k)));
|
|
125
|
+
return { ...rest, ...(Object.keys(kept).length ? { pass: kept } : {}) };
|
|
126
|
+
}
|
|
127
|
+
if (!spec || typeof spec !== "object" || Array.isArray(spec)) throw new Error(`${where}: compare: it must be { "key", "expected", "candidates" }.`);
|
|
128
|
+
const saved = new Map((test.compare?.candidates ?? []).filter((c) => c.predictions).map((c) => [candidateName(c), c]));
|
|
129
|
+
const candidates = (spec.candidates ?? []).map((c) => {
|
|
130
|
+
if (c && typeof c === "object" && c.pane === undefined && c.predictions === undefined && saved.has(c.name)) return saved.get(c.name);
|
|
131
|
+
if (c && typeof c === "object" && panes && c.pane !== undefined && !panes.includes(c.pane)) throw new Error(`${where}: compare: no checkable pane called "${c.pane}" (panes: ${panes.join(", ") || "none"}).`);
|
|
132
|
+
return c && typeof c === "object" ? Object.fromEntries(Object.entries(c).filter(([k]) => ["name", "pane", "path"].includes(k) && c[k] !== undefined && c[k] !== "")) : c;
|
|
133
|
+
});
|
|
134
|
+
const compare = { key: spec.key, ...(spec.expected !== undefined && spec.expected !== "" ? { expected: spec.expected } : {}), candidates, ...(spec.positive !== undefined && spec.positive !== "" ? { positive: spec.positive } : {}), ...(spec.average !== undefined && spec.average !== "" ? { average: spec.average } : {}) };
|
|
135
|
+
validateCompare(compare, where);
|
|
136
|
+
// `pass` thresholds that named candidates the new block no longer has would fail validation: say so instead.
|
|
137
|
+
const figures = new Set(compareFigureNames(compare));
|
|
138
|
+
const stale = Object.keys(test.pass ?? {}).filter((k) => compareFigureNames(test.compare).includes(k) && !figures.has(k));
|
|
139
|
+
if (stale.length) throw new Error(`${where}: "pass" still has thresholds on ${stale.join(", ")}: keep those candidates or remove the thresholds first.`);
|
|
140
|
+
return { ...test, compare };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Most saved predictions a file may give one candidate (`mallory test --predictions`); inline in a canvas the limit is 5,000. */
|
|
144
|
+
export const MAX_FILE_PREDICTIONS = 1_000_000;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Saved predictions from a file's text: JSON (a list of `{ key, actual }`, `{ predictions: [...] }`, or an object
|
|
148
|
+
* mapping each key to its prediction), JSON Lines (one `{ key, actual }` per line) or CSV (`key` and `actual`
|
|
149
|
+
* columns, cells as text). `name` (the file's name) and `type` decide when they say; otherwise JSON is tried first.
|
|
150
|
+
* Throws a readable error.
|
|
151
|
+
*/
|
|
152
|
+
export function parsePredictions(text, { name = "", type = "" } = {}) {
|
|
153
|
+
const t = String(text ?? "");
|
|
154
|
+
const csv = /csv|tab-separated/.test(type) || /\.(csv|tsv)$/i.test(name);
|
|
155
|
+
const jsonl = /jsonl|ndjson/.test(type) || /\.(jsonl|ndjson)$/i.test(name);
|
|
156
|
+
let list;
|
|
157
|
+
if (csv) {
|
|
158
|
+
const rows = parseCsv(t);
|
|
159
|
+
if (rows.length && !(Object.hasOwn(rows[0], "key") && Object.hasOwn(rows[0], "actual"))) throw new Error('a CSV of predictions needs "key" and "actual" columns.');
|
|
160
|
+
list = rows.map((r) => ({ key: r.key, actual: r.actual }));
|
|
161
|
+
} else {
|
|
162
|
+
let value;
|
|
163
|
+
try {
|
|
164
|
+
value = JSON.parse(t);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
const lines = t.split(/\r?\n/).filter((l) => l.trim());
|
|
167
|
+
if (!jsonl && !(lines.length > 1 && lines.every((l) => l.trim().startsWith("{")))) throw new Error(`the predictions aren't valid: ${error.message}`);
|
|
168
|
+
try {
|
|
169
|
+
value = lines.map((l) => JSON.parse(l));
|
|
170
|
+
} catch (lineError) {
|
|
171
|
+
throw new Error(`the predictions aren't valid: ${lineError.message}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (Array.isArray(value?.predictions)) value = value.predictions;
|
|
175
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
176
|
+
// One object: a { key, actual } line, or a map from each key to its prediction.
|
|
177
|
+
value = Object.hasOwn(value, "key") && Object.hasOwn(value, "actual") ? [value] : Object.entries(value).map(([key, actual]) => ({ key, actual }));
|
|
178
|
+
}
|
|
179
|
+
if (!Array.isArray(value)) throw new Error('the predictions are a list of { "key", "actual" } (or an object from each key to its prediction).');
|
|
180
|
+
list = value;
|
|
181
|
+
}
|
|
182
|
+
if (!list.length) throw new Error("the file has no predictions.");
|
|
183
|
+
if (list.length > MAX_FILE_PREDICTIONS) throw new Error(`the file has ${list.length} predictions; at most ${MAX_FILE_PREDICTIONS} are read.`);
|
|
184
|
+
for (const p of list) if (!p || typeof p !== "object" || Array.isArray(p) || !Object.hasOwn(p, "key")) throw new Error('every prediction is { "key", "actual" }.');
|
|
185
|
+
return list;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* An evaluation with saved-predictions candidates added or replaced by name (`{ name: [{ key, actual }] }`), for a
|
|
190
|
+
* run that was given them (`mallory test --predictions`); returns a new test. A name that is a pane candidate
|
|
191
|
+
* of the block is refused, and so is a test with no `compare`.
|
|
192
|
+
*/
|
|
193
|
+
export function withSavedPredictions(test, byName) {
|
|
194
|
+
const where = `"${test.name}"`;
|
|
195
|
+
if (!test.compare) throw new Error(`${where} has no compare block to add predictions to.`);
|
|
196
|
+
const candidates = [...test.compare.candidates];
|
|
197
|
+
for (const [name, predictions] of Object.entries(byName)) {
|
|
198
|
+
const at = candidates.findIndex((c) => candidateName(c) === name);
|
|
199
|
+
if (at >= 0 && candidates[at].pane !== undefined) throw new Error(`${where}: "${name}" is a pane candidate, not saved predictions.`);
|
|
200
|
+
if (at >= 0) candidates[at] = { name, predictions };
|
|
201
|
+
else candidates.push({ name, predictions });
|
|
202
|
+
}
|
|
203
|
+
if (candidates.length > 8) throw new Error(`${where}: compare: at most 8 candidates.`);
|
|
204
|
+
return { ...test, compare: { ...test.compare, candidates } };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The panes a compare block reads from a row's run. */
|
|
208
|
+
export const comparePanes = (compare) => [...new Set((compare?.candidates ?? []).filter((c) => c.pane).map((c) => c.pane))];
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Saved predictions from an earlier evaluation result: one per row with a key, taken from the pane's value
|
|
212
|
+
* (and `path`). A row that had no value is left out, so it reads as missing, not as a label.
|
|
213
|
+
*/
|
|
214
|
+
export function predictionsFromResult(result, key, { pane, path } = {}) {
|
|
215
|
+
const out = [];
|
|
216
|
+
for (const r of result?.rows ?? []) {
|
|
217
|
+
const k = pick(r.row, keyPath(key));
|
|
218
|
+
if (keyText(k) === undefined || !Object.hasOwn(r.values ?? {}, pane)) continue;
|
|
219
|
+
const actual = pick(r.values[pane], path);
|
|
220
|
+
if (!isMissing(actual)) out.push({ key: k, actual });
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ── metrics ────────────────────────────────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
const ratio = (a, b) => (b === 0 ? null : round(a / b));
|
|
228
|
+
const f1Of = (p, r) => (p === null || r === null ? null : p + r === 0 ? 0 : round((2 * p * r) / (p + r)));
|
|
229
|
+
const zero = (v) => v ?? 0;
|
|
230
|
+
|
|
231
|
+
/** The 95% Wilson interval of a proportion, or null with no trials. */
|
|
232
|
+
export function wilson(k, n, z = 1.96) {
|
|
233
|
+
if (!n) return null;
|
|
234
|
+
const p = k / n;
|
|
235
|
+
const d = 1 + (z * z) / n;
|
|
236
|
+
const centre = (p + (z * z) / (2 * n)) / d;
|
|
237
|
+
const half = (z * Math.sqrt((p * (1 - p)) / n + (z * z) / (4 * n * n))) / d;
|
|
238
|
+
return [round(Math.max(0, centre - half)), round(Math.min(1, centre + half))];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Confusion counts and the figures of the top comment for scored pairs.
|
|
243
|
+
* @param {{ expected: unknown, actual: unknown }[]} pairs rows with both labels
|
|
244
|
+
* @param {{ positive?: unknown, average?: "macro" | "micro" | "binary" }} [options]
|
|
245
|
+
*/
|
|
246
|
+
export function confusion(pairs, { positive, average } = {}) {
|
|
247
|
+
const texts = pairs.map((p) => [labelText(p.expected), labelText(p.actual)]);
|
|
248
|
+
const classes = [...new Set(texts.flat())].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
249
|
+
const index = new Map(classes.map((c, i) => [c, i]));
|
|
250
|
+
const matrix = classes.map(() => classes.map(() => 0)); // [expected][predicted]
|
|
251
|
+
for (const [e, a] of texts) matrix[index.get(e)][index.get(a)]++;
|
|
252
|
+
const n = pairs.length;
|
|
253
|
+
const perClass = classes.map((label, i) => {
|
|
254
|
+
const tp = matrix[i][i];
|
|
255
|
+
const support = matrix[i].reduce((s, v) => s + v, 0);
|
|
256
|
+
const predicted = matrix.reduce((s, row) => s + row[i], 0);
|
|
257
|
+
const precision = ratio(tp, predicted);
|
|
258
|
+
const recall = ratio(tp, support);
|
|
259
|
+
return { label, tp, fp: predicted - tp, fn: support - tp, support, predicted, precision, recall, f1: f1Of(precision, recall) };
|
|
260
|
+
});
|
|
261
|
+
const correct = perClass.reduce((s, c) => s + c.tp, 0);
|
|
262
|
+
const mean = (key) => (perClass.length ? round(perClass.reduce((s, c) => s + zero(c[key]), 0) / perClass.length) : null);
|
|
263
|
+
const tp = correct;
|
|
264
|
+
const fp = perClass.reduce((s, c) => s + c.fp, 0);
|
|
265
|
+
const fn = perClass.reduce((s, c) => s + c.fn, 0);
|
|
266
|
+
const microP = ratio(tp, tp + fp);
|
|
267
|
+
const microR = ratio(tp, tp + fn);
|
|
268
|
+
const macro = { precision: mean("precision"), recall: mean("recall"), f1: mean("f1") };
|
|
269
|
+
const micro = { precision: microP, recall: microR, f1: f1Of(microP, microR) };
|
|
270
|
+
const positiveText = isMissing(positive) ? undefined : labelText(positive);
|
|
271
|
+
const cls = positiveText === undefined ? undefined : perClass.find((c) => c.label === positiveText);
|
|
272
|
+
// A positive class nobody expected or predicted still has a (zero-count) row, so the headline says so.
|
|
273
|
+
const binary = positiveText === undefined ? undefined : { label: positiveText, tp: cls?.tp ?? 0, fp: cls?.fp ?? 0, fn: cls?.fn ?? 0, support: cls?.support ?? 0, precision: cls?.precision ?? null, recall: cls?.recall ?? null, f1: cls?.f1 ?? null };
|
|
274
|
+
const averaging = average ?? (positiveText === undefined ? "macro" : "binary");
|
|
275
|
+
const headlineSource = averaging === "micro" ? micro : averaging === "binary" ? (binary ?? { precision: null, recall: null, f1: null }) : macro;
|
|
276
|
+
return {
|
|
277
|
+
n,
|
|
278
|
+
classes,
|
|
279
|
+
matrix,
|
|
280
|
+
perClass,
|
|
281
|
+
accuracy: ratio(correct, n),
|
|
282
|
+
interval: wilson(correct, n),
|
|
283
|
+
macro,
|
|
284
|
+
micro,
|
|
285
|
+
...(binary ? { binary } : {}),
|
|
286
|
+
averaging,
|
|
287
|
+
headline: { precision: headlineSource.precision, recall: headlineSource.recall, f1: headlineSource.f1 },
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ── alignment ──────────────────────────────────────────────────────────────
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Line the candidates up on the dataset rows, by key, and reduce them.
|
|
295
|
+
* @param {object} compare the validated block
|
|
296
|
+
* @param {{ index: number, row: object, values?: object, error?: string, noValue?: Record<string, string> }[]} rows
|
|
297
|
+
* the evaluation's per-row results (its dataset row, the panes' values, why a pane has none)
|
|
298
|
+
* @returns the report described in docs/guide/cli-and-serving.md "Comparing candidates"
|
|
299
|
+
*/
|
|
300
|
+
export function buildComparison(compare, rows) {
|
|
301
|
+
const path = keyPath(compare.key);
|
|
302
|
+
const candidates = compare.candidates.map((c) => ({ ...c, name: candidateName(c) }));
|
|
303
|
+
const names = candidates.map((c) => c.name);
|
|
304
|
+
|
|
305
|
+
// 1. Which dataset rows can be aligned: a key that is there, and only once.
|
|
306
|
+
const seen = new Map(); // keyText -> [row indexes]
|
|
307
|
+
const missingKey = [];
|
|
308
|
+
for (const r of rows) {
|
|
309
|
+
const k = pick(r.row, path);
|
|
310
|
+
const t = keyText(k);
|
|
311
|
+
if (t === undefined) missingKey.push(r.index);
|
|
312
|
+
else (seen.get(t) ?? seen.set(t, []).get(t)).push(r.index);
|
|
313
|
+
}
|
|
314
|
+
const duplicateKeys = [...seen.entries()].filter(([, at]) => at.length > 1).map(([t, at]) => ({ key: pick(rows.find((r) => r.index === at[0]).row, path), rows: at }));
|
|
315
|
+
const dup = new Set(duplicateKeys.flatMap((d) => d.rows));
|
|
316
|
+
const aligned = rows.filter((r) => keyText(pick(r.row, path)) !== undefined && !dup.has(r.index));
|
|
317
|
+
|
|
318
|
+
// 2. Saved predictions by key: a lookup per candidate, with duplicates and strangers set aside.
|
|
319
|
+
const alignment = { rows: rows.length, aligned: aligned.length, missingKey, duplicateKeys, candidates: {} };
|
|
320
|
+
const lookups = new Map();
|
|
321
|
+
for (const c of candidates) {
|
|
322
|
+
if (!c.predictions) continue;
|
|
323
|
+
const byKey = new Map();
|
|
324
|
+
const duplicate = new Set();
|
|
325
|
+
const unmatched = [];
|
|
326
|
+
for (const p of c.predictions) {
|
|
327
|
+
const t = keyText(p.key);
|
|
328
|
+
if (t === undefined || !seen.has(t)) unmatched.push(p.key ?? null);
|
|
329
|
+
else if (byKey.has(t)) duplicate.add(t);
|
|
330
|
+
else byKey.set(t, p);
|
|
331
|
+
}
|
|
332
|
+
for (const t of duplicate) byKey.delete(t);
|
|
333
|
+
lookups.set(c.name, { byKey, duplicate });
|
|
334
|
+
alignment.candidates[c.name] = { predictions: c.predictions.length, unmatched, duplicates: [...duplicate].map((t) => c.predictions.find((p) => keyText(p.key) === t).key) };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// 3. The aligned table: a row per dataset row, a prediction per candidate.
|
|
338
|
+
const hasExpected = compare.expected !== undefined;
|
|
339
|
+
const table = aligned.map((r) => {
|
|
340
|
+
const expected = hasExpected ? withRow(compare.expected, r.row) : null;
|
|
341
|
+
const row = { index: r.index, key: pick(r.row, path), ...(hasExpected ? { expected: isMissing(expected) ? null : expected } : {}), predictions: {} };
|
|
342
|
+
for (const c of candidates) {
|
|
343
|
+
let p;
|
|
344
|
+
if (c.predictions) {
|
|
345
|
+
const t = keyText(row.key);
|
|
346
|
+
const look = lookups.get(c.name);
|
|
347
|
+
if (look.duplicate.has(t)) p = { status: "unavailable", reason: "two saved predictions share this key" };
|
|
348
|
+
else if (!look.byKey.has(t)) p = { status: "missing", reason: "no saved prediction for this key" };
|
|
349
|
+
else p = isMissing(look.byKey.get(t).actual) ? { status: "unavailable", reason: "the saved prediction is empty" } : { status: "ok", actual: look.byKey.get(t).actual };
|
|
350
|
+
} else if (r.error) p = { status: "unavailable", reason: r.error };
|
|
351
|
+
else if (!Object.hasOwn(r.values ?? {}, c.pane)) p = { status: "unavailable", reason: r.noValue?.[c.pane] ?? "the pane has no value" };
|
|
352
|
+
else {
|
|
353
|
+
const actual = pick(r.values[c.pane], c.path);
|
|
354
|
+
p = isMissing(actual) ? { status: "unavailable", reason: c.path && !isMissing(r.values[c.pane]) ? `no value at "${c.path}"` : "the pane's value is empty" } : { status: "ok", actual };
|
|
355
|
+
}
|
|
356
|
+
if (p.status === "ok" && hasExpected && !isMissing(row.expected)) p.correct = labelText(p.actual) === labelText(row.expected);
|
|
357
|
+
row.predictions[c.name] = p;
|
|
358
|
+
}
|
|
359
|
+
return row;
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// 4. Per candidate: denominators, then the confusion over what was scored.
|
|
363
|
+
const reports = candidates.map((c) => {
|
|
364
|
+
const preds = table.map((r) => r.predictions[c.name]);
|
|
365
|
+
const labeled = hasExpected ? table.filter((r) => !isMissing(r.expected)).length : 0;
|
|
366
|
+
const pairs = hasExpected ? table.filter((r) => !isMissing(r.expected) && r.predictions[c.name].status === "ok").map((r) => ({ expected: r.expected, actual: r.predictions[c.name].actual })) : [];
|
|
367
|
+
const counts = {
|
|
368
|
+
rows: rows.length,
|
|
369
|
+
aligned: table.length,
|
|
370
|
+
available: preds.filter((p) => p.status === "ok").length,
|
|
371
|
+
unavailable: preds.filter((p) => p.status === "unavailable").length,
|
|
372
|
+
missing: preds.filter((p) => p.status === "missing").length,
|
|
373
|
+
unlabeled: hasExpected ? table.length - labeled : 0,
|
|
374
|
+
scored: pairs.length,
|
|
375
|
+
};
|
|
376
|
+
const report = { name: c.name, ...(c.pane ? { pane: c.pane, ...(c.path ? { path: c.path } : {}) } : { saved: true }), counts };
|
|
377
|
+
if (!hasExpected) return report;
|
|
378
|
+
const conf = confusion(pairs, { positive: compare.positive, average: compare.average });
|
|
379
|
+
// Rows that were labeled but have no valid prediction: how the accuracy reads if they are all counted wrong.
|
|
380
|
+
const accuracyOfLabeled = labeled ? round(pairs.filter((p) => labelText(p.expected) === labelText(p.actual)).length / labeled) : null;
|
|
381
|
+
return { ...report, ...conf, coverage: ratio(pairs.length, labeled), accuracyOfLabeled, sample: sampleNote(pairs.length) };
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
// 5. Pairs of candidates on the rows both predicted.
|
|
385
|
+
const pairsOut = [];
|
|
386
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
387
|
+
for (let j = i + 1; j < candidates.length; j++) {
|
|
388
|
+
const a = candidates[i].name;
|
|
389
|
+
const b = candidates[j].name;
|
|
390
|
+
const both = table.filter((r) => r.predictions[a].status === "ok" && r.predictions[b].status === "ok");
|
|
391
|
+
const out = { a, b, rows: both.length, agree: both.filter((r) => labelText(r.predictions[a].actual) === labelText(r.predictions[b].actual)).length };
|
|
392
|
+
if (hasExpected) {
|
|
393
|
+
const labeled = both.filter((r) => !isMissing(r.expected));
|
|
394
|
+
out.labeled = labeled.length;
|
|
395
|
+
out.bothRight = labeled.filter((r) => r.predictions[a].correct && r.predictions[b].correct).length;
|
|
396
|
+
out.onlyA = labeled.filter((r) => r.predictions[a].correct && !r.predictions[b].correct).length;
|
|
397
|
+
out.onlyB = labeled.filter((r) => !r.predictions[a].correct && r.predictions[b].correct).length;
|
|
398
|
+
out.bothWrong = labeled.filter((r) => !r.predictions[a].correct && !r.predictions[b].correct).length;
|
|
399
|
+
out.sample = sampleNote(labeled.length);
|
|
400
|
+
}
|
|
401
|
+
pairsOut.push(out);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return { key: path, ...(hasExpected ? { expected: compare.expected } : {}), candidates: names, alignment, reports, pairs: pairsOut, rows: table };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** n scored rows and whether that is too few to trust a difference. */
|
|
408
|
+
export function sampleNote(n) {
|
|
409
|
+
return n < SMALL_SAMPLE ? { n, small: true, note: `only ${n} scored row${n === 1 ? "" : "s"} (under ${SMALL_SAMPLE}): a difference between candidates may be chance` } : { n, small: false };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** The rows of a confusion cell: expected label × predicted label, for one candidate. */
|
|
413
|
+
export function cellRows(comparison, candidate, expected, predicted) {
|
|
414
|
+
const e = labelText(expected);
|
|
415
|
+
const p = labelText(predicted);
|
|
416
|
+
return comparison.rows.filter((r) => {
|
|
417
|
+
const pred = r.predictions[candidate];
|
|
418
|
+
return pred?.status === "ok" && r.expected !== null && r.expected !== undefined && labelText(r.expected) === e && labelText(pred.actual) === p;
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** The keys a person would hand to a select pane's `select` input (same dataset), for a list of aligned rows. */
|
|
423
|
+
export const keysOf = (rows) => rows.map((r) => r.key);
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* The comparison's figures as thresholds' aggregates: `<candidate>.accuracy`, `.precision`, `.recall` and
|
|
427
|
+
* `.f1` (the headline averaging), `.coverage`. A figure that is undefined has no mean, so a threshold on it fails.
|
|
428
|
+
*/
|
|
429
|
+
export function comparisonAggregates(comparison) {
|
|
430
|
+
const out = {};
|
|
431
|
+
for (const r of comparison.reports ?? []) {
|
|
432
|
+
const put = (figure, mean) => (out[`${r.name}.${figure}`] = { mean: mean ?? null, rows: r.counts.scored, scored: r.counts.scored, missing: r.counts.rows - r.counts.scored });
|
|
433
|
+
put("coverage", r.coverage);
|
|
434
|
+
if (r.headline) {
|
|
435
|
+
put("accuracy", r.accuracy);
|
|
436
|
+
put("precision", r.headline.precision);
|
|
437
|
+
put("recall", r.headline.recall);
|
|
438
|
+
put("f1", r.headline.f1);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return out;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** CSV of the aligned table: a line per row per candidate (key, expected, candidate, actual, status, correct). */
|
|
445
|
+
export function comparisonCsv(comparison) {
|
|
446
|
+
const cell = (v) => {
|
|
447
|
+
const text = v === undefined || v === null ? "" : typeof v === "string" ? v : JSON.stringify(v);
|
|
448
|
+
return /[",\n\r]/.test(text) ? `"${text.replaceAll('"', '""')}"` : text;
|
|
449
|
+
};
|
|
450
|
+
const lines = [["key", "row", "expected", "candidate", "actual", "status", "correct", "reason"].join(",")];
|
|
451
|
+
for (const r of comparison.rows) {
|
|
452
|
+
for (const name of comparison.candidates) {
|
|
453
|
+
const p = r.predictions[name];
|
|
454
|
+
lines.push([r.key, r.index + 1, r.expected, name, p.actual, p.status, p.correct, p.reason].map(cell).join(","));
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
return `${lines.join("\n")}\n`;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** A comparison's lines in a terminal report: alignment problems, a line per candidate, the pairs. */
|
|
461
|
+
export function formatComparison(c) {
|
|
462
|
+
const pct = (v) => (v === null || v === undefined ? "–" : `${Math.round(v * 1000) / 10}%`);
|
|
463
|
+
const a = c.alignment;
|
|
464
|
+
const lines = [` compare by "${c.key}": ${a.aligned} of ${a.rows} rows aligned`];
|
|
465
|
+
if (a.missingKey.length) lines.push(` ${a.missingKey.length} row${a.missingKey.length === 1 ? " has" : "s have"} no key (rows ${a.missingKey.slice(0, 8).map((i) => i + 1).join(", ")}${a.missingKey.length > 8 ? ", …" : ""}), left out`);
|
|
466
|
+
if (a.duplicateKeys.length) lines.push(` ${a.duplicateKeys.length} key${a.duplicateKeys.length === 1 ? " is" : "s are"} shared by several rows (${a.duplicateKeys.slice(0, 5).map((d) => JSON.stringify(d.key)).join(", ")}${a.duplicateKeys.length > 5 ? ", …" : ""}), those rows left out`);
|
|
467
|
+
for (const r of c.reports) {
|
|
468
|
+
const n = r.counts;
|
|
469
|
+
const saved = a.candidates[r.name];
|
|
470
|
+
lines.push(` ${r.name}: ${n.scored} scored of ${n.aligned} aligned (${n.unavailable} unavailable, ${n.missing} missing${n.unlabeled ? `, ${n.unlabeled} unlabeled` : ""})`);
|
|
471
|
+
if (saved?.unmatched.length) lines.push(` ${saved.unmatched.length} saved prediction${saved.unmatched.length === 1 ? "" : "s"} matched no row`);
|
|
472
|
+
if (saved?.duplicates.length) lines.push(` ${saved.duplicates.length} key${saved.duplicates.length === 1 ? "" : "s"} predicted twice, not used`);
|
|
473
|
+
if (r.headline) {
|
|
474
|
+
lines.push(` accuracy ${pct(r.accuracy)} [95% ${r.interval ? `${pct(r.interval[0])}–${pct(r.interval[1])}` : "–"}], ${r.averaging} precision ${pct(r.headline.precision)} recall ${pct(r.headline.recall)} F1 ${pct(r.headline.f1)}`);
|
|
475
|
+
if (r.sample?.small) lines.push(` ${r.sample.note}`);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
for (const p of c.pairs) if (p.labeled !== undefined) lines.push(` ${p.a} vs ${p.b}: both right ${p.bothRight}, only ${p.a} ${p.onlyA}, only ${p.b} ${p.onlyB}, both wrong ${p.bothWrong} (of ${p.labeled})`);
|
|
479
|
+
return lines;
|
|
480
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// Evaluation datasets (src/tests/evaluate.js): rows from a test's inline
|
|
2
|
+
// `dataset.rows`, or from a pane's value (a media pane holding a CSV or JSON
|
|
3
|
+
// file, a text pane, an eval pane returning an array), and `$row.column`
|
|
4
|
+
// references in a test's inputs, expect and metrics. Pure; no DOM.
|
|
5
|
+
|
|
6
|
+
/** Most rows one evaluation runs. */
|
|
7
|
+
export const MAX_ROWS = 5000;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* CSV text to rows: the first line is the header, each later line one row
|
|
11
|
+
* `{ column: text }`. Quoted fields ("a, b", "say ""hi""", line breaks
|
|
12
|
+
* inside quotes) follow RFC 4180; cells stay text. Blank lines are skipped.
|
|
13
|
+
*/
|
|
14
|
+
export function parseCsv(text) {
|
|
15
|
+
const records = [];
|
|
16
|
+
let record = [];
|
|
17
|
+
let field = "";
|
|
18
|
+
let quoted = false;
|
|
19
|
+
let i = 0;
|
|
20
|
+
const src = String(text ?? "").replace(/^/, "");
|
|
21
|
+
const delimiter = guessDelimiter(src);
|
|
22
|
+
const endField = () => (record.push(field), (field = ""));
|
|
23
|
+
const endRecord = () => {
|
|
24
|
+
endField();
|
|
25
|
+
if (record.length > 1 || record[0] !== "") records.push(record);
|
|
26
|
+
record = [];
|
|
27
|
+
};
|
|
28
|
+
while (i < src.length) {
|
|
29
|
+
const ch = src[i];
|
|
30
|
+
if (quoted) {
|
|
31
|
+
if (ch === '"' && src[i + 1] === '"') (field += '"'), (i += 2);
|
|
32
|
+
else if (ch === '"') (quoted = false), i++;
|
|
33
|
+
else (field += ch), i++;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (ch === '"' && field === "") (quoted = true), i++;
|
|
37
|
+
else if (ch === delimiter) endField(), i++;
|
|
38
|
+
else if (ch === "\r" && src[i + 1] === "\n") endRecord(), (i += 2);
|
|
39
|
+
else if (ch === "\n" || ch === "\r") endRecord(), i++;
|
|
40
|
+
else (field += ch), i++;
|
|
41
|
+
}
|
|
42
|
+
if (quoted) throw new Error("the CSV has a quote that is never closed");
|
|
43
|
+
if (field !== "" || record.length) endRecord();
|
|
44
|
+
if (!records.length) return [];
|
|
45
|
+
const header = records[0].map((h, n) => h.trim() || `column${n + 1}`);
|
|
46
|
+
return records.slice(1).map((cells) => Object.fromEntries(header.map((h, n) => [h, cells[n] ?? ""])));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A comma, unless the header line has more tabs or semicolons than commas. */
|
|
50
|
+
function guessDelimiter(text) {
|
|
51
|
+
const first = text.slice(0, text.search(/\r|\n|$/));
|
|
52
|
+
const count = (c) => first.split(c).length - 1;
|
|
53
|
+
const best = [",", "\t", ";"].map((c) => [c, count(c)]).sort((a, b) => b[1] - a[1])[0];
|
|
54
|
+
return best[1] > 0 ? best[0] : ",";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A list of rows from a JSON value: an array, or `{ rows: [...] }`. A row that isn't an object becomes `{ value }`. */
|
|
58
|
+
export function rowsFromValue(value) {
|
|
59
|
+
const list = Array.isArray(value) ? value : Array.isArray(value?.rows) ? value.rows : null;
|
|
60
|
+
if (!list) throw new Error(`a dataset is a list of rows (an array, or { "rows": [...] }), not ${describe(value)}`);
|
|
61
|
+
if (list.length > MAX_ROWS) throw new Error(`the dataset has ${list.length} rows; an evaluation runs at most ${MAX_ROWS}`);
|
|
62
|
+
return list.map((row) => (row && typeof row === "object" && !Array.isArray(row) ? row : { value: row }));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const describe = (v) => (v === null ? "null" : Array.isArray(v) ? "an array" : typeof v === "object" ? "an object without rows" : `a ${typeof v}`);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Rows from a file's text: JSON (an array, or `{ rows }`), JSON Lines (one
|
|
69
|
+
* object per line), or else CSV. `type` (a MIME type) or `name` decide when
|
|
70
|
+
* they say; otherwise the text is tried as JSON first.
|
|
71
|
+
*/
|
|
72
|
+
export function parseDataset(text, { type = "", name = "" } = {}) {
|
|
73
|
+
const t = String(text ?? "");
|
|
74
|
+
const csv = /csv|tab-separated/.test(type) || /\.(csv|tsv)$/i.test(name);
|
|
75
|
+
const jsonl = /jsonl|ndjson/.test(type) || /\.(jsonl|ndjson)$/i.test(name);
|
|
76
|
+
if (!csv && !jsonl) {
|
|
77
|
+
try {
|
|
78
|
+
return rowsFromValue(JSON.parse(t));
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (/json/.test(type) || /\.json$/i.test(name) || !(error instanceof SyntaxError)) throw new Error(`the dataset isn't valid: ${error.message}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!csv) {
|
|
84
|
+
const lines = t.split(/\r?\n/).filter((l) => l.trim());
|
|
85
|
+
try {
|
|
86
|
+
if (lines.length && lines.every((l) => /^\s*\{/.test(l))) return rowsFromValue(lines.map((l) => JSON.parse(l)));
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (jsonl) throw new Error(`the dataset isn't valid JSON Lines: ${error.message}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return rowsFromValue(parseCsv(t));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Base64 to text (UTF-8). */
|
|
95
|
+
export function base64Text(data) {
|
|
96
|
+
const binary = atob(String(data));
|
|
97
|
+
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
98
|
+
return new TextDecoder().decode(bytes);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ── $row references ─────────────────────────────────────────────────────────
|
|
102
|
+
|
|
103
|
+
const REF = /^\$row(?:\.(.+))?$/;
|
|
104
|
+
|
|
105
|
+
/** A row's value at a dotted path (`text`, `meta.label`, `tags.0`); undefined when it isn't there. */
|
|
106
|
+
export function pick(value, path) {
|
|
107
|
+
if (path === undefined || path === "") return value;
|
|
108
|
+
let v = value;
|
|
109
|
+
for (const part of String(path).split(".")) {
|
|
110
|
+
if (v === null || v === undefined || typeof v !== "object") return undefined;
|
|
111
|
+
v = v[part];
|
|
112
|
+
}
|
|
113
|
+
return v;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Replace `$row.column` references with the row's values, anywhere in a
|
|
118
|
+
* value (objects and arrays included). A reference is a whole string
|
|
119
|
+
* (`"$row.label"`, or `"$row"` for the whole row); text around it isn't
|
|
120
|
+
* interpolated. `"$$row.x"` stands for the literal text `"$row.x"`.
|
|
121
|
+
*/
|
|
122
|
+
export function withRow(value, row) {
|
|
123
|
+
if (typeof value === "string") {
|
|
124
|
+
if (value.startsWith("$$row")) return value.slice(1);
|
|
125
|
+
const m = REF.exec(value);
|
|
126
|
+
if (!m) return value;
|
|
127
|
+
const found = pick(row, m[1]);
|
|
128
|
+
return found === undefined ? null : found;
|
|
129
|
+
}
|
|
130
|
+
if (Array.isArray(value)) return value.map((v) => withRow(v, row));
|
|
131
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, withRow(v, row)]));
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The `$row.column` names a value refers to. */
|
|
136
|
+
export function rowRefs(value, out = new Set()) {
|
|
137
|
+
if (typeof value === "string") {
|
|
138
|
+
const m = !value.startsWith("$$") && REF.exec(value);
|
|
139
|
+
if (m) out.add(m[1] ?? "");
|
|
140
|
+
} else if (value && typeof value === "object") for (const v of Object.values(value)) rowRefs(v, out);
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A dataset row from the canvas as it is now (`inputs`: the input panes'
|
|
146
|
+
* values, `values`: other panes' values, by name): each test input written as
|
|
147
|
+
* `"$row.column"` gives that column the input's value, and each expected pane
|
|
148
|
+
* or metric `against` written as `"$row.column"` gives it that pane's value
|
|
149
|
+
* (at the metric's `path`). Other references are left out.
|
|
150
|
+
*/
|
|
151
|
+
export function rowFromCurrent(test, { inputs = {}, values = {} } = {}) {
|
|
152
|
+
const column = (template) => {
|
|
153
|
+
const m = typeof template === "string" && !template.startsWith("$$") ? REF.exec(template) : null;
|
|
154
|
+
return m?.[1] && !m[1].includes(".") ? m[1] : undefined;
|
|
155
|
+
};
|
|
156
|
+
const row = {};
|
|
157
|
+
for (const [pane, template] of Object.entries(test.inputs ?? {})) {
|
|
158
|
+
const col = column(template);
|
|
159
|
+
if (col && Object.hasOwn(inputs, pane)) row[col] = inputs[pane];
|
|
160
|
+
}
|
|
161
|
+
for (const [pane, expected] of Object.entries(test.expect ?? {})) {
|
|
162
|
+
const col = column(expected) ?? column(expected?.equals);
|
|
163
|
+
if (col && !Object.hasOwn(row, col) && Object.hasOwn(values, pane)) row[col] = values[pane];
|
|
164
|
+
}
|
|
165
|
+
for (const m of test.metrics ?? []) {
|
|
166
|
+
const col = column(m.against);
|
|
167
|
+
if (col && !Object.hasOwn(row, col) && Object.hasOwn(values, m.pane)) row[col] = pick(values[m.pane], m.path);
|
|
168
|
+
}
|
|
169
|
+
return row;
|
|
170
|
+
}
|