nova-personal-agent-server 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +30 -2
- package/bin/novapersonal-server.mjs +4 -0
- package/package.json +61 -4
- package/resources/endpointing/volcengine-v1/LICENSE.silero-vad.txt +21 -0
- package/resources/endpointing/volcengine-v1/MANIFEST.json +31 -0
- package/resources/endpointing/volcengine-v1/silence-16k-s16le.pcm +0 -0
- package/resources/endpointing/volcengine-v1/speech-16k-s16le.pcm +0 -0
- package/resources/native/codex-sandbox-probe +0 -0
- package/resources/native/project-native/nova_project_native.node +0 -0
- package/resources/native-resources-v1.json +1 -0
- package/runtime/dist/eval/demos.js +156 -0
- package/runtime/dist/eval/fixture-host.js +385 -0
- package/runtime/dist/eval/fixtures.js +319 -0
- package/runtime/dist/eval/realtime/session-fixtures.js +474 -0
- package/runtime/dist/eval/scorecard.js +285 -0
- package/runtime/dist/eval/sim.js +43 -0
- package/runtime/dist/src/cascaded-text-provider.js +31 -0
- package/runtime/dist/src/cli.js +96 -0
- package/runtime/dist/src/composition/assembly.js +352 -0
- package/runtime/dist/src/composition/cascaded-realtime-assembly.js +455 -0
- package/runtime/dist/src/composition/production-composition.js +389 -0
- package/runtime/dist/src/composition/realtime-assembly.js +1330 -0
- package/runtime/dist/src/config/capability-registry.js +315 -0
- package/runtime/dist/src/config/cascaded-realtime-config.js +146 -0
- package/runtime/dist/src/config/coding-backends.js +8 -0
- package/runtime/dist/src/config/coding-profiles.js +98 -0
- package/runtime/dist/src/config/config.js +857 -0
- package/runtime/dist/src/config/diagnostics.js +139 -0
- package/runtime/dist/src/config/environment-contract.js +202 -0
- package/runtime/dist/src/config/self-hosted.js +23 -0
- package/runtime/dist/src/connectors/composio/client.js +195 -0
- package/runtime/dist/src/connectors/composio/google.js +208 -0
- package/runtime/dist/src/connectors/composio/index.js +334 -0
- package/runtime/dist/src/connectors/feishu/cli.js +220 -0
- package/runtime/dist/src/connectors/feishu/index.js +980 -0
- package/runtime/dist/src/connectors/macos/calendar.js +35 -0
- package/runtime/dist/src/connectors/macos/mail.js +32 -0
- package/runtime/dist/src/connectors/macos/reader.js +62 -0
- package/runtime/dist/src/conversation-voice-provider.js +41 -0
- package/runtime/dist/src/core/approval-port.js +9 -0
- package/runtime/dist/src/core/approval.js +1328 -0
- package/runtime/dist/src/core/calls.js +35 -0
- package/runtime/dist/src/core/camera-session.js +32 -0
- package/runtime/dist/src/core/causal-runtime.js +650 -0
- package/runtime/dist/src/core/clock.js +139 -0
- package/runtime/dist/src/core/context-view.js +156 -0
- package/runtime/dist/src/core/effects.js +62 -0
- package/runtime/dist/src/core/events.js +217 -0
- package/runtime/dist/src/core/executor-diagnostic.js +9 -0
- package/runtime/dist/src/core/ids.js +32 -0
- package/runtime/dist/src/core/media-store.js +104 -0
- package/runtime/dist/src/core/memory.js +296 -0
- package/runtime/dist/src/core/pending-decision.js +64 -0
- package/runtime/dist/src/core/ports.js +125 -0
- package/runtime/dist/src/core/runtime.js +1459 -0
- package/runtime/dist/src/core/schedule-tools.js +15 -0
- package/runtime/dist/src/core/sims.js +186 -0
- package/runtime/dist/src/core/slots.js +97 -0
- package/runtime/dist/src/core/source-processing-instructions.js +3 -0
- package/runtime/dist/src/core/source-processing-tools.js +12 -0
- package/runtime/dist/src/core/suggestions.js +143 -0
- package/runtime/dist/src/core/task-tools.js +22 -0
- package/runtime/dist/src/core/tool-schema.js +212 -0
- package/runtime/dist/src/core/trace.js +34 -0
- package/runtime/dist/src/core/work-tools.js +94 -0
- package/runtime/dist/src/desktop/desktop-camera-source.js +31 -0
- package/runtime/dist/src/desktop/desktop-camera.js +281 -0
- package/runtime/dist/src/desktop/desktop-control.js +140 -0
- package/runtime/dist/src/desktop/desktop-progress.js +162 -0
- package/runtime/dist/src/desktop/desktop-session.js +2106 -0
- package/runtime/dist/src/desktop/desktop-tasks.js +80 -0
- package/runtime/dist/src/desktop/desktop-wire.js +605 -0
- package/runtime/dist/src/desktop/phone-desktop-entry.js +17 -0
- package/runtime/dist/src/desktop/startup-error.js +18 -0
- package/runtime/dist/src/desktop/workbench-acceptance.js +262 -0
- package/runtime/dist/src/desktop-entry.js +106 -0
- package/runtime/dist/src/desktop.js +1092 -0
- package/runtime/dist/src/executors/acp/backends.js +105 -0
- package/runtime/dist/src/executors/acp/mcp.js +81 -0
- package/runtime/dist/src/executors/acp/preflight.js +14 -0
- package/runtime/dist/src/executors/acp/process-owner.js +112 -0
- package/runtime/dist/src/executors/acp/transport.js +760 -0
- package/runtime/dist/src/executors/acp/windows-launch.js +68 -0
- package/runtime/dist/src/executors/agent-controller.js +244 -0
- package/runtime/dist/src/executors/autoglm.js +376 -0
- package/runtime/dist/src/executors/browser-device.js +102 -0
- package/runtime/dist/src/executors/browser.js +61 -0
- package/runtime/dist/src/executors/camera.js +101 -0
- package/runtime/dist/src/executors/chromium-frame-source.js +173 -0
- package/runtime/dist/src/executors/codex/adapter-live.js +292 -0
- package/runtime/dist/src/executors/codex/adapter-project.js +1306 -0
- package/runtime/dist/src/executors/codex/app-server-schema.js +712 -0
- package/runtime/dist/src/executors/codex/app-server-transport.js +1798 -0
- package/runtime/dist/src/executors/codex/approval-protocol.js +439 -0
- package/runtime/dist/src/executors/codex/common.js +655 -0
- package/runtime/dist/src/executors/codex/contract.js +581 -0
- package/runtime/dist/src/executors/codex/controller.js +141 -0
- package/runtime/dist/src/executors/codex/credential-snapshot.js +195 -0
- package/runtime/dist/src/executors/codex/factory.js +463 -0
- package/runtime/dist/src/executors/codex/host-config.js +238 -0
- package/runtime/dist/src/executors/codex/host.js +17 -0
- package/runtime/dist/src/executors/codex/index.js +19 -0
- package/runtime/dist/src/executors/codex/launch-profile.js +37 -0
- package/runtime/dist/src/executors/codex/local-sessions.js +119 -0
- package/runtime/dist/src/executors/codex/managed-mcp.js +196 -0
- package/runtime/dist/src/executors/codex/process-owner.js +398 -0
- package/runtime/dist/src/executors/codex/production-host.js +887 -0
- package/runtime/dist/src/executors/codex/progress-instructions.js +10 -0
- package/runtime/dist/src/executors/codex/protocol.js +581 -0
- package/runtime/dist/src/executors/codex/safe-json.js +87 -0
- package/runtime/dist/src/executors/codex/session-title.js +61 -0
- package/runtime/dist/src/executors/codex/shared-home.js +29 -0
- package/runtime/dist/src/executors/codex/spawn-env.js +162 -0
- package/runtime/dist/src/executors/codex/turn-projection.js +435 -0
- package/runtime/dist/src/executors/codex/version.js +38 -0
- package/runtime/dist/src/executors/codex/windows-guardian.js +716 -0
- package/runtime/dist/src/executors/coding/intake-model.js +103 -0
- package/runtime/dist/src/executors/coding/intake.js +824 -0
- package/runtime/dist/src/executors/coding/json-completion.js +14 -0
- package/runtime/dist/src/executors/coding/target-resolution.js +61 -0
- package/runtime/dist/src/executors/coding/work-order.js +50 -0
- package/runtime/dist/src/executors/coding-executor.js +23 -0
- package/runtime/dist/src/executors/computer-backend.js +188 -0
- package/runtime/dist/src/executors/computer-device.js +187 -0
- package/runtime/dist/src/executors/computer-worker.js +125 -0
- package/runtime/dist/src/executors/computer.js +100 -0
- package/runtime/dist/src/executors/device-lock.js +127 -0
- package/runtime/dist/src/executors/frame-source.js +29 -0
- package/runtime/dist/src/executors/gui-evidence.js +30 -0
- package/runtime/dist/src/executors/gui-execution.js +237 -0
- package/runtime/dist/src/executors/gui-runner.js +88 -0
- package/runtime/dist/src/executors/host-executor-capability.js +12 -0
- package/runtime/dist/src/executors/index.js +13 -0
- package/runtime/dist/src/executors/mcp-client.js +198 -0
- package/runtime/dist/src/executors/mcp.js +210 -0
- package/runtime/dist/src/executors/mobile-android.js +180 -0
- package/runtime/dist/src/executors/mobile-ios.js +303 -0
- package/runtime/dist/src/executors/mobile.js +78 -0
- package/runtime/dist/src/executors/search-mcp.js +146 -0
- package/runtime/dist/src/executors/search.js +524 -0
- package/runtime/dist/src/executors/task-resources.js +38 -0
- package/runtime/dist/src/executors/vision/assess.js +296 -0
- package/runtime/dist/src/executors/vision/controller-core.js +373 -0
- package/runtime/dist/src/executors/vision/controller.js +164 -0
- package/runtime/dist/src/executors/vision/lifecycle.js +69 -0
- package/runtime/dist/src/executors/vision.js +5 -0
- package/runtime/dist/src/executors/watcher.js +718 -0
- package/runtime/dist/src/http/bounded-response.js +37 -0
- package/runtime/dist/src/index.js +56 -0
- package/runtime/dist/src/knowledge/assembly.js +60 -0
- package/runtime/dist/src/knowledge/documents.js +520 -0
- package/runtime/dist/src/knowledge/embeddings.js +201 -0
- package/runtime/dist/src/knowledge/mcp.js +218 -0
- package/runtime/dist/src/knowledge/parse-worker.js +122 -0
- package/runtime/dist/src/knowledge/references.js +51 -0
- package/runtime/dist/src/knowledge/service.js +526 -0
- package/runtime/dist/src/knowledge/store-client.js +185 -0
- package/runtime/dist/src/knowledge/store-worker.js +843 -0
- package/runtime/dist/src/knowledge/types.js +2 -0
- package/runtime/dist/src/mcp/http-server.js +177 -0
- package/runtime/dist/src/mem0/resource.js +35 -0
- package/runtime/dist/src/mem0/store-worker.js +330 -0
- package/runtime/dist/src/memory/blackboard-session.js +205 -0
- package/runtime/dist/src/memory/blackboard-store.js +194 -0
- package/runtime/dist/src/memory/blackboard-worker.js +369 -0
- package/runtime/dist/src/memory/entry.js +39 -0
- package/runtime/dist/src/memory/factory.js +20 -0
- package/runtime/dist/src/memory/personal-memory-inspection.js +18 -0
- package/runtime/dist/src/memory/personal-memory.js +10 -0
- package/runtime/dist/src/memory/prerecall.js +20 -0
- package/runtime/dist/src/memory/provenance.js +17 -0
- package/runtime/dist/src/memory/remote-personal-memory.js +194 -0
- package/runtime/dist/src/memory/retrieval.js +235 -0
- package/runtime/dist/src/memory/sensitivity.js +261 -0
- package/runtime/dist/src/memory-ledger/models.js +98 -0
- package/runtime/dist/src/memory-ledger/store-client.js +210 -0
- package/runtime/dist/src/memory-ledger/store-worker.js +130 -0
- package/runtime/dist/src/memory-ledger/store.js +597 -0
- package/runtime/dist/src/memory-substrate/candidates.js +16 -0
- package/runtime/dist/src/memory-substrate/consolidation.js +124 -0
- package/runtime/dist/src/memory-substrate/file-authority.js +111 -0
- package/runtime/dist/src/memory-substrate/life-operations.js +280 -0
- package/runtime/dist/src/memory-substrate/markdown-repository.js +610 -0
- package/runtime/dist/src/memory-substrate/migration.js +87 -0
- package/runtime/dist/src/memory-substrate/profile-source-input.js +9 -0
- package/runtime/dist/src/memory-substrate/purge.js +398 -0
- package/runtime/dist/src/memory-substrate/resolution.js +32 -0
- package/runtime/dist/src/memory-substrate/resource.js +867 -0
- package/runtime/dist/src/memory-substrate/retrieval.js +85 -0
- package/runtime/dist/src/memory-substrate/source-operations.js +306 -0
- package/runtime/dist/src/memory-substrate/source-processing.js +326 -0
- package/runtime/dist/src/memory-substrate/source-state.js +93 -0
- package/runtime/dist/src/memory-substrate/store.js +534 -0
- package/runtime/dist/src/memory-substrate/workspace-projections.js +106 -0
- package/runtime/dist/src/memory-substrate/workspace.js +21 -0
- package/runtime/dist/src/model/jev-client.js +25 -0
- package/runtime/dist/src/model/model-adapters.js +64 -0
- package/runtime/dist/src/model/model-gateway.js +399 -0
- package/runtime/dist/src/model/model-lane.js +34 -0
- package/runtime/dist/src/model/model-purpose.js +5 -0
- package/runtime/dist/src/model/personal-writer.js +361 -0
- package/runtime/dist/src/model/proactivity.js +72 -0
- package/runtime/dist/src/model/prompting.js +279 -0
- package/runtime/dist/src/model/task-verifier.js +217 -0
- package/runtime/dist/src/model/vision-capability.js +10 -0
- package/runtime/dist/src/news/feeds.js +114 -0
- package/runtime/dist/src/news/jev-ranking.js +25 -0
- package/runtime/dist/src/news/ranking.js +26 -0
- package/runtime/dist/src/news/service.js +262 -0
- package/runtime/dist/src/notifications/adapters/feishu.js +10 -0
- package/runtime/dist/src/notifications/adapters/nova.js +15 -0
- package/runtime/dist/src/notifications/content.js +15 -0
- package/runtime/dist/src/notifications/contracts.js +9 -0
- package/runtime/dist/src/notifications/feed.js +8 -0
- package/runtime/dist/src/notifications/service.js +412 -0
- package/runtime/dist/src/notifications/store.js +4 -0
- package/runtime/dist/src/personal-agent/app-context.js +113 -0
- package/runtime/dist/src/personal-agent/approval-scope.js +8 -0
- package/runtime/dist/src/personal-agent/coding-targets.js +82 -0
- package/runtime/dist/src/personal-agent/context-candidates.js +94 -0
- package/runtime/dist/src/personal-agent/contracts.js +32 -0
- package/runtime/dist/src/personal-agent/conversation-runtime.js +536 -0
- package/runtime/dist/src/personal-agent/conversations.js +187 -0
- package/runtime/dist/src/personal-agent/daily-brief.js +53 -0
- package/runtime/dist/src/personal-agent/host.js +2403 -0
- package/runtime/dist/src/personal-agent/intake-task.js +37 -0
- package/runtime/dist/src/personal-agent/life.js +260 -0
- package/runtime/dist/src/personal-agent/memory-overview.js +19 -0
- package/runtime/dist/src/personal-agent/profile-sources.js +34 -0
- package/runtime/dist/src/personal-agent/profile-warmup.js +244 -0
- package/runtime/dist/src/personal-agent/project-digests.js +242 -0
- package/runtime/dist/src/personal-agent/sampling.js +20 -0
- package/runtime/dist/src/personal-agent/schedule-agent.js +60 -0
- package/runtime/dist/src/personal-agent/schedule-api.js +61 -0
- package/runtime/dist/src/personal-agent/schedule-presets.js +13 -0
- package/runtime/dist/src/personal-agent/schedule-reference.js +3 -0
- package/runtime/dist/src/personal-agent/scheduled-response-scope.js +38 -0
- package/runtime/dist/src/personal-agent/scheduled-run-authority.js +44 -0
- package/runtime/dist/src/personal-agent/scheduled-tasks.js +280 -0
- package/runtime/dist/src/personal-agent/scheduled-time.js +99 -0
- package/runtime/dist/src/personal-agent/scheduled-tool-runtime.js +109 -0
- package/runtime/dist/src/personal-agent/source-priority.js +142 -0
- package/runtime/dist/src/personal-agent/source-processing-agent.js +158 -0
- package/runtime/dist/src/personal-agent/source-walk.js +25 -0
- package/runtime/dist/src/personal-agent/sources.js +1484 -0
- package/runtime/dist/src/personal-agent/store-lock-worker.js +17 -0
- package/runtime/dist/src/personal-agent/store.js +146 -0
- package/runtime/dist/src/personal-agent/task-loop.js +163 -0
- package/runtime/dist/src/personal-agent/tasks.js +920 -0
- package/runtime/dist/src/personal-agent/turn-deadline.js +55 -0
- package/runtime/dist/src/personal-agent/understanding.js +160 -0
- package/runtime/dist/src/personal-agent/workbench-context.js +239 -0
- package/runtime/dist/src/projects/confirmed-project-capability.js +60 -0
- package/runtime/dist/src/projects/host-paths.js +133 -0
- package/runtime/dist/src/projects/managed-workspace-maintenance.js +351 -0
- package/runtime/dist/src/projects/project-confirmation.js +486 -0
- package/runtime/dist/src/projects/project-native-resource.js +284 -0
- package/runtime/dist/src/projects/project-node-files.js +169 -0
- package/runtime/dist/src/projects/project-root-file.js +16 -0
- package/runtime/dist/src/projects/project-state.js +504 -0
- package/runtime/dist/src/projects/project-store-files.js +1271 -0
- package/runtime/dist/src/projects/project-store.js +1345 -0
- package/runtime/dist/src/realtime/aoq.js +141 -0
- package/runtime/dist/src/realtime/bridge.js +652 -0
- package/runtime/dist/src/realtime/cascaded/adapter.js +1669 -0
- package/runtime/dist/src/realtime/cascaded/ark-llm.js +219 -0
- package/runtime/dist/src/realtime/cascaded/chat-completions-llm.js +478 -0
- package/runtime/dist/src/realtime/cascaded/gemini-speech.js +232 -0
- package/runtime/dist/src/realtime/cascaded/llm.js +37 -0
- package/runtime/dist/src/realtime/cascaded/ports.js +2 -0
- package/runtime/dist/src/realtime/cascaded/provider.js +216 -0
- package/runtime/dist/src/realtime/cascaded/qwen-llm.js +7 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-asr.js +110 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-tts.js +137 -0
- package/runtime/dist/src/realtime/cascaded/transcribe.js +42 -0
- package/runtime/dist/src/realtime/coding-progress-narration.js +28 -0
- package/runtime/dist/src/realtime/confirmation-turn-isolation.js +586 -0
- package/runtime/dist/src/realtime/dictation.js +28 -0
- package/runtime/dist/src/realtime/evidence.js +276 -0
- package/runtime/dist/src/realtime/floor.js +43 -0
- package/runtime/dist/src/realtime/frontend-instructions.js +195 -0
- package/runtime/dist/src/realtime/gemini.js +517 -0
- package/runtime/dist/src/realtime/history.js +122 -0
- package/runtime/dist/src/realtime/host-delivery.js +1711 -0
- package/runtime/dist/src/realtime/integrated-wire-profile.js +128 -0
- package/runtime/dist/src/realtime/memory-board.js +105 -0
- package/runtime/dist/src/realtime/openai-wire-profile.js +46 -0
- package/runtime/dist/src/realtime/playback.js +242 -0
- package/runtime/dist/src/realtime/project-confirmation-flow.js +1428 -0
- package/runtime/dist/src/realtime/prompt-language.js +17 -0
- package/runtime/dist/src/realtime/protocol.js +428 -0
- package/runtime/dist/src/realtime/provider-projection.js +526 -0
- package/runtime/dist/src/realtime/provider-session.js +491 -0
- package/runtime/dist/src/realtime/qwen-transport.js +144 -0
- package/runtime/dist/src/realtime/qwen.js +1377 -0
- package/runtime/dist/src/realtime/recall.js +341 -0
- package/runtime/dist/src/realtime/service-ports.js +8 -0
- package/runtime/dist/src/realtime/service-state.js +259 -0
- package/runtime/dist/src/realtime/service.js +1726 -0
- package/runtime/dist/src/realtime/session-state.js +684 -0
- package/runtime/dist/src/realtime/session.js +1976 -0
- package/runtime/dist/src/realtime/speech-prep.js +63 -0
- package/runtime/dist/src/realtime/streaming-speech.js +120 -0
- package/runtime/dist/src/realtime/system-prompts.en.js +136 -0
- package/runtime/dist/src/realtime/telemetry.js +144 -0
- package/runtime/dist/src/realtime/tool-continuations.js +1510 -0
- package/runtime/dist/src/realtime/transport.js +8 -0
- package/runtime/dist/src/realtime/usage.js +55 -0
- package/runtime/dist/src/realtime/user-origin-binding.js +173 -0
- package/runtime/dist/src/realtime/volcengine/ark.js +428 -0
- package/runtime/dist/src/realtime/volcengine/asr.js +573 -0
- package/runtime/dist/src/realtime/volcengine/audio.js +39 -0
- package/runtime/dist/src/realtime/volcengine/endpointing-capability.js +834 -0
- package/runtime/dist/src/realtime/volcengine/livekit-endpointing.js +714 -0
- package/runtime/dist/src/realtime/volcengine/local-eot-executor.js +33 -0
- package/runtime/dist/src/realtime/volcengine/protocol.js +247 -0
- package/runtime/dist/src/realtime/volcengine/silence-endpointing.js +237 -0
- package/runtime/dist/src/realtime/volcengine/tts.js +444 -0
- package/runtime/dist/src/realtime/volcengine/websocket.js +293 -0
- package/runtime/dist/src/server/aoq-chat-server.js +497 -0
- package/runtime/dist/src/server/client-pairing.js +192 -0
- package/runtime/dist/src/server/client-protocol.js +87 -0
- package/runtime/dist/src/server/client-server.js +312 -0
- package/runtime/dist/src/server/server-config.js +72 -0
- package/runtime/dist/src/server-entry.js +135 -0
- package/runtime/dist/src/storage/bounded-json.js +63 -0
- package/runtime/dist/src/storage/native-file-lock.js +8 -0
- package/runtime/dist/src/storage/native-resource-snapshot.js +41 -0
- package/runtime/dist/src/storage/private-database.js +120 -0
- package/runtime/dist/src/text/canonical-json.js +60 -0
- package/runtime/dist/src/text/python-number.js +18 -0
- package/runtime/dist/src/text/python-text.js +92 -0
- package/runtime/dist/src/text/unicode-casefold.js +29 -0
- package/runtime/dist/src/text/unicode-normalize.js +138 -0
- package/runtime/dist/src/text/unicode-tables.js +210 -0
- package/runtime/dist/src/understanding/candidates.js +77 -0
- package/runtime/dist/src/understanding/fixture.js +7 -0
- package/runtime/dist/src/understanding/jev.js +20 -0
- package/runtime/dist/src/understanding/pipeline.js +38 -0
- package/runtime/dist/src/voicemem/store-client.js +476 -0
- package/runtime/dist/src/voicemem/store-worker.js +380 -0
- package/runtime/dist/src/voicemem/versioned-memory.js +170 -0
- package/runtime/scripts/pair-device.mjs +109 -0
- package/src/command.mjs +48 -0
|
@@ -0,0 +1,684 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded ledgers and per-response bookkeeping for the realtime session.
|
|
3
|
+
*
|
|
4
|
+
* Ported from the state half of `src/nova_personal_agent/realtime/session.py`. This layer is
|
|
5
|
+
* deliberately pure: no provider, no transport, no clock. It owns the questions the
|
|
6
|
+
* session's event reducer keeps asking -- has this user turn already been counted, has
|
|
7
|
+
* this response spoken, was this host event already answered -- and every answer is
|
|
8
|
+
* bounded so a long session cannot grow without limit.
|
|
9
|
+
*
|
|
10
|
+
* Provider-allocated identities are keyed with the session epoch: a reconnect starts a new
|
|
11
|
+
* epoch, and an item id from the old provider session must never satisfy a dedup check in the
|
|
12
|
+
* new one. Host-allocated event ids are deliberately *not* epoch-scoped -- a host event that
|
|
13
|
+
* was already answered stays answered across a reconnect, because the host owns that identity
|
|
14
|
+
* and re-answering it would repeat the utterance to the user.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { PROGRESS_SUMMARY_LIMIT } from '../core/events.js';
|
|
18
|
+
export const MAX_TRACKED_USER_TRANSCRIPTS = 4_096;
|
|
19
|
+
export const MAX_CAPTION_CHARS = 160;
|
|
20
|
+
export const MAX_TRACKED_HOST_EVENTS = 500;
|
|
21
|
+
export const MAX_PENDING_HOST_EVENTS = 532;
|
|
22
|
+
export const MAX_TRACKED_PROVIDER_TURNS = 500;
|
|
23
|
+
export const MAX_PREMAP_AUDIO_BYTES = 64 * 1_024;
|
|
24
|
+
export const MAX_CONTINUATION_TASK_SUMMARY = 240;
|
|
25
|
+
export const ACTIVE_EXECUTOR_ELAPSED_BUCKET_S = 15;
|
|
26
|
+
export const ACTIVE_EXECUTOR_ACTIVITY_BUCKET = 8;
|
|
27
|
+
export const delegateStateSchema = z.enum(['running', 'completed', 'refused', 'failed', 'unknown']);
|
|
28
|
+
export const providerTurnPhaseSchema = z.enum([
|
|
29
|
+
'active',
|
|
30
|
+
'yielded',
|
|
31
|
+
'cancel_requested',
|
|
32
|
+
'completed',
|
|
33
|
+
'cancelled',
|
|
34
|
+
'failed',
|
|
35
|
+
]);
|
|
36
|
+
/**
|
|
37
|
+
* Canonical data boundary shared by provider context rendering and change detection.
|
|
38
|
+
*
|
|
39
|
+
* Host-owned state is intentionally nested away from executor-authored progress text. The latter
|
|
40
|
+
* remains useful evidence for a concise status answer, but can never acquire instruction authority.
|
|
41
|
+
*/
|
|
42
|
+
export function activeExecutorContextRecords(delegates, agentNameForChannel = () => null) {
|
|
43
|
+
return delegates.map(([delegateId, record]) => Object.freeze({
|
|
44
|
+
delegate_id: delegateId,
|
|
45
|
+
host_state: Object.freeze({
|
|
46
|
+
channel: agentNameForChannel(record.channel) ?? record.channel,
|
|
47
|
+
state: record.state,
|
|
48
|
+
elapsed_s: Math.floor(record.elapsed / ACTIVE_EXECUTOR_ELAPSED_BUCKET_S)
|
|
49
|
+
* ACTIVE_EXECUTOR_ELAPSED_BUCKET_S,
|
|
50
|
+
internal_activity: Math.floor(record.internal_activity / ACTIVE_EXECUTOR_ACTIVITY_BUCKET)
|
|
51
|
+
* ACTIVE_EXECUTOR_ACTIVITY_BUCKET,
|
|
52
|
+
...(record.project === undefined ? {} : { project: record.project }),
|
|
53
|
+
...(record.title === undefined ? {} : { title: record.title }),
|
|
54
|
+
}),
|
|
55
|
+
progress_summary: Object.freeze({
|
|
56
|
+
executable: false,
|
|
57
|
+
text: record.progress_summary === null || record.progress_summary === ''
|
|
58
|
+
? null
|
|
59
|
+
: [...record.progress_summary].slice(0, 120).join(''),
|
|
60
|
+
}),
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
/** Exact bounded structure visible to the provider and used for publication deduplication. */
|
|
64
|
+
export function activeExecutorContextData(delegates, agentNameForChannel = () => null) {
|
|
65
|
+
const records = activeExecutorContextRecords(delegates, agentNameForChannel);
|
|
66
|
+
return Object.freeze({
|
|
67
|
+
delegates: Object.freeze(records.slice(0, 3)),
|
|
68
|
+
omitted_count: Math.max(0, records.length - 3),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* An epoch-scoped, insertion-ordered set with a hard size bound.
|
|
73
|
+
*
|
|
74
|
+
* Eviction is oldest-first, which is what makes a long session safe: a provider that never
|
|
75
|
+
* repeats an item id would otherwise grow this without limit. Keys embed the epoch so a
|
|
76
|
+
* reconnect cannot inherit the previous session's answers.
|
|
77
|
+
*/
|
|
78
|
+
export class EpochLedger {
|
|
79
|
+
#keys = [];
|
|
80
|
+
#seen = new Set();
|
|
81
|
+
#limit;
|
|
82
|
+
constructor(limit) {
|
|
83
|
+
if (!Number.isInteger(limit) || limit < 1) {
|
|
84
|
+
throw new RangeError(`ledger limit must be a positive integer: ${limit}`);
|
|
85
|
+
}
|
|
86
|
+
this.#limit = limit;
|
|
87
|
+
}
|
|
88
|
+
get size() {
|
|
89
|
+
return this.#keys.length;
|
|
90
|
+
}
|
|
91
|
+
has(epoch, id) {
|
|
92
|
+
return this.#seen.has(key(epoch, id));
|
|
93
|
+
}
|
|
94
|
+
/** Record the id; false means it was already present and nothing changed. */
|
|
95
|
+
add(epoch, id) {
|
|
96
|
+
const entry = key(epoch, id);
|
|
97
|
+
if (this.#seen.has(entry))
|
|
98
|
+
return false;
|
|
99
|
+
this.#seen.add(entry);
|
|
100
|
+
this.#keys.push(entry);
|
|
101
|
+
if (this.#keys.length > this.#limit) {
|
|
102
|
+
const evicted = this.#keys.shift();
|
|
103
|
+
if (evicted !== undefined)
|
|
104
|
+
this.#seen.delete(evicted);
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Rename an already-recorded id in place, keeping its position.
|
|
110
|
+
*
|
|
111
|
+
* The provider can reveal a turn's real item id after the turn was first observed under a
|
|
112
|
+
* provisional one. Re-adding would move it to the end of the eviction order and could
|
|
113
|
+
* evict an older turn that is still live, so the slot is rewritten instead.
|
|
114
|
+
*/
|
|
115
|
+
replace(epoch, previousId, id) {
|
|
116
|
+
const previous = key(epoch, previousId);
|
|
117
|
+
const entry = key(epoch, id);
|
|
118
|
+
if (!this.#seen.has(previous) || this.#seen.has(entry))
|
|
119
|
+
return false;
|
|
120
|
+
const index = this.#keys.indexOf(previous);
|
|
121
|
+
if (index === -1)
|
|
122
|
+
return false;
|
|
123
|
+
this.#keys[index] = entry;
|
|
124
|
+
this.#seen.delete(previous);
|
|
125
|
+
this.#seen.add(entry);
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
clear() {
|
|
129
|
+
this.#keys.length = 0;
|
|
130
|
+
this.#seen.clear();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function key(epoch, id) {
|
|
134
|
+
// The separator cannot occur in a decimal epoch, so the pair round-trips unambiguously.
|
|
135
|
+
return `${epoch}:${id}`;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Tracks user turns, transcripts, captions, provider turns, and delegates for one session.
|
|
139
|
+
*
|
|
140
|
+
* Split out from the session so the bookkeeping can be tested without a provider, and so
|
|
141
|
+
* the session's own reducer reads as decisions rather than as bookkeeping.
|
|
142
|
+
*/
|
|
143
|
+
export class RealtimeSessionState {
|
|
144
|
+
#transcripts = new EpochLedger(MAX_TRACKED_USER_TRANSCRIPTS);
|
|
145
|
+
#turns = new EpochLedger(MAX_TRACKED_USER_TRANSCRIPTS);
|
|
146
|
+
// A Map is insertion-ordered, and `delete` + `set` moves a key to the end, so these two
|
|
147
|
+
// reproduce OrderedDict with `move_to_end` on every touch. That LRU-by-touch order is
|
|
148
|
+
// load-bearing: eviction is oldest-touched-first, not oldest-inserted-first.
|
|
149
|
+
#providerTurns = new Map();
|
|
150
|
+
#delegates = new Map();
|
|
151
|
+
#spokenEventIds = [];
|
|
152
|
+
#interruptedEventIds = [];
|
|
153
|
+
#respondedEventIds = new Map();
|
|
154
|
+
#injectedEventEpochs = new Map();
|
|
155
|
+
#injectedProviderItems = new Map();
|
|
156
|
+
#retainedSuggestionInjectionIds = new Set();
|
|
157
|
+
#pendingResponses = [];
|
|
158
|
+
#suppressedResponseIds = new Set();
|
|
159
|
+
#pendingAudio = [];
|
|
160
|
+
#pendingAudioBytes = 0;
|
|
161
|
+
#premapResponseId = null;
|
|
162
|
+
#epoch = 0;
|
|
163
|
+
#userInputRevision = 0;
|
|
164
|
+
#snapshotVersion = 0;
|
|
165
|
+
#userCaptionItem = null;
|
|
166
|
+
#userCaptionText = '';
|
|
167
|
+
#assistantCaptionResponse = null;
|
|
168
|
+
#assistantCaptionText = '';
|
|
169
|
+
get sessionEpoch() {
|
|
170
|
+
return this.#epoch;
|
|
171
|
+
}
|
|
172
|
+
get userInputRevision() {
|
|
173
|
+
return this.#userInputRevision;
|
|
174
|
+
}
|
|
175
|
+
get snapshotVersion() {
|
|
176
|
+
return this.#snapshotVersion;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Begin a new provider session.
|
|
180
|
+
*
|
|
181
|
+
* Epoch-scoped answers start over because their keys embed the epoch, not because anything is
|
|
182
|
+
* cleared here. Captions are deliberately left alone: clearing them belongs to the layer above,
|
|
183
|
+
* which does it around a reconnect, and doing it here too would hide a missing reset there.
|
|
184
|
+
*/
|
|
185
|
+
beginEpoch(epoch) {
|
|
186
|
+
if (!Number.isInteger(epoch) || epoch <= this.#epoch) {
|
|
187
|
+
throw new RangeError(`session epoch must increase: ${this.#epoch} -> ${epoch}`);
|
|
188
|
+
}
|
|
189
|
+
this.#epoch = epoch;
|
|
190
|
+
this.advanceSnapshot();
|
|
191
|
+
}
|
|
192
|
+
/** Count a user transcript terminal once; false means it was a duplicate. */
|
|
193
|
+
acceptUserTranscriptTerminal(itemId) {
|
|
194
|
+
return this.#transcripts.add(this.#epoch, itemId);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Count a user turn once, advancing the input revision.
|
|
198
|
+
*
|
|
199
|
+
* The revision is what later lets a response be recognised as answering a world that no
|
|
200
|
+
* longer exists, so it must advance exactly once per genuinely new turn.
|
|
201
|
+
*/
|
|
202
|
+
acceptUserTurn(itemId) {
|
|
203
|
+
if (!this.#turns.add(this.#epoch, itemId))
|
|
204
|
+
return false;
|
|
205
|
+
this.#userInputRevision += 1;
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
/** Rename a counted turn without advancing the revision or reordering eviction. */
|
|
209
|
+
replaceUserTurnIdentity(previousItemId, itemId) {
|
|
210
|
+
return this.#turns.replace(this.#epoch, previousItemId, itemId);
|
|
211
|
+
}
|
|
212
|
+
hasUserTurn(itemId) {
|
|
213
|
+
return this.#turns.has(this.#epoch, itemId);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Record that a host event has been answered, so it is never answered twice.
|
|
217
|
+
*
|
|
218
|
+
* Re-recording an already-answered event moves it to the end of the eviction order without
|
|
219
|
+
* changing the answer, which is how a repeated response request keeps a still-relevant event
|
|
220
|
+
* from ageing out. `false` reports that it was already present.
|
|
221
|
+
*
|
|
222
|
+
* The bound is not enforced here: pruning is a separate step the session drives once per
|
|
223
|
+
* response request, so a burst of records cannot evict an event the same burst still needs.
|
|
224
|
+
*/
|
|
225
|
+
markEventResponded(eventId) {
|
|
226
|
+
const known = this.#respondedEventIds.delete(eventId);
|
|
227
|
+
this.#respondedEventIds.set(eventId, null);
|
|
228
|
+
return !known;
|
|
229
|
+
}
|
|
230
|
+
hostEventIsDeduplicated(eventId) {
|
|
231
|
+
return this.#respondedEventIds.has(eventId);
|
|
232
|
+
}
|
|
233
|
+
/** Every answered host event id, oldest-touched first. */
|
|
234
|
+
get respondedEventIds() {
|
|
235
|
+
return [...this.#respondedEventIds.keys()];
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Withdraw an event's answer so it can be answered again.
|
|
239
|
+
*
|
|
240
|
+
* A suggestion whose response was interrupted never reached the user, so its authority goes
|
|
241
|
+
* back and the event becomes eligible again.
|
|
242
|
+
*/
|
|
243
|
+
releaseRespondedEvent(eventId) {
|
|
244
|
+
return this.#respondedEventIds.delete(eventId);
|
|
245
|
+
}
|
|
246
|
+
/** Drop oldest-touched answered events down to the bound. */
|
|
247
|
+
pruneRespondedEvents() {
|
|
248
|
+
while (this.#respondedEventIds.size > MAX_TRACKED_HOST_EVENTS) {
|
|
249
|
+
const oldest = this.#respondedEventIds.keys().next();
|
|
250
|
+
if (oldest.done === true)
|
|
251
|
+
break;
|
|
252
|
+
this.#respondedEventIds.delete(oldest.value);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Open a provider turn, evicting the oldest-touched when the bound is reached.
|
|
257
|
+
*
|
|
258
|
+
* A response id that is already recorded returns its existing turn **unchanged**. Its phase,
|
|
259
|
+
* fence flag, and input revision are decisions already taken; rebuilding the entry would
|
|
260
|
+
* revive a `cancel_requested` or terminal turn as `active` and would re-date a stale turn
|
|
261
|
+
* against the current input revision. Only the eviction position moves.
|
|
262
|
+
*/
|
|
263
|
+
openProviderTurn(responseId, userInputRevision = this.#userInputRevision) {
|
|
264
|
+
const composite = key(this.#epoch, responseId);
|
|
265
|
+
const known = this.#providerTurns.get(composite);
|
|
266
|
+
const entry = known ?? {
|
|
267
|
+
phase: 'active',
|
|
268
|
+
user_input_revision: userInputRevision,
|
|
269
|
+
locally_fenced: false,
|
|
270
|
+
defer_playback_fence: false,
|
|
271
|
+
};
|
|
272
|
+
// Touch the key so eviction order is by last use, matching `OrderedDict.move_to_end`.
|
|
273
|
+
this.#providerTurns.delete(composite);
|
|
274
|
+
this.#providerTurns.set(composite, entry);
|
|
275
|
+
while (this.#providerTurns.size > MAX_TRACKED_PROVIDER_TURNS) {
|
|
276
|
+
const oldest = this.#providerTurns.keys().next();
|
|
277
|
+
if (oldest.done === true)
|
|
278
|
+
break;
|
|
279
|
+
this.#providerTurns.delete(oldest.value);
|
|
280
|
+
}
|
|
281
|
+
return entry;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* One provider turn. `sessionEpoch` names an older session, which only a handoff needs.
|
|
285
|
+
*
|
|
286
|
+
* A reconnect has to finish accounting for the turns it is abandoning, and by then the epoch has
|
|
287
|
+
* already advanced, so those turns can no longer be reached by the current one.
|
|
288
|
+
*/
|
|
289
|
+
providerTurn(responseId, sessionEpoch) {
|
|
290
|
+
if (responseId === null)
|
|
291
|
+
return undefined;
|
|
292
|
+
return this.#providerTurns.get(key(sessionEpoch ?? this.#epoch, responseId));
|
|
293
|
+
}
|
|
294
|
+
providerTurnPhase(responseId) {
|
|
295
|
+
return this.providerTurn(responseId)?.phase;
|
|
296
|
+
}
|
|
297
|
+
providerTurnUserInputRevision(responseId) {
|
|
298
|
+
return this.providerTurn(responseId)?.user_input_revision;
|
|
299
|
+
}
|
|
300
|
+
providerTurnWasFenced(responseId) {
|
|
301
|
+
return this.providerTurn(responseId)?.locally_fenced ?? false;
|
|
302
|
+
}
|
|
303
|
+
/** Whether the world this response answers has been superseded by newer user input. */
|
|
304
|
+
providerTurnIsStale(responseId) {
|
|
305
|
+
const revision = this.providerTurnUserInputRevision(responseId);
|
|
306
|
+
return revision !== undefined && revision < this.#userInputRevision;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Append a spoken event id once.
|
|
310
|
+
*
|
|
311
|
+
* This deliberately does not advance the snapshot version. Callers append a whole response's
|
|
312
|
+
* event ids in a loop and then publish once, so the version counts published changes rather
|
|
313
|
+
* than individual appends.
|
|
314
|
+
*/
|
|
315
|
+
markEventSpoken(eventId) {
|
|
316
|
+
if (!this.#spokenEventIds.includes(eventId))
|
|
317
|
+
this.#spokenEventIds.push(eventId);
|
|
318
|
+
}
|
|
319
|
+
/** Append an interrupted event id once. Does not advance the snapshot; see markEventSpoken. */
|
|
320
|
+
markEventInterrupted(eventId) {
|
|
321
|
+
if (!this.#interruptedEventIds.includes(eventId))
|
|
322
|
+
this.#interruptedEventIds.push(eventId);
|
|
323
|
+
}
|
|
324
|
+
eventWasSpoken(eventId) {
|
|
325
|
+
return this.#spokenEventIds.includes(eventId);
|
|
326
|
+
}
|
|
327
|
+
/** Replace a caption for one role, bounded to what a display can show. */
|
|
328
|
+
setCaption(frame) {
|
|
329
|
+
const text = truncateCaption(frame.text);
|
|
330
|
+
if (frame.role === 'user')
|
|
331
|
+
this.#userCaptionText = text;
|
|
332
|
+
else
|
|
333
|
+
this.#assistantCaptionText = text;
|
|
334
|
+
return { role: frame.role, text, final: frame.final };
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Extend a caption with one delta, keeping its newest end within the bound.
|
|
338
|
+
*
|
|
339
|
+
* A delta is a continuation, not a replacement, so the accumulated text is what gets bounded --
|
|
340
|
+
* bounding each delta on its own would keep every one of them whole and let the total run past
|
|
341
|
+
* the limit.
|
|
342
|
+
*/
|
|
343
|
+
appendCaption(frame) {
|
|
344
|
+
const previous = frame.role === 'user' ? this.#userCaptionText : this.#assistantCaptionText;
|
|
345
|
+
const text = truncateCaption(previous + frame.text);
|
|
346
|
+
if (frame.role === 'user')
|
|
347
|
+
this.#userCaptionText = text;
|
|
348
|
+
else
|
|
349
|
+
this.#assistantCaptionText = text;
|
|
350
|
+
return { role: frame.role, text, final: frame.final };
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Forget which item the user caption was accumulating.
|
|
354
|
+
*
|
|
355
|
+
* A final ends the accumulation: the next delta starts a new one even under the same item id,
|
|
356
|
+
* because what follows a final is a revision rather than a continuation.
|
|
357
|
+
*/
|
|
358
|
+
resetUserCaptionTarget() {
|
|
359
|
+
this.#userCaptionItem = null;
|
|
360
|
+
this.#userCaptionText = '';
|
|
361
|
+
}
|
|
362
|
+
resetAssistantCaptionTarget() {
|
|
363
|
+
this.#assistantCaptionResponse = null;
|
|
364
|
+
this.#assistantCaptionText = '';
|
|
365
|
+
}
|
|
366
|
+
trackUserCaption(itemId) {
|
|
367
|
+
if (this.#userCaptionItem === itemId)
|
|
368
|
+
return false;
|
|
369
|
+
this.#userCaptionItem = itemId;
|
|
370
|
+
this.#userCaptionText = '';
|
|
371
|
+
return true;
|
|
372
|
+
}
|
|
373
|
+
trackAssistantCaption(responseId) {
|
|
374
|
+
if (this.#assistantCaptionResponse === responseId)
|
|
375
|
+
return false;
|
|
376
|
+
this.#assistantCaptionResponse = responseId;
|
|
377
|
+
this.#assistantCaptionText = '';
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
get userCaption() {
|
|
381
|
+
return this.#userCaptionText;
|
|
382
|
+
}
|
|
383
|
+
get assistantCaption() {
|
|
384
|
+
return this.#assistantCaptionText;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Drop the assistant caption if it belongs to this response.
|
|
388
|
+
*
|
|
389
|
+
* A delivered response loses caption authority: further speculative text under its id would be
|
|
390
|
+
* attributed to a turn the user has already heard the end of.
|
|
391
|
+
*/
|
|
392
|
+
clearAssistantCaptionFor(responseId) {
|
|
393
|
+
if (this.#assistantCaptionResponse !== responseId)
|
|
394
|
+
return;
|
|
395
|
+
this.#assistantCaptionResponse = null;
|
|
396
|
+
this.#assistantCaptionText = '';
|
|
397
|
+
}
|
|
398
|
+
clearCaptions() {
|
|
399
|
+
this.#userCaptionItem = null;
|
|
400
|
+
this.#userCaptionText = '';
|
|
401
|
+
this.#assistantCaptionResponse = null;
|
|
402
|
+
this.#assistantCaptionText = '';
|
|
403
|
+
}
|
|
404
|
+
/** Forget every conversation-owned projection while keeping the provider epoch monotonic. */
|
|
405
|
+
resetConversation() {
|
|
406
|
+
this.#transcripts.clear();
|
|
407
|
+
this.#turns.clear();
|
|
408
|
+
this.#providerTurns.clear();
|
|
409
|
+
this.#delegates.clear();
|
|
410
|
+
this.#spokenEventIds.length = 0;
|
|
411
|
+
this.#interruptedEventIds.length = 0;
|
|
412
|
+
this.#respondedEventIds.clear();
|
|
413
|
+
this.#injectedEventEpochs.clear();
|
|
414
|
+
this.#injectedProviderItems.clear();
|
|
415
|
+
this.#retainedSuggestionInjectionIds.clear();
|
|
416
|
+
this.#pendingResponses.length = 0;
|
|
417
|
+
this.#suppressedResponseIds.clear();
|
|
418
|
+
this.#pendingAudio = [];
|
|
419
|
+
this.#pendingAudioBytes = 0;
|
|
420
|
+
this.#premapResponseId = null;
|
|
421
|
+
this.#userInputRevision = 0;
|
|
422
|
+
this.clearCaptions();
|
|
423
|
+
this.advanceSnapshot();
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Record or update one delegate.
|
|
427
|
+
*
|
|
428
|
+
* An omitted progress field preserves the previous slot rather than clearing it, which is
|
|
429
|
+
* what lets a handoff update state without erasing the progress a caller never mentioned.
|
|
430
|
+
* An explicitly null progress summary does clear it.
|
|
431
|
+
*/
|
|
432
|
+
registerDelegate(delegateId, update) {
|
|
433
|
+
if (delegateId === '' || update.summary === '') {
|
|
434
|
+
throw new TypeError('delegate_id and summary are required');
|
|
435
|
+
}
|
|
436
|
+
const previous = this.#delegates.get(delegateId);
|
|
437
|
+
const progress = 'progress_summary' in update
|
|
438
|
+
? boundedProgress(update.progress_summary ?? null)
|
|
439
|
+
: previous?.progress_summary ?? null;
|
|
440
|
+
const project = update.project ?? previous?.project;
|
|
441
|
+
const title = update.title ?? previous?.title;
|
|
442
|
+
this.#delegates.set(delegateId, {
|
|
443
|
+
summary: update.summary,
|
|
444
|
+
state: delegateStateSchema.parse(update.state),
|
|
445
|
+
channel: update.channel,
|
|
446
|
+
progress_summary: progress,
|
|
447
|
+
internal_activity: update.internal_activity ?? previous?.internal_activity ?? 0,
|
|
448
|
+
elapsed: update.elapsed ?? previous?.elapsed ?? 0,
|
|
449
|
+
...(project === undefined ? {} : { project }),
|
|
450
|
+
...(title === undefined ? {} : { title }),
|
|
451
|
+
});
|
|
452
|
+
this.advanceSnapshot();
|
|
453
|
+
}
|
|
454
|
+
delegateRecord(delegateId) {
|
|
455
|
+
const record = this.#delegates.get(delegateId);
|
|
456
|
+
return record === undefined ? undefined : { ...record };
|
|
457
|
+
}
|
|
458
|
+
delegateState(delegateId) {
|
|
459
|
+
return this.#delegates.get(delegateId)?.state;
|
|
460
|
+
}
|
|
461
|
+
/** Only running delegates are visible; the rest are history. */
|
|
462
|
+
snapshot() {
|
|
463
|
+
return {
|
|
464
|
+
version: this.#snapshotVersion,
|
|
465
|
+
active_delegates: [...this.#delegates]
|
|
466
|
+
.filter(([, record]) => record.state === 'running')
|
|
467
|
+
.map(([delegateId, record]) => [delegateId, { ...record }]),
|
|
468
|
+
spoken_event_ids: [...this.#spokenEventIds],
|
|
469
|
+
interrupted_event_ids: [...this.#interruptedEventIds],
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
// -------------------------------------------------------------------------
|
|
473
|
+
// Injected host items
|
|
474
|
+
// -------------------------------------------------------------------------
|
|
475
|
+
/**
|
|
476
|
+
* Record that a host item reached the provider in the current epoch.
|
|
477
|
+
*
|
|
478
|
+
* A continuation may only be requested for an item the provider has confirmed, and the epoch it
|
|
479
|
+
* was confirmed in decides whether a later request still refers to the same conversation. The
|
|
480
|
+
* bound here (532) is deliberately above the pruning target (500): recording never drops an item
|
|
481
|
+
* the caller may still be about to reference, and pruning reclaims the slack once entries become
|
|
482
|
+
* prunable.
|
|
483
|
+
*/
|
|
484
|
+
recordInjectedEvent(eventId, providerItemId) {
|
|
485
|
+
this.#injectedEventEpochs.delete(eventId);
|
|
486
|
+
this.#injectedEventEpochs.set(eventId, this.#epoch);
|
|
487
|
+
this.#injectedProviderItems.delete(eventId);
|
|
488
|
+
if (providerItemId !== undefined) {
|
|
489
|
+
this.#injectedProviderItems.set(eventId, {
|
|
490
|
+
epoch: this.#epoch,
|
|
491
|
+
provider_item_id: providerItemId,
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
while (this.#injectedEventEpochs.size > MAX_PENDING_HOST_EVENTS) {
|
|
495
|
+
const oldest = this.#injectedEventEpochs.keys().next();
|
|
496
|
+
if (oldest.done === true)
|
|
497
|
+
break;
|
|
498
|
+
this.#injectedEventEpochs.delete(oldest.value);
|
|
499
|
+
this.#injectedProviderItems.delete(oldest.value);
|
|
500
|
+
this.#retainedSuggestionInjectionIds.delete(oldest.value);
|
|
501
|
+
}
|
|
502
|
+
this.advanceSnapshot();
|
|
503
|
+
}
|
|
504
|
+
injectedEventEpoch(eventId) {
|
|
505
|
+
return this.#injectedEventEpochs.get(eventId);
|
|
506
|
+
}
|
|
507
|
+
injectedProviderItem(eventId) {
|
|
508
|
+
const identity = this.#injectedProviderItems.get(eventId);
|
|
509
|
+
return identity === undefined ? undefined : { ...identity };
|
|
510
|
+
}
|
|
511
|
+
releaseInjectedEvent(eventId) {
|
|
512
|
+
this.#injectedProviderItems.delete(eventId);
|
|
513
|
+
return this.#injectedEventEpochs.delete(eventId);
|
|
514
|
+
}
|
|
515
|
+
releaseInjectedProviderItem(eventId, expected) {
|
|
516
|
+
const current = this.#injectedProviderItems.get(eventId);
|
|
517
|
+
if (current?.epoch !== expected.epoch
|
|
518
|
+
|| current.provider_item_id !== expected.provider_item_id)
|
|
519
|
+
return false;
|
|
520
|
+
this.#injectedProviderItems.delete(eventId);
|
|
521
|
+
return this.#injectedEventEpochs.delete(eventId);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Retain a suggestion's injection so a fenced response can be re-offered, then revoke the lot.
|
|
525
|
+
*
|
|
526
|
+
* A suggestion whose response was interrupted keeps its injection for the rest of the epoch, so
|
|
527
|
+
* the same suggestion is not injected twice; the retention is dropped when the epoch ends.
|
|
528
|
+
*/
|
|
529
|
+
retainSuggestionInjection(eventId) {
|
|
530
|
+
this.#retainedSuggestionInjectionIds.add(eventId);
|
|
531
|
+
}
|
|
532
|
+
/** Drop one retention: the suggestion was heard, so there is nothing left to re-offer. */
|
|
533
|
+
releaseRetainedSuggestionInjection(eventId) {
|
|
534
|
+
return this.#retainedSuggestionInjectionIds.delete(eventId);
|
|
535
|
+
}
|
|
536
|
+
revokeRetainedSuggestionInjections() {
|
|
537
|
+
for (const eventId of this.#retainedSuggestionInjectionIds) {
|
|
538
|
+
this.#injectedEventEpochs.delete(eventId);
|
|
539
|
+
this.#injectedProviderItems.delete(eventId);
|
|
540
|
+
}
|
|
541
|
+
this.#retainedSuggestionInjectionIds.clear();
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Reclaim both host-event ledgers down to their shared target.
|
|
545
|
+
*
|
|
546
|
+
* An injected item is only prunable once its response has been answered or has completed, so the
|
|
547
|
+
* scan skips entries a caller may still reference and stops as soon as the target is met.
|
|
548
|
+
*/
|
|
549
|
+
pruneHostEventLedgers(completedEventIds) {
|
|
550
|
+
const prunable = new Set([...completedEventIds, ...this.#respondedEventIds.keys()]);
|
|
551
|
+
for (const eventId of [...this.#injectedEventEpochs.keys()]) {
|
|
552
|
+
if (this.#injectedEventEpochs.size <= MAX_TRACKED_HOST_EVENTS)
|
|
553
|
+
break;
|
|
554
|
+
if (!prunable.has(eventId))
|
|
555
|
+
continue;
|
|
556
|
+
this.#injectedEventEpochs.delete(eventId);
|
|
557
|
+
this.#injectedProviderItems.delete(eventId);
|
|
558
|
+
this.#retainedSuggestionInjectionIds.delete(eventId);
|
|
559
|
+
}
|
|
560
|
+
this.pruneRespondedEvents();
|
|
561
|
+
this.advanceSnapshot();
|
|
562
|
+
}
|
|
563
|
+
// -------------------------------------------------------------------------
|
|
564
|
+
// Pending responses
|
|
565
|
+
// -------------------------------------------------------------------------
|
|
566
|
+
get pendingResponseCount() {
|
|
567
|
+
return this.#pendingResponses.length;
|
|
568
|
+
}
|
|
569
|
+
/** The response the provider is expected to start next, if any. */
|
|
570
|
+
get headPendingResponse() {
|
|
571
|
+
return this.#pendingResponses[0];
|
|
572
|
+
}
|
|
573
|
+
queuePendingResponse(pending) {
|
|
574
|
+
this.#pendingResponses.push(pending);
|
|
575
|
+
}
|
|
576
|
+
/** Roll back only this command's unconsumed admission; a later request is a different owner. */
|
|
577
|
+
discardPendingResponse(pending) {
|
|
578
|
+
const index = this.#pendingResponses.indexOf(pending);
|
|
579
|
+
if (index >= 0)
|
|
580
|
+
this.#pendingResponses.splice(index, 1);
|
|
581
|
+
}
|
|
582
|
+
/** Remove the head, whether it started or was given up on. */
|
|
583
|
+
popPendingResponse() {
|
|
584
|
+
return this.#pendingResponses.shift();
|
|
585
|
+
}
|
|
586
|
+
/** Exact provider evidence must not consume another host request's slot. */
|
|
587
|
+
takePendingResponse(hostItemId) {
|
|
588
|
+
const index = this.#pendingResponses.findIndex(pending => pending.provider_intent.item.host_item_id === hostItemId);
|
|
589
|
+
return index < 0 ? undefined : this.#pendingResponses.splice(index, 1)[0];
|
|
590
|
+
}
|
|
591
|
+
/** Every queued response, oldest first, for a caller that has to give all of them up. */
|
|
592
|
+
get pendingResponses() {
|
|
593
|
+
return [...this.#pendingResponses];
|
|
594
|
+
}
|
|
595
|
+
clearPendingResponses() {
|
|
596
|
+
this.#pendingResponses.length = 0;
|
|
597
|
+
}
|
|
598
|
+
// -------------------------------------------------------------------------
|
|
599
|
+
// Suppressed responses
|
|
600
|
+
// -------------------------------------------------------------------------
|
|
601
|
+
/**
|
|
602
|
+
* Mark a response's audible output as already accounted for.
|
|
603
|
+
*
|
|
604
|
+
* The host's played-origin proof owns audible acknowledgement, so a provider response that would
|
|
605
|
+
* repeat it is refused frame by frame rather than cancelled: the model still gets to finish its
|
|
606
|
+
* turn, the user just does not hear it twice.
|
|
607
|
+
*/
|
|
608
|
+
suppressResponse(responseId) {
|
|
609
|
+
this.#suppressedResponseIds.add(responseId);
|
|
610
|
+
}
|
|
611
|
+
responseIsSuppressed(responseId) {
|
|
612
|
+
return this.#suppressedResponseIds.has(responseId);
|
|
613
|
+
}
|
|
614
|
+
releaseSuppressedResponse(responseId) {
|
|
615
|
+
return this.#suppressedResponseIds.delete(responseId);
|
|
616
|
+
}
|
|
617
|
+
clearSuppressedResponses() {
|
|
618
|
+
this.#suppressedResponseIds.clear();
|
|
619
|
+
}
|
|
620
|
+
// -------------------------------------------------------------------------
|
|
621
|
+
// Pre-map audio
|
|
622
|
+
// -------------------------------------------------------------------------
|
|
623
|
+
/**
|
|
624
|
+
* The response whose audio is buffered, if any.
|
|
625
|
+
*
|
|
626
|
+
* The buffer belongs to one response at a time: audio can arrive before the event that names
|
|
627
|
+
* which renderer generation it belongs to, but a second response's audio cannot be mixed into
|
|
628
|
+
* the same buffer without losing which is which.
|
|
629
|
+
*/
|
|
630
|
+
get premapResponseId() {
|
|
631
|
+
return this.#premapResponseId;
|
|
632
|
+
}
|
|
633
|
+
get premapAudioBytes() {
|
|
634
|
+
return this.#pendingAudioBytes;
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Whether one more delta would exceed the budget.
|
|
638
|
+
*
|
|
639
|
+
* Deliberately separate from buffering. The reducer checks this in two places that then do
|
|
640
|
+
* different things -- one refuses without recording a turn at all, the other refuses a delta for
|
|
641
|
+
* a turn it already knows about -- and folding the check into the buffer call would hide that.
|
|
642
|
+
*/
|
|
643
|
+
premapAudioWouldExceed(byteLength) {
|
|
644
|
+
return this.#pendingAudioBytes + byteLength > MAX_PREMAP_AUDIO_BYTES;
|
|
645
|
+
}
|
|
646
|
+
bufferPremapAudio(responseId, pcm) {
|
|
647
|
+
this.#premapResponseId = responseId;
|
|
648
|
+
this.#pendingAudio.push(pcm);
|
|
649
|
+
this.#pendingAudioBytes += pcm.byteLength;
|
|
650
|
+
}
|
|
651
|
+
get premapAudio() {
|
|
652
|
+
return [...this.#pendingAudio];
|
|
653
|
+
}
|
|
654
|
+
/** Discard the whole buffer. Over budget means the audio is unusable, not that a delta is. */
|
|
655
|
+
clearPremapAudio() {
|
|
656
|
+
this.#premapResponseId = null;
|
|
657
|
+
this.#pendingAudio = [];
|
|
658
|
+
this.#pendingAudioBytes = 0;
|
|
659
|
+
}
|
|
660
|
+
/** Publish the accumulated changes as one new snapshot version. */
|
|
661
|
+
advanceSnapshot() {
|
|
662
|
+
this.#snapshotVersion += 1;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
function boundedProgress(value) {
|
|
666
|
+
if (value === null)
|
|
667
|
+
return null;
|
|
668
|
+
return [...value].slice(0, PROGRESS_SUMMARY_LIMIT).join('');
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Keep the last MAX_CAPTION_CHARS code points.
|
|
672
|
+
*
|
|
673
|
+
* A caption accumulates from deltas and the interesting end is the newest one: the display should
|
|
674
|
+
* follow what is being said now, not the opening of a long utterance. This is deliberately the
|
|
675
|
+
* opposite end from the progress-summary bound, which keeps its head. Slicing by code point is
|
|
676
|
+
* what keeps either bound from splitting an astral character.
|
|
677
|
+
*/
|
|
678
|
+
export function truncateCaption(text) {
|
|
679
|
+
const characters = [...text];
|
|
680
|
+
return characters.length <= MAX_CAPTION_CHARS
|
|
681
|
+
? text
|
|
682
|
+
: characters.slice(-MAX_CAPTION_CHARS).join('');
|
|
683
|
+
}
|
|
684
|
+
//# sourceMappingURL=session-state.js.map
|