dsh-folia 4.26.0-pre.13 → 4.26.0-pre.15

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
@@ -20705,6 +20705,44 @@ window.__ModuleLoader__.load({
20705
20705
  },
20706
20706
  });
20707
20707
 
20708
+ /**
20709
+ * 语音模型(SenseVoice)自举:调宿主 /pet-chat/asr(它会 speechToText.prepare),
20710
+ * 然后轮询 /pet-chat/asr 把下载进度显示在气泡里。
20711
+ * 这样用户装完插件直接说话就行,不需要去 DSH 设置里找「语音输入」开关。
20712
+ */
20713
+ async function ensureAsrReady() {
20714
+ petBubble.setState('think');
20715
+ try {
20716
+ await apiPostJson('/pet-chat/asr', {}, { parse: 'always' });
20717
+ } catch { /* 触发失败也要继续轮询看状态 */ }
20718
+ for (let i = 0; i < 240; i += 1) {
20719
+ await new Promise((resolve) => { setTimeout(resolve, 1500); });
20720
+ const resp = await apiJson('/pet-chat/asr', { parse: 'always' }).catch(() => null);
20721
+ const doc = (resp && resp.data) || {};
20722
+ if (doc.ready) {
20723
+ petBubble.show('语音准备好了,点我再说一次');
20724
+ petBubble.hide(3000);
20725
+ return;
20726
+ }
20727
+ if (doc.available === false) {
20728
+ petBubble.show('这个 DSH 版本没有语音转写服务,先打字吧');
20729
+ petBubble.hide(3000);
20730
+ return;
20731
+ }
20732
+ if (doc.phase === 'failed') {
20733
+ petBubble.show('语音模型下载失败,稍后再点我试试');
20734
+ petBubble.hide(3000);
20735
+ return;
20736
+ }
20737
+ const pct = doc.totalBytes > 0 && doc.completedBytes >= 0
20738
+ ? Math.max(0, Math.min(100, Math.round((doc.completedBytes / doc.totalBytes) * 100)))
20739
+ : null;
20740
+ petBubble.show(pct === null ? '语音模型准备中…' : `语音模型下载中 ${pct}%`);
20741
+ }
20742
+ petBubble.show('语音模型还没下完,过一会儿再点我');
20743
+ petBubble.hide(3000);
20744
+ }
20745
+
20708
20746
  /** 语音输入(v0.1):前端不碰麦克风 —— 宿主用 ffmpeg 录,这里只发请求、收文本。 */
