@myun/gimi-chat 0.0.3 → 0.0.4

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 (206) hide show
  1. package/dist/apis/fetch.d.ts +64 -0
  2. package/dist/apis/fetch.js +512 -0
  3. package/dist/apis/useApi.d.ts +57 -0
  4. package/dist/apis/useApi.js +378 -0
  5. package/dist/assets/image/Subtract.png +0 -0
  6. package/dist/assets/image/ai-logo.png +0 -0
  7. package/dist/assets/image/copyIcon.png +0 -0
  8. package/dist/assets/image/deepthinkActive.png +0 -0
  9. package/dist/assets/image/empty-img-dark.png +0 -0
  10. package/dist/assets/image/empty-img.png +0 -0
  11. package/dist/assets/image/like_active.png +0 -0
  12. package/dist/assets/image/love.png +0 -0
  13. package/dist/assets/image/retry.png +0 -0
  14. package/dist/assets/image/soundOut.png +0 -0
  15. package/dist/assets/image/unlike_active.png +0 -0
  16. package/dist/assets/image/unlove.png +0 -0
  17. package/dist/assets/lottie/ai-bg.json +1704 -0
  18. package/dist/assets/lottie/ai-loading.json +192 -0
  19. package/dist/assets/lottie/ai-output-loading.json +272 -0
  20. package/dist/assets/lottie/ai-output-normal.json +17694 -0
  21. package/dist/assets/lottie/ai-output-strengthen.json +17321 -0
  22. package/dist/assets/lottie/ai-voice-play.json +1 -0
  23. package/dist/assets/lottie/living.json +705 -0
  24. package/dist/assets/lottie/myun-living.json +726 -0
  25. package/dist/assets/lottie/myun-loading.json +530 -0
  26. package/dist/components/ai-chat-dialogue/index.d.ts +16 -0
  27. package/dist/components/ai-chat-dialogue/index.js +1056 -0
  28. package/dist/components/ai-chat-dialogue/index.module.scss +272 -0
  29. package/dist/components/ai-chat-dialogue/message-parser.d.ts +36 -0
  30. package/dist/components/ai-chat-dialogue/message-parser.js +52 -0
  31. package/dist/components/ai-chat-dialogue/tlv.d.ts +7 -0
  32. package/dist/components/ai-chat-dialogue/tlv.js +32 -0
  33. package/dist/components/ai-chat-dialogue/volc-voice-call-client.d.ts +47 -0
  34. package/dist/components/ai-chat-dialogue/volc-voice-call-client.js +246 -0
  35. package/dist/components/ai-correction/index.d.ts +5 -0
  36. package/dist/components/ai-correction/index.js +45 -0
  37. package/dist/components/ai-loading/index.d.ts +3 -0
  38. package/dist/components/ai-loading/index.js +14 -0
  39. package/dist/components/ai-loading/index.module.scss +11 -0
  40. package/dist/components/answer-item/index.d.ts +27 -0
  41. package/dist/components/answer-item/index.js +194 -0
  42. package/dist/components/answer-item/index.module.scss +295 -0
  43. package/dist/components/ask-card/index.d.ts +10 -0
  44. package/dist/components/ask-card/index.js +76 -0
  45. package/dist/components/ask-card/index.module.scss +125 -0
  46. package/dist/components/chat-input/index.d.ts +31 -0
  47. package/dist/components/chat-input/index.js +433 -0
  48. package/dist/components/chat-input/index.module.scss +44 -0
  49. package/dist/components/chat-voice/VoiceCommunication.d.ts +7 -0
  50. package/dist/components/chat-voice/VoiceCommunication.js +69 -0
  51. package/dist/components/chat-voice/VoiceRecord.d.ts +7 -0
  52. package/dist/components/chat-voice/VoiceRecord.js +34 -0
  53. package/dist/components/conversation-delete/index.d.ts +5 -0
  54. package/dist/components/conversation-delete/index.js +24 -0
  55. package/dist/components/dots-loading/index.d.ts +2 -0
  56. package/dist/components/dots-loading/index.js +11 -0
  57. package/dist/components/dots-loading/index.module.scss +45 -0
  58. package/dist/components/empty/index.d.ts +9 -0
  59. package/dist/components/empty/index.js +32 -0
  60. package/dist/components/empty/index.module.scss +27 -0
  61. package/dist/components/excel-components/ExcelCard.d.ts +11 -0
  62. package/dist/components/excel-components/ExcelCard.js +35 -0
  63. package/dist/components/excel-components/ExcelExcuting.d.ts +10 -0
  64. package/dist/components/excel-components/ExcelExcuting.js +263 -0
  65. package/dist/components/excel-components/ExcelFailCard.d.ts +9 -0
  66. package/dist/components/excel-components/ExcelFailCard.js +20 -0
  67. package/dist/components/excel-components/ExcelSuccessCard.d.ts +5 -0
  68. package/dist/components/excel-components/ExcelSuccessCard.js +50 -0
  69. package/dist/components/excel-components/index.module.scss +210 -0
  70. package/dist/components/excel-components/styles.module.scss +186 -0
  71. package/dist/components/file-card/fileCardSidebar.d.ts +8 -0
  72. package/dist/components/file-card/fileCardSidebar.js +56 -0
  73. package/dist/components/file-card/index.d.ts +15 -0
  74. package/dist/components/file-card/index.js +97 -0
  75. package/dist/components/file-card/index.module.scss +139 -0
  76. package/dist/components/file-preview/index.d.ts +3 -0
  77. package/dist/components/file-preview/index.js +229 -0
  78. package/dist/components/file-preview/index.module.scss +120 -0
  79. package/dist/components/file-upload/index.d.ts +16 -0
  80. package/dist/components/file-upload/index.js +238 -0
  81. package/dist/components/file-upload/uploadV1.d.ts +16 -0
  82. package/dist/components/file-upload/uploadV1.js +267 -0
  83. package/dist/components/gimi-sidebar/index.d.ts +17 -0
  84. package/dist/components/gimi-sidebar/index.js +41 -0
  85. package/dist/components/gimi-sidebar/index.module.scss +136 -0
  86. package/dist/components/header/index.d.ts +5 -0
  87. package/dist/components/header/index.js +20 -0
  88. package/dist/components/header/index.module.scss +26 -0
  89. package/dist/components/iconfont-com/index.d.ts +11 -0
  90. package/dist/components/iconfont-com/index.js +29 -0
  91. package/dist/components/index.d.ts +1 -0
  92. package/dist/components/index.js +1 -0
  93. package/dist/components/knowledge-trace/KnowledgeIconComponent.d.ts +7 -0
  94. package/dist/components/knowledge-trace/KnowledgeIconComponent.js +101 -0
  95. package/dist/components/knowledge-trace/classList.d.ts +6 -0
  96. package/dist/components/knowledge-trace/classList.js +72 -0
  97. package/dist/components/knowledge-trace/documentList.d.ts +6 -0
  98. package/dist/components/knowledge-trace/documentList.js +75 -0
  99. package/dist/components/knowledge-trace/index.d.ts +18 -0
  100. package/dist/components/knowledge-trace/index.js +155 -0
  101. package/dist/components/knowledge-trace/index.module.scss +262 -0
  102. package/dist/components/knowledge-trace/interfaces.d.ts +166 -0
  103. package/dist/components/knowledge-trace/interfaces.js +1 -0
  104. package/dist/components/knowledge-trace/videoList.d.ts +6 -0
  105. package/dist/components/knowledge-trace/videoList.js +91 -0
  106. package/dist/components/lottie-img/index.d.ts +8 -0
  107. package/dist/components/lottie-img/index.js +99 -0
  108. package/dist/components/message-actions/CopyButton.d.ts +7 -0
  109. package/dist/components/message-actions/CopyButton.js +33 -0
  110. package/dist/components/message-actions/LikeButton.d.ts +7 -0
  111. package/dist/components/message-actions/LikeButton.js +34 -0
  112. package/dist/components/message-actions/RegenerateButton.d.ts +5 -0
  113. package/dist/components/message-actions/RegenerateButton.js +27 -0
  114. package/dist/components/message-actions/UnLikeButton.d.ts +7 -0
  115. package/dist/components/message-actions/UnLikeButton.js +34 -0
  116. package/dist/components/message-actions/VoicePlay.d.ts +10 -0
  117. package/dist/components/message-actions/VoicePlay.js +74 -0
  118. package/dist/components/message-list/index.d.ts +19 -0
  119. package/dist/components/message-list/index.js +167 -0
  120. package/dist/components/message-list/index.module.scss +304 -0
  121. package/dist/components/no-microphone-root/index.d.ts +6 -0
  122. package/dist/components/no-microphone-root/index.js +36 -0
  123. package/dist/components/no-microphone-root/index.module.scss +42 -0
  124. package/dist/components/preset-agent-content/index.d.ts +8 -0
  125. package/dist/components/preset-agent-content/index.js +24 -0
  126. package/dist/components/preset-agent-content/index.module.scss +48 -0
  127. package/dist/components/quoted-content/index.d.ts +0 -0
  128. package/dist/components/quoted-content/index.js +171 -0
  129. package/dist/components/quoted-content/index.module.scss +76 -0
  130. package/dist/components/reasoning-content/index.d.ts +6 -0
  131. package/dist/components/reasoning-content/index.js +116 -0
  132. package/dist/components/reasoning-content/index.module.scss +164 -0
  133. package/dist/components/reference-content/index.d.ts +28 -0
  134. package/dist/components/reference-content/index.js +125 -0
  135. package/dist/components/reference-content/index.module.scss +73 -0
  136. package/dist/components/templates/CommonChat.d.ts +5 -0
  137. package/dist/components/templates/CommonChat.js +218 -0
  138. package/dist/components/templates/GimiChatComponent.d.ts +4 -0
  139. package/dist/components/templates/GimiChatComponent.js +23 -0
  140. package/dist/components/templates/demo/demo.d.ts +2 -0
  141. package/dist/components/templates/demo/demo.js +110 -0
  142. package/dist/components/templates/index.module.scss +119 -0
  143. package/dist/components/upload-list/index.d.ts +15 -0
  144. package/dist/components/upload-list/index.js +90 -0
  145. package/dist/components/upload-list/index.module.scss +119 -0
  146. package/dist/components/voice-bars/index.d.ts +4 -0
  147. package/dist/components/voice-bars/index.js +174 -0
  148. package/dist/components/voice-check-dialog/index.d.ts +8 -0
  149. package/dist/components/voice-check-dialog/index.js +163 -0
  150. package/dist/components/voice-check-dialog/index.module.scss +9 -0
  151. package/dist/components/voice-recording/index.d.ts +9 -0
  152. package/dist/components/voice-recording/index.js +54 -0
  153. package/dist/components/voice-recording/index.module.scss +41 -0
  154. package/dist/components/work-flow-content/demo.d.ts +3 -0
  155. package/dist/components/work-flow-content/demo.js +11 -0
  156. package/dist/components/work-flow-content/index.d.ts +6 -0
  157. package/dist/components/work-flow-content/index.js +55 -0
  158. package/dist/components/work-flow-content/index.module.scss +23 -0
  159. package/dist/constants.d.ts +24 -0
  160. package/dist/constants.js +24 -0
  161. package/dist/hooks/useChatActions.d.ts +12 -0
  162. package/dist/hooks/useChatActions.js +207 -0
  163. package/dist/hooks/useChatHistory.d.ts +8 -0
  164. package/dist/hooks/useChatHistory.js +225 -0
  165. package/dist/hooks/useChatMessage.d.ts +49 -0
  166. package/dist/hooks/useChatMessage.js +87 -0
  167. package/dist/hooks/useChatRecommend.d.ts +8 -0
  168. package/dist/hooks/useChatRecommend.js +126 -0
  169. package/dist/hooks/useChatStream.d.ts +20 -0
  170. package/dist/hooks/useChatStream.js +244 -0
  171. package/dist/hooks/useChatUI.d.ts +32 -0
  172. package/dist/hooks/useChatUI.js +134 -0
  173. package/dist/hooks/useChatVoice.d.ts +11 -0
  174. package/dist/hooks/useChatVoice.js +379 -0
  175. package/dist/hooks/useCommonChatAPI.d.ts +27 -0
  176. package/dist/hooks/useCommonChatAPI.js +1276 -0
  177. package/dist/hooks/useFile.d.ts +13 -0
  178. package/dist/hooks/useFile.js +288 -0
  179. package/dist/hooks/useKnowledgeService.d.ts +17 -0
  180. package/dist/hooks/useKnowledgeService.js +210 -0
  181. package/dist/hooks/useLastEventId.d.ts +8 -0
  182. package/dist/hooks/useLastEventId.js +66 -0
  183. package/dist/hooks/useLongPoll.d.ts +3 -0
  184. package/dist/hooks/useLongPoll.js +111 -0
  185. package/dist/hooks/useScroll.d.ts +4 -0
  186. package/dist/hooks/useScroll.js +172 -0
  187. package/dist/index.d.ts +1 -0
  188. package/dist/index.js +1 -0
  189. package/dist/interfaces/chatMessage.d.ts +139 -0
  190. package/dist/interfaces/fileInterface.d.ts +10 -0
  191. package/dist/interfaces/fileInterface.js +11 -0
  192. package/dist/interfaces/knowledgeTrace.d.ts +177 -0
  193. package/dist/store/hooks.d.ts +4 -0
  194. package/dist/store/hooks.js +3 -0
  195. package/dist/store/index.d.ts +7 -0
  196. package/dist/store/index.js +7 -0
  197. package/dist/store/slices/gimiMenuSlice.d.ts +95 -0
  198. package/dist/store/slices/gimiMenuSlice.js +251 -0
  199. package/dist/utils/chatInputUtil.d.ts +19 -0
  200. package/dist/utils/chatInputUtil.js +45 -0
  201. package/dist/utils/tools.d.ts +50 -0
  202. package/dist/utils/tools.js +474 -0
  203. package/dist/utils/voice-stream.d.ts +119 -0
  204. package/dist/utils/voice-stream.js +1162 -0
  205. package/package.json +2 -3
  206. package/LICENSE +0 -21
