@chatterfly/widget 0.4.0 → 0.6.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/embed.css +1 -1
- package/dist/embed.css.map +1 -1
- package/dist/embed.global.js +31 -25
- package/dist/embed.global.js.map +1 -1
- package/dist/index.css +104 -14
- package/dist/index.css.map +1 -1
- package/dist/index.d.mts +73 -5
- package/dist/index.d.ts +73 -5
- package/dist/index.js +762 -213
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +763 -214
- package/dist/index.mjs.map +1 -1
- package/package.json +67 -61
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { forwardRef, useCallback, useImperativeHandle,
|
|
1
|
+
import { forwardRef, useCallback, useImperativeHandle, useState, useEffect, useMemo, useRef, createElement } from 'react';
|
|
2
2
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
3
3
|
|
|
4
4
|
// src/components/ChatterflyWidget.tsx
|
|
@@ -22,6 +22,8 @@ var defaultStrings = {
|
|
|
22
22
|
statusCompletedTitle: "Completed",
|
|
23
23
|
statusCompletedBody: "This workflow run has finished.",
|
|
24
24
|
statusErrorTitle: "Something went wrong",
|
|
25
|
+
statusAuthorizationTitle: "Authorization required",
|
|
26
|
+
statusAuthorizationBody: "Reconnect the linked account to continue this workflow.",
|
|
25
27
|
statusOfflineTitle: "Connection lost",
|
|
26
28
|
statusOfflineBody: "Unable to reach the server. Please check your connection and refresh.",
|
|
27
29
|
statusExpiredTitle: "Session expired",
|
|
@@ -30,7 +32,9 @@ var defaultStrings = {
|
|
|
30
32
|
resumingConversation: "Resuming conversation\u2026",
|
|
31
33
|
reconnecting: "Reconnecting\u2026",
|
|
32
34
|
conversationCompleted: "Conversation completed.",
|
|
35
|
+
conversationTimedOut: "This conversation timed out.",
|
|
33
36
|
somethingWentWrong: "Something went wrong",
|
|
37
|
+
tryAgain: "Try again",
|
|
34
38
|
chatInputPlaceholder: "Type a message\u2026",
|
|
35
39
|
sendButton: "Send",
|
|
36
40
|
handBackButton: "Hand Back",
|
|
@@ -76,6 +80,7 @@ function useRunStream(config, onEvent) {
|
|
|
76
80
|
const pollIntervalMs = (_b = !dormancyDisabled && typeof policyConfig === "object" ? policyConfig == null ? void 0 : policyConfig.pollIntervalMs : void 0) != null ? _b : DEFAULT_POLL_INTERVAL_MS;
|
|
77
81
|
const hibernateAfterMs = (_c = !dormancyDisabled && typeof policyConfig === "object" ? policyConfig == null ? void 0 : policyConfig.hibernateAfterMs : void 0) != null ? _c : DEFAULT_HIBERNATE_AFTER_MS;
|
|
78
82
|
const [status, setStatus] = useState("idle");
|
|
83
|
+
const [failureKind, setFailureKind] = useState(null);
|
|
79
84
|
const [activeSession, setActiveSession] = useState(null);
|
|
80
85
|
const [messages, setMessages] = useState([]);
|
|
81
86
|
const [timeline, setTimeline] = useState([]);
|
|
@@ -297,7 +302,7 @@ function useRunStream(config, onEvent) {
|
|
|
297
302
|
};
|
|
298
303
|
}, [dormancyDisabled, wake]);
|
|
299
304
|
function handleEvent(event) {
|
|
300
|
-
var _a2, _b2, _c2, _d, _e;
|
|
305
|
+
var _a2, _b2, _c2, _d, _e, _f;
|
|
301
306
|
(_a2 = onEventRef.current) == null ? void 0 : _a2.call(onEventRef, event);
|
|
302
307
|
switch (event.type) {
|
|
303
308
|
case "timeline.replay": {
|
|
@@ -315,11 +320,27 @@ function useRunStream(config, onEvent) {
|
|
|
315
320
|
setStreamingText("");
|
|
316
321
|
break;
|
|
317
322
|
}
|
|
323
|
+
case "provider_operation": {
|
|
324
|
+
if (event.payload) {
|
|
325
|
+
mergeTimeline([{
|
|
326
|
+
id: `provider-${(_b2 = event.node_id) != null ? _b2 : "operation"}-${Date.now()}`,
|
|
327
|
+
run_id: runId,
|
|
328
|
+
session_id: null,
|
|
329
|
+
seq: Number.MAX_SAFE_INTEGER,
|
|
330
|
+
event_type: "provider_operation",
|
|
331
|
+
sender: "system",
|
|
332
|
+
payload: event.payload,
|
|
333
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
334
|
+
}]);
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
318
338
|
case "agent.token": {
|
|
319
339
|
setStreamingText((prev) => prev + event.token);
|
|
320
340
|
break;
|
|
321
341
|
}
|
|
322
342
|
case "run.started":
|
|
343
|
+
setFailureKind(null);
|
|
323
344
|
updateStatus("running");
|
|
324
345
|
break;
|
|
325
346
|
case "run.status_changed":
|
|
@@ -335,7 +356,7 @@ function useRunStream(config, onEvent) {
|
|
|
335
356
|
entry_context: event.entry_context,
|
|
336
357
|
status: "open",
|
|
337
358
|
messages: replayedMessages,
|
|
338
|
-
document_content: (
|
|
359
|
+
document_content: (_c2 = event.document_content) != null ? _c2 : null
|
|
339
360
|
};
|
|
340
361
|
setActiveSession(session);
|
|
341
362
|
if (replayedMessages.length > 0) {
|
|
@@ -348,7 +369,7 @@ function useRunStream(config, onEvent) {
|
|
|
348
369
|
} else if (event.layout === "form") {
|
|
349
370
|
updateStatus("awaiting_form");
|
|
350
371
|
} else {
|
|
351
|
-
const surface = (
|
|
372
|
+
const surface = (_d = event.entry_context) == null ? void 0 : _d.surface;
|
|
352
373
|
const isVoice = typeof surface === "string" && (surface === "widget/voice" || surface === "phone" || surface === "phone/outbound");
|
|
353
374
|
if (isVoice) {
|
|
354
375
|
updateStatus("awaiting_voice");
|
|
@@ -363,6 +384,11 @@ function useRunStream(config, onEvent) {
|
|
|
363
384
|
setMessages([]);
|
|
364
385
|
updateStatus("running");
|
|
365
386
|
break;
|
|
387
|
+
case "run.authorization_required":
|
|
388
|
+
setStreamingText("");
|
|
389
|
+
setFailureKind(null);
|
|
390
|
+
updateStatus("awaiting_authorization");
|
|
391
|
+
break;
|
|
366
392
|
case "message.sent": {
|
|
367
393
|
setMessages((prev) => [
|
|
368
394
|
...prev,
|
|
@@ -370,17 +396,22 @@ function useRunStream(config, onEvent) {
|
|
|
370
396
|
]);
|
|
371
397
|
break;
|
|
372
398
|
}
|
|
399
|
+
case "run_complete":
|
|
400
|
+
case "run_error":
|
|
401
|
+
break;
|
|
373
402
|
case "run.completed":
|
|
374
403
|
case "run.cancelled":
|
|
375
404
|
setStreamingText("");
|
|
405
|
+
setFailureKind(null);
|
|
376
406
|
if (event.state) setOutput(event.state);
|
|
377
407
|
updateStatus("completed");
|
|
378
|
-
(
|
|
408
|
+
(_e = wsRef.current) == null ? void 0 : _e.close();
|
|
379
409
|
break;
|
|
380
410
|
case "run.failed":
|
|
381
411
|
setStreamingText("");
|
|
412
|
+
setFailureKind(/timed out/i.test(event.error) ? "timeout" : null);
|
|
382
413
|
updateStatus("error");
|
|
383
|
-
(
|
|
414
|
+
(_f = wsRef.current) == null ? void 0 : _f.close();
|
|
384
415
|
break;
|
|
385
416
|
}
|
|
386
417
|
}
|
|
@@ -486,6 +517,8 @@ function useRunStream(config, onEvent) {
|
|
|
486
517
|
}, [connect]);
|
|
487
518
|
return {
|
|
488
519
|
status,
|
|
520
|
+
failureKind,
|
|
521
|
+
currentParticipantToken: currentToken,
|
|
489
522
|
activeSession,
|
|
490
523
|
messages,
|
|
491
524
|
timeline,
|
|
@@ -654,6 +687,10 @@ var widget_default = {
|
|
|
654
687
|
messageFeed: "widget_messageFeed",
|
|
655
688
|
feedMessage: "widget_feedMessage",
|
|
656
689
|
chatTimeline: "widget_chatTimeline",
|
|
690
|
+
toolActivity: "widget_toolActivity",
|
|
691
|
+
toolActivityPulse: "widget_toolActivityPulse",
|
|
692
|
+
toolActivityDone: "widget_toolActivityDone",
|
|
693
|
+
"cf-tool-pulse": "widget_cf-tool-pulse",
|
|
657
694
|
bubbleRow: "widget_bubbleRow",
|
|
658
695
|
bubbleUserRow: "widget_bubbleUserRow",
|
|
659
696
|
bubble: "widget_bubble",
|
|
@@ -676,6 +713,7 @@ var widget_default = {
|
|
|
676
713
|
formKvValue: "widget_formKvValue",
|
|
677
714
|
formKvNested: "widget_formKvNested",
|
|
678
715
|
activeInputArea: "widget_activeInputArea",
|
|
716
|
+
activeInputAreaVoice: "widget_activeInputAreaVoice",
|
|
679
717
|
formTimelineWrapper: "widget_formTimelineWrapper",
|
|
680
718
|
formScrollArea: "widget_formScrollArea",
|
|
681
719
|
activeInputPrompt: "widget_activeInputPrompt",
|
|
@@ -723,13 +761,19 @@ var widget_default = {
|
|
|
723
761
|
voiceRatingMatched: "widget_voiceRatingMatched",
|
|
724
762
|
voiceRatingLabels: "widget_voiceRatingLabels",
|
|
725
763
|
voiceTranscript: "widget_voiceTranscript",
|
|
726
|
-
|
|
764
|
+
voiceLiveLog: "widget_voiceLiveLog",
|
|
765
|
+
voiceLiveLogUser: "widget_voiceLiveLogUser",
|
|
766
|
+
voiceLiveLogAssistant: "widget_voiceLiveLogAssistant",
|
|
727
767
|
voiceTranscriptText: "widget_voiceTranscriptText",
|
|
768
|
+
voiceTranscriptLabel: "widget_voiceTranscriptLabel",
|
|
728
769
|
voiceError: "widget_voiceError",
|
|
729
770
|
cfVoiceShake: "widget_cfVoiceShake",
|
|
771
|
+
voiceErrorGroup: "widget_voiceErrorGroup",
|
|
772
|
+
voiceRetryButton: "widget_voiceRetryButton",
|
|
730
773
|
voiceOrbContainer: "widget_voiceOrbContainer",
|
|
731
|
-
|
|
774
|
+
voiceOrbRow: "widget_voiceOrbRow",
|
|
732
775
|
voiceOrbHint: "widget_voiceOrbHint",
|
|
776
|
+
voiceAssistantStatus: "widget_voiceAssistantStatus",
|
|
733
777
|
voiceOrb: "widget_voiceOrb",
|
|
734
778
|
cfVoiceBreathe: "widget_cfVoiceBreathe",
|
|
735
779
|
voiceOrbDisabled: "widget_voiceOrbDisabled",
|
|
@@ -738,12 +782,12 @@ var widget_default = {
|
|
|
738
782
|
voiceOrbRing2: "widget_voiceOrbRing2",
|
|
739
783
|
voiceOrb_idle: "widget_voiceOrb_idle",
|
|
740
784
|
voiceOrb_listening: "widget_voiceOrb_listening",
|
|
741
|
-
cfVoicePulse: "widget_cfVoicePulse",
|
|
742
785
|
voiceOrb_speaking: "widget_voiceOrb_speaking",
|
|
743
|
-
cfVoiceWave: "widget_cfVoiceWave",
|
|
744
786
|
voiceOrb_processing: "widget_voiceOrb_processing",
|
|
745
787
|
voiceOrb_confirming: "widget_voiceOrb_confirming",
|
|
746
788
|
voiceOrb_error: "widget_voiceOrb_error",
|
|
789
|
+
cfVoicePulse: "widget_cfVoicePulse",
|
|
790
|
+
cfVoiceWave: "widget_cfVoiceWave",
|
|
747
791
|
voiceAudioLevelBar: "widget_voiceAudioLevelBar",
|
|
748
792
|
voiceMuteButton: "widget_voiceMuteButton",
|
|
749
793
|
voiceConfirmation: "widget_voiceConfirmation",
|
|
@@ -1192,6 +1236,29 @@ function entryToDisplay(entry) {
|
|
|
1192
1236
|
case "delay_resumed":
|
|
1193
1237
|
return null;
|
|
1194
1238
|
// no visible bubble
|
|
1239
|
+
case "tool_call": {
|
|
1240
|
+
const tool = typeof payload.callee_agent === "string" ? payload.callee_agent : typeof payload.name === "string" ? payload.name : "Tool";
|
|
1241
|
+
return { text: `${tool} is working`, sender: "system", activity: "running" };
|
|
1242
|
+
}
|
|
1243
|
+
case "tool_result": {
|
|
1244
|
+
const tool = typeof payload.callee_agent === "string" ? payload.callee_agent : typeof payload.name === "string" ? payload.name : "Tool";
|
|
1245
|
+
return { text: `${tool} finished`, sender: "system", activity: "completed" };
|
|
1246
|
+
}
|
|
1247
|
+
case "provider_operation": {
|
|
1248
|
+
const operation = typeof payload.operation === "string" ? payload.operation : "Provider operation";
|
|
1249
|
+
if (payload.outcome === "error") {
|
|
1250
|
+
const code = typeof payload.error_code === "string" ? payload.error_code : "provider_operation_failed";
|
|
1251
|
+
return { text: `${operation} could not be completed (${code}).`, sender: "system" };
|
|
1252
|
+
}
|
|
1253
|
+
const recordID = typeof payload.record_id === "string" ? payload.record_id : "";
|
|
1254
|
+
const recordURL = typeof payload.record_url === "string" && payload.record_url.startsWith("https://") ? payload.record_url : "";
|
|
1255
|
+
return {
|
|
1256
|
+
text: payload.replayed === true ? `${operation} was already completed; the saved result was reused.` : `${operation} completed.`,
|
|
1257
|
+
tags: recordID ? [`Record ${recordID}`] : void 0,
|
|
1258
|
+
sender: "system",
|
|
1259
|
+
link: recordURL ? { href: recordURL, label: "Open record" } : void 0
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1195
1262
|
default:
|
|
1196
1263
|
return null;
|
|
1197
1264
|
}
|
|
@@ -1260,10 +1327,33 @@ function ChatTimeline({ entries, showTyping, streamingText, components }) {
|
|
|
1260
1327
|
}, [streamingText, scheduleScrollToBottom]);
|
|
1261
1328
|
return /* @__PURE__ */ jsxs("div", { ref: containerRef, className: widget_default.chatTimeline, onScroll: handleScroll, children: [
|
|
1262
1329
|
entries.map((entry) => {
|
|
1330
|
+
var _a2;
|
|
1263
1331
|
const display = entryToDisplay(entry);
|
|
1264
1332
|
if (!display) return null;
|
|
1265
1333
|
if (display.sender === "system") {
|
|
1266
|
-
return /* @__PURE__ */
|
|
1334
|
+
return /* @__PURE__ */ jsxs(
|
|
1335
|
+
"div",
|
|
1336
|
+
{
|
|
1337
|
+
className: display.activity ? widget_default.toolActivity : widget_default.bubbleSystem,
|
|
1338
|
+
role: display.activity === "running" ? "status" : void 0,
|
|
1339
|
+
children: [
|
|
1340
|
+
display.activity && /* @__PURE__ */ jsx(
|
|
1341
|
+
"span",
|
|
1342
|
+
{
|
|
1343
|
+
className: display.activity === "running" ? widget_default.toolActivityPulse : widget_default.toolActivityDone,
|
|
1344
|
+
"aria-hidden": "true"
|
|
1345
|
+
}
|
|
1346
|
+
),
|
|
1347
|
+
/* @__PURE__ */ jsx(MarkdownBubble, { content: display.text }),
|
|
1348
|
+
(_a2 = display.tags) == null ? void 0 : _a2.map((tag) => /* @__PURE__ */ jsxs("span", { children: [
|
|
1349
|
+
" ",
|
|
1350
|
+
tag
|
|
1351
|
+
] }, tag)),
|
|
1352
|
+
display.link && /* @__PURE__ */ jsx("a", { href: display.link.href, target: "_blank", rel: "noopener noreferrer", children: display.link.label })
|
|
1353
|
+
]
|
|
1354
|
+
},
|
|
1355
|
+
entry.id
|
|
1356
|
+
);
|
|
1267
1357
|
}
|
|
1268
1358
|
if (display.sender === "user") {
|
|
1269
1359
|
if (display.formData) {
|
|
@@ -1357,6 +1447,8 @@ function getProviderId(serviceType) {
|
|
|
1357
1447
|
return "gmail";
|
|
1358
1448
|
case "github":
|
|
1359
1449
|
return "github";
|
|
1450
|
+
case "zoho":
|
|
1451
|
+
return "zoho";
|
|
1360
1452
|
default:
|
|
1361
1453
|
return null;
|
|
1362
1454
|
}
|
|
@@ -1367,12 +1459,14 @@ function getServiceLabel(serviceType) {
|
|
|
1367
1459
|
return "Gmail";
|
|
1368
1460
|
case "github":
|
|
1369
1461
|
return "GitHub";
|
|
1462
|
+
case "zoho":
|
|
1463
|
+
return "Zoho";
|
|
1370
1464
|
default:
|
|
1371
1465
|
return serviceType;
|
|
1372
1466
|
}
|
|
1373
1467
|
}
|
|
1374
1468
|
function supportsSwitch(serviceType) {
|
|
1375
|
-
return serviceType === "gmail";
|
|
1469
|
+
return serviceType === "gmail" || serviceType === "zoho";
|
|
1376
1470
|
}
|
|
1377
1471
|
function ConnectionOnboarding({
|
|
1378
1472
|
connections,
|
|
@@ -2892,6 +2986,11 @@ function StatusDisplay({ status, error, strings }) {
|
|
|
2892
2986
|
/* @__PURE__ */ jsx("div", { className: `${widget_default.statusTitle} ${widget_default.statusError}`, children: t.statusErrorTitle }),
|
|
2893
2987
|
error && /* @__PURE__ */ jsx("div", { className: `${widget_default.statusLabel} ${widget_default.statusError}`, children: error })
|
|
2894
2988
|
] });
|
|
2989
|
+
case "awaiting_authorization":
|
|
2990
|
+
return /* @__PURE__ */ jsxs("div", { className: widget_default.statusWrapper, children: [
|
|
2991
|
+
/* @__PURE__ */ jsx("div", { className: widget_default.statusTitle, children: t.statusAuthorizationTitle }),
|
|
2992
|
+
/* @__PURE__ */ jsx("div", { className: widget_default.statusLabel, children: t.statusAuthorizationBody })
|
|
2993
|
+
] });
|
|
2895
2994
|
case "offline":
|
|
2896
2995
|
return /* @__PURE__ */ jsxs("div", { className: widget_default.statusWrapper, children: [
|
|
2897
2996
|
/* @__PURE__ */ jsx("div", { className: `${widget_default.statusTitle} ${widget_default.statusError}`, children: t.statusOfflineTitle }),
|
|
@@ -2913,6 +3012,7 @@ function StatusDisplay({ status, error, strings }) {
|
|
|
2913
3012
|
}
|
|
2914
3013
|
function VoiceOrb({
|
|
2915
3014
|
state,
|
|
3015
|
+
inputLevel = 0,
|
|
2916
3016
|
onPress,
|
|
2917
3017
|
onRelease,
|
|
2918
3018
|
disabled
|
|
@@ -2938,6 +3038,7 @@ function VoiceOrb({
|
|
|
2938
3038
|
},
|
|
2939
3039
|
[disabled, onRelease]
|
|
2940
3040
|
);
|
|
3041
|
+
const level = Math.min(1, Math.max(0, inputLevel));
|
|
2941
3042
|
return /* @__PURE__ */ jsxs(
|
|
2942
3043
|
"button",
|
|
2943
3044
|
{
|
|
@@ -2950,8 +3051,26 @@ function VoiceOrb({
|
|
|
2950
3051
|
disabled,
|
|
2951
3052
|
"aria-label": ariaForState(state),
|
|
2952
3053
|
children: [
|
|
2953
|
-
/* @__PURE__ */ jsx(
|
|
2954
|
-
|
|
3054
|
+
/* @__PURE__ */ jsx(
|
|
3055
|
+
"span",
|
|
3056
|
+
{
|
|
3057
|
+
className: widget_default.voiceOrbRing1,
|
|
3058
|
+
style: {
|
|
3059
|
+
opacity: 0.18 + level * 0.42,
|
|
3060
|
+
transform: `scale(${1.08 + level * 0.24})`
|
|
3061
|
+
}
|
|
3062
|
+
}
|
|
3063
|
+
),
|
|
3064
|
+
/* @__PURE__ */ jsx(
|
|
3065
|
+
"span",
|
|
3066
|
+
{
|
|
3067
|
+
className: widget_default.voiceOrbRing2,
|
|
3068
|
+
style: {
|
|
3069
|
+
opacity: 0.1 + level * 0.32,
|
|
3070
|
+
transform: `scale(${1.18 + level * 0.42})`
|
|
3071
|
+
}
|
|
3072
|
+
}
|
|
3073
|
+
),
|
|
2955
3074
|
/* @__PURE__ */ jsx("span", { className: widget_default.voiceOrbIcon, children: state === "listening" ? micIcon : state === "speaking" ? speakerIcon : micIcon })
|
|
2956
3075
|
]
|
|
2957
3076
|
}
|
|
@@ -4324,13 +4443,29 @@ function VoiceContinuousRenderer({
|
|
|
4324
4443
|
/* @__PURE__ */ jsx("button", { onClick: () => handleConfirmation(false), children: "No, retry" })
|
|
4325
4444
|
] }),
|
|
4326
4445
|
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbContainer, children: [
|
|
4327
|
-
/* @__PURE__ */
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4446
|
+
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbRow, children: [
|
|
4447
|
+
/* @__PURE__ */ jsx(
|
|
4448
|
+
VoiceOrb,
|
|
4449
|
+
{
|
|
4450
|
+
state: getOrbState(),
|
|
4451
|
+
disabled: disabled || voiceState === "processing" || transcribing
|
|
4452
|
+
}
|
|
4453
|
+
),
|
|
4454
|
+
/* @__PURE__ */ jsx(
|
|
4455
|
+
"button",
|
|
4456
|
+
{
|
|
4457
|
+
type: "button",
|
|
4458
|
+
"data-cf-voice-mute": "true",
|
|
4459
|
+
className: `${widget_default.voiceMuteButton}${isMuted ? " " + widget_default.voiceMuteButtonMuted : ""}`,
|
|
4460
|
+
onClick: handleMuteToggle,
|
|
4461
|
+
title: isMuted ? "Unmute microphone (Spacebar)" : "Mute microphone (Spacebar)",
|
|
4462
|
+
"aria-label": isMuted ? "Unmute microphone" : "Mute microphone",
|
|
4463
|
+
"aria-pressed": isMuted,
|
|
4464
|
+
"aria-keyshortcuts": "Space",
|
|
4465
|
+
children: isMuted ? "\u{1F507}" : "\u{1F3A4}"
|
|
4466
|
+
}
|
|
4467
|
+
)
|
|
4468
|
+
] }),
|
|
4334
4469
|
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbStats, children: [
|
|
4335
4470
|
/* @__PURE__ */ jsxs("span", { className: widget_default.voiceOrbHint, children: [
|
|
4336
4471
|
isMuted && "\u{1F507} Muted",
|
|
@@ -4353,21 +4488,7 @@ function VoiceContinuousRenderer({
|
|
|
4353
4488
|
Math.round(confidence * 100),
|
|
4354
4489
|
"% confident"
|
|
4355
4490
|
] })
|
|
4356
|
-
] })
|
|
4357
|
-
/* @__PURE__ */ jsx(
|
|
4358
|
-
"button",
|
|
4359
|
-
{
|
|
4360
|
-
type: "button",
|
|
4361
|
-
"data-cf-voice-mute": "true",
|
|
4362
|
-
className: `${widget_default.voiceMuteButton}${isMuted ? " " + widget_default.voiceMuteButtonMuted : ""}`,
|
|
4363
|
-
onClick: handleMuteToggle,
|
|
4364
|
-
title: isMuted ? "Unmute microphone (Spacebar)" : "Mute microphone (Spacebar)",
|
|
4365
|
-
"aria-label": isMuted ? "Unmute microphone" : "Mute microphone",
|
|
4366
|
-
"aria-pressed": isMuted,
|
|
4367
|
-
"aria-keyshortcuts": "Space",
|
|
4368
|
-
children: isMuted ? "\u{1F507}" : "\u{1F3A4}"
|
|
4369
|
-
}
|
|
4370
|
-
)
|
|
4491
|
+
] })
|
|
4371
4492
|
] }),
|
|
4372
4493
|
retryCount >= 3 && /* @__PURE__ */ jsx("a", { href: "#", className: widget_default.voiceTypeFallback, children: "Switch to typing mode" })
|
|
4373
4494
|
] });
|
|
@@ -4417,7 +4538,7 @@ class PcmCaptureProcessor extends AudioWorkletProcessor {
|
|
|
4417
4538
|
this._buf = new Int16Array(this.frameSamples);
|
|
4418
4539
|
this._pos = 0;
|
|
4419
4540
|
this._acc = 0;
|
|
4420
|
-
this.
|
|
4541
|
+
this._accWeight = 0;
|
|
4421
4542
|
}
|
|
4422
4543
|
|
|
4423
4544
|
_emit() {
|
|
@@ -4432,18 +4553,24 @@ class PcmCaptureProcessor extends AudioWorkletProcessor {
|
|
|
4432
4553
|
if (!input || !input[0]) return true;
|
|
4433
4554
|
const ch = input[0];
|
|
4434
4555
|
for (let i = 0; i < ch.length; i++) {
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4556
|
+
let sourceWeight = 1;
|
|
4557
|
+
while (sourceWeight > 0) {
|
|
4558
|
+
const weight = Math.min(sourceWeight, this.ratio - this._accWeight);
|
|
4559
|
+
this._acc += ch[i] * weight;
|
|
4560
|
+
this._accWeight += weight;
|
|
4561
|
+
sourceWeight -= weight;
|
|
4562
|
+
|
|
4563
|
+
if (this._accWeight + 1e-10 >= this.ratio) {
|
|
4564
|
+
const avg = this._acc / this._accWeight;
|
|
4565
|
+
// Clamp to [-1, 1] then quantize to int16.
|
|
4566
|
+
let s = avg;
|
|
4567
|
+
if (s > 1) s = 1;
|
|
4568
|
+
else if (s < -1) s = -1;
|
|
4569
|
+
this._buf[this._pos++] = s < 0 ? s * 0x8000 : s * 0x7fff;
|
|
4570
|
+
this._acc = 0;
|
|
4571
|
+
this._accWeight = 0;
|
|
4572
|
+
if (this._pos >= this.frameSamples) this._emit();
|
|
4573
|
+
}
|
|
4447
4574
|
}
|
|
4448
4575
|
}
|
|
4449
4576
|
return true;
|
|
@@ -4467,6 +4594,7 @@ var PCM_WORKLET_NAME = "cf-pcm-capture";
|
|
|
4467
4594
|
|
|
4468
4595
|
// src/hooks/useGeminiLiveSession.ts
|
|
4469
4596
|
var SUBPROTOCOL = "chatterfly.voice.v1";
|
|
4597
|
+
var MAX_RECONNECT_ATTEMPTS2 = 3;
|
|
4470
4598
|
function useGeminiLiveSession(cfg, onValue) {
|
|
4471
4599
|
const {
|
|
4472
4600
|
apiBase,
|
|
@@ -4479,26 +4607,72 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4479
4607
|
const [transcripts, setTranscripts] = useState([]);
|
|
4480
4608
|
const [assistantText, setAssistantText] = useState("");
|
|
4481
4609
|
const [activeFieldId, setActiveFieldId] = useState(null);
|
|
4610
|
+
const [askedFieldId, setAskedFieldId] = useState(null);
|
|
4611
|
+
const [playbackComplete, setPlaybackComplete] = useState(false);
|
|
4482
4612
|
const [lastValue, setLastValue] = useState(null);
|
|
4613
|
+
const [checkpoint, setCheckpoint] = useState(null);
|
|
4614
|
+
const [completion, setCompletion] = useState(null);
|
|
4615
|
+
const [inputLevel, setInputLevel] = useState(0);
|
|
4483
4616
|
const [isMuted, setIsMuted] = useState(false);
|
|
4484
4617
|
const [error, setError] = useState(null);
|
|
4618
|
+
const [errorRecoverable, setErrorRecoverable] = useState(false);
|
|
4485
4619
|
const wsRef = useRef(null);
|
|
4486
4620
|
const ctxRef = useRef(null);
|
|
4487
4621
|
const streamRef = useRef(null);
|
|
4488
4622
|
const workletRef = useRef(null);
|
|
4489
4623
|
const sourceRef = useRef(null);
|
|
4490
4624
|
const playbackQueueTimeRef = useRef(0);
|
|
4625
|
+
const playbackNodesRef = useRef(/* @__PURE__ */ new Set());
|
|
4491
4626
|
const isMutedRef = useRef(false);
|
|
4492
4627
|
const onValueRef = useRef(onValue);
|
|
4493
4628
|
const disposedRef = useRef(false);
|
|
4629
|
+
const smoothedInputLevelRef = useRef(0);
|
|
4630
|
+
const inputLevelFrameRef = useRef(0);
|
|
4631
|
+
const terminalReceivedRef = useRef(false);
|
|
4632
|
+
const currentPlaybackTurnRef = useRef(null);
|
|
4633
|
+
const playbackTurnsRef = useRef(/* @__PURE__ */ new Map());
|
|
4634
|
+
const retryAllowedRef = useRef(true);
|
|
4635
|
+
const maxHistorySequenceRef = useRef(0);
|
|
4636
|
+
const socketSessionIdRef = useRef(null);
|
|
4637
|
+
if (sessionId && socketSessionIdRef.current === null) {
|
|
4638
|
+
socketSessionIdRef.current = sessionId;
|
|
4639
|
+
}
|
|
4640
|
+
const socketSessionId = socketSessionIdRef.current;
|
|
4494
4641
|
useEffect(() => {
|
|
4495
4642
|
onValueRef.current = onValue;
|
|
4496
4643
|
}, [onValue]);
|
|
4497
4644
|
useEffect(() => {
|
|
4498
4645
|
isMutedRef.current = isMuted;
|
|
4499
4646
|
}, [isMuted]);
|
|
4647
|
+
const maybeCompletePlayback = useCallback(() => {
|
|
4648
|
+
if (terminalReceivedRef.current && playbackNodesRef.current.size === 0) {
|
|
4649
|
+
setPlaybackComplete(true);
|
|
4650
|
+
}
|
|
4651
|
+
}, []);
|
|
4652
|
+
const maybeFinishPlaybackTurn = useCallback(
|
|
4653
|
+
(turnId) => {
|
|
4654
|
+
const turn = playbackTurnsRef.current.get(turnId);
|
|
4655
|
+
if (!turn || !turn.serverEnded || turn.nodes.size > 0) return;
|
|
4656
|
+
playbackTurnsRef.current.delete(turnId);
|
|
4657
|
+
if (turn.purpose === "field" && turn.fieldId !== null) {
|
|
4658
|
+
setAskedFieldId(turn.fieldId);
|
|
4659
|
+
}
|
|
4660
|
+
const ws = wsRef.current;
|
|
4661
|
+
if ((ws == null ? void 0 : ws.readyState) === WebSocket.OPEN) {
|
|
4662
|
+
ws.send(
|
|
4663
|
+
JSON.stringify({
|
|
4664
|
+
type: "playback_turn_complete",
|
|
4665
|
+
turnId
|
|
4666
|
+
})
|
|
4667
|
+
);
|
|
4668
|
+
}
|
|
4669
|
+
maybeCompletePlayback();
|
|
4670
|
+
},
|
|
4671
|
+
[maybeCompletePlayback]
|
|
4672
|
+
);
|
|
4500
4673
|
const enqueuePlayback = useCallback(
|
|
4501
4674
|
(pcm) => {
|
|
4675
|
+
var _a;
|
|
4502
4676
|
const ctx = ctxRef.current;
|
|
4503
4677
|
if (!ctx) return;
|
|
4504
4678
|
const samples = new Int16Array(pcm);
|
|
@@ -4515,70 +4689,327 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4515
4689
|
const startAt = Math.max(now, playbackQueueTimeRef.current);
|
|
4516
4690
|
node.start(startAt);
|
|
4517
4691
|
playbackQueueTimeRef.current = startAt + buf.duration;
|
|
4692
|
+
const scheduled = playbackNodesRef.current;
|
|
4693
|
+
scheduled.add(node);
|
|
4694
|
+
const turnId = currentPlaybackTurnRef.current;
|
|
4695
|
+
if (turnId !== null) {
|
|
4696
|
+
(_a = playbackTurnsRef.current.get(turnId)) == null ? void 0 : _a.nodes.add(node);
|
|
4697
|
+
}
|
|
4698
|
+
node.onended = () => {
|
|
4699
|
+
var _a2;
|
|
4700
|
+
scheduled.delete(node);
|
|
4701
|
+
let turnEnded = false;
|
|
4702
|
+
if (turnId !== null) {
|
|
4703
|
+
const turn = playbackTurnsRef.current.get(turnId);
|
|
4704
|
+
turnEnded = (_a2 = turn == null ? void 0 : turn.serverEnded) != null ? _a2 : false;
|
|
4705
|
+
turn == null ? void 0 : turn.nodes.delete(node);
|
|
4706
|
+
maybeFinishPlaybackTurn(turnId);
|
|
4707
|
+
}
|
|
4708
|
+
if (scheduled.size === 0) {
|
|
4709
|
+
setState(
|
|
4710
|
+
(prev) => prev === "speaking" ? turnEnded ? "listening" : "processing" : prev
|
|
4711
|
+
);
|
|
4712
|
+
maybeCompletePlayback();
|
|
4713
|
+
}
|
|
4714
|
+
};
|
|
4715
|
+
setState(
|
|
4716
|
+
(prev) => prev === "listening" || prev === "processing" ? "speaking" : prev
|
|
4717
|
+
);
|
|
4518
4718
|
},
|
|
4519
|
-
[outputSampleRate]
|
|
4719
|
+
[maybeCompletePlayback, maybeFinishPlaybackTurn, outputSampleRate]
|
|
4520
4720
|
);
|
|
4521
|
-
const
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4721
|
+
const noteMicLevel = useCallback((pcm) => {
|
|
4722
|
+
const samples = new Int16Array(pcm);
|
|
4723
|
+
if (samples.length === 0) return;
|
|
4724
|
+
let sum = 0;
|
|
4725
|
+
for (let i = 0; i < samples.length; i++) sum += samples[i] * samples[i];
|
|
4726
|
+
const rms = Math.sqrt(sum / samples.length);
|
|
4727
|
+
const normalized = Math.min(1, Math.max(0, (rms - 80) / 2400));
|
|
4728
|
+
const previous = smoothedInputLevelRef.current;
|
|
4729
|
+
const smoothing = normalized > previous ? 0.45 : 0.16;
|
|
4730
|
+
const next = previous + (normalized - previous) * smoothing;
|
|
4731
|
+
smoothedInputLevelRef.current = next;
|
|
4732
|
+
inputLevelFrameRef.current += 1;
|
|
4733
|
+
if (inputLevelFrameRef.current % 3 === 0) setInputLevel(next);
|
|
4734
|
+
}, []);
|
|
4735
|
+
const flushPlayback = useCallback(() => {
|
|
4736
|
+
const ws = wsRef.current;
|
|
4737
|
+
if ((ws == null ? void 0 : ws.readyState) === WebSocket.OPEN) {
|
|
4738
|
+
for (const turnId of playbackTurnsRef.current.keys()) {
|
|
4739
|
+
ws.send(
|
|
4740
|
+
JSON.stringify({
|
|
4741
|
+
type: "playback_turn_cancelled",
|
|
4742
|
+
turnId
|
|
4743
|
+
})
|
|
4744
|
+
);
|
|
4530
4745
|
}
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4746
|
+
}
|
|
4747
|
+
const scheduled = playbackNodesRef.current;
|
|
4748
|
+
scheduled.forEach((node) => {
|
|
4749
|
+
try {
|
|
4750
|
+
node.onended = null;
|
|
4751
|
+
node.stop();
|
|
4752
|
+
node.disconnect();
|
|
4753
|
+
} catch {
|
|
4754
|
+
}
|
|
4755
|
+
});
|
|
4756
|
+
scheduled.clear();
|
|
4757
|
+
playbackTurnsRef.current.clear();
|
|
4758
|
+
currentPlaybackTurnRef.current = null;
|
|
4759
|
+
playbackQueueTimeRef.current = 0;
|
|
4760
|
+
}, []);
|
|
4761
|
+
const handleInbound = useCallback(
|
|
4762
|
+
(msg) => {
|
|
4763
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
4764
|
+
switch (msg.type) {
|
|
4765
|
+
case "state": {
|
|
4766
|
+
const next = String((_a = msg.state) != null ? _a : "");
|
|
4767
|
+
if (next === "listening" || next === "speaking" || next === "idle" || next === "connecting" || next === "finalizing") {
|
|
4768
|
+
setState(next);
|
|
4769
|
+
} else if (next === "processing") {
|
|
4770
|
+
setState("connecting");
|
|
4771
|
+
}
|
|
4772
|
+
break;
|
|
4773
|
+
}
|
|
4774
|
+
case "history": {
|
|
4775
|
+
if (!Array.isArray(msg.history)) break;
|
|
4776
|
+
const restored = msg.history.flatMap((item) => {
|
|
4777
|
+
if (typeof item !== "object" || item === null) return [];
|
|
4778
|
+
const who = item.who;
|
|
4779
|
+
const text = item.text;
|
|
4780
|
+
const sequence = item.sequence;
|
|
4781
|
+
if (who !== "user" && who !== "assistant" || typeof text !== "string" || text.length === 0) return [];
|
|
4782
|
+
return [{
|
|
4783
|
+
who,
|
|
4784
|
+
text,
|
|
4785
|
+
final: true,
|
|
4786
|
+
sequence: typeof sequence === "number" ? sequence : void 0
|
|
4787
|
+
}];
|
|
4788
|
+
});
|
|
4789
|
+
const maxSequence = restored.reduce(
|
|
4790
|
+
(max, item) => {
|
|
4791
|
+
var _a2;
|
|
4792
|
+
return Math.max(max, (_a2 = item.sequence) != null ? _a2 : 0);
|
|
4793
|
+
},
|
|
4794
|
+
0
|
|
4795
|
+
);
|
|
4796
|
+
if (maxSequence > 0 && maxSequence <= maxHistorySequenceRef.current) break;
|
|
4797
|
+
maxHistorySequenceRef.current = maxSequence;
|
|
4798
|
+
setTranscripts(restored);
|
|
4799
|
+
if (typeof msg.fieldId === "string" && msg.fieldId.length > 0) {
|
|
4800
|
+
setActiveFieldId(msg.fieldId);
|
|
4801
|
+
setAskedFieldId(msg.fieldId);
|
|
4802
|
+
}
|
|
4803
|
+
const lastAssistant = [...restored].reverse().find((item) => item.who === "assistant");
|
|
4804
|
+
if (lastAssistant) setAssistantText(lastAssistant.text);
|
|
4805
|
+
break;
|
|
4806
|
+
}
|
|
4807
|
+
case "checkpoint": {
|
|
4808
|
+
const sequence = Number(msg.sequence);
|
|
4809
|
+
const turn = Number(msg.turn);
|
|
4810
|
+
if (!Number.isSafeInteger(sequence) || sequence < 1) break;
|
|
4811
|
+
if (!Number.isSafeInteger(turn) || turn < 0) break;
|
|
4812
|
+
setCheckpoint(
|
|
4813
|
+
(previous) => previous && previous.sequence >= sequence ? previous : {
|
|
4814
|
+
sequence,
|
|
4815
|
+
turn,
|
|
4816
|
+
reconstructed: msg.reconstructed === true
|
|
4817
|
+
}
|
|
4818
|
+
);
|
|
4819
|
+
break;
|
|
4820
|
+
}
|
|
4821
|
+
case "completion": {
|
|
4822
|
+
const status = msg.status;
|
|
4823
|
+
if (status !== "finalizing" && status !== "completed") break;
|
|
4824
|
+
setCompletion({
|
|
4825
|
+
status,
|
|
4826
|
+
reason: typeof msg.completionReason === "string" && msg.completionReason.length > 0 ? msg.completionReason : null
|
|
4827
|
+
});
|
|
4828
|
+
setState(
|
|
4829
|
+
(previous) => previous === "error" || previous === "completed" ? previous : "finalizing"
|
|
4830
|
+
);
|
|
4831
|
+
break;
|
|
4832
|
+
}
|
|
4833
|
+
case "interrupt": {
|
|
4834
|
+
flushPlayback();
|
|
4835
|
+
setState("listening");
|
|
4836
|
+
break;
|
|
4837
|
+
}
|
|
4838
|
+
case "transcript": {
|
|
4839
|
+
const text = String((_b = msg.text) != null ? _b : "");
|
|
4840
|
+
const final = msg.partial !== true;
|
|
4841
|
+
if (text.length === 0) break;
|
|
4842
|
+
setState((prev) => prev === "listening" ? "processing" : prev);
|
|
4843
|
+
setTranscripts((prev) => {
|
|
4537
4844
|
const next = prev.slice();
|
|
4538
|
-
const
|
|
4539
|
-
if (
|
|
4540
|
-
next[
|
|
4845
|
+
const last = next[next.length - 1];
|
|
4846
|
+
if (last && last.who === "user" && !last.final) {
|
|
4847
|
+
next[next.length - 1] = {
|
|
4848
|
+
who: "user",
|
|
4849
|
+
text: last.text + text,
|
|
4850
|
+
final
|
|
4851
|
+
};
|
|
4541
4852
|
return next;
|
|
4542
4853
|
}
|
|
4543
|
-
return [...next, { who, text, final }];
|
|
4854
|
+
return [...next, { who: "user", text, final }];
|
|
4855
|
+
});
|
|
4856
|
+
break;
|
|
4857
|
+
}
|
|
4858
|
+
case "assistant_text": {
|
|
4859
|
+
const text = String((_c = msg.text) != null ? _c : "");
|
|
4860
|
+
if (text.length === 0) break;
|
|
4861
|
+
setAssistantText(text);
|
|
4862
|
+
setTranscripts((prev) => {
|
|
4863
|
+
const next = prev.slice();
|
|
4864
|
+
const last = next[next.length - 1];
|
|
4865
|
+
if (last && last.who === "assistant" && !last.final) {
|
|
4866
|
+
next[next.length - 1] = {
|
|
4867
|
+
who: "assistant",
|
|
4868
|
+
text: last.text + text,
|
|
4869
|
+
final: false
|
|
4870
|
+
};
|
|
4871
|
+
return next;
|
|
4872
|
+
}
|
|
4873
|
+
return [...next, { who: "assistant", text, final: false }];
|
|
4874
|
+
});
|
|
4875
|
+
break;
|
|
4876
|
+
}
|
|
4877
|
+
case "audio_turn_start": {
|
|
4878
|
+
const turnId = String((_d = msg.turnId) != null ? _d : "");
|
|
4879
|
+
if (turnId.length === 0) break;
|
|
4880
|
+
currentPlaybackTurnRef.current = turnId;
|
|
4881
|
+
playbackTurnsRef.current.set(turnId, {
|
|
4882
|
+
purpose: String((_e = msg.purpose) != null ? _e : ""),
|
|
4883
|
+
fieldId: typeof msg.fieldId === "string" && msg.fieldId.length > 0 ? msg.fieldId : null,
|
|
4884
|
+
nodes: /* @__PURE__ */ new Set(),
|
|
4885
|
+
serverEnded: false
|
|
4886
|
+
});
|
|
4887
|
+
setState(
|
|
4888
|
+
(prev) => prev === "speaking" || prev === "completed" || prev === "error" ? prev : "processing"
|
|
4889
|
+
);
|
|
4890
|
+
break;
|
|
4891
|
+
}
|
|
4892
|
+
case "audio_turn_end": {
|
|
4893
|
+
const turnId = String((_f = msg.turnId) != null ? _f : "");
|
|
4894
|
+
if (turnId.length === 0) break;
|
|
4895
|
+
const turn = playbackTurnsRef.current.get(turnId);
|
|
4896
|
+
if (turn) turn.serverEnded = true;
|
|
4897
|
+
if (currentPlaybackTurnRef.current === turnId) {
|
|
4898
|
+
currentPlaybackTurnRef.current = null;
|
|
4544
4899
|
}
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4900
|
+
setTranscripts((prev) => {
|
|
4901
|
+
const last = prev[prev.length - 1];
|
|
4902
|
+
if (!last || last.who !== "assistant" || last.final) return prev;
|
|
4903
|
+
const next = prev.slice();
|
|
4904
|
+
next[next.length - 1] = { ...last, final: true };
|
|
4905
|
+
return next;
|
|
4906
|
+
});
|
|
4907
|
+
maybeFinishPlaybackTurn(turnId);
|
|
4908
|
+
break;
|
|
4909
|
+
}
|
|
4910
|
+
case "field_change": {
|
|
4911
|
+
const fieldId = String((_g = msg.fieldId) != null ? _g : "");
|
|
4912
|
+
setActiveFieldId(fieldId);
|
|
4913
|
+
setState(
|
|
4914
|
+
(prev) => prev === "speaking" || prev === "error" ? prev : "processing"
|
|
4915
|
+
);
|
|
4916
|
+
break;
|
|
4917
|
+
}
|
|
4918
|
+
case "value": {
|
|
4919
|
+
const v = msg.value;
|
|
4920
|
+
setLastValue(v);
|
|
4921
|
+
setState(
|
|
4922
|
+
(prev) => prev === "speaking" || prev === "error" ? prev : "processing"
|
|
4923
|
+
);
|
|
4924
|
+
try {
|
|
4925
|
+
onValueRef.current(v);
|
|
4926
|
+
} catch {
|
|
4927
|
+
}
|
|
4928
|
+
break;
|
|
4929
|
+
}
|
|
4930
|
+
case "completed": {
|
|
4931
|
+
retryAllowedRef.current = false;
|
|
4932
|
+
terminalReceivedRef.current = true;
|
|
4933
|
+
setState("completed");
|
|
4934
|
+
maybeCompletePlayback();
|
|
4935
|
+
break;
|
|
4936
|
+
}
|
|
4937
|
+
case "error": {
|
|
4938
|
+
const recoverable = msg.fatal !== true;
|
|
4939
|
+
if (!recoverable) retryAllowedRef.current = false;
|
|
4940
|
+
setError(String((_h = msg.message) != null ? _h : "voice session error"));
|
|
4941
|
+
setErrorRecoverable(recoverable);
|
|
4942
|
+
setState("error");
|
|
4943
|
+
break;
|
|
4564
4944
|
}
|
|
4565
|
-
break;
|
|
4566
|
-
}
|
|
4567
|
-
case "completed": {
|
|
4568
|
-
setState("completed");
|
|
4569
|
-
break;
|
|
4570
|
-
}
|
|
4571
|
-
case "error": {
|
|
4572
|
-
setError(String((_e = msg.message) != null ? _e : "voice session error"));
|
|
4573
|
-
setState("error");
|
|
4574
|
-
break;
|
|
4575
4945
|
}
|
|
4576
|
-
}
|
|
4577
|
-
|
|
4946
|
+
},
|
|
4947
|
+
[flushPlayback, maybeCompletePlayback, maybeFinishPlaybackTurn]
|
|
4948
|
+
);
|
|
4578
4949
|
useEffect(() => {
|
|
4579
|
-
if (!
|
|
4950
|
+
if (!socketSessionId || !participantToken) return;
|
|
4580
4951
|
let cancelled = false;
|
|
4581
4952
|
disposedRef.current = false;
|
|
4953
|
+
retryAllowedRef.current = true;
|
|
4954
|
+
terminalReceivedRef.current = false;
|
|
4955
|
+
currentPlaybackTurnRef.current = null;
|
|
4956
|
+
playbackTurnsRef.current.clear();
|
|
4957
|
+
setPlaybackComplete(false);
|
|
4958
|
+
let reconnectAttempts = 0;
|
|
4959
|
+
let reconnectTimer = null;
|
|
4960
|
+
let stableConnectionTimer = null;
|
|
4961
|
+
const connectSocket = () => {
|
|
4962
|
+
if (cancelled || disposedRef.current) return;
|
|
4963
|
+
setState("connecting");
|
|
4964
|
+
const wsUrl = apiBase.replace(/^http/, "ws") + `/api/participant/inbox/${encodeURIComponent(socketSessionId)}/voice-stream?token=${encodeURIComponent(participantToken)}`;
|
|
4965
|
+
const ws = new WebSocket(wsUrl, SUBPROTOCOL);
|
|
4966
|
+
ws.binaryType = "arraybuffer";
|
|
4967
|
+
wsRef.current = ws;
|
|
4968
|
+
ws.onopen = () => {
|
|
4969
|
+
if (cancelled) return;
|
|
4970
|
+
setError(null);
|
|
4971
|
+
setErrorRecoverable(false);
|
|
4972
|
+
setState("listening");
|
|
4973
|
+
ws.send(
|
|
4974
|
+
JSON.stringify({
|
|
4975
|
+
type: "hello",
|
|
4976
|
+
input_sample_rate: inputSampleRate,
|
|
4977
|
+
output_sample_rate: outputSampleRate
|
|
4978
|
+
})
|
|
4979
|
+
);
|
|
4980
|
+
stableConnectionTimer = setTimeout(() => {
|
|
4981
|
+
reconnectAttempts = 0;
|
|
4982
|
+
}, 1e4);
|
|
4983
|
+
};
|
|
4984
|
+
ws.onmessage = (ev) => {
|
|
4985
|
+
if (typeof ev.data === "string") {
|
|
4986
|
+
try {
|
|
4987
|
+
handleInbound(JSON.parse(ev.data));
|
|
4988
|
+
} catch {
|
|
4989
|
+
}
|
|
4990
|
+
return;
|
|
4991
|
+
}
|
|
4992
|
+
if (ev.data instanceof ArrayBuffer) enqueuePlayback(ev.data);
|
|
4993
|
+
};
|
|
4994
|
+
ws.onerror = () => {
|
|
4995
|
+
};
|
|
4996
|
+
ws.onclose = () => {
|
|
4997
|
+
if (stableConnectionTimer !== null) {
|
|
4998
|
+
clearTimeout(stableConnectionTimer);
|
|
4999
|
+
stableConnectionTimer = null;
|
|
5000
|
+
}
|
|
5001
|
+
if (cancelled || disposedRef.current || !retryAllowedRef.current) return;
|
|
5002
|
+
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS2) {
|
|
5003
|
+
setError("voice connection lost");
|
|
5004
|
+
setState("error");
|
|
5005
|
+
return;
|
|
5006
|
+
}
|
|
5007
|
+
const delay = 500 * 2 ** reconnectAttempts;
|
|
5008
|
+
reconnectAttempts += 1;
|
|
5009
|
+
setState("connecting");
|
|
5010
|
+
reconnectTimer = setTimeout(connectSocket, delay);
|
|
5011
|
+
};
|
|
5012
|
+
};
|
|
4582
5013
|
const start = async () => {
|
|
4583
5014
|
try {
|
|
4584
5015
|
setState("connecting");
|
|
@@ -4610,47 +5041,14 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4610
5041
|
});
|
|
4611
5042
|
workletRef.current = worklet;
|
|
4612
5043
|
source.connect(worklet);
|
|
4613
|
-
|
|
4614
|
-
const ws = new WebSocket(wsUrl, SUBPROTOCOL);
|
|
4615
|
-
ws.binaryType = "arraybuffer";
|
|
4616
|
-
wsRef.current = ws;
|
|
4617
|
-
ws.onopen = () => {
|
|
4618
|
-
if (cancelled) return;
|
|
4619
|
-
setState("listening");
|
|
4620
|
-
ws.send(
|
|
4621
|
-
JSON.stringify({
|
|
4622
|
-
type: "hello",
|
|
4623
|
-
input_sample_rate: inputSampleRate,
|
|
4624
|
-
output_sample_rate: outputSampleRate
|
|
4625
|
-
})
|
|
4626
|
-
);
|
|
4627
|
-
};
|
|
4628
|
-
ws.onmessage = (ev) => {
|
|
4629
|
-
if (typeof ev.data === "string") {
|
|
4630
|
-
try {
|
|
4631
|
-
const msg = JSON.parse(ev.data);
|
|
4632
|
-
handleInbound(msg);
|
|
4633
|
-
} catch {
|
|
4634
|
-
}
|
|
4635
|
-
return;
|
|
4636
|
-
}
|
|
4637
|
-
if (ev.data instanceof ArrayBuffer) {
|
|
4638
|
-
enqueuePlayback(ev.data);
|
|
4639
|
-
}
|
|
4640
|
-
};
|
|
4641
|
-
ws.onerror = () => {
|
|
4642
|
-
if (disposedRef.current) return;
|
|
4643
|
-
setError("voice connection error");
|
|
4644
|
-
setState("error");
|
|
4645
|
-
};
|
|
4646
|
-
ws.onclose = () => {
|
|
4647
|
-
if (disposedRef.current) return;
|
|
4648
|
-
setState((prev) => prev === "completed" ? prev : "idle");
|
|
4649
|
-
};
|
|
5044
|
+
connectSocket();
|
|
4650
5045
|
worklet.port.onmessage = (ev) => {
|
|
4651
5046
|
if (isMutedRef.current) return;
|
|
4652
|
-
|
|
4653
|
-
ws.
|
|
5047
|
+
const ws = wsRef.current;
|
|
5048
|
+
if ((ws == null ? void 0 : ws.readyState) !== WebSocket.OPEN) return;
|
|
5049
|
+
const pcm = ev.data;
|
|
5050
|
+
noteMicLevel(pcm);
|
|
5051
|
+
ws.send(pcm);
|
|
4654
5052
|
};
|
|
4655
5053
|
} catch (err) {
|
|
4656
5054
|
if (cancelled) return;
|
|
@@ -4663,6 +5061,8 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4663
5061
|
var _a, _b, _c, _d, _e, _f;
|
|
4664
5062
|
cancelled = true;
|
|
4665
5063
|
disposedRef.current = true;
|
|
5064
|
+
if (reconnectTimer !== null) clearTimeout(reconnectTimer);
|
|
5065
|
+
if (stableConnectionTimer !== null) clearTimeout(stableConnectionTimer);
|
|
4666
5066
|
try {
|
|
4667
5067
|
(_a = wsRef.current) == null ? void 0 : _a.send(JSON.stringify({ type: "bye" }));
|
|
4668
5068
|
} catch {
|
|
@@ -4691,11 +5091,12 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4691
5091
|
}, [
|
|
4692
5092
|
apiBase,
|
|
4693
5093
|
participantToken,
|
|
4694
|
-
|
|
5094
|
+
socketSessionId,
|
|
4695
5095
|
inputSampleRate,
|
|
4696
5096
|
outputSampleRate,
|
|
4697
5097
|
enqueuePlayback,
|
|
4698
|
-
handleInbound
|
|
5098
|
+
handleInbound,
|
|
5099
|
+
noteMicLevel
|
|
4699
5100
|
]);
|
|
4700
5101
|
const toggleMute = useCallback(() => {
|
|
4701
5102
|
setIsMuted((prev) => {
|
|
@@ -4708,15 +5109,42 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4708
5109
|
return next;
|
|
4709
5110
|
});
|
|
4710
5111
|
}, []);
|
|
5112
|
+
const retryError = useCallback(() => {
|
|
5113
|
+
if (!errorRecoverable) return;
|
|
5114
|
+
setError(null);
|
|
5115
|
+
setErrorRecoverable(false);
|
|
5116
|
+
setState("listening");
|
|
5117
|
+
}, [errorRecoverable]);
|
|
5118
|
+
const requestEnd = useCallback(() => {
|
|
5119
|
+
const socket = wsRef.current;
|
|
5120
|
+
if (!socket || socket.readyState !== WebSocket.OPEN) {
|
|
5121
|
+
setError("Voice connection is not ready.");
|
|
5122
|
+
setErrorRecoverable(true);
|
|
5123
|
+
setState("error");
|
|
5124
|
+
return;
|
|
5125
|
+
}
|
|
5126
|
+
socket.send(JSON.stringify({ type: "end" }));
|
|
5127
|
+
setError(null);
|
|
5128
|
+
setErrorRecoverable(false);
|
|
5129
|
+
setState("finalizing");
|
|
5130
|
+
}, []);
|
|
4711
5131
|
return {
|
|
4712
5132
|
state,
|
|
4713
5133
|
transcripts,
|
|
4714
5134
|
assistantText,
|
|
4715
5135
|
activeFieldId,
|
|
5136
|
+
askedFieldId,
|
|
5137
|
+
playbackComplete,
|
|
4716
5138
|
lastValue,
|
|
5139
|
+
checkpoint,
|
|
5140
|
+
completion,
|
|
5141
|
+
inputLevel: isMuted ? 0 : inputLevel,
|
|
4717
5142
|
isMuted,
|
|
4718
5143
|
toggleMute,
|
|
4719
|
-
error
|
|
5144
|
+
error,
|
|
5145
|
+
errorRecoverable,
|
|
5146
|
+
retryError,
|
|
5147
|
+
requestEnd
|
|
4720
5148
|
};
|
|
4721
5149
|
}
|
|
4722
5150
|
function VoiceLiveRenderer({
|
|
@@ -4724,8 +5152,16 @@ function VoiceLiveRenderer({
|
|
|
4724
5152
|
apiBase,
|
|
4725
5153
|
participantToken,
|
|
4726
5154
|
sessionId,
|
|
5155
|
+
fieldSessionId,
|
|
4727
5156
|
liveConfig,
|
|
4728
5157
|
onValue,
|
|
5158
|
+
onPlaybackComplete,
|
|
5159
|
+
onFieldRevealed,
|
|
5160
|
+
timedOut = false,
|
|
5161
|
+
timeoutMessage = "This conversation timed out.",
|
|
5162
|
+
retryLabel = "Try again",
|
|
5163
|
+
userCanEnd = false,
|
|
5164
|
+
endLabel = "I'm done",
|
|
4729
5165
|
disabled
|
|
4730
5166
|
}) {
|
|
4731
5167
|
var _a;
|
|
@@ -4739,13 +5175,20 @@ function VoiceLiveRenderer({
|
|
|
4739
5175
|
},
|
|
4740
5176
|
onValue
|
|
4741
5177
|
);
|
|
5178
|
+
useEffect(() => {
|
|
5179
|
+
if (session.playbackComplete) onPlaybackComplete == null ? void 0 : onPlaybackComplete();
|
|
5180
|
+
}, [session.playbackComplete, onPlaybackComplete]);
|
|
4742
5181
|
useEffect(() => {
|
|
4743
5182
|
}, [session.activeFieldId]);
|
|
5183
|
+
const fieldRevealed = session.askedFieldId === field.id;
|
|
5184
|
+
useEffect(() => {
|
|
5185
|
+
if (fieldRevealed) onFieldRevealed == null ? void 0 : onFieldRevealed(fieldSessionId);
|
|
5186
|
+
}, [fieldRevealed, fieldSessionId, onFieldRevealed]);
|
|
4744
5187
|
const promptRef = useRef(null);
|
|
4745
5188
|
useEffect(() => {
|
|
4746
5189
|
var _a2;
|
|
4747
5190
|
(_a2 = promptRef.current) == null ? void 0 : _a2.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
|
4748
|
-
}, [field]);
|
|
5191
|
+
}, [field, fieldRevealed]);
|
|
4749
5192
|
const orbState = (() => {
|
|
4750
5193
|
if (session.error) return "error";
|
|
4751
5194
|
if (session.isMuted) return "idle";
|
|
@@ -4755,8 +5198,11 @@ function VoiceLiveRenderer({
|
|
|
4755
5198
|
return "idle";
|
|
4756
5199
|
case "listening":
|
|
4757
5200
|
return "listening";
|
|
4758
|
-
case "
|
|
5201
|
+
case "processing":
|
|
5202
|
+
case "finalizing":
|
|
4759
5203
|
return "processing";
|
|
5204
|
+
case "speaking":
|
|
5205
|
+
return "speaking";
|
|
4760
5206
|
case "completed":
|
|
4761
5207
|
return "idle";
|
|
4762
5208
|
case "error":
|
|
@@ -4765,12 +5211,22 @@ function VoiceLiveRenderer({
|
|
|
4765
5211
|
return "idle";
|
|
4766
5212
|
}
|
|
4767
5213
|
})();
|
|
4768
|
-
const
|
|
5214
|
+
const logRef = useRef(null);
|
|
5215
|
+
useEffect(() => {
|
|
5216
|
+
const el = logRef.current;
|
|
5217
|
+
if (el) el.scrollTop = el.scrollHeight;
|
|
5218
|
+
}, [session.transcripts]);
|
|
4769
5219
|
const announcement = (() => {
|
|
5220
|
+
var _a2;
|
|
5221
|
+
if (timedOut) return timeoutMessage;
|
|
4770
5222
|
if (session.error) return session.error;
|
|
4771
5223
|
if (session.isMuted) return "Microphone muted.";
|
|
4772
5224
|
if (session.state === "connecting") return "Connecting to voice service.";
|
|
4773
|
-
if (session.state === "listening") return "
|
|
5225
|
+
if (session.state === "listening") return "Microphone listening.";
|
|
5226
|
+
if (session.state === "processing") return "Preparing the next step.";
|
|
5227
|
+
if (session.state === "finalizing") {
|
|
5228
|
+
return ((_a2 = session.completion) == null ? void 0 : _a2.status) === "completed" ? "Conversation complete. Finishing playback." : "Finalizing the conversation.";
|
|
5229
|
+
}
|
|
4774
5230
|
if (session.state === "speaking") return "Assistant speaking.";
|
|
4775
5231
|
return "";
|
|
4776
5232
|
})();
|
|
@@ -4785,40 +5241,74 @@ function VoiceLiveRenderer({
|
|
|
4785
5241
|
children: announcement
|
|
4786
5242
|
}
|
|
4787
5243
|
),
|
|
4788
|
-
/* @__PURE__ */
|
|
4789
|
-
|
|
4790
|
-
|
|
4791
|
-
|
|
4792
|
-
|
|
4793
|
-
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
5244
|
+
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbContainer, children: [
|
|
5245
|
+
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbRow, children: [
|
|
5246
|
+
/* @__PURE__ */ jsx(
|
|
5247
|
+
VoiceOrb,
|
|
5248
|
+
{
|
|
5249
|
+
state: orbState,
|
|
5250
|
+
inputLevel: session.isMuted ? 0 : session.inputLevel,
|
|
5251
|
+
disabled
|
|
5252
|
+
}
|
|
5253
|
+
),
|
|
5254
|
+
/* @__PURE__ */ jsx(
|
|
5255
|
+
"button",
|
|
5256
|
+
{
|
|
5257
|
+
type: "button",
|
|
5258
|
+
"data-cf-voice-mute": "true",
|
|
5259
|
+
className: `${widget_default.voiceMuteButton}${session.isMuted ? " " + widget_default.voiceMuteButtonMuted : ""}`,
|
|
5260
|
+
onClick: session.toggleMute,
|
|
5261
|
+
"aria-label": session.isMuted ? "Unmute microphone" : "Mute microphone",
|
|
5262
|
+
"aria-pressed": session.isMuted,
|
|
5263
|
+
children: session.isMuted ? "Muted" : "Mute"
|
|
5264
|
+
}
|
|
5265
|
+
)
|
|
5266
|
+
] }),
|
|
5267
|
+
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbStats, children: [
|
|
5268
|
+
session.isMuted && /* @__PURE__ */ jsx("span", { className: widget_default.voiceOrbHint, "aria-hidden": "true", children: "Microphone muted" }),
|
|
5269
|
+
!session.isMuted && session.state === "connecting" && /* @__PURE__ */ jsx("span", { className: widget_default.voiceAssistantStatus, children: "Connecting\u2026" }),
|
|
5270
|
+
!session.isMuted && session.state === "processing" && /* @__PURE__ */ jsx("span", { className: widget_default.voiceAssistantStatus, children: "Preparing the next step\u2026" }),
|
|
5271
|
+
!session.isMuted && session.state === "finalizing" && /* @__PURE__ */ jsx("span", { className: widget_default.voiceAssistantStatus, children: ((_a = session.completion) == null ? void 0 : _a.status) === "completed" ? "Conversation complete \xB7 finishing playback\u2026" : "Finalizing\u2026" }),
|
|
5272
|
+
!session.isMuted && session.state === "speaking" && /* @__PURE__ */ jsx("span", { className: widget_default.voiceAssistantStatus, children: "Assistant speaking\u2026" }),
|
|
5273
|
+
!session.isMuted && session.state === "listening" && session.checkpoint && /* @__PURE__ */ jsxs("span", { className: widget_default.voiceOrbHint, children: [
|
|
5274
|
+
"Progress saved \xB7 turn ",
|
|
5275
|
+
session.checkpoint.turn
|
|
5276
|
+
] })
|
|
5277
|
+
] })
|
|
5278
|
+
] }),
|
|
5279
|
+
session.transcripts.length > 0 && /* @__PURE__ */ jsx("div", { ref: logRef, className: widget_default.voiceLiveLog, children: session.transcripts.map((t, i) => /* @__PURE__ */ jsxs(
|
|
4797
5280
|
"div",
|
|
4798
5281
|
{
|
|
4799
|
-
className:
|
|
5282
|
+
className: t.who === "user" ? widget_default.voiceLiveLogUser : widget_default.voiceLiveLogAssistant,
|
|
4800
5283
|
children: [
|
|
4801
|
-
/* @__PURE__ */ jsx(
|
|
4802
|
-
/* @__PURE__ */ jsx("
|
|
4803
|
-
session.isMuted && "\u{1F507} Muted",
|
|
4804
|
-
!session.isMuted && session.state === "connecting" && "Connecting\u2026",
|
|
4805
|
-
!session.isMuted && session.state === "listening" && "Listening\u2026",
|
|
4806
|
-
!session.isMuted && session.state === "speaking" && "Assistant speaking\u2026",
|
|
4807
|
-
!session.isMuted && session.state === "idle" && "Ready"
|
|
4808
|
-
] }) }),
|
|
4809
|
-
/* @__PURE__ */ jsx(
|
|
4810
|
-
"button",
|
|
4811
|
-
{
|
|
4812
|
-
type: "button",
|
|
4813
|
-
"data-cf-voice-mute": "true",
|
|
4814
|
-
className: `${widget_default.voiceMuteButton}${session.isMuted ? " " + widget_default.voiceMuteButtonMuted : ""}`,
|
|
4815
|
-
onClick: session.toggleMute,
|
|
4816
|
-
"aria-label": session.isMuted ? "Unmute microphone" : "Mute microphone",
|
|
4817
|
-
"aria-pressed": session.isMuted,
|
|
4818
|
-
children: session.isMuted ? "\u{1F507}" : "\u{1F3A4}"
|
|
4819
|
-
}
|
|
4820
|
-
)
|
|
5284
|
+
/* @__PURE__ */ jsx("span", { className: widget_default.voiceTranscriptLabel, children: t.who === "user" ? "You" : "Assistant" }),
|
|
5285
|
+
/* @__PURE__ */ jsx("span", { className: widget_default.voiceTranscriptText, children: t.text })
|
|
4821
5286
|
]
|
|
5287
|
+
},
|
|
5288
|
+
i
|
|
5289
|
+
)) }),
|
|
5290
|
+
fieldRevealed && /* @__PURE__ */ jsx("div", { ref: promptRef, children: /* @__PURE__ */ jsx(VoicePromptCard, { field }) }),
|
|
5291
|
+
timedOut && /* @__PURE__ */ jsx("p", { className: widget_default.voiceError, children: timeoutMessage }),
|
|
5292
|
+
!timedOut && session.error && /* @__PURE__ */ jsxs("div", { className: widget_default.voiceErrorGroup, children: [
|
|
5293
|
+
/* @__PURE__ */ jsx("p", { className: widget_default.voiceError, children: session.error }),
|
|
5294
|
+
session.errorRecoverable && /* @__PURE__ */ jsx(
|
|
5295
|
+
"button",
|
|
5296
|
+
{
|
|
5297
|
+
type: "button",
|
|
5298
|
+
className: widget_default.voiceRetryButton,
|
|
5299
|
+
onClick: session.retryError,
|
|
5300
|
+
children: retryLabel
|
|
5301
|
+
}
|
|
5302
|
+
)
|
|
5303
|
+
] }),
|
|
5304
|
+
userCanEnd && !timedOut && session.state !== "finalizing" && session.state !== "completed" && /* @__PURE__ */ jsx(
|
|
5305
|
+
"button",
|
|
5306
|
+
{
|
|
5307
|
+
type: "button",
|
|
5308
|
+
className: widget_default.converseDoneButton,
|
|
5309
|
+
onClick: session.requestEnd,
|
|
5310
|
+
disabled: disabled || session.state === "connecting",
|
|
5311
|
+
children: endLabel
|
|
4822
5312
|
}
|
|
4823
5313
|
)
|
|
4824
5314
|
] });
|
|
@@ -6019,7 +6509,7 @@ var ChatAdapter = forwardRef(
|
|
|
6019
6509
|
components,
|
|
6020
6510
|
strings
|
|
6021
6511
|
}, ref) {
|
|
6022
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
6512
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
|
|
6023
6513
|
const t = resolveStrings(strings);
|
|
6024
6514
|
const apiBaseResolved = apiBase;
|
|
6025
6515
|
const appBaseResolved = typeof window !== "undefined" ? window.location.origin : (_a = process.env.APP_URL) != null ? _a : "http://localhost:3000";
|
|
@@ -6046,6 +6536,8 @@ var ChatAdapter = forwardRef(
|
|
|
6046
6536
|
);
|
|
6047
6537
|
const {
|
|
6048
6538
|
status,
|
|
6539
|
+
failureKind,
|
|
6540
|
+
currentParticipantToken,
|
|
6049
6541
|
activeSession,
|
|
6050
6542
|
timeline,
|
|
6051
6543
|
submitHandback,
|
|
@@ -6065,12 +6557,6 @@ var ChatAdapter = forwardRef(
|
|
|
6065
6557
|
}),
|
|
6066
6558
|
[reconnect, disconnect, sendMessage]
|
|
6067
6559
|
);
|
|
6068
|
-
const prevStatusRef = useRef(null);
|
|
6069
|
-
useEffect(() => {
|
|
6070
|
-
if (prevStatusRef.current === status) return;
|
|
6071
|
-
prevStatusRef.current = status;
|
|
6072
|
-
onStatusChange == null ? void 0 : onStatusChange(status);
|
|
6073
|
-
}, [status, onStatusChange]);
|
|
6074
6560
|
const {
|
|
6075
6561
|
connections: requiredConnections,
|
|
6076
6562
|
allProvided: allConnectionsProvided,
|
|
@@ -6154,7 +6640,11 @@ var ChatAdapter = forwardRef(
|
|
|
6154
6640
|
if (!isVoiceSurface && activeSession && extractSurface(activeSession) === "widget/voice") {
|
|
6155
6641
|
setIsVoiceSurface(true);
|
|
6156
6642
|
}
|
|
6157
|
-
const
|
|
6643
|
+
const [isLiveVoiceSurface, setIsLiveVoiceSurface] = useState(false);
|
|
6644
|
+
if (!isLiveVoiceSurface && activeSession && extractSurface(activeSession) === "widget/voice" && ((_b = extractSurfaceConfig(activeSession)) == null ? void 0 : _b.style) === "live") {
|
|
6645
|
+
setIsLiveVoiceSurface(true);
|
|
6646
|
+
}
|
|
6647
|
+
const voiceSessionId = (_c = activeSession == null ? void 0 : activeSession.id) != null ? _c : null;
|
|
6158
6648
|
const prevVoiceSessionIdRef = useRef(null);
|
|
6159
6649
|
useEffect(() => {
|
|
6160
6650
|
if (!isVoiceSurface) return;
|
|
@@ -6164,6 +6654,25 @@ var ChatAdapter = forwardRef(
|
|
|
6164
6654
|
}
|
|
6165
6655
|
prevVoiceSessionIdRef.current = voiceSessionId;
|
|
6166
6656
|
}, [voiceSessionId, isVoiceSurface, clearSpeech]);
|
|
6657
|
+
const liveVoiceRef = useRef(null);
|
|
6658
|
+
const [revealedLivePromptSessions, setRevealedLivePromptSessions] = useState(
|
|
6659
|
+
() => /* @__PURE__ */ new Set()
|
|
6660
|
+
);
|
|
6661
|
+
const [livePlaybackComplete, setLivePlaybackComplete] = useState(false);
|
|
6662
|
+
const livePlaybackSessionRef = useRef(null);
|
|
6663
|
+
useEffect(() => {
|
|
6664
|
+
if (!activeSession || activeSession.id === livePlaybackSessionRef.current) return;
|
|
6665
|
+
livePlaybackSessionRef.current = activeSession.id;
|
|
6666
|
+
setLivePlaybackComplete(false);
|
|
6667
|
+
}, [activeSession]);
|
|
6668
|
+
const terminalVoiceDraining = (status === "completed" || status === "error") && isLiveVoiceSurface && liveVoiceRef.current !== null && !livePlaybackComplete;
|
|
6669
|
+
const reportedStatus = terminalVoiceDraining ? "running" : status;
|
|
6670
|
+
const prevStatusRef = useRef(null);
|
|
6671
|
+
useEffect(() => {
|
|
6672
|
+
if (prevStatusRef.current === reportedStatus) return;
|
|
6673
|
+
prevStatusRef.current = reportedStatus;
|
|
6674
|
+
onStatusChange == null ? void 0 : onStatusChange(reportedStatus);
|
|
6675
|
+
}, [reportedStatus, onStatusChange]);
|
|
6167
6676
|
useEffect(() => {
|
|
6168
6677
|
if (!isVoiceSurface) return;
|
|
6169
6678
|
for (const entry of timeline) {
|
|
@@ -6171,16 +6680,17 @@ var ChatAdapter = forwardRef(
|
|
|
6171
6680
|
if (entry.sender === "user") continue;
|
|
6172
6681
|
if (spokenMessageSeqsRef.current.has(entry.seq)) continue;
|
|
6173
6682
|
spokenMessageSeqsRef.current.add(entry.seq);
|
|
6683
|
+
if (isLiveVoiceSurface) continue;
|
|
6174
6684
|
const text = entry.payload.voice_hint || entry.payload.content || "";
|
|
6175
6685
|
void enqueueSpeak(text);
|
|
6176
6686
|
}
|
|
6177
|
-
}, [timeline, isVoiceSurface, enqueueSpeak]);
|
|
6687
|
+
}, [timeline, isVoiceSurface, isLiveVoiceSurface, enqueueSpeak]);
|
|
6178
6688
|
const completedFiredRef = useRef(false);
|
|
6179
6689
|
const errorFiredRef = useRef(false);
|
|
6180
6690
|
const expiredFiredRef = useRef(false);
|
|
6181
6691
|
const inputAreaRef = useRef(null);
|
|
6182
6692
|
const lastFocusedSessionRef = useRef(null);
|
|
6183
|
-
const activeSessionId = (
|
|
6693
|
+
const activeSessionId = (_d = activeSession == null ? void 0 : activeSession.id) != null ? _d : null;
|
|
6184
6694
|
useEffect(() => {
|
|
6185
6695
|
if (!autoFocusInput) return;
|
|
6186
6696
|
if (!activeSessionId) return;
|
|
@@ -6196,11 +6706,11 @@ var ChatAdapter = forwardRef(
|
|
|
6196
6706
|
}
|
|
6197
6707
|
}, [autoFocusInput, activeSessionId, status]);
|
|
6198
6708
|
useEffect(() => {
|
|
6199
|
-
if (status === "completed" && !completedFiredRef.current) {
|
|
6709
|
+
if (status === "completed" && !terminalVoiceDraining && !completedFiredRef.current) {
|
|
6200
6710
|
completedFiredRef.current = true;
|
|
6201
6711
|
onCompleted == null ? void 0 : onCompleted(output != null ? output : void 0);
|
|
6202
6712
|
}
|
|
6203
|
-
if (status === "error" && !errorFiredRef.current) {
|
|
6713
|
+
if (status === "error" && !terminalVoiceDraining && !errorFiredRef.current) {
|
|
6204
6714
|
errorFiredRef.current = true;
|
|
6205
6715
|
onError == null ? void 0 : onError(new WidgetError("run_failed", "The run encountered an error."));
|
|
6206
6716
|
}
|
|
@@ -6210,7 +6720,7 @@ var ChatAdapter = forwardRef(
|
|
|
6210
6720
|
new WidgetError("token_expired", "The participant token expired and could not be renewed.")
|
|
6211
6721
|
);
|
|
6212
6722
|
}
|
|
6213
|
-
}, [status, output, onCompleted, onError]);
|
|
6723
|
+
}, [status, terminalVoiceDraining, output, onCompleted, onError]);
|
|
6214
6724
|
const colorSchemeClass = colorScheme === "dark" ? widget_default.cfDark : colorScheme === "auto" ? widget_default.cfAuto : void 0;
|
|
6215
6725
|
const cls = [widget_default.widget, colorSchemeClass, className].filter(Boolean).join(" ");
|
|
6216
6726
|
const rootStyle = useMemo(() => themeToStyle(theme), [theme]);
|
|
@@ -6270,7 +6780,8 @@ var ChatAdapter = forwardRef(
|
|
|
6270
6780
|
/* @__PURE__ */ jsx(ChatTimeline, { entries: timeline, streamingText, components })
|
|
6271
6781
|
] });
|
|
6272
6782
|
}
|
|
6273
|
-
|
|
6783
|
+
const awaitingLivePlayback = terminalVoiceDraining;
|
|
6784
|
+
if ((status === "completed" || status === "error") && !awaitingLivePlayback) {
|
|
6274
6785
|
const hasChatContent = timeline.some(
|
|
6275
6786
|
(e) => {
|
|
6276
6787
|
var _a2, _b2;
|
|
@@ -6280,7 +6791,7 @@ var ChatAdapter = forwardRef(
|
|
|
6280
6791
|
return /* @__PURE__ */ jsxs("div", { className: cls, style: rootStyle, children: [
|
|
6281
6792
|
hasChatContent && /* @__PURE__ */ jsx(ChatTimeline, { entries: timeline, streamingText, components }),
|
|
6282
6793
|
status === "completed" && /* @__PURE__ */ jsx("div", { className: widget_default.terminalCompleted, children: completedState != null ? completedState : t.conversationCompleted }),
|
|
6283
|
-
status === "error" && /* @__PURE__ */ jsx("div", { className: widget_default.terminalError, children: errorState != null ? errorState : t.somethingWentWrong })
|
|
6794
|
+
status === "error" && /* @__PURE__ */ jsx("div", { className: widget_default.terminalError, children: errorState != null ? errorState : failureKind === "timeout" ? t.conversationTimedOut : t.somethingWentWrong })
|
|
6284
6795
|
] });
|
|
6285
6796
|
}
|
|
6286
6797
|
if (status === "awaiting_form" && activeSession) {
|
|
@@ -6290,7 +6801,7 @@ var ChatAdapter = forwardRef(
|
|
|
6290
6801
|
FormRenderer,
|
|
6291
6802
|
{
|
|
6292
6803
|
fields: formSpec == null ? void 0 : formSpec.fields,
|
|
6293
|
-
repeater: (
|
|
6804
|
+
repeater: (_e = formSpec == null ? void 0 : formSpec.repeaters) == null ? void 0 : _e[0],
|
|
6294
6805
|
onSubmit: (values) => void submitHandback(values),
|
|
6295
6806
|
disabled: submitting
|
|
6296
6807
|
}
|
|
@@ -6322,11 +6833,12 @@ var ChatAdapter = forwardRef(
|
|
|
6322
6833
|
const converseCtx = activeSession == null ? void 0 : activeSession.entry_context;
|
|
6323
6834
|
const isConverseSession = (converseCtx == null ? void 0 : converseCtx.converse_session) === true;
|
|
6324
6835
|
const converseUserCanEnd = (converseCtx == null ? void 0 : converseCtx.converse_user_can_end) === true;
|
|
6836
|
+
const converseRuntime = (_f = converseCtx == null ? void 0 : converseCtx.converse_runtime) != null ? _f : "turn_based";
|
|
6325
6837
|
const field = activeSession ? extractField(activeSession) : null;
|
|
6326
6838
|
if (status === "awaiting_chat" && activeSession && !(isConverseSession && field)) {
|
|
6327
|
-
const hbCtx = (
|
|
6839
|
+
const hbCtx = (_g = activeSession.entry_context) == null ? void 0 : _g.handback;
|
|
6328
6840
|
const outputSchema = hbCtx == null ? void 0 : hbCtx.output_schema;
|
|
6329
|
-
const requireOutput = (
|
|
6841
|
+
const requireOutput = (_h = hbCtx == null ? void 0 : hbCtx.require_output) != null ? _h : false;
|
|
6330
6842
|
const handbackReady = !requireOutput || outputSchema != null && Object.keys(outputSchema).every((k) => {
|
|
6331
6843
|
var _a2;
|
|
6332
6844
|
return ((_a2 = handbackValues[k]) != null ? _a2 : "").trim() !== "";
|
|
@@ -6427,31 +6939,68 @@ var ChatAdapter = forwardRef(
|
|
|
6427
6939
|
const shouldUseVoice = effectiveVoiceMode !== "off";
|
|
6428
6940
|
const voiceRenderer = effectiveVoiceMode === "push_to_talk" ? "click" : "continuous";
|
|
6429
6941
|
const isLiveVoice = surface === "widget/voice" && (surfaceConfig == null ? void 0 : surfaceConfig.style) === "live";
|
|
6430
|
-
if (
|
|
6431
|
-
|
|
6432
|
-
|
|
6433
|
-
|
|
6434
|
-
|
|
6435
|
-
|
|
6436
|
-
|
|
6437
|
-
|
|
6942
|
+
if (activeSession && !isLiveVoice) {
|
|
6943
|
+
liveVoiceRef.current = null;
|
|
6944
|
+
} else if (isLiveVoice && activeSession && field) {
|
|
6945
|
+
liveVoiceRef.current = {
|
|
6946
|
+
// Pin the FIRST id: it only picks which socket to open, and the proxy
|
|
6947
|
+
// tracks field transitions itself from then on.
|
|
6948
|
+
sessionId: (_j = (_i = liveVoiceRef.current) == null ? void 0 : _i.sessionId) != null ? _j : activeSession.id,
|
|
6949
|
+
fieldSessionId: activeSession.id,
|
|
6950
|
+
field,
|
|
6951
|
+
hint: (_l = nodeVoice == null ? void 0 : nodeVoice.hint) != null ? _l : (_k = field.voice) == null ? void 0 : _k.hint,
|
|
6952
|
+
config: surfaceConfig != null ? surfaceConfig : void 0
|
|
6438
6953
|
};
|
|
6954
|
+
}
|
|
6955
|
+
const liveVoice = isLiveVoice && activeSession && field || !activeSession && status === "running" || awaitingLivePlayback ? liveVoiceRef.current : null;
|
|
6956
|
+
if (liveVoice) {
|
|
6957
|
+
const respondedSessionIds = new Set(
|
|
6958
|
+
timeline.filter((entry) => entry.event_type === "response" && entry.session_id).map((entry) => entry.session_id)
|
|
6959
|
+
);
|
|
6960
|
+
const visibleLiveTimeline = timeline.filter(
|
|
6961
|
+
(entry) => entry.event_type !== "prompt" || entry.session_id === null || respondedSessionIds.has(entry.session_id) || revealedLivePromptSessions.has(entry.session_id)
|
|
6962
|
+
);
|
|
6439
6963
|
return /* @__PURE__ */ jsxs("div", { className: cls, style: rootStyle, children: [
|
|
6440
6964
|
header,
|
|
6441
|
-
/* @__PURE__ */ jsx(ChatTimeline, { entries:
|
|
6442
|
-
/* @__PURE__ */ jsx("div", { className: widget_default.activeInputArea
|
|
6965
|
+
/* @__PURE__ */ jsx(ChatTimeline, { entries: visibleLiveTimeline, showTyping, streamingText, components }),
|
|
6966
|
+
/* @__PURE__ */ jsx("div", { className: `${widget_default.activeInputArea} ${widget_default.activeInputAreaVoice}`, children: /* @__PURE__ */ jsx(
|
|
6443
6967
|
VoiceLiveRenderer,
|
|
6444
6968
|
{
|
|
6445
|
-
field,
|
|
6446
|
-
voiceHint:
|
|
6969
|
+
field: liveVoice.field,
|
|
6970
|
+
voiceHint: liveVoice.hint,
|
|
6447
6971
|
apiBase: apiBaseResolved,
|
|
6448
|
-
participantToken,
|
|
6449
|
-
sessionId:
|
|
6450
|
-
|
|
6451
|
-
|
|
6972
|
+
participantToken: currentParticipantToken,
|
|
6973
|
+
sessionId: liveVoice.sessionId,
|
|
6974
|
+
fieldSessionId: liveVoice.fieldSessionId,
|
|
6975
|
+
liveConfig: liveVoice.config,
|
|
6976
|
+
onValue: (value) => setInputValue(value),
|
|
6977
|
+
onPlaybackComplete: () => setLivePlaybackComplete(true),
|
|
6978
|
+
timedOut: failureKind === "timeout",
|
|
6979
|
+
timeoutMessage: t.conversationTimedOut,
|
|
6980
|
+
retryLabel: t.tryAgain,
|
|
6981
|
+
userCanEnd: converseUserCanEnd,
|
|
6982
|
+
endLabel: t.converseDoneButton,
|
|
6983
|
+
onFieldRevealed: (revealedSessionId) => setRevealedLivePromptSessions((prev) => {
|
|
6984
|
+
if (prev.has(revealedSessionId)) return prev;
|
|
6985
|
+
const next = new Set(prev);
|
|
6986
|
+
next.add(revealedSessionId);
|
|
6987
|
+
return next;
|
|
6988
|
+
}),
|
|
6452
6989
|
disabled: submitting
|
|
6453
6990
|
}
|
|
6454
|
-
)
|
|
6991
|
+
) })
|
|
6992
|
+
] });
|
|
6993
|
+
}
|
|
6994
|
+
if (shouldUseVoice && activeSession && field) {
|
|
6995
|
+
const spokenHint = (_n = nodeVoice == null ? void 0 : nodeVoice.hint) != null ? _n : (_m = field.voice) == null ? void 0 : _m.hint;
|
|
6996
|
+
const handleVoiceValue = (value) => {
|
|
6997
|
+
setInputValue(value);
|
|
6998
|
+
void submitHandback(value, crypto.randomUUID());
|
|
6999
|
+
};
|
|
7000
|
+
return /* @__PURE__ */ jsxs("div", { className: cls, style: rootStyle, children: [
|
|
7001
|
+
header,
|
|
7002
|
+
/* @__PURE__ */ jsx(ChatTimeline, { entries: timeline, showTyping, streamingText, components }),
|
|
7003
|
+
/* @__PURE__ */ jsx("div", { className: `${widget_default.activeInputArea} ${widget_default.activeInputAreaVoice}`, children: voiceRenderer === "continuous" ? /* @__PURE__ */ jsx(
|
|
6455
7004
|
VoiceContinuousRenderer,
|
|
6456
7005
|
{
|
|
6457
7006
|
field,
|
|
@@ -6460,7 +7009,7 @@ var ChatAdapter = forwardRef(
|
|
|
6460
7009
|
participantToken,
|
|
6461
7010
|
onValue: handleVoiceValue,
|
|
6462
7011
|
autoSubmitMode: "confirm",
|
|
6463
|
-
confidenceThreshold: (
|
|
7012
|
+
confidenceThreshold: (_o = nodeVoice == null ? void 0 : nodeVoice.confidence_threshold) != null ? _o : typeof (surfaceConfig == null ? void 0 : surfaceConfig.confidence_threshold) === "number" ? surfaceConfig.confidence_threshold : 0.7,
|
|
6464
7013
|
enqueueSpeak,
|
|
6465
7014
|
isSpeaking,
|
|
6466
7015
|
disabled: submitting
|
|
@@ -6502,7 +7051,7 @@ var ChatAdapter = forwardRef(
|
|
|
6502
7051
|
activeSession && field && InputComponent && /* @__PURE__ */ jsxs("form", { ref: inputAreaRef, className: widget_default.activeInputArea, onSubmit: handleSubmit, children: [
|
|
6503
7052
|
isConverseSession && /* @__PURE__ */ jsxs("div", { className: widget_default.converseHint, children: [
|
|
6504
7053
|
/* @__PURE__ */ jsx("span", { className: widget_default.converseHintDot }),
|
|
6505
|
-
"Collecting your answers\u2026"
|
|
7054
|
+
converseRuntime === "live" ? "Live conversation in progress\u2026" : "Collecting your answers\u2026"
|
|
6506
7055
|
] }),
|
|
6507
7056
|
field.prompt && /* @__PURE__ */ jsx("div", { className: widget_default.activeInputPrompt, children: field.prompt }),
|
|
6508
7057
|
/* @__PURE__ */ jsxs("div", { className: widget_default.activeInputRow, children: [
|