@coze/realtime-api 1.1.0 → 1.1.1-beta.2

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/README.md CHANGED
@@ -56,6 +56,15 @@ const client = new RealtimeClient({
56
56
  voiceId: "your_voice_id", // Optional: Specify voice ID
57
57
  conversationId: "conversation_id", // Optional: For conversation continuity
58
58
  debug: true, // Optional: Enable debug logging
59
+ getRoomInfo: async () => {
60
+ // Customize get room info
61
+ return {
62
+ token: "your_token",
63
+ uid: "your_uid",
64
+ room_id: "your_room_id",
65
+ app_id: "your_app_id",
66
+ };
67
+ },
59
68
  allowPersonalAccessTokenInBrowser: true, // Optional: Enable PAT token usage in browser
60
69
  audioMutedDefault: false, // Optional: Initial audio state (default: false)
61
70
  suppressStationaryNoise: false, // Optional: Enable stationary noise suppression(default: false)
package/README.zh-CN.md CHANGED
@@ -56,6 +56,15 @@ const client = new RealtimeClient({
56
56
  voiceId: "your_voice_id", // 可选:指定音色 ID
57
57
  conversationId: "conversation_id", // 可选:用于对话连续性
58
58
  debug: true, // 可选:启用调试日志
59
+ getRoomInfo: async () => {
60
+ // 自定义获取房间信息
61
+ return {
62
+ token: "your_token",
63
+ uid: "your_uid",
64
+ room_id: "your_room_id",
65
+ app_id: "your_app_id",
66
+ };
67
+ },
59
68
  allowPersonalAccessTokenInBrowser: true, // 可选:在浏览器中启用 PAT 令牌使用
60
69
  audioMutedDefault: false, // 可选:初始音频状态(默认:false)
61
70
  suppressStationaryNoise: false, // 可选:启用静态噪声抑制(默认:false)
@@ -92,7 +92,7 @@ __webpack_require__.d(__webpack_exports__, {
92
92
  RealtimeAPIError: ()=>/* reexport */ RealtimeAPIError,
93
93
  RealtimeUtils: ()=>/* reexport */ src_utils_namespaceObject,
94
94
  RealtimeError: ()=>/* reexport */ error_RealtimeError,
95
- EventNames: ()=>/* reexport */ event_handler_EventNames,
95
+ EventNames: ()=>/* reexport */ event_names_EventNames,
96
96
  RealtimeClient: ()=>/* binding */ RealtimeClient
97
97
  });
98
98
  // NAMESPACE OBJECT: ../../common/temp/default/node_modules/.pnpm/axios@1.7.7_debug@4.3.7/node_modules/axios/lib/platform/common/utils.js
@@ -358,6 +358,12 @@ const handleAdditionalMessages = (additional_messages)=>null == additional_messa
358
358
  ...i,
359
359
  content: 'object' == typeof i.content ? JSON.stringify(i.content) : i.content
360
360
  }));
361
+ const handleParameters = (parameters)=>{
362
+ if (parameters) {
363
+ for (const [key, value] of Object.entries(parameters))if ('object' == typeof value) parameters[key] = JSON.stringify(value);
364
+ }
365
+ return parameters;
366
+ };
361
367
  class Chat extends APIResource {
362
368
  /**
363
369
  * Call the Chat API to send messages to a published Coze agent. | 调用此接口发起一次对话,支持添加上下文
@@ -380,6 +386,10 @@ class Chat extends APIResource {
380
386
  const payload = {
381
387
  ...rest,
382
388
  additional_messages: handleAdditionalMessages(params.additional_messages),
389
+ shortcut_command: params.shortcut_command ? {
390
+ ...params.shortcut_command,
391
+ parameters: handleParameters(params.shortcut_command.parameters)
392
+ } : void 0,
383
393
  stream: false
384
394
  };
385
395
  const result = await this._client.post(apiUrl, payload, false, options);
@@ -406,6 +416,10 @@ class Chat extends APIResource {
406
416
  const payload = {
407
417
  ...rest,
408
418
  additional_messages: handleAdditionalMessages(params.additional_messages),
419
+ shortcut_command: params.shortcut_command ? {
420
+ ...params.shortcut_command,
421
+ parameters: handleParameters(params.shortcut_command.parameters)
422
+ } : void 0,
409
423
  stream: false
410
424
  };
411
425
  const result = await this._client.post(apiUrl, payload, false, options);
@@ -444,6 +458,10 @@ class Chat extends APIResource {
444
458
  const payload = {
445
459
  ...rest,
446
460
  additional_messages: handleAdditionalMessages(params.additional_messages),
461
+ shortcut_command: params.shortcut_command ? {
462
+ ...params.shortcut_command,
463
+ parameters: handleParameters(params.shortcut_command.parameters)
464
+ } : void 0,
447
465
  stream: true
448
466
  };
449
467
  const result = await this._client.post(apiUrl, payload, true, options);
@@ -4607,6 +4625,8 @@ class WebSocketAPI {
4607
4625
  });
4608
4626
  this.rws.addEventListener('error', (event)=>{
4609
4627
  var _event_target__req_res, _event_target__req, _event_target, _event_target__req_res1, _event_target__req1, _event_target1, _this_onerror, _this;
4628
+ const { readyState } = this.rws;
4629
+ if (3 === readyState) return;
4610
4630
  const statusCode = null === (_event_target = event.target) || void 0 === _event_target ? void 0 : null === (_event_target__req = _event_target._req) || void 0 === _event_target__req ? void 0 : null === (_event_target__req_res = _event_target__req.res) || void 0 === _event_target__req_res ? void 0 : _event_target__req_res.statusCode;
4611
4631
  const rawHeaders = (null === (_event_target1 = event.target) || void 0 === _event_target1 ? void 0 : null === (_event_target__req1 = _event_target1._req) || void 0 === _event_target__req1 ? void 0 : null === (_event_target__req_res1 = _event_target__req1.res) || void 0 === _event_target__req_res1 ? void 0 : _event_target__req_res1.rawHeaders) || [];
4612
4632
  const logidIndex = rawHeaders.findIndex((header)=>'X-Tt-Logid' === header);
@@ -4630,7 +4650,8 @@ class WebSocketAPI {
4630
4650
  error.data.msg = 'Forbidden';
4631
4651
  } else {
4632
4652
  error.data.code = 500;
4633
- error.data.msg = String(null == event ? void 0 : event.error) || 'WebSocket error';
4653
+ var _event_error;
4654
+ error.data.msg = String(null !== (_event_error = null == event ? void 0 : event.error) && void 0 !== _event_error ? _event_error : '') || 'WebSocket error';
4634
4655
  }
4635
4656
  null === (_this_onerror = (_this = this).onerror) || void 0 === _this_onerror || _this_onerror.call(_this, error, event);
4636
4657
  });
@@ -4639,7 +4660,7 @@ class WebSocketAPI {
4639
4660
  // EXTERNAL MODULE: os (ignored)
4640
4661
  var os_ignored_ = __webpack_require__("?9050");
4641
4662
  var os_ignored_default = /*#__PURE__*/ __webpack_require__.n(os_ignored_);
4642
- var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.1.0","description":"Official Coze Node.js SDK for seamless AI integration into your applications | 扣子官方 Node.js SDK,助您轻松集成 AI 能力到应用中","keywords":["coze","ai","nodejs","sdk","chatbot","typescript"],"homepage":"https://github.com/coze-dev/coze-js/tree/main/packages/coze-js","bugs":{"url":"https://github.com/coze-dev/coze-js/issues"},"repository":{"type":"git","url":"https://github.com/coze-dev/coze-js.git","directory":"packages/coze-js"},"license":"MIT","author":"Leeight <leeight@gmail.com>","type":"module","exports":{".":"./src/index.ts","./ws-tools":"./src/ws-tools/index.ts"},"main":"src/index.ts","module":"src/index.ts","browser":{"crypto":false,"os":false,"jsonwebtoken":false,"node-fetch":false},"typesVersions":{"*":{".":["dist/types/index.d.ts"],"ws-tools":["dist/types/ws-tools/ws-tools/index.d.ts"]}},"files":["dist","LICENSE","README.md","README.zh-CN.md"],"scripts":{"build":"rslib build","format":"prettier --write .","lint":"eslint ./ --cache --quiet","start":"rslib build -w","test":"vitest","test:cov":"vitest --coverage --run"},"dependencies":{"jsonwebtoken":"^9.0.2","node-fetch":"^2.x","reconnecting-websocket":"^4.4.0","uuid":"^10.0.0","ws":"^8.11.0"},"devDependencies":{"@coze-infra/eslint-config":"workspace:*","@coze-infra/ts-config":"workspace:*","@coze-infra/vitest-config":"workspace:*","@rslib/core":"0.0.18","@swc/core":"^1.3.14","@types/jsonwebtoken":"^9.0.0","@types/node":"^20","@types/node-fetch":"^2.x","@types/uuid":"^9.0.1","@types/whatwg-fetch":"^0.0.33","@types/ws":"^8.5.1","@vitest/coverage-v8":"~2.1.4","axios":"^1.7.7","typescript":"^5.5.3","vitest":"~2.1.4"},"peerDependencies":{"axios":"^1.7.1"},"cozePublishConfig":{"exports":{".":{"require":"./dist/cjs/index.cjs","import":"./dist/esm/index.js","types":"./dist/types/index.d.ts"},"./ws-tools":{"require":"./dist/cjs/ws-tools/index.cjs","import":"./dist/esm/ws-tools/index.js","types":"./dist/types/ws-tools/ws-tools/index.d.ts"}},"main":"dist/cjs/index.cjs","module":"dist/esm/index.js","types":"dist/types/index.d.ts"}}'); // CONCATENATED MODULE: ../coze-js/src/version.ts
4663
+ var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.1.1-beta.1","description":"Official Coze Node.js SDK for seamless AI integration into your applications | 扣子官方 Node.js SDK,助您轻松集成 AI 能力到应用中","keywords":["coze","ai","nodejs","sdk","chatbot","typescript"],"homepage":"https://github.com/coze-dev/coze-js/tree/main/packages/coze-js","bugs":{"url":"https://github.com/coze-dev/coze-js/issues"},"repository":{"type":"git","url":"https://github.com/coze-dev/coze-js.git","directory":"packages/coze-js"},"license":"MIT","author":"Leeight <leeight@gmail.com>","exports":{".":"./src/index.ts","./ws-tools":"./src/ws-tools/index.ts"},"main":"src/index.ts","module":"src/index.ts","browser":{"crypto":false,"os":false,"jsonwebtoken":false,"node-fetch":false},"typesVersions":{"*":{".":["dist/types/index.d.ts"],"ws-tools":["dist/types/ws-tools/ws-tools/index.d.ts"]}},"files":["dist","LICENSE","README.md","README.zh-CN.md"],"scripts":{"build":"rslib build","format":"prettier --write .","lint":"eslint ./ --cache --quiet","start":"rslib build -w","test":"vitest","test:cov":"vitest --coverage --run"},"dependencies":{"jsonwebtoken":"^9.0.2","node-fetch":"^2.x","reconnecting-websocket":"^4.4.0","uuid":"^10.0.0","ws":"^8.11.0"},"devDependencies":{"@coze-infra/eslint-config":"workspace:*","@coze-infra/ts-config":"workspace:*","@coze-infra/vitest-config":"workspace:*","@rslib/core":"0.0.18","@swc/core":"^1.3.14","@types/jsonwebtoken":"^9.0.0","@types/node":"^20","@types/node-fetch":"^2.x","@types/uuid":"^9.0.1","@types/whatwg-fetch":"^0.0.33","@types/ws":"^8.5.1","@vitest/coverage-v8":"~2.1.4","axios":"^1.7.7","typescript":"^5.5.3","vitest":"~2.1.4"},"peerDependencies":{"axios":"^1.7.1"},"cozePublishConfig":{"exports":{".":{"require":"./dist/cjs/index.js","import":"./dist/esm/index.mjs","types":"./dist/types/index.d.ts"},"./ws-tools":{"require":"./dist/cjs/ws-tools/index.js","import":"./dist/esm/ws-tools/index.mjs","types":"./dist/types/ws-tools/ws-tools/index.d.ts"}},"main":"dist/cjs/index.js","module":"dist/esm/index.mjs","types":"dist/types/index.d.ts"}}'); // CONCATENATED MODULE: ../coze-js/src/version.ts
4643
4664
  const { version: version_version } = package_namespaceObject;
4644
4665
  const getEnv = ()=>{
4645
4666
  const nodeVersion = process.version.slice(1); // Remove 'v' prefix
@@ -39443,34 +39464,7 @@ const isScreenShareDevice = (deviceId)=>'screenShare' === deviceId;
39443
39464
  var _navigator_mediaDevices, _navigator;
39444
39465
  return !!(null === (_navigator = navigator) || void 0 === _navigator ? void 0 : null === (_navigator_mediaDevices = _navigator.mediaDevices) || void 0 === _navigator_mediaDevices ? void 0 : _navigator_mediaDevices.getDisplayMedia);
39445
39466
  }
39446
- var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
39447
- RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
39448
- RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
39449
- RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
39450
- RealtimeError["DISCONNECTION_ERROR"] = "DISCONNECTION_ERROR";
39451
- RealtimeError["INTERRUPT_ERROR"] = "INTERRUPT_ERROR";
39452
- RealtimeError["EVENT_HANDLER_ERROR"] = "EVENT_HANDLER_ERROR";
39453
- RealtimeError["PERMISSION_DENIED"] = "PERMISSION_DENIED";
39454
- RealtimeError["NETWORK_ERROR"] = "NETWORK_ERROR";
39455
- RealtimeError["INVALID_STATE"] = "INVALID_STATE";
39456
- RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
39457
- RealtimeError["PARSE_MESSAGE_ERROR"] = "PARSE_MESSAGE_ERROR";
39458
- RealtimeError["HANDLER_MESSAGE_ERROR"] = "HANDLER_MESSAGE_ERROR";
39459
- return RealtimeError;
39460
- }({});
39461
- class RealtimeAPIError extends Error {
39462
- /**
39463
- * @param code - Error code
39464
- * @param message - Error message
39465
- * @param error - Error object
39466
- */ constructor(code, message, error){
39467
- super(`[${code}] ${message}`);
39468
- this.name = 'RealtimeAPIError';
39469
- this.code = code;
39470
- this.error = error;
39471
- }
39472
- }
39473
- var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
39467
+ var event_names_EventNames = /*#__PURE__*/ function(EventNames) {
39474
39468
  /**
39475
39469
  * en: All events
39476
39470
  * zh: 所有事件
@@ -39488,6 +39482,10 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
39488
39482
  * zh: 客户端连接
39489
39483
  */ EventNames["CONNECTED"] = "client.connected";
39490
39484
  /**
39485
+ * en: Client connecting
39486
+ * zh: 客户端连接中
39487
+ */ EventNames["CONNECTING"] = "client.connecting";
39488
+ /**
39491
39489
  * en: Client interrupted
39492
39490
  * zh: 客户端中断
39493
39491
  */ EventNames["INTERRUPTED"] = "client.interrupted";
@@ -39540,6 +39538,10 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
39540
39538
  * zh: 视频输入设备改变
39541
39539
  */ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
39542
39540
  /**
39541
+ * en: Network quality changed
39542
+ * zh: 网络质量改变
39543
+ */ EventNames["NETWORK_QUALITY"] = "client.network.quality";
39544
+ /**
39543
39545
  * en: Bot joined
39544
39546
  * zh: Bot 加入
39545
39547
  */ EventNames["BOT_JOIN"] = "server.bot.join";
@@ -39554,7 +39556,7 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
39554
39556
  /**
39555
39557
  * en: Audio speech stopped
39556
39558
  * zh: 停止说话
39557
- */ EventNames["AUDIO_SPEECH_STOPPED"] = "server.audio.speech_stopped";
39559
+ */ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
39558
39560
  /**
39559
39561
  * en: Server error
39560
39562
  * zh: 服务端错误
@@ -39577,6 +39579,33 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
39577
39579
  */ EventNames["SESSION_UPDATE"] = "server.session.update";
39578
39580
  return EventNames;
39579
39581
  }({});
39582
+ var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
39583
+ RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
39584
+ RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
39585
+ RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
39586
+ RealtimeError["DISCONNECTION_ERROR"] = "DISCONNECTION_ERROR";
39587
+ RealtimeError["INTERRUPT_ERROR"] = "INTERRUPT_ERROR";
39588
+ RealtimeError["EVENT_HANDLER_ERROR"] = "EVENT_HANDLER_ERROR";
39589
+ RealtimeError["PERMISSION_DENIED"] = "PERMISSION_DENIED";
39590
+ RealtimeError["NETWORK_ERROR"] = "NETWORK_ERROR";
39591
+ RealtimeError["INVALID_STATE"] = "INVALID_STATE";
39592
+ RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
39593
+ RealtimeError["PARSE_MESSAGE_ERROR"] = "PARSE_MESSAGE_ERROR";
39594
+ RealtimeError["HANDLER_MESSAGE_ERROR"] = "HANDLER_MESSAGE_ERROR";
39595
+ return RealtimeError;
39596
+ }({});
39597
+ class RealtimeAPIError extends Error {
39598
+ /**
39599
+ * @param code - Error code
39600
+ * @param message - Error message
39601
+ * @param error - Error object
39602
+ */ constructor(code, message, error){
39603
+ super(`[${code}] ${message}`);
39604
+ this.name = 'RealtimeAPIError';
39605
+ this.code = code;
39606
+ this.error = error;
39607
+ }
39608
+ }
39580
39609
  class RealtimeEventHandler {
39581
39610
  clearEventHandlers() {
39582
39611
  this.eventHandlers = {};
@@ -39592,7 +39621,10 @@ class RealtimeEventHandler {
39592
39621
  const handlers = this.eventHandlers[eventName] || [];
39593
39622
  if (callback) {
39594
39623
  const index = handlers.indexOf(callback);
39595
- if (-1 === index) throw new RealtimeAPIError(error_RealtimeError.EVENT_HANDLER_ERROR, `Could not turn off specified event listener for "${eventName}": not found as a listener`);
39624
+ if (-1 === index) {
39625
+ console.warn(`Could not turn off specified event listener for "${eventName}": not found as a listener`);
39626
+ return;
39627
+ }
39596
39628
  handlers.splice(index, 1);
39597
39629
  } else delete this.eventHandlers[eventName];
39598
39630
  }
@@ -39606,18 +39638,18 @@ class RealtimeEventHandler {
39606
39638
  }
39607
39639
  dispatch(eventName, event) {
39608
39640
  let consoleLog = !(arguments.length > 2) || void 0 === arguments[2] || arguments[2];
39609
- if (consoleLog) this._log(`dispatch ${eventName} event`);
39641
+ if (consoleLog) this._log(`dispatch ${eventName} event`, event);
39610
39642
  const handlers = (this.eventHandlers[eventName] || []).slice();
39611
39643
  this._dispatchToHandlers(eventName, event, handlers);
39612
- const allHandlers = (this.eventHandlers["realtime.event"] || []).slice();
39644
+ const allHandlers = (this.eventHandlers[event_names_EventNames.ALL] || []).slice();
39613
39645
  this._dispatchToHandlers(eventName, event, allHandlers);
39614
- const allClientHandlers = (this.eventHandlers["client.*"] || []).slice();
39646
+ const allClientHandlers = (this.eventHandlers[event_names_EventNames.ALL_CLIENT] || []).slice();
39615
39647
  this._dispatchToHandlers(eventName, event, allClientHandlers, 'client.');
39616
- const allServerHandlers = (this.eventHandlers["server.*"] || []).slice();
39648
+ const allServerHandlers = (this.eventHandlers[event_names_EventNames.ALL_SERVER] || []).slice();
39617
39649
  this._dispatchToHandlers(eventName, event, allServerHandlers, 'server.');
39618
39650
  }
39619
- _log(message) {
39620
- if (this._debug) console.log(`[RealtimeClient] ${message}`);
39651
+ _log(message, event) {
39652
+ if (this._debug) console.log(`[RealtimeClient] ${message}`, event);
39621
39653
  }
39622
39654
  constructor(debug = false){
39623
39655
  this.eventHandlers = {};
@@ -42875,6 +42907,7 @@ class EngineClient extends RealtimeEventHandler {
42875
42907
  this.engine.on(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
42876
42908
  this.engine.on(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
42877
42909
  this.engine.on(index_esm_min_index.events.onError, this.handleEventError);
42910
+ this.engine.on(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
42878
42911
  if (this._isSupportVideo) this.engine.on(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
42879
42912
  if (this._debug) {
42880
42913
  this.engine.on(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
@@ -42886,6 +42919,7 @@ class EngineClient extends RealtimeEventHandler {
42886
42919
  this.engine.off(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
42887
42920
  this.engine.off(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
42888
42921
  this.engine.off(index_esm_min_index.events.onError, this.handleEventError);
42922
+ this.engine.off(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
42889
42923
  if (this._isSupportVideo) this.engine.off(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
42890
42924
  if (this._debug) {
42891
42925
  this.engine.off(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
@@ -42906,29 +42940,35 @@ class EngineClient extends RealtimeEventHandler {
42906
42940
  this.dispatch(`server.${message.event_type}`, message);
42907
42941
  } catch (e) {
42908
42942
  if (e instanceof RealtimeAPIError) {
42909
- if (e.code === error_RealtimeError.PARSE_MESSAGE_ERROR) this.dispatch(event_handler_EventNames.ERROR, {
42943
+ if (e.code === error_RealtimeError.PARSE_MESSAGE_ERROR) this.dispatch(event_names_EventNames.ERROR, {
42910
42944
  message: `Failed to parse message: ${event.message}`,
42911
42945
  error: e
42912
42946
  });
42913
- else if (e.code === error_RealtimeError.HANDLER_MESSAGE_ERROR) this.dispatch(event_handler_EventNames.ERROR, {
42947
+ else if (e.code === error_RealtimeError.HANDLER_MESSAGE_ERROR) this.dispatch(event_names_EventNames.ERROR, {
42914
42948
  message: `Failed to handle message: ${event.message}`,
42915
42949
  error: e
42916
42950
  });
42917
- } else this.dispatch(event_handler_EventNames.ERROR, e);
42951
+ } else this.dispatch(event_names_EventNames.ERROR, e);
42918
42952
  }
42919
42953
  }
42920
42954
  handleEventError(e) {
42921
- this.dispatch(event_handler_EventNames.ERROR, e);
42955
+ this.dispatch(event_names_EventNames.ERROR, e);
42922
42956
  }
42923
42957
  handleUserJoin(event) {
42924
42958
  this.joinUserId = event.userInfo.userId;
42925
- this.dispatch(event_handler_EventNames.BOT_JOIN, event);
42959
+ this.dispatch(event_names_EventNames.BOT_JOIN, event);
42926
42960
  }
42927
42961
  handleUserLeave(event) {
42928
- this.dispatch(event_handler_EventNames.BOT_LEAVE, event);
42962
+ this.dispatch(event_names_EventNames.BOT_LEAVE, event);
42929
42963
  }
42930
42964
  handlePlayerEvent(event) {
42931
- this.dispatch(event_handler_EventNames.PLAYER_EVENT, event);
42965
+ this.dispatch(event_names_EventNames.PLAYER_EVENT, event);
42966
+ }
42967
+ handleNetworkQuality(uplinkNetworkQuality, downlinkNetworkQuality) {
42968
+ this.dispatch(event_names_EventNames.NETWORK_QUALITY, {
42969
+ uplinkNetworkQuality,
42970
+ downlinkNetworkQuality
42971
+ });
42932
42972
  }
42933
42973
  async joinRoom(options) {
42934
42974
  const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
@@ -42996,13 +43036,12 @@ class EngineClient extends RealtimeEventHandler {
42996
43036
  }
42997
43037
  async disconnect() {
42998
43038
  try {
42999
- if (this._isSupportVideo) await this.changeVideoState(false);
43000
- await this.changeAudioState(false);
43001
- await this.engine.unpublishStream(MediaType$1.AUDIO);
43002
43039
  await this.engine.leaveRoom();
43003
43040
  this.removeEventListener();
43041
+ this.clearEventHandlers();
43042
+ index_esm_min_index.destroyEngine(this.engine);
43004
43043
  } catch (e) {
43005
- this.dispatch(event_handler_EventNames.ERROR, e);
43044
+ this.dispatch(event_names_EventNames.ERROR, e);
43006
43045
  throw e;
43007
43046
  }
43008
43047
  }
@@ -43011,7 +43050,7 @@ class EngineClient extends RealtimeEventHandler {
43011
43050
  if (isMicOn) await this.engine.publishStream(MediaType$1.AUDIO);
43012
43051
  else await this.engine.unpublishStream(MediaType$1.AUDIO);
43013
43052
  } catch (e) {
43014
- this.dispatch(event_handler_EventNames.ERROR, e);
43053
+ this.dispatch(event_names_EventNames.ERROR, e);
43015
43054
  throw e;
43016
43055
  }
43017
43056
  }
@@ -43031,7 +43070,7 @@ class EngineClient extends RealtimeEventHandler {
43031
43070
  await this.engine.unpublishScreen(MediaType$1.VIDEO);
43032
43071
  }
43033
43072
  } catch (e) {
43034
- this.dispatch(event_handler_EventNames.ERROR, e);
43073
+ this.dispatch(event_names_EventNames.ERROR, e);
43035
43074
  throw e;
43036
43075
  }
43037
43076
  }
@@ -43044,7 +43083,7 @@ class EngineClient extends RealtimeEventHandler {
43044
43083
  }));
43045
43084
  this._log(`interrupt ${this.joinUserId} ${result}`);
43046
43085
  } catch (e) {
43047
- this.dispatch(event_handler_EventNames.ERROR, e);
43086
+ this.dispatch(event_names_EventNames.ERROR, e);
43048
43087
  throw e;
43049
43088
  }
43050
43089
  }
@@ -43053,7 +43092,7 @@ class EngineClient extends RealtimeEventHandler {
43053
43092
  const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify(message));
43054
43093
  this._log(`sendMessage ${this.joinUserId} ${JSON.stringify(message)} ${result}`);
43055
43094
  } catch (e) {
43056
- this.dispatch(event_handler_EventNames.ERROR, e);
43095
+ this.dispatch(event_names_EventNames.ERROR, e);
43057
43096
  throw e;
43058
43097
  }
43059
43098
  }
@@ -43094,7 +43133,7 @@ class EngineClient extends RealtimeEventHandler {
43094
43133
  try {
43095
43134
  await this.engine.startAudioPlaybackDeviceTest('audio-test.wav', 200);
43096
43135
  } catch (e) {
43097
- this.dispatch(event_handler_EventNames.ERROR, e);
43136
+ this.dispatch(event_names_EventNames.ERROR, e);
43098
43137
  throw e;
43099
43138
  }
43100
43139
  }
@@ -43102,7 +43141,7 @@ class EngineClient extends RealtimeEventHandler {
43102
43141
  try {
43103
43142
  this.engine.stopAudioPlaybackDeviceTest();
43104
43143
  } catch (e) {
43105
- this.dispatch(event_handler_EventNames.ERROR, e);
43144
+ this.dispatch(event_names_EventNames.ERROR, e);
43106
43145
  throw e;
43107
43146
  }
43108
43147
  }
@@ -43121,6 +43160,7 @@ class EngineClient extends RealtimeEventHandler {
43121
43160
  this.handleUserLeave = this.handleUserLeave.bind(this);
43122
43161
  this.handleEventError = this.handleEventError.bind(this);
43123
43162
  this.handlePlayerEvent = this.handlePlayerEvent.bind(this);
43163
+ this.handleNetworkQuality = this.handleNetworkQuality.bind(this);
43124
43164
  // Debug only
43125
43165
  this.handleLocalAudioPropertiesReport = this.handleLocalAudioPropertiesReport.bind(this);
43126
43166
  this.handleRemoteAudioPropertiesReport = this.handleRemoteAudioPropertiesReport.bind(this);
@@ -43128,6 +43168,8 @@ class EngineClient extends RealtimeEventHandler {
43128
43168
  this._videoConfig = videoConfig;
43129
43169
  }
43130
43170
  }
43171
+ // Only use for test
43172
+ const TEST_APP_ID = '6705332c79516e015e3e5f0c';
43131
43173
  class RealtimeClient extends RealtimeEventHandler {
43132
43174
  /**
43133
43175
  * en: Establish a connection to the Coze API and join the room
@@ -43135,48 +43177,55 @@ class RealtimeClient extends RealtimeEventHandler {
43135
43177
  * zh: 建立与 Coze API 的连接并加入房间
43136
43178
  */ async connect() {
43137
43179
  var _this__config_videoConfig;
43138
- const { botId, conversationId, voiceId } = this._config;
43180
+ const { botId, conversationId, voiceId, getRoomInfo } = this._config;
43181
+ this.dispatch(event_names_EventNames.CONNECTING, {});
43139
43182
  let roomInfo;
43140
43183
  try {
43141
43184
  // Step1 get token
43142
- let config;
43143
- if (this._config.videoConfig) config = isScreenShareDevice(this._config.videoConfig.videoInputDeviceId) ? {
43144
- video_config: {
43145
- stream_video_type: 'screen'
43146
- }
43147
- } : {
43148
- video_config: {
43149
- stream_video_type: 'main'
43150
- }
43151
- };
43152
- roomInfo = await this._api.audio.rooms.create({
43153
- bot_id: botId,
43154
- conversation_id: conversationId || void 0,
43155
- voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
43156
- connector_id: this._config.connectorId,
43157
- uid: this._config.userId || void 0,
43158
- workflow_id: this._config.workflowId || void 0,
43159
- config
43160
- });
43185
+ if (getRoomInfo) roomInfo = await getRoomInfo();
43186
+ else {
43187
+ let config;
43188
+ if (this._config.videoConfig) config = isScreenShareDevice(this._config.videoConfig.videoInputDeviceId) ? {
43189
+ video_config: {
43190
+ stream_video_type: 'screen'
43191
+ }
43192
+ } : {
43193
+ video_config: {
43194
+ stream_video_type: 'main'
43195
+ }
43196
+ };
43197
+ roomInfo = await this._api.audio.rooms.create({
43198
+ bot_id: botId,
43199
+ conversation_id: conversationId || void 0,
43200
+ voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
43201
+ connector_id: this._config.connectorId,
43202
+ uid: this._config.userId || void 0,
43203
+ workflow_id: this._config.workflowId || void 0,
43204
+ config
43205
+ });
43206
+ }
43161
43207
  } catch (error) {
43162
- this.dispatch(event_handler_EventNames.ERROR, error);
43208
+ this.dispatch(event_names_EventNames.ERROR, error);
43163
43209
  throw new RealtimeAPIError(error_RealtimeError.CREATE_ROOM_ERROR, error instanceof Error ? error.message : 'Unknown error', error);
43164
43210
  }
43211
+ this._isTestEnv = TEST_APP_ID === roomInfo.app_id;
43165
43212
  // Step2 create engine
43166
43213
  this._client = new EngineClient(roomInfo.app_id, this._config.debug, this._isTestEnv, this._isSupportVideo, this._config.videoConfig);
43167
43214
  // Step3 bind engine events
43168
43215
  this._client.bindEngineEvents();
43169
- this._client.on(event_handler_EventNames.ALL, (eventName, data)=>{
43216
+ this._client.on(event_names_EventNames.ALL, (eventName, data)=>{
43170
43217
  this.dispatch(eventName, data, false);
43171
43218
  });
43172
43219
  if (this._config.suppressStationaryNoise) {
43173
43220
  await this._client.enableAudioNoiseReduction();
43174
- this.dispatch(event_handler_EventNames.SUPPRESS_STATIONARY_NOISE, {});
43221
+ this.dispatch(event_names_EventNames.SUPPRESS_STATIONARY_NOISE, {});
43175
43222
  }
43176
- if (this._config.suppressNonStationaryNoise) {
43223
+ if (this._config.suppressNonStationaryNoise) try {
43177
43224
  await this._client.initAIAnsExtension();
43178
43225
  this._client.changeAIAnsExtension(true);
43179
- this.dispatch(event_handler_EventNames.SUPPRESS_NON_STATIONARY_NOISE, {});
43226
+ this.dispatch(event_names_EventNames.SUPPRESS_NON_STATIONARY_NOISE, {});
43227
+ } catch (error) {
43228
+ console.warn('Config suppressNonStationaryNoise is not supported', error);
43180
43229
  }
43181
43230
  var _this__config_audioMutedDefault, _this__config_videoConfig_videoOnDefault, _this__config_isAutoSubscribeAudio;
43182
43231
  // Step4 join room
@@ -43192,7 +43241,7 @@ class RealtimeClient extends RealtimeEventHandler {
43192
43241
  await this._client.createLocalStream(roomInfo.uid, this._config.videoConfig);
43193
43242
  // step6 set connected and dispatch connected event
43194
43243
  this.isConnected = true;
43195
- this.dispatch(event_handler_EventNames.CONNECTED, {
43244
+ this.dispatch(event_names_EventNames.CONNECTED, {
43196
43245
  roomId: roomInfo.room_id,
43197
43246
  uid: roomInfo.uid,
43198
43247
  token: roomInfo.token,
@@ -43206,7 +43255,7 @@ class RealtimeClient extends RealtimeEventHandler {
43206
43255
  */ async interrupt() {
43207
43256
  var _this__client;
43208
43257
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.stop());
43209
- this.dispatch(event_handler_EventNames.INTERRUPTED, {});
43258
+ this.dispatch(event_names_EventNames.INTERRUPTED, {});
43210
43259
  }
43211
43260
  /**
43212
43261
  * en: Disconnect from the current session
@@ -43216,7 +43265,8 @@ class RealtimeClient extends RealtimeEventHandler {
43216
43265
  var _this__client;
43217
43266
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.disconnect());
43218
43267
  this.isConnected = false;
43219
- this.dispatch(event_handler_EventNames.DISCONNECTED, {});
43268
+ this._client = null;
43269
+ this.dispatch(event_names_EventNames.DISCONNECTED, {});
43220
43270
  }
43221
43271
  /**
43222
43272
  * en: Send a message to the bot
@@ -43235,14 +43285,14 @@ class RealtimeClient extends RealtimeEventHandler {
43235
43285
  */ async setAudioEnable(isEnable) {
43236
43286
  var _this__client;
43237
43287
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.changeAudioState(isEnable));
43238
- if (isEnable) this.dispatch(event_handler_EventNames.AUDIO_UNMUTED, {});
43239
- else this.dispatch(event_handler_EventNames.AUDIO_MUTED, {});
43288
+ if (isEnable) this.dispatch(event_names_EventNames.AUDIO_UNMUTED, {});
43289
+ else this.dispatch(event_names_EventNames.AUDIO_MUTED, {});
43240
43290
  }
43241
43291
  async setVideoEnable(isEnable) {
43242
43292
  var _this__client;
43243
43293
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.changeVideoState(isEnable));
43244
- if (isEnable) this.dispatch(event_handler_EventNames.VIDEO_ON, {});
43245
- else this.dispatch(event_handler_EventNames.VIDEO_OFF, {});
43294
+ if (isEnable) this.dispatch(event_names_EventNames.VIDEO_ON, {});
43295
+ else this.dispatch(event_names_EventNames.VIDEO_OFF, {});
43246
43296
  }
43247
43297
  /**
43248
43298
  * en: Enable audio properties reporting (debug mode only)
@@ -43284,7 +43334,7 @@ class RealtimeClient extends RealtimeEventHandler {
43284
43334
  */ async setAudioInputDevice(deviceId) {
43285
43335
  var _this__client;
43286
43336
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioInputDevice(deviceId));
43287
- this.dispatch(event_handler_EventNames.AUDIO_INPUT_DEVICE_CHANGED, {
43337
+ this.dispatch(event_names_EventNames.AUDIO_INPUT_DEVICE_CHANGED, {
43288
43338
  deviceId
43289
43339
  });
43290
43340
  }
@@ -43295,14 +43345,14 @@ class RealtimeClient extends RealtimeEventHandler {
43295
43345
  */ async setAudioOutputDevice(deviceId) {
43296
43346
  var _this__client;
43297
43347
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioOutputDevice(deviceId));
43298
- this.dispatch(event_handler_EventNames.AUDIO_OUTPUT_DEVICE_CHANGED, {
43348
+ this.dispatch(event_names_EventNames.AUDIO_OUTPUT_DEVICE_CHANGED, {
43299
43349
  deviceId
43300
43350
  });
43301
43351
  }
43302
43352
  async setVideoInputDevice(deviceId) {
43303
43353
  var _this__client;
43304
43354
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setVideoInputDevice(deviceId));
43305
- this.dispatch(event_handler_EventNames.VIDEO_INPUT_DEVICE_CHANGED, {
43355
+ this.dispatch(event_names_EventNames.VIDEO_INPUT_DEVICE_CHANGED, {
43306
43356
  deviceId
43307
43357
  });
43308
43358
  }
@@ -43368,7 +43418,6 @@ class RealtimeClient extends RealtimeEventHandler {
43368
43418
  baseURL: defaultBaseURL,
43369
43419
  allowPersonalAccessTokenInBrowser: this._config.allowPersonalAccessTokenInBrowser
43370
43420
  });
43371
- this._isTestEnv = 'https://api.coze.cn' !== defaultBaseURL;
43372
43421
  this._isSupportVideo = !!config.videoConfig;
43373
43422
  }
43374
43423
  }