20709
20747
  const petVoiceIn = petVoiceInputCreate({
20710
20748
  postRecord: async () => {
@@ -20724,6 +20762,10 @@ window.__ModuleLoader__.load({
20724
20762
  method: 'POST',
20725
20763
  headers: { 'content-type': 'application/json' },
20726
20764
  body: JSON.stringify({ text }),
20765
+ // ⚠️ 必须 parse:'none'。默认路径会 await response.json(),
20766
+ // 那会把 SSE 流**整个消费掉**(等到生成结束、再解析失败),
20767
+ // 之后 res.response.body 已 used ⇒ getReader() 抛错 ⇒ 气泡一个字都不出。
20768
+ parse: 'none',
20727
20769
  });
20728
20770
  } catch {
20729
20771
  petBubble.show('连不上宿主,稍后再试');
@@ -20767,13 +20809,18 @@ window.__ModuleLoader__.load({
20767
20809
  const map = {
20768
20810
  'no-device': '没找到麦克风,先打字吧',
20769
20811
  'ffmpeg-missing': '缺少 ffmpeg,先打字吧',
20770
- 'asr-not-ready': '语音输入还没就绪:去 DSH 设置里开启(需下载模型)',
20812
+ 'asr-not-ready': '语音模型还没下载,我来准备一下…',
20771
20813
  timeout: '这次没听清,再说一次或直接打字',
20772
20814
  'no-speech': '没听到声音,再说一次或直接打字',
20773
20815
  network: '网络不太顺,重试或直接打字',
20774
20816
  };
20775
20817
  const message = map[error && error.code] || '没听清,直接打字也行';
20776
20818
  petBubble.show(message);
20819
+ // asr-not-ready:自动触发模型下载并在气泡里显示进度,用户不用去 DSH 设置里找开关
20820
+ if (error && error.code === 'asr-not-ready') {
20821
+ void ensureAsrReady();
20822
+ return;
20823
+ }
20777
20824
  petBubble.hide(3000);
20778
20825
  petChatOpenTyping({ hint: message });
20779
20826
  },
@@ -20827,6 +20874,15 @@ window.__ModuleLoader__.load({
20827
20874
  function petChatWake() {
20828
20875
  if (petBubble.isVisible()) { petBubble.hide(0); petVoiceIn.stop(); return; }
20829
20876
  petBubble.setState('listen');
20877
+ // 先看语音模型是否就绪;没就绪就**立刻**开始下载并在气泡里显示进度,
20878
+ // 不要等用户先撞一次 asr-not-ready 才被动触发(那之前会像"没反应")。
20879
+ void (async () => {
20880
+ const resp = await apiJson('/pet-chat/asr', { parse: 'always' }).catch(() => null);
20881
+ const doc = (resp && resp.data) || {};
20882
+ if (doc.ready) return;
20883
+ if (doc.available === false) return; // 该 DSH 没转写服务,交给 record 的失败分支提示
20884
+ await ensureAsrReady();
20885
+ })();
20830
20886
  void petVoiceIn.start().then((started) => {
20831
20887
  if (!started) return; // 失败分支已经由 onError 提示(回落打字)
20832
20888
  petBubble.show('我在听…');
@@ -813,6 +813,65 @@ export function registerPetChatRoutes(webServer, c) {
813
813
  }
814
814
  }
815
815
 
816
+ /**
817
+ * GET /pet-chat/asr —— 语音转写模型的就绪状态与下载进度。
818
+ * 读 `speechToText.snapshot()` 里该 provider 的 preparation:
819
+ * phase: unprepared | checking | downloading | loading | waking | ready | failed | standby …
820
+ * downloading 时带 completedBytes / totalBytes。
821
+ */
822
+ function handleAsrStatus(res) {
823
+ const send = (payload, status = 200) => {
824
+ res.statusCode = status;
825
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
826
+ res.end(JSON.stringify(payload));
827
+ };
828
+ const service = speechToText;
829
+ if (!service || typeof service.snapshot !== 'function') {
830
+ send({ ok: true, available: false, providerId: ASR_PROVIDER_ID, phase: 'unavailable', ready: false });
831
+ return;
832
+ }
833
+ let prep = { phase: 'missing' };
834
+ try {
835
+ const snap = service.snapshot();
836
+ const prov = (snap && Array.isArray(snap.providers) ? snap.providers : [])
837
+ .find((item) => item && item.id === ASR_PROVIDER_ID);
838
+ prep = (prov && prov.preparation) || { phase: 'missing' };
839
+ } catch { /* snapshot 失败按未就绪处理 */ }
840
+ send({
841
+ ok: true,
842
+ available: true,
843
+ providerId: ASR_PROVIDER_ID,
844
+ phase: prep.phase || 'missing',
845
+ completedBytes: typeof prep.completedBytes === 'number' ? prep.completedBytes : null,
846
+ totalBytes: typeof prep.totalBytes === 'number' ? prep.totalBytes : null,
847
+ ready: prep.phase === 'ready',
848
+ message: typeof prep.message === 'string' ? prep.message : null,
849
+ });
850
+ }
851
+
852
+ /**
853
+ * POST /pet-chat/asr —— 触发模型下载/准备(幂等;已在准备中会 join 同一个任务)。
854
+ * 这样用户装完插件就能用语音,不必去 DSH 设置里找"语音输入"开关。
855
+ */
856
+ function handleAsrPrepare(res) {
857
+ const send = (payload, status = 200) => {
858
+ res.statusCode = status;
859
+ res.setHeader('Content-Type', 'application/json; charset=utf-8');
860
+ res.end(JSON.stringify(payload));
861
+ };
862
+ const service = speechToText;
863
+ if (!service || typeof service.prepare !== 'function') {
864
+ send({ ok: false, code: 'asr-unavailable', hint: '这个 DSH 版本没有提供语音转写服务' });
865
+ return;
866
+ }
867
+ try {
868
+ service.prepare(ASR_PROVIDER_ID);
869
+ send({ ok: true, started: true, providerId: ASR_PROVIDER_ID });
870
+ } catch (error) {
871
+ send({ ok: false, code: 'asr-prepare-failed', hint: String((error && error.message) || error) });
872
+ }
873
+ }
874
+
816
875
  async function handleConfigGet(res) {
817
876
  const config = readPetChat();
818
877
  const key = String(config.apiKey || '');
@@ -993,6 +1052,10 @@ export function registerPetChatRoutes(webServer, c) {
993
1052
  if (tail === '/reset' && method === 'POST') { handleReset(res); return; }
994
1053
  if (tail === '/transcribe' && method === 'POST') { handleTranscribe(req, res); return; }
995
1054
  if (tail === '/record' && method === 'POST') { await handleRecord(req, res); return; }
1055
+ // 语音模型(SenseVoice)自举:用户不需要去 DSH 设置里找开关,
1056
+ // 插件自己触发 preparation 并轮询进度(DSH 的 speechToText.prepare 是公开方法)。
1057
+ if (tail === '/asr' && (method === 'GET' || method === 'HEAD')) { handleAsrStatus(res); return; }
1058
+ if (tail === '/asr' && method === 'POST') { handleAsrPrepare(res); return; }
996
1059
  // v0.1「零配置」主动搭话:没有开关,只有内部策略 + 语音指令静默
997
1060
  if (tail === '/proactive' && (method === 'GET' || method === 'HEAD')) {
998
1061
  res.setHeader('Content-Type', 'application/json; charset=utf-8');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-folia",
3
- "version": "4.26.0-pre.13",
3
+ "version": "4.26.0-pre.15",
4
4
  "description": "DSH 壁纸引擎:本机 WE 壁纸实时渲染,视频/网页/开机动画/桌宠皮肤,一键拉取在线素材。",
5
5
  "type": "module",
6
6
  "engines": {