@djangocfg/ui-tools 2.1.381 → 2.1.383
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 +132 -899
- package/dist/ChatRoot-6IZFM5HM.mjs +5 -0
- package/dist/{ChatRoot-EJC5Y2YM.cjs.map → ChatRoot-6IZFM5HM.mjs.map} +1 -1
- package/dist/ChatRoot-LW4XNIKP.cjs +14 -0
- package/dist/{ChatRoot-QOSKJPM6.mjs.map → ChatRoot-LW4XNIKP.cjs.map} +1 -1
- package/dist/DictationField-U25MEYAL.mjs +4 -0
- package/dist/DictationField-U25MEYAL.mjs.map +1 -0
- package/dist/DictationField-XWR5VOID.cjs +13 -0
- package/dist/DictationField-XWR5VOID.cjs.map +1 -0
- package/dist/{DocsLayout-2YKPXZYO.mjs → DocsLayout-2P3ONDWJ.mjs} +3 -3
- package/dist/{DocsLayout-2YKPXZYO.mjs.map → DocsLayout-2P3ONDWJ.mjs.map} +1 -1
- package/dist/{DocsLayout-Q4KS3QWW.cjs → DocsLayout-2YZNS5VK.cjs} +8 -8
- package/dist/{DocsLayout-Q4KS3QWW.cjs.map → DocsLayout-2YZNS5VK.cjs.map} +1 -1
- package/dist/chunk-4PFW7MIJ.cjs +837 -0
- package/dist/chunk-4PFW7MIJ.cjs.map +1 -0
- package/dist/chunk-C2YN6WEO.mjs +833 -0
- package/dist/chunk-C2YN6WEO.mjs.map +1 -0
- package/dist/{chunk-XACCHZH2.cjs → chunk-FIRK5CEH.cjs} +42 -4
- package/dist/chunk-FIRK5CEH.cjs.map +1 -0
- package/dist/{chunk-NWUT327A.mjs → chunk-HIK6BPL7.mjs} +38 -5
- package/dist/chunk-HIK6BPL7.mjs.map +1 -0
- package/dist/chunk-OZAU3QWD.cjs +2493 -0
- package/dist/chunk-OZAU3QWD.cjs.map +1 -0
- package/dist/chunk-UWVP6LCW.mjs +2447 -0
- package/dist/chunk-UWVP6LCW.mjs.map +1 -0
- package/dist/index.cjs +1668 -99
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1215 -107
- package/dist/index.d.ts +1215 -107
- package/dist/index.mjs +1555 -50
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -15
- package/src/audio-assets.d.ts +8 -0
- package/src/components/markdown/MarkdownMessage/CollapseToggle.tsx +3 -1
- package/src/components/markdown/MarkdownMessage/components.tsx +2 -5
- package/src/tools/Chat/README.md +347 -530
- package/src/tools/Chat/components/Attachments.tsx +6 -1
- package/src/tools/Chat/components/ChatRoot.tsx +30 -2
- package/src/tools/Chat/components/Composer.tsx +20 -3
- package/src/tools/Chat/components/ErrorBanner.tsx +7 -3
- package/src/tools/Chat/components/MessageActions.tsx +3 -1
- package/src/tools/Chat/components/MessageBubble.tsx +6 -5
- package/src/tools/Chat/components/MessageList.tsx +87 -1
- package/src/tools/Chat/components/ToolCalls.tsx +21 -3
- package/src/tools/Chat/context/ChatProvider.tsx +21 -3
- package/src/tools/Chat/core/audio/audioBus.ts +10 -163
- package/src/tools/Chat/core/audio/defaults.ts +43 -0
- package/src/tools/Chat/core/audio/index.ts +1 -0
- package/src/tools/Chat/core/audio/preferences.ts +5 -59
- package/src/tools/Chat/core/audio/sounds/error.mp3 +0 -0
- package/src/tools/Chat/core/audio/sounds/mention.mp3 +0 -0
- package/src/tools/Chat/core/audio/sounds/notification.mp3 +0 -0
- package/src/tools/Chat/core/audio/sounds/received.mp3 +0 -0
- package/src/tools/Chat/core/audio/sounds/sent.mp3 +0 -0
- package/src/tools/Chat/core/audio/sounds/start.mp3 +0 -0
- package/src/tools/Chat/core/audio/types.ts +28 -0
- package/src/tools/Chat/core/reducer.ts +33 -0
- package/src/tools/Chat/core/transport/index.ts +13 -0
- package/src/tools/Chat/core/transport/mappers/index.ts +6 -0
- package/src/tools/Chat/core/transport/mappers/pydantic-ai.ts +142 -0
- package/src/tools/Chat/core/transport/pydantic-ai-transport.ts +208 -0
- package/src/tools/Chat/core/transport/sse.ts +18 -5
- package/src/tools/Chat/hooks/index.ts +25 -0
- package/src/tools/Chat/hooks/useAutoFocusOnStreamEnd.ts +5 -3
- package/src/tools/Chat/hooks/useChat.ts +28 -0
- package/src/tools/Chat/hooks/useChatAudio.ts +59 -180
- package/src/tools/Chat/hooks/useChatDockPrefs.ts +74 -0
- package/src/tools/Chat/hooks/useChatReset.ts +70 -0
- package/src/tools/Chat/hooks/useChatUnread.ts +87 -0
- package/src/tools/Chat/hooks/useFocusOnEmptyClick.ts +111 -0
- package/src/tools/Chat/hooks/useVisitorFingerprint.ts +48 -0
- package/src/tools/Chat/index.ts +84 -1
- package/src/tools/Chat/launcher/ChatDock.tsx +263 -0
- package/src/tools/Chat/launcher/ChatFAB.tsx +349 -0
- package/src/tools/Chat/launcher/ChatGreeting.tsx +200 -0
- package/src/tools/Chat/launcher/ChatHeader.tsx +76 -0
- package/src/tools/Chat/launcher/ChatHeaderActionButton.tsx +87 -0
- package/src/tools/Chat/launcher/ChatHeaderAudioToggle.tsx +47 -0
- package/src/tools/Chat/launcher/ChatHeaderLanguageButton.tsx +179 -0
- package/src/tools/Chat/launcher/ChatHeaderModeToggle.tsx +57 -0
- package/src/tools/Chat/launcher/ChatHeaderResetButton.tsx +93 -0
- package/src/tools/Chat/launcher/ChatLauncher.tsx +321 -0
- package/src/tools/Chat/launcher/ChatUnreadPreview.tsx +197 -0
- package/src/tools/Chat/launcher/index.ts +46 -0
- package/src/tools/Chat/launcher/useChatPresence.ts +44 -0
- package/src/tools/Chat/styles/bubbleTokens.ts +71 -0
- package/src/tools/Chat/styles/index.ts +16 -0
- package/src/tools/Chat/styles/useChatStyles.ts +101 -0
- package/src/tools/Chat/types/attachment.ts +25 -0
- package/src/tools/Chat/types/config.ts +48 -0
- package/src/tools/Chat/types/events.ts +35 -0
- package/src/tools/Chat/types/index.ts +34 -0
- package/src/tools/Chat/types/labels.ts +38 -0
- package/src/tools/Chat/types/message.ts +32 -0
- package/src/tools/Chat/types/persona.ts +31 -0
- package/src/tools/Chat/types/session.ts +43 -0
- package/src/tools/Chat/types/tool-call.ts +17 -0
- package/src/tools/Chat/types/transport.ts +28 -0
- package/src/tools/Chat/types.ts +5 -240
- package/src/tools/MarkdownEditor/MarkdownEditor.tsx +50 -14
- package/src/tools/MarkdownEditor/index.ts +1 -1
- package/src/tools/SpeechRecognition/README.md +336 -0
- package/src/tools/SpeechRecognition/__tests__/ids.test.ts +15 -0
- package/src/tools/SpeechRecognition/__tests__/language.test.ts +59 -0
- package/src/tools/SpeechRecognition/__tests__/reducer.test.ts +71 -0
- package/src/tools/SpeechRecognition/__tests__/transcript.test.ts +52 -0
- package/src/tools/SpeechRecognition/components/DevicePicker.tsx +49 -0
- package/src/tools/SpeechRecognition/components/DictationButton.tsx +93 -0
- package/src/tools/SpeechRecognition/components/EngineBadge.tsx +30 -0
- package/src/tools/SpeechRecognition/components/ErrorBanner.tsx +52 -0
- package/src/tools/SpeechRecognition/components/LanguagePicker.tsx +63 -0
- package/src/tools/SpeechRecognition/components/MicMeter.tsx +63 -0
- package/src/tools/SpeechRecognition/components/PushToTalkHint.tsx +51 -0
- package/src/tools/SpeechRecognition/components/TranscriptView.tsx +55 -0
- package/src/tools/SpeechRecognition/components/index.ts +16 -0
- package/src/tools/SpeechRecognition/context/SpeechRecognitionProvider.tsx +47 -0
- package/src/tools/SpeechRecognition/context/index.ts +6 -0
- package/src/tools/SpeechRecognition/core/audio/defaults.ts +24 -0
- package/src/tools/SpeechRecognition/core/engine/external.ts +222 -0
- package/src/tools/SpeechRecognition/core/engine/http.ts +147 -0
- package/src/tools/SpeechRecognition/core/engine/index.ts +52 -0
- package/src/tools/SpeechRecognition/core/engine/mediarecorder.ts +105 -0
- package/src/tools/SpeechRecognition/core/engine/websocket.ts +211 -0
- package/src/tools/SpeechRecognition/core/engine/webspeech.ts +188 -0
- package/src/tools/SpeechRecognition/core/ids.ts +11 -0
- package/src/tools/SpeechRecognition/core/index.ts +14 -0
- package/src/tools/SpeechRecognition/core/language.ts +78 -0
- package/src/tools/SpeechRecognition/core/languages-catalog.ts +229 -0
- package/src/tools/SpeechRecognition/core/logger.ts +3 -0
- package/src/tools/SpeechRecognition/core/reducer.ts +105 -0
- package/src/tools/SpeechRecognition/core/transcript.ts +36 -0
- package/src/tools/SpeechRecognition/hooks/index.ts +14 -0
- package/src/tools/SpeechRecognition/hooks/useDictation.ts +59 -0
- package/src/tools/SpeechRecognition/hooks/useEnginePrefs.ts +15 -0
- package/src/tools/SpeechRecognition/hooks/useMicDevices.ts +57 -0
- package/src/tools/SpeechRecognition/hooks/useMicLevel.ts +52 -0
- package/src/tools/SpeechRecognition/hooks/usePushToTalk.ts +85 -0
- package/src/tools/SpeechRecognition/hooks/useResolvedLanguage.ts +28 -0
- package/src/tools/SpeechRecognition/hooks/useSpeechLanguageInfo.ts +108 -0
- package/src/tools/SpeechRecognition/hooks/useSpeechRecognition.ts +188 -0
- package/src/tools/SpeechRecognition/hooks/useVoiceSupport.ts +78 -0
- package/src/tools/SpeechRecognition/index.ts +82 -0
- package/src/tools/SpeechRecognition/lazy.tsx +19 -0
- package/src/tools/SpeechRecognition/store/index.ts +2 -0
- package/src/tools/SpeechRecognition/store/prefsStore.ts +54 -0
- package/src/tools/SpeechRecognition/types.ts +133 -0
- package/src/tools/SpeechRecognition/widgets/DictationField.tsx +105 -0
- package/src/tools/SpeechRecognition/widgets/VoiceComposerSlot.tsx +305 -0
- package/src/tools/SpeechRecognition/widgets/VoiceMessageRecorder.tsx +88 -0
- package/src/tools/SpeechRecognition/widgets/index.ts +6 -0
- package/dist/ChatRoot-EJC5Y2YM.cjs +0 -14
- package/dist/ChatRoot-QOSKJPM6.mjs +0 -5
- package/dist/chunk-NWUT327A.mjs.map +0 -1
- package/dist/chunk-QLMKCSR6.mjs +0 -2420
- package/dist/chunk-QLMKCSR6.mjs.map +0 -1
- package/dist/chunk-SI5RD2GD.cjs +0 -2460
- package/dist/chunk-SI5RD2GD.cjs.map +0 -1
- package/dist/chunk-XACCHZH2.cjs.map +0 -1
- package/src/components/markdown/MarkdownMessage/MarkdownMessage.story.tsx +0 -771
- package/src/stories/index.ts +0 -33
- package/src/tools/AudioPlayer/AudioPlayer.story.tsx +0 -481
- package/src/tools/Chat/Chat.story.tsx +0 -1457
- package/src/tools/CodeEditor/CodeEditor.story.tsx +0 -202
- package/src/tools/CronScheduler/CronScheduler.story.tsx +0 -300
- package/src/tools/Gallery/Gallery.story.tsx +0 -237
- package/src/tools/ImageViewer/ImageViewer.story.tsx +0 -85
- package/src/tools/JsonForm/JsonForm.story.tsx +0 -350
- package/src/tools/JsonTree/JsonTree.story.tsx +0 -141
- package/src/tools/LottiePlayer/LottiePlayer.story.tsx +0 -95
- package/src/tools/Map/Map.story.tsx +0 -458
- package/src/tools/MarkdownEditor/MarkdownEditor.story.tsx +0 -225
- package/src/tools/Mermaid/Mermaid.story.tsx +0 -251
- package/src/tools/OpenapiViewer/OpenapiViewer.story.tsx +0 -230
- package/src/tools/PrettyCode/PrettyCode.story.tsx +0 -304
- package/src/tools/Tour/Tour.story.tsx +0 -279
- package/src/tools/Tree/Tree.story.tsx +0 -620
- package/src/tools/Uploader/Uploader.story.tsx +0 -415
- package/src/tools/VideoPlayer/VideoPlayer.story.tsx +0 -87
|
@@ -0,0 +1,833 @@
|
|
|
1
|
+
import { __name } from './chunk-N2XQF2OL.mjs';
|
|
2
|
+
import { cn } from '@djangocfg/ui-core/lib';
|
|
3
|
+
import { Loader2, MicOff, Mic, AlertTriangle } from 'lucide-react';
|
|
4
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
5
|
+
import { useState, useRef, useEffect, useMemo, useReducer, useCallback } from 'react';
|
|
6
|
+
import { consola } from 'consola';
|
|
7
|
+
import { create } from 'zustand';
|
|
8
|
+
import { persist, createJSONStorage } from 'zustand/middleware';
|
|
9
|
+
import { useLocaleOptional } from '@djangocfg/i18n';
|
|
10
|
+
|
|
11
|
+
var SIZE_CLS = {
|
|
12
|
+
sm: "h-8 w-8 [&_svg]:h-4 [&_svg]:w-4",
|
|
13
|
+
md: "h-10 w-10 [&_svg]:h-5 [&_svg]:w-5",
|
|
14
|
+
lg: "h-12 w-12 [&_svg]:h-6 [&_svg]:w-6"
|
|
15
|
+
};
|
|
16
|
+
function DictationButton({
|
|
17
|
+
status,
|
|
18
|
+
onClick,
|
|
19
|
+
isSupported = true,
|
|
20
|
+
size = "md",
|
|
21
|
+
className,
|
|
22
|
+
style,
|
|
23
|
+
ariaLabel,
|
|
24
|
+
idleIcon,
|
|
25
|
+
listeningIcon,
|
|
26
|
+
disabled
|
|
27
|
+
}) {
|
|
28
|
+
const listening = status === "listening" || status === "starting";
|
|
29
|
+
const stopping = status === "stopping";
|
|
30
|
+
const off = !isSupported;
|
|
31
|
+
return /* @__PURE__ */ jsxs(
|
|
32
|
+
"button",
|
|
33
|
+
{
|
|
34
|
+
type: "button",
|
|
35
|
+
onClick,
|
|
36
|
+
disabled: disabled || off,
|
|
37
|
+
"aria-pressed": listening,
|
|
38
|
+
"aria-label": ariaLabel ?? (listening ? "Stop dictation" : off ? "Dictation not supported" : "Start dictation"),
|
|
39
|
+
className: cn(
|
|
40
|
+
"relative inline-flex items-center justify-center rounded-full transition-colors",
|
|
41
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
|
|
42
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
43
|
+
SIZE_CLS[size],
|
|
44
|
+
listening ? "bg-destructive text-destructive-foreground hover:bg-destructive/90" : "bg-primary text-primary-foreground hover:bg-primary/90",
|
|
45
|
+
className
|
|
46
|
+
),
|
|
47
|
+
style,
|
|
48
|
+
children: [
|
|
49
|
+
listening && /* @__PURE__ */ jsx(
|
|
50
|
+
"span",
|
|
51
|
+
{
|
|
52
|
+
"aria-hidden": true,
|
|
53
|
+
className: "absolute inset-0 rounded-full bg-destructive/40 animate-ping"
|
|
54
|
+
}
|
|
55
|
+
),
|
|
56
|
+
stopping ? /* @__PURE__ */ jsx(Loader2, { className: "animate-spin" }) : off ? listeningIcon ?? /* @__PURE__ */ jsx(MicOff, {}) : listening ? listeningIcon ?? /* @__PURE__ */ jsx(Mic, {}) : idleIcon ?? /* @__PURE__ */ jsx(Mic, {})
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
__name(DictationButton, "DictationButton");
|
|
62
|
+
var FRIENDLY = {
|
|
63
|
+
unsupported: "Speech recognition isn't available in this browser.",
|
|
64
|
+
"permission-denied": "Microphone access was denied. Allow it in your browser settings to dictate.",
|
|
65
|
+
"no-microphone": "No microphone found.",
|
|
66
|
+
network: "Network error talking to the speech service.",
|
|
67
|
+
aborted: "Recognition was interrupted.",
|
|
68
|
+
"no-speech": "No speech was detected. Try again.",
|
|
69
|
+
language: "The requested language isn't supported by the engine.",
|
|
70
|
+
engine: "The speech engine reported an error.",
|
|
71
|
+
unknown: "Something went wrong with the microphone."
|
|
72
|
+
};
|
|
73
|
+
function ErrorBanner({ error, className, onDismiss }) {
|
|
74
|
+
if (!error) return null;
|
|
75
|
+
return /* @__PURE__ */ jsxs(
|
|
76
|
+
"div",
|
|
77
|
+
{
|
|
78
|
+
role: "alert",
|
|
79
|
+
className: cn(
|
|
80
|
+
"flex items-start gap-2 rounded-md border border-destructive/40 bg-destructive/10 px-3 py-2 text-xs text-destructive",
|
|
81
|
+
className
|
|
82
|
+
),
|
|
83
|
+
children: [
|
|
84
|
+
/* @__PURE__ */ jsx(AlertTriangle, { className: "mt-0.5 h-3.5 w-3.5 shrink-0" }),
|
|
85
|
+
/* @__PURE__ */ jsx("div", { className: "flex-1", children: FRIENDLY[error.code] ?? error.message }),
|
|
86
|
+
onDismiss && /* @__PURE__ */ jsx(
|
|
87
|
+
"button",
|
|
88
|
+
{
|
|
89
|
+
type: "button",
|
|
90
|
+
onClick: onDismiss,
|
|
91
|
+
className: "text-destructive hover:underline",
|
|
92
|
+
children: "Dismiss"
|
|
93
|
+
}
|
|
94
|
+
)
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
__name(ErrorBanner, "ErrorBanner");
|
|
100
|
+
function MicMeter({
|
|
101
|
+
level,
|
|
102
|
+
bars = 12,
|
|
103
|
+
gap = 2,
|
|
104
|
+
barWidth = 3,
|
|
105
|
+
height = 24,
|
|
106
|
+
className
|
|
107
|
+
}) {
|
|
108
|
+
const clamped = Math.min(1, Math.max(0, level));
|
|
109
|
+
return /* @__PURE__ */ jsx(
|
|
110
|
+
"div",
|
|
111
|
+
{
|
|
112
|
+
role: "meter",
|
|
113
|
+
"aria-valuemin": 0,
|
|
114
|
+
"aria-valuemax": 1,
|
|
115
|
+
"aria-valuenow": clamped,
|
|
116
|
+
className: cn("inline-flex items-center", className),
|
|
117
|
+
style: { gap, height },
|
|
118
|
+
children: Array.from({ length: bars }).map((_, i) => {
|
|
119
|
+
const ratio = (i + 1) / bars;
|
|
120
|
+
const active = clamped >= ratio - 0.5 / bars;
|
|
121
|
+
const heightRatio = 1 - Math.abs(i - (bars - 1) / 2) / ((bars - 1) / 2 || 1);
|
|
122
|
+
const barH = Math.max(2, height * (0.35 + heightRatio * 0.65));
|
|
123
|
+
return /* @__PURE__ */ jsx(
|
|
124
|
+
"span",
|
|
125
|
+
{
|
|
126
|
+
className: cn(
|
|
127
|
+
"rounded-sm transition-colors",
|
|
128
|
+
active ? "bg-primary" : "bg-border"
|
|
129
|
+
),
|
|
130
|
+
style: { width: barWidth, height: barH }
|
|
131
|
+
},
|
|
132
|
+
i
|
|
133
|
+
);
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
__name(MicMeter, "MicMeter");
|
|
139
|
+
function PushToTalkHint({ chord, className }) {
|
|
140
|
+
const formatted = chord.split("+").map((p) => p.trim().toLowerCase()).map((p) => {
|
|
141
|
+
switch (p) {
|
|
142
|
+
case "mod":
|
|
143
|
+
case "meta":
|
|
144
|
+
return "\u2318";
|
|
145
|
+
case "alt":
|
|
146
|
+
return "\u2325";
|
|
147
|
+
case "shift":
|
|
148
|
+
return "\u21E7";
|
|
149
|
+
case "ctrl":
|
|
150
|
+
return "\u2303";
|
|
151
|
+
default:
|
|
152
|
+
return p.length === 1 ? p.toUpperCase() : p;
|
|
153
|
+
}
|
|
154
|
+
}).join("");
|
|
155
|
+
return /* @__PURE__ */ jsxs(
|
|
156
|
+
"span",
|
|
157
|
+
{
|
|
158
|
+
className: cn(
|
|
159
|
+
"inline-flex items-center gap-1 text-[11px] text-muted-foreground",
|
|
160
|
+
className
|
|
161
|
+
),
|
|
162
|
+
children: [
|
|
163
|
+
"Hold",
|
|
164
|
+
/* @__PURE__ */ jsx("kbd", { className: "rounded border border-border bg-muted px-1 py-0.5 font-mono text-[10px] text-foreground", children: formatted }),
|
|
165
|
+
"to talk"
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
__name(PushToTalkHint, "PushToTalkHint");
|
|
171
|
+
|
|
172
|
+
// src/tools/SpeechRecognition/core/transcript.ts
|
|
173
|
+
var EMPTY_TRANSCRIPT = {
|
|
174
|
+
interim: "",
|
|
175
|
+
final: "",
|
|
176
|
+
segments: []
|
|
177
|
+
};
|
|
178
|
+
function joinFinal(segments) {
|
|
179
|
+
let out = "";
|
|
180
|
+
for (const seg of segments) {
|
|
181
|
+
if (!seg.isFinal) continue;
|
|
182
|
+
const text = seg.text.trim();
|
|
183
|
+
if (!text) continue;
|
|
184
|
+
out = out ? `${out} ${text}` : text;
|
|
185
|
+
}
|
|
186
|
+
return out;
|
|
187
|
+
}
|
|
188
|
+
__name(joinFinal, "joinFinal");
|
|
189
|
+
function buildTranscript(segments) {
|
|
190
|
+
const last = segments[segments.length - 1];
|
|
191
|
+
const interim = last && !last.isFinal ? last.text : "";
|
|
192
|
+
return {
|
|
193
|
+
interim,
|
|
194
|
+
final: joinFinal(segments),
|
|
195
|
+
segments
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
__name(buildTranscript, "buildTranscript");
|
|
199
|
+
function normaliseFinal(text) {
|
|
200
|
+
return text.replace(/\s+/g, " ").replace(/\s+([,.!?])/g, "$1").trim();
|
|
201
|
+
}
|
|
202
|
+
__name(normaliseFinal, "normaliseFinal");
|
|
203
|
+
|
|
204
|
+
// src/tools/SpeechRecognition/core/ids.ts
|
|
205
|
+
var counter = 0;
|
|
206
|
+
function newSegmentId() {
|
|
207
|
+
counter = (counter + 1) % Number.MAX_SAFE_INTEGER;
|
|
208
|
+
return `seg_${Date.now().toString(36)}_${counter.toString(36)}`;
|
|
209
|
+
}
|
|
210
|
+
__name(newSegmentId, "newSegmentId");
|
|
211
|
+
var sttLogger = consola.withTag("ui-tools:speech");
|
|
212
|
+
|
|
213
|
+
// src/tools/SpeechRecognition/core/reducer.ts
|
|
214
|
+
var INITIAL_STATE = {
|
|
215
|
+
status: "idle",
|
|
216
|
+
segments: [],
|
|
217
|
+
error: null,
|
|
218
|
+
startedAt: null
|
|
219
|
+
};
|
|
220
|
+
function nowSinceStart(state) {
|
|
221
|
+
return state.startedAt ? Date.now() - state.startedAt : 0;
|
|
222
|
+
}
|
|
223
|
+
__name(nowSinceStart, "nowSinceStart");
|
|
224
|
+
function upsertSegment(segments, patch) {
|
|
225
|
+
const idx = segments.findIndex((s) => s.id === patch.id);
|
|
226
|
+
if (idx === -1) return [...segments, patch];
|
|
227
|
+
const next = segments.slice();
|
|
228
|
+
next[idx] = { ...next[idx], ...patch };
|
|
229
|
+
return next;
|
|
230
|
+
}
|
|
231
|
+
__name(upsertSegment, "upsertSegment");
|
|
232
|
+
function reducer(state, action) {
|
|
233
|
+
switch (action.type) {
|
|
234
|
+
case "START":
|
|
235
|
+
return {
|
|
236
|
+
...state,
|
|
237
|
+
status: "starting",
|
|
238
|
+
error: null,
|
|
239
|
+
startedAt: Date.now()
|
|
240
|
+
};
|
|
241
|
+
case "STARTED":
|
|
242
|
+
return { ...state, status: "listening" };
|
|
243
|
+
case "STOP":
|
|
244
|
+
return { ...state, status: "stopping" };
|
|
245
|
+
case "STOPPED":
|
|
246
|
+
case "ABORT":
|
|
247
|
+
return { ...state, status: "idle" };
|
|
248
|
+
case "PARTIAL": {
|
|
249
|
+
const seg = {
|
|
250
|
+
id: action.segmentId,
|
|
251
|
+
text: action.text,
|
|
252
|
+
isFinal: false,
|
|
253
|
+
confidence: action.confidence,
|
|
254
|
+
startedAt: nowSinceStart(state)
|
|
255
|
+
};
|
|
256
|
+
return { ...state, segments: upsertSegment(state.segments, seg) };
|
|
257
|
+
}
|
|
258
|
+
case "FINAL": {
|
|
259
|
+
const seg = {
|
|
260
|
+
id: action.segmentId || newSegmentId(),
|
|
261
|
+
text: action.text,
|
|
262
|
+
isFinal: true,
|
|
263
|
+
confidence: action.confidence,
|
|
264
|
+
startedAt: nowSinceStart(state),
|
|
265
|
+
endedAt: nowSinceStart(state)
|
|
266
|
+
};
|
|
267
|
+
return { ...state, segments: upsertSegment(state.segments, seg) };
|
|
268
|
+
}
|
|
269
|
+
case "ERROR":
|
|
270
|
+
return { ...state, status: "error", error: action.error };
|
|
271
|
+
case "RESET":
|
|
272
|
+
return { ...INITIAL_STATE };
|
|
273
|
+
default:
|
|
274
|
+
return state;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
__name(reducer, "reducer");
|
|
278
|
+
|
|
279
|
+
// src/tools/SpeechRecognition/core/engine/index.ts
|
|
280
|
+
function createEngineBus() {
|
|
281
|
+
const listeners = {
|
|
282
|
+
partial: /* @__PURE__ */ new Set(),
|
|
283
|
+
final: /* @__PURE__ */ new Set(),
|
|
284
|
+
error: /* @__PURE__ */ new Set(),
|
|
285
|
+
state: /* @__PURE__ */ new Set()
|
|
286
|
+
};
|
|
287
|
+
return {
|
|
288
|
+
on(event, cb) {
|
|
289
|
+
const set = listeners[event];
|
|
290
|
+
set.add(cb);
|
|
291
|
+
return () => {
|
|
292
|
+
set.delete(cb);
|
|
293
|
+
};
|
|
294
|
+
},
|
|
295
|
+
emit(event, ...args) {
|
|
296
|
+
const set = listeners[event];
|
|
297
|
+
for (const cb of set) {
|
|
298
|
+
try {
|
|
299
|
+
cb(...args);
|
|
300
|
+
} catch {
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
clear() {
|
|
305
|
+
for (const key of Object.keys(listeners)) {
|
|
306
|
+
listeners[key].clear();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
__name(createEngineBus, "createEngineBus");
|
|
312
|
+
|
|
313
|
+
// src/tools/SpeechRecognition/core/engine/webspeech.ts
|
|
314
|
+
function resolveCtor() {
|
|
315
|
+
if (typeof window === "undefined") return null;
|
|
316
|
+
const w = window;
|
|
317
|
+
return w.SpeechRecognition ?? w.webkitSpeechRecognition ?? null;
|
|
318
|
+
}
|
|
319
|
+
__name(resolveCtor, "resolveCtor");
|
|
320
|
+
var ERROR_MAP = {
|
|
321
|
+
"no-speech": "no-speech",
|
|
322
|
+
aborted: "aborted",
|
|
323
|
+
"audio-capture": "no-microphone",
|
|
324
|
+
network: "network",
|
|
325
|
+
"not-allowed": "permission-denied",
|
|
326
|
+
"service-not-allowed": "permission-denied",
|
|
327
|
+
"bad-grammar": "engine",
|
|
328
|
+
"language-not-supported": "language"
|
|
329
|
+
};
|
|
330
|
+
function createWebSpeechEngine(opts = {}) {
|
|
331
|
+
const Ctor = resolveCtor();
|
|
332
|
+
const bus = createEngineBus();
|
|
333
|
+
let instance = null;
|
|
334
|
+
let currentSegmentId = null;
|
|
335
|
+
function teardown() {
|
|
336
|
+
if (!instance) return;
|
|
337
|
+
instance.onresult = null;
|
|
338
|
+
instance.onerror = null;
|
|
339
|
+
instance.onstart = null;
|
|
340
|
+
instance.onend = null;
|
|
341
|
+
instance = null;
|
|
342
|
+
currentSegmentId = null;
|
|
343
|
+
}
|
|
344
|
+
__name(teardown, "teardown");
|
|
345
|
+
return {
|
|
346
|
+
id: "webspeech",
|
|
347
|
+
isSupported: Ctor !== null,
|
|
348
|
+
on(event, cb) {
|
|
349
|
+
return bus.on(event, cb);
|
|
350
|
+
},
|
|
351
|
+
async start(start) {
|
|
352
|
+
if (!Ctor) {
|
|
353
|
+
const err = {
|
|
354
|
+
code: "unsupported",
|
|
355
|
+
message: "Web Speech API is not available in this browser."
|
|
356
|
+
};
|
|
357
|
+
bus.emit("error", err);
|
|
358
|
+
throw err;
|
|
359
|
+
}
|
|
360
|
+
if (instance) {
|
|
361
|
+
sttLogger.debug("[webspeech] start() called while running \u2014 ignoring");
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
bus.emit("state", "connecting");
|
|
365
|
+
const rec = new Ctor();
|
|
366
|
+
rec.lang = start.language;
|
|
367
|
+
rec.interimResults = start.interim;
|
|
368
|
+
rec.continuous = opts.continuous ?? true;
|
|
369
|
+
rec.maxAlternatives = opts.maxAlternatives ?? 1;
|
|
370
|
+
rec.onstart = () => {
|
|
371
|
+
bus.emit("state", "listening");
|
|
372
|
+
};
|
|
373
|
+
rec.onend = () => {
|
|
374
|
+
bus.emit("state", "closed");
|
|
375
|
+
teardown();
|
|
376
|
+
};
|
|
377
|
+
rec.onerror = (e) => {
|
|
378
|
+
const code = ERROR_MAP[e.error] ?? "engine";
|
|
379
|
+
const err = {
|
|
380
|
+
code,
|
|
381
|
+
message: e.message || `Web Speech error: ${e.error}`
|
|
382
|
+
};
|
|
383
|
+
bus.emit("error", err);
|
|
384
|
+
};
|
|
385
|
+
rec.onresult = (e) => {
|
|
386
|
+
for (let i = e.resultIndex; i < e.results.length; i += 1) {
|
|
387
|
+
const res = e.results[i];
|
|
388
|
+
const alt = res[0];
|
|
389
|
+
const text = alt.transcript;
|
|
390
|
+
if (!currentSegmentId) currentSegmentId = newSegmentId();
|
|
391
|
+
if (res.isFinal) {
|
|
392
|
+
bus.emit("final", text, currentSegmentId, alt.confidence);
|
|
393
|
+
currentSegmentId = null;
|
|
394
|
+
} else {
|
|
395
|
+
bus.emit("partial", text, currentSegmentId);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
if (start.signal) {
|
|
400
|
+
start.signal.addEventListener("abort", () => {
|
|
401
|
+
rec.abort();
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
instance = rec;
|
|
405
|
+
try {
|
|
406
|
+
rec.start();
|
|
407
|
+
} catch (cause) {
|
|
408
|
+
const err = {
|
|
409
|
+
code: "engine",
|
|
410
|
+
message: "Failed to start Web Speech recognition.",
|
|
411
|
+
cause
|
|
412
|
+
};
|
|
413
|
+
bus.emit("error", err);
|
|
414
|
+
teardown();
|
|
415
|
+
throw err;
|
|
416
|
+
}
|
|
417
|
+
},
|
|
418
|
+
async stop() {
|
|
419
|
+
if (!instance) return;
|
|
420
|
+
bus.emit("state", "closing");
|
|
421
|
+
instance.stop();
|
|
422
|
+
},
|
|
423
|
+
abort() {
|
|
424
|
+
if (!instance) return;
|
|
425
|
+
instance.abort();
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
__name(createWebSpeechEngine, "createWebSpeechEngine");
|
|
430
|
+
var DEFAULTS = {
|
|
431
|
+
language: null,
|
|
432
|
+
deviceId: null,
|
|
433
|
+
engineId: null,
|
|
434
|
+
earcons: false
|
|
435
|
+
};
|
|
436
|
+
var useSpeechPrefs = create()(
|
|
437
|
+
persist(
|
|
438
|
+
(set) => ({
|
|
439
|
+
...DEFAULTS,
|
|
440
|
+
setLanguage: /* @__PURE__ */ __name((language) => set({ language }), "setLanguage"),
|
|
441
|
+
setDeviceId: /* @__PURE__ */ __name((deviceId) => set({ deviceId }), "setDeviceId"),
|
|
442
|
+
setEngineId: /* @__PURE__ */ __name((engineId) => set({ engineId }), "setEngineId"),
|
|
443
|
+
setEarcons: /* @__PURE__ */ __name((earcons) => set({ earcons }), "setEarcons"),
|
|
444
|
+
reset: /* @__PURE__ */ __name(() => set({ ...DEFAULTS }), "reset")
|
|
445
|
+
}),
|
|
446
|
+
{
|
|
447
|
+
name: "djangocfg-stt:prefs",
|
|
448
|
+
storage: createJSONStorage(
|
|
449
|
+
() => typeof window === "undefined" ? void 0 : window.localStorage
|
|
450
|
+
)
|
|
451
|
+
}
|
|
452
|
+
)
|
|
453
|
+
);
|
|
454
|
+
function useMicLevel(stream) {
|
|
455
|
+
const [level, setLevel] = useState(0);
|
|
456
|
+
const raf = useRef(null);
|
|
457
|
+
useEffect(() => {
|
|
458
|
+
if (!stream) {
|
|
459
|
+
setLevel(0);
|
|
460
|
+
return void 0;
|
|
461
|
+
}
|
|
462
|
+
const AC = window.AudioContext ?? window.webkitAudioContext;
|
|
463
|
+
if (!AC) return void 0;
|
|
464
|
+
const ctx = new AC();
|
|
465
|
+
const source = ctx.createMediaStreamSource(stream);
|
|
466
|
+
const analyser = ctx.createAnalyser();
|
|
467
|
+
analyser.fftSize = 1024;
|
|
468
|
+
analyser.smoothingTimeConstant = 0.7;
|
|
469
|
+
source.connect(analyser);
|
|
470
|
+
const buf = new Float32Array(analyser.fftSize);
|
|
471
|
+
const tick = /* @__PURE__ */ __name(() => {
|
|
472
|
+
analyser.getFloatTimeDomainData(buf);
|
|
473
|
+
let sum = 0;
|
|
474
|
+
for (let i = 0; i < buf.length; i += 1) sum += buf[i] * buf[i];
|
|
475
|
+
const rms = Math.sqrt(sum / buf.length);
|
|
476
|
+
setLevel(Math.min(1, rms * 2.5));
|
|
477
|
+
raf.current = requestAnimationFrame(tick);
|
|
478
|
+
}, "tick");
|
|
479
|
+
tick();
|
|
480
|
+
return () => {
|
|
481
|
+
if (raf.current != null) cancelAnimationFrame(raf.current);
|
|
482
|
+
raf.current = null;
|
|
483
|
+
source.disconnect();
|
|
484
|
+
analyser.disconnect();
|
|
485
|
+
void ctx.close();
|
|
486
|
+
};
|
|
487
|
+
}, [stream]);
|
|
488
|
+
return level;
|
|
489
|
+
}
|
|
490
|
+
__name(useMicLevel, "useMicLevel");
|
|
491
|
+
|
|
492
|
+
// src/tools/SpeechRecognition/core/language.ts
|
|
493
|
+
var ISO_TO_BCP47 = {
|
|
494
|
+
en: "en-US",
|
|
495
|
+
ru: "ru-RU",
|
|
496
|
+
ko: "ko-KR",
|
|
497
|
+
ja: "ja-JP",
|
|
498
|
+
zh: "zh-CN",
|
|
499
|
+
de: "de-DE",
|
|
500
|
+
fr: "fr-FR",
|
|
501
|
+
it: "it-IT",
|
|
502
|
+
es: "es-ES",
|
|
503
|
+
nl: "nl-NL",
|
|
504
|
+
ar: "ar-SA",
|
|
505
|
+
tr: "tr-TR",
|
|
506
|
+
pl: "pl-PL",
|
|
507
|
+
sv: "sv-SE",
|
|
508
|
+
no: "nb-NO",
|
|
509
|
+
da: "da-DK",
|
|
510
|
+
pt: "pt-BR"
|
|
511
|
+
};
|
|
512
|
+
function toBCP47(code, table = ISO_TO_BCP47) {
|
|
513
|
+
if (!code) return void 0;
|
|
514
|
+
const trimmed = code.trim();
|
|
515
|
+
if (!trimmed) return void 0;
|
|
516
|
+
if (trimmed.includes("-")) return trimmed;
|
|
517
|
+
const lower = trimmed.toLowerCase();
|
|
518
|
+
return table[lower] ?? `${lower}-${lower.toUpperCase()}`;
|
|
519
|
+
}
|
|
520
|
+
__name(toBCP47, "toBCP47");
|
|
521
|
+
function resolveSpeechLanguage(opts) {
|
|
522
|
+
return toBCP47(opts.explicit) ?? toBCP47(opts.prefs) ?? toBCP47(opts.i18n) ?? toBCP47(typeof navigator !== "undefined" ? navigator.language : null) ?? "en-US";
|
|
523
|
+
}
|
|
524
|
+
__name(resolveSpeechLanguage, "resolveSpeechLanguage");
|
|
525
|
+
|
|
526
|
+
// src/tools/SpeechRecognition/hooks/useResolvedLanguage.ts
|
|
527
|
+
function useResolvedLanguage(explicit) {
|
|
528
|
+
const prefs = useSpeechPrefs();
|
|
529
|
+
const locale = useLocaleOptional();
|
|
530
|
+
return resolveSpeechLanguage({
|
|
531
|
+
explicit,
|
|
532
|
+
prefs: prefs.language,
|
|
533
|
+
i18n: locale
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
__name(useResolvedLanguage, "useResolvedLanguage");
|
|
537
|
+
|
|
538
|
+
// src/tools/SpeechRecognition/hooks/useSpeechRecognition.ts
|
|
539
|
+
function useSpeechRecognition(config = {}) {
|
|
540
|
+
const prefs = useSpeechPrefs();
|
|
541
|
+
const language = useResolvedLanguage(config.language);
|
|
542
|
+
const engine = useMemo(
|
|
543
|
+
() => config.engine ?? createWebSpeechEngine(),
|
|
544
|
+
[config.engine]
|
|
545
|
+
);
|
|
546
|
+
const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
|
|
547
|
+
const [stream, setStream] = useState(null);
|
|
548
|
+
const level = useMicLevel(stream);
|
|
549
|
+
const cbRef = useRef(config);
|
|
550
|
+
cbRef.current = config;
|
|
551
|
+
useEffect(() => {
|
|
552
|
+
const offs = [
|
|
553
|
+
engine.on("partial", (text, segmentId) => {
|
|
554
|
+
dispatch({ type: "PARTIAL", text, segmentId });
|
|
555
|
+
const seg = {
|
|
556
|
+
id: segmentId,
|
|
557
|
+
text,
|
|
558
|
+
isFinal: false,
|
|
559
|
+
startedAt: Date.now()
|
|
560
|
+
};
|
|
561
|
+
cbRef.current.onPartial?.(text, seg);
|
|
562
|
+
}),
|
|
563
|
+
engine.on("final", (text, segmentId, confidence) => {
|
|
564
|
+
dispatch({ type: "FINAL", text, segmentId, confidence });
|
|
565
|
+
const seg = {
|
|
566
|
+
id: segmentId,
|
|
567
|
+
text,
|
|
568
|
+
isFinal: true,
|
|
569
|
+
confidence,
|
|
570
|
+
startedAt: Date.now(),
|
|
571
|
+
endedAt: Date.now()
|
|
572
|
+
};
|
|
573
|
+
cbRef.current.onFinal?.(text, seg);
|
|
574
|
+
}),
|
|
575
|
+
engine.on("error", (err) => {
|
|
576
|
+
dispatch({ type: "ERROR", error: err });
|
|
577
|
+
cbRef.current.onError?.(err);
|
|
578
|
+
}),
|
|
579
|
+
engine.on("state", (s) => {
|
|
580
|
+
if (s === "listening") {
|
|
581
|
+
dispatch({ type: "STARTED" });
|
|
582
|
+
cbRef.current.onStart?.();
|
|
583
|
+
setStream(engine.getStream?.() ?? null);
|
|
584
|
+
} else if (s === "closed") {
|
|
585
|
+
dispatch({ type: "STOPPED" });
|
|
586
|
+
cbRef.current.onStop?.();
|
|
587
|
+
setStream(null);
|
|
588
|
+
}
|
|
589
|
+
})
|
|
590
|
+
];
|
|
591
|
+
return () => {
|
|
592
|
+
offs.forEach((off) => off());
|
|
593
|
+
};
|
|
594
|
+
}, [engine]);
|
|
595
|
+
const silenceTimer = useRef(null);
|
|
596
|
+
const maxTimer = useRef(null);
|
|
597
|
+
useEffect(() => {
|
|
598
|
+
if (state.status !== "listening") return void 0;
|
|
599
|
+
const { silenceMs, maxMs, silenceThreshold = 0.02 } = config.autoStop ?? {};
|
|
600
|
+
if (maxMs) {
|
|
601
|
+
maxTimer.current = window.setTimeout(() => {
|
|
602
|
+
sttLogger.debug("[autoStop] max duration hit");
|
|
603
|
+
void engine.stop();
|
|
604
|
+
}, maxMs);
|
|
605
|
+
}
|
|
606
|
+
if (silenceMs) {
|
|
607
|
+
const checkInterval = window.setInterval(() => {
|
|
608
|
+
if (level < silenceThreshold) {
|
|
609
|
+
if (silenceTimer.current == null) {
|
|
610
|
+
silenceTimer.current = window.setTimeout(() => {
|
|
611
|
+
sttLogger.debug("[autoStop] silence detected");
|
|
612
|
+
void engine.stop();
|
|
613
|
+
}, silenceMs);
|
|
614
|
+
}
|
|
615
|
+
} else if (silenceTimer.current != null) {
|
|
616
|
+
clearTimeout(silenceTimer.current);
|
|
617
|
+
silenceTimer.current = null;
|
|
618
|
+
}
|
|
619
|
+
}, 200);
|
|
620
|
+
return () => {
|
|
621
|
+
clearInterval(checkInterval);
|
|
622
|
+
if (silenceTimer.current != null) clearTimeout(silenceTimer.current);
|
|
623
|
+
silenceTimer.current = null;
|
|
624
|
+
if (maxTimer.current != null) clearTimeout(maxTimer.current);
|
|
625
|
+
maxTimer.current = null;
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
return () => {
|
|
629
|
+
if (maxTimer.current != null) clearTimeout(maxTimer.current);
|
|
630
|
+
maxTimer.current = null;
|
|
631
|
+
};
|
|
632
|
+
}, [state.status, config.autoStop, level, engine]);
|
|
633
|
+
const start = useCallback(async () => {
|
|
634
|
+
if (state.status === "listening" || state.status === "starting") return;
|
|
635
|
+
dispatch({ type: "START" });
|
|
636
|
+
try {
|
|
637
|
+
await engine.start({
|
|
638
|
+
language,
|
|
639
|
+
interim: config.interim ?? true,
|
|
640
|
+
deviceId: config.deviceId ?? prefs.deviceId ?? void 0
|
|
641
|
+
});
|
|
642
|
+
} catch (cause) {
|
|
643
|
+
sttLogger.debug("[start] engine threw", cause);
|
|
644
|
+
}
|
|
645
|
+
}, [engine, language, config.interim, config.deviceId, prefs.deviceId, state.status]);
|
|
646
|
+
const stop = useCallback(async () => {
|
|
647
|
+
if (state.status === "idle" || state.status === "stopping") return;
|
|
648
|
+
dispatch({ type: "STOP" });
|
|
649
|
+
await engine.stop();
|
|
650
|
+
}, [engine, state.status]);
|
|
651
|
+
const abort = useCallback(() => {
|
|
652
|
+
engine.abort();
|
|
653
|
+
dispatch({ type: "ABORT" });
|
|
654
|
+
}, [engine]);
|
|
655
|
+
const toggle = useCallback(async () => {
|
|
656
|
+
if (state.status === "listening" || state.status === "starting") {
|
|
657
|
+
await stop();
|
|
658
|
+
} else {
|
|
659
|
+
await start();
|
|
660
|
+
}
|
|
661
|
+
}, [state.status, start, stop]);
|
|
662
|
+
const reset = useCallback(() => {
|
|
663
|
+
dispatch({ type: "RESET" });
|
|
664
|
+
}, []);
|
|
665
|
+
const transcript = useMemo(
|
|
666
|
+
() => state.segments.length === 0 ? EMPTY_TRANSCRIPT : buildTranscript(state.segments),
|
|
667
|
+
[state.segments]
|
|
668
|
+
);
|
|
669
|
+
return {
|
|
670
|
+
status: state.status,
|
|
671
|
+
isSupported: engine.isSupported,
|
|
672
|
+
transcript,
|
|
673
|
+
error: state.error,
|
|
674
|
+
level,
|
|
675
|
+
start,
|
|
676
|
+
stop,
|
|
677
|
+
abort,
|
|
678
|
+
toggle,
|
|
679
|
+
reset
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
__name(useSpeechRecognition, "useSpeechRecognition");
|
|
683
|
+
|
|
684
|
+
// src/tools/SpeechRecognition/hooks/useDictation.ts
|
|
685
|
+
function useDictation(config) {
|
|
686
|
+
const { value, onChange, separator = " ", ...rest } = config;
|
|
687
|
+
const valueRef = useRef(value);
|
|
688
|
+
valueRef.current = value;
|
|
689
|
+
const onChangeRef = useRef(onChange);
|
|
690
|
+
onChangeRef.current = onChange;
|
|
691
|
+
const rec = useSpeechRecognition({
|
|
692
|
+
...rest,
|
|
693
|
+
onFinal: /* @__PURE__ */ __name((text) => {
|
|
694
|
+
const clean = normaliseFinal(text);
|
|
695
|
+
if (!clean) return;
|
|
696
|
+
const prev = valueRef.current;
|
|
697
|
+
const next = prev ? `${prev}${separator}${clean}` : clean;
|
|
698
|
+
onChangeRef.current(next);
|
|
699
|
+
}, "onFinal")
|
|
700
|
+
});
|
|
701
|
+
useEffect(() => {
|
|
702
|
+
valueRef.current = value;
|
|
703
|
+
}, [value]);
|
|
704
|
+
return {
|
|
705
|
+
...rec,
|
|
706
|
+
toggleDictation: rec.toggle
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
__name(useDictation, "useDictation");
|
|
710
|
+
var MOD_KEYS = /* @__PURE__ */ new Set(["shift", "ctrl", "alt", "meta", "mod"]);
|
|
711
|
+
function parseChord(chord) {
|
|
712
|
+
const parts = chord.toLowerCase().split("+").map((s) => s.trim());
|
|
713
|
+
const mods = /* @__PURE__ */ new Set();
|
|
714
|
+
let main = null;
|
|
715
|
+
for (const part of parts) {
|
|
716
|
+
if (MOD_KEYS.has(part)) {
|
|
717
|
+
mods.add(part === "mod" ? "meta" : part);
|
|
718
|
+
} else {
|
|
719
|
+
main = part;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
return { mods, main };
|
|
723
|
+
}
|
|
724
|
+
__name(parseChord, "parseChord");
|
|
725
|
+
function matches(e, mods, main) {
|
|
726
|
+
if (mods.has("shift") !== e.shiftKey) return false;
|
|
727
|
+
if (mods.has("ctrl") !== e.ctrlKey) return false;
|
|
728
|
+
if (mods.has("alt") !== e.altKey) return false;
|
|
729
|
+
if (mods.has("meta") !== (e.metaKey || !e.metaKey && false)) return false;
|
|
730
|
+
if (main && e.key.toLowerCase() !== main) return false;
|
|
731
|
+
return true;
|
|
732
|
+
}
|
|
733
|
+
__name(matches, "matches");
|
|
734
|
+
function usePushToTalk(recognition, opts) {
|
|
735
|
+
const { key, enabled = true } = opts;
|
|
736
|
+
useEffect(() => {
|
|
737
|
+
if (!enabled || typeof window === "undefined") return void 0;
|
|
738
|
+
const { mods, main } = parseChord(key);
|
|
739
|
+
const onDown = /* @__PURE__ */ __name((e) => {
|
|
740
|
+
if (e.repeat) return;
|
|
741
|
+
if (!main && mods.size === 0) return;
|
|
742
|
+
const target = e.target;
|
|
743
|
+
const inField = target?.tagName === "INPUT" || target?.tagName === "TEXTAREA" || target?.isContentEditable;
|
|
744
|
+
if (inField && mods.size === 0) return;
|
|
745
|
+
if (!matches(e, mods, main)) return;
|
|
746
|
+
if (recognition.status === "listening" || recognition.status === "starting") return;
|
|
747
|
+
e.preventDefault();
|
|
748
|
+
void recognition.start();
|
|
749
|
+
}, "onDown");
|
|
750
|
+
const onUp = /* @__PURE__ */ __name((e) => {
|
|
751
|
+
if (!matches(e, mods, main) && main && e.key.toLowerCase() !== main) return;
|
|
752
|
+
if (recognition.status !== "listening" && recognition.status !== "starting") return;
|
|
753
|
+
void recognition.stop();
|
|
754
|
+
}, "onUp");
|
|
755
|
+
window.addEventListener("keydown", onDown);
|
|
756
|
+
window.addEventListener("keyup", onUp);
|
|
757
|
+
return () => {
|
|
758
|
+
window.removeEventListener("keydown", onDown);
|
|
759
|
+
window.removeEventListener("keyup", onUp);
|
|
760
|
+
};
|
|
761
|
+
}, [enabled, key, recognition]);
|
|
762
|
+
}
|
|
763
|
+
__name(usePushToTalk, "usePushToTalk");
|
|
764
|
+
function DictationField({
|
|
765
|
+
value,
|
|
766
|
+
onChange,
|
|
767
|
+
engine,
|
|
768
|
+
language,
|
|
769
|
+
pushToTalk,
|
|
770
|
+
placeholder = "Type or press the mic to dictate\u2026",
|
|
771
|
+
rows = 3,
|
|
772
|
+
disabled,
|
|
773
|
+
showInterim = true,
|
|
774
|
+
showMeter = true,
|
|
775
|
+
className,
|
|
776
|
+
textareaClassName
|
|
777
|
+
}) {
|
|
778
|
+
const rec = useDictation({
|
|
779
|
+
value,
|
|
780
|
+
onChange,
|
|
781
|
+
engine,
|
|
782
|
+
language
|
|
783
|
+
});
|
|
784
|
+
usePushToTalk(rec, {
|
|
785
|
+
key: pushToTalk?.key ?? "alt",
|
|
786
|
+
enabled: !!pushToTalk && pushToTalk.enabled !== false
|
|
787
|
+
});
|
|
788
|
+
return /* @__PURE__ */ jsxs("div", { className: cn("flex flex-col gap-2", className), children: [
|
|
789
|
+
/* @__PURE__ */ jsxs("div", { className: "relative", children: [
|
|
790
|
+
/* @__PURE__ */ jsx(
|
|
791
|
+
"textarea",
|
|
792
|
+
{
|
|
793
|
+
value,
|
|
794
|
+
onChange: (e) => onChange(e.target.value),
|
|
795
|
+
placeholder,
|
|
796
|
+
rows,
|
|
797
|
+
disabled,
|
|
798
|
+
className: cn(
|
|
799
|
+
"w-full resize-y rounded-md border border-input bg-background px-3 py-2 text-sm",
|
|
800
|
+
"placeholder:text-muted-foreground",
|
|
801
|
+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
|
|
802
|
+
"disabled:cursor-not-allowed disabled:opacity-50",
|
|
803
|
+
textareaClassName
|
|
804
|
+
)
|
|
805
|
+
}
|
|
806
|
+
),
|
|
807
|
+
showInterim && rec.transcript.interim && /* @__PURE__ */ jsxs("div", { className: "pointer-events-none mt-1 text-xs italic text-muted-foreground", children: [
|
|
808
|
+
"\u2026 ",
|
|
809
|
+
rec.transcript.interim
|
|
810
|
+
] })
|
|
811
|
+
] }),
|
|
812
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
|
|
813
|
+
/* @__PURE__ */ jsx(
|
|
814
|
+
DictationButton,
|
|
815
|
+
{
|
|
816
|
+
status: rec.status,
|
|
817
|
+
isSupported: rec.isSupported,
|
|
818
|
+
onClick: () => void rec.toggleDictation(),
|
|
819
|
+
size: "sm",
|
|
820
|
+
disabled
|
|
821
|
+
}
|
|
822
|
+
),
|
|
823
|
+
showMeter && /* @__PURE__ */ jsx(MicMeter, { level: rec.level, bars: 10, height: 20 }),
|
|
824
|
+
pushToTalk && /* @__PURE__ */ jsx(PushToTalkHint, { chord: pushToTalk.key, className: "ml-auto" })
|
|
825
|
+
] }),
|
|
826
|
+
/* @__PURE__ */ jsx(ErrorBanner, { error: rec.error })
|
|
827
|
+
] });
|
|
828
|
+
}
|
|
829
|
+
__name(DictationField, "DictationField");
|
|
830
|
+
|
|
831
|
+
export { DictationField, useResolvedLanguage, useSpeechPrefs };
|
|
832
|
+
//# sourceMappingURL=chunk-C2YN6WEO.mjs.map
|
|
833
|
+
//# sourceMappingURL=chunk-C2YN6WEO.mjs.map
|