tz-voice-chat 1.0.2 → 1.1.2
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/README.md +31 -0
- package/dist/assets/llmWorker-CVEGn6yI.js +1 -0
- package/dist/index.cjs +2 -1
- package/dist/index.js +801 -438
- package/dist/style.css +1 -1
- package/package.json +18 -4
- package/server/index.js +263 -0
- package/server/tts.js +116 -0
package/dist/index.js
CHANGED
|
@@ -1,31 +1,41 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
import { jsx as
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { useState as q, useRef as R, useEffect as j, useMemo as Re } from "react";
|
|
2
|
+
import { jsx as p, jsxs as S } from "react/jsx-runtime";
|
|
3
|
+
const me = 60, se = 200;
|
|
4
|
+
function Me() {
|
|
5
|
+
let e = "", r = !0;
|
|
6
|
+
function s(t, i, n) {
|
|
7
|
+
t.push(i), e = n, r = !1;
|
|
8
|
+
}
|
|
9
|
+
function a() {
|
|
6
10
|
const t = [];
|
|
7
11
|
for (; ; ) {
|
|
8
|
-
const
|
|
9
|
-
if (
|
|
10
|
-
const n = e.slice(0,
|
|
12
|
+
const i = /([.!?]{1,3})(\s|$)/.exec(e);
|
|
13
|
+
if (i) {
|
|
14
|
+
const n = e.slice(0, i.index + i[0].length).trim();
|
|
11
15
|
if (n.length >= 10) {
|
|
12
|
-
t
|
|
16
|
+
s(t, n, e.slice(i.index + i[0].length));
|
|
13
17
|
continue;
|
|
14
18
|
}
|
|
15
19
|
}
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
if (r) {
|
|
21
|
+
if (e.length >= 25) {
|
|
22
|
+
const n = /[,;:—–](\s|$)/.exec(e);
|
|
23
|
+
if (n) {
|
|
24
|
+
const l = e.slice(0, n.index + n[0].length).trim();
|
|
25
|
+
if (l.length >= 20) {
|
|
26
|
+
s(t, l, e.slice(n.index + n[0].length));
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
23
29
|
}
|
|
24
30
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
if (e.length >= me) {
|
|
32
|
+
const n = e.lastIndexOf(" ", me - 1), l = n > 10 ? n : me;
|
|
33
|
+
s(t, e.slice(0, l).trim(), e.slice(l).trimStart());
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
} else if (e.length >= se) {
|
|
37
|
+
const n = e.slice(0, se), l = Math.max(n.lastIndexOf(", "), n.lastIndexOf("; "), n.lastIndexOf(": ")), o = l > se / 2 ? l + 1 : n.lastIndexOf(" ") > 10 ? n.lastIndexOf(" ") : se;
|
|
38
|
+
s(t, e.slice(0, o).trim(), e.slice(o).trimStart());
|
|
29
39
|
continue;
|
|
30
40
|
}
|
|
31
41
|
break;
|
|
@@ -34,7 +44,7 @@ function me() {
|
|
|
34
44
|
}
|
|
35
45
|
return {
|
|
36
46
|
write(t) {
|
|
37
|
-
return e += t,
|
|
47
|
+
return e += t, a();
|
|
38
48
|
},
|
|
39
49
|
flush() {
|
|
40
50
|
const t = e.trim();
|
|
@@ -42,188 +52,212 @@ function me() {
|
|
|
42
52
|
}
|
|
43
53
|
};
|
|
44
54
|
}
|
|
45
|
-
async function
|
|
46
|
-
dtype:
|
|
47
|
-
device:
|
|
48
|
-
modelProxyUrl:
|
|
55
|
+
async function qe(e, {
|
|
56
|
+
dtype: r = "q8",
|
|
57
|
+
device: s = "cpu",
|
|
58
|
+
modelProxyUrl: a = "/api/models/"
|
|
49
59
|
} = {}) {
|
|
50
|
-
var
|
|
51
|
-
const { env:
|
|
60
|
+
var d, u;
|
|
61
|
+
const { env: t } = await import(
|
|
52
62
|
/* @vite-ignore */
|
|
53
63
|
"/vendor/transformers/transformers.min.js"
|
|
54
64
|
);
|
|
55
|
-
|
|
56
|
-
const
|
|
65
|
+
t.remoteHost = a, (u = (d = t == null ? void 0 : t.backends) == null ? void 0 : d.onnx) != null && u.wasm && (t.backends.onnx.wasm.wasmPaths = "/vendor/transformers/"), t != null && t.onnx && (t.onnx.logSeverityLevel = 3);
|
|
66
|
+
const i = await import(
|
|
57
67
|
/* @vite-ignore */
|
|
58
68
|
"/vendor/kokoro/kokoro.web.js"
|
|
59
|
-
), { KokoroTTS:
|
|
60
|
-
let
|
|
61
|
-
|
|
62
|
-
let
|
|
63
|
-
const
|
|
64
|
-
typeof (
|
|
69
|
+
), { KokoroTTS: n } = i;
|
|
70
|
+
let l = r;
|
|
71
|
+
s === "webgpu" && (r === "q4" || r === "q8") && (console.warn(`[modelLoader] dtype "${r}" not supported on WebGPU — using "fp32".`), l = "fp32");
|
|
72
|
+
let o = 0;
|
|
73
|
+
const f = (m) => {
|
|
74
|
+
typeof (m == null ? void 0 : m.progress) == "number" && (o = Math.max(o, m.progress / 100), e == null || e("kokoro", Math.min(0.99, o)));
|
|
65
75
|
};
|
|
66
|
-
async function c(
|
|
67
|
-
return
|
|
68
|
-
dtype:
|
|
69
|
-
device:
|
|
70
|
-
progress_callback:
|
|
76
|
+
async function c(m, w) {
|
|
77
|
+
return n.from_pretrained("onnx-community/Kokoro-82M-ONNX", {
|
|
78
|
+
dtype: w,
|
|
79
|
+
device: m,
|
|
80
|
+
progress_callback: f
|
|
71
81
|
});
|
|
72
82
|
}
|
|
73
|
-
let
|
|
83
|
+
let v;
|
|
74
84
|
try {
|
|
75
|
-
|
|
76
|
-
} catch (
|
|
77
|
-
if (
|
|
78
|
-
console.warn(`[modelLoader] Kokoro on "${
|
|
85
|
+
v = await c(s, l);
|
|
86
|
+
} catch (m) {
|
|
87
|
+
if (s !== "wasm")
|
|
88
|
+
console.warn(`[modelLoader] Kokoro on "${s}" failed (${m == null ? void 0 : m.message}) — falling back to WASM.`), o = 0, v = await c("wasm", r);
|
|
79
89
|
else
|
|
80
|
-
throw
|
|
90
|
+
throw m;
|
|
91
|
+
}
|
|
92
|
+
return e == null || e("kokoro", 1), { kokoro: v };
|
|
93
|
+
}
|
|
94
|
+
function ie(e, r, { threshold: s = 0.01, padMs: a = 25 } = {}) {
|
|
95
|
+
const { numberOfChannels: t, length: i, sampleRate: n } = r;
|
|
96
|
+
let l = i, o = -1;
|
|
97
|
+
for (let u = 0; u < t; u++) {
|
|
98
|
+
const m = r.getChannelData(u);
|
|
99
|
+
for (let w = 0; w < i; w++)
|
|
100
|
+
if (Math.abs(m[w]) > s) {
|
|
101
|
+
w < l && (l = w);
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
for (let w = i - 1; w >= 0; w--)
|
|
105
|
+
if (Math.abs(m[w]) > s) {
|
|
106
|
+
w > o && (o = w);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
81
109
|
}
|
|
82
|
-
|
|
110
|
+
if (o < l) return r;
|
|
111
|
+
const f = Math.round(a / 1e3 * n), c = Math.max(0, l - f), v = Math.min(i, o + 1 + f);
|
|
112
|
+
if (c === 0 && v === i) return r;
|
|
113
|
+
const d = e.createBuffer(t, v - c, n);
|
|
114
|
+
for (let u = 0; u < t; u++)
|
|
115
|
+
d.copyToChannel(r.getChannelData(u).subarray(c, v), u);
|
|
116
|
+
return d;
|
|
83
117
|
}
|
|
84
|
-
function
|
|
85
|
-
const
|
|
86
|
-
let
|
|
87
|
-
async function
|
|
88
|
-
if (
|
|
89
|
-
const
|
|
90
|
-
if (
|
|
91
|
-
if (
|
|
92
|
-
const
|
|
93
|
-
return
|
|
118
|
+
function Ve(e, { voice: r = "af_bella", speed: s = 1, onPlayStart: a } = {}) {
|
|
119
|
+
const t = new AudioContext(), i = [];
|
|
120
|
+
let n = !1, l = null, o = !1;
|
|
121
|
+
async function f(d) {
|
|
122
|
+
if (d instanceof AudioBuffer) return ie(t, d);
|
|
123
|
+
const u = (d == null ? void 0 : d.audio) ?? d;
|
|
124
|
+
if (u instanceof ArrayBuffer) return ie(t, await t.decodeAudioData(u.slice(0)));
|
|
125
|
+
if (u instanceof Float32Array) {
|
|
126
|
+
const m = (d == null ? void 0 : d.samplingRate) ?? 24e3, w = t.createBuffer(1, u.length, m);
|
|
127
|
+
return w.copyToChannel(u, 0), ie(t, w);
|
|
94
128
|
}
|
|
95
129
|
throw new Error("Unsupported TTS payload");
|
|
96
130
|
}
|
|
97
|
-
async function
|
|
98
|
-
await
|
|
99
|
-
const
|
|
100
|
-
return
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
},
|
|
131
|
+
async function c(d) {
|
|
132
|
+
await t.resume();
|
|
133
|
+
const u = t.createBufferSource();
|
|
134
|
+
return u.buffer = d, u.connect(t.destination), new Promise((m) => {
|
|
135
|
+
u.onended = () => {
|
|
136
|
+
l === u && (l = null), m();
|
|
137
|
+
}, l = u, u.start(), o || (o = !0, a == null || a());
|
|
104
138
|
});
|
|
105
139
|
}
|
|
106
|
-
async function
|
|
107
|
-
if (!
|
|
108
|
-
for (
|
|
109
|
-
const
|
|
140
|
+
async function v() {
|
|
141
|
+
if (!n) {
|
|
142
|
+
for (n = !0; i.length > 0; ) {
|
|
143
|
+
const d = i.shift();
|
|
110
144
|
try {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
} catch (
|
|
114
|
-
console.error("[TtsQueue]",
|
|
145
|
+
const u = await d;
|
|
146
|
+
u && await c(u);
|
|
147
|
+
} catch (u) {
|
|
148
|
+
console.error("[TtsQueue]", u);
|
|
115
149
|
}
|
|
116
150
|
}
|
|
117
|
-
|
|
151
|
+
n = !1;
|
|
118
152
|
}
|
|
119
153
|
}
|
|
120
154
|
return {
|
|
121
|
-
enqueue(
|
|
122
|
-
|
|
155
|
+
enqueue(d) {
|
|
156
|
+
i.push(e.generate(d, { voice: r, speed: s }).then(f)), v();
|
|
123
157
|
},
|
|
124
158
|
cancel() {
|
|
125
|
-
if (
|
|
159
|
+
if (i.length = 0, o = !1, l) {
|
|
126
160
|
try {
|
|
127
|
-
|
|
161
|
+
l.stop();
|
|
128
162
|
} catch {
|
|
129
163
|
}
|
|
130
|
-
|
|
164
|
+
l = null;
|
|
131
165
|
}
|
|
132
166
|
},
|
|
133
167
|
async finished() {
|
|
134
|
-
for (;
|
|
168
|
+
for (; n || i.length > 0; ) await new Promise((d) => setTimeout(d, 200));
|
|
135
169
|
}
|
|
136
170
|
};
|
|
137
171
|
}
|
|
138
|
-
function
|
|
139
|
-
const
|
|
140
|
-
let
|
|
141
|
-
async function
|
|
142
|
-
const
|
|
143
|
-
e && (
|
|
144
|
-
const
|
|
172
|
+
function Oe({ voice: e = "", speed: r = 1, onPlayStart: s, url: a = "/api/tts" } = {}) {
|
|
173
|
+
const t = new AudioContext(), i = [];
|
|
174
|
+
let n = !1, l = null, o = !1;
|
|
175
|
+
async function f(d) {
|
|
176
|
+
const u = { text: d };
|
|
177
|
+
e && (u.voice = e), r !== 1 && (u.speed = r);
|
|
178
|
+
const m = await fetch(a, {
|
|
145
179
|
method: "POST",
|
|
146
180
|
headers: { "Content-Type": "application/json" },
|
|
147
|
-
body: JSON.stringify(
|
|
181
|
+
body: JSON.stringify(u)
|
|
148
182
|
});
|
|
149
|
-
if (!
|
|
150
|
-
return
|
|
183
|
+
if (!m.ok) throw new Error(`TTS ${m.status}: ${await m.text()}`);
|
|
184
|
+
return ie(t, await t.decodeAudioData(await m.arrayBuffer()));
|
|
151
185
|
}
|
|
152
|
-
async function
|
|
153
|
-
await
|
|
154
|
-
const
|
|
155
|
-
return
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
},
|
|
186
|
+
async function c(d) {
|
|
187
|
+
await t.resume();
|
|
188
|
+
const u = t.createBufferSource();
|
|
189
|
+
return u.buffer = d, u.connect(t.destination), new Promise((m) => {
|
|
190
|
+
u.onended = () => {
|
|
191
|
+
l === u && (l = null), m();
|
|
192
|
+
}, l = u, u.start(), o || (o = !0, s == null || s());
|
|
159
193
|
});
|
|
160
194
|
}
|
|
161
|
-
async function
|
|
162
|
-
if (!
|
|
163
|
-
for (
|
|
164
|
-
const
|
|
195
|
+
async function v() {
|
|
196
|
+
if (!n) {
|
|
197
|
+
for (n = !0; i.length > 0; ) {
|
|
198
|
+
const d = i.shift();
|
|
165
199
|
try {
|
|
166
|
-
await
|
|
167
|
-
} catch (
|
|
168
|
-
console.error("[ServerTTS]",
|
|
200
|
+
await c(await d);
|
|
201
|
+
} catch (u) {
|
|
202
|
+
console.error("[ServerTTS]", u);
|
|
169
203
|
}
|
|
170
204
|
}
|
|
171
|
-
|
|
205
|
+
n = !1;
|
|
172
206
|
}
|
|
173
207
|
}
|
|
174
208
|
return {
|
|
175
|
-
enqueue(
|
|
176
|
-
|
|
209
|
+
enqueue(d) {
|
|
210
|
+
d != null && d.trim() && (i.push(f(d)), v());
|
|
177
211
|
},
|
|
178
212
|
cancel() {
|
|
179
|
-
if (
|
|
213
|
+
if (i.length = 0, l) {
|
|
180
214
|
try {
|
|
181
|
-
|
|
215
|
+
l.stop();
|
|
182
216
|
} catch {
|
|
183
217
|
}
|
|
184
|
-
|
|
218
|
+
l = null;
|
|
185
219
|
}
|
|
186
|
-
|
|
220
|
+
o = !1;
|
|
187
221
|
},
|
|
188
222
|
async finished() {
|
|
189
|
-
for (;
|
|
223
|
+
for (; n || i.length > 0; ) await new Promise((d) => setTimeout(d, 100));
|
|
190
224
|
}
|
|
191
225
|
};
|
|
192
226
|
}
|
|
193
|
-
function
|
|
194
|
-
const
|
|
195
|
-
let
|
|
196
|
-
function
|
|
227
|
+
function Pe({ voice: e = "", rate: r = 1, pitch: s = 1, onPlayStart: a } = {}) {
|
|
228
|
+
const t = [];
|
|
229
|
+
let i = !1, n = !1;
|
|
230
|
+
function l() {
|
|
197
231
|
return e ? speechSynthesis.getVoices().find((c) => c.name === e || c.voiceURI === e) ?? null : null;
|
|
198
232
|
}
|
|
199
|
-
function
|
|
200
|
-
if (
|
|
201
|
-
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
233
|
+
function o() {
|
|
234
|
+
if (i || t.length === 0) return;
|
|
235
|
+
i = !0;
|
|
236
|
+
const f = t.shift(), c = new SpeechSynthesisUtterance(f), v = l();
|
|
237
|
+
v && (c.voice = v), c.rate = r, c.pitch = s, c.onstart = () => {
|
|
238
|
+
n || (n = !0, a == null || a());
|
|
205
239
|
}, c.onend = () => {
|
|
206
|
-
|
|
207
|
-
}, c.onerror = (
|
|
208
|
-
console.error("[WebSpeech TTS] error",
|
|
240
|
+
i = !1, o();
|
|
241
|
+
}, c.onerror = (d) => {
|
|
242
|
+
console.error("[WebSpeech TTS] error", d.error), i = !1, o();
|
|
209
243
|
}, speechSynthesis.speak(c);
|
|
210
244
|
}
|
|
211
245
|
return {
|
|
212
|
-
enqueue(
|
|
213
|
-
|
|
246
|
+
enqueue(f) {
|
|
247
|
+
f != null && f.trim() && (t.push(f), o());
|
|
214
248
|
},
|
|
215
249
|
cancel() {
|
|
216
|
-
|
|
250
|
+
t.length = 0, speechSynthesis.cancel(), i = !1, n = !1;
|
|
217
251
|
},
|
|
218
252
|
async finished() {
|
|
219
|
-
for (;
|
|
253
|
+
for (; i || t.length > 0; ) await new Promise((f) => setTimeout(f, 100));
|
|
220
254
|
}
|
|
221
255
|
};
|
|
222
256
|
}
|
|
223
|
-
async function
|
|
257
|
+
async function Ke({ onSpeechStart: e, onSpeechEnd: r, onError: s, deviceId: a }) {
|
|
224
258
|
try {
|
|
225
|
-
const { MicVAD:
|
|
226
|
-
return await
|
|
259
|
+
const { MicVAD: t } = window.vad;
|
|
260
|
+
return await t.new({
|
|
227
261
|
model: "legacy",
|
|
228
262
|
baseAssetPath: "/vendor/vad/",
|
|
229
263
|
onnxWASMBasePath: "/vendor/ort/",
|
|
@@ -231,11 +265,11 @@ async function Te({ onSpeechStart: e, onSpeechEnd: s, onError: t, deviceId: r })
|
|
|
231
265
|
echoCancellation: !0,
|
|
232
266
|
noiseSuppression: !0,
|
|
233
267
|
autoGainControl: !1,
|
|
234
|
-
...
|
|
268
|
+
...a ? { deviceId: { exact: a } } : {}
|
|
235
269
|
},
|
|
236
270
|
onSpeechStart: () => e == null ? void 0 : e(),
|
|
237
|
-
onSpeechEnd: (
|
|
238
|
-
console.log("[VAD] onSpeechEnd — samples:",
|
|
271
|
+
onSpeechEnd: (n) => {
|
|
272
|
+
console.log("[VAD] onSpeechEnd — samples:", n == null ? void 0 : n.length), r == null || r(n);
|
|
239
273
|
},
|
|
240
274
|
onVADMisfire: () => {
|
|
241
275
|
},
|
|
@@ -245,451 +279,780 @@ async function Te({ onSpeechStart: e, onSpeechEnd: s, onError: t, deviceId: r })
|
|
|
245
279
|
minSpeechFrames: 3,
|
|
246
280
|
preSpeechPadFrames: 1
|
|
247
281
|
});
|
|
248
|
-
} catch (
|
|
249
|
-
throw
|
|
282
|
+
} catch (t) {
|
|
283
|
+
throw s == null || s("VAD initialization failed. Falling back to manual microphone capture."), t;
|
|
250
284
|
}
|
|
251
285
|
}
|
|
252
|
-
const
|
|
286
|
+
const z = {
|
|
253
287
|
IDLE: "idle",
|
|
254
288
|
LISTENING: "listening",
|
|
255
289
|
TRANSCRIBING: "transcribing",
|
|
256
290
|
THINKING: "thinking",
|
|
257
291
|
SPEAKING: "speaking"
|
|
258
|
-
},
|
|
259
|
-
function
|
|
292
|
+
}, Ge = "You are a friendly assistant for quick voice interactions. Provide answers only in the form of a single concise sentence, without any introductory phrases. Don't use any formatting, markdown, emoji, or code blocks — just plain text. The answers will be spoken aloud, so keep them short and natural.";
|
|
293
|
+
function Ce({
|
|
260
294
|
processInput: e,
|
|
261
|
-
systemPrompt:
|
|
262
|
-
ttsProvider:
|
|
263
|
-
ttsVoice:
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
295
|
+
systemPrompt: r = Ge,
|
|
296
|
+
ttsProvider: s = "server-kokoro",
|
|
297
|
+
ttsVoice: a = "af_bella",
|
|
298
|
+
ttsSpeed: t = 1,
|
|
299
|
+
ttsServerUrl: i = "/api/tts",
|
|
300
|
+
ttsSpeechRate: n = 1,
|
|
301
|
+
ttsSpeechPitch: l = 1,
|
|
302
|
+
whisperModel: o = "onnx-community/whisper-tiny.en",
|
|
303
|
+
whisperDtype: f = "q8",
|
|
304
|
+
whisperDevice: c = "cpu",
|
|
305
|
+
kokoroDtype: v = "q8",
|
|
306
|
+
kokoroBrowserDevice: d = "wasm",
|
|
307
|
+
modelProxyUrl: u = "/api/models/"
|
|
273
308
|
} = {}) {
|
|
274
|
-
const [
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}, [
|
|
278
|
-
Z.current =
|
|
279
|
-
}, [
|
|
280
|
-
|
|
281
|
-
}, [e]),
|
|
282
|
-
async function
|
|
309
|
+
const [m, w] = q(z.IDLE), [I, x] = q(!0), [L, b] = q({ whisper: 0, kokoro: 0 }), [E, A] = q(null), [P, D] = q([]), [k, V] = q(null), [$, C] = q(!1), [M, y] = q(""), [W, oe] = q([]), [ce, ee] = q(null), J = R(z.IDLE), Z = R(!1), K = R(null), U = R(null), B = R(null), H = R(null), le = R(null), ue = R(null), te = R([{ role: "system", content: r }]), de = R(e), ge = R(0), ve = R({ id: null, enqueuedAt: null, ttsMs: null }), Te = R(1);
|
|
310
|
+
j(() => {
|
|
311
|
+
J.current = m;
|
|
312
|
+
}, [m]), j(() => {
|
|
313
|
+
Z.current = $;
|
|
314
|
+
}, [$]), j(() => {
|
|
315
|
+
de.current = e;
|
|
316
|
+
}, [e]), j(() => {
|
|
317
|
+
async function _() {
|
|
283
318
|
try {
|
|
284
|
-
const
|
|
285
|
-
|
|
319
|
+
const T = await navigator.mediaDevices.enumerateDevices();
|
|
320
|
+
oe(T.filter((g) => g.kind === "audioinput"));
|
|
286
321
|
} catch {
|
|
287
322
|
}
|
|
288
323
|
}
|
|
289
|
-
return
|
|
290
|
-
}, [
|
|
291
|
-
let
|
|
292
|
-
async function
|
|
324
|
+
return _(), navigator.mediaDevices.addEventListener("devicechange", _), () => navigator.mediaDevices.removeEventListener("devicechange", _);
|
|
325
|
+
}, [$]), j(() => {
|
|
326
|
+
let _ = !1;
|
|
327
|
+
async function T() {
|
|
293
328
|
try {
|
|
294
|
-
const
|
|
295
|
-
var
|
|
296
|
-
|
|
329
|
+
const g = () => {
|
|
330
|
+
var N;
|
|
331
|
+
const h = ve.current;
|
|
332
|
+
h.enqueuedAt != null && h.ttsMs == null && (h.ttsMs = Math.round(performance.now() - h.enqueuedAt), h.id != null && ze(h.id, { timings: { ttsMs: h.ttsMs } })), s === "webspeech" && ((N = U.current) == null || N.pause());
|
|
297
333
|
};
|
|
298
|
-
if (
|
|
299
|
-
|
|
300
|
-
voice:
|
|
301
|
-
rate:
|
|
302
|
-
pitch:
|
|
303
|
-
onPlayStart:
|
|
304
|
-
}),
|
|
305
|
-
else if (
|
|
306
|
-
const { kokoro:
|
|
307
|
-
(
|
|
308
|
-
{ dtype:
|
|
334
|
+
if (s === "webspeech")
|
|
335
|
+
K.current = Pe({
|
|
336
|
+
voice: a,
|
|
337
|
+
rate: n,
|
|
338
|
+
pitch: l,
|
|
339
|
+
onPlayStart: g
|
|
340
|
+
}), b((h) => ({ ...h, kokoro: 1 }));
|
|
341
|
+
else if (s === "kokoro") {
|
|
342
|
+
const { kokoro: h } = await qe(
|
|
343
|
+
(N, Q) => b((F) => ({ ...F, [N]: Q })),
|
|
344
|
+
{ dtype: v, device: d, modelProxyUrl: u }
|
|
309
345
|
);
|
|
310
|
-
if (
|
|
311
|
-
|
|
346
|
+
if (_) return;
|
|
347
|
+
K.current = Ve(h, { voice: a, speed: t, onPlayStart: g });
|
|
312
348
|
} else
|
|
313
|
-
|
|
314
|
-
if (
|
|
315
|
-
|
|
349
|
+
K.current = Oe({ voice: a, speed: t, onPlayStart: g, url: i }), b((h) => ({ ...h, kokoro: 1 }));
|
|
350
|
+
if (_) return;
|
|
351
|
+
H.current = new Worker(
|
|
316
352
|
new URL(
|
|
317
353
|
/* @vite-ignore */
|
|
318
354
|
"./assets/whisperWorker-BJuP9cl9.js",
|
|
319
355
|
import.meta.url
|
|
320
356
|
),
|
|
321
357
|
{ type: "module" }
|
|
322
|
-
),
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
},
|
|
358
|
+
), H.current.onmessage = (h) => {
|
|
359
|
+
h.data.type === "transcript" ? be(h.data.text, {
|
|
360
|
+
transcribeMs: Math.round(performance.now() - ge.current)
|
|
361
|
+
}) : h.data.type === "progress" ? b((N) => ({ ...N, whisper: h.data.progress })) : h.data.type === "ready" ? (b((N) => ({ ...N, whisper: 1 })), x(!1)) : h.data.type === "error" && (A(`Whisper: ${h.data.message}`), x(!1));
|
|
362
|
+
}, H.current.onerror = () => {
|
|
363
|
+
A("Model load failed. Check WebGPU/WASM browser support."), x(!1);
|
|
364
|
+
}, H.current.postMessage({
|
|
327
365
|
type: "init",
|
|
328
|
-
model:
|
|
329
|
-
dtype:
|
|
330
|
-
device:
|
|
331
|
-
modelProxyUrl:
|
|
366
|
+
model: o,
|
|
367
|
+
dtype: f,
|
|
368
|
+
device: c,
|
|
369
|
+
modelProxyUrl: u
|
|
332
370
|
});
|
|
333
|
-
} catch (
|
|
334
|
-
|
|
371
|
+
} catch (g) {
|
|
372
|
+
_ || (A("Initialisation failed: " + ((g == null ? void 0 : g.message) ?? String(g))), x(!1));
|
|
335
373
|
}
|
|
336
374
|
}
|
|
337
|
-
return
|
|
338
|
-
var
|
|
339
|
-
|
|
375
|
+
return T(), () => {
|
|
376
|
+
var g;
|
|
377
|
+
_ = !0, (g = H.current) == null || g.terminate();
|
|
340
378
|
};
|
|
341
|
-
}, [
|
|
342
|
-
function
|
|
343
|
-
|
|
379
|
+
}, [s, o, f, c, v, d]);
|
|
380
|
+
function ye(_, T, g) {
|
|
381
|
+
const h = Te.current++;
|
|
382
|
+
return D((N) => [...N, { id: h, role: _, content: T, timings: g, time: (/* @__PURE__ */ new Date()).toISOString() }]), h;
|
|
383
|
+
}
|
|
384
|
+
function ze(_, { timings: T }) {
|
|
385
|
+
D((g) => g.map((h) => h.id === _ ? { ...h, timings: { ...h.timings, ...T } } : h));
|
|
344
386
|
}
|
|
345
|
-
async function
|
|
346
|
-
if (!(
|
|
347
|
-
|
|
387
|
+
async function be(_, { transcribeMs: T } = {}) {
|
|
388
|
+
if (!(_ != null && _.trim())) {
|
|
389
|
+
w(Z.current ? z.LISTENING : z.IDLE);
|
|
348
390
|
return;
|
|
349
391
|
}
|
|
350
|
-
|
|
351
|
-
const
|
|
352
|
-
|
|
392
|
+
w(z.THINKING), ye("user", _, T != null ? { transcribeMs: T } : void 0);
|
|
393
|
+
const g = [...te.current, { role: "user", content: _ }];
|
|
394
|
+
te.current = g, await Ie(_, g);
|
|
353
395
|
}
|
|
354
|
-
async function
|
|
355
|
-
var
|
|
356
|
-
(
|
|
396
|
+
async function Ie(_, T) {
|
|
397
|
+
var g, h, N, Q;
|
|
398
|
+
(g = B.current) == null || g.abort(), B.current = new AbortController(), (h = K.current) == null || h.cancel();
|
|
357
399
|
try {
|
|
358
|
-
if (!
|
|
359
|
-
const
|
|
360
|
-
let ne = "",
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
400
|
+
if (!de.current) throw new Error("processInput is not provided");
|
|
401
|
+
const F = Me();
|
|
402
|
+
let ne = "", fe = !1, he = null;
|
|
403
|
+
const Ee = performance.now(), Y = { id: null, enqueuedAt: null, ttsMs: null };
|
|
404
|
+
ve.current = Y;
|
|
405
|
+
const ke = (re) => {
|
|
406
|
+
var pe;
|
|
407
|
+
Y.enqueuedAt == null && (Y.enqueuedAt = performance.now()), (pe = K.current) == null || pe.enqueue(re);
|
|
408
|
+
};
|
|
409
|
+
V("");
|
|
410
|
+
for await (const re of de.current(_, T, B.current.signal))
|
|
411
|
+
he == null && (he = Math.round(performance.now() - Ee)), ne += re, V(ne), F.write(re).forEach((De) => {
|
|
412
|
+
fe || (fe = !0, w(z.SPEAKING)), ke(De);
|
|
366
413
|
});
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
414
|
+
const _e = F.flush();
|
|
415
|
+
_e && (fe || w(z.SPEAKING), ke(_e)), Y.id = ye("assistant", ne, {
|
|
416
|
+
thinkMs: he,
|
|
417
|
+
...Y.ttsMs != null ? { ttsMs: Y.ttsMs } : {}
|
|
418
|
+
}), V(null), te.current = [...te.current, { role: "assistant", content: ne }], await ((N = K.current) == null ? void 0 : N.finished()), s === "webspeech" && ((Q = U.current) == null || Q.start());
|
|
419
|
+
} catch (F) {
|
|
420
|
+
V(null), F.name !== "AbortError" && A(F.message || "Processing failed.");
|
|
371
421
|
} finally {
|
|
372
|
-
|
|
422
|
+
w(Z.current ? z.LISTENING : z.IDLE);
|
|
373
423
|
}
|
|
374
424
|
}
|
|
375
|
-
async function
|
|
376
|
-
var
|
|
377
|
-
|
|
425
|
+
async function Le() {
|
|
426
|
+
var _, T;
|
|
427
|
+
A(null), J.current === z.SPEAKING && ((_ = K.current) == null || _.cancel(), (T = B.current) == null || T.abort());
|
|
378
428
|
try {
|
|
379
|
-
|
|
429
|
+
w(z.LISTENING), C(!0);
|
|
380
430
|
try {
|
|
381
|
-
const
|
|
382
|
-
audio:
|
|
431
|
+
const g = await navigator.mediaDevices.getUserMedia({
|
|
432
|
+
audio: M ? { deviceId: { exact: M } } : !0,
|
|
383
433
|
video: !1
|
|
384
434
|
});
|
|
385
|
-
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
} catch (
|
|
391
|
-
console.warn("[TZVoiceChat] waveform analyser unavailable:",
|
|
435
|
+
ue.current = g;
|
|
436
|
+
const h = new AudioContext();
|
|
437
|
+
le.current = h;
|
|
438
|
+
const N = h.createAnalyser();
|
|
439
|
+
N.fftSize = 256, h.createMediaStreamSource(g).connect(N), ee(N);
|
|
440
|
+
} catch (g) {
|
|
441
|
+
console.warn("[TZVoiceChat] waveform analyser unavailable:", g);
|
|
392
442
|
}
|
|
393
|
-
|
|
443
|
+
U.current = await Ke({
|
|
394
444
|
onSpeechStart: () => {
|
|
395
|
-
var
|
|
396
|
-
|
|
445
|
+
var g, h;
|
|
446
|
+
s === "webspeech" && J.current === z.SPEAKING || (J.current === z.SPEAKING && ((g = K.current) == null || g.cancel(), (h = B.current) == null || h.abort()), w(z.LISTENING));
|
|
397
447
|
},
|
|
398
|
-
onSpeechEnd: (
|
|
399
|
-
var
|
|
400
|
-
|
|
448
|
+
onSpeechEnd: (g) => {
|
|
449
|
+
var h;
|
|
450
|
+
s === "webspeech" && J.current === z.SPEAKING || (w(z.TRANSCRIBING), ge.current = performance.now(), (h = H.current) == null || h.postMessage({ type: "transcribe", audio: g }, [g.buffer]));
|
|
401
451
|
},
|
|
402
|
-
onError: (
|
|
403
|
-
|
|
452
|
+
onError: (g) => {
|
|
453
|
+
A(g), w(z.IDLE);
|
|
404
454
|
},
|
|
405
|
-
deviceId:
|
|
455
|
+
deviceId: M || void 0
|
|
406
456
|
});
|
|
407
|
-
} catch (
|
|
408
|
-
console.error("[TZVoiceChat] startListening failed:",
|
|
457
|
+
} catch (g) {
|
|
458
|
+
console.error("[TZVoiceChat] startListening failed:", g), A("Microphone access failed. Allow audio permissions and refresh."), w(z.IDLE), C(!1);
|
|
409
459
|
}
|
|
410
460
|
}
|
|
411
|
-
function
|
|
412
|
-
var
|
|
413
|
-
|
|
461
|
+
function $e() {
|
|
462
|
+
var _, T, g, h, N;
|
|
463
|
+
C(!1), (_ = U.current) == null || _.destroy(), U.current = null, (T = ue.current) == null || T.getTracks().forEach((Q) => Q.stop()), ue.current = null, (g = le.current) == null || g.close(), le.current = null, ee(null), (h = K.current) == null || h.cancel(), (N = B.current) == null || N.abort(), w(z.IDLE);
|
|
414
464
|
}
|
|
415
|
-
function
|
|
416
|
-
var
|
|
417
|
-
if (!(
|
|
418
|
-
|
|
419
|
-
const
|
|
420
|
-
[...
|
|
421
|
-
var
|
|
422
|
-
return (
|
|
423
|
-
}), (
|
|
424
|
-
|
|
465
|
+
function xe(_) {
|
|
466
|
+
var N;
|
|
467
|
+
if (!(_ != null && _.trim())) return;
|
|
468
|
+
w(z.SPEAKING);
|
|
469
|
+
const T = Me(), g = T.write(_), h = T.flush();
|
|
470
|
+
[...g, ...h ? [h] : []].forEach((Q) => {
|
|
471
|
+
var F;
|
|
472
|
+
return (F = K.current) == null ? void 0 : F.enqueue(Q);
|
|
473
|
+
}), (N = K.current) == null || N.finished().then(() => {
|
|
474
|
+
w(Z.current ? z.LISTENING : z.IDLE);
|
|
425
475
|
});
|
|
426
476
|
}
|
|
427
477
|
return {
|
|
428
478
|
/** Current pipeline state: 'idle' | 'listening' | 'transcribing' | 'thinking' | 'speaking' */
|
|
429
|
-
status:
|
|
479
|
+
status: m,
|
|
430
480
|
/** True while models (Whisper + TTS) are still loading. */
|
|
431
|
-
loading:
|
|
481
|
+
loading: I,
|
|
432
482
|
/** Per-model load progress: { whisper: 0–1, kokoro: 0–1 } */
|
|
433
|
-
progress:
|
|
483
|
+
progress: L,
|
|
434
484
|
/** Last error string, or null. Cleared on startListening(). */
|
|
435
|
-
error:
|
|
436
|
-
/** Completed conversation turns: [{ role, content, time }] */
|
|
437
|
-
transcript:
|
|
485
|
+
error: E,
|
|
486
|
+
/** Completed conversation turns: [{ id, role, content, time, timings }]. timings (ms): user → transcribeMs; assistant → thinkMs (time to first token), ttsMs (first sentence queued → audio starts). */
|
|
487
|
+
transcript: P,
|
|
438
488
|
/** Partial assistant response being streamed, or null. */
|
|
439
|
-
streamingMessage:
|
|
489
|
+
streamingMessage: k,
|
|
440
490
|
/** Whether the microphone is currently open. */
|
|
441
|
-
micEnabled:
|
|
491
|
+
micEnabled: $,
|
|
442
492
|
/** Open the microphone and start the VAD → STT → AI → TTS pipeline. */
|
|
443
|
-
startListening:
|
|
493
|
+
startListening: Le,
|
|
444
494
|
/** Close the microphone and cancel any in-progress pipeline. */
|
|
445
|
-
stopListening:
|
|
495
|
+
stopListening: $e,
|
|
446
496
|
/** Directly synthesise and play text via the configured TTS provider. */
|
|
447
|
-
speak:
|
|
497
|
+
speak: xe,
|
|
448
498
|
/** Submit text as a user message, triggering the full AI → TTS pipeline (same as voice transcription). */
|
|
449
|
-
submitText:
|
|
499
|
+
submitText: be,
|
|
450
500
|
/** AnalyserNode for real-time waveform rendering, or null when mic is off. */
|
|
451
|
-
liveAnalyser:
|
|
501
|
+
liveAnalyser: ce,
|
|
452
502
|
/** Available audio input devices (updated on permission grant). */
|
|
453
|
-
devices:
|
|
503
|
+
devices: W,
|
|
454
504
|
/** Currently selected device ID ('': system default). */
|
|
455
|
-
selectedMicId:
|
|
505
|
+
selectedMicId: M,
|
|
456
506
|
/** Select an audio input device by its deviceId. */
|
|
457
|
-
setSelectedMicId:
|
|
507
|
+
setSelectedMicId: y
|
|
458
508
|
};
|
|
459
509
|
}
|
|
460
|
-
function
|
|
461
|
-
const
|
|
510
|
+
function Ne(e) {
|
|
511
|
+
const r = e.replace("#", ""), s = r.length === 3 ? r.split("").map((a) => a + a).join("") : r;
|
|
462
512
|
return [
|
|
463
|
-
parseInt(
|
|
464
|
-
parseInt(
|
|
465
|
-
parseInt(
|
|
513
|
+
parseInt(s.slice(0, 2), 16),
|
|
514
|
+
parseInt(s.slice(2, 4), 16),
|
|
515
|
+
parseInt(s.slice(4, 6), 16)
|
|
466
516
|
];
|
|
467
517
|
}
|
|
468
|
-
function
|
|
469
|
-
e /= 255,
|
|
470
|
-
const
|
|
471
|
-
if (
|
|
472
|
-
const
|
|
473
|
-
let
|
|
474
|
-
switch (
|
|
518
|
+
function Be(e, r, s) {
|
|
519
|
+
e /= 255, r /= 255, s /= 255;
|
|
520
|
+
const a = Math.max(e, r, s), t = Math.min(e, r, s), i = (a + t) / 2;
|
|
521
|
+
if (a === t) return [0, 0, i * 100];
|
|
522
|
+
const n = a - t, l = i > 0.5 ? n / (2 - a - t) : n / (a + t);
|
|
523
|
+
let o;
|
|
524
|
+
switch (a) {
|
|
475
525
|
case e:
|
|
476
|
-
|
|
526
|
+
o = ((r - s) / n + (r < s ? 6 : 0)) / 6;
|
|
477
527
|
break;
|
|
478
|
-
case
|
|
479
|
-
|
|
528
|
+
case r:
|
|
529
|
+
o = ((s - e) / n + 2) / 6;
|
|
480
530
|
break;
|
|
481
531
|
default:
|
|
482
|
-
|
|
532
|
+
o = ((e - r) / n + 4) / 6;
|
|
483
533
|
}
|
|
484
|
-
return [
|
|
534
|
+
return [o * 360, l * 100, i * 100];
|
|
485
535
|
}
|
|
486
|
-
function
|
|
487
|
-
|
|
488
|
-
const
|
|
489
|
-
return [Math.round(
|
|
536
|
+
function je(e, r, s) {
|
|
537
|
+
r /= 100, s /= 100;
|
|
538
|
+
const a = (n) => (n + e / 30) % 12, t = r * Math.min(s, 1 - s), i = (n) => s - t * Math.max(-1, Math.min(a(n) - 3, Math.min(9 - a(n), 1)));
|
|
539
|
+
return [Math.round(i(0) * 255), Math.round(i(8) * 255), Math.round(i(4) * 255)];
|
|
490
540
|
}
|
|
491
|
-
function
|
|
492
|
-
const [
|
|
493
|
-
return `rgb(${
|
|
541
|
+
function G(e, r) {
|
|
542
|
+
const [s, a, t] = Ne(e), [i, n, l] = Be(s, a, t), [o, f, c] = je(i, n, Math.max(0, Math.min(100, l + r)));
|
|
543
|
+
return `rgb(${o}, ${f}, ${c})`;
|
|
494
544
|
}
|
|
495
|
-
function
|
|
496
|
-
const [
|
|
497
|
-
return `rgba(${
|
|
545
|
+
function O(e, r) {
|
|
546
|
+
const [s, a, t] = Ne(e);
|
|
547
|
+
return `rgba(${s}, ${a}, ${t}, ${r})`;
|
|
498
548
|
}
|
|
499
|
-
function
|
|
500
|
-
return e.replace("rgb(", "rgba(").replace(")", `, ${
|
|
549
|
+
function X(e, r) {
|
|
550
|
+
return e.replace("rgb(", "rgba(").replace(")", `, ${r})`);
|
|
501
551
|
}
|
|
502
|
-
const
|
|
503
|
-
function
|
|
504
|
-
const
|
|
505
|
-
return
|
|
506
|
-
if (cancelAnimationFrame(
|
|
507
|
-
const
|
|
508
|
-
if (!
|
|
509
|
-
const
|
|
510
|
-
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
const
|
|
514
|
-
for (let
|
|
515
|
-
const
|
|
516
|
-
|
|
552
|
+
const ae = 10;
|
|
553
|
+
function Fe({ analyser: e }) {
|
|
554
|
+
const r = R(null), s = R(null);
|
|
555
|
+
return j(() => {
|
|
556
|
+
if (cancelAnimationFrame(s.current), !e) return;
|
|
557
|
+
const a = r.current;
|
|
558
|
+
if (!a) return;
|
|
559
|
+
const t = a.getContext("2d"), i = e.frequencyBinCount, n = new Uint8Array(i), l = Math.max(1, Math.floor(i / ae)), o = getComputedStyle(a), f = +o.getPropertyValue("--bar-hue"), c = +o.getPropertyValue("--bar-hue-range"), v = +o.getPropertyValue("--bar-sat"), d = +o.getPropertyValue("--bar-lit"), u = +o.getPropertyValue("--bar-lit-range"), m = () => {
|
|
560
|
+
s.current = requestAnimationFrame(m), e.getByteFrequencyData(n);
|
|
561
|
+
const w = a.width, I = a.height;
|
|
562
|
+
t.clearRect(0, 0, w, I);
|
|
563
|
+
const x = (w - (ae - 1) * 2) / ae;
|
|
564
|
+
for (let L = 0; L < ae; L++) {
|
|
565
|
+
const b = n[L * l] / 255, E = Math.max(3, b * I * 0.88);
|
|
566
|
+
t.fillStyle = `hsl(${f + b * c}, ${v}%, ${d + b * u}%)`, t.beginPath(), t.roundRect(L * (x + 2), (I - E) / 2, x, E, [2]), t.fill();
|
|
517
567
|
}
|
|
518
568
|
};
|
|
519
|
-
return
|
|
520
|
-
}, [e]), /* @__PURE__ */
|
|
569
|
+
return m(), () => cancelAnimationFrame(s.current);
|
|
570
|
+
}, [e]), /* @__PURE__ */ p("canvas", { ref: r, width: 55, height: 35, className: "ai-btn__canvas" });
|
|
521
571
|
}
|
|
522
|
-
function
|
|
572
|
+
function We({
|
|
523
573
|
status: e = "idle",
|
|
524
|
-
active:
|
|
525
|
-
loading:
|
|
526
|
-
analyser:
|
|
527
|
-
onToggle:
|
|
528
|
-
devices:
|
|
529
|
-
selectedMicId:
|
|
530
|
-
onDeviceChange:
|
|
574
|
+
active: r = !1,
|
|
575
|
+
loading: s = !1,
|
|
576
|
+
analyser: a = null,
|
|
577
|
+
onToggle: t,
|
|
578
|
+
devices: i = [],
|
|
579
|
+
selectedMicId: n = "",
|
|
580
|
+
onDeviceChange: l,
|
|
531
581
|
// ── Manual input ───────────────────────────────────────────
|
|
532
|
-
inputData:
|
|
533
|
-
sendData:
|
|
582
|
+
inputData: o = "",
|
|
583
|
+
sendData: f,
|
|
534
584
|
// ── Theming ────────────────────────────────────────────────
|
|
535
585
|
accentColor: c = "#f97316",
|
|
536
|
-
uiColor:
|
|
537
|
-
size:
|
|
538
|
-
barHue:
|
|
539
|
-
barHueRange:
|
|
540
|
-
barSat:
|
|
541
|
-
barLit:
|
|
542
|
-
barLitRange:
|
|
586
|
+
uiColor: v = "#64748b",
|
|
587
|
+
size: d = 70,
|
|
588
|
+
barHue: u = 25,
|
|
589
|
+
barHueRange: m = 20,
|
|
590
|
+
barSat: w = 88,
|
|
591
|
+
barLit: I = 65,
|
|
592
|
+
barLitRange: x = 20
|
|
543
593
|
}) {
|
|
544
|
-
var
|
|
545
|
-
const [
|
|
546
|
-
|
|
547
|
-
if (!
|
|
548
|
-
function
|
|
549
|
-
var
|
|
550
|
-
(
|
|
594
|
+
var C;
|
|
595
|
+
const [L, b] = q(!1), E = R(null);
|
|
596
|
+
j(() => {
|
|
597
|
+
if (!L) return;
|
|
598
|
+
function M(y) {
|
|
599
|
+
var W;
|
|
600
|
+
(W = E.current) != null && W.contains(y.target) || b(!1);
|
|
551
601
|
}
|
|
552
|
-
return document.addEventListener("pointerdown",
|
|
553
|
-
}, [
|
|
554
|
-
function
|
|
555
|
-
|
|
602
|
+
return document.addEventListener("pointerdown", M), () => document.removeEventListener("pointerdown", M);
|
|
603
|
+
}, [L]);
|
|
604
|
+
function A(M) {
|
|
605
|
+
l == null || l(M), b(!1);
|
|
556
606
|
}
|
|
557
|
-
const
|
|
558
|
-
const
|
|
607
|
+
const P = Re(() => {
|
|
608
|
+
const M = G(c, -8), y = G(c, -15), W = G(c, -22), oe = G(c, -30), ce = G(c, -42), ee = G(c, 8), J = G(c, 20), Z = G(c, 30), K = G(v, -22), U = G(v, -30), B = G(v, 18), H = G(v, 37);
|
|
559
609
|
return {
|
|
560
|
-
"--tz-size": `${
|
|
610
|
+
"--tz-size": `${d}px`,
|
|
561
611
|
// Accent shades
|
|
562
612
|
"--tz-a": c,
|
|
563
|
-
"--tz-a-d8":
|
|
564
|
-
"--tz-a-d15":
|
|
565
|
-
"--tz-a-d22":
|
|
566
|
-
"--tz-a-d30":
|
|
567
|
-
"--tz-a-d42":
|
|
568
|
-
"--tz-a-l8":
|
|
569
|
-
"--tz-a-l20":
|
|
570
|
-
"--tz-a-l30":
|
|
613
|
+
"--tz-a-d8": M,
|
|
614
|
+
"--tz-a-d15": y,
|
|
615
|
+
"--tz-a-d22": W,
|
|
616
|
+
"--tz-a-d30": oe,
|
|
617
|
+
"--tz-a-d42": ce,
|
|
618
|
+
"--tz-a-l8": ee,
|
|
619
|
+
"--tz-a-l20": J,
|
|
620
|
+
"--tz-a-l30": Z,
|
|
571
621
|
// Accent + alpha
|
|
572
|
-
"--tz-a-25":
|
|
573
|
-
"--tz-a-28":
|
|
574
|
-
"--tz-a-30":
|
|
575
|
-
"--tz-a-32":
|
|
576
|
-
"--tz-a-34":
|
|
577
|
-
"--tz-a-35":
|
|
578
|
-
"--tz-a-38":
|
|
579
|
-
"--tz-a-45":
|
|
580
|
-
"--tz-a-50":
|
|
581
|
-
"--tz-a-55":
|
|
582
|
-
"--tz-a-60":
|
|
583
|
-
"--tz-a-65":
|
|
584
|
-
"--tz-a-68":
|
|
622
|
+
"--tz-a-25": O(c, 0.25),
|
|
623
|
+
"--tz-a-28": O(c, 0.28),
|
|
624
|
+
"--tz-a-30": O(c, 0.3),
|
|
625
|
+
"--tz-a-32": O(c, 0.32),
|
|
626
|
+
"--tz-a-34": O(c, 0.34),
|
|
627
|
+
"--tz-a-35": O(c, 0.35),
|
|
628
|
+
"--tz-a-38": O(c, 0.38),
|
|
629
|
+
"--tz-a-45": O(c, 0.45),
|
|
630
|
+
"--tz-a-50": O(c, 0.5),
|
|
631
|
+
"--tz-a-55": O(c, 0.55),
|
|
632
|
+
"--tz-a-60": O(c, 0.6),
|
|
633
|
+
"--tz-a-65": O(c, 0.65),
|
|
634
|
+
"--tz-a-68": O(c, 0.68),
|
|
585
635
|
// UI shades
|
|
586
|
-
"--tz-u":
|
|
587
|
-
"--tz-u-d22":
|
|
588
|
-
"--tz-u-d30":
|
|
589
|
-
"--tz-u-l18":
|
|
636
|
+
"--tz-u": v,
|
|
637
|
+
"--tz-u-d22": K,
|
|
638
|
+
"--tz-u-d30": U,
|
|
639
|
+
"--tz-u-l18": B,
|
|
590
640
|
"--tz-u-l37": H,
|
|
591
641
|
// UI + alpha
|
|
592
|
-
"--tz-u-30":
|
|
593
|
-
"--tz-u-55":
|
|
594
|
-
"--tz-u-d30-a85":
|
|
595
|
-
"--tz-u-l18-a18":
|
|
596
|
-
"--tz-u-l18-a20":
|
|
597
|
-
"--tz-u-l18-a32":
|
|
598
|
-
"--tz-u-d22-a90":
|
|
642
|
+
"--tz-u-30": O(v, 0.3),
|
|
643
|
+
"--tz-u-55": O(v, 0.55),
|
|
644
|
+
"--tz-u-d30-a85": X(U, 0.85),
|
|
645
|
+
"--tz-u-l18-a18": X(B, 0.18),
|
|
646
|
+
"--tz-u-l18-a20": X(B, 0.2),
|
|
647
|
+
"--tz-u-l18-a32": X(B, 0.32),
|
|
648
|
+
"--tz-u-d22-a90": X(K, 0.9),
|
|
599
649
|
// Waveform bar colors (cascade to canvas via getComputedStyle)
|
|
600
|
-
"--bar-hue":
|
|
601
|
-
"--bar-hue-range":
|
|
602
|
-
"--bar-sat":
|
|
603
|
-
"--bar-lit":
|
|
604
|
-
"--bar-lit-range":
|
|
650
|
+
"--bar-hue": u,
|
|
651
|
+
"--bar-hue-range": m,
|
|
652
|
+
"--bar-sat": w,
|
|
653
|
+
"--bar-lit": I,
|
|
654
|
+
"--bar-lit-range": x
|
|
605
655
|
};
|
|
606
|
-
}, [c,
|
|
607
|
-
let
|
|
608
|
-
return
|
|
609
|
-
/* @__PURE__ */
|
|
610
|
-
/* @__PURE__ */
|
|
611
|
-
/* @__PURE__ */
|
|
612
|
-
] }) : e === "listening" ?
|
|
613
|
-
/* @__PURE__ */
|
|
614
|
-
/* @__PURE__ */
|
|
615
|
-
/* @__PURE__ */
|
|
616
|
-
] }) : e === "speaking" && (
|
|
617
|
-
/* @__PURE__ */
|
|
618
|
-
/* @__PURE__ */
|
|
619
|
-
/* @__PURE__ */
|
|
656
|
+
}, [c, v, d, u, m, w, I, x]), D = ((C = i.find((M) => M.deviceId === n)) == null ? void 0 : C.label) || (n ? `Mic ${n.slice(0, 6)}…` : "Default mic"), k = o.trim().length > 0, V = k ? "ready" : r ? e : "off";
|
|
657
|
+
let $;
|
|
658
|
+
return k ? $ = /* @__PURE__ */ p("svg", { className: "ai-btn__mic", viewBox: "0 0 24 24", fill: "currentColor", "aria-hidden": "true", children: /* @__PURE__ */ p("path", { d: "M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" }) }) : !r || e === "idle" ? $ = /* @__PURE__ */ S("svg", { className: "ai-btn__mic", viewBox: "0 0 24 24", fill: "currentColor", "aria-hidden": "true", children: [
|
|
659
|
+
/* @__PURE__ */ p("path", { fillOpacity: "0.52", d: "M12 1a4 4 0 0 1 4 4v6a4 4 0 0 1-8 0V5a4 4 0 0 1 4-4zm6 10a1 1 0 0 1 2 0 8 8 0 0 1-7 7.94V21h2a1 1 0 0 1 0 2H9a1 1 0 0 1 0-2h2v-2.06A8 8 0 0 1 4 11a1 1 0 0 1 2 0 6 6 0 0 0 12 0z" }),
|
|
660
|
+
/* @__PURE__ */ p("path", { fill: "white", fillOpacity: "0.98", d: "M14.5 0L15.65 3.45L19.1 4.6L15.65 5.75L14.5 9.2L13.35 5.75L9.9 4.6L13.35 3.45Z" }),
|
|
661
|
+
/* @__PURE__ */ p("path", { fill: "white", fillOpacity: "0.88", d: "M19 8.62L19.75 10.75L21.88 11.5L19.75 12.25L19 14.38L18.25 12.25L16.12 11.5L18.25 10.75Z" })
|
|
662
|
+
] }) : e === "listening" ? $ = /* @__PURE__ */ p(Fe, { analyser: a }) : e === "transcribing" ? $ = /* @__PURE__ */ p("div", { className: "ai-btn__bars ai-btn__bars--transcribing", children: Array.from({ length: 7 }, (M, y) => /* @__PURE__ */ p("span", { style: { animationDelay: `${y * 0.1}s` } }, y)) }) : e === "thinking" ? $ = /* @__PURE__ */ S("div", { className: "ai-btn__dots", children: [
|
|
663
|
+
/* @__PURE__ */ p("span", {}),
|
|
664
|
+
/* @__PURE__ */ p("span", { style: { animationDelay: "0.18s" } }),
|
|
665
|
+
/* @__PURE__ */ p("span", { style: { animationDelay: "0.36s" } })
|
|
666
|
+
] }) : e === "speaking" && ($ = /* @__PURE__ */ p("div", { className: "ai-btn__bars ai-btn__bars--speaking", children: Array.from({ length: 7 }, (M, y) => /* @__PURE__ */ p("span", { style: { animationDelay: `${y * 0.11}s` } }, y)) })), /* @__PURE__ */ S("div", { className: "ai-btn-root", style: P, children: [
|
|
667
|
+
/* @__PURE__ */ S("div", { className: `ai-btn-rings ai-btn-rings--${V}`, "aria-hidden": "true", children: [
|
|
668
|
+
/* @__PURE__ */ p("span", { className: "ai-ring ai-ring--1" }),
|
|
669
|
+
/* @__PURE__ */ p("span", { className: "ai-ring ai-ring--2" })
|
|
620
670
|
] }),
|
|
621
|
-
/* @__PURE__ */
|
|
671
|
+
/* @__PURE__ */ S(
|
|
622
672
|
"button",
|
|
623
673
|
{
|
|
624
|
-
className: `ai-btn ai-btn--${
|
|
625
|
-
disabled:
|
|
626
|
-
onClick:
|
|
627
|
-
"aria-label":
|
|
628
|
-
"aria-pressed":
|
|
674
|
+
className: `ai-btn ai-btn--${V}`,
|
|
675
|
+
disabled: s,
|
|
676
|
+
onClick: k ? () => f == null ? void 0 : f(o) : t,
|
|
677
|
+
"aria-label": k ? "Send message" : r ? "Stop assistant" : "Start assistant",
|
|
678
|
+
"aria-pressed": k ? void 0 : r,
|
|
629
679
|
children: [
|
|
630
|
-
|
|
631
|
-
|
|
680
|
+
$,
|
|
681
|
+
r && !k && /* @__PURE__ */ p("span", { className: "ai-btn__hover-stop", "aria-hidden": "true", children: "■" })
|
|
632
682
|
]
|
|
633
683
|
}
|
|
634
684
|
),
|
|
635
|
-
/* @__PURE__ */
|
|
636
|
-
/* @__PURE__ */
|
|
685
|
+
/* @__PURE__ */ S("div", { className: `ai-mic-sel${L ? " open" : ""}`, ref: E, children: [
|
|
686
|
+
/* @__PURE__ */ S(
|
|
637
687
|
"button",
|
|
638
688
|
{
|
|
639
689
|
className: "ai-mic-sel__trigger",
|
|
640
|
-
onClick: () =>
|
|
690
|
+
onClick: () => b((M) => !M),
|
|
641
691
|
title: "Change microphone",
|
|
642
692
|
"aria-label": "Change microphone",
|
|
643
|
-
"aria-expanded":
|
|
693
|
+
"aria-expanded": L,
|
|
644
694
|
children: [
|
|
645
|
-
/* @__PURE__ */
|
|
646
|
-
/* @__PURE__ */
|
|
647
|
-
/* @__PURE__ */
|
|
695
|
+
/* @__PURE__ */ S("svg", { width: "11", height: "11", viewBox: "0 0 24 24", fill: "currentColor", "aria-hidden": "true", children: [
|
|
696
|
+
/* @__PURE__ */ p("path", { d: "M12 1a4 4 0 0 1 4 4v6a4 4 0 0 1-8 0V5a4 4 0 0 1 4-4z" }),
|
|
697
|
+
/* @__PURE__ */ p("path", { d: "M5 11a1 1 0 0 0-2 0 9 9 0 0 0 8 8.94V22H9a1 1 0 0 0 0 2h6a1 1 0 0 0 0-2h-2v-2.06A9 9 0 0 0 21 11a1 1 0 0 0-2 0 7 7 0 0 1-14 0z" })
|
|
648
698
|
] }),
|
|
649
|
-
/* @__PURE__ */
|
|
650
|
-
/* @__PURE__ */
|
|
699
|
+
/* @__PURE__ */ p("span", { className: "ai-mic-sel__label", children: D }),
|
|
700
|
+
/* @__PURE__ */ p(
|
|
651
701
|
"svg",
|
|
652
702
|
{
|
|
653
|
-
className: `ai-mic-sel__caret${
|
|
703
|
+
className: `ai-mic-sel__caret${L ? " open" : ""}`,
|
|
654
704
|
width: "9",
|
|
655
705
|
height: "9",
|
|
656
706
|
viewBox: "0 0 24 24",
|
|
657
707
|
fill: "currentColor",
|
|
658
708
|
"aria-hidden": "true",
|
|
659
|
-
children: /* @__PURE__ */
|
|
709
|
+
children: /* @__PURE__ */ p("path", { d: "M12 16L4 8h16l-8 8z" })
|
|
660
710
|
}
|
|
661
711
|
)
|
|
662
712
|
]
|
|
663
713
|
}
|
|
664
714
|
),
|
|
665
|
-
|
|
666
|
-
/* @__PURE__ */
|
|
715
|
+
L && /* @__PURE__ */ S("ul", { className: "ai-mic-sel__menu", role: "listbox", "aria-label": "Select microphone", children: [
|
|
716
|
+
/* @__PURE__ */ p(
|
|
667
717
|
"li",
|
|
668
718
|
{
|
|
669
719
|
role: "option",
|
|
670
|
-
className: `ai-mic-sel__option${
|
|
671
|
-
onClick: () =>
|
|
672
|
-
"aria-selected":
|
|
720
|
+
className: `ai-mic-sel__option${n === "" ? " selected" : ""}`,
|
|
721
|
+
onClick: () => A(""),
|
|
722
|
+
"aria-selected": n === "",
|
|
673
723
|
children: "Default microphone"
|
|
674
724
|
}
|
|
675
725
|
),
|
|
676
|
-
|
|
726
|
+
i.map((M) => /* @__PURE__ */ p(
|
|
677
727
|
"li",
|
|
678
728
|
{
|
|
679
729
|
role: "option",
|
|
680
|
-
className: `ai-mic-sel__option${
|
|
681
|
-
onClick: () =>
|
|
682
|
-
"aria-selected":
|
|
683
|
-
children:
|
|
730
|
+
className: `ai-mic-sel__option${n === M.deviceId ? " selected" : ""}`,
|
|
731
|
+
onClick: () => A(M.deviceId),
|
|
732
|
+
"aria-selected": n === M.deviceId,
|
|
733
|
+
children: M.label || `Microphone ${M.deviceId.slice(0, 8)}`
|
|
684
734
|
},
|
|
685
|
-
|
|
735
|
+
M.deviceId
|
|
686
736
|
))
|
|
687
737
|
] })
|
|
688
738
|
] })
|
|
689
739
|
] });
|
|
690
740
|
}
|
|
741
|
+
async function Ue(e, r, s = "/api/chat") {
|
|
742
|
+
var l;
|
|
743
|
+
const a = await fetch(s, {
|
|
744
|
+
method: "POST",
|
|
745
|
+
headers: {
|
|
746
|
+
"Content-Type": "application/json"
|
|
747
|
+
},
|
|
748
|
+
signal: r,
|
|
749
|
+
body: JSON.stringify({ messages: e })
|
|
750
|
+
});
|
|
751
|
+
if (!a.ok) {
|
|
752
|
+
const o = await a.text();
|
|
753
|
+
throw new Error(`Server error: ${a.status} ${o}`);
|
|
754
|
+
}
|
|
755
|
+
const t = (l = a.body) == null ? void 0 : l.getReader();
|
|
756
|
+
if (!t) {
|
|
757
|
+
const o = await a.json();
|
|
758
|
+
return { stream: async function* () {
|
|
759
|
+
yield o.text;
|
|
760
|
+
} };
|
|
761
|
+
}
|
|
762
|
+
const i = new TextDecoder();
|
|
763
|
+
let n = "";
|
|
764
|
+
return {
|
|
765
|
+
stream: async function* () {
|
|
766
|
+
var f, c, v;
|
|
767
|
+
let o = "";
|
|
768
|
+
for (; ; ) {
|
|
769
|
+
const { value: d, done: u } = await t.read();
|
|
770
|
+
if (u) break;
|
|
771
|
+
n += i.decode(d, { stream: !0 });
|
|
772
|
+
const m = n.split(`
|
|
773
|
+
`);
|
|
774
|
+
n = m.pop();
|
|
775
|
+
for (const w of m) {
|
|
776
|
+
const I = w.trim();
|
|
777
|
+
if (!I.startsWith("data:")) continue;
|
|
778
|
+
const x = I.slice(5).trimStart();
|
|
779
|
+
if (x === "[DONE]") return;
|
|
780
|
+
try {
|
|
781
|
+
const b = (v = (c = (f = JSON.parse(x).choices) == null ? void 0 : f[0]) == null ? void 0 : c.delta) == null ? void 0 : v.content;
|
|
782
|
+
b && (o && /\w/.test(o) && /\w/.test(b[0]) && (yield " "), o = b[b.length - 1], yield b);
|
|
783
|
+
} catch {
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
function He(e, r, s) {
|
|
791
|
+
return {
|
|
792
|
+
stream: async function* () {
|
|
793
|
+
const a = [];
|
|
794
|
+
let t = null, i = !1, n = "";
|
|
795
|
+
const l = ({ data: f }) => {
|
|
796
|
+
f.type === "token" ? (a.push(f.text), t == null || t(), t = null) : f.type === "done" && (i = !0, t == null || t(), t = null);
|
|
797
|
+
}, o = () => {
|
|
798
|
+
i = !0, t == null || t(), t = null;
|
|
799
|
+
};
|
|
800
|
+
e.addEventListener("message", l), s == null || s.addEventListener("abort", o), e.postMessage({ type: "generate", messages: r });
|
|
801
|
+
try {
|
|
802
|
+
for (; !i || a.length > 0; )
|
|
803
|
+
if (a.length > 0) {
|
|
804
|
+
const f = a.shift();
|
|
805
|
+
n && /\w/.test(n) && /\w/.test(f[0]) && (yield " "), n = f[f.length - 1], yield f;
|
|
806
|
+
} else
|
|
807
|
+
await new Promise((f) => {
|
|
808
|
+
t = f;
|
|
809
|
+
});
|
|
810
|
+
} finally {
|
|
811
|
+
e.removeEventListener("message", l), s == null || s.removeEventListener("abort", o);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
function Qe({ apiBase: e = "/api", systemPrompt: r } = {}) {
|
|
817
|
+
const [s, a] = q(!0), [t, i] = q(0), [n, l] = q(""), [o, f] = q({}), [c, v] = q(null), [d, u] = q({}), m = R(null), w = R(null);
|
|
818
|
+
j(() => {
|
|
819
|
+
let b = !1;
|
|
820
|
+
async function E() {
|
|
821
|
+
let A = !0, P = {};
|
|
822
|
+
try {
|
|
823
|
+
const k = await (await fetch(`${e}/config`)).json();
|
|
824
|
+
A = k.hasApiKey, v(k), P = {
|
|
825
|
+
ttsProvider: k.ttsProvider || "server-kokoro",
|
|
826
|
+
ttsVoice: k.ttsProvider === "openai" ? k.openaiTtsVoice || "alloy" : k.kokoroVoice || "af_bella",
|
|
827
|
+
ttsServerUrl: `${e}/tts`,
|
|
828
|
+
ttsSpeed: k.kokoroSpeed ?? 1,
|
|
829
|
+
ttsSpeechRate: k.webSpeechRate ?? 1,
|
|
830
|
+
ttsSpeechPitch: k.webSpeechPitch ?? 1,
|
|
831
|
+
whisperModel: k.whisperModel || "onnx-community/whisper-tiny.en",
|
|
832
|
+
whisperDtype: k.whisperDtype || "q8",
|
|
833
|
+
whisperDevice: k.whisperDevice || "cpu",
|
|
834
|
+
kokoroDtype: k.kokoroDtype || "q8",
|
|
835
|
+
kokoroBrowserDevice: k.kokoroBrowserDevice || "wasm",
|
|
836
|
+
modelProxyUrl: `${e}/models/`
|
|
837
|
+
};
|
|
838
|
+
} catch {
|
|
839
|
+
P = { ttsProvider: "server-kokoro", ttsServerUrl: `${e}/tts`, modelProxyUrl: `${e}/models/` };
|
|
840
|
+
}
|
|
841
|
+
if (!b)
|
|
842
|
+
if (A)
|
|
843
|
+
m.current = async function* (D, k, V) {
|
|
844
|
+
const { stream: $ } = await Ue(k, V, `${e}/chat`);
|
|
845
|
+
yield* $();
|
|
846
|
+
}, f(P), a(!1);
|
|
847
|
+
else {
|
|
848
|
+
l("No API key — using SmolLM2-360M-Instruct locally in your browser.");
|
|
849
|
+
const D = new Worker(new URL(
|
|
850
|
+
/* @vite-ignore */
|
|
851
|
+
"./assets/llmWorker-CVEGn6yI.js",
|
|
852
|
+
import.meta.url
|
|
853
|
+
), { type: "module" });
|
|
854
|
+
w.current = D, D.onmessage = (k) => {
|
|
855
|
+
k.data.type === "progress" ? i(k.data.progress ?? 0) : k.data.type === "ready" ? (i(1), m.current = async function* (V, $, C) {
|
|
856
|
+
const { stream: M } = He(D, $, C);
|
|
857
|
+
yield* M();
|
|
858
|
+
}, f(P), a(!1)) : k.data.type === "error" && a(!1);
|
|
859
|
+
}, D.onerror = () => a(!1), D.postMessage({ type: "init", modelProxyUrl: `${e}/models/` });
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
return E(), () => {
|
|
863
|
+
var A;
|
|
864
|
+
b = !0, (A = w.current) == null || A.terminate();
|
|
865
|
+
};
|
|
866
|
+
}, [e]);
|
|
867
|
+
const I = Ce({
|
|
868
|
+
processInput: async function* (b, E, A) {
|
|
869
|
+
m.current && (yield* m.current(b, E, A));
|
|
870
|
+
},
|
|
871
|
+
...r ? { systemPrompt: r } : {},
|
|
872
|
+
...o
|
|
873
|
+
});
|
|
874
|
+
j(() => {
|
|
875
|
+
let b = !1;
|
|
876
|
+
async function E() {
|
|
877
|
+
const P = {};
|
|
878
|
+
typeof performance < "u" && performance.memory && (P.browser = performance.memory.usedJSHeapSize);
|
|
879
|
+
try {
|
|
880
|
+
const D = await fetch(`${e}/stats`);
|
|
881
|
+
D.ok && (P.server = (await D.json()).rss);
|
|
882
|
+
} catch {
|
|
883
|
+
}
|
|
884
|
+
b || u(P);
|
|
885
|
+
}
|
|
886
|
+
E();
|
|
887
|
+
const A = setInterval(E, 3e3);
|
|
888
|
+
return () => {
|
|
889
|
+
b = !0, clearInterval(A);
|
|
890
|
+
};
|
|
891
|
+
}, [e]);
|
|
892
|
+
const { micEnabled: x, stopListening: L } = I;
|
|
893
|
+
return j(() => {
|
|
894
|
+
if (!x) return;
|
|
895
|
+
const b = (E) => {
|
|
896
|
+
E.key === "Escape" && L();
|
|
897
|
+
};
|
|
898
|
+
return window.addEventListener("keydown", b), () => window.removeEventListener("keydown", b);
|
|
899
|
+
}, [x, L]), {
|
|
900
|
+
chat: I,
|
|
901
|
+
info: c,
|
|
902
|
+
config: o,
|
|
903
|
+
memory: d,
|
|
904
|
+
llmProgress: t,
|
|
905
|
+
note: n,
|
|
906
|
+
loading: I.loading || s
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
const Ae = (e, r) => e === "webgpu" && (r === "q4" || r === "q8") ? "fp32" : r, we = (e) => e >= 1e3 ? `${(e / 1e3).toFixed(1)} s` : `${e} ms`, Se = (e) => `${(e / 1048576).toFixed(0)} MB`;
|
|
910
|
+
function Je(e) {
|
|
911
|
+
switch (e.ttsProvider) {
|
|
912
|
+
case "openai":
|
|
913
|
+
return `OpenAI · ${e.openaiTtsModel} · ${e.ttsVoice}`;
|
|
914
|
+
case "webspeech":
|
|
915
|
+
return `Web Speech API · rate ${e.ttsSpeechRate} · pitch ${e.ttsSpeechPitch}`;
|
|
916
|
+
case "server-kokoro":
|
|
917
|
+
return `Kokoro (server) · ${e.ttsVoice} · ${e.kokoroDtype}/${e.kokoroDevice}`;
|
|
918
|
+
default:
|
|
919
|
+
return `Kokoro (browser) · ${e.ttsVoice} · ${Ae(e.kokoroBrowserDevice, e.kokoroDtype)}/${e.kokoroBrowserDevice}`;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
const Ze = (e) => e.replace(/\b\w/g, (r) => r.toUpperCase());
|
|
923
|
+
function et({
|
|
924
|
+
apiBase: e = "/api",
|
|
925
|
+
title: r = "Voice Assistant",
|
|
926
|
+
showInfo: s = !0,
|
|
927
|
+
systemPrompt: a,
|
|
928
|
+
className: t = ""
|
|
929
|
+
} = {}) {
|
|
930
|
+
const { chat: i, info: n, config: l, memory: o, llmProgress: f, note: c, loading: v } = Qe({ apiBase: e, systemPrompt: a }), {
|
|
931
|
+
status: d,
|
|
932
|
+
progress: u,
|
|
933
|
+
error: m,
|
|
934
|
+
transcript: w,
|
|
935
|
+
streamingMessage: I,
|
|
936
|
+
micEnabled: x,
|
|
937
|
+
startListening: L,
|
|
938
|
+
stopListening: b,
|
|
939
|
+
submitText: E,
|
|
940
|
+
liveAnalyser: A,
|
|
941
|
+
devices: P,
|
|
942
|
+
selectedMicId: D,
|
|
943
|
+
setSelectedMicId: k
|
|
944
|
+
} = i, [V, $] = q(""), C = R(null);
|
|
945
|
+
j(() => {
|
|
946
|
+
var y;
|
|
947
|
+
(y = C.current) == null || y.scrollIntoView({ behavior: I !== null ? "auto" : "smooth" });
|
|
948
|
+
}, [w, I]);
|
|
949
|
+
const M = n && l.whisperDevice && [
|
|
950
|
+
["STT", `Whisper · ${(l.whisperModel || n.whisperModel).split("/").pop()} · ${Ae(l.whisperDevice, l.whisperDtype)}/${l.whisperDevice}`],
|
|
951
|
+
["TTS", Je({ ...n, ...l })],
|
|
952
|
+
["Chat", n.hasApiKey ? `${n.chatProvider} · ${n.chatModel}` : "SmolLM2-360M-Instruct (in-browser)"],
|
|
953
|
+
["Memory", [
|
|
954
|
+
o.browser != null ? `browser heap ${Se(o.browser)}` : "browser heap n/a",
|
|
955
|
+
o.server != null ? `server ${Se(o.server)}` : null
|
|
956
|
+
].filter(Boolean).join(" · ")]
|
|
957
|
+
];
|
|
958
|
+
return /* @__PURE__ */ S("div", { className: `tz-va ${t}`.trim(), children: [
|
|
959
|
+
/* @__PURE__ */ S("section", { className: "tz-va__card tz-va__header", children: [
|
|
960
|
+
/* @__PURE__ */ p("h1", { children: r }),
|
|
961
|
+
/* @__PURE__ */ p("span", { className: `tz-va__chip tz-va__chip--${d}`, children: Ze(d) })
|
|
962
|
+
] }),
|
|
963
|
+
s && M && /* @__PURE__ */ p("section", { className: "tz-va__card tz-va__info", children: M.map(([y, W]) => /* @__PURE__ */ S("div", { className: "tz-va__info-row", children: [
|
|
964
|
+
/* @__PURE__ */ p("span", { className: "tz-va__info-label", children: y }),
|
|
965
|
+
/* @__PURE__ */ p("span", { className: "tz-va__info-value", children: W })
|
|
966
|
+
] }, y)) }),
|
|
967
|
+
/* @__PURE__ */ S("section", { className: "tz-va__card", children: [
|
|
968
|
+
v && /* @__PURE__ */ S("div", { className: "tz-va__progress", children: [
|
|
969
|
+
/* @__PURE__ */ S("label", { children: [
|
|
970
|
+
"Whisper STT",
|
|
971
|
+
/* @__PURE__ */ p("div", { className: "tz-va__bar", children: /* @__PURE__ */ p("span", { style: { width: `${u.whisper * 100}%` } }) })
|
|
972
|
+
] }),
|
|
973
|
+
/* @__PURE__ */ S("label", { children: [
|
|
974
|
+
"Kokoro TTS",
|
|
975
|
+
/* @__PURE__ */ p("div", { className: "tz-va__bar", children: /* @__PURE__ */ p("span", { style: { width: `${u.kokoro * 100}%` } }) })
|
|
976
|
+
] }),
|
|
977
|
+
f > 0 && /* @__PURE__ */ S("label", { children: [
|
|
978
|
+
"Local LLM",
|
|
979
|
+
/* @__PURE__ */ p("div", { className: "tz-va__bar", children: /* @__PURE__ */ p("span", { style: { width: `${f * 100}%` } }) })
|
|
980
|
+
] })
|
|
981
|
+
] }),
|
|
982
|
+
/* @__PURE__ */ p("div", { className: "tz-va__button", children: /* @__PURE__ */ p(
|
|
983
|
+
We,
|
|
984
|
+
{
|
|
985
|
+
status: d,
|
|
986
|
+
active: x,
|
|
987
|
+
loading: v,
|
|
988
|
+
analyser: A,
|
|
989
|
+
onToggle: x ? b : L,
|
|
990
|
+
devices: P,
|
|
991
|
+
selectedMicId: D,
|
|
992
|
+
onDeviceChange: k,
|
|
993
|
+
inputData: V,
|
|
994
|
+
sendData: (y) => {
|
|
995
|
+
$(""), E(y);
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
) }),
|
|
999
|
+
/* @__PURE__ */ p("div", { className: "tz-va__input-row", children: /* @__PURE__ */ p(
|
|
1000
|
+
"input",
|
|
1001
|
+
{
|
|
1002
|
+
type: "text",
|
|
1003
|
+
className: "tz-va__input",
|
|
1004
|
+
value: V,
|
|
1005
|
+
onChange: (y) => $(y.target.value),
|
|
1006
|
+
onKeyDown: (y) => {
|
|
1007
|
+
y.key === "Enter" && V.trim() && ($(""), E(V));
|
|
1008
|
+
},
|
|
1009
|
+
placeholder: "Type a message…",
|
|
1010
|
+
disabled: v
|
|
1011
|
+
}
|
|
1012
|
+
) }),
|
|
1013
|
+
/* @__PURE__ */ p("p", { className: "tz-va__note", children: v ? u.kokoro < 1 ? "Loading Kokoro TTS…" : u.whisper < 1 ? "Loading Whisper STT…" : f > 0 && f < 1 ? "Loading local LLM (SmolLM2-360M, ~200 MB)…" : "Finishing up…" : "Ready." }),
|
|
1014
|
+
c && /* @__PURE__ */ p("p", { className: "tz-va__note", children: c })
|
|
1015
|
+
] }),
|
|
1016
|
+
/* @__PURE__ */ S("section", { className: "tz-va__card tz-va__transcript", children: [
|
|
1017
|
+
w.map((y) => /* @__PURE__ */ S("div", { className: `tz-va__message tz-va__message--${y.role}`, children: [
|
|
1018
|
+
/* @__PURE__ */ p("small", { children: y.role === "user" ? "You" : "Assistant" }),
|
|
1019
|
+
/* @__PURE__ */ p("p", { children: y.content }),
|
|
1020
|
+
y.timings && /* @__PURE__ */ S("div", { className: "tz-va__timings", children: [
|
|
1021
|
+
y.timings.transcribeMs != null && /* @__PURE__ */ S("span", { children: [
|
|
1022
|
+
"transcribe ",
|
|
1023
|
+
we(y.timings.transcribeMs)
|
|
1024
|
+
] }),
|
|
1025
|
+
y.timings.thinkMs != null && /* @__PURE__ */ S("span", { children: [
|
|
1026
|
+
"think ",
|
|
1027
|
+
we(y.timings.thinkMs)
|
|
1028
|
+
] }),
|
|
1029
|
+
y.timings.ttsMs != null && /* @__PURE__ */ S("span", { children: [
|
|
1030
|
+
"tts ",
|
|
1031
|
+
we(y.timings.ttsMs)
|
|
1032
|
+
] })
|
|
1033
|
+
] })
|
|
1034
|
+
] }, y.id)),
|
|
1035
|
+
I !== null && /* @__PURE__ */ S("div", { className: "tz-va__message tz-va__message--assistant tz-va__message--streaming", children: [
|
|
1036
|
+
/* @__PURE__ */ p("small", { children: "Assistant" }),
|
|
1037
|
+
/* @__PURE__ */ S("p", { children: [
|
|
1038
|
+
I,
|
|
1039
|
+
/* @__PURE__ */ p("span", { className: "tz-va__cursor" })
|
|
1040
|
+
] })
|
|
1041
|
+
] }),
|
|
1042
|
+
/* @__PURE__ */ p("div", { ref: C })
|
|
1043
|
+
] }),
|
|
1044
|
+
m ? /* @__PURE__ */ S("section", { className: "tz-va__card", children: [
|
|
1045
|
+
/* @__PURE__ */ p("strong", { children: "Error:" }),
|
|
1046
|
+
/* @__PURE__ */ p("p", { children: m })
|
|
1047
|
+
] }) : null
|
|
1048
|
+
] });
|
|
1049
|
+
}
|
|
691
1050
|
export {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
1051
|
+
We as AIAssistantButton,
|
|
1052
|
+
z as STATES,
|
|
1053
|
+
Ce as TZVoiceChat,
|
|
1054
|
+
et as VoiceAssistant,
|
|
1055
|
+
He as createLocalChatStream,
|
|
1056
|
+
Ue as sendChatRequest,
|
|
1057
|
+
Qe as useVoiceAssistant
|
|
695
1058
|
};
|