@hyprnos/dsh-remote-plugin 0.0.0-stage → 0.8.9

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.
Files changed (211) hide show
  1. package/README.en.md +61 -0
  2. package/README.md +60 -2
  3. package/apk/dsh-remote.apk +0 -0
  4. package/approval-summary.mjs +115 -0
  5. package/client.js +197 -0
  6. package/cordis.patch.yml +5 -0
  7. package/doctor-skill.mjs +99 -0
  8. package/doctor-target.mjs +24 -0
  9. package/file-delivery.mjs +84 -0
  10. package/gateway-stats.cjs +501 -0
  11. package/gateway.cjs +8451 -0
  12. package/index.mjs +1788 -0
  13. package/insights.mjs +111 -0
  14. package/market.mjs +86 -0
  15. package/package.json +73 -4
  16. package/peak-reminders.mjs +50 -0
  17. package/plugin-center.mjs +777 -0
  18. package/plugin-transaction.mjs +300 -0
  19. package/public/activity.js +188 -0
  20. package/public/admin.html +820 -0
  21. package/public/admin.js +1167 -0
  22. package/public/announcement-forms.js +89 -0
  23. package/public/announcements.json +464 -0
  24. package/public/app.js +8826 -0
  25. package/public/approval-records.js +301 -0
  26. package/public/approval-summary.js +78 -0
  27. package/public/build.json +5 -0
  28. package/public/cache-upgrade.js +51 -0
  29. package/public/chat-copy.js +59 -0
  30. package/public/chat-files.js +87 -0
  31. package/public/desktop/desktop.css +816 -0
  32. package/public/desktop/desktop.html +871 -0
  33. package/public/desktop/desktop.js +4548 -0
  34. package/public/desktop/i18n.js +85 -0
  35. package/public/diagnostics.js +203 -0
  36. package/public/doctor-entry.js +94 -0
  37. package/public/doctor.css +205 -0
  38. package/public/doctor.js +769 -0
  39. package/public/donate.png +0 -0
  40. package/public/e2ee-transport.js +219 -0
  41. package/public/extensions.css +109 -0
  42. package/public/extensions.js +215 -0
  43. package/public/file-inbox.css +66 -0
  44. package/public/file-inbox.js +289 -0
  45. package/public/genui.css +8 -0
  46. package/public/genui.js +254 -0
  47. package/public/history.js +304 -0
  48. package/public/i18n.js +57 -0
  49. package/public/icon.svg +12 -0
  50. package/public/image-cache.js +141 -0
  51. package/public/image-preview.js +290 -0
  52. package/public/index.html +1568 -0
  53. package/public/insights.css +28 -0
  54. package/public/insights.js +290 -0
  55. package/public/jsqr.min.js +10104 -0
  56. package/public/lan-discovery.js +94 -0
  57. package/public/link-check.js +109 -0
  58. package/public/manifest.webmanifest +16 -0
  59. package/public/md.js +333 -0
  60. package/public/morphicons-init.js +41 -0
  61. package/public/motion.js +557 -0
  62. package/public/network.js +100 -0
  63. package/public/onboarding.css +36 -0
  64. package/public/onboarding.js +253 -0
  65. package/public/operations.js +141 -0
  66. package/public/permission-presets.js +159 -0
  67. package/public/plugin-center.css +120 -0
  68. package/public/plugin-center.js +666 -0
  69. package/public/plugin-icon.svg +5 -0
  70. package/public/plugin.html +68 -0
  71. package/public/plugin.js +211 -0
  72. package/public/qrcode.min.js +2297 -0
  73. package/public/relay-autopair.js +251 -0
  74. package/public/relay-devices.js +80 -0
  75. package/public/relay-settings.css +24 -0
  76. package/public/relay-settings.js +194 -0
  77. package/public/remote-state.js +27 -0
  78. package/public/remote-ui.css +148 -0
  79. package/public/remote-ui.js +243 -0
  80. package/public/session-deck.css +79 -0
  81. package/public/session-deck.js +180 -0
  82. package/public/session-presets.js +47 -0
  83. package/public/setup.css +18 -0
  84. package/public/setup.js +213 -0
  85. package/public/sha256.js +119 -0
  86. package/public/skills.css +81 -0
  87. package/public/skills.js +202 -0
  88. package/public/skins/blue-fish/DESIGN.md +24 -0
  89. package/public/skins/blue-fish/face.png +0 -0
  90. package/public/skins/blue-fish/icon-blush.png +0 -0
  91. package/public/skins/blue-fish/icon-sleep.png +0 -0
  92. package/public/skins/blue-fish/icon-smile.png +0 -0
  93. package/public/skins/blue-fish/icon-star.png +0 -0
  94. package/public/skins/blue-fish/icon-wink.png +0 -0
  95. package/public/skins/blue-fish/icon.png +0 -0
  96. package/public/skins/blue-fish/mascot.png +0 -0
  97. package/public/skins/blue-fish/pattern.png +0 -0
  98. package/public/skins/blue-fish/preview.html +12 -0
  99. package/public/skins/blue-fish/skin.css +122 -0
  100. package/public/skins/blue-fish/sparkles.svg +18 -0
  101. package/public/skins/bocchi/DESIGN.md +99 -0
  102. package/public/skins/bocchi/face.png +0 -0
  103. package/public/skins/bocchi/grain.png +0 -0
  104. package/public/skins/bocchi/icon-blush.png +0 -0
  105. package/public/skins/bocchi/icon-sleep.png +0 -0
  106. package/public/skins/bocchi/icon-smile.png +0 -0
  107. package/public/skins/bocchi/icon-star.png +0 -0
  108. package/public/skins/bocchi/icon-wink.png +0 -0
  109. package/public/skins/bocchi/icon.png +0 -0
  110. package/public/skins/bocchi/pattern-soft.png +0 -0
  111. package/public/skins/bocchi/pattern.png +0 -0
  112. package/public/skins/bocchi/preview.html +55 -0
  113. package/public/skins/bocchi/skin.css +233 -0
  114. package/public/skins/bocchi/stage-bg.png +0 -0
  115. package/public/skins/bocchi/stage-soft.png +0 -0
  116. package/public/skins/bocchi/stand.png +0 -0
  117. package/public/skins/bocchi/sticker-guitar.png +0 -0
  118. package/public/skins/kessoku/DESIGN.md +38 -0
  119. package/public/skins/kessoku/grain.png +0 -0
  120. package/public/skins/kessoku/skin.css +212 -0
  121. package/public/skins/kita/SOURCES.json +51 -0
  122. package/public/skins/kita/face.png +0 -0
  123. package/public/skins/kita/icon-live.png +0 -0
  124. package/public/skins/kita/icon-reddress.png +0 -0
  125. package/public/skins/kita/icon-seifuku.png +0 -0
  126. package/public/skins/kita/pattern.png +0 -0
  127. package/public/skins/kita/preview.html +56 -0
  128. package/public/skins/kita/skin.css +26 -0
  129. package/public/skins/kita/stand.png +0 -0
  130. package/public/skins/kita/sticker-live.png +0 -0
  131. package/public/skins/nijika/SOURCES.json +51 -0
  132. package/public/skins/nijika/face.png +0 -0
  133. package/public/skins/nijika/icon-live.png +0 -0
  134. package/public/skins/nijika/icon-reddress.png +0 -0
  135. package/public/skins/nijika/icon-seifuku.png +0 -0
  136. package/public/skins/nijika/pattern.png +0 -0
  137. package/public/skins/nijika/preview.html +56 -0
  138. package/public/skins/nijika/skin.css +25 -0
  139. package/public/skins/nijika/stand.png +0 -0
  140. package/public/skins/nijika/sticker-live.png +0 -0
  141. package/public/skins/penguin/DESIGN.md +33 -0
  142. package/public/skins/penguin/SOURCES.json +22 -0
  143. package/public/skins/penguin/aic-blueprint.svg +24 -0
  144. package/public/skins/penguin/endfield-triangles.svg +4 -0
  145. package/public/skins/penguin/face.png +0 -0
  146. package/public/skins/penguin/icon-blush.png +0 -0
  147. package/public/skins/penguin/icon-dizzy.png +0 -0
  148. package/public/skins/penguin/icon-laugh.png +0 -0
  149. package/public/skins/penguin/icon-sleep.png +0 -0
  150. package/public/skins/penguin/icon-smile.png +0 -0
  151. package/public/skins/penguin/icon-star.png +0 -0
  152. package/public/skins/penguin/icon-wink.png +0 -0
  153. package/public/skins/penguin/icon.png +0 -0
  154. package/public/skins/penguin/mascot.png +0 -0
  155. package/public/skins/penguin/pattern-night.png +0 -0
  156. package/public/skins/penguin/pattern.png +0 -0
  157. package/public/skins/penguin/preview.html +12 -0
  158. package/public/skins/penguin/skin.css +212 -0
  159. package/public/skins/ryo/SOURCES.json +51 -0
  160. package/public/skins/ryo/face.png +0 -0
  161. package/public/skins/ryo/icon-live.png +0 -0
  162. package/public/skins/ryo/icon-reddress.png +0 -0
  163. package/public/skins/ryo/icon-seifuku.png +0 -0
  164. package/public/skins/ryo/pattern.png +0 -0
  165. package/public/skins/ryo/preview.html +56 -0
  166. package/public/skins/ryo/skin.css +25 -0
  167. package/public/skins/ryo/stand.png +0 -0
  168. package/public/skins/ryo/sticker-live.png +0 -0
  169. package/public/sponsor-entry.js +21 -0
  170. package/public/styles.css +1526 -0
  171. package/public/subagents.css +61 -0
  172. package/public/subagents.js +275 -0
  173. package/public/task-board.js +109 -0
  174. package/public/teams.js +98 -0
  175. package/public/theme-picker.css +58 -0
  176. package/public/theme-vars.css +1044 -0
  177. package/public/theme.js +166 -0
  178. package/public/transcribe-core.js +74 -0
  179. package/public/turn-results.js +121 -0
  180. package/public/update.json +49 -0
  181. package/public/vendor/gsap/NOTICE.md +10 -0
  182. package/public/vendor/gsap/gsap.min.js +11 -0
  183. package/public/vendor/morphicons/LICENSE +21 -0
  184. package/public/vendor/morphicons/README.md +10 -0
  185. package/public/vendor/morphicons/controller-CXZuwJ_M.js +152 -0
  186. package/public/vendor/morphicons/dom.js +206 -0
  187. package/public/vendor/morphicons/element.js +261 -0
  188. package/public/vendor/morphicons/normalize-CYnN3Npw.js +540 -0
  189. package/public/vendor/morphicons/spring-CFHloqPP.js +623 -0
  190. package/public/version.json +3 -0
  191. package/public/voice-input.js +1151 -0
  192. package/public/workflow.css +365 -0
  193. package/public/workflow.js +419 -0
  194. package/session-file.mjs +39 -0
  195. package/skills/dsh-remote-doctor/SKILL.md +186 -0
  196. package/skills/dsh-remote-doctor/references/wiki/README.md +40 -0
  197. package/skills/dsh-remote-doctor/references/wiki/ai-guide.md +61 -0
  198. package/skills/dsh-remote-doctor/references/wiki/connection.md +76 -0
  199. package/skills/dsh-remote-doctor/references/wiki/errors.md +26 -0
  200. package/skills/dsh-remote-doctor/references/wiki/evidence.md +44 -0
  201. package/skills/dsh-remote-doctor/references/wiki/files.md +31 -0
  202. package/skills/dsh-remote-doctor/references/wiki/maintenance.md +30 -0
  203. package/skills/dsh-remote-doctor/references/wiki/notifications.md +33 -0
  204. package/skills/dsh-remote-doctor/references/wiki/operations.md +32 -0
  205. package/skills/dsh-remote-doctor/references/wiki/plugins.md +90 -0
  206. package/skills/dsh-remote-doctor/references/wiki/sessions.md +45 -0
  207. package/skills/dsh-remote-doctor/references/wiki/sources.md +27 -0
  208. package/skills.mjs +53 -0
  209. package/speech-whisper.mjs +705 -0
  210. package/task-board.mjs +57 -0
  211. package/teams.mjs +58 -0
