@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,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-agnostic helper that constructs a `VisionPublisher`, wires up
|
|
3
|
+
* status/error callbacks, calls `start()`, and returns a handle whose
|
|
4
|
+
* `stop()` tears everything down in the correct order.
|
|
5
|
+
*
|
|
6
|
+
* Both React sites (`useVisionPublisher` and the chat-ws page) share the
|
|
7
|
+
* identical construct→subscribe→start→cleanup sequence; only the track
|
|
8
|
+
* source (captureStream vs getUserMedia) differs, which stays at the
|
|
9
|
+
* call site.
|
|
10
|
+
*
|
|
11
|
+
* Spec: `specs/2026-05-22-phase-2-multimodal-watch.md`.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { ChatVisionRtcCredentialsResponse } from '../api';
|
|
15
|
+
import type { ChatWebSocket } from '../websocket';
|
|
16
|
+
import { toVisionRtcCredentials } from './credentials';
|
|
17
|
+
import type { VisionStatus } from './types';
|
|
18
|
+
import { VisionPublisher } from './VisionPublisher';
|
|
19
|
+
|
|
20
|
+
export interface StartVisionPublisherOptions {
|
|
21
|
+
ws: ChatWebSocket;
|
|
22
|
+
track: MediaStreamTrack;
|
|
23
|
+
credentialsResponse: ChatVisionRtcCredentialsResponse;
|
|
24
|
+
fps: number;
|
|
25
|
+
onStatus?: (status: VisionStatus) => void;
|
|
26
|
+
onError?: (error: Error) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface VisionPublisherHandle {
|
|
30
|
+
publisher: VisionPublisher;
|
|
31
|
+
stop: () => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Construct, wire, and start a `VisionPublisher` in one call.
|
|
36
|
+
*
|
|
37
|
+
* Returns a handle whose `stop()` calls both unsubscribers and
|
|
38
|
+
* `publisher.stop()`. The caller is still responsible for stopping
|
|
39
|
+
* the `MediaStream` tracks (since the stream may outlive the publisher)
|
|
40
|
+
* and for nulling any ref to `handle.publisher`.
|
|
41
|
+
*/
|
|
42
|
+
export function startVisionPublisher(
|
|
43
|
+
opts: StartVisionPublisherOptions,
|
|
44
|
+
): VisionPublisherHandle {
|
|
45
|
+
const publisher = new VisionPublisher({
|
|
46
|
+
ws: opts.ws,
|
|
47
|
+
track: opts.track,
|
|
48
|
+
config: { fps: opts.fps },
|
|
49
|
+
credentials: toVisionRtcCredentials(opts.credentialsResponse),
|
|
50
|
+
});
|
|
51
|
+
const offStatus = opts.onStatus ? publisher.onStatus(opts.onStatus) : (): void => {};
|
|
52
|
+
const offError = opts.onError ? publisher.onError(opts.onError) : (): void => {};
|
|
53
|
+
publisher
|
|
54
|
+
.start()
|
|
55
|
+
.catch((e) => opts.onError?.(e instanceof Error ? e : new Error(String(e))));
|
|
56
|
+
return {
|
|
57
|
+
publisher,
|
|
58
|
+
stop: () => {
|
|
59
|
+
offStatus();
|
|
60
|
+
offError();
|
|
61
|
+
publisher.stop();
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types for the framework-agnostic vision publisher.
|
|
3
|
+
*
|
|
4
|
+
* Wire-shape source of truth: `shared/vision_protocol.py` (Python Pydantic
|
|
5
|
+
* models) — TS interfaces here MUST stay in lockstep with the snake_case
|
|
6
|
+
* field names on the wire. Internally we use camelCase; serialization to
|
|
7
|
+
* snake_case happens at the WebSocket boundary in `websocket.ts`.
|
|
8
|
+
*
|
|
9
|
+
* Spec: `specs/2026-05-22-phase-2-multimodal-watch.md`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ChatWebSocket } from '../websocket';
|
|
13
|
+
|
|
14
|
+
export interface VisionPublisherConfig {
|
|
15
|
+
/** Encoded-frame cadence, in frames per second. Clamped server-side to
|
|
16
|
+
* the persona's allowed range (0.5–3.0 fps). Defaults to 1.0 fps. */
|
|
17
|
+
fps?: number;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* ICE servers (STUN / TURN) for the underlying `RTCPeerConnection`.
|
|
21
|
+
*
|
|
22
|
+
* **Default:** `[{ urls: 'stun:stun.l.google.com:19302' }]` — see
|
|
23
|
+
* `VisionPublisher.DEFAULT_ICE_SERVERS`. The browser uses this to gather
|
|
24
|
+
* server-reflexive (`srflx`) candidates, which local-dev needs because
|
|
25
|
+
* aiortc binds to all interfaces and may not advertise a candidate the
|
|
26
|
+
* browser can route to directly.
|
|
27
|
+
*
|
|
28
|
+
* **Pass `iceServers: []` to skip STUN entirely** — appropriate when
|
|
29
|
+
* the caller passes a `VisionRtcCredentials` whose `iceServers` already
|
|
30
|
+
* carries KVS-managed STUN + TURN URIs (kvs mode). In that case the
|
|
31
|
+
* publisher uses `credentials.iceServers` and this default is dead
|
|
32
|
+
* code; passing `[]` here just makes that explicit.
|
|
33
|
+
*/
|
|
34
|
+
iceServers?: RTCIceServer[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Backend → client telemetry shape that drives the vision HUD. Camel-case
|
|
39
|
+
* here; the wire payload uses snake_case (`buffered_frames`,
|
|
40
|
+
* `processing_paused`) and is converted at the `ChatWebSocket` boundary
|
|
41
|
+
* before reaching the listener.
|
|
42
|
+
*
|
|
43
|
+
* `lastInferenceMs` is not part of the protocol — the receiver has no
|
|
44
|
+
* way to know when the dispatcher finishes a vision-augmented inference,
|
|
45
|
+
* so the field would always be 0. Re-introducing it requires a callback
|
|
46
|
+
* from the dispatcher; tracked in `specs/technical-debt.md` (Issue #153).
|
|
47
|
+
*/
|
|
48
|
+
export interface VisionStatus {
|
|
49
|
+
bufferedFrames: number;
|
|
50
|
+
processingPaused: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type VisionStatusListener = (status: VisionStatus) => void;
|
|
54
|
+
export type VisionErrorListener = (error: Error) => void;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Mode + connection-establishment bundle returned by the chat-API's
|
|
58
|
+
* `POST /api/v1/chat/vision-rtc/credentials` endpoint. The frontend
|
|
59
|
+
* passes this to the publisher unchanged.
|
|
60
|
+
*
|
|
61
|
+
* In `local` mode the publisher uses the existing chat-WS
|
|
62
|
+
* `vision_offer` / `vision_ice` / `vision_close` protocol — no AWS
|
|
63
|
+
* dependency, used by `.env.local`.
|
|
64
|
+
*
|
|
65
|
+
* In `kvs` mode the publisher dynamically imports the Amazon KVS
|
|
66
|
+
* WebRTC SDK and opens the signed wss:// URL directly. The
|
|
67
|
+
* `iceServers` field carries KVS-managed STUN + short-term TURN
|
|
68
|
+
* credentials; never includes raw AWS IAM creds.
|
|
69
|
+
*/
|
|
70
|
+
export interface VisionRtcCredentials {
|
|
71
|
+
mode: 'local' | 'kvs';
|
|
72
|
+
iceServers: RTCIceServer[];
|
|
73
|
+
/** kvs mode only — SigV4-presigned wss:// URL for VIEWER. */
|
|
74
|
+
signedSignalingUrl?: string | null;
|
|
75
|
+
/** kvs mode only — KVS signaling channel ARN. Informational. */
|
|
76
|
+
channelArn?: string | null;
|
|
77
|
+
/** kvs mode only — unique VIEWER ClientId KVS uses to scope the session. */
|
|
78
|
+
clientId?: string | null;
|
|
79
|
+
/** kvs mode only — AWS region for the signaling channel. The KVS
|
|
80
|
+
* WebRTC JS SDK uses this when SigV4-signing follow-up ICE
|
|
81
|
+
* candidate messages. */
|
|
82
|
+
region?: string | null;
|
|
83
|
+
/** Seconds until the credentials expire. Browser should re-fetch
|
|
84
|
+
* before this elapses if a session outlives the TTL. */
|
|
85
|
+
expiresIn: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface VisionPublisherOptions {
|
|
89
|
+
ws: ChatWebSocket;
|
|
90
|
+
/** A single video track, or a `MediaStream` whose first video track is
|
|
91
|
+
* picked. */
|
|
92
|
+
track: MediaStreamTrack | MediaStream;
|
|
93
|
+
config?: VisionPublisherConfig;
|
|
94
|
+
/**
|
|
95
|
+
* Credentials from `/api/v1/chat/vision-rtc/credentials`. When omitted,
|
|
96
|
+
* the publisher falls back to local-mode signaling (chat-WS protocol)
|
|
97
|
+
* with the configured STUN servers — the legacy path that predates
|
|
98
|
+
* KVS support. New code should always pass this.
|
|
99
|
+
*/
|
|
100
|
+
credentials?: VisionRtcCredentials;
|
|
101
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure scheduling helper for the vision publisher.
|
|
3
|
+
*
|
|
4
|
+
* Inputs: the current target fps + the most recent `vision_status` event
|
|
5
|
+
* from the backend. Output: the fps the publisher should now encode at.
|
|
6
|
+
*
|
|
7
|
+
* The heuristic is deliberately tiny — no timers, no sockets, no state
|
|
8
|
+
* beyond the last-known fps. The publisher (or its React/Vue hook) is
|
|
9
|
+
* responsible for actually applying the new fps to its RTCRtpSender
|
|
10
|
+
* encodings (`sender.setParameters({ ...p, encodings: [{ maxFramerate }]
|
|
11
|
+
* })`) or, for the v1 stub backend, just remembering it for the next
|
|
12
|
+
* offer.
|
|
13
|
+
*
|
|
14
|
+
* Backpressure principle (mirrors `~/projects/live-vlm-webui`):
|
|
15
|
+
* - When the backend reports `processingPaused` (inference is behind),
|
|
16
|
+
* halve fps so we stop queuing stale frames.
|
|
17
|
+
* - When the buffer is empty (consumer is keeping up perfectly), gently
|
|
18
|
+
* bump fps so we use the available headroom.
|
|
19
|
+
* - Otherwise leave fps alone.
|
|
20
|
+
*
|
|
21
|
+
* Earlier drafts also gated the bump-up branch on
|
|
22
|
+
* `status.lastInferenceMs <= FAST_INFERENCE_MS`; that field is absent
|
|
23
|
+
* from `VisionStatus` because the receiver has no way to populate it
|
|
24
|
+
* (see Issue #153). Using only `bufferedFrames === 0` as the bump signal
|
|
25
|
+
* is a reasonable proxy for "consumer is keeping up": if the buffer
|
|
26
|
+
* drained while not paused, inference completed before the next frame
|
|
27
|
+
* arrived, which is exactly the headroom we want to use.
|
|
28
|
+
*
|
|
29
|
+
* Spec: `specs/2026-05-22-phase-2-multimodal-watch.md`.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
import type { VisionStatus, VisionStatusListener } from './types';
|
|
33
|
+
|
|
34
|
+
export interface ComputeNextFpsInput {
|
|
35
|
+
currentFps: number;
|
|
36
|
+
status: VisionStatus;
|
|
37
|
+
minFps: number;
|
|
38
|
+
maxFps: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const UP_NUDGE = 0.25;
|
|
42
|
+
|
|
43
|
+
const clamp = (value: number, min: number, max: number): number =>
|
|
44
|
+
Math.min(max, Math.max(min, value));
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Pure decision function — no side effects, no state. Returns the next
|
|
48
|
+
* target fps given the current fps and the most recent status report.
|
|
49
|
+
*/
|
|
50
|
+
export const computeNextFps = (input: ComputeNextFpsInput): number => {
|
|
51
|
+
const { currentFps, status, minFps, maxFps } = input;
|
|
52
|
+
const safeCurrent = clamp(currentFps, minFps, maxFps);
|
|
53
|
+
|
|
54
|
+
if (status.processingPaused) {
|
|
55
|
+
return clamp(safeCurrent / 2, minFps, maxFps);
|
|
56
|
+
}
|
|
57
|
+
if (status.bufferedFrames === 0) {
|
|
58
|
+
return clamp(safeCurrent + UP_NUDGE, minFps, maxFps);
|
|
59
|
+
}
|
|
60
|
+
return safeCurrent;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export interface VisionSchedulerOptions {
|
|
64
|
+
initialFps: number;
|
|
65
|
+
minFps: number;
|
|
66
|
+
maxFps: number;
|
|
67
|
+
/** Invoked when the scheduler decides to change fps. Only fires when
|
|
68
|
+
* the new fps differs from the previous fps. */
|
|
69
|
+
onFpsChange: (fps: number) => void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface VisionScheduler {
|
|
73
|
+
/** Feed a status event into the scheduler. Triggers `onFpsChange` iff
|
|
74
|
+
* the new fps differs from the current. */
|
|
75
|
+
handleStatus: VisionStatusListener;
|
|
76
|
+
/** Last fps the scheduler decided on. */
|
|
77
|
+
getFps: () => number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Stateful adapter on top of {@link computeNextFps}. Wires nicely to
|
|
82
|
+
* `VisionPublisher.onStatus(scheduler.handleStatus)`.
|
|
83
|
+
*/
|
|
84
|
+
export const createVisionScheduler = (
|
|
85
|
+
options: VisionSchedulerOptions,
|
|
86
|
+
): VisionScheduler => {
|
|
87
|
+
const { minFps, maxFps, onFpsChange } = options;
|
|
88
|
+
let currentFps = clamp(options.initialFps, minFps, maxFps);
|
|
89
|
+
|
|
90
|
+
const handleStatus: VisionStatusListener = (status) => {
|
|
91
|
+
const nextFps = computeNextFps({ currentFps, status, minFps, maxFps });
|
|
92
|
+
if (nextFps !== currentFps) {
|
|
93
|
+
currentFps = nextFps;
|
|
94
|
+
onFpsChange(nextFps);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
handleStatus,
|
|
100
|
+
getFps: () => currentFps,
|
|
101
|
+
};
|
|
102
|
+
};
|
package/src/websocket.ts
CHANGED
|
@@ -87,14 +87,22 @@ export interface PlaybackState {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
export type ChatWsEvent =
|
|
90
|
-
| { type: 'token'; timestamp?: number; content: string }
|
|
90
|
+
| { type: 'token'; timestamp?: number; content: string; personaId?: string }
|
|
91
91
|
| {
|
|
92
92
|
type: 'citation';
|
|
93
93
|
timestamp?: number;
|
|
94
94
|
citation: Citation;
|
|
95
95
|
span?: string;
|
|
96
|
+
/**
|
|
97
|
+
* Active speaking persona for this frame, set ONLY by the M10 Story
|
|
98
|
+
* Engine Phase 9a multi-persona scene orchestrator (when an adventure
|
|
99
|
+
* has a non-empty party). Undefined for all other chat — the frame
|
|
100
|
+
* stays byte-identical (land-dark). Matches the additive `personaId`
|
|
101
|
+
* the `WebSocketEmitter` stamps in `api/chat/ws.py`.
|
|
102
|
+
*/
|
|
103
|
+
personaId?: string;
|
|
96
104
|
}
|
|
97
|
-
| { type: 'done'; timestamp?: number; reason?: DoneReason }
|
|
105
|
+
| { type: 'done'; timestamp?: number; reason?: DoneReason; personaId?: string }
|
|
98
106
|
| { type: 'error'; timestamp?: number; message: string }
|
|
99
107
|
| {
|
|
100
108
|
type: 'image';
|
|
@@ -103,6 +111,8 @@ export type ChatWsEvent =
|
|
|
103
111
|
entity?: string;
|
|
104
112
|
prompt?: string;
|
|
105
113
|
sourceTimestamp?: number;
|
|
114
|
+
/** See the `citation` case — orchestrator-stamped persona id (M10 9a). */
|
|
115
|
+
personaId?: string;
|
|
106
116
|
}
|
|
107
117
|
| {
|
|
108
118
|
type: 'tool_use';
|
|
@@ -143,6 +153,13 @@ export type ChatWsEvent =
|
|
|
143
153
|
chunkId?: string;
|
|
144
154
|
audio: string;
|
|
145
155
|
format: 'mp3' | 'pcm16' | 'ogg_opus';
|
|
156
|
+
/**
|
|
157
|
+
* Active speaking persona for this audio frame (M10 9a, additive). The
|
|
158
|
+
* 9a server only STAMPS this so the client can swap face/voice per turn;
|
|
159
|
+
* per-turn server-side TTS voice swap is deferred (tracked in tech-debt).
|
|
160
|
+
* Undefined for all non-orchestrated audio (land-dark).
|
|
161
|
+
*/
|
|
162
|
+
personaId?: string;
|
|
146
163
|
}
|
|
147
164
|
| {
|
|
148
165
|
type: 'metrics';
|
|
@@ -152,7 +169,24 @@ export type ChatWsEvent =
|
|
|
152
169
|
transcript_to_first_audio_ms?: number;
|
|
153
170
|
}
|
|
154
171
|
| { type: 'heartbeat'; timestamp?: number }
|
|
155
|
-
| { type: 'pong'; timestamp?: number }
|
|
172
|
+
| { type: 'pong'; timestamp?: number }
|
|
173
|
+
| {
|
|
174
|
+
/**
|
|
175
|
+
* The active speaking persona changed. Emitted by the M10 Story Engine
|
|
176
|
+
* Phase 9a multi-persona scene orchestrator whenever it hands the turn
|
|
177
|
+
* to a different persona (DM ↔ party member, or one party member to
|
|
178
|
+
* another via a `cue_persona` tool call). Carries the new `personaId`
|
|
179
|
+
* and the `previousPersonaId` it replaced (omitted/undefined for the
|
|
180
|
+
* first turn of a connection). Never emitted for non-adventure chat or
|
|
181
|
+
* M9 single-DM (empty-party) adventures — those keep the existing event
|
|
182
|
+
* set verbatim (land-dark). Mirrors `ChatEventType.TURN_CHANGED`
|
|
183
|
+
* (`"turn_changed"`) in `shared/chat_dispatcher.py`.
|
|
184
|
+
*/
|
|
185
|
+
type: 'turn_changed';
|
|
186
|
+
timestamp?: number;
|
|
187
|
+
personaId: string;
|
|
188
|
+
previousPersonaId?: string;
|
|
189
|
+
};
|
|
156
190
|
|
|
157
191
|
/**
|
|
158
192
|
* @deprecated Prefer {@link ChatWsEvent} — the flat-shape interface
|
|
@@ -225,6 +259,57 @@ export interface WebSocketMessage {
|
|
|
225
259
|
export type MessageHandler = (message: WebSocketMessage) => void;
|
|
226
260
|
export type BinaryHandler = (data: ArrayBuffer) => void;
|
|
227
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Vision signaling event names routed through the typed `on()` / `off()`
|
|
264
|
+
* dispatcher. The wire payload is snake_case (`vision_answer`,
|
|
265
|
+
* `vision_ice`, `vision_status`) and matches `shared/vision_protocol.py`;
|
|
266
|
+
* we use the snake_case names here too because they double as the
|
|
267
|
+
* incoming `type`/`action` discriminator.
|
|
268
|
+
*
|
|
269
|
+
* Spec: `specs/2026-05-22-phase-2-multimodal-watch.md`.
|
|
270
|
+
*/
|
|
271
|
+
export type ChatWsTypedEvent = 'vision_answer' | 'vision_ice' | 'vision_status';
|
|
272
|
+
|
|
273
|
+
/** Inbound `vision_answer` payload from the backend after it processes
|
|
274
|
+
* the publisher's offer. */
|
|
275
|
+
export interface VisionAnswerEvent {
|
|
276
|
+
type: 'vision_answer';
|
|
277
|
+
sdp: string;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Inbound `vision_ice` payload from the backend's trickle-ICE stream.
|
|
281
|
+
* Wire shape uses snake_case (`sdp_mid` / `sdp_m_line_index`); the
|
|
282
|
+
* dispatcher converts to camel-case before invoking listeners. */
|
|
283
|
+
export interface VisionIceEvent {
|
|
284
|
+
action: 'vision_ice';
|
|
285
|
+
candidate: string | null;
|
|
286
|
+
sdpMid: string | null;
|
|
287
|
+
sdpMLineIndex: number | null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Inbound `vision_status` HUD telemetry. Camel-case here (`bufferedFrames`,
|
|
291
|
+
* `processingPaused`); wire shape is snake_case and is converted in the
|
|
292
|
+
* dispatcher. `lastInferenceMs` is not part of the protocol — see
|
|
293
|
+
* `specs/technical-debt.md` Issue #153. */
|
|
294
|
+
export interface VisionStatusEvent {
|
|
295
|
+
type: 'vision_status';
|
|
296
|
+
bufferedFrames: number;
|
|
297
|
+
processingPaused: boolean;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export type ChatWsTypedEventPayload<E extends ChatWsTypedEvent> =
|
|
301
|
+
E extends 'vision_answer'
|
|
302
|
+
? VisionAnswerEvent
|
|
303
|
+
: E extends 'vision_ice'
|
|
304
|
+
? VisionIceEvent
|
|
305
|
+
: E extends 'vision_status'
|
|
306
|
+
? VisionStatusEvent
|
|
307
|
+
: never;
|
|
308
|
+
|
|
309
|
+
export type TypedEventHandler<E extends ChatWsTypedEvent = ChatWsTypedEvent> = (
|
|
310
|
+
payload: ChatWsTypedEventPayload<E>,
|
|
311
|
+
) => void | Promise<void>;
|
|
312
|
+
|
|
228
313
|
export interface ChatWebSocketOptions {
|
|
229
314
|
url: string;
|
|
230
315
|
/**
|
|
@@ -272,6 +357,15 @@ export class ChatWebSocket {
|
|
|
272
357
|
private ws: WebSocket | null = null;
|
|
273
358
|
private messageHandlers: MessageHandler[] = [];
|
|
274
359
|
private binaryHandlers: BinaryHandler[] = [];
|
|
360
|
+
/**
|
|
361
|
+
* Typed event-emitter storage for the vision signaling channel. Keyed
|
|
362
|
+
* by `ChatWsTypedEvent`; values are lists of camel-case-payload
|
|
363
|
+
* listeners. The raw `messageHandlers` list above is unchanged and
|
|
364
|
+
* still fires for every inbound message — typed listeners are
|
|
365
|
+
* additive, not a replacement.
|
|
366
|
+
*/
|
|
367
|
+
private typedEventHandlers: Map<ChatWsTypedEvent, TypedEventHandler[]> =
|
|
368
|
+
new Map();
|
|
275
369
|
private url: string;
|
|
276
370
|
private authToken?: string;
|
|
277
371
|
private fetchToken?: () => Promise<string>;
|
|
@@ -382,7 +476,15 @@ export class ChatWebSocket {
|
|
|
382
476
|
this.ws.onmessage = async (event) => {
|
|
383
477
|
try {
|
|
384
478
|
if (typeof event.data === 'string') {
|
|
385
|
-
const
|
|
479
|
+
const raw = JSON.parse(event.data) as Record<string, unknown>;
|
|
480
|
+
// Vision signaling messages bypass the legacy
|
|
481
|
+
// `WebSocketMessage` shape — they're snake_case on the wire
|
|
482
|
+
// and have their own typed dispatcher. Fire those first;
|
|
483
|
+
// they also flow through `messageHandlers` cast as
|
|
484
|
+
// `WebSocketMessage` so existing observers keep seeing
|
|
485
|
+
// them (back-compat).
|
|
486
|
+
this._dispatchVisionEvent(raw);
|
|
487
|
+
const message = raw as unknown as WebSocketMessage;
|
|
386
488
|
this.messageHandlers.forEach((handler) => handler(message));
|
|
387
489
|
return;
|
|
388
490
|
}
|
|
@@ -450,6 +552,44 @@ export class ChatWebSocket {
|
|
|
450
552
|
messages?: Array<{ role: string; content: string; modelId?: string }>;
|
|
451
553
|
systemPrompt?: string;
|
|
452
554
|
systemPromptOverride?: boolean;
|
|
555
|
+
/**
|
|
556
|
+
* Entity id of the character a bound persona portrays. When set, the
|
|
557
|
+
* backend auto-scopes entity_timeline / speaker_dialog tools to this
|
|
558
|
+
* entity and appends a "you are portraying X" suffix. Omit (undefined)
|
|
559
|
+
* for unbound personas — the wire stays unchanged for them (land-dark).
|
|
560
|
+
* Frontend-resolved, same as `systemPrompt`. Matches
|
|
561
|
+
* `SendActionMessage.entityId` in `shared/websocket_models.py`.
|
|
562
|
+
*/
|
|
563
|
+
entityId?: string;
|
|
564
|
+
/**
|
|
565
|
+
* Persona id the chat memory plane is namespaced to (M8 Memory Plane
|
|
566
|
+
* Phase D). Independent of `entityId`: when set, the backend scopes memory
|
|
567
|
+
* recall/store to this persona so two personas accumulate distinct memory.
|
|
568
|
+
* Omit (undefined) for general chat / no active persona — recall stays
|
|
569
|
+
* user-scoped (land-dark). Matches `SendActionMessage.personaId` in
|
|
570
|
+
* `shared/websocket_models.py`.
|
|
571
|
+
*/
|
|
572
|
+
personaId?: string;
|
|
573
|
+
/**
|
|
574
|
+
* Adventure session id the chat turn is running inside (M9 Adventure
|
|
575
|
+
* Runtime Phase 8a). When set, the backend exposes the Dungeon-Master
|
|
576
|
+
* tool surface and threads the active adventure's state into the system
|
|
577
|
+
* prompt so the persona drives the story. Omit (undefined) for ordinary
|
|
578
|
+
* chat — the wire stays unchanged (land-dark). Matches
|
|
579
|
+
* `SendActionMessage.adventureSessionId` in `shared/websocket_models.py`.
|
|
580
|
+
*/
|
|
581
|
+
adventureSessionId?: string;
|
|
582
|
+
/**
|
|
583
|
+
* Party persona ids for a multi-persona adventure (M10 Story Engine
|
|
584
|
+
* Phase 9a). Advisory / forward-compat only — the server's
|
|
585
|
+
* `AdventureState.party_persona_ids` is authoritative for scene
|
|
586
|
+
* routing; the orchestrator loads the roster from `AdventureState`,
|
|
587
|
+
* not from this field. Omit / leave empty for ordinary chat and M9
|
|
588
|
+
* single-DM (empty-party) adventures — the wire stays unchanged
|
|
589
|
+
* (land-dark). Matches `SendActionMessage.partyPersonaIds` in
|
|
590
|
+
* `shared/websocket_models.py`.
|
|
591
|
+
*/
|
|
592
|
+
partyPersonaIds?: string[];
|
|
453
593
|
heartbeatInterval?: number;
|
|
454
594
|
idleTimeout?: number;
|
|
455
595
|
}
|
|
@@ -501,6 +641,196 @@ export class ChatWebSocket {
|
|
|
501
641
|
);
|
|
502
642
|
}
|
|
503
643
|
|
|
644
|
+
/**
|
|
645
|
+
* Send the WebRTC SDP offer that opens a vision session. The
|
|
646
|
+
* `VisionPublisher` calls this after `pc.createOffer()` /
|
|
647
|
+
* `setLocalDescription()`. Wire shape exactly matches
|
|
648
|
+
* `shared/vision_protocol.py::VisionOfferMessage`.
|
|
649
|
+
*/
|
|
650
|
+
sendVisionOffer(payload: {
|
|
651
|
+
action: 'vision_offer';
|
|
652
|
+
sdp: string;
|
|
653
|
+
fps: number;
|
|
654
|
+
}): void {
|
|
655
|
+
this._sendVisionJson(payload);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* Send a single trickle-ICE candidate from the publisher. The TS
|
|
660
|
+
* payload uses camel-case (`sdpMid`, `sdpMLineIndex`); the wire form
|
|
661
|
+
* is snake_case (`sdp_mid`, `sdp_m_line_index`) to match
|
|
662
|
+
* `shared/vision_protocol.py::VisionIceMessage`. Conversion happens
|
|
663
|
+
* here so callers don't need to remember the dialect boundary.
|
|
664
|
+
* A `candidate === null` payload signals end-of-candidates.
|
|
665
|
+
*/
|
|
666
|
+
sendVisionIce(payload: {
|
|
667
|
+
action: 'vision_ice';
|
|
668
|
+
candidate: string | null;
|
|
669
|
+
sdpMid?: string | null;
|
|
670
|
+
sdpMLineIndex?: number | null;
|
|
671
|
+
}): void {
|
|
672
|
+
this._sendVisionJson({
|
|
673
|
+
action: payload.action,
|
|
674
|
+
candidate: payload.candidate,
|
|
675
|
+
sdp_mid: payload.sdpMid ?? null,
|
|
676
|
+
sdp_m_line_index: payload.sdpMLineIndex ?? null,
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Tell the backend the publisher is done with this vision session so
|
|
682
|
+
* it can tear down its `FrameBuffer` and aiortc receiver. Fire-and-
|
|
683
|
+
* forget — if the socket is already closed, the backend will GC on
|
|
684
|
+
* disconnect, so a swallow-and-return is safe.
|
|
685
|
+
*/
|
|
686
|
+
sendVisionClose(): void {
|
|
687
|
+
this._sendVisionJson({ action: 'vision_close' });
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* KVS-mode only: bind this chat-WS session to the KVS viewer
|
|
692
|
+
* ClientId the browser received from `/vision-rtc/credentials`. The
|
|
693
|
+
* backend uses the ClientId to look up the right `FrameBuffer` in
|
|
694
|
+
* `VisionSessionRouter` when serving chat turns; without this call
|
|
695
|
+
* the dispatcher can't find any vision frames in KVS mode.
|
|
696
|
+
*
|
|
697
|
+
* Local mode never sends this — receivers live on `session.vision_buffer`
|
|
698
|
+
* and the chat WS already knows about them via the existing
|
|
699
|
+
* `vision_offer` handler.
|
|
700
|
+
*/
|
|
701
|
+
sendVisionAttach(payload: { action: 'vision_attach'; clientId: string }): void {
|
|
702
|
+
this._sendVisionJson({
|
|
703
|
+
action: payload.action,
|
|
704
|
+
// The wire field is camelCase `clientId` — the backend reads
|
|
705
|
+
// `message.get("clientId")` in ws.py's vision_attach handler.
|
|
706
|
+
// Do NOT "normalize" this to snake_case `client_id`: that would
|
|
707
|
+
// make the backend read None and silently break KVS attach.
|
|
708
|
+
clientId: payload.clientId,
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* KVS-mode only: release the per-ClientId receiver + FrameBuffer in
|
|
714
|
+
* `VisionSessionRouter`. Sent from `VisionPublisher.stop()` in kvs
|
|
715
|
+
* mode. The backend also cleans up on WS disconnect, so this is
|
|
716
|
+
* fire-and-forget; the explicit detach just frees resources slightly
|
|
717
|
+
* sooner during long-lived chat sessions.
|
|
718
|
+
*/
|
|
719
|
+
sendVisionDetach(): void {
|
|
720
|
+
this._sendVisionJson({ action: 'vision_detach' });
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/**
|
|
724
|
+
* Register a typed listener for a vision-signaling event. Returns
|
|
725
|
+
* void (matches the `messageHandlers` pattern); unsubscribe via
|
|
726
|
+
* `off(event, handler)`.
|
|
727
|
+
*/
|
|
728
|
+
on<E extends ChatWsTypedEvent>(event: E, handler: TypedEventHandler<E>): void {
|
|
729
|
+
const list = this.typedEventHandlers.get(event) ?? [];
|
|
730
|
+
list.push(handler as TypedEventHandler);
|
|
731
|
+
this.typedEventHandlers.set(event, list);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/** Remove a previously-registered typed listener. */
|
|
735
|
+
off<E extends ChatWsTypedEvent>(event: E, handler: TypedEventHandler<E>): void {
|
|
736
|
+
const list = this.typedEventHandlers.get(event);
|
|
737
|
+
if (!list) return;
|
|
738
|
+
this.typedEventHandlers.set(
|
|
739
|
+
event,
|
|
740
|
+
list.filter((h) => h !== handler),
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Best-effort JSON send for vision signaling. Vision messages are
|
|
746
|
+
* fire-and-forget; if the socket isn't OPEN we drop silently (the
|
|
747
|
+
* publisher will tear down when its connection state changes).
|
|
748
|
+
*/
|
|
749
|
+
private _sendVisionJson(payload: Record<string, unknown>): void {
|
|
750
|
+
if (this.ws?.readyState !== WebSocket.OPEN) return;
|
|
751
|
+
this.ws.send(JSON.stringify(payload));
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Inspect an inbound parsed message and, if it's one of the three
|
|
756
|
+
* vision signaling events, fan it out to the typed listeners. Owns
|
|
757
|
+
* the snake_case → camel_case conversion for `vision_ice` and
|
|
758
|
+
* `vision_status` so listeners see the camel-cased payloads declared
|
|
759
|
+
* in the `Vision*Event` interfaces.
|
|
760
|
+
*/
|
|
761
|
+
private _dispatchVisionEvent(raw: Record<string, unknown>): void {
|
|
762
|
+
const t = raw['type'] as string | undefined;
|
|
763
|
+
const action = raw['action'] as string | undefined;
|
|
764
|
+
if (t === 'vision_answer') {
|
|
765
|
+
this._fireTyped('vision_answer', {
|
|
766
|
+
type: 'vision_answer',
|
|
767
|
+
sdp: String(raw['sdp'] ?? ''),
|
|
768
|
+
});
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
if (t === 'vision_status') {
|
|
772
|
+
// Wire shape is snake_case; surface camel-case to listeners.
|
|
773
|
+
const bufferedFrames =
|
|
774
|
+
(raw['bufferedFrames'] as number | undefined) ??
|
|
775
|
+
(raw['buffered_frames'] as number | undefined) ??
|
|
776
|
+
0;
|
|
777
|
+
const processingPaused =
|
|
778
|
+
(raw['processingPaused'] as boolean | undefined) ??
|
|
779
|
+
(raw['processing_paused'] as boolean | undefined) ??
|
|
780
|
+
false;
|
|
781
|
+
this._fireTyped('vision_status', {
|
|
782
|
+
type: 'vision_status',
|
|
783
|
+
bufferedFrames,
|
|
784
|
+
processingPaused,
|
|
785
|
+
});
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
if (action === 'vision_ice') {
|
|
789
|
+
const sdpMid =
|
|
790
|
+
(raw['sdpMid'] as string | null | undefined) ??
|
|
791
|
+
(raw['sdp_mid'] as string | null | undefined) ??
|
|
792
|
+
null;
|
|
793
|
+
const sdpMLineIndex =
|
|
794
|
+
(raw['sdpMLineIndex'] as number | null | undefined) ??
|
|
795
|
+
(raw['sdp_m_line_index'] as number | null | undefined) ??
|
|
796
|
+
null;
|
|
797
|
+
this._fireTyped('vision_ice', {
|
|
798
|
+
action: 'vision_ice',
|
|
799
|
+
candidate: (raw['candidate'] as string | null) ?? null,
|
|
800
|
+
sdpMid,
|
|
801
|
+
sdpMLineIndex,
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
private _fireTyped<E extends ChatWsTypedEvent>(
|
|
807
|
+
event: E,
|
|
808
|
+
payload: ChatWsTypedEventPayload<E>,
|
|
809
|
+
): void {
|
|
810
|
+
const list = this.typedEventHandlers.get(event);
|
|
811
|
+
if (!list) return;
|
|
812
|
+
for (const handler of list) {
|
|
813
|
+
try {
|
|
814
|
+
const result = handler(payload);
|
|
815
|
+
// Async handlers (the publisher's onAnswer/onIce are both
|
|
816
|
+
// `async`) return a Promise. `void` would swallow rejections
|
|
817
|
+
// into the unhandled-rejection channel where they're invisible
|
|
818
|
+
// to ChatWebSocket consumers. Catch + log so async failures
|
|
819
|
+
// show up alongside sync ones.
|
|
820
|
+
if (result instanceof Promise) {
|
|
821
|
+
result.catch((err) => {
|
|
822
|
+
console.error(
|
|
823
|
+
`ChatWebSocket typed handler for ${event} rejected:`,
|
|
824
|
+
err,
|
|
825
|
+
);
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
} catch (err) {
|
|
829
|
+
console.error(`ChatWebSocket typed handler for ${event} threw:`, err);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
|
|
504
834
|
/**
|
|
505
835
|
* Send a binary microphone frame (tagged with TAG_MIC)
|
|
506
836
|
*/
|