dsh-total-quality 2.0.55 → 2.0.57
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/CONTRIBUTING.md +51 -47
- package/LICENSE.md +37 -37
- package/README.en.md +32 -30
- package/README.md +90 -88
- package/agent-host.cjs +277 -277
- package/client.js +199 -92
- package/cordis.patch.yml +5 -5
- package/docs/enterprise-onboarding.md +10 -10
- package/docs/technical-reference.md +223 -223
- package/host.cjs +91 -124
- package/llm-bridge.cjs +234 -234
- package/locale/en.json +5 -5
- package/locale/zh.json +5 -5
- package/package.json +3 -2
- package/public/app.css +3 -0
- package/public/app.js +6 -2
- package/public/index.html +8 -9
- package/public/js/api.js +152 -152
- package/public/js/assistant-history.js +62 -62
- package/public/js/material-library.js +100 -100
- package/public/js/mobile-preview.js +119 -0
- package/public/js/pages-company-knowledge.js +9 -32
- package/public/js/pages-core.js +232 -232
- package/public/js/pages-growth.js +148 -148
- package/public/js/pages-qms.js +1 -1
- package/public/js/pages-qtools.js +410 -410
- package/public/js/pages-thanks.js +71 -71
- package/public/js/plugin-updates.js +53 -0
- package/public/js/quality-workflow.js +205 -205
- package/public/js/survey-dates.js +41 -41
- package/public/js/survey-knowledge.js +301 -301
- package/public/js/tool-wizards.js +621 -621
- package/public/js/ui.js +467 -467
- package/public/js/voice-capture-worklet.js +30 -30
- package/public/js/xiaozhi-voice.js +264 -264
- package/public/js/xiaozhi.js +468 -468
- package/public/platform-help.html +426 -426
- package/public/platform-site/assets/0468a68574112812.css +10703 -10703
- package/public/platform-site/assets/159e086d35adfa1c.css +2094 -2094
- package/public/platform-site/assets/169e3159cade0d9f.css +98 -98
- package/public/platform-site/assets/22b917c4a3d4ae3d.svg +14 -14
- package/public/platform-site/assets/476f9254ccf723e7.css +8265 -8265
- package/public/platform-site/assets/85a0a4fb4416ded9.css +120 -120
- package/public/platform-site/assets/a3b272383226f6d9.svg +135 -135
- package/public/platform-site/assets/fd16dc5f3cc9d97e.svg +104 -104
- package/public/platform-site/assets/qip-favicon.svg +5 -5
- package/public/platform-site/index.html +279 -279
- package/public/platform-site/offline.js +261 -261
- package/public/platform-site/qip-brand.css +566 -566
- package/public/platform-site/qip-module-guides.js +237 -237
- package/public/responsive.css +103 -101
- package/public/vendor/markdown-it-15.0.3.LICENSE +22 -22
- package/public/vendor/markdown-it-15.0.3.umd.min.js +13 -13
- package/public/workbench.css +7 -7
- package/public/xiaozhi.css +292 -292
- package/resources/qms-templates/019ffb58f24ed48d.docx +0 -0
- package/resources/qms-templates/04a69165fc15143a.docx +0 -0
- package/resources/qms-templates/09af69358c989a63.docx +0 -0
- package/resources/qms-templates/0e7ac77844e45311.docx +0 -0
- package/resources/qms-templates/12446a1b5e173c71.docx +0 -0
- package/resources/qms-templates/15ae40f67f9a3efc.docx +0 -0
- package/resources/qms-templates/18a69bbebf2ece7f.docx +0 -0
- package/resources/qms-templates/1978faa989134f02.docx +0 -0
- package/resources/qms-templates/1a30e121de752f7b.docx +0 -0
- package/resources/qms-templates/1b88907969ea7d4c.docx +0 -0
- package/resources/qms-templates/1e92b808722bf90a.docx +0 -0
- package/resources/qms-templates/2091cad340dc0e7f.docx +0 -0
- package/resources/qms-templates/271ae1002c896ed7.docx +0 -0
- package/resources/qms-templates/280ce53e5d3b5fcf.docx +0 -0
- package/resources/qms-templates/28ca8e4ede0adf6d.docx +0 -0
- package/resources/qms-templates/2ac6675f3cb1f993.docx +0 -0
- package/resources/qms-templates/2c3b799389909d8b.docx +0 -0
- package/resources/qms-templates/2c51038ba2cc3264.docx +0 -0
- package/resources/qms-templates/30c888851d16d764.docx +0 -0
- package/resources/qms-templates/3459fc4daaa2743d.docx +0 -0
- package/resources/qms-templates/3751dafc335e0e80.docx +0 -0
- package/resources/qms-templates/38798cd4298cd2db.docx +0 -0
- package/resources/qms-templates/38f551f9d738ab0a.docx +0 -0
- package/resources/qms-templates/3c85a23ea17018b4.docx +0 -0
- package/resources/qms-templates/3cdb7d14caf1b852.docx +0 -0
- package/resources/qms-templates/3f23239bb6b3e1db.docx +0 -0
- package/resources/qms-templates/42a5b9efeeaa8007.docx +0 -0
- package/resources/qms-templates/47352aafe9b8dac1.docx +0 -0
- package/resources/qms-templates/495359b1e3cbb8fe.docx +0 -0
- package/resources/qms-templates/4ca6920464785166.docx +0 -0
- package/resources/qms-templates/4ce862582a7791be.docx +0 -0
- package/resources/qms-templates/54c9a02a10991b9e.docx +0 -0
- package/resources/qms-templates/5aeb8fddf5f3693f.docx +0 -0
- package/resources/qms-templates/5d7e5576b0e73be7.docx +0 -0
- package/resources/qms-templates/5db3a40edac37791.docx +0 -0
- package/resources/qms-templates/5ffa21eb78eb6f77.docx +0 -0
- package/resources/qms-templates/60d0fbcb709b7161.docx +0 -0
- package/resources/qms-templates/63e1d3a166756095.docx +0 -0
- package/resources/qms-templates/65c724c080d32f10.docx +0 -0
- package/resources/qms-templates/674bd7826c6cbac2.docx +0 -0
- package/resources/qms-templates/677e851acca1b879.docx +0 -0
- package/resources/qms-templates/6bf15feb1a0d2a8d.docx +0 -0
- package/resources/qms-templates/6c3889c7d0918db9.docx +0 -0
- package/resources/qms-templates/6c4c349eeb0a96a8.docx +0 -0
- package/resources/qms-templates/6c5befbd33f759c1.docx +0 -0
- package/resources/qms-templates/6df8367901347508.docx +0 -0
- package/resources/qms-templates/71db51997037692b.docx +0 -0
- package/resources/qms-templates/78a8ef8c76510c99.docx +0 -0
- package/resources/qms-templates/8397bfd1c0a1b013.docx +0 -0
- package/resources/qms-templates/84d750cf858b60a1.docx +0 -0
- package/resources/qms-templates/880de58a4b334533.docx +0 -0
- package/resources/qms-templates/88c37d1b87d9b78c.docx +0 -0
- package/resources/qms-templates/88e9cd42f1849462.docx +0 -0
- package/resources/qms-templates/8956597403be35b6.docx +0 -0
- package/resources/qms-templates/94675c810a233e1f.docx +0 -0
- package/resources/qms-templates/94dff59dd7ebec6e.docx +0 -0
- package/resources/qms-templates/9670b9cd02141594.docx +0 -0
- package/resources/qms-templates/98c373ebd7b5163d.docx +0 -0
- package/resources/qms-templates/a4078cf2378a9388.docx +0 -0
- package/resources/qms-templates/a5b110c7ef917139.docx +0 -0
- package/resources/qms-templates/aec75820e45774be.docx +0 -0
- package/resources/qms-templates/b31c085e281d230c.docx +0 -0
- package/resources/qms-templates/b7c1c3c72f8ffeef.docx +0 -0
- package/resources/qms-templates/b81b82eaa5c8f422.docx +0 -0
- package/resources/qms-templates/b92a2fa31f75f137.docx +0 -0
- package/resources/qms-templates/b96c54ae9cfffc54.docx +0 -0
- package/resources/qms-templates/bc47cfd626c010e7.docx +0 -0
- package/resources/qms-templates/be60792734afca69.docx +0 -0
- package/resources/qms-templates/c0018d8ae9ad19e1.docx +0 -0
- package/resources/qms-templates/c171027c964c8be3.docx +0 -0
- package/resources/qms-templates/c4b93d71f5d98b4b.docx +0 -0
- package/resources/qms-templates/c531da29fcb3090f.docx +0 -0
- package/resources/qms-templates/c88eddd43358d015.docx +0 -0
- package/resources/qms-templates/cb978da542f0d892.docx +0 -0
- package/resources/qms-templates/ccde4a94532a8d36.docx +0 -0
- package/resources/qms-templates/cf3fb1e20ae6f24d.docx +0 -0
- package/resources/qms-templates/d231b4982fc8113e.docx +0 -0
- package/resources/qms-templates/d3c55aa5ed14055c.docx +0 -0
- package/resources/qms-templates/db45c44a00a13349.docx +0 -0
- package/resources/qms-templates/db849a41210f54e0.docx +0 -0
- package/resources/qms-templates/dba9ce363d83ddc8.docx +0 -0
- package/resources/qms-templates/dca3ea4c95eb2225.docx +0 -0
- package/resources/qms-templates/dfa4220d2b6b8131.docx +0 -0
- package/resources/qms-templates/dfa51222c777734f.docx +0 -0
- package/resources/qms-templates/e07ea511507e14f5.docx +0 -0
- package/resources/qms-templates/e0a2e00c03458dfa.docx +0 -0
- package/resources/qms-templates/e0e802f46b6764d1.docx +0 -0
- package/resources/qms-templates/e53fc85717f2852b.docx +0 -0
- package/resources/qms-templates/e9dd82d5e54520e5.docx +0 -0
- package/resources/qms-templates/ea7bfc22c06d82a9.docx +0 -0
- package/resources/qms-templates/ecf0db9f37294536.docx +0 -0
- package/resources/qms-templates/f05fcfc87e82ff39.docx +0 -0
- package/resources/qms-templates/fb9e56a17c4a4f97.docx +0 -0
- package/resources/qms-templates/manifest.json +932 -0
- package/src/data/seed-sme.js +317 -317
- package/src/data/seed.js +879 -879
- package/src/lib/backup-identity.js +57 -57
- package/src/lib/chat-answer.js +225 -225
- package/src/lib/chat-context.js +99 -99
- package/src/lib/company-library.js +28 -19
- package/src/lib/data-import.js +217 -217
- package/src/lib/data-sources.js +118 -118
- package/src/lib/demo-data.js +27 -1
- package/src/lib/employee-intake.js +84 -84
- package/src/lib/growth.js +53 -53
- package/src/lib/intake-packets.js +119 -119
- package/src/lib/issue-intake.js +88 -88
- package/src/lib/kb-revision.js +214 -214
- package/src/lib/llm.js +279 -279
- package/src/lib/model-error.js +14 -14
- package/src/lib/privacy.js +26 -26
- package/src/lib/qms-drafting.js +194 -194
- package/src/lib/qms-jobs.js +11 -7
- package/src/lib/qms-manual-review.js +185 -185
- package/src/lib/qms-procedure-review.js +109 -109
- package/src/lib/qms-templates.js +358 -326
- package/src/lib/qms.js +369 -369
- package/src/lib/quality-priority.js +100 -100
- package/src/lib/quality-state.js +92 -92
- package/src/lib/quality-workflow.js +65 -65
- package/src/lib/retrieval.js +159 -159
- package/src/lib/spc.js +127 -127
- package/src/lib/voice-hotwords.txt +22 -22
- package/src/lib/voice-text.js +13 -13
- package/src/lib/voice-worker.js +72 -72
- package/src/lib/voice.js +330 -330
- package/src/lib/workbench-notices.js +49 -49
- package/src/lib/workbench.js +103 -106
- package/src/package.json +3 -3
- package/src/tools/analysis.js +306 -306
- package/src/tools/data.js +596 -596
- package/src/tools/docs.js +430 -430
- package/src/tools/registry.js +57 -57
- package/tools-dsh.cjs +153 -153
- package//345/256/211/350/243/205/350/257/264/346/230/216.md +33 -25
- package//346/234/254/346/234/272/347/213/254/347/253/213/345/256/211/350/243/205/350/257/264/346/230/216.md +17 -17
- package//350/207/252/346/243/200.cjs +88 -88
- package/public/js/remote-knowledge.js +0 -165
- package/public/js/workbench-notices.js +0 -67
- package/public/remote-knowledge-help.html +0 -35
- package/public/remote-knowledge.css +0 -40
- package/src/lib/remote-knowledge.js +0 -284
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
class DraftVoiceCapture extends AudioWorkletProcessor {
|
|
2
|
-
constructor() {
|
|
3
|
-
super();
|
|
4
|
-
this.buffer = new Float32Array(2048);
|
|
5
|
-
this.used = 0;
|
|
6
|
-
this.port.onmessage = ({data}) => {
|
|
7
|
-
if (data !== 'finish' || this.finished) return;
|
|
8
|
-
this.flush();
|
|
9
|
-
this.finished = true;
|
|
10
|
-
this.port.postMessage('finished');
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
flush() {
|
|
14
|
-
if (!this.used) return;
|
|
15
|
-
const chunk = this.buffer.slice(0,this.used);
|
|
16
|
-
this.port.postMessage(chunk,[chunk.buffer]);
|
|
17
|
-
this.used = 0;
|
|
18
|
-
}
|
|
19
|
-
process(inputs) {
|
|
20
|
-
const channel = inputs[0]?.[0];
|
|
21
|
-
if (this.finished || !channel) return true;
|
|
22
|
-
for (const value of channel) {
|
|
23
|
-
this.buffer[this.used++] = value;
|
|
24
|
-
if (this.used === this.buffer.length) this.flush();
|
|
25
|
-
}
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
registerProcessor('qip-draft-voice', DraftVoiceCapture);
|
|
1
|
+
class DraftVoiceCapture extends AudioWorkletProcessor {
|
|
2
|
+
constructor() {
|
|
3
|
+
super();
|
|
4
|
+
this.buffer = new Float32Array(2048);
|
|
5
|
+
this.used = 0;
|
|
6
|
+
this.port.onmessage = ({data}) => {
|
|
7
|
+
if (data !== 'finish' || this.finished) return;
|
|
8
|
+
this.flush();
|
|
9
|
+
this.finished = true;
|
|
10
|
+
this.port.postMessage('finished');
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
flush() {
|
|
14
|
+
if (!this.used) return;
|
|
15
|
+
const chunk = this.buffer.slice(0,this.used);
|
|
16
|
+
this.port.postMessage(chunk,[chunk.buffer]);
|
|
17
|
+
this.used = 0;
|
|
18
|
+
}
|
|
19
|
+
process(inputs) {
|
|
20
|
+
const channel = inputs[0]?.[0];
|
|
21
|
+
if (this.finished || !channel) return true;
|
|
22
|
+
for (const value of channel) {
|
|
23
|
+
this.buffer[this.used++] = value;
|
|
24
|
+
if (this.used === this.buffer.length) this.flush();
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
registerProcessor('qip-draft-voice', DraftVoiceCapture);
|
|
@@ -1,264 +1,264 @@
|
|
|
1
|
-
(function (global) {
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
const endpoint = '/qip-real-workbench/api/voice';
|
|
5
|
-
const worklet = '/qip-real-workbench/js/voice-capture-worklet.js';
|
|
6
|
-
const rate = 16000;
|
|
7
|
-
const maxSeconds = 45;
|
|
8
|
-
const chunkSamples = rate * 0.75;
|
|
9
|
-
const pcmHeaders = { 'Content-Type': 'application/octet-stream' };
|
|
10
|
-
const labels = { opening: '取消开启麦克风', recording: '停止录音', processing: '取消转写' };
|
|
11
|
-
|
|
12
|
-
function joinSamples(chunks, length) {
|
|
13
|
-
const source = new Float32Array(length);
|
|
14
|
-
let offset = 0;
|
|
15
|
-
for (const chunk of chunks) { source.set(chunk, offset); offset += chunk.length; }
|
|
16
|
-
return source;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function audioSamples(capture) {
|
|
20
|
-
const source = joinSamples(capture.chunks, capture.count);
|
|
21
|
-
const ratio = capture.context.sampleRate / rate;
|
|
22
|
-
if (ratio === 1) return source;
|
|
23
|
-
const result = new Float32Array(Math.min(Math.floor(source.length / ratio), rate * maxSeconds));
|
|
24
|
-
for (let i = 0; i < result.length; i++) {
|
|
25
|
-
const start = Math.floor(i * ratio);
|
|
26
|
-
result[i] = source[start] + (source[Math.min(start + 1, source.length - 1)] - source[start]) * (i * ratio - start);
|
|
27
|
-
}
|
|
28
|
-
return result;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function insertDraft(input, { draft, cursor }, text) {
|
|
32
|
-
const index = input.value === draft ? cursor : input.value.length;
|
|
33
|
-
const room = input.maxLength < 0 ? Infinity : input.maxLength - input.value.length;
|
|
34
|
-
if (room <= 0) throw Error('输入框已达字数上限。');
|
|
35
|
-
const before = input.value.slice(0, index);
|
|
36
|
-
const after = input.value.slice(index);
|
|
37
|
-
const prefix = /[a-z\d]$/i.test(before) && /^[a-z\d]/i.test(text) ? ' ' : '';
|
|
38
|
-
const suffix = /^[a-z\d]/i.test(after) && /[a-z\d]$/i.test(text) ? ' ' : '';
|
|
39
|
-
const addition = prefix + text + suffix;
|
|
40
|
-
input.setRangeText(addition.slice(0, room), index, index, 'end');
|
|
41
|
-
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
42
|
-
input.focus({ preventScroll: true });
|
|
43
|
-
return addition.length > room;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async function request(path, options, signal) {
|
|
47
|
-
const headers = new Headers(options?.headers);
|
|
48
|
-
if (global.Workbench?.employee()) headers.set('X-QIP-Workbench','employee');
|
|
49
|
-
const response = await fetch(endpoint + path, { ...options, headers, signal, cache: 'no-store' });
|
|
50
|
-
const body = await response.json().catch(() => null);
|
|
51
|
-
if (!response.ok || !body?.ok) throw Error(body?.error || '语音服务暂不可用,请重试。');
|
|
52
|
-
return body;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function bind({ button, input, status, canStart, onChange }) {
|
|
56
|
-
let current;
|
|
57
|
-
|
|
58
|
-
function show(phase, message = '') {
|
|
59
|
-
button.dataset.phase = phase;
|
|
60
|
-
button.setAttribute('aria-pressed', String(phase === 'recording'));
|
|
61
|
-
button.setAttribute('aria-label', labels[phase] || '语音输入');
|
|
62
|
-
button.title = labels[phase] || '语音输入';
|
|
63
|
-
status.textContent = message;
|
|
64
|
-
onChange();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function release(capture) {
|
|
68
|
-
if (capture.released) return;
|
|
69
|
-
capture.released = true;
|
|
70
|
-
clearInterval(capture.tick);
|
|
71
|
-
clearTimeout(capture.flush);
|
|
72
|
-
capture.stream?.getTracks().forEach((track) => track.stop());
|
|
73
|
-
capture.source?.disconnect();
|
|
74
|
-
capture.node?.disconnect();
|
|
75
|
-
capture.node?.port.close();
|
|
76
|
-
capture.gain?.disconnect();
|
|
77
|
-
if (capture.context?.state !== 'closed') capture.context?.close().catch(() => {});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function closeSession(capture) {
|
|
81
|
-
if (!capture.sessionPath) return;
|
|
82
|
-
const path = capture.sessionPath;
|
|
83
|
-
capture.sessionPath = '';
|
|
84
|
-
void request(path, { method: 'DELETE', keepalive: true }).catch(() => {});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function cancel(message = '', phase = 'idle') {
|
|
88
|
-
const capture = current;
|
|
89
|
-
if (capture) {
|
|
90
|
-
current = null;
|
|
91
|
-
capture.controller.abort();
|
|
92
|
-
release(capture);
|
|
93
|
-
closeSession(capture);
|
|
94
|
-
}
|
|
95
|
-
show(phase, message);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async function prepare(capture) {
|
|
99
|
-
const { signal } = capture.controller;
|
|
100
|
-
let engine = await request('/prepare', { method: 'POST' }, signal);
|
|
101
|
-
while (engine.phase !== 'ready') {
|
|
102
|
-
signal.throwIfAborted();
|
|
103
|
-
if (engine.phase === 'error') throw Error(engine.error || '暂时无法启动语音识别。');
|
|
104
|
-
capture.preparing = engine.phase === 'downloading' ? '正在下载中文语音模型…' : '正在启动语音识别…';
|
|
105
|
-
if (capture.stopping) show('processing', capture.preparing);
|
|
106
|
-
await new Promise((resolve) => setTimeout(resolve, engine.phase === 'downloading' ? 650 : 150));
|
|
107
|
-
signal.throwIfAborted();
|
|
108
|
-
engine = await request('', {}, signal);
|
|
109
|
-
}
|
|
110
|
-
capture.preparing = '';
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function openSession(capture) {
|
|
114
|
-
if (capture.context.sampleRate !== rate) return null;
|
|
115
|
-
return capture.ready.then(async (error) => {
|
|
116
|
-
if (error || current !== capture) return null;
|
|
117
|
-
const { id } = await request('/sessions', { method: 'POST' }, AbortSignal.timeout(5000));
|
|
118
|
-
capture.sessionPath = '/sessions/' + encodeURIComponent(id);
|
|
119
|
-
if (current !== capture) { closeSession(capture); return null; }
|
|
120
|
-
return true;
|
|
121
|
-
}).catch(() => null);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function enqueue(capture, final) {
|
|
125
|
-
if (!capture.queue) return;
|
|
126
|
-
const { uploadIndex, uploadedCount, count } = capture;
|
|
127
|
-
const length = count - uploadedCount;
|
|
128
|
-
if (!final && length < chunkSamples) return;
|
|
129
|
-
const chunks = capture.chunks.slice(uploadIndex);
|
|
130
|
-
capture.uploadIndex = capture.chunks.length;
|
|
131
|
-
capture.uploadedCount = count;
|
|
132
|
-
capture.queue = capture.queue.then((previous) => {
|
|
133
|
-
if (!previous || current !== capture || !capture.sessionPath) return null;
|
|
134
|
-
return request(capture.sessionPath, {
|
|
135
|
-
method: final ? 'POST' : 'PUT',
|
|
136
|
-
headers: pcmHeaders,
|
|
137
|
-
body: joinSamples(chunks, length).buffer,
|
|
138
|
-
}, capture.controller.signal);
|
|
139
|
-
}).catch(() => { closeSession(capture); return null; });
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async function transcribe(capture) {
|
|
143
|
-
if (current !== capture || capture.transcribing) return;
|
|
144
|
-
capture.transcribing = true;
|
|
145
|
-
release(capture);
|
|
146
|
-
try {
|
|
147
|
-
if (capture.count < capture.context.sampleRate / 4) throw Error('录音太短,请再说一次。');
|
|
148
|
-
enqueue(capture, true);
|
|
149
|
-
const failed = await capture.ready;
|
|
150
|
-
if (current !== capture) return;
|
|
151
|
-
if (failed) throw failed;
|
|
152
|
-
show('processing', '正在转成文字…');
|
|
153
|
-
const streamed = await capture.queue;
|
|
154
|
-
if (current !== capture) return;
|
|
155
|
-
const { text } = streamed || await request('/transcribe', {
|
|
156
|
-
method: 'POST', headers: pcmHeaders, body: audioSamples(capture).buffer,
|
|
157
|
-
}, capture.controller.signal);
|
|
158
|
-
capture.sessionPath = '';
|
|
159
|
-
capture.chunks = [];
|
|
160
|
-
if (current !== capture || !input.isConnected) return;
|
|
161
|
-
const recognized = String(text || '').trim();
|
|
162
|
-
if (!recognized) throw Error('没听清,请靠近麦克风再试。');
|
|
163
|
-
const truncated = insertDraft(input, capture, recognized);
|
|
164
|
-
current = null;
|
|
165
|
-
show('idle', truncated ? '已填至字数上限。' : '');
|
|
166
|
-
} catch (error) {
|
|
167
|
-
if (current === capture) cancel(error.message, 'error');
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function receive(capture, data) {
|
|
172
|
-
if (current !== capture) return;
|
|
173
|
-
if (data === 'finished') { void transcribe(capture); return; }
|
|
174
|
-
if (!(data instanceof Float32Array)) return;
|
|
175
|
-
const take = Math.min(capture.context.sampleRate * maxSeconds - capture.count, data.length);
|
|
176
|
-
if (take <= 0) return;
|
|
177
|
-
capture.chunks.push(data.subarray(0, take));
|
|
178
|
-
capture.count += take;
|
|
179
|
-
if (!capture.stopping) enqueue(capture, false);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function stop(capture) {
|
|
183
|
-
if (current !== capture || capture.stopping || !capture.node) return;
|
|
184
|
-
capture.stopping = true;
|
|
185
|
-
clearInterval(capture.tick);
|
|
186
|
-
show('processing', capture.preparing || '正在转成文字…');
|
|
187
|
-
capture.flush = setTimeout(() => transcribe(capture), 300);
|
|
188
|
-
capture.node.port.postMessage('finish');
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
async function start() {
|
|
192
|
-
const { AudioContext, AudioWorkletNode } = global;
|
|
193
|
-
if (!global.isSecureContext || !navigator.mediaDevices?.getUserMedia || !AudioContext || !AudioWorkletNode) {
|
|
194
|
-
show('error', '此页面无法使用麦克风,请在本机 localhost 或 HTTPS 打开。');
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
const capture = current = { controller: new AbortController(), chunks: [], count: 0, uploadIndex: 0,
|
|
198
|
-
uploadedCount: 0, draft: input.value, cursor: input.selectionEnd };
|
|
199
|
-
try {
|
|
200
|
-
show('opening', '正在开启麦克风…');
|
|
201
|
-
capture.ready = prepare(capture).catch((error) => {
|
|
202
|
-
if (current === capture && !capture.stopping) cancel(error.message, 'error');
|
|
203
|
-
return error;
|
|
204
|
-
});
|
|
205
|
-
const stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true }, video: false });
|
|
206
|
-
if (current !== capture) { stream.getTracks().forEach((track) => track.stop()); return; }
|
|
207
|
-
capture.stream = stream;
|
|
208
|
-
stream.getAudioTracks().forEach((track) => track.addEventListener('ended', () => {
|
|
209
|
-
if (current === capture && !capture.stopping) cancel('麦克风已断开,请重试。', 'error');
|
|
210
|
-
}, { once: true }));
|
|
211
|
-
try { capture.context = new AudioContext({ sampleRate: rate }); }
|
|
212
|
-
catch (error) {
|
|
213
|
-
if (error.name !== 'NotSupportedError') throw error;
|
|
214
|
-
capture.context = new AudioContext();
|
|
215
|
-
}
|
|
216
|
-
await capture.context.audioWorklet.addModule(worklet);
|
|
217
|
-
if (current !== capture) return;
|
|
218
|
-
await capture.context.resume();
|
|
219
|
-
if (current !== capture) return;
|
|
220
|
-
capture.source = capture.context.createMediaStreamSource(stream);
|
|
221
|
-
capture.node = new AudioWorkletNode(capture.context, 'qip-draft-voice', { channelCount: 1, channelCountMode: 'explicit' });
|
|
222
|
-
capture.gain = capture.context.createGain();
|
|
223
|
-
capture.gain.gain.value = 0;
|
|
224
|
-
capture.source.connect(capture.node).connect(capture.gain).connect(capture.context.destination);
|
|
225
|
-
capture.queue = openSession(capture);
|
|
226
|
-
capture.node.port.onmessage = ({ data }) => receive(capture, data);
|
|
227
|
-
show('recording', '正在录音 0:00');
|
|
228
|
-
const started = performance.now();
|
|
229
|
-
capture.tick = setInterval(() => {
|
|
230
|
-
const seconds = Math.floor((performance.now() - started) / 1000);
|
|
231
|
-
if (seconds >= maxSeconds) stop(capture);
|
|
232
|
-
else {
|
|
233
|
-
const message = '正在录音 0:' + String(seconds).padStart(2, '0');
|
|
234
|
-
if (status.textContent !== message) status.textContent = message;
|
|
235
|
-
}
|
|
236
|
-
}, 250);
|
|
237
|
-
} catch (error) {
|
|
238
|
-
if (current !== capture) return;
|
|
239
|
-
const message = {
|
|
240
|
-
NotAllowedError: '请在浏览器中允许使用麦克风后重试。',
|
|
241
|
-
NotFoundError: '没有找到可用麦克风。',
|
|
242
|
-
NotReadableError: '麦克风暂不可用,请检查设备。',
|
|
243
|
-
}[error.name] || '无法开启录音,请检查麦克风后重试。';
|
|
244
|
-
cancel(message, 'error');
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
button.addEventListener('click', () => {
|
|
249
|
-
if (button.dataset.phase === 'recording') stop(current);
|
|
250
|
-
else if (current) cancel();
|
|
251
|
-
else if (canStart()) void start();
|
|
252
|
-
});
|
|
253
|
-
global.addEventListener('pagehide', () => cancel());
|
|
254
|
-
return { active: () => !!current, cancel: () => cancel() };
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function warm() {
|
|
258
|
-
if (global.isSecureContext && navigator.mediaDevices?.getUserMedia && global.AudioWorkletNode) {
|
|
259
|
-
void request('/warm', { method: 'POST' }).catch(() => {});
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
global.XiaozhiVoice = { bind, warm };
|
|
264
|
-
})(window);
|
|
1
|
+
(function (global) {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const endpoint = '/qip-real-workbench/api/voice';
|
|
5
|
+
const worklet = '/qip-real-workbench/js/voice-capture-worklet.js';
|
|
6
|
+
const rate = 16000;
|
|
7
|
+
const maxSeconds = 45;
|
|
8
|
+
const chunkSamples = rate * 0.75;
|
|
9
|
+
const pcmHeaders = { 'Content-Type': 'application/octet-stream' };
|
|
10
|
+
const labels = { opening: '取消开启麦克风', recording: '停止录音', processing: '取消转写' };
|
|
11
|
+
|
|
12
|
+
function joinSamples(chunks, length) {
|
|
13
|
+
const source = new Float32Array(length);
|
|
14
|
+
let offset = 0;
|
|
15
|
+
for (const chunk of chunks) { source.set(chunk, offset); offset += chunk.length; }
|
|
16
|
+
return source;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function audioSamples(capture) {
|
|
20
|
+
const source = joinSamples(capture.chunks, capture.count);
|
|
21
|
+
const ratio = capture.context.sampleRate / rate;
|
|
22
|
+
if (ratio === 1) return source;
|
|
23
|
+
const result = new Float32Array(Math.min(Math.floor(source.length / ratio), rate * maxSeconds));
|
|
24
|
+
for (let i = 0; i < result.length; i++) {
|
|
25
|
+
const start = Math.floor(i * ratio);
|
|
26
|
+
result[i] = source[start] + (source[Math.min(start + 1, source.length - 1)] - source[start]) * (i * ratio - start);
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function insertDraft(input, { draft, cursor }, text) {
|
|
32
|
+
const index = input.value === draft ? cursor : input.value.length;
|
|
33
|
+
const room = input.maxLength < 0 ? Infinity : input.maxLength - input.value.length;
|
|
34
|
+
if (room <= 0) throw Error('输入框已达字数上限。');
|
|
35
|
+
const before = input.value.slice(0, index);
|
|
36
|
+
const after = input.value.slice(index);
|
|
37
|
+
const prefix = /[a-z\d]$/i.test(before) && /^[a-z\d]/i.test(text) ? ' ' : '';
|
|
38
|
+
const suffix = /^[a-z\d]/i.test(after) && /[a-z\d]$/i.test(text) ? ' ' : '';
|
|
39
|
+
const addition = prefix + text + suffix;
|
|
40
|
+
input.setRangeText(addition.slice(0, room), index, index, 'end');
|
|
41
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
42
|
+
input.focus({ preventScroll: true });
|
|
43
|
+
return addition.length > room;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function request(path, options, signal) {
|
|
47
|
+
const headers = new Headers(options?.headers);
|
|
48
|
+
if (global.Workbench?.employee()) headers.set('X-QIP-Workbench','employee');
|
|
49
|
+
const response = await fetch(endpoint + path, { ...options, headers, signal, cache: 'no-store' });
|
|
50
|
+
const body = await response.json().catch(() => null);
|
|
51
|
+
if (!response.ok || !body?.ok) throw Error(body?.error || '语音服务暂不可用,请重试。');
|
|
52
|
+
return body;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function bind({ button, input, status, canStart, onChange }) {
|
|
56
|
+
let current;
|
|
57
|
+
|
|
58
|
+
function show(phase, message = '') {
|
|
59
|
+
button.dataset.phase = phase;
|
|
60
|
+
button.setAttribute('aria-pressed', String(phase === 'recording'));
|
|
61
|
+
button.setAttribute('aria-label', labels[phase] || '语音输入');
|
|
62
|
+
button.title = labels[phase] || '语音输入';
|
|
63
|
+
status.textContent = message;
|
|
64
|
+
onChange();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function release(capture) {
|
|
68
|
+
if (capture.released) return;
|
|
69
|
+
capture.released = true;
|
|
70
|
+
clearInterval(capture.tick);
|
|
71
|
+
clearTimeout(capture.flush);
|
|
72
|
+
capture.stream?.getTracks().forEach((track) => track.stop());
|
|
73
|
+
capture.source?.disconnect();
|
|
74
|
+
capture.node?.disconnect();
|
|
75
|
+
capture.node?.port.close();
|
|
76
|
+
capture.gain?.disconnect();
|
|
77
|
+
if (capture.context?.state !== 'closed') capture.context?.close().catch(() => {});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function closeSession(capture) {
|
|
81
|
+
if (!capture.sessionPath) return;
|
|
82
|
+
const path = capture.sessionPath;
|
|
83
|
+
capture.sessionPath = '';
|
|
84
|
+
void request(path, { method: 'DELETE', keepalive: true }).catch(() => {});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function cancel(message = '', phase = 'idle') {
|
|
88
|
+
const capture = current;
|
|
89
|
+
if (capture) {
|
|
90
|
+
current = null;
|
|
91
|
+
capture.controller.abort();
|
|
92
|
+
release(capture);
|
|
93
|
+
closeSession(capture);
|
|
94
|
+
}
|
|
95
|
+
show(phase, message);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function prepare(capture) {
|
|
99
|
+
const { signal } = capture.controller;
|
|
100
|
+
let engine = await request('/prepare', { method: 'POST' }, signal);
|
|
101
|
+
while (engine.phase !== 'ready') {
|
|
102
|
+
signal.throwIfAborted();
|
|
103
|
+
if (engine.phase === 'error') throw Error(engine.error || '暂时无法启动语音识别。');
|
|
104
|
+
capture.preparing = engine.phase === 'downloading' ? '正在下载中文语音模型…' : '正在启动语音识别…';
|
|
105
|
+
if (capture.stopping) show('processing', capture.preparing);
|
|
106
|
+
await new Promise((resolve) => setTimeout(resolve, engine.phase === 'downloading' ? 650 : 150));
|
|
107
|
+
signal.throwIfAborted();
|
|
108
|
+
engine = await request('', {}, signal);
|
|
109
|
+
}
|
|
110
|
+
capture.preparing = '';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function openSession(capture) {
|
|
114
|
+
if (capture.context.sampleRate !== rate) return null;
|
|
115
|
+
return capture.ready.then(async (error) => {
|
|
116
|
+
if (error || current !== capture) return null;
|
|
117
|
+
const { id } = await request('/sessions', { method: 'POST' }, AbortSignal.timeout(5000));
|
|
118
|
+
capture.sessionPath = '/sessions/' + encodeURIComponent(id);
|
|
119
|
+
if (current !== capture) { closeSession(capture); return null; }
|
|
120
|
+
return true;
|
|
121
|
+
}).catch(() => null);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function enqueue(capture, final) {
|
|
125
|
+
if (!capture.queue) return;
|
|
126
|
+
const { uploadIndex, uploadedCount, count } = capture;
|
|
127
|
+
const length = count - uploadedCount;
|
|
128
|
+
if (!final && length < chunkSamples) return;
|
|
129
|
+
const chunks = capture.chunks.slice(uploadIndex);
|
|
130
|
+
capture.uploadIndex = capture.chunks.length;
|
|
131
|
+
capture.uploadedCount = count;
|
|
132
|
+
capture.queue = capture.queue.then((previous) => {
|
|
133
|
+
if (!previous || current !== capture || !capture.sessionPath) return null;
|
|
134
|
+
return request(capture.sessionPath, {
|
|
135
|
+
method: final ? 'POST' : 'PUT',
|
|
136
|
+
headers: pcmHeaders,
|
|
137
|
+
body: joinSamples(chunks, length).buffer,
|
|
138
|
+
}, capture.controller.signal);
|
|
139
|
+
}).catch(() => { closeSession(capture); return null; });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async function transcribe(capture) {
|
|
143
|
+
if (current !== capture || capture.transcribing) return;
|
|
144
|
+
capture.transcribing = true;
|
|
145
|
+
release(capture);
|
|
146
|
+
try {
|
|
147
|
+
if (capture.count < capture.context.sampleRate / 4) throw Error('录音太短,请再说一次。');
|
|
148
|
+
enqueue(capture, true);
|
|
149
|
+
const failed = await capture.ready;
|
|
150
|
+
if (current !== capture) return;
|
|
151
|
+
if (failed) throw failed;
|
|
152
|
+
show('processing', '正在转成文字…');
|
|
153
|
+
const streamed = await capture.queue;
|
|
154
|
+
if (current !== capture) return;
|
|
155
|
+
const { text } = streamed || await request('/transcribe', {
|
|
156
|
+
method: 'POST', headers: pcmHeaders, body: audioSamples(capture).buffer,
|
|
157
|
+
}, capture.controller.signal);
|
|
158
|
+
capture.sessionPath = '';
|
|
159
|
+
capture.chunks = [];
|
|
160
|
+
if (current !== capture || !input.isConnected) return;
|
|
161
|
+
const recognized = String(text || '').trim();
|
|
162
|
+
if (!recognized) throw Error('没听清,请靠近麦克风再试。');
|
|
163
|
+
const truncated = insertDraft(input, capture, recognized);
|
|
164
|
+
current = null;
|
|
165
|
+
show('idle', truncated ? '已填至字数上限。' : '');
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (current === capture) cancel(error.message, 'error');
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function receive(capture, data) {
|
|
172
|
+
if (current !== capture) return;
|
|
173
|
+
if (data === 'finished') { void transcribe(capture); return; }
|
|
174
|
+
if (!(data instanceof Float32Array)) return;
|
|
175
|
+
const take = Math.min(capture.context.sampleRate * maxSeconds - capture.count, data.length);
|
|
176
|
+
if (take <= 0) return;
|
|
177
|
+
capture.chunks.push(data.subarray(0, take));
|
|
178
|
+
capture.count += take;
|
|
179
|
+
if (!capture.stopping) enqueue(capture, false);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function stop(capture) {
|
|
183
|
+
if (current !== capture || capture.stopping || !capture.node) return;
|
|
184
|
+
capture.stopping = true;
|
|
185
|
+
clearInterval(capture.tick);
|
|
186
|
+
show('processing', capture.preparing || '正在转成文字…');
|
|
187
|
+
capture.flush = setTimeout(() => transcribe(capture), 300);
|
|
188
|
+
capture.node.port.postMessage('finish');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async function start() {
|
|
192
|
+
const { AudioContext, AudioWorkletNode } = global;
|
|
193
|
+
if (!global.isSecureContext || !navigator.mediaDevices?.getUserMedia || !AudioContext || !AudioWorkletNode) {
|
|
194
|
+
show('error', '此页面无法使用麦克风,请在本机 localhost 或 HTTPS 打开。');
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const capture = current = { controller: new AbortController(), chunks: [], count: 0, uploadIndex: 0,
|
|
198
|
+
uploadedCount: 0, draft: input.value, cursor: input.selectionEnd };
|
|
199
|
+
try {
|
|
200
|
+
show('opening', '正在开启麦克风…');
|
|
201
|
+
capture.ready = prepare(capture).catch((error) => {
|
|
202
|
+
if (current === capture && !capture.stopping) cancel(error.message, 'error');
|
|
203
|
+
return error;
|
|
204
|
+
});
|
|
205
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true }, video: false });
|
|
206
|
+
if (current !== capture) { stream.getTracks().forEach((track) => track.stop()); return; }
|
|
207
|
+
capture.stream = stream;
|
|
208
|
+
stream.getAudioTracks().forEach((track) => track.addEventListener('ended', () => {
|
|
209
|
+
if (current === capture && !capture.stopping) cancel('麦克风已断开,请重试。', 'error');
|
|
210
|
+
}, { once: true }));
|
|
211
|
+
try { capture.context = new AudioContext({ sampleRate: rate }); }
|
|
212
|
+
catch (error) {
|
|
213
|
+
if (error.name !== 'NotSupportedError') throw error;
|
|
214
|
+
capture.context = new AudioContext();
|
|
215
|
+
}
|
|
216
|
+
await capture.context.audioWorklet.addModule(worklet);
|
|
217
|
+
if (current !== capture) return;
|
|
218
|
+
await capture.context.resume();
|
|
219
|
+
if (current !== capture) return;
|
|
220
|
+
capture.source = capture.context.createMediaStreamSource(stream);
|
|
221
|
+
capture.node = new AudioWorkletNode(capture.context, 'qip-draft-voice', { channelCount: 1, channelCountMode: 'explicit' });
|
|
222
|
+
capture.gain = capture.context.createGain();
|
|
223
|
+
capture.gain.gain.value = 0;
|
|
224
|
+
capture.source.connect(capture.node).connect(capture.gain).connect(capture.context.destination);
|
|
225
|
+
capture.queue = openSession(capture);
|
|
226
|
+
capture.node.port.onmessage = ({ data }) => receive(capture, data);
|
|
227
|
+
show('recording', '正在录音 0:00');
|
|
228
|
+
const started = performance.now();
|
|
229
|
+
capture.tick = setInterval(() => {
|
|
230
|
+
const seconds = Math.floor((performance.now() - started) / 1000);
|
|
231
|
+
if (seconds >= maxSeconds) stop(capture);
|
|
232
|
+
else {
|
|
233
|
+
const message = '正在录音 0:' + String(seconds).padStart(2, '0');
|
|
234
|
+
if (status.textContent !== message) status.textContent = message;
|
|
235
|
+
}
|
|
236
|
+
}, 250);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
if (current !== capture) return;
|
|
239
|
+
const message = {
|
|
240
|
+
NotAllowedError: '请在浏览器中允许使用麦克风后重试。',
|
|
241
|
+
NotFoundError: '没有找到可用麦克风。',
|
|
242
|
+
NotReadableError: '麦克风暂不可用,请检查设备。',
|
|
243
|
+
}[error.name] || '无法开启录音,请检查麦克风后重试。';
|
|
244
|
+
cancel(message, 'error');
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
button.addEventListener('click', () => {
|
|
249
|
+
if (button.dataset.phase === 'recording') stop(current);
|
|
250
|
+
else if (current) cancel();
|
|
251
|
+
else if (canStart()) void start();
|
|
252
|
+
});
|
|
253
|
+
global.addEventListener('pagehide', () => cancel());
|
|
254
|
+
return { active: () => !!current, cancel: () => cancel() };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function warm() {
|
|
258
|
+
if (global.isSecureContext && navigator.mediaDevices?.getUserMedia && global.AudioWorkletNode) {
|
|
259
|
+
void request('/warm', { method: 'POST' }).catch(() => {});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
global.XiaozhiVoice = { bind, warm };
|
|
264
|
+
})(window);
|