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