@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/dist/umd/index.js CHANGED
@@ -98,7 +98,7 @@
98
98
  RealtimeAPIError: ()=>/* reexport */ RealtimeAPIError,
99
99
  RealtimeUtils: ()=>/* reexport */ src_utils_namespaceObject,
100
100
  RealtimeError: ()=>/* reexport */ error_RealtimeError,
101
- EventNames: ()=>/* reexport */ event_handler_EventNames,
101
+ EventNames: ()=>/* reexport */ event_names_EventNames,
102
102
  RealtimeClient: ()=>/* binding */ RealtimeClient
103
103
  });
104
104
  // NAMESPACE OBJECT: ../../common/temp/default/node_modules/.pnpm/axios@1.7.7_debug@4.3.7/node_modules/axios/lib/platform/common/utils.js
@@ -364,6 +364,12 @@
364
364
  ...i,
365
365
  content: 'object' == typeof i.content ? JSON.stringify(i.content) : i.content
366
366
  }));
367
+ const handleParameters = (parameters)=>{
368
+ if (parameters) {
369
+ for (const [key, value] of Object.entries(parameters))if ('object' == typeof value) parameters[key] = JSON.stringify(value);
370
+ }
371
+ return parameters;
372
+ };
367
373
  class Chat extends APIResource {
368
374
  /**
369
375
  * Call the Chat API to send messages to a published Coze agent. | 调用此接口发起一次对话,支持添加上下文
@@ -386,6 +392,10 @@
386
392
  const payload = {
387
393
  ...rest,
388
394
  additional_messages: handleAdditionalMessages(params.additional_messages),
395
+ shortcut_command: params.shortcut_command ? {
396
+ ...params.shortcut_command,
397
+ parameters: handleParameters(params.shortcut_command.parameters)
398
+ } : void 0,
389
399
  stream: false
390
400
  };
391
401
  const result = await this._client.post(apiUrl, payload, false, options);
@@ -412,6 +422,10 @@
412
422
  const payload = {
413
423
  ...rest,
414
424
  additional_messages: handleAdditionalMessages(params.additional_messages),
425
+ shortcut_command: params.shortcut_command ? {
426
+ ...params.shortcut_command,
427
+ parameters: handleParameters(params.shortcut_command.parameters)
428
+ } : void 0,
415
429
  stream: false
416
430
  };
417
431
  const result = await this._client.post(apiUrl, payload, false, options);
@@ -450,6 +464,10 @@
450
464
  const payload = {
451
465
  ...rest,
452
466
  additional_messages: handleAdditionalMessages(params.additional_messages),
467
+ shortcut_command: params.shortcut_command ? {
468
+ ...params.shortcut_command,
469
+ parameters: handleParameters(params.shortcut_command.parameters)
470
+ } : void 0,
453
471
  stream: true
454
472
  };
455
473
  const result = await this._client.post(apiUrl, payload, true, options);
@@ -4613,6 +4631,8 @@ and limitations under the License.
4613
4631
  });
4614
4632
  this.rws.addEventListener('error', (event)=>{
4615
4633
  var _event_target__req_res, _event_target__req, _event_target, _event_target__req_res1, _event_target__req1, _event_target1, _this_onerror, _this;
4634
+ const { readyState } = this.rws;
4635
+ if (3 === readyState) return;
4616
4636
  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;
4617
4637
  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) || [];
4618
4638
  const logidIndex = rawHeaders.findIndex((header)=>'X-Tt-Logid' === header);
@@ -4636,7 +4656,8 @@ and limitations under the License.
4636
4656
  error.data.msg = 'Forbidden';
4637
4657
  } else {
4638
4658
  error.data.code = 500;
4639
- error.data.msg = String(null == event ? void 0 : event.error) || 'WebSocket error';
4659
+ var _event_error;
4660
+ error.data.msg = String(null !== (_event_error = null == event ? void 0 : event.error) && void 0 !== _event_error ? _event_error : '') || 'WebSocket error';
4640
4661
  }
4641
4662
  null === (_this_onerror = (_this = this).onerror) || void 0 === _this_onerror || _this_onerror.call(_this, error, event);
4642
4663
  });
@@ -4645,7 +4666,7 @@ and limitations under the License.
4645
4666
  // EXTERNAL MODULE: os (ignored)
4646
4667
  var os_ignored_ = __webpack_require__("?9050");
4647
4668
  var os_ignored_default = /*#__PURE__*/ __webpack_require__.n(os_ignored_);
4648
- 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
4669
+ 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
4649
4670
  const { version: version_version } = package_namespaceObject;
4650
4671
  const getEnv = ()=>{
4651
4672
  const nodeVersion = process.version.slice(1); // Remove 'v' prefix
@@ -39449,34 +39470,7 @@ and limitations under the License.
39449
39470
  var _navigator_mediaDevices, _navigator;
39450
39471
  return !!(null === (_navigator = navigator) || void 0 === _navigator ? void 0 : null === (_navigator_mediaDevices = _navigator.mediaDevices) || void 0 === _navigator_mediaDevices ? void 0 : _navigator_mediaDevices.getDisplayMedia);
39451
39472
  }
39452
- var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
39453
- RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
39454
- RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
39455
- RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
39456
- RealtimeError["DISCONNECTION_ERROR"] = "DISCONNECTION_ERROR";
39457
- RealtimeError["INTERRUPT_ERROR"] = "INTERRUPT_ERROR";
39458
- RealtimeError["EVENT_HANDLER_ERROR"] = "EVENT_HANDLER_ERROR";
39459
- RealtimeError["PERMISSION_DENIED"] = "PERMISSION_DENIED";
39460
- RealtimeError["NETWORK_ERROR"] = "NETWORK_ERROR";
39461
- RealtimeError["INVALID_STATE"] = "INVALID_STATE";
39462
- RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
39463
- RealtimeError["PARSE_MESSAGE_ERROR"] = "PARSE_MESSAGE_ERROR";
39464
- RealtimeError["HANDLER_MESSAGE_ERROR"] = "HANDLER_MESSAGE_ERROR";
39465
- return RealtimeError;
39466
- }({});
39467
- class RealtimeAPIError extends Error {
39468
- /**
39469
- * @param code - Error code
39470
- * @param message - Error message
39471
- * @param error - Error object
39472
- */ constructor(code, message, error){
39473
- super(`[${code}] ${message}`);
39474
- this.name = 'RealtimeAPIError';
39475
- this.code = code;
39476
- this.error = error;
39477
- }
39478
- }
39479
- var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
39473
+ var event_names_EventNames = /*#__PURE__*/ function(EventNames) {
39480
39474
  /**
39481
39475
  * en: All events
39482
39476
  * zh: 所有事件
@@ -39494,6 +39488,10 @@ and limitations under the License.
39494
39488
  * zh: 客户端连接
39495
39489
  */ EventNames["CONNECTED"] = "client.connected";
39496
39490
  /**
39491
+ * en: Client connecting
39492
+ * zh: 客户端连接中
39493
+ */ EventNames["CONNECTING"] = "client.connecting";
39494
+ /**
39497
39495
  * en: Client interrupted
39498
39496
  * zh: 客户端中断
39499
39497
  */ EventNames["INTERRUPTED"] = "client.interrupted";
@@ -39546,6 +39544,10 @@ and limitations under the License.
39546
39544
  * zh: 视频输入设备改变
39547
39545
  */ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
39548
39546
  /**
39547
+ * en: Network quality changed
39548
+ * zh: 网络质量改变
39549
+ */ EventNames["NETWORK_QUALITY"] = "client.network.quality";
39550
+ /**
39549
39551
  * en: Bot joined
39550
39552
  * zh: Bot 加入
39551
39553
  */ EventNames["BOT_JOIN"] = "server.bot.join";
@@ -39560,7 +39562,7 @@ and limitations under the License.
39560
39562
  /**
39561
39563
  * en: Audio speech stopped
39562
39564
  * zh: 停止说话
39563
- */ EventNames["AUDIO_SPEECH_STOPPED"] = "server.audio.speech_stopped";
39565
+ */ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
39564
39566
  /**
39565
39567
  * en: Server error
39566
39568
  * zh: 服务端错误
@@ -39583,6 +39585,33 @@ and limitations under the License.
39583
39585
  */ EventNames["SESSION_UPDATE"] = "server.session.update";
39584
39586
  return EventNames;
39585
39587
  }({});
