@yieldfabric/terminal 1.1.2 → 1.3.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/index.cjs +1274 -12
- package/dist/index.d.cts +423 -3
- package/dist/index.d.ts +423 -3
- package/dist/index.js +1260 -14
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* Runtime use requires a separate express license.
|
|
7
7
|
*/
|
|
8
8
|
import React15, { createContext, memo, useMemo, useRef, useEffect, useCallback, useContext, useState, useReducer } from 'react';
|
|
9
|
-
import { X, Check, Plus, FileText, MessageCircle, ChevronRight, Paperclip, FolderOpen, Brain, XCircle, Loader2, Minimize2, Maximize2, AlertTriangle, HelpCircle, ChevronLeft, Pencil, ChevronDown, ExternalLink, Search, Network, Boxes, Settings, Terminal, CheckCircle, ArrowRight, Users, Eye, AlertCircle, CheckCircle2, Copy, Zap, Download, Trash2, Link2 } from 'lucide-react';
|
|
9
|
+
import { X, Check, Plus, FileText, MessageCircle, ChevronRight, Paperclip, FolderOpen, Brain, XCircle, Loader2, Minimize2, Maximize2, Mic, MicOff, AlertTriangle, HelpCircle, ChevronLeft, Pencil, ChevronDown, ExternalLink, Search, Network, Boxes, Settings, Terminal, CheckCircle, ArrowRight, Users, Eye, AlertCircle, CheckCircle2, Copy, Zap, Download, Trash2, Link2 } from 'lucide-react';
|
|
10
10
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
11
11
|
import ReactMarkdown from 'react-markdown';
|
|
12
12
|
import remarkGfm from 'remark-gfm';
|
|
@@ -809,11 +809,11 @@ var AVATAR_COLORS = [
|
|
|
809
809
|
"#ef4444"
|
|
810
810
|
];
|
|
811
811
|
function avatarColor(entityId) {
|
|
812
|
-
let
|
|
812
|
+
let hash2 = 0;
|
|
813
813
|
for (let i = 0; i < entityId.length; i++) {
|
|
814
|
-
|
|
814
|
+
hash2 = entityId.charCodeAt(i) + ((hash2 << 5) - hash2);
|
|
815
815
|
}
|
|
816
|
-
return AVATAR_COLORS[Math.abs(
|
|
816
|
+
return AVATAR_COLORS[Math.abs(hash2) % AVATAR_COLORS.length];
|
|
817
817
|
}
|
|
818
818
|
function initials(name) {
|
|
819
819
|
return name.split(" ").map((w) => w[0]).join("").toUpperCase().slice(0, 2);
|
|
@@ -2225,18 +2225,21 @@ function CopyButton({ content }) {
|
|
|
2225
2225
|
}
|
|
2226
2226
|
);
|
|
2227
2227
|
}
|
|
2228
|
+
var MESSAGES = {
|
|
2229
|
+
rounds_exhausted: "Partial answer \u2014 I reached the step limit. Ask a narrower follow-up to continue.",
|
|
2230
|
+
budget_exhausted: "Partial answer \u2014 this question needs more context than fits in one answer. Try narrowing the question.",
|
|
2231
|
+
deadline: "Partial answer \u2014 I ran out of time before finishing. Ask a narrower follow-up to continue.",
|
|
2232
|
+
output_limit: "Partial answer \u2014 it was cut off at its length limit. Ask for the rest, or a narrower follow-up.",
|
|
2233
|
+
awaiting_tool_results: "This answer paused before its next step. Ask again to review a fresh answer.",
|
|
2234
|
+
empty: "No answer was returned. Please try again.",
|
|
2235
|
+
failed: "The answer could not be completed. What arrived so far is kept here.",
|
|
2236
|
+
interrupted: "Connection interrupted. The assistant may still be working; check the conversation before sending again.",
|
|
2237
|
+
cancelled: "Stopped. You can change your question and ask again."
|
|
2238
|
+
};
|
|
2228
2239
|
function AgentCompletionNotice({ completion }) {
|
|
2229
2240
|
const t = useTheme();
|
|
2230
2241
|
if (!completion || completion.stop_reason === "answered") return null;
|
|
2231
|
-
const message =
|
|
2232
|
-
rounds_exhausted: "Partial answer \u2014 I reached the step limit. Ask a narrower follow-up to continue.",
|
|
2233
|
-
budget_exhausted: "Partial answer \u2014 this question needs more context than fits in one answer. Try narrowing the question.",
|
|
2234
|
-
awaiting_tool_results: "This answer paused before its next step. Ask again to review a fresh answer.",
|
|
2235
|
-
empty: "No answer was returned. Please try again.",
|
|
2236
|
-
failed: "The answer could not be completed. What arrived so far is kept here.",
|
|
2237
|
-
interrupted: "Connection interrupted. The assistant may still be working; check the conversation before sending again.",
|
|
2238
|
-
cancelled: "Stopped. You can change your question and ask again."
|
|
2239
|
-
}[completion.stop_reason];
|
|
2242
|
+
const message = MESSAGES[completion.stop_reason];
|
|
2240
2243
|
if (!message) return null;
|
|
2241
2244
|
return /* @__PURE__ */ jsx("p", { role: "status", className: "mt-2 rounded-md px-3 py-2 text-xs", style: { color: t.textSecondary, backgroundColor: t.bgRaisedAlt, border: `1px solid ${t.border}` }, children: message });
|
|
2242
2245
|
}
|
|
@@ -3380,6 +3383,7 @@ function TerminalInputBar({
|
|
|
3380
3383
|
selectedPaletteItem,
|
|
3381
3384
|
onSelectPaletteItem,
|
|
3382
3385
|
inputBarExtra,
|
|
3386
|
+
leading,
|
|
3383
3387
|
modeSelector,
|
|
3384
3388
|
compactToolbar = false,
|
|
3385
3389
|
submitTitle = "Send",
|
|
@@ -3658,6 +3662,7 @@ function TerminalInputBar({
|
|
|
3658
3662
|
})(),
|
|
3659
3663
|
/* @__PURE__ */ jsxs("div", { ref: toolbarRef, className: "flex items-center justify-between pt-2.5 gap-2", children: [
|
|
3660
3664
|
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5 text-[11px] min-w-0 flex-wrap", children: [
|
|
3665
|
+
leading,
|
|
3661
3666
|
chipsVisible && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
3662
3667
|
/* @__PURE__ */ jsx(
|
|
3663
3668
|
"button",
|
|
@@ -5538,6 +5543,7 @@ Path: ${parsed.endpoint.path}`);
|
|
|
5538
5543
|
core.setCommandPaletteItems([]);
|
|
5539
5544
|
},
|
|
5540
5545
|
inputBarExtra: slots?.inputBarExtra,
|
|
5546
|
+
leading: slots?.inputBarLeading,
|
|
5541
5547
|
modeSelector: slots?.modeSelector
|
|
5542
5548
|
}
|
|
5543
5549
|
),
|
|
@@ -6210,6 +6216,7 @@ function ChatTerminal({
|
|
|
6210
6216
|
core.setCommandPaletteItems([]);
|
|
6211
6217
|
},
|
|
6212
6218
|
inputBarExtra: slots?.inputBarExtra,
|
|
6219
|
+
leading: slots?.inputBarLeading,
|
|
6213
6220
|
modeSelector: slots?.modeSelector
|
|
6214
6221
|
}
|
|
6215
6222
|
),
|
|
@@ -7613,6 +7620,1245 @@ function agentStepTraceExtra(entry) {
|
|
|
7613
7620
|
const working = meta?.agentStepsLive ?? entry.content.endsWith("\u258B");
|
|
7614
7621
|
return /* @__PURE__ */ jsx(AgentStepTrace, { steps, working });
|
|
7615
7622
|
}
|
|
7623
|
+
var BAR_SHAPE = [0.55, 1, 0.8, 0.45];
|
|
7624
|
+
function tintOf(color, alpha) {
|
|
7625
|
+
return `color-mix(in srgb, ${color} ${Math.round(alpha * 100)}%, transparent)`;
|
|
7626
|
+
}
|
|
7627
|
+
function useCoarsePointer() {
|
|
7628
|
+
const [coarse, setCoarse] = useState(
|
|
7629
|
+
() => typeof window !== "undefined" && !!window.matchMedia?.("(pointer: coarse)").matches
|
|
7630
|
+
);
|
|
7631
|
+
useEffect(() => {
|
|
7632
|
+
const mq = window.matchMedia?.("(pointer: coarse)");
|
|
7633
|
+
if (!mq) return void 0;
|
|
7634
|
+
const on = () => setCoarse(mq.matches);
|
|
7635
|
+
mq.addEventListener?.("change", on);
|
|
7636
|
+
return () => mq.removeEventListener?.("change", on);
|
|
7637
|
+
}, []);
|
|
7638
|
+
return coarse;
|
|
7639
|
+
}
|
|
7640
|
+
function VoiceInputButton({
|
|
7641
|
+
state,
|
|
7642
|
+
onToggle,
|
|
7643
|
+
disabled = false,
|
|
7644
|
+
level,
|
|
7645
|
+
startTitle = "Talk",
|
|
7646
|
+
stopTitle = "End voice"
|
|
7647
|
+
}) {
|
|
7648
|
+
const t = useTheme();
|
|
7649
|
+
const coarse = useCoarsePointer();
|
|
7650
|
+
const isLight = t.mode ? t.mode === "light" : t.bgRaised === "#ffffff";
|
|
7651
|
+
const on = state !== "off";
|
|
7652
|
+
const tint = (a) => tintOf(t.primary, a);
|
|
7653
|
+
const bars = useRef([]);
|
|
7654
|
+
const levelRef = useRef(level);
|
|
7655
|
+
levelRef.current = level;
|
|
7656
|
+
useEffect(() => {
|
|
7657
|
+
if (state !== "live") return void 0;
|
|
7658
|
+
let raf = 0;
|
|
7659
|
+
let smooth = 0;
|
|
7660
|
+
const tick = (now) => {
|
|
7661
|
+
const raw = levelRef.current ? Math.min(1, Math.max(0, levelRef.current())) : 0;
|
|
7662
|
+
smooth = raw > smooth ? smooth + (raw - smooth) * 0.55 : smooth + (raw - smooth) * 0.12;
|
|
7663
|
+
bars.current.forEach((bar, i) => {
|
|
7664
|
+
if (!bar) return;
|
|
7665
|
+
const idle = 0.18 + 0.1 * Math.sin(now / 320 + i * 1.3);
|
|
7666
|
+
const h = Math.max(idle, smooth * BAR_SHAPE[i] * (0.85 + 0.15 * Math.sin(now / 90 + i * 2.1)));
|
|
7667
|
+
bar.style.transform = `scaleY(${Math.min(1, h).toFixed(3)})`;
|
|
7668
|
+
});
|
|
7669
|
+
raf = window.requestAnimationFrame(tick);
|
|
7670
|
+
};
|
|
7671
|
+
raf = window.requestAnimationFrame(tick);
|
|
7672
|
+
return () => window.cancelAnimationFrame(raf);
|
|
7673
|
+
}, [state]);
|
|
7674
|
+
const size = coarse ? 32 : 22;
|
|
7675
|
+
const title = on ? stopTitle : startTitle;
|
|
7676
|
+
const style = on ? {
|
|
7677
|
+
backgroundColor: t.primary,
|
|
7678
|
+
border: `1px solid ${t.primary}`,
|
|
7679
|
+
color: t.textInverse,
|
|
7680
|
+
boxShadow: `0 0 0 3px ${tint(0.18)}, 0 6px 18px -6px ${tint(0.75)}`
|
|
7681
|
+
} : {
|
|
7682
|
+
backgroundColor: isLight ? tint(0.07) : tint(0.1),
|
|
7683
|
+
border: `1px solid ${tint(isLight ? 0.35 : 0.3)}`,
|
|
7684
|
+
color: t.primary
|
|
7685
|
+
};
|
|
7686
|
+
return /* @__PURE__ */ jsxs(
|
|
7687
|
+
"button",
|
|
7688
|
+
{
|
|
7689
|
+
type: "button",
|
|
7690
|
+
disabled: disabled && !on,
|
|
7691
|
+
"aria-pressed": on,
|
|
7692
|
+
"aria-label": title,
|
|
7693
|
+
title,
|
|
7694
|
+
className: `relative flex items-center justify-center disabled:opacity-30 disabled:cursor-not-allowed transition-all duration-200 active:scale-95 ${coarse ? "rounded-full" : "rounded-lg px-1.5"}`,
|
|
7695
|
+
style: { ...style, height: size, minWidth: size, WebkitTapHighlightColor: "transparent" },
|
|
7696
|
+
onClick: onToggle,
|
|
7697
|
+
children: [
|
|
7698
|
+
state === "live" ? /* @__PURE__ */ jsx(
|
|
7699
|
+
"span",
|
|
7700
|
+
{
|
|
7701
|
+
"aria-hidden": "true",
|
|
7702
|
+
className: `pointer-events-none absolute inset-0 animate-ping ${coarse ? "rounded-full" : "rounded-lg"}`,
|
|
7703
|
+
style: { backgroundColor: tint(0.35), animationDuration: "1.8s" }
|
|
7704
|
+
}
|
|
7705
|
+
) : null,
|
|
7706
|
+
state === "connecting" ? /* @__PURE__ */ jsx(Loader2, { className: coarse ? "w-4 h-4 animate-spin" : "w-3.5 h-3.5 animate-spin" }) : state === "live" ? /* @__PURE__ */ jsx("span", { "aria-hidden": "true", className: "relative flex items-center gap-[2px]", style: { height: coarse ? 14 : 11 }, children: BAR_SHAPE.map((_, i) => /* @__PURE__ */ jsx(
|
|
7707
|
+
"span",
|
|
7708
|
+
{
|
|
7709
|
+
ref: (el) => {
|
|
7710
|
+
bars.current[i] = el;
|
|
7711
|
+
},
|
|
7712
|
+
className: "block rounded-full",
|
|
7713
|
+
style: {
|
|
7714
|
+
width: coarse ? 2.5 : 2,
|
|
7715
|
+
height: "100%",
|
|
7716
|
+
backgroundColor: "currentColor",
|
|
7717
|
+
transform: "scaleY(0.25)",
|
|
7718
|
+
transformOrigin: "center",
|
|
7719
|
+
transition: "transform 60ms linear"
|
|
7720
|
+
}
|
|
7721
|
+
},
|
|
7722
|
+
i
|
|
7723
|
+
)) }) : /* @__PURE__ */ jsx(Mic, { className: coarse ? "w-4 h-4" : "w-3.5 h-3.5" })
|
|
7724
|
+
]
|
|
7725
|
+
}
|
|
7726
|
+
);
|
|
7727
|
+
}
|
|
7728
|
+
|
|
7729
|
+
// src/voice/askQueue.ts
|
|
7730
|
+
var REPLACED_OUTPUT = "Replaced by their corrected question, which is being answered instead. Say nothing about this one.";
|
|
7731
|
+
var AskQueue = class {
|
|
7732
|
+
/**
|
|
7733
|
+
* `run` asks the host (resolves with what the voice speaks from; a
|
|
7734
|
+
* rejection is reported as a failure). `finish` sends the result: `speak`
|
|
7735
|
+
* false for a replaced question.
|
|
7736
|
+
*/
|
|
7737
|
+
constructor(run, finish) {
|
|
7738
|
+
this.run = run;
|
|
7739
|
+
this.finish = finish;
|
|
7740
|
+
this.waiting = [];
|
|
7741
|
+
this.running = null;
|
|
7742
|
+
}
|
|
7743
|
+
/** Questions being worked on or waiting. */
|
|
7744
|
+
get size() {
|
|
7745
|
+
return this.waiting.length + (this.running ? 1 : 0);
|
|
7746
|
+
}
|
|
7747
|
+
add(ask) {
|
|
7748
|
+
if (ask.replaces && this.size > 0) {
|
|
7749
|
+
for (const dropped of this.waiting) this.finish(dropped, REPLACED_OUTPUT, false);
|
|
7750
|
+
this.waiting = [ask];
|
|
7751
|
+
this.running?.controller.abort();
|
|
7752
|
+
} else {
|
|
7753
|
+
this.waiting.push(ask);
|
|
7754
|
+
}
|
|
7755
|
+
this.next();
|
|
7756
|
+
}
|
|
7757
|
+
next() {
|
|
7758
|
+
if (this.running) return;
|
|
7759
|
+
const ask = this.waiting.shift();
|
|
7760
|
+
if (!ask) return;
|
|
7761
|
+
const controller = new AbortController();
|
|
7762
|
+
this.running = { ask, controller };
|
|
7763
|
+
this.run(ask, controller.signal).catch((e) => `The assistant could not answer: ${e?.message || "unknown error"}. Tell them it failed; do not guess.`).then((output) => {
|
|
7764
|
+
this.running = null;
|
|
7765
|
+
if (controller.signal.aborted) this.finish(ask, REPLACED_OUTPUT, false);
|
|
7766
|
+
else this.finish(ask, output, true);
|
|
7767
|
+
this.next();
|
|
7768
|
+
});
|
|
7769
|
+
}
|
|
7770
|
+
};
|
|
7771
|
+
|
|
7772
|
+
// src/voice/pcm.ts
|
|
7773
|
+
var WIRE_RATE = 24e3;
|
|
7774
|
+
var FRAME_SAMPLES = 2400;
|
|
7775
|
+
var Downsampler = class {
|
|
7776
|
+
constructor(inputRate, outputRate = WIRE_RATE) {
|
|
7777
|
+
/** Input samples not yet consumed. */
|
|
7778
|
+
this.carry = new Float32Array(0);
|
|
7779
|
+
/** Fractional read position into `carry`. */
|
|
7780
|
+
this.pos = 0;
|
|
7781
|
+
if (!(inputRate > 0) || !(outputRate > 0)) throw new Error("Sample rates must be positive");
|
|
7782
|
+
this.ratio = inputRate / outputRate;
|
|
7783
|
+
}
|
|
7784
|
+
push(input) {
|
|
7785
|
+
const buf = new Float32Array(this.carry.length + input.length);
|
|
7786
|
+
buf.set(this.carry, 0);
|
|
7787
|
+
buf.set(input, this.carry.length);
|
|
7788
|
+
const out = [];
|
|
7789
|
+
let pos = this.pos;
|
|
7790
|
+
while (pos + this.ratio <= buf.length) {
|
|
7791
|
+
const start = Math.floor(pos);
|
|
7792
|
+
const end = Math.max(start + 1, Math.floor(pos + this.ratio));
|
|
7793
|
+
let sum = 0;
|
|
7794
|
+
for (let i = start; i < end; i += 1) sum += buf[i];
|
|
7795
|
+
out.push(floatToInt16(sum / (end - start)));
|
|
7796
|
+
pos += this.ratio;
|
|
7797
|
+
}
|
|
7798
|
+
const consumed = Math.floor(pos);
|
|
7799
|
+
this.carry = buf.slice(consumed);
|
|
7800
|
+
this.pos = pos - consumed;
|
|
7801
|
+
return Int16Array.from(out);
|
|
7802
|
+
}
|
|
7803
|
+
};
|
|
7804
|
+
function floatToInt16(sample) {
|
|
7805
|
+
const s = Math.max(-1, Math.min(1, Number.isFinite(sample) ? sample : 0));
|
|
7806
|
+
return s < 0 ? Math.round(s * 32768) : Math.round(s * 32767);
|
|
7807
|
+
}
|
|
7808
|
+
var FrameBuffer = class {
|
|
7809
|
+
constructor() {
|
|
7810
|
+
this.pending = [];
|
|
7811
|
+
}
|
|
7812
|
+
push(samples) {
|
|
7813
|
+
for (let i = 0; i < samples.length; i += 1) this.pending.push(samples[i]);
|
|
7814
|
+
const frames = [];
|
|
7815
|
+
while (this.pending.length >= FRAME_SAMPLES) {
|
|
7816
|
+
frames.push(toLittleEndian(this.pending.splice(0, FRAME_SAMPLES)));
|
|
7817
|
+
}
|
|
7818
|
+
return frames;
|
|
7819
|
+
}
|
|
7820
|
+
};
|
|
7821
|
+
function toLittleEndian(samples) {
|
|
7822
|
+
const view = new DataView(new ArrayBuffer(samples.length * 2));
|
|
7823
|
+
samples.forEach((s, i) => view.setInt16(i * 2, s, true));
|
|
7824
|
+
return view.buffer;
|
|
7825
|
+
}
|
|
7826
|
+
function pcm16ToFloat32(bytes) {
|
|
7827
|
+
const view = new DataView(bytes);
|
|
7828
|
+
const out = new Float32Array(Math.floor(bytes.byteLength / 2));
|
|
7829
|
+
for (let i = 0; i < out.length; i += 1) out[i] = view.getInt16(i * 2, true) / 32768;
|
|
7830
|
+
return out;
|
|
7831
|
+
}
|
|
7832
|
+
|
|
7833
|
+
// src/voice/persona.ts
|
|
7834
|
+
var VOICE_NAMES = [
|
|
7835
|
+
"alloy",
|
|
7836
|
+
"ash",
|
|
7837
|
+
"ballad",
|
|
7838
|
+
"cedar",
|
|
7839
|
+
"coral",
|
|
7840
|
+
"echo",
|
|
7841
|
+
"marin",
|
|
7842
|
+
"sage",
|
|
7843
|
+
"shimmer",
|
|
7844
|
+
"verse"
|
|
7845
|
+
];
|
|
7846
|
+
var VOICE_LANGUAGES = ["en", "es", "pt", "fr", "de", "it", "nl", "ar", "hi", "ja", "ko", "zh"];
|
|
7847
|
+
function encodePersona(persona) {
|
|
7848
|
+
if (!persona) return null;
|
|
7849
|
+
const fields = Object.fromEntries(Object.entries(persona).filter(([, v]) => v !== void 0));
|
|
7850
|
+
return Object.keys(fields).length ? JSON.stringify(fields) : null;
|
|
7851
|
+
}
|
|
7852
|
+
|
|
7853
|
+
// src/voice/screenAwake.ts
|
|
7854
|
+
function browserWakeLock() {
|
|
7855
|
+
return typeof navigator === "undefined" ? void 0 : navigator.wakeLock;
|
|
7856
|
+
}
|
|
7857
|
+
var ScreenAwake = class {
|
|
7858
|
+
constructor(api = browserWakeLock()) {
|
|
7859
|
+
this.api = api;
|
|
7860
|
+
this.sentinel = null;
|
|
7861
|
+
this.wanted = false;
|
|
7862
|
+
this.requesting = false;
|
|
7863
|
+
this.onVisibility = () => {
|
|
7864
|
+
if (document.visibilityState === "visible") void this.take();
|
|
7865
|
+
};
|
|
7866
|
+
}
|
|
7867
|
+
/** The screen is being kept on right now. */
|
|
7868
|
+
get held() {
|
|
7869
|
+
return !!this.sentinel && !this.sentinel.released;
|
|
7870
|
+
}
|
|
7871
|
+
/** Keep the screen on until `release`. Call it from the tap that starts
|
|
7872
|
+
* the session. */
|
|
7873
|
+
hold() {
|
|
7874
|
+
if (!this.api || this.wanted) return;
|
|
7875
|
+
this.wanted = true;
|
|
7876
|
+
document.addEventListener("visibilitychange", this.onVisibility);
|
|
7877
|
+
void this.take();
|
|
7878
|
+
}
|
|
7879
|
+
release() {
|
|
7880
|
+
if (!this.wanted) return;
|
|
7881
|
+
this.wanted = false;
|
|
7882
|
+
document.removeEventListener("visibilitychange", this.onVisibility);
|
|
7883
|
+
const sentinel = this.sentinel;
|
|
7884
|
+
this.sentinel = null;
|
|
7885
|
+
void sentinel?.release().catch(() => void 0);
|
|
7886
|
+
}
|
|
7887
|
+
async take() {
|
|
7888
|
+
if (!this.api || !this.wanted || this.held || this.requesting || document.visibilityState !== "visible") return;
|
|
7889
|
+
this.requesting = true;
|
|
7890
|
+
try {
|
|
7891
|
+
const sentinel = await this.api.request("screen");
|
|
7892
|
+
if (this.wanted) this.sentinel = sentinel;
|
|
7893
|
+
else void sentinel.release().catch(() => void 0);
|
|
7894
|
+
} catch {
|
|
7895
|
+
} finally {
|
|
7896
|
+
this.requesting = false;
|
|
7897
|
+
}
|
|
7898
|
+
}
|
|
7899
|
+
};
|
|
7900
|
+
|
|
7901
|
+
// src/voice/worklet.ts
|
|
7902
|
+
var VOICE_CAPTURE_WORKLET_SOURCE = `class YfVoiceCapture extends AudioWorkletProcessor {
|
|
7903
|
+
constructor() {
|
|
7904
|
+
super();
|
|
7905
|
+
this.batch = new Float32Array(2048);
|
|
7906
|
+
this.filled = 0;
|
|
7907
|
+
}
|
|
7908
|
+
process(inputs) {
|
|
7909
|
+
const channel = inputs[0] && inputs[0][0];
|
|
7910
|
+
if (channel) {
|
|
7911
|
+
let offset = 0;
|
|
7912
|
+
while (offset < channel.length) {
|
|
7913
|
+
const n = Math.min(channel.length - offset, this.batch.length - this.filled);
|
|
7914
|
+
this.batch.set(channel.subarray(offset, offset + n), this.filled);
|
|
7915
|
+
this.filled += n;
|
|
7916
|
+
offset += n;
|
|
7917
|
+
if (this.filled === this.batch.length) {
|
|
7918
|
+
this.port.postMessage(this.batch, [this.batch.buffer]);
|
|
7919
|
+
this.batch = new Float32Array(2048);
|
|
7920
|
+
this.filled = 0;
|
|
7921
|
+
}
|
|
7922
|
+
}
|
|
7923
|
+
}
|
|
7924
|
+
return true;
|
|
7925
|
+
}
|
|
7926
|
+
}
|
|
7927
|
+
registerProcessor('yf-voice-capture', YfVoiceCapture);
|
|
7928
|
+
`;
|
|
7929
|
+
var VOICE_CAPTURE_PROCESSOR = "yf-voice-capture";
|
|
7930
|
+
|
|
7931
|
+
// src/voice/session.ts
|
|
7932
|
+
function socketBase(base) {
|
|
7933
|
+
return base.replace(/^http/i, "ws").replace(/\/+$/, "");
|
|
7934
|
+
}
|
|
7935
|
+
var OFF = { enabled: false, blocked: null, maxSessionSecs: 0, persona: null };
|
|
7936
|
+
async function getVoiceStatus(connection, signal) {
|
|
7937
|
+
const token = await connection.getToken();
|
|
7938
|
+
if (!token) return OFF;
|
|
7939
|
+
try {
|
|
7940
|
+
const res = await fetch(`${connection.baseUrl.replace(/\/+$/, "")}/voice/status`, {
|
|
7941
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
7942
|
+
signal
|
|
7943
|
+
});
|
|
7944
|
+
if (!res.ok) return OFF;
|
|
7945
|
+
const body = await res.json();
|
|
7946
|
+
return {
|
|
7947
|
+
enabled: body?.enabled === true,
|
|
7948
|
+
blocked: typeof body?.blocked === "string" ? body.blocked : null,
|
|
7949
|
+
maxSessionSecs: Number(body?.max_session_secs) || 0,
|
|
7950
|
+
persona: body?.persona && typeof body.persona.name === "string" ? body.persona : null
|
|
7951
|
+
};
|
|
7952
|
+
} catch {
|
|
7953
|
+
return OFF;
|
|
7954
|
+
}
|
|
7955
|
+
}
|
|
7956
|
+
var SILENT = { input: 0, output: 0 };
|
|
7957
|
+
function rms(analyser, buf) {
|
|
7958
|
+
if (!analyser || !buf) return 0;
|
|
7959
|
+
analyser.getFloatTimeDomainData(buf);
|
|
7960
|
+
let sum = 0;
|
|
7961
|
+
for (let i = 0; i < buf.length; i += 1) sum += buf[i] * buf[i];
|
|
7962
|
+
return Math.sqrt(sum / buf.length);
|
|
7963
|
+
}
|
|
7964
|
+
var PcmPlayer = class {
|
|
7965
|
+
constructor(ctx, out, onIdle) {
|
|
7966
|
+
this.ctx = ctx;
|
|
7967
|
+
this.out = out;
|
|
7968
|
+
this.onIdle = onIdle;
|
|
7969
|
+
this.next = 0;
|
|
7970
|
+
this.live = /* @__PURE__ */ new Set();
|
|
7971
|
+
}
|
|
7972
|
+
get playing() {
|
|
7973
|
+
return this.live.size > 0;
|
|
7974
|
+
}
|
|
7975
|
+
enqueue(bytes) {
|
|
7976
|
+
const samples = pcm16ToFloat32(bytes);
|
|
7977
|
+
if (samples.length === 0) return;
|
|
7978
|
+
const buffer = this.ctx.createBuffer(1, samples.length, WIRE_RATE);
|
|
7979
|
+
buffer.getChannelData(0).set(samples);
|
|
7980
|
+
const source = this.ctx.createBufferSource();
|
|
7981
|
+
source.buffer = buffer;
|
|
7982
|
+
source.connect(this.out);
|
|
7983
|
+
const at = Math.max(this.ctx.currentTime + 0.02, this.next);
|
|
7984
|
+
source.start(at);
|
|
7985
|
+
this.next = at + buffer.duration;
|
|
7986
|
+
this.live.add(source);
|
|
7987
|
+
source.onended = () => {
|
|
7988
|
+
this.live.delete(source);
|
|
7989
|
+
if (this.live.size === 0) this.onIdle();
|
|
7990
|
+
};
|
|
7991
|
+
}
|
|
7992
|
+
stop() {
|
|
7993
|
+
this.live.forEach((source) => {
|
|
7994
|
+
source.onended = null;
|
|
7995
|
+
try {
|
|
7996
|
+
source.stop();
|
|
7997
|
+
} catch {
|
|
7998
|
+
}
|
|
7999
|
+
});
|
|
8000
|
+
this.live.clear();
|
|
8001
|
+
this.next = 0;
|
|
8002
|
+
}
|
|
8003
|
+
};
|
|
8004
|
+
var FINISH_GRACE_MS = 800;
|
|
8005
|
+
var FINISH_MAX_WAIT_MS = 15e3;
|
|
8006
|
+
var CLOSE_REASONS = {
|
|
8007
|
+
time_limit: "time_limit",
|
|
8008
|
+
replaced: "replaced",
|
|
8009
|
+
upstream_closed: "lost",
|
|
8010
|
+
upstream_error: "lost"
|
|
8011
|
+
};
|
|
8012
|
+
function voiceSocketUrl(connection, token) {
|
|
8013
|
+
const params = new URLSearchParams({ access_token: token });
|
|
8014
|
+
if (connection.threadId) params.set("thread_id", connection.threadId);
|
|
8015
|
+
if (connection.workingGroupId) params.set("working_group_id", connection.workingGroupId);
|
|
8016
|
+
const persona = encodePersona(connection.persona);
|
|
8017
|
+
if (persona) params.set("persona", persona);
|
|
8018
|
+
return `${socketBase(connection.baseUrl)}/voice/realtime?${params.toString()}`;
|
|
8019
|
+
}
|
|
8020
|
+
var VoiceSession = class {
|
|
8021
|
+
constructor(h, connection) {
|
|
8022
|
+
this.h = h;
|
|
8023
|
+
this.connection = connection;
|
|
8024
|
+
this.ctx = null;
|
|
8025
|
+
this.stream = null;
|
|
8026
|
+
this.node = null;
|
|
8027
|
+
this.ws = null;
|
|
8028
|
+
this.player = null;
|
|
8029
|
+
this.inputAnalyser = null;
|
|
8030
|
+
this.outputAnalyser = null;
|
|
8031
|
+
this.inputBuf = null;
|
|
8032
|
+
this.outputBuf = null;
|
|
8033
|
+
this.ready = false;
|
|
8034
|
+
this.ended = false;
|
|
8035
|
+
this.phase = null;
|
|
8036
|
+
/** Keeps a phone from locking mid-conversation (see `screenAwake.ts`). */
|
|
8037
|
+
this.awake = new ScreenAwake();
|
|
8038
|
+
/** The voice's questions to the host, one at a time (see `askQueue.ts`). */
|
|
8039
|
+
this.asks = new AskQueue(
|
|
8040
|
+
(ask, signal) => {
|
|
8041
|
+
let open = true;
|
|
8042
|
+
const progress = (line, kind) => {
|
|
8043
|
+
if (open && !signal.aborted && line.trim()) this.send({ type: "progress", kind, text: line.slice(0, 240) });
|
|
8044
|
+
};
|
|
8045
|
+
return this.h.onAsk(ask.question, ask.heard, ask.utterance, progress, signal).finally(() => {
|
|
8046
|
+
open = false;
|
|
8047
|
+
});
|
|
8048
|
+
},
|
|
8049
|
+
(ask, output, speak) => {
|
|
8050
|
+
if (this.ended) return;
|
|
8051
|
+
this.send({ type: "tool_result", call_id: ask.callId, output, ...speak ? {} : { speak: false } });
|
|
8052
|
+
this.settle();
|
|
8053
|
+
}
|
|
8054
|
+
);
|
|
8055
|
+
this.heard = null;
|
|
8056
|
+
this.partialHeard = "";
|
|
8057
|
+
this.saying = "";
|
|
8058
|
+
this.endingReason = null;
|
|
8059
|
+
this.finishTimer = null;
|
|
8060
|
+
this.muted = false;
|
|
8061
|
+
this.blobUrl = null;
|
|
8062
|
+
}
|
|
8063
|
+
/** Call from the tap itself: iOS only lets audio start inside a gesture,
|
|
8064
|
+
* so the AudioContext is created before the first `await`. */
|
|
8065
|
+
async start() {
|
|
8066
|
+
const Ctx = window.AudioContext ?? window.webkitAudioContext;
|
|
8067
|
+
if (!Ctx || !navigator.mediaDevices?.getUserMedia || typeof AudioWorkletNode === "undefined") {
|
|
8068
|
+
this.end("unavailable", "This browser cannot do live voice.");
|
|
8069
|
+
return;
|
|
8070
|
+
}
|
|
8071
|
+
const ctx = new Ctx();
|
|
8072
|
+
this.ctx = ctx;
|
|
8073
|
+
void ctx.resume();
|
|
8074
|
+
this.awake.hold();
|
|
8075
|
+
this.setPhase("connecting");
|
|
8076
|
+
const status = await getVoiceStatus(this.connection);
|
|
8077
|
+
if (this.ended) return;
|
|
8078
|
+
if (status.blocked) {
|
|
8079
|
+
this.end("blocked", status.blocked);
|
|
8080
|
+
return;
|
|
8081
|
+
}
|
|
8082
|
+
if (!status.enabled) {
|
|
8083
|
+
this.end("unavailable", "Voice is not switched on.");
|
|
8084
|
+
return;
|
|
8085
|
+
}
|
|
8086
|
+
try {
|
|
8087
|
+
this.stream = await navigator.mediaDevices.getUserMedia({
|
|
8088
|
+
audio: { channelCount: 1, echoCancellation: true, noiseSuppression: true, autoGainControl: true }
|
|
8089
|
+
});
|
|
8090
|
+
this.applyMute();
|
|
8091
|
+
} catch (e) {
|
|
8092
|
+
this.end("mic_denied", e?.message);
|
|
8093
|
+
return;
|
|
8094
|
+
}
|
|
8095
|
+
if (this.ended) {
|
|
8096
|
+
this.release();
|
|
8097
|
+
return;
|
|
8098
|
+
}
|
|
8099
|
+
try {
|
|
8100
|
+
let url = this.connection.workletUrl;
|
|
8101
|
+
if (!url) {
|
|
8102
|
+
this.blobUrl = URL.createObjectURL(new Blob([VOICE_CAPTURE_WORKLET_SOURCE], { type: "application/javascript" }));
|
|
8103
|
+
url = this.blobUrl;
|
|
8104
|
+
}
|
|
8105
|
+
await ctx.audioWorklet.addModule(url);
|
|
8106
|
+
} catch (e) {
|
|
8107
|
+
this.end("unavailable", e?.message);
|
|
8108
|
+
return;
|
|
8109
|
+
}
|
|
8110
|
+
if (this.ended) return;
|
|
8111
|
+
const down = new Downsampler(ctx.sampleRate);
|
|
8112
|
+
const frames = new FrameBuffer();
|
|
8113
|
+
const node = new AudioWorkletNode(ctx, VOICE_CAPTURE_PROCESSOR);
|
|
8114
|
+
this.node = node;
|
|
8115
|
+
node.port.onmessage = (e) => {
|
|
8116
|
+
const ws2 = this.ws;
|
|
8117
|
+
if (!this.ready || !ws2 || ws2.readyState !== WebSocket.OPEN) return;
|
|
8118
|
+
for (const frame of frames.push(down.push(e.data))) ws2.send(frame);
|
|
8119
|
+
};
|
|
8120
|
+
const mic = ctx.createMediaStreamSource(this.stream);
|
|
8121
|
+
const inputAnalyser = ctx.createAnalyser();
|
|
8122
|
+
inputAnalyser.fftSize = 1024;
|
|
8123
|
+
const outputAnalyser = ctx.createAnalyser();
|
|
8124
|
+
outputAnalyser.fftSize = 1024;
|
|
8125
|
+
this.inputAnalyser = inputAnalyser;
|
|
8126
|
+
this.outputAnalyser = outputAnalyser;
|
|
8127
|
+
this.inputBuf = new Float32Array(inputAnalyser.fftSize);
|
|
8128
|
+
this.outputBuf = new Float32Array(outputAnalyser.fftSize);
|
|
8129
|
+
const mute = ctx.createGain();
|
|
8130
|
+
mute.gain.value = 0;
|
|
8131
|
+
mic.connect(inputAnalyser);
|
|
8132
|
+
inputAnalyser.connect(node);
|
|
8133
|
+
node.connect(mute);
|
|
8134
|
+
mute.connect(ctx.destination);
|
|
8135
|
+
outputAnalyser.connect(ctx.destination);
|
|
8136
|
+
this.player = new PcmPlayer(ctx, outputAnalyser, () => this.settle());
|
|
8137
|
+
const token = await this.connection.getToken();
|
|
8138
|
+
if (this.ended) return;
|
|
8139
|
+
if (!token) {
|
|
8140
|
+
this.end("unavailable", "Sign in again to use voice.");
|
|
8141
|
+
return;
|
|
8142
|
+
}
|
|
8143
|
+
const ws = new WebSocket(voiceSocketUrl(this.connection, token));
|
|
8144
|
+
ws.binaryType = "arraybuffer";
|
|
8145
|
+
this.ws = ws;
|
|
8146
|
+
ws.onmessage = (e) => {
|
|
8147
|
+
if (typeof e.data === "string") this.onEvent(e.data);
|
|
8148
|
+
else this.onAudio(e.data);
|
|
8149
|
+
};
|
|
8150
|
+
ws.onclose = (e) => {
|
|
8151
|
+
if (this.ended) return;
|
|
8152
|
+
if (!this.ready) {
|
|
8153
|
+
this.end("unavailable");
|
|
8154
|
+
return;
|
|
8155
|
+
}
|
|
8156
|
+
this.end(this.endingReason ?? CLOSE_REASONS[e.reason] ?? "lost");
|
|
8157
|
+
};
|
|
8158
|
+
}
|
|
8159
|
+
stop() {
|
|
8160
|
+
this.end("stopped");
|
|
8161
|
+
}
|
|
8162
|
+
/**
|
|
8163
|
+
* Mute the microphone, or not. The track goes silent rather than the
|
|
8164
|
+
* stream stopping: the service keeps hearing (silence), so whatever the
|
|
8165
|
+
* person was saying ends as a turn and nothing hangs half-heard. The
|
|
8166
|
+
* voice can still talk while they are muted.
|
|
8167
|
+
*/
|
|
8168
|
+
setMuted(muted) {
|
|
8169
|
+
this.muted = muted;
|
|
8170
|
+
this.applyMute();
|
|
8171
|
+
}
|
|
8172
|
+
get isMuted() {
|
|
8173
|
+
return this.muted;
|
|
8174
|
+
}
|
|
8175
|
+
applyMute() {
|
|
8176
|
+
this.stream?.getAudioTracks().forEach((track) => {
|
|
8177
|
+
track.enabled = !this.muted;
|
|
8178
|
+
});
|
|
8179
|
+
}
|
|
8180
|
+
/** Cheap enough to call every animation frame. */
|
|
8181
|
+
levels() {
|
|
8182
|
+
if (this.ended) return SILENT;
|
|
8183
|
+
return {
|
|
8184
|
+
input: rms(this.inputAnalyser, this.inputBuf),
|
|
8185
|
+
output: this.player?.playing ? rms(this.outputAnalyser, this.outputBuf) : 0
|
|
8186
|
+
};
|
|
8187
|
+
}
|
|
8188
|
+
setPhase(phase) {
|
|
8189
|
+
if (this.ended || this.phase === phase) return;
|
|
8190
|
+
this.phase = phase;
|
|
8191
|
+
this.h.onPhase(phase);
|
|
8192
|
+
}
|
|
8193
|
+
settle() {
|
|
8194
|
+
if (this.asks.size > 0) this.setPhase("thinking");
|
|
8195
|
+
else if (this.player?.playing) this.setPhase("speaking");
|
|
8196
|
+
else this.setPhase("listening");
|
|
8197
|
+
}
|
|
8198
|
+
send(message) {
|
|
8199
|
+
if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(JSON.stringify(message));
|
|
8200
|
+
}
|
|
8201
|
+
onAudio(bytes) {
|
|
8202
|
+
if (this.ended || !this.player) return;
|
|
8203
|
+
this.player.enqueue(bytes);
|
|
8204
|
+
this.setPhase("speaking");
|
|
8205
|
+
}
|
|
8206
|
+
onEvent(raw) {
|
|
8207
|
+
let ev;
|
|
8208
|
+
try {
|
|
8209
|
+
ev = JSON.parse(raw);
|
|
8210
|
+
} catch {
|
|
8211
|
+
return;
|
|
8212
|
+
}
|
|
8213
|
+
const text = (k) => typeof ev[k] === "string" ? ev[k] : "";
|
|
8214
|
+
switch (ev.type) {
|
|
8215
|
+
case "session.ready":
|
|
8216
|
+
this.ready = true;
|
|
8217
|
+
this.setPhase("listening");
|
|
8218
|
+
break;
|
|
8219
|
+
case "input_audio_buffer.speech_started":
|
|
8220
|
+
this.cancelFinish();
|
|
8221
|
+
this.player?.stop();
|
|
8222
|
+
this.saying = "";
|
|
8223
|
+
this.partialHeard = "";
|
|
8224
|
+
this.heard = null;
|
|
8225
|
+
this.setPhase("hearing");
|
|
8226
|
+
break;
|
|
8227
|
+
case "input_audio_buffer.speech_stopped":
|
|
8228
|
+
this.setPhase("thinking");
|
|
8229
|
+
break;
|
|
8230
|
+
case "conversation.item.input_audio_transcription.delta":
|
|
8231
|
+
this.partialHeard += text("delta");
|
|
8232
|
+
this.h.onHeard(this.partialHeard, false);
|
|
8233
|
+
break;
|
|
8234
|
+
case "conversation.item.input_audio_transcription.completed":
|
|
8235
|
+
this.heard = text("transcript").trim() || null;
|
|
8236
|
+
this.partialHeard = "";
|
|
8237
|
+
this.h.onHeard(this.heard ?? "", true);
|
|
8238
|
+
if (this.heard && text("message_id")) {
|
|
8239
|
+
this.h.onUtterance?.({ itemId: text("item_id"), messageId: text("message_id"), text: this.heard });
|
|
8240
|
+
}
|
|
8241
|
+
break;
|
|
8242
|
+
case "voice.reply":
|
|
8243
|
+
if (text("message_id") && text("text")) {
|
|
8244
|
+
this.h.onReply?.({
|
|
8245
|
+
messageId: text("message_id"),
|
|
8246
|
+
itemId: text("item_id") || null,
|
|
8247
|
+
text: text("text"),
|
|
8248
|
+
interrupted: ev.interrupted === true
|
|
8249
|
+
});
|
|
8250
|
+
}
|
|
8251
|
+
break;
|
|
8252
|
+
case "response.audio_transcript.delta":
|
|
8253
|
+
this.saying += text("delta");
|
|
8254
|
+
this.h.onSaying(this.saying, false);
|
|
8255
|
+
break;
|
|
8256
|
+
case "response.audio_transcript.done":
|
|
8257
|
+
this.h.onSaying(text("transcript") || this.saying, true);
|
|
8258
|
+
this.saying = "";
|
|
8259
|
+
break;
|
|
8260
|
+
case "tool_call":
|
|
8261
|
+
this.asks.add({
|
|
8262
|
+
callId: text("call_id"),
|
|
8263
|
+
question: text("question"),
|
|
8264
|
+
replaces: ev.replaces_earlier === true,
|
|
8265
|
+
heard: this.heard,
|
|
8266
|
+
utterance: text("message_id") ? { itemId: text("item_id"), messageId: text("message_id") } : null
|
|
8267
|
+
});
|
|
8268
|
+
this.setPhase("thinking");
|
|
8269
|
+
break;
|
|
8270
|
+
case "response.done":
|
|
8271
|
+
if (!this.player?.playing) this.settle();
|
|
8272
|
+
break;
|
|
8273
|
+
case "session.ending":
|
|
8274
|
+
this.endingReason = CLOSE_REASONS[text("reason")] ?? "lost";
|
|
8275
|
+
break;
|
|
8276
|
+
case "session.finish":
|
|
8277
|
+
this.finishAfterGoodbye();
|
|
8278
|
+
break;
|
|
8279
|
+
case "error":
|
|
8280
|
+
console.warn("[voice]", ev.code, ev.message);
|
|
8281
|
+
break;
|
|
8282
|
+
}
|
|
8283
|
+
}
|
|
8284
|
+
/** They are done: end once the goodbye has finished playing. */
|
|
8285
|
+
finishAfterGoodbye() {
|
|
8286
|
+
this.cancelFinish();
|
|
8287
|
+
const deadline = Date.now() + FINISH_MAX_WAIT_MS;
|
|
8288
|
+
const check = () => {
|
|
8289
|
+
if (this.ended) return;
|
|
8290
|
+
if (this.player?.playing && Date.now() < deadline) {
|
|
8291
|
+
this.finishTimer = setTimeout(check, 200);
|
|
8292
|
+
return;
|
|
8293
|
+
}
|
|
8294
|
+
this.finishTimer = setTimeout(() => this.end("finished"), FINISH_GRACE_MS);
|
|
8295
|
+
};
|
|
8296
|
+
check();
|
|
8297
|
+
}
|
|
8298
|
+
cancelFinish() {
|
|
8299
|
+
if (this.finishTimer) clearTimeout(this.finishTimer);
|
|
8300
|
+
this.finishTimer = null;
|
|
8301
|
+
}
|
|
8302
|
+
release() {
|
|
8303
|
+
this.awake.release();
|
|
8304
|
+
this.node?.port.close();
|
|
8305
|
+
this.node?.disconnect();
|
|
8306
|
+
this.stream?.getTracks().forEach((t) => t.stop());
|
|
8307
|
+
this.player?.stop();
|
|
8308
|
+
void this.ctx?.close().catch(() => void 0);
|
|
8309
|
+
if (this.blobUrl) URL.revokeObjectURL(this.blobUrl);
|
|
8310
|
+
this.blobUrl = null;
|
|
8311
|
+
this.inputAnalyser = null;
|
|
8312
|
+
this.outputAnalyser = null;
|
|
8313
|
+
this.node = null;
|
|
8314
|
+
this.stream = null;
|
|
8315
|
+
this.ctx = null;
|
|
8316
|
+
}
|
|
8317
|
+
end(reason, detail) {
|
|
8318
|
+
if (this.ended) return;
|
|
8319
|
+
this.ended = true;
|
|
8320
|
+
this.cancelFinish();
|
|
8321
|
+
const ws = this.ws;
|
|
8322
|
+
this.ws = null;
|
|
8323
|
+
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) ws.close(1e3);
|
|
8324
|
+
this.release();
|
|
8325
|
+
this.h.onEnd(reason, detail);
|
|
8326
|
+
}
|
|
8327
|
+
};
|
|
8328
|
+
|
|
8329
|
+
// src/voice/voiceprint.ts
|
|
8330
|
+
var GLYPHS = "\uFF71\uFF72\uFF73\uFF74\uFF75\uFF76\uFF77\uFF78\uFF79\uFF7A\uFF7B\uFF7C\uFF7D\uFF7E\uFF7F\uFF80\uFF81\uFF82\uFF83\uFF84\uFF85\uFF86\uFF87\uFF88\uFF89\uFF8A\uFF8B\uFF8C\uFF8D\uFF8E\uFF8F\uFF90\uFF91\uFF92\uFF93\uFF94\uFF95\uFF96\uFF97\uFF98\uFF99\uFF9A\uFF9B\uFF9C\uFF9D01".split("");
|
|
8331
|
+
var STEP_MS = 42;
|
|
8332
|
+
function loudness(rms2) {
|
|
8333
|
+
const v = Math.max(0, rms2 - 4e-3);
|
|
8334
|
+
return Math.min(1, Math.sqrt(v * 7));
|
|
8335
|
+
}
|
|
8336
|
+
function nextColumn(levels, phase, t, seed) {
|
|
8337
|
+
const you = loudness(levels.input);
|
|
8338
|
+
const javier = loudness(levels.output);
|
|
8339
|
+
if (javier > 0.05 && javier >= you * 0.8) return { amp: javier, voice: 2 /* Javier */, seed };
|
|
8340
|
+
if (you > 0.12) return { amp: you, voice: 1 /* You */, seed };
|
|
8341
|
+
if (phase === "thinking" || phase === "connecting") {
|
|
8342
|
+
const swell = 0.24 + 0.17 * Math.sin(t / 170) * Math.sin(t / 530);
|
|
8343
|
+
return { amp: Math.max(0.06, swell), voice: 3 /* Working */, seed };
|
|
8344
|
+
}
|
|
8345
|
+
return { amp: 0.06 + 0.035 * Math.sin(t / 650), voice: 0 /* Quiet */, seed };
|
|
8346
|
+
}
|
|
8347
|
+
function cellIntensity(amp, rows, row) {
|
|
8348
|
+
const half = rows / 2;
|
|
8349
|
+
const d = Math.abs(row + 0.5 - half) / half;
|
|
8350
|
+
const reach = Math.max(amp, 0);
|
|
8351
|
+
if (reach <= 0 || d > reach) return 0;
|
|
8352
|
+
return 0.5 + 0.5 * Math.pow(1 - d / reach, 0.8);
|
|
8353
|
+
}
|
|
8354
|
+
function hash(a, b) {
|
|
8355
|
+
let h = Math.imul(a | 0, 374761393) + Math.imul(b | 0, 668265263) | 0;
|
|
8356
|
+
h = Math.imul(h ^ h >>> 13, 1274126177);
|
|
8357
|
+
return (h ^ h >>> 16) >>> 0;
|
|
8358
|
+
}
|
|
8359
|
+
function glyphFor(seed, row) {
|
|
8360
|
+
return GLYPHS[hash(seed, row) % GLYPHS.length];
|
|
8361
|
+
}
|
|
8362
|
+
function ageFade(x, width) {
|
|
8363
|
+
const span = width * 0.42;
|
|
8364
|
+
if (span <= 0) return 1;
|
|
8365
|
+
const k = Math.min(1, Math.max(0, x / span));
|
|
8366
|
+
return k * k * (3 - 2 * k);
|
|
8367
|
+
}
|
|
8368
|
+
function clock(ms) {
|
|
8369
|
+
const s = Math.max(0, Math.floor(ms / 1e3));
|
|
8370
|
+
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
|
8371
|
+
}
|
|
8372
|
+
function captionTail(text, max) {
|
|
8373
|
+
const t = text.trim();
|
|
8374
|
+
if (t.length <= max) return t;
|
|
8375
|
+
const cut = t.slice(t.length - max);
|
|
8376
|
+
const space = cut.indexOf(" ");
|
|
8377
|
+
return `\u2026${space > 0 && space < 24 ? cut.slice(space + 1) : cut}`;
|
|
8378
|
+
}
|
|
8379
|
+
function parseCssColor(value, fallback) {
|
|
8380
|
+
const v = value.trim();
|
|
8381
|
+
const srgb = /^color\(srgb\s+([\d.]+)\s+([\d.]+)\s+([\d.]+)/i.exec(v);
|
|
8382
|
+
if (srgb) return [srgb[1], srgb[2], srgb[3]].map((n) => Math.round(Math.min(1, Math.max(0, Number(n))) * 255));
|
|
8383
|
+
const rgb = /^rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)/i.exec(v);
|
|
8384
|
+
if (rgb) return [Number(rgb[1]), Number(rgb[2]), Number(rgb[3])].map((n) => Math.round(n));
|
|
8385
|
+
return fallback;
|
|
8386
|
+
}
|
|
8387
|
+
|
|
8388
|
+
// src/voice/useVoiceSession.ts
|
|
8389
|
+
function useVoiceSession(host, connection) {
|
|
8390
|
+
const [available, setAvailable] = useState(false);
|
|
8391
|
+
const [defaultName, setDefaultName] = useState(null);
|
|
8392
|
+
const [maxSessionSecs, setMaxSessionSecs] = useState(0);
|
|
8393
|
+
const [phase, setPhase] = useState("off");
|
|
8394
|
+
const [heard, setHeard] = useState("");
|
|
8395
|
+
const [saying, setSaying] = useState("");
|
|
8396
|
+
const [ended, setEnded] = useState(null);
|
|
8397
|
+
const [startedAt, setStartedAt] = useState(null);
|
|
8398
|
+
const [muted, setMuted] = useState(false);
|
|
8399
|
+
const session = useRef(null);
|
|
8400
|
+
const hostRef = useRef(host);
|
|
8401
|
+
hostRef.current = host;
|
|
8402
|
+
const connectionRef = useRef(connection);
|
|
8403
|
+
connectionRef.current = connection;
|
|
8404
|
+
const { baseUrl } = connection;
|
|
8405
|
+
useEffect(() => {
|
|
8406
|
+
const controller = new AbortController();
|
|
8407
|
+
void getVoiceStatus(connectionRef.current, controller.signal).then((s) => {
|
|
8408
|
+
if (controller.signal.aborted) return;
|
|
8409
|
+
setAvailable(s.enabled);
|
|
8410
|
+
setMaxSessionSecs(s.maxSessionSecs);
|
|
8411
|
+
setDefaultName(s.persona?.name ?? null);
|
|
8412
|
+
});
|
|
8413
|
+
return () => controller.abort();
|
|
8414
|
+
}, [baseUrl]);
|
|
8415
|
+
const start = useCallback(() => {
|
|
8416
|
+
if (session.current) return;
|
|
8417
|
+
setEnded(null);
|
|
8418
|
+
setHeard("");
|
|
8419
|
+
setSaying("");
|
|
8420
|
+
setStartedAt(null);
|
|
8421
|
+
setMuted(false);
|
|
8422
|
+
const s = new VoiceSession(
|
|
8423
|
+
{
|
|
8424
|
+
onPhase: (p) => {
|
|
8425
|
+
setPhase(p);
|
|
8426
|
+
if (p !== "connecting") setStartedAt((at) => at ?? Date.now());
|
|
8427
|
+
},
|
|
8428
|
+
onHeard: (text) => setHeard(text),
|
|
8429
|
+
onSaying: (text) => setSaying(text),
|
|
8430
|
+
onAsk: (question, h, utterance, progress, signal) => hostRef.current.onAsk(question, h, utterance, progress, signal),
|
|
8431
|
+
onUtterance: (u) => hostRef.current.onUtterance?.(u),
|
|
8432
|
+
onReply: (r) => hostRef.current.onReply?.(r),
|
|
8433
|
+
onEnd: (reason, detail) => {
|
|
8434
|
+
if (session.current === s) session.current = null;
|
|
8435
|
+
setMuted(false);
|
|
8436
|
+
setPhase("off");
|
|
8437
|
+
setStartedAt(null);
|
|
8438
|
+
setEnded(reason === "stopped" || reason === "finished" ? null : { reason, detail });
|
|
8439
|
+
}
|
|
8440
|
+
},
|
|
8441
|
+
{ ...connectionRef.current }
|
|
8442
|
+
);
|
|
8443
|
+
session.current = s;
|
|
8444
|
+
void s.start();
|
|
8445
|
+
}, []);
|
|
8446
|
+
const stop = useCallback(() => session.current?.stop(), []);
|
|
8447
|
+
const toggle = useCallback(() => session.current ? stop() : start(), [start, stop]);
|
|
8448
|
+
const dismiss = useCallback(() => setEnded(null), []);
|
|
8449
|
+
const toggleMute = useCallback(() => {
|
|
8450
|
+
const s = session.current;
|
|
8451
|
+
if (!s) return;
|
|
8452
|
+
s.setMuted(!s.isMuted);
|
|
8453
|
+
setMuted(s.isMuted);
|
|
8454
|
+
}, []);
|
|
8455
|
+
const levels = useCallback(() => session.current?.levels() ?? SILENT, []);
|
|
8456
|
+
const level = useCallback(() => {
|
|
8457
|
+
const l = levels();
|
|
8458
|
+
return Math.max(loudness(l.input), loudness(l.output));
|
|
8459
|
+
}, [levels]);
|
|
8460
|
+
useEffect(() => () => session.current?.stop(), []);
|
|
8461
|
+
return {
|
|
8462
|
+
available,
|
|
8463
|
+
defaultName,
|
|
8464
|
+
phase,
|
|
8465
|
+
heard,
|
|
8466
|
+
saying,
|
|
8467
|
+
ended,
|
|
8468
|
+
startedAt,
|
|
8469
|
+
maxSessionSecs,
|
|
8470
|
+
levels,
|
|
8471
|
+
level,
|
|
8472
|
+
buttonState: phase === "off" ? "off" : phase === "connecting" ? "connecting" : "live",
|
|
8473
|
+
muted,
|
|
8474
|
+
toggleMute,
|
|
8475
|
+
start,
|
|
8476
|
+
stop,
|
|
8477
|
+
toggle,
|
|
8478
|
+
dismiss
|
|
8479
|
+
};
|
|
8480
|
+
}
|
|
8481
|
+
function voiceEndedMessage(ended, name = "the assistant") {
|
|
8482
|
+
if (!ended) return null;
|
|
8483
|
+
switch (ended.reason) {
|
|
8484
|
+
case "time_limit":
|
|
8485
|
+
return "Voice ends after a while to keep costs in check. Tap the microphone to keep talking.";
|
|
8486
|
+
case "replaced":
|
|
8487
|
+
return "Voice moved to your other window.";
|
|
8488
|
+
case "blocked":
|
|
8489
|
+
return ended.detail === "card_required" ? "Add a card to use voice." : ended.detail === "credit_exhausted" ? "You are out of credit. Top up to use voice." : "Voice needs an active billing plan.";
|
|
8490
|
+
case "mic_denied":
|
|
8491
|
+
return `Allow microphone access to talk to ${name}.`;
|
|
8492
|
+
case "unavailable":
|
|
8493
|
+
return ended.detail && !/^[a-z_]+$/.test(ended.detail) ? ended.detail : "Voice could not connect. Try again in a moment.";
|
|
8494
|
+
case "lost":
|
|
8495
|
+
return "Voice disconnected. Tap the microphone to reconnect.";
|
|
8496
|
+
default:
|
|
8497
|
+
return null;
|
|
8498
|
+
}
|
|
8499
|
+
}
|
|
8500
|
+
|
|
8501
|
+
// src/voice/styles.ts
|
|
8502
|
+
var VOICE_DECK_CSS = `
|
|
8503
|
+
@property --yf-voice-angle { syntax: '<angle>'; inherits: false; initial-value: 0deg; }
|
|
8504
|
+
@keyframes yf-voice-orbit { to { --yf-voice-angle: 360deg; } }
|
|
8505
|
+
@keyframes yf-voice-rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
|
|
8506
|
+
@keyframes yf-voice-beacon {
|
|
8507
|
+
0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--yf-voice-live) 55%, transparent); }
|
|
8508
|
+
70% { box-shadow: 0 0 0 9px color-mix(in srgb, var(--yf-voice-live) 0%, transparent); }
|
|
8509
|
+
100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--yf-voice-live) 0%, transparent); }
|
|
8510
|
+
}
|
|
8511
|
+
.yf-voice-deck {
|
|
8512
|
+
--yf-voice-live: var(--yf-voice-accent);
|
|
8513
|
+
position: relative;
|
|
8514
|
+
border-radius: 16px;
|
|
8515
|
+
padding: 1px;
|
|
8516
|
+
background:
|
|
8517
|
+
conic-gradient(
|
|
8518
|
+
from var(--yf-voice-angle),
|
|
8519
|
+
transparent 0deg,
|
|
8520
|
+
color-mix(in srgb, var(--yf-voice-live) 95%, transparent) 50deg,
|
|
8521
|
+
color-mix(in srgb, var(--yf-voice-live) 15%, transparent) 110deg,
|
|
8522
|
+
transparent 180deg,
|
|
8523
|
+
transparent 360deg
|
|
8524
|
+
),
|
|
8525
|
+
linear-gradient(color-mix(in srgb, var(--yf-voice-live) 22%, transparent), color-mix(in srgb, var(--yf-voice-live) 22%, transparent));
|
|
8526
|
+
box-shadow: 0 24px 60px -32px color-mix(in srgb, var(--yf-voice-live) 65%, transparent);
|
|
8527
|
+
animation: yf-voice-orbit 5s linear infinite;
|
|
8528
|
+
transition: box-shadow 0.5s ease;
|
|
8529
|
+
}
|
|
8530
|
+
.yf-voice-deck[data-phase='hearing'] { --yf-voice-live: var(--yf-voice-ink); animation-duration: 2.6s; }
|
|
8531
|
+
.yf-voice-deck[data-phase='thinking'] { animation-duration: 1.4s; }
|
|
8532
|
+
.yf-voice-deck[data-phase='speaking'] { animation-duration: 3.2s; }
|
|
8533
|
+
.yf-voice-deck[data-phase='listening'],
|
|
8534
|
+
.yf-voice-deck[data-phase='connecting'] { animation-duration: 9s; }
|
|
8535
|
+
.yf-voice-deck__panel {
|
|
8536
|
+
position: relative;
|
|
8537
|
+
overflow: hidden;
|
|
8538
|
+
border-radius: 15px;
|
|
8539
|
+
background:
|
|
8540
|
+
radial-gradient(120% 160% at 100% 0%, color-mix(in srgb, var(--yf-voice-live) 13%, transparent), transparent 58%),
|
|
8541
|
+
radial-gradient(90% 120% at 0% 100%, color-mix(in srgb, var(--yf-voice-live) 5%, transparent), transparent 60%),
|
|
8542
|
+
color-mix(in srgb, var(--yf-voice-panel) 90%, transparent);
|
|
8543
|
+
-webkit-backdrop-filter: blur(14px) saturate(1.2);
|
|
8544
|
+
backdrop-filter: blur(14px) saturate(1.2);
|
|
8545
|
+
animation: yf-voice-rise 0.32s cubic-bezier(0.2, 0.7, 0.2, 1) both;
|
|
8546
|
+
}
|
|
8547
|
+
.yf-voice-beacon {
|
|
8548
|
+
width: 8px;
|
|
8549
|
+
height: 8px;
|
|
8550
|
+
flex-shrink: 0;
|
|
8551
|
+
border-radius: 999px;
|
|
8552
|
+
background: var(--yf-voice-live);
|
|
8553
|
+
animation: yf-voice-beacon 1.8s ease-out infinite;
|
|
8554
|
+
transition: background-color 0.3s ease;
|
|
8555
|
+
}
|
|
8556
|
+
.yf-voice-deck[data-phase='listening'] .yf-voice-beacon { animation-duration: 3.2s; }
|
|
8557
|
+
.yf-voice-phase {
|
|
8558
|
+
white-space: nowrap;
|
|
8559
|
+
font-size: 10.5px;
|
|
8560
|
+
font-weight: 600;
|
|
8561
|
+
letter-spacing: 0.14em;
|
|
8562
|
+
text-transform: uppercase;
|
|
8563
|
+
color: var(--yf-voice-live);
|
|
8564
|
+
transition: color 0.3s ease;
|
|
8565
|
+
}
|
|
8566
|
+
.yf-voice-clock { font-size: 11px; font-variant-numeric: tabular-nums; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--yf-voice-muted); }
|
|
8567
|
+
.yf-voice-clock[data-soon] { color: var(--yf-voice-accent); }
|
|
8568
|
+
.yf-voice-end {
|
|
8569
|
+
display: inline-flex;
|
|
8570
|
+
align-items: center;
|
|
8571
|
+
gap: 7px;
|
|
8572
|
+
height: 28px;
|
|
8573
|
+
padding: 0 12px 0 10px;
|
|
8574
|
+
border-radius: 999px;
|
|
8575
|
+
border: 1px solid color-mix(in srgb, var(--yf-voice-ink) 16%, transparent);
|
|
8576
|
+
background: color-mix(in srgb, var(--yf-voice-ink) 5%, transparent);
|
|
8577
|
+
color: color-mix(in srgb, var(--yf-voice-ink) 85%, transparent);
|
|
8578
|
+
font-size: 12px;
|
|
8579
|
+
font-weight: 500;
|
|
8580
|
+
transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
|
|
8581
|
+
-webkit-tap-highlight-color: transparent;
|
|
8582
|
+
}
|
|
8583
|
+
.yf-voice-end:hover { background: color-mix(in srgb, var(--yf-voice-ink) 10%, transparent); border-color: color-mix(in srgb, var(--yf-voice-ink) 28%, transparent); color: var(--yf-voice-ink); }
|
|
8584
|
+
.yf-voice-end:active { transform: scale(0.97); }
|
|
8585
|
+
.yf-voice-end__mark { width: 8px; height: 8px; border-radius: 2px; background: currentColor; }
|
|
8586
|
+
.yf-voice-mute {
|
|
8587
|
+
display: inline-flex;
|
|
8588
|
+
align-items: center;
|
|
8589
|
+
justify-content: center;
|
|
8590
|
+
width: 28px;
|
|
8591
|
+
height: 28px;
|
|
8592
|
+
border-radius: 999px;
|
|
8593
|
+
border: 1px solid color-mix(in srgb, var(--yf-voice-ink) 16%, transparent);
|
|
8594
|
+
background: color-mix(in srgb, var(--yf-voice-ink) 5%, transparent);
|
|
8595
|
+
color: color-mix(in srgb, var(--yf-voice-ink) 85%, transparent);
|
|
8596
|
+
transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
|
|
8597
|
+
-webkit-tap-highlight-color: transparent;
|
|
8598
|
+
}
|
|
8599
|
+
.yf-voice-mute svg { width: 14px; height: 14px; }
|
|
8600
|
+
.yf-voice-mute:hover { background: color-mix(in srgb, var(--yf-voice-ink) 10%, transparent); color: var(--yf-voice-ink); }
|
|
8601
|
+
.yf-voice-mute:active { transform: scale(0.95); }
|
|
8602
|
+
.yf-voice-mute:disabled { opacity: 0.4; }
|
|
8603
|
+
.yf-voice-mute[aria-pressed='true'] {
|
|
8604
|
+
border-color: color-mix(in srgb, var(--yf-voice-accent) 55%, transparent);
|
|
8605
|
+
background: color-mix(in srgb, var(--yf-voice-accent) 18%, transparent);
|
|
8606
|
+
color: var(--yf-voice-accent);
|
|
8607
|
+
}
|
|
8608
|
+
.yf-voice-deck[data-muted] .yf-voice-beacon { background: var(--yf-voice-muted); animation: none; }
|
|
8609
|
+
.yf-voice-deck[data-muted] .yf-voice-phase { color: var(--yf-voice-muted); }
|
|
8610
|
+
.yf-voice-print {
|
|
8611
|
+
-webkit-mask-image: linear-gradient(90deg, transparent 0, #000 12%, #000 100%);
|
|
8612
|
+
mask-image: linear-gradient(90deg, transparent 0, #000 12%, #000 100%);
|
|
8613
|
+
}
|
|
8614
|
+
.yf-voice-caption { display: flex; min-width: 0; align-items: baseline; gap: 8px; color: color-mix(in srgb, var(--yf-voice-ink) 72%, transparent); }
|
|
8615
|
+
.yf-voice-caption--javier { display: block; color: var(--yf-voice-ink); font-weight: 450; text-wrap: pretty; overflow-wrap: anywhere; }
|
|
8616
|
+
.yf-voice-hint { color: var(--yf-voice-muted); }
|
|
8617
|
+
.yf-voice-who {
|
|
8618
|
+
flex-shrink: 0;
|
|
8619
|
+
font-size: 9.5px;
|
|
8620
|
+
font-weight: 600;
|
|
8621
|
+
letter-spacing: 0.14em;
|
|
8622
|
+
text-transform: uppercase;
|
|
8623
|
+
color: var(--yf-voice-muted);
|
|
8624
|
+
}
|
|
8625
|
+
.yf-voice-deck[data-fill] { display: flex; flex-direction: column; height: 100%; min-height: 0; }
|
|
8626
|
+
.yf-voice-deck[data-fill] .yf-voice-deck__panel { flex: 1; display: flex; flex-direction: column; min-height: 0; }
|
|
8627
|
+
.yf-voice-detail {
|
|
8628
|
+
flex: 1;
|
|
8629
|
+
min-height: 0;
|
|
8630
|
+
overflow-y: auto;
|
|
8631
|
+
overflow-x: hidden;
|
|
8632
|
+
overflow-wrap: anywhere;
|
|
8633
|
+
overscroll-behavior: contain;
|
|
8634
|
+
-webkit-overflow-scrolling: touch;
|
|
8635
|
+
padding: 10px 16px 16px;
|
|
8636
|
+
border-top: 1px solid color-mix(in srgb, var(--yf-voice-ink) 8%, transparent);
|
|
8637
|
+
-webkit-mask-image: linear-gradient(180deg, transparent 0, #000 14px, #000 calc(100% - 18px), transparent 100%);
|
|
8638
|
+
mask-image: linear-gradient(180deg, transparent 0, #000 14px, #000 calc(100% - 18px), transparent 100%);
|
|
8639
|
+
animation: yf-voice-rise 0.32s cubic-bezier(0.2, 0.7, 0.2, 1) both;
|
|
8640
|
+
}
|
|
8641
|
+
.yf-voice-deck[data-phone] { border-radius: 20px; }
|
|
8642
|
+
.yf-voice-deck[data-phone] .yf-voice-deck__panel { border-radius: 19px; }
|
|
8643
|
+
.yf-voice-deck[data-phone] .yf-voice-end { height: 40px; padding: 0 16px 0 14px; font-size: 14px; }
|
|
8644
|
+
.yf-voice-deck[data-phone] .yf-voice-mute { width: 40px; height: 40px; margin-right: 2px; }
|
|
8645
|
+
.yf-voice-deck[data-phone] .yf-voice-mute svg { width: 18px; height: 18px; }
|
|
8646
|
+
.yf-voice-deck[data-phone] .yf-voice-phase { font-size: 11.5px; }
|
|
8647
|
+
@media (prefers-reduced-motion: reduce) {
|
|
8648
|
+
.yf-voice-deck, .yf-voice-beacon, .yf-voice-deck__panel { animation: none; }
|
|
8649
|
+
}
|
|
8650
|
+
`;
|
|
8651
|
+
var STYLE_ID = "yf-voice-deck-styles";
|
|
8652
|
+
function ensureVoiceDeckStyles(doc = typeof document === "undefined" ? void 0 : document) {
|
|
8653
|
+
if (!doc || doc.getElementById(STYLE_ID)) return;
|
|
8654
|
+
const style = doc.createElement("style");
|
|
8655
|
+
style.id = STYLE_ID;
|
|
8656
|
+
style.textContent = VOICE_DECK_CSS;
|
|
8657
|
+
doc.head.appendChild(style);
|
|
8658
|
+
}
|
|
8659
|
+
function VoiceDeck({ voice, name, phone = false, hint, fill = false, children }) {
|
|
8660
|
+
const t = useTheme();
|
|
8661
|
+
useEffect(() => ensureVoiceDeckStyles(), []);
|
|
8662
|
+
const now = useClock(voice.startedAt !== null);
|
|
8663
|
+
if (voice.phase === "off") return null;
|
|
8664
|
+
const speaker = name ?? voice.defaultName ?? "Assistant";
|
|
8665
|
+
const phaseWords = {
|
|
8666
|
+
connecting: "Connecting",
|
|
8667
|
+
listening: "Listening",
|
|
8668
|
+
hearing: "Hearing you",
|
|
8669
|
+
thinking: "Working",
|
|
8670
|
+
speaking: speaker
|
|
8671
|
+
};
|
|
8672
|
+
const phaseWord = voice.muted && voice.phase !== "speaking" && voice.phase !== "connecting" ? "Muted" : phaseWords[voice.phase];
|
|
8673
|
+
const { startedAt, maxSessionSecs } = voice;
|
|
8674
|
+
const elapsed = startedAt === null ? 0 : now - startedAt;
|
|
8675
|
+
const remaining = maxSessionSecs > 0 && startedAt !== null ? maxSessionSecs * 1e3 - elapsed : Infinity;
|
|
8676
|
+
const endingSoon = remaining < 6e4;
|
|
8677
|
+
const said = captionTail(voice.saying, phone ? 170 : 240);
|
|
8678
|
+
const words = captionTail(voice.heard, phone ? 90 : 140);
|
|
8679
|
+
const vars = {
|
|
8680
|
+
"--yf-voice-accent": t.primary,
|
|
8681
|
+
"--yf-voice-ink": t.text,
|
|
8682
|
+
"--yf-voice-muted": t.textMuted,
|
|
8683
|
+
"--yf-voice-panel": t.bgRaised
|
|
8684
|
+
};
|
|
8685
|
+
return /* @__PURE__ */ jsxs("div", { className: "yf-voice-deck", "data-phase": voice.phase, "data-phone": phone ? "" : void 0, "data-fill": fill ? "" : void 0, "data-muted": voice.muted ? "" : void 0, style: vars, children: [
|
|
8686
|
+
/* @__PURE__ */ jsxs("div", { className: "yf-voice-deck__panel", children: [
|
|
8687
|
+
/* @__PURE__ */ jsxs("div", { className: `flex items-center gap-2.5 ${phone ? "px-4 pt-3.5" : "px-4 pt-3"}`, children: [
|
|
8688
|
+
/* @__PURE__ */ jsx("span", { className: "yf-voice-beacon", "aria-hidden": "true" }),
|
|
8689
|
+
/* @__PURE__ */ jsx("span", { role: "status", "aria-live": "polite", className: "yf-voice-phase", children: phaseWord }),
|
|
8690
|
+
startedAt !== null ? /* @__PURE__ */ jsx("span", { className: "yf-voice-clock", "data-soon": endingSoon ? "" : void 0, title: endingSoon ? "Voice ends soon" : "Time in this voice session", children: endingSoon && Number.isFinite(remaining) ? `ends in ${clock(remaining)}` : clock(elapsed) }) : null,
|
|
8691
|
+
/* @__PURE__ */ jsx("span", { className: "flex-1" }),
|
|
8692
|
+
/* @__PURE__ */ jsx(
|
|
8693
|
+
"button",
|
|
8694
|
+
{
|
|
8695
|
+
type: "button",
|
|
8696
|
+
onClick: voice.toggleMute,
|
|
8697
|
+
className: "yf-voice-mute",
|
|
8698
|
+
"aria-pressed": voice.muted,
|
|
8699
|
+
"aria-label": voice.muted ? "Unmute microphone" : "Mute microphone",
|
|
8700
|
+
title: voice.muted ? "Unmute" : "Mute",
|
|
8701
|
+
disabled: voice.phase === "connecting",
|
|
8702
|
+
children: voice.muted ? /* @__PURE__ */ jsx(MicOff, { "aria-hidden": "true" }) : /* @__PURE__ */ jsx(Mic, { "aria-hidden": "true" })
|
|
8703
|
+
}
|
|
8704
|
+
),
|
|
8705
|
+
/* @__PURE__ */ jsxs("button", { type: "button", onClick: voice.stop, className: "yf-voice-end", "aria-label": "End voice", children: [
|
|
8706
|
+
/* @__PURE__ */ jsx("span", { className: "yf-voice-end__mark", "aria-hidden": "true" }),
|
|
8707
|
+
"End"
|
|
8708
|
+
] })
|
|
8709
|
+
] }),
|
|
8710
|
+
/* @__PURE__ */ jsx(Voiceprint, { phase: voice.phase, levels: voice.levels, height: fill ? 148 : phone ? 104 : 76, cell: fill ? 14 : phone ? 13 : 11 }),
|
|
8711
|
+
/* @__PURE__ */ jsxs("div", { className: `flex flex-col gap-1.5 ${phone ? "px-4 pb-4" : "px-4 pb-3.5"}`, style: { minHeight: "1.25rem" }, "aria-live": "off", children: [
|
|
8712
|
+
words ? /* @__PURE__ */ jsxs("p", { className: "yf-voice-caption", style: { fontSize: phone ? 13.5 : 12.5 }, children: [
|
|
8713
|
+
/* @__PURE__ */ jsx("span", { className: "yf-voice-who", children: "You" }),
|
|
8714
|
+
/* @__PURE__ */ jsx("span", { className: "min-w-0 truncate", children: words })
|
|
8715
|
+
] }) : null,
|
|
8716
|
+
said ? /* @__PURE__ */ jsx("p", { className: "yf-voice-caption yf-voice-caption--javier", style: { fontSize: phone ? 16 : 14, lineHeight: phone ? 1.45 : 1.5 }, children: said }) : !words ? /* @__PURE__ */ jsx("p", { className: "yf-voice-hint", style: { fontSize: phone ? 14 : 12.5 }, children: voice.phase === "connecting" ? "Opening the line\u2026" : voice.muted ? "Your microphone is off. Tap it to talk." : hint ?? "Ask anything. Talk over me to interrupt." }) : null
|
|
8717
|
+
] }),
|
|
8718
|
+
fill && children ? /* @__PURE__ */ jsx("div", { className: "yf-voice-detail", children }) : null
|
|
8719
|
+
] }),
|
|
8720
|
+
/* @__PURE__ */ jsx("span", { "data-voice-probe": "ink", hidden: true, style: { color: "var(--yf-voice-ink)" } }),
|
|
8721
|
+
/* @__PURE__ */ jsx("span", { "data-voice-probe": "accent", hidden: true, style: { color: "var(--yf-voice-accent)" } }),
|
|
8722
|
+
/* @__PURE__ */ jsx("span", { "data-voice-probe": "muted", hidden: true, style: { color: "var(--yf-voice-muted)" } })
|
|
8723
|
+
] });
|
|
8724
|
+
}
|
|
8725
|
+
function useClock(running) {
|
|
8726
|
+
const [now, setNow] = useState(() => Date.now());
|
|
8727
|
+
useEffect(() => {
|
|
8728
|
+
if (!running) return void 0;
|
|
8729
|
+
setNow(Date.now());
|
|
8730
|
+
const id = window.setInterval(() => setNow(Date.now()), 1e3);
|
|
8731
|
+
return () => window.clearInterval(id);
|
|
8732
|
+
}, [running]);
|
|
8733
|
+
return now;
|
|
8734
|
+
}
|
|
8735
|
+
function Voiceprint({ phase, levels, height, cell }) {
|
|
8736
|
+
const canvasRef = useRef(null);
|
|
8737
|
+
const phaseRef = useRef(phase);
|
|
8738
|
+
phaseRef.current = phase;
|
|
8739
|
+
const levelsRef = useRef(levels);
|
|
8740
|
+
levelsRef.current = levels;
|
|
8741
|
+
useEffect(() => {
|
|
8742
|
+
const canvas = canvasRef.current;
|
|
8743
|
+
const ctx = canvas?.getContext("2d");
|
|
8744
|
+
if (!canvas || !ctx) return void 0;
|
|
8745
|
+
const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches ?? false;
|
|
8746
|
+
const cellW = Math.round(cell * 0.8);
|
|
8747
|
+
const cellH = Math.round(cell * 0.95);
|
|
8748
|
+
let width = 0;
|
|
8749
|
+
let dpr = 1;
|
|
8750
|
+
let rows = 1;
|
|
8751
|
+
let columns = [];
|
|
8752
|
+
let colors = { ink: [236, 232, 225], accent: [255, 140, 66], muted: [140, 140, 140] };
|
|
8753
|
+
let seed = 1;
|
|
8754
|
+
let last = performance.now();
|
|
8755
|
+
let colorsAt = 0;
|
|
8756
|
+
let raf = 0;
|
|
8757
|
+
const readColors = () => {
|
|
8758
|
+
const deck = canvas.closest(".yf-voice-deck");
|
|
8759
|
+
const probe = (which, fallback) => {
|
|
8760
|
+
const el = deck?.querySelector(`[data-voice-probe="${which}"]`);
|
|
8761
|
+
return el ? parseCssColor(getComputedStyle(el).color, fallback) : fallback;
|
|
8762
|
+
};
|
|
8763
|
+
colors = { ink: probe("ink", colors.ink), accent: probe("accent", colors.accent), muted: probe("muted", colors.muted) };
|
|
8764
|
+
};
|
|
8765
|
+
const resize = () => {
|
|
8766
|
+
dpr = Math.min(window.devicePixelRatio || 1, 3);
|
|
8767
|
+
width = canvas.clientWidth;
|
|
8768
|
+
canvas.width = Math.round(width * dpr);
|
|
8769
|
+
canvas.height = Math.round(height * dpr);
|
|
8770
|
+
rows = Math.max(3, Math.floor(height / cellH));
|
|
8771
|
+
const need = Math.ceil(width / cellW) + 3;
|
|
8772
|
+
columns = columns.slice(-need);
|
|
8773
|
+
while (columns.length < need) columns.unshift({ amp: 0.05, voice: 0 /* Quiet */, seed: seed++ });
|
|
8774
|
+
};
|
|
8775
|
+
const rgba = (c, a) => `rgba(${c[0]},${c[1]},${c[2]},${a.toFixed(3)})`;
|
|
8776
|
+
const colorOf = (v) => v === 1 /* You */ ? colors.ink : v === 0 /* Quiet */ ? colors.muted : colors.accent;
|
|
8777
|
+
const draw = (time) => {
|
|
8778
|
+
if (time - colorsAt > 1e3) {
|
|
8779
|
+
readColors();
|
|
8780
|
+
colorsAt = time;
|
|
8781
|
+
}
|
|
8782
|
+
const step = reduced ? STEP_MS * 2.5 : STEP_MS;
|
|
8783
|
+
while (time - last >= step) {
|
|
8784
|
+
columns.shift();
|
|
8785
|
+
columns.push(nextColumn(levelsRef.current(), phaseRef.current, time, seed++));
|
|
8786
|
+
last += step;
|
|
8787
|
+
}
|
|
8788
|
+
if (time - last > step * 4) last = time;
|
|
8789
|
+
const frac = reduced ? 0 : (time - last) / step;
|
|
8790
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
8791
|
+
ctx.clearRect(0, 0, width, height);
|
|
8792
|
+
ctx.font = `500 ${cell}px "Hiragino Kaku Gothic ProN", "Noto Sans JP", ui-monospace, monospace`;
|
|
8793
|
+
ctx.textAlign = "center";
|
|
8794
|
+
ctx.textBaseline = "middle";
|
|
8795
|
+
const n = columns.length;
|
|
8796
|
+
const top = (height - rows * cellH) / 2 + cellH / 2;
|
|
8797
|
+
for (let i = 0; i < n; i += 1) {
|
|
8798
|
+
const col = columns[i];
|
|
8799
|
+
const x = width - (n - 1 - i + frac) * cellW + cellW / 2 - cellW;
|
|
8800
|
+
if (x < -cellW || x > width + cellW) continue;
|
|
8801
|
+
const fade = ageFade(x, width);
|
|
8802
|
+
const fresh = n - 1 - i < 3;
|
|
8803
|
+
const color = colorOf(col.voice);
|
|
8804
|
+
const reach = Math.min(1, col.amp * 1.15);
|
|
8805
|
+
for (let r = 0; r < rows; r += 1) {
|
|
8806
|
+
const k = cellIntensity(reach, rows, r);
|
|
8807
|
+
const s = fresh && !reduced ? col.seed + Math.floor(time / 90) : col.seed;
|
|
8808
|
+
const y = top + r * cellH;
|
|
8809
|
+
if (k <= 0.02) {
|
|
8810
|
+
ctx.fillStyle = rgba(colors.muted, 0.07 * (0.25 + 0.75 * fade));
|
|
8811
|
+
ctx.fillText(glyphFor(s + 7, r), x, y);
|
|
8812
|
+
continue;
|
|
8813
|
+
}
|
|
8814
|
+
const alpha = Math.min(1, k * (col.voice === 0 /* Quiet */ ? 0.45 : 0.95) * (0.12 + 0.88 * fade) * (fresh ? 1.3 : 1));
|
|
8815
|
+
const bright = k > 0.8 && col.voice !== 0 /* Quiet */ && !reduced;
|
|
8816
|
+
if (bright) {
|
|
8817
|
+
ctx.shadowColor = rgba(color, 0.9);
|
|
8818
|
+
ctx.shadowBlur = 7;
|
|
8819
|
+
}
|
|
8820
|
+
ctx.fillStyle = rgba(color, alpha);
|
|
8821
|
+
ctx.fillText(glyphFor(s, r), x, y);
|
|
8822
|
+
if (bright) ctx.shadowBlur = 0;
|
|
8823
|
+
}
|
|
8824
|
+
}
|
|
8825
|
+
const head = columns[n - 1];
|
|
8826
|
+
if (head) {
|
|
8827
|
+
const c = colorOf(head.voice);
|
|
8828
|
+
const radius = height * 0.48;
|
|
8829
|
+
ctx.save();
|
|
8830
|
+
ctx.translate(width, height / 2);
|
|
8831
|
+
ctx.scale(1.9, 1);
|
|
8832
|
+
const glow = ctx.createRadialGradient(0, 0, 0, 0, 0, radius);
|
|
8833
|
+
glow.addColorStop(0, rgba(c, 0.1 + 0.3 * head.amp));
|
|
8834
|
+
glow.addColorStop(1, rgba(c, 0));
|
|
8835
|
+
ctx.fillStyle = glow;
|
|
8836
|
+
ctx.beginPath();
|
|
8837
|
+
ctx.arc(0, 0, radius, 0, Math.PI * 2);
|
|
8838
|
+
ctx.fill();
|
|
8839
|
+
ctx.restore();
|
|
8840
|
+
const reach = Math.max(6, Math.min(1, head.amp * 1.15) * height * 0.9);
|
|
8841
|
+
const line = ctx.createLinearGradient(0, (height - reach) / 2, 0, (height + reach) / 2);
|
|
8842
|
+
line.addColorStop(0, rgba(c, 0));
|
|
8843
|
+
line.addColorStop(0.5, rgba(c, 0.5 + 0.5 * head.amp));
|
|
8844
|
+
line.addColorStop(1, rgba(c, 0));
|
|
8845
|
+
ctx.fillStyle = line;
|
|
8846
|
+
ctx.fillRect(width - 1.5, (height - reach) / 2, 1.5, reach);
|
|
8847
|
+
}
|
|
8848
|
+
raf = window.requestAnimationFrame(draw);
|
|
8849
|
+
};
|
|
8850
|
+
readColors();
|
|
8851
|
+
resize();
|
|
8852
|
+
const observer = new ResizeObserver(resize);
|
|
8853
|
+
observer.observe(canvas);
|
|
8854
|
+
raf = window.requestAnimationFrame(draw);
|
|
8855
|
+
return () => {
|
|
8856
|
+
window.cancelAnimationFrame(raf);
|
|
8857
|
+
observer.disconnect();
|
|
8858
|
+
};
|
|
8859
|
+
}, [height, cell]);
|
|
8860
|
+
return /* @__PURE__ */ jsx("canvas", { ref: canvasRef, className: "yf-voice-print block w-full", style: { height }, "aria-hidden": "true" });
|
|
8861
|
+
}
|
|
7616
8862
|
|
|
7617
8863
|
// src/knowledge/graphTheme.ts
|
|
7618
8864
|
var NODE_TYPE_COLORS = {
|
|
@@ -14646,4 +15892,4 @@ var terminalClassNames = {
|
|
|
14646
15892
|
promptSymbol: "terminal-prompt-symbol"
|
|
14647
15893
|
};
|
|
14648
15894
|
|
|
14649
|
-
export { ADVANCE_PHASE_SIGNAL, AGENT_PALETTE, AgentAvatar, AgentPresenceBar, AgentStepTrace, AnnotationThread, CLAIM_STATUS_COLORS, ChatTerminal, CheckpointCard, ClaimNode, ConstructionProgress, CoreTerminal, DEFAULT_AGENT_LABEL, DOCUMENT_PIPELINE_STEPS, DocumentProcessingProgress, EDGE_TYPE_COLORS, EdgeDetailDrawer, EdgeMarkerDefs, CoreTerminal as EmbeddedTerminal, ExpandableTerminal, GraphChatPanel, GraphEdge, GraphNode, InlineTerminal, KnowledgeGraphView, LIVE_KG_ID, LiveActivityTicker, MarkdownContent, MarkdownEditor, NODE_TYPE_COLORS, NODE_TYPE_LABELS, NodeDetailDrawer, PROGRESS_EVENT_NAME, PipelineProgressBar, ReasoningHistoryCard, ReasoningOverlay, ReasoningView, SSE_KEEPALIVE_MS, SSE_STALL_MS, SYNTHESIS_DONE_SIGNAL, ScopedReasoningProvider, SettlementBar, SlashMentionPicker, StageCanvas, StandardTerminal, TerminalInputBar, TerminalThemeProvider, agentSeed, agentStepTraceExtra, avatarColor, blueScope, buildClaimsGraph, buildTerminalCommand, buttonShadow, captureReasoningSnapshot, defaultTheme, dispatchProgressEvent, documentEventToActivity, emeraldCollab, emeraldRevised, eventToActivity, extractKGSources, extractKgId, filterCommands, formatAssetDisplay, formatPaymentAmount, formatTokenAssetId, getAgentColor, getBuiltInCommands, getClaimStatusColors, getEdgeColor, getEdgeTypeLabel, getNodeColors, getNodeTypeLabel, getVocabTypeColors, getVocabTypeLabel, greenAction, handleSSEStream, handleStreamingResponse, hostApp, initials, isFixedKgId, isNaturalLanguage, layoutGraph, mergeTheme, openPipelineEventStream, orangeRebuttal, parseTerminalCommand, pipelineBg, processStreamLines, purple, reduceProgress, renderMarkdownBlock, resolvePaymentAssetDisplay, snapshotToSubgraph, terminalClassNames, terminalColors, useScopedProgressEvent, useSlashMentionPicker, useTheme, warmBg, withStallTimeout };
|
|
15895
|
+
export { ADVANCE_PHASE_SIGNAL, AGENT_PALETTE, AgentAvatar, AgentPresenceBar, AgentStepTrace, AnnotationThread, AskQueue, CLAIM_STATUS_COLORS, ChatTerminal, CheckpointCard, ClaimNode, ConstructionProgress, CoreTerminal, DEFAULT_AGENT_LABEL, DOCUMENT_PIPELINE_STEPS, DocumentProcessingProgress, EDGE_TYPE_COLORS, EdgeDetailDrawer, EdgeMarkerDefs, CoreTerminal as EmbeddedTerminal, ExpandableTerminal, GraphChatPanel, GraphEdge, GraphNode, InlineTerminal, KnowledgeGraphView, LIVE_KG_ID, LiveActivityTicker, MarkdownContent, MarkdownEditor, NODE_TYPE_COLORS, NODE_TYPE_LABELS, NodeDetailDrawer, PROGRESS_EVENT_NAME, PipelineProgressBar, REPLACED_OUTPUT, ReasoningHistoryCard, ReasoningOverlay, ReasoningView, SILENT, SSE_KEEPALIVE_MS, SSE_STALL_MS, SYNTHESIS_DONE_SIGNAL, ScopedReasoningProvider, ScreenAwake, SettlementBar, SlashMentionPicker, StageCanvas, StandardTerminal, TerminalInputBar, TerminalThemeProvider, VOICE_CAPTURE_WORKLET_SOURCE, VOICE_LANGUAGES, VOICE_NAMES, VoiceDeck, VoiceInputButton, VoiceSession, agentSeed, agentStepTraceExtra, avatarColor, blueScope, buildClaimsGraph, buildTerminalCommand, buttonShadow, captureReasoningSnapshot, defaultTheme, dispatchProgressEvent, documentEventToActivity, emeraldCollab, emeraldRevised, encodePersona, eventToActivity, extractKGSources, extractKgId, filterCommands, formatAssetDisplay, formatPaymentAmount, formatTokenAssetId, getAgentColor, getBuiltInCommands, getClaimStatusColors, getEdgeColor, getEdgeTypeLabel, getNodeColors, getNodeTypeLabel, getVocabTypeColors, getVocabTypeLabel, getVoiceStatus, greenAction, handleSSEStream, handleStreamingResponse, hostApp, initials, isFixedKgId, isNaturalLanguage, layoutGraph, mergeTheme, openPipelineEventStream, orangeRebuttal, parseTerminalCommand, pipelineBg, processStreamLines, purple, reduceProgress, renderMarkdownBlock, resolvePaymentAssetDisplay, snapshotToSubgraph, socketBase, terminalClassNames, terminalColors, useScopedProgressEvent, useSlashMentionPicker, useTheme, useVoiceSession, voiceEndedMessage, voiceSocketUrl, warmBg, withStallTimeout };
|