@aiyiran/myclaw 1.1.210 → 1.1.211
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/assets/myclaw-inject.js +50 -17
- package/package.json +1 -1
- package/voice-input/voice-input.js +17 -6
package/assets/myclaw-inject.js
CHANGED
|
@@ -955,6 +955,36 @@ btn.addEventListener("click", function () {
|
|
|
955
955
|
|
|
956
956
|
// ═══ 4. 录音控制 ═══
|
|
957
957
|
|
|
958
|
+
// 连接失败重连:单通道 + 次数上限(参照 teacher-web XfIatRecorder 的 maxRetryCount)
|
|
959
|
+
var VOICE_MAX_RETRY = 3;
|
|
960
|
+
var voiceRetryCount = 0;
|
|
961
|
+
var voiceRetryTimer = null;
|
|
962
|
+
|
|
963
|
+
function clearVoiceRetry() {
|
|
964
|
+
if (voiceRetryTimer) {
|
|
965
|
+
clearTimeout(voiceRetryTimer);
|
|
966
|
+
voiceRetryTimer = null;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
function scheduleVoiceRetry(delay) {
|
|
971
|
+
if (!recording || voiceRetryTimer) return;
|
|
972
|
+
if (voiceRetryCount >= VOICE_MAX_RETRY) {
|
|
973
|
+
console.error("[myclaw-voice] ⛔ 连续 " + VOICE_MAX_RETRY + " 次重连失败,停止录音");
|
|
974
|
+
recording = false;
|
|
975
|
+
updateButtonUI();
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
voiceRetryCount++;
|
|
979
|
+
voiceRetryTimer = setTimeout(function () {
|
|
980
|
+
voiceRetryTimer = null;
|
|
981
|
+
if (recording && voice) {
|
|
982
|
+
console.log("[myclaw-voice] \u91cd\u8fde " + voiceRetryCount + "/" + VOICE_MAX_RETRY + "...");
|
|
983
|
+
voice.start();
|
|
984
|
+
}
|
|
985
|
+
}, delay);
|
|
986
|
+
}
|
|
987
|
+
|
|
958
988
|
function initVoice() {
|
|
959
989
|
if (typeof window.VoiceInput === "undefined") {
|
|
960
990
|
console.error("[myclaw-inject] VoiceInput SDK \u672a\u52a0\u8f7d");
|
|
@@ -973,6 +1003,10 @@ btn.addEventListener("click", function () {
|
|
|
973
1003
|
onStatusChange: function (oldStatus, newStatus) {
|
|
974
1004
|
console.log("[myclaw-voice] \u72b6\u6001:", oldStatus, "->", newStatus);
|
|
975
1005
|
|
|
1006
|
+
if (newStatus === "recording") {
|
|
1007
|
+
voiceRetryCount = 0;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
976
1010
|
if (newStatus === "idle" && recording && !stopping) {
|
|
977
1011
|
// 讯飞 60 秒断开,但用户没有点停止 → 自动重连
|
|
978
1012
|
// 把当前识别的文字提交,并更新光标位置
|
|
@@ -981,32 +1015,27 @@ btn.addEventListener("click", function () {
|
|
|
981
1015
|
pendingText = "";
|
|
982
1016
|
|
|
983
1017
|
console.log("[myclaw-voice] \u81ea\u52a8\u91cd\u8fde...");
|
|
984
|
-
|
|
985
|
-
if (recording && voice) {
|
|
986
|
-
voice.start();
|
|
987
|
-
}
|
|
988
|
-
}, 300);
|
|
1018
|
+
scheduleVoiceRetry(300);
|
|
989
1019
|
}
|
|
990
1020
|
},
|
|
991
1021
|
onError: function (err) {
|
|
992
1022
|
console.error("[myclaw-voice] \u9519\u8bef:", err);
|
|
993
|
-
//
|
|
1023
|
+
// 永久性错误重试只会刷屏,直接停止
|
|
994
1024
|
var errStr = String(err).toLowerCase();
|
|
995
|
-
if (
|
|
996
|
-
|
|
1025
|
+
if (
|
|
1026
|
+
errStr.indexOf("permission") !== -1 ||
|
|
1027
|
+
errStr.indexOf("not allowed") !== -1 ||
|
|
1028
|
+
errStr.indexOf("securityerror") !== -1 ||
|
|
1029
|
+
errStr.indexOf("insecure websocket") !== -1
|
|
1030
|
+
) {
|
|
1031
|
+
console.error("[myclaw-voice] ⛔ 麦克风权限或页面安全策略阻止录音,停止录音");
|
|
1032
|
+
clearVoiceRetry();
|
|
997
1033
|
recording = false;
|
|
998
1034
|
updateButtonUI();
|
|
999
1035
|
return;
|
|
1000
1036
|
}
|
|
1001
|
-
// 其他错误(如 WebSocket
|
|
1002
|
-
|
|
1003
|
-
setTimeout(function () {
|
|
1004
|
-
if (recording && voice) {
|
|
1005
|
-
console.log("[myclaw-voice] \u9519\u8bef\u540e\u91cd\u8fde...");
|
|
1006
|
-
voice.start();
|
|
1007
|
-
}
|
|
1008
|
-
}, 1000);
|
|
1009
|
-
}
|
|
1037
|
+
// 其他错误(如 WebSocket)走带次数上限的重连
|
|
1038
|
+
scheduleVoiceRetry(1000);
|
|
1010
1039
|
},
|
|
1011
1040
|
});
|
|
1012
1041
|
|
|
@@ -1024,6 +1053,10 @@ btn.addEventListener("click", function () {
|
|
|
1024
1053
|
cursorOffset = getCursorPosition();
|
|
1025
1054
|
pendingText = "";
|
|
1026
1055
|
|
|
1056
|
+
// 手动开始是一次新的会话:清掉待执行的重连和上次的失败计数
|
|
1057
|
+
clearVoiceRetry();
|
|
1058
|
+
voiceRetryCount = 0;
|
|
1059
|
+
|
|
1027
1060
|
recording = true;
|
|
1028
1061
|
updateButtonUI();
|
|
1029
1062
|
voice.start();
|
package/package.json
CHANGED
|
@@ -14,15 +14,26 @@
|
|
|
14
14
|
(function (global) {
|
|
15
15
|
'use strict';
|
|
16
16
|
|
|
17
|
+
// ============ 语音代理地址 ============
|
|
18
|
+
// 浏览器 WebSocket 握手带不了火山的鉴权头,签名只能在服务端做,所以前端只连 /ws/asr 代理。
|
|
19
|
+
// HTTPS 页面必须用 wss,否则浏览器按混合内容直接拒绝握手。
|
|
20
|
+
var PUBLIC_PROXY_URL = 'wss://www.yiranlaoshi.com/ws/asr';
|
|
21
|
+
|
|
22
|
+
function getDefaultProxyUrl() {
|
|
23
|
+
// 宿主自己反代了同源代理时指过去,例如 wss://<host>/ws/asr
|
|
24
|
+
if (typeof window !== 'undefined' && window.MYCLAW_VOICE_PROXY_URL) {
|
|
25
|
+
return window.MYCLAW_VOICE_PROXY_URL;
|
|
26
|
+
}
|
|
27
|
+
return PUBLIC_PROXY_URL;
|
|
28
|
+
}
|
|
29
|
+
|
|
17
30
|
// ============ 默认配置(火山引擎豆包语音) ============
|
|
18
31
|
var DEFAULT_CONFIG = {
|
|
19
32
|
APPID: '3086995618',
|
|
20
33
|
AccessToken: 'R8Ydr2JYkZSDiP3luU63eWTmjZQ6CQmf',
|
|
21
34
|
SecretKey: 'XgANQB0u3RNdEqENjGX76clLxPQH2dpb',
|
|
22
35
|
resourceId: 'volc.seedasr.sauc.duration',
|
|
23
|
-
proxyUrl: (
|
|
24
|
-
? ('ws://' + window.location.hostname + ':9998/ws/asr')
|
|
25
|
-
: 'ws://localhost:9998/ws/asr',
|
|
36
|
+
proxyUrl: getDefaultProxyUrl(),
|
|
26
37
|
url: 'wss://openspeech.bytedance.com/api/v3/sauc/bigmodel_async',
|
|
27
38
|
language: 'zh_cn'
|
|
28
39
|
};
|
|
@@ -142,7 +153,7 @@
|
|
|
142
153
|
|
|
143
154
|
VoiceInput.prototype._getWebSocketUrl = function () {
|
|
144
155
|
var self = this;
|
|
145
|
-
return Promise.resolve(self.proxyUrl ||
|
|
156
|
+
return Promise.resolve(self.proxyUrl || getDefaultProxyUrl());
|
|
146
157
|
};
|
|
147
158
|
|
|
148
159
|
VoiceInput.prototype.start = function () {
|
|
@@ -201,7 +212,7 @@
|
|
|
201
212
|
var timeout = setTimeout(function () {
|
|
202
213
|
if (ws.readyState === WebSocket.CONNECTING) {
|
|
203
214
|
ws.close();
|
|
204
|
-
self.onError('
|
|
215
|
+
self.onError('无法连接语音识别服务(' + url + '),请检查后端服务是否正常运行');
|
|
205
216
|
self._cleanup();
|
|
206
217
|
}
|
|
207
218
|
}, 5000);
|
|
@@ -219,7 +230,7 @@
|
|
|
219
230
|
|
|
220
231
|
ws.onerror = function () {
|
|
221
232
|
clearTimeout(timeout);
|
|
222
|
-
self.onError('
|
|
233
|
+
self.onError('无法连接语音识别服务(' + url + '),请检查后端服务是否正常运行');
|
|
223
234
|
self._cleanup();
|
|
224
235
|
};
|
|
225
236
|
|