nova-personal-agent-server 0.4.0 → 1.0.2
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 +674 -201
- package/README.md +12 -2
- package/bin/nova-space-host.mjs +4 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/cli.js +71 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/client.js +340 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/crypto.js +167 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/live.js +169 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/mailbox.js +217 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/media.js +320 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/pairing.js +13 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/protocol.js +50 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/security.js +305 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/server.js +477 -0
- package/node_modules/@nova-personal-agent/relay/dist/src/source-address.js +58 -0
- package/node_modules/@nova-personal-agent/relay/package.json +1 -0
- package/package.json +13 -6
- package/runtime/dist/src/cascaded-text-provider.js +8 -3
- package/runtime/dist/src/composition/assembly.js +133 -35
- package/runtime/dist/src/composition/cascaded-realtime-assembly.js +114 -59
- package/runtime/dist/src/composition/production-composition.js +57 -21
- package/runtime/dist/src/composition/realtime-assembly.js +160 -52
- package/runtime/dist/src/composition/soul-context.js +22 -0
- package/runtime/dist/src/config/capability-registry.js +48 -3
- package/runtime/dist/src/config/cascaded-realtime-config.js +23 -10
- package/runtime/dist/src/config/config.js +541 -128
- package/runtime/dist/src/config/credential-rotation.js +26 -0
- package/runtime/dist/src/config/data-root.js +764 -0
- package/runtime/dist/src/config/diagnostics.js +3 -1
- package/runtime/dist/src/config/environment-contract.js +190 -188
- package/runtime/dist/src/config/model-routes.js +140 -0
- package/runtime/dist/src/config/voice-credentials.js +55 -0
- package/runtime/dist/src/connectors/composio/index.js +9 -0
- package/runtime/dist/src/connectors/feishu/index.js +258 -44
- package/runtime/dist/src/connectors/macos/mail.js +7 -3
- package/runtime/dist/src/connectors/macos/reader.js +3 -1
- package/runtime/dist/src/conversation-voice-provider.js +5 -2
- package/runtime/dist/src/core/causal-runtime.js +38 -3
- package/runtime/dist/src/core/context-view.js +7 -0
- package/runtime/dist/src/core/guards.js +32 -0
- package/runtime/dist/src/core/runtime.js +3 -0
- package/runtime/dist/src/core/schedule-tools.js +2 -2
- package/runtime/dist/src/core/source-processing-instructions.js +2 -2
- package/runtime/dist/src/core/source-processing-tools.js +3 -1
- package/runtime/dist/src/core/tool-schema.js +42 -7
- package/runtime/dist/src/desktop/desktop-camera.js +3 -5
- package/runtime/dist/src/desktop/desktop-control.js +41 -8
- package/runtime/dist/src/desktop/desktop-session.js +90 -19
- package/runtime/dist/src/desktop/desktop-wire.js +5 -7
- package/runtime/dist/src/desktop/startup-error.js +18 -16
- package/runtime/dist/src/desktop/workbench-acceptance.js +52 -14
- package/runtime/dist/src/desktop-entry.js +6 -2
- package/runtime/dist/src/desktop.js +5 -1
- package/runtime/dist/src/device/attempt.js +65 -0
- package/runtime/dist/src/device/authority.js +159 -0
- package/runtime/dist/src/device/gateway.js +87 -0
- package/runtime/dist/src/device/ledger.js +91 -0
- package/runtime/dist/src/device/ownership.js +92 -0
- package/runtime/dist/src/device/port.js +2 -0
- package/runtime/dist/src/device/schedule-ownership.js +103 -0
- package/runtime/dist/src/executors/acp/transport.js +2 -0
- package/runtime/dist/src/executors/browser.js +4 -3
- package/runtime/dist/src/executors/codex/adapter-project.js +58 -12
- package/runtime/dist/src/executors/codex/app-server-schema.js +12 -21
- package/runtime/dist/src/executors/codex/app-server-transport.js +32 -18
- package/runtime/dist/src/executors/codex/common.js +1 -1
- package/runtime/dist/src/executors/codex/contract.js +12 -16
- package/runtime/dist/src/executors/codex/credential-snapshot.js +1 -9
- package/runtime/dist/src/executors/codex/factory.js +54 -32
- package/runtime/dist/src/executors/codex/host-config.js +69 -20
- package/runtime/dist/src/executors/codex/launch-profile.js +3 -2
- package/runtime/dist/src/executors/codex/local-provider-credential.js +39 -0
- package/runtime/dist/src/executors/codex/process-owner.js +4 -6
- package/runtime/dist/src/executors/codex/production-host.js +33 -34
- package/runtime/dist/src/executors/codex/protocol.js +9 -14
- package/runtime/dist/src/executors/codex/session-title.js +6 -8
- package/runtime/dist/src/executors/codex/spawn-env.js +27 -6
- package/runtime/dist/src/executors/codex/turn-projection.js +12 -17
- package/runtime/dist/src/executors/codex/windows-guardian.js +4 -6
- package/runtime/dist/src/executors/coding/intake.js +12 -7
- package/runtime/dist/src/executors/computer-backend.js +18 -10
- package/runtime/dist/src/executors/computer-config.js +32 -0
- package/runtime/dist/src/executors/computer-device.js +68 -16
- package/runtime/dist/src/executors/computer-worker.js +18 -6
- package/runtime/dist/src/executors/computer.js +29 -56
- package/runtime/dist/src/executors/device-lock.js +14 -5
- package/runtime/dist/src/executors/gui-execution.js +2 -0
- package/runtime/dist/src/executors/index.js +0 -1
- package/runtime/dist/src/executors/mobile.js +9 -8
- package/runtime/dist/src/executors/native-process.js +4 -0
- package/runtime/dist/src/executors/search.js +3 -5
- package/runtime/dist/src/executors/task-resources.js +21 -0
- package/runtime/dist/src/executors/watcher.js +2 -4
- package/runtime/dist/src/experience/host.js +179 -0
- package/runtime/dist/src/index.js +2 -0
- package/runtime/dist/src/knowledge/assembly.js +3 -4
- package/runtime/dist/src/knowledge/embeddings.js +50 -34
- package/runtime/dist/src/knowledge/service.js +1 -0
- package/runtime/dist/src/knowledge/store-client.js +1 -6
- package/runtime/dist/src/knowledge/store-worker.js +1 -3
- package/runtime/dist/src/mem0/resource.js +25 -0
- package/runtime/dist/src/mem0/store-worker.js +234 -21
- package/runtime/dist/src/memory/consumer.js +31 -0
- package/runtime/dist/src/memory/engine-worker.js +210 -0
- package/runtime/dist/src/memory/entry.js +8 -1
- package/runtime/dist/src/memory/explicit-memory.js +20 -0
- package/runtime/dist/src/memory/factory.js +30 -15
- package/runtime/dist/src/memory/grounded-voicemem.js +10 -0
- package/runtime/dist/src/memory/ledger-recall-engine.js +86 -0
- package/runtime/dist/src/memory/prerecall-port.js +27 -0
- package/runtime/dist/src/memory/prerecall.js +1 -1
- package/runtime/dist/src/memory/production-engine.js +151 -0
- package/runtime/dist/src/memory/provenance.js +1 -0
- package/runtime/dist/src/memory/remote-personal-memory.js +3 -1
- package/runtime/dist/src/memory/retrieval.js +107 -22
- package/runtime/dist/src/memory/sensitivity.js +6 -2
- package/runtime/dist/src/memory/source-retrieval.js +11 -0
- package/runtime/dist/src/memory/tool-capabilities.js +46 -0
- package/runtime/dist/src/memory/voicemem-sdk/LICENSE +202 -0
- package/runtime/dist/src/memory/voicemem-sdk/NOTICE.md +97 -0
- package/runtime/dist/src/memory/voicemem-sdk/PATCH-NOTICE.md +23 -0
- package/runtime/dist/src/memory/voicemem-sdk/archive.js +12 -0
- package/runtime/dist/src/memory/voicemem-sdk/audio.js +111 -0
- package/runtime/dist/src/memory/voicemem-sdk/barge-in.js +59 -0
- package/runtime/dist/src/memory/voicemem-sdk/checkpoint.js +56 -0
- package/runtime/dist/src/memory/voicemem-sdk/cleanup.js +139 -0
- package/runtime/dist/src/memory/voicemem-sdk/dashscope-asr.js +254 -0
- package/runtime/dist/src/memory/voicemem-sdk/dashscope-tts.js +247 -0
- package/runtime/dist/src/memory/voicemem-sdk/evaluation.js +330 -0
- package/runtime/dist/src/memory/voicemem-sdk/extraction.js +220 -0
- package/runtime/dist/src/memory/voicemem-sdk/finetune-data.js +115 -0
- package/runtime/dist/src/memory/voicemem-sdk/finetune.js +170 -0
- package/runtime/dist/src/memory/voicemem-sdk/grounded-reply-preferences.js +81 -0
- package/runtime/dist/src/memory/voicemem-sdk/index.js +22 -0
- package/runtime/dist/src/memory/voicemem-sdk/local-embedding.js +164 -0
- package/runtime/dist/src/memory/voicemem-sdk/local-runtime.js +53 -0
- package/runtime/dist/src/memory/voicemem-sdk/local-speaker.js +240 -0
- package/runtime/dist/src/memory/voicemem-sdk/memory.js +767 -0
- package/runtime/dist/src/memory/voicemem-sdk/model-json.js +14 -0
- package/runtime/dist/src/memory/voicemem-sdk/personamem.js +209 -0
- package/runtime/dist/src/memory/voicemem-sdk/prompts/addenda.js +215 -0
- package/runtime/dist/src/memory/voicemem-sdk/prompts/additive-extraction.js +478 -0
- package/runtime/dist/src/memory/voicemem-sdk/prompts/index.js +32 -0
- package/runtime/dist/src/memory/voicemem-sdk/prompts/update-memory.js +152 -0
- package/runtime/dist/src/memory/voicemem-sdk/providers.js +325 -0
- package/runtime/dist/src/memory/voicemem-sdk/ranking.js +65 -0
- package/runtime/dist/src/memory/voicemem-sdk/reaction.js +33 -0
- package/runtime/dist/src/memory/voicemem-sdk/realtime.js +215 -0
- package/runtime/dist/src/memory/voicemem-sdk/reply.js +68 -0
- package/runtime/dist/src/memory/voicemem-sdk/routing.js +209 -0
- package/runtime/dist/src/memory/voicemem-sdk/slots.js +61 -0
- package/runtime/dist/src/memory/voicemem-sdk/speaker-bindings.js +120 -0
- package/runtime/dist/src/memory/voicemem-sdk/speaker-profiles.js +327 -0
- package/runtime/dist/src/memory/voicemem-sdk/store.js +577 -0
- package/runtime/dist/src/memory/voicemem-sdk/stream.js +317 -0
- package/runtime/dist/src/memory/voicemem-sdk/subgraph.js +113 -0
- package/runtime/dist/src/memory/voicemem-sdk/summaries.js +56 -0
- package/runtime/dist/src/memory/voicemem-sdk/time.js +157 -0
- package/runtime/dist/src/memory/voicemem-sdk/types.js +2 -0
- package/runtime/dist/src/memory-ledger/store-client.js +235 -37
- package/runtime/dist/src/memory-ledger/store-worker.js +8 -5
- package/runtime/dist/src/memory-ledger/store.js +91 -1
- package/runtime/dist/src/memory-substrate/consolidation.js +23 -7
- package/runtime/dist/src/memory-substrate/diary.js +27 -0
- package/runtime/dist/src/memory-substrate/engine-outbox.js +107 -0
- package/runtime/dist/src/memory-substrate/explicit-memory.js +112 -0
- package/runtime/dist/src/memory-substrate/fact-fidelity.js +68 -0
- package/runtime/dist/src/memory-substrate/feishu-consent.js +41 -0
- package/runtime/dist/src/memory-substrate/file-authority.js +29 -6
- package/runtime/dist/src/memory-substrate/life-operations.js +35 -9
- package/runtime/dist/src/memory-substrate/markdown-repository.js +271 -65
- package/runtime/dist/src/memory-substrate/memory-documents.js +96 -0
- package/runtime/dist/src/memory-substrate/migration.js +2 -0
- package/runtime/dist/src/memory-substrate/purge.js +12 -2
- package/runtime/dist/src/memory-substrate/resource.js +514 -129
- package/runtime/dist/src/memory-substrate/retrieval.js +8 -5
- package/runtime/dist/src/memory-substrate/source-operations.js +9 -1
- package/runtime/dist/src/memory-substrate/source-processing.js +436 -40
- package/runtime/dist/src/memory-substrate/source-retrieval.js +28 -0
- package/runtime/dist/src/memory-substrate/source-state.js +8 -0
- package/runtime/dist/src/memory-substrate/space-source-input.js +90 -0
- package/runtime/dist/src/memory-substrate/store.js +125 -23
- package/runtime/dist/src/model/goal-checkin-writer.js +72 -0
- package/runtime/dist/src/model/goal-writer.js +80 -0
- package/runtime/dist/src/model/jev-client.js +24 -13
- package/runtime/dist/src/model/model-adapters.js +2 -3
- package/runtime/dist/src/model/model-catalog.js +80 -0
- package/runtime/dist/src/model/model-gateway.js +47 -18
- package/runtime/dist/src/model/model-purpose.js +3 -4
- package/runtime/dist/src/model/personal-writer.js +536 -44
- package/runtime/dist/src/model/proactivity.js +50 -6
- package/runtime/dist/src/model/responses-model-gateway.js +59 -0
- package/runtime/dist/src/model/task-verifier.js +3 -3
- package/runtime/dist/src/model/timeline-anchors.js +77 -0
- package/runtime/dist/src/model/timeline-perspective.js +13 -0
- package/runtime/dist/src/model/timeline-selection.js +182 -0
- package/runtime/dist/src/model/understanding-writer.js +27 -0
- package/runtime/dist/src/model/vision-capability.js +3 -1
- package/runtime/dist/src/model/workbench-writer.js +96 -0
- package/runtime/dist/src/news/feeds.js +6 -10
- package/runtime/dist/src/news/interests.js +23 -0
- package/runtime/dist/src/news/service.js +87 -31
- package/runtime/dist/src/news/sources.json +116 -0
- package/runtime/dist/src/notifications/adapters/space-relay.js +4 -0
- package/runtime/dist/src/notifications/content.js +41 -0
- package/runtime/dist/src/notifications/contracts.js +4 -2
- package/runtime/dist/src/notifications/feed.js +6 -2
- package/runtime/dist/src/notifications/service.js +27 -14
- package/runtime/dist/src/notifications/store.js +3 -1
- package/runtime/dist/src/personal-agent/app-context.js +39 -4
- package/runtime/dist/src/personal-agent/chat-failure.js +37 -0
- package/runtime/dist/src/personal-agent/computer-use/contract.js +42 -0
- package/runtime/dist/src/personal-agent/computer-use/session.js +280 -0
- package/runtime/dist/src/personal-agent/computer-use/skill.js +18 -0
- package/runtime/dist/src/personal-agent/computer-use/tools.js +15 -0
- package/runtime/dist/src/personal-agent/context-candidates.js +9 -13
- package/runtime/dist/src/personal-agent/context-dependencies.js +28 -0
- package/runtime/dist/src/personal-agent/contracts.js +24 -4
- package/runtime/dist/src/personal-agent/conversation-runtime.js +440 -42
- package/runtime/dist/src/personal-agent/conversation-tool-bridge.js +97 -0
- package/runtime/dist/src/personal-agent/conversation-tool-policy.js +22 -0
- package/runtime/dist/src/personal-agent/conversations.js +71 -15
- package/runtime/dist/src/personal-agent/daily-brief.js +5 -0
- package/runtime/dist/src/personal-agent/daily-workbench.js +558 -0
- package/runtime/dist/src/personal-agent/discovery-window.js +36 -0
- package/runtime/dist/src/personal-agent/goal-checkin.js +53 -0
- package/runtime/dist/src/personal-agent/host.js +2379 -223
- package/runtime/dist/src/personal-agent/identity-context.js +33 -0
- package/runtime/dist/src/personal-agent/life.js +129 -11
- package/runtime/dist/src/personal-agent/local-shell.js +181 -0
- package/runtime/dist/src/personal-agent/local-tools.js +234 -0
- package/runtime/dist/src/personal-agent/preferred-name-sync.js +35 -0
- package/runtime/dist/src/personal-agent/profile-warmup.js +100 -39
- package/runtime/dist/src/personal-agent/project-digests.js +7 -3
- package/runtime/dist/src/personal-agent/project-history.js +36 -0
- package/runtime/dist/src/personal-agent/reading-copies.js +183 -0
- package/runtime/dist/src/personal-agent/scheduled-tasks.js +50 -13
- package/runtime/dist/src/personal-agent/scheduled-tool-runtime.js +26 -10
- package/runtime/dist/src/personal-agent/source-processing-agent.js +239 -17
- package/runtime/dist/src/personal-agent/sources.js +9 -1
- package/runtime/dist/src/personal-agent/store.js +124 -14
- package/runtime/dist/src/personal-agent/task-loop.js +3 -0
- package/runtime/dist/src/personal-agent/tasks.js +16 -4
- package/runtime/dist/src/personal-agent/timeline-brief.js +462 -0
- package/runtime/dist/src/personal-agent/timeline-cache.js +191 -0
- package/runtime/dist/src/personal-agent/timeline-cluster.js +30 -0
- package/runtime/dist/src/personal-agent/timeline-inputs.js +47 -0
- package/runtime/dist/src/personal-agent/timeline-time.js +60 -0
- package/runtime/dist/src/personal-agent/understanding-products.js +49 -0
- package/runtime/dist/src/personal-agent/understanding.js +7 -22
- package/runtime/dist/src/personal-agent/workbench-context.js +17 -21
- package/runtime/dist/src/personal-agent/workbench-copy.js +124 -0
- package/runtime/dist/src/personal-agent/workbench-evidence.js +77 -0
- package/runtime/dist/src/personal-agent/workbench-instructions.js +21 -0
- package/runtime/dist/src/personal-agent/workbench-runtime.js +1006 -0
- package/runtime/dist/src/personal-agent/workbench-shell-class.js +304 -0
- package/runtime/dist/src/personal-agent/workbench-store.js +292 -0
- package/runtime/dist/src/personal-agent/workbench-temporal.js +66 -0
- package/runtime/dist/src/projects/project-native-resource.js +10 -8
- package/runtime/dist/src/projects/project-store.js +36 -4
- package/runtime/dist/src/realtime/bridge.js +77 -8
- package/runtime/dist/src/realtime/capabilities.js +19 -0
- package/runtime/dist/src/realtime/cascaded/adapter.js +180 -40
- package/runtime/dist/src/realtime/cascaded/ark-llm.js +150 -24
- package/runtime/dist/src/realtime/cascaded/chat-completions-llm.js +243 -83
- package/runtime/dist/src/realtime/cascaded/llm-stream-limits.js +18 -0
- package/runtime/dist/src/realtime/cascaded/llm.js +96 -0
- package/runtime/dist/src/realtime/cascaded/openai-compatible-speech.js +251 -0
- package/runtime/dist/src/realtime/cascaded/provider.js +8 -0
- package/runtime/dist/src/realtime/evidence.js +1 -1
- package/runtime/dist/src/realtime/frontend-instructions.js +76 -55
- package/runtime/dist/src/realtime/gemini.js +14 -3
- package/runtime/dist/src/realtime/host-delivery.js +37 -4
- package/runtime/dist/src/realtime/protocol.js +18 -3
- package/runtime/dist/src/realtime/provider-projection.js +4 -1
- package/runtime/dist/src/realtime/provider-session.js +73 -16
- package/runtime/dist/src/realtime/qwen-transport.js +35 -4
- package/runtime/dist/src/realtime/qwen.js +144 -9
- package/runtime/dist/src/realtime/service-state.js +0 -1
- package/runtime/dist/src/realtime/service.js +93 -7
- package/runtime/dist/src/realtime/session.js +39 -32
- package/runtime/dist/src/realtime/sglang-native.js +214 -0
- package/runtime/dist/src/realtime/system-prompts.en.js +14 -2
- package/runtime/dist/src/realtime/telemetry.js +6 -3
- package/runtime/dist/src/realtime/tool-continuations.js +84 -7
- package/runtime/dist/src/realtime/transport.js +17 -0
- package/runtime/dist/src/realtime/usage.js +33 -17
- package/runtime/dist/src/realtime/volcengine/ark.js +40 -15
- package/runtime/dist/src/realtime/volcengine/asr.js +2 -1
- package/runtime/dist/src/realtime/volcengine/silence-endpointing.js +1 -7
- package/runtime/dist/src/realtime/volcengine/tts.js +2 -1
- package/runtime/dist/src/server/aoq-chat-server.js +1 -1
- package/runtime/dist/src/server/client-server.js +21 -0
- package/runtime/dist/src/server-entry.js +3 -0
- package/runtime/dist/src/space-host/admin-socket.js +115 -0
- package/runtime/dist/src/space-host/audio-owner.js +44 -0
- package/runtime/dist/src/space-host/cli.js +195 -0
- package/runtime/dist/src/space-host/config.js +247 -0
- package/runtime/dist/src/space-host/context-input.js +12 -0
- package/runtime/dist/src/space-host/contracts.js +10 -0
- package/runtime/dist/src/space-host/host-admin.js +114 -0
- package/runtime/dist/src/space-host/key-delivery.js +28 -0
- package/runtime/dist/src/space-host/mobile-tools.js +9 -0
- package/runtime/dist/src/space-host/model-settings.js +213 -0
- package/runtime/dist/src/space-host/projection.js +37 -0
- package/runtime/dist/src/space-host/push.js +137 -0
- package/runtime/dist/src/space-host/relay-transport.js +55 -0
- package/runtime/dist/src/space-host/service.js +841 -0
- package/runtime/dist/src/space-host/voice-crypto.js +240 -0
- package/runtime/dist/src/space-host/voice.js +600 -0
- package/runtime/dist/src/space-host/windows-secret-key.js +82 -0
- package/runtime/dist/src/storage/private-database.js +7 -10
- package/runtime/dist/src/text/unicode-normalize.js +5 -1
- package/runtime/dist/src/understanding/candidates.js +8 -10
- package/runtime/dist/src/understanding/fixture.js +1 -1
- package/runtime/dist/src/understanding/pipeline.js +4 -4
- package/runtime/dist/src/usage/context.js +23 -0
- package/runtime/dist/src/usage/index.js +4 -0
- package/runtime/dist/src/usage/ledger-worker.js +90 -0
- package/runtime/dist/src/usage/ledger.js +74 -0
- package/runtime/dist/src/usage/model-call.js +63 -0
- package/runtime/dist/src/usage/pricing.js +68 -0
- package/runtime/dist/src/usage/summary.js +69 -0
- package/runtime/dist/src/voicemem/store-client.js +104 -21
- package/runtime/dist/src/voicemem/store-worker.js +5 -8
- package/runtime/dist/src/voicemem/versioned-memory.js +1 -1
- package/src/command.mjs +8 -1
- package/runtime/dist/src/executors/autoglm.js +0 -376
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHash, createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, hkdfSync, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* Space voice call crypto (docs/design/space-voice.md §2). Pure and volatile: nothing here is
|
|
4
|
+
* persisted. Byte-for-byte compatible with clients/flutter/lib/space/space_voice_crypto.dart;
|
|
5
|
+
* tests/fixtures/space-voice/v1/vectors.json pins both.
|
|
6
|
+
*/
|
|
7
|
+
export const VOICE_PROTOCOL = 'nova-voice/v1';
|
|
8
|
+
export const FRAME_AUDIO = 1, FRAME_CONTROL = 2, FRAME_KEEPALIVE = 3;
|
|
9
|
+
export const FRAME_HEADER_BYTES = 12, FRAME_TAG_BYTES = 16, FRAME_MIN_BYTES = FRAME_HEADER_BYTES + FRAME_TAG_BYTES, FRAME_MAX_BYTES = 4096;
|
|
10
|
+
export const AUDIO_PREFIX_BYTES = 10, CONTROL_MAX_BYTES = 2048;
|
|
11
|
+
export const MAX_SEQ = (1n << 48n) - 1n;
|
|
12
|
+
export const REPLAY_WINDOW = 128n;
|
|
13
|
+
const PKCS8_X25519 = Buffer.from('302e020100300506032b656e04220420', 'hex');
|
|
14
|
+
export class VoiceCryptoError extends Error {
|
|
15
|
+
code;
|
|
16
|
+
constructor(code) {
|
|
17
|
+
super(code);
|
|
18
|
+
this.code = code;
|
|
19
|
+
this.name = 'VoiceCryptoError';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** RFC 8785 subset: objects with sorted keys, arrays, strings, booleans, null and safe integers. */
|
|
23
|
+
export function canonicalJson(value) {
|
|
24
|
+
if (value === null || typeof value === 'boolean' || typeof value === 'string')
|
|
25
|
+
return JSON.stringify(value);
|
|
26
|
+
if (typeof value === 'number') {
|
|
27
|
+
if (!Number.isSafeInteger(value))
|
|
28
|
+
throw new VoiceCryptoError('canonical_number');
|
|
29
|
+
return String(value);
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(value))
|
|
32
|
+
return '[' + value.map(canonicalJson).join(',') + ']';
|
|
33
|
+
if (typeof value === 'object') {
|
|
34
|
+
const entries = Object.entries(value).filter(([, v]) => v !== undefined);
|
|
35
|
+
// JCS orders keys by UTF-16 code units, which is JavaScript's default string comparison.
|
|
36
|
+
entries.sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
37
|
+
return '{' + entries.map(([k, v]) => JSON.stringify(k) + ':' + canonicalJson(v)).join(',') + '}';
|
|
38
|
+
}
|
|
39
|
+
throw new VoiceCryptoError('canonical_type');
|
|
40
|
+
}
|
|
41
|
+
/** 4-byte big-endian length prefix, then the bytes. */
|
|
42
|
+
export function lp(value) { const bytes = typeof value === 'string' ? Buffer.from(value, 'utf8') : Buffer.from(value); const length = Buffer.alloc(4); length.writeUInt32BE(bytes.length); return Buffer.concat([length, bytes]); }
|
|
43
|
+
/** Binds the exact decrypted voice.start payload to its Space, epoch and both device identities. */
|
|
44
|
+
export function startHash(input) {
|
|
45
|
+
if (!Number.isSafeInteger(input.epoch) || input.epoch < 1)
|
|
46
|
+
throw new VoiceCryptoError('invalid_epoch');
|
|
47
|
+
return createHash('sha256').update(Buffer.concat([lp(canonicalJson(input.start)), lp(input.space), lp(String(input.epoch)), lp(input.client), lp(input.host)])).digest();
|
|
48
|
+
}
|
|
49
|
+
export function generateEphemeral() { const pair = generateKeyPairSync('x25519'); return { privateKey: pair.privateKey, publicKey: String(pair.publicKey.export({ format: 'jwk' }).x) }; }
|
|
50
|
+
/** Deterministic ephemeral for test vectors only. */
|
|
51
|
+
export function ephemeralFromSeed(seed) {
|
|
52
|
+
if (seed.length !== 32)
|
|
53
|
+
throw new VoiceCryptoError('invalid_seed');
|
|
54
|
+
const privateKey = createPrivateKey({ key: Buffer.concat([PKCS8_X25519, seed]), format: 'der', type: 'pkcs8' });
|
|
55
|
+
return { privateKey, publicKey: String(createPublicKey(privateKey).export({ format: 'jwk' }).x) };
|
|
56
|
+
}
|
|
57
|
+
export function decodePublicKey(value) {
|
|
58
|
+
if (typeof value !== 'string' || !/^[A-Za-z0-9_-]{43}$/.test(value))
|
|
59
|
+
throw new VoiceCryptoError('invalid_public_key');
|
|
60
|
+
const raw = Buffer.from(value, 'base64url');
|
|
61
|
+
if (raw.length !== 32 || raw.toString('base64url') !== value)
|
|
62
|
+
throw new VoiceCryptoError('invalid_public_key');
|
|
63
|
+
return raw;
|
|
64
|
+
}
|
|
65
|
+
/** X25519 → HKDF-SHA256 with the epoch content key as salt and every negotiated value in `info`. */
|
|
66
|
+
export function deriveCallKeys(input) {
|
|
67
|
+
if (input.contentKey.length !== 32)
|
|
68
|
+
throw new VoiceCryptoError('invalid_content_key');
|
|
69
|
+
if (input.startHash.length !== 32)
|
|
70
|
+
throw new VoiceCryptoError('invalid_start_hash');
|
|
71
|
+
if (!Number.isSafeInteger(input.frameMs) || input.frameMs < 1 || input.frameMs > 0xffff)
|
|
72
|
+
throw new VoiceCryptoError('invalid_frame_ms');
|
|
73
|
+
const peer = decodePublicKey(input.peer);
|
|
74
|
+
let shared;
|
|
75
|
+
try {
|
|
76
|
+
shared = diffieHellman({ privateKey: input.own.privateKey, publicKey: createPublicKey({ key: { kty: 'OKP', crv: 'X25519', x: peer.toString('base64url') }, format: 'jwk' }) });
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
throw new VoiceCryptoError('invalid_shared_secret');
|
|
80
|
+
}
|
|
81
|
+
if (shared.length !== 32 || timingSafeEqual(shared, Buffer.alloc(32)))
|
|
82
|
+
throw new VoiceCryptoError('invalid_shared_secret');
|
|
83
|
+
const frame = Buffer.alloc(2);
|
|
84
|
+
frame.writeUInt16BE(input.frameMs);
|
|
85
|
+
const info = (direction) => Buffer.concat([lp(VOICE_PROTOCOL), lp(input.startHash), lp(input.ephC), lp(input.ephH), lp(input.session), lp(input.codec), lp(frame), lp(direction)]);
|
|
86
|
+
const key = (direction) => Buffer.from(hkdfSync('sha256', shared, input.contentKey, info(direction), 32));
|
|
87
|
+
const keys = { up: key('up'), down: key('down') };
|
|
88
|
+
shared.fill(0);
|
|
89
|
+
return keys;
|
|
90
|
+
}
|
|
91
|
+
function header(type, seq) {
|
|
92
|
+
if (seq < 0n || seq > MAX_SEQ)
|
|
93
|
+
throw new VoiceCryptoError('seq_exhausted');
|
|
94
|
+
const value = Buffer.alloc(FRAME_HEADER_BYTES);
|
|
95
|
+
value[0] = 1;
|
|
96
|
+
value[1] = type;
|
|
97
|
+
value.writeBigUInt64BE(seq, 4);
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
function nonce(seq) { const value = Buffer.alloc(12); value.writeBigUInt64BE(seq, 4); return value; }
|
|
101
|
+
function validPlaintext(type, plaintext) {
|
|
102
|
+
if (type === FRAME_KEEPALIVE)
|
|
103
|
+
return plaintext.length === 0;
|
|
104
|
+
if (type === FRAME_AUDIO)
|
|
105
|
+
return plaintext.length >= AUDIO_PREFIX_BYTES + 2 && (plaintext.length - AUDIO_PREFIX_BYTES) % 2 === 0;
|
|
106
|
+
if (type === FRAME_CONTROL)
|
|
107
|
+
return plaintext.length >= 2 && plaintext.length <= CONTROL_MAX_BYTES;
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
export function sealFrame(key, type, seq, plaintext) {
|
|
111
|
+
if (key.length !== 32)
|
|
112
|
+
throw new VoiceCryptoError('invalid_key');
|
|
113
|
+
if (!validPlaintext(type, plaintext))
|
|
114
|
+
throw new VoiceCryptoError('invalid_plaintext');
|
|
115
|
+
const head = header(type, seq), cipher = createCipheriv('aes-256-gcm', key, nonce(seq));
|
|
116
|
+
cipher.setAAD(head);
|
|
117
|
+
const body = Buffer.concat([cipher.update(plaintext), cipher.final()]), frame = Buffer.concat([head, body, cipher.getAuthTag()]);
|
|
118
|
+
if (frame.length > FRAME_MAX_BYTES)
|
|
119
|
+
throw new VoiceCryptoError('frame_too_large');
|
|
120
|
+
return frame;
|
|
121
|
+
}
|
|
122
|
+
/** Structural checks before decryption, then AES-256-GCM. Throws VoiceCryptoError. */
|
|
123
|
+
export function openFrame(key, frame) {
|
|
124
|
+
const value = parseHeader(frame);
|
|
125
|
+
const raw = Buffer.from(frame), decipher = createDecipheriv('aes-256-gcm', key, nonce(value.seq));
|
|
126
|
+
decipher.setAAD(raw.subarray(0, FRAME_HEADER_BYTES));
|
|
127
|
+
decipher.setAuthTag(raw.subarray(raw.length - FRAME_TAG_BYTES));
|
|
128
|
+
let plaintext;
|
|
129
|
+
try {
|
|
130
|
+
plaintext = Buffer.concat([decipher.update(raw.subarray(FRAME_HEADER_BYTES, raw.length - FRAME_TAG_BYTES)), decipher.final()]);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
throw new VoiceCryptoError('auth_failed');
|
|
134
|
+
}
|
|
135
|
+
if (!validPlaintext(value.type, plaintext))
|
|
136
|
+
throw new VoiceCryptoError('invalid_plaintext');
|
|
137
|
+
return { type: value.type, seq: value.seq, plaintext };
|
|
138
|
+
}
|
|
139
|
+
export function parseHeader(frame) {
|
|
140
|
+
if (frame.length < FRAME_MIN_BYTES || frame.length > FRAME_MAX_BYTES)
|
|
141
|
+
throw new VoiceCryptoError('invalid_length');
|
|
142
|
+
if (frame[0] !== 1)
|
|
143
|
+
throw new VoiceCryptoError('invalid_version');
|
|
144
|
+
if (frame[1] !== FRAME_AUDIO && frame[1] !== FRAME_CONTROL && frame[1] !== FRAME_KEEPALIVE)
|
|
145
|
+
throw new VoiceCryptoError('invalid_type');
|
|
146
|
+
if (frame[2] !== 0 || frame[3] !== 0)
|
|
147
|
+
throw new VoiceCryptoError('invalid_flags');
|
|
148
|
+
const seq = Buffer.from(frame.buffer, frame.byteOffset, frame.byteLength).readBigUInt64BE(4);
|
|
149
|
+
if (seq > MAX_SEQ)
|
|
150
|
+
throw new VoiceCryptoError('seq_exhausted');
|
|
151
|
+
return { type: frame[1], seq };
|
|
152
|
+
}
|
|
153
|
+
/** One sender per direction: each sequence number is allocated exactly once, including for failed sends. */
|
|
154
|
+
export class FrameSender {
|
|
155
|
+
#next = 0n;
|
|
156
|
+
#key;
|
|
157
|
+
constructor(key) { this.#key = Buffer.from(key); }
|
|
158
|
+
seal(type, plaintext) { const seq = this.#next; if (seq > MAX_SEQ)
|
|
159
|
+
throw new VoiceCryptoError('seq_exhausted'); this.#next = seq + 1n; return sealFrame(this.#key, type, seq, plaintext); }
|
|
160
|
+
get next() { return this.#next; }
|
|
161
|
+
destroy() { this.#key.fill(0); }
|
|
162
|
+
}
|
|
163
|
+
/** Per-direction receive state; it advances only after a frame authenticates. */
|
|
164
|
+
export class FrameReceiver {
|
|
165
|
+
maxConsecutiveFailures;
|
|
166
|
+
#highest = -1n;
|
|
167
|
+
#bitmap = 0n;
|
|
168
|
+
#failures = 0;
|
|
169
|
+
#key;
|
|
170
|
+
constructor(key, maxConsecutiveFailures = 8) {
|
|
171
|
+
this.maxConsecutiveFailures = maxConsecutiveFailures;
|
|
172
|
+
this.#key = Buffer.from(key);
|
|
173
|
+
}
|
|
174
|
+
/** Returns null for a replayed/too-old frame; throws on structure or authentication failure. */
|
|
175
|
+
open(frame) {
|
|
176
|
+
let opened;
|
|
177
|
+
try {
|
|
178
|
+
const { seq } = parseHeader(frame);
|
|
179
|
+
if (this.#seen(seq))
|
|
180
|
+
return null;
|
|
181
|
+
opened = openFrame(this.#key, frame);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
if (++this.#failures > this.maxConsecutiveFailures)
|
|
185
|
+
throw new VoiceCryptoError('voice_crypto_failed');
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
this.#failures = 0;
|
|
189
|
+
this.#accept(opened.seq);
|
|
190
|
+
return opened;
|
|
191
|
+
}
|
|
192
|
+
#seen(seq) {
|
|
193
|
+
if (seq > this.#highest)
|
|
194
|
+
return false;
|
|
195
|
+
const offset = this.#highest - seq;
|
|
196
|
+
if (offset >= REPLAY_WINDOW)
|
|
197
|
+
return true;
|
|
198
|
+
return ((this.#bitmap >> offset) & 1n) === 1n;
|
|
199
|
+
}
|
|
200
|
+
#accept(seq) {
|
|
201
|
+
if (seq > this.#highest) {
|
|
202
|
+
const shift = seq - this.#highest;
|
|
203
|
+
this.#bitmap = shift >= REPLAY_WINDOW ? 1n : ((this.#bitmap << shift) | 1n) & ((1n << REPLAY_WINDOW) - 1n);
|
|
204
|
+
this.#highest = seq;
|
|
205
|
+
}
|
|
206
|
+
else
|
|
207
|
+
this.#bitmap |= 1n << (this.#highest - seq);
|
|
208
|
+
}
|
|
209
|
+
destroy() { this.#key.fill(0); }
|
|
210
|
+
}
|
|
211
|
+
export function encodeAudio(value) {
|
|
212
|
+
if (!Number.isInteger(value.gen) || value.gen < 0 || value.gen > 0xffff)
|
|
213
|
+
throw new VoiceCryptoError('invalid_gen');
|
|
214
|
+
const out = Buffer.alloc(AUDIO_PREFIX_BYTES);
|
|
215
|
+
out.writeUInt16BE(value.gen, 0);
|
|
216
|
+
out.writeUInt32BE(value.nativeSeq, 2);
|
|
217
|
+
out.writeUInt32BE(value.sampleOffset >>> 0, 6);
|
|
218
|
+
return Buffer.concat([out, Buffer.from(value.pcm)]);
|
|
219
|
+
}
|
|
220
|
+
export function decodeAudio(plaintext) {
|
|
221
|
+
const raw = Buffer.from(plaintext);
|
|
222
|
+
if (raw.length < AUDIO_PREFIX_BYTES + 2 || (raw.length - AUDIO_PREFIX_BYTES) % 2 !== 0)
|
|
223
|
+
throw new VoiceCryptoError('invalid_plaintext');
|
|
224
|
+
return { gen: raw.readUInt16BE(0), nativeSeq: raw.readUInt32BE(2), sampleOffset: raw.readUInt32BE(6), pcm: raw.subarray(AUDIO_PREFIX_BYTES) };
|
|
225
|
+
}
|
|
226
|
+
export function encodeControl(value) { const raw = Buffer.from(JSON.stringify(value), 'utf8'); if (raw.length < 2 || raw.length > CONTROL_MAX_BYTES)
|
|
227
|
+
throw new VoiceCryptoError('invalid_plaintext'); return raw; }
|
|
228
|
+
export function decodeControl(plaintext) {
|
|
229
|
+
let value;
|
|
230
|
+
try {
|
|
231
|
+
value = JSON.parse(Buffer.from(plaintext).toString('utf8'));
|
|
232
|
+
}
|
|
233
|
+
catch {
|
|
234
|
+
throw new VoiceCryptoError('invalid_control');
|
|
235
|
+
}
|
|
236
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) || typeof value.type !== 'string')
|
|
237
|
+
throw new VoiceCryptoError('invalid_control');
|
|
238
|
+
return value;
|
|
239
|
+
}
|
|
240
|
+
//# sourceMappingURL=voice-crypto.js.map
|