nova-personal-agent-server 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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,412 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { notificationStore } from './store.js';
|
|
3
|
+
import { bindingSchema, notificationSchema, partSchema } from './contracts.js';
|
|
4
|
+
const digest = (value) => createHash('sha256').update(JSON.stringify(value)).digest('hex');
|
|
5
|
+
const terminal = new Set(['accepted', 'cancelled', 'expired', 'superseded']);
|
|
6
|
+
const uncertain = (d) => d.status === 'sending' || d.status === 'unknown' || d.parts.some(p => p.receipt !== null);
|
|
7
|
+
/** The host owns the process lock. Network calls never hold the serialized state lock. */
|
|
8
|
+
export class NotificationService {
|
|
9
|
+
options;
|
|
10
|
+
#store;
|
|
11
|
+
#adapters;
|
|
12
|
+
#state;
|
|
13
|
+
#tail = Promise.resolve();
|
|
14
|
+
#run;
|
|
15
|
+
#stop = new AbortController();
|
|
16
|
+
#opened = false;
|
|
17
|
+
constructor(options) {
|
|
18
|
+
this.options = options;
|
|
19
|
+
this.#store = notificationStore(options.path);
|
|
20
|
+
this.#adapters = new Map(options.adapters.map(a => [a.providerId, a]));
|
|
21
|
+
this.#state = this.#initial();
|
|
22
|
+
}
|
|
23
|
+
#now() { return this.options.now?.() ?? Date.now(); }
|
|
24
|
+
#initial() { return { schema_version: 1, user_scope: this.options.userScope, route_revision: 0, primary_binding_id: 'nova', configured: false, legacy_binding_id: null, bindings: [{ id: 'nova', provider: 'nova', identity: 'local', generation: 0, user_scope: this.options.userScope, status: 'ready' }], deliveries: [], dedupe: {}, migrated: false, cutoff: 0 }; }
|
|
25
|
+
register(adapter) {
|
|
26
|
+
if (this.#opened)
|
|
27
|
+
throw Error('notification_adapter_already_started');
|
|
28
|
+
this.#adapters.set(adapter.providerId, adapter);
|
|
29
|
+
}
|
|
30
|
+
get local() { return this.#state.primary_binding_id === 'nova'; }
|
|
31
|
+
get migrated() { return this.#state.migrated; }
|
|
32
|
+
get configured() { return this.#state.configured; }
|
|
33
|
+
binding(id) { return structuredClone(this.#state.bindings.find(b => b.id === id)); }
|
|
34
|
+
#mutate(fn, notify = true) {
|
|
35
|
+
const run = this.#tail.then(async () => {
|
|
36
|
+
const next = structuredClone(this.#state);
|
|
37
|
+
const value = fn(next);
|
|
38
|
+
if (JSON.stringify(next) === JSON.stringify(this.#state))
|
|
39
|
+
return value;
|
|
40
|
+
await this.#store.write(next);
|
|
41
|
+
this.#state = next;
|
|
42
|
+
if (notify)
|
|
43
|
+
this.options.changed?.();
|
|
44
|
+
return value;
|
|
45
|
+
});
|
|
46
|
+
this.#tail = run.then(() => { }, () => { });
|
|
47
|
+
return run;
|
|
48
|
+
}
|
|
49
|
+
async open() {
|
|
50
|
+
if (this.#opened)
|
|
51
|
+
return;
|
|
52
|
+
try {
|
|
53
|
+
this.#state = await this.#store.read(this.#initial());
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
if (e.code !== 'ENOENT')
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
if (this.#state.user_scope !== this.options.userScope)
|
|
60
|
+
throw Error('notification_scope_mismatch');
|
|
61
|
+
this.#stop = new AbortController();
|
|
62
|
+
await this.#mutate(s => {
|
|
63
|
+
for (const d of s.deliveries)
|
|
64
|
+
if (d.status === 'sending') {
|
|
65
|
+
d.status = 'unknown';
|
|
66
|
+
d.error = 'interrupted_send';
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
this.#opened = true;
|
|
70
|
+
}
|
|
71
|
+
async close() { this.#opened = false; this.#stop.abort(); await this.#run; await this.#tail; }
|
|
72
|
+
snapshot() {
|
|
73
|
+
const counts = Object.fromEntries(['pending', 'sending', 'accepted', 'retry_wait', 'blocked', 'unknown', 'cancelled', 'expired', 'superseded'].map(s => [s, 0]));
|
|
74
|
+
for (const d of this.#state.deliveries)
|
|
75
|
+
counts[d.status]++;
|
|
76
|
+
return { primary_binding_id: this.#state.primary_binding_id, route_revision: this.#state.route_revision, local: this.local, configured: this.#state.configured, legacy_additional: this.#state.legacy_binding_id !== null, bindings: this.#state.bindings.map(b => ({ id: b.id, provider: b.provider, user_scope: b.user_scope, generation: b.generation, status: b.status })), counts, deliveries: this.#state.deliveries.slice(-100).map(d => ({ id: d.id, title: d.notification.title, source: d.notification.source, status: d.status, error: d.error, binding_id: d.binding.id, updated_at: d.updated_at })) };
|
|
77
|
+
}
|
|
78
|
+
async bind(value) {
|
|
79
|
+
const binding = bindingSchema.parse(value);
|
|
80
|
+
if (binding.user_scope !== this.options.userScope)
|
|
81
|
+
throw Error('notification_scope_mismatch');
|
|
82
|
+
await this.#mutate(s => {
|
|
83
|
+
const old = s.bindings.find(b => b.id === binding.id);
|
|
84
|
+
if (old && (binding.generation < old.generation || old.identity !== binding.identity && binding.generation === old.generation))
|
|
85
|
+
throw Error('notification_binding_generation');
|
|
86
|
+
s.bindings = s.bindings.filter(b => b.id !== binding.id);
|
|
87
|
+
s.bindings.push(binding);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async configure(id) {
|
|
91
|
+
const binding = this.binding(id);
|
|
92
|
+
const adapter = binding && this.#adapters.get(binding.provider);
|
|
93
|
+
if (binding?.status !== 'ready' || !adapter || !await adapter.inspect(binding, this.#stop.signal))
|
|
94
|
+
throw Error('notification_channel_unavailable');
|
|
95
|
+
await this.#mutate(s => {
|
|
96
|
+
const current = s.bindings.find(b => b.id === id);
|
|
97
|
+
if (current?.identity !== binding.identity || current.generation !== binding.generation || current.status !== 'ready')
|
|
98
|
+
throw Error('notification_binding_changed');
|
|
99
|
+
s.configured = true;
|
|
100
|
+
s.legacy_binding_id = null;
|
|
101
|
+
s.primary_binding_id = id;
|
|
102
|
+
s.route_revision++;
|
|
103
|
+
for (const d of [...s.deliveries]) {
|
|
104
|
+
if (terminal.has(d.status) || uncertain(d))
|
|
105
|
+
continue;
|
|
106
|
+
const original = s.bindings.find(b => b.id === d.binding.id);
|
|
107
|
+
if (original?.identity !== d.binding.identity || original.generation !== d.binding.generation) {
|
|
108
|
+
d.status = 'blocked';
|
|
109
|
+
d.error = 'binding_changed';
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
d.status = 'superseded';
|
|
113
|
+
d.updated_at = this.#now();
|
|
114
|
+
if (!d.invalidated)
|
|
115
|
+
this.#enqueue(s, d.notification, binding);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async migrate(legacyBindingId, delivered = []) {
|
|
120
|
+
if (this.#state.migrated)
|
|
121
|
+
return;
|
|
122
|
+
await this.#mutate(s => {
|
|
123
|
+
if (s.migrated)
|
|
124
|
+
return;
|
|
125
|
+
s.migrated = true;
|
|
126
|
+
s.cutoff = this.#now() - 90 * 86400000;
|
|
127
|
+
if (!s.configured)
|
|
128
|
+
s.legacy_binding_id = legacyBindingId;
|
|
129
|
+
for (const n of delivered)
|
|
130
|
+
s.dedupe[this.#messageKey(n)] = this.#now();
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
#messageKey(n) { return digest([this.options.userScope, n.event_key, n.revision, n.occurrence]); }
|
|
134
|
+
#enqueue(s, n, binding) {
|
|
135
|
+
const id = digest([this.#messageKey(n), binding.id, binding.generation, s.route_revision]);
|
|
136
|
+
if (s.deliveries.some(d => d.id === id))
|
|
137
|
+
return;
|
|
138
|
+
s.deliveries.push({ id, notification: structuredClone(n), route_revision: s.route_revision, binding: structuredClone(binding), status: 'pending', parts: [], attempts: 0, next_attempt_at: n.not_before, updated_at: this.#now(), error: null, invalidated: false });
|
|
139
|
+
}
|
|
140
|
+
async publish(input) {
|
|
141
|
+
if (!this.#opened)
|
|
142
|
+
throw Error('notifications_unavailable');
|
|
143
|
+
const n = notificationSchema.parse(input);
|
|
144
|
+
await this.#mutate(s => {
|
|
145
|
+
const key = this.#messageKey(n);
|
|
146
|
+
if (s.dedupe[key] !== undefined)
|
|
147
|
+
return;
|
|
148
|
+
this.#prune(s);
|
|
149
|
+
if (s.deliveries.length >= 10000)
|
|
150
|
+
throw Error('notification_capacity_blocked');
|
|
151
|
+
if (n.created_at < s.cutoff)
|
|
152
|
+
throw Error('notification_too_old');
|
|
153
|
+
if (n.coalesce_key)
|
|
154
|
+
for (const d of s.deliveries) {
|
|
155
|
+
if (d.notification.coalesce_key === n.coalesce_key && !terminal.has(d.status) && !uncertain(d)) {
|
|
156
|
+
d.status = 'superseded';
|
|
157
|
+
d.updated_at = this.#now();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const target = s.bindings.find(b => b.id === s.primary_binding_id);
|
|
161
|
+
if (!target)
|
|
162
|
+
throw Error('notification_binding_missing');
|
|
163
|
+
this.#enqueue(s, n, target);
|
|
164
|
+
if (s.legacy_binding_id && n.source.type === 'feed') {
|
|
165
|
+
const extra = s.bindings.find(b => b.id === s.legacy_binding_id);
|
|
166
|
+
if (extra)
|
|
167
|
+
this.#enqueue(s, n, extra);
|
|
168
|
+
}
|
|
169
|
+
s.dedupe[key] = this.#now();
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
#prune(s) {
|
|
173
|
+
const now = this.#now();
|
|
174
|
+
s.deliveries = s.deliveries.filter(d => !terminal.has(d.status) || d.updated_at > now - 30 * 86400000);
|
|
175
|
+
const keep = new Set(s.deliveries.map(d => this.#messageKey(d.notification)));
|
|
176
|
+
for (const [key, at] of Object.entries(s.dedupe))
|
|
177
|
+
if (at < now - 90 * 86400000 && !keep.has(key))
|
|
178
|
+
delete s.dedupe[key];
|
|
179
|
+
s.cutoff = Math.max(s.cutoff, now - 90 * 86400000);
|
|
180
|
+
}
|
|
181
|
+
async #invalidateDelivery(id) { await this.#mutate(s => { const d = s.deliveries.find(row => row.id === id); if (d)
|
|
182
|
+
this.#erase(d); }); }
|
|
183
|
+
#erase(d) { d.invalidated = true; d.notification.body = ''; d.notification.title = ''; for (const p of d.parts) {
|
|
184
|
+
p.part.body = '';
|
|
185
|
+
p.part.title = '';
|
|
186
|
+
delete p.part.payload;
|
|
187
|
+
} if (d.status !== 'sending' && d.status !== 'unknown' && !terminal.has(d.status))
|
|
188
|
+
d.status = 'cancelled'; }
|
|
189
|
+
async invalidate(type, id, notify = true) {
|
|
190
|
+
if (!this.#state.deliveries.some(d => d.notification.source.type === type && d.notification.source.id === id && !d.invalidated))
|
|
191
|
+
return;
|
|
192
|
+
await this.#mutate(s => {
|
|
193
|
+
for (const d of s.deliveries)
|
|
194
|
+
if (d.notification.source.type === type && d.notification.source.id === id) {
|
|
195
|
+
d.invalidated = true;
|
|
196
|
+
d.notification.body = '';
|
|
197
|
+
d.notification.title = '';
|
|
198
|
+
for (const p of d.parts) {
|
|
199
|
+
p.part.body = '';
|
|
200
|
+
p.part.title = '';
|
|
201
|
+
delete p.part.payload;
|
|
202
|
+
}
|
|
203
|
+
if (d.status !== 'sending' && d.status !== 'unknown' && !terminal.has(d.status))
|
|
204
|
+
d.status = 'cancelled';
|
|
205
|
+
}
|
|
206
|
+
}, notify);
|
|
207
|
+
}
|
|
208
|
+
/** Manual resend acknowledges an uncertain prior request; normal dispatch never uses this. */
|
|
209
|
+
async retry(id, confirmUnknown = false) {
|
|
210
|
+
await this.#mutate(s => {
|
|
211
|
+
const d = s.deliveries.find(d => d.id === id);
|
|
212
|
+
if (!d || d.invalidated || terminal.has(d.status) || d.status === 'sending')
|
|
213
|
+
throw Error('notification_not_retryable');
|
|
214
|
+
if (d.status === 'unknown' && !confirmUnknown)
|
|
215
|
+
throw Error('notification_resend_confirmation_required');
|
|
216
|
+
if (d.status === 'unknown')
|
|
217
|
+
for (const p of d.parts)
|
|
218
|
+
if (!p.receipt)
|
|
219
|
+
p.key = digest([p.key, this.#now(), d.attempts]);
|
|
220
|
+
d.status = 'pending';
|
|
221
|
+
d.next_attempt_at = this.#now();
|
|
222
|
+
d.error = null;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
dispatch() {
|
|
226
|
+
if (this.#run)
|
|
227
|
+
return this.#run;
|
|
228
|
+
const run = this.#dispatch();
|
|
229
|
+
this.#run = run;
|
|
230
|
+
void run.finally(() => {
|
|
231
|
+
if (this.#run === run)
|
|
232
|
+
this.#run = undefined;
|
|
233
|
+
}).catch(() => { });
|
|
234
|
+
return run;
|
|
235
|
+
}
|
|
236
|
+
async #dispatch() {
|
|
237
|
+
if (!this.#opened || this.#stop.signal.aborted)
|
|
238
|
+
return;
|
|
239
|
+
const ids = this.#state.deliveries.filter(d => ['pending', 'retry_wait', 'blocked'].includes(d.status)).map(d => d.id);
|
|
240
|
+
for (const id of ids) {
|
|
241
|
+
if (this.#stop.signal.aborted)
|
|
242
|
+
return;
|
|
243
|
+
await this.#deliver(id);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
async #deliver(id) {
|
|
247
|
+
let d = this.#state.deliveries.find(d => d.id === id);
|
|
248
|
+
if (!d || d.invalidated || d.next_attempt_at > this.#now() || !['pending', 'retry_wait', 'blocked'].includes(d.status))
|
|
249
|
+
return;
|
|
250
|
+
if (d.notification.expires_at !== null && d.notification.expires_at <= this.#now()) {
|
|
251
|
+
await this.#finish(id, 'expired', null);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const valid = await this.options.valid?.(structuredClone(d.notification)) ?? 'valid';
|
|
255
|
+
if (valid === 'invalid') {
|
|
256
|
+
await this.#invalidateDelivery(id);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (valid === 'defer')
|
|
260
|
+
return;
|
|
261
|
+
const binding = this.binding(d.binding.id);
|
|
262
|
+
if (binding?.generation !== d.binding.generation || binding.identity !== d.binding.identity || binding.status !== 'ready') {
|
|
263
|
+
await this.#finish(id, 'blocked', 'binding_changed');
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const adapter = this.#adapters.get(binding.provider);
|
|
267
|
+
if (!adapter) {
|
|
268
|
+
await this.#finish(id, 'blocked', 'adapter_unavailable');
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
if (!await adapter.inspect(binding, this.#stop.signal)) {
|
|
273
|
+
await this.#finish(id, 'blocked', 'channel_unavailable');
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
await this.#finish(id, 'blocked', 'channel_unavailable');
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (d.parts.length === 0) {
|
|
282
|
+
let parts;
|
|
283
|
+
try {
|
|
284
|
+
parts = (await adapter.prepare(structuredClone(d.notification), binding)).map(p => partSchema.parse(p));
|
|
285
|
+
if (parts.length === 0 || parts.length > 256)
|
|
286
|
+
throw Error('invalid_parts');
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
await this.#finish(id, 'blocked', 'content_unavailable');
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
await this.#mutate(s => {
|
|
293
|
+
const current = s.deliveries.find(x => x.id === id);
|
|
294
|
+
if (current.status === 'superseded' || current.invalidated)
|
|
295
|
+
return;
|
|
296
|
+
current.parts = parts.map((part, i) => ({ part, key: digest([id, i]), receipt: null }));
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
while (!this.#stop.signal.aborted) {
|
|
300
|
+
const before = this.#state.deliveries.find(x => x.id === id);
|
|
301
|
+
if (!before || before.invalidated)
|
|
302
|
+
return;
|
|
303
|
+
if (before.notification.expires_at !== null && before.notification.expires_at <= this.#now()) {
|
|
304
|
+
await this.#finish(id, 'expired', null);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const currentValidity = await this.options.valid?.(structuredClone(before.notification)) ?? 'valid';
|
|
308
|
+
if (currentValidity !== 'valid') {
|
|
309
|
+
if (currentValidity === 'invalid')
|
|
310
|
+
await this.#invalidateDelivery(id);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const claim = await this.#mutate(s => {
|
|
314
|
+
const current = s.deliveries.find(x => x.id === id);
|
|
315
|
+
if (!current || current.invalidated || !['pending', 'retry_wait', 'blocked'].includes(current.status))
|
|
316
|
+
return null;
|
|
317
|
+
const actual = s.bindings.find(b => b.id === current.binding.id);
|
|
318
|
+
if (actual?.generation !== current.binding.generation || actual.identity !== current.binding.identity || actual.status !== 'ready') {
|
|
319
|
+
current.status = 'blocked';
|
|
320
|
+
current.error = 'binding_changed';
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
if (current.route_revision !== s.route_revision && !uncertain(current)) {
|
|
324
|
+
current.status = 'superseded';
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
const index = current.parts.findIndex(p => p.receipt === null);
|
|
328
|
+
if (index < 0) {
|
|
329
|
+
current.status = 'accepted';
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
current.status = 'sending';
|
|
333
|
+
current.attempts++;
|
|
334
|
+
current.updated_at = this.#now();
|
|
335
|
+
return { delivery: structuredClone(current), index };
|
|
336
|
+
});
|
|
337
|
+
if (!claim)
|
|
338
|
+
return;
|
|
339
|
+
const part = claim.delivery.parts[claim.index];
|
|
340
|
+
let outcome;
|
|
341
|
+
try {
|
|
342
|
+
outcome = await adapter.send(part.part, { binding: claim.delivery.binding, idempotency_key: part.key, notification: claim.delivery.notification, signal: this.#stop.signal });
|
|
343
|
+
}
|
|
344
|
+
catch {
|
|
345
|
+
outcome = { kind: 'unknown', reason: 'transport_exception' };
|
|
346
|
+
}
|
|
347
|
+
try {
|
|
348
|
+
await this.#mutate(s => {
|
|
349
|
+
const current = s.deliveries.find(x => x.id === id);
|
|
350
|
+
current.updated_at = this.#now();
|
|
351
|
+
if (outcome.kind === 'accepted') {
|
|
352
|
+
if (!outcome.receipt.message_id) {
|
|
353
|
+
current.status = 'unknown';
|
|
354
|
+
current.error = 'missing_receipt';
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
current.parts[claim.index].receipt = outcome.receipt;
|
|
358
|
+
current.status = current.parts.every(p => p.receipt !== null) ? 'accepted' : current.invalidated ? 'cancelled' : 'pending';
|
|
359
|
+
current.error = null;
|
|
360
|
+
}
|
|
361
|
+
else if (outcome.kind === 'retryable') {
|
|
362
|
+
current.status = current.invalidated ? 'cancelled' : 'retry_wait';
|
|
363
|
+
current.next_attempt_at = this.#now() + Math.max(outcome.retryAfterMs ?? 0, Math.min(900000, 5000 * 2 ** Math.min(current.attempts - 1, 8)) * (0.8 + (this.options.random?.() ?? Math.random()) * 0.4));
|
|
364
|
+
current.error = 'retryable';
|
|
365
|
+
if (current.route_revision !== s.route_revision && !current.parts.some(p => p.receipt) && !current.invalidated) {
|
|
366
|
+
const original = s.bindings.find(b => b.id === current.binding.id);
|
|
367
|
+
if (original?.identity !== current.binding.identity || original.generation !== current.binding.generation) {
|
|
368
|
+
current.status = 'blocked';
|
|
369
|
+
current.error = 'binding_changed';
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
current.status = 'superseded';
|
|
373
|
+
const target = s.bindings.find(b => b.id === s.primary_binding_id);
|
|
374
|
+
if (target)
|
|
375
|
+
this.#enqueue(s, current.notification, target);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
current.status = outcome.kind;
|
|
380
|
+
current.error = outcome.reason.slice(0, 128);
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
catch {
|
|
385
|
+
await this.#finish(id, 'unknown', 'receipt_persistence_failed').catch(() => { });
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
d = this.#state.deliveries.find(x => x.id === id);
|
|
389
|
+
if (d?.status !== 'pending')
|
|
390
|
+
return;
|
|
391
|
+
const validity = await this.options.valid?.(structuredClone(d.notification)) ?? 'valid';
|
|
392
|
+
if (validity !== 'valid') {
|
|
393
|
+
if (validity === 'invalid')
|
|
394
|
+
await this.#invalidateDelivery(id);
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
async #finish(id, status, error) {
|
|
400
|
+
await this.#mutate(s => {
|
|
401
|
+
const d = s.deliveries.find(x => x.id === id);
|
|
402
|
+
if (d && !terminal.has(d.status)) {
|
|
403
|
+
d.status = status;
|
|
404
|
+
d.error = error;
|
|
405
|
+
d.updated_at = this.#now();
|
|
406
|
+
if (status === 'blocked')
|
|
407
|
+
d.next_attempt_at = this.#now() + 30000;
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { BoundedJsonStore } from '../storage/bounded-json.js';
|
|
2
|
+
import { notificationStateSchema } from './contracts.js';
|
|
3
|
+
export const notificationStore = (path) => new BoundedJsonStore(path, notificationStateSchema, 16 * 1024 * 1024);
|
|
4
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { scheduleHash } from './scheduled-tasks.js';
|
|
4
|
+
const sectionSchema = z.enum(['todos', 'goals', 'tasks']);
|
|
5
|
+
export class ScheduledAppContext {
|
|
6
|
+
host;
|
|
7
|
+
constructor(host) {
|
|
8
|
+
this.host = host;
|
|
9
|
+
}
|
|
10
|
+
async read(section, cursor, grant) {
|
|
11
|
+
section = sectionSchema.parse(section);
|
|
12
|
+
this.host.scheduledAuthority.assert(grant, grant.fence);
|
|
13
|
+
if (!this.host.schedules.getRun(grant.run_id).definition.context[section])
|
|
14
|
+
throw Error('context_not_authorized');
|
|
15
|
+
const projection = await this.#project(section, grant.consumer, grant.task_id), version = scheduleHash(projection);
|
|
16
|
+
let offset = 0;
|
|
17
|
+
if (cursor) {
|
|
18
|
+
let parsed;
|
|
19
|
+
try {
|
|
20
|
+
z.string().max(1024).parse(cursor);
|
|
21
|
+
parsed = z.object({ section: sectionSchema, version: z.string(), offset: z.number().int().nonnegative() }).strict().parse(JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8')));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
throw Error('context_cursor_invalid: start with cursor null; otherwise copy next_cursor unchanged');
|
|
25
|
+
}
|
|
26
|
+
if (parsed.section !== section || parsed.version !== version)
|
|
27
|
+
throw Error('context_cursor_stale');
|
|
28
|
+
offset = parsed.offset;
|
|
29
|
+
}
|
|
30
|
+
if (offset > projection.items.length)
|
|
31
|
+
throw Error('context_cursor_stale');
|
|
32
|
+
const items = projection.items.slice(offset, offset + 20), truncated = items.some(item => item.truncated === true);
|
|
33
|
+
const page = { section, owner_task_id: grant.task_id, consumer: grant.consumer, items, next_cursor: null, coverage: { complete: false, omitted: projection.omitted, truncated, collection_version: version }, evidence_refs: ['app:' + section + ':' + version], observed_at: new Date().toISOString() };
|
|
34
|
+
while (Buffer.byteLength(JSON.stringify(page)) > 30000 && items.length > 1) {
|
|
35
|
+
items.pop();
|
|
36
|
+
page.coverage.truncated = true;
|
|
37
|
+
}
|
|
38
|
+
const next = offset + items.length;
|
|
39
|
+
if (next < projection.items.length)
|
|
40
|
+
page.next_cursor = Buffer.from(JSON.stringify({ section, version, offset: next })).toString('base64url');
|
|
41
|
+
page.coverage.complete = offset === 0 && page.next_cursor === null && page.coverage.omitted === 0 && !page.coverage.truncated;
|
|
42
|
+
this.host.scheduledAuthority.assert(grant, grant.fence);
|
|
43
|
+
if (!await this.validateRefs(page.evidence_refs, grant.consumer, grant.task_id))
|
|
44
|
+
throw Error('context_authorization_changed');
|
|
45
|
+
if (Buffer.byteLength(JSON.stringify(page)) > 32768)
|
|
46
|
+
throw Error('context_capacity');
|
|
47
|
+
await this.host.tasks.recordContextEvidence(grant.fence, randomUUID(), JSON.stringify(page), page.evidence_refs);
|
|
48
|
+
return page;
|
|
49
|
+
}
|
|
50
|
+
async validateTask(taskId, consumer, recipient = consumer) {
|
|
51
|
+
for (const evidence of this.host.tasks.evidence(taskId).filter(item => item.kind === 'context')) {
|
|
52
|
+
let page;
|
|
53
|
+
try {
|
|
54
|
+
page = JSON.parse(evidence.content);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (page.projection) {
|
|
60
|
+
const memory = this.host.options.memory(), projection = page.projection;
|
|
61
|
+
if (Array.isArray(projection.entries))
|
|
62
|
+
for (const entry of projection.entries) {
|
|
63
|
+
const row = await memory?.get?.(entry.entry_id);
|
|
64
|
+
if (row?.status !== 'active' || row.version !== entry.revision || !row.content.startsWith(entry.text))
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
const snippets = Array.isArray(projection.snippets) ? projection.snippets : projection.evidence ? [projection.evidence] : [];
|
|
68
|
+
for (const snippet of snippets) {
|
|
69
|
+
const row = await memory?.readEvidence?.(snippet.evidence_id);
|
|
70
|
+
if (!row || !row.text.startsWith(snippet.text) || row.locator !== snippet.locator || row.observed_at !== snippet.observed_at)
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (page.consumer !== consumer || !await this.validateRefs(evidence.refs, consumer, taskId) || recipient !== consumer && !await this.validateRefs(evidence.refs, recipient, taskId))
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
async validateRefs(refs, consumer, taskId) {
|
|
80
|
+
for (const ref of refs) {
|
|
81
|
+
const match = /^app:(todos|goals|tasks):([a-f0-9]{64})$/.exec(ref);
|
|
82
|
+
if (match) {
|
|
83
|
+
if (scheduleHash(await this.#project(sectionSchema.parse(match[1]), consumer, taskId)) !== match[2])
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
else if (!consumer || !await this.host.options.memory()?.canReadConversationEvidence?.(ref, consumer))
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
async #project(section, consumer, taskId) {
|
|
92
|
+
await this.host.life.refresh();
|
|
93
|
+
const items = [];
|
|
94
|
+
let omitted = 0;
|
|
95
|
+
if (section === 'tasks') {
|
|
96
|
+
for (const task of this.host.tasks.list().filter(task => task.id !== taskId))
|
|
97
|
+
items.push({ id: task.id, version: task.control_revision + ':' + task.goal_revision + ':' + task.phase, status: task.phase, waiting: task.waiting_reason !== null, execution_route: task.execution_route ?? null });
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
for (const row of this.host.life.snapshot()[section]) {
|
|
101
|
+
const sources = [...new Set([...(row.provenance_refs ?? []), ...(row.sources ?? []).map(source => source.evidence_id)])];
|
|
102
|
+
if (row.source_changed || row.auto_recorded && !sources.length || sources.length && (!consumer || (await Promise.all(sources.map(ref => this.host.options.memory()?.canReadConversationEvidence?.(ref, consumer) ?? Promise.resolve(false)))).some(allowed => !allowed))) {
|
|
103
|
+
omitted++;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
const note = row.note.slice(0, 900), criteria = 'success_criteria' in row ? row.success_criteria.slice(0, 600) : null;
|
|
107
|
+
items.push({ id: row.id, version: row.version, title: row.title, note, status: row.status, ...('due' in row ? { due: row.due, goal_id: row.goal_id } : { success_criteria: criteria }), truncated: note.length < row.note.length || ('success_criteria' in row && criteria.length < row.success_criteria.length) });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return { items, omitted };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=app-context.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const empty = { pending_approval: false, pending_approval_busy: false, kind: null, local_detail: null, operation_summary: null, expires_at: null, work: null, queued: 0 };
|
|
2
|
+
/** Scope every read and mutation, including implicit hold/invalidate operations. */
|
|
3
|
+
export function scopeApprovalController(broker, owns) {
|
|
4
|
+
return { get view() { return owns(broker.view) ? broker.view : empty; }, get pending() { return owns(broker.view) && broker.pending; },
|
|
5
|
+
observe: listener => broker.observe(view => listener(owns(view) ? view : empty)),
|
|
6
|
+
acceptDecision: input => owns(broker.view) && broker.acceptDecision(input), invalidate: reason => owns(broker.view) && broker.invalidate(reason), hold: reason => owns(broker.view) && broker.hold(reason), release: (reason, options) => owns(broker.view) && broker.release(reason, options) };
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=approval-scope.js.map
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { ProjectResolutionError } from '../executors/coding-executor.js';
|
|
2
|
+
/** Owned by one conversation. Global project/session focus is never a default here. */
|
|
3
|
+
export class CodingTargetController {
|
|
4
|
+
port;
|
|
5
|
+
onUpdate;
|
|
6
|
+
#target = null;
|
|
7
|
+
#revision = 0;
|
|
8
|
+
#work = null;
|
|
9
|
+
constructor(port, onUpdate) {
|
|
10
|
+
this.port = port;
|
|
11
|
+
this.onUpdate = onUpdate;
|
|
12
|
+
}
|
|
13
|
+
get revision() { return this.#revision; }
|
|
14
|
+
/** Claim only accepted dispatches/switches; older tasks cannot replace a newer selection. */
|
|
15
|
+
async accepted(selection, workId, expectedRevision) {
|
|
16
|
+
if (expectedRevision !== this.#revision)
|
|
17
|
+
return;
|
|
18
|
+
const revision = ++this.#revision;
|
|
19
|
+
this.#work = null;
|
|
20
|
+
try {
|
|
21
|
+
if (selection !== null)
|
|
22
|
+
await this.#adopt(await this.port.validate(selection), revision);
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
if (revision === this.#revision)
|
|
26
|
+
this.#work = workId === undefined ? null : { id: workId, revision };
|
|
27
|
+
await this.refreshWork();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** Project-view/progress notifications are triggers only; the work binding supplies authority. */
|
|
31
|
+
async refreshWork() {
|
|
32
|
+
const work = this.#work;
|
|
33
|
+
if (work?.revision !== this.#revision || !this.port.forWork)
|
|
34
|
+
return;
|
|
35
|
+
const target = await this.port.forWork(work.id);
|
|
36
|
+
if (target === null || !await this.#adopt(target, work.revision))
|
|
37
|
+
return;
|
|
38
|
+
if (this.#work === work)
|
|
39
|
+
this.#work = null;
|
|
40
|
+
}
|
|
41
|
+
async #adopt(target, revision) {
|
|
42
|
+
const stillCurrent = () => revision === this.#revision;
|
|
43
|
+
if (!stillCurrent())
|
|
44
|
+
return false;
|
|
45
|
+
if (this.onUpdate && !await this.onUpdate({ ...target }, stillCurrent))
|
|
46
|
+
return false;
|
|
47
|
+
if (!stillCurrent())
|
|
48
|
+
return false;
|
|
49
|
+
this.#target = { ...target };
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
get target() { return this.#target === null ? null : { ...this.#target }; }
|
|
53
|
+
activeProject() { return this.#target?.project ?? null; }
|
|
54
|
+
list() { return this.port.list(); }
|
|
55
|
+
async setTarget(selection) {
|
|
56
|
+
const revision = ++this.#revision;
|
|
57
|
+
const target = selection === null ? null : await this.port.validate({ ...selection });
|
|
58
|
+
if (revision !== this.#revision)
|
|
59
|
+
throw new ProjectResolutionError('unknown_project', { reason: 'target_superseded' });
|
|
60
|
+
this.#target = target === null ? null : { ...target };
|
|
61
|
+
return this.target;
|
|
62
|
+
}
|
|
63
|
+
async resolveTarget(decision, taskContext) {
|
|
64
|
+
const target = this.#target;
|
|
65
|
+
if (decision.kind === 'create')
|
|
66
|
+
return this.port.resolve(decision, undefined, taskContext);
|
|
67
|
+
const bound = target !== null && (decision.project === null || decision.project.toLowerCase() === target.project.toLowerCase());
|
|
68
|
+
if (!bound && decision.project === null)
|
|
69
|
+
throw new ProjectResolutionError('unknown_project', { reason: 'explicit_project_required' });
|
|
70
|
+
if (decision.kind === 'work' && decision.session === 'latest' && !decision.session_title && (!bound || target.session_id === null)) {
|
|
71
|
+
throw new ProjectResolutionError('unknown_session', { reason: 'continuation_target_required' });
|
|
72
|
+
}
|
|
73
|
+
if (bound) {
|
|
74
|
+
return this.port.resolve({ ...decision, project: target.project }, {
|
|
75
|
+
workspace_id: target.workspace_id,
|
|
76
|
+
session_id: decision.session === 'new' || decision.session_title ? null : target.session_id,
|
|
77
|
+
}, taskContext);
|
|
78
|
+
}
|
|
79
|
+
return this.port.resolve(decision, undefined, taskContext);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=coding-targets.js.map
|