@@ -0,0 +1,1151 @@
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
+ // 纠错表保存在本机各自一份,格式每行「错误=正确」;从未保存过时才回落到默认条目。
92
+ const CORRECTIONS_KEY = 'dsr.voice.corrections'
93
+ const DEFAULT_CORRECTIONS = ['confit=config', 'servve=server', 'log=logs', 'mcp=MCP'].join('\n')
94
+ const CORRECTIONS_MAX_ENTRIES = 200
95
+ const USABLE_PHASES = ['ready', 'standby', 'waking']
96
+ const IDLE_PHASES = ['', 'unprepared', 'cancelled', 'failed']
97
+
98
+ function phaseUsable(phase) { return USABLE_PHASES.indexOf(String(phase || '')) >= 0 }
99
+
100
+ // 本地文本原样返回(空字符串表示「不纠错」);key 不存在才用默认条目。
101
+ function correctionsText(stored) {
102
+ return typeof stored === 'string' ? stored : DEFAULT_CORRECTIONS
103
+ }
104
+ function escapeRegExp(text) {
105
+ return String(text == null ? '' : text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
106
+ }
107
+ // 解析纠错表:忽略空行、# 注释、缺少 = 的行、左空与错误/正确完全相同的行;重复的「错误」只留第一条。
108
+ // 返回按「错误」长度降序的表,长词先替换,避免短词先把长词切碎。
109
+ function parseCorrections(text) {
110
+ const entries = []
111
+ const seen = new Set()
112
+ for (const raw of String(text == null ? '' : text).split(/\r?\n/)) {
113
+ if (entries.length >= CORRECTIONS_MAX_ENTRIES) break
114
+ const line = raw.trim()
115
+ if (!line || line.charAt(0) === '#') continue
116
+ const at = line.indexOf('=')
117
+ if (at <= 0) continue
118
+ const from = line.slice(0, at).trim()
119
+ const to = line.slice(at + 1).trim()
120
+ if (!from || from === to) continue
121
+ const key = from.toLowerCase()
122
+ if (seen.has(key)) continue
123
+ seen.add(key)
124
+ entries.push({ from, to })
125
+ }
126
+ entries.sort((left, right) => right.from.length - left.from.length)
127
+ return entries
128
+ }
129
+ // 应用纠错表:大小写不敏感、整词匹配(ASCII 单词字符两侧不成词),替换文本保留目标原文。
130
+ // 不用 lookbehind,兼容旧版 iOS Safari;行首行尾算边界,中文相邻文字也算边界。
131
+ function applyCorrections(text, table) {
132
+ let value = String(text == null ? '' : text)
133
+ const entries = Array.isArray(table) ? table : parseCorrections(table)
134
+ for (const entry of entries) {
135
+ if (!entry || !entry.from) continue
136
+ let pattern = null
137
+ try {
138
+ pattern = new RegExp('(^|[^A-Za-z0-9_])(' + escapeRegExp(entry.from) + ')(?![A-Za-z0-9_])', 'gi')
139
+ } catch (_) { continue }
140
+ value = value.replace(pattern, (match, lead, hit) => lead + entry.to)
141
+ }
142
+ return value
143
+ }
144
+ // speech.configure 的 payload 形状固定为 {patch:{providerId}} 或 {patch:{language}}。
145
+ function configureProviderPatch(providerId) {
146
+ const id = String(providerId == null ? '' : providerId).trim()
147
+ return id ? { patch: { providerId: id } } : null
148
+ }
149
+ function configureLanguagePatch(language) {
150
+ const code = String(language == null ? '' : language).trim()
151
+ return code ? { patch: { language: code } } : null
152
+ }
153
+ // 语言候选:provider 自带列表去重,auto(跟随识别/主机默认)固定排第一。
154
+ function voiceLanguages(provider) {
155
+ const out = []
156
+ const list = Array.isArray(provider && provider.languages) ? provider.languages : []
157
+ for (const item of list) {
158
+ const code = typeof item === 'string' ? item.trim() : ''
159
+ if (code && code !== 'auto' && out.indexOf(code) < 0) out.push(code)
160
+ }
161
+ return ['auto'].concat(out)
162
+ }
163
+ // 弹层展示用的准备状态:ready 可用 / busy 下载或加载中 / percent 进度 / failed 失败。
164
+ function providerStatus(provider) {
165
+ const preparation = (provider && provider.preparation) || {}
166
+ const phase = String(preparation.phase || '')
167
+ const total = Number(preparation.totalBytes)
168
+ const completed = Number(preparation.completedBytes)
169
+ const percent = Number.isFinite(total) && total > 0 && Number.isFinite(completed)
170
+ ? Math.max(0, Math.min(100, Math.floor(completed / total * 100))) : null
171
+ const ready = phaseUsable(phase)
172
+ return { phase, ready, busy: !ready && IDLE_PHASES.indexOf(phase) < 0, failed: phase === 'failed', percent }
173
+ }
174
+ /* <<< 纯函数区 */
175
+
176
+ const DEFAULT_MAX_DURATION_SECONDS = 120
177
+ const DEFAULT_MAX_AUDIO_BYTES = 4 * 1024 * 1024
178
+ const PREPARE_POLL_MS = 2000
179
+ const PREPARE_BACKOFF_MS = 5000
180
+ const PREPARE_MAX_MS = 30 * 60 * 1000
181
+ const PREPARE_RETRY_DELAYS = [5000, 20000, 60000]
182
+
183
+ let host = null
184
+ let catalog = null
185
+ let catalogState = 'idle' // idle | loading | ok | error | unavailable
186
+ let probeStamp = 0
187
+ let probeTimer = null
188
+ let probeFailures = 0
189
+ let mode = 'idle' // idle | recording | transcribing | preparing
190
+ let session = null
191
+ let barEls = null
192
+ let barAction = null
193
+ let prepareProviderId = ''
194
+ let pollTimer = null
195
+ let pollStartedAt = 0
196
+ let hideBarTimer = null
197
+ let settingsEls = null
198
+ let settingsOpen = false
199
+ let settingsLoading = false
200
+ let settingsBusy = false
201
+ let settingsError = ''
202
+ let settingsStamp = 0
203
+ const ABORTED = { voiceAbort: true }
204
+
205
+ function t(key, vars) {
206
+ if (host && typeof host.t === 'function') return host.t(key, vars)
207
+ return key
208
+ }
209
+ function toast(text, kind) {
210
+ if (host && typeof host.toast === 'function') host.toast(text, kind || '')
211
+ }
212
+ function el(id) {
213
+ if (host && typeof host.$ === 'function') {
214
+ const node = host.$(id)
215
+ if (node) return node
216
+ }
217
+ return typeof document === 'undefined' ? null : document.getElementById(id)
218
+ }
219
+ const messageOf = error => (error && error.message ? String(error.message) : '')
220
+ const reasonOf = error => (error && error.details && error.details.reason ? String(error.details.reason) : messageOf(error))
221
+
222
+ /* ---------------- catalog / 能力探测 ---------------- */
223
+ function normalizeCatalog(value) {
224
+ const raw = value && typeof value === 'object' ? value : {}
225
+ const providers = Array.isArray(raw.providers) ? raw.providers.filter(item => item && typeof item.id === 'string' && item.id) : []
226
+ const selection = raw.selection && typeof raw.selection === 'object' ? raw.selection : {}
227
+ const providerId = typeof selection.providerId === 'string' && selection.providerId ? selection.providerId : (providers[0] ? providers[0].id : '')
228
+ const maxDurationSeconds = Number(raw.maxDurationSeconds)
229
+ const maxAudioBytes = Number(raw.maxAudioBytes)
230
+ return {
231
+ providers,
232
+ providerId,
233
+ language: typeof selection.language === 'string' ? selection.language : '',
234
+ maxDurationSeconds: Number.isFinite(maxDurationSeconds) && maxDurationSeconds > 0 ? maxDurationSeconds : DEFAULT_MAX_DURATION_SECONDS,
235
+ maxAudioBytes: Number.isFinite(maxAudioBytes) && maxAudioBytes > 64 ? maxAudioBytes : DEFAULT_MAX_AUDIO_BYTES
236
+ }
237
+ }
238
+ function providerById(id) {
239
+ if (!catalog || !id) return null
240
+ return catalog.providers.find(item => item.id === id) || null
241
+ }
242
+ function selectedProvider() {
243
+ if (!catalog) return null
244
+ return providerById(catalog.providerId) || catalog.providers[0] || null
245
+ }
246
+ function canCaptureAudio() {
247
+ const navigatorRef = root.navigator || {}
248
+ const media = !!(navigatorRef.mediaDevices && typeof navigatorRef.mediaDevices.getUserMedia === 'function')
249
+ const recorder = typeof root.MediaRecorder === 'function'
250
+ const offline = typeof root.OfflineAudioContext === 'function' || typeof root.webkitOfflineAudioContext === 'function'
251
+ return media && recorder && offline
252
+ }
253
+ function effectiveLimitSeconds() {
254
+ const byTime = catalog ? catalog.maxDurationSeconds : DEFAULT_MAX_DURATION_SECONDS
255
+ const byBytes = catalog && catalog.maxAudioBytes > 44 ? Math.floor((catalog.maxAudioBytes - 44) / BYTES_PER_SECOND) : byTime
256
+ return Math.max(1, Math.min(byTime, byBytes))
257
+ }
258
+
259
+ function scheduleProbeRetry() {
260
+ if (probeFailures >= PREPARE_RETRY_DELAYS.length) return
261
+ const delay = PREPARE_RETRY_DELAYS[probeFailures]
262
+ probeFailures++
263
+ root.clearTimeout(probeTimer)
264
+ probeTimer = root.setTimeout(() => { probeTimer = null; void probeCatalog() }, delay)
265
+ }
266
+
267
+ // 能力探测失败一律静默:catalog 不存在(老网关/未启用语音 bundle)就隐藏入口,不刷错误。
268
+ async function probeCatalog() {
269
+ if (!host || typeof host.rpc !== 'function') return null
270
+ if (catalogState === 'unavailable') return null
271
+ if (host.token && !host.token()) { catalogState = 'idle'; renderMic(); return null }
272
+ const stamp = ++probeStamp
273
+ catalogState = 'loading'
274
+ try {
275
+ const value = await host.rpc('speech.catalog', {}, 15000)
276
+ if (stamp !== probeStamp) return catalog
277
+ catalog = normalizeCatalog(value)
278
+ catalogState = 'ok'
279
+ probeFailures = 0
280
+ renderMic()
281
+ // 弹层开着时用最新 catalog 重渲染(不含 selection 的本地猜测)
282
+ if (settingsOpen) renderSettings()
283
+ return catalog
284
+ } catch (error) {
285
+ if (stamp !== probeStamp) return catalog
286
+ catalog = null
287
+ const message = messageOf(error)
288
+ if (message === 'AUTH') { catalogState = 'idle'; renderMic(); return null }
289
+ if ((error && error.code === 'speech/unavailable') || /HTTP 404/.test(message)) {
290
+ catalogState = 'unavailable'
291
+ renderMic()
292
+ return null
293
+ }
294
+ catalogState = 'error'
295
+ renderMic()
296
+ scheduleProbeRetry()
297
+ return null
298
+ }
299
+ }
300
+
301
+ function markUnavailable() {
302
+ probeStamp++
303
+ catalogState = 'unavailable'
304
+ catalog = null
305
+ mode = 'idle'
306
+ stopPreparationPolling()
307
+ hideBar()
308
+ renderMic()
309
+ }
310
+
311
+ /* ---------------- 麦克风按钮 ---------------- */
312
+ function renderMic() {
313
+ const button = el('btn-mic')
314
+ if (!button) return
315
+ const visible = shouldShowMic({
316
+ secure: root.isSecureContext === true,
317
+ hasMediaDevices: canCaptureAudio(),
318
+ catalogOk: catalogState === 'ok' && !!catalog
319
+ })
320
+ button.classList.toggle('hidden', !visible)
321
+ button.classList.toggle('recording', mode === 'recording')
322
+ button.disabled = mode === 'starting' || mode === 'transcribing' || mode === 'preparing'
323
+ button.setAttribute('aria-pressed', mode === 'recording' ? 'true' : 'false')
324
+ const label = t(mode === 'recording' ? 'voice.stop' : 'voice.start')
325
+ button.title = label
326
+ button.setAttribute('aria-label', label)
327
+ }
328
+
329
+ /* ---------------- 内联提示行(录音/转写/模型准备) ---------------- */
330
+ function ensureBar() {
331
+ if (barEls && barEls.bar.isConnected) return barEls
332
+ const wrap = el('composer-wrap')
333
+ const composer = wrap && wrap.querySelector ? wrap.querySelector('.composer') : null
334
+ if (!wrap || !composer || typeof document === 'undefined') return null
335
+ const bar = document.createElement('div')
336
+ bar.id = 'voice-input-bar'
337
+ bar.className = 'voice-input-bar hidden'
338
+ bar.setAttribute('role', 'status')
339
+ bar.setAttribute('aria-live', 'polite')
340
+ const dot = document.createElement('span')
341
+ dot.className = 'voice-input-dot'
342
+ dot.setAttribute('aria-hidden', 'true')
343
+ const text = document.createElement('span')
344
+ text.className = 'voice-input-text'
345
+ const action = document.createElement('button')
346
+ action.id = 'voice-input-action'
347
+ action.className = 'voice-input-action hidden'
348
+ action.type = 'button'
349
+ action.addEventListener('click', () => { if (typeof barAction === 'function') void barAction() })
350
+ // 齿轮:录音/转写以外的状态都能打开语音设置(识别模型/语言/术语纠错)
351
+ const gear = document.createElement('button')
352
+ gear.id = 'voice-input-settings'
353
+ gear.className = 'voice-input-settings hidden'
354
+ gear.type = 'button'
355
+ gear.textContent = '⚙'
356
+ gear.addEventListener('click', () => { openSettings() })
357
+ bar.appendChild(dot)
358
+ bar.appendChild(text)
359
+ bar.appendChild(action)
360
+ bar.appendChild(gear)
361
+ wrap.insertBefore(bar, composer)
362
+ barEls = { bar, dot, text, action, gear }
363
+ return barEls
364
+ }
365
+
366
+ function setBar(text, options) {
367
+ const ui = ensureBar()
368
+ if (!ui) return
369
+ const settings = options || {}
370
+ root.clearTimeout(hideBarTimer)
371
+ hideBarTimer = null
372
+ ui.text.textContent = text
373
+ ui.bar.classList.toggle('recording', settings.recording === true)
374
+ ui.bar.classList.toggle('error', settings.error === true)
375
+ barAction = typeof settings.action === 'function' ? settings.action : null
376
+ if (barAction) {
377
+ ui.action.textContent = settings.actionLabel || ''
378
+ ui.action.classList.remove('hidden')
379
+ } else {
380
+ ui.action.textContent = ''
381
+ ui.action.classList.add('hidden')
382
+ }
383
+ // 录音与转写期间不需要改设置,隐藏齿轮
384
+ const hideSettings = settings.hideSettings === true || settings.recording === true || mode === 'transcribing' || mode === 'starting'
385
+ if (ui.gear) {
386
+ ui.gear.classList.toggle('hidden', hideSettings)
387
+ ui.gear.setAttribute('aria-label', t('voice.settings.title'))
388
+ }
389
+ ui.bar.classList.remove('hidden')
390
+ }
391
+
392
+ function hideBar() {
393
+ root.clearTimeout(hideBarTimer)
394
+ hideBarTimer = null
395
+ barAction = null
396
+ if (barEls && barEls.bar) {
397
+ barEls.bar.classList.add('hidden')
398
+ barEls.bar.classList.remove('recording', 'error')
399
+ barEls.text.textContent = ''
400
+ barEls.action.classList.add('hidden')
401
+ if (barEls.gear) barEls.gear.classList.add('hidden')
402
+ }
403
+ }
404
+
405
+ function hideBarLater(delay) {
406
+ root.clearTimeout(hideBarTimer)
407
+ hideBarTimer = root.setTimeout(() => { hideBarTimer = null; hideBar() }, delay)
408
+ }
409
+
410
+ /* ---------------- 录音 ---------------- */
411
+ function formatClock(totalSeconds) {
412
+ const seconds = Math.max(0, Math.floor(Number(totalSeconds) || 0))
413
+ return Math.floor(seconds / 60) + ':' + String(seconds % 60).padStart(2, '0')
414
+ }
415
+
416
+ async function toggleRecording() {
417
+ if (mode === 'recording') { stopRecording(false); return }
418
+ if (mode !== 'idle') return
419
+ await startRecording()
420
+ }
421
+
422
+ async function startRecording() {
423
+ if (mode !== 'idle') return
424
+ if (!catalog) {
425
+ const next = await probeCatalog()
426
+ if (!next) return
427
+ }
428
+ const provider = selectedProvider()
429
+ if (!provider || !phaseUsable(provider.preparation && provider.preparation.phase)) {
430
+ showPreparation(provider)
431
+ return
432
+ }
433
+ if (!canCaptureAudio()) { toast(t('voice.notSupported'), 'err'); return }
434
+ // starting 期间按钮禁用:授权弹窗前连点两次会拿到两条流
435
+ mode = 'starting'
436
+ renderMic()
437
+ let stream = null
438
+ try {
439
+ stream = await root.navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true }, video: false })
440
+ } catch (error) {
441
+ mode = 'idle'
442
+ renderMic()
443
+ const denied = error && (error.name === 'NotAllowedError' || error.name === 'SecurityError')
444
+ toast(denied ? t('voice.permissionDenied') : t('voice.failed', { msg: messageOf(error) }), 'err')
445
+ return
446
+ }
447
+ // 授权期间换了连接(reset()):放弃这条流
448
+ if (mode !== 'starting') { releaseStream(stream); return }
449
+ let recorder = null
450
+ try {
451
+ recorder = new root.MediaRecorder(stream)
452
+ } catch (error) {
453
+ mode = 'idle'
454
+ renderMic()
455
+ releaseStream(stream)
456
+ toast(t('voice.failed', { msg: messageOf(error) }), 'err')
457
+ return
458
+ }
459
+ session = { recorder, stream, chunks: [], startedAt: root.Date.now(), discard: false, timer: null }
460
+ recorder.addEventListener('dataavailable', event => {
461
+ if (session && event.data && event.data.size) session.chunks.push(event.data)
462
+ })
463
+ recorder.addEventListener('stop', () => { void finishRecording() })
464
+ recorder.addEventListener('error', event => {
465
+ const error = (event && event.error) || new Error('MediaRecorder')
466
+ if (session) session.failed = error
467
+ void finishRecording()
468
+ })
469
+ try {
470
+ recorder.start()
471
+ } catch (error) {
472
+ mode = 'idle'
473
+ renderMic()
474
+ releaseStream(stream)
475
+ session = null
476
+ toast(t('voice.failed', { msg: messageOf(error) }), 'err')
477
+ return
478
+ }
479
+ mode = 'recording'
480
+ renderMic()
481
+ paintRecording()
482
+ session.timer = root.setInterval(tickRecording, 250)
483
+ }
484
+
485
+ function paintRecording() {
486
+ if (!session) return
487
+ const elapsed = (root.Date.now() - session.startedAt) / 1000
488
+ setBar(t('voice.recording', { time: formatClock(elapsed), limit: formatClock(effectiveLimitSeconds()) }), {
489
+ recording: true,
490
+ action: () => stopRecording(true),
491
+ actionLabel: t('voice.cancel')
492
+ })
493
+ }
494
+
495
+ function tickRecording() {
496
+ if (!session || mode !== 'recording') return
497
+ if ((root.Date.now() - session.startedAt) / 1000 >= effectiveLimitSeconds()) { stopRecording(false); return }
498
+ paintRecording()
499
+ }
500
+
501
+ function stopRecording(discard) {
502
+ if (!session || mode !== 'recording') return
503
+ session.discard = discard === true
504
+ mode = 'transcribing'
505
+ if (session.timer) { root.clearInterval(session.timer); session.timer = null }
506
+ renderMic()
507
+ if (session.discard) hideBar()
508
+ else setBar(t('voice.transcribing'), {})
509
+ try {
510
+ if (session.recorder.state !== 'inactive') session.recorder.stop()
511
+ else void finishRecording()
512
+ } catch (_) {
513
+ void finishRecording()
514
+ }
515
+ }
516
+
517
+ function releaseStream(stream) {
518
+ try {
519
+ const tracks = stream && stream.getTracks ? stream.getTracks() : []
520
+ tracks.forEach(track => { try { track.stop() } catch (_) {} })
521
+ } catch (_) {}
522
+ }
523
+
524
+ function abortRecording() {
525
+ if (!session) return
526
+ const current = session
527
+ session = null
528
+ if (current.timer) root.clearInterval(current.timer)
529
+ current.discard = true
530
+ try { if (current.recorder.state !== 'inactive') current.recorder.stop() } catch (_) {}
531
+ releaseStream(current.stream)
532
+ }
533
+
534
+ async function finishRecording() {
535
+ const current = session
536
+ if (!current) return
537
+ session = null
538
+ releaseStream(current.stream)
539
+ mode = current.discard ? 'idle' : 'transcribing'
540
+ renderMic()
541
+ if (current.discard) { hideBar(); return }
542
+ if (current.failed) {
543
+ mode = 'idle'
544
+ renderMic()
545
+ hideBar()
546
+ toast(t('voice.failed', { msg: messageOf(current.failed) }), 'err')
547
+ return
548
+ }
549
+ const blob = current.chunks.length ? new Blob(current.chunks, { type: (current.recorder && current.recorder.mimeType) || '' }) : null
550
+ if (!blob || !blob.size) {
551
+ mode = 'idle'
552
+ renderMic()
553
+ hideBar()
554
+ toast(t('voice.empty'), 'err')
555
+ return
556
+ }
557
+ const connectionAtStart = connectionId()
558
+ try {
559
+ await transcribe(blob)
560
+ } catch (error) {
561
+ hideBar()
562
+ mode = 'idle'
563
+ renderMic()
564
+ // 换过服务器/令牌时旧请求的结果与错误都不再适用
565
+ if (error !== ABORTED && connectionId() === connectionAtStart) handleTranscribeError(error)
566
+ return
567
+ }
568
+ hideBar()
569
+ mode = 'idle'
570
+ renderMic()
571
+ }
572
+
573
+ function connectionId() {
574
+ return host && typeof host.server === 'function' ? String(host.server() || '') : ''
575
+ }
576
+
577
+ /* ---------------- 重采样 + 编码 + 转写 ---------------- */
578
+ function decodeAudioData(AudioCtx, arrayBuffer) {
579
+ const context = new AudioCtx()
580
+ return new Promise((resolve, reject) => {
581
+ let settled = false
582
+ const done = value => {
583
+ if (settled) return
584
+ settled = true
585
+ try { if (context.close) void context.close() } catch (_) {}
586
+ resolve(value)
587
+ }
588
+ const fail = error => {
589
+ if (settled) return
590
+ settled = true
591
+ try { if (context.close) void context.close() } catch (_) {}
592
+ reject(error instanceof Error ? error : new Error(messageOf(error) || 'decodeAudioData'))
593
+ }
594
+ let pending = null
595
+ try {
596
+ pending = context.decodeAudioData(arrayBuffer, done, fail)
597
+ } catch (error) {
598
+ fail(error)
599
+ return
600
+ }
601
+ if (pending && typeof pending.then === 'function') pending.then(done, fail)
602
+ })
603
+ }
604
+
605
+ async function renderWav(blob) {
606
+ const AudioCtx = root.AudioContext || root.webkitAudioContext
607
+ const Offline = root.OfflineAudioContext || root.webkitOfflineAudioContext
608
+ if (!AudioCtx || !Offline) throw new Error(t('voice.notSupported'))
609
+ const decoded = await decodeAudioData(AudioCtx, await blob.arrayBuffer())
610
+ const duration = Math.min(Number(decoded && decoded.duration) || 0, effectiveLimitSeconds())
611
+ if (!(duration > 0)) throw new Error(t('voice.empty'))
612
+ const frames = Math.max(1, Math.floor(duration * SAMPLE_RATE))
613
+ const offline = new Offline(1, frames, SAMPLE_RATE)
614
+ const source = offline.createBufferSource()
615
+ source.buffer = decoded
616
+ source.connect(offline.destination)
617
+ source.start(0)
618
+ const rendered = await offline.startRendering()
619
+ return encodeWav(rendered.getChannelData(0), SAMPLE_RATE)
620
+ }
621
+
622
+ function canonicalBase64(value) {
623
+ const text = String(value == null ? '' : value)
624
+ if (!text || text.length % 4 !== 0) return false
625
+ return /^[A-Za-z0-9+/]+={0,2}$/.test(text)
626
+ }
627
+
628
+ async function transcribe(blob) {
629
+ const connectionAtStart = connectionId()
630
+ const wav = await renderWav(blob)
631
+ const maxBytes = catalog ? catalog.maxAudioBytes : 0
632
+ if (maxBytes > 0 && wav.length > maxBytes) { toast(t('voice.tooLarge'), 'err'); throw ABORTED }
633
+ const audioBase64 = toBase64(wav)
634
+ if (!canonicalBase64(audioBase64)) { toast(t('voice.encodingFailed'), 'err'); throw ABORTED }
635
+ const providerId = catalog ? catalog.providerId : ''
636
+ const result = await host.rpc('speech.transcribe', {
637
+ request: { audioBase64, ...(providerId ? { providerId } : {}) }
638
+ }, 180000)
639
+ const text = String(result && result.text != null ? result.text : '').trim()
640
+ if (!text) { toast(t('voice.empty'), 'err'); throw ABORTED }
641
+ // 术语纠错在插入前应用:大小写不敏感整词替换,坏行忽略;替换后为空按「没识别到」处理
642
+ const corrected = applyCorrections(text, parseCorrections(savedCorrections()))
643
+ if (!corrected.trim()) { toast(t('voice.empty'), 'err'); throw ABORTED }
644
+ // 转写期间切换了服务器:文本不属于当前草稿,丢弃而不是插入
645
+ if (connectionId() !== connectionAtStart) throw ABORTED
646
+ insertTranscript(corrected)
647
+ }
648
+
649
+ // 只插入文本,不自动发送:保留原有内容与焦点,派发 input 让发送按钮状态刷新。
650
+ function insertTranscript(text) {
651
+ const input = el('composer-input')
652
+ if (!input) return
653
+ const caret = insertAtCursor(input, text)
654
+ try { input.focus() } catch (_) {}
655
+ if (typeof input.setSelectionRange === 'function') {
656
+ try { input.setSelectionRange(caret, caret) } catch (_) {}
657
+ }
658
+ try { if (typeof root.Event === 'function') input.dispatchEvent(new root.Event('input', { bubbles: true })) } catch (_) {}
659
+ if (host && typeof host.autosize === 'function') { try { host.autosize(input) } catch (_) {} }
660
+ try { input.scrollTop = input.scrollHeight } catch (_) {}
661
+ }
662
+
663
+ function handleTranscribeError(error) {
664
+ const message = messageOf(error)
665
+ const reason = reasonOf(error)
666
+ if (message === 'AUTH') { if (host && host.authFailure) host.authFailure(); return }
667
+ if (error && error.code === 'speech/unavailable') { markUnavailable(); return }
668
+ // 主机模型未准备:刷新一次 catalog 后直接进入准备流程,而不是只报错。
669
+ if (/Prepare the local speech provider/i.test(reason)) {
670
+ void probeCatalog().then(() => showPreparation(selectedProvider()))
671
+ return
672
+ }
673
+ toast(t('voice.failed', { msg: reason || t('err.dshError') }), 'err')
674
+ }
675
+
676
+ /* ---------------- 模型准备(下载/加载) ---------------- */
677
+ function showPreparation(provider) {
678
+ prepareProviderId = provider && provider.id ? provider.id : (catalog ? catalog.providerId : '')
679
+ if (!prepareProviderId) {
680
+ setBar(t('voice.notReady'), {})
681
+ return
682
+ }
683
+ const preparation = (provider && provider.preparation) || {}
684
+ const phase = String(preparation.phase || '')
685
+ if (phase === 'failed') { paintPreparation(provider); return }
686
+ if (phase && !phaseUsable(phase) && phase !== 'unprepared' && phase !== 'cancelled') {
687
+ startPreparationPolling()
688
+ paintPreparation(provider)
689
+ return
690
+ }
691
+ setBar(t('voice.notReady'), { action: beginPreparation, actionLabel: t('voice.prepare') })
692
+ }
693
+
694
+ async function beginPreparation() {
695
+ if (mode === 'preparing') return
696
+ const providerId = prepareProviderId || (catalog ? catalog.providerId : '')
697
+ if (!providerId) { toast(t('voice.unavailable'), 'err'); return }
698
+ prepareProviderId = providerId
699
+ mode = 'preparing'
700
+ renderMic()
701
+ setBar(t('voice.preparing'), {})
702
+ try {
703
+ await host.rpc('speech.prepare', { providerId }, 30000)
704
+ } catch (error) {
705
+ mode = 'idle'
706
+ renderMic()
707
+ handlePrepareError(error)
708
+ if (settingsOpen) renderSettings()
709
+ return
710
+ }
711
+ startPreparationPolling()
712
+ // 缓存里的 preparation 可能还是旧的 unprepared,先显示准备中,等第一次轮询出百分比
713
+ setBar(t('voice.preparing'), { action: cancelPreparation, actionLabel: t('voice.cancel') })
714
+ }
715
+
716
+ function startPreparationPolling() {
717
+ if (pollTimer) return
718
+ mode = 'preparing'
719
+ pollStartedAt = root.Date.now()
720
+ renderMic()
721
+ schedulePreparationPoll(PREPARE_POLL_MS)
722
+ }
723
+
724
+ function stopPreparationPolling() {
725
+ root.clearTimeout(pollTimer)
726
+ pollTimer = null
727
+ }
728
+
729
+ function schedulePreparationPoll(delay) {
730
+ root.clearTimeout(pollTimer)
731
+ pollTimer = root.setTimeout(() => { pollTimer = null; void pollPreparation() }, delay)
732
+ }
733
+
734
+ // 轮询 catalog 拿准备进度:正常 2s,失败退避 5s,最长 30 分钟;用户可取消。
735
+ async function pollPreparation() {
736
+ if (mode !== 'preparing') return
737
+ if (root.Date.now() - pollStartedAt > PREPARE_MAX_MS) {
738
+ mode = 'idle'
739
+ renderMic()
740
+ setBar(t('voice.prepareTimeout'), { error: true, action: prepareProviderId ? beginPreparation : null, actionLabel: t('voice.retry') })
741
+ return
742
+ }
743
+ let next = null
744
+ try {
745
+ next = await host.rpc('speech.catalog', {}, 20000)
746
+ } catch (error) {
747
+ if (messageOf(error) === 'AUTH') { mode = 'idle'; renderMic(); hideBar(); if (host.authFailure) host.authFailure(); return }
748
+ if (error && error.code === 'speech/unavailable') { markUnavailable(); return }
749
+ schedulePreparationPoll(PREPARE_BACKOFF_MS)
750
+ return
751
+ }
752
+ catalog = normalizeCatalog(next)
753
+ const provider = providerById(prepareProviderId) || selectedProvider()
754
+ const status = paintPreparation(provider)
755
+ // 弹层里的进度/状态跟内联条同步刷新
756
+ if (settingsOpen) renderSettings()
757
+ if (status === 'ready') { mode = 'idle'; renderMic(); hideBarLater(2600); return }
758
+ if (status === 'failed' || status === 'cancelled') { mode = 'idle'; renderMic(); return }
759
+ schedulePreparationPoll(PREPARE_POLL_MS)
760
+ }
761
+
762
+ function paintPreparation(provider) {
763
+ const preparation = (provider && provider.preparation) || {}
764
+ const phase = String(preparation.phase || '')
765
+ if (phaseUsable(phase)) { setBar(t('voice.prepareReady'), {}); return 'ready' }
766
+ if (phase === 'failed') {
767
+ const reason = preparation.message || (preparation.download && preparation.download.reason) || ''
768
+ setBar(t('voice.prepareFailed', { msg: reason || t('err.dshError') }), {
769
+ error: true, action: beginPreparation, actionLabel: t('voice.retry')
770
+ })
771
+ return 'failed'
772
+ }
773
+ if (!phase || phase === 'unprepared' || phase === 'cancelled') {
774
+ setBar(t('voice.notReady'), { action: beginPreparation, actionLabel: t('voice.prepare') })
775
+ return phase ? 'cancelled' : 'waiting'
776
+ }
777
+ const total = Number(preparation.totalBytes)
778
+ const completed = Number(preparation.completedBytes)
779
+ const percent = Number.isFinite(total) && total > 0 && Number.isFinite(completed)
780
+ ? Math.max(0, Math.min(100, Math.floor(completed / total * 100))) : null
781
+ setBar(percent == null ? t('voice.preparing') : t('voice.prepareProgress', { percent }), {
782
+ action: cancelPreparation, actionLabel: t('voice.cancel')
783
+ })
784
+ return 'progress'
785
+ }
786
+
787
+ function handlePrepareError(error) {
788
+ const message = messageOf(error)
789
+ if (message === 'AUTH') { if (host.authFailure) host.authFailure(); return }
790
+ if (error && error.code === 'speech/unavailable') { markUnavailable(); return }
791
+ setBar(t('voice.prepareFailed', { msg: reasonOf(error) || t('err.dshError') }), {
792
+ error: true, action: beginPreparation, actionLabel: t('voice.retry')
793
+ })
794
+ }
795
+
796
+ async function cancelPreparation() {
797
+ const providerId = prepareProviderId
798
+ stopPreparationPolling()
799
+ mode = 'idle'
800
+ renderMic()
801
+ setBar(t('voice.notReady'), providerId ? { action: beginPreparation, actionLabel: t('voice.prepare') } : {})
802
+ if (settingsOpen) renderSettings()
803
+ if (!providerId) return
804
+ try {
805
+ await host.rpc('speech.cancelPreparation', { providerId }, 30000)
806
+ } catch (error) {
807
+ if (messageOf(error) === 'AUTH' && host.authFailure) host.authFailure()
808
+ }
809
+ }
810
+
811
+ /* ---------------- 语音设置弹层(识别模型 / 识别语言 / 术语纠错) ----------------
812
+ * 入口两条:更多页「语音输入」行与内联条齿轮,都调 openSettings()。
813
+ * 弹层只是 catalog 的视图:configure 成功后才更新本地选中态,失败 toast 并回滚。 */
814
+ function storageRead(key) {
815
+ try {
816
+ const store = root.localStorage
817
+ return store && typeof store.getItem === 'function' ? store.getItem(key) : null
818
+ } catch (_) { return null }
819
+ }
820
+ function storageWrite(key, value) {
821
+ try {
822
+ const store = root.localStorage
823
+ if (store && typeof store.setItem === 'function') store.setItem(key, String(value))
824
+ } catch (_) {}
825
+ }
826
+ function savedCorrections() { return correctionsText(storageRead(CORRECTIONS_KEY)) }
827
+
828
+ function nodeOf(tag, className, text) {
829
+ const node = document.createElement(tag)
830
+ if (className) node.className = className
831
+ if (text != null) node.textContent = text
832
+ return node
833
+ }
834
+
835
+ function ensureSettings() {
836
+ if (settingsEls && settingsEls.overlay && settingsEls.overlay.isConnected) return settingsEls
837
+ if (typeof document === 'undefined' || !document.body) return null
838
+ const overlay = nodeOf('div', 'voice-settings hidden')
839
+ overlay.id = 'voice-settings'
840
+ overlay.setAttribute('role', 'dialog')
841
+ overlay.setAttribute('aria-modal', 'true')
842
+ overlay.setAttribute('aria-labelledby', 'voice-settings-title')
843
+ // 点遮罩关闭;面板内部点击不冒泡关闭;Esc 同样关闭
844
+ overlay.addEventListener('click', event => { if (event && event.target === overlay) closeSettings() })
845
+ overlay.addEventListener('keydown', event => { if (event && event.key === 'Escape') closeSettings() })
846
+ const sheet = nodeOf('div', 'voice-settings-sheet')
847
+ const head = nodeOf('div', 'voice-settings-head')
848
+ const title = nodeOf('h2')
849
+ title.id = 'voice-settings-title'
850
+ const close = nodeOf('button', 'voice-settings-close', '×')
851
+ close.id = 'voice-settings-close'
852
+ close.type = 'button'
853
+ close.addEventListener('click', () => closeSettings())
854
+ head.appendChild(title)
855
+ head.appendChild(close)
856
+
857
+ const body = nodeOf('div', 'voice-settings-body')
858
+ const status = nodeOf('p', 'voice-settings-status')
859
+ status.id = 'voice-settings-status'
860
+ status.setAttribute('role', 'status')
861
+ status.setAttribute('aria-live', 'polite')
862
+
863
+ // ① 识别模型:列出 catalog.providers 与准备状态,选中即 speech.configure
864
+ const modelSection = nodeOf('section', 'voice-settings-section')
865
+ const modelTitle = nodeOf('h3', 'voice-settings-section-title')
866
+ const modelHint = nodeOf('p', 'voice-settings-hint')
867
+ const modelList = nodeOf('div', 'voice-settings-options')
868
+ modelList.id = 'voice-settings-models'
869
+ modelList.setAttribute('role', 'radiogroup')
870
+ const prepare = nodeOf('button', 'voice-settings-primary hidden')
871
+ prepare.id = 'voice-settings-prepare'
872
+ prepare.type = 'button'
873
+ prepare.addEventListener('click', () => { void toggleSettingsPrepare() })
874
+ modelSection.appendChild(modelTitle)
875
+ modelSection.appendChild(modelHint)
876
+ modelSection.appendChild(modelList)
877
+ modelSection.appendChild(prepare)
878
+
879
+ // ② 识别语言:候选取所选 provider 的 languages(含 auto)
880
+ const langSection = nodeOf('section', 'voice-settings-section')
881
+ const langTitle = nodeOf('h3', 'voice-settings-section-title')
882
+ const langHint = nodeOf('p', 'voice-settings-hint')
883
+ const langSelect = nodeOf('select', 'voice-settings-select')
884
+ langSelect.id = 'voice-settings-language'
885
+ langSelect.addEventListener('change', () => { void chooseLanguage(langSelect.value) })
886
+ langSection.appendChild(langTitle)
887
+ langSection.appendChild(langHint)
888
+ langSection.appendChild(langSelect)
889
+
890
+ // ③ 术语纠错:每行「错误=正确」,本机保存,转写后整词替换
891
+ const correctionSection = nodeOf('section', 'voice-settings-section')
892
+ const correctionTitle = nodeOf('h3', 'voice-settings-section-title')
893
+ const correctionHint = nodeOf('p', 'voice-settings-hint')
894
+ const correctionText = nodeOf('textarea', 'voice-settings-textarea')
895
+ correctionText.id = 'voice-settings-corrections'
896
+ correctionText.rows = 6
897
+ correctionText.spellcheck = false
898
+ correctionText.addEventListener('input', () => {
899
+ storageWrite(CORRECTIONS_KEY, correctionText.value)
900
+ correctionStatus.textContent = t('voice.correction.saved', { count: parseCorrections(correctionText.value).length })
901
+ })
902
+ const correctionReset = nodeOf('button', 'voice-settings-secondary')
903
+ correctionReset.id = 'voice-settings-corrections-reset'
904
+ correctionReset.type = 'button'
905
+ correctionReset.addEventListener('click', () => {
906
+ correctionText.value = DEFAULT_CORRECTIONS
907
+ storageWrite(CORRECTIONS_KEY, DEFAULT_CORRECTIONS)
908
+ correctionStatus.textContent = t('voice.correction.saved', { count: parseCorrections(DEFAULT_CORRECTIONS).length })
909
+ })
910
+ const correctionStatus = nodeOf('p', 'voice-settings-hint')
911
+ correctionStatus.id = 'voice-settings-corrections-status'
912
+ correctionStatus.setAttribute('role', 'status')
913
+ correctionStatus.setAttribute('aria-live', 'polite')
914
+ correctionSection.appendChild(correctionTitle)
915
+ correctionSection.appendChild(correctionHint)
916
+ correctionSection.appendChild(correctionText)
917
+ correctionSection.appendChild(correctionReset)
918
+ correctionSection.appendChild(correctionStatus)
919
+
920
+ body.appendChild(status)
921
+ body.appendChild(modelSection)
922
+ body.appendChild(langSection)
923
+ body.appendChild(correctionSection)
924
+ sheet.appendChild(head)
925
+ sheet.appendChild(body)
926
+ overlay.appendChild(sheet)
927
+ document.body.appendChild(overlay)
928
+ settingsEls = {
929
+ overlay, sheet, title, close, status,
930
+ modelTitle, modelHint, modelList, prepare,
931
+ langTitle, langHint, langSelect,
932
+ correctionTitle, correctionHint, correctionText, correctionReset, correctionStatus
933
+ }
934
+ return settingsEls
935
+ }
936
+
937
+ function modelStatusLabel(state) {
938
+ if (state.ready) return t('voice.model.ready')
939
+ if (state.failed) return t('voice.model.failed')
940
+ if (state.busy) return state.percent == null ? t('voice.model.preparing') : t('voice.model.downloading', { percent: state.percent })
941
+ return t('voice.model.notReady')
942
+ }
943
+
944
+ // 用当前 catalog 渲染:打开弹层与每次轮询后都走这里,选中态只认 catalog.selection
945
+ function renderSettings() {
946
+ const ui = settingsEls
947
+ if (!ui) return
948
+ ui.title.textContent = t('voice.settings.title')
949
+ ui.close.setAttribute('aria-label', t('voice.settings.close'))
950
+ ui.modelTitle.textContent = t('voice.model.title')
951
+ ui.modelHint.textContent = t('voice.model.hint')
952
+ ui.langTitle.textContent = t('voice.lang.title')
953
+ ui.langHint.textContent = t('voice.lang.hint')
954
+ ui.correctionTitle.textContent = t('voice.correction.title')
955
+ ui.correctionHint.textContent = t('voice.correction.hint')
956
+ ui.correctionReset.textContent = t('voice.correction.reset')
957
+ ui.status.textContent = settingsLoading ? t('voice.settings.loading') : settingsError
958
+
959
+ const providers = catalog && Array.isArray(catalog.providers) ? catalog.providers : []
960
+ ui.modelList.replaceChildren()
961
+ let selected = null
962
+ for (const provider of providers) {
963
+ const id = String((provider && provider.id) || '')
964
+ if (!id) continue
965
+ const state = providerStatus(provider)
966
+ const chosen = !!catalog && catalog.providerId === id
967
+ const row = nodeOf('button', 'voice-settings-option' + (chosen ? ' selected' : ''))
968
+ row.id = 'voice-model-' + id
969
+ row.type = 'button'
970
+ row.setAttribute('role', 'radio')
971
+ row.setAttribute('data-voice-model', id)
972
+ row.setAttribute('aria-checked', chosen ? 'true' : 'false')
973
+ row.appendChild(nodeOf('span', 'voice-settings-option-name', String(provider.name || id)))
974
+ row.appendChild(nodeOf('span', 'voice-settings-option-state' + (state.ready ? ' ready' : state.failed ? ' failed' : ''), modelStatusLabel(state)))
975
+ row.addEventListener('click', () => { void chooseProvider(id) })
976
+ ui.modelList.appendChild(row)
977
+ if (chosen) selected = { provider, state }
978
+ }
979
+ if (!providers.length) ui.modelList.appendChild(nodeOf('p', 'voice-settings-empty', t('voice.model.empty')))
980
+
981
+ // 所选 provider 未就绪:弹层里直接「下载并准备」,复用内联条的 prepare 轮询
982
+ const needsPrepare = !!selected && !selected.state.ready
983
+ if (needsPrepare) {
984
+ const busy = selected.state.busy || mode === 'preparing'
985
+ ui.prepare.textContent = busy ? t('voice.cancel') : t('voice.model.prepare')
986
+ ui.prepare.disabled = settingsBusy
987
+ ui.prepare.classList.remove('hidden')
988
+ } else {
989
+ ui.prepare.textContent = ''
990
+ ui.prepare.disabled = false
991
+ ui.prepare.classList.add('hidden')
992
+ }
993
+
994
+ const provider = selected ? selected.provider : selectedProvider()
995
+ const languages = voiceLanguages(provider)
996
+ ui.langSelect.replaceChildren()
997
+ for (const code of languages) {
998
+ const option = nodeOf('option', '', code === 'auto' ? t('voice.lang.auto') : code)
999
+ option.value = code
1000
+ ui.langSelect.appendChild(option)
1001
+ }
1002
+ const current = catalog && catalog.language ? catalog.language : 'auto'
1003
+ ui.langSelect.value = languages.indexOf(current) >= 0 ? current : languages[0]
1004
+ ui.langSelect.disabled = !languages.length
1005
+ }
1006
+
1007
+ async function refreshSettings() {
1008
+ const stamp = ++settingsStamp
1009
+ if (!host || typeof host.rpc !== 'function') {
1010
+ settingsError = t('voice.unavailable')
1011
+ if (settingsOpen) renderSettings()
1012
+ return
1013
+ }
1014
+ settingsLoading = true
1015
+ settingsError = ''
1016
+ renderSettings()
1017
+ try {
1018
+ const value = await host.rpc('speech.catalog', {}, 15000)
1019
+ if (stamp !== settingsStamp) return
1020
+ catalog = normalizeCatalog(value)
1021
+ catalogState = 'ok'
1022
+ renderMic()
1023
+ } catch (error) {
1024
+ if (stamp !== settingsStamp) return
1025
+ settingsError = t('voice.settings.loadFailed', { msg: reasonOf(error) || t('err.dshError') })
1026
+ } finally {
1027
+ if (stamp === settingsStamp) {
1028
+ settingsLoading = false
1029
+ if (settingsOpen) renderSettings()
1030
+ }
1031
+ }
1032
+ }
1033
+
1034
+ // 选中指定 provider:先 configure,成功才改本地 catalog,失败 toast 并保持原选中态。
1035
+ async function chooseProvider(providerId) {
1036
+ const payload = configureProviderPatch(providerId)
1037
+ if (!payload || !catalog || settingsBusy) return
1038
+ if (catalog.providerId === payload.patch.providerId) { renderSettings(); return }
1039
+ const previous = catalog.providerId
1040
+ settingsBusy = true
1041
+ try {
1042
+ await host.rpc('speech.configure', payload, 20000)
1043
+ if (catalog) catalog.providerId = payload.patch.providerId
1044
+ // 语言候选随 provider 变化,用主机最新 catalog 覆盖本地选择
1045
+ void probeCatalog()
1046
+ if (settingsOpen) renderSettings()
1047
+ } catch (error) {
1048
+ if (catalog) catalog.providerId = previous
1049
+ toast(t('voice.model.selectFailed', { msg: reasonOf(error) || t('err.dshError') }), 'err')
1050
+ if (settingsOpen) renderSettings()
1051
+ } finally {
1052
+ settingsBusy = false
1053
+ renderMic()
1054
+ }
1055
+ }
1056
+
1057
+ // 识别语言:payload 固定 {patch:{language}}
1058
+ async function chooseLanguage(language) {
1059
+ const payload = configureLanguagePatch(language)
1060
+ if (!payload || !catalog) return
1061
+ if (catalog.language === payload.patch.language) { renderSettings(); return }
1062
+ const previous = catalog.language
1063
+ try {
1064
+ await host.rpc('speech.configure', payload, 20000)
1065
+ if (catalog) catalog.language = payload.patch.language
1066
+ } catch (error) {
1067
+ if (catalog) catalog.language = previous
1068
+ toast(t('voice.lang.selectFailed', { msg: reasonOf(error) || t('err.dshError') }), 'err')
1069
+ }
1070
+ if (settingsOpen) renderSettings()
1071
+ }
1072
+
1073
+ // 弹层里的「下载并准备 / 取消」:与内联条共用 beginPreparation + 轮询
1074
+ async function toggleSettingsPrepare() {
1075
+ const provider = selectedProvider()
1076
+ if (!provider || !provider.id || settingsBusy) return
1077
+ const state = providerStatus(provider)
1078
+ if (state.busy || mode === 'preparing') {
1079
+ await cancelPreparation()
1080
+ } else {
1081
+ prepareProviderId = provider.id
1082
+ await beginPreparation()
1083
+ }
1084
+ if (settingsOpen) renderSettings()
1085
+ }
1086
+
1087
+ function openSettings() {
1088
+ const ui = ensureSettings()
1089
+ if (!ui) return false
1090
+ settingsOpen = true
1091
+ settingsError = ''
1092
+ ui.overlay.classList.remove('hidden')
1093
+ ui.correctionText.value = savedCorrections()
1094
+ ui.correctionStatus.textContent = t('voice.correction.saved', { count: parseCorrections(ui.correctionText.value).length })
1095
+ renderSettings()
1096
+ try { if (typeof ui.close.focus === 'function') ui.close.focus() } catch (_) {}
1097
+ void refreshSettings()
1098
+ return true
1099
+ }
1100
+
1101
+ function closeSettings() {
1102
+ settingsOpen = false
1103
+ settingsStamp++
1104
+ settingsLoading = false
1105
+ if (settingsEls && settingsEls.overlay) settingsEls.overlay.classList.add('hidden')
1106
+ }
1107
+
1108
+ /* ---------------- 挂载 / 连接切换 ---------------- */
1109
+ function mount(api) {
1110
+ if (!api || typeof api.rpc !== 'function') return false
1111
+ host = api
1112
+ mode = 'idle'
1113
+ const button = el('btn-mic')
1114
+ if (button) {
1115
+ button.classList.add('hidden')
1116
+ button.addEventListener('click', () => { void toggleRecording() })
1117
+ }
1118
+ if (typeof document !== 'undefined' && document.addEventListener) {
1119
+ document.addEventListener('visibilitychange', () => {
1120
+ if (document.hidden && mode === 'recording') stopRecording(false)
1121
+ })
1122
+ }
1123
+ renderMic()
1124
+ if (host.token && !host.token()) return true
1125
+ void probeCatalog()
1126
+ return true
1127
+ }
1128
+
1129
+ // 连接身份(地址/令牌)变化:清缓存、停轮询、放弃录音,然后按新连接重新探测。
1130
+ function reset() {
1131
+ probeStamp++
1132
+ catalog = null
1133
+ catalogState = 'idle'
1134
+ probeFailures = 0
1135
+ root.clearTimeout(probeTimer)
1136
+ probeTimer = null
1137
+ stopPreparationPolling()
1138
+ abortRecording()
1139
+ mode = 'idle'
1140
+ hideBar()
1141
+ renderMic()
1142
+ // 弹层开着时换成空 catalog,等新连接的 probeCatalog 回来再重渲染
1143
+ if (settingsOpen) renderSettings()
1144
+ if (!host || (host.token && !host.token())) return
1145
+ void probeCatalog()
1146
+ }
1147
+
1148
+ const api = { encodeWav, toBase64, decodeBase64, shouldShowMic, insertAtCursor, mount, reset, openSettings, closeSettings }
1149
+ if (typeof module !== 'undefined' && module.exports) module.exports = api
1150
+ else root.DshVoiceInput = api
1151
+ })(typeof window === 'undefined' ? globalThis : window)