@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
package/src/audio.ts
CHANGED
|
@@ -394,6 +394,9 @@ export const buildHelloPayload = ({
|
|
|
394
394
|
systemPromptOverride = false,
|
|
395
395
|
modelId,
|
|
396
396
|
ttsModelId,
|
|
397
|
+
entityId,
|
|
398
|
+
adventureSessionId,
|
|
399
|
+
partyPersonaIds,
|
|
397
400
|
}: {
|
|
398
401
|
videoId?: string;
|
|
399
402
|
personaId?: string;
|
|
@@ -407,6 +410,35 @@ export const buildHelloPayload = ({
|
|
|
407
410
|
systemPromptOverride?: boolean;
|
|
408
411
|
modelId?: string;
|
|
409
412
|
ttsModelId?: string;
|
|
413
|
+
/**
|
|
414
|
+
* Entity id of the character a bound persona portrays. When set, the
|
|
415
|
+
* voice session is scoped to this entity exactly like the text path.
|
|
416
|
+
* Omit (undefined) for unbound personas — the hello frame stays
|
|
417
|
+
* unchanged for them (land-dark). Frontend-resolved, same as
|
|
418
|
+
* `systemPrompt`. Matches `HelloMessage.entityId` in
|
|
419
|
+
* `shared/audio_protocol.py`.
|
|
420
|
+
*/
|
|
421
|
+
entityId?: string;
|
|
422
|
+
/**
|
|
423
|
+
* Adventure session id the voice session runs inside (M9 Adventure Runtime
|
|
424
|
+
* Phase 8a). When set, the backend exposes the Dungeon-Master tool surface
|
|
425
|
+
* and threads the active adventure's state into the system prompt, exactly
|
|
426
|
+
* like the text path. Omit (undefined) for ordinary voice chat — the hello
|
|
427
|
+
* frame stays unchanged (land-dark). Matches `HelloMessage.adventureSessionId`
|
|
428
|
+
* in `shared/audio_protocol.py`.
|
|
429
|
+
*/
|
|
430
|
+
adventureSessionId?: string;
|
|
431
|
+
/**
|
|
432
|
+
* Party persona ids for a multi-persona adventure (M10 Story Engine Phase
|
|
433
|
+
* 9a). Advisory / forward-compat only: the SERVER's
|
|
434
|
+
* `AdventureState.party_persona_ids` is authoritative for scene-orchestrator
|
|
435
|
+
* routing — the orchestrator loads the roster from `AdventureState`, NOT from
|
|
436
|
+
* this hello field (mirrors the M9 8a "bound session authoritative"
|
|
437
|
+
* decision). Omit / leave empty for ordinary chat and M9 single-DM
|
|
438
|
+
* (empty-party) adventures — the hello frame stays unchanged (land-dark).
|
|
439
|
+
* Matches `HelloMessage.partyPersonaIds` in `shared/audio_protocol.py`.
|
|
440
|
+
*/
|
|
441
|
+
partyPersonaIds?: string[];
|
|
410
442
|
}) => ({
|
|
411
443
|
action: 'hello' as const,
|
|
412
444
|
version: HELLO_VERSION,
|
|
@@ -421,6 +453,9 @@ export const buildHelloPayload = ({
|
|
|
421
453
|
...(systemPrompt ? { systemPrompt, systemPromptOverride } : {}),
|
|
422
454
|
...(modelId ? { modelId } : {}),
|
|
423
455
|
...(ttsModelId ? { ttsModelId } : {}),
|
|
456
|
+
...(entityId ? { entityId } : {}),
|
|
457
|
+
...(adventureSessionId ? { adventureSessionId } : {}),
|
|
458
|
+
...(partyPersonaIds && partyPersonaIds.length > 0 ? { partyPersonaIds } : {}),
|
|
424
459
|
});
|
|
425
460
|
|
|
426
461
|
export type HelloPayload = ReturnType<typeof buildHelloPayload>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* castProjection — pure normalizer over the R5a cast-with-persona payload
|
|
3
|
+
* (Platform Restructure R5c).
|
|
4
|
+
*
|
|
5
|
+
* `castWithPersona(response)` turns a `CastWithPersonaResponse` (the one-call
|
|
6
|
+
* `GET /api/v1/tools/videos/{videoId}/cast` server-side join) into the
|
|
7
|
+
* render-ready `CastEntry[]` a Cast view consumes: deterministically ordered
|
|
8
|
+
* (entries by display name, scenes by id, arcs by index) with every list
|
|
9
|
+
* field coalesced and display-name fallbacks applied, so a renderer never
|
|
10
|
+
* branches on missing data (deploy skew between an older backend and a newer
|
|
11
|
+
* client degrades to empty lists, not crashes).
|
|
12
|
+
*
|
|
13
|
+
* Framework-agnostic and side-effect free — React (the studio CastGrid) and
|
|
14
|
+
* any other consumer (the escape.ai Web-App, a game engine UI) share this ONE
|
|
15
|
+
* projection instead of re-joining `getVideoGraph` + `listPersonas` client
|
|
16
|
+
* side by `entity_id` string equality (the pre-R5c fragility this milestone
|
|
17
|
+
* removes). The function never mutates its input.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import type {
|
|
21
|
+
CastArc,
|
|
22
|
+
CastCharacter,
|
|
23
|
+
CastEntry,
|
|
24
|
+
CastPersonaSummary,
|
|
25
|
+
CastScene,
|
|
26
|
+
CastWithPersonaResponse,
|
|
27
|
+
} from './api';
|
|
28
|
+
|
|
29
|
+
/** Normalize one character: coalesce lists, apply display fallbacks, sort. */
|
|
30
|
+
function normalizeCharacter(character: CastCharacter): CastCharacter {
|
|
31
|
+
const entityId = character.entityId ?? '';
|
|
32
|
+
const scenes: CastScene[] = (character.scenes ?? [])
|
|
33
|
+
.map((scene) => ({
|
|
34
|
+
sceneId: scene.sceneId,
|
|
35
|
+
title: scene.title || scene.sceneId,
|
|
36
|
+
}))
|
|
37
|
+
.sort((a, b) => a.sceneId.localeCompare(b.sceneId));
|
|
38
|
+
const arcs: CastArc[] = (character.arcs ?? [])
|
|
39
|
+
.map((arc) => ({
|
|
40
|
+
arcId: arc.arcId,
|
|
41
|
+
title: arc.title || arc.arcId,
|
|
42
|
+
summary: arc.summary ?? null,
|
|
43
|
+
index: arc.index ?? 0,
|
|
44
|
+
}))
|
|
45
|
+
.sort((a, b) => a.index - b.index || a.arcId.localeCompare(b.arcId));
|
|
46
|
+
return {
|
|
47
|
+
entityId,
|
|
48
|
+
name: character.name || entityId,
|
|
49
|
+
aliases: [...(character.aliases ?? [])],
|
|
50
|
+
scenes,
|
|
51
|
+
arcs,
|
|
52
|
+
relatedCharacterIds: [...(character.relatedCharacterIds ?? [])],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Normalize the bound persona (or null when unbound). */
|
|
57
|
+
function normalizePersona(
|
|
58
|
+
persona: CastPersonaSummary | null | undefined,
|
|
59
|
+
): CastPersonaSummary | null {
|
|
60
|
+
if (!persona) return null;
|
|
61
|
+
return {
|
|
62
|
+
personaId: persona.personaId,
|
|
63
|
+
name: persona.name || persona.personaId || '',
|
|
64
|
+
chatModelId: persona.chatModelId ?? '',
|
|
65
|
+
characterImageId: persona.characterImageId ?? null,
|
|
66
|
+
isDungeonMaster: persona.isDungeonMaster ?? false,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Project a `CastWithPersonaResponse` into render-ready `CastEntry[]`.
|
|
72
|
+
*
|
|
73
|
+
* Entries are sorted by character display name (`localeCompare`, entityId
|
|
74
|
+
* tie-break — the same order the legacy `deriveCast` client join rendered),
|
|
75
|
+
* with all nested list fields coalesced + deterministically sorted and the
|
|
76
|
+
* title/name fallbacks applied. Pure: the input response is not mutated.
|
|
77
|
+
*
|
|
78
|
+
* Name ordering is locale-aware via the RUNTIME's default locale
|
|
79
|
+
* (`localeCompare` with no locale argument) — deliberately matching the
|
|
80
|
+
* legacy `deriveCast` ordering rather than pinning a locale; two consumers in
|
|
81
|
+
* different locales may order accented names differently, but each render is
|
|
82
|
+
* internally deterministic (the entityId tie-break breaks equal names).
|
|
83
|
+
*/
|
|
84
|
+
export function castWithPersona(response: CastWithPersonaResponse): CastEntry[] {
|
|
85
|
+
return (response.cast ?? [])
|
|
86
|
+
.map((entry) => ({
|
|
87
|
+
character: normalizeCharacter(entry.character),
|
|
88
|
+
persona: normalizePersona(entry.persona),
|
|
89
|
+
}))
|
|
90
|
+
.sort(
|
|
91
|
+
(a, b) =>
|
|
92
|
+
a.character.name.localeCompare(b.character.name) ||
|
|
93
|
+
a.character.entityId.localeCompare(b.character.entityId),
|
|
94
|
+
);
|
|
95
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conversation context binding (R2.1).
|
|
3
|
+
*
|
|
4
|
+
* A `ConversationContextBinding` is the typed superset of everything
|
|
5
|
+
* `buildHelloPayload` (audio.ts) accepts for grounding a session: the Watch
|
|
6
|
+
* video, the Studio character/adventure session, and the advisory party roster.
|
|
7
|
+
* `toHelloArgs` maps it 1:1 onto `buildHelloPayload`'s argument object so there
|
|
8
|
+
* is exactly ONE place that shapes a hello (the cross-PR DRY rule) — the
|
|
9
|
+
* controller spreads the active participant's persona/model fields over this
|
|
10
|
+
* result and feeds the single `buildHelloPayload` call.
|
|
11
|
+
*
|
|
12
|
+
* `kind` is a frozen constant object (no inline string literals).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { buildHelloPayload } from '../audio';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* What the conversation is bound to. `VIDEO` = the Watch surface (`videoId`);
|
|
19
|
+
* `STUDIO_SESSION` = a Studio cast/play surface (`entityId` and/or
|
|
20
|
+
* `adventureSessionId`); `NONE` = an unbound general chat. The values are the
|
|
21
|
+
* `kind` discriminator carried on the binding.
|
|
22
|
+
*/
|
|
23
|
+
export const ConversationContextKind = {
|
|
24
|
+
VIDEO: 'video',
|
|
25
|
+
STUDIO_SESSION: 'studio_session',
|
|
26
|
+
NONE: 'none',
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
29
|
+
export type ConversationContextKind =
|
|
30
|
+
(typeof ConversationContextKind)[keyof typeof ConversationContextKind];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The bound context. Every field below is a `buildHelloPayload` arg, so the
|
|
34
|
+
* binding cannot drift from what the hello can carry. `partyPersonaIds` is the
|
|
35
|
+
* advisory roster (server `AdventureState.party_persona_ids` is authoritative —
|
|
36
|
+
* see `advisoryPartyPersonaIds` in `web/app/chat-ws/adventureMode.ts`); the
|
|
37
|
+
* controller re-derives it from the participant list on add/remove.
|
|
38
|
+
*/
|
|
39
|
+
export interface ConversationContextBinding {
|
|
40
|
+
kind: ConversationContextKind;
|
|
41
|
+
videoId?: string;
|
|
42
|
+
entityId?: string;
|
|
43
|
+
adventureSessionId?: string;
|
|
44
|
+
partyPersonaIds?: string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The exact argument object `buildHelloPayload` accepts. Derived from the
|
|
49
|
+
* function's own parameter type so this can never drift from audio.ts — there is
|
|
50
|
+
* one builder and one arg shape.
|
|
51
|
+
*/
|
|
52
|
+
export type HelloArgs = Parameters<typeof buildHelloPayload>[0];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Map a binding onto the context subset of `buildHelloPayload`'s args. PURE — no
|
|
56
|
+
* I/O. Only the context-derived fields are set here; the controller overlays the
|
|
57
|
+
* active participant's `personaId` / `modelId` (and any audio knobs) before the
|
|
58
|
+
* single `buildHelloPayload` call. Returning a partial keeps every default in
|
|
59
|
+
* `buildHelloPayload` (the one place defaults live).
|
|
60
|
+
*
|
|
61
|
+
* An empty `partyPersonaIds` is dropped so the wire stays byte-identical for
|
|
62
|
+
* ordinary chat and single-DM adventures — same land-dark contract as
|
|
63
|
+
* `advisoryPartyPersonaIds`. The falsy-drop is INTENTIONAL across all three id
|
|
64
|
+
* fields too: an empty-string id is treated as "unset" (ids are real
|
|
65
|
+
* UUIDs/handles — an `''` is a caller mistake, not a value to send), keeping the
|
|
66
|
+
* hello free of meaningless empty fields.
|
|
67
|
+
*/
|
|
68
|
+
export function toHelloArgs(
|
|
69
|
+
binding: ConversationContextBinding,
|
|
70
|
+
): Pick<HelloArgs, 'videoId' | 'entityId' | 'adventureSessionId' | 'partyPersonaIds'> {
|
|
71
|
+
return {
|
|
72
|
+
...(binding.videoId ? { videoId: binding.videoId } : {}),
|
|
73
|
+
...(binding.entityId ? { entityId: binding.entityId } : {}),
|
|
74
|
+
...(binding.adventureSessionId
|
|
75
|
+
? { adventureSessionId: binding.adventureSessionId }
|
|
76
|
+
: {}),
|
|
77
|
+
...(binding.partyPersonaIds && binding.partyPersonaIds.length > 0
|
|
78
|
+
? { partyPersonaIds: [...binding.partyPersonaIds] }
|
|
79
|
+
: {}),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Headless conversation orchestrator (R2.1).
|
|
3
|
+
*
|
|
4
|
+
* The framework-agnostic owner of a conversation's STATE — participants, the
|
|
5
|
+
* active one, the bound context, the model override — and a THIN state+reducer
|
|
6
|
+
* wrapper over the existing `ChatWebSocket` (websocket.ts). It does NOT
|
|
7
|
+
* re-implement transport, reconnect, or the wire protocol: it delegates connect/
|
|
8
|
+
* send to `ChatWebSocket` and builds the hello ONLY via `buildHelloPayload`
|
|
9
|
+
* (audio.ts) — the single hello-builder (the cross-PR DRY rule). React (R2.2)
|
|
10
|
+
* and PlayCanvas (R7) both drive this same controller and render its snapshots;
|
|
11
|
+
* the controller emits a typed `onState` callback, never framework events.
|
|
12
|
+
*
|
|
13
|
+
* Socket ownership: by default the controller OWNS one `ChatWebSocket` and
|
|
14
|
+
* disposes it on `dispose()`. When constructed with `existingSocket` it ADOPTS
|
|
15
|
+
* that socket instead — opens no new connection and does NOT close it on dispose
|
|
16
|
+
* (the owner does). That is the Watch shared-socket invariant: Watch's chat and
|
|
17
|
+
* vision must ride one `ChatWebSocket` so the dispatcher reads one `FrameBuffer`
|
|
18
|
+
* (split sockets empty the vision buffer).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { ChatWebSocket, type MessageHandler } from '../websocket';
|
|
22
|
+
import { buildHelloPayload } from '../audio';
|
|
23
|
+
import type { HelloPayload } from '../audio';
|
|
24
|
+
import type { AdapterRegistry, ChatAdapterDescriptor } from '../adapters';
|
|
25
|
+
import type { ChatModelCapability } from '../api';
|
|
26
|
+
import {
|
|
27
|
+
ParticipantRole,
|
|
28
|
+
type ConversationParticipant,
|
|
29
|
+
} from './participant';
|
|
30
|
+
import {
|
|
31
|
+
toHelloArgs,
|
|
32
|
+
type ConversationContextBinding,
|
|
33
|
+
type HelloArgs,
|
|
34
|
+
} from './binding';
|
|
35
|
+
import { resolveEffectiveModel, type EffectiveModel } from './effectiveModel';
|
|
36
|
+
|
|
37
|
+
/** Immutable view of the controller's state, handed to `onState` on each change. */
|
|
38
|
+
export interface ConversationSnapshot {
|
|
39
|
+
participants: readonly ConversationParticipant[];
|
|
40
|
+
activeParticipantId: string | undefined;
|
|
41
|
+
binding: ConversationContextBinding;
|
|
42
|
+
modelOverride: string | undefined;
|
|
43
|
+
/** True once `connect()` has driven a hello onto the socket. */
|
|
44
|
+
connected: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** One prior turn, in the wire shape `ChatWebSocket.send`'s `messages` expects. */
|
|
48
|
+
export interface ConversationHistoryMessage {
|
|
49
|
+
role: string;
|
|
50
|
+
content: string;
|
|
51
|
+
modelId?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ConversationSendOptions {
|
|
55
|
+
/**
|
|
56
|
+
* Prior turns to ride on THIS send. The backend builds `conversation_history`
|
|
57
|
+
* solely from each send's `messages` field (`api/chat/ws.py`) — omit it and
|
|
58
|
+
* the turn is treated as a first turn. The controller stays message-LESS (it
|
|
59
|
+
* owns context, not chat history), so the renderer that owns the transcript
|
|
60
|
+
* passes its prior turns here per send, exactly like the chat-ws page does.
|
|
61
|
+
*/
|
|
62
|
+
messages?: ConversationHistoryMessage[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ConversationControllerOptions {
|
|
66
|
+
binding: ConversationContextBinding;
|
|
67
|
+
participants?: ConversationParticipant[];
|
|
68
|
+
/** The R1 chat adapter registry — the capability source for the model gate. */
|
|
69
|
+
registry: AdapterRegistry<ChatAdapterDescriptor>;
|
|
70
|
+
/** Initial explicit model override, if any. */
|
|
71
|
+
modelOverride?: string;
|
|
72
|
+
/** Typed state callback. No framework events — the renderer subscribes here. */
|
|
73
|
+
onState?: (snapshot: ConversationSnapshot) => void;
|
|
74
|
+
/**
|
|
75
|
+
* Options for the `ChatWebSocket` the controller opens. Ignored when
|
|
76
|
+
* `existingSocket` is supplied (the adopted socket is already configured).
|
|
77
|
+
*/
|
|
78
|
+
socketOptions?: ConstructorParameters<typeof ChatWebSocket>[0];
|
|
79
|
+
/**
|
|
80
|
+
* An already-open `ChatWebSocket` to ADOPT. When set, `connect()` opens no new
|
|
81
|
+
* connection and `dispose()` does NOT close it (the Watch shared-socket
|
|
82
|
+
* invariant — the owner manages its lifecycle).
|
|
83
|
+
*/
|
|
84
|
+
existingSocket?: ChatWebSocket;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export class ConversationController {
|
|
88
|
+
private participants: ConversationParticipant[];
|
|
89
|
+
private activeParticipantId: string | undefined;
|
|
90
|
+
private binding: ConversationContextBinding;
|
|
91
|
+
private modelOverride: string | undefined;
|
|
92
|
+
private connected = false;
|
|
93
|
+
/** In-flight `connect()` promise — dedups concurrent callers so exactly one
|
|
94
|
+
* hello is sent even if `connect()` races with itself. */
|
|
95
|
+
private connectPromise: Promise<void> | undefined;
|
|
96
|
+
/** Set by `dispose()`. Guards `doConnect()` from sending a hello onto a
|
|
97
|
+
* socket the owner already tore down mid-connect. */
|
|
98
|
+
private disposed = false;
|
|
99
|
+
|
|
100
|
+
private readonly registry: AdapterRegistry<ChatAdapterDescriptor>;
|
|
101
|
+
private readonly onState?: (snapshot: ConversationSnapshot) => void;
|
|
102
|
+
private readonly socket: ChatWebSocket;
|
|
103
|
+
/** True iff the controller created `socket` (and must dispose it). */
|
|
104
|
+
private readonly ownsSocket: boolean;
|
|
105
|
+
|
|
106
|
+
constructor(opts: ConversationControllerOptions) {
|
|
107
|
+
this.participants = [...(opts.participants ?? [])];
|
|
108
|
+
this.activeParticipantId = this.participants[0]?.participantId;
|
|
109
|
+
this.registry = opts.registry;
|
|
110
|
+
this.modelOverride = opts.modelOverride;
|
|
111
|
+
this.onState = opts.onState;
|
|
112
|
+
|
|
113
|
+
if (opts.existingSocket) {
|
|
114
|
+
this.socket = opts.existingSocket;
|
|
115
|
+
this.ownsSocket = false;
|
|
116
|
+
} else {
|
|
117
|
+
// Fail loud at construction rather than cryptically at connect() time with
|
|
118
|
+
// an invalid-URL error: an owned controller needs real socket options.
|
|
119
|
+
if (!opts.socketOptions) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
'ConversationController: socketOptions is required when no existingSocket is provided.',
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
this.socket = new ChatWebSocket(opts.socketOptions);
|
|
125
|
+
this.ownsSocket = true;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Seed the binding with the party roster derived from the initial roster so
|
|
129
|
+
// the first hello carries it (re-derived again on every add/remove).
|
|
130
|
+
this.binding = this.bindingWithDerivedParty(opts.binding);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Open the conversation: drive exactly ONE hello onto the socket, built only
|
|
135
|
+
* via `buildHelloPayload(this.toHelloArgs())`. An OWNED socket is dialed
|
|
136
|
+
* first; an ADOPTED socket is already open, so we only send the hello onto it
|
|
137
|
+
* (no second connection — the Watch invariant).
|
|
138
|
+
*
|
|
139
|
+
* IDEMPOTENT + race-safe: returns immediately when already connected, and
|
|
140
|
+
* dedups a concurrent in-flight `connect()` via `connectPromise` so two callers
|
|
141
|
+
* that both enter before `connected` flips still send exactly ONE hello (and an
|
|
142
|
+
* owned controller never opens a second socket).
|
|
143
|
+
*/
|
|
144
|
+
async connect(): Promise<void> {
|
|
145
|
+
if (this.connected) return;
|
|
146
|
+
if (this.connectPromise) return this.connectPromise;
|
|
147
|
+
this.connectPromise = this.doConnect();
|
|
148
|
+
try {
|
|
149
|
+
await this.connectPromise;
|
|
150
|
+
} finally {
|
|
151
|
+
this.connectPromise = undefined;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private async doConnect(): Promise<void> {
|
|
156
|
+
if (this.ownsSocket) {
|
|
157
|
+
await this.socket.connect();
|
|
158
|
+
}
|
|
159
|
+
// If dispose() ran while we were dialing, don't send a hello onto the
|
|
160
|
+
// now-disconnected socket (a dispose-during-connect race).
|
|
161
|
+
if (this.disposed) return;
|
|
162
|
+
this.socket.sendHello(this.buildHello());
|
|
163
|
+
this.connected = true;
|
|
164
|
+
this.emit();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Build the hello payload for the current binding + active participant. The
|
|
169
|
+
* SINGLE hello-builder: every field flows through `buildHelloPayload`, and the
|
|
170
|
+
* args flow through `toHelloArgs` (the one binding→hello mapper). The active
|
|
171
|
+
* participant overlays `personaId` / `modelId`, where `modelId` is `override`
|
|
172
|
+
* if set, else the participant's bound model. Auto-switch is NOT applied here:
|
|
173
|
+
* the R2.2 consumer calls `resolveEffectiveModel(needed)`, and on `auto_switch`
|
|
174
|
+
* calls `setModelOverride()` BEFORE `connect()` — so the override path above is
|
|
175
|
+
* what carries an auto-switched model onto the wire.
|
|
176
|
+
*/
|
|
177
|
+
buildHello(): HelloPayload {
|
|
178
|
+
const active = this.activeParticipant();
|
|
179
|
+
// Effective model = override > active participant's bound model. Reuse the
|
|
180
|
+
// already-resolved `active` rather than re-scanning via a helper.
|
|
181
|
+
const modelId = this.modelOverride ?? active?.modelId;
|
|
182
|
+
return buildHelloPayload({
|
|
183
|
+
...this.toHelloArgs(),
|
|
184
|
+
...(active?.personaId ? { personaId: active.personaId } : {}),
|
|
185
|
+
...(modelId ? { modelId } : {}),
|
|
186
|
+
// TEXT mode, always (R2.3). The controller owns TEXT conversations — the
|
|
187
|
+
// R2.2 scoping finding keeps voice (and its rich `tts_stt` hello with the
|
|
188
|
+
// audio knobs) page-owned. `buildHelloPayload` defaults `mode` to
|
|
189
|
+
// `'tts_stt'`, which the backend routes into the full STT/TTS voice
|
|
190
|
+
// pipeline (`api/chat/ws.py`: `hello.mode == "tts_stt"` →
|
|
191
|
+
// `_handle_audio_session`, then `break`s out of the text loop). A `text`
|
|
192
|
+
// hello is a context/ack-only handshake — exactly what a
|
|
193
|
+
// controller-driven conversation needs.
|
|
194
|
+
mode: 'text',
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The binding→hello-args mapping (delegates to the single mapper in binding.ts). */
|
|
199
|
+
toHelloArgs(): ReturnType<typeof toHelloArgs> {
|
|
200
|
+
return toHelloArgs(this.binding);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Send a chat turn. Threads the bound context (`videoId` / `entityId` /
|
|
205
|
+
* `adventureSessionId` / advisory `partyPersonaIds`), the active persona, the
|
|
206
|
+
* effective model id, and the renderer-owned `opts.messages` history onto
|
|
207
|
+
* `ChatWebSocket.send` — the controller never builds its own send frame.
|
|
208
|
+
*/
|
|
209
|
+
send(text: string, opts?: ConversationSendOptions): void {
|
|
210
|
+
// Fail loud: a send before connect() would push a frame onto a socket that
|
|
211
|
+
// never received the hello context (silent message loss otherwise).
|
|
212
|
+
if (!this.connected) {
|
|
213
|
+
throw new Error('ConversationController: call connect() before send().');
|
|
214
|
+
}
|
|
215
|
+
const active = this.activeParticipant();
|
|
216
|
+
const args = this.toHelloArgs();
|
|
217
|
+
const modelId = this.modelOverride ?? active?.modelId;
|
|
218
|
+
this.socket.send(text, {
|
|
219
|
+
...(args.videoId ? { videoId: args.videoId } : {}),
|
|
220
|
+
...(args.entityId ? { entityId: args.entityId } : {}),
|
|
221
|
+
...(args.adventureSessionId
|
|
222
|
+
? { adventureSessionId: args.adventureSessionId }
|
|
223
|
+
: {}),
|
|
224
|
+
...(args.partyPersonaIds ? { partyPersonaIds: args.partyPersonaIds } : {}),
|
|
225
|
+
...(active?.personaId ? { personaId: active.personaId } : {}),
|
|
226
|
+
...(modelId ? { modelId } : {}),
|
|
227
|
+
// Omitted when empty so a zero-history send stays byte-identical to the
|
|
228
|
+
// pre-0.30.0 wire (the backend defaults a missing `messages` to []).
|
|
229
|
+
...(opts?.messages?.length ? { messages: opts.messages } : {}),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Subscribe to the conversation socket's inbound JSON messages (`token`,
|
|
235
|
+
* `citation`, `done`, `error`, `turn_changed`, …) and return an unsubscribe
|
|
236
|
+
* function (R2.3, additive). A thin passthrough to
|
|
237
|
+
* `ChatWebSocket.onMessage`/`offMessage` — the controller stays message-LESS
|
|
238
|
+
* (it owns context/state, not chat history), but a renderer (the React panel,
|
|
239
|
+
* a PlayCanvas scene) needs a seam to consume reply events without reaching
|
|
240
|
+
* into a socket the controller may privately own. Works for both owned and
|
|
241
|
+
* adopted sockets; on an adopted socket this is equivalent to subscribing on
|
|
242
|
+
* the socket directly.
|
|
243
|
+
*/
|
|
244
|
+
onMessage(handler: MessageHandler): () => void {
|
|
245
|
+
this.socket.onMessage(handler);
|
|
246
|
+
return () => this.socket.offMessage(handler);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The conversation's `ChatWebSocket`, for a `VoiceSession` (R7.5) to drive the
|
|
251
|
+
* voice round-trip over the SAME socket (one socket ⇒ one `FrameBuffer` — the
|
|
252
|
+
* Watch shared-socket invariant). Additive seam: the renderer constructs a
|
|
253
|
+
* `VoiceSession` against this socket + the controller's `toHelloArgs()` (so
|
|
254
|
+
* voice rides the single hello-builder + the active binding/participant), and
|
|
255
|
+
* the `VoiceSession` NEVER closes it (the controller / adopting surface owns
|
|
256
|
+
* the transport). Works for both owned and adopted sockets.
|
|
257
|
+
*/
|
|
258
|
+
voiceSocket(): ChatWebSocket {
|
|
259
|
+
return this.socket;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* The hello-args for the active binding + participant overlay — the SAME shape
|
|
264
|
+
* `buildHello()` spreads, exposed so a `VoiceSession` (R7.5) layers its voice
|
|
265
|
+
* knobs on top through the single `buildHelloPayload`. Includes the active
|
|
266
|
+
* participant's `personaId`/`modelId` (override-or-bound) so the voice hello
|
|
267
|
+
* is scoped identically to the text one. No second hello-construction site.
|
|
268
|
+
*/
|
|
269
|
+
voiceHelloArgs(): ReturnType<typeof toHelloArgs> &
|
|
270
|
+
Pick<HelloArgs, 'personaId' | 'modelId'> {
|
|
271
|
+
const active = this.activeParticipant();
|
|
272
|
+
const modelId = this.modelOverride ?? active?.modelId;
|
|
273
|
+
return {
|
|
274
|
+
...this.toHelloArgs(),
|
|
275
|
+
...(active?.personaId ? { personaId: active.personaId } : {}),
|
|
276
|
+
...(modelId ? { modelId } : {}),
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Set an explicit model override (wins over bound / auto-switch). */
|
|
281
|
+
setModelOverride(modelId: string): void {
|
|
282
|
+
this.modelOverride = modelId;
|
|
283
|
+
this.emit();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Clear the override — the effective model reverts to bound / auto-switch. */
|
|
287
|
+
clearModelOverride(): void {
|
|
288
|
+
this.modelOverride = undefined;
|
|
289
|
+
this.emit();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Resolve the effective model for the active participant given the requested
|
|
294
|
+
* capabilities (e.g. `['vision']` when the webcam is ON). Delegates to the
|
|
295
|
+
* single `resolveEffectiveModel` helper — the controller holds no capability
|
|
296
|
+
* logic of its own. Returns `undefined` when there is no active model-backed
|
|
297
|
+
* participant to gate.
|
|
298
|
+
*/
|
|
299
|
+
resolveEffectiveModel(
|
|
300
|
+
needed: readonly ChatModelCapability[] = [],
|
|
301
|
+
): EffectiveModel | undefined {
|
|
302
|
+
const boundModelId = this.activeParticipant()?.modelId;
|
|
303
|
+
if (!boundModelId) {
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
return resolveEffectiveModel({
|
|
307
|
+
boundModelId,
|
|
308
|
+
needed,
|
|
309
|
+
override: this.modelOverride,
|
|
310
|
+
registry: this.registry,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Add a participant and re-derive the advisory `partyPersonaIds` for the next
|
|
316
|
+
* hello (matches `advisoryPartyPersonaIds` semantics). First participant
|
|
317
|
+
* becomes active. THROWS on a duplicate `participantId` — additive, no silent
|
|
318
|
+
* overwrite (the registry discipline).
|
|
319
|
+
*/
|
|
320
|
+
addParticipant(participant: ConversationParticipant): void {
|
|
321
|
+
if (this.participants.some((p) => p.participantId === participant.participantId)) {
|
|
322
|
+
throw new Error(
|
|
323
|
+
`ConversationController: duplicate participantId "${participant.participantId}".`,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
this.participants.push(participant);
|
|
327
|
+
if (this.activeParticipantId === undefined) {
|
|
328
|
+
this.activeParticipantId = participant.participantId;
|
|
329
|
+
}
|
|
330
|
+
this.rederiveParty();
|
|
331
|
+
this.emit();
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Remove a participant by id and re-derive `partyPersonaIds`. If the removed
|
|
336
|
+
* one was active, the active pointer falls to the first remaining participant
|
|
337
|
+
* (or `undefined`). **No-op (idempotent) if `participantId` is not present** —
|
|
338
|
+
* deliberately asymmetric with `addParticipant`, which throws on a duplicate.
|
|
339
|
+
*/
|
|
340
|
+
removeParticipant(participantId: string): void {
|
|
341
|
+
this.participants = this.participants.filter(
|
|
342
|
+
(p) => p.participantId !== participantId,
|
|
343
|
+
);
|
|
344
|
+
if (this.activeParticipantId === participantId) {
|
|
345
|
+
this.activeParticipantId = this.participants[0]?.participantId;
|
|
346
|
+
}
|
|
347
|
+
this.rederiveParty();
|
|
348
|
+
this.emit();
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Point the active participant at `participantId`. THROWS on an unknown id —
|
|
353
|
+
* unlike `removeParticipant` (idempotent no-op), you cannot make an absent
|
|
354
|
+
* participant active, so a typo'd id is a caller error, not a tolerable no-op
|
|
355
|
+
* (the fail-loud discipline, consistent with `addParticipant`).
|
|
356
|
+
*/
|
|
357
|
+
setActiveParticipant(participantId: string): void {
|
|
358
|
+
if (!this.participants.some((p) => p.participantId === participantId)) {
|
|
359
|
+
throw new Error(
|
|
360
|
+
`ConversationController: cannot set active to unknown participantId "${participantId}".`,
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
this.activeParticipantId = participantId;
|
|
364
|
+
this.emit();
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Current immutable snapshot. Both the participant objects (and their
|
|
368
|
+
* `capabilities` array) and the binding's `partyPersonaIds` array are COPIED
|
|
369
|
+
* so mutating the snapshot can't reach into the controller's internal state. */
|
|
370
|
+
snapshot(): ConversationSnapshot {
|
|
371
|
+
return {
|
|
372
|
+
participants: this.participants.map((p) => ({
|
|
373
|
+
...p,
|
|
374
|
+
...(p.capabilities ? { capabilities: [...p.capabilities] } : {}),
|
|
375
|
+
})),
|
|
376
|
+
activeParticipantId: this.activeParticipantId,
|
|
377
|
+
binding: {
|
|
378
|
+
...this.binding,
|
|
379
|
+
...(this.binding.partyPersonaIds
|
|
380
|
+
? { partyPersonaIds: [...this.binding.partyPersonaIds] }
|
|
381
|
+
: {}),
|
|
382
|
+
},
|
|
383
|
+
modelOverride: this.modelOverride,
|
|
384
|
+
connected: this.connected,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Tear down. Disconnects the socket ONLY when the controller owns it — an
|
|
390
|
+
* ADOPTED socket is left untouched (the owner disposes it; the Watch
|
|
391
|
+
* shared-socket invariant). Idempotent.
|
|
392
|
+
*/
|
|
393
|
+
dispose(): void {
|
|
394
|
+
this.disposed = true;
|
|
395
|
+
this.connected = false;
|
|
396
|
+
if (this.ownsSocket) {
|
|
397
|
+
this.socket.disconnect();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// ── internals ────────────────────────────────────────────────────────────
|
|
402
|
+
|
|
403
|
+
private activeParticipant(): ConversationParticipant | undefined {
|
|
404
|
+
return this.participants.find(
|
|
405
|
+
(p) => p.participantId === this.activeParticipantId,
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Advisory party persona ids derived from the persona participants (the `dm`
|
|
411
|
+
* and `user` seats are excluded — only party members route scenes). Mirrors
|
|
412
|
+
* `advisoryPartyPersonaIds`: returns `undefined` when empty so the hello drops
|
|
413
|
+
* the key and stays byte-identical for ordinary chat / single-DM adventures.
|
|
414
|
+
*/
|
|
415
|
+
private derivePartyPersonaIds(): string[] | undefined {
|
|
416
|
+
const ids = this.participants
|
|
417
|
+
.filter((p) => p.role === ParticipantRole.PERSONA && Boolean(p.personaId))
|
|
418
|
+
.map((p) => p.personaId as string);
|
|
419
|
+
return ids.length > 0 ? ids : undefined;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
private bindingWithDerivedParty(
|
|
423
|
+
binding: ConversationContextBinding,
|
|
424
|
+
): ConversationContextBinding {
|
|
425
|
+
const partyPersonaIds = this.derivePartyPersonaIds();
|
|
426
|
+
return {
|
|
427
|
+
...binding,
|
|
428
|
+
...(partyPersonaIds ? { partyPersonaIds } : { partyPersonaIds: undefined }),
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private rederiveParty(): void {
|
|
433
|
+
this.binding = this.bindingWithDerivedParty(this.binding);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private emit(): void {
|
|
437
|
+
this.onState?.(this.snapshot());
|
|
438
|
+
}
|
|
439
|
+
}
|