@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,209 @@
|
|
|
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
|
+
import { ChatWebSocket, type MessageHandler } from '../websocket';
|
|
21
|
+
import type { HelloPayload } from '../audio';
|
|
22
|
+
import type { AdapterRegistry, ChatAdapterDescriptor } from '../adapters';
|
|
23
|
+
import type { ChatModelCapability } from '../api';
|
|
24
|
+
import { type ConversationParticipant } from './participant';
|
|
25
|
+
import { toHelloArgs, type ConversationContextBinding, type HelloArgs } from './binding';
|
|
26
|
+
import { type EffectiveModel } from './effectiveModel';
|
|
27
|
+
/** Immutable view of the controller's state, handed to `onState` on each change. */
|
|
28
|
+
export interface ConversationSnapshot {
|
|
29
|
+
participants: readonly ConversationParticipant[];
|
|
30
|
+
activeParticipantId: string | undefined;
|
|
31
|
+
binding: ConversationContextBinding;
|
|
32
|
+
modelOverride: string | undefined;
|
|
33
|
+
/** True once `connect()` has driven a hello onto the socket. */
|
|
34
|
+
connected: boolean;
|
|
35
|
+
}
|
|
36
|
+
/** One prior turn, in the wire shape `ChatWebSocket.send`'s `messages` expects. */
|
|
37
|
+
export interface ConversationHistoryMessage {
|
|
38
|
+
role: string;
|
|
39
|
+
content: string;
|
|
40
|
+
modelId?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface ConversationSendOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Prior turns to ride on THIS send. The backend builds `conversation_history`
|
|
45
|
+
* solely from each send's `messages` field (`api/chat/ws.py`) — omit it and
|
|
46
|
+
* the turn is treated as a first turn. The controller stays message-LESS (it
|
|
47
|
+
* owns context, not chat history), so the renderer that owns the transcript
|
|
48
|
+
* passes its prior turns here per send, exactly like the chat-ws page does.
|
|
49
|
+
*/
|
|
50
|
+
messages?: ConversationHistoryMessage[];
|
|
51
|
+
}
|
|
52
|
+
export interface ConversationControllerOptions {
|
|
53
|
+
binding: ConversationContextBinding;
|
|
54
|
+
participants?: ConversationParticipant[];
|
|
55
|
+
/** The R1 chat adapter registry — the capability source for the model gate. */
|
|
56
|
+
registry: AdapterRegistry<ChatAdapterDescriptor>;
|
|
57
|
+
/** Initial explicit model override, if any. */
|
|
58
|
+
modelOverride?: string;
|
|
59
|
+
/** Typed state callback. No framework events — the renderer subscribes here. */
|
|
60
|
+
onState?: (snapshot: ConversationSnapshot) => void;
|
|
61
|
+
/**
|
|
62
|
+
* Options for the `ChatWebSocket` the controller opens. Ignored when
|
|
63
|
+
* `existingSocket` is supplied (the adopted socket is already configured).
|
|
64
|
+
*/
|
|
65
|
+
socketOptions?: ConstructorParameters<typeof ChatWebSocket>[0];
|
|
66
|
+
/**
|
|
67
|
+
* An already-open `ChatWebSocket` to ADOPT. When set, `connect()` opens no new
|
|
68
|
+
* connection and `dispose()` does NOT close it (the Watch shared-socket
|
|
69
|
+
* invariant — the owner manages its lifecycle).
|
|
70
|
+
*/
|
|
71
|
+
existingSocket?: ChatWebSocket;
|
|
72
|
+
}
|
|
73
|
+
export declare class ConversationController {
|
|
74
|
+
private participants;
|
|
75
|
+
private activeParticipantId;
|
|
76
|
+
private binding;
|
|
77
|
+
private modelOverride;
|
|
78
|
+
private connected;
|
|
79
|
+
/** In-flight `connect()` promise — dedups concurrent callers so exactly one
|
|
80
|
+
* hello is sent even if `connect()` races with itself. */
|
|
81
|
+
private connectPromise;
|
|
82
|
+
/** Set by `dispose()`. Guards `doConnect()` from sending a hello onto a
|
|
83
|
+
* socket the owner already tore down mid-connect. */
|
|
84
|
+
private disposed;
|
|
85
|
+
private readonly registry;
|
|
86
|
+
private readonly onState?;
|
|
87
|
+
private readonly socket;
|
|
88
|
+
/** True iff the controller created `socket` (and must dispose it). */
|
|
89
|
+
private readonly ownsSocket;
|
|
90
|
+
constructor(opts: ConversationControllerOptions);
|
|
91
|
+
/**
|
|
92
|
+
* Open the conversation: drive exactly ONE hello onto the socket, built only
|
|
93
|
+
* via `buildHelloPayload(this.toHelloArgs())`. An OWNED socket is dialed
|
|
94
|
+
* first; an ADOPTED socket is already open, so we only send the hello onto it
|
|
95
|
+
* (no second connection — the Watch invariant).
|
|
96
|
+
*
|
|
97
|
+
* IDEMPOTENT + race-safe: returns immediately when already connected, and
|
|
98
|
+
* dedups a concurrent in-flight `connect()` via `connectPromise` so two callers
|
|
99
|
+
* that both enter before `connected` flips still send exactly ONE hello (and an
|
|
100
|
+
* owned controller never opens a second socket).
|
|
101
|
+
*/
|
|
102
|
+
connect(): Promise<void>;
|
|
103
|
+
private doConnect;
|
|
104
|
+
/**
|
|
105
|
+
* Build the hello payload for the current binding + active participant. The
|
|
106
|
+
* SINGLE hello-builder: every field flows through `buildHelloPayload`, and the
|
|
107
|
+
* args flow through `toHelloArgs` (the one binding→hello mapper). The active
|
|
108
|
+
* participant overlays `personaId` / `modelId`, where `modelId` is `override`
|
|
109
|
+
* if set, else the participant's bound model. Auto-switch is NOT applied here:
|
|
110
|
+
* the R2.2 consumer calls `resolveEffectiveModel(needed)`, and on `auto_switch`
|
|
111
|
+
* calls `setModelOverride()` BEFORE `connect()` — so the override path above is
|
|
112
|
+
* what carries an auto-switched model onto the wire.
|
|
113
|
+
*/
|
|
114
|
+
buildHello(): HelloPayload;
|
|
115
|
+
/** The binding→hello-args mapping (delegates to the single mapper in binding.ts). */
|
|
116
|
+
toHelloArgs(): ReturnType<typeof toHelloArgs>;
|
|
117
|
+
/**
|
|
118
|
+
* Send a chat turn. Threads the bound context (`videoId` / `entityId` /
|
|
119
|
+
* `adventureSessionId` / advisory `partyPersonaIds`), the active persona, the
|
|
120
|
+
* effective model id, and the renderer-owned `opts.messages` history onto
|
|
121
|
+
* `ChatWebSocket.send` — the controller never builds its own send frame.
|
|
122
|
+
*/
|
|
123
|
+
send(text: string, opts?: ConversationSendOptions): void;
|
|
124
|
+
/**
|
|
125
|
+
* Subscribe to the conversation socket's inbound JSON messages (`token`,
|
|
126
|
+
* `citation`, `done`, `error`, `turn_changed`, …) and return an unsubscribe
|
|
127
|
+
* function (R2.3, additive). A thin passthrough to
|
|
128
|
+
* `ChatWebSocket.onMessage`/`offMessage` — the controller stays message-LESS
|
|
129
|
+
* (it owns context/state, not chat history), but a renderer (the React panel,
|
|
130
|
+
* a PlayCanvas scene) needs a seam to consume reply events without reaching
|
|
131
|
+
* into a socket the controller may privately own. Works for both owned and
|
|
132
|
+
* adopted sockets; on an adopted socket this is equivalent to subscribing on
|
|
133
|
+
* the socket directly.
|
|
134
|
+
*/
|
|
135
|
+
onMessage(handler: MessageHandler): () => void;
|
|
136
|
+
/**
|
|
137
|
+
* The conversation's `ChatWebSocket`, for a `VoiceSession` (R7.5) to drive the
|
|
138
|
+
* voice round-trip over the SAME socket (one socket ⇒ one `FrameBuffer` — the
|
|
139
|
+
* Watch shared-socket invariant). Additive seam: the renderer constructs a
|
|
140
|
+
* `VoiceSession` against this socket + the controller's `toHelloArgs()` (so
|
|
141
|
+
* voice rides the single hello-builder + the active binding/participant), and
|
|
142
|
+
* the `VoiceSession` NEVER closes it (the controller / adopting surface owns
|
|
143
|
+
* the transport). Works for both owned and adopted sockets.
|
|
144
|
+
*/
|
|
145
|
+
voiceSocket(): ChatWebSocket;
|
|
146
|
+
/**
|
|
147
|
+
* The hello-args for the active binding + participant overlay — the SAME shape
|
|
148
|
+
* `buildHello()` spreads, exposed so a `VoiceSession` (R7.5) layers its voice
|
|
149
|
+
* knobs on top through the single `buildHelloPayload`. Includes the active
|
|
150
|
+
* participant's `personaId`/`modelId` (override-or-bound) so the voice hello
|
|
151
|
+
* is scoped identically to the text one. No second hello-construction site.
|
|
152
|
+
*/
|
|
153
|
+
voiceHelloArgs(): ReturnType<typeof toHelloArgs> & Pick<HelloArgs, 'personaId' | 'modelId'>;
|
|
154
|
+
/** Set an explicit model override (wins over bound / auto-switch). */
|
|
155
|
+
setModelOverride(modelId: string): void;
|
|
156
|
+
/** Clear the override — the effective model reverts to bound / auto-switch. */
|
|
157
|
+
clearModelOverride(): void;
|
|
158
|
+
/**
|
|
159
|
+
* Resolve the effective model for the active participant given the requested
|
|
160
|
+
* capabilities (e.g. `['vision']` when the webcam is ON). Delegates to the
|
|
161
|
+
* single `resolveEffectiveModel` helper — the controller holds no capability
|
|
162
|
+
* logic of its own. Returns `undefined` when there is no active model-backed
|
|
163
|
+
* participant to gate.
|
|
164
|
+
*/
|
|
165
|
+
resolveEffectiveModel(needed?: readonly ChatModelCapability[]): EffectiveModel | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* Add a participant and re-derive the advisory `partyPersonaIds` for the next
|
|
168
|
+
* hello (matches `advisoryPartyPersonaIds` semantics). First participant
|
|
169
|
+
* becomes active. THROWS on a duplicate `participantId` — additive, no silent
|
|
170
|
+
* overwrite (the registry discipline).
|
|
171
|
+
*/
|
|
172
|
+
addParticipant(participant: ConversationParticipant): void;
|
|
173
|
+
/**
|
|
174
|
+
* Remove a participant by id and re-derive `partyPersonaIds`. If the removed
|
|
175
|
+
* one was active, the active pointer falls to the first remaining participant
|
|
176
|
+
* (or `undefined`). **No-op (idempotent) if `participantId` is not present** —
|
|
177
|
+
* deliberately asymmetric with `addParticipant`, which throws on a duplicate.
|
|
178
|
+
*/
|
|
179
|
+
removeParticipant(participantId: string): void;
|
|
180
|
+
/**
|
|
181
|
+
* Point the active participant at `participantId`. THROWS on an unknown id —
|
|
182
|
+
* unlike `removeParticipant` (idempotent no-op), you cannot make an absent
|
|
183
|
+
* participant active, so a typo'd id is a caller error, not a tolerable no-op
|
|
184
|
+
* (the fail-loud discipline, consistent with `addParticipant`).
|
|
185
|
+
*/
|
|
186
|
+
setActiveParticipant(participantId: string): void;
|
|
187
|
+
/** Current immutable snapshot. Both the participant objects (and their
|
|
188
|
+
* `capabilities` array) and the binding's `partyPersonaIds` array are COPIED
|
|
189
|
+
* so mutating the snapshot can't reach into the controller's internal state. */
|
|
190
|
+
snapshot(): ConversationSnapshot;
|
|
191
|
+
/**
|
|
192
|
+
* Tear down. Disconnects the socket ONLY when the controller owns it — an
|
|
193
|
+
* ADOPTED socket is left untouched (the owner disposes it; the Watch
|
|
194
|
+
* shared-socket invariant). Idempotent.
|
|
195
|
+
*/
|
|
196
|
+
dispose(): void;
|
|
197
|
+
private activeParticipant;
|
|
198
|
+
/**
|
|
199
|
+
* Advisory party persona ids derived from the persona participants (the `dm`
|
|
200
|
+
* and `user` seats are excluded — only party members route scenes). Mirrors
|
|
201
|
+
* `advisoryPartyPersonaIds`: returns `undefined` when empty so the hello drops
|
|
202
|
+
* the key and stays byte-identical for ordinary chat / single-DM adventures.
|
|
203
|
+
*/
|
|
204
|
+
private derivePartyPersonaIds;
|
|
205
|
+
private bindingWithDerivedParty;
|
|
206
|
+
private rederiveParty;
|
|
207
|
+
private emit;
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/conversation/controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,aAAa,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAElE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7C,OAAO,KAAK,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAClD,OAAO,EAEL,KAAK,uBAAuB,EAC7B,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,WAAW,EACX,KAAK,0BAA0B,EAC/B,KAAK,SAAS,EACf,MAAM,WAAW,CAAC;AACnB,OAAO,EAAyB,KAAK,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAE9E,oFAAoF;AACpF,MAAM,WAAW,oBAAoB;IACnC,YAAY,EAAE,SAAS,uBAAuB,EAAE,CAAC;IACjD,mBAAmB,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,OAAO,EAAE,0BAA0B,CAAC;IACpC,aAAa,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,gEAAgE;IAChE,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,mFAAmF;AACnF,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB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IACrF,WAAW,IAAI,UAAU,CAAC,OAAO,WAAW,CAAC;IAI7C;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,uBAAuB,GAAG,IAAI;IAwBxD;;;;;;;;;;OAUG;IACH,SAAS,CAAC,OAAO,EAAE,cAAc,GAAG,MAAM,IAAI;IAK9C;;;;;;;;OAQG;IACH,WAAW,IAAI,aAAa;IAI5B;;;;;;OAMG;IACH,cAAc,IAAI,UAAU,CAAC,OAAO,WAAW,CAAC,GAC9C,IAAI,CAAC,SAAS,EAAE,WAAW,GAAG,SAAS,CAAC;IAU1C,sEAAsE;IACtE,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAKvC,+EAA+E;IAC/E,kBAAkB,IAAI,IAAI;IAK1B;;;;;;OAMG;IACH,qBAAqB,CACnB,MAAM,GAAE,SAAS,mBAAmB,EAAO,GAC1C,cAAc,GAAG,SAAS;IAa7B;;;;;OAKG;IACH,cAAc,CAAC,WAAW,EAAE,uBAAuB,GAAG,IAAI;IAc1D;;;;;OAKG;IACH,iBAAiB,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI;IAW9C;;;;;OAKG;IACH,oBAAoB,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI;IAUjD;;qFAEiF;IACjF,QAAQ,IAAI,oBAAoB;IAkBhC;;;;OAIG;IACH,OAAO,IAAI,IAAI;IAUf,OAAO,CAAC,iBAAiB;IAMzB;;;;;OAKG;IACH,OAAO,CAAC,qBAAqB;IAO7B,OAAO,CAAC,uBAAuB;IAU/B,OAAO,CAAC,aAAa;IAIrB,OAAO,CAAC,IAAI;CAGb"}
|
|
@@ -0,0 +1,342 @@
|
|
|
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
|
+
import { ChatWebSocket } from '../websocket.js';
|
|
21
|
+
import { buildHelloPayload } from '../audio.js';
|
|
22
|
+
import { ParticipantRole, } from './participant.js';
|
|
23
|
+
import { toHelloArgs, } from './binding.js';
|
|
24
|
+
import { resolveEffectiveModel } from './effectiveModel.js';
|
|
25
|
+
export class ConversationController {
|
|
26
|
+
participants;
|
|
27
|
+
activeParticipantId;
|
|
28
|
+
binding;
|
|
29
|
+
modelOverride;
|
|
30
|
+
connected = false;
|
|
31
|
+
/** In-flight `connect()` promise — dedups concurrent callers so exactly one
|
|
32
|
+
* hello is sent even if `connect()` races with itself. */
|
|
33
|
+
connectPromise;
|
|
34
|
+
/** Set by `dispose()`. Guards `doConnect()` from sending a hello onto a
|
|
35
|
+
* socket the owner already tore down mid-connect. */
|
|
36
|
+
disposed = false;
|
|
37
|
+
registry;
|
|
38
|
+
onState;
|
|
39
|
+
socket;
|
|
40
|
+
/** True iff the controller created `socket` (and must dispose it). */
|
|
41
|
+
ownsSocket;
|
|
42
|
+
constructor(opts) {
|
|
43
|
+
this.participants = [...(opts.participants ?? [])];
|
|
44
|
+
this.activeParticipantId = this.participants[0]?.participantId;
|
|
45
|
+
this.registry = opts.registry;
|
|
46
|
+
this.modelOverride = opts.modelOverride;
|
|
47
|
+
this.onState = opts.onState;
|
|
48
|
+
if (opts.existingSocket) {
|
|
49
|
+
this.socket = opts.existingSocket;
|
|
50
|
+
this.ownsSocket = false;
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
// Fail loud at construction rather than cryptically at connect() time with
|
|
54
|
+
// an invalid-URL error: an owned controller needs real socket options.
|
|
55
|
+
if (!opts.socketOptions) {
|
|
56
|
+
throw new Error('ConversationController: socketOptions is required when no existingSocket is provided.');
|
|
57
|
+
}
|
|
58
|
+
this.socket = new ChatWebSocket(opts.socketOptions);
|
|
59
|
+
this.ownsSocket = true;
|
|
60
|
+
}
|
|
61
|
+
// Seed the binding with the party roster derived from the initial roster so
|
|
62
|
+
// the first hello carries it (re-derived again on every add/remove).
|
|
63
|
+
this.binding = this.bindingWithDerivedParty(opts.binding);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Open the conversation: drive exactly ONE hello onto the socket, built only
|
|
67
|
+
* via `buildHelloPayload(this.toHelloArgs())`. An OWNED socket is dialed
|
|
68
|
+
* first; an ADOPTED socket is already open, so we only send the hello onto it
|
|
69
|
+
* (no second connection — the Watch invariant).
|
|
70
|
+
*
|
|
71
|
+
* IDEMPOTENT + race-safe: returns immediately when already connected, and
|
|
72
|
+
* dedups a concurrent in-flight `connect()` via `connectPromise` so two callers
|
|
73
|
+
* that both enter before `connected` flips still send exactly ONE hello (and an
|
|
74
|
+
* owned controller never opens a second socket).
|
|
75
|
+
*/
|
|
76
|
+
async connect() {
|
|
77
|
+
if (this.connected)
|
|
78
|
+
return;
|
|
79
|
+
if (this.connectPromise)
|
|
80
|
+
return this.connectPromise;
|
|
81
|
+
this.connectPromise = this.doConnect();
|
|
82
|
+
try {
|
|
83
|
+
await this.connectPromise;
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
this.connectPromise = undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async doConnect() {
|
|
90
|
+
if (this.ownsSocket) {
|
|
91
|
+
await this.socket.connect();
|
|
92
|
+
}
|
|
93
|
+
// If dispose() ran while we were dialing, don't send a hello onto the
|
|
94
|
+
// now-disconnected socket (a dispose-during-connect race).
|
|
95
|
+
if (this.disposed)
|
|
96
|
+
return;
|
|
97
|
+
this.socket.sendHello(this.buildHello());
|
|
98
|
+
this.connected = true;
|
|
99
|
+
this.emit();
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Build the hello payload for the current binding + active participant. The
|
|
103
|
+
* SINGLE hello-builder: every field flows through `buildHelloPayload`, and the
|
|
104
|
+
* args flow through `toHelloArgs` (the one binding→hello mapper). The active
|
|
105
|
+
* participant overlays `personaId` / `modelId`, where `modelId` is `override`
|
|
106
|
+
* if set, else the participant's bound model. Auto-switch is NOT applied here:
|
|
107
|
+
* the R2.2 consumer calls `resolveEffectiveModel(needed)`, and on `auto_switch`
|
|
108
|
+
* calls `setModelOverride()` BEFORE `connect()` — so the override path above is
|
|
109
|
+
* what carries an auto-switched model onto the wire.
|
|
110
|
+
*/
|
|
111
|
+
buildHello() {
|
|
112
|
+
const active = this.activeParticipant();
|
|
113
|
+
// Effective model = override > active participant's bound model. Reuse the
|
|
114
|
+
// already-resolved `active` rather than re-scanning via a helper.
|
|
115
|
+
const modelId = this.modelOverride ?? active?.modelId;
|
|
116
|
+
return buildHelloPayload({
|
|
117
|
+
...this.toHelloArgs(),
|
|
118
|
+
...(active?.personaId ? { personaId: active.personaId } : {}),
|
|
119
|
+
...(modelId ? { modelId } : {}),
|
|
120
|
+
// TEXT mode, always (R2.3). The controller owns TEXT conversations — the
|
|
121
|
+
// R2.2 scoping finding keeps voice (and its rich `tts_stt` hello with the
|
|
122
|
+
// audio knobs) page-owned. `buildHelloPayload` defaults `mode` to
|
|
123
|
+
// `'tts_stt'`, which the backend routes into the full STT/TTS voice
|
|
124
|
+
// pipeline (`api/chat/ws.py`: `hello.mode == "tts_stt"` →
|
|
125
|
+
// `_handle_audio_session`, then `break`s out of the text loop). A `text`
|
|
126
|
+
// hello is a context/ack-only handshake — exactly what a
|
|
127
|
+
// controller-driven conversation needs.
|
|
128
|
+
mode: 'text',
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
/** The binding→hello-args mapping (delegates to the single mapper in binding.ts). */
|
|
132
|
+
toHelloArgs() {
|
|
133
|
+
return toHelloArgs(this.binding);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Send a chat turn. Threads the bound context (`videoId` / `entityId` /
|
|
137
|
+
* `adventureSessionId` / advisory `partyPersonaIds`), the active persona, the
|
|
138
|
+
* effective model id, and the renderer-owned `opts.messages` history onto
|
|
139
|
+
* `ChatWebSocket.send` — the controller never builds its own send frame.
|
|
140
|
+
*/
|
|
141
|
+
send(text, opts) {
|
|
142
|
+
// Fail loud: a send before connect() would push a frame onto a socket that
|
|
143
|
+
// never received the hello context (silent message loss otherwise).
|
|
144
|
+
if (!this.connected) {
|
|
145
|
+
throw new Error('ConversationController: call connect() before send().');
|
|
146
|
+
}
|
|
147
|
+
const active = this.activeParticipant();
|
|
148
|
+
const args = this.toHelloArgs();
|
|
149
|
+
const modelId = this.modelOverride ?? active?.modelId;
|
|
150
|
+
this.socket.send(text, {
|
|
151
|
+
...(args.videoId ? { videoId: args.videoId } : {}),
|
|
152
|
+
...(args.entityId ? { entityId: args.entityId } : {}),
|
|
153
|
+
...(args.adventureSessionId
|
|
154
|
+
? { adventureSessionId: args.adventureSessionId }
|
|
155
|
+
: {}),
|
|
156
|
+
...(args.partyPersonaIds ? { partyPersonaIds: args.partyPersonaIds } : {}),
|
|
157
|
+
...(active?.personaId ? { personaId: active.personaId } : {}),
|
|
158
|
+
...(modelId ? { modelId } : {}),
|
|
159
|
+
// Omitted when empty so a zero-history send stays byte-identical to the
|
|
160
|
+
// pre-0.30.0 wire (the backend defaults a missing `messages` to []).
|
|
161
|
+
...(opts?.messages?.length ? { messages: opts.messages } : {}),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Subscribe to the conversation socket's inbound JSON messages (`token`,
|
|
166
|
+
* `citation`, `done`, `error`, `turn_changed`, …) and return an unsubscribe
|
|
167
|
+
* function (R2.3, additive). A thin passthrough to
|
|
168
|
+
* `ChatWebSocket.onMessage`/`offMessage` — the controller stays message-LESS
|
|
169
|
+
* (it owns context/state, not chat history), but a renderer (the React panel,
|
|
170
|
+
* a PlayCanvas scene) needs a seam to consume reply events without reaching
|
|
171
|
+
* into a socket the controller may privately own. Works for both owned and
|
|
172
|
+
* adopted sockets; on an adopted socket this is equivalent to subscribing on
|
|
173
|
+
* the socket directly.
|
|
174
|
+
*/
|
|
175
|
+
onMessage(handler) {
|
|
176
|
+
this.socket.onMessage(handler);
|
|
177
|
+
return () => this.socket.offMessage(handler);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The conversation's `ChatWebSocket`, for a `VoiceSession` (R7.5) to drive the
|
|
181
|
+
* voice round-trip over the SAME socket (one socket ⇒ one `FrameBuffer` — the
|
|
182
|
+
* Watch shared-socket invariant). Additive seam: the renderer constructs a
|
|
183
|
+
* `VoiceSession` against this socket + the controller's `toHelloArgs()` (so
|
|
184
|
+
* voice rides the single hello-builder + the active binding/participant), and
|
|
185
|
+
* the `VoiceSession` NEVER closes it (the controller / adopting surface owns
|
|
186
|
+
* the transport). Works for both owned and adopted sockets.
|
|
187
|
+
*/
|
|
188
|
+
voiceSocket() {
|
|
189
|
+
return this.socket;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The hello-args for the active binding + participant overlay — the SAME shape
|
|
193
|
+
* `buildHello()` spreads, exposed so a `VoiceSession` (R7.5) layers its voice
|
|
194
|
+
* knobs on top through the single `buildHelloPayload`. Includes the active
|
|
195
|
+
* participant's `personaId`/`modelId` (override-or-bound) so the voice hello
|
|
196
|
+
* is scoped identically to the text one. No second hello-construction site.
|
|
197
|
+
*/
|
|
198
|
+
voiceHelloArgs() {
|
|
199
|
+
const active = this.activeParticipant();
|
|
200
|
+
const modelId = this.modelOverride ?? active?.modelId;
|
|
201
|
+
return {
|
|
202
|
+
...this.toHelloArgs(),
|
|
203
|
+
...(active?.personaId ? { personaId: active.personaId } : {}),
|
|
204
|
+
...(modelId ? { modelId } : {}),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/** Set an explicit model override (wins over bound / auto-switch). */
|
|
208
|
+
setModelOverride(modelId) {
|
|
209
|
+
this.modelOverride = modelId;
|
|
210
|
+
this.emit();
|
|
211
|
+
}
|
|
212
|
+
/** Clear the override — the effective model reverts to bound / auto-switch. */
|
|
213
|
+
clearModelOverride() {
|
|
214
|
+
this.modelOverride = undefined;
|
|
215
|
+
this.emit();
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Resolve the effective model for the active participant given the requested
|
|
219
|
+
* capabilities (e.g. `['vision']` when the webcam is ON). Delegates to the
|
|
220
|
+
* single `resolveEffectiveModel` helper — the controller holds no capability
|
|
221
|
+
* logic of its own. Returns `undefined` when there is no active model-backed
|
|
222
|
+
* participant to gate.
|
|
223
|
+
*/
|
|
224
|
+
resolveEffectiveModel(needed = []) {
|
|
225
|
+
const boundModelId = this.activeParticipant()?.modelId;
|
|
226
|
+
if (!boundModelId) {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
return resolveEffectiveModel({
|
|
230
|
+
boundModelId,
|
|
231
|
+
needed,
|
|
232
|
+
override: this.modelOverride,
|
|
233
|
+
registry: this.registry,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Add a participant and re-derive the advisory `partyPersonaIds` for the next
|
|
238
|
+
* hello (matches `advisoryPartyPersonaIds` semantics). First participant
|
|
239
|
+
* becomes active. THROWS on a duplicate `participantId` — additive, no silent
|
|
240
|
+
* overwrite (the registry discipline).
|
|
241
|
+
*/
|
|
242
|
+
addParticipant(participant) {
|
|
243
|
+
if (this.participants.some((p) => p.participantId === participant.participantId)) {
|
|
244
|
+
throw new Error(`ConversationController: duplicate participantId "${participant.participantId}".`);
|
|
245
|
+
}
|
|
246
|
+
this.participants.push(participant);
|
|
247
|
+
if (this.activeParticipantId === undefined) {
|
|
248
|
+
this.activeParticipantId = participant.participantId;
|
|
249
|
+
}
|
|
250
|
+
this.rederiveParty();
|
|
251
|
+
this.emit();
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Remove a participant by id and re-derive `partyPersonaIds`. If the removed
|
|
255
|
+
* one was active, the active pointer falls to the first remaining participant
|
|
256
|
+
* (or `undefined`). **No-op (idempotent) if `participantId` is not present** —
|
|
257
|
+
* deliberately asymmetric with `addParticipant`, which throws on a duplicate.
|
|
258
|
+
*/
|
|
259
|
+
removeParticipant(participantId) {
|
|
260
|
+
this.participants = this.participants.filter((p) => p.participantId !== participantId);
|
|
261
|
+
if (this.activeParticipantId === participantId) {
|
|
262
|
+
this.activeParticipantId = this.participants[0]?.participantId;
|
|
263
|
+
}
|
|
264
|
+
this.rederiveParty();
|
|
265
|
+
this.emit();
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Point the active participant at `participantId`. THROWS on an unknown id —
|
|
269
|
+
* unlike `removeParticipant` (idempotent no-op), you cannot make an absent
|
|
270
|
+
* participant active, so a typo'd id is a caller error, not a tolerable no-op
|
|
271
|
+
* (the fail-loud discipline, consistent with `addParticipant`).
|
|
272
|
+
*/
|
|
273
|
+
setActiveParticipant(participantId) {
|
|
274
|
+
if (!this.participants.some((p) => p.participantId === participantId)) {
|
|
275
|
+
throw new Error(`ConversationController: cannot set active to unknown participantId "${participantId}".`);
|
|
276
|
+
}
|
|
277
|
+
this.activeParticipantId = participantId;
|
|
278
|
+
this.emit();
|
|
279
|
+
}
|
|
280
|
+
/** Current immutable snapshot. Both the participant objects (and their
|
|
281
|
+
* `capabilities` array) and the binding's `partyPersonaIds` array are COPIED
|
|
282
|
+
* so mutating the snapshot can't reach into the controller's internal state. */
|
|
283
|
+
snapshot() {
|
|
284
|
+
return {
|
|
285
|
+
participants: this.participants.map((p) => ({
|
|
286
|
+
...p,
|
|
287
|
+
...(p.capabilities ? { capabilities: [...p.capabilities] } : {}),
|
|
288
|
+
})),
|
|
289
|
+
activeParticipantId: this.activeParticipantId,
|
|
290
|
+
binding: {
|
|
291
|
+
...this.binding,
|
|
292
|
+
...(this.binding.partyPersonaIds
|
|
293
|
+
? { partyPersonaIds: [...this.binding.partyPersonaIds] }
|
|
294
|
+
: {}),
|
|
295
|
+
},
|
|
296
|
+
modelOverride: this.modelOverride,
|
|
297
|
+
connected: this.connected,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Tear down. Disconnects the socket ONLY when the controller owns it — an
|
|
302
|
+
* ADOPTED socket is left untouched (the owner disposes it; the Watch
|
|
303
|
+
* shared-socket invariant). Idempotent.
|
|
304
|
+
*/
|
|
305
|
+
dispose() {
|
|
306
|
+
this.disposed = true;
|
|
307
|
+
this.connected = false;
|
|
308
|
+
if (this.ownsSocket) {
|
|
309
|
+
this.socket.disconnect();
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
// ── internals ────────────────────────────────────────────────────────────
|
|
313
|
+
activeParticipant() {
|
|
314
|
+
return this.participants.find((p) => p.participantId === this.activeParticipantId);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Advisory party persona ids derived from the persona participants (the `dm`
|
|
318
|
+
* and `user` seats are excluded — only party members route scenes). Mirrors
|
|
319
|
+
* `advisoryPartyPersonaIds`: returns `undefined` when empty so the hello drops
|
|
320
|
+
* the key and stays byte-identical for ordinary chat / single-DM adventures.
|
|
321
|
+
*/
|
|
322
|
+
derivePartyPersonaIds() {
|
|
323
|
+
const ids = this.participants
|
|
324
|
+
.filter((p) => p.role === ParticipantRole.PERSONA && Boolean(p.personaId))
|
|
325
|
+
.map((p) => p.personaId);
|
|
326
|
+
return ids.length > 0 ? ids : undefined;
|
|
327
|
+
}
|
|
328
|
+
bindingWithDerivedParty(binding) {
|
|
329
|
+
const partyPersonaIds = this.derivePartyPersonaIds();
|
|
330
|
+
return {
|
|
331
|
+
...binding,
|
|
332
|
+
...(partyPersonaIds ? { partyPersonaIds } : { partyPersonaIds: undefined }),
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
rederiveParty() {
|
|
336
|
+
this.binding = this.bindingWithDerivedParty(this.binding);
|
|
337
|
+
}
|
|
338
|
+
emit() {
|
|
339
|
+
this.onState?.(this.snapshot());
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
//# sourceMappingURL=controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.js","sourceRoot":"","sources":["../../src/conversation/controller.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,aAAa,EAAuB,MAAM,cAAc,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAI7C,OAAO,EACL,eAAe,GAEhB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,WAAW,GAGZ,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,qBAAqB,EAAuB,MAAM,kBAAkB,CAAC;AAoD9E,MAAM,OAAO,sBAAsB;IACzB,YAAY,CAA4B;IACxC,mBAAmB,CAAqB;IACxC,OAAO,CAA6B;IACpC,aAAa,CAAqB;IAClC,SAAS,GAAG,KAAK,CAAC;IAC1B;+DAC2D;IACnD,cAAc,CAA4B;IAClD;0DACsD;IAC9C,QAAQ,GAAG,KAAK,CAAC;IAER,QAAQ,CAAyC;IACjD,OAAO,CAA4C;IACnD,MAAM,CAAgB;IACvC,sEAAsE;IACrD,UAAU,CAAU;IAErC,YAAY,IAAmC;QAC7C,IAAI,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B,CACpD,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACK,qBAAqB;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY;aAC1B,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,eAAe,CAAC,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;aACzE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAmB,CAAC,CAAC;QACrC,OAAO,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC;IAC1C,CAAC;IAEO,uBAAuB,CAC7B,OAAmC;QAEnC,MAAM,eAAe,GAAG,IAAI,CAAC,qBAAqB,EAAE,CAAC;QACrD,OAAO;YACL,GAAG,OAAO;YACV,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC;SAC5E,CAAC;IACJ,CAAC;IAEO,aAAa;QACnB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC5D,CAAC;IAEO,IAAI;QACV,IAAI,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;IAClC,CAAC;CACF"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability auto-switch — the single, pure model-selection helper (R2.1).
|
|
3
|
+
*
|
|
4
|
+
* This is the ONE place R2 decides which model a turn runs on when a capability
|
|
5
|
+
* (today: `'vision'` when the webcam toggles ON) is needed that the bound
|
|
6
|
+
* persona's model lacks. R3's vision-source picker CONSUMES this exact helper —
|
|
7
|
+
* there is no parallel `resolveVisionCapability`. It is fully unit-testable: no
|
|
8
|
+
* I/O, no network. Capability FACTS come only from the injected R1 chat adapter
|
|
9
|
+
* registry (which mirrors `shared/model_config.py`); this helper never hardcodes
|
|
10
|
+
* which models are vision-capable.
|
|
11
|
+
*
|
|
12
|
+
* Precedence: an explicit override wins; otherwise the bound model is kept iff
|
|
13
|
+
* it already satisfies the needed caps; otherwise we auto-switch to the first
|
|
14
|
+
* registry model advertising the needed caps.
|
|
15
|
+
*/
|
|
16
|
+
import type { AdapterRegistry } from '../adapters';
|
|
17
|
+
import type { ChatAdapterDescriptor } from '../adapters';
|
|
18
|
+
import type { ChatModelCapability } from '../api';
|
|
19
|
+
/** Why `resolveEffectiveModel` chose the model it returned. */
|
|
20
|
+
export declare const EffectiveModelReason: {
|
|
21
|
+
readonly BOUND: "bound";
|
|
22
|
+
readonly OVERRIDE: "override";
|
|
23
|
+
readonly AUTO_SWITCH: "auto_switch";
|
|
24
|
+
};
|
|
25
|
+
export type EffectiveModelReason = (typeof EffectiveModelReason)[keyof typeof EffectiveModelReason];
|
|
26
|
+
export interface ResolveEffectiveModelArgs {
|
|
27
|
+
/** The context-bound persona's model id (the default choice). */
|
|
28
|
+
boundModelId: string;
|
|
29
|
+
/**
|
|
30
|
+
* Capabilities required for this turn — e.g. `['vision']` when the webcam is
|
|
31
|
+
* ON. Empty means no gate, so the bound model is always kept.
|
|
32
|
+
*/
|
|
33
|
+
needed: readonly ChatModelCapability[];
|
|
34
|
+
/** An explicit user override, when set — wins over bound/auto-switch. */
|
|
35
|
+
override?: string;
|
|
36
|
+
/** The R1 chat adapter registry — the single source of capability facts. */
|
|
37
|
+
registry: Pick<AdapterRegistry<ChatAdapterDescriptor>, 'satisfies' | 'listWithCapability'>;
|
|
38
|
+
}
|
|
39
|
+
export interface EffectiveModel {
|
|
40
|
+
modelId: string;
|
|
41
|
+
reason: EffectiveModelReason;
|
|
42
|
+
/** The bound model we switched away from — set ONLY for `auto_switch`. */
|
|
43
|
+
switchedFrom?: string;
|
|
44
|
+
/** Echo of the needed caps that drove the decision (for UI banners). */
|
|
45
|
+
needed: readonly ChatModelCapability[];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Resolve the model a turn should run on given the bound model, the needed
|
|
49
|
+
* capabilities, and an optional override. PURE.
|
|
50
|
+
*
|
|
51
|
+
* - `override` set ⇒ `'override'` (we trust the user's explicit pick; we do NOT
|
|
52
|
+
* second-guess its caps — that is the user's choice to make).
|
|
53
|
+
* - bound model satisfies `needed` (or `needed` is empty) ⇒ `'bound'`.
|
|
54
|
+
* - otherwise ⇒ `'auto_switch'` to the first registry model that satisfies
|
|
55
|
+
* EVERY needed cap (not just the first unmet one — a vision-only model is no
|
|
56
|
+
* good when `['vision','audio']` is needed), carrying `switchedFrom`. If NO
|
|
57
|
+
* model satisfies the full set, we keep the bound model as a fail-soft
|
|
58
|
+
* `'bound'` — the panel surfaces the gap rather than the helper throwing (a
|
|
59
|
+
* registry that lists zero vision models is a config state, not a crash).
|
|
60
|
+
*/
|
|
61
|
+
export declare function resolveEffectiveModel(args: ResolveEffectiveModelArgs): EffectiveModel;
|
|
62
|
+
//# sourceMappingURL=effectiveModel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effectiveModel.d.ts","sourceRoot":"","sources":["../../src/conversation/effectiveModel.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAElD,+DAA+D;AAC/D,eAAO,MAAM,oBAAoB;;;;CAIvB,CAAC;AAEX,MAAM,MAAM,oBAAoB,GAC9B,CAAC,OAAO,oBAAoB,CAAC,CAAC,MAAM,OAAO,oBAAoB,CAAC,CAAC;AAEnE,MAAM,WAAW,yBAAyB;IACxC,iEAAiE;IACjE,YAAY,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,MAAM,EAAE,SAAS,mBAAmB,EAAE,CAAC;IACvC,yEAAyE;IACzE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,QAAQ,EAAE,IAAI,CACZ,eAAe,CAAC,qBAAqB,CAAC,EACtC,WAAW,GAAG,oBAAoB,CACnC,CAAC;CACH;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,oBAAoB,CAAC;IAC7B,0EAA0E;IAC1E,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,wEAAwE;IACxE,MAAM,EAAE,SAAS,mBAAmB,EAAE,CAAC;CACxC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,yBAAyB,GAAG,cAAc,CAwCrF"}
|