@@ -0,0 +1,1162 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
5
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
7
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
8
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
9
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
10
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
11
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
12
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
13
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
15
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
16
+ import { createFetch } from "../apis/fetch";
17
+ import { store } from "../store";
18
+
19
+ // 定义业务场景枚举
20
+ var HandlerMode = /*#__PURE__*/function (HandlerMode) {
21
+ HandlerMode["ASR_ONLY"] = "asr_only";
22
+ HandlerMode["TTS_ONLY"] = "tts_only";
23
+ HandlerMode["SHORT_ASR_ONLY"] = "short_asr_only";
24
+ return HandlerMode;
25
+ }(HandlerMode || {});
26
+ /** 一句话播放相关配置 */
27
+ /** 腾讯云录音器状态 */
28
+ /** 录音器状态 */
29
+ /** WebSocket 消息类型 */
30
+ /** 语音消息回调类型 */
31
+ // 新增:流式音频播放器状态
32
+ /**
33
+ * WebSocket 启动指令参数类型定义
34
+ * 对应 start 指令的消息体结构
35
+ */
36
+ // 腾讯云录音相关
37
+ var tencentRecorder = {
38
+ mediaRecorder: null,
39
+ audioChunks: [],
40
+ audioContext: null,
41
+ stream: null,
42
+ source: null,
43
+ processor: null
44
+ // isRecording: false
45
+ };
46
+ var VoiceRecorder = /*#__PURE__*/function () {
47
+ function VoiceRecorder() {
48
+ var _this = this;
49
+ _classCallCheck(this, VoiceRecorder);
50
+ _defineProperty(this, "mediaRecorder", null);
51
+ // 新增:存储 TTS 流式播放器实例
52
+ _defineProperty(this, "audioPlayer", null);
53
+ _defineProperty(this, "ws", null);
54
+ // 定义消息回调函数类型
55
+ _defineProperty(this, "onMessageCallback", null);
56
+ // 新增:当前 TTS 会话 ID
57
+ _defineProperty(this, "ttsSessionId", 0);
58
+ //监听网络断开重连
59
+ _defineProperty(this, "networkBound", false);
60
+ // 网络状态传给外部回调函数
61
+ _defineProperty(this, "handleOffline", function () {
62
+ var _this$onMessageCallba;
63
+ (_this$onMessageCallba = _this.onMessageCallback) === null || _this$onMessageCallba === void 0 || _this$onMessageCallba.call(_this, {
64
+ type: 'voice_error',
65
+ stage: 'network',
66
+ message: '网络已断开(offline)'
67
+ }, null);
68
+ });
69
+ _defineProperty(this, "handleOnline", function () {
70
+ var _this$onMessageCallba2;
71
+ (_this$onMessageCallba2 = _this.onMessageCallback) === null || _this$onMessageCallba2 === void 0 || _this$onMessageCallba2.call(_this, {
72
+ type: 'voice_info',
73
+ stage: 'network',
74
+ message: '网络已恢复(online)'
75
+ }, null);
76
+ });
77
+ // 腾讯云一句话录音
78
+ _defineProperty(this, "recordAndRecognize", function () {
79
+ // eslint-disable-next-line no-async-promise-executor
80
+ return new Promise( /*#__PURE__*/function () {
81
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(resolve, reject) {
82
+ var stream, audioContext, source, processor, error, name, isPermissionDenied, e;
83
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
84
+ while (1) switch (_context.prev = _context.next) {
85
+ case 0:
86
+ _context.prev = 0;
87
+ _context.next = 3;
88
+ return navigator.mediaDevices.getUserMedia({
89
+ audio: {
90
+ sampleRate: 16000,
91
+ channelCount: 1,
92
+ echoCancellation: true,
93
+ noiseSuppression: true
94
+ }
95
+ });
96
+ case 3:
97
+ stream = _context.sent;
98
+ // 使用 AudioContext 进行录音,确保格式正确
99
+ audioContext = new AudioContext({
100
+ sampleRate: 16000
101
+ });
102
+ source = audioContext.createMediaStreamSource(stream);
103
+ processor = audioContext.createScriptProcessor(4096, 1, 1);
104
+ tencentRecorder.audioChunks = [];
105
+ tencentRecorder.audioContext = audioContext;
106
+ tencentRecorder.stream = stream;
107
+ tencentRecorder.source = source;
108
+ tencentRecorder.processor = processor;
109
+ processor.onaudioprocess = function (e) {
110
+ var inputData = e.inputBuffer.getChannelData(0);
111
+ var pcmData = _this.convertFloat32ToInt16(inputData);
112
+ tencentRecorder.audioChunks.push(pcmData);
113
+ };
114
+ source.connect(processor);
115
+ processor.connect(audioContext.destination);
116
+ resolve();
117
+ _context.next = 29;
118
+ break;
119
+ case 18:
120
+ _context.prev = 18;
121
+ _context.t0 = _context["catch"](0);
122
+ error = _context.t0;
123
+ name = error === null || error === void 0 ? void 0 : error.name;
124
+ isPermissionDenied = name === 'NotAllowedError' || name === 'PermissionDeniedError';
125
+ if (!isPermissionDenied) {
126
+ _context.next = 28;
127
+ break;
128
+ }
129
+ e = new Error((error === null || error === void 0 ? void 0 : error.message) || '麦克风权限被拒绝');
130
+ e.code = 'MIC_PERMISSION_DENIED';
131
+ reject(e);
132
+ return _context.abrupt("return");
133
+ case 28:
134
+ reject(new Error(error.message));
135
+ case 29:
136
+ case "end":
137
+ return _context.stop();
138
+ }
139
+ }, _callee, null, [[0, 18]]);
140
+ }));
141
+ return function (_x, _x2) {
142
+ return _ref.apply(this, arguments);
143
+ };
144
+ }());
145
+ });
146
+ }
147
+ _createClass(VoiceRecorder, [{
148
+ key: "setOnMessageCallback",
149
+ value: function setOnMessageCallback(cb) {
150
+ this.onMessageCallback = cb;
151
+ }
152
+ }, {
153
+ key: "bindNetworkEvents",
154
+ value:
155
+ // 绑定监听事件
156
+ function bindNetworkEvents() {
157
+ if (this.networkBound) return;
158
+ console.log(window.addEventListener('offline', this.handleOffline));
159
+ window.addEventListener('offline', this.handleOffline);
160
+ window.addEventListener('online', this.handleOnline);
161
+ this.networkBound = true;
162
+ }
163
+ // 解绑监听事件
164
+ }, {
165
+ key: "unbindNetworkEvents",
166
+ value: function unbindNetworkEvents() {
167
+ if (!this.networkBound) return;
168
+ window.removeEventListener('offline', this.handleOffline);
169
+ window.removeEventListener('online', this.handleOnline);
170
+ this.networkBound = false;
171
+ }
172
+
173
+ // 新增:初始化流式音频播放器(用于 TTS 播放)
174
+ }, {
175
+ key: "initStreamingAudioPlayer",
176
+ value: function initStreamingAudioPlayer() {
177
+ // 关闭旧的 AudioContext
178
+ if (this.audioPlayer) {
179
+ this.audioPlayer.audioContext.close().catch(function () {});
180
+ }
181
+ var inputSampleRate = 24000;
182
+ var audioContext = new window.AudioContext({
183
+ sampleRate: inputSampleRate
184
+ });
185
+ var outputSampleRate = audioContext.sampleRate;
186
+ var initialBufferSec = 0.08;
187
+ var minAheadSec = 0.06;
188
+ var fadeSec = 0.004;
189
+ var resampleTail = new Float32Array(0);
190
+ var resamplePos = 0;
191
+ var applyEdgeFade = function applyEdgeFade(data) {
192
+ if (!data.length) return;
193
+ var maxFadeSamples = Math.floor(outputSampleRate * fadeSec);
194
+ var fadeSamples = Math.min(maxFadeSamples, Math.floor(data.length / 2));
195
+ if (fadeSamples <= 0) return;
196
+ for (var i = 0; i < fadeSamples; i++) {
197
+ var k = i / fadeSamples;
198
+ data[i] = data[i] * k;
199
+ var j = data.length - 1 - i;
200
+ data[j] = data[j] * k;
201
+ }
202
+ };
203
+ var concatFloat32 = function concatFloat32(a, b) {
204
+ if (!a.length) return b;
205
+ if (!b.length) return a;
206
+ var merged = new Float32Array(a.length + b.length);
207
+ merged.set(a, 0);
208
+ merged.set(b, a.length);
209
+ return merged;
210
+ };
211
+ var resampleIfNeeded = function resampleIfNeeded(input) {
212
+ if (inputSampleRate === outputSampleRate) return input;
213
+ var merged = concatFloat32(resampleTail, input);
214
+ if (merged.length < 2) {
215
+ resampleTail = merged;
216
+ return new Float32Array(0);
217
+ }
218
+ var step = inputSampleRate / outputSampleRate;
219
+ var available = merged.length - 1 - resamplePos;
220
+ var outLen = Math.floor(available / step);
221
+ if (outLen <= 0) {
222
+ var _keepIndex = Math.max(0, Math.floor(resamplePos) - 1);
223
+ resampleTail = merged.subarray(_keepIndex);
224
+ resamplePos = resamplePos - _keepIndex;
225
+ return new Float32Array(0);
226
+ }
227
+ var out = new Float32Array(outLen);
228
+ for (var i = 0; i < outLen; i++) {
229
+ var idx = Math.floor(resamplePos);
230
+ var frac = resamplePos - idx;
231
+ var a = merged[idx];
232
+ var b = merged[idx + 1];
233
+ out[i] = a + (b - a) * frac;
234
+ resamplePos += step;
235
+ }
236
+ var keepIndex = Math.max(0, Math.floor(resamplePos) - 1);
237
+ resampleTail = merged.subarray(keepIndex);
238
+ resamplePos = resamplePos - keepIndex;
239
+ return out;
240
+ };
241
+ var player = {
242
+ audioContext: audioContext,
243
+ inputSampleRate: inputSampleRate,
244
+ nextStartTime: 0,
245
+ scheduledCount: 0,
246
+ sources: new Set(),
247
+ addAudioData: function addAudioData(pcmData) {
248
+ if (audioContext.state === 'suspended') {
249
+ audioContext.resume().catch(function () {});
250
+ }
251
+ // PCM Int16 转 Float32
252
+ var int16Array = new Int16Array(pcmData);
253
+ var float32Array = new Float32Array(int16Array.length);
254
+ for (var i = 0; i < int16Array.length; i++) {
255
+ float32Array[i] = int16Array[i] / 32768.0;
256
+ }
257
+ var outputFloat32 = resampleIfNeeded(float32Array);
258
+ if (outputFloat32.length === 0) return;
259
+ applyEdgeFade(outputFloat32);
260
+
261
+ // 创建 AudioBuffer
262
+ var audioBuffer = audioContext.createBuffer(1, outputFloat32.length, outputSampleRate);
263
+ audioBuffer.getChannelData(0).set(outputFloat32);
264
+
265
+ // 创建音源
266
+ var source = audioContext.createBufferSource();
267
+ source.buffer = audioBuffer;
268
+ source.connect(audioContext.destination);
269
+ player.sources.add(source);
270
+ source.onended = function () {
271
+ player.sources.delete(source);
272
+ };
273
+
274
+ // 计算开始时间(确保无缝衔接)
275
+ var currentTime = audioContext.currentTime;
276
+ if (player.scheduledCount === 0 && player.nextStartTime === 0) {
277
+ player.nextStartTime = currentTime + initialBufferSec;
278
+ }
279
+ if (player.nextStartTime < currentTime + minAheadSec) {
280
+ player.nextStartTime = currentTime + minAheadSec;
281
+ }
282
+ var startTime = player.nextStartTime;
283
+
284
+ // 调度播放
285
+ source.start(startTime);
286
+
287
+ // 更新下次开始时间
288
+ player.nextStartTime = startTime + audioBuffer.duration;
289
+ player.scheduledCount++;
290
+ },
291
+ reset: function reset() {
292
+ var _iterator = _createForOfIteratorHelper(player.sources),
293
+ _step;
294
+ try {
295
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
296
+ var source = _step.value;
297
+ try {
298
+ source.stop(0);
299
+ } catch (_unused) {}
300
+ }
301
+ } catch (err) {
302
+ _iterator.e(err);
303
+ } finally {
304
+ _iterator.f();
305
+ }
306
+ player.sources.clear();
307
+ player.nextStartTime = 0;
308
+ player.scheduledCount = 0;
309
+ resampleTail = new Float32Array(0);
310
+ resamplePos = 0;
311
+ console.log('🔄 播放器已重置');
312
+ }
313
+ };
314
+ return player;
315
+ }
316
+
317
+ // 音频格式转换工具函数
318
+ }, {
319
+ key: "convertFloat32ToInt16",
320
+ value: function convertFloat32ToInt16(float32Array) {
321
+ var int16Array = new Int16Array(float32Array.length);
322
+ for (var i = 0; i < float32Array.length; i++) {
323
+ var s = Math.max(-1, Math.min(1, float32Array[i]));
324
+ int16Array[i] = s < 0 ? s * 0x8000 : s * 0x7fff;
325
+ }
326
+ return int16Array;
327
+ }
328
+ // 1. 连接WebSocket
329
+ }, {
330
+ key: "connectHandler",
331
+ value: function () {
332
+ var _connectHandler = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(modeType) {
333
+ var _this2 = this;
334
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
335
+ while (1) switch (_context2.prev = _context2.next) {
336
+ case 0:
337
+ return _context2.abrupt("return", new Promise(function (resolve, reject) {
338
+ // eslint-disable-next-line no-promise-executor-return
339
+ if (_this2.ws && _this2.ws.readyState === WebSocket.OPEN) return resolve();
340
+ var token = store.getState().gimiMenu.token;
341
+ var baseUrl = store.getState().gimiMenu.baseUrl;
342
+ try {
343
+ var ws = new WebSocket("wss://".concat(baseUrl, "/mbot-wss/ws/voice?token=").concat(token));
344
+ _this2.ws = ws;
345
+ ws.onopen = function () {
346
+ _this2.bindNetworkEvents();
347
+ console.log('✅ WebSocket 连接成功', 'green');
348
+ if (modeType === HandlerMode.TTS_ONLY) {
349
+ _this2.audioPlayer = _this2.initStreamingAudioPlayer();
350
+ console.log('🎵 初始化流式音频播放器 (24kHz PCM)');
351
+ }
352
+ resolve();
353
+ };
354
+ ws.onclose = function () {
355
+ // 语音合成的情况下在外部手动清理
356
+ if (modeType === HandlerMode.TTS_ONLY) return;
357
+ if (_this2.ws === ws) {
358
+ _this2.ws = null;
359
+ // 清理 TTS 播放器
360
+ if (_this2.audioPlayer) {
361
+ _this2.audioPlayer.audioContext.close();
362
+ _this2.audioPlayer = null;
363
+ }
364
+ }
365
+ };
366
+ ws.onerror = function () {
367
+ console.log('❌ WebSocket 连接错误', 'red');
368
+ reject(new Error('连接失败'));
369
+ };
370
+ ws.onmessage = function (e) {
371
+ if (typeof e.data === 'string') {
372
+ var _data = JSON.parse(e.data);
373
+
374
+ // ① 如果是 TTS 完成消息,并且有播放器 捕获语音播报完成
375
+ if (_data.type === 'tts_complete' && modeType === HandlerMode.TTS_ONLY && _this2.audioPlayer) {
376
+ var currentSessionId = _this2.ttsSessionId; // 读取当前会话 ID
377
+ // 计算距离真正播放完还有多久
378
+ var _this2$audioPlayer = _this2.audioPlayer,
379
+ audioContext = _this2$audioPlayer.audioContext,
380
+ nextStartTime = _this2$audioPlayer.nextStartTime;
381
+ var now = audioContext.currentTime;
382
+ var remainSec = Math.max(0, nextStartTime - now);
383
+ var remainMs = remainSec * 1000;
384
+ var MAX_RETRY = 10; // 最多补等 10 次
385
+ var RETRY_INTERVAL = 80; // 每次补等 80ms
386
+ var tryFinish = function tryFinish(retryLeft) {
387
+ var _this2$audioPlayer2, _this2$onMessageCallb, _this2$resetTTSPlayer;
388
+ if (currentSessionId !== _this2.ttsSessionId) return;
389
+ // 还有音频在播 => 继续等
390
+ if ((_this2$audioPlayer2 = _this2.audioPlayer) !== null && _this2$audioPlayer2 !== void 0 && (_this2$audioPlayer2 = _this2$audioPlayer2.sources) !== null && _this2$audioPlayer2 !== void 0 && _this2$audioPlayer2.size && _this2.audioPlayer.sources.size > 0 && retryLeft > 0) {
391
+ setTimeout(function () {
392
+ return tryFinish(retryLeft - 1);
393
+ }, RETRY_INTERVAL);
394
+ return;
395
+ }
396
+ (_this2$onMessageCallb = _this2.onMessageCallback) === null || _this2$onMessageCallb === void 0 || _this2$onMessageCallb.call(_this2, {
397
+ type: 'tts_play_end'
398
+ }, HandlerMode.TTS_ONLY);
399
+ (_this2$resetTTSPlayer = _this2.resetTTSPlayer) === null || _this2$resetTTSPlayer === void 0 || _this2$resetTTSPlayer.call(_this2);
400
+ _this2.disconnectHandler();
401
+ };
402
+ setTimeout(function () {
403
+ return tryFinish(MAX_RETRY);
404
+ }, remainMs + 150);
405
+ // // 在真正播放结束时,再给上层发一个自定义事件
406
+ // setTimeout(() => {
407
+ // if (currentSessionId !== this.ttsSessionId) return;
408
+ // if (this.onMessageCallback) {
409
+ // this.onMessageCallback({ type: 'tts_play_end' }, HandlerMode.TTS_ONLY);
410
+ // // 处理 TTS 播放完成后重置情况播放器
411
+ // this.resetTTSPlayer?.();
412
+ // this.disconnectHandler();
413
+ // }
414
+ // }, remainMs);
415
+ }
416
+ if (_this2.onMessageCallback) {
417
+ _this2.onMessageCallback(_data, modeType);
418
+ }
419
+ } else {
420
+ // 二进制音频数据(TTS 流式返回)
421
+ if (modeType === HandlerMode.TTS_ONLY && _this2.audioPlayer) {
422
+ // 读取二进制数据并播放
423
+ e.data.arrayBuffer().then(function (arrayBuffer) {
424
+ var _this2$audioPlayer3;
425
+ (_this2$audioPlayer3 = _this2.audioPlayer) === null || _this2$audioPlayer3 === void 0 || _this2$audioPlayer3.addAudioData(arrayBuffer);
426
+ });
427
+ }
428
+ }
429
+ };
430
+ } catch (e) {
431
+ reject(e);
432
+ }
433
+ }));
434
+ case 1:
435
+ case "end":
436
+ return _context2.stop();
437
+ }
438
+ }, _callee2);
439
+ }));
440
+ function connectHandler(_x3) {
441
+ return _connectHandler.apply(this, arguments);
442
+ }
443
+ return connectHandler;
444
+ }() // 发送start指令
445
+ }, {
446
+ key: "startHandler",
447
+ value: function startHandler(modeType, msgPrams) {
448
+ var _this3 = this;
449
+ return new Promise(function (resolve, reject) {
450
+ var ws = _this3.ws;
451
+ if (!ws) {
452
+ var errMsg = '请先建立WebSocket连接';
453
+ // eslint-disable-next-line no-promise-executor-return
454
+ return reject(new Error(errMsg));
455
+ }
456
+ var msg = _objectSpread({
457
+ type: 'start',
458
+ mode: modeType,
459
+ userId: 'test_user_5',
460
+ asr: {
461
+ format: 'pcm',
462
+ sampleRate: 16000,
463
+ bits: 16,
464
+ channel: 1,
465
+ enablePunctuation: true
466
+ },
467
+ tts: {
468
+ voiceType: 'zh_female_shuangkuaisisi_emo_v2_mars_bigtts'
469
+ }
470
+ }, msgPrams);
471
+ try {
472
+ ws.send(JSON.stringify(msg));
473
+ resolve();
474
+ } catch (e) {
475
+ var _errMsg = "\u53D1\u9001start\u5931\u8D25: ".concat(e.message);
476
+ reject(new Error(_errMsg));
477
+ }
478
+ });
479
+ }
480
+
481
+ // 打开麦克风权限
482
+ }, {
483
+ key: "toggleRecord",
484
+ value: function toggleRecord() {
485
+ var _this4 = this;
486
+ // eslint-disable-next-line no-async-promise-executor
487
+ return new Promise( /*#__PURE__*/function () {
488
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(resolve, reject) {
489
+ var stream, audioContext, source, processor, errMsg;
490
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
491
+ while (1) switch (_context3.prev = _context3.next) {
492
+ case 0:
493
+ _context3.prev = 0;
494
+ _context3.next = 3;
495
+ return navigator.mediaDevices.getUserMedia({
496
+ audio: {
497
+ sampleRate: 16000,
498
+ channelCount: 1,
499
+ echoCancellation: true,
500
+ noiseSuppression: true
501
+ }
502
+ });
503
+ case 3:
504
+ stream = _context3.sent;
505
+ audioContext = new AudioContext({
506
+ sampleRate: 16000
507
+ });
508
+ source = audioContext.createMediaStreamSource(stream);
509
+ processor = audioContext.createScriptProcessor(4096, 1, 1);
510
+ processor.onaudioprocess = function (e) {
511
+ var ws = _this4.ws;
512
+ if ((ws === null || ws === void 0 ? void 0 : ws.readyState) === WebSocket.OPEN) {
513
+ var inputData = e.inputBuffer.getChannelData(0);
514
+ var _pcmData = _this4.convertFloat32ToInt16(inputData);
515
+ ws.send(_pcmData.buffer);
516
+ }
517
+ };
518
+ source.connect(processor);
519
+ processor.connect(audioContext.destination);
520
+ _this4.mediaRecorder = {
521
+ stream: stream,
522
+ audioContext: audioContext,
523
+ processor: processor,
524
+ source: source
525
+ };
526
+ resolve();
527
+ _context3.next = 18;
528
+ break;
529
+ case 14:
530
+ _context3.prev = 14;
531
+ _context3.t0 = _context3["catch"](0);
532
+ errMsg = "\u9EA6\u514B\u98CE\u6743\u9650\u88AB\u62D2: ".concat(_context3.t0.message);
533
+ reject(new Error(errMsg));
534
+ case 18:
535
+ case "end":
536
+ return _context3.stop();
537
+ }
538
+ }, _callee3, null, [[0, 14]]);
539
+ }));
540
+ return function (_x4, _x5) {
541
+ return _ref2.apply(this, arguments);
542
+ };
543
+ }());
544
+ }
545
+
546
+ //停止录音
547
+ }, {
548
+ key: "stopRecordingEvent",
549
+ value: function () {
550
+ var _stopRecordingEvent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
551
+ var _this5 = this;
552
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
553
+ while (1) switch (_context5.prev = _context5.next) {
554
+ case 0:
555
+ return _context5.abrupt("return", new Promise( /*#__PURE__*/function () {
556
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(resolve, reject) {
557
+ var _this5$mediaRecorder, stream, audioContext, processor, source, errMsg, _errMsg2;
558
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
559
+ while (1) switch (_context4.prev = _context4.next) {
560
+ case 0:
561
+ _context4.prev = 0;
562
+ if (!_this5.mediaRecorder) {
563
+ _context4.next = 12;
564
+ break;
565
+ }
566
+ _this5$mediaRecorder = _this5.mediaRecorder, stream = _this5$mediaRecorder.stream, audioContext = _this5$mediaRecorder.audioContext, processor = _this5$mediaRecorder.processor, source = _this5$mediaRecorder.source;
567
+ source.disconnect();
568
+ processor.disconnect();
569
+ _context4.next = 7;
570
+ return audioContext.close();
571
+ case 7:
572
+ stream.getTracks().forEach(function (track) {
573
+ return track.stop();
574
+ });
575
+ _this5.mediaRecorder = null;
576
+ resolve();
577
+ _context4.next = 14;
578
+ break;
579
+ case 12:
580
+ errMsg = '不存在需要停止的录音';
581
+ reject(new Error(errMsg));
582
+ case 14:
583
+ _context4.next = 20;
584
+ break;
585
+ case 16:
586
+ _context4.prev = 16;
587
+ _context4.t0 = _context4["catch"](0);
588
+ _errMsg2 = "\u505C\u6B62\u5F55\u97F3\u5931\u8D25: ".concat(_context4.t0.message);
589
+ reject(new Error(_errMsg2));
590
+ case 20:
591
+ case "end":
592
+ return _context4.stop();
593
+ }
594
+ }, _callee4, null, [[0, 16]]);
595
+ }));
596
+ return function (_x6, _x7) {
597
+ return _ref3.apply(this, arguments);
598
+ };
599
+ }()));
600
+ case 1:
601
+ case "end":
602
+ return _context5.stop();
603
+ }
604
+ }, _callee5);
605
+ }));
606
+ function stopRecordingEvent() {
607
+ return _stopRecordingEvent.apply(this, arguments);
608
+ }
609
+ return stopRecordingEvent;
610
+ }() //发送end指令
611
+ }, {
612
+ key: "endHandler",
613
+ value: function endHandler() {
614
+ var _this6 = this;
615
+ return new Promise(function (resolve, reject) {
616
+ var ws = _this6.ws;
617
+ // eslint-disable-next-line no-promise-executor-return
618
+ if (!ws) return reject(new Error('无有效连接'));
619
+ try {
620
+ ws.send(JSON.stringify({
621
+ type: 'end'
622
+ }));
623
+ resolve();
624
+ } catch (e) {
625
+ var errMsg = "\u53D1\u9001end\u5931\u8D25: ".concat(e.message);
626
+ reject(new Error(errMsg));
627
+ }
628
+ });
629
+ }
630
+
631
+ // 断开WebSocket
632
+ }, {
633
+ key: "disconnectHandler",
634
+ value: function () {
635
+ var _disconnectHandler = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
636
+ var _this$mediaRecorder, stream, audioContext, processor, source;
637
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
638
+ while (1) switch (_context6.prev = _context6.next) {
639
+ case 0:
640
+ if (this.ws) {
641
+ this.ws.close();
642
+ this.ws = null;
643
+ }
644
+ // 清理 TTS 播放器
645
+ if (this.audioPlayer) {
646
+ this.audioPlayer.audioContext.close();
647
+ this.audioPlayer = null;
648
+ console.log('🗑️ 清理 TTS 音频播放器');
649
+ }
650
+ // 强制停止录音
651
+ if (!this.mediaRecorder) {
652
+ _context6.next = 10;
653
+ break;
654
+ }
655
+ _this$mediaRecorder = this.mediaRecorder, stream = _this$mediaRecorder.stream, audioContext = _this$mediaRecorder.audioContext, processor = _this$mediaRecorder.processor, source = _this$mediaRecorder.source;
656
+ source.disconnect();
657
+ processor.disconnect();
658
+ _context6.next = 8;
659
+ return audioContext.close();
660
+ case 8:
661
+ stream.getTracks().forEach(function (track) {
662
+ return track.stop();
663
+ });
664
+ this.mediaRecorder = null;
665
+ case 10:
666
+ // 解除绑定监听事件
667
+ this.unbindNetworkEvents();
668
+ case 11:
669
+ case "end":
670
+ return _context6.stop();
671
+ }
672
+ }, _callee6, this);
673
+ }));
674
+ function disconnectHandler() {
675
+ return _disconnectHandler.apply(this, arguments);
676
+ }
677
+ return disconnectHandler;
678
+ }() // 核心音频取消
679
+ }, {
680
+ key: "recordingCancel",
681
+ value: function () {
682
+ var _recordingCancel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(data) {
683
+ var _data$modeType, modeType;
684
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
685
+ while (1) switch (_context7.prev = _context7.next) {
686
+ case 0:
687
+ _data$modeType = data.modeType, modeType = _data$modeType === void 0 ? '' : _data$modeType;
688
+ _context7.prev = 1;
689
+ if (!(modeType === HandlerMode.ASR_ONLY)) {
690
+ _context7.next = 11;
691
+ break;
692
+ }
693
+ _context7.next = 5;
694
+ return this.stopRecordingEvent();
695
+ case 5:
696
+ _context7.next = 7;
697
+ return this.endHandler();
698
+ case 7:
699
+ _context7.next = 9;
700
+ return this.disconnectHandler();
701
+ case 9:
702
+ _context7.next = 16;
703
+ break;
704
+ case 11:
705
+ if (!(modeType === HandlerMode.SHORT_ASR_ONLY)) {
706
+ _context7.next = 16;
707
+ break;
708
+ }
709
+ _context7.next = 14;
710
+ return this.stopRecordingEvent();
711
+ case 14:
712
+ _context7.next = 16;
713
+ return this.endHandler();
714
+ case 16:
715
+ _context7.next = 21;
716
+ break;
717
+ case 18:
718
+ _context7.prev = 18;
719
+ _context7.t0 = _context7["catch"](1);
720
+ throw new Error(_context7.t0.message);
721
+ case 21:
722
+ case "end":
723
+ return _context7.stop();
724
+ }
725
+ }, _callee7, this, [[1, 18]]);
726
+ }));
727
+ function recordingCancel(_x8) {
728
+ return _recordingCancel.apply(this, arguments);
729
+ }
730
+ return recordingCancel;
731
+ }()
732
+ }, {
733
+ key: "recordingClick",
734
+ value: // 核心录入音频入口
735
+ function () {
736
+ var _recordingClick = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(data, msgPrams, callBack) {
737
+ var _data$modeType2, modeType, error, err;
738
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
739
+ while (1) switch (_context8.prev = _context8.next) {
740
+ case 0:
741
+ _data$modeType2 = data.modeType, modeType = _data$modeType2 === void 0 ? '' : _data$modeType2; // 语音转文字模式
742
+ if (!(modeType === HandlerMode.ASR_ONLY)) {
743
+ _context8.next = 16;
744
+ break;
745
+ }
746
+ _context8.prev = 2;
747
+ _context8.next = 5;
748
+ return this.connectHandler(modeType);
749
+ case 5:
750
+ _context8.next = 7;
751
+ return this.startHandler(modeType, msgPrams);
752
+ case 7:
753
+ _context8.next = 9;
754
+ return this.toggleRecord();
755
+ case 9:
756
+ _context8.next = 14;
757
+ break;
758
+ case 11:
759
+ _context8.prev = 11;
760
+ _context8.t0 = _context8["catch"](2);
761
+ throw new Error("\u542F\u52A8\u5931\u8D25: ".concat(_context8.t0.message));
762
+ case 14:
763
+ _context8.next = 29;
764
+ break;
765
+ case 16:
766
+ if (!(modeType === HandlerMode.SHORT_ASR_ONLY)) {
767
+ _context8.next = 29;
768
+ break;
769
+ }
770
+ _context8.prev = 17;
771
+ _context8.next = 20;
772
+ return this.recordAndRecognize();
773
+ case 20:
774
+ if (callBack) {
775
+ callBack();
776
+ }
777
+ _context8.next = 29;
778
+ break;
779
+ case 23:
780
+ _context8.prev = 23;
781
+ _context8.t1 = _context8["catch"](17);
782
+ error = _context8.t1;
783
+ err = new Error("\u542F\u52A8\u5931\u8D25: ".concat((error === null || error === void 0 ? void 0 : error.message) || ''));
784
+ err.code = error === null || error === void 0 ? void 0 : error.code;
785
+ throw err;
786
+ case 29:
787
+ case "end":
788
+ return _context8.stop();
789
+ }
790
+ }, _callee8, this, [[2, 11], [17, 23]]);
791
+ }));
792
+ function recordingClick(_x9, _x10, _x11) {
793
+ return _recordingClick.apply(this, arguments);
794
+ }
795
+ return recordingClick;
796
+ }()
797
+ }, {
798
+ key: "writeString",
799
+ value: function writeString(view, offset, string) {
800
+ for (var i = 0; i < string.length; i++) {
801
+ view.setUint8(offset + i, string.charCodeAt(i));
802
+ }
803
+ }
804
+
805
+ /**
806
+ * 创建标准 WAV 格式的 Blob
807
+ * @param {Int16Array} pcmData - PCM 音频数据
808
+ * @param {number} sampleRate - 采样率
809
+ * @param {number} numChannels - 声道数
810
+ * @param {number} bitsPerSample - 位深度
811
+ * @returns {Blob} WAV 格式的 Blob
812
+ */
813
+ }, {
814
+ key: "createWavBlob",
815
+ value: function createWavBlob(pcmData, sampleRate, numChannels, bitsPerSample) {
816
+ var dataLength = pcmData.length * (bitsPerSample / 8);
817
+ var buffer = new ArrayBuffer(44 + dataLength);
818
+ var view = new DataView(buffer);
819
+
820
+ // WAV 文件头
821
+ // RIFF chunk descriptor
822
+ this.writeString(view, 0, 'RIFF');
823
+ view.setUint32(4, 36 + dataLength, true);
824
+ this.writeString(view, 8, 'WAVE');
825
+
826
+ // fmt sub-chunk
827
+ this.writeString(view, 12, 'fmt ');
828
+ view.setUint32(16, 16, true); // fmt chunk size
829
+ view.setUint16(20, 1, true); // audio format (1 = PCM)
830
+ view.setUint16(22, numChannels, true);
831
+ view.setUint32(24, sampleRate, true);
832
+ view.setUint32(28, sampleRate * numChannels * (bitsPerSample / 8), true); // byte rate
833
+ view.setUint16(32, numChannels * (bitsPerSample / 8), true); // block align
834
+ view.setUint16(34, bitsPerSample, true);
835
+
836
+ // data sub-chunk
837
+ this.writeString(view, 36, 'data');
838
+ view.setUint32(40, dataLength, true);
839
+
840
+ // 写入 PCM 数据
841
+ var offset = 44;
842
+ for (var i = 0; i < pcmData.length; i++) {
843
+ view.setInt16(offset + i * 2, pcmData[i], true);
844
+ }
845
+ return new Blob([buffer], {
846
+ type: 'audio/wav'
847
+ });
848
+ }
849
+ }, {
850
+ key: "blobToBase64",
851
+ value: function blobToBase64(blob) {
852
+ return new Promise(function (resolve, reject) {
853
+ var reader = new FileReader();
854
+ reader.onload = function () {
855
+ // 1. 类型守卫:确保 result 是字符串类型
856
+ if (typeof reader.result !== 'string') {
857
+ return reject(new Error('Blob 转换 Base64 失败:结果不是字符串类型'));
858
+ }
859
+ // 2. 校验 Base64 格式是否正确
860
+ var base64Parts = reader.result.split(',');
861
+ if (base64Parts.length < 2) {
862
+ return reject(new Error('Blob 转换 Base64 失败:格式异常'));
863
+ }
864
+ var base64 = base64Parts[1];
865
+ resolve(base64);
866
+ };
867
+ reader.onerror = reject;
868
+ reader.readAsDataURL(blob);
869
+ });
870
+ }
871
+
872
+ // 一句话停止并识别录音
873
+ }, {
874
+ key: "toggleTencentRecord",
875
+ value: function () {
876
+ var _toggleTencentRecord = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
877
+ var configuration,
878
+ onResult,
879
+ stream,
880
+ audioContext,
881
+ processor,
882
+ source,
883
+ totalLength,
884
+ mergedPcm,
885
+ offset,
886
+ _iterator2,
887
+ _step2,
888
+ chunk,
889
+ wavBlob,
890
+ base64Data,
891
+ _data2,
892
+ _args9 = arguments;
893
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
894
+ while (1) switch (_context9.prev = _context9.next) {
895
+ case 0:
896
+ configuration = _args9.length > 0 && _args9[0] !== undefined ? _args9[0] : null;
897
+ onResult = _args9.length > 1 ? _args9[1] : undefined;
898
+ if (!(!tencentRecorder.stream || !tencentRecorder.audioContext)) {
899
+ _context9.next = 4;
900
+ break;
901
+ }
902
+ throw new Error('腾讯云录音未初始化,无法停止');
903
+ case 4:
904
+ _context9.prev = 4;
905
+ stream = tencentRecorder.stream, audioContext = tencentRecorder.audioContext, processor = tencentRecorder.processor, source = tencentRecorder.source;
906
+ source === null || source === void 0 || source.disconnect();
907
+ processor === null || processor === void 0 || processor.disconnect();
908
+ _context9.next = 10;
909
+ return audioContext.close();
910
+ case 10:
911
+ stream.getTracks().forEach(function (track) {
912
+ return track.stop();
913
+ });
914
+ // 合并所有 PCM 数据
915
+ totalLength = tencentRecorder.audioChunks.reduce(function (acc, chunk) {
916
+ return acc + chunk.length;
917
+ }, 0);
918
+ mergedPcm = new Int16Array(totalLength);
919
+ offset = 0;
920
+ _iterator2 = _createForOfIteratorHelper(tencentRecorder.audioChunks);
921
+ try {
922
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
923
+ chunk = _step2.value;
924
+ mergedPcm.set(chunk, offset);
925
+ offset += chunk.length;
926
+ }
927
+
928
+ // 转换为 WAV 格式
929
+ } catch (err) {
930
+ _iterator2.e(err);
931
+ } finally {
932
+ _iterator2.f();
933
+ }
934
+ wavBlob = this.createWavBlob(mergedPcm, 16000, 1, 16); // 转换为 Base64
935
+ _context9.next = 19;
936
+ return this.blobToBase64(wavBlob);
937
+ case 19:
938
+ base64Data = _context9.sent;
939
+ _context9.next = 22;
940
+ return this.callTencentAsrApi(base64Data, configuration);
941
+ case 22:
942
+ _data2 = _context9.sent;
943
+ if (onResult) {
944
+ onResult(_data2);
945
+ }
946
+ this.clearTencentRecorder();
947
+ _context9.next = 31;
948
+ break;
949
+ case 27:
950
+ _context9.prev = 27;
951
+ _context9.t0 = _context9["catch"](4);
952
+ this.clearTencentRecorder();
953
+ throw new Error(_context9.t0.message);
954
+ case 31:
955
+ case "end":
956
+ return _context9.stop();
957
+ }
958
+ }, _callee9, this, [[4, 27]]);
959
+ }));
960
+ function toggleTencentRecord() {
961
+ return _toggleTencentRecord.apply(this, arguments);
962
+ }
963
+ return toggleTencentRecord;
964
+ }() /** 清理tencentRecorder */
965
+ }, {
966
+ key: "clearTencentRecorder",
967
+ value: function clearTencentRecorder() {
968
+ tencentRecorder.mediaRecorder = null;
969
+ tencentRecorder.audioChunks = []; // 清空音频数据
970
+ tencentRecorder.audioContext = null;
971
+ tencentRecorder.stream = null;
972
+ tencentRecorder.source = null;
973
+ tencentRecorder.processor = null;
974
+ }
975
+
976
+ // 调用识别 API
977
+ }, {
978
+ key: "callTencentAsrApi",
979
+ value: function () {
980
+ var _callTencentAsrApi = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee10(base64Data) {
981
+ var configuration,
982
+ engineModel,
983
+ filterDirty,
984
+ filterModal,
985
+ filterPunc,
986
+ convertNum,
987
+ requestData,
988
+ baseUrl,
989
+ api,
990
+ response_data,
991
+ _result,
992
+ errorMsg,
993
+ _args10 = arguments;
994
+ return _regeneratorRuntime().wrap(function _callee10$(_context10) {
995
+ while (1) switch (_context10.prev = _context10.next) {
996
+ case 0:
997
+ configuration = _args10.length > 1 && _args10[1] !== undefined ? _args10[1] : null;
998
+ engineModel = null;
999
+ filterDirty = null;
1000
+ filterModal = null;
1001
+ filterPunc = null;
1002
+ convertNum = null;
1003
+ if (configuration) {
1004
+ engineModel = configuration.engineModel || null;
1005
+ filterDirty = configuration.filterDirty || null;
1006
+ filterModal = configuration.filterModal || null;
1007
+ filterPunc = configuration.filterPunc || null;
1008
+ convertNum = configuration.convertNum || null;
1009
+ }
1010
+ requestData = {
1011
+ audioData: base64Data,
1012
+ format: 'wav',
1013
+ engineModelType: engineModel,
1014
+ filterDirty: filterDirty,
1015
+ filterModal: filterModal,
1016
+ filterPunc: filterPunc,
1017
+ convertNumMode: convertNum
1018
+ };
1019
+ _context10.prev = 8;
1020
+ baseUrl = store.getState().gimiMenu.baseUrl;
1021
+ api = createFetch({
1022
+ baseURL: baseUrl,
1023
+ timeout: 10000,
1024
+ headers: {
1025
+ 'Authorization': 'Bearer ' + store.getState().gimiMenu.token,
1026
+ 'Content-Type': 'application/json'
1027
+ }
1028
+ });
1029
+ _context10.next = 13;
1030
+ return api.post("/mbot/api/tencent/asr/recognize", requestData);
1031
+ case 13:
1032
+ response_data = _context10.sent;
1033
+ if (!(response_data.status !== 0)) {
1034
+ _context10.next = 16;
1035
+ break;
1036
+ }
1037
+ throw new Error('HTTP ' + response_data.status);
1038
+ case 16:
1039
+ if (!(response_data.status === 0 && response_data.result)) {
1040
+ _context10.next = 21;
1041
+ break;
1042
+ }
1043
+ _result = response_data.result;
1044
+ return _context10.abrupt("return", _result);
1045
+ case 21:
1046
+ // 处理接口级别的错误
1047
+ errorMsg = response_data.message || '请求失败';
1048
+ throw new Error(errorMsg);
1049
+ case 23:
1050
+ _context10.next = 28;
1051
+ break;
1052
+ case 25:
1053
+ _context10.prev = 25;
1054
+ _context10.t0 = _context10["catch"](8);
1055
+ throw new Error(_context10.t0.message);
1056
+ case 28:
1057
+ case "end":
1058
+ return _context10.stop();
1059
+ }
1060
+ }, _callee10, null, [[8, 25]]);
1061
+ }));
1062
+ function callTencentAsrApi(_x12) {
1063
+ return _callTencentAsrApi.apply(this, arguments);
1064
+ }
1065
+ return callTencentAsrApi;
1066
+ }() // 新增:TTS 模式启动入口
1067
+ }, {
1068
+ key: "startTTS",
1069
+ value: function () {
1070
+ var _startTTS = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee11(text, msgPrams) {
1071
+ return _regeneratorRuntime().wrap(function _callee11$(_context11) {
1072
+ while (1) switch (_context11.prev = _context11.next) {
1073
+ case 0:
1074
+ this.ttsSessionId++;
1075
+ _context11.prev = 1;
1076
+ _context11.next = 4;
1077
+ return this.connectHandler(HandlerMode.TTS_ONLY);
1078
+ case 4:
1079
+ _context11.next = 6;
1080
+ return this.startHandler(HandlerMode.TTS_ONLY, msgPrams);
1081
+ case 6:
1082
+ if (!text) {
1083
+ _context11.next = 9;
1084
+ break;
1085
+ }
1086
+ _context11.next = 9;
1087
+ return this.sendTextForTTS(text);
1088
+ case 9:
1089
+ _context11.next = 14;
1090
+ break;
1091
+ case 11:
1092
+ _context11.prev = 11;
1093
+ _context11.t0 = _context11["catch"](1);
1094
+ throw new Error("\u97F3\u9891\u6D41\u9519\u8BEF: ".concat(_context11.t0.message));
1095
+ case 14:
1096
+ case "end":
1097
+ return _context11.stop();
1098
+ }
1099
+ }, _callee11, this, [[1, 11]]);
1100
+ }));
1101
+ function startTTS(_x13, _x14) {
1102
+ return _startTTS.apply(this, arguments);
1103
+ }
1104
+ return startTTS;
1105
+ }() // 新增:重置 TTS 播放器
1106
+ }, {
1107
+ key: "resetTTSPlayer",
1108
+ value: function resetTTSPlayer() {
1109
+ if (this.audioPlayer) {
1110
+ this.audioPlayer.reset();
1111
+ }
1112
+ }
1113
+
1114
+ // 新增:发送文本进行语音合成
1115
+ }, {
1116
+ key: "sendTextForTTS",
1117
+ value: function () {
1118
+ var _sendTextForTTS = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee12(text) {
1119
+ var _this7 = this;
1120
+ return _regeneratorRuntime().wrap(function _callee12$(_context12) {
1121
+ while (1) switch (_context12.prev = _context12.next) {
1122
+ case 0:
1123
+ return _context12.abrupt("return", new Promise(function (resolve, reject) {
1124
+ var ws = _this7.ws;
1125
+ if (!ws || ws.readyState !== WebSocket.OPEN) {
1126
+ var errMsg = '请先建立WebSocket连接';
1127
+ // eslint-disable-next-line no-promise-executor-return
1128
+ return reject(new Error(errMsg));
1129
+ }
1130
+ if (!text) {
1131
+ var _errMsg3 = '请输入合成文本';
1132
+ // eslint-disable-next-line no-promise-executor-return
1133
+ return reject(new Error(_errMsg3));
1134
+ }
1135
+ var msg = {
1136
+ type: 'text',
1137
+ text: text
1138
+ };
1139
+ try {
1140
+ ws.send(JSON.stringify(msg));
1141
+ resolve();
1142
+ } catch (e) {
1143
+ var _errMsg4 = "\u53D1\u9001\u6587\u672C\u5931\u8D25: ".concat(e.message);
1144
+ reject(new Error(_errMsg4));
1145
+ }
1146
+ }));
1147
+ case 1:
1148
+ case "end":
1149
+ return _context12.stop();
1150
+ }
1151
+ }, _callee12);
1152
+ }));
1153
+ function sendTextForTTS(_x15) {
1154
+ return _sendTextForTTS.apply(this, arguments);
1155
+ }
1156
+ return sendTextForTTS;
1157
+ }()
1158
+ }]);
1159
+ return VoiceRecorder;
1160
+ }();
1161
+ var VoiceRecorderEvent = new VoiceRecorder();
1162
+ export default VoiceRecorderEvent;