@escapeaihq/client-core 0.8.0 → 0.42.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/README.md +48 -4
- package/dist/adapters/capabilities.d.ts +10 -0
- package/dist/adapters/capabilities.d.ts.map +1 -0
- package/dist/adapters/capabilities.js +2 -0
- package/dist/adapters/capabilities.js.map +1 -0
- package/dist/adapters/chatAdapter.d.ts +38 -0
- package/dist/adapters/chatAdapter.d.ts.map +1 -0
- package/dist/adapters/chatAdapter.js +88 -0
- package/dist/adapters/chatAdapter.js.map +1 -0
- package/dist/adapters/directorAdapter.d.ts +62 -0
- package/dist/adapters/directorAdapter.d.ts.map +1 -0
- package/dist/adapters/directorAdapter.js +71 -0
- package/dist/adapters/directorAdapter.js.map +1 -0
- package/dist/adapters/imageAdapter.d.ts +44 -0
- package/dist/adapters/imageAdapter.d.ts.map +1 -0
- package/dist/adapters/imageAdapter.js +73 -0
- package/dist/adapters/imageAdapter.js.map +1 -0
- package/dist/adapters/index.d.ts +47 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +41 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/registry.d.ts +98 -0
- package/dist/adapters/registry.d.ts.map +1 -0
- package/dist/adapters/registry.js +106 -0
- package/dist/adapters/registry.js.map +1 -0
- package/dist/adapters/worldAdapter.d.ts +142 -0
- package/dist/adapters/worldAdapter.d.ts.map +1 -0
- package/dist/adapters/worldAdapter.js +164 -0
- package/dist/adapters/worldAdapter.js.map +1 -0
- package/dist/admin.d.ts +200 -0
- package/dist/admin.d.ts.map +1 -0
- package/dist/admin.js +124 -0
- package/dist/admin.js.map +1 -0
- package/dist/api.d.ts +1299 -3
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +683 -95
- package/dist/api.js.map +1 -1
- package/dist/audio.d.ts +33 -1
- package/dist/audio.d.ts.map +1 -1
- package/dist/audio.js +4 -1
- package/dist/audio.js.map +1 -1
- package/dist/castProjection.d.ts +35 -0
- package/dist/castProjection.d.ts.map +1 -0
- package/dist/castProjection.js +80 -0
- package/dist/castProjection.js.map +1 -0
- package/dist/conversation/binding.d.ts +62 -0
- package/dist/conversation/binding.d.ts.map +1 -0
- package/dist/conversation/binding.js +51 -0
- package/dist/conversation/binding.js.map +1 -0
- package/dist/conversation/controller.d.ts +209 -0
- package/dist/conversation/controller.d.ts.map +1 -0
- package/dist/conversation/controller.js +342 -0
- package/dist/conversation/controller.js.map +1 -0
- package/dist/conversation/effectiveModel.d.ts +62 -0
- package/dist/conversation/effectiveModel.d.ts.map +1 -0
- package/dist/conversation/effectiveModel.js +73 -0
- package/dist/conversation/effectiveModel.js.map +1 -0
- package/dist/conversation/index.d.ts +27 -0
- package/dist/conversation/index.d.ts.map +1 -0
- package/dist/conversation/index.js +27 -0
- package/dist/conversation/index.js.map +1 -0
- package/dist/conversation/participant.d.ts +66 -0
- package/dist/conversation/participant.d.ts.map +1 -0
- package/dist/conversation/participant.js +36 -0
- package/dist/conversation/participant.js.map +1 -0
- package/dist/conversation/presence.d.ts +56 -0
- package/dist/conversation/presence.d.ts.map +1 -0
- package/dist/conversation/presence.js +34 -0
- package/dist/conversation/presence.js.map +1 -0
- package/dist/conversation/presenceBus.d.ts +42 -0
- package/dist/conversation/presenceBus.d.ts.map +1 -0
- package/dist/conversation/presenceBus.js +81 -0
- package/dist/conversation/presenceBus.js.map +1 -0
- package/dist/conversation/presenceRegistry.d.ts +49 -0
- package/dist/conversation/presenceRegistry.d.ts.map +1 -0
- package/dist/conversation/presenceRegistry.js +59 -0
- package/dist/conversation/presenceRegistry.js.map +1 -0
- package/dist/conversation/roster.d.ts +72 -0
- package/dist/conversation/roster.d.ts.map +1 -0
- package/dist/conversation/roster.js +69 -0
- package/dist/conversation/roster.js.map +1 -0
- package/dist/conversation/rosterProjection.d.ts +52 -0
- package/dist/conversation/rosterProjection.d.ts.map +1 -0
- package/dist/conversation/rosterProjection.js +74 -0
- package/dist/conversation/rosterProjection.js.map +1 -0
- package/dist/conversation/router.d.ts +74 -0
- package/dist/conversation/router.d.ts.map +1 -0
- package/dist/conversation/router.js +58 -0
- package/dist/conversation/router.js.map +1 -0
- package/dist/conversation/runtime.d.ts +64 -0
- package/dist/conversation/runtime.d.ts.map +1 -0
- package/dist/conversation/runtime.js +172 -0
- package/dist/conversation/runtime.js.map +1 -0
- package/dist/conversation/types.d.ts +99 -0
- package/dist/conversation/types.d.ts.map +1 -0
- package/dist/conversation/types.js +13 -0
- package/dist/conversation/types.js.map +1 -0
- package/dist/conversation/voice.d.ts +185 -0
- package/dist/conversation/voice.d.ts.map +1 -0
- package/dist/conversation/voice.js +307 -0
- package/dist/conversation/voice.js.map +1 -0
- package/dist/graphSchema.d.ts +260 -0
- package/dist/graphSchema.d.ts.map +1 -0
- package/dist/graphSchema.js +354 -0
- package/dist/graphSchema.js.map +1 -0
- package/dist/http.d.ts +20 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +57 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -1
- package/dist/platformAssistant.d.ts +155 -0
- package/dist/platformAssistant.d.ts.map +1 -0
- package/dist/platformAssistant.js +256 -0
- package/dist/platformAssistant.js.map +1 -0
- package/dist/reactor/abortUtils.d.ts +16 -0
- package/dist/reactor/abortUtils.d.ts.map +1 -0
- package/dist/reactor/abortUtils.js +26 -0
- package/dist/reactor/abortUtils.js.map +1 -0
- package/dist/reactor/applyScenePrompt.d.ts +75 -0
- package/dist/reactor/applyScenePrompt.d.ts.map +1 -0
- package/dist/reactor/applyScenePrompt.js +60 -0
- package/dist/reactor/applyScenePrompt.js.map +1 -0
- package/dist/reactor/chunkDistribution.d.ts +43 -0
- package/dist/reactor/chunkDistribution.d.ts.map +1 -0
- package/dist/reactor/chunkDistribution.js +85 -0
- package/dist/reactor/chunkDistribution.js.map +1 -0
- package/dist/reactor/heliosCommandDispatcher.d.ts.map +1 -1
- package/dist/reactor/heliosCommandDispatcher.js +10 -15
- package/dist/reactor/heliosCommandDispatcher.js.map +1 -1
- package/dist/reactor/index.d.ts +4 -0
- package/dist/reactor/index.d.ts.map +1 -1
- package/dist/reactor/index.js +7 -0
- package/dist/reactor/index.js.map +1 -1
- package/dist/reactor/longLiveCommandDispatcher.d.ts +103 -0
- package/dist/reactor/longLiveCommandDispatcher.d.ts.map +1 -0
- package/dist/reactor/longLiveCommandDispatcher.js +87 -0
- package/dist/reactor/longLiveCommandDispatcher.js.map +1 -0
- package/dist/reactor/sceneEntry.d.ts +40 -6
- package/dist/reactor/sceneEntry.d.ts.map +1 -1
- package/dist/reactor/sceneEntry.js +63 -24
- package/dist/reactor/sceneEntry.js.map +1 -1
- package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -1
- package/dist/reactor/scenesFromEnrichment.js +2 -66
- package/dist/reactor/scenesFromEnrichment.js.map +1 -1
- package/dist/reactor/seedImageOrchestrator.d.ts +42 -2
- package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -1
- package/dist/reactor/seedImageOrchestrator.js +173 -4
- package/dist/reactor/seedImageOrchestrator.js.map +1 -1
- package/dist/reactor/storyTimeline.d.ts +44 -0
- package/dist/reactor/storyTimeline.d.ts.map +1 -0
- package/dist/reactor/storyTimeline.js +95 -0
- package/dist/reactor/storyTimeline.js.map +1 -0
- package/dist/storyGraph.d.ts +70 -0
- package/dist/storyGraph.d.ts.map +1 -0
- package/dist/storyGraph.js +178 -0
- package/dist/storyGraph.js.map +1 -0
- package/dist/timeFormat.d.ts +10 -0
- package/dist/timeFormat.d.ts.map +1 -1
- package/dist/timeFormat.js +12 -0
- package/dist/timeFormat.js.map +1 -1
- package/dist/toolSchemas.d.ts +370 -0
- package/dist/toolSchemas.d.ts.map +1 -0
- package/dist/toolSchemas.js +182 -0
- package/dist/toolSchemas.js.map +1 -0
- package/dist/vision/VisionPublisher.d.ts +115 -0
- package/dist/vision/VisionPublisher.d.ts.map +1 -0
- package/dist/vision/VisionPublisher.js +475 -0
- package/dist/vision/VisionPublisher.js.map +1 -0
- package/dist/vision/VisionSourceController.d.ts +127 -0
- package/dist/vision/VisionSourceController.d.ts.map +1 -0
- package/dist/vision/VisionSourceController.js +284 -0
- package/dist/vision/VisionSourceController.js.map +1 -0
- package/dist/vision/credentials.d.ts +22 -0
- package/dist/vision/credentials.d.ts.map +1 -0
- package/dist/vision/credentials.js +34 -0
- package/dist/vision/credentials.js.map +1 -0
- package/dist/vision/index.d.ts +18 -0
- package/dist/vision/index.d.ts.map +1 -0
- package/dist/vision/index.js +13 -0
- package/dist/vision/index.js.map +1 -0
- package/dist/vision/sourceKinds.d.ts +65 -0
- package/dist/vision/sourceKinds.d.ts.map +1 -0
- package/dist/vision/sourceKinds.js +26 -0
- package/dist/vision/sourceKinds.js.map +1 -0
- package/dist/vision/startVisionPublisher.d.ts +38 -0
- package/dist/vision/startVisionPublisher.d.ts.map +1 -0
- package/dist/vision/startVisionPublisher.js +44 -0
- package/dist/vision/startVisionPublisher.js.map +1 -0
- package/dist/vision/types.d.ts +95 -0
- package/dist/vision/types.d.ts.map +1 -0
- package/dist/vision/types.js +12 -0
- package/dist/vision/types.js.map +1 -0
- package/dist/vision/visionScheduler.d.ts +63 -0
- package/dist/vision/visionScheduler.d.ts.map +1 -0
- package/dist/vision/visionScheduler.js +67 -0
- package/dist/vision/visionScheduler.js.map +1 -0
- package/dist/websocket.d.ts +195 -0
- package/dist/websocket.d.ts.map +1 -1
- package/dist/websocket.js +179 -1
- package/dist/websocket.js.map +1 -1
- package/package.json +11 -1
- package/src/__tests__/adapters-capabilities.test.ts +46 -0
- package/src/__tests__/adapters-chat.test.ts +172 -0
- package/src/__tests__/adapters-director.test.ts +95 -0
- package/src/__tests__/adapters-image.test.ts +133 -0
- package/src/__tests__/adapters-platform.test.ts +95 -0
- package/src/__tests__/adapters-registry.test.ts +189 -0
- package/src/__tests__/adapters-world.test.ts +296 -0
- package/src/__tests__/admin.test.ts +312 -0
- package/src/__tests__/adventures-party.test.ts +219 -0
- package/src/__tests__/adventures.test.ts +282 -0
- package/src/__tests__/api-pollers.test.ts +104 -0
- package/src/__tests__/api.clip-lookup.test.ts +64 -0
- package/src/__tests__/api.list-images-filters.test.ts +103 -0
- package/src/__tests__/api.test.ts +470 -0
- package/src/__tests__/audio.test.ts +65 -0
- package/src/__tests__/cast.test.ts +95 -0
- package/src/__tests__/castProjection.test.ts +167 -0
- package/src/__tests__/chat-auth.test.ts +91 -0
- package/src/__tests__/conversation/controller.test.ts +697 -0
- package/src/__tests__/conversation/effectiveModel.test.ts +169 -0
- package/src/__tests__/conversation/headlessRenderer.ts +46 -0
- package/src/__tests__/conversation/presenceBus.test.ts +236 -0
- package/src/__tests__/conversation/presenceRegistry.test.ts +73 -0
- package/src/__tests__/conversation/roster.test.ts +100 -0
- package/src/__tests__/conversation/rosterProjection.test.ts +142 -0
- package/src/__tests__/conversation/router.test.ts +81 -0
- package/src/__tests__/conversation/runtime.test.ts +206 -0
- package/src/__tests__/conversation/voice.test.ts +440 -0
- package/src/__tests__/documents.test.ts +360 -0
- package/src/__tests__/graphSchema.test.ts +326 -0
- package/src/__tests__/longLiveCommandDispatcher.test.ts +185 -0
- package/src/__tests__/platformAssistant.test.ts +484 -0
- package/src/__tests__/reactor/applyScenePrompt.test.ts +192 -0
- package/src/__tests__/reactor/heliosCommandDispatcher.test.ts +4 -3
- package/src/__tests__/reactor/sceneEntry.test.ts +134 -10
- package/src/__tests__/reactor/seedImageOrchestrator.test.ts +51 -16
- package/src/__tests__/reactor/storyTimeline.test.ts +186 -0
- package/src/__tests__/regeneration.test.ts +486 -0
- package/src/__tests__/stories.test.ts +171 -0
- package/src/__tests__/storyGraph.test.ts +134 -0
- package/src/__tests__/timeFormat.test.ts +22 -1
- package/src/__tests__/toolSchemas.test.ts +57 -0
- package/src/__tests__/tools-wrappers.test.ts +272 -0
- package/src/__tests__/videoGraph.test.ts +94 -0
- package/src/__tests__/websocket-vision.test.ts +265 -0
- package/src/__tests__/websocket.test.ts +213 -0
- package/src/adapters/capabilities.ts +24 -0
- package/src/adapters/chatAdapter.ts +114 -0
- package/src/adapters/directorAdapter.ts +126 -0
- package/src/adapters/imageAdapter.ts +100 -0
- package/src/adapters/index.ts +67 -0
- package/src/adapters/registry.ts +159 -0
- package/src/adapters/worldAdapter.ts +272 -0
- package/src/admin.ts +310 -0
- package/src/api.ts +1890 -162
- package/src/audio.ts +35 -0
- package/src/castProjection.ts +95 -0
- package/src/conversation/binding.ts +81 -0
- package/src/conversation/controller.ts +439 -0
- package/src/conversation/effectiveModel.ts +111 -0
- package/src/conversation/index.ts +27 -0
- package/src/conversation/participant.ts +71 -0
- package/src/conversation/presence.ts +60 -0
- package/src/conversation/presenceBus.ts +109 -0
- package/src/conversation/presenceRegistry.ts +83 -0
- package/src/conversation/roster.ts +111 -0
- package/src/conversation/rosterProjection.ts +98 -0
- package/src/conversation/router.ts +96 -0
- package/src/conversation/runtime.ts +188 -0
- package/src/conversation/types.ts +100 -0
- package/src/conversation/voice.ts +402 -0
- package/src/graphSchema.ts +532 -0
- package/src/http.ts +72 -0
- package/src/index.ts +12 -0
- package/src/platformAssistant.ts +347 -0
- package/src/reactor/abortUtils.ts +22 -0
- package/src/reactor/applyScenePrompt.ts +136 -0
- package/src/reactor/chunkDistribution.ts +98 -0
- package/src/reactor/heliosCommandDispatcher.ts +10 -18
- package/src/reactor/index.ts +7 -0
- package/src/reactor/longLiveCommandDispatcher.ts +156 -0
- package/src/reactor/sceneEntry.ts +104 -33
- package/src/reactor/scenesFromEnrichment.ts +11 -76
- package/src/reactor/seedImageOrchestrator.ts +200 -4
- package/src/reactor/storyTimeline.ts +127 -0
- package/src/storyGraph.ts +298 -0
- package/src/timeFormat.ts +13 -0
- package/src/toolSchemas.ts +225 -0
- package/src/vision/VisionPublisher.ts +553 -0
- package/src/vision/VisionSourceController.ts +354 -0
- package/src/vision/__tests__/VisionPublisher.kvs.test.ts +244 -0
- package/src/vision/__tests__/VisionPublisher.test.ts +301 -0
- package/src/vision/__tests__/VisionSourceController.test.ts +719 -0
- package/src/vision/__tests__/startVisionPublisher.test.ts +251 -0
- package/src/vision/__tests__/toVisionRtcCredentials.test.ts +113 -0
- package/src/vision/__tests__/visionScheduler.test.ts +149 -0
- package/src/vision/credentials.ts +39 -0
- package/src/vision/index.ts +44 -0
- package/src/vision/sourceKinds.ts +75 -0
- package/src/vision/startVisionPublisher.ts +64 -0
- package/src/vision/types.ts +101 -0
- package/src/vision/visionScheduler.ts +102 -0
- package/src/websocket.ts +334 -4
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `VoiceSession` (R7.5) — the framework-agnostic voice pipeline for the
|
|
3
|
+
* Conversation overlay.
|
|
4
|
+
*
|
|
5
|
+
* The R2.3/R2.4 Conversation overlay was TEXT-only (the controller's hello is
|
|
6
|
+
* `mode:'text'`; tech-debt #441). R7.5 lifts the FOUR page-owned voice pieces
|
|
7
|
+
* that live inside the 3,000-line `web/app/chat-ws/page.tsx` — the voice-mode
|
|
8
|
+
* `tts_stt` hello, mic capture (STT), audio playback + barge-in (TTS), and
|
|
9
|
+
* viseme scheduling — into ONE module that drives the SAME `ChatWebSocket` the
|
|
10
|
+
* `ConversationController` owns/adopts.
|
|
11
|
+
*
|
|
12
|
+
* DRY: this is a THIN composition, not a re-implementation. The
|
|
13
|
+
* player/viseme/barge-in/sentence-anchoring machinery already lives in
|
|
14
|
+
* `AudioPipeline` (audio.ts) and the hello in the single `buildHelloPayload`.
|
|
15
|
+
* The mic is the existing `MicrophoneRecorder`. `VoiceSession` only owns the
|
|
16
|
+
* lifecycle (the iOS user-gesture priming order, the socket handler wiring, and
|
|
17
|
+
* the dispose contract). There is NO second viseme scheduler, NO second
|
|
18
|
+
* hello-construction site, and NO second sentence-anchoring loop.
|
|
19
|
+
*
|
|
20
|
+
* Viseme delivery: `onViseme` is the SINGLE viseme callback. The React panel
|
|
21
|
+
* routes it into the presence face, which is the React realisation of R7.3's
|
|
22
|
+
* `PresenceAdapter.applyViseme` — visemes reach a face through exactly one path.
|
|
23
|
+
*
|
|
24
|
+
* Socket ownership: `VoiceSession` NEVER closes the socket on `dispose()` — the
|
|
25
|
+
* `ConversationController` (or the surface that owns an adopted socket) manages
|
|
26
|
+
* the transport. This preserves the Watch shared-socket invariant: voice on an
|
|
27
|
+
* adopted socket must not tear down the one `FrameBuffer` vision rides.
|
|
28
|
+
*
|
|
29
|
+
* `/chat-ws` is the parity ORACLE and is NOT rewired by R7.5: its page-owned
|
|
30
|
+
* pipeline stays verbatim, and `buildHello()` here is fixture-diffed against
|
|
31
|
+
* `startVoiceChat()`'s payload so the overlay's `tts_stt` hello is byte-identical.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import {
|
|
35
|
+
AudioPipeline,
|
|
36
|
+
MicrophoneRecorder,
|
|
37
|
+
buildHelloPayload,
|
|
38
|
+
type HelloPayload,
|
|
39
|
+
} from '../audio';
|
|
40
|
+
import type { AudioFormat } from '../audioProtocol';
|
|
41
|
+
import type { ChatWebSocket, WebSocketMessage } from '../websocket';
|
|
42
|
+
import type { HelloArgs } from './binding';
|
|
43
|
+
import type { VisemeSymbol } from '../visemeScheduler';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The audio knobs `startVoiceChat()` passes into `buildHelloPayload` (page.tsx
|
|
47
|
+
* lines 1829-1858). Context fields (`videoId`/`personaId`/`entityId`/
|
|
48
|
+
* `adventureSessionId`/`partyPersonaIds`/`modelId`) come from `getHelloArgs`,
|
|
49
|
+
* NOT from here — they flow through the single `toHelloArgs` mapper the
|
|
50
|
+
* controller owns. Everything here is a voice-only setting. All optional: an
|
|
51
|
+
* omitted field falls to `buildHelloPayload`'s own default (the one place
|
|
52
|
+
* defaults live), exactly as the page relies on.
|
|
53
|
+
*/
|
|
54
|
+
export interface VoiceSessionOptions {
|
|
55
|
+
audioFormat?: AudioFormat;
|
|
56
|
+
enableAudioMarkups?: boolean;
|
|
57
|
+
voiceTemperature?: number;
|
|
58
|
+
speakingRate?: number;
|
|
59
|
+
voiceId?: string;
|
|
60
|
+
ttsModelId?: string;
|
|
61
|
+
systemPrompt?: string;
|
|
62
|
+
systemPromptOverride?: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface VoiceSessionCallbacks {
|
|
66
|
+
/**
|
|
67
|
+
* The single viseme delivery sink. Receives every scheduled viseme value
|
|
68
|
+
* from the internal `AudioPipeline`'s `VisemeScheduler` and is expected to
|
|
69
|
+
* route it onto a face via R7.3's `PresenceAdapter.applyViseme` (in React,
|
|
70
|
+
* the self-driving panel's presence face). This is the ONLY viseme path.
|
|
71
|
+
*/
|
|
72
|
+
onViseme: (viseme: VisemeSymbol) => void;
|
|
73
|
+
/** A final STT transcript of the user's speech (the panel appends a user
|
|
74
|
+
* turn). `final=false` is the interim partial (the live transcript bar). */
|
|
75
|
+
onTranscript?: (text: string, final: boolean) => void;
|
|
76
|
+
/** AI speaking flag transitions (gates the barge-in affordance). */
|
|
77
|
+
onSpeakingChange?: (speaking: boolean) => void;
|
|
78
|
+
/** Human-readable error surfaced to the panel's inline error banner. */
|
|
79
|
+
onError?: (message: string) => void;
|
|
80
|
+
/** Optional human-readable status line (mic priming / streaming / paused). */
|
|
81
|
+
onStatus?: (status: string) => void;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface VoiceSessionConfig extends VoiceSessionCallbacks {
|
|
85
|
+
/**
|
|
86
|
+
* The conversation's `ChatWebSocket` — the SAME one the controller owns or
|
|
87
|
+
* adopted. `VoiceSession` drives hello/audio frames onto it and subscribes
|
|
88
|
+
* to its message/binary streams, but NEVER closes it.
|
|
89
|
+
*/
|
|
90
|
+
socket: ChatWebSocket;
|
|
91
|
+
/**
|
|
92
|
+
* Returns the context + active-participant args for the hello (the
|
|
93
|
+
* controller's `toHelloArgs()` spread with the active participant's
|
|
94
|
+
* `personaId`/`modelId`). Called at `start()` time so the hello reflects the
|
|
95
|
+
* live binding. Voice knobs are layered on top from {@link VoiceSessionOptions}.
|
|
96
|
+
*/
|
|
97
|
+
getHelloArgs: () => HelloArgs;
|
|
98
|
+
/** The voice-only hello knobs (see {@link VoiceSessionOptions}). */
|
|
99
|
+
voiceOptions?: VoiceSessionOptions;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Drives a full voice round-trip (STT in, TTS + visemes out) over a shared
|
|
104
|
+
* `ChatWebSocket`. Construct one per conversation; dispose it with the
|
|
105
|
+
* conversation. The socket is NOT owned here.
|
|
106
|
+
*/
|
|
107
|
+
export class VoiceSession {
|
|
108
|
+
private readonly socket: ChatWebSocket;
|
|
109
|
+
private readonly getHelloArgs: () => HelloArgs;
|
|
110
|
+
private readonly voiceOptions: VoiceSessionOptions;
|
|
111
|
+
private readonly callbacks: VoiceSessionCallbacks;
|
|
112
|
+
|
|
113
|
+
private pipeline: AudioPipeline | null = null;
|
|
114
|
+
private recorder: MicrophoneRecorder | null = null;
|
|
115
|
+
|
|
116
|
+
/** True once `hello_ack` lands — gates outbound mic frames so they are never
|
|
117
|
+
* sent into a server that has not wired up its receive side yet (the page's
|
|
118
|
+
* `audioReadyRef` contract). */
|
|
119
|
+
private audioReady = false;
|
|
120
|
+
/** True between `start()` and `stop()`/`dispose()`. Gates reconnect re-send. */
|
|
121
|
+
private active = false;
|
|
122
|
+
private disposed = false;
|
|
123
|
+
|
|
124
|
+
/** The last hello sent, so a reconnect can re-send it (the page's
|
|
125
|
+
* `lastHelloRef`). Exposed via {@link reconnectHello}. */
|
|
126
|
+
private lastHello: HelloPayload | null = null;
|
|
127
|
+
|
|
128
|
+
private readonly messageHandler = (message: WebSocketMessage): void =>
|
|
129
|
+
this.handleMessage(message);
|
|
130
|
+
|
|
131
|
+
private readonly binaryHandler = (data: ArrayBuffer): void =>
|
|
132
|
+
this.handleBinary(data);
|
|
133
|
+
|
|
134
|
+
constructor(config: VoiceSessionConfig) {
|
|
135
|
+
this.socket = config.socket;
|
|
136
|
+
this.getHelloArgs = config.getHelloArgs;
|
|
137
|
+
this.voiceOptions = config.voiceOptions ?? {};
|
|
138
|
+
this.callbacks = {
|
|
139
|
+
onViseme: config.onViseme,
|
|
140
|
+
onTranscript: config.onTranscript,
|
|
141
|
+
onSpeakingChange: config.onSpeakingChange,
|
|
142
|
+
onError: config.onError,
|
|
143
|
+
onStatus: config.onStatus,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Build the voice (`tts_stt`) hello. The SINGLE hello-builder: the context +
|
|
149
|
+
* active-participant args come from `getHelloArgs()` (which itself flows
|
|
150
|
+
* through `toHelloArgs`), the voice knobs from `voiceOptions`, and `mode` is
|
|
151
|
+
* forced to `'tts_stt'` — fed to the one `buildHelloPayload`. NO second
|
|
152
|
+
* construction site. This is the parity oracle target: for identical inputs
|
|
153
|
+
* it is byte-identical to the page's `startVoiceChat()` payload.
|
|
154
|
+
*/
|
|
155
|
+
buildHello(): HelloPayload {
|
|
156
|
+
return buildHelloPayload({
|
|
157
|
+
...this.getHelloArgs(),
|
|
158
|
+
mode: 'tts_stt',
|
|
159
|
+
...this.voiceOptions,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** True while a voice session is running (between `start()` and stop). */
|
|
164
|
+
get isActive(): boolean {
|
|
165
|
+
return this.active;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Start voice. Mirrors `startVoiceChat()` (page.tsx:1770-1873) EXACTLY,
|
|
170
|
+
* including the iOS Safari user-gesture audio-activation order: the
|
|
171
|
+
* `AudioPipeline` (which creates the player's AudioContext) and the
|
|
172
|
+
* `MicrophoneRecorder.prime()` run in the SYNCHRONOUS prefix BEFORE the first
|
|
173
|
+
* await — an async wrapper around these silently breaks Safari mic activation.
|
|
174
|
+
* Callers MUST invoke `start()` from the synchronous prefix of the overlay
|
|
175
|
+
* button's click handler.
|
|
176
|
+
*/
|
|
177
|
+
async start(): Promise<void> {
|
|
178
|
+
if (this.disposed) {
|
|
179
|
+
throw new Error('VoiceSession: start() after dispose().');
|
|
180
|
+
}
|
|
181
|
+
if (!this.socket.isConnected) {
|
|
182
|
+
this.callbacks.onError?.('WebSocket connection required before starting voice chat.');
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (this.active) return;
|
|
186
|
+
|
|
187
|
+
// ─── iOS Safari user-gesture audio activation (synchronous prefix) ───
|
|
188
|
+
// Both AudioContexts (player + recorder) MUST be created and resume()'d
|
|
189
|
+
// inside the synchronous prefix of the click handler — see page.tsx:1776.
|
|
190
|
+
if (!this.pipeline) {
|
|
191
|
+
this.pipeline = new AudioPipeline((viseme) =>
|
|
192
|
+
this.callbacks.onViseme(viseme as VisemeSymbol),
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
void this.pipeline.resume();
|
|
196
|
+
const recorder = new MicrophoneRecorder(
|
|
197
|
+
(frame) => {
|
|
198
|
+
if (!this.audioReady) return;
|
|
199
|
+
try {
|
|
200
|
+
this.socket.sendAudioFrame(frame);
|
|
201
|
+
} catch (err) {
|
|
202
|
+
console.error('Failed to send audio frame', err);
|
|
203
|
+
this.callbacks.onError?.('Unable to stream audio frame to server.');
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
(message) => this.callbacks.onError?.(message),
|
|
207
|
+
);
|
|
208
|
+
recorder.prime();
|
|
209
|
+
this.recorder = recorder;
|
|
210
|
+
|
|
211
|
+
// Subscribe to the socket BEFORE the hello so no early frame is missed.
|
|
212
|
+
this.socket.onMessage(this.messageHandler);
|
|
213
|
+
this.socket.onBinary(this.binaryHandler);
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
this.callbacks.onStatus?.('Requesting microphone permission...');
|
|
217
|
+
this.audioReady = false;
|
|
218
|
+
|
|
219
|
+
// Safety-net await for non-iOS browsers (page.tsx:1809-1811).
|
|
220
|
+
await this.pipeline.resume();
|
|
221
|
+
|
|
222
|
+
// Barge-in: attach the session against the raw socket + start the
|
|
223
|
+
// playback-position tracker (page.tsx:1813-1827). `attachSession`
|
|
224
|
+
// wires the player; tracking makes the server's barge-in arbitration
|
|
225
|
+
// work against the real playhead.
|
|
226
|
+
const rawWs = this.socket.getRawSocket();
|
|
227
|
+
if (rawWs) {
|
|
228
|
+
this.pipeline.attachSession(rawWs);
|
|
229
|
+
this.pipeline.sessionClient?.startPlaybackTracking();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const hello = this.buildHello();
|
|
233
|
+
this.lastHello = hello;
|
|
234
|
+
this.socket.sendHello(hello);
|
|
235
|
+
|
|
236
|
+
await recorder.start();
|
|
237
|
+
this.active = true;
|
|
238
|
+
this.callbacks.onStatus?.(
|
|
239
|
+
'Microphone streaming. Say something to begin transcription.',
|
|
240
|
+
);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
// A failed start (typically a denied mic permission from
|
|
243
|
+
// `recorder.start()`) must NOT resolve silently: the panel flips
|
|
244
|
+
// `isVoiceActive` true BEFORE calling start() and only reverts it on a
|
|
245
|
+
// rejection, so swallowing here would strand the UI showing Stop/Interrupt
|
|
246
|
+
// while no session runs (codex P2). Tear down + surface the error AND
|
|
247
|
+
// rethrow so the caller's `.catch()` clears its active state.
|
|
248
|
+
console.error('Failed to start voice chat', err);
|
|
249
|
+
this.callbacks.onError?.(
|
|
250
|
+
'Unable to start voice chat. Please check microphone permissions.',
|
|
251
|
+
);
|
|
252
|
+
this.resetAudio();
|
|
253
|
+
throw err;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Stop voice capture/playback but keep the session reusable. Mirrors the
|
|
259
|
+
* page's `stopVoiceChat()` + `resetAudioState()`. Detaches socket handlers
|
|
260
|
+
* and tears down the audio resources; the socket itself is left OPEN (the
|
|
261
|
+
* owner manages it).
|
|
262
|
+
*/
|
|
263
|
+
stop(): void {
|
|
264
|
+
this.active = false;
|
|
265
|
+
this.lastHello = null;
|
|
266
|
+
this.resetAudio();
|
|
267
|
+
this.callbacks.onStatus?.('Voice capture paused.');
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Barge-in: interrupt the assistant's current audio. Mirrors the page's
|
|
272
|
+
* `handleInterrupt()` — delegates to the pipeline's `bargeIn()` (which sends
|
|
273
|
+
* the server `barge_in`, clears the player queue + visemes) and flips the
|
|
274
|
+
* speaking flag off. No-op when nothing is playing.
|
|
275
|
+
*/
|
|
276
|
+
async interrupt(): Promise<void> {
|
|
277
|
+
if (!this.pipeline) return;
|
|
278
|
+
try {
|
|
279
|
+
await this.pipeline.bargeIn();
|
|
280
|
+
this.callbacks.onSpeakingChange?.(false);
|
|
281
|
+
} catch (err) {
|
|
282
|
+
console.error('Failed to interrupt playback', err);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Re-send the voice hello after a socket reconnect, IF a voice session is
|
|
288
|
+
* active. Returns the payload (so a host that owns socket construction can
|
|
289
|
+
* wire this into `ChatWebSocket`'s `onReconnect` option) or `null` when voice
|
|
290
|
+
* is inactive (text reconnect — nothing to re-send). NOTE: it does NOT
|
|
291
|
+
* re-prime the mic/AudioSession (the page does, but that path is iOS-gesture
|
|
292
|
+
* gapped on an event-loop reconnect and requires the host to own socket
|
|
293
|
+
* construction — tracked as a follow-up). The hello re-send alone restores
|
|
294
|
+
* the server-side voice routing for the existing recorder's frames.
|
|
295
|
+
*/
|
|
296
|
+
reconnectHello(): HelloPayload | null {
|
|
297
|
+
if (!this.active || !this.lastHello) return null;
|
|
298
|
+
return this.lastHello;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Tear down. Stops audio + detaches handlers, then becomes inert. NEVER
|
|
303
|
+
* closes the socket — the controller / adopting surface owns the transport
|
|
304
|
+
* (the Watch shared-socket invariant). Idempotent.
|
|
305
|
+
*/
|
|
306
|
+
dispose(): void {
|
|
307
|
+
if (this.disposed) return;
|
|
308
|
+
this.disposed = true;
|
|
309
|
+
this.active = false;
|
|
310
|
+
this.lastHello = null;
|
|
311
|
+
this.resetAudio();
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// ── internals ──────────────────────────────────────────────────────────
|
|
315
|
+
|
|
316
|
+
/** Tear down audio resources + detach socket handlers, leaving the socket
|
|
317
|
+
* open. Idempotent; mirrors the page's `resetAudioState`. */
|
|
318
|
+
private resetAudio(): void {
|
|
319
|
+
this.socket.offMessage(this.messageHandler);
|
|
320
|
+
this.socket.offBinary(this.binaryHandler);
|
|
321
|
+
this.recorder?.stop();
|
|
322
|
+
this.recorder = null;
|
|
323
|
+
if (this.pipeline) {
|
|
324
|
+
void this.pipeline.destroy();
|
|
325
|
+
this.pipeline = null;
|
|
326
|
+
}
|
|
327
|
+
this.audioReady = false;
|
|
328
|
+
this.callbacks.onSpeakingChange?.(false);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Reset per-turn audio state for a new assistant reply. Mirrors the page's
|
|
333
|
+
* new-assistant-turn reset (page.tsx:1244-1251): the pipeline's barge-in /
|
|
334
|
+
* sentence-anchor state (`AudioPipeline.resetTurn`), the player's
|
|
335
|
+
* `playbackOrigin` (so playback positions for the next reply are 0-based), and
|
|
336
|
+
* the viseme scheduler (so a stale tail doesn't bleed into the new turn). The
|
|
337
|
+
* pipeline's `player`/`visemeScheduler` are public readonly handles. No-op
|
|
338
|
+
* before the first `start()` (no pipeline yet).
|
|
339
|
+
*/
|
|
340
|
+
private resetTurn(): void {
|
|
341
|
+
if (!this.pipeline) return;
|
|
342
|
+
this.pipeline.resetTurn();
|
|
343
|
+
this.pipeline.player.playbackOrigin = 0;
|
|
344
|
+
this.pipeline.visemeScheduler.clear();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private handleMessage(message: WebSocketMessage): void {
|
|
348
|
+
switch (message.type) {
|
|
349
|
+
case 'hello_ack': {
|
|
350
|
+
this.audioReady = true;
|
|
351
|
+
this.callbacks.onStatus?.(
|
|
352
|
+
'Audio handshake acknowledged. Streaming microphone frames.',
|
|
353
|
+
);
|
|
354
|
+
this.callbacks.onSpeakingChange?.(false);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
case 'transcript': {
|
|
358
|
+
this.audioReady = true;
|
|
359
|
+
const text = message.text?.trim() ?? '';
|
|
360
|
+
if (message.final) {
|
|
361
|
+
// A final transcript begins a NEW assistant turn. Reset the
|
|
362
|
+
// pipeline/player turn state BEFORE the new reply's audio is tracked
|
|
363
|
+
// — the backend resets its barge-in timestamps to 0 for every
|
|
364
|
+
// `_process_turn`, so without this the client keeps reporting
|
|
365
|
+
// playback positions relative to the FIRST audio frame of the whole
|
|
366
|
+
// session and a later-turn Interrupt makes the server think the reply
|
|
367
|
+
// was already fully spoken (codex P2; mirrors page.tsx:1244-1251).
|
|
368
|
+
this.resetTurn();
|
|
369
|
+
if (text) this.callbacks.onTranscript?.(text, true);
|
|
370
|
+
} else {
|
|
371
|
+
this.callbacks.onTranscript?.(text, false);
|
|
372
|
+
}
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
case 'audio_metadata': {
|
|
376
|
+
// Visemes + word timestamps. The pipeline buffers metadata that races
|
|
377
|
+
// ahead of binary frames (Inworld ASYNC mode) and schedules visemes
|
|
378
|
+
// through the SINGLE scheduler → `onViseme`.
|
|
379
|
+
this.pipeline?.handleAudioMetadata(
|
|
380
|
+
message as unknown as Parameters<AudioPipeline['handleAudioMetadata']>[0],
|
|
381
|
+
);
|
|
382
|
+
this.callbacks.onSpeakingChange?.(true);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
case 'barge_in_ack': {
|
|
386
|
+
this.callbacks.onSpeakingChange?.(false);
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
default:
|
|
390
|
+
// token/citation/image/done/error/turn_changed/tool_result/metrics —
|
|
391
|
+
// the panel's text consumer owns those; VoiceSession only handles the
|
|
392
|
+
// audio-pipeline frames.
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
private handleBinary(data: ArrayBuffer): void {
|
|
398
|
+
if (!this.pipeline) return;
|
|
399
|
+
this.pipeline.handleBinaryFrame(data);
|
|
400
|
+
this.callbacks.onSpeakingChange?.(true);
|
|
401
|
+
}
|
|
402
|
+
}
|