39588
+ var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
39589
+ RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
39590
+ RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
39591
+ RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
39592
+ RealtimeError["DISCONNECTION_ERROR"] = "DISCONNECTION_ERROR";
39593
+ RealtimeError["INTERRUPT_ERROR"] = "INTERRUPT_ERROR";
39594
+ RealtimeError["EVENT_HANDLER_ERROR"] = "EVENT_HANDLER_ERROR";
39595
+ RealtimeError["PERMISSION_DENIED"] = "PERMISSION_DENIED";
39596
+ RealtimeError["NETWORK_ERROR"] = "NETWORK_ERROR";
39597
+ RealtimeError["INVALID_STATE"] = "INVALID_STATE";
39598
+ RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
39599
+ RealtimeError["PARSE_MESSAGE_ERROR"] = "PARSE_MESSAGE_ERROR";
39600
+ RealtimeError["HANDLER_MESSAGE_ERROR"] = "HANDLER_MESSAGE_ERROR";
39601
+ return RealtimeError;
39602
+ }({});
39603
+ class RealtimeAPIError extends Error {
39604
+ /**
39605
+ * @param code - Error code
39606
+ * @param message - Error message
39607
+ * @param error - Error object
39608
+ */ constructor(code, message, error){
39609
+ super(`[${code}] ${message}`);
39610
+ this.name = 'RealtimeAPIError';
39611
+ this.code = code;
39612
+ this.error = error;
39613
+ }
39614
+ }
39586
39615
  class RealtimeEventHandler {
39587
39616
  clearEventHandlers() {
39588
39617
  this.eventHandlers = {};
@@ -39598,7 +39627,10 @@ and limitations under the License.
39598
39627
  const handlers = this.eventHandlers[eventName] || [];
39599
39628
  if (callback) {
39600
39629
  const index = handlers.indexOf(callback);
39601
- 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`);
39630
+ if (-1 === index) {
39631
+ console.warn(`Could not turn off specified event listener for "${eventName}": not found as a listener`);
39632
+ return;
39633
+ }
39602
39634
  handlers.splice(index, 1);
39603
39635
  } else delete this.eventHandlers[eventName];
39604
39636
  }
@@ -39612,18 +39644,18 @@ and limitations under the License.
39612
39644
  }
39613
39645
  dispatch(eventName, event) {
39614
39646
  let consoleLog = !(arguments.length > 2) || void 0 === arguments[2] || arguments[2];
39615
- if (consoleLog) this._log(`dispatch ${eventName} event`);
39647
+ if (consoleLog) this._log(`dispatch ${eventName} event`, event);
39616
39648
  const handlers = (this.eventHandlers[eventName] || []).slice();
39617
39649
  this._dispatchToHandlers(eventName, event, handlers);
39618
- const allHandlers = (this.eventHandlers["realtime.event"] || []).slice();
39650
+ const allHandlers = (this.eventHandlers[event_names_EventNames.ALL] || []).slice();
39619
39651
  this._dispatchToHandlers(eventName, event, allHandlers);
39620
- const allClientHandlers = (this.eventHandlers["client.*"] || []).slice();
39652
+ const allClientHandlers = (this.eventHandlers[event_names_EventNames.ALL_CLIENT] || []).slice();
39621
39653
  this._dispatchToHandlers(eventName, event, allClientHandlers, 'client.');
39622
- const allServerHandlers = (this.eventHandlers["server.*"] || []).slice();
39654
+ const allServerHandlers = (this.eventHandlers[event_names_EventNames.ALL_SERVER] || []).slice();
39623
39655
  this._dispatchToHandlers(eventName, event, allServerHandlers, 'server.');
39624
39656
  }
39625
- _log(message) {
39626
- if (this._debug) console.log(`[RealtimeClient] ${message}`);
39657
+ _log(message, event) {
39658
+ if (this._debug) console.log(`[RealtimeClient] ${message}`, event);
39627
39659
  }
39628
39660
  constructor(debug = false){
39629
39661
  this.eventHandlers = {};
@@ -42881,6 +42913,7 @@ and limitations under the License.
42881
42913
  this.engine.on(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
42882
42914
  this.engine.on(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
42883
42915
  this.engine.on(index_esm_min_index.events.onError, this.handleEventError);
42916
+ this.engine.on(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
42884
42917
  if (this._isSupportVideo) this.engine.on(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
42885
42918
  if (this._debug) {
42886
42919
  this.engine.on(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
@@ -42892,6 +42925,7 @@ and limitations under the License.
42892
42925
  this.engine.off(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
42893
42926
  this.engine.off(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
42894
42927
  this.engine.off(index_esm_min_index.events.onError, this.handleEventError);
42928
+ this.engine.off(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
42895
42929
  if (this._isSupportVideo) this.engine.off(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
42896
42930
  if (this._debug) {
42897
42931
  this.engine.off(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
@@ -42912,29 +42946,35 @@ and limitations under the License.
42912
42946
  this.dispatch(`server.${message.event_type}`, message);
42913
42947
  } catch (e) {
42914
42948
  if (e instanceof RealtimeAPIError) {
42915
- if (e.code === error_RealtimeError.PARSE_MESSAGE_ERROR) this.dispatch(event_handler_EventNames.ERROR, {
42949
+ if (e.code === error_RealtimeError.PARSE_MESSAGE_ERROR) this.dispatch(event_names_EventNames.ERROR, {
42916
42950
  message: `Failed to parse message: ${event.message}`,
42917
42951
  error: e
42918
42952
  });
42919
- else if (e.code === error_RealtimeError.HANDLER_MESSAGE_ERROR) this.dispatch(event_handler_EventNames.ERROR, {
42953
+ else if (e.code === error_RealtimeError.HANDLER_MESSAGE_ERROR) this.dispatch(event_names_EventNames.ERROR, {
42920
42954
  message: `Failed to handle message: ${event.message}`,
42921
42955
  error: e
42922
42956
  });
42923
- } else this.dispatch(event_handler_EventNames.ERROR, e);
42957
+ } else this.dispatch(event_names_EventNames.ERROR, e);
42924
42958
  }
42925
42959
  }
42926
42960
  handleEventError(e) {
42927
- this.dispatch(event_handler_EventNames.ERROR, e);
42961
+ this.dispatch(event_names_EventNames.ERROR, e);
42928
42962
  }
42929
42963
  handleUserJoin(event) {
42930
42964
  this.joinUserId = event.userInfo.userId;
42931
- this.dispatch(event_handler_EventNames.BOT_JOIN, event);
42965
+ this.dispatch(event_names_EventNames.BOT_JOIN, event);
42932
42966
  }
42933
42967
  handleUserLeave(event) {
42934
- this.dispatch(event_handler_EventNames.BOT_LEAVE, event);
42968
+ this.dispatch(event_names_EventNames.BOT_LEAVE, event);
42935
42969
  }
42936
42970
  handlePlayerEvent(event) {
42937
- this.dispatch(event_handler_EventNames.PLAYER_EVENT, event);
42971
+ this.dispatch(event_names_EventNames.PLAYER_EVENT, event);
42972
+ }
42973
+ handleNetworkQuality(uplinkNetworkQuality, downlinkNetworkQuality) {
42974
+ this.dispatch(event_names_EventNames.NETWORK_QUALITY, {
42975
+ uplinkNetworkQuality,
42976
+ downlinkNetworkQuality
42977
+ });
42938
42978
  }
42939
42979
  async joinRoom(options) {
42940
42980
  const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
@@ -43002,13 +43042,12 @@ and limitations under the License.
43002
43042
  }
43003
43043
  async disconnect() {
43004
43044
  try {
43005
- if (this._isSupportVideo) await this.changeVideoState(false);
43006
- await this.changeAudioState(false);
43007
- await this.engine.unpublishStream(MediaType$1.AUDIO);
43008
43045
  await this.engine.leaveRoom();
43009
43046
  this.removeEventListener();
43047
+ this.clearEventHandlers();
43048
+ index_esm_min_index.destroyEngine(this.engine);
43010
43049
  } catch (e) {
43011
- this.dispatch(event_handler_EventNames.ERROR, e);
43050
+ this.dispatch(event_names_EventNames.ERROR, e);
43012
43051
  throw e;
43013
43052
  }
43014
43053
  }
@@ -43017,7 +43056,7 @@ and limitations under the License.
43017
43056
  if (isMicOn) await this.engine.publishStream(MediaType$1.AUDIO);
43018
43057
  else await this.engine.unpublishStream(MediaType$1.AUDIO);
43019
43058
  } catch (e) {
43020
- this.dispatch(event_handler_EventNames.ERROR, e);
43059
+ this.dispatch(event_names_EventNames.ERROR, e);
43021
43060
  throw e;
43022
43061
  }
43023
43062
  }
@@ -43037,7 +43076,7 @@ and limitations under the License.
43037
43076
  await this.engine.unpublishScreen(MediaType$1.VIDEO);
43038
43077
  }
43039
43078
  } catch (e) {
43040
- this.dispatch(event_handler_EventNames.ERROR, e);
43079
+ this.dispatch(event_names_EventNames.ERROR, e);
43041
43080
  throw e;
43042
43081
  }
43043
43082
  }
@@ -43050,7 +43089,7 @@ and limitations under the License.
43050
43089
  }));
43051
43090
  this._log(`interrupt ${this.joinUserId} ${result}`);
43052
43091
  } catch (e) {
43053
- this.dispatch(event_handler_EventNames.ERROR, e);
43092
+ this.dispatch(event_names_EventNames.ERROR, e);
43054
43093
  throw e;
43055
43094
  }
43056
43095
  }
@@ -43059,7 +43098,7 @@ and limitations under the License.
43059
43098
  const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify(message));
43060
43099
  this._log(`sendMessage ${this.joinUserId} ${JSON.stringify(message)} ${result}`);
43061
43100
  } catch (e) {
43062
- this.dispatch(event_handler_EventNames.ERROR, e);
43101
+ this.dispatch(event_names_EventNames.ERROR, e);
43063
43102
  throw e;
43064
43103
  }
43065
43104
  }
@@ -43100,7 +43139,7 @@ and limitations under the License.
43100
43139
  try {
43101
43140
  await this.engine.startAudioPlaybackDeviceTest('audio-test.wav', 200);
43102
43141
  } catch (e) {
43103
- this.dispatch(event_handler_EventNames.ERROR, e);
43142
+ this.dispatch(event_names_EventNames.ERROR, e);
43104
43143
  throw e;
43105
43144
  }
43106
43145
  }
@@ -43108,7 +43147,7 @@ and limitations under the License.
43108
43147
  try {
43109
43148
  this.engine.stopAudioPlaybackDeviceTest();
43110
43149
  } catch (e) {
43111
- this.dispatch(event_handler_EventNames.ERROR, e);
43150
+ this.dispatch(event_names_EventNames.ERROR, e);
43112
43151
  throw e;
43113
43152
  }
43114
43153
  }
@@ -43127,6 +43166,7 @@ and limitations under the License.
43127
43166
  this.handleUserLeave = this.handleUserLeave.bind(this);
43128
43167
  this.handleEventError = this.handleEventError.bind(this);
43129
43168
  this.handlePlayerEvent = this.handlePlayerEvent.bind(this);
43169
+ this.handleNetworkQuality = this.handleNetworkQuality.bind(this);
43130
43170
  // Debug only
43131
43171
  this.handleLocalAudioPropertiesReport = this.handleLocalAudioPropertiesReport.bind(this);
43132
43172
  this.handleRemoteAudioPropertiesReport = this.handleRemoteAudioPropertiesReport.bind(this);
@@ -43134,6 +43174,8 @@ and limitations under the License.
43134
43174
  this._videoConfig = videoConfig;
43135
43175
  }
43136
43176
  }
43177
+ // Only use for test
43178
+ const TEST_APP_ID = '6705332c79516e015e3e5f0c';
43137
43179
  class RealtimeClient extends RealtimeEventHandler {
43138
43180
  /**
43139
43181
  * en: Establish a connection to the Coze API and join the room
@@ -43141,48 +43183,55 @@ and limitations under the License.
43141
43183
  * zh: 建立与 Coze API 的连接并加入房间
43142
43184
  */ async connect() {
43143
43185
  var _this__config_videoConfig;
43144
- const { botId, conversationId, voiceId } = this._config;
43186
+ const { botId, conversationId, voiceId, getRoomInfo } = this._config;
43187
+ this.dispatch(event_names_EventNames.CONNECTING, {});
43145
43188
  let roomInfo;
43146
43189
  try {
43147
43190
  // Step1 get token
43148
- let config;
43149
- if (this._config.videoConfig) config = isScreenShareDevice(this._config.videoConfig.videoInputDeviceId) ? {
43150
- video_config: {
43151
- stream_video_type: 'screen'
43152
- }
43153
- } : {
43154
- video_config: {
43155
- stream_video_type: 'main'
43156
- }
43157
- };
43158
- roomInfo = await this._api.audio.rooms.create({
43159
- bot_id: botId,
43160
- conversation_id: conversationId || void 0,
43161
- voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
43162
- connector_id: this._config.connectorId,
43163
- uid: this._config.userId || void 0,
43164
- workflow_id: this._config.workflowId || void 0,
43165
- config
43166
- });
43191
+ if (getRoomInfo) roomInfo = await getRoomInfo();
43192
+ else {
43193
+ let config;
43194
+ if (this._config.videoConfig) config = isScreenShareDevice(this._config.videoConfig.videoInputDeviceId) ? {
43195
+ video_config: {
43196
+ stream_video_type: 'screen'
43197
+ }
43198
+ } : {
43199
+ video_config: {
43200
+ stream_video_type: 'main'
43201
+ }
43202
+ };
43203
+ roomInfo = await this._api.audio.rooms.create({
43204
+ bot_id: botId,
43205
+ conversation_id: conversationId || void 0,
43206
+ voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
43207
+ connector_id: this._config.connectorId,
43208
+ uid: this._config.userId || void 0,
43209
+ workflow_id: this._config.workflowId || void 0,
43210
+ config
43211
+ });
43212
+ }
43167
43213
  } catch (error) {
43168
- this.dispatch(event_handler_EventNames.ERROR, error);
43214
+ this.dispatch(event_names_EventNames.ERROR, error);
43169
43215
  throw new RealtimeAPIError(error_RealtimeError.CREATE_ROOM_ERROR, error instanceof Error ? error.message : 'Unknown error', error);
43170
43216
  }
43217
+ this._isTestEnv = TEST_APP_ID === roomInfo.app_id;
43171
43218
  // Step2 create engine
43172
43219
  this._client = new EngineClient(roomInfo.app_id, this._config.debug, this._isTestEnv, this._isSupportVideo, this._config.videoConfig);
43173
43220
  // Step3 bind engine events
43174
43221
  this._client.bindEngineEvents();
43175
- this._client.on(event_handler_EventNames.ALL, (eventName, data)=>{
43222
+ this._client.on(event_names_EventNames.ALL, (eventName, data)=>{
43176
43223
  this.dispatch(eventName, data, false);
43177
43224
  });
43178
43225
  if (this._config.suppressStationaryNoise) {
43179
43226
  await this._client.enableAudioNoiseReduction();
43180
- this.dispatch(event_handler_EventNames.SUPPRESS_STATIONARY_NOISE, {});
43227
+ this.dispatch(event_names_EventNames.SUPPRESS_STATIONARY_NOISE, {});
43181
43228
  }
43182
- if (this._config.suppressNonStationaryNoise) {
43229
+ if (this._config.suppressNonStationaryNoise) try {
43183
43230
  await this._client.initAIAnsExtension();
43184
43231
  this._client.changeAIAnsExtension(true);
43185
- this.dispatch(event_handler_EventNames.SUPPRESS_NON_STATIONARY_NOISE, {});
43232
+ this.dispatch(event_names_EventNames.SUPPRESS_NON_STATIONARY_NOISE, {});
43233
+ } catch (error) {
43234
+ console.warn('Config suppressNonStationaryNoise is not supported', error);
43186
43235
  }
43187
43236
  var _this__config_audioMutedDefault, _this__config_videoConfig_videoOnDefault, _this__config_isAutoSubscribeAudio;
43188
43237
  // Step4 join room
@@ -43198,7 +43247,7 @@ and limitations under the License.
43198
43247
  await this._client.createLocalStream(roomInfo.uid, this._config.videoConfig);
43199
43248
  // step6 set connected and dispatch connected event
43200
43249
  this.isConnected = true;
43201
- this.dispatch(event_handler_EventNames.CONNECTED, {
43250
+ this.dispatch(event_names_EventNames.CONNECTED, {
43202
43251
  roomId: roomInfo.room_id,
43203
43252
  uid: roomInfo.uid,
43204
43253
  token: roomInfo.token,
@@ -43212,7 +43261,7 @@ and limitations under the License.
43212
43261
  */ async interrupt() {
43213
43262
  var _this__client;
43214
43263
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.stop());
43215
- this.dispatch(event_handler_EventNames.INTERRUPTED, {});
43264
+ this.dispatch(event_names_EventNames.INTERRUPTED, {});
43216
43265
  }
43217
43266
  /**
43218
43267
  * en: Disconnect from the current session
@@ -43222,7 +43271,8 @@ and limitations under the License.
43222
43271
  var _this__client;
43223
43272
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.disconnect());
43224
43273
  this.isConnected = false;
43225
- this.dispatch(event_handler_EventNames.DISCONNECTED, {});
43274
+ this._client = null;
43275
+ this.dispatch(event_names_EventNames.DISCONNECTED, {});
43226
43276
  }
43227
43277
  /**
43228
43278
  * en: Send a message to the bot
@@ -43241,14 +43291,14 @@ and limitations under the License.
43241
43291
  */ async setAudioEnable(isEnable) {
43242
43292
  var _this__client;
43243
43293
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.changeAudioState(isEnable));
43244
- if (isEnable) this.dispatch(event_handler_EventNames.AUDIO_UNMUTED, {});
43245
- else this.dispatch(event_handler_EventNames.AUDIO_MUTED, {});
43294
+ if (isEnable) this.dispatch(event_names_EventNames.AUDIO_UNMUTED, {});
43295
+ else this.dispatch(event_names_EventNames.AUDIO_MUTED, {});
43246
43296
  }
43247
43297
  async setVideoEnable(isEnable) {
43248
43298
  var _this__client;
43249
43299
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.changeVideoState(isEnable));
43250
- if (isEnable) this.dispatch(event_handler_EventNames.VIDEO_ON, {});
43251
- else this.dispatch(event_handler_EventNames.VIDEO_OFF, {});
43300
+ if (isEnable) this.dispatch(event_names_EventNames.VIDEO_ON, {});
43301
+ else this.dispatch(event_names_EventNames.VIDEO_OFF, {});
43252
43302
  }
43253
43303
  /**
43254
43304
  * en: Enable audio properties reporting (debug mode only)
@@ -43290,7 +43340,7 @@ and limitations under the License.
43290
43340
  */ async setAudioInputDevice(deviceId) {
43291
43341
  var _this__client;
43292
43342
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioInputDevice(deviceId));
43293
- this.dispatch(event_handler_EventNames.AUDIO_INPUT_DEVICE_CHANGED, {
43343
+ this.dispatch(event_names_EventNames.AUDIO_INPUT_DEVICE_CHANGED, {
43294
43344
  deviceId
43295
43345
  });
43296
43346
  }
@@ -43301,14 +43351,14 @@ and limitations under the License.
43301
43351
  */ async setAudioOutputDevice(deviceId) {
43302
43352
  var _this__client;
43303
43353
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioOutputDevice(deviceId));
43304
- this.dispatch(event_handler_EventNames.AUDIO_OUTPUT_DEVICE_CHANGED, {
43354
+ this.dispatch(event_names_EventNames.AUDIO_OUTPUT_DEVICE_CHANGED, {
43305
43355
  deviceId
43306
43356
  });
43307
43357
  }
43308
43358
  async setVideoInputDevice(deviceId) {
43309
43359
  var _this__client;
43310
43360
  await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setVideoInputDevice(deviceId));
43311
- this.dispatch(event_handler_EventNames.VIDEO_INPUT_DEVICE_CHANGED, {
43361
+ this.dispatch(event_names_EventNames.VIDEO_INPUT_DEVICE_CHANGED, {
43312
43362
  deviceId
43313
43363
  });
43314
43364
  }
@@ -43374,7 +43424,6 @@ and limitations under the License.
43374
43424
  baseURL: defaultBaseURL,
43375
43425
  allowPersonalAccessTokenInBrowser: this._config.allowPersonalAccessTokenInBrowser
43376
43426
  });
43377
- this._isTestEnv = 'https://api.coze.cn' !== defaultBaseURL;
43378
43427
  this._isSupportVideo = !!config.videoConfig;
43379
43428
  }
43380
43429
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coze/realtime-api",
3
- "version": "1.1.0",
3
+ "version": "1.1.1-beta.2",
4
4
  "description": "A powerful real-time communication SDK for voice interactions with Coze AI bots | 扣子官方实时通信 SDK,用于与 Coze AI bots 进行语音交互",
5
5
  "keywords": [
6
6
  "coze",
@@ -49,7 +49,7 @@
49
49
  "test:cov": "vitest --coverage --run"
50
50
  },
51
51
  "dependencies": {
52
- "@coze/api": "1.1.0",
52
+ "@coze/api": "1.1.1-beta.1",
53
53
  "@volcengine/rtc": "^4.62.1"
54
54
  },
55
55
  "devDependencies": {