dsh-folia 4.26.0-pre.57 → 4.26.0-pre.59

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/lib/client.js CHANGED
@@ -796,6 +796,14 @@ window.__ModuleLoader__.load({
796
796
  "对话": "Chat",
797
797
  "可选工作能力(已安装才可用)": "Optional work capabilities (installed only)",
798
798
  "能力": "Capabilities",
799
+ "麦克风测试": "Microphone test",
800
+ "测试麦克风": "Test microphone",
801
+ "录音中…": "Recording…",
802
+ "正在录音 3 秒…对着麦说话": "Recording 3s… speak now",
803
+ "录到 {kb}KB,正在播回你的声音(RMS {rms})": "Captured {kb}KB, playing it back (RMS {rms})",
804
+ "没录到:": "Nothing captured: ",
805
+ "测试失败:": "Test failed: ",
806
+ "点一下录 3 秒,会直接播回你刚说的话。": "Tap to record 3s; it plays back exactly what you said.",
799
807
  "如何使用": "How to use",
800
808
  "作者描述:": "Author description: ",
801
809
  "近 30 天下载 {d} · 星 {s}": "Downloads (30d) {d} · Stars {s}",
@@ -19775,6 +19783,7 @@ window.__ModuleLoader__.load({
19775
19783
  React.createElement('div', { key: 'assistant-note', className: 'we-picker__hint', style: { margin: '6px 0' } },
19776
19784
  weT('填完 Key 后去「朗读与音色」点一次「一键启用语音」,麦克风、音色和识别模型会自动准备好。')),
19777
19785
  ));
19786
+ child.push(React.createElement(MicTestBlock, { key: 'mic-test' }));
19778
19787
  }
19779
19788
 
19780
19789
  if (tab === 'voice') {
@@ -20106,6 +20115,47 @@ window.__ModuleLoader__.load({
20106
20115
  return React.createElement('div', { id: 'we-pet-chat-settings', style: { ...PET_STYLE.card, marginTop: '10px' } }, child);
20107
20116
  }
20108
20117
 
20118
+ /**
20119
+ * 麦克风测试:录 3 秒 → 直接播回给用户。
20120
+ * 用途:让用户自己确认“到底是没说话,还是插件没录到”。
20121
+ */
20122
+ function MicTestBlock() {
20123
+ const [state, setState] = React.useState({ text: '', busy: false, ok: null });
20124
+ const run = async () => {
20125
+ setState({ text: weT('正在录音 3 秒…对着麦说话'), busy: true, ok: null });
20126
+ try {
20127
+ const res = await apiFetch('/pet-chat/voice/mic-test', { parse: false, cache: 'no-store' });
20128
+ if (!res.ok) {
20129
+ // apiFetch 返回 { ok, status, data, error, response }:非 2xx 时体在 response 里,不是 res.json()
20130
+ let body = {};
20131
+ try { body = await res.response.text().then((x) => (x ? JSON.parse(x) : {})); } catch { body = {}; }
20132
+ setState({ text: weT('没录到:') + (body.hint || body.code || res.status || res.error || ''), busy: false, ok: false });
20133
+ return;
20134
+ }
20135
+ const response = res.response;
20136
+ const blob = await response.blob();
20137
+ const bytes = Number(response.headers.get('X-Folia-Mic-Bytes') || blob.size || 0);
20138
+ const rms = response.headers.get('X-Folia-Mic-Rms') || '';
20139
+ const url = URL.createObjectURL(blob);
20140
+ try {
20141
+ const audio = new Audio(url);
20142
+ audio.onended = () => { try { URL.revokeObjectURL(url); } catch { /* ignore */ } };
20143
+ await audio.play();
20144
+ } catch { /* 播放被拒就保留 URL,不影响结论 */ }
20145
+ setState({ text: weT('录到 {kb}KB,正在播回你的声音(RMS {rms})', { kb: Math.round(bytes / 1024), rms }), busy: false, ok: true });
20146
+ } catch (error) {
20147
+ setState({ text: weT('测试失败:') + String((error && error.message) || error), busy: false, ok: false });
20148
+ }
20149
+ };
20150
+ return React.createElement('div', { style: { marginTop: '10px', padding: '8px', border: '1px solid rgba(255,255,255,.10)', borderRadius: '8px' } },
20151
+ React.createElement('div', { style: { fontSize: '12px', fontWeight: 600, marginBottom: '4px' } }, weT('麦克风测试')),
20152
+ React.createElement('div', { className: 'we-picker__hint', style: { marginBottom: '6px' } }, weT('点一下录 3 秒,会直接播回你刚说的话。')),
20153
+ React.createElement('button', { type: 'button', className: 'we-picker__btn', disabled: state.busy, onClick: run },
20154
+ state.busy ? weT('录音中…') : weT('测试麦克风')),
20155
+ state.text ? React.createElement('div', { className: 'we-picker__hint', style: { marginTop: '4px', color: state.ok === false ? '#f87171' : undefined } }, state.text) : null,
20156
+ );
20157
+ }
20158
+
20109
20159
  /**
20110
20160
  * 「智能大肥鱼」一级设置页。只做版面装配:标题 + 三分页表单。
20111
20161
  * 设置导航的注册在 `src/client.js`(id=folia-assistant,order 502)。
@@ -22173,13 +22223,15 @@ window.__ModuleLoader__.load({
22173
22223
 
22174
22224
  const CSS = [
22175
22225
  '.we-pet-wrap{position:fixed;z-index:9999;display:flex;flex-direction:column;align-items:center;gap:6px;max-width:320px;pointer-events:none;transform-origin:50% 100%;}',
22176
- // 回答气泡:半透明深色底 + 轻描边 + 14–16px 圆角;长回答 160px 内滚动。
22177
- '.we-pet-bubble{position:relative;max-width:320px;max-height:160px;overflow-y:auto;padding:10px 12px;border-radius:15px;background:rgba(20,20,24,.82);color:#f2f2f5;font-size:13px;line-height:1.5;border:1px solid rgba(255,255,255,.14);box-shadow:0 8px 24px rgba(0,0,0,.28);backdrop-filter:blur(8px);-webkit-backdrop-filter:blur(8px);white-space:pre-wrap;word-break:break-word;opacity:0;transition:opacity 150ms ease;pointer-events:auto;scrollbar-width:thin;}',
22226
+ // 回答气泡:半透明深色底 + 轻描边 + 14–16px 圆角;长回答在内部文本层 160px 滚动。
22227
+ // 滚动放在 .we-pet-bubble-text,气泡本身 overflow:visible,尾巴和关闭按钮才不会被裁掉。
22228
+ '.we-pet-bubble{position:relative;max-width:320px;padding:10px 12px;border-radius:15px;background:rgba(20,20,24,.82);color:#f2f2f5;font-size:13px;line-height:1.5;border:1px solid rgba(255,255,255,.14);box-shadow:0 8px 24px rgba(0,0,0,.28);backdrop-filter:blur(8px);-webkit-backdrop-filter:blur(8px);white-space:pre-wrap;word-break:break-word;opacity:0;transition:opacity 150ms ease;pointer-events:auto;overflow:visible;}',
22229
+ '.we-pet-bubble-text{max-height:160px;overflow-y:auto;padding-right:3px;scrollbar-width:thin;}',
22178
22230
  '.we-pet-bubble[data-visible="1"]{opacity:1;}',
22179
22231
  // 小尾巴指向桌宠(在气泡底部中央;不挡脸)。
22180
22232
  '.we-pet-bubble::after{content:"";position:absolute;left:50%;bottom:-6px;width:12px;height:12px;background:inherit;border-right:1px solid rgba(255,255,255,.14);border-bottom:1px solid rgba(255,255,255,.14);transform:translateX(-50%) rotate(45deg);}',
22181
- '.we-pet-bubble::-webkit-scrollbar{width:4px;}',
22182
- '.we-pet-bubble::-webkit-scrollbar-thumb{background:rgba(255,255,255,.22);border-radius:2px;}',
22233
+ '.we-pet-bubble-text::-webkit-scrollbar{width:4px;}',
22234
+ '.we-pet-bubble-text::-webkit-scrollbar-thumb{background:rgba(255,255,255,.22);border-radius:2px;}',
22183
22235
  // 状态条:细、独立、可点、3–5 秒淡出。
22184
22236
  '.we-pet-status{max-width:320px;padding:5px 10px;border-radius:999px;background:rgba(20,20,24,.74);color:#ececf2;font-size:12px;line-height:1.4;border:1px solid rgba(255,255,255,.12);box-shadow:0 4px 14px rgba(0,0,0,.22);backdrop-filter:blur(8px);-webkit-backdrop-filter:blur(8px);display:flex;align-items:center;gap:6px;opacity:0;transition:opacity 150ms ease;pointer-events:auto;cursor:pointer;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}',
22185
22237
  '.we-pet-status[data-visible="1"]{opacity:1;}',
@@ -1037,14 +1037,22 @@ export function registerPetChatRoutes(webServer, c) {
1037
1037
  if (!result.ok) {
1038
1038
  const hints = {
1039
1039
  'no-device': '没找到可用麦克风(检查系统录音设备或是否被其它程序占用)',
1040
- 'no-signal': '麦克风没有收到声音:检查是否静音、输入设备是否选对,或离麦克风近一点',
1041
1040
  'too-quiet': '太小声了,离麦克风近一点(30–50cm)再说一次',
1042
1041
  'unsupported-platform': '这个系统不支持桌面录音,用浏览器打开 DSH 才能语音',
1043
1042
  'ffmpeg-missing': '缺少 ffmpeg,无法录音',
1044
1043
  timeout: '录音超时,再说一次',
1045
1044
  'no-speech': '没听到声音,再说一次或直接打字',
1046
1045
  };
1047
- send({ ok: false, code: result.code, hint: hints[result.code] || '录音失败' }, result.code === 'ffmpeg-missing' ? 503 : 422);
1046
+ send({
1047
+ ok: false,
1048
+ code: result.code,
1049
+ hint: hints[result.code] || result.hint || '录音失败',
1050
+ attempts: result.attempts || [],
1051
+ devices: result.devices || [],
1052
+ lastWav: result.lastWav || null,
1053
+ lastWavBytes: result.lastWavBytes || 0,
1054
+ lastWavRms: result.lastWavRms == null ? null : result.lastWavRms,
1055
+ }, result.code === 'ffmpeg-missing' ? 503 : 422);
1048
1056
  return;
1049
1057
  }
1050
1058
  // ── 转写:本地 SenseVoice 优先;没装/没下好 ⇒ asr-not-ready,由客户端触发下载 ──
@@ -1900,6 +1908,47 @@ const roots = [
1900
1908
  });
1901
1909
  }
1902
1910
 
1911
+ /**
1912
+ * GET /pet-chat/voice/mic-test —— 固定录 3 秒,直接回 audio/wav + 诊断响应头。
1913
+ * 用途:用户点「测试麦克风」后亲耳听到自己刚录的声音。
1914
+ */
1915
+ async function handleMicTest(req, res) {
1916
+ const sendJson = (payload, status = 200) => {
1917
+ res.statusCode = status;
1918
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
1919
+ res.end(JSON.stringify(payload));
1920
+ };
1921
+ if (typeof voiceRecorder.recordFixed !== 'function') {
1922
+ sendJson({ ok: false, code: 'unsupported', hint: '当前版本不支持麦克风测试' }, 501);
1923
+ return;
1924
+ }
1925
+ const result = await voiceRecorder.recordFixed(3000);
1926
+ if (!result || !result.ok) {
1927
+ sendJson({
1928
+ ok: false,
1929
+ code: (result && result.code) || 'mic-test-failed',
1930
+ hint: '麦克风打不开或没采到音频',
1931
+ bytes: (result && result.bytes) || 0,
1932
+ device: (result && result.device) || null,
1933
+ }, 422);
1934
+ return;
1935
+ }
1936
+ try {
1937
+ const bytes = readFileSync(result.audioPath);
1938
+ res.statusCode = 200;
1939
+ res.setHeader('Content-Type', 'audio/wav');
1940
+ res.setHeader('Content-Length', String(bytes.length));
1941
+ res.setHeader('Cache-Control', 'no-store');
1942
+ res.setHeader('X-Folia-Mic-Device', String(result.device || ''));
1943
+ res.setHeader('X-Folia-Mic-Rms', String(result.audioRms == null ? '' : result.audioRms));
1944
+ res.setHeader('X-Folia-Mic-Db', String(result.audioDb == null ? '' : result.audioDb));
1945
+ res.setHeader('X-Folia-Mic-Bytes', String(result.bytes || bytes.length));
1946
+ res.end(bytes);
1947
+ } catch (error) {
1948
+ sendJson({ ok: false, code: 'mic-test-read-failed', error: String((error && error.message) || error) }, 500);
1949
+ }
1950
+ }
1951
+
1903
1952
  /**
1904
1953
  * GET /pet-chat/voice/diagnose —— 一次看完整条语音链:
1905
1954
  * ffmpeg / 麦克风 / 本地 ASR / TTS(Edge 在线、Piper 安装状态、SAPI 开关)/ 上次结果。
@@ -2455,6 +2504,7 @@ const roots = [
2455
2504
  if (tail === '/record' && method === 'POST') { await handleRecord(req, res); return; }
2456
2505
  // v0.2 语音自举:诊断 / 一键启用 / SAPI 开关(都在 /voice 段内)
2457
2506
  if (tail === '/voice/diagnose' && (method === 'GET' || method === 'HEAD')) { await handleVoiceDiagnose(res); return; }
2507
+ if (tail === '/voice/mic-test' && (method === 'GET' || method === 'HEAD')) { await handleMicTest(req, res); return; }
2458
2508
  if (tail === '/voice/setup' && method === 'POST') { await handleVoiceSetup(req, res); return; }
2459
2509
  if (tail === '/voice/enable' && method === 'POST') { await handleVoiceEnable(req, res); return; }
2460
2510
  if (tail === '/voice/piper/progress' && (method === 'GET' || method === 'HEAD')) { handlePiperProgress(res); return; }