@convai/web-sdk 1.8.0-beta.8 → 1.9.0-beta.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/dist/core/ConvaiClient.d.ts.map +1 -1
- package/dist/core/ConvaiClient.js +25 -6
- package/dist/core/ConvaiClient.js.map +1 -1
- package/dist/core/MessageHandler.d.ts +7 -1
- package/dist/core/MessageHandler.d.ts.map +1 -1
- package/dist/core/MessageHandler.js +65 -51
- package/dist/core/MessageHandler.js.map +1 -1
- package/dist/core/SSESession.d.ts +15 -0
- package/dist/core/SSESession.d.ts.map +1 -1
- package/dist/core/SSESession.js +234 -24
- package/dist/core/SSESession.js.map +1 -1
- package/dist/core/types.d.ts +9 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/embed/chat-embed-v1.js +14 -14
- package/dist/embed/chat-embed-v1.js.map +3 -3
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +2 -2
|
@@ -68,6 +68,12 @@ export declare class MessageHandler extends EventEmitter {
|
|
|
68
68
|
* next bot-started-speaking can activate cleanly.
|
|
69
69
|
*/
|
|
70
70
|
interruptLipsyncPlayback(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Finalize local response state after the client intentionally aborts a
|
|
73
|
+
* transport before the server can deliver its terminal response frame.
|
|
74
|
+
*/
|
|
75
|
+
finalizeInterruptedResponse(finalizeAll?: boolean): void;
|
|
76
|
+
private finalizeStreamingBotMessages;
|
|
71
77
|
/**
|
|
72
78
|
* Set the LiveKit room instance
|
|
73
79
|
*/
|
|
@@ -94,7 +100,7 @@ export declare class MessageHandler extends EventEmitter {
|
|
|
94
100
|
/**
|
|
95
101
|
* Public entry point for WebSocket transport to inject messages into the same pipeline.
|
|
96
102
|
*/
|
|
97
|
-
handleDataReceivedPublic(payload: Uint8Array): void;
|
|
103
|
+
handleDataReceivedPublic(payload: Uint8Array, transport?: "websocket" | "sse"): void;
|
|
98
104
|
/** Append a submitted text message without waiting for a server echo. */
|
|
99
105
|
addUserTextMessage(text: string): void;
|
|
100
106
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MessageHandler.d.ts","sourceRoot":"","sources":["../../src/core/MessageHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAa,MAAM,gBAAgB,CAAC;AACjD,OAAO,EACL,WAAW,EAQZ,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAM9C,OAAO,EACL,eAAe,EAEhB,MAAM,mBAAmB,CAAC;AA8B3B;;;GAGG;AACH,qBAAa,cAAe,SAAQ,YAAY;IAC9C,OAAO,CAAC,IAAI,CAAqB;IACjC,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,qBAAqB,CAAuB;IACpD,OAAO,CAAC,iBAAiB,CAAc;IACvC,OAAO,CAAC,eAAe,CAAiB;IACxC,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,UAAU,CAAkB;IACpC,OAAO,CAAC,WAAW,CAAkB;IACrC,OAAO,CAAC,wBAAwB,CAAa;IAC7C,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,eAAe,CAAoD;IAC3E,OAAO,CAAC,kBAAkB,CAAY;IACtC,OAAO,CAAC,uBAAuB,CAAqB;IACpD,OAAO,CAAC,gCAAgC,CAAK;IAI7C,OAAO,CAAC,qBAAqB,CAA8B;IAK3D,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAG/B;gBAEE,IAAI,CAAC,EAAE,IAAI;IASvB;;OAEG;IACH,kBAAkB,IAAI,eAAe;IAIrC;;OAEG;IACH,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAI7C,8EAA8E;IAC9E,qBAAqB,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,GAAG,IAAI;IAK3C;;OAEG;IACH,eA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U,EACnB,SAAS,GAAE,WAAW,GAAG,KAAmB,GAC3C,IAAI;IAIP,yEAAyE;IACzE,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAYtC;;;;;;;OAOG;IACH,kBAAkB,CAAC,OAAO,EAAE;QAC1B,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;QACvB,OAAO,EAAE,MAAM,CAAC;QAChB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,GAAG,WAAW;IAcf;;OAEG;IACH,KAAK,IAAI,IAAI;CAmBd"}
|
|
@@ -116,6 +116,52 @@ export class MessageHandler extends EventEmitter {
|
|
|
116
116
|
this.emit("speakingChange", false);
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Finalize local response state after the client intentionally aborts a
|
|
121
|
+
* transport before the server can deliver its terminal response frame.
|
|
122
|
+
*/
|
|
123
|
+
finalizeInterruptedResponse(finalizeAll = false) {
|
|
124
|
+
const finalizedMessages = this.finalizeStreamingBotMessages(finalizeAll);
|
|
125
|
+
this.isBotResponding = false;
|
|
126
|
+
this.emit("botRespondingChange", false);
|
|
127
|
+
if (finalizedMessages.length > 0) {
|
|
128
|
+
this.emit("messagesChange", this.chatMessages, undefined, finalizedMessages);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
finalizeStreamingBotMessages(finalizeAll = this.modelOutputVersion === 2) {
|
|
132
|
+
const finalizedMessages = [];
|
|
133
|
+
if (finalizeAll) {
|
|
134
|
+
for (let index = 0; index < this.chatMessages.length; index++) {
|
|
135
|
+
const message = this.chatMessages[index];
|
|
136
|
+
if ((message.type === "bot-llm-text" || message.type === "bot-output") &&
|
|
137
|
+
message.isStreaming) {
|
|
138
|
+
const finalizedMessage = {
|
|
139
|
+
...message,
|
|
140
|
+
isStreaming: false,
|
|
141
|
+
};
|
|
142
|
+
this.chatMessages[index] = finalizedMessage;
|
|
143
|
+
finalizedMessages.push(finalizedMessage);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
let stopIndex = this.activeBotLlmMessageId
|
|
149
|
+
? this.chatMessages.findIndex((message) => message.id === this.activeBotLlmMessageId)
|
|
150
|
+
: -1;
|
|
151
|
+
if (stopIndex === -1)
|
|
152
|
+
stopIndex = this.chatMessages.length - 1;
|
|
153
|
+
const lastBotMsg = this.chatMessages[stopIndex];
|
|
154
|
+
if (lastBotMsg &&
|
|
155
|
+
(lastBotMsg.type === "bot-llm-text" ||
|
|
156
|
+
lastBotMsg.type === "bot-output") &&
|
|
157
|
+
lastBotMsg.isStreaming) {
|
|
158
|
+
const finalizedMessage = { ...lastBotMsg, isStreaming: false };
|
|
159
|
+
this.chatMessages[stopIndex] = finalizedMessage;
|
|
160
|
+
finalizedMessages.push(finalizedMessage);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return finalizedMessages;
|
|
164
|
+
}
|
|
119
165
|
/**
|
|
120
166
|
* Set the LiveKit room instance
|
|
121
167
|
*/
|
|
@@ -146,7 +192,7 @@ export class MessageHandler extends EventEmitter {
|
|
|
146
192
|
/**
|
|
147
193
|
* Handle incoming data message
|
|
148
194
|
*/
|
|
149
|
-
handleDataReceived(payload, _participant,
|
|
195
|
+
handleDataReceived(payload, _participant, transport = "livekit") {
|
|
150
196
|
let messageString = "";
|
|
151
197
|
let parsed = false;
|
|
152
198
|
try {
|
|
@@ -211,17 +257,17 @@ export class MessageHandler extends EventEmitter {
|
|
|
211
257
|
this.isBotResponding = true;
|
|
212
258
|
this.emit("botRespondingChange", true);
|
|
213
259
|
this.seenBotOutputSegments.clear();
|
|
214
|
-
// Lifecycle frames do not carry output_id. In v2, keep at most one
|
|
260
|
+
// Lifecycle frames do not carry output_id. In v2 or SSE, keep at most one
|
|
215
261
|
// unowned placeholder so concurrent starts cannot create anonymous
|
|
216
262
|
// rows that later get adopted in reverse order. Invocation-correlated
|
|
217
263
|
// chunks establish additional rows in authoritative arrival order.
|
|
218
|
-
const
|
|
264
|
+
const hasUnownedCorrelatedStarter = (this.modelOutputVersion === 2 || transport === "sse") &&
|
|
219
265
|
this.chatMessages.some((message) => message.type === "bot-llm-text" &&
|
|
220
266
|
message.isStreaming &&
|
|
221
267
|
message.content === "" &&
|
|
222
268
|
message.outputId === undefined &&
|
|
223
269
|
message.logicalTurnId === undefined);
|
|
224
|
-
if (
|
|
270
|
+
if (hasUnownedCorrelatedStarter)
|
|
225
271
|
break;
|
|
226
272
|
// Add initial streaming message to chat.
|
|
227
273
|
const streamingMessage = {
|
|
@@ -299,13 +345,16 @@ export class MessageHandler extends EventEmitter {
|
|
|
299
345
|
case "bot-llm-text":
|
|
300
346
|
if (messageData.data?.text) {
|
|
301
347
|
const newChunk = messageData.data.text;
|
|
302
|
-
// Core
|
|
303
|
-
//
|
|
304
|
-
|
|
348
|
+
// Core's Interaction API/SSE path can carry logical-turn metadata
|
|
349
|
+
// before hosts opt into the full model-output v2 contract. Preserve
|
|
350
|
+
// it for SSE rows so callers can correlate legacy/default text
|
|
351
|
+
// completions without changing LiveKit's historical v1 shape.
|
|
352
|
+
const acceptsSseCorrelation = transport === "sse";
|
|
353
|
+
const outputId = (this.modelOutputVersion === 2 || acceptsSseCorrelation) &&
|
|
305
354
|
isValidOutputId(messageData.data.output_id)
|
|
306
355
|
? messageData.data.output_id
|
|
307
356
|
: undefined;
|
|
308
|
-
const logicalTurnId = this.modelOutputVersion === 2 &&
|
|
357
|
+
const logicalTurnId = (this.modelOutputVersion === 2 || acceptsSseCorrelation) &&
|
|
309
358
|
isValidLogicalTurnId(messageData.data.logical_turn_id)
|
|
310
359
|
? messageData.data.logical_turn_id
|
|
311
360
|
: undefined;
|
|
@@ -443,37 +492,7 @@ export class MessageHandler extends EventEmitter {
|
|
|
443
492
|
case "bot-llm-stopped": {
|
|
444
493
|
// A v2 response can have interleaved correlated rows. The stop event
|
|
445
494
|
// is response-scoped, so none may remain open for later chunks.
|
|
446
|
-
const finalizedMessages =
|
|
447
|
-
if (this.modelOutputVersion === 2) {
|
|
448
|
-
for (let index = 0; index < this.chatMessages.length; index++) {
|
|
449
|
-
const message = this.chatMessages[index];
|
|
450
|
-
if ((message.type === "bot-llm-text" ||
|
|
451
|
-
message.type === "bot-output") &&
|
|
452
|
-
message.isStreaming) {
|
|
453
|
-
const finalizedMessage = {
|
|
454
|
-
...message,
|
|
455
|
-
isStreaming: false,
|
|
456
|
-
};
|
|
457
|
-
this.chatMessages[index] = finalizedMessage;
|
|
458
|
-
finalizedMessages.push(finalizedMessage);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
else {
|
|
463
|
-
// v1: a stop frame finalizes the bubble this turn owns; only when no
|
|
464
|
-
// bubble was opened does the historical last-row rule apply.
|
|
465
|
-
let stopIndex = this.activeBotLlmMessageId
|
|
466
|
-
? this.chatMessages.findIndex((message) => message.id === this.activeBotLlmMessageId)
|
|
467
|
-
: -1;
|
|
468
|
-
if (stopIndex === -1)
|
|
469
|
-
stopIndex = this.chatMessages.length - 1;
|
|
470
|
-
const lastBotMsg = this.chatMessages[stopIndex];
|
|
471
|
-
if (lastBotMsg && lastBotMsg.isStreaming) {
|
|
472
|
-
const finalizedMessage = { ...lastBotMsg, isStreaming: false };
|
|
473
|
-
this.chatMessages[stopIndex] = finalizedMessage;
|
|
474
|
-
finalizedMessages.push(finalizedMessage);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
495
|
+
const finalizedMessages = this.finalizeStreamingBotMessages();
|
|
477
496
|
// Clear streaming state
|
|
478
497
|
this.isBotResponding = false;
|
|
479
498
|
this.emit("botRespondingChange", false);
|
|
@@ -485,21 +504,16 @@ export class MessageHandler extends EventEmitter {
|
|
|
485
504
|
// Text-only Interactions API terminal marker. This is deliberately a
|
|
486
505
|
// separate event: a completed REST turn must not be represented as a
|
|
487
506
|
// synthetic bot-llm-stopped message.
|
|
488
|
-
case "bot-turn-completed":
|
|
489
|
-
const
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (completedIndex >= 0 && this.chatMessages[completedIndex].isStreaming) {
|
|
493
|
-
this.chatMessages[completedIndex] = {
|
|
494
|
-
...this.chatMessages[completedIndex],
|
|
495
|
-
isStreaming: false,
|
|
496
|
-
};
|
|
497
|
-
this.emit("messagesChange", this.chatMessages);
|
|
507
|
+
case "bot-turn-completed": {
|
|
508
|
+
const finalizedMessages = this.finalizeStreamingBotMessages(transport === "sse");
|
|
509
|
+
if (finalizedMessages.length > 0) {
|
|
510
|
+
this.emit("messagesChange", this.chatMessages, undefined, finalizedMessages);
|
|
498
511
|
}
|
|
499
512
|
this.isBotResponding = false;
|
|
500
513
|
this.emit("botRespondingChange", false);
|
|
501
514
|
this.emit("botTurnCompleted", messageData.data ?? {});
|
|
502
515
|
break;
|
|
516
|
+
}
|
|
503
517
|
// User Transcription Messages
|
|
504
518
|
case "user-transcription":
|
|
505
519
|
if (messageData.data?.text) {
|
|
@@ -972,8 +986,8 @@ export class MessageHandler extends EventEmitter {
|
|
|
972
986
|
/**
|
|
973
987
|
* Public entry point for WebSocket transport to inject messages into the same pipeline.
|
|
974
988
|
*/
|
|
975
|
-
handleDataReceivedPublic(payload) {
|
|
976
|
-
this.handleDataReceived(payload, null,
|
|
989
|
+
handleDataReceivedPublic(payload, transport = "websocket") {
|
|
990
|
+
this.handleDataReceived(payload, null, transport);
|
|
977
991
|
}
|
|
978
992
|
/** Append a submitted text message without waiting for a server echo. */
|
|
979
993
|
addUserTextMessage(text) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MessageHandler.js","sourceRoot":"","sources":["../../src/core/MessageHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAWjD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EACL,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,eAAe,CAAC;AACvB,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AwB,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;QAC5B,IAAI,CAAC,qBAAqB,CAAC,KAAK,EAAE,CAAC;QACnC,IAAI,CAAC,6BAA6B,EAAE,CAAC;QACrC,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QAC/C,IAAI,CAAC,IAAI,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAAC;QACzC,IAAI,CAAC,IAAI,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAC;QACxC,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,KAAK,CAAC,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;CACF","sourcesContent":["import { Room, RoomEvent } from \"livekit-client\";\nimport {\n ChatMessage,\n BotLlmTextRawEvent,\n ConvaiAction,\n InteractionCreated,\n ServerResponse,\n CharacterStatusEvent,\n ConvaiToolCall,\n ModelOutputMessage,\n} from \"./types\";\nimport { EventEmitter } from \"./EventEmitter\";\nimport {\n extractModelOutputPayload,\n parseModelOutputMessage,\n} from \"./modelOutput\";\nimport { isValidLogicalTurnId, isValidOutputId } from \"./logicalTurn\";\nimport {\n BlendshapeQueue,\n type BlendshapeChunkMetadata,\n} from \"./BlendshapeQueue\";\n\ntype LifecycleOwnerPayload = {\n response_id?: string;\n neurosync_turn_id?: number;\n turn_id?: number;\n epoch?: number;\n};\n\nfunction stableJsonFingerprint(value: unknown): string {\n if (Array.isArray(value)) {\n return `[${value.map(stableJsonFingerprint).join(\",\")}]`;\n }\n if (value && typeof value === \"object\") {\n const entries = Object.entries(value as Record<string, unknown>)\n .sort(([left], [right]) => left.localeCompare(right))\n .map(\n ([key, item]) =>\n `${JSON.stringify(key)}:${stableJsonFingerprint(item)}`,\n );\n return `{${entries.join(\",\")}}`;\n }\n return JSON.stringify(value);\n}\n\nconst MAX_MODEL_OUTPUT_REPLAY_FINGERPRINTS = 400;\n// Preserve exact replay matching instead of accepting hash collisions, while\n// bounding the aggregate strings retained by a long-lived v2 session.\nconst MAX_MODEL_OUTPUT_REPLAY_RETAINED_CHARS = 4 * 1024 * 1024;\n\n/**\n * Handles incoming messages from Convai\n * Processes various message types from the AI assistant and updates chat history\n */\nexport class MessageHandler extends EventEmitter {\n private room: Room | null = null;\n private chatMessages: ChatMessage[] = [];\n private activeBotLlmMessageId: string | null = null;\n private userTranscription: string = \"\";\n private logRtviMessages: boolean = true;\n private isBotResponding: boolean = false;\n private isSpeaking: boolean = false;\n private isListening: boolean = false;\n private totalBlendshapesReceived: number = 0;\n private blendshapeQueue: BlendshapeQueue;\n private _currentEmotion: { emotion: string; scale?: number } | null = null;\n private modelOutputVersion: 1 | 2 = 1;\n private seenModelOutputPayloads = new Set<string>();\n private seenModelOutputPayloadCharacters = 0;\n // bot-output segments already added to the chat. The server re-sends the\n // same segment (full text) as TTS progress advances (spoken_status\n // in-progress → completed); only the first sighting may touch the chat.\n private seenBotOutputSegments = new Set<number | string>();\n\n // Fix 4: store a single bound reference so setupListeners/cleanupListeners use the\n // exact same function object. Previously .bind(this) in each call created a new\n // reference every time, making room.off() a no-op and leaking listeners on reconnect.\n private readonly _boundHandleDataReceived: (\n payload: Uint8Array,\n participant: unknown,\n ) => void;\n\n constructor(room?: Room) {\n super();\n this._boundHandleDataReceived = this.handleDataReceived.bind(this);\n this.blendshapeQueue = new BlendshapeQueue();\n if (room) {\n this.setRoom(room);\n }\n }\n\n /**\n * Get the blendshape queue instance\n */\n getBlendshapeQueue(): BlendshapeQueue {\n return this.blendshapeQueue;\n }\n\n /**\n * Enable or disable console logging for decoded incoming RTVI messages.\n */\n setRtviMessageLogging(enabled: boolean): void {\n this.logRtviMessages = enabled;\n }\n\n /** Select the server-negotiated canonical output version for this session. */\n setModelOutputVersion(version: 1 | 2): void {\n this.modelOutputVersion = version;\n this._clearModelOutputReplayLedger();\n }\n\n /**\n * Get current chat messages\n */\n getChatMessages(): ChatMessage[] {\n return this.chatMessages;\n }\n\n /**\n * Get current user transcription\n */\n getUserTranscription(): string {\n return this.userTranscription;\n }\n\n /**\n * Get bot responding state\n */\n getIsBotResponding(): boolean {\n return this.isBotResponding;\n }\n\n /**\n * Get speaking state\n */\n getIsSpeaking(): boolean {\n return this.isSpeaking;\n }\n\n /**\n * Get current bot emotion (updated from bot-emotion messages)\n */\n getEmotion(): { emotion: string; scale?: number } | null {\n return this._currentEmotion;\n }\n\n /**\n * Get whether the user is currently speaking (bot is in \"listening\" state).\n * True from user-started-speaking until user-stopped-speaking.\n */\n getIsListening(): boolean {\n return this.isListening;\n }\n\n /**\n * Local interruption path used by the client before the server cancel/terminal\n * arrives. Keep MessageHandler state and BlendshapeQueue state in sync so the\n * next bot-started-speaking can activate cleanly.\n */\n interruptLipsyncPlayback(): void {\n const wasSpeaking = this.isSpeaking;\n this.isSpeaking = false;\n this.blendshapeQueue.interrupt();\n if (wasSpeaking) {\n this.emit(\"speakingChange\", false);\n }\n }\n\n /**\n * Set the LiveKit room instance\n */\n setRoom(room: Room): void {\n // Clean up previous room listeners\n if (this.room) {\n this.cleanupListeners();\n }\n\n this.room = room;\n this.setupListeners();\n }\n\n /**\n * Setup message listeners\n */\n private setupListeners(): void {\n if (!this.room) return;\n\n this.room.on(RoomEvent.DataReceived, this._boundHandleDataReceived);\n }\n\n /**\n * Clean up message listeners\n */\n private cleanupListeners(): void {\n if (!this.room) return;\n\n this.room.off(RoomEvent.DataReceived, this._boundHandleDataReceived);\n }\n\n /**\n * Handle incoming data message\n */\n private handleDataReceived(\n payload: Uint8Array,\n _participant: unknown,\n _transport: \"livekit\" | \"websocket\" = \"livekit\",\n ): void {\n let messageString = \"\";\n let parsed = false;\n\n try {\n // Decode bytes to string\n const decoder = new TextDecoder();\n messageString = decoder.decode(payload);\n\n // Parse JSON\n const messageData = JSON.parse(messageString);\n parsed = true;\n\n // Canonical output has two transport envelopes. Normalize it before the\n // legacy message switch so both LiveKit and WebSocket execute one path.\n const modelOutputPayload = extractModelOutputPayload(messageData);\n if (modelOutputPayload !== undefined) {\n this._handleModelOutput(modelOutputPayload);\n return;\n }\n\n // Extract and categorize messages for chat display\n const timestamp = new Date().toISOString();\n const messageId = `${messageData.type}-${Date.now()}-${Math.random()}`;\n\n // Multi-character routing identity, carried inline on every\n // character-originated event. Undefined in single-character sessions, so\n // these spread to nothing and every existing message keeps its shape.\n const attribution: Pick<ChatMessage, \"membershipId\" | \"characterId\"> = {\n ...(messageData.data?.membership_id && {\n membershipId: messageData.data.membership_id as string,\n }),\n ...(messageData.data?.character_id && {\n characterId: messageData.data.character_id as string,\n }),\n };\n /**\n * Two instances must never share a streaming bubble. Without this a\n * target switch mid-turn splices one character's text onto another's,\n * because the merge would only be checking `isStreaming`.\n */\n const sameSpeaker = (msg: ChatMessage | undefined): boolean =>\n !!msg && msg.membershipId === attribution.membershipId;\n\n // Handle different message types\n switch (messageData.type) {\n // Submitted text is appended locally by sendUserTextMessage. Ignore\n // server echoes so each user message appears exactly once.\n case \"user_text_message\":\n case \"user-llm-text\":\n case \"text-input\":\n case \"user-input\":\n case \"text-message\":\n case \"chat-message\":\n case \"input-text\":\n case \"message\":\n // Skip server echoes; local text and voice transcription are handled separately.\n break;\n\n // Bot Ready - Bot is ready, connection should be true\n case \"bot-ready\":\n // `about` carries this instance's routing identity in a roster room,\n // and the same fields are also flattened onto `data`. Prefer `about`,\n // fall back to the flattened copy, and pass undefined for a\n // single-character session.\n this.emit(\n \"botReady\",\n messageData.data?.about ??\n (messageData.data?.membership_id ? messageData.data : undefined),\n );\n break;\n\n // Bot LLM Started - Begin streaming response\n case \"bot-llm-started\":\n this.isBotResponding = true;\n this.emit(\"botRespondingChange\", true);\n this.seenBotOutputSegments.clear();\n\n // Lifecycle frames do not carry output_id. In v2, keep at most one\n // unowned placeholder so concurrent starts cannot create anonymous\n // rows that later get adopted in reverse order. Invocation-correlated\n // chunks establish additional rows in authoritative arrival order.\n const hasUnownedV2Starter =\n this.modelOutputVersion === 2 &&\n this.chatMessages.some(\n (message) =>\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n message.content === \"\" &&\n message.outputId === undefined &&\n message.logicalTurnId === undefined,\n );\n if (hasUnownedV2Starter) break;\n\n // Add initial streaming message to chat.\n const streamingMessage: ChatMessage = {\n id: messageId,\n type: \"bot-llm-text\",\n content: \"\",\n timestamp: timestamp,\n isStreaming: true,\n ...attribution,\n };\n this.activeBotLlmMessageId = messageId;\n this.chatMessages.push(streamingMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n break;\n\n // Bot Output - Aggregated output stream (sentence/word-level, replaces bot-transcript)\n // Payload: { text, spoken, aggregated_by, segment_id, spoken_status,\n // spoken_progress }. The same segment_id is re-sent with the FULL\n // segment text on every TTS progress tick (spoken_status\n // \"in-progress\" → \"completed\"), so appending per event repeats the\n // sentence once per tick.\n case \"bot-output\":\n if (messageData.data?.text) {\n const newText = messageData.data.text;\n const segmentId = messageData.data.segment_id as\n | number\n | string\n | undefined;\n this.emit(\"botOutput\", {\n text: newText,\n spoken: messageData.data.spoken ?? false,\n aggregatedBy: messageData.data.aggregated_by ?? \"sentence\",\n segmentId,\n spokenStatus: messageData.data.spoken_status,\n });\n\n // Progress re-send of a segment already in the chat — skip.\n if (segmentId !== undefined) {\n if (this.seenBotOutputSegments.has(segmentId)) break;\n this.seenBotOutputSegments.add(segmentId);\n }\n\n const activeBotLlmMessage = this.activeBotLlmMessageId\n ? this.chatMessages.find(\n (message) => message.id === this.activeBotLlmMessageId,\n )\n : undefined;\n const lastMsg = this.chatMessages[this.chatMessages.length - 1];\n // When the reply is rendered by the LLM text stream\n // (bot-llm-text), the TTS echo adds nothing — never mix\n // bot-output into an LLM bubble (streaming or complete).\n if (activeBotLlmMessage?.type === \"bot-llm-text\") break;\n if (lastMsg?.type === \"bot-llm-text\") break;\n if (\n lastMsg?.type === \"bot-output\" &&\n lastMsg.content.includes(newText)\n ) {\n break;\n }\n if (\n lastMsg?.type === \"bot-output\" &&\n lastMsg.isStreaming &&\n sameSpeaker(lastMsg)\n ) {\n this.chatMessages[this.chatMessages.length - 1] = {\n ...lastMsg,\n content: lastMsg.content + newText,\n };\n } else {\n this.chatMessages.push({\n id: `bot-output-${Date.now()}-${Math.random()}`,\n type: \"bot-output\",\n content: newText,\n timestamp: new Date().toISOString(),\n isStreaming: true,\n ...attribution,\n });\n }\n this.emit(\"messagesChange\", this.chatMessages);\n }\n break;\n\n // Bot LLM Text Messages - Streaming chunks\n case \"bot-llm-text\":\n if (messageData.data?.text) {\n const newChunk = messageData.data.text;\n // Core adds this correlation metadata only for negotiated v2.\n // Legacy frames retain their exact historical shape and behavior.\n const outputId =\n this.modelOutputVersion === 2 &&\n isValidOutputId(messageData.data.output_id)\n ? messageData.data.output_id\n : undefined;\n const logicalTurnId =\n this.modelOutputVersion === 2 &&\n isValidLogicalTurnId(messageData.data.logical_turn_id)\n ? messageData.data.logical_turn_id\n : undefined;\n\n // A logical turn can contain multiple provider invocations (for\n // example, the response before a tool call and the continuation\n // after its result). Prefer the invocation id, then fall back to\n // the older v2 logical-turn id when Core does not send one.\n let targetMessageIndex = -1;\n if (outputId !== undefined) {\n for (\n let index = this.chatMessages.length - 1;\n index >= 0;\n index--\n ) {\n const message = this.chatMessages[index];\n if (\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n message.outputId === outputId &&\n sameSpeaker(message)\n ) {\n targetMessageIndex = index;\n break;\n }\n }\n\n // bot-llm-started has no correlation metadata. Let one empty,\n // unowned row adopt the first authoritative invocation id.\n if (targetMessageIndex === -1) {\n for (\n let index = this.chatMessages.length - 1;\n index >= 0;\n index--\n ) {\n const message = this.chatMessages[index];\n if (\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n message.content === \"\" &&\n message.outputId === undefined &&\n message.logicalTurnId === undefined &&\n sameSpeaker(message)\n ) {\n targetMessageIndex = index;\n break;\n }\n }\n }\n } else if (logicalTurnId !== undefined) {\n for (\n let index = this.chatMessages.length - 1;\n index >= 0;\n index--\n ) {\n const message = this.chatMessages[index];\n if (\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n message.outputId === undefined &&\n message.logicalTurnId === logicalTurnId &&\n sameSpeaker(message)\n ) {\n targetMessageIndex = index;\n break;\n }\n }\n\n // Older v2 Core still needs the starter-row behavior that was\n // introduced with logical-turn correlation.\n if (targetMessageIndex === -1) {\n const lastIndex = this.chatMessages.length - 1;\n const lastMessage = this.chatMessages[lastIndex];\n if (\n lastMessage?.type === \"bot-llm-text\" &&\n lastMessage.isStreaming &&\n lastMessage.content === \"\" &&\n lastMessage.outputId === undefined &&\n lastMessage.logicalTurnId === undefined &&\n sameSpeaker(lastMessage)\n ) {\n targetMessageIndex = lastIndex;\n }\n }\n } else {\n // Uncorrelated (v1) chunk: it belongs to the bubble bot-llm-started\n // opened for this speaker. Only when no such bubble exists does the\n // historical \"last streaming row\" rule apply -- and still only for\n // the same speaker, so a target switch mid-turn cannot splice one\n // member's text onto another's row.\n // Ownership applies among uncorrelated rows only: an uncorrelated\n // chunk must never join a row that carries an output or turn id,\n // or a v2 correlated stream would absorb legacy text.\n if (this.activeBotLlmMessageId) {\n targetMessageIndex = this.chatMessages.findIndex(\n (message) =>\n message.id === this.activeBotLlmMessageId &&\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n !message.outputId &&\n !message.logicalTurnId &&\n sameSpeaker(message),\n );\n }\n if (targetMessageIndex === -1) {\n const lastIndex = this.chatMessages.length - 1;\n const lastMessage = this.chatMessages[lastIndex];\n if (\n lastMessage &&\n lastMessage.type === \"bot-llm-text\" &&\n lastMessage.isStreaming &&\n !lastMessage.outputId &&\n !lastMessage.logicalTurnId &&\n sameSpeaker(lastMessage)\n ) {\n targetMessageIndex = lastIndex;\n }\n }\n }\n\n let updatedMessage: ChatMessage;\n if (targetMessageIndex !== -1) {\n const targetMessage = this.chatMessages[targetMessageIndex];\n updatedMessage = {\n ...targetMessage,\n content: targetMessage.content + newChunk,\n ...(outputId !== undefined &&\n targetMessage.outputId === undefined\n ? { outputId }\n : {}),\n ...(logicalTurnId !== undefined &&\n targetMessage.logicalTurnId === undefined\n ? { logicalTurnId }\n : {}),\n };\n this.chatMessages[targetMessageIndex] = updatedMessage;\n } else {\n // If no streaming message exists yet, create one\n updatedMessage = {\n id: `${messageData.type}-${Date.now()}-${Math.random()}`,\n type: \"bot-llm-text\",\n content: newChunk,\n timestamp: new Date().toISOString(),\n isStreaming: true,\n ...(outputId ? { outputId } : {}),\n ...(logicalTurnId ? { logicalTurnId } : {}),\n ...attribution,\n };\n this.activeBotLlmMessageId = updatedMessage.id;\n this.chatMessages.push(updatedMessage);\n }\n this.emit(\"messagesChange\", this.chatMessages, updatedMessage);\n }\n break;\n\n // Parallel diagnostics-only provider output. This must never affect\n // normal chat, speech, TTS, or response lifecycle state.\n case \"bot-llm-text-raw\":\n this._handleBotLlmTextRaw(messageData.data);\n break;\n\n // Bot LLM Stopped - Complete streaming response\n case \"bot-llm-stopped\": {\n // A v2 response can have interleaved correlated rows. The stop event\n // is response-scoped, so none may remain open for later chunks.\n const finalizedMessages: ChatMessage[] = [];\n if (this.modelOutputVersion === 2) {\n for (let index = 0; index < this.chatMessages.length; index++) {\n const message = this.chatMessages[index];\n if (\n (message.type === \"bot-llm-text\" ||\n message.type === \"bot-output\") &&\n message.isStreaming\n ) {\n const finalizedMessage = {\n ...message,\n isStreaming: false,\n };\n this.chatMessages[index] = finalizedMessage;\n finalizedMessages.push(finalizedMessage);\n }\n }\n } else {\n // v1: a stop frame finalizes the bubble this turn owns; only when no\n // bubble was opened does the historical last-row rule apply.\n let stopIndex = this.activeBotLlmMessageId\n ? this.chatMessages.findIndex(\n (message) => message.id === this.activeBotLlmMessageId,\n )\n : -1;\n if (stopIndex === -1) stopIndex = this.chatMessages.length - 1;\n const lastBotMsg = this.chatMessages[stopIndex];\n if (lastBotMsg && lastBotMsg.isStreaming) {\n const finalizedMessage = { ...lastBotMsg, isStreaming: false };\n this.chatMessages[stopIndex] = finalizedMessage;\n finalizedMessages.push(finalizedMessage);\n }\n }\n\n // Clear streaming state\n this.isBotResponding = false;\n this.emit(\"botRespondingChange\", false);\n // Keep the full-list event at one emission per stop while carrying\n // every v2 terminal delta for singular-message consumers.\n this.emit(\n \"messagesChange\",\n this.chatMessages,\n undefined,\n finalizedMessages,\n );\n break;\n }\n\n // Text-only Interactions API terminal marker. This is deliberately a\n // separate event: a completed REST turn must not be represented as a\n // synthetic bot-llm-stopped message.\n case \"bot-turn-completed\":\n const completedIndex = this.activeBotLlmMessageId\n ? this.chatMessages.findIndex(\n (message) => message.id === this.activeBotLlmMessageId,\n )\n : -1;\n if (completedIndex >= 0 && this.chatMessages[completedIndex].isStreaming) {\n this.chatMessages[completedIndex] = {\n ...this.chatMessages[completedIndex],\n isStreaming: false,\n };\n this.emit(\"messagesChange\", this.chatMessages);\n }\n this.isBotResponding = false;\n this.emit(\"botRespondingChange\", false);\n this.emit(\"botTurnCompleted\", messageData.data ?? {});\n break;\n\n // User Transcription Messages\n case \"user-transcription\":\n if (messageData.data?.text) {\n const messageContent = messageData.data.text;\n\n if (messageData.data?.final) {\n const logicalTurnId = this.transcriptionLogicalTurnId(\n messageData.data.logical_turn_id,\n );\n // Final transcription - accumulate in single bubble until bot responds\n const lastUserMsg =\n this.chatMessages[this.chatMessages.length - 1];\n // A final transcription identical to the line appended locally on send is\n // the server echoing that send back: drop it, or the user sees it twice.\n if (\n lastUserMsg?.type === \"user-llm-text\" &&\n lastUserMsg.content.trim() === String(messageContent).trim()\n ) {\n this.userTranscription = \"\";\n this.emit(\"userTranscriptionChange\", \"\");\n break;\n }\n\n // Check if last message is an accumulating user message (isStreaming=true)\n if (\n lastUserMsg &&\n lastUserMsg.type === \"user-transcription\" &&\n lastUserMsg.isStreaming === true\n ) {\n // Accumulate: append new content to existing message with space\n const accumulatedContent =\n lastUserMsg.content.trim() + \" \" + messageContent.trim();\n\n this.chatMessages[this.chatMessages.length - 1] = {\n ...lastUserMsg,\n ...(logicalTurnId === undefined ? {} : { logicalTurnId }),\n content: accumulatedContent,\n timestamp: timestamp,\n };\n } else {\n // Start new accumulating message (isStreaming=true means \"still accumulating\")\n const userMessage: ChatMessage = {\n id: messageId,\n type: \"user-transcription\",\n content: messageContent,\n timestamp: timestamp,\n isStreaming: true,\n ...(logicalTurnId === undefined ? {} : { logicalTurnId }),\n };\n this.chatMessages.push(userMessage);\n }\n this.emit(\"messagesChange\", this.chatMessages);\n\n // Clear live transcription when final\n this.userTranscription = \"\";\n this.emit(\"userTranscriptionChange\", \"\");\n } else {\n // Non-final transcription - show in input field\n this.userTranscription = messageContent;\n this.emit(\"userTranscriptionChange\", messageContent);\n }\n }\n break;\n\n // Bot Emotion Messages — LiveKit path: arrives as an RTVI message frame (messageData.data)\n case \"bot-emotion\":\n if (messageData.data?.emotion) {\n this._currentEmotion = {\n emotion: messageData.data.emotion,\n scale: messageData.data.scale,\n };\n if (this.logRtviMessages) console.log(\"[SDK bot-emotion]\", messageData.data);\n this.emit(\"emotionChange\", this._currentEmotion);\n const emotionMessage: ChatMessage = {\n id: messageId,\n type: \"bot-emotion\",\n content: `${messageData.data.emotion} (scale: ${messageData.data.scale || 1})`,\n timestamp,\n };\n this.chatMessages.push(emotionMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n }\n break;\n // Bot TTS Text - Words as spoken by TTS (word-by-word for socket-based TTS)\n case \"bot-tts-text\":\n if (messageData.data?.text) {\n this.emit(\"botTtsText\", { text: messageData.data.text });\n }\n break;\n\n // Bot TTS lifecycle (more granular than bot-started/stopped-speaking)\n case \"bot-tts-started\":\n this.emit(\"botTtsStarted\");\n break;\n\n case \"bot-tts-stopped\":\n this.emit(\"botTtsStopped\");\n break;\n\n // Server-side muting indicators\n case \"user-mute-started\":\n this.emit(\"userMuteStarted\");\n break;\n\n case \"user-mute-stopped\":\n this.emit(\"userMuteStopped\");\n break;\n\n case \"user-started-speaking\":\n this.isListening = true;\n this.emit(\"listeningChange\", true);\n break;\n\n case \"user-stopped-speaking\":\n this.isListening = false;\n this.emit(\"listeningChange\", false);\n break;\n\n // Bot Started Speaking (priority over listening: state becomes speaking)\n case \"bot-started-speaking\": {\n const owner = this.lifecycleOwner(messageData.data);\n if (this.blendshapeQueue.isStaleOwner(owner)) {\n break;\n }\n const allowed = this.blendshapeQueue.startBotSpeaking();\n if (allowed) {\n this.isSpeaking = true;\n this.totalBlendshapesReceived = 0;\n this.emit(\"speakingChange\", true);\n }\n break;\n }\n\n // Bot Stopped Speaking\n case \"bot-stopped-speaking\":\n if (this.blendshapeQueue.isStaleOwner(this.lifecycleOwner(messageData.data))) {\n break;\n }\n if (this.isSpeaking) {\n this.isSpeaking = false;\n this.blendshapeQueue.stopBotSpeaking();\n this.emit(\"speakingChange\", false);\n }\n break;\n\n // Action Response Messages (LiveKit/WebRTC path)\n case \"action-response\":\n this._handleActionResponse(messageData.data?.actions ?? [], messageId, timestamp);\n break;\n\n case \"metrics\":\n if (messageData.data) {\n this.emit(\"metrics\", messageData.data);\n }\n break;\n\n // Per-turn usage/cost breakdown. Informational only: the server streams\n // it once the client opts in with usage-toggle, and it never affects\n // server-side usage tracking or billing.\n case \"usage-update\":\n if (messageData.data) {\n this.emit(\"usageUpdate\", messageData.data);\n }\n break;\n // Behavior Tree Response Messages\n case \"behavior-tree-response\":\n if (messageData.data?.narrative_section_id) {\n const behaviorMessage: ChatMessage = {\n id: messageId,\n type: \"behavior-tree\",\n content: `Narrative Section: ${messageData.data.narrative_section_id}`,\n timestamp,\n };\n this.chatMessages.push(behaviorMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n }\n break;\n\n // Server Message - Contains various data types like blendshapes\n case \"server-message\":\n if (messageData.data) {\n const serverData = messageData.data;\n\n switch (serverData.type) {\n case \"bot-llm-text-raw\":\n this._handleBotLlmTextRaw(serverData.data ?? serverData);\n break;\n\n case \"final-user-transcription\":\n if (serverData.text) {\n const finalText = serverData.text;\n const logicalTurnId = this.transcriptionLogicalTurnId(\n serverData.logical_turn_id,\n );\n const lastUserMsg =\n this.chatMessages[this.chatMessages.length - 1];\n // A final transcription identical to the line appended locally on send is\n // the server echoing that send back: drop it, or the user sees it twice.\n if (\n lastUserMsg?.type === \"user-llm-text\" &&\n lastUserMsg.content.trim() === String(finalText).trim()\n ) {\n this.userTranscription = \"\";\n this.emit(\"userTranscriptionChange\", \"\");\n break;\n }\n if (\n lastUserMsg &&\n lastUserMsg.type === \"user-transcription\" &&\n lastUserMsg.isStreaming === true\n ) {\n this.chatMessages[this.chatMessages.length - 1] = {\n ...lastUserMsg,\n ...(logicalTurnId === undefined\n ? {}\n : { logicalTurnId }),\n content: finalText,\n timestamp,\n isStreaming: false,\n };\n } else {\n this.chatMessages.push({\n id: `${serverData.type}-${Date.now()}-${Math.random()}`,\n type: \"user-transcription\",\n content: finalText,\n timestamp,\n isStreaming: false,\n ...(logicalTurnId === undefined\n ? {}\n : { logicalTurnId }),\n });\n }\n this.emit(\"messagesChange\", this.chatMessages);\n this.userTranscription = \"\";\n this.emit(\"userTranscriptionChange\", \"\");\n }\n break;\n\n // WebSocket path: arrives inside a bot-text data message as serverData (not messageData.data)\n case \"bot-emotion\":\n if (serverData.emotion) {\n this._currentEmotion = {\n emotion: serverData.emotion,\n scale: serverData.scale,\n };\n if (this.logRtviMessages) console.log(\"[SDK bot-emotion (ws)]\", serverData);\n this.emit(\"emotionChange\", this._currentEmotion);\n }\n break;\n\n case \"blendshape-turn-stats\":\n if (this.blendshapeQueue.isStaleOwner(this.lifecycleOwner(serverData))) {\n break;\n }\n this.blendshapeQueue.endConversation(\n serverData.stats,\n this.lifecycleOwner(serverData),\n );\n this.emit(\"blendshapeStatsReceived\", serverData);\n break;\n\n case \"chunked-neurosync-blendshapes\":\n if (serverData.blendshapes) {\n const owner = this.lifecycleOwner(serverData);\n if (this.blendshapeQueue.isRetiredOwner(owner)) {\n break;\n }\n // If the queue is idle (no active conversation, bot not yet speaking),\n // start a conversation so early chunks from an initial bot greeting\n // are accepted rather than silently dropped.\n if (\n !this.blendshapeQueue.isConversationActive() &&\n !this.isSpeaking\n ) {\n this.blendshapeQueue.startConversation();\n }\n this.blendshapeQueue.addChunk(\n serverData.blendshapes,\n owner,\n );\n if (\n this.blendshapeQueue.consumeOwnerReplacementStoppedSpeaking() &&\n this.isSpeaking\n ) {\n this.isSpeaking = false;\n this.emit(\"speakingChange\", false);\n }\n this.totalBlendshapesReceived +=\n serverData.blendshapes.length;\n this.emit(\"blendshapes\", serverData);\n }\n break;\n\n case \"neurosync-blendshapes-cancel\": {\n const cancelledActive = this.blendshapeQueue.cancelOwner(\n this.lifecycleOwner(serverData),\n );\n if (cancelledActive && this.isSpeaking) {\n this.isSpeaking = false;\n this.emit(\"speakingChange\", false);\n }\n this.emit(\"blendshapeCancel\", serverData);\n break;\n }\n\n case \"user-idle-warning\": {\n const remainingSeconds: number | null =\n serverData.remaining_seconds ??\n serverData.remainingSeconds ??\n null;\n this.emit(\"idleWarning\", { remainingSeconds });\n this.chatMessages.push({\n id: `user-idle-warning-${Date.now()}-${Math.random()}`,\n type: \"idle-warning\",\n content:\n remainingSeconds !== null ? String(remainingSeconds) : \"\",\n timestamp,\n });\n this.emit(\"messagesChange\", this.chatMessages);\n break;\n }\n\n case \"llm-no-response\":\n this.emit(\"llmNoResponse\");\n this.chatMessages.push({\n id: `llm-no-response-${Date.now()}-${Math.random()}`,\n type: \"llm-no-response\",\n content: \"\",\n timestamp,\n });\n this.emit(\"messagesChange\", this.chatMessages);\n break;\n\n // Action Response (WebSocket/RTVI path — wrapped in server-message)\n case \"action-response\":\n this._handleActionResponse(serverData.actions ?? [], messageId, timestamp);\n break;\n\n // Server acknowledgment — arrives wrapped in server-message envelope\n case \"server-response\": {\n // client-ready is an internal RTVI handshake; server echoes an\n // \"Unknown message type\" error for it which is expected and harmless.\n if (serverData.event_type === \"client-ready\") break;\n const serverResponse: ServerResponse = {\n event_type: serverData.event_type ?? \"unknown\",\n status: serverData.status ?? \"error\",\n message: serverData.message ?? null,\n extras: serverData.extras ?? null,\n };\n this.emit(\"serverResponse\", serverResponse);\n break;\n }\n\n /**\n * Lifecycle of one roster member. Emitted when a member is added\n * at runtime, becomes ready, or fails after /connect already\n * returned 200. `bot-ready` stays the readiness authority --\n * a `character-status` of \"ready\" must not enable interaction on\n * its own -- but this is the only way a background dispatch\n * failure is ever reported.\n */\n case \"character-status\": {\n const status: CharacterStatusEvent = {\n status: (serverData.status as CharacterStatusEvent[\"status\"]) ?? \"starting\",\n membershipId: (serverData.membership_id as string) ?? \"\",\n characterId: (serverData.character_id as string) ?? \"\",\n characterSessionId:\n (serverData.character_session_id as string) ?? null,\n participantIdentity:\n (serverData.participant_identity as string) ??\n (serverData.membership_id\n ? `character:${serverData.membership_id}`\n : \"\"),\n roomSessionId: (serverData.room_session_id as string) ?? null,\n rosterEpoch:\n typeof serverData.roster_epoch === \"number\"\n ? serverData.roster_epoch\n : null,\n failureCode: (serverData.failure_code as string) ?? null,\n };\n // Unattributable status is dropped rather than guessed at.\n if (status.membershipId) this.emit(\"characterStatus\", status);\n break;\n }\n\n // interaction-created: session IDs for analytics/tracking\n case \"interaction-created\": {\n const interactionCreated: InteractionCreated = {\n interactionId: serverData.interaction_id,\n characterSessionId: serverData.character_session_id,\n };\n this.emit(\"interactionCreated\", interactionCreated);\n break;\n }\n }\n }\n break;\n\n // Fallback: server-response at top level (not currently observed but kept for safety)\n case \"server-response\": {\n if (messageData.event_type === \"client-ready\") break;\n const serverResponse: ServerResponse = {\n event_type: messageData.event_type ?? \"unknown\",\n status: messageData.status ?? \"error\",\n message: messageData.message ?? null,\n extras: messageData.extras ?? null,\n };\n this.emit(\"serverResponse\", serverResponse);\n break;\n }\n\n // Silently ignore other message types\n case \"moderation-response\":\n case \"trigger-message\":\n case \"update-template-keys\":\n case \"update-dynamic-info\":\n case \"interrupt-bot\":\n break;\n }\n } catch (error) {\n // Silently handle parse errors\n }\n }\n\n private _handleActionResponse(\n actions: Array<ConvaiAction | ConvaiToolCall>,\n messageId: string,\n timestamp: string,\n ): void {\n // model-output v2 is canonical. action-response remains a compatibility\n // projection and executing both would duplicate the same operation.\n if (this.modelOutputVersion === 2) return;\n this.emit(\"actionResponse\", { actions });\n const label = actions.length === 0\n ? \"No action\"\n : actions.map((a) => (a.target ? `${a.name} → ${a.target}` : a.name)).join(\", \");\n const actionMessage: ChatMessage = {\n id: messageId,\n type: \"action\",\n content: label,\n timestamp,\n };\n this.chatMessages.push(actionMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n }\n\n private _handleModelOutput(value: unknown): void {\n if (this.modelOutputVersion !== 2) return;\n const output = parseModelOutputMessage(value);\n if (!output) {\n this.emit(\"modelOutputProtocolError\", {\n code: \"invalid_model_output\",\n message: \"Core sent a malformed or unsupported model-output envelope\",\n });\n return;\n }\n // output_id identifies a provider invocation, not a serialized envelope.\n // One invocation can produce distinct text and tool-call projections with\n // the same id, and consumers must receive both in transport arrival order.\n // Suppress only an exact transport replay of an already delivered payload.\n const fingerprint = stableJsonFingerprint(output);\n if (this.seenModelOutputPayloads.has(fingerprint)) return;\n if (fingerprint.length > MAX_MODEL_OUTPUT_REPLAY_RETAINED_CHARS) {\n this.emit(\"modelOutputProtocolError\", {\n code: \"invalid_model_output\",\n message: \"Core sent an oversized model-output envelope\",\n });\n return;\n }\n this.seenModelOutputPayloads.add(fingerprint);\n this.seenModelOutputPayloadCharacters += fingerprint.length;\n while (\n this.seenModelOutputPayloads.size > MAX_MODEL_OUTPUT_REPLAY_FINGERPRINTS ||\n this.seenModelOutputPayloadCharacters >\n MAX_MODEL_OUTPUT_REPLAY_RETAINED_CHARS\n ) {\n const oldestFingerprint = this.seenModelOutputPayloads.values().next()\n .value as string | undefined;\n if (!oldestFingerprint) break;\n this.seenModelOutputPayloads.delete(oldestFingerprint);\n this.seenModelOutputPayloadCharacters -= oldestFingerprint.length;\n }\n this.emit(\"modelOutput\", output satisfies ModelOutputMessage);\n }\n\n private _clearModelOutputReplayLedger(): void {\n this.seenModelOutputPayloads.clear();\n this.seenModelOutputPayloadCharacters = 0;\n }\n\n private _handleBotLlmTextRaw(value: unknown): void {\n if (this.modelOutputVersion !== 2 || !value || typeof value !== \"object\") {\n return;\n }\n const data = value as {\n text?: unknown;\n logical_turn_id?: unknown;\n output_id?: unknown;\n };\n if (\n typeof data.text !== \"string\" ||\n !isValidLogicalTurnId(data.logical_turn_id)\n ) {\n return;\n }\n this.emit(\"botLlmTextRaw\", {\n text: data.text,\n logical_turn_id: data.logical_turn_id,\n ...(isValidOutputId(data.output_id)\n ? { output_id: data.output_id }\n : {}),\n } satisfies BotLlmTextRawEvent);\n }\n\n private lifecycleOwner(\n data: LifecycleOwnerPayload | undefined,\n ): BlendshapeChunkMetadata {\n if (!data) {\n return {};\n }\n\n return {\n responseId: data.response_id,\n neurosyncTurnId: data.neurosync_turn_id ?? data.turn_id,\n epoch: data.epoch,\n fps: this.numberOrUndefined((data as { fps?: number }).fps),\n startFrameIndex: this.numberOrUndefined(\n (data as { start_frame_index?: number }).start_frame_index,\n ),\n };\n }\n\n private numberOrUndefined(value: unknown): number | undefined {\n return typeof value === \"number\" && Number.isFinite(value)\n ? value\n : undefined;\n }\n\n private transcriptionLogicalTurnId(value: unknown): string | undefined {\n return this.modelOutputVersion === 2 && isValidLogicalTurnId(value)\n ? value\n : undefined;\n }\n\n /**\n * Public entry point for WebSocket transport to inject messages into the same pipeline.\n */\n handleDataReceivedPublic(payload: Uint8Array): void {\n this.handleDataReceived(payload, null, \"websocket\");\n }\n\n /** Append a submitted text message without waiting for a server echo. */\n addUserTextMessage(text: string): void {\n const content = text.trim();\n if (!content) return;\n this.chatMessages.push({\n id: `user-llm-text-${Date.now()}-${Math.random()}`,\n type: \"user-llm-text\",\n content,\n timestamp: new Date().toISOString(),\n });\n this.emit(\"messagesChange\", this.chatMessages);\n }\n\n /**\n * Insert a message into the transcript locally. Nothing is transmitted and\n * no LLM turn is billed -- this is for client-side lines (a greeting, a\n * system notice, a replayed history entry) that must appear in the chat.\n *\n * Subscribers see it through the same `messagesChange` as a live message,\n * so React and the widgets pick it up with no special casing.\n */\n appendLocalMessage(message: {\n sender: \"user\" | \"bot\";\n content: string;\n membershipId?: string;\n characterId?: string;\n }): ChatMessage {\n const chatMessage: ChatMessage = {\n id: `local-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`,\n type: message.sender === \"user\" ? \"user-llm-text\" : \"bot-llm-text\",\n content: message.content,\n timestamp: new Date().toISOString(),\n ...(message.membershipId && { membershipId: message.membershipId }),\n ...(message.characterId && { characterId: message.characterId }),\n };\n this.chatMessages.push(chatMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n return chatMessage;\n }\n\n /**\n * Reset message state\n */\n reset(): void {\n this.chatMessages = [];\n this.activeBotLlmMessageId = null;\n this.userTranscription = \"\";\n this.isBotResponding = false;\n this.isSpeaking = false;\n this.isListening = false;\n this.totalBlendshapesReceived = 0;\n this._currentEmotion = null;\n this.seenBotOutputSegments.clear();\n this._clearModelOutputReplayLedger();\n this.blendshapeQueue.reset();\n this.emit(\"messagesChange\", this.chatMessages);\n this.emit(\"userTranscriptionChange\", \"\");\n this.emit(\"botRespondingChange\", false);\n this.emit(\"speakingChange\", false);\n this.emit(\"listeningChange\", false);\n this.emit(\"emotionChange\", null);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"MessageHandler.js","sourceRoot":"","sources":["../../src/core/MessageHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAWjD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EACL,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,EACL,eAAe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,CAAC,WAAW,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QAC/C,OAAO,WAAW,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,YAAY,GAAG,EAAE,CAAC;QACvB,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC;QAClC,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;QAC5B,IAAI,CAAC,eAAe,GAAG,KAAK,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QACxB,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QACzB,IAAI,CAAC,wBAAwB,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;QAC5B,IAAI,CAAC,qBAAqB,CAAC,KAAK,EAAE,CAAC;QACnC,IAAI,CAAC,6BAA6B,EAAE,CAAC;QACrC,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QAC/C,IAAI,CAAC,IAAI,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAAC;QACzC,IAAI,CAAC,IAAI,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAC;QACxC,IAAI,CAAC,IAAI,CAAC,gBAAgB,EAAE,KAAK,CAAC,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,KAAK,CAAC,CAAC;QACpC,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;CACF","sourcesContent":["import { Room, RoomEvent } from \"livekit-client\";\nimport {\n ChatMessage,\n BotLlmTextRawEvent,\n ConvaiAction,\n InteractionCreated,\n ServerResponse,\n CharacterStatusEvent,\n ConvaiToolCall,\n ModelOutputMessage,\n} from \"./types\";\nimport { EventEmitter } from \"./EventEmitter\";\nimport {\n extractModelOutputPayload,\n parseModelOutputMessage,\n} from \"./modelOutput\";\nimport { isValidLogicalTurnId, isValidOutputId } from \"./logicalTurn\";\nimport {\n BlendshapeQueue,\n type BlendshapeChunkMetadata,\n} from \"./BlendshapeQueue\";\n\ntype LifecycleOwnerPayload = {\n response_id?: string;\n neurosync_turn_id?: number;\n turn_id?: number;\n epoch?: number;\n};\n\nfunction stableJsonFingerprint(value: unknown): string {\n if (Array.isArray(value)) {\n return `[${value.map(stableJsonFingerprint).join(\",\")}]`;\n }\n if (value && typeof value === \"object\") {\n const entries = Object.entries(value as Record<string, unknown>)\n .sort(([left], [right]) => left.localeCompare(right))\n .map(\n ([key, item]) =>\n `${JSON.stringify(key)}:${stableJsonFingerprint(item)}`,\n );\n return `{${entries.join(\",\")}}`;\n }\n return JSON.stringify(value);\n}\n\nconst MAX_MODEL_OUTPUT_REPLAY_FINGERPRINTS = 400;\n// Preserve exact replay matching instead of accepting hash collisions, while\n// bounding the aggregate strings retained by a long-lived v2 session.\nconst MAX_MODEL_OUTPUT_REPLAY_RETAINED_CHARS = 4 * 1024 * 1024;\n\n/**\n * Handles incoming messages from Convai\n * Processes various message types from the AI assistant and updates chat history\n */\nexport class MessageHandler extends EventEmitter {\n private room: Room | null = null;\n private chatMessages: ChatMessage[] = [];\n private activeBotLlmMessageId: string | null = null;\n private userTranscription: string = \"\";\n private logRtviMessages: boolean = true;\n private isBotResponding: boolean = false;\n private isSpeaking: boolean = false;\n private isListening: boolean = false;\n private totalBlendshapesReceived: number = 0;\n private blendshapeQueue: BlendshapeQueue;\n private _currentEmotion: { emotion: string; scale?: number } | null = null;\n private modelOutputVersion: 1 | 2 = 1;\n private seenModelOutputPayloads = new Set<string>();\n private seenModelOutputPayloadCharacters = 0;\n // bot-output segments already added to the chat. The server re-sends the\n // same segment (full text) as TTS progress advances (spoken_status\n // in-progress → completed); only the first sighting may touch the chat.\n private seenBotOutputSegments = new Set<number | string>();\n\n // Fix 4: store a single bound reference so setupListeners/cleanupListeners use the\n // exact same function object. Previously .bind(this) in each call created a new\n // reference every time, making room.off() a no-op and leaking listeners on reconnect.\n private readonly _boundHandleDataReceived: (\n payload: Uint8Array,\n participant: unknown,\n ) => void;\n\n constructor(room?: Room) {\n super();\n this._boundHandleDataReceived = this.handleDataReceived.bind(this);\n this.blendshapeQueue = new BlendshapeQueue();\n if (room) {\n this.setRoom(room);\n }\n }\n\n /**\n * Get the blendshape queue instance\n */\n getBlendshapeQueue(): BlendshapeQueue {\n return this.blendshapeQueue;\n }\n\n /**\n * Enable or disable console logging for decoded incoming RTVI messages.\n */\n setRtviMessageLogging(enabled: boolean): void {\n this.logRtviMessages = enabled;\n }\n\n /** Select the server-negotiated canonical output version for this session. */\n setModelOutputVersion(version: 1 | 2): void {\n this.modelOutputVersion = version;\n this._clearModelOutputReplayLedger();\n }\n\n /**\n * Get current chat messages\n */\n getChatMessages(): ChatMessage[] {\n return this.chatMessages;\n }\n\n /**\n * Get current user transcription\n */\n getUserTranscription(): string {\n return this.userTranscription;\n }\n\n /**\n * Get bot responding state\n */\n getIsBotResponding(): boolean {\n return this.isBotResponding;\n }\n\n /**\n * Get speaking state\n */\n getIsSpeaking(): boolean {\n return this.isSpeaking;\n }\n\n /**\n * Get current bot emotion (updated from bot-emotion messages)\n */\n getEmotion(): { emotion: string; scale?: number } | null {\n return this._currentEmotion;\n }\n\n /**\n * Get whether the user is currently speaking (bot is in \"listening\" state).\n * True from user-started-speaking until user-stopped-speaking.\n */\n getIsListening(): boolean {\n return this.isListening;\n }\n\n /**\n * Local interruption path used by the client before the server cancel/terminal\n * arrives. Keep MessageHandler state and BlendshapeQueue state in sync so the\n * next bot-started-speaking can activate cleanly.\n */\n interruptLipsyncPlayback(): void {\n const wasSpeaking = this.isSpeaking;\n this.isSpeaking = false;\n this.blendshapeQueue.interrupt();\n if (wasSpeaking) {\n this.emit(\"speakingChange\", false);\n }\n }\n\n /**\n * Finalize local response state after the client intentionally aborts a\n * transport before the server can deliver its terminal response frame.\n */\n finalizeInterruptedResponse(finalizeAll = false): void {\n const finalizedMessages = this.finalizeStreamingBotMessages(finalizeAll);\n this.isBotResponding = false;\n this.emit(\"botRespondingChange\", false);\n if (finalizedMessages.length > 0) {\n this.emit(\n \"messagesChange\",\n this.chatMessages,\n undefined,\n finalizedMessages,\n );\n }\n }\n\n private finalizeStreamingBotMessages(\n finalizeAll = this.modelOutputVersion === 2,\n ): ChatMessage[] {\n const finalizedMessages: ChatMessage[] = [];\n if (finalizeAll) {\n for (let index = 0; index < this.chatMessages.length; index++) {\n const message = this.chatMessages[index];\n if (\n (message.type === \"bot-llm-text\" || message.type === \"bot-output\") &&\n message.isStreaming\n ) {\n const finalizedMessage = {\n ...message,\n isStreaming: false,\n };\n this.chatMessages[index] = finalizedMessage;\n finalizedMessages.push(finalizedMessage);\n }\n }\n } else {\n let stopIndex = this.activeBotLlmMessageId\n ? this.chatMessages.findIndex(\n (message) => message.id === this.activeBotLlmMessageId,\n )\n : -1;\n if (stopIndex === -1) stopIndex = this.chatMessages.length - 1;\n const lastBotMsg = this.chatMessages[stopIndex];\n if (\n lastBotMsg &&\n (lastBotMsg.type === \"bot-llm-text\" ||\n lastBotMsg.type === \"bot-output\") &&\n lastBotMsg.isStreaming\n ) {\n const finalizedMessage = { ...lastBotMsg, isStreaming: false };\n this.chatMessages[stopIndex] = finalizedMessage;\n finalizedMessages.push(finalizedMessage);\n }\n }\n return finalizedMessages;\n }\n\n /**\n * Set the LiveKit room instance\n */\n setRoom(room: Room): void {\n // Clean up previous room listeners\n if (this.room) {\n this.cleanupListeners();\n }\n\n this.room = room;\n this.setupListeners();\n }\n\n /**\n * Setup message listeners\n */\n private setupListeners(): void {\n if (!this.room) return;\n\n this.room.on(RoomEvent.DataReceived, this._boundHandleDataReceived);\n }\n\n /**\n * Clean up message listeners\n */\n private cleanupListeners(): void {\n if (!this.room) return;\n\n this.room.off(RoomEvent.DataReceived, this._boundHandleDataReceived);\n }\n\n /**\n * Handle incoming data message\n */\n private handleDataReceived(\n payload: Uint8Array,\n _participant: unknown,\n transport: \"livekit\" | \"websocket\" | \"sse\" = \"livekit\",\n ): void {\n let messageString = \"\";\n let parsed = false;\n\n try {\n // Decode bytes to string\n const decoder = new TextDecoder();\n messageString = decoder.decode(payload);\n\n // Parse JSON\n const messageData = JSON.parse(messageString);\n parsed = true;\n\n // Canonical output has two transport envelopes. Normalize it before the\n // legacy message switch so both LiveKit and WebSocket execute one path.\n const modelOutputPayload = extractModelOutputPayload(messageData);\n if (modelOutputPayload !== undefined) {\n this._handleModelOutput(modelOutputPayload);\n return;\n }\n\n // Extract and categorize messages for chat display\n const timestamp = new Date().toISOString();\n const messageId = `${messageData.type}-${Date.now()}-${Math.random()}`;\n\n // Multi-character routing identity, carried inline on every\n // character-originated event. Undefined in single-character sessions, so\n // these spread to nothing and every existing message keeps its shape.\n const attribution: Pick<ChatMessage, \"membershipId\" | \"characterId\"> = {\n ...(messageData.data?.membership_id && {\n membershipId: messageData.data.membership_id as string,\n }),\n ...(messageData.data?.character_id && {\n characterId: messageData.data.character_id as string,\n }),\n };\n /**\n * Two instances must never share a streaming bubble. Without this a\n * target switch mid-turn splices one character's text onto another's,\n * because the merge would only be checking `isStreaming`.\n */\n const sameSpeaker = (msg: ChatMessage | undefined): boolean =>\n !!msg && msg.membershipId === attribution.membershipId;\n\n // Handle different message types\n switch (messageData.type) {\n // Submitted text is appended locally by sendUserTextMessage. Ignore\n // server echoes so each user message appears exactly once.\n case \"user_text_message\":\n case \"user-llm-text\":\n case \"text-input\":\n case \"user-input\":\n case \"text-message\":\n case \"chat-message\":\n case \"input-text\":\n case \"message\":\n // Skip server echoes; local text and voice transcription are handled separately.\n break;\n\n // Bot Ready - Bot is ready, connection should be true\n case \"bot-ready\":\n // `about` carries this instance's routing identity in a roster room,\n // and the same fields are also flattened onto `data`. Prefer `about`,\n // fall back to the flattened copy, and pass undefined for a\n // single-character session.\n this.emit(\n \"botReady\",\n messageData.data?.about ??\n (messageData.data?.membership_id ? messageData.data : undefined),\n );\n break;\n\n // Bot LLM Started - Begin streaming response\n case \"bot-llm-started\":\n this.isBotResponding = true;\n this.emit(\"botRespondingChange\", true);\n this.seenBotOutputSegments.clear();\n\n // Lifecycle frames do not carry output_id. In v2 or SSE, keep at most one\n // unowned placeholder so concurrent starts cannot create anonymous\n // rows that later get adopted in reverse order. Invocation-correlated\n // chunks establish additional rows in authoritative arrival order.\n const hasUnownedCorrelatedStarter =\n (this.modelOutputVersion === 2 || transport === \"sse\") &&\n this.chatMessages.some(\n (message) =>\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n message.content === \"\" &&\n message.outputId === undefined &&\n message.logicalTurnId === undefined,\n );\n if (hasUnownedCorrelatedStarter) break;\n\n // Add initial streaming message to chat.\n const streamingMessage: ChatMessage = {\n id: messageId,\n type: \"bot-llm-text\",\n content: \"\",\n timestamp: timestamp,\n isStreaming: true,\n ...attribution,\n };\n this.activeBotLlmMessageId = messageId;\n this.chatMessages.push(streamingMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n break;\n\n // Bot Output - Aggregated output stream (sentence/word-level, replaces bot-transcript)\n // Payload: { text, spoken, aggregated_by, segment_id, spoken_status,\n // spoken_progress }. The same segment_id is re-sent with the FULL\n // segment text on every TTS progress tick (spoken_status\n // \"in-progress\" → \"completed\"), so appending per event repeats the\n // sentence once per tick.\n case \"bot-output\":\n if (messageData.data?.text) {\n const newText = messageData.data.text;\n const segmentId = messageData.data.segment_id as\n | number\n | string\n | undefined;\n this.emit(\"botOutput\", {\n text: newText,\n spoken: messageData.data.spoken ?? false,\n aggregatedBy: messageData.data.aggregated_by ?? \"sentence\",\n segmentId,\n spokenStatus: messageData.data.spoken_status,\n });\n\n // Progress re-send of a segment already in the chat — skip.\n if (segmentId !== undefined) {\n if (this.seenBotOutputSegments.has(segmentId)) break;\n this.seenBotOutputSegments.add(segmentId);\n }\n\n const activeBotLlmMessage = this.activeBotLlmMessageId\n ? this.chatMessages.find(\n (message) => message.id === this.activeBotLlmMessageId,\n )\n : undefined;\n const lastMsg = this.chatMessages[this.chatMessages.length - 1];\n // When the reply is rendered by the LLM text stream\n // (bot-llm-text), the TTS echo adds nothing — never mix\n // bot-output into an LLM bubble (streaming or complete).\n if (activeBotLlmMessage?.type === \"bot-llm-text\") break;\n if (lastMsg?.type === \"bot-llm-text\") break;\n if (\n lastMsg?.type === \"bot-output\" &&\n lastMsg.content.includes(newText)\n ) {\n break;\n }\n if (\n lastMsg?.type === \"bot-output\" &&\n lastMsg.isStreaming &&\n sameSpeaker(lastMsg)\n ) {\n this.chatMessages[this.chatMessages.length - 1] = {\n ...lastMsg,\n content: lastMsg.content + newText,\n };\n } else {\n this.chatMessages.push({\n id: `bot-output-${Date.now()}-${Math.random()}`,\n type: \"bot-output\",\n content: newText,\n timestamp: new Date().toISOString(),\n isStreaming: true,\n ...attribution,\n });\n }\n this.emit(\"messagesChange\", this.chatMessages);\n }\n break;\n\n // Bot LLM Text Messages - Streaming chunks\n case \"bot-llm-text\":\n if (messageData.data?.text) {\n const newChunk = messageData.data.text;\n // Core's Interaction API/SSE path can carry logical-turn metadata\n // before hosts opt into the full model-output v2 contract. Preserve\n // it for SSE rows so callers can correlate legacy/default text\n // completions without changing LiveKit's historical v1 shape.\n const acceptsSseCorrelation = transport === \"sse\";\n const outputId =\n (this.modelOutputVersion === 2 || acceptsSseCorrelation) &&\n isValidOutputId(messageData.data.output_id)\n ? messageData.data.output_id\n : undefined;\n const logicalTurnId =\n (this.modelOutputVersion === 2 || acceptsSseCorrelation) &&\n isValidLogicalTurnId(messageData.data.logical_turn_id)\n ? messageData.data.logical_turn_id\n : undefined;\n\n // A logical turn can contain multiple provider invocations (for\n // example, the response before a tool call and the continuation\n // after its result). Prefer the invocation id, then fall back to\n // the older v2 logical-turn id when Core does not send one.\n let targetMessageIndex = -1;\n if (outputId !== undefined) {\n for (\n let index = this.chatMessages.length - 1;\n index >= 0;\n index--\n ) {\n const message = this.chatMessages[index];\n if (\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n message.outputId === outputId &&\n sameSpeaker(message)\n ) {\n targetMessageIndex = index;\n break;\n }\n }\n\n // bot-llm-started has no correlation metadata. Let one empty,\n // unowned row adopt the first authoritative invocation id.\n if (targetMessageIndex === -1) {\n for (\n let index = this.chatMessages.length - 1;\n index >= 0;\n index--\n ) {\n const message = this.chatMessages[index];\n if (\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n message.content === \"\" &&\n message.outputId === undefined &&\n message.logicalTurnId === undefined &&\n sameSpeaker(message)\n ) {\n targetMessageIndex = index;\n break;\n }\n }\n }\n } else if (logicalTurnId !== undefined) {\n for (\n let index = this.chatMessages.length - 1;\n index >= 0;\n index--\n ) {\n const message = this.chatMessages[index];\n if (\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n message.outputId === undefined &&\n message.logicalTurnId === logicalTurnId &&\n sameSpeaker(message)\n ) {\n targetMessageIndex = index;\n break;\n }\n }\n\n // Older v2 Core still needs the starter-row behavior that was\n // introduced with logical-turn correlation.\n if (targetMessageIndex === -1) {\n const lastIndex = this.chatMessages.length - 1;\n const lastMessage = this.chatMessages[lastIndex];\n if (\n lastMessage?.type === \"bot-llm-text\" &&\n lastMessage.isStreaming &&\n lastMessage.content === \"\" &&\n lastMessage.outputId === undefined &&\n lastMessage.logicalTurnId === undefined &&\n sameSpeaker(lastMessage)\n ) {\n targetMessageIndex = lastIndex;\n }\n }\n } else {\n // Uncorrelated (v1) chunk: it belongs to the bubble bot-llm-started\n // opened for this speaker. Only when no such bubble exists does the\n // historical \"last streaming row\" rule apply -- and still only for\n // the same speaker, so a target switch mid-turn cannot splice one\n // member's text onto another's row.\n // Ownership applies among uncorrelated rows only: an uncorrelated\n // chunk must never join a row that carries an output or turn id,\n // or a v2 correlated stream would absorb legacy text.\n if (this.activeBotLlmMessageId) {\n targetMessageIndex = this.chatMessages.findIndex(\n (message) =>\n message.id === this.activeBotLlmMessageId &&\n message.type === \"bot-llm-text\" &&\n message.isStreaming &&\n !message.outputId &&\n !message.logicalTurnId &&\n sameSpeaker(message),\n );\n }\n if (targetMessageIndex === -1) {\n const lastIndex = this.chatMessages.length - 1;\n const lastMessage = this.chatMessages[lastIndex];\n if (\n lastMessage &&\n lastMessage.type === \"bot-llm-text\" &&\n lastMessage.isStreaming &&\n !lastMessage.outputId &&\n !lastMessage.logicalTurnId &&\n sameSpeaker(lastMessage)\n ) {\n targetMessageIndex = lastIndex;\n }\n }\n }\n\n let updatedMessage: ChatMessage;\n if (targetMessageIndex !== -1) {\n const targetMessage = this.chatMessages[targetMessageIndex];\n updatedMessage = {\n ...targetMessage,\n content: targetMessage.content + newChunk,\n ...(outputId !== undefined &&\n targetMessage.outputId === undefined\n ? { outputId }\n : {}),\n ...(logicalTurnId !== undefined &&\n targetMessage.logicalTurnId === undefined\n ? { logicalTurnId }\n : {}),\n };\n this.chatMessages[targetMessageIndex] = updatedMessage;\n } else {\n // If no streaming message exists yet, create one\n updatedMessage = {\n id: `${messageData.type}-${Date.now()}-${Math.random()}`,\n type: \"bot-llm-text\",\n content: newChunk,\n timestamp: new Date().toISOString(),\n isStreaming: true,\n ...(outputId ? { outputId } : {}),\n ...(logicalTurnId ? { logicalTurnId } : {}),\n ...attribution,\n };\n this.activeBotLlmMessageId = updatedMessage.id;\n this.chatMessages.push(updatedMessage);\n }\n this.emit(\"messagesChange\", this.chatMessages, updatedMessage);\n }\n break;\n\n // Parallel diagnostics-only provider output. This must never affect\n // normal chat, speech, TTS, or response lifecycle state.\n case \"bot-llm-text-raw\":\n this._handleBotLlmTextRaw(messageData.data);\n break;\n\n // Bot LLM Stopped - Complete streaming response\n case \"bot-llm-stopped\": {\n // A v2 response can have interleaved correlated rows. The stop event\n // is response-scoped, so none may remain open for later chunks.\n const finalizedMessages = this.finalizeStreamingBotMessages();\n\n // Clear streaming state\n this.isBotResponding = false;\n this.emit(\"botRespondingChange\", false);\n // Keep the full-list event at one emission per stop while carrying\n // every v2 terminal delta for singular-message consumers.\n this.emit(\n \"messagesChange\",\n this.chatMessages,\n undefined,\n finalizedMessages,\n );\n break;\n }\n\n // Text-only Interactions API terminal marker. This is deliberately a\n // separate event: a completed REST turn must not be represented as a\n // synthetic bot-llm-stopped message.\n case \"bot-turn-completed\": {\n const finalizedMessages = this.finalizeStreamingBotMessages(\n transport === \"sse\",\n );\n if (finalizedMessages.length > 0) {\n this.emit(\n \"messagesChange\",\n this.chatMessages,\n undefined,\n finalizedMessages,\n );\n }\n this.isBotResponding = false;\n this.emit(\"botRespondingChange\", false);\n this.emit(\"botTurnCompleted\", messageData.data ?? {});\n break;\n }\n\n // User Transcription Messages\n case \"user-transcription\":\n if (messageData.data?.text) {\n const messageContent = messageData.data.text;\n\n if (messageData.data?.final) {\n const logicalTurnId = this.transcriptionLogicalTurnId(\n messageData.data.logical_turn_id,\n );\n // Final transcription - accumulate in single bubble until bot responds\n const lastUserMsg =\n this.chatMessages[this.chatMessages.length - 1];\n // A final transcription identical to the line appended locally on send is\n // the server echoing that send back: drop it, or the user sees it twice.\n if (\n lastUserMsg?.type === \"user-llm-text\" &&\n lastUserMsg.content.trim() === String(messageContent).trim()\n ) {\n this.userTranscription = \"\";\n this.emit(\"userTranscriptionChange\", \"\");\n break;\n }\n\n // Check if last message is an accumulating user message (isStreaming=true)\n if (\n lastUserMsg &&\n lastUserMsg.type === \"user-transcription\" &&\n lastUserMsg.isStreaming === true\n ) {\n // Accumulate: append new content to existing message with space\n const accumulatedContent =\n lastUserMsg.content.trim() + \" \" + messageContent.trim();\n\n this.chatMessages[this.chatMessages.length - 1] = {\n ...lastUserMsg,\n ...(logicalTurnId === undefined ? {} : { logicalTurnId }),\n content: accumulatedContent,\n timestamp: timestamp,\n };\n } else {\n // Start new accumulating message (isStreaming=true means \"still accumulating\")\n const userMessage: ChatMessage = {\n id: messageId,\n type: \"user-transcription\",\n content: messageContent,\n timestamp: timestamp,\n isStreaming: true,\n ...(logicalTurnId === undefined ? {} : { logicalTurnId }),\n };\n this.chatMessages.push(userMessage);\n }\n this.emit(\"messagesChange\", this.chatMessages);\n\n // Clear live transcription when final\n this.userTranscription = \"\";\n this.emit(\"userTranscriptionChange\", \"\");\n } else {\n // Non-final transcription - show in input field\n this.userTranscription = messageContent;\n this.emit(\"userTranscriptionChange\", messageContent);\n }\n }\n break;\n\n // Bot Emotion Messages — LiveKit path: arrives as an RTVI message frame (messageData.data)\n case \"bot-emotion\":\n if (messageData.data?.emotion) {\n this._currentEmotion = {\n emotion: messageData.data.emotion,\n scale: messageData.data.scale,\n };\n if (this.logRtviMessages) console.log(\"[SDK bot-emotion]\", messageData.data);\n this.emit(\"emotionChange\", this._currentEmotion);\n const emotionMessage: ChatMessage = {\n id: messageId,\n type: \"bot-emotion\",\n content: `${messageData.data.emotion} (scale: ${messageData.data.scale || 1})`,\n timestamp,\n };\n this.chatMessages.push(emotionMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n }\n break;\n // Bot TTS Text - Words as spoken by TTS (word-by-word for socket-based TTS)\n case \"bot-tts-text\":\n if (messageData.data?.text) {\n this.emit(\"botTtsText\", { text: messageData.data.text });\n }\n break;\n\n // Bot TTS lifecycle (more granular than bot-started/stopped-speaking)\n case \"bot-tts-started\":\n this.emit(\"botTtsStarted\");\n break;\n\n case \"bot-tts-stopped\":\n this.emit(\"botTtsStopped\");\n break;\n\n // Server-side muting indicators\n case \"user-mute-started\":\n this.emit(\"userMuteStarted\");\n break;\n\n case \"user-mute-stopped\":\n this.emit(\"userMuteStopped\");\n break;\n\n case \"user-started-speaking\":\n this.isListening = true;\n this.emit(\"listeningChange\", true);\n break;\n\n case \"user-stopped-speaking\":\n this.isListening = false;\n this.emit(\"listeningChange\", false);\n break;\n\n // Bot Started Speaking (priority over listening: state becomes speaking)\n case \"bot-started-speaking\": {\n const owner = this.lifecycleOwner(messageData.data);\n if (this.blendshapeQueue.isStaleOwner(owner)) {\n break;\n }\n const allowed = this.blendshapeQueue.startBotSpeaking();\n if (allowed) {\n this.isSpeaking = true;\n this.totalBlendshapesReceived = 0;\n this.emit(\"speakingChange\", true);\n }\n break;\n }\n\n // Bot Stopped Speaking\n case \"bot-stopped-speaking\":\n if (this.blendshapeQueue.isStaleOwner(this.lifecycleOwner(messageData.data))) {\n break;\n }\n if (this.isSpeaking) {\n this.isSpeaking = false;\n this.blendshapeQueue.stopBotSpeaking();\n this.emit(\"speakingChange\", false);\n }\n break;\n\n // Action Response Messages (LiveKit/WebRTC path)\n case \"action-response\":\n this._handleActionResponse(messageData.data?.actions ?? [], messageId, timestamp);\n break;\n\n case \"metrics\":\n if (messageData.data) {\n this.emit(\"metrics\", messageData.data);\n }\n break;\n\n // Per-turn usage/cost breakdown. Informational only: the server streams\n // it once the client opts in with usage-toggle, and it never affects\n // server-side usage tracking or billing.\n case \"usage-update\":\n if (messageData.data) {\n this.emit(\"usageUpdate\", messageData.data);\n }\n break;\n // Behavior Tree Response Messages\n case \"behavior-tree-response\":\n if (messageData.data?.narrative_section_id) {\n const behaviorMessage: ChatMessage = {\n id: messageId,\n type: \"behavior-tree\",\n content: `Narrative Section: ${messageData.data.narrative_section_id}`,\n timestamp,\n };\n this.chatMessages.push(behaviorMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n }\n break;\n\n // Server Message - Contains various data types like blendshapes\n case \"server-message\":\n if (messageData.data) {\n const serverData = messageData.data;\n\n switch (serverData.type) {\n case \"bot-llm-text-raw\":\n this._handleBotLlmTextRaw(serverData.data ?? serverData);\n break;\n\n case \"final-user-transcription\":\n if (serverData.text) {\n const finalText = serverData.text;\n const logicalTurnId = this.transcriptionLogicalTurnId(\n serverData.logical_turn_id,\n );\n const lastUserMsg =\n this.chatMessages[this.chatMessages.length - 1];\n // A final transcription identical to the line appended locally on send is\n // the server echoing that send back: drop it, or the user sees it twice.\n if (\n lastUserMsg?.type === \"user-llm-text\" &&\n lastUserMsg.content.trim() === String(finalText).trim()\n ) {\n this.userTranscription = \"\";\n this.emit(\"userTranscriptionChange\", \"\");\n break;\n }\n if (\n lastUserMsg &&\n lastUserMsg.type === \"user-transcription\" &&\n lastUserMsg.isStreaming === true\n ) {\n this.chatMessages[this.chatMessages.length - 1] = {\n ...lastUserMsg,\n ...(logicalTurnId === undefined\n ? {}\n : { logicalTurnId }),\n content: finalText,\n timestamp,\n isStreaming: false,\n };\n } else {\n this.chatMessages.push({\n id: `${serverData.type}-${Date.now()}-${Math.random()}`,\n type: \"user-transcription\",\n content: finalText,\n timestamp,\n isStreaming: false,\n ...(logicalTurnId === undefined\n ? {}\n : { logicalTurnId }),\n });\n }\n this.emit(\"messagesChange\", this.chatMessages);\n this.userTranscription = \"\";\n this.emit(\"userTranscriptionChange\", \"\");\n }\n break;\n\n // WebSocket path: arrives inside a bot-text data message as serverData (not messageData.data)\n case \"bot-emotion\":\n if (serverData.emotion) {\n this._currentEmotion = {\n emotion: serverData.emotion,\n scale: serverData.scale,\n };\n if (this.logRtviMessages) console.log(\"[SDK bot-emotion (ws)]\", serverData);\n this.emit(\"emotionChange\", this._currentEmotion);\n }\n break;\n\n case \"blendshape-turn-stats\":\n if (this.blendshapeQueue.isStaleOwner(this.lifecycleOwner(serverData))) {\n break;\n }\n this.blendshapeQueue.endConversation(\n serverData.stats,\n this.lifecycleOwner(serverData),\n );\n this.emit(\"blendshapeStatsReceived\", serverData);\n break;\n\n case \"chunked-neurosync-blendshapes\":\n if (serverData.blendshapes) {\n const owner = this.lifecycleOwner(serverData);\n if (this.blendshapeQueue.isRetiredOwner(owner)) {\n break;\n }\n // If the queue is idle (no active conversation, bot not yet speaking),\n // start a conversation so early chunks from an initial bot greeting\n // are accepted rather than silently dropped.\n if (\n !this.blendshapeQueue.isConversationActive() &&\n !this.isSpeaking\n ) {\n this.blendshapeQueue.startConversation();\n }\n this.blendshapeQueue.addChunk(\n serverData.blendshapes,\n owner,\n );\n if (\n this.blendshapeQueue.consumeOwnerReplacementStoppedSpeaking() &&\n this.isSpeaking\n ) {\n this.isSpeaking = false;\n this.emit(\"speakingChange\", false);\n }\n this.totalBlendshapesReceived +=\n serverData.blendshapes.length;\n this.emit(\"blendshapes\", serverData);\n }\n break;\n\n case \"neurosync-blendshapes-cancel\": {\n const cancelledActive = this.blendshapeQueue.cancelOwner(\n this.lifecycleOwner(serverData),\n );\n if (cancelledActive && this.isSpeaking) {\n this.isSpeaking = false;\n this.emit(\"speakingChange\", false);\n }\n this.emit(\"blendshapeCancel\", serverData);\n break;\n }\n\n case \"user-idle-warning\": {\n const remainingSeconds: number | null =\n serverData.remaining_seconds ??\n serverData.remainingSeconds ??\n null;\n this.emit(\"idleWarning\", { remainingSeconds });\n this.chatMessages.push({\n id: `user-idle-warning-${Date.now()}-${Math.random()}`,\n type: \"idle-warning\",\n content:\n remainingSeconds !== null ? String(remainingSeconds) : \"\",\n timestamp,\n });\n this.emit(\"messagesChange\", this.chatMessages);\n break;\n }\n\n case \"llm-no-response\":\n this.emit(\"llmNoResponse\");\n this.chatMessages.push({\n id: `llm-no-response-${Date.now()}-${Math.random()}`,\n type: \"llm-no-response\",\n content: \"\",\n timestamp,\n });\n this.emit(\"messagesChange\", this.chatMessages);\n break;\n\n // Action Response (WebSocket/RTVI path — wrapped in server-message)\n case \"action-response\":\n this._handleActionResponse(serverData.actions ?? [], messageId, timestamp);\n break;\n\n // Server acknowledgment — arrives wrapped in server-message envelope\n case \"server-response\": {\n // client-ready is an internal RTVI handshake; server echoes an\n // \"Unknown message type\" error for it which is expected and harmless.\n if (serverData.event_type === \"client-ready\") break;\n const serverResponse: ServerResponse = {\n event_type: serverData.event_type ?? \"unknown\",\n status: serverData.status ?? \"error\",\n message: serverData.message ?? null,\n extras: serverData.extras ?? null,\n };\n this.emit(\"serverResponse\", serverResponse);\n break;\n }\n\n /**\n * Lifecycle of one roster member. Emitted when a member is added\n * at runtime, becomes ready, or fails after /connect already\n * returned 200. `bot-ready` stays the readiness authority --\n * a `character-status` of \"ready\" must not enable interaction on\n * its own -- but this is the only way a background dispatch\n * failure is ever reported.\n */\n case \"character-status\": {\n const status: CharacterStatusEvent = {\n status: (serverData.status as CharacterStatusEvent[\"status\"]) ?? \"starting\",\n membershipId: (serverData.membership_id as string) ?? \"\",\n characterId: (serverData.character_id as string) ?? \"\",\n characterSessionId:\n (serverData.character_session_id as string) ?? null,\n participantIdentity:\n (serverData.participant_identity as string) ??\n (serverData.membership_id\n ? `character:${serverData.membership_id}`\n : \"\"),\n roomSessionId: (serverData.room_session_id as string) ?? null,\n rosterEpoch:\n typeof serverData.roster_epoch === \"number\"\n ? serverData.roster_epoch\n : null,\n failureCode: (serverData.failure_code as string) ?? null,\n };\n // Unattributable status is dropped rather than guessed at.\n if (status.membershipId) this.emit(\"characterStatus\", status);\n break;\n }\n\n // interaction-created: session IDs for analytics/tracking\n case \"interaction-created\": {\n const interactionCreated: InteractionCreated = {\n interactionId: serverData.interaction_id,\n characterSessionId: serverData.character_session_id,\n };\n this.emit(\"interactionCreated\", interactionCreated);\n break;\n }\n }\n }\n break;\n\n // Fallback: server-response at top level (not currently observed but kept for safety)\n case \"server-response\": {\n if (messageData.event_type === \"client-ready\") break;\n const serverResponse: ServerResponse = {\n event_type: messageData.event_type ?? \"unknown\",\n status: messageData.status ?? \"error\",\n message: messageData.message ?? null,\n extras: messageData.extras ?? null,\n };\n this.emit(\"serverResponse\", serverResponse);\n break;\n }\n\n // Silently ignore other message types\n case \"moderation-response\":\n case \"trigger-message\":\n case \"update-template-keys\":\n case \"update-dynamic-info\":\n case \"interrupt-bot\":\n break;\n }\n } catch (error) {\n // Silently handle parse errors\n }\n }\n\n private _handleActionResponse(\n actions: Array<ConvaiAction | ConvaiToolCall>,\n messageId: string,\n timestamp: string,\n ): void {\n // model-output v2 is canonical. action-response remains a compatibility\n // projection and executing both would duplicate the same operation.\n if (this.modelOutputVersion === 2) return;\n this.emit(\"actionResponse\", { actions });\n const label = actions.length === 0\n ? \"No action\"\n : actions.map((a) => (a.target ? `${a.name} → ${a.target}` : a.name)).join(\", \");\n const actionMessage: ChatMessage = {\n id: messageId,\n type: \"action\",\n content: label,\n timestamp,\n };\n this.chatMessages.push(actionMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n }\n\n private _handleModelOutput(value: unknown): void {\n if (this.modelOutputVersion !== 2) return;\n const output = parseModelOutputMessage(value);\n if (!output) {\n this.emit(\"modelOutputProtocolError\", {\n code: \"invalid_model_output\",\n message: \"Core sent a malformed or unsupported model-output envelope\",\n });\n return;\n }\n // output_id identifies a provider invocation, not a serialized envelope.\n // One invocation can produce distinct text and tool-call projections with\n // the same id, and consumers must receive both in transport arrival order.\n // Suppress only an exact transport replay of an already delivered payload.\n const fingerprint = stableJsonFingerprint(output);\n if (this.seenModelOutputPayloads.has(fingerprint)) return;\n if (fingerprint.length > MAX_MODEL_OUTPUT_REPLAY_RETAINED_CHARS) {\n this.emit(\"modelOutputProtocolError\", {\n code: \"invalid_model_output\",\n message: \"Core sent an oversized model-output envelope\",\n });\n return;\n }\n this.seenModelOutputPayloads.add(fingerprint);\n this.seenModelOutputPayloadCharacters += fingerprint.length;\n while (\n this.seenModelOutputPayloads.size > MAX_MODEL_OUTPUT_REPLAY_FINGERPRINTS ||\n this.seenModelOutputPayloadCharacters >\n MAX_MODEL_OUTPUT_REPLAY_RETAINED_CHARS\n ) {\n const oldestFingerprint = this.seenModelOutputPayloads.values().next()\n .value as string | undefined;\n if (!oldestFingerprint) break;\n this.seenModelOutputPayloads.delete(oldestFingerprint);\n this.seenModelOutputPayloadCharacters -= oldestFingerprint.length;\n }\n this.emit(\"modelOutput\", output satisfies ModelOutputMessage);\n }\n\n private _clearModelOutputReplayLedger(): void {\n this.seenModelOutputPayloads.clear();\n this.seenModelOutputPayloadCharacters = 0;\n }\n\n private _handleBotLlmTextRaw(value: unknown): void {\n if (this.modelOutputVersion !== 2 || !value || typeof value !== \"object\") {\n return;\n }\n const data = value as {\n text?: unknown;\n logical_turn_id?: unknown;\n output_id?: unknown;\n };\n if (\n typeof data.text !== \"string\" ||\n !isValidLogicalTurnId(data.logical_turn_id)\n ) {\n return;\n }\n this.emit(\"botLlmTextRaw\", {\n text: data.text,\n logical_turn_id: data.logical_turn_id,\n ...(isValidOutputId(data.output_id)\n ? { output_id: data.output_id }\n : {}),\n } satisfies BotLlmTextRawEvent);\n }\n\n private lifecycleOwner(\n data: LifecycleOwnerPayload | undefined,\n ): BlendshapeChunkMetadata {\n if (!data) {\n return {};\n }\n\n return {\n responseId: data.response_id,\n neurosyncTurnId: data.neurosync_turn_id ?? data.turn_id,\n epoch: data.epoch,\n fps: this.numberOrUndefined((data as { fps?: number }).fps),\n startFrameIndex: this.numberOrUndefined(\n (data as { start_frame_index?: number }).start_frame_index,\n ),\n };\n }\n\n private numberOrUndefined(value: unknown): number | undefined {\n return typeof value === \"number\" && Number.isFinite(value)\n ? value\n : undefined;\n }\n\n private transcriptionLogicalTurnId(value: unknown): string | undefined {\n return this.modelOutputVersion === 2 && isValidLogicalTurnId(value)\n ? value\n : undefined;\n }\n\n /**\n * Public entry point for WebSocket transport to inject messages into the same pipeline.\n */\n handleDataReceivedPublic(\n payload: Uint8Array,\n transport: \"websocket\" | \"sse\" = \"websocket\",\n ): void {\n this.handleDataReceived(payload, null, transport);\n }\n\n /** Append a submitted text message without waiting for a server echo. */\n addUserTextMessage(text: string): void {\n const content = text.trim();\n if (!content) return;\n this.chatMessages.push({\n id: `user-llm-text-${Date.now()}-${Math.random()}`,\n type: \"user-llm-text\",\n content,\n timestamp: new Date().toISOString(),\n });\n this.emit(\"messagesChange\", this.chatMessages);\n }\n\n /**\n * Insert a message into the transcript locally. Nothing is transmitted and\n * no LLM turn is billed -- this is for client-side lines (a greeting, a\n * system notice, a replayed history entry) that must appear in the chat.\n *\n * Subscribers see it through the same `messagesChange` as a live message,\n * so React and the widgets pick it up with no special casing.\n */\n appendLocalMessage(message: {\n sender: \"user\" | \"bot\";\n content: string;\n membershipId?: string;\n characterId?: string;\n }): ChatMessage {\n const chatMessage: ChatMessage = {\n id: `local-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`,\n type: message.sender === \"user\" ? \"user-llm-text\" : \"bot-llm-text\",\n content: message.content,\n timestamp: new Date().toISOString(),\n ...(message.membershipId && { membershipId: message.membershipId }),\n ...(message.characterId && { characterId: message.characterId }),\n };\n this.chatMessages.push(chatMessage);\n this.emit(\"messagesChange\", this.chatMessages);\n return chatMessage;\n }\n\n /**\n * Reset message state\n */\n reset(): void {\n this.chatMessages = [];\n this.activeBotLlmMessageId = null;\n this.userTranscription = \"\";\n this.isBotResponding = false;\n this.isSpeaking = false;\n this.isListening = false;\n this.totalBlendshapesReceived = 0;\n this._currentEmotion = null;\n this.seenBotOutputSegments.clear();\n this._clearModelOutputReplayLedger();\n this.blendshapeQueue.reset();\n this.emit(\"messagesChange\", this.chatMessages);\n this.emit(\"userTranscriptionChange\", \"\");\n this.emit(\"botRespondingChange\", false);\n this.emit(\"speakingChange\", false);\n this.emit(\"listeningChange\", false);\n this.emit(\"emotionChange\", null);\n }\n}\n"]}
|