@burdenoff/fe-libs 2026.1114.13 → 2026.1114.14
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/shared/assistant/conversation.d.ts +16 -0
- package/dist/shared/assistant/i18nManifest.d.ts +30 -0
- package/dist/shared/assistant/i18nManifest.js +30 -0
- package/dist/shared/assistant/index.d.ts +6 -0
- package/dist/shared/assistant/store.d.ts +7 -0
- package/dist/shared/assistant/store.js +3 -0
- package/dist/shared/assistant/ui/AssistantChatArea.js +1269 -1188
- package/dist/shared/assistant/ui/AssistantMessageList.d.ts +14 -0
- package/dist/shared/assistant/ui/AssistantMessageList.js +55 -52
- package/dist/shared/assistant/ui/composer/AssistantComposer.js +387 -395
- package/dist/shared/assistant/ui/composer/AssistantDictation.d.ts +42 -0
- package/dist/shared/assistant/ui/composer/AssistantDictation.js +127 -0
- package/dist/shared/assistant/ui/composer/AssistantMicMenu.d.ts +23 -0
- package/dist/shared/assistant/ui/composer/AssistantMicMenu.js +176 -0
- package/dist/shared/assistant/ui/composer/microphones.d.ts +13 -0
- package/dist/shared/assistant/ui/composer/microphones.js +15 -0
- package/dist/shared/assistant/ui/composer/useCompactDictation.d.ts +70 -0
- package/dist/shared/assistant/ui/composer/useCompactDictation.js +136 -0
- package/dist/shared/assistant/ui/useVoiceSession.js +166 -164
- package/dist/shared/assistant/ui/voice/AssistantPageVoiceBar.d.ts +20 -17
- package/dist/shared/assistant/ui/voice/AssistantPageVoiceBar.js +230 -123
- package/dist/shared/assistant/ui/voice/AssistantVoiceSession.d.ts +2 -0
- package/dist/shared/assistant/ui/voice/AssistantVoiceSession.js +227 -229
- package/dist/shared/assistant/ui/voice/AssistantVoiceSessionButton.d.ts +5 -0
- package/dist/shared/assistant/ui/voice/AssistantVoiceSessionButton.js +6 -6
- package/dist/shared/assistant/ui/voice/LiveWaveform.d.ts +4 -3
- package/dist/shared/assistant/ui/voice/LiveWaveform.js +1 -1
- package/dist/shared/assistant/ui/voice/VoiceTurnMarks.d.ts +15 -0
- package/dist/shared/assistant/ui/voice/VoiceTurnMarks.js +43 -0
- package/dist/shared/assistant/ui/voice/formatClock.d.ts +2 -0
- package/dist/shared/assistant/ui/voice/formatClock.js +3 -0
- package/dist/shared/assistant/ui/voice/useLiveLevel.d.ts +3 -1
- package/dist/shared/assistant/ui/voice/useLiveLevel.js +23 -19
- package/dist/shared/assistant/voice/liveLevel.d.ts +4 -3
- package/dist/shared/assistant/voice/liveLevel.js +18 -15
- package/dist/shared/assistant/voice/useVoiceInput.d.ts +27 -1
- package/dist/shared/assistant/voice/useVoiceInput.js +141 -71
- package/dist/shared-assistant.js +383 -377
- package/package.json +1 -1
|
@@ -23,4 +23,9 @@ export declare const AssistantVoiceSessionButton: FC<{
|
|
|
23
23
|
compact?: boolean;
|
|
24
24
|
/** Keep the compact opener mounted for focus return while its session is live. */
|
|
25
25
|
invisible?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* ★ BOFF-13461 (page): a secondary circle beside Send, the wave in the
|
|
28
|
+
* assistant's blue, as the operator's design draws it.
|
|
29
|
+
*/
|
|
30
|
+
secondary?: boolean;
|
|
26
31
|
}>;
|
|
@@ -2,17 +2,17 @@ import { useAssistantTr as e } from "../../i18n.js";
|
|
|
2
2
|
import { AudioLines as t } from "lucide-react";
|
|
3
3
|
import { jsx as n } from "react/jsx-runtime";
|
|
4
4
|
// src/shared/assistant/ui/voice/AssistantVoiceSessionButton.tsx
|
|
5
|
-
var r = ({ onStart: r, disabled: i = !1, compact: a = !1, invisible: o = !1 }) => {
|
|
6
|
-
let
|
|
5
|
+
var r = ({ onStart: r, disabled: i = !1, compact: a = !1, invisible: o = !1, secondary: s = !1 }) => {
|
|
6
|
+
let c = e();
|
|
7
7
|
return /* @__PURE__ */ n("button", {
|
|
8
8
|
type: "button",
|
|
9
9
|
onClick: r,
|
|
10
10
|
disabled: i,
|
|
11
|
-
className: a ? `flex size-11 shrink-0 items-center justify-center rounded-full bg-action-primary-bg text-action-primary-text transition-colors hover:bg-action-primary-bg-hover focus:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-focus-ring)] disabled:cursor-not-allowed disabled:opacity-50 sm:size-10${o ? " invisible" : ""}` : "flex size-9 flex-shrink-0 items-center justify-center rounded-full bg-bg-elevated text-text-secondary shadow-sm transition-all hover:scale-105 hover:bg-bg-raised hover:text-text-primary focus:outline-none focus-visible:ring-2 focus-visible:ring-border-strong disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:scale-100",
|
|
12
|
-
"aria-label":
|
|
13
|
-
title:
|
|
11
|
+
className: s ? `flex size-11 shrink-0 items-center justify-center rounded-full border border-border-default text-status-info-bg transition-colors hover:bg-bg-sunken focus:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-focus-ring)] disabled:cursor-not-allowed disabled:opacity-50 sm:size-10${o ? " invisible" : ""}` : a ? `flex size-11 shrink-0 items-center justify-center rounded-full bg-action-primary-bg text-action-primary-text transition-colors hover:bg-action-primary-bg-hover focus:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-focus-ring)] disabled:cursor-not-allowed disabled:opacity-50 sm:size-10${o ? " invisible" : ""}` : "flex size-9 flex-shrink-0 items-center justify-center rounded-full bg-bg-elevated text-text-secondary shadow-sm transition-all hover:scale-105 hover:bg-bg-raised hover:text-text-primary focus:outline-none focus-visible:ring-2 focus-visible:ring-border-strong disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:scale-100",
|
|
12
|
+
"aria-label": c("voiceSession.start", "Start a voice conversation"),
|
|
13
|
+
title: c("voiceSession.start", "Start a voice conversation"),
|
|
14
14
|
"data-testid": "assistant-voice-session-start",
|
|
15
|
-
children: /* @__PURE__ */ n(t, { className: a ? "size-5" : "size-4" })
|
|
15
|
+
children: /* @__PURE__ */ n(t, { className: a || s ? "size-5" : "size-4" })
|
|
16
16
|
});
|
|
17
17
|
};
|
|
18
18
|
export { r as AssistantVoiceSessionButton };
|
|
@@ -19,9 +19,10 @@ interface Props {
|
|
|
19
19
|
* runs.
|
|
20
20
|
*
|
|
21
21
|
* Colours are the theme's: the canvas reads its own CSS `color`, which the tone's
|
|
22
|
-
* semantic token class sets
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* semantic token class sets, so it follows light, dark and any product theme
|
|
23
|
+
* without a colour value in code. The operator's design (14 Nov): amber
|
|
24
|
+
* (`status-warning`) always means "we are hearing you", blue (`accent-blue`)
|
|
25
|
+
* always means "the assistant is talking".
|
|
25
26
|
*
|
|
26
27
|
* ★ Under either reduced-motion signal (BOFF-8566) nothing scrolls: the bars stay
|
|
27
28
|
* a still, short line. The words next to it (the status, or the words appearing
|
|
@@ -45,7 +45,7 @@ var a = 3, o = 2, s = 2, c = ({ source: c, tone: l, className: u = "", testId: d
|
|
|
45
45
|
"data-tone": l,
|
|
46
46
|
children: /* @__PURE__ */ i("canvas", {
|
|
47
47
|
ref: m,
|
|
48
|
-
className: `absolute inset-0 ${l === "speaking" ? "text-
|
|
48
|
+
className: `absolute inset-0 ${l === "speaking" ? "text-status-info-bg" : "text-status-warning-bg"}`
|
|
49
49
|
})
|
|
50
50
|
});
|
|
51
51
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { FC } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* What the page's thread says about voice turns (BOFF-13461; the operator's design
|
|
4
|
+
* of 14 Nov, "How the answer reaches the chat"): the voice chat's turns are the
|
|
5
|
+
* thread's own, and each says how it happened.
|
|
6
|
+
*/
|
|
7
|
+
export type VoiceTurnMark = 'spoken' | 'spokenAloud' | 'interrupted';
|
|
8
|
+
/** Under a voice turn: "Spoken", "Spoken aloud", or "Interrupted…". */
|
|
9
|
+
export declare const VoiceTurnTag: FC<{
|
|
10
|
+
mark: VoiceTurnMark;
|
|
11
|
+
}>;
|
|
12
|
+
/** After a voice chat's last turn: "Voice chat ended", with its length when known. */
|
|
13
|
+
export declare const VoiceChatEnded: FC<{
|
|
14
|
+
seconds?: number;
|
|
15
|
+
}>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { useAssistantTr as e } from "../../i18n.js";
|
|
2
|
+
import { formatClock as t } from "./formatClock.js";
|
|
3
|
+
import { AudioLines as n, Mic as r } from "lucide-react";
|
|
4
|
+
import { jsx as i, jsxs as a } from "react/jsx-runtime";
|
|
5
|
+
// src/shared/assistant/ui/voice/VoiceTurnMarks.tsx
|
|
6
|
+
var o = ({ mark: t }) => {
|
|
7
|
+
let o = e(), s = t === "spoken", c = t === "spoken" ? o("voiceTurn.spoken", "Spoken") : t === "interrupted" ? o("voiceTurn.interrupted", "Interrupted. Only the spoken part is kept.") : o("voiceTurn.spokenAloud", "Spoken aloud");
|
|
8
|
+
return /* @__PURE__ */ i("div", {
|
|
9
|
+
className: `-mt-2 flex px-1 ${s ? "justify-end" : "justify-start ps-10"}`,
|
|
10
|
+
"data-testid": "assistant-voice-turn-tag",
|
|
11
|
+
"data-mark": t,
|
|
12
|
+
children: /* @__PURE__ */ a("span", {
|
|
13
|
+
className: "inline-flex items-center gap-1 text-xs text-text-muted",
|
|
14
|
+
children: [/* @__PURE__ */ i(s ? r : n, {
|
|
15
|
+
className: "size-3",
|
|
16
|
+
"aria-hidden": "true"
|
|
17
|
+
}), c]
|
|
18
|
+
})
|
|
19
|
+
});
|
|
20
|
+
}, s = ({ seconds: n }) => {
|
|
21
|
+
let r = e(), o = n !== void 0 && n > 0 ? r("voiceTurn.endedAfter", "Voice chat ended · {{duration}}", { duration: t(n) }) : r("voiceTurn.ended", "Voice chat ended");
|
|
22
|
+
return /* @__PURE__ */ a("div", {
|
|
23
|
+
role: "separator",
|
|
24
|
+
"aria-label": o,
|
|
25
|
+
className: "flex items-center gap-3 py-1 text-xs text-text-muted",
|
|
26
|
+
"data-testid": "assistant-voice-chat-ended",
|
|
27
|
+
children: [
|
|
28
|
+
/* @__PURE__ */ i("span", {
|
|
29
|
+
"aria-hidden": "true",
|
|
30
|
+
className: "h-px flex-1 bg-border-subtle"
|
|
31
|
+
}),
|
|
32
|
+
/* @__PURE__ */ i("span", {
|
|
33
|
+
"aria-hidden": "true",
|
|
34
|
+
children: o
|
|
35
|
+
}),
|
|
36
|
+
/* @__PURE__ */ i("span", {
|
|
37
|
+
"aria-hidden": "true",
|
|
38
|
+
className: "h-px flex-1 bg-border-subtle"
|
|
39
|
+
})
|
|
40
|
+
]
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
export { s as VoiceChatEnded, o as VoiceTurnTag };
|
|
@@ -12,10 +12,12 @@ export interface UseLiveLevelOptions {
|
|
|
12
12
|
pulseKey?: string;
|
|
13
13
|
/** The assistant is speaking: a steady, speech-like motion, never the microphone. */
|
|
14
14
|
speaking?: boolean;
|
|
15
|
+
/** The microphone chosen in the options (absent or null: the system default). */
|
|
16
|
+
deviceId?: string | null;
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
19
|
* The level a `LiveWaveform` draws (BOFF-13461). Stable for the component's
|
|
18
20
|
* life; the frames run only while `active`. See `voice/liveLevel.ts` for the two
|
|
19
21
|
* sources and why a phone never gets the microphone's own level.
|
|
20
22
|
*/
|
|
21
|
-
export declare function useLiveLevel({ active, mic, pulseKey, speaking, }: UseLiveLevelOptions): LiveLevelSource;
|
|
23
|
+
export declare function useLiveLevel({ active, mic, pulseKey, speaking, deviceId, }: UseLiveLevelOptions): LiveLevelSource;
|
|
@@ -2,33 +2,37 @@ import { createSpeechPulse as e, micLevelAllowed as t, openMicLevel as n, smooth
|
|
|
2
2
|
import { useEffect as i, useMemo as a, useRef as o, useState as s } from "react";
|
|
3
3
|
// src/shared/assistant/ui/voice/useLiveLevel.ts
|
|
4
4
|
var c = 33;
|
|
5
|
-
function l({ active: l, mic: u = !1, pulseKey: d = "", speaking: f = !1 }) {
|
|
6
|
-
let [
|
|
5
|
+
function l({ active: l, mic: u = !1, pulseKey: d = "", speaking: f = !1, deviceId: p = null }) {
|
|
6
|
+
let [m] = s(() => /* @__PURE__ */ new Set()), [h] = s(e), g = o(null), _ = o(f);
|
|
7
7
|
i(() => {
|
|
8
|
-
|
|
8
|
+
_.current = f;
|
|
9
9
|
}, [f]);
|
|
10
|
-
let
|
|
11
|
-
return
|
|
12
|
-
|
|
10
|
+
let v = a(() => ({ subscribe(e) {
|
|
11
|
+
return m.add(e), () => {
|
|
12
|
+
m.delete(e);
|
|
13
13
|
};
|
|
14
|
-
} }), [
|
|
14
|
+
} }), [m]);
|
|
15
15
|
return i(() => {
|
|
16
|
-
l && d !== "" &&
|
|
16
|
+
l && d !== "" && h.pulse();
|
|
17
17
|
}, [
|
|
18
18
|
l,
|
|
19
19
|
d,
|
|
20
|
-
|
|
20
|
+
h
|
|
21
21
|
]), i(() => {
|
|
22
22
|
if (!l || !u || !t(typeof navigator > "u" ? void 0 : navigator, typeof AudioContext < "u")) return;
|
|
23
23
|
let e = !1;
|
|
24
|
-
return n().then((t) => {
|
|
25
|
-
e ? t?.close() :
|
|
24
|
+
return n(p).then((t) => {
|
|
25
|
+
e ? t?.close() : g.current = t;
|
|
26
26
|
}), () => {
|
|
27
|
-
e = !0,
|
|
27
|
+
e = !0, g.current?.close(), g.current = null;
|
|
28
28
|
};
|
|
29
|
-
}, [
|
|
29
|
+
}, [
|
|
30
|
+
l,
|
|
31
|
+
u,
|
|
32
|
+
p
|
|
33
|
+
]), i(() => {
|
|
30
34
|
let e = (e) => {
|
|
31
|
-
for (let t of
|
|
35
|
+
for (let t of m) t(e);
|
|
32
36
|
};
|
|
33
37
|
if (!l || typeof requestAnimationFrame > "u") {
|
|
34
38
|
e(0);
|
|
@@ -36,8 +40,8 @@ function l({ active: l, mic: u = !1, pulseKey: d = "", speaking: f = !1 }) {
|
|
|
36
40
|
}
|
|
37
41
|
let t = 0, n = 0, i = 0, a = 0, o = (s) => {
|
|
38
42
|
if (t = requestAnimationFrame(o), s - n < c) return;
|
|
39
|
-
n = s,
|
|
40
|
-
let l =
|
|
43
|
+
n = s, _.current && s - i > 170 && (i = s, h.pulse(.45 + .45 * Math.abs(Math.sin(s / 53))));
|
|
44
|
+
let l = g.current, u = l && !_.current ? l.read() : h.next(s);
|
|
41
45
|
a = r(a, u), e(a);
|
|
42
46
|
};
|
|
43
47
|
return t = requestAnimationFrame(o), () => {
|
|
@@ -45,8 +49,8 @@ function l({ active: l, mic: u = !1, pulseKey: d = "", speaking: f = !1 }) {
|
|
|
45
49
|
};
|
|
46
50
|
}, [
|
|
47
51
|
l,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
]),
|
|
52
|
+
m,
|
|
53
|
+
h
|
|
54
|
+
]), v;
|
|
51
55
|
}
|
|
52
56
|
export { l as useLiveLevel };
|
|
@@ -78,7 +78,8 @@ export interface MicLevelHandle {
|
|
|
78
78
|
/**
|
|
79
79
|
* Open a capture of our own and an analyser on it. Echo cancellation, noise
|
|
80
80
|
* suppression and auto gain are asked for (plain `true`, never `exact`): the
|
|
81
|
-
* assistant's own voice must not drive the person's bars.
|
|
82
|
-
*
|
|
81
|
+
* assistant's own voice must not drive the person's bars. `deviceId`: the chosen
|
|
82
|
+
* microphone (absent or null: the system default). Resolves null when the browser
|
|
83
|
+
* refuses; the caller then falls back to the recogniser's pulses.
|
|
83
84
|
*/
|
|
84
|
-
export declare function openMicLevel(): Promise<MicLevelHandle | null>;
|
|
85
|
+
export declare function openMicLevel(deviceId?: string | null): Promise<MicLevelHandle | null>;
|
|
@@ -42,33 +42,36 @@ function a() {
|
|
|
42
42
|
}
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
|
-
async function o() {
|
|
45
|
+
async function o(n) {
|
|
46
46
|
if (typeof navigator > "u" || typeof AudioContext > "u") return null;
|
|
47
|
-
let
|
|
47
|
+
let r = {
|
|
48
|
+
echoCancellation: !0,
|
|
49
|
+
noiseSuppression: !0,
|
|
50
|
+
autoGainControl: !0
|
|
51
|
+
}, i;
|
|
48
52
|
try {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
} });
|
|
53
|
+
i = await navigator.mediaDevices.getUserMedia({ audio: n ? {
|
|
54
|
+
...r,
|
|
55
|
+
deviceId: { exact: n }
|
|
56
|
+
} : r });
|
|
54
57
|
} catch {
|
|
55
58
|
return null;
|
|
56
59
|
}
|
|
57
|
-
let
|
|
60
|
+
let a;
|
|
58
61
|
try {
|
|
59
|
-
|
|
62
|
+
a = new AudioContext();
|
|
60
63
|
} catch {
|
|
61
|
-
return
|
|
64
|
+
return i.getTracks().forEach((e) => e.stop()), null;
|
|
62
65
|
}
|
|
63
|
-
let
|
|
64
|
-
|
|
65
|
-
let
|
|
66
|
+
let o = a.createMediaStreamSource(i), s = a.createAnalyser();
|
|
67
|
+
s.fftSize = 512, s.smoothingTimeConstant = .2, o.connect(s);
|
|
68
|
+
let c = new Uint8Array(s.fftSize), l = !1;
|
|
66
69
|
return {
|
|
67
70
|
read() {
|
|
68
|
-
return
|
|
71
|
+
return l ? 0 : (s.getByteTimeDomainData(c), t(e(c)));
|
|
69
72
|
},
|
|
70
73
|
close() {
|
|
71
|
-
|
|
74
|
+
l || (l = !0, o.disconnect(), i.getTracks().forEach((e) => e.stop()), a.close().catch(() => void 0));
|
|
72
75
|
}
|
|
73
76
|
};
|
|
74
77
|
}
|
|
@@ -105,7 +105,25 @@ export interface UseVoiceInputOptions {
|
|
|
105
105
|
* only way to dictate Tamil was to translate the entire app.
|
|
106
106
|
*/
|
|
107
107
|
language?: string;
|
|
108
|
+
/**
|
|
109
|
+
* ★ BOFF-13461: the microphone to listen to (a `deviceId`); absent or null is
|
|
110
|
+
* the system default, which is exactly the behaviour before this option.
|
|
111
|
+
* Honoured on a desktop browser only, by handing the recogniser a track of that
|
|
112
|
+
* device (`start(audioTrack)`, Chrome 135+; an older engine ignores the track
|
|
113
|
+
* and listens to the default). Never on a phone or tablet: the recogniser there
|
|
114
|
+
* may be a system service a capture of ours would take the microphone from.
|
|
115
|
+
*/
|
|
116
|
+
deviceId?: string | null;
|
|
117
|
+
/**
|
|
118
|
+
* ★ BOFF-13461: the recogniser went away on its own, with no `finish()` waiting:
|
|
119
|
+
* its own end (the end of a phrase on a phone, an error) or a start that failed.
|
|
120
|
+
* Not called by `stop()`. Dictation lands what it heard so far here, so no
|
|
121
|
+
* word is lost.
|
|
122
|
+
*/
|
|
123
|
+
onEnded?: () => void;
|
|
108
124
|
}
|
|
125
|
+
/** How long `finish()` waits for the recogniser's last words before it gives up. */
|
|
126
|
+
export declare const FINISH_WAIT_MS = 1500;
|
|
109
127
|
export interface UseVoiceInputResult {
|
|
110
128
|
isSupported: boolean;
|
|
111
129
|
isListening: boolean;
|
|
@@ -135,8 +153,16 @@ export interface UseVoiceInputResult {
|
|
|
135
153
|
*/
|
|
136
154
|
start: (source?: StartSource) => void;
|
|
137
155
|
stop: () => void;
|
|
156
|
+
/**
|
|
157
|
+
* ★ BOFF-13461: stop listening, but WAIT for the recogniser's last words: the
|
|
158
|
+
* phrase it was still working on arrives through `onFinalize` before this
|
|
159
|
+
* resolves (or after `FINISH_WAIT_MS`, whichever is first). `stop()` drops it.
|
|
160
|
+
*/
|
|
161
|
+
finish: () => Promise<void>;
|
|
162
|
+
/** `finish()` is waiting for the last words. */
|
|
163
|
+
isFinishing: boolean;
|
|
138
164
|
/** Start if idle, stop if listening — what the mic button binds to. */
|
|
139
165
|
toggle: () => void;
|
|
140
166
|
clearError: () => void;
|
|
141
167
|
}
|
|
142
|
-
export declare function useVoiceInput({ onFinalize, onSpeechEnd, language: requested, }: UseVoiceInputOptions): UseVoiceInputResult;
|
|
168
|
+
export declare function useVoiceInput({ onFinalize, onSpeechEnd, language: requested, deviceId, onEnded, }: UseVoiceInputOptions): UseVoiceInputResult;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { micLevelAllowed as e } from "./liveLevel.js";
|
|
2
|
+
import { useI18n as t } from "../../providers/shell/I18nProvider.js";
|
|
3
|
+
import { useCallback as n, useEffect as r, useRef as i, useState as a } from "react";
|
|
3
4
|
// src/shared/assistant/voice/useVoiceInput.ts
|
|
4
|
-
var
|
|
5
|
+
var o = {
|
|
5
6
|
en: "en-US",
|
|
6
7
|
es: "es-ES",
|
|
7
8
|
fr: "fr-FR",
|
|
@@ -12,19 +13,19 @@ var a = {
|
|
|
12
13
|
hi: "hi-IN",
|
|
13
14
|
ar: "ar-SA"
|
|
14
15
|
};
|
|
15
|
-
function
|
|
16
|
+
function s() {
|
|
16
17
|
if (typeof window > "u") return null;
|
|
17
18
|
let e = window;
|
|
18
19
|
return e.SpeechRecognition ?? e.webkitSpeechRecognition ?? null;
|
|
19
20
|
}
|
|
20
|
-
function
|
|
21
|
-
return
|
|
21
|
+
function c() {
|
|
22
|
+
return s() !== null;
|
|
22
23
|
}
|
|
23
|
-
var
|
|
24
|
-
function
|
|
25
|
-
return t - e <
|
|
24
|
+
var l = 2e3, u = 2;
|
|
25
|
+
function d(e, t) {
|
|
26
|
+
return t - e < l;
|
|
26
27
|
}
|
|
27
|
-
function
|
|
28
|
+
function f(e) {
|
|
28
29
|
switch (e) {
|
|
29
30
|
case "not-allowed":
|
|
30
31
|
case "service-not-allowed": return "not-allowed";
|
|
@@ -35,78 +36,147 @@ function d(e) {
|
|
|
35
36
|
default: return "unknown";
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
n(() => {
|
|
41
|
-
|
|
42
|
-
}, [c]);
|
|
43
|
-
let D = r(s);
|
|
44
|
-
n(() => {
|
|
45
|
-
D.current = s;
|
|
46
|
-
}, [s]);
|
|
47
|
-
let O = o() !== null, k = f ?? a[p] ?? "en-US";
|
|
48
|
-
n(() => () => {
|
|
49
|
-
S.current?.abort(), S.current = null;
|
|
39
|
+
var p = 1500;
|
|
40
|
+
function m({ onFinalize: e, onSpeechEnd: c, language: l, deviceId: m = null, onEnded: _ }) {
|
|
41
|
+
let { locale: v } = t(), [y, b] = a(!1), [x, S] = a(!1), [C, w] = a(""), [T, E] = a(null), D = i(null), O = i(0), k = i(-Infinity), A = i(0), [j, M] = a(!1), N = i(null), P = i(null), F = i(null), I = i(m), L = n(() => {
|
|
42
|
+
P.current?.stop(), P.current = null;
|
|
50
43
|
}, []);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
44
|
+
r(() => {
|
|
45
|
+
I.current = m, L();
|
|
46
|
+
}, [m, L]);
|
|
47
|
+
let R = i(c);
|
|
48
|
+
r(() => {
|
|
49
|
+
R.current = c;
|
|
50
|
+
}, [c]);
|
|
51
|
+
let z = i(e);
|
|
52
|
+
r(() => {
|
|
53
|
+
z.current = e;
|
|
54
|
+
}, [e]);
|
|
55
|
+
let B = i(_);
|
|
56
|
+
r(() => {
|
|
57
|
+
B.current = _;
|
|
58
|
+
}, [_]);
|
|
59
|
+
let V = s() !== null, H = l ?? o[v] ?? "en-US";
|
|
60
|
+
r(() => () => {
|
|
61
|
+
let e = D.current;
|
|
62
|
+
D.current = null, O.current += 1, e?.abort(), N.current?.complete(), L();
|
|
63
|
+
}, [L]);
|
|
64
|
+
let U = n((e = "manual", t) => {
|
|
65
|
+
let n = s();
|
|
66
|
+
if (!n || D.current || e === "auto" && d(k.current, performance.now())) return;
|
|
67
|
+
let r = new n();
|
|
68
|
+
r.continuous = !0, r.interimResults = !0, r.lang = H;
|
|
69
|
+
let i = O.current;
|
|
70
|
+
r.onspeechend = () => {
|
|
71
|
+
i === O.current && R.current?.(performance.now());
|
|
72
|
+
}, r.onresult = (e) => {
|
|
73
|
+
if (i !== O.current) return;
|
|
61
74
|
let t = "", n = "";
|
|
62
75
|
for (let r = e.resultIndex; r < e.results.length; r += 1) {
|
|
63
76
|
let i = e.results[r], a = i[0]?.transcript ?? "";
|
|
64
77
|
i.isFinal ? t += a : n += a;
|
|
65
78
|
}
|
|
66
|
-
t.trim() &&
|
|
67
|
-
},
|
|
68
|
-
if (
|
|
69
|
-
let t =
|
|
70
|
-
t !== "no-speech" &&
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
},
|
|
79
|
+
t.trim() && z.current(t.trim()), w(n.trim());
|
|
80
|
+
}, r.onerror = (e) => {
|
|
81
|
+
if (i !== O.current) return;
|
|
82
|
+
let t = f(e.error);
|
|
83
|
+
t !== "no-speech" && E(t);
|
|
84
|
+
}, r.onend = () => {
|
|
85
|
+
i === O.current && (N.current?.session === i ? N.current.complete() : (D.current = null, L(), S(!1), b(!1), w(""), B.current?.()));
|
|
86
|
+
}, r.onstart = () => {
|
|
87
|
+
i === O.current && (S(!1), b(!0));
|
|
88
|
+
}, S(!0);
|
|
76
89
|
try {
|
|
77
|
-
|
|
90
|
+
t ? r.start(t) : r.start();
|
|
78
91
|
} catch {
|
|
79
|
-
|
|
92
|
+
k.current = performance.now(), A.current += 1, A.current >= u && E("unknown"), D.current = null, L(), S(!1), b(!1), B.current?.();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
D.current = r, A.current = 0, E(null), w("");
|
|
96
|
+
}, [H, L]), W = n((e = "manual") => {
|
|
97
|
+
if (!s() || D.current || F.current === O.current) return;
|
|
98
|
+
let t = I.current;
|
|
99
|
+
if (!t || !h()) {
|
|
100
|
+
U(e);
|
|
80
101
|
return;
|
|
81
102
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
103
|
+
let n = P.current;
|
|
104
|
+
if (n && n.deviceId === t && n.track.readyState === "live") {
|
|
105
|
+
U(e, n.track);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
let r = O.current;
|
|
109
|
+
F.current = r, S(!0), g(t).then((t) => {
|
|
110
|
+
F.current === r && (F.current = null), r === O.current ? (t && (P.current = t), U(e, t?.track)) : t?.stop();
|
|
111
|
+
});
|
|
112
|
+
}, [U]), G = n(() => {
|
|
113
|
+
let e = D.current;
|
|
114
|
+
D.current = null, O.current += 1, e?.stop(), L(), N.current?.complete(), S(!1), b(!1), w("");
|
|
115
|
+
}, [L]), K = n(() => {
|
|
116
|
+
let e = D.current;
|
|
117
|
+
if (!e) return G(), Promise.resolve();
|
|
118
|
+
if (N.current) return new Promise((e) => setTimeout(e, p));
|
|
119
|
+
let t = O.current;
|
|
120
|
+
return M(!0), new Promise((n) => {
|
|
121
|
+
let r = !1, i = () => {
|
|
122
|
+
r || (r = !0, clearTimeout(a), N.current = null, t === O.current && (D.current = null, O.current += 1), L(), M(!1), S(!1), b(!1), w(""), n());
|
|
123
|
+
}, a = setTimeout(i, p);
|
|
124
|
+
N.current = {
|
|
125
|
+
session: t,
|
|
126
|
+
complete: i
|
|
127
|
+
};
|
|
128
|
+
try {
|
|
129
|
+
e.stop();
|
|
130
|
+
} catch {
|
|
131
|
+
i();
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}, [G, L]), q = i(H);
|
|
135
|
+
r(() => {
|
|
136
|
+
q.current !== H && (q.current = H, G(), E(null));
|
|
137
|
+
}, [H, G]);
|
|
138
|
+
let J = n(() => {
|
|
139
|
+
y || x ? G() : W();
|
|
92
140
|
}, [
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
]),
|
|
141
|
+
y,
|
|
142
|
+
x,
|
|
143
|
+
W,
|
|
144
|
+
G
|
|
145
|
+
]), Y = n(() => E(null), []);
|
|
98
146
|
return {
|
|
99
|
-
isSupported:
|
|
100
|
-
isListening:
|
|
101
|
-
isStarting:
|
|
102
|
-
isActive:
|
|
103
|
-
interim:
|
|
104
|
-
error:
|
|
105
|
-
language:
|
|
106
|
-
start:
|
|
107
|
-
stop:
|
|
108
|
-
|
|
109
|
-
|
|
147
|
+
isSupported: V,
|
|
148
|
+
isListening: y,
|
|
149
|
+
isStarting: x,
|
|
150
|
+
isActive: y || x,
|
|
151
|
+
interim: C,
|
|
152
|
+
error: T,
|
|
153
|
+
language: H,
|
|
154
|
+
start: W,
|
|
155
|
+
stop: G,
|
|
156
|
+
finish: K,
|
|
157
|
+
isFinishing: j,
|
|
158
|
+
toggle: J,
|
|
159
|
+
clearError: Y
|
|
110
160
|
};
|
|
111
161
|
}
|
|
112
|
-
|
|
162
|
+
function h() {
|
|
163
|
+
return e(typeof navigator > "u" ? void 0 : navigator, !0);
|
|
164
|
+
}
|
|
165
|
+
async function g(e) {
|
|
166
|
+
try {
|
|
167
|
+
let t = await navigator.mediaDevices.getUserMedia({ audio: {
|
|
168
|
+
deviceId: { exact: e },
|
|
169
|
+
echoCancellation: !0,
|
|
170
|
+
noiseSuppression: !0,
|
|
171
|
+
autoGainControl: !0
|
|
172
|
+
} }), n = t.getAudioTracks()[0];
|
|
173
|
+
return n ? {
|
|
174
|
+
deviceId: e,
|
|
175
|
+
track: n,
|
|
176
|
+
stop: () => t.getTracks().forEach((e) => e.stop())
|
|
177
|
+
} : (t.getTracks().forEach((e) => e.stop()), null);
|
|
178
|
+
} catch {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
export { p as FINISH_WAIT_MS, l as START_RETRY_COOLDOWN_MS, c as isSpeechRecognitionSupported, d as startIsCoolingDown, m as useVoiceInput };
|