@hyprnos/dsh-remote-plugin 0.0.0-stage → 0.8.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/README.en.md +61 -0
- package/README.md +60 -2
- package/apk/dsh-remote.apk +0 -0
- package/approval-summary.mjs +115 -0
- package/client.js +197 -0
- package/cordis.patch.yml +5 -0
- package/doctor-skill.mjs +99 -0
- package/doctor-target.mjs +24 -0
- package/file-delivery.mjs +84 -0
- package/gateway-stats.cjs +501 -0
- package/gateway.cjs +8451 -0
- package/index.mjs +1784 -0
- package/insights.mjs +111 -0
- package/market.mjs +86 -0
- package/package.json +71 -4
- package/peak-reminders.mjs +50 -0
- package/plugin-center.mjs +777 -0
- package/plugin-transaction.mjs +300 -0
- package/public/activity.js +188 -0
- package/public/admin.html +820 -0
- package/public/admin.js +1167 -0
- package/public/announcement-forms.js +89 -0
- package/public/announcements.json +464 -0
- package/public/app.js +8826 -0
- package/public/approval-records.js +301 -0
- package/public/approval-summary.js +78 -0
- package/public/build.json +5 -0
- package/public/cache-upgrade.js +51 -0
- package/public/chat-copy.js +59 -0
- package/public/chat-files.js +87 -0
- package/public/desktop/desktop.css +816 -0
- package/public/desktop/desktop.html +871 -0
- package/public/desktop/desktop.js +4548 -0
- package/public/desktop/i18n.js +85 -0
- package/public/diagnostics.js +203 -0
- package/public/doctor-entry.js +94 -0
- package/public/doctor.css +205 -0
- package/public/doctor.js +769 -0
- package/public/donate.png +0 -0
- package/public/e2ee-transport.js +219 -0
- package/public/extensions.css +109 -0
- package/public/extensions.js +215 -0
- package/public/file-inbox.css +66 -0
- package/public/file-inbox.js +289 -0
- package/public/genui.css +8 -0
- package/public/genui.js +254 -0
- package/public/history.js +304 -0
- package/public/i18n.js +57 -0
- package/public/icon.svg +12 -0
- package/public/image-cache.js +141 -0
- package/public/image-preview.js +290 -0
- package/public/index.html +1548 -0
- package/public/insights.css +28 -0
- package/public/insights.js +290 -0
- package/public/jsqr.min.js +10104 -0
- package/public/lan-discovery.js +94 -0
- package/public/link-check.js +109 -0
- package/public/manifest.webmanifest +16 -0
- package/public/md.js +333 -0
- package/public/morphicons-init.js +41 -0
- package/public/motion.js +557 -0
- package/public/network.js +100 -0
- package/public/onboarding.css +36 -0
- package/public/onboarding.js +253 -0
- package/public/operations.js +141 -0
- package/public/permission-presets.js +159 -0
- package/public/plugin-center.css +120 -0
- package/public/plugin-center.js +666 -0
- package/public/plugin-icon.svg +5 -0
- package/public/plugin.html +68 -0
- package/public/plugin.js +211 -0
- package/public/qrcode.min.js +2297 -0
- package/public/relay-autopair.js +251 -0
- package/public/relay-devices.js +80 -0
- package/public/relay-settings.css +24 -0
- package/public/relay-settings.js +194 -0
- package/public/remote-state.js +27 -0
- package/public/remote-ui.css +148 -0
- package/public/remote-ui.js +243 -0
- package/public/session-deck.css +79 -0
- package/public/session-deck.js +180 -0
- package/public/session-presets.js +47 -0
- package/public/setup.css +18 -0
- package/public/setup.js +213 -0
- package/public/sha256.js +119 -0
- package/public/skills.css +81 -0
- package/public/skills.js +202 -0
- package/public/skins/blue-fish/DESIGN.md +24 -0
- package/public/skins/blue-fish/face.png +0 -0
- package/public/skins/blue-fish/icon-blush.png +0 -0
- package/public/skins/blue-fish/icon-sleep.png +0 -0
- package/public/skins/blue-fish/icon-smile.png +0 -0
- package/public/skins/blue-fish/icon-star.png +0 -0
- package/public/skins/blue-fish/icon-wink.png +0 -0
- package/public/skins/blue-fish/icon.png +0 -0
- package/public/skins/blue-fish/mascot.png +0 -0
- package/public/skins/blue-fish/pattern.png +0 -0
- package/public/skins/blue-fish/preview.html +12 -0
- package/public/skins/blue-fish/skin.css +122 -0
- package/public/skins/blue-fish/sparkles.svg +18 -0
- package/public/skins/bocchi/DESIGN.md +99 -0
- package/public/skins/bocchi/face.png +0 -0
- package/public/skins/bocchi/grain.png +0 -0
- package/public/skins/bocchi/icon-blush.png +0 -0
- package/public/skins/bocchi/icon-sleep.png +0 -0
- package/public/skins/bocchi/icon-smile.png +0 -0
- package/public/skins/bocchi/icon-star.png +0 -0
- package/public/skins/bocchi/icon-wink.png +0 -0
- package/public/skins/bocchi/icon.png +0 -0
- package/public/skins/bocchi/pattern-soft.png +0 -0
- package/public/skins/bocchi/pattern.png +0 -0
- package/public/skins/bocchi/preview.html +55 -0
- package/public/skins/bocchi/skin.css +233 -0
- package/public/skins/bocchi/stage-bg.png +0 -0
- package/public/skins/bocchi/stage-soft.png +0 -0
- package/public/skins/bocchi/stand.png +0 -0
- package/public/skins/bocchi/sticker-guitar.png +0 -0
- package/public/skins/kessoku/DESIGN.md +38 -0
- package/public/skins/kessoku/grain.png +0 -0
- package/public/skins/kessoku/skin.css +212 -0
- package/public/skins/kita/SOURCES.json +51 -0
- package/public/skins/kita/face.png +0 -0
- package/public/skins/kita/icon-live.png +0 -0
- package/public/skins/kita/icon-reddress.png +0 -0
- package/public/skins/kita/icon-seifuku.png +0 -0
- package/public/skins/kita/pattern.png +0 -0
- package/public/skins/kita/preview.html +56 -0
- package/public/skins/kita/skin.css +26 -0
- package/public/skins/kita/stand.png +0 -0
- package/public/skins/kita/sticker-live.png +0 -0
- package/public/skins/nijika/SOURCES.json +51 -0
- package/public/skins/nijika/face.png +0 -0
- package/public/skins/nijika/icon-live.png +0 -0
- package/public/skins/nijika/icon-reddress.png +0 -0
- package/public/skins/nijika/icon-seifuku.png +0 -0
- package/public/skins/nijika/pattern.png +0 -0
- package/public/skins/nijika/preview.html +56 -0
- package/public/skins/nijika/skin.css +25 -0
- package/public/skins/nijika/stand.png +0 -0
- package/public/skins/nijika/sticker-live.png +0 -0
- package/public/skins/penguin/DESIGN.md +33 -0
- package/public/skins/penguin/SOURCES.json +22 -0
- package/public/skins/penguin/aic-blueprint.svg +24 -0
- package/public/skins/penguin/endfield-triangles.svg +4 -0
- package/public/skins/penguin/face.png +0 -0
- package/public/skins/penguin/icon-blush.png +0 -0
- package/public/skins/penguin/icon-dizzy.png +0 -0
- package/public/skins/penguin/icon-laugh.png +0 -0
- package/public/skins/penguin/icon-sleep.png +0 -0
- package/public/skins/penguin/icon-smile.png +0 -0
- package/public/skins/penguin/icon-star.png +0 -0
- package/public/skins/penguin/icon-wink.png +0 -0
- package/public/skins/penguin/icon.png +0 -0
- package/public/skins/penguin/mascot.png +0 -0
- package/public/skins/penguin/pattern-night.png +0 -0
- package/public/skins/penguin/pattern.png +0 -0
- package/public/skins/penguin/preview.html +12 -0
- package/public/skins/penguin/skin.css +212 -0
- package/public/skins/ryo/SOURCES.json +51 -0
- package/public/skins/ryo/face.png +0 -0
- package/public/skins/ryo/icon-live.png +0 -0
- package/public/skins/ryo/icon-reddress.png +0 -0
- package/public/skins/ryo/icon-seifuku.png +0 -0
- package/public/skins/ryo/pattern.png +0 -0
- package/public/skins/ryo/preview.html +56 -0
- package/public/skins/ryo/skin.css +25 -0
- package/public/skins/ryo/stand.png +0 -0
- package/public/skins/ryo/sticker-live.png +0 -0
- package/public/sponsor-entry.js +21 -0
- package/public/styles.css +1494 -0
- package/public/subagents.css +61 -0
- package/public/subagents.js +275 -0
- package/public/task-board.js +109 -0
- package/public/teams.js +98 -0
- package/public/theme-picker.css +58 -0
- package/public/theme-vars.css +1044 -0
- package/public/theme.js +166 -0
- package/public/transcribe-core.js +74 -0
- package/public/turn-results.js +121 -0
- package/public/update.json +49 -0
- package/public/vendor/gsap/NOTICE.md +10 -0
- package/public/vendor/gsap/gsap.min.js +11 -0
- package/public/vendor/morphicons/LICENSE +21 -0
- package/public/vendor/morphicons/README.md +10 -0
- package/public/vendor/morphicons/controller-CXZuwJ_M.js +152 -0
- package/public/vendor/morphicons/dom.js +206 -0
- package/public/vendor/morphicons/element.js +261 -0
- package/public/vendor/morphicons/normalize-CYnN3Npw.js +540 -0
- package/public/vendor/morphicons/spring-CFHloqPP.js +623 -0
- package/public/version.json +3 -0
- package/public/voice-input.js +739 -0
- package/public/workflow.css +365 -0
- package/public/workflow.js +409 -0
- package/session-file.mjs +39 -0
- package/skills/dsh-remote-doctor/SKILL.md +186 -0
- package/skills/dsh-remote-doctor/references/wiki/README.md +40 -0
- package/skills/dsh-remote-doctor/references/wiki/ai-guide.md +61 -0
- package/skills/dsh-remote-doctor/references/wiki/connection.md +76 -0
- package/skills/dsh-remote-doctor/references/wiki/errors.md +26 -0
- package/skills/dsh-remote-doctor/references/wiki/evidence.md +44 -0
- package/skills/dsh-remote-doctor/references/wiki/files.md +31 -0
- package/skills/dsh-remote-doctor/references/wiki/maintenance.md +30 -0
- package/skills/dsh-remote-doctor/references/wiki/notifications.md +33 -0
- package/skills/dsh-remote-doctor/references/wiki/operations.md +32 -0
- package/skills/dsh-remote-doctor/references/wiki/plugins.md +90 -0
- package/skills/dsh-remote-doctor/references/wiki/sessions.md +45 -0
- package/skills/dsh-remote-doctor/references/wiki/sources.md +27 -0
- package/skills.mjs +53 -0
- package/task-board.mjs +57 -0
- package/teams.mjs +58 -0
|
@@ -0,0 +1,739 @@
|
|
|
1
|
+
/* DSH Remote 手机端语音输入 · 零依赖
|
|
2
|
+
* 链路:getUserMedia + MediaRecorder 录音 → decodeAudioData → OfflineAudioContext(1ch, 16kHz)
|
|
3
|
+
* 重采样 → PCM16 → 44 字节标准 WAV → 规范 base64 → speech.transcribe RPC
|
|
4
|
+
* → 文本插入 #composer-input 光标处(不自动发送,派发 input 让发送按钮状态刷新)。
|
|
5
|
+
* DSH 宿主对音频强校验:16 kHz / 单声道 / PCM16 / 标准 WAV 头 / 无换行规范 base64,
|
|
6
|
+
* 浏览器给的录音容器(webm/opus 等)必须前端自行重采样后重新编码,服务端不做转换。
|
|
7
|
+
* 入口可见性:安全上下文 + 具备录音能力 + speech.catalog 成功。
|
|
8
|
+
* 手机浏览器直连 http://局域网IP:8787 不是安全上下文(navigator.mediaDevices 通常不存在),
|
|
9
|
+
* 此时隐藏麦克风,避免点了没反应;安卓 App 内页面是 http://localhost,属可信来源。 */
|
|
10
|
+
;(function (root) {
|
|
11
|
+
'use strict'
|
|
12
|
+
|
|
13
|
+
/* >>> 纯函数区:tests/voice-input.test.js 用 vm 切片直接执行,勿依赖 window/document */
|
|
14
|
+
const SAMPLE_RATE = 16000
|
|
15
|
+
const BYTES_PER_SECOND = SAMPLE_RATE * 2
|
|
16
|
+
|
|
17
|
+
// 44 字节标准 WAV 头(PCM): RIFF/WAVE/fmt /data,16 kHz、单声道、16 bit。
|
|
18
|
+
// 宿主 validateWave 要求 RIFF size == len-8、data size == len-44 且 data size 为偶数。
|
|
19
|
+
function encodeWav(samples, sampleRate) {
|
|
20
|
+
const rate = Number.isFinite(sampleRate) && sampleRate > 0 ? Math.round(sampleRate) : SAMPLE_RATE
|
|
21
|
+
const total = samples && samples.length ? samples.length : 0
|
|
22
|
+
const dataSize = total * 2
|
|
23
|
+
const buffer = new ArrayBuffer(44 + dataSize)
|
|
24
|
+
const view = new DataView(buffer)
|
|
25
|
+
const writeTag = (offset, text) => {
|
|
26
|
+
for (let i = 0; i < text.length; i++) view.setUint8(offset + i, text.charCodeAt(i))
|
|
27
|
+
}
|
|
28
|
+
writeTag(0, 'RIFF')
|
|
29
|
+
view.setUint32(4, 36 + dataSize, true)
|
|
30
|
+
writeTag(8, 'WAVE')
|
|
31
|
+
writeTag(12, 'fmt ')
|
|
32
|
+
view.setUint32(16, 16, true) // fmt chunk size
|
|
33
|
+
view.setUint16(20, 1, true) // audioFormat = PCM
|
|
34
|
+
view.setUint16(22, 1, true) // channels = 1
|
|
35
|
+
view.setUint32(24, rate, true) // sampleRate
|
|
36
|
+
view.setUint32(28, rate * 2, true) // byteRate
|
|
37
|
+
view.setUint16(32, 2, true) // blockAlign
|
|
38
|
+
view.setUint16(34, 16, true) // bitsPerSample
|
|
39
|
+
writeTag(36, 'data')
|
|
40
|
+
view.setUint32(40, dataSize, true)
|
|
41
|
+
for (let i = 0; i < total; i++) {
|
|
42
|
+
const value = Math.max(-1, Math.min(1, Number(samples[i]) || 0))
|
|
43
|
+
view.setInt16(44 + i * 2, value < 0 ? Math.round(value * 0x8000) : Math.round(value * 0x7fff), true)
|
|
44
|
+
}
|
|
45
|
+
return new Uint8Array(buffer)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 规范 base64:无换行、无 data: 前缀、非 URL-safe。分块避免超大数组 apply 爆栈。
|
|
49
|
+
function toBase64(bytes) {
|
|
50
|
+
const source = bytes || new Uint8Array(0)
|
|
51
|
+
const chunk = 8192
|
|
52
|
+
let binary = ''
|
|
53
|
+
for (let offset = 0; offset < source.length; offset += chunk) {
|
|
54
|
+
binary += String.fromCharCode.apply(null, source.subarray(offset, offset + chunk))
|
|
55
|
+
}
|
|
56
|
+
return btoa(binary)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 仅测试/自检用:把规范 base64 还原成字节。
|
|
60
|
+
function decodeBase64(text) {
|
|
61
|
+
const binary = atob(String(text == null ? '' : text))
|
|
62
|
+
const bytes = new Uint8Array(binary.length)
|
|
63
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i)
|
|
64
|
+
return bytes
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 入口可见性判定:安全上下文、录音能力、speech.catalog 三项全真才显示。
|
|
68
|
+
// hasMediaDevices 传入的是「能否录音」的综合能力(mediaDevices + MediaRecorder + 离线渲染)。
|
|
69
|
+
function shouldShowMic(flags) {
|
|
70
|
+
const value = flags || {}
|
|
71
|
+
return value.secure === true && value.hasMediaDevices === true && value.catalogOk === true
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 在光标处插入文本并保留原有内容,返回插入后的光标位置。
|
|
75
|
+
function insertAtCursor(el, text) {
|
|
76
|
+
if (!el) return 0
|
|
77
|
+
const value = String(el.value == null ? '' : el.value)
|
|
78
|
+
const start = Number.isInteger(el.selectionStart) && el.selectionStart >= 0 ? Math.min(el.selectionStart, value.length) : value.length
|
|
79
|
+
const end = Number.isInteger(el.selectionEnd) && el.selectionEnd >= start ? Math.min(el.selectionEnd, value.length) : start
|
|
80
|
+
const inserted = String(text == null ? '' : text)
|
|
81
|
+
const before = value.slice(0, start)
|
|
82
|
+
const caret = before.length + inserted.length
|
|
83
|
+
el.value = before + inserted + value.slice(end)
|
|
84
|
+
if (typeof el.setSelectionRange === 'function') {
|
|
85
|
+
try { el.setSelectionRange(caret, caret) } catch (_) {}
|
|
86
|
+
}
|
|
87
|
+
return caret
|
|
88
|
+
}
|
|
89
|
+
/* <<< 纯函数区 */
|
|
90
|
+
|
|
91
|
+
const DEFAULT_MAX_DURATION_SECONDS = 120
|
|
92
|
+
const DEFAULT_MAX_AUDIO_BYTES = 4 * 1024 * 1024
|
|
93
|
+
const PREPARE_POLL_MS = 2000
|
|
94
|
+
const PREPARE_BACKOFF_MS = 5000
|
|
95
|
+
const PREPARE_MAX_MS = 30 * 60 * 1000
|
|
96
|
+
const PREPARE_RETRY_DELAYS = [5000, 20000, 60000]
|
|
97
|
+
const USABLE_PHASES = ['ready', 'standby', 'waking']
|
|
98
|
+
|
|
99
|
+
let host = null
|
|
100
|
+
let catalog = null
|
|
101
|
+
let catalogState = 'idle' // idle | loading | ok | error | unavailable
|
|
102
|
+
let probeStamp = 0
|
|
103
|
+
let probeTimer = null
|
|
104
|
+
let probeFailures = 0
|
|
105
|
+
let mode = 'idle' // idle | recording | transcribing | preparing
|
|
106
|
+
let session = null
|
|
107
|
+
let barEls = null
|
|
108
|
+
let barAction = null
|
|
109
|
+
let prepareProviderId = ''
|
|
110
|
+
let pollTimer = null
|
|
111
|
+
let pollStartedAt = 0
|
|
112
|
+
let hideBarTimer = null
|
|
113
|
+
const ABORTED = { voiceAbort: true }
|
|
114
|
+
|
|
115
|
+
function t(key, vars) {
|
|
116
|
+
if (host && typeof host.t === 'function') return host.t(key, vars)
|
|
117
|
+
return key
|
|
118
|
+
}
|
|
119
|
+
function toast(text, kind) {
|
|
120
|
+
if (host && typeof host.toast === 'function') host.toast(text, kind || '')
|
|
121
|
+
}
|
|
122
|
+
function el(id) {
|
|
123
|
+
if (host && typeof host.$ === 'function') {
|
|
124
|
+
const node = host.$(id)
|
|
125
|
+
if (node) return node
|
|
126
|
+
}
|
|
127
|
+
return typeof document === 'undefined' ? null : document.getElementById(id)
|
|
128
|
+
}
|
|
129
|
+
const messageOf = error => (error && error.message ? String(error.message) : '')
|
|
130
|
+
const reasonOf = error => (error && error.details && error.details.reason ? String(error.details.reason) : messageOf(error))
|
|
131
|
+
|
|
132
|
+
/* ---------------- catalog / 能力探测 ---------------- */
|
|
133
|
+
function normalizeCatalog(value) {
|
|
134
|
+
const raw = value && typeof value === 'object' ? value : {}
|
|
135
|
+
const providers = Array.isArray(raw.providers) ? raw.providers.filter(item => item && typeof item.id === 'string' && item.id) : []
|
|
136
|
+
const selection = raw.selection && typeof raw.selection === 'object' ? raw.selection : {}
|
|
137
|
+
const providerId = typeof selection.providerId === 'string' && selection.providerId ? selection.providerId : (providers[0] ? providers[0].id : '')
|
|
138
|
+
const maxDurationSeconds = Number(raw.maxDurationSeconds)
|
|
139
|
+
const maxAudioBytes = Number(raw.maxAudioBytes)
|
|
140
|
+
return {
|
|
141
|
+
providers,
|
|
142
|
+
providerId,
|
|
143
|
+
language: typeof selection.language === 'string' ? selection.language : '',
|
|
144
|
+
maxDurationSeconds: Number.isFinite(maxDurationSeconds) && maxDurationSeconds > 0 ? maxDurationSeconds : DEFAULT_MAX_DURATION_SECONDS,
|
|
145
|
+
maxAudioBytes: Number.isFinite(maxAudioBytes) && maxAudioBytes > 64 ? maxAudioBytes : DEFAULT_MAX_AUDIO_BYTES
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function providerById(id) {
|
|
149
|
+
if (!catalog || !id) return null
|
|
150
|
+
return catalog.providers.find(item => item.id === id) || null
|
|
151
|
+
}
|
|
152
|
+
function selectedProvider() {
|
|
153
|
+
if (!catalog) return null
|
|
154
|
+
return providerById(catalog.providerId) || catalog.providers[0] || null
|
|
155
|
+
}
|
|
156
|
+
function phaseUsable(phase) { return USABLE_PHASES.indexOf(String(phase || '')) >= 0 }
|
|
157
|
+
function canCaptureAudio() {
|
|
158
|
+
const navigatorRef = root.navigator || {}
|
|
159
|
+
const media = !!(navigatorRef.mediaDevices && typeof navigatorRef.mediaDevices.getUserMedia === 'function')
|
|
160
|
+
const recorder = typeof root.MediaRecorder === 'function'
|
|
161
|
+
const offline = typeof root.OfflineAudioContext === 'function' || typeof root.webkitOfflineAudioContext === 'function'
|
|
162
|
+
return media && recorder && offline
|
|
163
|
+
}
|
|
164
|
+
function effectiveLimitSeconds() {
|
|
165
|
+
const byTime = catalog ? catalog.maxDurationSeconds : DEFAULT_MAX_DURATION_SECONDS
|
|
166
|
+
const byBytes = catalog && catalog.maxAudioBytes > 44 ? Math.floor((catalog.maxAudioBytes - 44) / BYTES_PER_SECOND) : byTime
|
|
167
|
+
return Math.max(1, Math.min(byTime, byBytes))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function scheduleProbeRetry() {
|
|
171
|
+
if (probeFailures >= PREPARE_RETRY_DELAYS.length) return
|
|
172
|
+
const delay = PREPARE_RETRY_DELAYS[probeFailures]
|
|
173
|
+
probeFailures++
|
|
174
|
+
root.clearTimeout(probeTimer)
|
|
175
|
+
probeTimer = root.setTimeout(() => { probeTimer = null; void probeCatalog() }, delay)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 能力探测失败一律静默:catalog 不存在(老网关/未启用语音 bundle)就隐藏入口,不刷错误。
|
|
179
|
+
async function probeCatalog() {
|
|
180
|
+
if (!host || typeof host.rpc !== 'function') return null
|
|
181
|
+
if (catalogState === 'unavailable') return null
|
|
182
|
+
if (host.token && !host.token()) { catalogState = 'idle'; renderMic(); return null }
|
|
183
|
+
const stamp = ++probeStamp
|
|
184
|
+
catalogState = 'loading'
|
|
185
|
+
try {
|
|
186
|
+
const value = await host.rpc('speech.catalog', {}, 15000)
|
|
187
|
+
if (stamp !== probeStamp) return catalog
|
|
188
|
+
catalog = normalizeCatalog(value)
|
|
189
|
+
catalogState = 'ok'
|
|
190
|
+
probeFailures = 0
|
|
191
|
+
renderMic()
|
|
192
|
+
return catalog
|
|
193
|
+
} catch (error) {
|
|
194
|
+
if (stamp !== probeStamp) return catalog
|
|
195
|
+
catalog = null
|
|
196
|
+
const message = messageOf(error)
|
|
197
|
+
if (message === 'AUTH') { catalogState = 'idle'; renderMic(); return null }
|
|
198
|
+
if ((error && error.code === 'speech/unavailable') || /HTTP 404/.test(message)) {
|
|
199
|
+
catalogState = 'unavailable'
|
|
200
|
+
renderMic()
|
|
201
|
+
return null
|
|
202
|
+
}
|
|
203
|
+
catalogState = 'error'
|
|
204
|
+
renderMic()
|
|
205
|
+
scheduleProbeRetry()
|
|
206
|
+
return null
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function markUnavailable() {
|
|
211
|
+
probeStamp++
|
|
212
|
+
catalogState = 'unavailable'
|
|
213
|
+
catalog = null
|
|
214
|
+
mode = 'idle'
|
|
215
|
+
stopPreparationPolling()
|
|
216
|
+
hideBar()
|
|
217
|
+
renderMic()
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* ---------------- 麦克风按钮 ---------------- */
|
|
221
|
+
function renderMic() {
|
|
222
|
+
const button = el('btn-mic')
|
|
223
|
+
if (!button) return
|
|
224
|
+
const visible = shouldShowMic({
|
|
225
|
+
secure: root.isSecureContext === true,
|
|
226
|
+
hasMediaDevices: canCaptureAudio(),
|
|
227
|
+
catalogOk: catalogState === 'ok' && !!catalog
|
|
228
|
+
})
|
|
229
|
+
button.classList.toggle('hidden', !visible)
|
|
230
|
+
button.classList.toggle('recording', mode === 'recording')
|
|
231
|
+
button.disabled = mode === 'starting' || mode === 'transcribing' || mode === 'preparing'
|
|
232
|
+
button.setAttribute('aria-pressed', mode === 'recording' ? 'true' : 'false')
|
|
233
|
+
const label = t(mode === 'recording' ? 'voice.stop' : 'voice.start')
|
|
234
|
+
button.title = label
|
|
235
|
+
button.setAttribute('aria-label', label)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/* ---------------- 内联提示行(录音/转写/模型准备) ---------------- */
|
|
239
|
+
function ensureBar() {
|
|
240
|
+
if (barEls && barEls.bar.isConnected) return barEls
|
|
241
|
+
const wrap = el('composer-wrap')
|
|
242
|
+
const composer = wrap && wrap.querySelector ? wrap.querySelector('.composer') : null
|
|
243
|
+
if (!wrap || !composer || typeof document === 'undefined') return null
|
|
244
|
+
const bar = document.createElement('div')
|
|
245
|
+
bar.id = 'voice-input-bar'
|
|
246
|
+
bar.className = 'voice-input-bar hidden'
|
|
247
|
+
bar.setAttribute('role', 'status')
|
|
248
|
+
bar.setAttribute('aria-live', 'polite')
|
|
249
|
+
const dot = document.createElement('span')
|
|
250
|
+
dot.className = 'voice-input-dot'
|
|
251
|
+
dot.setAttribute('aria-hidden', 'true')
|
|
252
|
+
const text = document.createElement('span')
|
|
253
|
+
text.className = 'voice-input-text'
|
|
254
|
+
const action = document.createElement('button')
|
|
255
|
+
action.id = 'voice-input-action'
|
|
256
|
+
action.className = 'voice-input-action hidden'
|
|
257
|
+
action.type = 'button'
|
|
258
|
+
action.addEventListener('click', () => { if (typeof barAction === 'function') void barAction() })
|
|
259
|
+
bar.appendChild(dot)
|
|
260
|
+
bar.appendChild(text)
|
|
261
|
+
bar.appendChild(action)
|
|
262
|
+
wrap.insertBefore(bar, composer)
|
|
263
|
+
barEls = { bar, dot, text, action }
|
|
264
|
+
return barEls
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function setBar(text, options) {
|
|
268
|
+
const ui = ensureBar()
|
|
269
|
+
if (!ui) return
|
|
270
|
+
const settings = options || {}
|
|
271
|
+
root.clearTimeout(hideBarTimer)
|
|
272
|
+
hideBarTimer = null
|
|
273
|
+
ui.text.textContent = text
|
|
274
|
+
ui.bar.classList.toggle('recording', settings.recording === true)
|
|
275
|
+
ui.bar.classList.toggle('error', settings.error === true)
|
|
276
|
+
barAction = typeof settings.action === 'function' ? settings.action : null
|
|
277
|
+
if (barAction) {
|
|
278
|
+
ui.action.textContent = settings.actionLabel || ''
|
|
279
|
+
ui.action.classList.remove('hidden')
|
|
280
|
+
} else {
|
|
281
|
+
ui.action.textContent = ''
|
|
282
|
+
ui.action.classList.add('hidden')
|
|
283
|
+
}
|
|
284
|
+
ui.bar.classList.remove('hidden')
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function hideBar() {
|
|
288
|
+
root.clearTimeout(hideBarTimer)
|
|
289
|
+
hideBarTimer = null
|
|
290
|
+
barAction = null
|
|
291
|
+
if (barEls && barEls.bar) {
|
|
292
|
+
barEls.bar.classList.add('hidden')
|
|
293
|
+
barEls.bar.classList.remove('recording', 'error')
|
|
294
|
+
barEls.text.textContent = ''
|
|
295
|
+
barEls.action.classList.add('hidden')
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function hideBarLater(delay) {
|
|
300
|
+
root.clearTimeout(hideBarTimer)
|
|
301
|
+
hideBarTimer = root.setTimeout(() => { hideBarTimer = null; hideBar() }, delay)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/* ---------------- 录音 ---------------- */
|
|
305
|
+
function formatClock(totalSeconds) {
|
|
306
|
+
const seconds = Math.max(0, Math.floor(Number(totalSeconds) || 0))
|
|
307
|
+
return Math.floor(seconds / 60) + ':' + String(seconds % 60).padStart(2, '0')
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async function toggleRecording() {
|
|
311
|
+
if (mode === 'recording') { stopRecording(false); return }
|
|
312
|
+
if (mode !== 'idle') return
|
|
313
|
+
await startRecording()
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
async function startRecording() {
|
|
317
|
+
if (mode !== 'idle') return
|
|
318
|
+
if (!catalog) {
|
|
319
|
+
const next = await probeCatalog()
|
|
320
|
+
if (!next) return
|
|
321
|
+
}
|
|
322
|
+
const provider = selectedProvider()
|
|
323
|
+
if (!provider || !phaseUsable(provider.preparation && provider.preparation.phase)) {
|
|
324
|
+
showPreparation(provider)
|
|
325
|
+
return
|
|
326
|
+
}
|
|
327
|
+
if (!canCaptureAudio()) { toast(t('voice.notSupported'), 'err'); return }
|
|
328
|
+
// starting 期间按钮禁用:授权弹窗前连点两次会拿到两条流
|
|
329
|
+
mode = 'starting'
|
|
330
|
+
renderMic()
|
|
331
|
+
let stream = null
|
|
332
|
+
try {
|
|
333
|
+
stream = await root.navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true }, video: false })
|
|
334
|
+
} catch (error) {
|
|
335
|
+
mode = 'idle'
|
|
336
|
+
renderMic()
|
|
337
|
+
const denied = error && (error.name === 'NotAllowedError' || error.name === 'SecurityError')
|
|
338
|
+
toast(denied ? t('voice.permissionDenied') : t('voice.failed', { msg: messageOf(error) }), 'err')
|
|
339
|
+
return
|
|
340
|
+
}
|
|
341
|
+
// 授权期间换了连接(reset()):放弃这条流
|
|
342
|
+
if (mode !== 'starting') { releaseStream(stream); return }
|
|
343
|
+
let recorder = null
|
|
344
|
+
try {
|
|
345
|
+
recorder = new root.MediaRecorder(stream)
|
|
346
|
+
} catch (error) {
|
|
347
|
+
mode = 'idle'
|
|
348
|
+
renderMic()
|
|
349
|
+
releaseStream(stream)
|
|
350
|
+
toast(t('voice.failed', { msg: messageOf(error) }), 'err')
|
|
351
|
+
return
|
|
352
|
+
}
|
|
353
|
+
session = { recorder, stream, chunks: [], startedAt: root.Date.now(), discard: false, timer: null }
|
|
354
|
+
recorder.addEventListener('dataavailable', event => {
|
|
355
|
+
if (session && event.data && event.data.size) session.chunks.push(event.data)
|
|
356
|
+
})
|
|
357
|
+
recorder.addEventListener('stop', () => { void finishRecording() })
|
|
358
|
+
recorder.addEventListener('error', event => {
|
|
359
|
+
const error = (event && event.error) || new Error('MediaRecorder')
|
|
360
|
+
if (session) session.failed = error
|
|
361
|
+
void finishRecording()
|
|
362
|
+
})
|
|
363
|
+
try {
|
|
364
|
+
recorder.start()
|
|
365
|
+
} catch (error) {
|
|
366
|
+
mode = 'idle'
|
|
367
|
+
renderMic()
|
|
368
|
+
releaseStream(stream)
|
|
369
|
+
session = null
|
|
370
|
+
toast(t('voice.failed', { msg: messageOf(error) }), 'err')
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
mode = 'recording'
|
|
374
|
+
renderMic()
|
|
375
|
+
paintRecording()
|
|
376
|
+
session.timer = root.setInterval(tickRecording, 250)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function paintRecording() {
|
|
380
|
+
if (!session) return
|
|
381
|
+
const elapsed = (root.Date.now() - session.startedAt) / 1000
|
|
382
|
+
setBar(t('voice.recording', { time: formatClock(elapsed), limit: formatClock(effectiveLimitSeconds()) }), {
|
|
383
|
+
recording: true,
|
|
384
|
+
action: () => stopRecording(true),
|
|
385
|
+
actionLabel: t('voice.cancel')
|
|
386
|
+
})
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function tickRecording() {
|
|
390
|
+
if (!session || mode !== 'recording') return
|
|
391
|
+
if ((root.Date.now() - session.startedAt) / 1000 >= effectiveLimitSeconds()) { stopRecording(false); return }
|
|
392
|
+
paintRecording()
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function stopRecording(discard) {
|
|
396
|
+
if (!session || mode !== 'recording') return
|
|
397
|
+
session.discard = discard === true
|
|
398
|
+
mode = 'transcribing'
|
|
399
|
+
if (session.timer) { root.clearInterval(session.timer); session.timer = null }
|
|
400
|
+
renderMic()
|
|
401
|
+
if (session.discard) hideBar()
|
|
402
|
+
else setBar(t('voice.transcribing'), {})
|
|
403
|
+
try {
|
|
404
|
+
if (session.recorder.state !== 'inactive') session.recorder.stop()
|
|
405
|
+
else void finishRecording()
|
|
406
|
+
} catch (_) {
|
|
407
|
+
void finishRecording()
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function releaseStream(stream) {
|
|
412
|
+
try {
|
|
413
|
+
const tracks = stream && stream.getTracks ? stream.getTracks() : []
|
|
414
|
+
tracks.forEach(track => { try { track.stop() } catch (_) {} })
|
|
415
|
+
} catch (_) {}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function abortRecording() {
|
|
419
|
+
if (!session) return
|
|
420
|
+
const current = session
|
|
421
|
+
session = null
|
|
422
|
+
if (current.timer) root.clearInterval(current.timer)
|
|
423
|
+
current.discard = true
|
|
424
|
+
try { if (current.recorder.state !== 'inactive') current.recorder.stop() } catch (_) {}
|
|
425
|
+
releaseStream(current.stream)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
async function finishRecording() {
|
|
429
|
+
const current = session
|
|
430
|
+
if (!current) return
|
|
431
|
+
session = null
|
|
432
|
+
releaseStream(current.stream)
|
|
433
|
+
mode = current.discard ? 'idle' : 'transcribing'
|
|
434
|
+
renderMic()
|
|
435
|
+
if (current.discard) { hideBar(); return }
|
|
436
|
+
if (current.failed) {
|
|
437
|
+
mode = 'idle'
|
|
438
|
+
renderMic()
|
|
439
|
+
hideBar()
|
|
440
|
+
toast(t('voice.failed', { msg: messageOf(current.failed) }), 'err')
|
|
441
|
+
return
|
|
442
|
+
}
|
|
443
|
+
const blob = current.chunks.length ? new Blob(current.chunks, { type: (current.recorder && current.recorder.mimeType) || '' }) : null
|
|
444
|
+
if (!blob || !blob.size) {
|
|
445
|
+
mode = 'idle'
|
|
446
|
+
renderMic()
|
|
447
|
+
hideBar()
|
|
448
|
+
toast(t('voice.empty'), 'err')
|
|
449
|
+
return
|
|
450
|
+
}
|
|
451
|
+
const connectionAtStart = connectionId()
|
|
452
|
+
try {
|
|
453
|
+
await transcribe(blob)
|
|
454
|
+
} catch (error) {
|
|
455
|
+
hideBar()
|
|
456
|
+
mode = 'idle'
|
|
457
|
+
renderMic()
|
|
458
|
+
// 换过服务器/令牌时旧请求的结果与错误都不再适用
|
|
459
|
+
if (error !== ABORTED && connectionId() === connectionAtStart) handleTranscribeError(error)
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
hideBar()
|
|
463
|
+
mode = 'idle'
|
|
464
|
+
renderMic()
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function connectionId() {
|
|
468
|
+
return host && typeof host.server === 'function' ? String(host.server() || '') : ''
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/* ---------------- 重采样 + 编码 + 转写 ---------------- */
|
|
472
|
+
function decodeAudioData(AudioCtx, arrayBuffer) {
|
|
473
|
+
const context = new AudioCtx()
|
|
474
|
+
return new Promise((resolve, reject) => {
|
|
475
|
+
let settled = false
|
|
476
|
+
const done = value => {
|
|
477
|
+
if (settled) return
|
|
478
|
+
settled = true
|
|
479
|
+
try { if (context.close) void context.close() } catch (_) {}
|
|
480
|
+
resolve(value)
|
|
481
|
+
}
|
|
482
|
+
const fail = error => {
|
|
483
|
+
if (settled) return
|
|
484
|
+
settled = true
|
|
485
|
+
try { if (context.close) void context.close() } catch (_) {}
|
|
486
|
+
reject(error instanceof Error ? error : new Error(messageOf(error) || 'decodeAudioData'))
|
|
487
|
+
}
|
|
488
|
+
let pending = null
|
|
489
|
+
try {
|
|
490
|
+
pending = context.decodeAudioData(arrayBuffer, done, fail)
|
|
491
|
+
} catch (error) {
|
|
492
|
+
fail(error)
|
|
493
|
+
return
|
|
494
|
+
}
|
|
495
|
+
if (pending && typeof pending.then === 'function') pending.then(done, fail)
|
|
496
|
+
})
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
async function renderWav(blob) {
|
|
500
|
+
const AudioCtx = root.AudioContext || root.webkitAudioContext
|
|
501
|
+
const Offline = root.OfflineAudioContext || root.webkitOfflineAudioContext
|
|
502
|
+
if (!AudioCtx || !Offline) throw new Error(t('voice.notSupported'))
|
|
503
|
+
const decoded = await decodeAudioData(AudioCtx, await blob.arrayBuffer())
|
|
504
|
+
const duration = Math.min(Number(decoded && decoded.duration) || 0, effectiveLimitSeconds())
|
|
505
|
+
if (!(duration > 0)) throw new Error(t('voice.empty'))
|
|
506
|
+
const frames = Math.max(1, Math.floor(duration * SAMPLE_RATE))
|
|
507
|
+
const offline = new Offline(1, frames, SAMPLE_RATE)
|
|
508
|
+
const source = offline.createBufferSource()
|
|
509
|
+
source.buffer = decoded
|
|
510
|
+
source.connect(offline.destination)
|
|
511
|
+
source.start(0)
|
|
512
|
+
const rendered = await offline.startRendering()
|
|
513
|
+
return encodeWav(rendered.getChannelData(0), SAMPLE_RATE)
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function canonicalBase64(value) {
|
|
517
|
+
const text = String(value == null ? '' : value)
|
|
518
|
+
if (!text || text.length % 4 !== 0) return false
|
|
519
|
+
return /^[A-Za-z0-9+/]+={0,2}$/.test(text)
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
async function transcribe(blob) {
|
|
523
|
+
const connectionAtStart = connectionId()
|
|
524
|
+
const wav = await renderWav(blob)
|
|
525
|
+
const maxBytes = catalog ? catalog.maxAudioBytes : 0
|
|
526
|
+
if (maxBytes > 0 && wav.length > maxBytes) { toast(t('voice.tooLarge'), 'err'); throw ABORTED }
|
|
527
|
+
const audioBase64 = toBase64(wav)
|
|
528
|
+
if (!canonicalBase64(audioBase64)) { toast(t('voice.encodingFailed'), 'err'); throw ABORTED }
|
|
529
|
+
const providerId = catalog ? catalog.providerId : ''
|
|
530
|
+
const result = await host.rpc('speech.transcribe', {
|
|
531
|
+
request: { audioBase64, ...(providerId ? { providerId } : {}) }
|
|
532
|
+
}, 180000)
|
|
533
|
+
const text = String(result && result.text != null ? result.text : '').trim()
|
|
534
|
+
if (!text) { toast(t('voice.empty'), 'err'); throw ABORTED }
|
|
535
|
+
// 转写期间切换了服务器:文本不属于当前草稿,丢弃而不是插入
|
|
536
|
+
if (connectionId() !== connectionAtStart) throw ABORTED
|
|
537
|
+
insertTranscript(text)
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// 只插入文本,不自动发送:保留原有内容与焦点,派发 input 让发送按钮状态刷新。
|
|
541
|
+
function insertTranscript(text) {
|
|
542
|
+
const input = el('composer-input')
|
|
543
|
+
if (!input) return
|
|
544
|
+
const caret = insertAtCursor(input, text)
|
|
545
|
+
try { input.focus() } catch (_) {}
|
|
546
|
+
if (typeof input.setSelectionRange === 'function') {
|
|
547
|
+
try { input.setSelectionRange(caret, caret) } catch (_) {}
|
|
548
|
+
}
|
|
549
|
+
try { if (typeof root.Event === 'function') input.dispatchEvent(new root.Event('input', { bubbles: true })) } catch (_) {}
|
|
550
|
+
if (host && typeof host.autosize === 'function') { try { host.autosize(input) } catch (_) {} }
|
|
551
|
+
try { input.scrollTop = input.scrollHeight } catch (_) {}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function handleTranscribeError(error) {
|
|
555
|
+
const message = messageOf(error)
|
|
556
|
+
const reason = reasonOf(error)
|
|
557
|
+
if (message === 'AUTH') { if (host && host.authFailure) host.authFailure(); return }
|
|
558
|
+
if (error && error.code === 'speech/unavailable') { markUnavailable(); return }
|
|
559
|
+
// 主机模型未准备:刷新一次 catalog 后直接进入准备流程,而不是只报错。
|
|
560
|
+
if (/Prepare the local speech provider/i.test(reason)) {
|
|
561
|
+
void probeCatalog().then(() => showPreparation(selectedProvider()))
|
|
562
|
+
return
|
|
563
|
+
}
|
|
564
|
+
toast(t('voice.failed', { msg: reason || t('err.dshError') }), 'err')
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/* ---------------- 模型准备(下载/加载) ---------------- */
|
|
568
|
+
function showPreparation(provider) {
|
|
569
|
+
prepareProviderId = provider && provider.id ? provider.id : (catalog ? catalog.providerId : '')
|
|
570
|
+
if (!prepareProviderId) {
|
|
571
|
+
setBar(t('voice.notReady'), {})
|
|
572
|
+
return
|
|
573
|
+
}
|
|
574
|
+
const preparation = (provider && provider.preparation) || {}
|
|
575
|
+
const phase = String(preparation.phase || '')
|
|
576
|
+
if (phase === 'failed') { paintPreparation(provider); return }
|
|
577
|
+
if (phase && !phaseUsable(phase) && phase !== 'unprepared' && phase !== 'cancelled') {
|
|
578
|
+
startPreparationPolling()
|
|
579
|
+
paintPreparation(provider)
|
|
580
|
+
return
|
|
581
|
+
}
|
|
582
|
+
setBar(t('voice.notReady'), { action: beginPreparation, actionLabel: t('voice.prepare') })
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
async function beginPreparation() {
|
|
586
|
+
if (mode === 'preparing') return
|
|
587
|
+
const providerId = prepareProviderId || (catalog ? catalog.providerId : '')
|
|
588
|
+
if (!providerId) { toast(t('voice.unavailable'), 'err'); return }
|
|
589
|
+
prepareProviderId = providerId
|
|
590
|
+
mode = 'preparing'
|
|
591
|
+
renderMic()
|
|
592
|
+
setBar(t('voice.preparing'), {})
|
|
593
|
+
try {
|
|
594
|
+
await host.rpc('speech.prepare', { providerId }, 30000)
|
|
595
|
+
} catch (error) {
|
|
596
|
+
mode = 'idle'
|
|
597
|
+
renderMic()
|
|
598
|
+
handlePrepareError(error)
|
|
599
|
+
return
|
|
600
|
+
}
|
|
601
|
+
startPreparationPolling()
|
|
602
|
+
// 缓存里的 preparation 可能还是旧的 unprepared,先显示准备中,等第一次轮询出百分比
|
|
603
|
+
setBar(t('voice.preparing'), { action: cancelPreparation, actionLabel: t('voice.cancel') })
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function startPreparationPolling() {
|
|
607
|
+
if (pollTimer) return
|
|
608
|
+
mode = 'preparing'
|
|
609
|
+
pollStartedAt = root.Date.now()
|
|
610
|
+
renderMic()
|
|
611
|
+
schedulePreparationPoll(PREPARE_POLL_MS)
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function stopPreparationPolling() {
|
|
615
|
+
root.clearTimeout(pollTimer)
|
|
616
|
+
pollTimer = null
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
function schedulePreparationPoll(delay) {
|
|
620
|
+
root.clearTimeout(pollTimer)
|
|
621
|
+
pollTimer = root.setTimeout(() => { pollTimer = null; void pollPreparation() }, delay)
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// 轮询 catalog 拿准备进度:正常 2s,失败退避 5s,最长 30 分钟;用户可取消。
|
|
625
|
+
async function pollPreparation() {
|
|
626
|
+
if (mode !== 'preparing') return
|
|
627
|
+
if (root.Date.now() - pollStartedAt > PREPARE_MAX_MS) {
|
|
628
|
+
mode = 'idle'
|
|
629
|
+
renderMic()
|
|
630
|
+
setBar(t('voice.prepareTimeout'), { error: true, action: prepareProviderId ? beginPreparation : null, actionLabel: t('voice.retry') })
|
|
631
|
+
return
|
|
632
|
+
}
|
|
633
|
+
let next = null
|
|
634
|
+
try {
|
|
635
|
+
next = await host.rpc('speech.catalog', {}, 20000)
|
|
636
|
+
} catch (error) {
|
|
637
|
+
if (messageOf(error) === 'AUTH') { mode = 'idle'; renderMic(); hideBar(); if (host.authFailure) host.authFailure(); return }
|
|
638
|
+
if (error && error.code === 'speech/unavailable') { markUnavailable(); return }
|
|
639
|
+
schedulePreparationPoll(PREPARE_BACKOFF_MS)
|
|
640
|
+
return
|
|
641
|
+
}
|
|
642
|
+
catalog = normalizeCatalog(next)
|
|
643
|
+
const provider = providerById(prepareProviderId) || selectedProvider()
|
|
644
|
+
const status = paintPreparation(provider)
|
|
645
|
+
if (status === 'ready') { mode = 'idle'; renderMic(); hideBarLater(2600); return }
|
|
646
|
+
if (status === 'failed' || status === 'cancelled') { mode = 'idle'; renderMic(); return }
|
|
647
|
+
schedulePreparationPoll(PREPARE_POLL_MS)
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function paintPreparation(provider) {
|
|
651
|
+
const preparation = (provider && provider.preparation) || {}
|
|
652
|
+
const phase = String(preparation.phase || '')
|
|
653
|
+
if (phaseUsable(phase)) { setBar(t('voice.prepareReady'), {}); return 'ready' }
|
|
654
|
+
if (phase === 'failed') {
|
|
655
|
+
const reason = preparation.message || (preparation.download && preparation.download.reason) || ''
|
|
656
|
+
setBar(t('voice.prepareFailed', { msg: reason || t('err.dshError') }), {
|
|
657
|
+
error: true, action: beginPreparation, actionLabel: t('voice.retry')
|
|
658
|
+
})
|
|
659
|
+
return 'failed'
|
|
660
|
+
}
|
|
661
|
+
if (!phase || phase === 'unprepared' || phase === 'cancelled') {
|
|
662
|
+
setBar(t('voice.notReady'), { action: beginPreparation, actionLabel: t('voice.prepare') })
|
|
663
|
+
return phase ? 'cancelled' : 'waiting'
|
|
664
|
+
}
|
|
665
|
+
const total = Number(preparation.totalBytes)
|
|
666
|
+
const completed = Number(preparation.completedBytes)
|
|
667
|
+
const percent = Number.isFinite(total) && total > 0 && Number.isFinite(completed)
|
|
668
|
+
? Math.max(0, Math.min(100, Math.floor(completed / total * 100))) : null
|
|
669
|
+
setBar(percent == null ? t('voice.preparing') : t('voice.prepareProgress', { percent }), {
|
|
670
|
+
action: cancelPreparation, actionLabel: t('voice.cancel')
|
|
671
|
+
})
|
|
672
|
+
return 'progress'
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function handlePrepareError(error) {
|
|
676
|
+
const message = messageOf(error)
|
|
677
|
+
if (message === 'AUTH') { if (host.authFailure) host.authFailure(); return }
|
|
678
|
+
if (error && error.code === 'speech/unavailable') { markUnavailable(); return }
|
|
679
|
+
setBar(t('voice.prepareFailed', { msg: reasonOf(error) || t('err.dshError') }), {
|
|
680
|
+
error: true, action: beginPreparation, actionLabel: t('voice.retry')
|
|
681
|
+
})
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
async function cancelPreparation() {
|
|
685
|
+
const providerId = prepareProviderId
|
|
686
|
+
stopPreparationPolling()
|
|
687
|
+
mode = 'idle'
|
|
688
|
+
renderMic()
|
|
689
|
+
setBar(t('voice.notReady'), providerId ? { action: beginPreparation, actionLabel: t('voice.prepare') } : {})
|
|
690
|
+
if (!providerId) return
|
|
691
|
+
try {
|
|
692
|
+
await host.rpc('speech.cancelPreparation', { providerId }, 30000)
|
|
693
|
+
} catch (error) {
|
|
694
|
+
if (messageOf(error) === 'AUTH' && host.authFailure) host.authFailure()
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/* ---------------- 挂载 / 连接切换 ---------------- */
|
|
699
|
+
function mount(api) {
|
|
700
|
+
if (!api || typeof api.rpc !== 'function') return false
|
|
701
|
+
host = api
|
|
702
|
+
mode = 'idle'
|
|
703
|
+
const button = el('btn-mic')
|
|
704
|
+
if (button) {
|
|
705
|
+
button.classList.add('hidden')
|
|
706
|
+
button.addEventListener('click', () => { void toggleRecording() })
|
|
707
|
+
}
|
|
708
|
+
if (typeof document !== 'undefined' && document.addEventListener) {
|
|
709
|
+
document.addEventListener('visibilitychange', () => {
|
|
710
|
+
if (document.hidden && mode === 'recording') stopRecording(false)
|
|
711
|
+
})
|
|
712
|
+
}
|
|
713
|
+
renderMic()
|
|
714
|
+
if (host.token && !host.token()) return true
|
|
715
|
+
void probeCatalog()
|
|
716
|
+
return true
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// 连接身份(地址/令牌)变化:清缓存、停轮询、放弃录音,然后按新连接重新探测。
|
|
720
|
+
function reset() {
|
|
721
|
+
probeStamp++
|
|
722
|
+
catalog = null
|
|
723
|
+
catalogState = 'idle'
|
|
724
|
+
probeFailures = 0
|
|
725
|
+
root.clearTimeout(probeTimer)
|
|
726
|
+
probeTimer = null
|
|
727
|
+
stopPreparationPolling()
|
|
728
|
+
abortRecording()
|
|
729
|
+
mode = 'idle'
|
|
730
|
+
hideBar()
|
|
731
|
+
renderMic()
|
|
732
|
+
if (!host || (host.token && !host.token())) return
|
|
733
|
+
void probeCatalog()
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
const api = { encodeWav, toBase64, decodeBase64, shouldShowMic, insertAtCursor, mount, reset }
|
|
737
|
+
if (typeof module !== 'undefined' && module.exports) module.exports = api
|
|
738
|
+
else root.DshVoiceInput = api
|
|
739
|
+
})(typeof window === 'undefined' ? globalThis : window)
|