@cortex-agent/server 2026.10.15 → 2026.10.25
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 +81 -51
- package/defaults/board/board.css +140 -62
- package/defaults/board/scaffold.html +36 -13
- package/defaults/config/environments/creative.json +0 -1
- package/defaults/config/environments/main.json +1 -2
- package/defaults/context/projects/AGENTS.md +1 -1
- package/defaults/gitignore +3 -3
- package/defaults/hooks/new-session-hook.mjs +1 -1
- package/defaults/hooks/status-md-guard.mjs +6 -6
- package/defaults/plugins/cortex-board/skills/board/SKILL.md +30 -22
- package/defaults/plugins/cortex-system/plugin.json +2 -2
- package/defaults/plugins/cortex-system/skills/browser/SKILL.md +56 -0
- package/defaults/plugins/cortex-system/skills/client-manage/SKILL.md +16 -9
- package/defaults/plugins/cortex-system/skills/project-init/SKILL.md +2 -2
- package/defaults/rules/agents-md.md +1 -1
- package/defaults/rules/issues-md.md +1 -1
- package/defaults/rules/meta-conventions.md +3 -3
- package/defaults/rules/mission-md.md +1 -1
- package/defaults/rules/status-md.md +1 -1
- package/dist/agent-adapter/claude/adapter.d.ts +2 -5
- package/dist/agent-adapter/claude/adapter.js +6 -5
- package/dist/agent-adapter/claude/assistant-blocks.d.ts +5 -0
- package/dist/agent-adapter/claude/assistant-blocks.js +61 -0
- package/dist/agent-adapter/claude/bg-task-tracker.d.ts +3 -1
- package/dist/agent-adapter/claude/bg-task-tracker.js +6 -2
- package/dist/agent-adapter/claude/cache-refresh.d.ts +58 -0
- package/dist/agent-adapter/claude/cache-refresh.js +145 -0
- package/dist/agent-adapter/claude/cache-touch.d.ts +12 -0
- package/dist/agent-adapter/claude/cache-touch.js +75 -0
- package/dist/agent-adapter/claude/event-parser.d.ts +8 -3
- package/dist/agent-adapter/claude/event-parser.js +33 -17
- package/dist/agent-adapter/claude/event-translator.d.ts +2 -0
- package/dist/agent-adapter/claude/event-translator.js +2 -0
- package/dist/agent-adapter/claude/mcp-config.js +11 -0
- package/dist/agent-adapter/claude/session-options.d.ts +6 -13
- package/dist/agent-adapter/claude/session-options.js +2 -13
- package/dist/agent-adapter/claude/spawn-args.d.ts +4 -21
- package/dist/agent-adapter/claude/spawn-args.js +17 -24
- package/dist/agent-adapter/claude/transcript-clear.d.ts +42 -0
- package/dist/agent-adapter/claude/transcript-clear.js +217 -0
- package/dist/agent-adapter/claude/turn-machine.d.ts +5 -3
- package/dist/agent-adapter/claude/turn-machine.js +29 -73
- package/dist/agent-adapter/claude/window-warmup.d.ts +13 -0
- package/dist/agent-adapter/claude/window-warmup.js +38 -0
- package/dist/agent-adapter/continuation-phase.d.ts +6 -3
- package/dist/agent-adapter/continuation-phase.js +11 -3
- package/dist/agent-adapter/normalize/event-types.d.ts +8 -0
- package/dist/agent-adapter/pi/adapter.d.ts +0 -1
- package/dist/agent-adapter/pi/adapter.js +5 -13
- package/dist/agent-adapter/pi/event-parser.d.ts +1 -1
- package/dist/agent-adapter/pi/event-parser.js +43 -27
- package/dist/agent-adapter/pi/mcp-bridge.d.ts +1 -1
- package/dist/agent-adapter/pi/pi-session.d.ts +2 -0
- package/dist/agent-adapter/pi/pi-session.js +15 -0
- package/dist/agent-adapter/pi/providers-config.d.ts +2 -2
- package/dist/agent-adapter/pi/providers-config.js +1 -1
- package/dist/agent-adapter/pi/quota-sink.d.ts +3 -3
- package/dist/agent-adapter/pi/quota-sink.js +3 -3
- package/dist/agent-adapter/pi/runtime.js +6 -1
- package/dist/agent-adapter/pi/session-options.d.ts +9 -13
- package/dist/agent-adapter/pi/session-options.js +12 -25
- package/dist/agent-adapter/run-events.d.ts +15 -10
- package/dist/agent-adapter/run-events.js +2 -0
- package/dist/agent-adapter/types.d.ts +14 -6
- package/dist/build-info.json +1 -0
- package/dist/client-bundle/client.mjs +10 -5
- package/dist/client-bundle/manifest.json +3 -3
- package/dist/core/claude-credentials.d.ts +6 -0
- package/dist/core/claude-credentials.js +11 -0
- package/dist/core/codex-quota.d.ts +1 -1
- package/dist/core/codex-quota.js +1 -1
- package/dist/core/gateway-generator.d.ts +26 -11
- package/dist/core/gateway-generator.js +73 -62
- package/dist/core/hook-bus.d.ts +1 -1
- package/dist/core/hook-bus.js +1 -8
- package/dist/core/locales/en.d.ts +52 -77
- package/dist/core/locales/slices/commands.d.ts +26 -31
- package/dist/core/locales/slices/commands.js +54 -64
- package/dist/core/locales/slices/init.d.ts +9 -19
- package/dist/core/locales/slices/init.js +19 -53
- package/dist/core/locales/slices/interactions.d.ts +3 -3
- package/dist/core/locales/slices/interactions.js +6 -6
- package/dist/core/locales/slices/notices.d.ts +1 -3
- package/dist/core/locales/slices/notices.js +2 -6
- package/dist/core/locales/slices/scheduling.d.ts +9 -9
- package/dist/core/locales/slices/scheduling.js +18 -18
- package/dist/core/locales/slices/status.d.ts +0 -4
- package/dist/core/locales/slices/status.js +0 -8
- package/dist/core/locales/slices/tui.d.ts +0 -1
- package/dist/core/locales/slices/tui.js +0 -2
- package/dist/core/locales/slices/uimsg.d.ts +4 -7
- package/dist/core/locales/slices/uimsg.js +9 -15
- package/dist/core/log.d.ts +3 -0
- package/dist/core/log.js +18 -1
- package/dist/core/models-generator.d.ts +29 -0
- package/dist/core/models-generator.js +82 -0
- package/dist/core/paths.d.ts +1 -1
- package/dist/core/paths.js +1 -1
- package/dist/core/settings-spec.d.ts +30 -0
- package/dist/core/settings-spec.js +38 -0
- package/dist/core/status-format.d.ts +3 -2
- package/dist/core/status-format.js +2 -2
- package/dist/core/types/agent-types.d.ts +3 -1
- package/dist/core/types/environment-types.d.ts +1 -8
- package/dist/domain/agents/agent-state.d.ts +11 -56
- package/dist/domain/agents/agent-state.js +48 -115
- package/dist/domain/agents/agent-switch.d.ts +2 -24
- package/dist/domain/agents/agent-switch.js +9 -72
- package/dist/domain/agents/config.d.ts +11 -61
- package/dist/domain/agents/config.js +47 -154
- package/dist/domain/agents/engine-selection.d.ts +21 -0
- package/dist/domain/agents/engine-selection.js +37 -0
- package/dist/domain/agents/environments/registry.js +4 -2
- package/dist/domain/agents/environments/resolve.js +0 -1
- package/dist/domain/agents/environments/schema.d.ts +3 -3
- package/dist/domain/agents/environments/schema.js +4 -4
- package/dist/domain/agents/index.d.ts +2 -1
- package/dist/domain/agents/index.js +3 -7
- package/dist/domain/agents/model-selection.d.ts +18 -70
- package/dist/domain/agents/model-selection.js +100 -150
- package/dist/domain/agents/models.d.ts +12 -0
- package/dist/domain/agents/models.js +94 -0
- package/dist/domain/agents/subagent/registry.d.ts +2 -0
- package/dist/domain/agents/subagent/registry.js +8 -0
- package/dist/domain/agents/subagent/runner.d.ts +5 -5
- package/dist/domain/agents/subagent/runner.js +12 -35
- package/dist/domain/auth/auth-status.d.ts +4 -4
- package/dist/domain/auth/auth-status.js +17 -28
- package/dist/domain/auth/auth-watch.js +48 -18
- package/dist/domain/auth/gateway-sync.d.ts +6 -26
- package/dist/domain/auth/gateway-sync.js +31 -75
- package/dist/domain/benchmark/identity.d.ts +3 -5
- package/dist/domain/benchmark/identity.js +0 -1
- package/dist/domain/benchmark/production-attempt-identity.d.ts +6 -5
- package/dist/domain/benchmark/production-attempt-identity.js +32 -35
- package/dist/domain/benchmark/session-evidence.d.ts +2 -1
- package/dist/domain/benchmark/session-evidence.js +8 -1
- package/dist/domain/browser/broker.d.ts +99 -0
- package/dist/domain/browser/broker.js +487 -0
- package/dist/domain/costs/claude-account.js +21 -0
- package/dist/domain/costs/rate-limit-throttle.js +3 -1
- package/dist/domain/costs/reset-account.d.ts +2 -0
- package/dist/domain/costs/usage-service.d.ts +1 -1
- package/dist/domain/costs/usage-service.js +20 -7
- package/dist/domain/mcp/tools/context.d.ts +5 -7
- package/dist/domain/mcp/tools/context.js +17 -4
- package/dist/domain/mcp/tools/subagent.js +1 -1
- package/dist/domain/mcp/tools/ui-file.js +1 -1
- package/dist/domain/monitor/event-loop-monitor.d.ts +2 -0
- package/dist/domain/monitor/event-loop-monitor.js +5 -1
- package/dist/domain/pi-providers/custom-provider-model.d.ts +1 -1
- package/dist/domain/pi-providers/custom-provider-model.js +1 -1
- package/dist/domain/plugins/authoring.d.ts +8 -1
- package/dist/domain/plugins/authoring.js +24 -9
- package/dist/domain/plugins/runtime-gc.d.ts +7 -0
- package/dist/domain/plugins/runtime-gc.js +52 -19
- package/dist/domain/plugins/runtime.js +2 -1
- package/dist/domain/remote/client-bootstrap.js +1 -1
- package/dist/domain/remote/client-manager.js +6 -1
- package/dist/domain/remote/device-browser.js +1 -1
- package/dist/domain/remote/device-chrome-commands.js +2 -0
- package/dist/domain/remote/device-port.js +2 -2
- package/dist/domain/runs/attempt.d.ts +3 -3
- package/dist/domain/runs/attempt.js +2 -3
- package/dist/domain/runs/builders.d.ts +4 -4
- package/dist/domain/runs/builders.js +4 -5
- package/dist/domain/runs/compact.d.ts +4 -17
- package/dist/domain/runs/compact.js +14 -56
- package/dist/domain/runs/config-resolver.d.ts +15 -83
- package/dist/domain/runs/config-resolver.js +24 -150
- package/dist/domain/runs/engine-spec.d.ts +8 -8
- package/dist/domain/runs/engine-spec.js +15 -31
- package/dist/domain/runs/events.d.ts +1 -1
- package/dist/domain/runs/fallback.d.ts +6 -38
- package/dist/domain/runs/fallback.js +10 -72
- package/dist/domain/runs/notices.d.ts +1 -11
- package/dist/domain/runs/notices.js +3 -30
- package/dist/domain/runs/request.d.ts +4 -3
- package/dist/domain/runs/retryable-errors.d.ts +0 -13
- package/dist/domain/runs/retryable-errors.js +3 -18
- package/dist/domain/runs/run.d.ts +12 -42
- package/dist/domain/runs/run.js +40 -147
- package/dist/domain/runs/service.d.ts +0 -9
- package/dist/domain/runs/service.js +33 -14
- package/dist/domain/runs/spec-loader.d.ts +1 -1
- package/dist/domain/runs/spend.d.ts +1 -1
- package/dist/domain/runs/spend.js +1 -1
- package/dist/domain/scheduling/builtin-job-controller.d.ts +3 -1
- package/dist/domain/scheduling/builtin-job-controller.js +5 -2
- package/dist/domain/scheduling/builtin-jobs.js +7 -0
- package/dist/domain/scheduling/jobs/scheduled-task.d.ts +3 -2
- package/dist/domain/scheduling/jobs/scheduled-task.js +25 -19
- package/dist/domain/scheduling/schedule-ops.d.ts +0 -1
- package/dist/domain/scheduling/schedule-ops.js +2 -5
- package/dist/domain/scheduling/schedule-selection.d.ts +3 -0
- package/dist/domain/scheduling/schedule-selection.js +16 -0
- package/dist/domain/scheduling/schedule-spec.d.ts +6 -5
- package/dist/domain/scheduling/schedule-spec.js +6 -6
- package/dist/domain/scheduling/scheduler.d.ts +3 -1
- package/dist/domain/scheduling/scheduler.js +36 -26
- package/dist/domain/sessions/session-hooks.d.ts +5 -4
- package/dist/domain/sessions/session-hooks.js +15 -33
- package/dist/domain/sessions/session-lifecycle.d.ts +19 -51
- package/dist/domain/sessions/session-lifecycle.js +34 -96
- package/dist/domain/system/doctor.d.ts +3 -3
- package/dist/domain/system/doctor.js +22 -26
- package/dist/domain/system/own-bin-path.d.ts +13 -0
- package/dist/domain/system/own-bin-path.js +36 -0
- package/dist/domain/system/server-update-check.js +2 -1
- package/dist/domain/system/terminal-cli/index.d.ts +58 -0
- package/dist/domain/system/terminal-cli/index.js +100 -0
- package/dist/domain/system/terminal-cli/posix-profiles.d.ts +23 -0
- package/dist/domain/system/terminal-cli/posix-profiles.js +74 -0
- package/dist/domain/system/terminal-cli/registrar.d.ts +12 -0
- package/dist/domain/system/terminal-cli/registrar.js +1 -0
- package/dist/domain/system/terminal-cli/shims.d.ts +27 -0
- package/dist/domain/system/terminal-cli/shims.js +108 -0
- package/dist/domain/system/terminal-cli/state.d.ts +7 -0
- package/dist/domain/system/terminal-cli/state.js +36 -0
- package/dist/domain/system/terminal-cli/windows-user-path.d.ts +13 -0
- package/dist/domain/system/terminal-cli/windows-user-path.js +67 -0
- package/dist/domain/telemetry/bus-collector.d.ts +3 -0
- package/dist/domain/telemetry/bus-collector.js +22 -0
- package/dist/domain/telemetry/child-exit.d.ts +11 -0
- package/dist/domain/telemetry/child-exit.js +36 -0
- package/dist/domain/telemetry/client-kind.d.ts +4 -0
- package/dist/domain/telemetry/client-kind.js +7 -0
- package/dist/domain/telemetry/crash-limits.d.ts +15 -0
- package/dist/domain/telemetry/crash-limits.js +50 -0
- package/dist/domain/telemetry/crash.d.ts +25 -0
- package/dist/domain/telemetry/crash.js +70 -0
- package/dist/domain/telemetry/daily-upload.d.ts +5 -0
- package/dist/domain/telemetry/daily-upload.js +20 -0
- package/dist/domain/telemetry/daily.d.ts +5 -0
- package/dist/domain/telemetry/daily.js +104 -0
- package/dist/domain/telemetry/endpoint.d.ts +7 -0
- package/dist/domain/telemetry/endpoint.js +40 -0
- package/dist/domain/telemetry/envelope.d.ts +6 -0
- package/dist/domain/telemetry/envelope.js +35 -0
- package/dist/domain/telemetry/error-digest.d.ts +7 -0
- package/dist/domain/telemetry/error-digest.js +43 -0
- package/dist/domain/telemetry/funnel.d.ts +6 -0
- package/dist/domain/telemetry/funnel.js +22 -0
- package/dist/domain/telemetry/gate.d.ts +19 -0
- package/dist/domain/telemetry/gate.js +67 -0
- package/dist/domain/telemetry/heartbeat.d.ts +17 -0
- package/dist/domain/telemetry/heartbeat.js +49 -0
- package/dist/domain/telemetry/info.d.ts +9 -0
- package/dist/domain/telemetry/info.js +12 -0
- package/dist/domain/telemetry/init-answer.d.ts +12 -0
- package/dist/domain/telemetry/init-answer.js +42 -0
- package/dist/domain/telemetry/install-id.d.ts +5 -0
- package/dist/domain/telemetry/install-id.js +42 -0
- package/dist/domain/telemetry/normalize.d.ts +33 -0
- package/dist/domain/telemetry/normalize.js +88 -0
- package/dist/domain/telemetry/outbox.d.ts +18 -0
- package/dist/domain/telemetry/outbox.js +93 -0
- package/dist/domain/telemetry/panic-group.d.ts +4 -0
- package/dist/domain/telemetry/panic-group.js +51 -0
- package/dist/domain/telemetry/paths.d.ts +7 -0
- package/dist/domain/telemetry/paths.js +11 -0
- package/dist/domain/telemetry/redact.d.ts +18 -0
- package/dist/domain/telemetry/redact.js +109 -0
- package/dist/domain/telemetry/rollup-types.d.ts +81 -0
- package/dist/domain/telemetry/rollup-types.js +31 -0
- package/dist/domain/telemetry/rollup.d.ts +16 -0
- package/dist/domain/telemetry/rollup.js +102 -0
- package/dist/domain/telemetry/schema.d.ts +931 -0
- package/dist/domain/telemetry/schema.js +275 -0
- package/dist/domain/telemetry/service.d.ts +34 -0
- package/dist/domain/telemetry/service.js +146 -0
- package/dist/domain/telemetry/state.d.ts +17 -0
- package/dist/domain/telemetry/state.js +29 -0
- package/dist/domain/telemetry/turns.d.ts +17 -0
- package/dist/domain/telemetry/turns.js +176 -0
- package/dist/domain/telemetry/upgrade.d.ts +14 -0
- package/dist/domain/telemetry/upgrade.js +87 -0
- package/dist/domain/telemetry/uploader.d.ts +21 -0
- package/dist/domain/telemetry/uploader.js +82 -0
- package/dist/domain/telemetry/usage.d.ts +25 -0
- package/dist/domain/telemetry/usage.js +153 -0
- package/dist/domain/telemetry/web-report.d.ts +8 -0
- package/dist/domain/telemetry/web-report.js +15 -0
- package/dist/domain/tui-session/tui-session-service.js +1 -1
- package/dist/domain/tui-session/types.d.ts +2 -2
- package/dist/domain/ui-service/app-router.d.ts +117 -95
- package/dist/domain/ui-service/app-router.js +17 -14
- package/dist/domain/ui-service/assistant-snapshots.js +2 -0
- package/dist/domain/ui-service/contract.parity.d.ts +19 -0
- package/dist/domain/ui-service/contract.parity.js +1 -0
- package/dist/domain/ui-service/inline-media-types.d.ts +18 -0
- package/dist/domain/ui-service/inline-media-types.js +1 -0
- package/dist/domain/ui-service/input-schemas.d.ts +120 -119
- package/dist/domain/ui-service/input-schemas.js +47 -85
- package/dist/domain/ui-service/mutate/approvals.d.ts +3 -2
- package/dist/domain/ui-service/mutate/approvals.js +3 -2
- package/dist/domain/ui-service/mutate/auth.d.ts +1 -1
- package/dist/domain/ui-service/mutate/auth.js +3 -2
- package/dist/domain/ui-service/mutate/config.d.ts +0 -8
- package/dist/domain/ui-service/mutate/config.js +0 -22
- package/dist/domain/ui-service/mutate/models.d.ts +13 -0
- package/dist/domain/ui-service/mutate/models.js +137 -0
- package/dist/domain/ui-service/mutate/plugin-packages.js +1 -1
- package/dist/domain/ui-service/mutate/resolve-media.d.ts +4 -0
- package/dist/domain/ui-service/mutate/resolve-media.js +17 -0
- package/dist/domain/ui-service/mutate/sessions.d.ts +2 -2
- package/dist/domain/ui-service/mutate/sessions.js +29 -126
- package/dist/domain/ui-service/mutate/telemetry.d.ts +5 -0
- package/dist/domain/ui-service/mutate/telemetry.js +6 -0
- package/dist/domain/ui-service/query/approvals.js +4 -3
- package/dist/domain/ui-service/query/config.js +22 -42
- package/dist/domain/ui-service/query/machine-probe.d.ts +1 -1
- package/dist/domain/ui-service/query/machine-probe.js +3 -2
- package/dist/domain/ui-service/query/models.d.ts +3 -0
- package/dist/domain/ui-service/query/models.js +49 -32
- package/dist/domain/ui-service/query/plugin-source.js +4 -3
- package/dist/domain/ui-service/query/schedules.js +1 -1
- package/dist/domain/ui-service/query/sessions.js +8 -13
- package/dist/domain/ui-service/query/telemetry.d.ts +3 -0
- package/dist/domain/ui-service/query/telemetry.js +4 -0
- package/dist/domain/ui-service/trpc.d.ts +8 -3
- package/dist/domain/ui-service/trpc.js +1 -1
- package/dist/domain/ui-service/types.d.ts +71 -179
- package/dist/domain/ui-service/ui-service.d.ts +56 -53
- package/dist/domain/ui-service/ui-service.js +27 -24
- package/dist/entry/app.js +62 -21
- package/dist/entry/browser-cli.d.ts +39 -0
- package/dist/entry/browser-cli.js +317 -0
- package/dist/entry/cli-help.d.ts +1 -0
- package/dist/entry/cli-help.js +64 -20
- package/dist/entry/cli.js +16 -14
- package/dist/entry/daemon.d.ts +1 -1
- package/dist/entry/daemon.js +88 -72
- package/dist/entry/init-auth.js +1 -1
- package/dist/entry/init.d.ts +10 -8
- package/dist/entry/init.js +138 -276
- package/dist/entry/path-cli.d.ts +4 -0
- package/dist/entry/path-cli.js +65 -0
- package/dist/entry/schedule-cli.js +13 -6
- package/dist/entry/shutdown.d.ts +1 -1
- package/dist/entry/shutdown.js +11 -1
- package/dist/entry/signal-cli.js +8 -11
- package/dist/entry/start-ui-http.js +48 -18
- package/dist/entry/telemetry-wiring.d.ts +12 -0
- package/dist/entry/telemetry-wiring.js +56 -0
- package/dist/entry/ui-cli.d.ts +0 -11
- package/dist/entry/ui-cli.js +42 -33
- package/dist/entry/ui-service-wiring.js +8 -10
- package/dist/events/event-types.d.ts +4 -6
- package/dist/orchestration/agent-runner.d.ts +2 -2
- package/dist/orchestration/agent-runner.js +10 -9
- package/dist/orchestration/benchmark-session-webhook.js +13 -1
- package/dist/orchestration/browser-webhook.d.ts +11 -0
- package/dist/orchestration/browser-webhook.js +28 -0
- package/dist/orchestration/cache-keep-warm.d.ts +77 -0
- package/dist/orchestration/cache-keep-warm.js +213 -0
- package/dist/orchestration/claude-window-warmup.d.ts +40 -0
- package/dist/orchestration/claude-window-warmup.js +126 -0
- package/dist/orchestration/conversation-request.d.ts +6 -10
- package/dist/orchestration/conversation-request.js +5 -23
- package/dist/orchestration/delta-coalescer.d.ts +4 -2
- package/dist/orchestration/delta-coalescer.js +5 -5
- package/dist/orchestration/edit-retry.js +9 -11
- package/dist/orchestration/inline-media-eager.d.ts +13 -0
- package/dist/orchestration/inline-media-eager.js +58 -0
- package/dist/orchestration/inline-media.d.ts +22 -0
- package/dist/orchestration/inline-media.js +151 -0
- package/dist/orchestration/interactions/ask-user-resume.js +3 -7
- package/dist/orchestration/interactions/interaction-handlers.js +6 -6
- package/dist/orchestration/mid-turn-inject.d.ts +2 -1
- package/dist/orchestration/mid-turn-inject.js +1 -1
- package/dist/orchestration/pending-injection-recovery.d.ts +2 -1
- package/dist/orchestration/pending-injection-recovery.js +1 -1
- package/dist/orchestration/routing/commands/index.js +4 -4
- package/dist/orchestration/routing/commands/mode.d.ts +5 -44
- package/dist/orchestration/routing/commands/mode.js +98 -272
- package/dist/orchestration/routing/commands/schedule.js +29 -18
- package/dist/orchestration/routing/commands/session.js +9 -9
- package/dist/orchestration/routing/commands/status.js +1 -1
- package/dist/orchestration/routing/commands/usage-format.js +3 -0
- package/dist/orchestration/routing/message-router.js +2 -0
- package/dist/orchestration/routing/webhook.js +6 -0
- package/dist/orchestration/session-compact.d.ts +0 -1
- package/dist/orchestration/session-compact.js +5 -7
- package/dist/orchestration/session-events.d.ts +2 -1
- package/dist/orchestration/session-events.js +1 -0
- package/dist/orchestration/status-helpers.d.ts +0 -4
- package/dist/orchestration/status-helpers.js +0 -18
- package/dist/orchestration/subagent-webhook.js +9 -34
- package/dist/orchestration/team-webhook.d.ts +8 -9
- package/dist/orchestration/team-webhook.js +16 -42
- package/dist/orchestration/transcript-sink.d.ts +12 -0
- package/dist/orchestration/transcript-sink.js +38 -2
- package/dist/orchestration/turn/terminal.js +7 -7
- package/dist/orchestration/turn/turn-prep.d.ts +0 -8
- package/dist/orchestration/turn/turn-prep.js +0 -51
- package/dist/orchestration/turn/turn-tracking.d.ts +2 -2
- package/dist/orchestration/turn/turn-tracking.js +8 -5
- package/dist/orchestration/turn/turn.d.ts +0 -2
- package/dist/orchestration/turn/turn.js +13 -15
- package/dist/orchestration/waitpoint-ingress.js +3 -0
- package/dist/platform/adapters/tui/ports.d.ts +1 -1
- package/dist/platform/adapters/tui/tui-transcript.js +2 -0
- package/dist/platform/browser/agent-browser.d.ts +50 -0
- package/dist/platform/browser/agent-browser.js +165 -0
- package/dist/platform/browser/managed-browser.d.ts +1 -11
- package/dist/platform/browser/managed-browser.js +4 -12
- package/dist/platform/ui-http/browser-stream.d.ts +110 -0
- package/dist/platform/ui-http/browser-stream.js +447 -0
- package/dist/platform/ui-http/ui-auth-routes.d.ts +2 -0
- package/dist/platform/ui-http/ui-auth-routes.js +9 -2
- package/dist/platform/ui-http/ui-device-routes.d.ts +13 -0
- package/dist/platform/ui-http/ui-device-routes.js +81 -0
- package/dist/platform/ui-http/ui-devices.d.ts +24 -0
- package/dist/platform/ui-http/ui-devices.js +107 -0
- package/dist/platform/ui-http/ui-http-server.d.ts +22 -3
- package/dist/platform/ui-http/ui-http-server.js +33 -12
- package/dist/platform/ui-http/ui-pairing.d.ts +25 -0
- package/dist/platform/ui-http/ui-pairing.js +98 -0
- package/dist/store/conversation-history-reader.js +8 -0
- package/dist/store/conversation-history-repo.d.ts +8 -1
- package/dist/store/conversation-history-repo.js +5 -0
- package/dist/store/conversation-ledger-repo.d.ts +6 -6
- package/dist/store/conversation-ledger-repo.js +4 -4
- package/dist/store/gitignore-migration.d.ts +2 -0
- package/dist/store/gitignore-migration.js +16 -0
- package/dist/store/hook-registry.d.ts +1 -1
- package/dist/store/hook-registry.js +11 -6
- package/dist/store/models-repo.d.ts +18 -0
- package/dist/store/models-repo.js +74 -0
- package/dist/store/pending-injection-repo.d.ts +2 -1
- package/dist/store/pending-injection-repo.js +3 -1
- package/dist/store/retire-profiles-migration.d.ts +28 -0
- package/dist/store/retire-profiles-migration.js +335 -0
- package/dist/store/schedule-repo.d.ts +2 -1
- package/dist/store/session-registry-import.js +3 -2
- package/dist/store/session-registry-journal.d.ts +10 -13
- package/dist/store/session-registry-journal.js +51 -25
- package/dist/store/session-registry-repo.d.ts +4 -4
- package/dist/store/session-registry-repo.js +4 -5
- package/dist/store/version-migrations.d.ts +7 -3
- package/dist/store/version-migrations.js +133 -80
- package/dist/tui/components/Dashboard.js +4 -3
- package/dist/tui/hooks/useDashboardData.js +1 -1
- package/dist/tui/slash-commands.js +2 -2
- package/dist/tui/turn-status.js +1 -1
- package/package.json +6 -4
- package/web/dist/assets/board-math-css-spk7yR6n.js +2 -0
- package/web/dist/assets/index-BQ69u-92.css +1 -0
- package/web/dist/assets/{index-CndZZ1IQ.js → index-CKNLatCd.js} +1 -1
- package/web/dist/assets/index-CkzWfC98.js +525 -0
- package/web/dist/index.html +3 -2
- package/web/dist/theme.css +6 -0
- package/dist/agent-adapter/browser-mcp-server.d.ts +0 -13
- package/dist/agent-adapter/browser-mcp-server.js +0 -28
- package/dist/agent-adapter/claude/browser-mcp.d.ts +0 -9
- package/dist/agent-adapter/claude/browser-mcp.js +0 -10
- package/dist/core/profile-generator.d.ts +0 -79
- package/dist/core/profile-generator.js +0 -215
- package/dist/domain/agents/profile-manager.d.ts +0 -103
- package/dist/domain/agents/profile-manager.js +0 -286
- package/dist/domain/agents/profile-switch.d.ts +0 -49
- package/dist/domain/agents/profile-switch.js +0 -62
- package/dist/domain/ui-service/mutate/profiles.d.ts +0 -36
- package/dist/domain/ui-service/mutate/profiles.js +0 -151
- package/dist/store/profile-repo.d.ts +0 -20
- package/dist/store/profile-repo.js +0 -97
- package/web/dist/assets/index-B76ueW3q.js +0 -494
- package/web/dist/assets/index-C8O0yxg8.css +0 -1
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
|
+
import { WebSocket, WebSocketServer } from 'ws';
|
|
3
|
+
import { timingSafeEqualStr } from '../../core/auth.js';
|
|
4
|
+
import { createLogger } from '../../core/log.js';
|
|
5
|
+
import { readJsonBody, sendError, sendJson, sendMethodNotAllowed } from './http-json.js';
|
|
6
|
+
const log = createLogger('browser-stream');
|
|
7
|
+
// The agent's browser, seen and driven from the app. Each Cortex session that holds a browser lease
|
|
8
|
+
// has an agent-browser daemon session, and that daemon serves a live JPEG stream plus an input
|
|
9
|
+
// channel on a loopback port. This module lists those sessions, proxies the stream to the app over
|
|
10
|
+
// the ui-http server, and lets a person take the page over from the agent.
|
|
11
|
+
//
|
|
12
|
+
// Authentication of the stream: a page cannot put `x-cortex-token` on a WebSocket handshake, and the
|
|
13
|
+
// cookie / Cloudflare Access legs would ride along on any cross-site handshake. So the socket is
|
|
14
|
+
// opened with a one-shot ticket instead: the app first POSTs for one through the ordinary gate (token,
|
|
15
|
+
// session cookie or Access JWT, plus the Origin check every state-changing request gets), then
|
|
16
|
+
// presents it on the handshake. A ticket names one session, lives for seconds and is spent on use.
|
|
17
|
+
//
|
|
18
|
+
// A takeover belongs to the app window that asked for it (its `client` id, which also rides on that
|
|
19
|
+
// window's stream ticket). Only that window's input reaches the page, and the page goes back to the
|
|
20
|
+
// agent by itself when the person is gone: once the window's stream has been closed for
|
|
21
|
+
// TAKEOVER_GRACE_MS without coming back (dock closed, page reloaded, network lost), or after
|
|
22
|
+
// TAKEOVER_IDLE_MS without a click, key or paste. The broker only knows on/off; the watch lives here.
|
|
23
|
+
export const BROWSER_SESSIONS_PATH = '/api/browser/sessions';
|
|
24
|
+
export const BROWSER_TICKET_PATH = '/api/browser/stream-ticket';
|
|
25
|
+
export const BROWSER_STREAM_PATH = '/api/browser/stream';
|
|
26
|
+
export const BROWSER_TAKEOVER_PATH = '/api/browser/takeover';
|
|
27
|
+
export const BROWSER_INSERT_TEXT_PATH = '/api/browser/insert-text';
|
|
28
|
+
/** The session has no stream to attach to (no lease, still starting, or its daemon went away). */
|
|
29
|
+
export const CLOSE_NO_STREAM = 4004;
|
|
30
|
+
/** The upstream stream ended underneath a live viewer. The app reconnects with a fresh ticket. */
|
|
31
|
+
export const CLOSE_UPSTREAM_GONE = 4010;
|
|
32
|
+
const TICKET_TTL_MS = 30_000;
|
|
33
|
+
const MAX_TICKETS = 256;
|
|
34
|
+
const MAX_STREAMS = 32;
|
|
35
|
+
const DEFAULT_FPS = 10;
|
|
36
|
+
const MAX_FPS = 15;
|
|
37
|
+
/** Input and acks are tiny; anything near this is not a client of ours. */
|
|
38
|
+
const MAX_CLIENT_MESSAGE_BYTES = 16 * 1024;
|
|
39
|
+
/** Longest text the paste field may insert at once. */
|
|
40
|
+
export const MAX_INSERT_TEXT_CHARS = 4000;
|
|
41
|
+
/** A viewer this far behind is shed frames rather than buffered without bound. */
|
|
42
|
+
const MAX_DOWNSTREAM_BUFFER = 4 * 1024 * 1024;
|
|
43
|
+
const UPSTREAM_CONNECT_TIMEOUT_MS = 5_000;
|
|
44
|
+
/** How long a takeover survives its window's stream being gone, so a reload or a blip keeps it. */
|
|
45
|
+
export const TAKEOVER_GRACE_MS = 30_000;
|
|
46
|
+
/** How long a takeover lasts without any input from the person. */
|
|
47
|
+
export const TAKEOVER_IDLE_MS = 10 * 60_000;
|
|
48
|
+
/** Message the proxy adds to the stream whenever the takeover changes (and once on connect). */
|
|
49
|
+
export const TAKEOVER_MESSAGE = 'cortex_takeover';
|
|
50
|
+
/** An app window's id: opaque, not a credential — every caller is already authenticated. */
|
|
51
|
+
const CLIENT_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
52
|
+
// ── Pure helpers ─────────────────────────────────────────────────────────────
|
|
53
|
+
const INPUT_TYPES = new Set(['input_mouse', 'input_keyboard', 'input_touch']);
|
|
54
|
+
/**
|
|
55
|
+
* Decide what of a viewer's message reaches the page. Acks always pass (they only pace frames);
|
|
56
|
+
* input passes only from the window holding the page, so a watcher can never steer it by accident.
|
|
57
|
+
* Everything else (a `config` raising the frame rate, unknown types) is dropped. `input` marks the
|
|
58
|
+
* person's own activity, which keeps the takeover alive.
|
|
59
|
+
*/
|
|
60
|
+
export function admitClientMessage(raw, holdsPage) {
|
|
61
|
+
let msg;
|
|
62
|
+
try {
|
|
63
|
+
msg = JSON.parse(raw);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
if (!msg || typeof msg !== 'object')
|
|
69
|
+
return null;
|
|
70
|
+
const type = msg.type;
|
|
71
|
+
if (type === 'ack') {
|
|
72
|
+
const seq = msg.seq;
|
|
73
|
+
return typeof seq === 'number' && Number.isFinite(seq) ? { message: JSON.stringify({ type: 'ack', seq }), input: false } : null;
|
|
74
|
+
}
|
|
75
|
+
if (typeof type === 'string' && INPUT_TYPES.has(type) && holdsPage)
|
|
76
|
+
return { message: raw, input: true };
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
/** A body's `client`: '' when absent, null when present but malformed. */
|
|
80
|
+
export function parseClientId(value) {
|
|
81
|
+
if (value === undefined || value === null || value === '')
|
|
82
|
+
return '';
|
|
83
|
+
return typeof value === 'string' && CLIENT_ID.test(value) ? value : null;
|
|
84
|
+
}
|
|
85
|
+
/** The frame-rate cap a viewer asked for, clamped to what the tunnel should carry. */
|
|
86
|
+
export function clampFps(raw) {
|
|
87
|
+
const n = Number(raw);
|
|
88
|
+
if (!raw || !Number.isFinite(n) || n <= 0)
|
|
89
|
+
return DEFAULT_FPS;
|
|
90
|
+
return Math.min(MAX_FPS, Math.max(1, Math.round(n)));
|
|
91
|
+
}
|
|
92
|
+
export function parseStreamTarget(rawUrl) {
|
|
93
|
+
let url;
|
|
94
|
+
try {
|
|
95
|
+
url = new URL(rawUrl, 'http://localhost');
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
const sessionId = url.searchParams.get('session') ?? '';
|
|
101
|
+
const ticket = url.searchParams.get('ticket') ?? '';
|
|
102
|
+
if (!sessionId || !ticket)
|
|
103
|
+
return null;
|
|
104
|
+
return { sessionId, ticket, fps: clampFps(url.searchParams.get('maxFps')) };
|
|
105
|
+
}
|
|
106
|
+
export function createTicketStore(now = Date.now) {
|
|
107
|
+
const tickets = new Map();
|
|
108
|
+
const prune = () => {
|
|
109
|
+
const t = now();
|
|
110
|
+
for (const [id, entry] of tickets)
|
|
111
|
+
if (entry.expiresAt <= t)
|
|
112
|
+
tickets.delete(id);
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
mint(sessionId, client = '') {
|
|
116
|
+
prune();
|
|
117
|
+
// A burst past the cap drops the oldest: a ticket is cheap to ask for again.
|
|
118
|
+
while (tickets.size >= MAX_TICKETS)
|
|
119
|
+
tickets.delete(tickets.keys().next().value);
|
|
120
|
+
const id = crypto.randomBytes(32).toString('hex');
|
|
121
|
+
tickets.set(id, { sessionId, client, expiresAt: now() + TICKET_TTL_MS });
|
|
122
|
+
return id;
|
|
123
|
+
},
|
|
124
|
+
// A constant-time walk rather than a Map lookup, as the UI session store does: the ticket is a
|
|
125
|
+
// credential for a few seconds, and the set is a handful of entries.
|
|
126
|
+
redeem(ticket, sessionId) {
|
|
127
|
+
prune();
|
|
128
|
+
let hit = null;
|
|
129
|
+
for (const id of tickets.keys())
|
|
130
|
+
if (timingSafeEqualStr(id, ticket))
|
|
131
|
+
hit = id;
|
|
132
|
+
if (hit === null)
|
|
133
|
+
return null;
|
|
134
|
+
const entry = tickets.get(hit);
|
|
135
|
+
tickets.delete(hit);
|
|
136
|
+
return entry.sessionId === sessionId ? { client: entry.client } : null;
|
|
137
|
+
},
|
|
138
|
+
size: () => {
|
|
139
|
+
prune();
|
|
140
|
+
return tickets.size;
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
export function createTakeoverWatch(deps) {
|
|
145
|
+
const holds = new Map();
|
|
146
|
+
const stop = (hold) => {
|
|
147
|
+
if (hold.grace)
|
|
148
|
+
clearTimeout(hold.grace);
|
|
149
|
+
if (hold.idle)
|
|
150
|
+
clearTimeout(hold.idle);
|
|
151
|
+
hold.grace = null;
|
|
152
|
+
hold.idle = null;
|
|
153
|
+
};
|
|
154
|
+
const expire = (sessionId, hold, reason) => {
|
|
155
|
+
if (holds.get(sessionId) !== hold)
|
|
156
|
+
return;
|
|
157
|
+
stop(hold);
|
|
158
|
+
holds.delete(sessionId);
|
|
159
|
+
// The broker may already have dropped it (lease retired); then there is nothing to hand back.
|
|
160
|
+
if (deps.isTakenOver(sessionId))
|
|
161
|
+
deps.release(sessionId, reason);
|
|
162
|
+
};
|
|
163
|
+
// One timer per hold, checked lazily: input only moves the timestamp, the timer re-arms itself.
|
|
164
|
+
const armIdle = (sessionId, hold) => {
|
|
165
|
+
const wait = Math.max(0, hold.lastInputAt + deps.idleMs - deps.now());
|
|
166
|
+
hold.idle = setTimeout(() => {
|
|
167
|
+
hold.idle = null;
|
|
168
|
+
if (deps.now() - hold.lastInputAt >= deps.idleMs)
|
|
169
|
+
expire(sessionId, hold, 'idle');
|
|
170
|
+
else
|
|
171
|
+
armIdle(sessionId, hold);
|
|
172
|
+
}, wait);
|
|
173
|
+
hold.idle.unref?.();
|
|
174
|
+
};
|
|
175
|
+
const armGrace = (sessionId, hold) => {
|
|
176
|
+
if (hold.grace)
|
|
177
|
+
return;
|
|
178
|
+
hold.grace = setTimeout(() => {
|
|
179
|
+
hold.grace = null;
|
|
180
|
+
expire(sessionId, hold, 'disconnected');
|
|
181
|
+
}, deps.graceMs);
|
|
182
|
+
hold.grace.unref?.();
|
|
183
|
+
};
|
|
184
|
+
return {
|
|
185
|
+
begin(sessionId, client) {
|
|
186
|
+
const prev = holds.get(sessionId);
|
|
187
|
+
if (prev)
|
|
188
|
+
stop(prev);
|
|
189
|
+
const hold = { client, lastInputAt: deps.now(), grace: null, idle: null };
|
|
190
|
+
holds.set(sessionId, hold);
|
|
191
|
+
armIdle(sessionId, hold);
|
|
192
|
+
if (!deps.hasClient(sessionId, client))
|
|
193
|
+
armGrace(sessionId, hold);
|
|
194
|
+
},
|
|
195
|
+
end(sessionId) {
|
|
196
|
+
const hold = holds.get(sessionId);
|
|
197
|
+
if (!hold)
|
|
198
|
+
return;
|
|
199
|
+
stop(hold);
|
|
200
|
+
holds.delete(sessionId);
|
|
201
|
+
},
|
|
202
|
+
holder: (sessionId) => holds.get(sessionId)?.client ?? null,
|
|
203
|
+
connected(sessionId, client) {
|
|
204
|
+
const hold = holds.get(sessionId);
|
|
205
|
+
if (hold?.client === client && hold.grace) {
|
|
206
|
+
clearTimeout(hold.grace);
|
|
207
|
+
hold.grace = null;
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
disconnected(sessionId, client) {
|
|
211
|
+
const hold = holds.get(sessionId);
|
|
212
|
+
if (hold?.client === client && !deps.hasClient(sessionId, client))
|
|
213
|
+
armGrace(sessionId, hold);
|
|
214
|
+
},
|
|
215
|
+
input(sessionId) {
|
|
216
|
+
const hold = holds.get(sessionId);
|
|
217
|
+
if (hold)
|
|
218
|
+
hold.lastInputAt = deps.now();
|
|
219
|
+
},
|
|
220
|
+
close() {
|
|
221
|
+
for (const hold of holds.values())
|
|
222
|
+
stop(hold);
|
|
223
|
+
holds.clear();
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
// ── Routes ───────────────────────────────────────────────────────────────────
|
|
228
|
+
function publicView(s) {
|
|
229
|
+
return {
|
|
230
|
+
sessionId: s.sessionId,
|
|
231
|
+
device: s.device,
|
|
232
|
+
state: s.state,
|
|
233
|
+
...(s.error ? { error: s.error } : {}),
|
|
234
|
+
...(s.url ? { url: s.url } : {}),
|
|
235
|
+
...(s.title ? { title: s.title } : {}),
|
|
236
|
+
takenOver: s.takenOver,
|
|
237
|
+
lastUsedAt: s.lastUsedAt,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/** `{ session }` from a POST body for a session that holds a browser, or null after answering. */
|
|
241
|
+
async function readSessionBody(req, res, deps) {
|
|
242
|
+
if (req.method !== 'POST') {
|
|
243
|
+
sendMethodNotAllowed(res, 'POST');
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
const body = await readJsonBody(req, res);
|
|
247
|
+
if (!body)
|
|
248
|
+
return null;
|
|
249
|
+
const sessionId = typeof body.session === 'string' ? body.session : '';
|
|
250
|
+
if (!sessionId) {
|
|
251
|
+
sendError(res, 400, 'missing-session', 'session is required');
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
if (!deps.listSessions().some((s) => s.sessionId === sessionId)) {
|
|
255
|
+
sendError(res, 404, 'no-browser', 'This session has no browser');
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
return { sessionId, body };
|
|
259
|
+
}
|
|
260
|
+
export function createBrowserStream(deps, opts = {}) {
|
|
261
|
+
const now = deps.now ?? Date.now;
|
|
262
|
+
const tickets = createTicketStore(now);
|
|
263
|
+
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_CLIENT_MESSAGE_BYTES });
|
|
264
|
+
const live = new Set();
|
|
265
|
+
const viewersOf = (sessionId) => [...live].filter((v) => v.sessionId === sessionId);
|
|
266
|
+
const holdsPage = (viewer) => viewer.client !== '' && deps.isTakenOver(viewer.sessionId) && watch.holder(viewer.sessionId) === viewer.client;
|
|
267
|
+
/** Tell each viewer of the session whether the page is held, and whether by its own window. */
|
|
268
|
+
const announce = (sessionId) => {
|
|
269
|
+
for (const viewer of viewersOf(sessionId)) {
|
|
270
|
+
if (viewer.ws.readyState !== WebSocket.OPEN)
|
|
271
|
+
continue;
|
|
272
|
+
viewer.ws.send(JSON.stringify({
|
|
273
|
+
type: TAKEOVER_MESSAGE, takenOver: deps.isTakenOver(sessionId), mine: holdsPage(viewer),
|
|
274
|
+
}));
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
const watch = createTakeoverWatch({
|
|
278
|
+
isTakenOver: deps.isTakenOver,
|
|
279
|
+
hasClient: (sessionId, client) => client !== '' && viewersOf(sessionId).some((v) => v.client === client),
|
|
280
|
+
now,
|
|
281
|
+
graceMs: opts.graceMs ?? TAKEOVER_GRACE_MS,
|
|
282
|
+
idleMs: opts.idleMs ?? TAKEOVER_IDLE_MS,
|
|
283
|
+
release: (sessionId, reason) => {
|
|
284
|
+
void (async () => {
|
|
285
|
+
try {
|
|
286
|
+
await deps.setTakeover(sessionId, false);
|
|
287
|
+
}
|
|
288
|
+
catch (err) {
|
|
289
|
+
log.warn(`could not hand ${sessionId} back: ${err.message}`);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
log.info(`browser handed back automatically (${reason === 'idle' ? 'no input' : 'window gone'}): ${sessionId}`);
|
|
293
|
+
announce(sessionId);
|
|
294
|
+
})();
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
const routes = {
|
|
298
|
+
[BROWSER_SESSIONS_PATH]: async (req, res) => {
|
|
299
|
+
if (req.method !== 'GET')
|
|
300
|
+
return sendMethodNotAllowed(res, 'GET');
|
|
301
|
+
sendJson(res, 200, { ok: true, data: { sessions: deps.listSessions().map(publicView) } }, { 'Cache-Control': 'no-store' });
|
|
302
|
+
},
|
|
303
|
+
[BROWSER_TICKET_PATH]: async (req, res) => {
|
|
304
|
+
const target = await readSessionBody(req, res, deps);
|
|
305
|
+
if (!target)
|
|
306
|
+
return;
|
|
307
|
+
const client = parseClientId(target.body.client);
|
|
308
|
+
if (client === null)
|
|
309
|
+
return sendError(res, 400, 'bad-client', 'client must be 1-64 letters, digits, _ or -');
|
|
310
|
+
sendJson(res, 200, { ok: true, data: { ticket: tickets.mint(target.sessionId, client) } }, { 'Cache-Control': 'no-store' });
|
|
311
|
+
},
|
|
312
|
+
[BROWSER_TAKEOVER_PATH]: async (req, res) => {
|
|
313
|
+
const target = await readSessionBody(req, res, deps);
|
|
314
|
+
if (!target)
|
|
315
|
+
return;
|
|
316
|
+
const on = target.body.on;
|
|
317
|
+
if (typeof on !== 'boolean')
|
|
318
|
+
return sendError(res, 400, 'bad-on', 'on must be a boolean');
|
|
319
|
+
const client = parseClientId(target.body.client);
|
|
320
|
+
// A takeover is held by a window, so taking one needs to say which.
|
|
321
|
+
if (client === null || (on && client === '')) {
|
|
322
|
+
return sendError(res, 400, 'bad-client', 'client must be 1-64 letters, digits, _ or -');
|
|
323
|
+
}
|
|
324
|
+
await deps.setTakeover(target.sessionId, on);
|
|
325
|
+
if (on)
|
|
326
|
+
watch.begin(target.sessionId, client);
|
|
327
|
+
else
|
|
328
|
+
watch.end(target.sessionId);
|
|
329
|
+
log.info(`browser ${on ? 'taken over' : 'handed back'}: ${target.sessionId}`);
|
|
330
|
+
announce(target.sessionId);
|
|
331
|
+
sendJson(res, 200, { ok: true, data: { takenOver: deps.isTakenOver(target.sessionId) } });
|
|
332
|
+
},
|
|
333
|
+
[BROWSER_INSERT_TEXT_PATH]: async (req, res) => {
|
|
334
|
+
const target = await readSessionBody(req, res, deps);
|
|
335
|
+
if (!target)
|
|
336
|
+
return;
|
|
337
|
+
const text = typeof target.body.text === 'string' ? target.body.text : '';
|
|
338
|
+
if (!text)
|
|
339
|
+
return sendError(res, 400, 'missing-text', 'text is required');
|
|
340
|
+
if (text.length > MAX_INSERT_TEXT_CHARS) {
|
|
341
|
+
return sendError(res, 400, 'text-too-long', `text exceeds ${MAX_INSERT_TEXT_CHARS} characters`);
|
|
342
|
+
}
|
|
343
|
+
// Typing into the page is the person's move, so it needs the page to be theirs.
|
|
344
|
+
if (!deps.isTakenOver(target.sessionId))
|
|
345
|
+
return sendError(res, 409, 'not-taken-over', 'Take over the browser first');
|
|
346
|
+
watch.input(target.sessionId);
|
|
347
|
+
try {
|
|
348
|
+
await deps.insertText(target.sessionId, text);
|
|
349
|
+
}
|
|
350
|
+
catch (err) {
|
|
351
|
+
return sendError(res, 502, 'insert-failed', err.message);
|
|
352
|
+
}
|
|
353
|
+
sendJson(res, 200, { ok: true, data: {} });
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
const attach = async (viewer, target) => {
|
|
357
|
+
const { ws } = viewer;
|
|
358
|
+
let port = null;
|
|
359
|
+
try {
|
|
360
|
+
port = await deps.streamPort(target.sessionId);
|
|
361
|
+
}
|
|
362
|
+
catch (err) {
|
|
363
|
+
log.warn(`stream port lookup failed for ${target.sessionId}: ${err.message}`);
|
|
364
|
+
}
|
|
365
|
+
if (ws.readyState !== WebSocket.OPEN)
|
|
366
|
+
return;
|
|
367
|
+
if (port === null) {
|
|
368
|
+
ws.close(CLOSE_NO_STREAM, 'no stream');
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
// Ack pacing end to end: the app acks each frame once drawn, so a slow tunnel holds one frame in
|
|
372
|
+
// flight instead of a queue of stale ones.
|
|
373
|
+
const upstream = new WebSocket(`ws://127.0.0.1:${port}/?maxFps=${target.fps}&pacing=ack`, {
|
|
374
|
+
handshakeTimeout: UPSTREAM_CONNECT_TIMEOUT_MS,
|
|
375
|
+
});
|
|
376
|
+
let opened = false;
|
|
377
|
+
const finish = (code, reason) => {
|
|
378
|
+
if (upstream.readyState === WebSocket.OPEN || upstream.readyState === WebSocket.CONNECTING)
|
|
379
|
+
upstream.terminate();
|
|
380
|
+
if (ws.readyState === WebSocket.OPEN)
|
|
381
|
+
ws.close(code, reason);
|
|
382
|
+
};
|
|
383
|
+
upstream.on('open', () => {
|
|
384
|
+
opened = true;
|
|
385
|
+
});
|
|
386
|
+
upstream.on('message', (data, isBinary) => {
|
|
387
|
+
if (ws.readyState !== WebSocket.OPEN)
|
|
388
|
+
return;
|
|
389
|
+
if (ws.bufferedAmount > MAX_DOWNSTREAM_BUFFER)
|
|
390
|
+
return;
|
|
391
|
+
ws.send(data, { binary: isBinary });
|
|
392
|
+
});
|
|
393
|
+
upstream.on('close', () => finish(opened ? CLOSE_UPSTREAM_GONE : CLOSE_NO_STREAM, opened ? 'stream ended' : 'no stream'));
|
|
394
|
+
upstream.on('error', (err) => {
|
|
395
|
+
log.warn(`upstream stream error for ${target.sessionId}: ${err.message}`);
|
|
396
|
+
finish(opened ? CLOSE_UPSTREAM_GONE : CLOSE_NO_STREAM, opened ? 'stream ended' : 'no stream');
|
|
397
|
+
});
|
|
398
|
+
ws.on('message', (data, isBinary) => {
|
|
399
|
+
if (isBinary || upstream.readyState !== WebSocket.OPEN)
|
|
400
|
+
return;
|
|
401
|
+
const admitted = admitClientMessage(data.toString(), holdsPage(viewer));
|
|
402
|
+
if (!admitted)
|
|
403
|
+
return;
|
|
404
|
+
if (admitted.input)
|
|
405
|
+
watch.input(target.sessionId);
|
|
406
|
+
upstream.send(admitted.message);
|
|
407
|
+
});
|
|
408
|
+
ws.on('close', () => finish(1000, 'viewer closed'));
|
|
409
|
+
};
|
|
410
|
+
const handleUpgrade = (req, socket, head) => {
|
|
411
|
+
const target = parseStreamTarget(req.url ?? '');
|
|
412
|
+
const redeemed = target ? tickets.redeem(target.ticket, target.sessionId) : null;
|
|
413
|
+
if (!target || !redeemed) {
|
|
414
|
+
log.warn('browser stream refused: missing or invalid ticket');
|
|
415
|
+
socket.write('HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n');
|
|
416
|
+
socket.destroy();
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (live.size >= MAX_STREAMS) {
|
|
420
|
+
log.warn(`browser stream refused (${live.size} already open)`);
|
|
421
|
+
socket.write('HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\n');
|
|
422
|
+
socket.destroy();
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
426
|
+
const viewer = { ws, sessionId: target.sessionId, client: redeemed.client };
|
|
427
|
+
live.add(viewer);
|
|
428
|
+
watch.connected(viewer.sessionId, viewer.client);
|
|
429
|
+
ws.on('close', () => {
|
|
430
|
+
live.delete(viewer);
|
|
431
|
+
watch.disconnected(viewer.sessionId, viewer.client);
|
|
432
|
+
});
|
|
433
|
+
ws.on('error', () => ws.terminate());
|
|
434
|
+
// Before any frame: whether this window may steer the page.
|
|
435
|
+
ws.send(JSON.stringify({ type: TAKEOVER_MESSAGE, takenOver: deps.isTakenOver(viewer.sessionId), mine: holdsPage(viewer) }));
|
|
436
|
+
void attach(viewer, target);
|
|
437
|
+
});
|
|
438
|
+
};
|
|
439
|
+
const close = () => {
|
|
440
|
+
watch.close();
|
|
441
|
+
for (const viewer of live)
|
|
442
|
+
viewer.ws.terminate();
|
|
443
|
+
live.clear();
|
|
444
|
+
wss.close();
|
|
445
|
+
};
|
|
446
|
+
return { routes, handleUpgrade, close };
|
|
447
|
+
}
|
|
@@ -23,6 +23,8 @@ export interface UiAuthRouteDeps {
|
|
|
23
23
|
authorize: (req: http.IncomingMessage) => Promise<boolean>;
|
|
24
24
|
/** Session lifetime, mirrored into the cookie's Max-Age. */
|
|
25
25
|
ttlMs: number;
|
|
26
|
+
/** Consume a one-time pairing code (ui-pairing.ts). Absent → only the client token signs in. */
|
|
27
|
+
redeemCode?: (code: string) => boolean;
|
|
26
28
|
}
|
|
27
29
|
/**
|
|
28
30
|
* Whether this request arrived over a channel where a `Secure` cookie will actually be stored:
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { timingSafeEqualStr } from '../../core/auth.js';
|
|
5
5
|
import { createLogger } from '../../core/log.js';
|
|
6
6
|
import { readJsonBody, sendError, sendJson, sendMethodNotAllowed } from './http-json.js';
|
|
7
|
+
import { normalizePairingCode } from './ui-pairing.js';
|
|
7
8
|
import { parseCookie } from './ui-session.js';
|
|
8
9
|
const log = createLogger('ui-http');
|
|
9
10
|
/** Cookie carrying the opaque session id. Name is stable — the SPA never reads it (HttpOnly). */
|
|
@@ -55,6 +56,12 @@ export function sessionCookie(sid, maxAgeSec, secure) {
|
|
|
55
56
|
export function sessionIdFrom(req) {
|
|
56
57
|
return parseCookie(req.headers.cookie, UI_SESSION_COOKIE);
|
|
57
58
|
}
|
|
59
|
+
/** A typed pairing code is spent on the code store; anything else must be the client token. */
|
|
60
|
+
function acceptsLogin(deps, presented) {
|
|
61
|
+
if (deps.redeemCode && normalizePairingCode(presented))
|
|
62
|
+
return deps.redeemCode(presented);
|
|
63
|
+
return timingSafeEqualStr(deps.getToken(), presented);
|
|
64
|
+
}
|
|
58
65
|
/**
|
|
59
66
|
* The three token-login routes. `/api/ui/login` and `/api/ui/session` MUST be registered as public
|
|
60
67
|
* routes on the server (a browser with no credential has to be able to reach them); `/api/ui/logout`
|
|
@@ -72,9 +79,9 @@ export function createUiAuthRoutes(deps) {
|
|
|
72
79
|
if (!body)
|
|
73
80
|
return;
|
|
74
81
|
const presented = typeof body.token === 'string' ? body.token.trim() : '';
|
|
75
|
-
if (!
|
|
82
|
+
if (!acceptsLogin(deps, presented)) {
|
|
76
83
|
await new Promise((resolve) => setTimeout(resolve, LOGIN_FAILURE_DELAY_MS));
|
|
77
|
-
log.warn('ui login rejected: wrong token');
|
|
84
|
+
log.warn('ui login rejected: wrong token or pairing code');
|
|
78
85
|
return sendError(res, 401, 'invalid-token', 'Invalid token');
|
|
79
86
|
}
|
|
80
87
|
const sid = deps.store.create();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CustomRouteHandler } from './ui-http-server.js';
|
|
2
|
+
import type { UiDeviceStore } from './ui-devices.js';
|
|
3
|
+
import type { PairingStore } from './ui-pairing.js';
|
|
4
|
+
export declare const UI_PAIR_PATH = "/api/ui/pair";
|
|
5
|
+
export declare const UI_PAIRING_CODE_PATH = "/api/ui/pairing-code";
|
|
6
|
+
export declare const UI_DEVICES_PATH = "/api/ui/devices";
|
|
7
|
+
export declare const UI_DEVICE_REVOKE_PATH = "/api/ui/devices/revoke";
|
|
8
|
+
export interface UiDeviceRouteDeps {
|
|
9
|
+
devices: UiDeviceStore;
|
|
10
|
+
/** Absent when token login is off. */
|
|
11
|
+
pairing?: PairingStore;
|
|
12
|
+
}
|
|
13
|
+
export declare function createUiDeviceRoutes(deps: UiDeviceRouteDeps): Record<string, CustomRouteHandler>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Pairing and paired-device routes.
|
|
2
|
+
//
|
|
3
|
+
// POST /api/ui/pair PUBLIC {code, name} → {token}: an app trades a pairing code for its
|
|
4
|
+
// own device token.
|
|
5
|
+
// POST /api/ui/pairing-code gated → {code, expiresAt}: a signed-in UI mints a code.
|
|
6
|
+
// GET /api/ui/devices gated → {devices, pairing}: paired apps, `current` marking the caller.
|
|
7
|
+
// POST /api/ui/devices/revoke gated {id}: the device's token stops working at once.
|
|
8
|
+
//
|
|
9
|
+
// Pairing (the first two) exists only while browser token login is on; with it off the server has
|
|
10
|
+
// no public door at all, and the list only offers revoking what was paired before.
|
|
11
|
+
import { AUTH_HEADER } from '../../core/auth.js';
|
|
12
|
+
import { createLogger } from '../../core/log.js';
|
|
13
|
+
import { readJsonBody, sendError, sendJson, sendMethodNotAllowed } from './http-json.js';
|
|
14
|
+
import { LOGIN_FAILURE_DELAY_MS } from './ui-auth-routes.js';
|
|
15
|
+
const log = createLogger('ui-http');
|
|
16
|
+
export const UI_PAIR_PATH = '/api/ui/pair';
|
|
17
|
+
export const UI_PAIRING_CODE_PATH = '/api/ui/pairing-code';
|
|
18
|
+
export const UI_DEVICES_PATH = '/api/ui/devices';
|
|
19
|
+
export const UI_DEVICE_REVOKE_PATH = '/api/ui/devices/revoke';
|
|
20
|
+
function presentedToken(req) {
|
|
21
|
+
const v = req.headers[AUTH_HEADER];
|
|
22
|
+
return Array.isArray(v) ? v[0] : v;
|
|
23
|
+
}
|
|
24
|
+
function pairRoute(devices, pairing) {
|
|
25
|
+
return async (req, res) => {
|
|
26
|
+
if (req.method !== 'POST')
|
|
27
|
+
return sendMethodNotAllowed(res, 'POST');
|
|
28
|
+
const body = await readJsonBody(req, res);
|
|
29
|
+
if (!body)
|
|
30
|
+
return;
|
|
31
|
+
const code = typeof body.code === 'string' ? body.code : '';
|
|
32
|
+
if (!pairing.redeem(code)) {
|
|
33
|
+
await new Promise((resolve) => setTimeout(resolve, LOGIN_FAILURE_DELAY_MS));
|
|
34
|
+
log.warn('app pairing rejected: wrong or expired code');
|
|
35
|
+
return sendError(res, 401, 'invalid-code', 'Invalid or expired pairing code');
|
|
36
|
+
}
|
|
37
|
+
const { device, token } = devices.issue(typeof body.name === 'string' ? body.name : '');
|
|
38
|
+
log.info(`app paired: ${device.name} (${device.id})`);
|
|
39
|
+
sendJson(res, 200, { ok: true, data: { token, device } });
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function pairingCodeRoute(pairing) {
|
|
43
|
+
return async (req, res) => {
|
|
44
|
+
if (req.method !== 'POST')
|
|
45
|
+
return sendMethodNotAllowed(res, 'POST');
|
|
46
|
+
sendJson(res, 200, { ok: true, data: pairing.create() }, { 'Cache-Control': 'no-store' });
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function listRoute(deps) {
|
|
50
|
+
return async (req, res) => {
|
|
51
|
+
if (req.method !== 'GET')
|
|
52
|
+
return sendMethodNotAllowed(res, 'GET');
|
|
53
|
+
const current = deps.devices.match(presentedToken(req))?.id;
|
|
54
|
+
const devices = deps.devices.list().map((d) => ({ ...d, current: d.id === current }));
|
|
55
|
+
sendJson(res, 200, { ok: true, data: { devices, pairing: !!deps.pairing } }, { 'Cache-Control': 'no-store' });
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function revokeRoute(devices) {
|
|
59
|
+
return async (req, res) => {
|
|
60
|
+
if (req.method !== 'POST')
|
|
61
|
+
return sendMethodNotAllowed(res, 'POST');
|
|
62
|
+
const body = await readJsonBody(req, res);
|
|
63
|
+
if (!body)
|
|
64
|
+
return;
|
|
65
|
+
const id = typeof body.id === 'string' ? body.id : '';
|
|
66
|
+
if (!devices.revoke(id))
|
|
67
|
+
return sendError(res, 404, 'not-found', 'No such device');
|
|
68
|
+
log.info(`paired app revoked: ${id}`);
|
|
69
|
+
sendJson(res, 200, { ok: true });
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
export function createUiDeviceRoutes(deps) {
|
|
73
|
+
const pairingRoutes = deps.pairing
|
|
74
|
+
? { [UI_PAIR_PATH]: pairRoute(deps.devices, deps.pairing), [UI_PAIRING_CODE_PATH]: pairingCodeRoute(deps.pairing) }
|
|
75
|
+
: {};
|
|
76
|
+
return {
|
|
77
|
+
...pairingRoutes,
|
|
78
|
+
[UI_DEVICES_PATH]: listRoute(deps),
|
|
79
|
+
[UI_DEVICE_REVOKE_PATH]: revokeRoute(deps.devices),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Last-use times move on every request; disk sees them at most this often per device. */
|
|
2
|
+
export declare const DEVICE_SEEN_PERSIST_MS: number;
|
|
3
|
+
export interface UiDevice {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
createdAt: number;
|
|
7
|
+
lastSeenAt: number;
|
|
8
|
+
}
|
|
9
|
+
export interface UiDeviceStore {
|
|
10
|
+
/** Register a device and return its token. The token is never retrievable again. */
|
|
11
|
+
issue(name: string): {
|
|
12
|
+
device: UiDevice;
|
|
13
|
+
token: string;
|
|
14
|
+
};
|
|
15
|
+
/** The device a presented token belongs to, recording the use. */
|
|
16
|
+
match(token: string | undefined): UiDevice | undefined;
|
|
17
|
+
list(): UiDevice[];
|
|
18
|
+
/** False when no such device exists. */
|
|
19
|
+
revoke(id: string): boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function createUiDeviceStore(opts?: {
|
|
22
|
+
file?: string;
|
|
23
|
+
now?: () => number;
|
|
24
|
+
}): UiDeviceStore;
|