@tensorpass/aiconnect-react 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -1
- package/dist/index.js.map +1 -1
- package/dist/moduleMessages.d.ts +22 -0
- package/dist/moduleMessages.js +110 -0
- package/dist/moduleMessages.js.map +1 -1
- package/dist/modules/AiSearch.js +16 -5
- package/dist/modules/AiSearch.js.map +1 -1
- package/dist/modules/parts/ChatFrame.d.ts +1 -1
- package/dist/modules/parts/ChatFrame.js +11 -2
- package/dist/modules/parts/ChatFrame.js.map +1 -1
- package/dist/modules/parts/Composer.d.ts +9 -2
- package/dist/modules/parts/Composer.js +49 -12
- package/dist/modules/parts/Composer.js.map +1 -1
- package/dist/modules/parts/Conversation.js +14 -11
- package/dist/modules/parts/Conversation.js.map +1 -1
- package/dist/modules/parts/attachments/AttachButton.d.ts +5 -0
- package/dist/modules/parts/attachments/AttachButton.js +29 -0
- package/dist/modules/parts/attachments/AttachButton.js.map +1 -0
- package/dist/modules/parts/attachments/AttachmentChip.d.ts +12 -0
- package/dist/modules/parts/attachments/AttachmentChip.js +17 -0
- package/dist/modules/parts/attachments/AttachmentChip.js.map +1 -0
- package/dist/modules/parts/attachments/AttachmentRows.d.ts +11 -0
- package/dist/modules/parts/attachments/AttachmentRows.js +29 -0
- package/dist/modules/parts/attachments/AttachmentRows.js.map +1 -0
- package/dist/modules/parts/attachments/DropZone.d.ts +8 -0
- package/dist/modules/parts/attachments/DropZone.js +57 -0
- package/dist/modules/parts/attachments/DropZone.js.map +1 -0
- package/dist/modules/parts/attachments/Lightbox.d.ts +10 -0
- package/dist/modules/parts/attachments/Lightbox.js +43 -0
- package/dist/modules/parts/attachments/Lightbox.js.map +1 -0
- package/dist/modules/parts/attachments/attachmentList.d.ts +25 -0
- package/dist/modules/parts/attachments/attachmentList.js +26 -0
- package/dist/modules/parts/attachments/attachmentList.js.map +1 -0
- package/dist/modules/parts/attachments/fileSize.d.ts +1 -0
- package/dist/modules/parts/attachments/fileSize.js +14 -0
- package/dist/modules/parts/attachments/fileSize.js.map +1 -0
- package/dist/modules/parts/attachments/useAttachments.d.ts +13 -0
- package/dist/modules/parts/attachments/useAttachments.js +77 -0
- package/dist/modules/parts/attachments/useAttachments.js.map +1 -0
- package/dist/modules/parts/dictation/DictationBar.d.ts +4 -0
- package/dist/modules/parts/dictation/DictationBar.js +26 -0
- package/dist/modules/parts/dictation/DictationBar.js.map +1 -0
- package/dist/modules/parts/dictation/MicButton.d.ts +5 -0
- package/dist/modules/parts/dictation/MicButton.js +20 -0
- package/dist/modules/parts/dictation/MicButton.js.map +1 -0
- package/dist/modules/parts/dictation/audioCapture.d.ts +7 -0
- package/dist/modules/parts/dictation/audioCapture.js +44 -0
- package/dist/modules/parts/dictation/audioCapture.js.map +1 -0
- package/dist/modules/parts/dictation/browserSpeech.d.ts +37 -0
- package/dist/modules/parts/dictation/browserSpeech.js +32 -0
- package/dist/modules/parts/dictation/browserSpeech.js.map +1 -0
- package/dist/modules/parts/dictation/dictationLanguage.d.ts +2 -0
- package/dist/modules/parts/dictation/dictationLanguage.js +15 -0
- package/dist/modules/parts/dictation/dictationLanguage.js.map +1 -0
- package/dist/modules/parts/dictation/pcmWorklet.d.ts +1 -0
- package/dist/modules/parts/dictation/pcmWorklet.js +48 -0
- package/dist/modules/parts/dictation/pcmWorklet.js.map +1 -0
- package/dist/modules/parts/dictation/pcmWorkletUrl.d.ts +1 -0
- package/dist/modules/parts/dictation/pcmWorkletUrl.js +5 -0
- package/dist/modules/parts/dictation/pcmWorkletUrl.js.map +1 -0
- package/dist/modules/parts/dictation/recording.d.ts +19 -0
- package/dist/modules/parts/dictation/recording.js +45 -0
- package/dist/modules/parts/dictation/recording.js.map +1 -0
- package/dist/modules/parts/dictation/useDictation.d.ts +22 -0
- package/dist/modules/parts/dictation/useDictation.js +164 -0
- package/dist/modules/parts/dictation/useDictation.js.map +1 -0
- package/dist/modules/parts/icons.d.ts +5 -0
- package/dist/modules/parts/icons.js +6 -0
- package/dist/modules/parts/icons.js.map +1 -1
- package/dist/modules/parts/moduleAnalytics.d.ts +1 -1
- package/dist/modules/parts/moduleAnalytics.js.map +1 -1
- package/dist/modules/parts/useModuleChat.d.ts +6 -3
- package/dist/modules/parts/useModuleChat.js +20 -16
- package/dist/modules/parts/useModuleChat.js.map +1 -1
- package/dist/modules/types.d.ts +2 -0
- package/dist/modules/types.js +3 -2
- package/dist/modules/types.js.map +1 -1
- package/dist/modules/useOverlay.js +2 -1
- package/dist/modules/useOverlay.js.map +1 -1
- package/dist/styles.css +394 -0
- package/dist/transcriptBudget.d.ts +3 -0
- package/dist/transcriptBudget.js +34 -0
- package/dist/transcriptBudget.js.map +1 -0
- package/dist/useAiChat.d.ts +10 -1
- package/dist/useAiChat.js +30 -9
- package/dist/useAiChat.js.map +1 -1
- package/package.json +5 -4
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// The files of the question being written: added from a drop, a paste or the attach button, checked against
|
|
2
|
+
// the site's policy, read in the page, and taken out of the list when the visitor removes them or the
|
|
3
|
+
// question is sent. A file the policy refuses leaves a note saying why, in the module's language.
|
|
4
|
+
import { useCallback, useEffect, useReducer, useRef, useState } from 'react';
|
|
5
|
+
import { checkAttachment, DEFAULT_MAX_ATTACHMENT_BYTES, DEFAULT_MAX_ATTACHMENTS, fitsQuestion, prepareAttachment, UnreadableFileError, } from '@tensorpass/aiconnect/attachments';
|
|
6
|
+
import { useAiConnect } from "../../../AiConnectProvider.js";
|
|
7
|
+
import { attachmentList, heldBytes, readyAttachments, stillReading } from "./attachmentList.js";
|
|
8
|
+
import { fileSize } from "./fileSize.js";
|
|
9
|
+
function refusalText(refusal, policy, copy, locale) {
|
|
10
|
+
switch (refusal) {
|
|
11
|
+
case 'type':
|
|
12
|
+
return copy.attachRefusedType;
|
|
13
|
+
case 'count':
|
|
14
|
+
return copy.attachRefusedCount.replaceAll('{count}', String(policy.maxFiles ?? DEFAULT_MAX_ATTACHMENTS));
|
|
15
|
+
case 'size': {
|
|
16
|
+
const cap = fileSize(policy.maxBytes ?? DEFAULT_MAX_ATTACHMENT_BYTES, locale);
|
|
17
|
+
return copy.attachRefusedSize.replaceAll('{size}', cap);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export function useAttachments(policy) {
|
|
22
|
+
const { moduleMessages, locale } = useAiConnect();
|
|
23
|
+
const [items, dispatch] = useReducer(attachmentList, []);
|
|
24
|
+
// The list as last rendered, for a file read after other files changed it.
|
|
25
|
+
const latest = useRef(items);
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
latest.current = items;
|
|
28
|
+
}, [items]);
|
|
29
|
+
const [note, setNote] = useState(null);
|
|
30
|
+
const read = useCallback(async (file, item, mediaType, rules) => {
|
|
31
|
+
try {
|
|
32
|
+
const prepared = await prepareAttachment(file, item.kind, mediaType);
|
|
33
|
+
if (!fitsQuestion(prepared.bytes, heldBytes(latest.current, item.id), rules)) {
|
|
34
|
+
dispatch({ type: 'remove', id: item.id });
|
|
35
|
+
setNote(refusalText('size', rules, moduleMessages, locale));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
dispatch({ type: 'ready', id: item.id, prepared });
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
dispatch({ type: 'remove', id: item.id });
|
|
42
|
+
setNote(error instanceof UnreadableFileError
|
|
43
|
+
? moduleMessages.attachUnreadable.replaceAll('{name}', file.name)
|
|
44
|
+
: moduleMessages.attachRefusedType);
|
|
45
|
+
}
|
|
46
|
+
}, [moduleMessages, locale]);
|
|
47
|
+
const add = useCallback((files) => {
|
|
48
|
+
if (!policy) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
setNote(null);
|
|
52
|
+
let count = latest.current.length;
|
|
53
|
+
for (const file of files) {
|
|
54
|
+
const check = checkAttachment(file, policy, count);
|
|
55
|
+
if (!check.ok) {
|
|
56
|
+
setNote(refusalText(check.refusal, policy, moduleMessages, locale));
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
count += 1;
|
|
60
|
+
const item = { id: crypto.randomUUID(), name: file.name, size: file.size, kind: check.kind, prepared: null };
|
|
61
|
+
dispatch({ type: 'add', item });
|
|
62
|
+
void read(file, item, check.mediaType, policy);
|
|
63
|
+
}
|
|
64
|
+
}, [policy, moduleMessages, locale, read]);
|
|
65
|
+
const remove = useCallback((id) => {
|
|
66
|
+
setNote(null);
|
|
67
|
+
dispatch({ type: 'remove', id });
|
|
68
|
+
}, []);
|
|
69
|
+
const clear = useCallback(() => {
|
|
70
|
+
setNote(null);
|
|
71
|
+
dispatch({ type: 'clear' });
|
|
72
|
+
}, []);
|
|
73
|
+
return {
|
|
74
|
+
policy, items, note, add, remove, clear, ready: readyAttachments(items), reading: stillReading(items),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=useAttachments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAttachments.js","sourceRoot":"","sources":["../../../../src/modules/parts/attachments/useAttachments.ts"],"names":[],"mappings":"AAAA,4GAA4G;AAC5G,sGAAsG;AACtG,kGAAkG;AAElG,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC7E,OAAO,EACL,eAAe,EAAE,4BAA4B,EAAE,uBAAuB,EAAE,YAAY,EAAE,iBAAiB,EACvG,mBAAmB,GACpB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAE,MAAM,+BAAgC,CAAC;AAE9D,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEhG,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC,SAAS,WAAW,CAAC,OAA0B,EAAE,MAAwB,EAAE,IAAoB,EAAE,MAAc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KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,CAAC,CAAC,CAAC;gBACpE,SAAS;YACX,CAAC;YACD,KAAK,IAAI,CAAC,CAAC;YACX,MAAM,IAAI,GAAG,EAAE,EAAE,EAAE,MAAM,CAAC,UAAU,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;YAC7G,QAAQ,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;YAChC,KAAK,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACjD,CAAC;IACH,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;IAE3C,MAAM,MAAM,GAAG,WAAW,CAAC,CAAC,EAAU,EAAE,EAAE;QACxC,OAAO,CAAC,IAAI,CAAC,CAAC;QACd,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAC;IACnC,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;QAC7B,OAAO,CAAC,IAAI,CAAC,CAAC;QACd,QAAQ,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IAC9B,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO;QACL,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,KAAK,CAAC;KACtG,CAAC;AACJ,CAAC","sourcesContent":["// The files of the question being written: added from a drop, a paste or the attach button, checked against\n// the site's policy, read in the page, and taken out of the list when the visitor removes them or the\n// question is sent. A file the policy refuses leaves a note saying why, in the module's language.\n\nimport { useCallback, useEffect, useReducer, useRef, useState } from 'react';\nimport {\n checkAttachment, DEFAULT_MAX_ATTACHMENT_BYTES, DEFAULT_MAX_ATTACHMENTS, fitsQuestion, prepareAttachment,\n UnreadableFileError,\n} from '@tensorpass/aiconnect/attachments';\nimport type { AttachmentPolicy, AttachmentRefusal } from '@tensorpass/aiconnect/attachments';\nimport { useAiConnect } from '../../../AiConnectProvider.tsx';\nimport type { ModuleMessages } from '../../../messages.ts';\nimport { attachmentList, heldBytes, readyAttachments, stillReading } from './attachmentList.ts';\nimport type { AttachmentItem } from './attachmentList.ts';\nimport { fileSize } from './fileSize.ts';\n\nfunction refusalText(refusal: AttachmentRefusal, policy: AttachmentPolicy, copy: ModuleMessages, locale: string) {\n switch (refusal) {\n case 'type':\n return copy.attachRefusedType;\n case 'count':\n return copy.attachRefusedCount.replaceAll('{count}', String(policy.maxFiles ?? DEFAULT_MAX_ATTACHMENTS));\n case 'size': {\n const cap = fileSize(policy.maxBytes ?? DEFAULT_MAX_ATTACHMENT_BYTES, locale);\n return copy.attachRefusedSize.replaceAll('{size}', cap);\n }\n }\n}\n\nexport function useAttachments(policy: AttachmentPolicy | undefined) {\n const { moduleMessages, locale } = useAiConnect();\n const [items, dispatch] = useReducer(attachmentList, []);\n // The list as last rendered, for a file read after other files changed it.\n const latest = useRef<AttachmentItem[]>(items);\n useEffect(() => {\n latest.current = items;\n }, [items]);\n const [note, setNote] = useState<string | null>(null);\n\n const read = useCallback(async (file: File, item: AttachmentItem, mediaType: string, rules: AttachmentPolicy) => {\n try {\n const prepared = await prepareAttachment(file, item.kind, mediaType);\n if (!fitsQuestion(prepared.bytes, heldBytes(latest.current, item.id), rules)) {\n dispatch({ type: 'remove', id: item.id });\n setNote(refusalText('size', rules, moduleMessages, locale));\n return;\n }\n dispatch({ type: 'ready', id: item.id, prepared });\n } catch (error) {\n dispatch({ type: 'remove', id: item.id });\n setNote(error instanceof UnreadableFileError\n ? moduleMessages.attachUnreadable.replaceAll('{name}', file.name)\n : moduleMessages.attachRefusedType);\n }\n }, [moduleMessages, locale]);\n\n const add = useCallback((files: File[]) => {\n if (!policy) {\n return;\n }\n setNote(null);\n let count = latest.current.length;\n for (const file of files) {\n const check = checkAttachment(file, policy, count);\n if (!check.ok) {\n setNote(refusalText(check.refusal, policy, moduleMessages, locale));\n continue;\n }\n count += 1;\n const item = { id: crypto.randomUUID(), name: file.name, size: file.size, kind: check.kind, prepared: null };\n dispatch({ type: 'add', item });\n void read(file, item, check.mediaType, policy);\n }\n }, [policy, moduleMessages, locale, read]);\n\n const remove = useCallback((id: string) => {\n setNote(null);\n dispatch({ type: 'remove', id });\n }, []);\n\n const clear = useCallback(() => {\n setNote(null);\n dispatch({ type: 'clear' });\n }, []);\n\n return {\n policy, items, note, add, remove, clear, ready: readyAttachments(items), reading: stillReading(items),\n };\n}\n\nexport type Attachments = ReturnType<typeof useAttachments>;\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useAiConnect } from "../../../AiConnectProvider.js";
|
|
3
|
+
import { CheckIcon, CloseIcon } from "../icons.js";
|
|
4
|
+
import { WAVE_BARS } from "./useDictation.js";
|
|
5
|
+
// A level of 0.25 or more fills the bar; speech sits well under full scale.
|
|
6
|
+
const FULL_LEVEL = 0.25;
|
|
7
|
+
function clock(seconds) {
|
|
8
|
+
return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;
|
|
9
|
+
}
|
|
10
|
+
function Waveform({ levels }) {
|
|
11
|
+
const bars = [...Array.from({ length: WAVE_BARS - levels.length }, () => 0), ...levels];
|
|
12
|
+
return (_jsx("span", { className: "aic-wave", "aria-hidden": "true", children: bars.map((level, index) => (_jsx("i", { style: { transform: `scaleY(${Math.max(0.08, Math.min(1, level / FULL_LEVEL))})` } }, index))) }));
|
|
13
|
+
}
|
|
14
|
+
export function DictationBar({ dictation }) {
|
|
15
|
+
const { moduleMessages } = useAiConnect();
|
|
16
|
+
const transcribing = dictation.status === 'transcribing';
|
|
17
|
+
const onKeyDown = (event) => {
|
|
18
|
+
if (event.key === 'Escape') {
|
|
19
|
+
event.stopPropagation();
|
|
20
|
+
event.nativeEvent.stopImmediatePropagation();
|
|
21
|
+
dictation.cancel();
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
return (_jsxs("div", { className: "aic-dictation", "data-status": dictation.status, onKeyDown: onKeyDown, children: [_jsxs("div", { className: "aic-dictation-live", role: "status", children: [_jsx("span", { className: "aic-visually-hidden", children: transcribing ? moduleMessages.transcribing : moduleMessages.dictating }), dictation.partial ? _jsx("p", { className: "aic-dictation-words", dir: "auto", children: dictation.partial }) : null, _jsxs("span", { className: "aic-dictation-meter", children: [_jsx(Waveform, { levels: dictation.levels }), _jsx("span", { className: "aic-dictation-time", "aria-hidden": "true", children: transcribing ? moduleMessages.transcribing : clock(dictation.seconds) })] })] }), _jsx("button", { type: "button", className: "aic-tool-button", "aria-label": moduleMessages.dictationCancel, title: moduleMessages.dictationCancel, onClick: dictation.cancel, children: _jsx(CloseIcon, {}) }), _jsx("button", { type: "button", className: "aic-send", "aria-label": moduleMessages.dictationStop, title: moduleMessages.dictationStop, disabled: transcribing, onClick: () => void dictation.stop(), autoFocus: true, children: _jsx(CheckIcon, {}) })] }));
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=DictationBar.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DictationBar.js","sourceRoot":"","sources":["../../../../src/modules/parts/dictation/DictationBar.tsx"],"names":[],"mappings":";AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,+BAAgC,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,aAAc,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AA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CAAC;IACH,CAAC,CAAC;IACF,OAAO,CACL,eAAK,SAAS,EAAC,eAAe,iBAAc,SAAS,CAAC,MAAM,EAAE,SAAS,EAAE,SAAS,aAChF,eAAK,SAAS,EAAC,oBAAoB,EAAC,IAAI,EAAC,QAAQ,aAC/C,eAAM,SAAS,EAAC,qBAAqB,YAClC,YAAY,CAAC,CAAC,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,cAAc,CAAC,SAAS,GACjE,EACN,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,YAAG,SAAS,EAAC,qBAAqB,EAAC,GAAG,EAAC,MAAM,YAAE,SAAS,CAAC,OAAO,GAAK,CAAC,CAAC,CAAC,IAAI,EACjG,gBAAM,SAAS,EAAC,qBAAqB,aACnC,KAAC,QAAQ,IAAC,MAAM,EAAE,SAAS,CAAC,MAAM,GAAI,EACtC,eAAM,SAAS,EAAC,oBAAoB,iBAAa,MAAM,YACpD,YAAY,CAAC,CAAC,CAAC,cAAc,CAAC,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,GACjE,IACF,IACH,EACN,iBAAQ,IAAI,EAAC,QAAQ,EAAC,SAAS,EAAC,iBAAiB,gBAAa,cAAc,CAAC,eAAe,EAC1F,KAAK,EAAE,cAAc,CAAC,eAAe,EAAE,OAAO,EAAE,SAAS,CAAC,MAAM,YAChE,KAAC,SAAS,KAAG,GACN,EACT,iBAAQ,IAAI,EAAC,QAAQ,EAAC,SAAS,EAAC,UAAU,gBAAa,cAAc,CAAC,aAAa,EACjF,KAAK,EAAE,cAAc,CAAC,aAAa,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,KAAK,SAAS,CAAC,IAAI,EAAE,EAAE,SAAS,kBAC5G,KAAC,SAAS,KAAG,GACN,IACL,CACP,CAAC;AACJ,CAAC","sourcesContent":["// The question field while the visitor dictates: a live waveform drawn from the microphone's level, the\n// time spoken, the words as the transcription writes them, Cancel, and Done, which puts the transcript in\n// the field. Escape cancels without closing the module.\n\nimport type { KeyboardEvent } from 'react';\nimport { useAiConnect } from '../../../AiConnectProvider.tsx';\nimport { CheckIcon, CloseIcon } from '../icons.tsx';\nimport { WAVE_BARS } from './useDictation.ts';\nimport type { DictationState } from './useDictation.ts';\n\n// A level of 0.25 or more fills the bar; speech sits well under full scale.\nconst FULL_LEVEL = 0.25;\n\nfunction clock(seconds: number): string {\n return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`;\n}\n\nfunction Waveform({ levels }: { levels: number[] }) {\n const bars = [...Array.from({ length: WAVE_BARS - levels.length }, () => 0), ...levels];\n return (\n <span className=\"aic-wave\" aria-hidden=\"true\">\n {bars.map((level, index) => (\n <i key={index} style={{ transform: `scaleY(${Math.max(0.08, Math.min(1, level / FULL_LEVEL))})` }} />\n ))}\n </span>\n );\n}\n\nexport function DictationBar({ dictation }: { dictation: DictationState }) {\n const { moduleMessages } = useAiConnect();\n const transcribing = dictation.status === 'transcribing';\n const onKeyDown = (event: KeyboardEvent) => {\n if (event.key === 'Escape') {\n event.stopPropagation();\n event.nativeEvent.stopImmediatePropagation();\n dictation.cancel();\n }\n };\n return (\n <div className=\"aic-dictation\" data-status={dictation.status} onKeyDown={onKeyDown}>\n <div className=\"aic-dictation-live\" role=\"status\">\n <span className=\"aic-visually-hidden\">\n {transcribing ? moduleMessages.transcribing : moduleMessages.dictating}\n </span>\n {dictation.partial ? <p className=\"aic-dictation-words\" dir=\"auto\">{dictation.partial}</p> : null}\n <span className=\"aic-dictation-meter\">\n <Waveform levels={dictation.levels} />\n <span className=\"aic-dictation-time\" aria-hidden=\"true\">\n {transcribing ? moduleMessages.transcribing : clock(dictation.seconds)}\n </span>\n </span>\n </div>\n <button type=\"button\" className=\"aic-tool-button\" aria-label={moduleMessages.dictationCancel}\n title={moduleMessages.dictationCancel} onClick={dictation.cancel}>\n <CloseIcon />\n </button>\n <button type=\"button\" className=\"aic-send\" aria-label={moduleMessages.dictationStop}\n title={moduleMessages.dictationStop} disabled={transcribing} onClick={() => void dictation.stop()} autoFocus>\n <CheckIcon />\n </button>\n </div>\n );\n}\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Dictation } from '@tensorpass/aiconnect';
|
|
2
|
+
export declare function useVoiceAvailable(dictation: Dictation | null | undefined, voice: boolean | undefined): boolean;
|
|
3
|
+
export declare function MicButton({ onClick }: {
|
|
4
|
+
onClick: () => void;
|
|
5
|
+
}): import("react").JSX.Element;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
// The microphone button that starts dictation, and whether the visitor can dictate here: a microphone, and
|
|
3
|
+
// the account's transcription or the browser's recognition. The check runs in the browser only, so a server
|
|
4
|
+
// render and its hydration agree.
|
|
5
|
+
import { useSyncExternalStore } from 'react';
|
|
6
|
+
import { useAiConnect } from "../../../AiConnectProvider.js";
|
|
7
|
+
import { MicIcon } from "../icons.js";
|
|
8
|
+
import { useActionAttribute } from "../moduleAnalytics.js";
|
|
9
|
+
import { dictationAvailable } from "./useDictation.js";
|
|
10
|
+
const noSubscription = () => () => undefined;
|
|
11
|
+
export function useVoiceAvailable(dictation, voice) {
|
|
12
|
+
const available = () => Boolean(voice) && dictationAvailable(dictation ?? null);
|
|
13
|
+
return useSyncExternalStore(noSubscription, available, () => false);
|
|
14
|
+
}
|
|
15
|
+
export function MicButton({ onClick }) {
|
|
16
|
+
const { moduleMessages } = useAiConnect();
|
|
17
|
+
const tracking = useActionAttribute('voice');
|
|
18
|
+
return (_jsx("button", { type: "button", className: "aic-tool-button", "aria-label": moduleMessages.dictate, title: moduleMessages.dictate, onClick: onClick, ...tracking, children: _jsx(MicIcon, {}) }));
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=MicButton.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MicButton.js","sourceRoot":"","sources":["../../../../src/modules/parts/dictation/MicButton.tsx"],"names":[],"mappings":";AAAA,2GAA2G;AAC3G,4GAA4G;AAC5G,kCAAkC;AAElC,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAE7C,OAAO,EAAE,YAAY,EAAE,MAAM,+BAAgC,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,aAAc,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,MAAM,cAAc,GAAG,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC;AAE7C,MAAM,UAAU,iBAAiB,CAAC,SAAuC,EAAE,KAA0B;IACnG,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,kBAAkB,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC;IAChF,OAAO,oBAAoB,CAAC,cAAc,EAAE,SAAS,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC;AACtE,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,EAAE,OAAO,EAA2B;IAC5D,MAAM,EAAE,cAAc,EAAE,GAAG,YAAY,EAAE,CAAC;IAC1C,MAAM,QAAQ,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAC7C,OAAO,CACL,iBAAQ,IAAI,EAAC,QAAQ,EAAC,SAAS,EAAC,iBAAiB,gBAAa,cAAc,CAAC,OAAO,EAAE,KAAK,EAAE,cAAc,CAAC,OAAO,EACjH,OAAO,EAAE,OAAO,KAAM,QAAQ,YAC9B,KAAC,OAAO,KAAG,GACJ,CACV,CAAC;AACJ,CAAC","sourcesContent":["// The microphone button that starts dictation, and whether the visitor can dictate here: a microphone, and\n// the account's transcription or the browser's recognition. The check runs in the browser only, so a server\n// render and its hydration agree.\n\nimport { useSyncExternalStore } from 'react';\nimport type { Dictation } from '@tensorpass/aiconnect';\nimport { useAiConnect } from '../../../AiConnectProvider.tsx';\nimport { MicIcon } from '../icons.tsx';\nimport { useActionAttribute } from '../moduleAnalytics.ts';\nimport { dictationAvailable } from './useDictation.ts';\n\nconst noSubscription = () => () => undefined;\n\nexport function useVoiceAvailable(dictation: Dictation | null | undefined, voice: boolean | undefined): boolean {\n const available = () => Boolean(voice) && dictationAvailable(dictation ?? null);\n return useSyncExternalStore(noSubscription, available, () => false);\n}\n\nexport function MicButton({ onClick }: { onClick: () => void }) {\n const { moduleMessages } = useAiConnect();\n const tracking = useActionAttribute('voice');\n return (\n <button type=\"button\" className=\"aic-tool-button\" aria-label={moduleMessages.dictate} title={moduleMessages.dictate}\n onClick={onClick} {...tracking}>\n <MicIcon />\n </button>\n );\n}\n"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// The microphone while the visitor dictates: the browser asks the visitor for it on the site's behalf, and the
|
|
2
|
+
// PCM worklet hands every 100 ms frame and its level to the dictation. Stopping releases the microphone, so
|
|
3
|
+
// the browser's recording light goes off; stopping again does nothing.
|
|
4
|
+
import { PCM_WORKLET_URL } from "./pcmWorkletUrl.js";
|
|
5
|
+
const MIC_CONSTRAINTS = {
|
|
6
|
+
channelCount: 1,
|
|
7
|
+
echoCancellation: true,
|
|
8
|
+
noiseSuppression: true,
|
|
9
|
+
autoGainControl: true,
|
|
10
|
+
};
|
|
11
|
+
export async function startCapture({ onFrame }) {
|
|
12
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: MIC_CONSTRAINTS });
|
|
13
|
+
const context = new AudioContext();
|
|
14
|
+
const release = () => {
|
|
15
|
+
stream.getTracks().forEach((track) => track.stop());
|
|
16
|
+
void context.close();
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
await context.audioWorklet.addModule(PCM_WORKLET_URL);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
release();
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
const source = context.createMediaStreamSource(stream);
|
|
26
|
+
const node = new AudioWorkletNode(context, 'aiconnect-pcm');
|
|
27
|
+
node.port.onmessage = ({ data }) => onFrame(data.pcm, data.level);
|
|
28
|
+
// The worklet writes no output, so the destination plays silence; the connection keeps the browser pulling it.
|
|
29
|
+
source.connect(node).connect(context.destination);
|
|
30
|
+
let stopped = false;
|
|
31
|
+
return {
|
|
32
|
+
stop: () => {
|
|
33
|
+
if (stopped) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
stopped = true;
|
|
37
|
+
node.port.onmessage = null;
|
|
38
|
+
source.disconnect();
|
|
39
|
+
node.disconnect();
|
|
40
|
+
release();
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=audioCapture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audioCapture.js","sourceRoot":"","sources":["../../../../src/modules/parts/dictation/audioCapture.ts"],"names":[],"mappings":"AAAA,+GAA+G;AAC/G,4GAA4G;AAC5G,uEAAuE;AAEvE,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAUrD,MAAM,eAAe,GAA0B;IAC7C,YAAY,EAAE,CAAC;IACf,gBAAgB,EAAE,IAAI;IACtB,gBAAgB,EAAE,IAAI;IACtB,eAAe,EAAE,IAAI;CACtB,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,EAAE,OAAO,EAAgB;IAC1D,MAAM,MAAM,GAAG,MAAM,SAAS,CAAC,YAAY,CAAC,YAAY,CAAC,EAAE,KAAK,EAAE,eAAe,EAAE,CAAC,CAAC;IACrF,MAAM,OAAO,GAAG,IAAI,YAAY,EAAE,CAAC;IACnC,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,MAAM,CAAC,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;QACpD,KAAK,OAAO,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC,CAAC;IACF,IAAI,CAAC;QACH,MAAM,OAAO,CAAC,YAAY,CAAC,SAAS,CAAC,eAAe,CAAC,CAAC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,CAAC;QACV,MAAM,KAAK,CAAC;IACd,CAAC;IACD,MAAM,MAAM,GAAG,OAAO,CAAC,uBAAuB,CAAC,MAAM,CAAC,CAAC;IACvD,MAAM,IAAI,GAAG,IAAI,gBAAgB,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;IAC5D,IAAI,CAAC,IAAI,CAAC,SAAS,GAAG,CAAC,EAAE,IAAI,EAAoD,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IACpH,+GAA+G;IAC/G,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IAClD,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,OAAO;QACL,IAAI,EAAE,GAAG,EAAE;YACT,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YACD,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,CAAC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YAC3B,MAAM,CAAC,UAAU,EAAE,CAAC;YACpB,IAAI,CAAC,UAAU,EAAE,CAAC;YAClB,OAAO,EAAE,CAAC;QACZ,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// The microphone while the visitor dictates: the browser asks the visitor for it on the site's behalf, and the\n// PCM worklet hands every 100 ms frame and its level to the dictation. Stopping releases the microphone, so\n// the browser's recording light goes off; stopping again does nothing.\n\nimport { PCM_WORKLET_URL } from './pcmWorkletUrl.ts';\n\nexport interface CaptureHooks {\n onFrame(pcm: Int16Array, level: number): void;\n}\n\nexport interface AudioCapture {\n stop(): void;\n}\n\nconst MIC_CONSTRAINTS: MediaTrackConstraints = {\n channelCount: 1,\n echoCancellation: true,\n noiseSuppression: true,\n autoGainControl: true,\n};\n\nexport async function startCapture({ onFrame }: CaptureHooks): Promise<AudioCapture> {\n const stream = await navigator.mediaDevices.getUserMedia({ audio: MIC_CONSTRAINTS });\n const context = new AudioContext();\n const release = () => {\n stream.getTracks().forEach((track) => track.stop());\n void context.close();\n };\n try {\n await context.audioWorklet.addModule(PCM_WORKLET_URL);\n } catch (error) {\n release();\n throw error;\n }\n const source = context.createMediaStreamSource(stream);\n const node = new AudioWorkletNode(context, 'aiconnect-pcm');\n node.port.onmessage = ({ data }: MessageEvent<{ pcm: Int16Array; level: number }>) => onFrame(data.pcm, data.level);\n // The worklet writes no output, so the destination plays silence; the connection keeps the browser pulling it.\n source.connect(node).connect(context.destination);\n let stopped = false;\n return {\n stop: () => {\n if (stopped) {\n return;\n }\n stopped = true;\n node.port.onmessage = null;\n source.disconnect();\n node.disconnect();\n release();\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
interface RecognitionResultList {
|
|
2
|
+
length: number;
|
|
3
|
+
[index: number]: {
|
|
4
|
+
isFinal: boolean;
|
|
5
|
+
0: {
|
|
6
|
+
transcript: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
interface Recognition {
|
|
11
|
+
lang: string;
|
|
12
|
+
continuous: boolean;
|
|
13
|
+
interimResults: boolean;
|
|
14
|
+
onresult: ((event: {
|
|
15
|
+
results: RecognitionResultList;
|
|
16
|
+
}) => void) | null;
|
|
17
|
+
onerror: ((event: {
|
|
18
|
+
error: string;
|
|
19
|
+
}) => void) | null;
|
|
20
|
+
onend: (() => void) | null;
|
|
21
|
+
start(): void;
|
|
22
|
+
stop(): void;
|
|
23
|
+
abort(): void;
|
|
24
|
+
}
|
|
25
|
+
type RecognitionClass = new () => Recognition;
|
|
26
|
+
export declare function recognitionClass(): RecognitionClass | null;
|
|
27
|
+
export interface SpeechHooks {
|
|
28
|
+
onText(text: string): void;
|
|
29
|
+
onError(code: string): void;
|
|
30
|
+
onEnd(): void;
|
|
31
|
+
}
|
|
32
|
+
export interface BrowserSpeech {
|
|
33
|
+
stop(): void;
|
|
34
|
+
cancel(): void;
|
|
35
|
+
}
|
|
36
|
+
export declare function startBrowserSpeech(language: string, hooks: SpeechHooks): BrowserSpeech;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The browser's own speech recognition, for an account without a transcription service: Chrome, Edge and
|
|
2
|
+
// Safari offer it, and Firefox does not yet. It writes the words while the visitor speaks; the whole
|
|
3
|
+
// transcript comes with each result. Chrome sends the voice to Google's recognition service. Its on-device
|
|
4
|
+
// recognition is left aside: asking whether a language is installed (`SpeechRecognition.available`)
|
|
5
|
+
// crashed Chromium's page on 23 November 2026.
|
|
6
|
+
export function recognitionClass() {
|
|
7
|
+
const scope = globalThis;
|
|
8
|
+
return scope.SpeechRecognition ?? scope.webkitSpeechRecognition ?? null;
|
|
9
|
+
}
|
|
10
|
+
function transcript(results) {
|
|
11
|
+
let text = '';
|
|
12
|
+
for (let index = 0; index < results.length; index += 1) {
|
|
13
|
+
text += results[index][0].transcript;
|
|
14
|
+
}
|
|
15
|
+
return text.trim();
|
|
16
|
+
}
|
|
17
|
+
export function startBrowserSpeech(language, hooks) {
|
|
18
|
+
const Recognition = recognitionClass();
|
|
19
|
+
if (!Recognition) {
|
|
20
|
+
throw new Error('The browser has no speech recognition');
|
|
21
|
+
}
|
|
22
|
+
const recognition = new Recognition();
|
|
23
|
+
recognition.lang = language;
|
|
24
|
+
recognition.continuous = true;
|
|
25
|
+
recognition.interimResults = true;
|
|
26
|
+
recognition.onresult = (event) => hooks.onText(transcript(event.results));
|
|
27
|
+
recognition.onerror = (event) => hooks.onError(event.error);
|
|
28
|
+
recognition.onend = () => hooks.onEnd();
|
|
29
|
+
recognition.start();
|
|
30
|
+
return { stop: () => recognition.stop(), cancel: () => recognition.abort() };
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=browserSpeech.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browserSpeech.js","sourceRoot":"","sources":["../../../../src/modules/parts/dictation/browserSpeech.ts"],"names":[],"mappings":"AAAA,yGAAyG;AACzG,qGAAqG;AACrG,2GAA2G;AAC3G,oGAAoG;AACpG,+CAA+C;AAqB/C,MAAM,UAAU,gBAAgB;IAC9B,MAAM,KAAK,GAAG,UAAkG,CAAC;IACjH,OAAO,KAAK,CAAC,iBAAiB,IAAI,KAAK,CAAC,uBAAuB,IAAI,IAAI,CAAC;AAC1E,CAAC;AAcD,SAAS,UAAU,CAAC,OAA8B;IAChD,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACvD,IAAI,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC;IACvC,CAAC;IACD,OAAO,IAAI,CAAC,IAAI,EAAE,CAAC;AACrB,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,QAAgB,EAAE,KAAkB;IACrE,MAAM,WAAW,GAAG,gBAAgB,EAAE,CAAC;IACvC,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IACD,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;IACtC,WAAW,CAAC,IAAI,GAAG,QAAQ,CAAC;IAC5B,WAAW,CAAC,UAAU,GAAG,IAAI,CAAC;IAC9B,WAAW,CAAC,cAAc,GAAG,IAAI,CAAC;IAClC,WAAW,CAAC,QAAQ,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IAC1E,WAAW,CAAC,OAAO,GAAG,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC5D,WAAW,CAAC,KAAK,GAAG,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACxC,WAAW,CAAC,KAAK,EAAE,CAAC;IACpB,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,EAAE,CAAC;AAC/E,CAAC","sourcesContent":["// The browser's own speech recognition, for an account without a transcription service: Chrome, Edge and\n// Safari offer it, and Firefox does not yet. It writes the words while the visitor speaks; the whole\n// transcript comes with each result. Chrome sends the voice to Google's recognition service. Its on-device\n// recognition is left aside: asking whether a language is installed (`SpeechRecognition.available`)\n// crashed Chromium's page on 23 November 2026.\n\ninterface RecognitionResultList {\n length: number;\n [index: number]: { isFinal: boolean; 0: { transcript: string } };\n}\n\ninterface Recognition {\n lang: string;\n continuous: boolean;\n interimResults: boolean;\n onresult: ((event: { results: RecognitionResultList }) => void) | null;\n onerror: ((event: { error: string }) => void) | null;\n onend: (() => void) | null;\n start(): void;\n stop(): void;\n abort(): void;\n}\n\ntype RecognitionClass = new () => Recognition;\n\nexport function recognitionClass(): RecognitionClass | null {\n const scope = globalThis as { SpeechRecognition?: RecognitionClass; webkitSpeechRecognition?: RecognitionClass };\n return scope.SpeechRecognition ?? scope.webkitSpeechRecognition ?? null;\n}\n\nexport interface SpeechHooks {\n onText(text: string): void;\n onError(code: string): void;\n onEnd(): void;\n}\n\nexport interface BrowserSpeech {\n // Ends the listening; `onEnd` follows once the last words are written.\n stop(): void;\n cancel(): void;\n}\n\nfunction transcript(results: RecognitionResultList): string {\n let text = '';\n for (let index = 0; index < results.length; index += 1) {\n text += results[index][0].transcript;\n }\n return text.trim();\n}\n\nexport function startBrowserSpeech(language: string, hooks: SpeechHooks): BrowserSpeech {\n const Recognition = recognitionClass();\n if (!Recognition) {\n throw new Error('The browser has no speech recognition');\n }\n const recognition = new Recognition();\n recognition.lang = language;\n recognition.continuous = true;\n recognition.interimResults = true;\n recognition.onresult = (event) => hooks.onText(transcript(event.results));\n recognition.onerror = (event) => hooks.onError(event.error);\n recognition.onend = () => hooks.onEnd();\n recognition.start();\n return { stop: () => recognition.stop(), cancel: () => recognition.abort() };\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The language the visitor dictates in: the module's language, with the region of the first of the
|
|
2
|
+
// visitor's browser languages that shares it, such as `he-IL` or `ar-MA`, so the recognition hears the
|
|
3
|
+
// visitor's accent. Without one, the language's most spoken region.
|
|
4
|
+
const DEFAULT_REGIONS = {
|
|
5
|
+
en: 'en-US',
|
|
6
|
+
fr: 'fr-FR',
|
|
7
|
+
he: 'he-IL',
|
|
8
|
+
ar: 'ar-EG',
|
|
9
|
+
zh: 'zh-CN',
|
|
10
|
+
};
|
|
11
|
+
export function dictationLanguage(locale, browserLanguages) {
|
|
12
|
+
const match = browserLanguages.find((tag) => tag.toLowerCase().split('-')[0] === locale && tag.includes('-'));
|
|
13
|
+
return match ?? DEFAULT_REGIONS[locale];
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=dictationLanguage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dictationLanguage.js","sourceRoot":"","sources":["../../../../src/modules/parts/dictation/dictationLanguage.ts"],"names":[],"mappings":"AAAA,mGAAmG;AACnG,uGAAuG;AACvG,oEAAoE;AAIpE,MAAM,eAAe,GAA2B;IAC9C,EAAE,EAAE,OAAO;IACX,EAAE,EAAE,OAAO;IACX,EAAE,EAAE,OAAO;IACX,EAAE,EAAE,OAAO;IACX,EAAE,EAAE,OAAO;CACZ,CAAC;AAEF,MAAM,UAAU,iBAAiB,CAAC,MAAc,EAAE,gBAAmC;IACnF,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,MAAM,IAAI,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IAC9G,OAAO,KAAK,IAAI,eAAe,CAAC,MAAM,CAAC,CAAC;AAC1C,CAAC","sourcesContent":["// The language the visitor dictates in: the module's language, with the region of the first of the\n// visitor's browser languages that shares it, such as `he-IL` or `ar-MA`, so the recognition hears the\n// visitor's accent. Without one, the language's most spoken region.\n\nimport type { Locale } from '../../../pickerMessages.ts';\n\nconst DEFAULT_REGIONS: Record<Locale, string> = {\n en: 'en-US',\n fr: 'fr-FR',\n he: 'he-IL',\n ar: 'ar-EG',\n zh: 'zh-CN',\n};\n\nexport function dictationLanguage(locale: Locale, browserLanguages: readonly string[]): string {\n const match = browserLanguages.find((tag) => tag.toLowerCase().split('-')[0] === locale && tag.includes('-'));\n return match ?? DEFAULT_REGIONS[locale];\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// The audio worklet of dictation: it turns the microphone's samples into 16 kHz mono 16-bit PCM, averaging
|
|
2
|
+
// the samples of each 16 kHz step, and posts a frame every 100 ms with the frame's level for the waveform.
|
|
3
|
+
// It runs in the browser's audio thread, which has its own globals.
|
|
4
|
+
const TARGET_RATE = 16_000;
|
|
5
|
+
const FRAME_SAMPLES = 1_600;
|
|
6
|
+
class PcmProcessor extends AudioWorkletProcessor {
|
|
7
|
+
step = sampleRate / TARGET_RATE;
|
|
8
|
+
position = 0;
|
|
9
|
+
sum = 0;
|
|
10
|
+
count = 0;
|
|
11
|
+
frame = new Int16Array(FRAME_SAMPLES);
|
|
12
|
+
filled = 0;
|
|
13
|
+
energy = 0;
|
|
14
|
+
process(inputs) {
|
|
15
|
+
const channel = inputs[0]?.[0];
|
|
16
|
+
if (!channel) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
for (const sample of channel) {
|
|
20
|
+
this.sum += sample;
|
|
21
|
+
this.count += 1;
|
|
22
|
+
this.position += 1;
|
|
23
|
+
if (this.position >= this.step) {
|
|
24
|
+
this.position -= this.step;
|
|
25
|
+
this.push(this.sum / this.count);
|
|
26
|
+
this.sum = 0;
|
|
27
|
+
this.count = 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
push(value) {
|
|
33
|
+
const clamped = Math.max(-1, Math.min(1, value));
|
|
34
|
+
this.frame[this.filled] = clamped < 0 ? clamped * 0x8000 : clamped * 0x7fff;
|
|
35
|
+
this.energy += clamped * clamped;
|
|
36
|
+
this.filled += 1;
|
|
37
|
+
if (this.filled === FRAME_SAMPLES) {
|
|
38
|
+
const level = Math.sqrt(this.energy / FRAME_SAMPLES);
|
|
39
|
+
this.port.postMessage({ pcm: this.frame, level }, [this.frame.buffer]);
|
|
40
|
+
this.frame = new Int16Array(FRAME_SAMPLES);
|
|
41
|
+
this.filled = 0;
|
|
42
|
+
this.energy = 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
registerProcessor('aiconnect-pcm', PcmProcessor);
|
|
47
|
+
export {};
|
|
48
|
+
//# sourceMappingURL=pcmWorklet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pcmWorklet.js","sourceRoot":"","sources":["../../../../src/modules/parts/dictation/pcmWorklet.ts"],"names":[],"mappings":"AAAA,2GAA2G;AAC3G,2GAA2G;AAC3G,oEAAoE;AAQpE,MAAM,WAAW,GAAG,MAAM,CAAC;AAC3B,MAAM,aAAa,GAAG,KAAK,CAAC;AAE5B,MAAM,YAAa,SAAQ,qBAAqB;IAC7B,IAAI,GAAG,UAAU,GAAG,WAAW,CAAC;IACzC,QAAQ,GAAG,CAAC,CAAC;IACb,GAAG,GAAG,CAAC,CA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AAM,IAAI,CAAC,CAAC;QACjB,IAAI,IAAI,CAAC,MAAM,KAAK,aAAa,EAAE,CAAC;YAClC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,CAAC;YACrD,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;YACvE,IAAI,CAAC,KAAK,GAAG,IAAI,UAAU,CAAC,aAAa,CAAC,CAAC;YAC3C,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;YAChB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;QAClB,CAAC;IACH,CAAC;CACF;AAED,iBAAiB,CAAC,eAAe,EAAE,YAAY,CAAC,CAAC","sourcesContent":["// The audio worklet of dictation: it turns the microphone's samples into 16 kHz mono 16-bit PCM, averaging\n// the samples of each 16 kHz step, and posts a frame every 100 ms with the frame's level for the waveform.\n// It runs in the browser's audio thread, which has its own globals.\n\ndeclare const sampleRate: number;\ndeclare function registerProcessor(name: string, processor: unknown): void;\ndeclare class AudioWorkletProcessor {\n readonly port: MessagePort;\n}\n\nconst TARGET_RATE = 16_000;\nconst FRAME_SAMPLES = 1_600;\n\nclass PcmProcessor extends AudioWorkletProcessor {\n private readonly step = sampleRate / TARGET_RATE;\n private position = 0;\n private sum = 0;\n private count = 0;\n private frame = new Int16Array(FRAME_SAMPLES);\n private filled = 0;\n private energy = 0;\n\n process(inputs: Float32Array[][]): boolean {\n const channel = inputs[0]?.[0];\n if (!channel) {\n return true;\n }\n for (const sample of channel) {\n this.sum += sample;\n this.count += 1;\n this.position += 1;\n if (this.position >= this.step) {\n this.position -= this.step;\n this.push(this.sum / this.count);\n this.sum = 0;\n this.count = 0;\n }\n }\n return true;\n }\n\n private push(value: number): void {\n const clamped = Math.max(-1, Math.min(1, value));\n this.frame[this.filled] = clamped < 0 ? clamped * 0x8000 : clamped * 0x7fff;\n this.energy += clamped * clamped;\n this.filled += 1;\n if (this.filled === FRAME_SAMPLES) {\n const level = Math.sqrt(this.energy / FRAME_SAMPLES);\n this.port.postMessage({ pcm: this.frame, level }, [this.frame.buffer]);\n this.frame = new Int16Array(FRAME_SAMPLES);\n this.filled = 0;\n this.energy = 0;\n }\n }\n}\n\nregisterProcessor('aiconnect-pcm', PcmProcessor);\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PCM_WORKLET_URL: string;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The address of the PCM worklet, the file beside this module. Bundlers that follow
|
|
2
|
+
// `new URL('…', import.meta.url)` copy it with the site's code. The Web Components build puts this module's
|
|
3
|
+
// place to the worklet's copy beside its own script.
|
|
4
|
+
export const PCM_WORKLET_URL = new URL('./pcmWorklet.js', import.meta.url).href;
|
|
5
|
+
//# sourceMappingURL=pcmWorkletUrl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pcmWorkletUrl.js","sourceRoot":"","sources":["../../../../src/modules/parts/dictation/pcmWorkletUrl.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,4GAA4G;AAC5G,qDAAqD;AAErD,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,iBAAiB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC","sourcesContent":["// The address of the PCM worklet, the file beside this module. Bundlers that follow\n// `new URL('…', import.meta.url)` copy it with the site's code. The Web Components build puts this module's\n// place to the worklet's copy beside its own script.\n\nexport const PCM_WORKLET_URL = new URL('./pcmWorklet.js', import.meta.url).href;\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DictationSession } from '@tensorpass/aiconnect';
|
|
2
|
+
import type { AudioCapture } from './audioCapture.ts';
|
|
3
|
+
import type { BrowserSpeech } from './browserSpeech.ts';
|
|
4
|
+
export interface Recording {
|
|
5
|
+
capture: AudioCapture | null;
|
|
6
|
+
session: DictationSession | null;
|
|
7
|
+
speech: BrowserSpeech | null;
|
|
8
|
+
controller: AbortController;
|
|
9
|
+
frames: number;
|
|
10
|
+
silent: number;
|
|
11
|
+
stopping: boolean;
|
|
12
|
+
ended: Promise<void>;
|
|
13
|
+
markEnded: () => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function countFrame(recording: Recording, level: number): boolean;
|
|
16
|
+
export declare function spokenSeconds(recording: Recording): number;
|
|
17
|
+
export declare function newRecording(): Recording;
|
|
18
|
+
export declare function handOver(recording: Recording): void;
|
|
19
|
+
export declare function releaseRecording(recording: Recording | null): void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// One recording while the visitor dictates: the microphone, the account's transcription or the browser's
|
|
2
|
+
// recognition, the frames counted for the timer and the silence, and the moment the recognition wrote its
|
|
3
|
+
// last words. Releasing it stops all of them, the microphone first.
|
|
4
|
+
const FRAMES_PER_SECOND = 10;
|
|
5
|
+
const MAX_FRAMES = 120 * FRAMES_PER_SECOND;
|
|
6
|
+
const SILENT_FRAMES = 15 * FRAMES_PER_SECOND;
|
|
7
|
+
const SILENCE_LEVEL = 0.01;
|
|
8
|
+
// Counts a 100 ms frame for the timer and the silence; true once the recording ends on its own, after 2
|
|
9
|
+
// minutes or 15 seconds of silence.
|
|
10
|
+
export function countFrame(recording, level) {
|
|
11
|
+
recording.frames += 1;
|
|
12
|
+
recording.silent = level < SILENCE_LEVEL ? recording.silent + 1 : 0;
|
|
13
|
+
return recording.frames >= MAX_FRAMES || recording.silent >= SILENT_FRAMES;
|
|
14
|
+
}
|
|
15
|
+
export function spokenSeconds(recording) {
|
|
16
|
+
return Math.floor(recording.frames / FRAMES_PER_SECOND);
|
|
17
|
+
}
|
|
18
|
+
function endOf() {
|
|
19
|
+
let markEnded = () => undefined;
|
|
20
|
+
const ended = new Promise((resolve) => {
|
|
21
|
+
markEnded = resolve;
|
|
22
|
+
});
|
|
23
|
+
return { ended, markEnded };
|
|
24
|
+
}
|
|
25
|
+
export function newRecording() {
|
|
26
|
+
return {
|
|
27
|
+
capture: null, session: null, speech: null, controller: new AbortController(), frames: 0, silent: 0,
|
|
28
|
+
stopping: false, ...endOf(),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// The recording goes on with the browser's recognition in place of the account's transcription, whose
|
|
32
|
+
// words end with the recognition's own end.
|
|
33
|
+
export function handOver(recording) {
|
|
34
|
+
recording.session = null;
|
|
35
|
+
Object.assign(recording, endOf());
|
|
36
|
+
}
|
|
37
|
+
export function releaseRecording(recording) {
|
|
38
|
+
if (!recording) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
recording.capture?.stop();
|
|
42
|
+
recording.controller.abort();
|
|
43
|
+
recording.speech?.cancel();
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=recording.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recording.js","sourceRoot":"","sources":["../../../../src/modules/parts/dictation/recording.ts"],"names":[],"mappings":"AAAA,yGAAyG;AACzG,0GAA0G;AAC1G,oEAAoE;AAoBpE,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAC7B,MAAM,UAAU,GAAG,GAAG,GAAG,iBAAiB,CAAC;AAC3C,MAAM,aAAa,GAAG,EAAE,GAAG,iBAAiB,CAAC;AAC7C,MAAM,aAAa,GAAG,IAAI,CAAC;AAE3B,wGAAwG;AACxG,oCAAoC;AACpC,MAAM,UAAU,UAAU,CAAC,SAAoB,EAAE,KAAa;IAC5D,SAAS,CAAC,MAAM,IAAI,CAAC,CAAC;IACtB,SAAS,CAAC,MAAM,GAAG,KAAK,GAAG,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACpE,OAAO,SAAS,CAAC,MAAM,IAAI,UAAU,IAAI,SAAS,CAAC,MAAM,IAAI,aAAa,CAAC;AAC7E,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,SAAoB;IAChD,OAAO,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,GAAG,iBAAiB,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,KAAK;IACZ,IAAI,SAAS,GAAe,GAAG,EAAE,CAAC,SAAS,CAAC;IAC5C,MAAM,KAAK,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QAC1C,SAAS,GAAG,OAAO,CAAC;IACtB,CAAC,CAAC,CAAC;IACH,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;AAC9B,CAAC;AAED,MAAM,UAAU,YAAY;IAC1B,OAAO;QACL,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,eAAe,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC;QACnG,QAAQ,EAAE,KAAK,EAAE,GAAG,KAAK,EAAE;KAC5B,CAAC;AACJ,CAAC;AAED,sGAAsG;AACtG,4CAA4C;AAC5C,MAAM,UAAU,QAAQ,CAAC,SAAoB;IAC3C,SAAS,CAAC,OAAO,GAAG,IAAI,CAAC;IACzB,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;AACpC,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,SAA2B;IAC1D,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO;IACT,CAAC;IACD,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC;IAC1B,SAAS,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;IAC7B,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;AAC7B,CAAC","sourcesContent":["// One recording while the visitor dictates: the microphone, the account's transcription or the browser's\n// recognition, the frames counted for the timer and the silence, and the moment the recognition wrote its\n// last words. Releasing it stops all of them, the microphone first.\n\nimport type { DictationSession } from '@tensorpass/aiconnect';\nimport type { AudioCapture } from './audioCapture.ts';\nimport type { BrowserSpeech } from './browserSpeech.ts';\n\nexport interface Recording {\n capture: AudioCapture | null;\n session: DictationSession | null;\n speech: BrowserSpeech | null;\n controller: AbortController;\n frames: number;\n silent: number;\n stopping: boolean;\n // Resolved once the transcription has its last words: at once for the account's, at the end of the\n // browser's recognition.\n ended: Promise<void>;\n markEnded: () => void;\n}\n\nconst FRAMES_PER_SECOND = 10;\nconst MAX_FRAMES = 120 * FRAMES_PER_SECOND;\nconst SILENT_FRAMES = 15 * FRAMES_PER_SECOND;\nconst SILENCE_LEVEL = 0.01;\n\n// Counts a 100 ms frame for the timer and the silence; true once the recording ends on its own, after 2\n// minutes or 15 seconds of silence.\nexport function countFrame(recording: Recording, level: number): boolean {\n recording.frames += 1;\n recording.silent = level < SILENCE_LEVEL ? recording.silent + 1 : 0;\n return recording.frames >= MAX_FRAMES || recording.silent >= SILENT_FRAMES;\n}\n\nexport function spokenSeconds(recording: Recording): number {\n return Math.floor(recording.frames / FRAMES_PER_SECOND);\n}\n\nfunction endOf(): Pick<Recording, 'ended' | 'markEnded'> {\n let markEnded: () => void = () => undefined;\n const ended = new Promise<void>((resolve) => {\n markEnded = resolve;\n });\n return { ended, markEnded };\n}\n\nexport function newRecording(): Recording {\n return {\n capture: null, session: null, speech: null, controller: new AbortController(), frames: 0, silent: 0,\n stopping: false, ...endOf(),\n };\n}\n\n// The recording goes on with the browser's recognition in place of the account's transcription, whose\n// words end with the recognition's own end.\nexport function handOver(recording: Recording): void {\n recording.session = null;\n Object.assign(recording, endOf());\n}\n\nexport function releaseRecording(recording: Recording | null): void {\n if (!recording) {\n return;\n }\n recording.capture?.stop();\n recording.controller.abort();\n recording.speech?.cancel();\n}\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Dictation } from '@tensorpass/aiconnect';
|
|
2
|
+
export type DictationStatus = 'idle' | 'listening' | 'transcribing';
|
|
3
|
+
export declare const WAVE_BARS = 40;
|
|
4
|
+
export interface DictationView {
|
|
5
|
+
status: DictationStatus;
|
|
6
|
+
partial: string;
|
|
7
|
+
levels: number[];
|
|
8
|
+
seconds: number;
|
|
9
|
+
}
|
|
10
|
+
export declare function dictationAvailable(dictation: Dictation | null): boolean;
|
|
11
|
+
export declare function useDictation(dictation: Dictation | null, onFinal: (text: string) => void): {
|
|
12
|
+
error: string | null;
|
|
13
|
+
start: () => Promise<void>;
|
|
14
|
+
stop: () => Promise<void>;
|
|
15
|
+
cancel: () => void;
|
|
16
|
+
dismissError: () => void;
|
|
17
|
+
status: DictationStatus;
|
|
18
|
+
partial: string;
|
|
19
|
+
levels: number[];
|
|
20
|
+
seconds: number;
|
|
21
|
+
};
|
|
22
|
+
export type DictationState = ReturnType<typeof useDictation>;
|