@chatterfly/widget 0.4.0 → 0.5.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 +33 -27
- package/dist/embed.global.js.map +1 -1
- package/dist/index.css +50 -15
- package/dist/index.css.map +1 -1
- package/dist/index.d.mts +27 -3
- package/dist/index.d.ts +27 -3
- package/dist/index.js +465 -170
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +466 -171
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
|
@@ -370,6 +370,9 @@ function useRunStream(config, onEvent) {
|
|
|
370
370
|
]);
|
|
371
371
|
break;
|
|
372
372
|
}
|
|
373
|
+
case "run_complete":
|
|
374
|
+
case "run_error":
|
|
375
|
+
break;
|
|
373
376
|
case "run.completed":
|
|
374
377
|
case "run.cancelled":
|
|
375
378
|
setStreamingText("");
|
|
@@ -676,6 +679,7 @@ var widget_default = {
|
|
|
676
679
|
formKvValue: "widget_formKvValue",
|
|
677
680
|
formKvNested: "widget_formKvNested",
|
|
678
681
|
activeInputArea: "widget_activeInputArea",
|
|
682
|
+
activeInputAreaVoice: "widget_activeInputAreaVoice",
|
|
679
683
|
formTimelineWrapper: "widget_formTimelineWrapper",
|
|
680
684
|
formScrollArea: "widget_formScrollArea",
|
|
681
685
|
activeInputPrompt: "widget_activeInputPrompt",
|
|
@@ -723,13 +727,17 @@ var widget_default = {
|
|
|
723
727
|
voiceRatingMatched: "widget_voiceRatingMatched",
|
|
724
728
|
voiceRatingLabels: "widget_voiceRatingLabels",
|
|
725
729
|
voiceTranscript: "widget_voiceTranscript",
|
|
726
|
-
|
|
730
|
+
voiceLiveLog: "widget_voiceLiveLog",
|
|
731
|
+
voiceLiveLogUser: "widget_voiceLiveLogUser",
|
|
732
|
+
voiceLiveLogAssistant: "widget_voiceLiveLogAssistant",
|
|
727
733
|
voiceTranscriptText: "widget_voiceTranscriptText",
|
|
734
|
+
voiceTranscriptLabel: "widget_voiceTranscriptLabel",
|
|
728
735
|
voiceError: "widget_voiceError",
|
|
729
736
|
cfVoiceShake: "widget_cfVoiceShake",
|
|
730
737
|
voiceOrbContainer: "widget_voiceOrbContainer",
|
|
731
|
-
|
|
738
|
+
voiceOrbRow: "widget_voiceOrbRow",
|
|
732
739
|
voiceOrbHint: "widget_voiceOrbHint",
|
|
740
|
+
voiceAssistantStatus: "widget_voiceAssistantStatus",
|
|
733
741
|
voiceOrb: "widget_voiceOrb",
|
|
734
742
|
cfVoiceBreathe: "widget_cfVoiceBreathe",
|
|
735
743
|
voiceOrbDisabled: "widget_voiceOrbDisabled",
|
|
@@ -738,12 +746,12 @@ var widget_default = {
|
|
|
738
746
|
voiceOrbRing2: "widget_voiceOrbRing2",
|
|
739
747
|
voiceOrb_idle: "widget_voiceOrb_idle",
|
|
740
748
|
voiceOrb_listening: "widget_voiceOrb_listening",
|
|
741
|
-
cfVoicePulse: "widget_cfVoicePulse",
|
|
742
749
|
voiceOrb_speaking: "widget_voiceOrb_speaking",
|
|
743
|
-
cfVoiceWave: "widget_cfVoiceWave",
|
|
744
750
|
voiceOrb_processing: "widget_voiceOrb_processing",
|
|
745
751
|
voiceOrb_confirming: "widget_voiceOrb_confirming",
|
|
746
752
|
voiceOrb_error: "widget_voiceOrb_error",
|
|
753
|
+
cfVoicePulse: "widget_cfVoicePulse",
|
|
754
|
+
cfVoiceWave: "widget_cfVoiceWave",
|
|
747
755
|
voiceAudioLevelBar: "widget_voiceAudioLevelBar",
|
|
748
756
|
voiceMuteButton: "widget_voiceMuteButton",
|
|
749
757
|
voiceConfirmation: "widget_voiceConfirmation",
|
|
@@ -2913,6 +2921,7 @@ function StatusDisplay({ status, error, strings }) {
|
|
|
2913
2921
|
}
|
|
2914
2922
|
function VoiceOrb({
|
|
2915
2923
|
state,
|
|
2924
|
+
inputLevel = 0,
|
|
2916
2925
|
onPress,
|
|
2917
2926
|
onRelease,
|
|
2918
2927
|
disabled
|
|
@@ -2938,6 +2947,7 @@ function VoiceOrb({
|
|
|
2938
2947
|
},
|
|
2939
2948
|
[disabled, onRelease]
|
|
2940
2949
|
);
|
|
2950
|
+
const level = Math.min(1, Math.max(0, inputLevel));
|
|
2941
2951
|
return /* @__PURE__ */ jsxs(
|
|
2942
2952
|
"button",
|
|
2943
2953
|
{
|
|
@@ -2950,8 +2960,26 @@ function VoiceOrb({
|
|
|
2950
2960
|
disabled,
|
|
2951
2961
|
"aria-label": ariaForState(state),
|
|
2952
2962
|
children: [
|
|
2953
|
-
/* @__PURE__ */ jsx(
|
|
2954
|
-
|
|
2963
|
+
/* @__PURE__ */ jsx(
|
|
2964
|
+
"span",
|
|
2965
|
+
{
|
|
2966
|
+
className: widget_default.voiceOrbRing1,
|
|
2967
|
+
style: {
|
|
2968
|
+
opacity: 0.18 + level * 0.42,
|
|
2969
|
+
transform: `scale(${1.08 + level * 0.24})`
|
|
2970
|
+
}
|
|
2971
|
+
}
|
|
2972
|
+
),
|
|
2973
|
+
/* @__PURE__ */ jsx(
|
|
2974
|
+
"span",
|
|
2975
|
+
{
|
|
2976
|
+
className: widget_default.voiceOrbRing2,
|
|
2977
|
+
style: {
|
|
2978
|
+
opacity: 0.1 + level * 0.32,
|
|
2979
|
+
transform: `scale(${1.18 + level * 0.42})`
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
),
|
|
2955
2983
|
/* @__PURE__ */ jsx("span", { className: widget_default.voiceOrbIcon, children: state === "listening" ? micIcon : state === "speaking" ? speakerIcon : micIcon })
|
|
2956
2984
|
]
|
|
2957
2985
|
}
|
|
@@ -4324,13 +4352,29 @@ function VoiceContinuousRenderer({
|
|
|
4324
4352
|
/* @__PURE__ */ jsx("button", { onClick: () => handleConfirmation(false), children: "No, retry" })
|
|
4325
4353
|
] }),
|
|
4326
4354
|
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbContainer, children: [
|
|
4327
|
-
/* @__PURE__ */
|
|
4328
|
-
|
|
4329
|
-
|
|
4330
|
-
|
|
4331
|
-
|
|
4332
|
-
|
|
4333
|
-
|
|
4355
|
+
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbRow, children: [
|
|
4356
|
+
/* @__PURE__ */ jsx(
|
|
4357
|
+
VoiceOrb,
|
|
4358
|
+
{
|
|
4359
|
+
state: getOrbState(),
|
|
4360
|
+
disabled: disabled || voiceState === "processing" || transcribing
|
|
4361
|
+
}
|
|
4362
|
+
),
|
|
4363
|
+
/* @__PURE__ */ jsx(
|
|
4364
|
+
"button",
|
|
4365
|
+
{
|
|
4366
|
+
type: "button",
|
|
4367
|
+
"data-cf-voice-mute": "true",
|
|
4368
|
+
className: `${widget_default.voiceMuteButton}${isMuted ? " " + widget_default.voiceMuteButtonMuted : ""}`,
|
|
4369
|
+
onClick: handleMuteToggle,
|
|
4370
|
+
title: isMuted ? "Unmute microphone (Spacebar)" : "Mute microphone (Spacebar)",
|
|
4371
|
+
"aria-label": isMuted ? "Unmute microphone" : "Mute microphone",
|
|
4372
|
+
"aria-pressed": isMuted,
|
|
4373
|
+
"aria-keyshortcuts": "Space",
|
|
4374
|
+
children: isMuted ? "\u{1F507}" : "\u{1F3A4}"
|
|
4375
|
+
}
|
|
4376
|
+
)
|
|
4377
|
+
] }),
|
|
4334
4378
|
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbStats, children: [
|
|
4335
4379
|
/* @__PURE__ */ jsxs("span", { className: widget_default.voiceOrbHint, children: [
|
|
4336
4380
|
isMuted && "\u{1F507} Muted",
|
|
@@ -4353,21 +4397,7 @@ function VoiceContinuousRenderer({
|
|
|
4353
4397
|
Math.round(confidence * 100),
|
|
4354
4398
|
"% confident"
|
|
4355
4399
|
] })
|
|
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
|
-
)
|
|
4400
|
+
] })
|
|
4371
4401
|
] }),
|
|
4372
4402
|
retryCount >= 3 && /* @__PURE__ */ jsx("a", { href: "#", className: widget_default.voiceTypeFallback, children: "Switch to typing mode" })
|
|
4373
4403
|
] });
|
|
@@ -4417,7 +4447,7 @@ class PcmCaptureProcessor extends AudioWorkletProcessor {
|
|
|
4417
4447
|
this._buf = new Int16Array(this.frameSamples);
|
|
4418
4448
|
this._pos = 0;
|
|
4419
4449
|
this._acc = 0;
|
|
4420
|
-
this.
|
|
4450
|
+
this._accWeight = 0;
|
|
4421
4451
|
}
|
|
4422
4452
|
|
|
4423
4453
|
_emit() {
|
|
@@ -4432,18 +4462,24 @@ class PcmCaptureProcessor extends AudioWorkletProcessor {
|
|
|
4432
4462
|
if (!input || !input[0]) return true;
|
|
4433
4463
|
const ch = input[0];
|
|
4434
4464
|
for (let i = 0; i < ch.length; i++) {
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
4441
|
-
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4465
|
+
let sourceWeight = 1;
|
|
4466
|
+
while (sourceWeight > 0) {
|
|
4467
|
+
const weight = Math.min(sourceWeight, this.ratio - this._accWeight);
|
|
4468
|
+
this._acc += ch[i] * weight;
|
|
4469
|
+
this._accWeight += weight;
|
|
4470
|
+
sourceWeight -= weight;
|
|
4471
|
+
|
|
4472
|
+
if (this._accWeight + 1e-10 >= this.ratio) {
|
|
4473
|
+
const avg = this._acc / this._accWeight;
|
|
4474
|
+
// Clamp to [-1, 1] then quantize to int16.
|
|
4475
|
+
let s = avg;
|
|
4476
|
+
if (s > 1) s = 1;
|
|
4477
|
+
else if (s < -1) s = -1;
|
|
4478
|
+
this._buf[this._pos++] = s < 0 ? s * 0x8000 : s * 0x7fff;
|
|
4479
|
+
this._acc = 0;
|
|
4480
|
+
this._accWeight = 0;
|
|
4481
|
+
if (this._pos >= this.frameSamples) this._emit();
|
|
4482
|
+
}
|
|
4447
4483
|
}
|
|
4448
4484
|
}
|
|
4449
4485
|
return true;
|
|
@@ -4479,7 +4515,10 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4479
4515
|
const [transcripts, setTranscripts] = useState([]);
|
|
4480
4516
|
const [assistantText, setAssistantText] = useState("");
|
|
4481
4517
|
const [activeFieldId, setActiveFieldId] = useState(null);
|
|
4518
|
+
const [askedFieldId, setAskedFieldId] = useState(null);
|
|
4519
|
+
const [playbackComplete, setPlaybackComplete] = useState(false);
|
|
4482
4520
|
const [lastValue, setLastValue] = useState(null);
|
|
4521
|
+
const [inputLevel, setInputLevel] = useState(0);
|
|
4483
4522
|
const [isMuted, setIsMuted] = useState(false);
|
|
4484
4523
|
const [error, setError] = useState(null);
|
|
4485
4524
|
const wsRef = useRef(null);
|
|
@@ -4488,17 +4527,55 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4488
4527
|
const workletRef = useRef(null);
|
|
4489
4528
|
const sourceRef = useRef(null);
|
|
4490
4529
|
const playbackQueueTimeRef = useRef(0);
|
|
4530
|
+
const playbackNodesRef = useRef(/* @__PURE__ */ new Set());
|
|
4491
4531
|
const isMutedRef = useRef(false);
|
|
4492
4532
|
const onValueRef = useRef(onValue);
|
|
4493
4533
|
const disposedRef = useRef(false);
|
|
4534
|
+
const smoothedInputLevelRef = useRef(0);
|
|
4535
|
+
const inputLevelFrameRef = useRef(0);
|
|
4536
|
+
const terminalReceivedRef = useRef(false);
|
|
4537
|
+
const currentPlaybackTurnRef = useRef(null);
|
|
4538
|
+
const playbackTurnsRef = useRef(/* @__PURE__ */ new Map());
|
|
4539
|
+
const socketSessionIdRef = useRef(null);
|
|
4540
|
+
if (sessionId && socketSessionIdRef.current === null) {
|
|
4541
|
+
socketSessionIdRef.current = sessionId;
|
|
4542
|
+
}
|
|
4543
|
+
const socketSessionId = socketSessionIdRef.current;
|
|
4494
4544
|
useEffect(() => {
|
|
4495
4545
|
onValueRef.current = onValue;
|
|
4496
4546
|
}, [onValue]);
|
|
4497
4547
|
useEffect(() => {
|
|
4498
4548
|
isMutedRef.current = isMuted;
|
|
4499
4549
|
}, [isMuted]);
|
|
4550
|
+
const maybeCompletePlayback = useCallback(() => {
|
|
4551
|
+
if (terminalReceivedRef.current && playbackNodesRef.current.size === 0) {
|
|
4552
|
+
setPlaybackComplete(true);
|
|
4553
|
+
}
|
|
4554
|
+
}, []);
|
|
4555
|
+
const maybeFinishPlaybackTurn = useCallback(
|
|
4556
|
+
(turnId) => {
|
|
4557
|
+
const turn = playbackTurnsRef.current.get(turnId);
|
|
4558
|
+
if (!turn || !turn.serverEnded || turn.nodes.size > 0) return;
|
|
4559
|
+
playbackTurnsRef.current.delete(turnId);
|
|
4560
|
+
if (turn.purpose === "field" && turn.fieldId !== null) {
|
|
4561
|
+
setAskedFieldId(turn.fieldId);
|
|
4562
|
+
}
|
|
4563
|
+
const ws = wsRef.current;
|
|
4564
|
+
if ((ws == null ? void 0 : ws.readyState) === WebSocket.OPEN) {
|
|
4565
|
+
ws.send(
|
|
4566
|
+
JSON.stringify({
|
|
4567
|
+
type: "playback_turn_complete",
|
|
4568
|
+
turnId
|
|
4569
|
+
})
|
|
4570
|
+
);
|
|
4571
|
+
}
|
|
4572
|
+
maybeCompletePlayback();
|
|
4573
|
+
},
|
|
4574
|
+
[maybeCompletePlayback]
|
|
4575
|
+
);
|
|
4500
4576
|
const enqueuePlayback = useCallback(
|
|
4501
4577
|
(pcm) => {
|
|
4578
|
+
var _a;
|
|
4502
4579
|
const ctx = ctxRef.current;
|
|
4503
4580
|
if (!ctx) return;
|
|
4504
4581
|
const samples = new Int16Array(pcm);
|
|
@@ -4515,70 +4592,208 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4515
4592
|
const startAt = Math.max(now, playbackQueueTimeRef.current);
|
|
4516
4593
|
node.start(startAt);
|
|
4517
4594
|
playbackQueueTimeRef.current = startAt + buf.duration;
|
|
4595
|
+
const scheduled = playbackNodesRef.current;
|
|
4596
|
+
scheduled.add(node);
|
|
4597
|
+
const turnId = currentPlaybackTurnRef.current;
|
|
4598
|
+
if (turnId !== null) {
|
|
4599
|
+
(_a = playbackTurnsRef.current.get(turnId)) == null ? void 0 : _a.nodes.add(node);
|
|
4600
|
+
}
|
|
4601
|
+
node.onended = () => {
|
|
4602
|
+
var _a2;
|
|
4603
|
+
scheduled.delete(node);
|
|
4604
|
+
let turnEnded = false;
|
|
4605
|
+
if (turnId !== null) {
|
|
4606
|
+
const turn = playbackTurnsRef.current.get(turnId);
|
|
4607
|
+
turnEnded = (_a2 = turn == null ? void 0 : turn.serverEnded) != null ? _a2 : false;
|
|
4608
|
+
turn == null ? void 0 : turn.nodes.delete(node);
|
|
4609
|
+
maybeFinishPlaybackTurn(turnId);
|
|
4610
|
+
}
|
|
4611
|
+
if (scheduled.size === 0) {
|
|
4612
|
+
setState(
|
|
4613
|
+
(prev) => prev === "speaking" ? turnEnded ? "listening" : "processing" : prev
|
|
4614
|
+
);
|
|
4615
|
+
maybeCompletePlayback();
|
|
4616
|
+
}
|
|
4617
|
+
};
|
|
4618
|
+
setState(
|
|
4619
|
+
(prev) => prev === "listening" || prev === "processing" ? "speaking" : prev
|
|
4620
|
+
);
|
|
4518
4621
|
},
|
|
4519
|
-
[outputSampleRate]
|
|
4622
|
+
[maybeCompletePlayback, maybeFinishPlaybackTurn, outputSampleRate]
|
|
4520
4623
|
);
|
|
4521
|
-
const
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4529
|
-
|
|
4624
|
+
const noteMicLevel = useCallback((pcm) => {
|
|
4625
|
+
const samples = new Int16Array(pcm);
|
|
4626
|
+
if (samples.length === 0) return;
|
|
4627
|
+
let sum = 0;
|
|
4628
|
+
for (let i = 0; i < samples.length; i++) sum += samples[i] * samples[i];
|
|
4629
|
+
const rms = Math.sqrt(sum / samples.length);
|
|
4630
|
+
const normalized = Math.min(1, Math.max(0, (rms - 80) / 2400));
|
|
4631
|
+
const previous = smoothedInputLevelRef.current;
|
|
4632
|
+
const smoothing = normalized > previous ? 0.45 : 0.16;
|
|
4633
|
+
const next = previous + (normalized - previous) * smoothing;
|
|
4634
|
+
smoothedInputLevelRef.current = next;
|
|
4635
|
+
inputLevelFrameRef.current += 1;
|
|
4636
|
+
if (inputLevelFrameRef.current % 3 === 0) setInputLevel(next);
|
|
4637
|
+
}, []);
|
|
4638
|
+
const flushPlayback = useCallback(() => {
|
|
4639
|
+
const ws = wsRef.current;
|
|
4640
|
+
if ((ws == null ? void 0 : ws.readyState) === WebSocket.OPEN) {
|
|
4641
|
+
for (const turnId of playbackTurnsRef.current.keys()) {
|
|
4642
|
+
ws.send(
|
|
4643
|
+
JSON.stringify({
|
|
4644
|
+
type: "playback_turn_cancelled",
|
|
4645
|
+
turnId
|
|
4646
|
+
})
|
|
4647
|
+
);
|
|
4648
|
+
}
|
|
4649
|
+
}
|
|
4650
|
+
const scheduled = playbackNodesRef.current;
|
|
4651
|
+
scheduled.forEach((node) => {
|
|
4652
|
+
try {
|
|
4653
|
+
node.onended = null;
|
|
4654
|
+
node.stop();
|
|
4655
|
+
node.disconnect();
|
|
4656
|
+
} catch {
|
|
4530
4657
|
}
|
|
4531
|
-
|
|
4532
|
-
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4658
|
+
});
|
|
4659
|
+
scheduled.clear();
|
|
4660
|
+
playbackTurnsRef.current.clear();
|
|
4661
|
+
currentPlaybackTurnRef.current = null;
|
|
4662
|
+
playbackQueueTimeRef.current = 0;
|
|
4663
|
+
}, []);
|
|
4664
|
+
const handleInbound = useCallback(
|
|
4665
|
+
(msg) => {
|
|
4666
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
4667
|
+
switch (msg.type) {
|
|
4668
|
+
case "state": {
|
|
4669
|
+
const next = String((_a = msg.state) != null ? _a : "");
|
|
4670
|
+
if (next === "listening" || next === "speaking" || next === "idle" || next === "connecting") {
|
|
4671
|
+
setState(next);
|
|
4672
|
+
} else if (next === "processing") {
|
|
4673
|
+
setState("connecting");
|
|
4674
|
+
}
|
|
4675
|
+
break;
|
|
4676
|
+
}
|
|
4677
|
+
case "interrupt": {
|
|
4678
|
+
flushPlayback();
|
|
4679
|
+
setState("listening");
|
|
4680
|
+
break;
|
|
4681
|
+
}
|
|
4682
|
+
case "transcript": {
|
|
4683
|
+
const text = String((_b = msg.text) != null ? _b : "");
|
|
4684
|
+
const final = msg.partial !== true;
|
|
4685
|
+
if (text.length === 0) break;
|
|
4686
|
+
setState((prev) => prev === "listening" ? "processing" : prev);
|
|
4687
|
+
setTranscripts((prev) => {
|
|
4688
|
+
const next = prev.slice();
|
|
4689
|
+
const last = next[next.length - 1];
|
|
4690
|
+
if (last && last.who === "user" && !last.final) {
|
|
4691
|
+
next[next.length - 1] = {
|
|
4692
|
+
who: "user",
|
|
4693
|
+
text: last.text + text,
|
|
4694
|
+
final
|
|
4695
|
+
};
|
|
4696
|
+
return next;
|
|
4697
|
+
}
|
|
4698
|
+
return [...next, { who: "user", text, final }];
|
|
4699
|
+
});
|
|
4700
|
+
break;
|
|
4701
|
+
}
|
|
4702
|
+
case "assistant_text": {
|
|
4703
|
+
const text = String((_c = msg.text) != null ? _c : "");
|
|
4704
|
+
if (text.length === 0) break;
|
|
4705
|
+
setAssistantText(text);
|
|
4706
|
+
setTranscripts((prev) => {
|
|
4537
4707
|
const next = prev.slice();
|
|
4538
|
-
const
|
|
4539
|
-
if (
|
|
4540
|
-
next[
|
|
4708
|
+
const last = next[next.length - 1];
|
|
4709
|
+
if (last && last.who === "assistant" && !last.final) {
|
|
4710
|
+
next[next.length - 1] = {
|
|
4711
|
+
who: "assistant",
|
|
4712
|
+
text: last.text + text,
|
|
4713
|
+
final: false
|
|
4714
|
+
};
|
|
4541
4715
|
return next;
|
|
4542
4716
|
}
|
|
4543
|
-
return [...next, { who, text, final }];
|
|
4717
|
+
return [...next, { who: "assistant", text, final: false }];
|
|
4718
|
+
});
|
|
4719
|
+
break;
|
|
4720
|
+
}
|
|
4721
|
+
case "audio_turn_start": {
|
|
4722
|
+
const turnId = String((_d = msg.turnId) != null ? _d : "");
|
|
4723
|
+
if (turnId.length === 0) break;
|
|
4724
|
+
currentPlaybackTurnRef.current = turnId;
|
|
4725
|
+
playbackTurnsRef.current.set(turnId, {
|
|
4726
|
+
purpose: String((_e = msg.purpose) != null ? _e : ""),
|
|
4727
|
+
fieldId: typeof msg.fieldId === "string" && msg.fieldId.length > 0 ? msg.fieldId : null,
|
|
4728
|
+
nodes: /* @__PURE__ */ new Set(),
|
|
4729
|
+
serverEnded: false
|
|
4730
|
+
});
|
|
4731
|
+
setState(
|
|
4732
|
+
(prev) => prev === "speaking" || prev === "completed" || prev === "error" ? prev : "processing"
|
|
4733
|
+
);
|
|
4734
|
+
break;
|
|
4735
|
+
}
|
|
4736
|
+
case "audio_turn_end": {
|
|
4737
|
+
const turnId = String((_f = msg.turnId) != null ? _f : "");
|
|
4738
|
+
if (turnId.length === 0) break;
|
|
4739
|
+
const turn = playbackTurnsRef.current.get(turnId);
|
|
4740
|
+
if (turn) turn.serverEnded = true;
|
|
4741
|
+
if (currentPlaybackTurnRef.current === turnId) {
|
|
4742
|
+
currentPlaybackTurnRef.current = null;
|
|
4544
4743
|
}
|
|
4545
|
-
|
|
4546
|
-
|
|
4547
|
-
|
|
4548
|
-
|
|
4549
|
-
|
|
4550
|
-
|
|
4551
|
-
|
|
4552
|
-
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4562
|
-
|
|
4563
|
-
|
|
4744
|
+
setTranscripts((prev) => {
|
|
4745
|
+
const last = prev[prev.length - 1];
|
|
4746
|
+
if (!last || last.who !== "assistant" || last.final) return prev;
|
|
4747
|
+
const next = prev.slice();
|
|
4748
|
+
next[next.length - 1] = { ...last, final: true };
|
|
4749
|
+
return next;
|
|
4750
|
+
});
|
|
4751
|
+
maybeFinishPlaybackTurn(turnId);
|
|
4752
|
+
break;
|
|
4753
|
+
}
|
|
4754
|
+
case "field_change": {
|
|
4755
|
+
const fieldId = String((_g = msg.fieldId) != null ? _g : "");
|
|
4756
|
+
setActiveFieldId(fieldId);
|
|
4757
|
+
setState(
|
|
4758
|
+
(prev) => prev === "speaking" || prev === "error" ? prev : "processing"
|
|
4759
|
+
);
|
|
4760
|
+
break;
|
|
4761
|
+
}
|
|
4762
|
+
case "value": {
|
|
4763
|
+
const v = msg.value;
|
|
4764
|
+
setLastValue(v);
|
|
4765
|
+
setState(
|
|
4766
|
+
(prev) => prev === "speaking" || prev === "error" ? prev : "processing"
|
|
4767
|
+
);
|
|
4768
|
+
try {
|
|
4769
|
+
onValueRef.current(v);
|
|
4770
|
+
} catch {
|
|
4771
|
+
}
|
|
4772
|
+
break;
|
|
4773
|
+
}
|
|
4774
|
+
case "completed": {
|
|
4775
|
+
terminalReceivedRef.current = true;
|
|
4776
|
+
setState("completed");
|
|
4777
|
+
maybeCompletePlayback();
|
|
4778
|
+
break;
|
|
4779
|
+
}
|
|
4780
|
+
case "error": {
|
|
4781
|
+
setError(String((_h = msg.message) != null ? _h : "voice session error"));
|
|
4782
|
+
setState("error");
|
|
4783
|
+
break;
|
|
4564
4784
|
}
|
|
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
4785
|
}
|
|
4576
|
-
}
|
|
4577
|
-
|
|
4786
|
+
},
|
|
4787
|
+
[flushPlayback, maybeCompletePlayback, maybeFinishPlaybackTurn]
|
|
4788
|
+
);
|
|
4578
4789
|
useEffect(() => {
|
|
4579
|
-
if (!
|
|
4790
|
+
if (!socketSessionId || !participantToken) return;
|
|
4580
4791
|
let cancelled = false;
|
|
4581
4792
|
disposedRef.current = false;
|
|
4793
|
+
terminalReceivedRef.current = false;
|
|
4794
|
+
currentPlaybackTurnRef.current = null;
|
|
4795
|
+
playbackTurnsRef.current.clear();
|
|
4796
|
+
setPlaybackComplete(false);
|
|
4582
4797
|
const start = async () => {
|
|
4583
4798
|
try {
|
|
4584
4799
|
setState("connecting");
|
|
@@ -4610,7 +4825,7 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4610
4825
|
});
|
|
4611
4826
|
workletRef.current = worklet;
|
|
4612
4827
|
source.connect(worklet);
|
|
4613
|
-
const wsUrl = apiBase.replace(/^http/, "ws") + `/api/participant/inbox/${encodeURIComponent(
|
|
4828
|
+
const wsUrl = apiBase.replace(/^http/, "ws") + `/api/participant/inbox/${encodeURIComponent(socketSessionId)}/voice-stream?token=${encodeURIComponent(participantToken)}`;
|
|
4614
4829
|
const ws = new WebSocket(wsUrl, SUBPROTOCOL);
|
|
4615
4830
|
ws.binaryType = "arraybuffer";
|
|
4616
4831
|
wsRef.current = ws;
|
|
@@ -4650,7 +4865,9 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4650
4865
|
worklet.port.onmessage = (ev) => {
|
|
4651
4866
|
if (isMutedRef.current) return;
|
|
4652
4867
|
if (ws.readyState !== WebSocket.OPEN) return;
|
|
4653
|
-
|
|
4868
|
+
const pcm = ev.data;
|
|
4869
|
+
noteMicLevel(pcm);
|
|
4870
|
+
ws.send(pcm);
|
|
4654
4871
|
};
|
|
4655
4872
|
} catch (err) {
|
|
4656
4873
|
if (cancelled) return;
|
|
@@ -4691,11 +4908,12 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4691
4908
|
}, [
|
|
4692
4909
|
apiBase,
|
|
4693
4910
|
participantToken,
|
|
4694
|
-
|
|
4911
|
+
socketSessionId,
|
|
4695
4912
|
inputSampleRate,
|
|
4696
4913
|
outputSampleRate,
|
|
4697
4914
|
enqueuePlayback,
|
|
4698
|
-
handleInbound
|
|
4915
|
+
handleInbound,
|
|
4916
|
+
noteMicLevel
|
|
4699
4917
|
]);
|
|
4700
4918
|
const toggleMute = useCallback(() => {
|
|
4701
4919
|
setIsMuted((prev) => {
|
|
@@ -4713,7 +4931,10 @@ function useGeminiLiveSession(cfg, onValue) {
|
|
|
4713
4931
|
transcripts,
|
|
4714
4932
|
assistantText,
|
|
4715
4933
|
activeFieldId,
|
|
4934
|
+
askedFieldId,
|
|
4935
|
+
playbackComplete,
|
|
4716
4936
|
lastValue,
|
|
4937
|
+
inputLevel: isMuted ? 0 : inputLevel,
|
|
4717
4938
|
isMuted,
|
|
4718
4939
|
toggleMute,
|
|
4719
4940
|
error
|
|
@@ -4724,11 +4945,13 @@ function VoiceLiveRenderer({
|
|
|
4724
4945
|
apiBase,
|
|
4725
4946
|
participantToken,
|
|
4726
4947
|
sessionId,
|
|
4948
|
+
fieldSessionId,
|
|
4727
4949
|
liveConfig,
|
|
4728
4950
|
onValue,
|
|
4951
|
+
onPlaybackComplete,
|
|
4952
|
+
onFieldRevealed,
|
|
4729
4953
|
disabled
|
|
4730
4954
|
}) {
|
|
4731
|
-
var _a;
|
|
4732
4955
|
const session = useGeminiLiveSession(
|
|
4733
4956
|
{
|
|
4734
4957
|
apiBase,
|
|
@@ -4739,13 +4962,20 @@ function VoiceLiveRenderer({
|
|
|
4739
4962
|
},
|
|
4740
4963
|
onValue
|
|
4741
4964
|
);
|
|
4965
|
+
useEffect(() => {
|
|
4966
|
+
if (session.playbackComplete) onPlaybackComplete == null ? void 0 : onPlaybackComplete();
|
|
4967
|
+
}, [session.playbackComplete, onPlaybackComplete]);
|
|
4742
4968
|
useEffect(() => {
|
|
4743
4969
|
}, [session.activeFieldId]);
|
|
4970
|
+
const fieldRevealed = session.askedFieldId === field.id;
|
|
4971
|
+
useEffect(() => {
|
|
4972
|
+
if (fieldRevealed) onFieldRevealed == null ? void 0 : onFieldRevealed(fieldSessionId);
|
|
4973
|
+
}, [fieldRevealed, fieldSessionId, onFieldRevealed]);
|
|
4744
4974
|
const promptRef = useRef(null);
|
|
4745
4975
|
useEffect(() => {
|
|
4746
|
-
var
|
|
4747
|
-
(
|
|
4748
|
-
}, [field]);
|
|
4976
|
+
var _a;
|
|
4977
|
+
(_a = promptRef.current) == null ? void 0 : _a.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
|
4978
|
+
}, [field, fieldRevealed]);
|
|
4749
4979
|
const orbState = (() => {
|
|
4750
4980
|
if (session.error) return "error";
|
|
4751
4981
|
if (session.isMuted) return "idle";
|
|
@@ -4755,8 +4985,10 @@ function VoiceLiveRenderer({
|
|
|
4755
4985
|
return "idle";
|
|
4756
4986
|
case "listening":
|
|
4757
4987
|
return "listening";
|
|
4758
|
-
case "
|
|
4988
|
+
case "processing":
|
|
4759
4989
|
return "processing";
|
|
4990
|
+
case "speaking":
|
|
4991
|
+
return "speaking";
|
|
4760
4992
|
case "completed":
|
|
4761
4993
|
return "idle";
|
|
4762
4994
|
case "error":
|
|
@@ -4765,12 +4997,17 @@ function VoiceLiveRenderer({
|
|
|
4765
4997
|
return "idle";
|
|
4766
4998
|
}
|
|
4767
4999
|
})();
|
|
4768
|
-
const
|
|
5000
|
+
const logRef = useRef(null);
|
|
5001
|
+
useEffect(() => {
|
|
5002
|
+
const el = logRef.current;
|
|
5003
|
+
if (el) el.scrollTop = el.scrollHeight;
|
|
5004
|
+
}, [session.transcripts]);
|
|
4769
5005
|
const announcement = (() => {
|
|
4770
5006
|
if (session.error) return session.error;
|
|
4771
5007
|
if (session.isMuted) return "Microphone muted.";
|
|
4772
5008
|
if (session.state === "connecting") return "Connecting to voice service.";
|
|
4773
|
-
if (session.state === "listening") return "
|
|
5009
|
+
if (session.state === "listening") return "Microphone listening.";
|
|
5010
|
+
if (session.state === "processing") return "Preparing the next step.";
|
|
4774
5011
|
if (session.state === "speaking") return "Assistant speaking.";
|
|
4775
5012
|
return "";
|
|
4776
5013
|
})();
|
|
@@ -4785,42 +5022,49 @@ function VoiceLiveRenderer({
|
|
|
4785
5022
|
children: announcement
|
|
4786
5023
|
}
|
|
4787
5024
|
),
|
|
4788
|
-
/* @__PURE__ */
|
|
4789
|
-
|
|
4790
|
-
|
|
4791
|
-
|
|
4792
|
-
|
|
4793
|
-
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
5025
|
+
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbContainer, children: [
|
|
5026
|
+
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbRow, children: [
|
|
5027
|
+
/* @__PURE__ */ jsx(
|
|
5028
|
+
VoiceOrb,
|
|
5029
|
+
{
|
|
5030
|
+
state: orbState,
|
|
5031
|
+
inputLevel: session.isMuted ? 0 : session.inputLevel,
|
|
5032
|
+
disabled
|
|
5033
|
+
}
|
|
5034
|
+
),
|
|
5035
|
+
/* @__PURE__ */ jsx(
|
|
5036
|
+
"button",
|
|
5037
|
+
{
|
|
5038
|
+
type: "button",
|
|
5039
|
+
"data-cf-voice-mute": "true",
|
|
5040
|
+
className: `${widget_default.voiceMuteButton}${session.isMuted ? " " + widget_default.voiceMuteButtonMuted : ""}`,
|
|
5041
|
+
onClick: session.toggleMute,
|
|
5042
|
+
"aria-label": session.isMuted ? "Unmute microphone" : "Mute microphone",
|
|
5043
|
+
"aria-pressed": session.isMuted,
|
|
5044
|
+
children: session.isMuted ? "Muted" : "Mute"
|
|
5045
|
+
}
|
|
5046
|
+
)
|
|
5047
|
+
] }),
|
|
5048
|
+
/* @__PURE__ */ jsxs("div", { className: widget_default.voiceOrbStats, children: [
|
|
5049
|
+
session.isMuted && /* @__PURE__ */ jsx("span", { className: widget_default.voiceOrbHint, "aria-hidden": "true", children: "Microphone muted" }),
|
|
5050
|
+
!session.isMuted && session.state === "connecting" && /* @__PURE__ */ jsx("span", { className: widget_default.voiceAssistantStatus, children: "Connecting\u2026" }),
|
|
5051
|
+
!session.isMuted && session.state === "processing" && /* @__PURE__ */ jsx("span", { className: widget_default.voiceAssistantStatus, children: "Preparing the next step\u2026" }),
|
|
5052
|
+
!session.isMuted && session.state === "speaking" && /* @__PURE__ */ jsx("span", { className: widget_default.voiceAssistantStatus, children: "Assistant speaking\u2026" })
|
|
5053
|
+
] })
|
|
5054
|
+
] }),
|
|
5055
|
+
session.transcripts.length > 0 && /* @__PURE__ */ jsx("div", { ref: logRef, className: widget_default.voiceLiveLog, children: session.transcripts.map((t, i) => /* @__PURE__ */ jsxs(
|
|
4797
5056
|
"div",
|
|
4798
5057
|
{
|
|
4799
|
-
className:
|
|
5058
|
+
className: t.who === "user" ? widget_default.voiceLiveLogUser : widget_default.voiceLiveLogAssistant,
|
|
4800
5059
|
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
|
-
)
|
|
5060
|
+
/* @__PURE__ */ jsx("span", { className: widget_default.voiceTranscriptLabel, children: t.who === "user" ? "You" : "Assistant" }),
|
|
5061
|
+
/* @__PURE__ */ jsx("span", { className: widget_default.voiceTranscriptText, children: t.text })
|
|
4821
5062
|
]
|
|
4822
|
-
}
|
|
4823
|
-
|
|
5063
|
+
},
|
|
5064
|
+
i
|
|
5065
|
+
)) }),
|
|
5066
|
+
fieldRevealed && /* @__PURE__ */ jsx("div", { ref: promptRef, children: /* @__PURE__ */ jsx(VoicePromptCard, { field }) }),
|
|
5067
|
+
session.error && /* @__PURE__ */ jsx("p", { className: widget_default.voiceError, children: session.error })
|
|
4824
5068
|
] });
|
|
4825
5069
|
}
|
|
4826
5070
|
function InputText({
|
|
@@ -6019,7 +6263,7 @@ var ChatAdapter = forwardRef(
|
|
|
6019
6263
|
components,
|
|
6020
6264
|
strings
|
|
6021
6265
|
}, ref) {
|
|
6022
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
6266
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
|
|
6023
6267
|
const t = resolveStrings(strings);
|
|
6024
6268
|
const apiBaseResolved = apiBase;
|
|
6025
6269
|
const appBaseResolved = typeof window !== "undefined" ? window.location.origin : (_a = process.env.APP_URL) != null ? _a : "http://localhost:3000";
|
|
@@ -6065,12 +6309,6 @@ var ChatAdapter = forwardRef(
|
|
|
6065
6309
|
}),
|
|
6066
6310
|
[reconnect, disconnect, sendMessage]
|
|
6067
6311
|
);
|
|
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
6312
|
const {
|
|
6075
6313
|
connections: requiredConnections,
|
|
6076
6314
|
allProvided: allConnectionsProvided,
|
|
@@ -6154,7 +6392,11 @@ var ChatAdapter = forwardRef(
|
|
|
6154
6392
|
if (!isVoiceSurface && activeSession && extractSurface(activeSession) === "widget/voice") {
|
|
6155
6393
|
setIsVoiceSurface(true);
|
|
6156
6394
|
}
|
|
6157
|
-
const
|
|
6395
|
+
const [isLiveVoiceSurface, setIsLiveVoiceSurface] = useState(false);
|
|
6396
|
+
if (!isLiveVoiceSurface && activeSession && extractSurface(activeSession) === "widget/voice" && ((_b = extractSurfaceConfig(activeSession)) == null ? void 0 : _b.style) === "live") {
|
|
6397
|
+
setIsLiveVoiceSurface(true);
|
|
6398
|
+
}
|
|
6399
|
+
const voiceSessionId = (_c = activeSession == null ? void 0 : activeSession.id) != null ? _c : null;
|
|
6158
6400
|
const prevVoiceSessionIdRef = useRef(null);
|
|
6159
6401
|
useEffect(() => {
|
|
6160
6402
|
if (!isVoiceSurface) return;
|
|
@@ -6164,6 +6406,25 @@ var ChatAdapter = forwardRef(
|
|
|
6164
6406
|
}
|
|
6165
6407
|
prevVoiceSessionIdRef.current = voiceSessionId;
|
|
6166
6408
|
}, [voiceSessionId, isVoiceSurface, clearSpeech]);
|
|
6409
|
+
const liveVoiceRef = useRef(null);
|
|
6410
|
+
const [revealedLivePromptSessions, setRevealedLivePromptSessions] = useState(
|
|
6411
|
+
() => /* @__PURE__ */ new Set()
|
|
6412
|
+
);
|
|
6413
|
+
const [livePlaybackComplete, setLivePlaybackComplete] = useState(false);
|
|
6414
|
+
const livePlaybackSessionRef = useRef(null);
|
|
6415
|
+
useEffect(() => {
|
|
6416
|
+
if (!activeSession || activeSession.id === livePlaybackSessionRef.current) return;
|
|
6417
|
+
livePlaybackSessionRef.current = activeSession.id;
|
|
6418
|
+
setLivePlaybackComplete(false);
|
|
6419
|
+
}, [activeSession]);
|
|
6420
|
+
const terminalVoiceDraining = (status === "completed" || status === "error") && isLiveVoiceSurface && liveVoiceRef.current !== null && !livePlaybackComplete;
|
|
6421
|
+
const reportedStatus = terminalVoiceDraining ? "running" : status;
|
|
6422
|
+
const prevStatusRef = useRef(null);
|
|
6423
|
+
useEffect(() => {
|
|
6424
|
+
if (prevStatusRef.current === reportedStatus) return;
|
|
6425
|
+
prevStatusRef.current = reportedStatus;
|
|
6426
|
+
onStatusChange == null ? void 0 : onStatusChange(reportedStatus);
|
|
6427
|
+
}, [reportedStatus, onStatusChange]);
|
|
6167
6428
|
useEffect(() => {
|
|
6168
6429
|
if (!isVoiceSurface) return;
|
|
6169
6430
|
for (const entry of timeline) {
|
|
@@ -6171,16 +6432,17 @@ var ChatAdapter = forwardRef(
|
|
|
6171
6432
|
if (entry.sender === "user") continue;
|
|
6172
6433
|
if (spokenMessageSeqsRef.current.has(entry.seq)) continue;
|
|
6173
6434
|
spokenMessageSeqsRef.current.add(entry.seq);
|
|
6435
|
+
if (isLiveVoiceSurface) continue;
|
|
6174
6436
|
const text = entry.payload.voice_hint || entry.payload.content || "";
|
|
6175
6437
|
void enqueueSpeak(text);
|
|
6176
6438
|
}
|
|
6177
|
-
}, [timeline, isVoiceSurface, enqueueSpeak]);
|
|
6439
|
+
}, [timeline, isVoiceSurface, isLiveVoiceSurface, enqueueSpeak]);
|
|
6178
6440
|
const completedFiredRef = useRef(false);
|
|
6179
6441
|
const errorFiredRef = useRef(false);
|
|
6180
6442
|
const expiredFiredRef = useRef(false);
|
|
6181
6443
|
const inputAreaRef = useRef(null);
|
|
6182
6444
|
const lastFocusedSessionRef = useRef(null);
|
|
6183
|
-
const activeSessionId = (
|
|
6445
|
+
const activeSessionId = (_d = activeSession == null ? void 0 : activeSession.id) != null ? _d : null;
|
|
6184
6446
|
useEffect(() => {
|
|
6185
6447
|
if (!autoFocusInput) return;
|
|
6186
6448
|
if (!activeSessionId) return;
|
|
@@ -6196,11 +6458,11 @@ var ChatAdapter = forwardRef(
|
|
|
6196
6458
|
}
|
|
6197
6459
|
}, [autoFocusInput, activeSessionId, status]);
|
|
6198
6460
|
useEffect(() => {
|
|
6199
|
-
if (status === "completed" && !completedFiredRef.current) {
|
|
6461
|
+
if (status === "completed" && !terminalVoiceDraining && !completedFiredRef.current) {
|
|
6200
6462
|
completedFiredRef.current = true;
|
|
6201
6463
|
onCompleted == null ? void 0 : onCompleted(output != null ? output : void 0);
|
|
6202
6464
|
}
|
|
6203
|
-
if (status === "error" && !errorFiredRef.current) {
|
|
6465
|
+
if (status === "error" && !terminalVoiceDraining && !errorFiredRef.current) {
|
|
6204
6466
|
errorFiredRef.current = true;
|
|
6205
6467
|
onError == null ? void 0 : onError(new WidgetError("run_failed", "The run encountered an error."));
|
|
6206
6468
|
}
|
|
@@ -6210,7 +6472,7 @@ var ChatAdapter = forwardRef(
|
|
|
6210
6472
|
new WidgetError("token_expired", "The participant token expired and could not be renewed.")
|
|
6211
6473
|
);
|
|
6212
6474
|
}
|
|
6213
|
-
}, [status, output, onCompleted, onError]);
|
|
6475
|
+
}, [status, terminalVoiceDraining, output, onCompleted, onError]);
|
|
6214
6476
|
const colorSchemeClass = colorScheme === "dark" ? widget_default.cfDark : colorScheme === "auto" ? widget_default.cfAuto : void 0;
|
|
6215
6477
|
const cls = [widget_default.widget, colorSchemeClass, className].filter(Boolean).join(" ");
|
|
6216
6478
|
const rootStyle = useMemo(() => themeToStyle(theme), [theme]);
|
|
@@ -6270,7 +6532,8 @@ var ChatAdapter = forwardRef(
|
|
|
6270
6532
|
/* @__PURE__ */ jsx(ChatTimeline, { entries: timeline, streamingText, components })
|
|
6271
6533
|
] });
|
|
6272
6534
|
}
|
|
6273
|
-
|
|
6535
|
+
const awaitingLivePlayback = terminalVoiceDraining;
|
|
6536
|
+
if ((status === "completed" || status === "error") && !awaitingLivePlayback) {
|
|
6274
6537
|
const hasChatContent = timeline.some(
|
|
6275
6538
|
(e) => {
|
|
6276
6539
|
var _a2, _b2;
|
|
@@ -6290,7 +6553,7 @@ var ChatAdapter = forwardRef(
|
|
|
6290
6553
|
FormRenderer,
|
|
6291
6554
|
{
|
|
6292
6555
|
fields: formSpec == null ? void 0 : formSpec.fields,
|
|
6293
|
-
repeater: (
|
|
6556
|
+
repeater: (_e = formSpec == null ? void 0 : formSpec.repeaters) == null ? void 0 : _e[0],
|
|
6294
6557
|
onSubmit: (values) => void submitHandback(values),
|
|
6295
6558
|
disabled: submitting
|
|
6296
6559
|
}
|
|
@@ -6324,9 +6587,9 @@ var ChatAdapter = forwardRef(
|
|
|
6324
6587
|
const converseUserCanEnd = (converseCtx == null ? void 0 : converseCtx.converse_user_can_end) === true;
|
|
6325
6588
|
const field = activeSession ? extractField(activeSession) : null;
|
|
6326
6589
|
if (status === "awaiting_chat" && activeSession && !(isConverseSession && field)) {
|
|
6327
|
-
const hbCtx = (
|
|
6590
|
+
const hbCtx = (_f = activeSession.entry_context) == null ? void 0 : _f.handback;
|
|
6328
6591
|
const outputSchema = hbCtx == null ? void 0 : hbCtx.output_schema;
|
|
6329
|
-
const requireOutput = (
|
|
6592
|
+
const requireOutput = (_g = hbCtx == null ? void 0 : hbCtx.require_output) != null ? _g : false;
|
|
6330
6593
|
const handbackReady = !requireOutput || outputSchema != null && Object.keys(outputSchema).every((k) => {
|
|
6331
6594
|
var _a2;
|
|
6332
6595
|
return ((_a2 = handbackValues[k]) != null ? _a2 : "").trim() !== "";
|
|
@@ -6427,31 +6690,63 @@ var ChatAdapter = forwardRef(
|
|
|
6427
6690
|
const shouldUseVoice = effectiveVoiceMode !== "off";
|
|
6428
6691
|
const voiceRenderer = effectiveVoiceMode === "push_to_talk" ? "click" : "continuous";
|
|
6429
6692
|
const isLiveVoice = surface === "widget/voice" && (surfaceConfig == null ? void 0 : surfaceConfig.style) === "live";
|
|
6430
|
-
if (
|
|
6431
|
-
|
|
6432
|
-
|
|
6433
|
-
|
|
6434
|
-
|
|
6435
|
-
|
|
6436
|
-
|
|
6437
|
-
|
|
6693
|
+
if (activeSession && !isLiveVoice) {
|
|
6694
|
+
liveVoiceRef.current = null;
|
|
6695
|
+
} else if (isLiveVoice && activeSession && field) {
|
|
6696
|
+
liveVoiceRef.current = {
|
|
6697
|
+
// Pin the FIRST id: it only picks which socket to open, and the proxy
|
|
6698
|
+
// tracks field transitions itself from then on.
|
|
6699
|
+
sessionId: (_i = (_h = liveVoiceRef.current) == null ? void 0 : _h.sessionId) != null ? _i : activeSession.id,
|
|
6700
|
+
fieldSessionId: activeSession.id,
|
|
6701
|
+
field,
|
|
6702
|
+
hint: (_k = nodeVoice == null ? void 0 : nodeVoice.hint) != null ? _k : (_j = field.voice) == null ? void 0 : _j.hint,
|
|
6703
|
+
config: surfaceConfig != null ? surfaceConfig : void 0
|
|
6438
6704
|
};
|
|
6705
|
+
}
|
|
6706
|
+
const liveVoice = isLiveVoice && activeSession && field || !activeSession && status === "running" || awaitingLivePlayback ? liveVoiceRef.current : null;
|
|
6707
|
+
if (liveVoice) {
|
|
6708
|
+
const respondedSessionIds = new Set(
|
|
6709
|
+
timeline.filter((entry) => entry.event_type === "response" && entry.session_id).map((entry) => entry.session_id)
|
|
6710
|
+
);
|
|
6711
|
+
const visibleLiveTimeline = timeline.filter(
|
|
6712
|
+
(entry) => entry.event_type !== "prompt" || entry.session_id === null || respondedSessionIds.has(entry.session_id) || revealedLivePromptSessions.has(entry.session_id)
|
|
6713
|
+
);
|
|
6439
6714
|
return /* @__PURE__ */ jsxs("div", { className: cls, style: rootStyle, children: [
|
|
6440
6715
|
header,
|
|
6441
|
-
/* @__PURE__ */ jsx(ChatTimeline, { entries:
|
|
6442
|
-
/* @__PURE__ */ jsx("div", { className: widget_default.activeInputArea
|
|
6716
|
+
/* @__PURE__ */ jsx(ChatTimeline, { entries: visibleLiveTimeline, showTyping, streamingText, components }),
|
|
6717
|
+
/* @__PURE__ */ jsx("div", { className: `${widget_default.activeInputArea} ${widget_default.activeInputAreaVoice}`, children: /* @__PURE__ */ jsx(
|
|
6443
6718
|
VoiceLiveRenderer,
|
|
6444
6719
|
{
|
|
6445
|
-
field,
|
|
6446
|
-
voiceHint:
|
|
6720
|
+
field: liveVoice.field,
|
|
6721
|
+
voiceHint: liveVoice.hint,
|
|
6447
6722
|
apiBase: apiBaseResolved,
|
|
6448
6723
|
participantToken,
|
|
6449
|
-
sessionId:
|
|
6450
|
-
|
|
6451
|
-
|
|
6724
|
+
sessionId: liveVoice.sessionId,
|
|
6725
|
+
fieldSessionId: liveVoice.fieldSessionId,
|
|
6726
|
+
liveConfig: liveVoice.config,
|
|
6727
|
+
onValue: (value) => setInputValue(value),
|
|
6728
|
+
onPlaybackComplete: () => setLivePlaybackComplete(true),
|
|
6729
|
+
onFieldRevealed: (revealedSessionId) => setRevealedLivePromptSessions((prev) => {
|
|
6730
|
+
if (prev.has(revealedSessionId)) return prev;
|
|
6731
|
+
const next = new Set(prev);
|
|
6732
|
+
next.add(revealedSessionId);
|
|
6733
|
+
return next;
|
|
6734
|
+
}),
|
|
6452
6735
|
disabled: submitting
|
|
6453
6736
|
}
|
|
6454
|
-
)
|
|
6737
|
+
) })
|
|
6738
|
+
] });
|
|
6739
|
+
}
|
|
6740
|
+
if (shouldUseVoice && activeSession && field) {
|
|
6741
|
+
const spokenHint = (_m = nodeVoice == null ? void 0 : nodeVoice.hint) != null ? _m : (_l = field.voice) == null ? void 0 : _l.hint;
|
|
6742
|
+
const handleVoiceValue = (value) => {
|
|
6743
|
+
setInputValue(value);
|
|
6744
|
+
void submitHandback(value, crypto.randomUUID());
|
|
6745
|
+
};
|
|
6746
|
+
return /* @__PURE__ */ jsxs("div", { className: cls, style: rootStyle, children: [
|
|
6747
|
+
header,
|
|
6748
|
+
/* @__PURE__ */ jsx(ChatTimeline, { entries: timeline, showTyping, streamingText, components }),
|
|
6749
|
+
/* @__PURE__ */ jsx("div", { className: `${widget_default.activeInputArea} ${widget_default.activeInputAreaVoice}`, children: voiceRenderer === "continuous" ? /* @__PURE__ */ jsx(
|
|
6455
6750
|
VoiceContinuousRenderer,
|
|
6456
6751
|
{
|
|
6457
6752
|
field,
|
|
@@ -6460,7 +6755,7 @@ var ChatAdapter = forwardRef(
|
|
|
6460
6755
|
participantToken,
|
|
6461
6756
|
onValue: handleVoiceValue,
|
|
6462
6757
|
autoSubmitMode: "confirm",
|
|
6463
|
-
confidenceThreshold: (
|
|
6758
|
+
confidenceThreshold: (_n = nodeVoice == null ? void 0 : nodeVoice.confidence_threshold) != null ? _n : typeof (surfaceConfig == null ? void 0 : surfaceConfig.confidence_threshold) === "number" ? surfaceConfig.confidence_threshold : 0.7,
|
|
6464
6759
|
enqueueSpeak,
|
|
6465
6760
|
isSpeaking,
|
|
6466
6761
|
disabled: submitting
|