@zergai/ztc 0.4.0-beta.1 → 0.5.0-beta.1
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 +398 -45
- package/RUST_THIRD_PARTY_LICENSES.txt +1080 -822
- package/THIRD_PARTY_NOTICES.md +273 -12
- package/bin/zmux-darwin-arm64 +0 -0
- package/bin/ztc-audio-darwin-arm64 +0 -0
- package/bin/ztc-capture-darwin-arm64 +0 -0
- package/bin/ztc-computer-darwin-arm64 +0 -0
- package/bin/ztc-llm-darwin-arm64 +0 -0
- package/bin/ztc-tts-darwin-arm64 +0 -0
- package/bin/ztc-tui-darwin-arm64 +0 -0
- package/bin/ztc-zlicky-overlay-darwin-arm64 +0 -0
- package/bin/zterm-darwin-arm64 +0 -0
- package/dist/App.js +1206 -209
- package/dist/agent/agent.d.ts +74 -8
- package/dist/agent/agent.js +1073 -184
- package/dist/agent/backends/account_rotation.d.ts +4 -0
- package/dist/agent/backends/account_rotation.js +8 -2
- package/dist/agent/backends/anthropic.d.ts +13 -0
- package/dist/agent/backends/anthropic.js +169 -28
- package/dist/agent/backends/claude_cli.js +18 -8
- package/dist/agent/backends/codex_app_server.d.ts +1 -0
- package/dist/agent/backends/codex_app_server.js +55 -3
- package/dist/agent/backends/codex_auth.d.ts +1 -0
- package/dist/agent/backends/codex_auth.js +67 -13
- package/dist/agent/backends/codex_buffered_recovery.d.ts +27 -0
- package/dist/agent/backends/codex_buffered_recovery.js +293 -0
- package/dist/agent/backends/gemini.js +3 -1
- package/dist/agent/backends/http_error.d.ts +26 -0
- package/dist/agent/backends/http_error.js +221 -0
- package/dist/agent/backends/index.d.ts +9 -2
- package/dist/agent/backends/index.js +22 -2
- package/dist/agent/backends/openai.d.ts +0 -6
- package/dist/agent/backends/openai.js +5 -2
- package/dist/agent/backends/openai_compatible.d.ts +5 -4
- package/dist/agent/backends/openai_compatible.js +26 -19
- package/dist/agent/backends/openai_responses.d.ts +8 -3
- package/dist/agent/backends/openai_responses.js +77 -33
- package/dist/agent/backends/provider_ids.d.ts +1 -0
- package/dist/agent/backends/provider_ids.js +4 -0
- package/dist/agent/backends/request_metrics.js +12 -2
- package/dist/agent/backends/types.d.ts +109 -6
- package/dist/agent/backends/types.js +5 -1
- package/dist/agent/backends/zrouter_stream.d.ts +6 -1
- package/dist/agent/backends/zrouter_stream.js +160 -42
- package/dist/agent/background_registry.d.ts +2 -0
- package/dist/agent/background_registry.js +1 -0
- package/dist/agent/claim_observation.d.ts +6 -0
- package/dist/agent/claim_observation.js +54 -0
- package/dist/agent/commands/accounts.js +46 -177
- package/dist/agent/commands/attention.d.ts +2 -0
- package/dist/agent/commands/attention.js +67 -0
- package/dist/agent/commands/auth.js +18 -2
- package/dist/agent/commands/board.d.ts +2 -0
- package/dist/agent/commands/board.js +169 -0
- package/dist/agent/commands/clipboard.js +50 -1
- package/dist/agent/commands/compact.js +21 -13
- package/dist/agent/commands/config.js +4 -1
- package/dist/agent/commands/context.js +6 -1
- package/dist/agent/commands/desktop.d.ts +2 -0
- package/dist/agent/commands/desktop.js +29 -0
- package/dist/agent/commands/effort.js +14 -6
- package/dist/agent/commands/environment.d.ts +3 -0
- package/dist/agent/commands/environment.js +109 -0
- package/dist/agent/commands/evidence.d.ts +2 -0
- package/dist/agent/commands/evidence.js +9 -0
- package/dist/agent/commands/fork.js +4 -3
- package/dist/agent/commands/goal.d.ts +33 -1
- package/dist/agent/commands/goal.js +174 -14
- package/dist/agent/commands/goal_exit.d.ts +36 -0
- package/dist/agent/commands/goal_exit.js +120 -0
- package/dist/agent/commands/help.js +3 -1
- package/dist/agent/commands/history.js +3 -1
- package/dist/agent/commands/inbox.d.ts +2 -0
- package/dist/agent/commands/inbox.js +148 -0
- package/dist/agent/commands/index.js +16 -1
- package/dist/agent/commands/input_mode.js +8 -6
- package/dist/agent/commands/job.d.ts +2 -0
- package/dist/agent/commands/job.js +93 -0
- package/dist/agent/commands/keybindings.js +4 -1
- package/dist/agent/commands/label.d.ts +2 -0
- package/dist/agent/commands/label.js +77 -0
- package/dist/agent/commands/layout.d.ts +2 -0
- package/dist/agent/commands/layout.js +20 -0
- package/dist/agent/commands/mirror.js +16 -1
- package/dist/agent/commands/model.js +12 -2
- package/dist/agent/commands/permissions.d.ts +3 -1
- package/dist/agent/commands/permissions.js +79 -89
- package/dist/agent/commands/plan.js +5 -15
- package/dist/agent/commands/profile.js +21 -2
- package/dist/agent/commands/provider_selection.js +20 -1
- package/dist/agent/commands/rename.js +1 -1
- package/dist/agent/commands/resources.d.ts +2 -0
- package/dist/agent/commands/resources.js +67 -0
- package/dist/agent/commands/rewind.d.ts +3 -0
- package/dist/agent/commands/rewind.js +16 -0
- package/dist/agent/commands/runbooks.d.ts +2 -0
- package/dist/agent/commands/runbooks.js +73 -0
- package/dist/agent/commands/runbooks_lazy.d.ts +2 -0
- package/dist/agent/commands/runbooks_lazy.js +8 -0
- package/dist/agent/commands/sessions.d.ts +1 -1
- package/dist/agent/commands/sessions.js +33 -5
- package/dist/agent/commands/settings.js +185 -39
- package/dist/agent/commands/setup.d.ts +2 -0
- package/dist/agent/commands/setup.js +78 -0
- package/dist/agent/commands/share.d.ts +2 -0
- package/dist/agent/commands/share.js +64 -0
- package/dist/agent/commands/status.js +14 -2
- package/dist/agent/commands/types.d.ts +99 -6
- package/dist/agent/commands/update.js +8 -0
- package/dist/agent/commands/usage.d.ts +2 -0
- package/dist/agent/commands/usage.js +8 -0
- package/dist/agent/factory.d.ts +11 -0
- package/dist/agent/factory.js +113 -88
- package/dist/agent/loop_policy.d.ts +6 -3
- package/dist/agent/loop_policy.js +12 -3
- package/dist/agent/measurement_guidance.d.ts +2 -0
- package/dist/agent/measurement_guidance.js +5 -0
- package/dist/agent/request_encoder.d.ts +8 -1
- package/dist/agent/request_encoder.js +95 -23
- package/dist/agent/runtime/benchmark_comparison.d.ts +30 -0
- package/dist/agent/runtime/benchmark_comparison.js +94 -0
- package/dist/agent/runtime/benchmark_contract.d.ts +48 -0
- package/dist/agent/runtime/benchmark_contract.js +90 -0
- package/dist/agent/runtime/checked_commands.d.ts +32 -0
- package/dist/agent/runtime/checked_commands.js +119 -0
- package/dist/agent/runtime/command_execution_policy.d.ts +26 -0
- package/dist/agent/runtime/command_execution_policy.js +91 -0
- package/dist/agent/runtime/command_jobs.d.ts +36 -18
- package/dist/agent/runtime/command_jobs.js +151 -13
- package/dist/agent/runtime/command_panes.d.ts +59 -0
- package/dist/agent/runtime/command_panes.js +398 -0
- package/dist/agent/runtime/command_wait.d.ts +19 -0
- package/dist/agent/runtime/command_wait.js +167 -0
- package/dist/agent/runtime/cpu_benchmark.d.ts +30 -0
- package/dist/agent/runtime/cpu_benchmark.js +126 -0
- package/dist/agent/runtime/delegated_evidence.d.ts +26 -0
- package/dist/agent/runtime/delegated_evidence.js +80 -0
- package/dist/agent/runtime/evidence.d.ts +127 -0
- package/dist/agent/runtime/evidence.js +468 -0
- package/dist/agent/runtime/execution_context.d.ts +7 -0
- package/dist/agent/runtime/execution_context.js +39 -3
- package/dist/agent/runtime/json_verifier_recipe.d.ts +8 -0
- package/dist/agent/runtime/json_verifier_recipe.js +66 -0
- package/dist/agent/runtime/measurements.d.ts +110 -0
- package/dist/agent/runtime/measurements.js +317 -0
- package/dist/agent/runtime/policy.d.ts +4 -0
- package/dist/agent/runtime/policy.js +31 -20
- package/dist/agent/runtime/provider_native_evidence.d.ts +20 -0
- package/dist/agent/runtime/provider_native_evidence.js +86 -0
- package/dist/agent/runtime/recovery.d.ts +2 -0
- package/dist/agent/runtime/recovery.js +45 -2
- package/dist/agent/runtime/remote_broker_transport.d.ts +10 -0
- package/dist/agent/runtime/remote_broker_transport.js +84 -0
- package/dist/agent/runtime/remote_deletion_receipts.d.ts +3 -0
- package/dist/agent/runtime/remote_deletion_receipts.js +43 -0
- package/dist/agent/runtime/remote_journal_replay.d.ts +13 -0
- package/dist/agent/runtime/remote_journal_replay.js +233 -0
- package/dist/agent/runtime/remote_mutation_journal.d.ts +25 -0
- package/dist/agent/runtime/remote_mutation_journal.js +208 -0
- package/dist/agent/runtime/remote_resource_endpoint.d.ts +7 -0
- package/dist/agent/runtime/remote_resource_endpoint.js +6 -0
- package/dist/agent/runtime/remote_resource_errors.d.ts +5 -0
- package/dist/agent/runtime/remote_resource_errors.js +9 -0
- package/dist/agent/runtime/remote_resource_script.d.ts +2 -0
- package/dist/agent/runtime/remote_resource_script.js +1009 -0
- package/dist/agent/runtime/remote_resources.d.ts +29 -0
- package/dist/agent/runtime/remote_resources.js +160 -0
- package/dist/agent/runtime/source_verification.d.ts +12 -0
- package/dist/agent/runtime/source_verification.js +210 -0
- package/dist/agent/runtime/source_verification_contract.d.ts +75 -0
- package/dist/agent/runtime/source_verification_contract.js +203 -0
- package/dist/agent/runtime/tool_checkpoints.d.ts +11 -0
- package/dist/agent/runtime/tool_checkpoints.js +26 -0
- package/dist/agent/runtime/tool_effects.d.ts +5 -0
- package/dist/agent/runtime/tool_progress.d.ts +12 -0
- package/dist/agent/runtime/tool_progress.js +76 -0
- package/dist/agent/runtime/tool_runtime.d.ts +16 -1
- package/dist/agent/runtime/tool_runtime.js +204 -10
- package/dist/agent/runtime/tracing.d.ts +1 -1
- package/dist/agent/runtime/verifier_capture.d.ts +9 -0
- package/dist/agent/runtime/verifier_capture.js +100 -0
- package/dist/agent/runtime/verifier_contract.d.ts +24 -0
- package/dist/agent/runtime/verifier_contract.js +41 -0
- package/dist/agent/subagent.d.ts +20 -10
- package/dist/agent/subagent.js +266 -29
- package/dist/agent/tool_response_directory.d.ts +24 -0
- package/dist/agent/tool_response_directory.js +61 -0
- package/dist/agent/tool_response_source.d.ts +8 -0
- package/dist/agent/tool_response_source.js +49 -0
- package/dist/agent/tool_response_store.d.ts +75 -4
- package/dist/agent/tool_response_store.js +583 -64
- package/dist/agent/tools/ask_user.d.ts +3 -1
- package/dist/agent/tools/ask_user.js +30 -11
- package/dist/agent/tools/background.js +1 -0
- package/dist/agent/tools/benchmark_cpu.d.ts +2 -0
- package/dist/agent/tools/benchmark_cpu.js +102 -0
- package/dist/agent/tools/compare_benchmarks.d.ts +2 -0
- package/dist/agent/tools/compare_benchmarks.js +53 -0
- package/dist/agent/tools/deferred.js +1 -0
- package/dist/agent/tools/delegate.js +18 -2
- package/dist/agent/tools/helper_requirements.d.ts +7 -0
- package/dist/agent/tools/helper_requirements.js +19 -0
- package/dist/agent/tools/index.d.ts +1 -0
- package/dist/agent/tools/index.js +3 -0
- package/dist/agent/tools/measurement_report.d.ts +22 -0
- package/dist/agent/tools/measurement_report.js +190 -0
- package/dist/agent/tools/pane.d.ts +0 -7
- package/dist/agent/tools/pane.js +5 -4
- package/dist/agent/tools/project_context.d.ts +3 -0
- package/dist/agent/tools/project_context.js +45 -0
- package/dist/agent/tools/recall_tool_response.d.ts +3 -5
- package/dist/agent/tools/recall_tool_response.js +19 -29
- package/dist/agent/tools/registry.d.ts +1 -0
- package/dist/agent/tools/registry.js +23 -0
- package/dist/agent/tools/related_sessions.d.ts +2 -0
- package/dist/agent/tools/related_sessions.js +156 -0
- package/dist/agent/tools/routine.js +13 -3
- package/dist/agent/tools/session_evidence.d.ts +3 -0
- package/dist/agent/tools/session_evidence.js +72 -0
- package/dist/agent/tools/shell.d.ts +4 -3
- package/dist/agent/tools/shell.js +174 -47
- package/dist/agent/tools/tool_search.js +3 -2
- package/dist/agent/tools/types.d.ts +28 -0
- package/dist/agent/tools/verify_artifact.d.ts +2 -0
- package/dist/agent/tools/verify_artifact.js +60 -0
- package/dist/agent/tools/workflow.js +1 -0
- package/dist/app/activity.d.ts +6 -1
- package/dist/app/activity.js +81 -12
- package/dist/app/agent_runner.d.ts +6 -1
- package/dist/app/agent_runner.js +34 -19
- package/dist/app/command_context.js +106 -10
- package/dist/app/controller.d.ts +15 -4
- package/dist/app/controller.js +29 -3
- package/dist/app/escape_stop.d.ts +11 -0
- package/dist/app/escape_stop.js +15 -0
- package/dist/app/liveness.d.ts +6 -4
- package/dist/app/liveness.js +13 -3
- package/dist/app/model_catalog_cache.d.ts +5 -0
- package/dist/app/model_catalog_cache.js +138 -0
- package/dist/app/ordered_transcript.js +43 -4
- package/dist/app/prompt_turn.d.ts +14 -0
- package/dist/app/prompt_turn.js +53 -0
- package/dist/app/recap.d.ts +3 -0
- package/dist/app/recap.js +9 -3
- package/dist/app/restart.d.ts +3 -2
- package/dist/app/restart.js +22 -7
- package/dist/app/rewind.d.ts +3 -3
- package/dist/app/rewind.js +3 -3
- package/dist/app/selection_completion.d.ts +17 -2
- package/dist/app/selection_completion.js +84 -9
- package/dist/app/session_manager.d.ts +38 -6
- package/dist/app/session_manager.js +263 -60
- package/dist/app/session_messages.d.ts +3 -0
- package/dist/app/session_messages.js +60 -0
- package/dist/app/task_scope_router.d.ts +20 -0
- package/dist/app/task_scope_router.js +173 -0
- package/dist/app/transcript_window.d.ts +2 -0
- package/dist/app/transcript_window.js +4 -0
- package/dist/app/workspace_admission_details.d.ts +3 -0
- package/dist/app/workspace_admission_details.js +26 -0
- package/dist/app/workspace_layout_controller.d.ts +11 -0
- package/dist/app/workspace_layout_controller.js +88 -0
- package/dist/app/workspace_monitor.d.ts +1 -0
- package/dist/app/workspace_monitor.js +216 -0
- package/dist/app/workspace_presets.d.ts +29 -0
- package/dist/app/workspace_presets.js +192 -0
- package/dist/attention/queue.d.ts +37 -0
- package/dist/attention/queue.js +183 -0
- package/dist/bench/rf_shared.d.ts +2 -0
- package/dist/board/binding.d.ts +42 -0
- package/dist/board/binding.js +38 -0
- package/dist/board/client.d.ts +42 -0
- package/dist/board/client.js +60 -0
- package/dist/cli.js +1 -1
- package/dist/cli_args.js +19 -17
- package/dist/cli_main.js +110 -13
- package/dist/cli_ratatui.d.ts +183 -16
- package/dist/cli_ratatui.js +3989 -1255
- package/dist/components/ActivityLine.d.ts +3 -2
- package/dist/components/ActivityLine.js +2 -2
- package/dist/components/InputArea.d.ts +19 -1
- package/dist/components/InputArea.js +264 -190
- package/dist/components/MessageList.d.ts +2 -0
- package/dist/components/MessageList.js +3 -1
- package/dist/components/SingleMessage.d.ts +2 -0
- package/dist/components/SingleMessage.js +4 -2
- package/dist/components/StatusBar.d.ts +4 -2
- package/dist/components/StatusBar.js +3 -1
- package/dist/conductor_branch.js +3 -0
- package/dist/config/api_retry.d.ts +6 -1
- package/dist/config/api_retry.js +19 -1
- package/dist/config/defaults.d.ts +2 -1
- package/dist/config/defaults.js +9 -2
- package/dist/config/message_display.d.ts +1 -0
- package/dist/config/message_display.js +17 -2
- package/dist/config/permissions.d.ts +21 -0
- package/dist/config/permissions.js +89 -0
- package/dist/config/resolved.d.ts +2 -2
- package/dist/config/types.d.ts +32 -5
- package/dist/config.d.ts +38 -4
- package/dist/config.js +290 -42
- package/dist/context/project_intelligence.d.ts +44 -0
- package/dist/context/project_intelligence.js +246 -0
- package/dist/desktop/cli_launch.d.ts +8 -0
- package/dist/desktop/cli_launch.js +6 -0
- package/dist/desktop/cli_shim.d.ts +8 -0
- package/dist/desktop/cli_shim.js +120 -0
- package/dist/desktop/runtime.d.ts +16 -0
- package/dist/desktop/runtime.js +161 -0
- package/dist/desktop/worker_launch.d.ts +7 -0
- package/dist/desktop/worker_launch.js +7 -0
- package/dist/diagnostics/cargo_mutants_selection.d.ts +27 -0
- package/dist/diagnostics/cargo_mutants_selection.js +48 -0
- package/dist/diagnostics/native_terminal_benchmark.d.ts +58 -0
- package/dist/diagnostics/native_terminal_benchmark.js +1247 -0
- package/dist/diagnostics/session_evidence_package.d.ts +40 -0
- package/dist/diagnostics/session_evidence_package.js +234 -0
- package/dist/diagnostics/session_export.d.ts +93 -0
- package/dist/diagnostics/session_export.js +634 -0
- package/dist/emulation/tool_adapters.js +48 -18
- package/dist/emulation/ztc_addendum.d.ts +1 -0
- package/dist/emulation/ztc_addendum.js +13 -12
- package/dist/environment/service_runner.d.ts +1 -0
- package/dist/environment/service_runner.js +68 -0
- package/dist/environment/store.d.ts +120 -0
- package/dist/environment/store.js +387 -0
- package/dist/events.d.ts +40 -1
- package/dist/events.js +10 -17
- package/dist/goal_contract.d.ts +25 -0
- package/dist/goal_contract.js +44 -0
- package/dist/headless.d.ts +31 -0
- package/dist/headless.js +578 -70
- package/dist/headless_cli.js +8 -0
- package/dist/headless_cli_args.d.ts +1 -0
- package/dist/headless_cli_args.js +1 -0
- package/dist/intake/cli.d.ts +1 -0
- package/dist/intake/cli.js +33 -0
- package/dist/intake/format.d.ts +2 -0
- package/dist/intake/format.js +14 -0
- package/dist/intake/notifications.d.ts +16 -0
- package/dist/intake/notifications.js +132 -0
- package/dist/intake/store.d.ts +56 -0
- package/dist/intake/store.js +238 -0
- package/dist/library.d.ts +8 -0
- package/dist/library.js +19 -10
- package/dist/mcp/client.d.ts +1 -0
- package/dist/mcp/client.js +20 -0
- package/dist/modes/builtin/plan.yaml +2 -2
- package/dist/modes/handlers.js +1 -1
- package/dist/onboarding/first_run.d.ts +3 -0
- package/dist/onboarding/first_run.js +33 -25
- package/dist/onboarding/setup.d.ts +32 -0
- package/dist/onboarding/setup.js +133 -0
- package/dist/onboarding/setup_agent_health.d.ts +17 -0
- package/dist/onboarding/setup_agent_health.js +79 -0
- package/dist/onboarding/setup_environment.d.ts +66 -0
- package/dist/onboarding/setup_environment.js +219 -0
- package/dist/onboarding/setup_workspace.d.ts +17 -0
- package/dist/onboarding/setup_workspace.js +129 -0
- package/dist/optimize/adapters/index.d.ts +4 -1
- package/dist/optimize/adapters/index.js +7 -0
- package/dist/optimize/adapters/kernelbench.js +14 -5
- package/dist/optimize/adapters/popcorn.js +3 -2
- package/dist/optimize/adapters/profile_benchmark.js +13 -7
- package/dist/optimize/adapters/rfbench.js +25 -14
- package/dist/optimize/adapters/types.d.ts +3 -0
- package/dist/optimize/adapters/zergpod.js +5 -2
- package/dist/optimize/baseline.js +10 -21
- package/dist/optimize/branch.js +9 -12
- package/dist/optimize/combine.js +2 -3
- package/dist/optimize/executor.js +34 -48
- package/dist/optimize/measurements.d.ts +55 -0
- package/dist/optimize/measurements.js +173 -0
- package/dist/optimize/results.d.ts +1 -0
- package/dist/optimize/results.js +11 -33
- package/dist/optimize/tge.js +2 -1
- package/dist/panes/initial_input.d.ts +19 -0
- package/dist/panes/initial_input.js +206 -0
- package/dist/panes/labels.d.ts +16 -0
- package/dist/panes/labels.js +42 -0
- package/dist/panes/model_catalog.d.ts +3 -3
- package/dist/panes/model_catalog.js +28 -4
- package/dist/panes/pane_registry.js +6 -2
- package/dist/panes/types.d.ts +3 -0
- package/dist/panes/ztc_launch.js +19 -0
- package/dist/plugin/optimization.d.ts +2 -0
- package/dist/plugin/optimization.js +1 -0
- package/dist/plugin/runtime.d.ts +2 -0
- package/dist/plugin/simulation.d.ts +6 -1
- package/dist/plugin/simulation_contract.d.ts +836 -1
- package/dist/plugin/simulation_documents.d.ts +22 -0
- package/dist/plugin/simulation_documents.js +1 -0
- package/dist/plugin/simulation_interventions.d.ts +103 -0
- package/dist/plugin/simulation_interventions.js +1 -0
- package/dist/plugin/simulation_living.d.ts +1 -0
- package/dist/plugin/simulation_living.js +3 -0
- package/dist/plugin/simulation_meetings.d.ts +121 -0
- package/dist/plugin/simulation_meetings.js +1 -0
- package/dist/plugin/simulation_story_review.d.ts +111 -0
- package/dist/plugin/simulation_story_review.js +1 -0
- package/dist/plugin/simulation_storyteller.d.ts +91 -0
- package/dist/plugin/simulation_storyteller.js +1 -0
- package/dist/plugin/simulation_structured.d.ts +2 -0
- package/dist/plugin/simulation_structured.js +2 -0
- package/dist/plugin/simulation_toolkit.d.ts +21 -0
- package/dist/plugin/simulation_toolkit.js +24 -0
- package/dist/prompts/tool_delegate_task.j2 +1 -1
- package/dist/remote-broker/Dockerfile +10 -0
- package/dist/remote-broker/archive.py +1227 -0
- package/dist/remote-broker/cutover.py +761 -0
- package/dist/remote-broker/requirements.txt +5 -0
- package/dist/remote-broker/resources.json +6 -0
- package/dist/remote-broker/resources.write-staging.example.json +18 -0
- package/dist/remote-broker/retained.py +686 -0
- package/dist/remote-broker/retained_cleanup.py +550 -0
- package/dist/remote-broker/server.py +648 -0
- package/dist/remote-broker/worker.py +1007 -0
- package/dist/router_auth.d.ts +31 -0
- package/dist/router_auth.js +155 -9
- package/dist/runbooks/catalog.d.ts +3 -0
- package/dist/runbooks/catalog.js +26 -0
- package/dist/runbooks/library.d.ts +11 -0
- package/dist/runbooks/library.js +95 -0
- package/dist/runbooks/package.d.ts +43 -0
- package/dist/runbooks/package.js +102 -0
- package/dist/session.d.ts +65 -13
- package/dist/session.js +548 -43
- package/dist/session_handoff.d.ts +25 -0
- package/dist/session_handoff.js +322 -0
- package/dist/session_handoff_contract.d.ts +89 -0
- package/dist/session_handoff_contract.js +179 -0
- package/dist/session_media.d.ts +41 -0
- package/dist/session_media.js +357 -0
- package/dist/session_ownership.d.ts +16 -0
- package/dist/session_ownership.js +221 -0
- package/dist/session_public.d.ts +10 -2
- package/dist/session_public.js +6 -1
- package/dist/simulation/dynamic_history.d.ts +42 -0
- package/dist/simulation/dynamic_history.js +173 -0
- package/dist/simulation/engines/swarm/pane_manager.js +7 -6
- package/dist/simulation/inference.d.ts +3 -1
- package/dist/simulation/inference.js +19 -1
- package/dist/simulation/intervention_generation.d.ts +27 -0
- package/dist/simulation/intervention_generation.js +246 -0
- package/dist/simulation/intervention_material.d.ts +4 -0
- package/dist/simulation/intervention_material.js +81 -0
- package/dist/simulation/living/context.d.ts +3130 -0
- package/dist/simulation/living/context.js +944 -0
- package/dist/simulation/living/definer.d.ts +12 -0
- package/dist/simulation/living/definer.js +223 -0
- package/dist/simulation/living/definition.d.ts +257 -0
- package/dist/simulation/living/definition.js +341 -0
- package/dist/simulation/living/frame.d.ts +259 -0
- package/dist/simulation/living/frame.js +223 -0
- package/dist/simulation/living/identifiers.d.ts +2 -0
- package/dist/simulation/living/identifiers.js +12 -0
- package/dist/simulation/living/index.d.ts +15 -0
- package/dist/simulation/living/index.js +15 -0
- package/dist/simulation/living/inference.d.ts +219 -0
- package/dist/simulation/living/inference.js +576 -0
- package/dist/simulation/living/mechanics.d.ts +54 -0
- package/dist/simulation/living/mechanics.js +588 -0
- package/dist/simulation/living/model-input.d.ts +25 -0
- package/dist/simulation/living/model-input.js +215 -0
- package/dist/simulation/living/open-world.d.ts +257 -0
- package/dist/simulation/living/open-world.js +102 -0
- package/dist/simulation/living/projection.d.ts +7 -0
- package/dist/simulation/living/projection.js +31 -0
- package/dist/simulation/living/recall.d.ts +17 -0
- package/dist/simulation/living/recall.js +25 -0
- package/dist/simulation/living/reduce.d.ts +15 -0
- package/dist/simulation/living/reduce.js +1853 -0
- package/dist/simulation/living/replay.d.ts +6 -0
- package/dist/simulation/living/replay.js +22 -0
- package/dist/simulation/living/runtime.d.ts +140 -0
- package/dist/simulation/living/runtime.js +3020 -0
- package/dist/simulation/living/shared-kinds.d.ts +24 -0
- package/dist/simulation/living/shared-kinds.js +96 -0
- package/dist/simulation/living/types.d.ts +2785 -0
- package/dist/simulation/living/types.js +669 -0
- package/dist/simulation/llm.d.ts +3 -1
- package/dist/simulation/llm.js +90 -30
- package/dist/simulation/memory/embedding.d.ts +1 -1
- package/dist/simulation/memory/embedding.js +9 -3
- package/dist/simulation/memory/filesystem.d.ts +2 -0
- package/dist/simulation/memory/filesystem.js +5 -1
- package/dist/simulation/memory/filesystem_file_tree.d.ts +4 -1
- package/dist/simulation/memory/filesystem_file_tree.js +41 -10
- package/dist/simulation/memory/filesystem_invariants.js +1 -2
- package/dist/simulation/memory/filesystem_types.d.ts +1 -0
- package/dist/simulation/memory/memory.d.ts +2 -0
- package/dist/simulation/memory/memory.js +5 -1
- package/dist/simulation/orchestration/intent.js +0 -2
- package/dist/simulation/orchestration/lifecycle.js +0 -2
- package/dist/simulation/plugin_host.d.ts +25 -1
- package/dist/simulation/plugin_host.js +334 -17
- package/dist/simulation/session_documents.d.ts +20 -0
- package/dist/simulation/session_documents.js +62 -0
- package/dist/simulation/session_input_validation.d.ts +43 -0
- package/dist/simulation/session_input_validation.js +364 -0
- package/dist/simulation/session_inputs.d.ts +32 -0
- package/dist/simulation/session_inputs.js +154 -0
- package/dist/simulation/telemetry.d.ts +11 -14
- package/dist/simulation/telemetry.js +177 -54
- package/dist/simulation/toolkit/bounded_turns.d.ts +13 -0
- package/dist/simulation/toolkit/bounded_turns.js +80 -0
- package/dist/simulation/toolkit/dynamic_agents.d.ts +25 -0
- package/dist/simulation/toolkit/dynamic_agents.js +218 -0
- package/dist/simulation/toolkit/dynamic_digest.d.ts +1 -0
- package/dist/simulation/toolkit/dynamic_digest.js +8 -0
- package/dist/simulation/toolkit/dynamic_documents.d.ts +7 -0
- package/dist/simulation/toolkit/dynamic_documents.js +71 -0
- package/dist/simulation/toolkit/dynamic_driver.d.ts +8 -0
- package/dist/simulation/toolkit/dynamic_driver.js +269 -0
- package/dist/simulation/toolkit/dynamic_game.d.ts +53 -0
- package/dist/simulation/toolkit/dynamic_game.js +805 -0
- package/dist/simulation/toolkit/dynamic_knowledge.d.ts +15 -0
- package/dist/simulation/toolkit/dynamic_knowledge.js +91 -0
- package/dist/simulation/toolkit/dynamic_mechanics.d.ts +22 -0
- package/dist/simulation/toolkit/dynamic_mechanics.js +910 -0
- package/dist/simulation/toolkit/dynamic_meetings.d.ts +239 -0
- package/dist/simulation/toolkit/dynamic_meetings.js +350 -0
- package/dist/simulation/toolkit/dynamic_progress.d.ts +131 -0
- package/dist/simulation/toolkit/dynamic_progress.js +214 -0
- package/dist/simulation/toolkit/dynamic_projection.d.ts +9 -0
- package/dist/simulation/toolkit/dynamic_projection.js +219 -0
- package/dist/simulation/toolkit/dynamic_runtime.d.ts +412 -0
- package/dist/simulation/toolkit/dynamic_runtime.js +6536 -0
- package/dist/simulation/toolkit/dynamic_state.d.ts +111 -0
- package/dist/simulation/toolkit/dynamic_state.js +1967 -0
- package/dist/simulation/toolkit/dynamic_story_review.d.ts +508 -0
- package/dist/simulation/toolkit/dynamic_story_review.js +333 -0
- package/dist/simulation/toolkit/dynamic_storyteller.d.ts +77 -0
- package/dist/simulation/toolkit/dynamic_storyteller.js +114 -0
- package/dist/simulation/toolkit/dynamic_storyteller_runtime.d.ts +26 -0
- package/dist/simulation/toolkit/dynamic_storyteller_runtime.js +126 -0
- package/dist/simulation/toolkit/dynamic_world.d.ts +22 -0
- package/dist/simulation/toolkit/dynamic_world.js +377 -0
- package/dist/simulation/toolkit/structured_output.d.ts +5 -0
- package/dist/simulation/toolkit/structured_output.js +321 -0
- package/dist/simulation/types/world.d.ts +2 -0
- package/dist/simulation/world/artifact_store.js +1 -1
- package/dist/simulation/world/memory_manager.d.ts +5 -0
- package/dist/simulation/world/memory_manager.js +17 -2
- package/dist/simulation/world/state.d.ts +3 -2
- package/dist/simulation/world/state.js +15 -5
- package/dist/skills/bundled/measurement-validation/SKILL.md +144 -0
- package/dist/terminal_attach.d.ts +1 -0
- package/dist/terminal_attach.js +113 -0
- package/dist/tool_progress.d.ts +3 -0
- package/dist/tool_progress.js +27 -0
- package/dist/types.d.ts +145 -10
- package/dist/types.js +3 -0
- package/dist/ui/core/input.d.ts +3 -0
- package/dist/ui/core/input_segments.d.ts +18 -1
- package/dist/ui/core/input_segments.js +131 -47
- package/dist/ui/core/input_state.d.ts +12 -0
- package/dist/ui/core/layout_yoga.d.ts +1 -1
- package/dist/ui/core/turn_metadata.d.ts +5 -0
- package/dist/ui/core/turn_metadata.js +22 -0
- package/dist/ui/ink/render.d.ts +2 -1
- package/dist/ui/ink/render.js +6 -5
- package/dist/ui/ratatui/headless_pane_manager.d.ts +14 -1
- package/dist/ui/ratatui/headless_pane_manager.js +293 -104
- package/dist/ui/ratatui/index.d.ts +1 -0
- package/dist/ui/ratatui/input.d.ts +4 -0
- package/dist/ui/ratatui/input.js +15 -0
- package/dist/ui/ratatui/ipc_path.d.ts +2 -0
- package/dist/ui/ratatui/ipc_path.js +29 -1
- package/dist/ui/ratatui/mux_persistence.d.ts +3 -1
- package/dist/ui/ratatui/mux_persistence.js +139 -1
- package/dist/ui/ratatui/pane_relay.d.ts +1 -1
- package/dist/ui/ratatui/pane_relay.js +55 -27
- package/dist/ui/ratatui/process.d.ts +17 -1
- package/dist/ui/ratatui/process.js +80 -7
- package/dist/ui/ratatui/protocol.d.ts +236 -1
- package/dist/ui/ratatui/protocol.js +90 -0
- package/dist/ui/scrollback.d.ts +2 -1
- package/dist/ui/scrollback.js +9 -4
- package/dist/ui/stream_typewriter.d.ts +8 -3
- package/dist/ui/stream_typewriter.js +30 -1
- package/dist/ui/streaming_scrollback.d.ts +1 -0
- package/dist/ui/streaming_scrollback.js +1 -1
- package/dist/ui/transcript_render_cache.js +10 -0
- package/dist/ui/views/activity_line.d.ts +5 -3
- package/dist/ui/views/activity_line.js +65 -7
- package/dist/ui/views/app.d.ts +6 -3
- package/dist/ui/views/app.js +17 -6
- package/dist/ui/views/completion_menu.js +10 -5
- package/dist/ui/views/input_area.d.ts +7 -3
- package/dist/ui/views/input_area.js +15 -9
- package/dist/ui/views/message_list.d.ts +4 -1
- package/dist/ui/views/message_list.js +89 -19
- package/dist/ui/views/queue_panel.d.ts +3 -1
- package/dist/ui/views/queue_panel.js +9 -2
- package/dist/ui/views/status_bar.d.ts +4 -2
- package/dist/ui/views/status_bar.js +28 -13
- package/dist/ui/views/theme.d.ts +2 -0
- package/dist/ui/views/theme.js +2 -0
- package/dist/ui/widgets/color_panel.js +18 -0
- package/dist/ui/widgets/form.js +8 -0
- package/dist/ui/widgets/log_view.js +7 -0
- package/dist/ui/widgets/navigator_panel.js +8 -0
- package/dist/ui/widgets/prompt_viewer.js +2 -0
- package/dist/ui/widgets/questionnaire.d.ts +2 -0
- package/dist/ui/widgets/questionnaire.js +79 -16
- package/dist/ui/widgets/resume_panel.d.ts +2 -0
- package/dist/ui/widgets/resume_panel.js +26 -15
- package/dist/ui/widgets/rewind_panel.d.ts +2 -2
- package/dist/ui/widgets/rewind_panel.js +4 -2
- package/dist/ui/widgets/select.js +16 -27
- package/dist/ui/widgets/settings_panel.js +16 -0
- package/dist/ui/widgets/status_line_panel.d.ts +1 -0
- package/dist/ui/widgets/status_line_panel.js +30 -4
- package/dist/ui/widgets/text_input.d.ts +2 -0
- package/dist/ui/widgets/text_input.js +15 -1
- package/dist/ui/widgets/types.d.ts +2 -0
- package/dist/usage_projection.d.ts +24 -0
- package/dist/usage_projection.js +130 -0
- package/dist/usage_snapshot.d.ts +36 -0
- package/dist/usage_snapshot.js +131 -0
- package/dist/utils/assistant_message.d.ts +2 -0
- package/dist/utils/assistant_message.js +5 -0
- package/dist/utils/atomic_write.d.ts +6 -2
- package/dist/utils/atomic_write.js +29 -4
- package/dist/utils/checked_shell.d.ts +25 -0
- package/dist/utils/checked_shell.js +107 -0
- package/dist/utils/compaction.d.ts +4 -1
- package/dist/utils/compaction.js +31 -7
- package/dist/utils/context.d.ts +4 -4
- package/dist/utils/context.js +14 -12
- package/dist/utils/deepseek.d.ts +2 -2
- package/dist/utils/deepseek.js +13 -5
- package/dist/utils/ephemeral.js +4 -0
- package/dist/utils/ephemeral_tools.d.ts +18 -0
- package/dist/utils/ephemeral_tools.js +97 -0
- package/dist/utils/jev_client.d.ts +4 -0
- package/dist/utils/jev_client.js +15 -0
- package/dist/utils/model_cache.d.ts +15 -0
- package/dist/utils/model_cache.js +143 -0
- package/dist/utils/models.d.ts +4 -2
- package/dist/utils/models.js +12 -16
- package/dist/utils/reasoning.d.ts +8 -1
- package/dist/utils/reasoning.js +31 -11
- package/dist/utils/router_catalog.d.ts +49 -0
- package/dist/utils/router_catalog.js +271 -0
- package/dist/utils/sandbox.d.ts +9 -0
- package/dist/utils/sandbox.js +118 -15
- package/dist/utils/shell.d.ts +7 -0
- package/dist/utils/shell.js +98 -34
- package/dist/utils/subprocess_env.d.ts +3 -0
- package/dist/utils/subprocess_env.js +27 -1
- package/dist/web/accounts_mirror.d.ts +3 -1
- package/dist/web/accounts_mirror.js +13 -9
- package/dist/web/child_publication.d.ts +17 -0
- package/dist/web/child_publication.js +45 -0
- package/dist/web/mirror_authenticator.d.ts +31 -11
- package/dist/web/mirror_authenticator.js +67 -17
- package/dist/web/mirror_client.d.ts +31 -4
- package/dist/web/mirror_client.js +189 -23
- package/dist/web/mirror_stream.d.ts +84 -22
- package/dist/web/mirror_stream.js +676 -95
- package/dist/web/observer_query_redaction.d.ts +6 -0
- package/dist/web/observer_query_redaction.js +58 -0
- package/dist/web/pane_registry.d.ts +2 -0
- package/dist/web/pane_registry.js +86 -22
- package/dist/web/tool_image.d.ts +6 -0
- package/dist/web/tool_image.js +17 -0
- package/dist/web/view_plugin.d.ts +14 -0
- package/dist/web/view_plugin.js +23 -1
- package/dist/worker/executor.js +31 -12
- package/dist/worker/index.d.ts +1 -0
- package/dist/worker/toolsets.js +3 -1
- package/dist/worker/types.d.ts +12 -1
- package/dist/workflows/registry.js +1 -0
- package/dist/workflows/registry_handle.d.ts +2 -0
- package/dist/workflows/runtime.d.ts +2 -0
- package/dist/workflows/runtime.js +1 -0
- package/dist/workspace_agent.d.ts +7 -0
- package/dist/workspace_agent.js +36 -0
- package/dist/workspace_collision.d.ts +76 -0
- package/dist/workspace_collision.js +688 -0
- package/dist/workspace_layout.d.ts +18 -0
- package/dist/workspace_layout.js +121 -0
- package/dist/workspace_runtime.d.ts +44 -0
- package/dist/workspace_runtime.js +82 -0
- package/dist/workspace_session_context.d.ts +62 -0
- package/dist/workspace_session_context.js +160 -0
- package/dist/zlm-embed.mjs +32518 -9615
- package/dist/zlm.js +1 -0
- package/node_modules/@zerg/model-contracts/LICENSE.md +375 -0
- package/node_modules/@zerg/model-contracts/README.md +90 -0
- package/node_modules/@zerg/model-contracts/index.d.ts +20 -0
- package/node_modules/@zerg/model-contracts/index.js +223 -0
- package/node_modules/@zerg/model-contracts/package.json +8 -0
- package/node_modules/glob/node_modules/brace-expansion/index.js +78 -22
- package/node_modules/glob/node_modules/brace-expansion/package.json +1 -1
- package/node_modules/readdir-glob/node_modules/brace-expansion/index.js +78 -22
- package/node_modules/readdir-glob/node_modules/brace-expansion/package.json +1 -1
- package/package.json +45 -24
- package/packages/model-contracts/LICENSE.md +375 -0
- package/packages/model-contracts/README.md +90 -0
- package/packages/model-contracts/index.d.ts +20 -0
- package/packages/model-contracts/index.js +223 -0
- package/packages/model-contracts/package.json +8 -0
- package/remote-broker/Dockerfile +10 -0
- package/remote-broker/README.md +308 -0
- package/remote-broker/archive.py +1227 -0
- package/remote-broker/cutover.py +761 -0
- package/remote-broker/requirements.txt +5 -0
- package/remote-broker/resources.example.json +6 -0
- package/remote-broker/resources.write-staging.example.json +18 -0
- package/remote-broker/retained.py +686 -0
- package/remote-broker/retained_cleanup.py +550 -0
- package/remote-broker/server.py +648 -0
- package/scripts/build-remote-broker.mjs +13 -0
- package/scripts/ensure-local-packages.mjs +6 -0
- package/scripts/node-pty-helpers.mjs +103 -0
|
@@ -0,0 +1,910 @@
|
|
|
1
|
+
export class DynamicMechanicContractError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(code, message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.name = 'DynamicMechanicContractError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function validateDynamicMechanicCatalog(definitions) {
|
|
10
|
+
if (!Array.isArray(definitions) || definitions.length === 0 || definitions.length > 64) {
|
|
11
|
+
fail('mechanic-catalog-invalid', 'A mechanic catalog needs one to sixty-four definitions');
|
|
12
|
+
}
|
|
13
|
+
assertUnique(definitions.map(definition => definition.id), 'mechanic identifiers');
|
|
14
|
+
for (const definition of definitions) {
|
|
15
|
+
assertIdentifier(definition.id, 'mechanic identifier');
|
|
16
|
+
assertText(definition.description, 'mechanic description', 500);
|
|
17
|
+
if (!['required', 'optional', 'game-master'].includes(definition.actor)) {
|
|
18
|
+
fail('mechanic-catalog-invalid', `Mechanic ${definition.id} has an invalid actor rule`);
|
|
19
|
+
}
|
|
20
|
+
if (!['none', 'optional', 'required'].includes(definition.target)) {
|
|
21
|
+
fail('mechanic-catalog-invalid', `Mechanic ${definition.id} has an invalid target rule`);
|
|
22
|
+
}
|
|
23
|
+
if (!Array.isArray(definition.intentParameters)
|
|
24
|
+
|| !Array.isArray(definition.resolutionParameters)
|
|
25
|
+
|| definition.intentParameters.length > 12
|
|
26
|
+
|| definition.resolutionParameters.length > 12) {
|
|
27
|
+
fail('mechanic-catalog-invalid', `Mechanic ${definition.id} has too many parameters`);
|
|
28
|
+
}
|
|
29
|
+
const parameters = [
|
|
30
|
+
...definition.intentParameters,
|
|
31
|
+
...definition.resolutionParameters,
|
|
32
|
+
];
|
|
33
|
+
assertUnique(parameters.map(parameter => parameter.id), `parameters for ${definition.id}`);
|
|
34
|
+
for (const parameter of parameters)
|
|
35
|
+
validateParameterDefinition(parameter, definition.id);
|
|
36
|
+
if (definition.resolutionParameters.some((parameter) => ((parameter.kind === 'object-reference' || parameter.kind === 'object-reference-list')
|
|
37
|
+
&& parameter.actorLinks !== undefined)))
|
|
38
|
+
fail('mechanic-catalog-invalid', 'Actor links constrain intent references only');
|
|
39
|
+
validateScalarBindings(definition.intentParameters);
|
|
40
|
+
if (definition.resolutionParameters.some((parameter) => parameter.valueFrom !== undefined)) {
|
|
41
|
+
fail('mechanic-catalog-invalid', 'Scalar bindings constrain intent parameters only');
|
|
42
|
+
}
|
|
43
|
+
referencePairDefinition(definition.intentParameters);
|
|
44
|
+
referencePairDefinition(definition.resolutionParameters);
|
|
45
|
+
}
|
|
46
|
+
return structuredClone(definitions);
|
|
47
|
+
}
|
|
48
|
+
export function validateDynamicMechanicAction(action, definitions, context, mode, resolutionContext = { ...context, actingActorId: null }) {
|
|
49
|
+
const validatedDefinitions = validateDynamicMechanicCatalog(definitions);
|
|
50
|
+
const definition = validatedDefinitions.find(candidate => candidate.id === action.mechanic);
|
|
51
|
+
if (!definition)
|
|
52
|
+
fail('mechanic-not-allowed', `Mechanic ${action.mechanic} is not defined`);
|
|
53
|
+
validateActors(action, definition, context);
|
|
54
|
+
const parameters = assertRecord(action.parameters, 'action parameters');
|
|
55
|
+
const allowedEnvelopeKeys = mode === 'intent' || definition.resolutionParameters.length === 0
|
|
56
|
+
? ['intent']
|
|
57
|
+
: ['intent', 'resolution'];
|
|
58
|
+
assertExactKeys(parameters, allowedEnvelopeKeys, ['intent'], 'action parameter envelope');
|
|
59
|
+
const intent = assertRecord(parameters.intent, 'intent parameters');
|
|
60
|
+
const participantContext = { ...context, actingActorId: action.actorId ?? null, targetActorId: action.targetActorId ?? null };
|
|
61
|
+
validateParameterRecord(intent, definition.intentParameters, participantContext);
|
|
62
|
+
if (mode === 'intent' && parameters.resolution !== undefined) {
|
|
63
|
+
fail('mechanic-parameter-invalid', 'An intent cannot author Game Master resolution fields');
|
|
64
|
+
}
|
|
65
|
+
if (mode === 'resolution' && definition.resolutionParameters.length > 0) {
|
|
66
|
+
const requiredResolution = definition.resolutionParameters.some(parameter => parameter.required !== false);
|
|
67
|
+
if (parameters.resolution === undefined) {
|
|
68
|
+
if (requiredResolution) {
|
|
69
|
+
fail('mechanic-parameter-invalid', 'Required resolution parameters are missing');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
validateParameterRecord(assertRecord(parameters.resolution, 'resolution parameters'), definition.resolutionParameters, resolutionContext);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return structuredClone(action.parameters);
|
|
77
|
+
}
|
|
78
|
+
export function dynamicMechanicParametersSchema(definition, context, mode) {
|
|
79
|
+
validateDynamicMechanicCatalog([definition]);
|
|
80
|
+
const properties = {
|
|
81
|
+
intent: parameterRecordSchema(definition.intentParameters, context),
|
|
82
|
+
};
|
|
83
|
+
const required = ['intent'];
|
|
84
|
+
if (mode === 'resolution' && definition.resolutionParameters.length > 0) {
|
|
85
|
+
properties.resolution = parameterRecordSchema(definition.resolutionParameters, { ...context, actingActorId: null });
|
|
86
|
+
if (definition.resolutionParameters.some(parameter => parameter.required !== false)) {
|
|
87
|
+
required.push('resolution');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return strictObjectSchema(required, properties);
|
|
91
|
+
}
|
|
92
|
+
export function constrainDynamicMechanicActionSchema(schema, definition, context) {
|
|
93
|
+
if (!definition.intentParameters.some(parameter => ((parameter.kind === 'object-reference' || parameter.kind === 'object-reference-list')
|
|
94
|
+
&& parameter.actorLinks?.target !== undefined)))
|
|
95
|
+
return schema;
|
|
96
|
+
const properties = schema.properties;
|
|
97
|
+
const targetSchema = properties.targetActorId;
|
|
98
|
+
const targets = targetSchema?.enum ?? (targetSchema?.const === undefined ? [] : [targetSchema.const]);
|
|
99
|
+
return { oneOf: targets.flatMap(targetActorId => {
|
|
100
|
+
const bound = { ...context, targetActorId };
|
|
101
|
+
if (targetActorId === context.actingActorId || !dynamicMechanicHasRequiredReferences(definition, bound))
|
|
102
|
+
return [];
|
|
103
|
+
return [{ ...schema, properties: { ...properties, targetActorId: { const: targetActorId },
|
|
104
|
+
parameters: dynamicMechanicParametersSchema(definition, bound, 'intent'),
|
|
105
|
+
} }];
|
|
106
|
+
}) };
|
|
107
|
+
}
|
|
108
|
+
export function dynamicMechanicHasRequiredReferences(definition, context) {
|
|
109
|
+
const pair = referencePairDefinition(definition.intentParameters);
|
|
110
|
+
if (pair !== null && compatibleReferencePairs(pair, context).length === 0)
|
|
111
|
+
return false;
|
|
112
|
+
if (definition.intentParameters.some(parameter => parameter.valueFrom !== undefined)
|
|
113
|
+
&& parameterRecordSchema(definition.intentParameters, context).anyOf.length === 0)
|
|
114
|
+
return false;
|
|
115
|
+
return definition.intentParameters.every(parameter => {
|
|
116
|
+
if (parameter.required === false)
|
|
117
|
+
return true;
|
|
118
|
+
switch (parameter.kind) {
|
|
119
|
+
case 'object-reference':
|
|
120
|
+
return compatibleObjects(parameter, context).length > 0;
|
|
121
|
+
case 'object-reference-list':
|
|
122
|
+
return compatibleObjects(parameter, context).length >= parameter.minimumItems;
|
|
123
|
+
case 'actor-reference':
|
|
124
|
+
return context.actorIds.some(actorId => parameter.allowSelf === true || actorId !== context.actingActorId);
|
|
125
|
+
case 'resource-reference':
|
|
126
|
+
return parameter.resourceIds.some(resourceId => context.resourceIds.includes(resourceId));
|
|
127
|
+
default:
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
export function makeDynamicFallbackParameters(definition, context, variant = 0) {
|
|
133
|
+
validateDynamicMechanicCatalog([definition]);
|
|
134
|
+
if (!Number.isSafeInteger(variant) || variant < 0) {
|
|
135
|
+
fail('mechanic-fallback-variant-invalid', 'A fallback variant must be a non-negative integer');
|
|
136
|
+
}
|
|
137
|
+
const intent = fallbackParameterRecord(definition.intentParameters, context, variant);
|
|
138
|
+
return { intent };
|
|
139
|
+
}
|
|
140
|
+
export function makeDynamicFallbackResolution(definition, context, variant = 0) {
|
|
141
|
+
validateDynamicMechanicCatalog([definition]);
|
|
142
|
+
if (!Number.isSafeInteger(variant) || variant < 0) {
|
|
143
|
+
fail('mechanic-fallback-variant-invalid', 'A fallback variant must be a non-negative integer');
|
|
144
|
+
}
|
|
145
|
+
return fallbackParameterRecord(definition.resolutionParameters, { ...context, actingActorId: null }, variant);
|
|
146
|
+
}
|
|
147
|
+
export function resolveDynamicActionParticipantIds(action, definition, objects, effects) {
|
|
148
|
+
const actorIds = new Set();
|
|
149
|
+
if (action.actorId !== undefined)
|
|
150
|
+
actorIds.add(action.actorId);
|
|
151
|
+
if (action.targetActorId !== undefined)
|
|
152
|
+
actorIds.add(action.targetActorId);
|
|
153
|
+
const referencedObjectIds = new Set([
|
|
154
|
+
...objectReferenceValues(action.parameters.intent, definition.intentParameters),
|
|
155
|
+
...objectReferenceValues(action.parameters.resolution, definition.resolutionParameters),
|
|
156
|
+
]);
|
|
157
|
+
const actionEffectObjects = effects.flatMap(effect => (effect.actionId === action.id && effect.operation === 'object.put' ? [effect.object] : []));
|
|
158
|
+
for (const object of actionEffectObjects)
|
|
159
|
+
referencedObjectIds.add(object.id);
|
|
160
|
+
const objectById = new Map(objects.map(object => [object.id, object]));
|
|
161
|
+
for (const object of actionEffectObjects)
|
|
162
|
+
objectById.set(object.id, object);
|
|
163
|
+
for (let pass = 0; pass <= objectById.size; pass += 1) {
|
|
164
|
+
let changed = false;
|
|
165
|
+
for (const object of objectById.values()) {
|
|
166
|
+
const reachesAffected = object.links.some(link => (link.targetType === 'object' && referencedObjectIds.has(link.targetId)));
|
|
167
|
+
if (reachesAffected && !referencedObjectIds.has(object.id)) {
|
|
168
|
+
referencedObjectIds.add(object.id);
|
|
169
|
+
changed = true;
|
|
170
|
+
}
|
|
171
|
+
if (!referencedObjectIds.has(object.id))
|
|
172
|
+
continue;
|
|
173
|
+
for (const link of object.links) {
|
|
174
|
+
if (link.targetType === 'object' && !referencedObjectIds.has(link.targetId)) {
|
|
175
|
+
referencedObjectIds.add(link.targetId);
|
|
176
|
+
changed = true;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (!changed)
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
for (const objectId of referencedObjectIds) {
|
|
184
|
+
const object = objectById.get(objectId);
|
|
185
|
+
if (!object)
|
|
186
|
+
continue;
|
|
187
|
+
if (object.ownerActorId !== null)
|
|
188
|
+
actorIds.add(object.ownerActorId);
|
|
189
|
+
for (const link of object.links) {
|
|
190
|
+
if (link.targetType === 'actor')
|
|
191
|
+
actorIds.add(link.targetId);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return [...actorIds];
|
|
195
|
+
}
|
|
196
|
+
export function dynamicObjectGraph(objects) {
|
|
197
|
+
return objects.map(({ id, ownerActorId, links }) => ({
|
|
198
|
+
id, ownerActorId, links: structuredClone(links),
|
|
199
|
+
}));
|
|
200
|
+
}
|
|
201
|
+
export function dynamicActionObjectGraph(actionId, actions, objects, effects) {
|
|
202
|
+
const graph = new Map(dynamicObjectGraph(objects).map(object => [object.id, object]));
|
|
203
|
+
for (const action of actions) {
|
|
204
|
+
for (const effect of effects) {
|
|
205
|
+
if (effect.actionId === action.id && effect.operation === 'object.put') {
|
|
206
|
+
const { id, ownerActorId, links } = effect.object;
|
|
207
|
+
graph.set(id, { id, ownerActorId, links: structuredClone(links) });
|
|
208
|
+
for (const retiredId of effect.sceneRetention?.retiredIds ?? [])
|
|
209
|
+
graph.delete(retiredId);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (action.id === actionId)
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
return [...graph.values()];
|
|
216
|
+
}
|
|
217
|
+
function objectReferenceValues(value, definitions) {
|
|
218
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
219
|
+
return [];
|
|
220
|
+
const record = value;
|
|
221
|
+
return definitions.flatMap((definition) => {
|
|
222
|
+
if (definition.kind === 'object-reference') {
|
|
223
|
+
const objectId = record[definition.id];
|
|
224
|
+
return typeof objectId === 'string' ? [objectId] : [];
|
|
225
|
+
}
|
|
226
|
+
if (definition.kind === 'object-reference-list') {
|
|
227
|
+
const objectIds = record[definition.id];
|
|
228
|
+
return Array.isArray(objectIds)
|
|
229
|
+
? objectIds.filter((item) => typeof item === 'string')
|
|
230
|
+
: [];
|
|
231
|
+
}
|
|
232
|
+
return [];
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function validateScalarBindings(definitions) {
|
|
236
|
+
for (const [index, parameter] of definitions.entries()) {
|
|
237
|
+
if (parameter.valueFrom === undefined)
|
|
238
|
+
continue;
|
|
239
|
+
const binding = parameter.valueFrom;
|
|
240
|
+
if (!['text', 'integer', 'number', 'boolean', 'enum'].includes(parameter.kind)
|
|
241
|
+
|| binding === null || typeof binding !== 'object' || Array.isArray(binding)
|
|
242
|
+
|| Object.keys(binding).sort().join(',') !== 'objectParameter,path'
|
|
243
|
+
|| typeof binding.objectParameter !== 'string' || !Array.isArray(binding.path)
|
|
244
|
+
|| binding.path.length === 0 || binding.path.length > 4
|
|
245
|
+
|| binding.path.some(key => typeof key !== 'string' || !/^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(key)
|
|
246
|
+
|| ['constructor', 'prototype', '__proto__'].includes(key))) {
|
|
247
|
+
fail('mechanic-catalog-invalid', `Scalar parameter ${parameter.id} has an invalid value binding`);
|
|
248
|
+
}
|
|
249
|
+
const source = definitions.slice(0, index).find(candidate => candidate.id === binding.objectParameter);
|
|
250
|
+
if (source?.kind !== 'object-reference' || source.required === false) {
|
|
251
|
+
fail('mechanic-catalog-invalid', `Scalar parameter ${parameter.id} needs a preceding required object reference`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function scalarBindingValue(parameter, record, definitions, context) {
|
|
256
|
+
const binding = parameter.valueFrom;
|
|
257
|
+
const source = definitions.find(candidate => candidate.id === binding.objectParameter);
|
|
258
|
+
if (source.kind !== 'object-reference')
|
|
259
|
+
fail('mechanic-catalog-invalid', 'Scalar source must be an object reference');
|
|
260
|
+
const object = compatibleObjects(source, context).find(candidate => candidate.id === record[source.id]);
|
|
261
|
+
if (!object)
|
|
262
|
+
fail('mechanic-reference-invalid', `${source.label} must reference a visible compatible object`);
|
|
263
|
+
let value = object;
|
|
264
|
+
for (const key of binding.path) {
|
|
265
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value) || !Object.hasOwn(value, key)) {
|
|
266
|
+
fail('mechanic-value-binding-invalid', `${parameter.label} has no current bound value`);
|
|
267
|
+
}
|
|
268
|
+
value = value[key];
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
validateParameterValue(value, parameter, context);
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
if (!(error instanceof DynamicMechanicContractError))
|
|
275
|
+
throw error;
|
|
276
|
+
fail('mechanic-value-binding-invalid', `${parameter.label} has an invalid current bound value`);
|
|
277
|
+
}
|
|
278
|
+
return value;
|
|
279
|
+
}
|
|
280
|
+
function validateParameterDefinition(parameter, mechanicId) {
|
|
281
|
+
assertIdentifier(parameter.id, `parameter identifier for ${mechanicId}`);
|
|
282
|
+
assertText(parameter.label, `parameter label for ${mechanicId}`, 120);
|
|
283
|
+
switch (parameter.kind) {
|
|
284
|
+
case 'text': {
|
|
285
|
+
const minimum = parameter.minimumLength ?? 1;
|
|
286
|
+
if (!Number.isSafeInteger(minimum)
|
|
287
|
+
|| !Number.isSafeInteger(parameter.maximumLength)
|
|
288
|
+
|| minimum < 0
|
|
289
|
+
|| parameter.maximumLength < Math.max(1, minimum)
|
|
290
|
+
|| parameter.maximumLength > 2_000) {
|
|
291
|
+
fail('mechanic-catalog-invalid', `Text parameter ${parameter.id} has invalid length bounds`);
|
|
292
|
+
}
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
case 'number':
|
|
296
|
+
case 'integer':
|
|
297
|
+
if (!Number.isFinite(parameter.minimum)
|
|
298
|
+
|| !Number.isFinite(parameter.maximum)
|
|
299
|
+
|| parameter.minimum > parameter.maximum
|
|
300
|
+
|| (parameter.kind === 'integer'
|
|
301
|
+
&& (!Number.isSafeInteger(parameter.minimum) || !Number.isSafeInteger(parameter.maximum)))) {
|
|
302
|
+
fail('mechanic-catalog-invalid', `Numeric parameter ${parameter.id} has invalid bounds`);
|
|
303
|
+
}
|
|
304
|
+
return;
|
|
305
|
+
case 'boolean':
|
|
306
|
+
return;
|
|
307
|
+
case 'enum':
|
|
308
|
+
if (!Array.isArray(parameter.values)
|
|
309
|
+
|| parameter.values.length === 0
|
|
310
|
+
|| parameter.values.length > 32
|
|
311
|
+
|| parameter.values.some(value => typeof value !== 'string' || value.trim() === '' || value.length > 100)) {
|
|
312
|
+
fail('mechanic-catalog-invalid', `Enum parameter ${parameter.id} has invalid choices`);
|
|
313
|
+
}
|
|
314
|
+
assertUnique(parameter.values, `choices for ${parameter.id}`);
|
|
315
|
+
return;
|
|
316
|
+
case 'actor-reference':
|
|
317
|
+
return;
|
|
318
|
+
case 'resource-reference':
|
|
319
|
+
if (!Array.isArray(parameter.resourceIds)
|
|
320
|
+
|| parameter.resourceIds.length === 0
|
|
321
|
+
|| parameter.resourceIds.length > 16) {
|
|
322
|
+
fail('mechanic-catalog-invalid', `Resource parameter ${parameter.id} needs bounded resources`);
|
|
323
|
+
}
|
|
324
|
+
for (const resourceId of parameter.resourceIds) {
|
|
325
|
+
assertIdentifier(resourceId, `resource for ${parameter.id}`);
|
|
326
|
+
}
|
|
327
|
+
assertUnique(parameter.resourceIds, `resources for ${parameter.id}`);
|
|
328
|
+
return;
|
|
329
|
+
case 'object-reference':
|
|
330
|
+
case 'object-reference-list':
|
|
331
|
+
if (!Array.isArray(parameter.objectKinds)
|
|
332
|
+
|| parameter.objectKinds.length === 0
|
|
333
|
+
|| parameter.objectKinds.length > 16) {
|
|
334
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} needs bounded kinds`);
|
|
335
|
+
}
|
|
336
|
+
for (const kind of parameter.objectKinds)
|
|
337
|
+
assertIdentifier(kind, `object kind for ${parameter.id}`);
|
|
338
|
+
assertUnique(parameter.objectKinds, `object kinds for ${parameter.id}`);
|
|
339
|
+
if (parameter.ownedByActor !== undefined && typeof parameter.ownedByActor !== 'boolean') {
|
|
340
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} has an invalid ownership rule`);
|
|
341
|
+
}
|
|
342
|
+
if (parameter.statuses !== undefined) {
|
|
343
|
+
if (!Array.isArray(parameter.statuses) || parameter.statuses.length === 0 || parameter.statuses.length > 16) {
|
|
344
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} has invalid statuses`);
|
|
345
|
+
}
|
|
346
|
+
for (const status of parameter.statuses)
|
|
347
|
+
assertIdentifier(status, `object status for ${parameter.id}`);
|
|
348
|
+
assertUnique(parameter.statuses, `object statuses for ${parameter.id}`);
|
|
349
|
+
}
|
|
350
|
+
if (parameter.actorLinks !== undefined) {
|
|
351
|
+
const links = parameter.actorLinks;
|
|
352
|
+
if (links === null || typeof links !== 'object' || Array.isArray(links)
|
|
353
|
+
|| Object.keys(links).length === 0 || Object.keys(links).some(key => key !== 'actor' && key !== 'target')) {
|
|
354
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} needs valid actor links`);
|
|
355
|
+
}
|
|
356
|
+
for (const relation of Object.values(links))
|
|
357
|
+
assertIdentifier(relation, `actor link for ${parameter.id}`);
|
|
358
|
+
}
|
|
359
|
+
if (parameter.actorLinkExceptions !== undefined) {
|
|
360
|
+
const exceptions = parameter.actorLinkExceptions;
|
|
361
|
+
if (parameter.actorLinks?.actor === undefined || !Array.isArray(exceptions)
|
|
362
|
+
|| exceptions.length === 0 || exceptions.length > 8) {
|
|
363
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} needs bounded actor-link exceptions`);
|
|
364
|
+
}
|
|
365
|
+
for (const actorId of exceptions)
|
|
366
|
+
assertIdentifier(actorId, `actor-link exception for ${parameter.id}`);
|
|
367
|
+
assertUnique(exceptions, `actor-link exceptions for ${parameter.id}`);
|
|
368
|
+
}
|
|
369
|
+
if (parameter.requiredNumericData !== undefined) {
|
|
370
|
+
const constraints = parameter.requiredNumericData;
|
|
371
|
+
if (constraints === null || typeof constraints !== 'object' || Array.isArray(constraints)
|
|
372
|
+
|| Object.keys(constraints).length === 0 || Object.keys(constraints).length > 8) {
|
|
373
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} needs bounded numeric filters`);
|
|
374
|
+
}
|
|
375
|
+
for (const [key, bounds] of Object.entries(constraints)) {
|
|
376
|
+
assertIdentifier(key, `numeric data key for ${parameter.id}`);
|
|
377
|
+
if (bounds === null || typeof bounds !== 'object' || Array.isArray(bounds)
|
|
378
|
+
|| (bounds.minimum === undefined && bounds.maximumExclusive === undefined)
|
|
379
|
+
|| Object.keys(bounds).some(name => name !== 'minimum' && name !== 'maximumExclusive')
|
|
380
|
+
|| (bounds.minimum !== undefined && !Number.isFinite(bounds.minimum))
|
|
381
|
+
|| (bounds.maximumExclusive !== undefined && !Number.isFinite(bounds.maximumExclusive))
|
|
382
|
+
|| (bounds.minimum !== undefined && bounds.maximumExclusive !== undefined && bounds.minimum >= bounds.maximumExclusive)) {
|
|
383
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} has invalid numeric bounds`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (parameter.requiredData !== undefined) {
|
|
388
|
+
const data = parameter.requiredData;
|
|
389
|
+
if (data === null || typeof data !== 'object' || Array.isArray(data)
|
|
390
|
+
|| Object.keys(data).length === 0 || Object.keys(data).length > 8) {
|
|
391
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} needs bounded data filters`);
|
|
392
|
+
}
|
|
393
|
+
for (const [key, value] of Object.entries(data)) {
|
|
394
|
+
assertIdentifier(key, `object data key for ${parameter.id}`);
|
|
395
|
+
if (!(typeof value === 'boolean'
|
|
396
|
+
|| (typeof value === 'number' && Number.isFinite(value))
|
|
397
|
+
|| (typeof value === 'string' && value.length <= 240))) {
|
|
398
|
+
fail('mechanic-catalog-invalid', `Object parameter ${parameter.id} has an invalid data filter`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (parameter.kind === 'object-reference-list'
|
|
403
|
+
&& (!Number.isSafeInteger(parameter.minimumItems)
|
|
404
|
+
|| !Number.isSafeInteger(parameter.maximumItems)
|
|
405
|
+
|| parameter.minimumItems < 1
|
|
406
|
+
|| parameter.maximumItems < parameter.minimumItems
|
|
407
|
+
|| parameter.maximumItems > 16)) {
|
|
408
|
+
fail('mechanic-catalog-invalid', `Object list parameter ${parameter.id} has invalid item bounds`);
|
|
409
|
+
}
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
function validateActors(action, definition, context) {
|
|
414
|
+
if (definition.actor === 'required' && action.actorId === undefined) {
|
|
415
|
+
fail('mechanic-actor-invalid', `Mechanic ${definition.id} needs an actor`);
|
|
416
|
+
}
|
|
417
|
+
if (definition.actor === 'game-master' && action.actorId !== undefined) {
|
|
418
|
+
fail('mechanic-actor-invalid', `Mechanic ${definition.id} is Game Master only`);
|
|
419
|
+
}
|
|
420
|
+
if (action.actorId !== undefined && !context.actorIds.includes(action.actorId)) {
|
|
421
|
+
fail('mechanic-actor-invalid', `Actor ${action.actorId} is unknown`);
|
|
422
|
+
}
|
|
423
|
+
if (context.actingActorId !== null
|
|
424
|
+
&& action.actorId !== undefined
|
|
425
|
+
&& context.actingActorId !== action.actorId) {
|
|
426
|
+
fail('mechanic-actor-invalid', 'The acting character cannot author another actor action');
|
|
427
|
+
}
|
|
428
|
+
if (definition.target === 'required' && action.targetActorId === undefined) {
|
|
429
|
+
fail('mechanic-target-invalid', `Mechanic ${definition.id} needs a target actor`);
|
|
430
|
+
}
|
|
431
|
+
if (definition.target === 'none' && action.targetActorId !== undefined) {
|
|
432
|
+
fail('mechanic-target-invalid', `Mechanic ${definition.id} cannot target an actor`);
|
|
433
|
+
}
|
|
434
|
+
if (action.targetActorId !== undefined && !context.actorIds.includes(action.targetActorId)) {
|
|
435
|
+
fail('mechanic-target-invalid', `Target ${action.targetActorId} is unknown`);
|
|
436
|
+
}
|
|
437
|
+
if (action.actorId !== undefined && action.actorId === action.targetActorId) {
|
|
438
|
+
fail('mechanic-target-invalid', 'An actor cannot target itself');
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function validateParameterRecord(record, definitions, context) {
|
|
442
|
+
const allowed = definitions.map(definition => definition.id);
|
|
443
|
+
const required = definitions
|
|
444
|
+
.filter(definition => definition.required !== false)
|
|
445
|
+
.map(definition => definition.id);
|
|
446
|
+
assertExactKeys(record, allowed, required, 'mechanic parameters');
|
|
447
|
+
for (const definition of definitions) {
|
|
448
|
+
const value = record[definition.id];
|
|
449
|
+
if (value === undefined && definition.required === false)
|
|
450
|
+
continue;
|
|
451
|
+
validateParameterValue(value, definition, context);
|
|
452
|
+
if (definition.valueFrom !== undefined && value !== scalarBindingValue(definition, record, definitions, context)) {
|
|
453
|
+
fail('mechanic-value-binding-invalid', `${definition.label} differs from the selected object's current value`);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
const relation = referencePairDefinition(definitions);
|
|
457
|
+
if (relation !== null && !compatibleReferencePairs(relation, context).some(pair => (record[relation.peer.id] === pair.peerId && record[relation.parameter.id] === pair.parameterId))) {
|
|
458
|
+
fail('mechanic-reference-invalid', `${relation.parameter.label} must match the selected ${relation.peer.label}`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
function validateParameterValue(value, definition, context) {
|
|
462
|
+
switch (definition.kind) {
|
|
463
|
+
case 'text': {
|
|
464
|
+
const minimum = definition.minimumLength ?? 1;
|
|
465
|
+
if (typeof value !== 'string' || value.length < minimum || value.length > definition.maximumLength) {
|
|
466
|
+
fail('mechanic-parameter-invalid', `${definition.label} has invalid text`);
|
|
467
|
+
}
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
case 'number':
|
|
471
|
+
case 'integer':
|
|
472
|
+
if (typeof value !== 'number'
|
|
473
|
+
|| !Number.isFinite(value)
|
|
474
|
+
|| (definition.kind === 'integer' && !Number.isSafeInteger(value))
|
|
475
|
+
|| value < definition.minimum
|
|
476
|
+
|| value > definition.maximum) {
|
|
477
|
+
fail('mechanic-parameter-invalid', `${definition.label} is outside its bounds`);
|
|
478
|
+
}
|
|
479
|
+
return;
|
|
480
|
+
case 'boolean':
|
|
481
|
+
if (typeof value !== 'boolean')
|
|
482
|
+
fail('mechanic-parameter-invalid', `${definition.label} must be true or false`);
|
|
483
|
+
return;
|
|
484
|
+
case 'enum':
|
|
485
|
+
if (typeof value !== 'string' || !definition.values.includes(value)) {
|
|
486
|
+
fail('mechanic-parameter-invalid', `${definition.label} is not an allowed choice`);
|
|
487
|
+
}
|
|
488
|
+
return;
|
|
489
|
+
case 'actor-reference':
|
|
490
|
+
if (typeof value !== 'string'
|
|
491
|
+
|| !context.actorIds.includes(value)
|
|
492
|
+
|| (definition.allowSelf !== true && value === context.actingActorId)) {
|
|
493
|
+
fail('mechanic-reference-invalid', `${definition.label} must reference an allowed actor`);
|
|
494
|
+
}
|
|
495
|
+
return;
|
|
496
|
+
case 'resource-reference':
|
|
497
|
+
if (typeof value !== 'string'
|
|
498
|
+
|| !definition.resourceIds.includes(value)
|
|
499
|
+
|| !context.resourceIds.includes(value)) {
|
|
500
|
+
fail('mechanic-reference-invalid', `${definition.label} must reference an allowed resource`);
|
|
501
|
+
}
|
|
502
|
+
return;
|
|
503
|
+
case 'object-reference': {
|
|
504
|
+
if (typeof value !== 'string') {
|
|
505
|
+
fail('mechanic-reference-invalid', `${definition.label} must reference an object`);
|
|
506
|
+
}
|
|
507
|
+
const object = compatibleObjects(definition, context).find(candidate => candidate.id === value);
|
|
508
|
+
if (!object) {
|
|
509
|
+
fail('mechanic-reference-invalid', `${definition.label} must reference a visible compatible object`);
|
|
510
|
+
}
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
case 'object-reference-list': {
|
|
514
|
+
if (!Array.isArray(value)
|
|
515
|
+
|| value.length < definition.minimumItems
|
|
516
|
+
|| value.length > definition.maximumItems
|
|
517
|
+
|| value.some(candidate => typeof candidate !== 'string')
|
|
518
|
+
|| new Set(value).size !== value.length) {
|
|
519
|
+
fail('mechanic-reference-invalid', `${definition.label} must contain unique object references`);
|
|
520
|
+
}
|
|
521
|
+
for (const objectId of value) {
|
|
522
|
+
const object = compatibleObjects(definition, context).find(candidate => candidate.id === objectId);
|
|
523
|
+
if (!object) {
|
|
524
|
+
fail('mechanic-reference-invalid', `${definition.label} must reference visible compatible objects`);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
function parameterRecordSchema(definitions, context) {
|
|
532
|
+
const bindings = definitions.filter(parameter => parameter.valueFrom !== undefined);
|
|
533
|
+
if (bindings.length > 0) {
|
|
534
|
+
const unbound = definitions.map(({ valueFrom: _binding, ...parameter }) => parameter);
|
|
535
|
+
const base = parameterRecordSchema(unbound, context);
|
|
536
|
+
let branches = (base.anyOf ?? [base]);
|
|
537
|
+
for (const sourceId of new Set(bindings.map(parameter => parameter.valueFrom.objectParameter))) {
|
|
538
|
+
const source = definitions.find(parameter => parameter.id === sourceId);
|
|
539
|
+
if (source.kind !== 'object-reference')
|
|
540
|
+
fail('mechanic-catalog-invalid', 'Scalar source must be an object reference');
|
|
541
|
+
const objects = compatibleObjects(source, context);
|
|
542
|
+
if (branches.length * objects.length > 4096)
|
|
543
|
+
fail('mechanic-catalog-invalid', 'Scalar binding schema exceeds its branch bound');
|
|
544
|
+
branches = branches.flatMap(branch => objects.flatMap(object => {
|
|
545
|
+
const offered = branch.properties[sourceId].enum;
|
|
546
|
+
if (!offered.includes(object.id))
|
|
547
|
+
return [];
|
|
548
|
+
const properties = { ...branch.properties, [sourceId]: { enum: [object.id] } };
|
|
549
|
+
for (const parameter of bindings.filter(item => item.valueFrom.objectParameter === sourceId)) {
|
|
550
|
+
try {
|
|
551
|
+
properties[parameter.id] = { const: scalarBindingValue(parameter, { [sourceId]: object.id }, definitions, context) };
|
|
552
|
+
}
|
|
553
|
+
catch (error) {
|
|
554
|
+
if (!(error instanceof DynamicMechanicContractError) || error.code !== 'mechanic-value-binding-invalid')
|
|
555
|
+
throw error;
|
|
556
|
+
if (parameter.required !== false)
|
|
557
|
+
return [];
|
|
558
|
+
delete properties[parameter.id];
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return [{ ...branch, properties }];
|
|
562
|
+
}));
|
|
563
|
+
}
|
|
564
|
+
return { anyOf: branches };
|
|
565
|
+
}
|
|
566
|
+
const relation = referencePairDefinition(definitions);
|
|
567
|
+
if (relation !== null) {
|
|
568
|
+
const choices = new Map();
|
|
569
|
+
for (const pair of compatibleReferencePairs(relation, context)) {
|
|
570
|
+
const values = choices.get(pair.peerId) ?? [];
|
|
571
|
+
values.push(pair.parameterId);
|
|
572
|
+
choices.set(pair.peerId, values);
|
|
573
|
+
}
|
|
574
|
+
return {
|
|
575
|
+
anyOf: [...choices].map(([peerId, parameterIds]) => strictObjectSchema(definitions.filter(definition => definition.required !== false).map(definition => definition.id), Object.fromEntries(definitions.map(definition => [definition.id,
|
|
576
|
+
definition.id === relation.peer.id ? { enum: [peerId] }
|
|
577
|
+
: definition.id === relation.parameter.id ? { enum: parameterIds }
|
|
578
|
+
: parameterSchema(definition, context),
|
|
579
|
+
])))),
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
return strictObjectSchema(definitions
|
|
583
|
+
.filter(definition => definition.required !== false)
|
|
584
|
+
.map(definition => definition.id), Object.fromEntries(definitions.map(definition => [
|
|
585
|
+
definition.id,
|
|
586
|
+
parameterSchema(definition, context),
|
|
587
|
+
])));
|
|
588
|
+
}
|
|
589
|
+
function parameterSchema(definition, context) {
|
|
590
|
+
switch (definition.kind) {
|
|
591
|
+
case 'text':
|
|
592
|
+
return {
|
|
593
|
+
type: 'string',
|
|
594
|
+
minLength: definition.minimumLength ?? 1,
|
|
595
|
+
maxLength: definition.maximumLength,
|
|
596
|
+
};
|
|
597
|
+
case 'number':
|
|
598
|
+
case 'integer':
|
|
599
|
+
return {
|
|
600
|
+
type: definition.kind,
|
|
601
|
+
minimum: definition.minimum,
|
|
602
|
+
maximum: definition.maximum,
|
|
603
|
+
};
|
|
604
|
+
case 'boolean':
|
|
605
|
+
return { type: 'boolean' };
|
|
606
|
+
case 'enum':
|
|
607
|
+
return { enum: [...definition.values] };
|
|
608
|
+
case 'actor-reference':
|
|
609
|
+
return {
|
|
610
|
+
enum: context.actorIds.filter(actorId => (definition.allowSelf === true || actorId !== context.actingActorId)),
|
|
611
|
+
};
|
|
612
|
+
case 'resource-reference':
|
|
613
|
+
return { enum: definition.resourceIds.filter(resourceId => context.resourceIds.includes(resourceId)) };
|
|
614
|
+
case 'object-reference':
|
|
615
|
+
return {
|
|
616
|
+
enum: compatibleObjects(definition, context).map(object => object.id),
|
|
617
|
+
};
|
|
618
|
+
case 'object-reference-list':
|
|
619
|
+
return {
|
|
620
|
+
type: 'array',
|
|
621
|
+
minItems: definition.minimumItems,
|
|
622
|
+
maxItems: definition.maximumItems,
|
|
623
|
+
uniqueItems: true,
|
|
624
|
+
items: {
|
|
625
|
+
enum: compatibleObjects(definition, context).map(object => object.id),
|
|
626
|
+
},
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
function fallbackValue(definition, context, variant) {
|
|
631
|
+
switch (definition.kind) {
|
|
632
|
+
case 'text': {
|
|
633
|
+
const minimum = Math.max(1, definition.minimumLength ?? 1);
|
|
634
|
+
const base = definition.label.slice(0, definition.maximumLength);
|
|
635
|
+
const candidate = base.length >= minimum ? base : 'x'.repeat(minimum);
|
|
636
|
+
if (variant === 0)
|
|
637
|
+
return candidate;
|
|
638
|
+
const width = Math.max(minimum, candidate.length);
|
|
639
|
+
const stem = candidate.padEnd(width, 'x').slice(0, width);
|
|
640
|
+
const marker = String.fromCharCode(97 + ((variant - 1) % 26));
|
|
641
|
+
const alternative = `${stem.slice(0, -1)}${marker}`;
|
|
642
|
+
if (alternative !== candidate)
|
|
643
|
+
return alternative;
|
|
644
|
+
return `${stem.slice(0, -1)}${marker === 'z' ? 'a' : String.fromCharCode(marker.charCodeAt(0) + 1)}`;
|
|
645
|
+
}
|
|
646
|
+
case 'number':
|
|
647
|
+
return variant === 0 || definition.minimum === definition.maximum
|
|
648
|
+
? definition.minimum
|
|
649
|
+
: definition.maximum;
|
|
650
|
+
case 'integer': {
|
|
651
|
+
const bounded = (value) => Math.min(definition.maximum, Math.max(definition.minimum, value));
|
|
652
|
+
const candidates = [
|
|
653
|
+
definition.minimum,
|
|
654
|
+
definition.maximum,
|
|
655
|
+
bounded(0),
|
|
656
|
+
bounded(1),
|
|
657
|
+
bounded(-1),
|
|
658
|
+
Math.floor((definition.minimum + definition.maximum) / 2),
|
|
659
|
+
Math.ceil((definition.minimum + definition.maximum) / 2),
|
|
660
|
+
];
|
|
661
|
+
const candidate = candidates[variant % candidates.length];
|
|
662
|
+
if (candidate === undefined)
|
|
663
|
+
fail('mechanic-fallback-unavailable', `${definition.label} has no fallback number`);
|
|
664
|
+
return candidate;
|
|
665
|
+
}
|
|
666
|
+
case 'boolean':
|
|
667
|
+
return variant % 2 === 1;
|
|
668
|
+
case 'enum': {
|
|
669
|
+
const value = definition.values[variant % definition.values.length];
|
|
670
|
+
if (value === undefined)
|
|
671
|
+
fail('mechanic-fallback-unavailable', `${definition.label} has no fallback choice`);
|
|
672
|
+
return value;
|
|
673
|
+
}
|
|
674
|
+
case 'actor-reference': {
|
|
675
|
+
const candidates = context.actorIds.filter(actorId => (definition.allowSelf === true || actorId !== context.actingActorId));
|
|
676
|
+
const value = candidates[variant % candidates.length];
|
|
677
|
+
if (value === undefined)
|
|
678
|
+
fail('mechanic-fallback-unavailable', `${definition.label} has no visible actor`);
|
|
679
|
+
return value;
|
|
680
|
+
}
|
|
681
|
+
case 'resource-reference': {
|
|
682
|
+
const candidates = definition.resourceIds.filter(resourceId => context.resourceIds.includes(resourceId));
|
|
683
|
+
const value = candidates[variant % candidates.length];
|
|
684
|
+
if (value === undefined)
|
|
685
|
+
fail('mechanic-fallback-unavailable', `${definition.label} has no resource`);
|
|
686
|
+
return value;
|
|
687
|
+
}
|
|
688
|
+
case 'object-reference': {
|
|
689
|
+
const objects = compatibleObjects(definition, context);
|
|
690
|
+
const value = objects[variant % objects.length]?.id;
|
|
691
|
+
if (value === undefined)
|
|
692
|
+
fail('mechanic-fallback-unavailable', `${definition.label} has no visible object`);
|
|
693
|
+
return value;
|
|
694
|
+
}
|
|
695
|
+
case 'object-reference-list': {
|
|
696
|
+
const objects = compatibleObjects(definition, context);
|
|
697
|
+
const offset = objects.length === 0 ? 0 : variant % objects.length;
|
|
698
|
+
const values = [...objects.slice(offset), ...objects.slice(0, offset)]
|
|
699
|
+
.slice(0, definition.minimumItems)
|
|
700
|
+
.map(object => object.id);
|
|
701
|
+
if (values.length < definition.minimumItems) {
|
|
702
|
+
fail('mechanic-fallback-unavailable', `${definition.label} has too few visible objects`);
|
|
703
|
+
}
|
|
704
|
+
return values;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
function fallbackParameterRecord(definitions, context, variant) {
|
|
709
|
+
const relation = referencePairDefinition(definitions);
|
|
710
|
+
const pairs = relation === null ? null : compatibleReferencePairs(relation, context);
|
|
711
|
+
if (pairs !== null && pairs.length === 0) {
|
|
712
|
+
fail('mechanic-fallback-unavailable', 'The mechanic has no compatible reference pair');
|
|
713
|
+
}
|
|
714
|
+
const selected = pairs === null ? null : pairs[variant % pairs.length];
|
|
715
|
+
let stride = pairs?.length ?? 1;
|
|
716
|
+
const entries = definitions
|
|
717
|
+
.filter(definition => definition.required !== false)
|
|
718
|
+
.map((definition) => {
|
|
719
|
+
if (relation !== null && selected !== null) {
|
|
720
|
+
if (definition.id === relation.peer.id)
|
|
721
|
+
return [definition.id, selected.peerId];
|
|
722
|
+
if (definition.id === relation.parameter.id)
|
|
723
|
+
return [definition.id, selected.parameterId];
|
|
724
|
+
}
|
|
725
|
+
if (definition.valueFrom !== undefined)
|
|
726
|
+
return [definition.id, null];
|
|
727
|
+
const parameterVariant = Math.floor(variant / stride);
|
|
728
|
+
const entry = [
|
|
729
|
+
definition.id,
|
|
730
|
+
fallbackValue(definition, context, parameterVariant),
|
|
731
|
+
];
|
|
732
|
+
stride = Math.min(Number.MAX_SAFE_INTEGER, stride * Math.max(1, fallbackVariantCount(definition, context)));
|
|
733
|
+
return entry;
|
|
734
|
+
});
|
|
735
|
+
const record = Object.fromEntries(entries);
|
|
736
|
+
for (const definition of definitions) {
|
|
737
|
+
if (definition.valueFrom !== undefined && definition.required !== false) {
|
|
738
|
+
record[definition.id] = scalarBindingValue(definition, record, definitions, context);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return record;
|
|
742
|
+
}
|
|
743
|
+
function referencePairDefinition(definitions) {
|
|
744
|
+
const linked = definitions.filter((parameter) => (parameter.kind === 'object-reference' && (parameter.dataMatch !== undefined || parameter.capacityMatch !== undefined || parameter.linkMatch !== undefined)));
|
|
745
|
+
if (linked.length === 0)
|
|
746
|
+
return null;
|
|
747
|
+
if (linked.length > 1) {
|
|
748
|
+
fail('mechanic-catalog-invalid', 'A parameter record supports one reference pair');
|
|
749
|
+
}
|
|
750
|
+
const parameter = linked[0];
|
|
751
|
+
const capacity = parameter.capacityMatch;
|
|
752
|
+
const link = parameter.linkMatch;
|
|
753
|
+
const match = capacity ?? link ?? parameter.dataMatch;
|
|
754
|
+
const keys = link !== undefined ? ['parameterId', 'relation'] : capacity === undefined ? ['parameterId', 'key', 'otherKey']
|
|
755
|
+
: ['parameterId', 'amountKey', 'capacityKey', 'usedKey', 'excludePeerReferenceKey'];
|
|
756
|
+
const required = capacity === undefined ? keys : keys.slice(0, 4);
|
|
757
|
+
if (match === null || typeof match !== 'object' || Array.isArray(match)
|
|
758
|
+
|| [capacity, link, parameter.dataMatch].filter(value => value !== undefined).length !== 1
|
|
759
|
+
|| required.some(key => !Object.hasOwn(match, key))
|
|
760
|
+
|| Object.keys(match).some(key => !keys.includes(key))) {
|
|
761
|
+
fail('mechanic-catalog-invalid', 'A reference pair needs one complete supported constraint');
|
|
762
|
+
}
|
|
763
|
+
for (const value of Object.values(match))
|
|
764
|
+
assertIdentifier(value, 'reference pair field');
|
|
765
|
+
const peer = definitions.find(candidate => candidate.id === match.parameterId);
|
|
766
|
+
if (peer === undefined || peer.kind !== 'object-reference' || peer.id === parameter.id
|
|
767
|
+
|| peer.required === false || parameter.required === false) {
|
|
768
|
+
fail('mechanic-catalog-invalid', 'A reference pair needs two distinct required object parameters');
|
|
769
|
+
}
|
|
770
|
+
if (link !== undefined) {
|
|
771
|
+
return { parameter, peer, matches: (candidate, other) => {
|
|
772
|
+
const links = other.links.filter(edge => edge.relation === link.relation);
|
|
773
|
+
return links.length === 1 && links[0].targetType === 'object' && links[0].targetId === candidate.id;
|
|
774
|
+
} };
|
|
775
|
+
}
|
|
776
|
+
if (capacity !== undefined) {
|
|
777
|
+
return { parameter, peer, matches: (candidate, other) => {
|
|
778
|
+
const amount = other.data[capacity.amountKey];
|
|
779
|
+
const maximum = candidate.data[capacity.capacityKey];
|
|
780
|
+
const used = candidate.data[capacity.usedKey];
|
|
781
|
+
if (typeof amount !== 'number' || !Number.isFinite(amount) || amount < 0
|
|
782
|
+
|| typeof maximum !== 'number' || !Number.isFinite(maximum) || maximum < 0
|
|
783
|
+
|| typeof used !== 'number' || !Number.isFinite(used) || used < 0 || used > maximum)
|
|
784
|
+
return false;
|
|
785
|
+
if (capacity.excludePeerReferenceKey !== undefined) {
|
|
786
|
+
const sourceId = other.data[capacity.excludePeerReferenceKey];
|
|
787
|
+
if (typeof sourceId !== 'string' || sourceId.length === 0 || sourceId === candidate.id)
|
|
788
|
+
return false;
|
|
789
|
+
}
|
|
790
|
+
return amount <= maximum - used;
|
|
791
|
+
} };
|
|
792
|
+
}
|
|
793
|
+
const equality = parameter.dataMatch;
|
|
794
|
+
return { parameter, peer, matches: (candidate, other) => {
|
|
795
|
+
const value = other.data[equality.otherKey];
|
|
796
|
+
return (typeof value === 'string' || typeof value === 'boolean'
|
|
797
|
+
|| (typeof value === 'number' && Number.isFinite(value))) && candidate.data[equality.key] === value;
|
|
798
|
+
} };
|
|
799
|
+
}
|
|
800
|
+
function compatibleReferencePairs(relation, context) {
|
|
801
|
+
const candidates = compatibleObjects(relation.parameter, context);
|
|
802
|
+
return compatibleObjects(relation.peer, context).flatMap(peer => (candidates.filter(candidate => relation.matches(candidate, peer))
|
|
803
|
+
.map(candidate => ({ peerId: peer.id, parameterId: candidate.id }))));
|
|
804
|
+
}
|
|
805
|
+
function fallbackVariantCount(definition, context) {
|
|
806
|
+
switch (definition.kind) {
|
|
807
|
+
case 'text':
|
|
808
|
+
return 27;
|
|
809
|
+
case 'number':
|
|
810
|
+
return definition.minimum === definition.maximum ? 1 : 2;
|
|
811
|
+
case 'integer':
|
|
812
|
+
return 7;
|
|
813
|
+
case 'boolean':
|
|
814
|
+
return 2;
|
|
815
|
+
case 'enum':
|
|
816
|
+
return definition.values.length;
|
|
817
|
+
case 'actor-reference':
|
|
818
|
+
return context.actorIds.filter(actorId => (definition.allowSelf === true || actorId !== context.actingActorId)).length;
|
|
819
|
+
case 'resource-reference':
|
|
820
|
+
return definition.resourceIds.filter(resourceId => context.resourceIds.includes(resourceId)).length;
|
|
821
|
+
case 'object-reference':
|
|
822
|
+
case 'object-reference-list':
|
|
823
|
+
return compatibleObjects(definition, context).length;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
function compatibleObjects(definition, context) {
|
|
827
|
+
return context.objects.filter(object => (definition.objectKinds.includes(object.kind)
|
|
828
|
+
&& (definition.ownedByActor !== true || (context.actingActorId !== null && object.ownerActorId === context.actingActorId))
|
|
829
|
+
&& (definition.statuses === undefined || definition.statuses.includes(object.status))
|
|
830
|
+
&& (definition.requiredData === undefined
|
|
831
|
+
|| Object.entries(definition.requiredData).every(([key, value]) => object.data[key] === value))
|
|
832
|
+
&& (definition.actorLinks === undefined || Object.entries(definition.actorLinks).every(([role, relation]) => {
|
|
833
|
+
const links = object.links.filter(link => link.targetType === 'actor' && link.relation === relation);
|
|
834
|
+
if (links.length !== 1)
|
|
835
|
+
return false;
|
|
836
|
+
const linked = links[0].targetId;
|
|
837
|
+
if (role === 'actor')
|
|
838
|
+
return linked === context.actingActorId
|
|
839
|
+
|| (context.actingActorId !== null && context.actorIds.includes(context.actingActorId)
|
|
840
|
+
&& definition.actorLinkExceptions?.includes(context.actingActorId) === true);
|
|
841
|
+
return context.targetActorId === undefined
|
|
842
|
+
? context.actorIds.includes(linked) && linked !== context.actingActorId
|
|
843
|
+
: linked === context.targetActorId;
|
|
844
|
+
}))
|
|
845
|
+
&& (definition.requiredNumericData === undefined || Object.entries(definition.requiredNumericData).every(([key, bounds]) => {
|
|
846
|
+
const value = object.data[key];
|
|
847
|
+
return typeof value === 'number' && Number.isFinite(value)
|
|
848
|
+
&& (bounds.minimum === undefined || value >= bounds.minimum)
|
|
849
|
+
&& (bounds.maximumExclusive === undefined || value < bounds.maximumExclusive);
|
|
850
|
+
}))
|
|
851
|
+
&& isObjectVisibleToActor(object, context.actingActorId))).map((object, index) => ({ object, index })).sort((left, right) => {
|
|
852
|
+
if (definition.statuses === undefined)
|
|
853
|
+
return left.index - right.index;
|
|
854
|
+
const statusOrder = definition.statuses.indexOf(left.object.status)
|
|
855
|
+
- definition.statuses.indexOf(right.object.status);
|
|
856
|
+
return statusOrder === 0 ? left.index - right.index : statusOrder;
|
|
857
|
+
}).map(item => item.object);
|
|
858
|
+
}
|
|
859
|
+
function isObjectVisibleToActor(object, actorId) {
|
|
860
|
+
if (actorId === null || object.visibility.scope === 'world')
|
|
861
|
+
return true;
|
|
862
|
+
if (object.visibility.scope === 'shared')
|
|
863
|
+
return object.visibility.actorIds.includes(actorId);
|
|
864
|
+
return object.ownerActorId === actorId;
|
|
865
|
+
}
|
|
866
|
+
function strictObjectSchema(required, properties) {
|
|
867
|
+
return {
|
|
868
|
+
type: 'object',
|
|
869
|
+
additionalProperties: false,
|
|
870
|
+
required: [...required],
|
|
871
|
+
properties: structuredClone(properties),
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
function assertRecord(value, label) {
|
|
875
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
876
|
+
fail('mechanic-parameter-invalid', `${label} must be an object`);
|
|
877
|
+
}
|
|
878
|
+
return value;
|
|
879
|
+
}
|
|
880
|
+
function assertExactKeys(value, allowed, required, label) {
|
|
881
|
+
const keys = Object.keys(value);
|
|
882
|
+
if (keys.some(key => !allowed.includes(key))) {
|
|
883
|
+
fail('mechanic-parameter-invalid', `${label} contains an unknown field`);
|
|
884
|
+
}
|
|
885
|
+
if (required.some(key => !Object.prototype.hasOwnProperty.call(value, key))) {
|
|
886
|
+
fail('mechanic-parameter-invalid', `${label} is missing a required field`);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
function assertIdentifier(value, label) {
|
|
890
|
+
if (typeof value !== 'string'
|
|
891
|
+
|| value.trim() !== value
|
|
892
|
+
|| value.length === 0
|
|
893
|
+
|| value.length > 128
|
|
894
|
+
|| !/^[a-zA-Z][a-zA-Z0-9-]*$/.test(value)) {
|
|
895
|
+
fail('mechanic-catalog-invalid', `${label} is invalid`);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
function assertText(value, label, maximum) {
|
|
899
|
+
if (typeof value !== 'string' || value.trim() === '' || value.length > maximum) {
|
|
900
|
+
fail('mechanic-catalog-invalid', `${label} must contain bounded text`);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
function assertUnique(values, label) {
|
|
904
|
+
if (new Set(values).size !== values.length) {
|
|
905
|
+
fail('mechanic-catalog-invalid', `${label} must be unique`);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
function fail(code, message) {
|
|
909
|
+
throw new DynamicMechanicContractError(code, message);
|
|
910
|
+
}
|