dsh-folia 4.26.0-pre.52 → 4.26.0-pre.54
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 +401 -139
- package/lib/routes/pet-chat.js +29 -3
- package/lib/settings-schema.js +8 -3
- package/lib/tts/catalog.js +48 -0
- package/lib/tts/tts-worker.cjs +117 -22
- package/lib/tts/tts.js +27 -3
- package/lib/voice/record.js +26 -18
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -741,6 +741,35 @@ window.__ModuleLoader__.load({
|
|
|
741
741
|
// pre.49:桌宠页控制行最右侧的语音助手开关(点开才挂载设置块)。
|
|
742
742
|
"智能大肥鱼": "Smart Chubby Fish",
|
|
743
743
|
"桌宠语音助手": "Pet voice assistant",
|
|
744
|
+
// pre.54:气泡状态条三态 + 音色回落提示。
|
|
745
|
+
"在听": "Listening",
|
|
746
|
+
"在想": "Thinking",
|
|
747
|
+
"在说": "Speaking",
|
|
748
|
+
"所选音色不可用,已用系统默认:{name}": "Selected voice is unavailable — using the system default: {name}",
|
|
749
|
+
"所选音色不可用,已用系统默认音色": "Selected voice is unavailable — using the system default voice",
|
|
750
|
+
// pre.54:本机音色来源 / 可下载目录骨架。
|
|
751
|
+
"(系统音色,离线)": "(system voice, offline)",
|
|
752
|
+
"(本机未检测到,将回落)": "(not detected on this machine; will fall back)",
|
|
753
|
+
"只列本机真实可用的系统音色(离线);云端音色不随包分发。":
|
|
754
|
+
"Only voices actually available on this machine are listed (offline); cloud voices are not bundled.",
|
|
755
|
+
"正在读取本机音色…": "Reading local voices…",
|
|
756
|
+
"未检测到可用音色(系统 TTS 不可用)": "No usable voice detected (system TTS unavailable)",
|
|
757
|
+
"可下载音色(即将支持)": "Downloadable voices (coming soon)",
|
|
758
|
+
"即将支持": "coming soon",
|
|
759
|
+
"这些是开源离线方案,本版只接目录;合成接通后才会出现在上面的音色下拉里。":
|
|
760
|
+
"These are open-source offline options; this version only wires the catalogue. They will appear in the voice list above once synthesis is connected.",
|
|
761
|
+
// pre.53:朗读开关 + 音色(voice.muted / voice.name);桌宠页的音效标签同步改名。
|
|
762
|
+
"音色": "Voice",
|
|
763
|
+
"🔊 朗读回答": "🔊 Read replies aloud",
|
|
764
|
+
"关闭后只显示字幕、不合成语音(省算力);保存后立刻生效。":
|
|
765
|
+
"When off, replies are shown as subtitles only (no speech synthesis); takes effect as soon as you save.",
|
|
766
|
+
"填 Key、起名字、选音色,再决定要不要朗读。主动搭话与记忆是固定策略;停止/恢复对它说一声就行。":
|
|
767
|
+
"Fill in the key, give it a name, pick a voice, then decide whether it should read replies aloud. Proactive chat and memory are fixed behaviours; just tell it to stop or resume.",
|
|
768
|
+
"已保存(朗读已关闭)": "Saved (read-aloud is off)",
|
|
769
|
+
"桌宠音效": "Pet sound effects",
|
|
770
|
+
"音效音量": "SFX volume",
|
|
771
|
+
"点击与动作音效;朗读回答的开关在「智能大肥鱼」页":
|
|
772
|
+
"Click and action sound effects; the read-aloud toggle lives in the Smart Chubby Fish section",
|
|
744
773
|
"卸载": "Uninstall",
|
|
745
774
|
"{cols} 列 · {size}": "{cols} cols · {size}",
|
|
746
775
|
"还没有安装任何桌宠 —— 从下面的站点清单里挑一只。": "No pets installed yet — pick one from the site list below.",
|
|
@@ -8161,8 +8190,10 @@ window.__ModuleLoader__.load({
|
|
|
8161
8190
|
*/
|
|
8162
8191
|
petName: '', // 桌宠名字(空 ⇒ 用宠物包 displayName);同时是唤醒词
|
|
8163
8192
|
ownerName: '', // 用户希望被怎么称呼
|
|
8164
|
-
// 朗读:只有一个音色 + 一个静音开关(不再有 rate/pitch/volume/voiceURI
|
|
8165
|
-
|
|
8193
|
+
// 朗读:只有一个音色 + 一个静音开关(不再有 rate/pitch/volume/voiceURI 五件套)。
|
|
8194
|
+
// pre.54:默认**空串** = 由宿主选本机第一个可用中文音色(这台是 Microsoft Huihui Desktop)。
|
|
8195
|
+
// 旧默认是 Edge 云端音色,本机没有模型/许可不允许随包分发。
|
|
8196
|
+
voice: { name: '', muted: false },
|
|
8166
8197
|
// 唤醒:点一下默认开;叫名字默认关(S6 才实现,先在设置里留位)
|
|
8167
8198
|
wake: { click: true, name: false, keyword: '' },
|
|
8168
8199
|
// 对话记忆默认开启且**不暴露开关**(追加决定 1):能力常开,用户只能在右键里查看/清空
|
|
@@ -8911,7 +8942,10 @@ window.__ModuleLoader__.load({
|
|
|
8911
8942
|
const def = DEFAULTS.petChat.voice;
|
|
8912
8943
|
const src = raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : {};
|
|
8913
8944
|
const old = legacy && typeof legacy === 'object' && !Array.isArray(legacy) ? legacy : {};
|
|
8914
|
-
const
|
|
8945
|
+
const picked = petChatStr(src.name, 120).trim() || petChatStr(old.voiceURI, 200).trim() || def.name;
|
|
8946
|
+
// pre.54:Edge neural 名(`zh-CN-XxxNeural` 这类)是云端音色,本机没有本地模型、
|
|
8947
|
+
// 也不允许随包分发 ⇒ 读档时迁成空串(= 宿主自动选本机第一个可用中文音色)。
|
|
8948
|
+
const name = /^[a-z]{2}-[A-Z]{2}-[A-Za-z0-9]+Neural$/.test(picked) ? '' : picked;
|
|
8915
8949
|
const muted = typeof src.muted === 'boolean' ? src.muted : old.enabled === false;
|
|
8916
8950
|
return { name, muted };
|
|
8917
8951
|
}
|
|
@@ -18800,17 +18834,22 @@ window.__ModuleLoader__.load({
|
|
|
18800
18834
|
}),
|
|
18801
18835
|
weT("显示桌宠"),
|
|
18802
18836
|
),
|
|
18803
|
-
React.createElement("label", {
|
|
18837
|
+
React.createElement("label", {
|
|
18838
|
+
// pre.53:这里一直是**桌宠音效**(cfg.sound,见 pet-layer.js 的 petVoice),
|
|
18839
|
+
// 不是朗读回答。旧文案「声音」会被误解成语音朗读开关 ⇒ 明确写成「桌宠音效」。
|
|
18840
|
+
title: weT("点击与动作音效;朗读回答的开关在「智能大肥鱼」页"),
|
|
18841
|
+
style: { display: "flex", gap: "6px", alignItems: "center", fontSize: "12px" },
|
|
18842
|
+
},
|
|
18804
18843
|
React.createElement("input", {
|
|
18805
18844
|
type: "checkbox",
|
|
18806
18845
|
checked: Boolean(cfg.sound),
|
|
18807
18846
|
disabled: Boolean(busy),
|
|
18808
18847
|
onChange: (e) => act("config", { sound: e.target.checked }, ""),
|
|
18809
18848
|
}),
|
|
18810
|
-
weT("
|
|
18849
|
+
weT("桌宠音效"),
|
|
18811
18850
|
),
|
|
18812
18851
|
React.createElement("label", { style: { display: "flex", gap: "4px", alignItems: "center", fontSize: "12px" } },
|
|
18813
|
-
weT("
|
|
18852
|
+
weT("音效音量"),
|
|
18814
18853
|
React.createElement("select", {
|
|
18815
18854
|
className: "we-picker__playlist-select",
|
|
18816
18855
|
value: String(cfg.volume == null ? 0.6 : cfg.volume),
|
|
@@ -18998,19 +19037,28 @@ window.__ModuleLoader__.load({
|
|
|
18998
19037
|
hasApiKey: false,
|
|
18999
19038
|
petName: '',
|
|
19000
19039
|
ownerName: '',
|
|
19040
|
+
// pre.53:朗读回答 = voice.muted 取反;音色 = voice.name(与 /pet-chat/config 同结构)。
|
|
19041
|
+
// pre.54:默认**空串** = 宿主自动选本机第一个可用中文音色(这台是 Microsoft Huihui Desktop);
|
|
19042
|
+
// 不再写死 Edge neural(云端音色,不能随包分发,本机也没有本地模型)。
|
|
19043
|
+
voice: { name: '', muted: false },
|
|
19001
19044
|
};
|
|
19002
19045
|
|
|
19003
19046
|
/**
|
|
19004
|
-
*
|
|
19005
|
-
* 1) 模型 Key 2) 名字(桌宠名 = 唤醒词 + 你的称呼) 3) 音色(v0.2 占位,灰色只读)
|
|
19047
|
+
* 设置页:模型 Key / 名字 / 称呼 / 音色 / **朗读回答开关** / 保存。
|
|
19006
19048
|
*
|
|
19007
|
-
*
|
|
19008
|
-
*
|
|
19049
|
+
* 朗读开关是 pre.53 补的:对应 `voice.muted`(旧版只有默认不静音,没有界面入口)。
|
|
19050
|
+
* 关闭后只出字幕、不合成语音(客户端 petTts 在 `speak()` 入口直接跳过,省算力)。
|
|
19009
19051
|
*/
|
|
19010
19052
|
/** apiJson 的返回是 { ok, status, data },而 /pet-chat/* 的 data 里还套一层 { ok, data }。
|
|
19011
19053
|
* 各调用点各写一遍「多解一层」就是 P1 那个"保存成功却显示保存失败"的来源 —— 统一到这里解。 */
|
|
19012
19054
|
const unwrapPetChat = (resp) => (resp && resp.data && resp.data.data) || (resp && resp.data) || {};
|
|
19013
19055
|
|
|
19056
|
+
/** 本机音色里选默认:优先中文,其次第一个 usable。 */
|
|
19057
|
+
function assistantDefaultVoice(list) {
|
|
19058
|
+
const items = (Array.isArray(list) ? list : []).filter((v) => v && v.usable === true);
|
|
19059
|
+
return items.find((v) => /^zh/i.test(String(v.lang || ''))) || items[0] || null;
|
|
19060
|
+
}
|
|
19061
|
+
|
|
19014
19062
|
function PetChatSettings() {
|
|
19015
19063
|
const [draft, setDraft] = React.useState(CHAT_DEFAULTS);
|
|
19016
19064
|
const [status, setStatus] = React.useState({ ok: null, text: '' });
|
|
@@ -19018,6 +19066,9 @@ window.__ModuleLoader__.load({
|
|
|
19018
19066
|
const [busy, setBusy] = React.useState(false);
|
|
19019
19067
|
// 工作能力插件(聚合携带)的安装状态;只读展示,不加开关
|
|
19020
19068
|
const [workPlugins, setWorkPlugins] = React.useState(null);
|
|
19069
|
+
// pre.54:本机真实可用音色(/tts/voices)与可下载目录(/tts/catalog)。
|
|
19070
|
+
const [voices, setVoices] = React.useState(null);
|
|
19071
|
+
const [catalog, setCatalog] = React.useState(null);
|
|
19021
19072
|
const update = React.useCallback((patch) => setDraft((prev) => ({ ...prev, ...patch })), []);
|
|
19022
19073
|
|
|
19023
19074
|
const load = React.useCallback(async () => {
|
|
@@ -19025,7 +19076,9 @@ window.__ModuleLoader__.load({
|
|
|
19025
19076
|
const resp = await apiJson('/pet-chat/config', { parse: 'always' });
|
|
19026
19077
|
const doc = unwrapPetChat(resp);
|
|
19027
19078
|
if (doc && doc.ok !== false) {
|
|
19028
|
-
|
|
19079
|
+
// voice 是嵌套对象:缺字段时补默认,避免 draft.voice 变 undefined 后开关读不了。
|
|
19080
|
+
const voice = Object.assign({}, CHAT_DEFAULTS.voice, (doc && doc.voice && typeof doc.voice === 'object') ? doc.voice : {});
|
|
19081
|
+
setDraft({ ...CHAT_DEFAULTS, ...doc, voice, apiKey: '' });
|
|
19029
19082
|
setStatus(doc.hasApiKey
|
|
19030
19083
|
? { ok: true, text: weT('已保存的 Key:{mask}', { mask: doc.apiKeyMasked || '••••' }) }
|
|
19031
19084
|
: { ok: null, text: weT('还没有填 API Key') });
|
|
@@ -19039,18 +19092,58 @@ window.__ModuleLoader__.load({
|
|
|
19039
19092
|
setWorkPlugins(unwrapPetChat(resp));
|
|
19040
19093
|
})();
|
|
19041
19094
|
}, []);
|
|
19095
|
+
// pre.54:只列本机真实可用音色;默认选第一个可用中文音色。
|
|
19096
|
+
React.useEffect(() => {
|
|
19097
|
+
void (async () => {
|
|
19098
|
+
const resp = await apiJson('/pet-chat/tts/voices', { parse: 'always' }).catch(() => null);
|
|
19099
|
+
const doc = unwrapPetChat(resp);
|
|
19100
|
+
const items = (doc && Array.isArray(doc.voices) ? doc.voices : []).filter((v) => v && v.usable === true);
|
|
19101
|
+
setVoices(items);
|
|
19102
|
+
const firstZh = assistantDefaultVoice(items);
|
|
19103
|
+
if (firstZh) {
|
|
19104
|
+
setDraft((prev) => {
|
|
19105
|
+
const cur = prev.voice || {};
|
|
19106
|
+
if (cur.name) return prev;
|
|
19107
|
+
return { ...prev, voice: Object.assign({}, cur, { name: firstZh.id }) };
|
|
19108
|
+
});
|
|
19109
|
+
}
|
|
19110
|
+
})();
|
|
19111
|
+
}, []);
|
|
19112
|
+
React.useEffect(() => {
|
|
19113
|
+
void (async () => {
|
|
19114
|
+
const resp = await apiJson('/pet-chat/tts/catalog', { parse: 'always' }).catch(() => null);
|
|
19115
|
+
const doc = unwrapPetChat(resp);
|
|
19116
|
+
if (doc && Array.isArray(doc.items)) setCatalog(doc.items);
|
|
19117
|
+
})();
|
|
19118
|
+
}, []);
|
|
19042
19119
|
|
|
19043
19120
|
const save = async () => {
|
|
19044
19121
|
setBusy(true);
|
|
19045
19122
|
try {
|
|
19046
|
-
// 填了 Key
|
|
19047
|
-
|
|
19123
|
+
// 填了 Key 就启用:面板不放"开启对话"开关,语义由 Key 的有无决定。
|
|
19124
|
+
// pre.53:voice 必须显式带上(宿主保存时会整体替换 voice 对象)。
|
|
19125
|
+
const voice = Object.assign({}, CHAT_DEFAULTS.voice, draft.voice || {});
|
|
19126
|
+
// pre.54:空音色 ⇒ 落成当前本机默认(列表没加载出来时仍保持空串,由宿主自动选)。
|
|
19127
|
+
if (!voice.name) {
|
|
19128
|
+
const fallback = assistantDefaultVoice(voices);
|
|
19129
|
+
if (fallback) voice.name = fallback.id;
|
|
19130
|
+
}
|
|
19131
|
+
const body = {
|
|
19132
|
+
...draft, voice,
|
|
19133
|
+
apiKey: draft.apiKey || '',
|
|
19134
|
+
enabled: Boolean(draft.apiKey) || Boolean(draft.hasApiKey),
|
|
19135
|
+
};
|
|
19048
19136
|
// ⚠️ 必须用 apiPostJson:apiJson 只拼 URL,不做 JSON.stringify,
|
|
19049
19137
|
// 传对象会被浏览器发成 "[object Object]" ⇒ 宿主回 400 bad-json ⇒ 界面永远"保存失败"。
|
|
19050
19138
|
const resp = await apiPostJson('/pet-chat/config', body, { parse: 'always' });
|
|
19051
19139
|
const doc = unwrapPetChat(resp);
|
|
19052
19140
|
if (doc.saved) {
|
|
19053
|
-
|
|
19141
|
+
// pre.53:保存后**立刻**生效 —— 静音时停掉正在念的那句,并刷新客户端缓存的 voice。
|
|
19142
|
+
if (voice.muted === true) {
|
|
19143
|
+
try { petSpeakStop(); } catch { /* 朗读是增强:停不下来也不能影响保存 */ }
|
|
19144
|
+
}
|
|
19145
|
+
try { void petChatRefreshVoice(); } catch { /* 刷新失败下次唤醒会再读 */ }
|
|
19146
|
+
setNote({ ok: true, text: voice.muted === true ? weT('已保存(朗读已关闭)') : weT('已保存') });
|
|
19054
19147
|
await load();
|
|
19055
19148
|
const state = await apiJson('/pet-chat/state', { parse: 'always' });
|
|
19056
19149
|
const sdoc = unwrapPetChat(state);
|
|
@@ -19068,9 +19161,11 @@ window.__ModuleLoader__.load({
|
|
|
19068
19161
|
}, weT(label), node);
|
|
19069
19162
|
|
|
19070
19163
|
const child = [];
|
|
19164
|
+
const voice = Object.assign({}, CHAT_DEFAULTS.voice, draft.voice || {});
|
|
19165
|
+
const voiceOn = voice.muted !== true;
|
|
19071
19166
|
child.push(React.createElement('div', { key: 'title', style: { fontWeight: 600, margin: '14px 0 4px', fontSize: '13px' } }, weT('桌宠语音助手')));
|
|
19072
19167
|
child.push(React.createElement('div', { key: 'hint', className: 'we-picker__hint', style: { margin: '0 0 6px' } },
|
|
19073
|
-
weT('
|
|
19168
|
+
weT('填 Key、起名字、选音色,再决定要不要朗读。主动搭话与记忆是固定策略;停止/恢复对它说一声就行。')));
|
|
19074
19169
|
|
|
19075
19170
|
child.push(React.createElement('div', { key: 'key', style: { display: 'flex', gap: '6px', alignItems: 'center', fontSize: '12px', margin: '4px 0' } },
|
|
19076
19171
|
weT('模型 Key'),
|
|
@@ -19091,12 +19186,68 @@ window.__ModuleLoader__.load({
|
|
|
19091
19186
|
style: { flex: 1, minWidth: 0 }, onChange: (e) => update({ ownerName: e.target.value }),
|
|
19092
19187
|
})));
|
|
19093
19188
|
|
|
19094
|
-
//
|
|
19095
|
-
child.push(
|
|
19096
|
-
|
|
19189
|
+
// pre.53:朗读开关 = voice.muted 取反。关闭后只出字幕、不合成语音(客户端入口直接跳过)。
|
|
19190
|
+
child.push(React.createElement('label', {
|
|
19191
|
+
key: 'speak-toggle',
|
|
19192
|
+
style: { display: 'flex', gap: '6px', alignItems: 'center', fontSize: '12px', margin: '6px 0 2px' },
|
|
19097
19193
|
},
|
|
19098
|
-
React.createElement('
|
|
19099
|
-
|
|
19194
|
+
React.createElement('input', {
|
|
19195
|
+
type: 'checkbox',
|
|
19196
|
+
checked: voiceOn,
|
|
19197
|
+
disabled: busy,
|
|
19198
|
+
onChange: (e) => update({ voice: Object.assign({}, voice, { muted: !e.target.checked }) }),
|
|
19199
|
+
}),
|
|
19200
|
+
weT('🔊 朗读回答'),
|
|
19201
|
+
));
|
|
19202
|
+
child.push(React.createElement('div', {
|
|
19203
|
+
key: 'speak-hint', className: 'we-picker__hint', style: { margin: '0 0 4px' },
|
|
19204
|
+
}, weT('关闭后只显示字幕、不合成语音(省算力);保存后立刻生效。')));
|
|
19205
|
+
|
|
19206
|
+
// pre.54(B3③):音色下拉只列**本机真实可用**(/tts/voices 的 usable=true)。
|
|
19207
|
+
// 不再有 4 个硬编码 Edge neural;默认选第一个可用中文音色(这台是 Microsoft Huihui Desktop)。
|
|
19208
|
+
const usableVoices = (Array.isArray(voices) ? voices : []).filter((v) => v && v.usable === true);
|
|
19209
|
+
const defaultVoice = assistantDefaultVoice(usableVoices);
|
|
19210
|
+
const currentVoiceName = voice.name || (defaultVoice ? defaultVoice.id : '');
|
|
19211
|
+
const voiceKnown = usableVoices.some((v) => String(v.id) === currentVoiceName);
|
|
19212
|
+
if (usableVoices.length || currentVoiceName) {
|
|
19213
|
+
child.push(row('voice', '音色', React.createElement('select', {
|
|
19214
|
+
className: 'we-picker__playlist-select',
|
|
19215
|
+
disabled: busy || !usableVoices.length,
|
|
19216
|
+
value: currentVoiceName,
|
|
19217
|
+
onChange: (e) => update({ voice: Object.assign({}, voice, { name: e.target.value }) }),
|
|
19218
|
+
},
|
|
19219
|
+
// 旧配置里的音色不在本机(或读取失败)也保留一行,避免下拉显示为空;
|
|
19220
|
+
// 真正合成时宿主会回落并在状态条提示。
|
|
19221
|
+
!voiceKnown && currentVoiceName
|
|
19222
|
+
? React.createElement('option', { key: 'stale', value: currentVoiceName }, currentVoiceName + ' ' + weT('(本机未检测到,将回落)'))
|
|
19223
|
+
: null,
|
|
19224
|
+
...usableVoices.map((v) => React.createElement('option', { key: v.id, value: v.id },
|
|
19225
|
+
v.label + (v.lang ? ' · ' + v.lang : '') + ' ' + weT('(系统音色,离线)'))),
|
|
19226
|
+
)));
|
|
19227
|
+
} else {
|
|
19228
|
+
child.push(React.createElement('div', {
|
|
19229
|
+
key: 'voice-empty', className: 'we-picker__hint', style: { margin: '4px 0' },
|
|
19230
|
+
}, voices === null ? weT('正在读取本机音色…') : weT('未检测到可用音色(系统 TTS 不可用)')));
|
|
19231
|
+
}
|
|
19232
|
+
child.push(React.createElement('div', {
|
|
19233
|
+
key: 'voice-hint', className: 'we-picker__hint', style: { margin: '0 0 4px' },
|
|
19234
|
+
}, weT('只列本机真实可用的系统音色(离线);云端音色不随包分发。')));
|
|
19235
|
+
|
|
19236
|
+
// pre.54(任务 C 骨架):可下载离线音色目录 —— 本版**只列目录,不谎称已支持**。
|
|
19237
|
+
if (Array.isArray(catalog) && catalog.length) {
|
|
19238
|
+
child.push(React.createElement('div', {
|
|
19239
|
+
key: 'catalog-head', style: { marginTop: '12px', fontSize: '12px', fontWeight: 600 },
|
|
19240
|
+
}, weT('可下载音色(即将支持)')));
|
|
19241
|
+
catalog.forEach((item) => {
|
|
19242
|
+
child.push(React.createElement('div', {
|
|
19243
|
+
key: 'cat-' + item.id, className: 'we-picker__hint', style: { margin: '2px 0' },
|
|
19244
|
+
}, (item.name || item.id) + ' · ' + (item.license || '') + ' · ' + (item.size || '')
|
|
19245
|
+
+ ' · ' + weT('即将支持')));
|
|
19246
|
+
});
|
|
19247
|
+
child.push(React.createElement('div', {
|
|
19248
|
+
key: 'catalog-hint', className: 'we-picker__hint', style: { margin: '2px 0 0' },
|
|
19249
|
+
}, weT('这些是开源离线方案,本版只接目录;合成接通后才会出现在上面的音色下拉里。')));
|
|
19250
|
+
}
|
|
19100
19251
|
|
|
19101
19252
|
child.push(React.createElement('div', { key: 'status', className: 'we-picker__hint', style: { margin: '6px 0', color: status.ok === false ? '#f87171' : undefined } },
|
|
19102
19253
|
status.text || weT('未连接')));
|
|
@@ -21164,20 +21315,24 @@ window.__ModuleLoader__.load({
|
|
|
21164
21315
|
|
|
21165
21316
|
// ── 内联模块:src/pet-bubble.js —— 头顶气泡:锚定桌宠头顶、跟随位置/缩放、逐字显示、最多 6 行、朗读结束 +3s 淡出(构建期注入;勿手改本段)──
|
|
21166
21317
|
/**
|
|
21167
|
-
* pet-bubble.js —
|
|
21318
|
+
* pet-bubble.js — 桌宠头顶的**双层气泡**(pre.54 重做)。
|
|
21168
21319
|
*
|
|
21169
|
-
*
|
|
21170
|
-
*
|
|
21171
|
-
*
|
|
21320
|
+
* 用户口径:提示/错误不能和回答挤在同一个框里。
|
|
21321
|
+
* ① 状态条(独立、细、可点、3–5 秒淡出):在听 / 在想 / 在说 / 没找到麦克风 / …
|
|
21322
|
+
* ② 回答气泡(只有回答,小尾巴指向桌宠):逐句显示、随朗读推进、长回答可滚。
|
|
21172
21323
|
*
|
|
21173
|
-
*
|
|
21174
|
-
*
|
|
21324
|
+
* 结构:
|
|
21325
|
+
* .we-pet-wrap(固定定位、列布局、整体锚定在桌宠头顶)
|
|
21326
|
+
* ├─ .we-pet-status(状态条;自带 data-state 控制 🎤/💭/🔊 动效)
|
|
21327
|
+
* └─ .we-pet-bubble(回答气泡;内部 .we-pet-bubble-text + 关闭按钮)
|
|
21328
|
+
*
|
|
21329
|
+
* 依赖全部注入(document / 定时器 / 锚点),Node 里能用假 DOM 跑判据;
|
|
21330
|
+
* 浏览器侧由 pet-chat.js 用真实 window 构造。`setState` 仍同步写 bubble 的
|
|
21331
|
+
* `data-state`(既有判据与桌宠动作读它),视觉三态则落在状态条上。
|
|
21175
21332
|
*/
|
|
21176
21333
|
function petBubbleCreate(deps) {
|
|
21177
21334
|
const options = deps || {};
|
|
21178
21335
|
const doc = options.document;
|
|
21179
|
-
// 没有定时器的环境(极简 DOM 替身的冒烟测试)不能因为裸 setTimeout 直接炸:
|
|
21180
|
-
// 退化成立即执行 / 空取消,气泡仍能显示,只是没有延迟淡出。
|
|
21181
21336
|
const setTimer = typeof options.setTimeout === 'function'
|
|
21182
21337
|
? options.setTimeout
|
|
21183
21338
|
: (typeof setTimeout === 'function' ? setTimeout : (fn) => { if (typeof fn === 'function') fn(); return 0; });
|
|
@@ -21185,56 +21340,61 @@ window.__ModuleLoader__.load({
|
|
|
21185
21340
|
? options.clearTimeout
|
|
21186
21341
|
: (typeof clearTimeout === 'function' ? clearTimeout : () => {});
|
|
21187
21342
|
const getAnchor = typeof options.getAnchor === 'function' ? options.getAnchor : () => null;
|
|
21188
|
-
|
|
21343
|
+
const onStatusClick = typeof options.onStatusClick === 'function' ? options.onStatusClick : null;
|
|
21189
21344
|
const MAX_LINES = 6;
|
|
21345
|
+
const BUBBLE_MAX_WIDTH = 320;
|
|
21346
|
+
const BUBBLE_MAX_HEIGHT = 160;
|
|
21190
21347
|
|
|
21191
21348
|
const raf = typeof options.requestAnimationFrame === 'function' ? options.requestAnimationFrame : null;
|
|
21192
21349
|
const cancelRaf = typeof options.cancelAnimationFrame === 'function' ? options.cancelAnimationFrame : null;
|
|
21193
21350
|
let rafId = null;
|
|
21194
21351
|
const onClose = typeof options.onClose === 'function' ? options.onClose : () => {};
|
|
21352
|
+
/** 独立冒烟(只 eval 本文件)时没有 weT;有就用词表,没有就回落中文原文。 */
|
|
21353
|
+
const tr = (text) => {
|
|
21354
|
+
try { return typeof weT === 'function' ? weT(text) : text; } catch { return text; }
|
|
21355
|
+
};
|
|
21356
|
+
|
|
21357
|
+
const CSS = [
|
|
21358
|
+
'.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%;}',
|
|
21359
|
+
// 回答气泡:半透明深色底 + 轻描边 + 14–16px 圆角;长回答 160px 内滚动。
|
|
21360
|
+
'.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;}',
|
|
21361
|
+
'.we-pet-bubble[data-visible="1"]{opacity:1;}',
|
|
21362
|
+
// 小尾巴指向桌宠(在气泡底部中央;不挡脸)。
|
|
21363
|
+
'.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);}',
|
|
21364
|
+
'.we-pet-bubble::-webkit-scrollbar{width:4px;}',
|
|
21365
|
+
'.we-pet-bubble::-webkit-scrollbar-thumb{background:rgba(255,255,255,.22);border-radius:2px;}',
|
|
21366
|
+
// 状态条:细、独立、可点、3–5 秒淡出。
|
|
21367
|
+
'.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;}',
|
|
21368
|
+
'.we-pet-status[data-visible="1"]{opacity:1;}',
|
|
21369
|
+
'.we-pet-status[data-state="listen"]::after{content:"";width:8px;height:8px;border-radius:50%;background:currentColor;opacity:.7;animation:we-pet-status-pulse 1s ease-in-out infinite;}',
|
|
21370
|
+
'.we-pet-status[data-state="think"]::after{content:"…";margin-left:1px;opacity:.8;animation:we-pet-status-blink 1.1s steps(2,end) infinite;}',
|
|
21371
|
+
'.we-pet-status[data-state="speak"]::after{content:"";display:block;width:28px;height:10px;opacity:.85;background:linear-gradient(currentColor,currentColor) 0 100%/3px 45% no-repeat,linear-gradient(currentColor,currentColor) 7px 100%/3px 90% no-repeat,linear-gradient(currentColor,currentColor) 14px 100%/3px 60% no-repeat,linear-gradient(currentColor,currentColor) 21px 100%/3px 100% no-repeat;transform-origin:0 100%;animation:we-pet-status-wave .8s ease-in-out infinite alternate;}',
|
|
21372
|
+
'.we-pet-status[data-state="error"]{border-color:rgba(255,120,120,.45);}',
|
|
21373
|
+
'@keyframes we-pet-status-wave{from{transform:scaleY(.55);opacity:.55;}to{transform:scaleY(1);opacity:.95;}}',
|
|
21374
|
+
'@keyframes we-pet-status-pulse{0%,100%{transform:scale(.8);opacity:.45;}50%{transform:scale(1.15);opacity:.9;}}',
|
|
21375
|
+
'@keyframes we-pet-status-blink{0%,60%{opacity:.35;}100%{opacity:.9;}}',
|
|
21376
|
+
].join('');
|
|
21377
|
+
|
|
21378
|
+
let wrap = null;
|
|
21379
|
+
let el = null; // 回答气泡
|
|
21380
|
+
let textEl = null;
|
|
21381
|
+
let statusEl = null;
|
|
21382
|
+
let text = '';
|
|
21383
|
+
let visible = false;
|
|
21384
|
+
let fadeTimer = null;
|
|
21385
|
+
let statusTimer = null;
|
|
21386
|
+
let stylesInjected = false;
|
|
21195
21387
|
|
|
21196
|
-
/** P3:打开期间每帧跟一次(拖动/缩放/窗口变化都能跟上),关闭时停。 */
|
|
21197
21388
|
function startFollow() {
|
|
21198
21389
|
if (!raf || rafId != null) return;
|
|
21199
|
-
const step = () => {
|
|
21200
|
-
place();
|
|
21201
|
-
rafId = raf(step);
|
|
21202
|
-
};
|
|
21390
|
+
const step = () => { place(); rafId = raf(step); };
|
|
21203
21391
|
rafId = raf(step);
|
|
21204
21392
|
}
|
|
21205
|
-
|
|
21206
21393
|
function stopFollow() {
|
|
21207
21394
|
if (rafId != null && cancelRaf) { try { cancelRaf(rafId); } catch { /* ignore */ } }
|
|
21208
21395
|
rafId = null;
|
|
21209
21396
|
}
|
|
21210
21397
|
|
|
21211
|
-
let el = null;
|
|
21212
|
-
let text = '';
|
|
21213
|
-
let visible = false;
|
|
21214
|
-
let fadeTimer = null;
|
|
21215
|
-
let stylesInjected = false;
|
|
21216
|
-
|
|
21217
|
-
/**
|
|
21218
|
-
* 三态与“说”的波形(P1):
|
|
21219
|
-
* listen / think / speak / idle —— 只改 `data-state`,由一小段**无颜色**的 CSS 表现:
|
|
21220
|
-
* · listen:正文前的实心点;think:省略号;speak:正文下一排四根跳动竖条。
|
|
21221
|
-
* 用伪元素而不是新增 DOM 子节点,是因为气泡正文一直用 `textContent` 整体写入
|
|
21222
|
-
* (设 textContent 会把子节点一起清掉);伪元素不会被它清掉,也不会引入第二种“文字容器”。
|
|
21223
|
-
*/
|
|
21224
|
-
const WAVE_CSS = [
|
|
21225
|
-
'.we-pet-bubble[data-state="listen"]::before{content:"●";margin-right:4px;opacity:.55;}',
|
|
21226
|
-
'.we-pet-bubble[data-state="think"]::before{content:"…";margin-right:4px;opacity:.6;}',
|
|
21227
|
-
'.we-pet-bubble[data-state="speak"]::after{content:"";display:block;width:34px;height:10px;margin-top:6px;opacity:.85;',
|
|
21228
|
-
'background:',
|
|
21229
|
-
'linear-gradient(currentColor,currentColor) 0 100%/3px 45% no-repeat,',
|
|
21230
|
-
'linear-gradient(currentColor,currentColor) 8px 100%/3px 90% no-repeat,',
|
|
21231
|
-
'linear-gradient(currentColor,currentColor) 16px 100%/3px 60% no-repeat,',
|
|
21232
|
-
'linear-gradient(currentColor,currentColor) 24px 100%/3px 100% no-repeat;',
|
|
21233
|
-
'transform-origin:0 100%;animation:we-pet-bubble-wave .8s ease-in-out infinite alternate;}',
|
|
21234
|
-
'@keyframes we-pet-bubble-wave{from{transform:scaleY(.55);opacity:.55;}to{transform:scaleY(1);opacity:.95;}}',
|
|
21235
|
-
].join('');
|
|
21236
|
-
|
|
21237
|
-
/** 只注入一次;没有 head 的环境(判据的极简 DOM)静默跳过。 */
|
|
21238
21398
|
function ensureStyles() {
|
|
21239
21399
|
if (stylesInjected) return;
|
|
21240
21400
|
stylesInjected = true;
|
|
@@ -21242,73 +21402,86 @@ window.__ModuleLoader__.load({
|
|
|
21242
21402
|
if (!doc || !doc.head || typeof doc.createElement !== 'function') return;
|
|
21243
21403
|
const style = doc.createElement('style');
|
|
21244
21404
|
style.setAttribute('data-folia-pet-bubble', '1');
|
|
21245
|
-
style.textContent =
|
|
21405
|
+
style.textContent = CSS;
|
|
21246
21406
|
if (typeof doc.head.appendChild === 'function') doc.head.appendChild(style);
|
|
21247
21407
|
} catch { /* 气泡是装饰层,样式失败不能影响显示 */ }
|
|
21248
21408
|
}
|
|
21249
21409
|
|
|
21250
|
-
/** 锚点 →
|
|
21410
|
+
/** 锚点 → 整组位置:水平居中于桌宠,垂直贴在头顶上方。 */
|
|
21251
21411
|
function place() {
|
|
21252
|
-
if (!
|
|
21412
|
+
if (!wrap) return;
|
|
21253
21413
|
const anchor = getAnchor();
|
|
21254
21414
|
if (!anchor) return;
|
|
21255
21415
|
const width = Number(anchor.width) || 0;
|
|
21256
|
-
const height = Number(anchor.height) || 0;
|
|
21257
21416
|
const left = Math.round(Number(anchor.left || 0) + width / 2);
|
|
21258
21417
|
const top = Math.round(Number(anchor.top || 0) - 8);
|
|
21259
|
-
|
|
21260
|
-
|
|
21261
|
-
|
|
21262
|
-
el.style.transform = `translate(-50%, -100%) scale(${Number(anchor.scale) || 1})`;
|
|
21418
|
+
wrap.style.left = `${left}px`;
|
|
21419
|
+
wrap.style.top = `${top}px`;
|
|
21420
|
+
wrap.style.transform = `translate(-50%, -100%) scale(${Number(anchor.scale) || 1})`;
|
|
21263
21421
|
}
|
|
21264
21422
|
|
|
21265
21423
|
function ensure() {
|
|
21266
|
-
if (
|
|
21424
|
+
if (wrap || !doc || typeof doc.createElement !== 'function') return wrap;
|
|
21425
|
+
ensureStyles();
|
|
21426
|
+
wrap = doc.createElement('div');
|
|
21427
|
+
wrap.className = 'we-pet-wrap';
|
|
21428
|
+
|
|
21429
|
+
statusEl = doc.createElement('div');
|
|
21430
|
+
statusEl.className = 'we-pet-status';
|
|
21431
|
+
statusEl.dataset = statusEl.dataset || {};
|
|
21432
|
+
statusEl.setAttribute('role', 'status');
|
|
21433
|
+
statusEl.setAttribute('aria-live', 'polite');
|
|
21434
|
+
statusEl.addEventListener('click', () => {
|
|
21435
|
+
try { if (onStatusClick) onStatusClick(); else hideStatus(0); } catch { /* ignore */ }
|
|
21436
|
+
});
|
|
21437
|
+
|
|
21267
21438
|
el = doc.createElement('div');
|
|
21268
21439
|
el.className = 'we-pet-bubble';
|
|
21269
|
-
el.
|
|
21270
|
-
el.style.
|
|
21271
|
-
el.style.
|
|
21272
|
-
|
|
21273
|
-
el.
|
|
21274
|
-
|
|
21275
|
-
|
|
21276
|
-
|
|
21277
|
-
el.
|
|
21278
|
-
|
|
21279
|
-
|
|
21280
|
-
|
|
21281
|
-
|
|
21282
|
-
|
|
21283
|
-
|
|
21284
|
-
|
|
21285
|
-
|
|
21286
|
-
|
|
21287
|
-
|
|
21288
|
-
|
|
21289
|
-
|
|
21290
|
-
|
|
21291
|
-
|
|
21292
|
-
|
|
21293
|
-
|
|
21294
|
-
|
|
21295
|
-
}
|
|
21296
|
-
el.style.position = 'fixed';
|
|
21297
|
-
if (doc.body && typeof doc.body.appendChild === 'function') doc.body.appendChild(el);
|
|
21298
|
-
return el;
|
|
21440
|
+
el.dataset = el.dataset || {};
|
|
21441
|
+
el.style.position = 'relative';
|
|
21442
|
+
el.style.pointerEvents = 'auto';
|
|
21443
|
+
// 点气泡 = 收起(二选一里的“收起”;不复制,避免误触剪贴板)。拖选文字时不触发 click。
|
|
21444
|
+
el.addEventListener('click', () => { hide(0); onClose(); });
|
|
21445
|
+
|
|
21446
|
+
textEl = doc.createElement('div');
|
|
21447
|
+
textEl.className = 'we-pet-bubble-text';
|
|
21448
|
+
if (typeof el.appendChild === 'function') el.appendChild(textEl);
|
|
21449
|
+
|
|
21450
|
+
const close = doc.createElement('button');
|
|
21451
|
+
close.className = 'pet-bubble-close';
|
|
21452
|
+
close.setAttribute('aria-label', '关闭');
|
|
21453
|
+
close.textContent = '×';
|
|
21454
|
+
close.style.cssText = 'position:absolute;top:-6px;right:-6px;width:18px;height:18px;border-radius:50%;border:none;background:#333;color:#ddd;font-size:12px;line-height:1;cursor:pointer;pointer-events:auto;';
|
|
21455
|
+
close.addEventListener('click', (event) => {
|
|
21456
|
+
// 阻止冒泡到气泡(否则同一次点击会触发两次 onClose)。
|
|
21457
|
+
try { if (event && typeof event.stopPropagation === 'function') event.stopPropagation(); } catch { /* ignore */ }
|
|
21458
|
+
hide(0);
|
|
21459
|
+
onClose();
|
|
21460
|
+
});
|
|
21461
|
+
if (typeof el.appendChild === 'function') el.appendChild(close);
|
|
21462
|
+
|
|
21463
|
+
if (typeof wrap.appendChild === 'function') { wrap.appendChild(statusEl); wrap.appendChild(el); }
|
|
21464
|
+
if (doc.body && typeof doc.body.appendChild === 'function') doc.body.appendChild(wrap);
|
|
21465
|
+
return wrap;
|
|
21299
21466
|
}
|
|
21300
21467
|
|
|
21301
21468
|
function cancelFade() {
|
|
21302
21469
|
if (fadeTimer != null) { clearTimer(fadeTimer); fadeTimer = null; }
|
|
21303
21470
|
}
|
|
21471
|
+
function cancelStatus() {
|
|
21472
|
+
if (statusTimer != null) { clearTimer(statusTimer); statusTimer = null; }
|
|
21473
|
+
}
|
|
21474
|
+
function statusVisible() {
|
|
21475
|
+
return Boolean(statusEl && statusEl.style && String(statusEl.style.opacity) === '1');
|
|
21476
|
+
}
|
|
21304
21477
|
|
|
21305
|
-
/**
|
|
21478
|
+
/** 显示(或继续显示)一段**回答**;状态/错误请走 `status()`。 */
|
|
21306
21479
|
function show(nextText) {
|
|
21307
21480
|
if (!ensure()) return;
|
|
21308
21481
|
cancelFade();
|
|
21309
21482
|
text = String(nextText == null ? '' : nextText);
|
|
21310
|
-
|
|
21311
|
-
el.style.opacity = '1';
|
|
21483
|
+
if (textEl) textEl.textContent = text;
|
|
21484
|
+
if (el) { el.dataset.visible = '1'; el.style.opacity = '1'; }
|
|
21312
21485
|
visible = true;
|
|
21313
21486
|
place();
|
|
21314
21487
|
startFollow();
|
|
@@ -21319,53 +21492,120 @@ window.__ModuleLoader__.load({
|
|
|
21319
21492
|
if (!ensure()) return;
|
|
21320
21493
|
cancelFade();
|
|
21321
21494
|
text += String(chunk == null ? '' : chunk);
|
|
21322
|
-
|
|
21323
|
-
el.style.opacity = '1';
|
|
21495
|
+
if (textEl) textEl.textContent = text;
|
|
21496
|
+
if (el) { el.dataset.visible = '1'; el.style.opacity = '1'; }
|
|
21324
21497
|
visible = true;
|
|
21498
|
+
place();
|
|
21325
21499
|
startFollow();
|
|
21326
21500
|
}
|
|
21327
21501
|
|
|
21328
|
-
/**
|
|
21502
|
+
/** 隐藏回答气泡;带 delay 时先挂着,到点淡出(朗读结束后 +3s 用这个)。 */
|
|
21329
21503
|
function hide(delayMs) {
|
|
21330
21504
|
if (!el) return;
|
|
21331
21505
|
cancelFade();
|
|
21332
21506
|
const delay = Number(delayMs) || 0;
|
|
21333
21507
|
const doHide = () => {
|
|
21334
21508
|
fadeTimer = null;
|
|
21335
|
-
stopFollow();
|
|
21509
|
+
if (!statusVisible()) stopFollow();
|
|
21336
21510
|
el.style.opacity = '0';
|
|
21511
|
+
if (el.dataset) el.dataset.visible = '0';
|
|
21337
21512
|
visible = false;
|
|
21338
21513
|
};
|
|
21339
21514
|
if (delay > 0) { fadeTimer = setTimer(doHide, delay); return; }
|
|
21340
21515
|
doHide();
|
|
21341
21516
|
}
|
|
21342
21517
|
|
|
21343
|
-
/**
|
|
21518
|
+
/** 显示状态条(错误/提示/在听/在想/在说)。 */
|
|
21519
|
+
function status(kind, message, opts) {
|
|
21520
|
+
if (!ensure()) return;
|
|
21521
|
+
cancelStatus();
|
|
21522
|
+
const o = opts || {};
|
|
21523
|
+
const k = String(kind || 'info');
|
|
21524
|
+
const icon = k === 'error' ? '🔴 ' : (k === 'info' ? 'ℹ️ ' : '');
|
|
21525
|
+
if (statusEl) {
|
|
21526
|
+
statusEl.dataset.state = k;
|
|
21527
|
+
statusEl.textContent = icon + String(message == null ? '' : message);
|
|
21528
|
+
statusEl.dataset.visible = '1';
|
|
21529
|
+
statusEl.style.opacity = '1';
|
|
21530
|
+
}
|
|
21531
|
+
// 状态条可能独立于回答气泡出现(没麦克风/在听)—— 也必须锚在桌宠头顶。
|
|
21532
|
+
place();
|
|
21533
|
+
startFollow();
|
|
21534
|
+
const delay = o.delay !== undefined ? Number(o.delay) || 0 : (k === 'error' ? 6000 : 4000);
|
|
21535
|
+
if (delay > 0) statusTimer = setTimer(() => { statusTimer = null; hideStatus(0); }, delay);
|
|
21536
|
+
}
|
|
21537
|
+
|
|
21538
|
+
/** 隐藏状态条(独立于回答气泡)。 */
|
|
21539
|
+
function hideStatus(delayMs) {
|
|
21540
|
+
if (!statusEl) return;
|
|
21541
|
+
cancelStatus();
|
|
21542
|
+
const delay = Number(delayMs) || 0;
|
|
21543
|
+
const doHide = () => {
|
|
21544
|
+
statusTimer = null;
|
|
21545
|
+
statusEl.style.opacity = '0';
|
|
21546
|
+
if (statusEl.dataset) { statusEl.dataset.visible = '0'; statusEl.dataset.state = ''; }
|
|
21547
|
+
if (!visible) stopFollow();
|
|
21548
|
+
};
|
|
21549
|
+
if (delay > 0) { statusTimer = setTimer(doHide, delay); return; }
|
|
21550
|
+
doHide();
|
|
21551
|
+
}
|
|
21552
|
+
|
|
21553
|
+
/** 三态:听 / 想 / 说(落在状态条上;bubble 的 data-state 同步保留给既有判据)。 */
|
|
21344
21554
|
function setState(kind) {
|
|
21345
21555
|
if (!ensure()) return;
|
|
21346
|
-
|
|
21347
|
-
el.dataset
|
|
21348
|
-
|
|
21556
|
+
const k = String(kind || '');
|
|
21557
|
+
if (el && el.dataset) el.dataset.state = k;
|
|
21558
|
+
if (k === 'idle') {
|
|
21559
|
+
// 错误/提示正在显示时不许被 idle 抹掉(TTS 错误后常紧跟一个 idle)。
|
|
21560
|
+
const cur = statusEl && statusEl.dataset ? String(statusEl.dataset.state || '') : '';
|
|
21561
|
+
if (cur === 'error' || cur === 'info') return;
|
|
21562
|
+
cancelStatus();
|
|
21563
|
+
hideStatus(1200);
|
|
21564
|
+
return;
|
|
21565
|
+
}
|
|
21566
|
+
const labels = { listen: '🎤 ' + tr('在听'), think: '💭 ' + tr('在想'), speak: '🔊 ' + tr('在说') };
|
|
21567
|
+
const label = labels[k];
|
|
21568
|
+
if (!label) return;
|
|
21569
|
+
// 错误/提示(含音色回落)在它自己的 3–6 秒窗口内优先于三态,别被下一句“在说”立刻盖掉。
|
|
21570
|
+
const curState = statusEl && statusEl.dataset ? String(statusEl.dataset.state || '') : '';
|
|
21571
|
+
if (curState === 'error' || curState === 'info') return;
|
|
21572
|
+
cancelStatus();
|
|
21573
|
+
if (statusEl) {
|
|
21574
|
+
statusEl.dataset.state = k;
|
|
21575
|
+
statusEl.textContent = label;
|
|
21576
|
+
statusEl.dataset.visible = '1';
|
|
21577
|
+
statusEl.style.opacity = '1';
|
|
21578
|
+
}
|
|
21579
|
+
place();
|
|
21580
|
+
startFollow();
|
|
21349
21581
|
}
|
|
21350
21582
|
|
|
21351
21583
|
function isVisible() { return visible; }
|
|
21352
21584
|
|
|
21353
21585
|
function dispose() {
|
|
21354
21586
|
cancelFade();
|
|
21587
|
+
cancelStatus();
|
|
21355
21588
|
stopFollow();
|
|
21356
|
-
if (
|
|
21589
|
+
if (wrap && typeof wrap.remove === 'function') wrap.remove();
|
|
21590
|
+
wrap = null;
|
|
21357
21591
|
el = null;
|
|
21592
|
+
textEl = null;
|
|
21593
|
+
statusEl = null;
|
|
21358
21594
|
text = '';
|
|
21359
21595
|
visible = false;
|
|
21360
21596
|
}
|
|
21361
21597
|
|
|
21362
21598
|
return {
|
|
21363
|
-
show, append, hide, setState, place, isVisible, dispose,
|
|
21599
|
+
show, append, hide, setState, status, hideStatus, place, isVisible, dispose,
|
|
21364
21600
|
get following() { return rafId != null; },
|
|
21365
|
-
// 判据要读的内部状态
|
|
21366
21601
|
get text() { return text; },
|
|
21367
21602
|
get element() { return el; },
|
|
21603
|
+
get textElement() { return textEl; },
|
|
21604
|
+
get statusElement() { return statusEl; },
|
|
21605
|
+
get wrapElement() { return wrap; },
|
|
21368
21606
|
MAX_LINES,
|
|
21607
|
+
BUBBLE_MAX_WIDTH,
|
|
21608
|
+
BUBBLE_MAX_HEIGHT,
|
|
21369
21609
|
};
|
|
21370
21610
|
}
|
|
21371
21611
|
|
|
@@ -21470,6 +21710,7 @@ window.__ModuleLoader__.load({
|
|
|
21470
21710
|
const onState = typeof options.onState === 'function' ? options.onState : () => {};
|
|
21471
21711
|
const onError = typeof options.onError === 'function' ? options.onError : () => {};
|
|
21472
21712
|
const onIdle = typeof options.onIdle === 'function' ? options.onIdle : () => {};
|
|
21713
|
+
const onVoiceFallback = typeof options.onVoiceFallback === 'function' ? options.onVoiceFallback : () => {};
|
|
21473
21714
|
const log = typeof options.log === 'function' ? options.log : () => {};
|
|
21474
21715
|
|
|
21475
21716
|
let generation = 0;
|
|
@@ -21478,6 +21719,8 @@ window.__ModuleLoader__.load({
|
|
|
21478
21719
|
let playing = null;
|
|
21479
21720
|
let prefetch = null;
|
|
21480
21721
|
let disposed = false;
|
|
21722
|
+
/** pre.54:音色回落只提示一次(每个实例),避免每句都弹状态条。 */
|
|
21723
|
+
let voiceFallbackWarned = false;
|
|
21481
21724
|
|
|
21482
21725
|
function revoke(url) {
|
|
21483
21726
|
if (!url || !UrlApi || typeof UrlApi.revokeObjectURL !== 'function') return;
|
|
@@ -21509,7 +21752,19 @@ window.__ModuleLoader__.load({
|
|
|
21509
21752
|
} catch { /* 非 JSON 错误体 */ }
|
|
21510
21753
|
return { error: { code: 'tts-failed', status: (res && res.status) || 0, hint: hint || '语音合成失败' } };
|
|
21511
21754
|
}
|
|
21755
|
+
// ⚠️ blob() 必须紧跟 apiFetch 同一个 try 调用窗(verify-api-client ①b 的判据);
|
|
21756
|
+
// 音色回落的响应头检查放在拿到 blob 之后,不隔断这层关系。
|
|
21512
21757
|
blob = await res.response.blob();
|
|
21758
|
+
try {
|
|
21759
|
+
const headers = res.response.headers;
|
|
21760
|
+
const fb = headers && typeof headers.get === 'function' ? headers.get('x-folia-tts-fallback') : '';
|
|
21761
|
+
if (fb === '1' && !voiceFallbackWarned) {
|
|
21762
|
+
voiceFallbackWarned = true;
|
|
21763
|
+
let actual = '';
|
|
21764
|
+
try { actual = decodeURIComponent(String(headers.get('x-folia-tts-voice') || '')); } catch { actual = ''; }
|
|
21765
|
+
onVoiceFallback(actual || '');
|
|
21766
|
+
}
|
|
21767
|
+
} catch { /* 头读不到不影响播放 */ }
|
|
21513
21768
|
} catch (error) {
|
|
21514
21769
|
if (!res) return { error: { code: 'tts-network', hint: '连不上宿主,语音没出来' } };
|
|
21515
21770
|
return { error: { code: 'tts-read-failed', hint: '读不到语音数据' } };
|
|
@@ -21760,6 +22015,15 @@ window.__ModuleLoader__.load({
|
|
|
21760
22015
|
return { left: rect.left, top: rect.top, width: rect.width, height: rect.height, scale };
|
|
21761
22016
|
} catch { return null; }
|
|
21762
22017
|
},
|
|
22018
|
+
// 状态条可点:错误/提示 ⇒ 打开打字入口;在听/在想/在说 ⇒ 停止(与再点桌宠同一语义)。
|
|
22019
|
+
onStatusClick: () => {
|
|
22020
|
+
try {
|
|
22021
|
+
const st = petBubble.statusElement && petBubble.statusElement.dataset
|
|
22022
|
+
? String(petBubble.statusElement.dataset.state || '') : '';
|
|
22023
|
+
if (st === 'error' || st === 'info') petChatOpenTyping();
|
|
22024
|
+
else petVoiceIn.stop();
|
|
22025
|
+
} catch { /* 状态条点击是增强 */ }
|
|
22026
|
+
},
|
|
21763
22027
|
});
|
|
21764
22028
|
|
|
21765
22029
|
/**
|
|
@@ -21767,7 +22031,8 @@ window.__ModuleLoader__.load({
|
|
|
21767
22031
|
* 然后轮询 /pet-chat/asr 把下载进度显示在气泡里。
|
|
21768
22032
|
* 这样用户装完插件直接说话就行,不需要去 DSH 设置里找「语音输入」开关。
|
|
21769
22033
|
*/
|
|
21770
|
-
async function ensureAsrReady() {
|
|
22034
|
+
async function ensureAsrReady() {
|
|
22035
|
+
petBubble.setState('think');
|
|
21771
22036
|
try {
|
|
21772
22037
|
await apiPostJson('/pet-chat/asr', {}, { parse: 'always' });
|
|
21773
22038
|
} catch { /* 触发失败也要继续轮询看状态 */ }
|
|
@@ -21776,27 +22041,23 @@ window.__ModuleLoader__.load({
|
|
|
21776
22041
|
const resp = await apiJson('/pet-chat/asr', { parse: 'always' }).catch(() => null);
|
|
21777
22042
|
const doc = (resp && resp.data) || {};
|
|
21778
22043
|
if (doc.ready) {
|
|
21779
|
-
petBubble.
|
|
21780
|
-
petBubble.hide(3000);
|
|
22044
|
+
petBubble.status('info', '语音准备好了,点我再说一次', { delay: 3000 });
|
|
21781
22045
|
return;
|
|
21782
22046
|
}
|
|
21783
22047
|
if (doc.available === false) {
|
|
21784
|
-
petBubble.
|
|
21785
|
-
petBubble.hide(3000);
|
|
22048
|
+
petBubble.status('error', '这个 DSH 版本没有语音转写服务,先打字吧', { delay: 5000 });
|
|
21786
22049
|
return;
|
|
21787
22050
|
}
|
|
21788
22051
|
if (doc.phase === 'failed') {
|
|
21789
|
-
petBubble.
|
|
21790
|
-
petBubble.hide(3000);
|
|
22052
|
+
petBubble.status('error', '语音模型下载失败,稍后再点我试试', { delay: 5000 });
|
|
21791
22053
|
return;
|
|
21792
22054
|
}
|
|
21793
22055
|
const pct = doc.totalBytes > 0 && doc.completedBytes >= 0
|
|
21794
22056
|
? Math.max(0, Math.min(100, Math.round((doc.completedBytes / doc.totalBytes) * 100)))
|
|
21795
22057
|
: null;
|
|
21796
|
-
petBubble.
|
|
22058
|
+
petBubble.status('info', pct === null ? '语音模型准备中…' : `语音模型下载中 ${pct}%`, { delay: 0 });
|
|
21797
22059
|
}
|
|
21798
|
-
petBubble.
|
|
21799
|
-
petBubble.hide(3000);
|
|
22060
|
+
petBubble.status('info', '语音模型还没下完,过一会儿再点我', { delay: 5000 });
|
|
21800
22061
|
}
|
|
21801
22062
|
|
|
21802
22063
|
/**
|
|
@@ -21840,12 +22101,16 @@ window.__ModuleLoader__.load({
|
|
|
21840
22101
|
},
|
|
21841
22102
|
onState: (kind) => { petBubble.setState(kind); },
|
|
21842
22103
|
onIdle: () => { petBubble.hide(3000); },
|
|
22104
|
+
onVoiceFallback: (actual) => {
|
|
22105
|
+
// pre.54(B3④):宿主回落到本机默认音色时明确提示,不静默换音色。
|
|
22106
|
+
petBubble.status('info', actual
|
|
22107
|
+
? weT('所选音色不可用,已用系统默认:{name}', { name: actual })
|
|
22108
|
+
: weT('所选音色不可用,已用系统默认音色'), { delay: 5000 });
|
|
22109
|
+
},
|
|
21843
22110
|
onError: (error) => {
|
|
21844
|
-
//
|
|
22111
|
+
// 失败只进**状态条**(不污染回答气泡、不弹窗);不静默 —— 用户至少知道为什么没声音
|
|
21845
22112
|
const hint = (error && error.hint) || '语音没出来';
|
|
21846
|
-
petBubble.
|
|
21847
|
-
petBubble.show(hint);
|
|
21848
|
-
petBubble.hide(4000);
|
|
22113
|
+
petBubble.status('error', hint, { delay: 5000 });
|
|
21849
22114
|
},
|
|
21850
22115
|
});
|
|
21851
22116
|
|
|
@@ -21882,15 +22147,13 @@ window.__ModuleLoader__.load({
|
|
|
21882
22147
|
parse: 'none',
|
|
21883
22148
|
});
|
|
21884
22149
|
} catch {
|
|
21885
|
-
petBubble.
|
|
21886
|
-
petBubble.hide(3000);
|
|
22150
|
+
petBubble.status('error', '连不上宿主,稍后再试', { delay: 4000 });
|
|
21887
22151
|
if (typeof onState === 'function') onState({ state: 'idle' });
|
|
21888
22152
|
return;
|
|
21889
22153
|
}
|
|
21890
22154
|
const stream = res && res.response;
|
|
21891
22155
|
if (!res || !res.ok || !stream || !stream.body) {
|
|
21892
|
-
petBubble.
|
|
21893
|
-
petBubble.hide(3000);
|
|
22156
|
+
petBubble.status('error', res && res.status === 401 ? '模型 Key 无效,去设置里换一把' : '这次没答上来,再试一次', { delay: 4000 });
|
|
21894
22157
|
if (typeof onState === 'function') onState({ state: 'idle' });
|
|
21895
22158
|
return;
|
|
21896
22159
|
}
|
|
@@ -21976,16 +22239,15 @@ window.__ModuleLoader__.load({
|
|
|
21976
22239
|
network: '网络不太顺,重试或直接打字',
|
|
21977
22240
|
};
|
|
21978
22241
|
const message = map[error && error.code] || '没听清,直接打字也行';
|
|
21979
|
-
|
|
21980
|
-
// asr-not-ready:自动触发模型下载并在气泡里显示进度,用户不用去 DSH 设置里找开关
|
|
22242
|
+
// asr-not-ready:自动触发模型下载并在状态条里显示进度,用户不用去 DSH 设置里找开关
|
|
21981
22243
|
if (error && error.code === 'asr-not-ready') {
|
|
22244
|
+
petBubble.status('info', message, { delay: 0 });
|
|
21982
22245
|
void ensureAsrReady();
|
|
21983
22246
|
return;
|
|
21984
22247
|
}
|
|
21985
22248
|
// ⚠️ 失败**不要自动弹输入框**(用户会以为"点一下就变对话框")。
|
|
21986
22249
|
// 打字入口只从右键菜单「对话」进;这里只给一行提示 + 引导。
|
|
21987
|
-
petBubble.
|
|
21988
|
-
petBubble.hide(4000);
|
|
22250
|
+
petBubble.status('error', `${message}(右键 → 对话)`, { delay: 5000 });
|
|
21989
22251
|
},
|
|
21990
22252
|
});
|
|
21991
22253
|
|
|
@@ -22041,7 +22303,7 @@ window.__ModuleLoader__.load({
|
|
|
22041
22303
|
// **任何一次唤醒都是 barge-in**:先把正在念的停掉,再决定是收起还是开麦。
|
|
22042
22304
|
petSpeakStop();
|
|
22043
22305
|
void petChatRefreshVoice();
|
|
22044
|
-
if (petBubble.isVisible()) { petBubble.hide(0); petVoiceIn.stop(); return; }
|
|
22306
|
+
if (petBubble.isVisible()) { petBubble.hide(0); petBubble.hideStatus(0); petVoiceIn.stop(); return; }
|
|
22045
22307
|
petBubble.setState('listen');
|
|
22046
22308
|
// 先看语音模型是否就绪;没就绪就**立刻**开始下载并在气泡里显示进度,
|
|
22047
22309
|
// 不要等用户先撞一次 asr-not-ready 才被动触发(那之前会像"没反应")。
|
|
@@ -22054,7 +22316,7 @@ window.__ModuleLoader__.load({
|
|
|
22054
22316
|
})();
|
|
22055
22317
|
void petVoiceIn.start().then((started) => {
|
|
22056
22318
|
if (!started) return; // 失败分支已经由 onError 提示(回落打字)
|
|
22057
|
-
|
|
22319
|
+
// 状态条已经由 setState('listen') 显示「🎤 在听」,这里不再往回答气泡里塞提示。
|
|
22058
22320
|
});
|
|
22059
22321
|
}
|
|
22060
22322
|
|