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