@coze/realtime-api 1.3.1 → 1.3.2-alpha.30153e

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/cjs/index.js CHANGED
@@ -1,40 +1,27 @@
1
1
  "use strict";
2
- // The require scope
3
2
  var __webpack_require__ = {};
4
- /************************************************************************/ // webpack/runtime/compat_get_default_export
5
3
  (()=>{
6
- // getDefaultExport function for compatibility with non-ESM modules
7
- __webpack_require__.n = function(module) {
8
- var getter = module && module.__esModule ? function() {
9
- return module['default'];
10
- } : function() {
11
- return module;
12
- };
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
13
6
  __webpack_require__.d(getter, {
14
7
  a: getter
15
8
  });
16
9
  return getter;
17
10
  };
18
11
  })();
19
- // webpack/runtime/define_property_getters
20
12
  (()=>{
21
- __webpack_require__.d = function(exports1, definition) {
13
+ __webpack_require__.d = (exports1, definition)=>{
22
14
  for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
23
15
  enumerable: true,
24
16
  get: definition[key]
25
17
  });
26
18
  };
27
19
  })();
28
- // webpack/runtime/has_own_property
29
20
  (()=>{
30
- __webpack_require__.o = function(obj, prop) {
31
- return Object.prototype.hasOwnProperty.call(obj, prop);
32
- };
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
33
22
  })();
34
- // webpack/runtime/make_namespace_object
35
23
  (()=>{
36
- // define __esModule on exports
37
- __webpack_require__.r = function(exports1) {
24
+ __webpack_require__.r = (exports1)=>{
38
25
  if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
39
26
  value: 'Module'
40
27
  });
@@ -43,18 +30,15 @@ var __webpack_require__ = {};
43
30
  });
44
31
  };
45
32
  })();
46
- /************************************************************************/ var __webpack_exports__ = {};
47
- // ESM COMPAT FLAG
33
+ var __webpack_exports__ = {};
48
34
  __webpack_require__.r(__webpack_exports__);
49
- // EXPORTS
50
35
  __webpack_require__.d(__webpack_exports__, {
51
- RealtimeAPIError: ()=>/* reexport */ RealtimeAPIError,
52
- RealtimeUtils: ()=>/* reexport */ utils_namespaceObject,
53
- RealtimeError: ()=>/* reexport */ error_RealtimeError,
54
- EventNames: ()=>/* reexport */ event_names,
55
- RealtimeClient: ()=>/* binding */ RealtimeClient
36
+ RealtimeError: ()=>error_RealtimeError,
37
+ RealtimeUtils: ()=>utils_namespaceObject,
38
+ EventNames: ()=>event_names,
39
+ RealtimeAPIError: ()=>RealtimeAPIError,
40
+ RealtimeClient: ()=>RealtimeClient
56
41
  });
57
- // NAMESPACE OBJECT: ./src/utils.ts
58
42
  var utils_namespaceObject = {};
59
43
  __webpack_require__.r(utils_namespaceObject);
60
44
  __webpack_require__.d(utils_namespaceObject, {
@@ -69,257 +53,131 @@ __webpack_require__.d(utils_namespaceObject, {
69
53
  const api_namespaceObject = require("@coze/api");
70
54
  const rtc_namespaceObject = require("@volcengine/rtc");
71
55
  var rtc_default = /*#__PURE__*/ __webpack_require__.n(rtc_namespaceObject);
72
- /**
73
- + * Delays execution for the specified duration
74
- + * @param milliseconds The time to sleep in milliseconds
75
- + * @throws {Error} If milliseconds is negative
76
- + * @returns Promise that resolves after the specified duration
77
- + */ const sleep = (milliseconds)=>{
78
- if (milliseconds < 0) throw new Error('Sleep duration must be non-negative');
79
- return new Promise((resolve)=>setTimeout(resolve, milliseconds));
80
- };
81
- /**
82
- * @deprecated use checkDevicePermission instead
83
- * Check microphone permission,return boolean
84
- */ const checkPermission = async function() {
85
- let { audio = true, video = false } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
56
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
86
57
  try {
87
- const result = await rtc_default().enableDevices({
88
- audio,
89
- video
90
- });
91
- return result.audio;
58
+ var info = gen[key](arg);
59
+ var value = info.value;
92
60
  } catch (error) {
93
- console.error('Failed to check device permissions:', error);
94
- return false;
61
+ reject(error);
62
+ return;
95
63
  }
96
- };
97
- /**
98
- * Checks device permissions for audio and video
99
- * @param checkVideo Whether to check video permissions (default: false)
100
- * @returns Promise that resolves with the device permission status
101
- */ const checkDevicePermission = async function() {
102
- let checkVideo = arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
103
- return await rtc_default().enableDevices({
104
- audio: true,
105
- video: checkVideo
106
- });
107
- };
108
- /**
109
- * Get audio devices
110
- * @returns Promise<AudioDevices> Object containing arrays of audio input and output devices
111
- */ const getAudioDevices = async function() {
112
- let { video = false } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
113
- let devices = [];
114
- if (video) {
115
- devices = await rtc_default().enumerateDevices();
116
- if (isScreenShareSupported()) // @ts-expect-error - add screenShare device to devices
117
- devices.push({
118
- deviceId: 'screenShare',
119
- kind: 'videoinput',
120
- label: 'Screen Share',
121
- groupId: 'screenShare'
64
+ if (info.done) resolve(value);
65
+ else Promise.resolve(value).then(_next, _throw);
66
+ }
67
+ function _async_to_generator(fn) {
68
+ return function() {
69
+ var self = this, args = arguments;
70
+ return new Promise(function(resolve, reject) {
71
+ var gen = fn.apply(self, args);
72
+ function _next(value) {
73
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
74
+ }
75
+ function _throw(err) {
76
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
77
+ }
78
+ _next(void 0);
122
79
  });
123
- } else devices = await [
124
- ...await rtc_default().enumerateAudioCaptureDevices(),
125
- ...await rtc_default().enumerateAudioPlaybackDevices()
126
- ];
127
- if (!(null == devices ? void 0 : devices.length)) return {
128
- audioInputs: [],
129
- audioOutputs: [],
130
- videoInputs: []
131
- };
132
- return {
133
- audioInputs: devices.filter((i)=>i.deviceId && 'audioinput' === i.kind),
134
- audioOutputs: devices.filter((i)=>i.deviceId && 'audiooutput' === i.kind),
135
- videoInputs: devices.filter((i)=>i.deviceId && 'videoinput' === i.kind)
136
80
  };
81
+ }
82
+ const sleep = (milliseconds)=>{
83
+ if (milliseconds < 0) throw new Error('Sleep duration must be non-negative');
84
+ return new Promise((resolve)=>setTimeout(resolve, milliseconds));
137
85
  };
86
+ const checkPermission = ({ audio = true, video = false } = {})=>_async_to_generator(function*() {
87
+ try {
88
+ const result = yield rtc_default().enableDevices({
89
+ audio,
90
+ video
91
+ });
92
+ return result.audio;
93
+ } catch (error) {
94
+ console.error('Failed to check device permissions:', error);
95
+ return false;
96
+ }
97
+ })();
98
+ const checkDevicePermission = (checkVideo = false)=>_async_to_generator(function*() {
99
+ return yield rtc_default().enableDevices({
100
+ audio: true,
101
+ video: checkVideo
102
+ });
103
+ })();
104
+ const getAudioDevices = ({ video = false } = {})=>_async_to_generator(function*() {
105
+ let devices = [];
106
+ if (video) {
107
+ devices = yield rtc_default().enumerateDevices();
108
+ if (isScreenShareSupported()) devices.push({
109
+ deviceId: 'screenShare',
110
+ kind: 'videoinput',
111
+ label: 'Screen Share',
112
+ groupId: 'screenShare'
113
+ });
114
+ } else devices = yield [
115
+ ...yield rtc_default().enumerateAudioCaptureDevices(),
116
+ ...yield rtc_default().enumerateAudioPlaybackDevices()
117
+ ];
118
+ if (!(null == devices ? void 0 : devices.length)) return {
119
+ audioInputs: [],
120
+ audioOutputs: [],
121
+ videoInputs: []
122
+ };
123
+ return {
124
+ audioInputs: devices.filter((i)=>i.deviceId && 'audioinput' === i.kind),
125
+ audioOutputs: devices.filter((i)=>i.deviceId && 'audiooutput' === i.kind),
126
+ videoInputs: devices.filter((i)=>i.deviceId && 'videoinput' === i.kind)
127
+ };
128
+ })();
138
129
  const isScreenShareDevice = (deviceId)=>'screenShare' === deviceId;
139
- /**
140
- * 判断是否前后置摄像头
141
- * @param deviceId
142
- * @returns
143
- */ const isMobileVideoDevice = (deviceId)=>'user' === deviceId || 'environment' === deviceId;
144
- /**
145
- * Check if browser supports screen sharing
146
- * 检查浏览器是否支持屏幕共享
147
- */ function isScreenShareSupported() {
130
+ const isMobileVideoDevice = (deviceId)=>'user' === deviceId || 'environment' === deviceId;
131
+ function isScreenShareSupported() {
148
132
  var _navigator_mediaDevices, _navigator;
149
- return !!(null === (_navigator = navigator) || void 0 === _navigator ? void 0 : null === (_navigator_mediaDevices = _navigator.mediaDevices) || void 0 === _navigator_mediaDevices ? void 0 : _navigator_mediaDevices.getDisplayMedia);
133
+ return !!(null == (_navigator = navigator) ? void 0 : null == (_navigator_mediaDevices = _navigator.mediaDevices) ? void 0 : _navigator_mediaDevices.getDisplayMedia);
150
134
  }
151
135
  var event_names_EventNames = /*#__PURE__*/ function(EventNames) {
152
- /**
153
- * en: All events
154
- * zh: 所有事件
155
- */ EventNames["ALL"] = "realtime.event";
156
- /**
157
- * en: All client events
158
- * zh: 所有客户端事件
159
- */ EventNames["ALL_CLIENT"] = "client.*";
160
- /**
161
- * en: All server events
162
- * zh: 所有服务端事件
163
- */ EventNames["ALL_SERVER"] = "server.*";
164
- /**
165
- * en: Room info
166
- * zh: 房间信息
167
- */ EventNames["ROOM_INFO"] = "client.room.info";
168
- /**
169
- * en: Client connected
170
- * zh: 客户端连接
171
- */ EventNames["CONNECTED"] = "client.connected";
172
- /**
173
- * en: Client connecting
174
- * zh: 客户端连接中
175
- */ EventNames["CONNECTING"] = "client.connecting";
176
- /**
177
- * en: Client interrupted
178
- * zh: 客户端中断
179
- */ EventNames["INTERRUPTED"] = "client.interrupted";
180
- /**
181
- * en: Client disconnected
182
- * zh: 客户端断开
183
- */ EventNames["DISCONNECTED"] = "client.disconnected";
184
- /**
185
- * en: Client audio unmuted
186
- * zh: 客户端音频未静音
187
- */ EventNames["AUDIO_UNMUTED"] = "client.audio.unmuted";
188
- /**
189
- * en: Client audio muted
190
- * zh: 客户端音频静音
191
- */ EventNames["AUDIO_MUTED"] = "client.audio.muted";
192
- /**
193
- * en: Client video on
194
- * zh: 客户端视频开启
195
- */ EventNames["VIDEO_ON"] = "client.video.on";
196
- /**
197
- * en: Client video off
198
- * zh: 客户端视频关闭
199
- */ EventNames["VIDEO_OFF"] = "client.video.off";
200
- /**
201
- * en: Client video error
202
- * zh: 客户端视频(或屏幕共享)错误
203
- */ EventNames["VIDEO_ERROR"] = "client.video.error";
204
- /**
205
- * en: Client video event
206
- * zh: 客户端视频事件
207
- */ EventNames["PLAYER_EVENT"] = "client.video.event";
208
- /**
209
- * en: Client error
210
- * zh: 客户端错误
211
- */ EventNames["ERROR"] = "client.error";
212
- /**
213
- * en: Audio noise reduction enabled
214
- * zh: 抑制平稳噪声
215
- */ EventNames["SUPPRESS_STATIONARY_NOISE"] = "client.suppress.stationary.noise";
216
- /**
217
- * en: Suppress non-stationary noise
218
- * zh: 抑制非平稳噪声
219
- */ EventNames["SUPPRESS_NON_STATIONARY_NOISE"] = "client.suppress.non.stationary.noise";
220
- /**
221
- * en: Audio input device changed
222
- * zh: 音频输入设备改变
223
- */ EventNames["AUDIO_INPUT_DEVICE_CHANGED"] = "client.input.device.changed";
224
- /**
225
- * en: Audio output device changed
226
- * zh: 音频输出设备改变
227
- */ EventNames["AUDIO_OUTPUT_DEVICE_CHANGED"] = "client.output.device.changed";
228
- /**
229
- * en: Video input device changed
230
- * zh: 视频输入设备改变
231
- */ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
232
- /**
233
- * en: Network quality changed
234
- * zh: 网络质量改变
235
- */ EventNames["NETWORK_QUALITY"] = "client.network.quality";
236
- /**
237
- * en: Bot joined
238
- * zh: Bot 加入
239
- */ EventNames["BOT_JOIN"] = "server.bot.join";
240
- /**
241
- * en: Bot left
242
- * zh: Bot 离开
243
- */ EventNames["BOT_LEAVE"] = "server.bot.leave";
244
- /**
245
- * en: Audio speech started
246
- * zh: 开始说话
247
- */ EventNames["AUDIO_AGENT_SPEECH_STARTED"] = "server.audio.agent.speech_started";
248
- /**
249
- * en: Audio speech stopped
250
- * zh: 停止说话
251
- */ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
252
- /**
253
- * en: Server error
254
- * zh: 服务端错误
255
- */ EventNames["SERVER_ERROR"] = "server.error";
256
- /**
257
- * en: User speech started
258
- * zh: 用户开始说话
259
- */ EventNames["AUDIO_USER_SPEECH_STARTED"] = "server.audio.user.speech_started";
260
- /**
261
- * en: User speech stopped
262
- * zh: 用户停止说话
263
- */ EventNames["AUDIO_USER_SPEECH_STOPPED"] = "server.audio.user.speech_stopped";
264
- /**
265
- * en: User successfully enters the room
266
- * zh: 用户成功进入房间后,会收到该事件
267
- */ EventNames["SESSION_CREATED"] = "server.session.created";
268
- /**
269
- * en: Session updated
270
- * zh: 会话更新
271
- */ EventNames["SESSION_UPDATED"] = "server.session.updated";
272
- /**
273
- * en: Conversation created
274
- * zh: 会话创建
275
- */ EventNames["CONVERSATION_CREATED"] = "server.conversation.created";
276
- /**
277
- * en: Conversation chat created
278
- * zh: 会话对话创建
279
- */ EventNames["CONVERSATION_CHAT_CREATED"] = "server.conversation.chat.created";
280
- /**
281
- * en: Conversation chat in progress
282
- * zh: 对话正在处理中
283
- */ EventNames["CONVERSATION_CHAT_IN_PROGRESS"] = "server.conversation.chat.in_progress";
284
- /**
285
- * en: Conversation message delta received
286
- * zh: 文本消息增量返回
287
- */ EventNames["CONVERSATION_MESSAGE_DELTA"] = "server.conversation.message.delta";
288
- /**
289
- * en: Conversation message completed
290
- * zh: 文本消息完成
291
- */ EventNames["CONVERSATION_MESSAGE_COMPLETED"] = "server.conversation.message.completed";
292
- /**
293
- * en: Conversation chat completed
294
- * zh: 对话完成
295
- */ EventNames["CONVERSATION_CHAT_COMPLETED"] = "server.conversation.chat.completed";
296
- /**
297
- * en: Conversation chat requires action
298
- * zh: 对话需要插件
299
- */ EventNames["CONVERSATION_CHAT_REQUIRES_ACTION"] = "server.conversation.chat.requires_action";
300
- /**
301
- * en: Conversation chat failed
302
- * zh: 对话失败
303
- */ EventNames["CONVERSATION_CHAT_FAILED"] = "server.conversation.chat.failed";
304
- /**
305
- * en: Session pre answer updated
306
- * zh: 安抚配置更新成功
307
- */ EventNames["SESSION_PRE_ANSWER_UPDATED"] = "server.session.pre_answer.updated";
308
- /**
309
- * en: Conversation audio transcript delta
310
- * zh: 用户语音识别字幕
311
- */ EventNames["CONVERSATION_AUDIO_TRANSCRIPT_DELTA"] = "server.conversation.audio_transcript.delta";
312
- /**
313
- * en: Mode updated
314
- * zh: 更新房间模式成功
315
- */ EventNames["MODE_UPDATED"] = "server.mode.updated";
316
- /**
317
- * en: Live created
318
- * zh: 直播创建
319
- */ EventNames["LIVE_CREATED"] = "server.live.created";
136
+ EventNames["ALL"] = "realtime.event";
137
+ EventNames["ALL_CLIENT"] = "client.*";
138
+ EventNames["ALL_SERVER"] = "server.*";
139
+ EventNames["ROOM_INFO"] = "client.room.info";
140
+ EventNames["CONNECTED"] = "client.connected";
141
+ EventNames["CONNECTING"] = "client.connecting";
142
+ EventNames["INTERRUPTED"] = "client.interrupted";
143
+ EventNames["DISCONNECTED"] = "client.disconnected";
144
+ EventNames["AUDIO_UNMUTED"] = "client.audio.unmuted";
145
+ EventNames["AUDIO_MUTED"] = "client.audio.muted";
146
+ EventNames["VIDEO_ON"] = "client.video.on";
147
+ EventNames["VIDEO_OFF"] = "client.video.off";
148
+ EventNames["VIDEO_ERROR"] = "client.video.error";
149
+ EventNames["PLAYER_EVENT"] = "client.video.event";
150
+ EventNames["ERROR"] = "client.error";
151
+ EventNames["SUPPRESS_STATIONARY_NOISE"] = "client.suppress.stationary.noise";
152
+ EventNames["SUPPRESS_NON_STATIONARY_NOISE"] = "client.suppress.non.stationary.noise";
153
+ EventNames["AUDIO_INPUT_DEVICE_CHANGED"] = "client.input.device.changed";
154
+ EventNames["AUDIO_OUTPUT_DEVICE_CHANGED"] = "client.output.device.changed";
155
+ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
156
+ EventNames["NETWORK_QUALITY"] = "client.network.quality";
157
+ EventNames["BOT_JOIN"] = "server.bot.join";
158
+ EventNames["BOT_LEAVE"] = "server.bot.leave";
159
+ EventNames["AUDIO_AGENT_SPEECH_STARTED"] = "server.audio.agent.speech_started";
160
+ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
161
+ EventNames["SERVER_ERROR"] = "server.error";
162
+ EventNames["AUDIO_USER_SPEECH_STARTED"] = "server.audio.user.speech_started";
163
+ EventNames["AUDIO_USER_SPEECH_STOPPED"] = "server.audio.user.speech_stopped";
164
+ EventNames["SESSION_CREATED"] = "server.session.created";
165
+ EventNames["SESSION_UPDATED"] = "server.session.updated";
166
+ EventNames["CONVERSATION_CREATED"] = "server.conversation.created";
167
+ EventNames["CONVERSATION_CHAT_CREATED"] = "server.conversation.chat.created";
168
+ EventNames["CONVERSATION_CHAT_IN_PROGRESS"] = "server.conversation.chat.in_progress";
169
+ EventNames["CONVERSATION_MESSAGE_DELTA"] = "server.conversation.message.delta";
170
+ EventNames["CONVERSATION_MESSAGE_COMPLETED"] = "server.conversation.message.completed";
171
+ EventNames["CONVERSATION_CHAT_COMPLETED"] = "server.conversation.chat.completed";
172
+ EventNames["CONVERSATION_CHAT_REQUIRES_ACTION"] = "server.conversation.chat.requires_action";
173
+ EventNames["CONVERSATION_CHAT_FAILED"] = "server.conversation.chat.failed";
174
+ EventNames["SESSION_PRE_ANSWER_UPDATED"] = "server.session.pre_answer.updated";
175
+ EventNames["CONVERSATION_AUDIO_TRANSCRIPT_DELTA"] = "server.conversation.audio_transcript.delta";
176
+ EventNames["MODE_UPDATED"] = "server.mode.updated";
177
+ EventNames["LIVE_CREATED"] = "server.live.created";
320
178
  return EventNames;
321
179
  }(event_names_EventNames || {});
322
- /* ESM default export */ const event_names = event_names_EventNames;
180
+ const event_names = event_names_EventNames;
323
181
  var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
324
182
  RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
325
183
  RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
@@ -336,11 +194,7 @@ var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
336
194
  return RealtimeError;
337
195
  }({});
338
196
  class RealtimeAPIError extends Error {
339
- /**
340
- * @param code - Error code
341
- * @param message - Error message
342
- * @param error - Error object
343
- */ constructor(code, message, error){
197
+ constructor(code, message, error){
344
198
  super(`[${code}] ${message}`);
345
199
  this.name = 'RealtimeAPIError';
346
200
  this.code = code;
@@ -362,14 +216,10 @@ class RealtimeEventHandler {
362
216
  const handlers = this.eventHandlers[eventName] || [];
363
217
  if (callback) {
364
218
  const index = handlers.indexOf(callback);
365
- if (-1 === index) {
366
- console.warn(`Could not turn off specified event listener for "${eventName}": not found as a listener`);
367
- return;
368
- }
219
+ if (-1 === index) return void console.warn(`Could not turn off specified event listener for "${eventName}": not found as a listener`);
369
220
  handlers.splice(index, 1);
370
221
  } else delete this.eventHandlers[eventName];
371
222
  }
372
- // eslint-disable-next-line max-params
373
223
  _dispatchToHandlers(eventName, event, handlers, prefix) {
374
224
  for (const handler of handlers)if (!prefix || eventName.startsWith(prefix)) try {
375
225
  handler(eventName, event);
@@ -377,8 +227,7 @@ class RealtimeEventHandler {
377
227
  throw new RealtimeAPIError(error_RealtimeError.HANDLER_MESSAGE_ERROR, `Failed to handle message: ${eventName}`);
378
228
  }
379
229
  }
380
- dispatch(eventName, event) {
381
- let consoleLog = !(arguments.length > 2) || void 0 === arguments[2] || arguments[2];
230
+ dispatch(eventName, event, consoleLog = true) {
382
231
  if (consoleLog) this._log(`dispatch ${eventName} event`, event);
383
232
  const handlers = (this.eventHandlers[eventName] || []).slice();
384
233
  this._dispatchToHandlers(eventName, event, handlers);
@@ -399,6 +248,32 @@ class RealtimeEventHandler {
399
248
  }
400
249
  const extension_ainr_namespaceObject = require("@volcengine/rtc/extension-ainr");
401
250
  var extension_ainr_default = /*#__PURE__*/ __webpack_require__.n(extension_ainr_namespaceObject);
251
+ function client_asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
252
+ try {
253
+ var info = gen[key](arg);
254
+ var value = info.value;
255
+ } catch (error) {
256
+ reject(error);
257
+ return;
258
+ }
259
+ if (info.done) resolve(value);
260
+ else Promise.resolve(value).then(_next, _throw);
261
+ }
262
+ function client_async_to_generator(fn) {
263
+ return function() {
264
+ var self = this, args = arguments;
265
+ return new Promise(function(resolve, reject) {
266
+ var gen = fn.apply(self, args);
267
+ function _next(value) {
268
+ client_asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
269
+ }
270
+ function _throw(err) {
271
+ client_asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
272
+ }
273
+ _next(void 0);
274
+ });
275
+ };
276
+ }
402
277
  class EngineClient extends RealtimeEventHandler {
403
278
  bindEngineEvents() {
404
279
  this.engine.on(rtc_default().events.onUserMessageReceived, this.handleMessage);
@@ -429,7 +304,6 @@ class EngineClient extends RealtimeEventHandler {
429
304
  _parseMessage(event) {
430
305
  try {
431
306
  return JSON.parse(event.message);
432
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
433
307
  } catch (e) {
434
308
  throw new RealtimeAPIError(error_RealtimeError.PARSE_MESSAGE_ERROR, (null == e ? void 0 : e.message) || 'Unknown error');
435
309
  }
@@ -470,176 +344,198 @@ class EngineClient extends RealtimeEventHandler {
470
344
  downlinkNetworkQuality
471
345
  });
472
346
  }
473
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
474
347
  handleTrackEnded(event) {
475
348
  if ((null == event ? void 0 : event.kind) === 'video') this.dispatch(event_names.VIDEO_OFF, event);
476
349
  }
477
- async joinRoom(options) {
478
- const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
479
- try {
480
- await this.engine.joinRoom(token, roomId, {
481
- userId: uid
482
- }, {
483
- isAutoPublish: !audioMutedDefault,
484
- isAutoSubscribeAudio,
485
- isAutoSubscribeVideo: this._isSupportVideo && videoOnDefault
350
+ joinRoom(options) {
351
+ return client_async_to_generator(function*() {
352
+ const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
353
+ try {
354
+ yield this.engine.joinRoom(token, roomId, {
355
+ userId: uid
356
+ }, {
357
+ isAutoPublish: !audioMutedDefault,
358
+ isAutoSubscribeAudio,
359
+ isAutoSubscribeVideo: this._isSupportVideo && videoOnDefault
360
+ });
361
+ } catch (e) {
362
+ if (e instanceof Error) throw new RealtimeAPIError(error_RealtimeError.CONNECTION_ERROR, e.message);
363
+ }
364
+ }).call(this);
365
+ }
366
+ setAudioInputDevice(deviceId) {
367
+ return client_async_to_generator(function*() {
368
+ const devices = yield getAudioDevices();
369
+ if (-1 === devices.audioInputs.findIndex((i)=>i.deviceId === deviceId)) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, `Audio input device not found: ${deviceId}`);
370
+ this.engine.stopAudioCapture();
371
+ yield this.engine.startAudioCapture(deviceId);
372
+ }).call(this);
373
+ }
374
+ setAudioOutputDevice(deviceId) {
375
+ return client_async_to_generator(function*() {
376
+ const devices = yield getAudioDevices({
377
+ video: false
486
378
  });
487
- } catch (e) {
488
- if (e instanceof Error) throw new RealtimeAPIError(error_RealtimeError.CONNECTION_ERROR, e.message);
489
- }
490
- }
491
- async setAudioInputDevice(deviceId) {
492
- const devices = await getAudioDevices();
493
- if (-1 === devices.audioInputs.findIndex((i)=>i.deviceId === deviceId)) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, `Audio input device not found: ${deviceId}`);
494
- this.engine.stopAudioCapture();
495
- await this.engine.startAudioCapture(deviceId);
496
- }
497
- async setAudioOutputDevice(deviceId) {
498
- const devices = await getAudioDevices({
499
- video: false
500
- });
501
- if (-1 === devices.audioOutputs.findIndex((i)=>i.deviceId === deviceId)) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, `Audio output device not found: ${deviceId}`);
502
- await this.engine.setAudioPlaybackDevice(deviceId);
503
- }
504
- async setVideoInputDevice(deviceId) {
505
- let isAutoCapture = !(arguments.length > 1) || void 0 === arguments[1] || arguments[1];
506
- var _this__videoConfig;
507
- const devices = await getAudioDevices({
508
- video: true
509
- });
510
- if (!isMobileVideoDevice(deviceId) && -1 === devices.videoInputs.findIndex((i)=>i.deviceId === deviceId)) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, `Video input device not found: ${deviceId}`);
511
- this.engine.setLocalVideoPlayer(isScreenShareDevice(deviceId) ? rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN : rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN, {
512
- renderDom: (null === (_this__videoConfig = this._videoConfig) || void 0 === _this__videoConfig ? void 0 : _this__videoConfig.renderDom) || 'local-player',
513
- userId: this._roomUserId
514
- });
515
- await this.changeVideoState(false);
516
- if (isScreenShareDevice(deviceId)) {
517
- if (this._streamIndex === rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN) this.engine.setLocalVideoPlayer(rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN);
518
- if (isAutoCapture) {
519
- var _this__videoConfig1;
520
- this.engine.setVideoSourceType(rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN, rtc_namespaceObject.VideoSourceType.VIDEO_SOURCE_TYPE_INTERNAL);
521
- await this.engine.startScreenCapture(null === (_this__videoConfig1 = this._videoConfig) || void 0 === _this__videoConfig1 ? void 0 : _this__videoConfig1.screenConfig);
522
- await this.engine.publishScreen(rtc_namespaceObject.MediaType.VIDEO);
379
+ if (-1 === devices.audioOutputs.findIndex((i)=>i.deviceId === deviceId)) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, `Audio output device not found: ${deviceId}`);
380
+ yield this.engine.setAudioPlaybackDevice(deviceId);
381
+ }).call(this);
382
+ }
383
+ setVideoInputDevice(deviceId, isAutoCapture = true) {
384
+ return client_async_to_generator(function*() {
385
+ var _this__videoConfig;
386
+ const devices = yield getAudioDevices({
387
+ video: true
388
+ });
389
+ if (!isMobileVideoDevice(deviceId) && -1 === devices.videoInputs.findIndex((i)=>i.deviceId === deviceId)) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, `Video input device not found: ${deviceId}`);
390
+ this.engine.setLocalVideoPlayer(isScreenShareDevice(deviceId) ? rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN : rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN, {
391
+ renderDom: (null == (_this__videoConfig = this._videoConfig) ? void 0 : _this__videoConfig.renderDom) || 'local-player',
392
+ userId: this._roomUserId
393
+ });
394
+ yield this.changeVideoState(false);
395
+ if (isScreenShareDevice(deviceId)) {
396
+ if (this._streamIndex === rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN) this.engine.setLocalVideoPlayer(rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN);
397
+ if (isAutoCapture) {
398
+ var _this__videoConfig1;
399
+ this.engine.setVideoSourceType(rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN, rtc_namespaceObject.VideoSourceType.VIDEO_SOURCE_TYPE_INTERNAL);
400
+ yield this.engine.startScreenCapture(null == (_this__videoConfig1 = this._videoConfig) ? void 0 : _this__videoConfig1.screenConfig);
401
+ yield this.engine.publishScreen(rtc_namespaceObject.MediaType.VIDEO);
402
+ }
403
+ this._streamIndex = rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN;
404
+ } else {
405
+ if (this._streamIndex === rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN) this.engine.setLocalVideoPlayer(rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN);
406
+ if (isAutoCapture) yield this.engine.startVideoCapture(deviceId);
407
+ this._streamIndex = rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN;
523
408
  }
524
- this._streamIndex = rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN;
525
- } else {
526
- if (this._streamIndex === rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN) this.engine.setLocalVideoPlayer(rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN);
527
- if (isAutoCapture) await this.engine.startVideoCapture(deviceId);
528
- this._streamIndex = rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN;
529
- }
530
- }
531
- async createLocalStream(userId, videoConfig) {
532
- this._roomUserId = userId;
533
- const devices = await getAudioDevices({
534
- video: this._isSupportVideo
535
- });
536
- if (!devices.audioInputs.length) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, 'Failed to get audio devices');
537
- if (this._isSupportVideo && !devices.videoInputs.length) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, 'Failed to get video devices');
538
- await this.engine.startAudioCapture(devices.audioInputs[0].deviceId);
539
- if (this._isSupportVideo) try {
540
- await this.setVideoInputDevice((null == videoConfig ? void 0 : videoConfig.videoInputDeviceId) || devices.videoInputs[0].deviceId, null == videoConfig ? void 0 : videoConfig.videoOnDefault);
541
- this.dispatch((null == videoConfig ? void 0 : videoConfig.videoOnDefault) ? event_names.VIDEO_ON : event_names.VIDEO_OFF, {});
542
- } catch (e) {
543
- this.dispatch(event_names.VIDEO_ERROR, e);
544
- }
409
+ }).call(this);
545
410
  }
546
- async disconnect() {
547
- try {
548
- await this.engine.leaveRoom();
549
- this.removeEventListener();
550
- this.clearEventHandlers();
551
- rtc_default().destroyEngine(this.engine);
552
- } catch (e) {
553
- this.dispatch(event_names.ERROR, e);
554
- throw e;
555
- }
556
- }
557
- async changeAudioState(isMicOn) {
558
- try {
559
- if (isMicOn) await this.engine.publishStream(rtc_namespaceObject.MediaType.AUDIO);
560
- else await this.engine.unpublishStream(rtc_namespaceObject.MediaType.AUDIO);
561
- } catch (e) {
562
- this.dispatch(event_names.ERROR, e);
563
- throw e;
564
- }
411
+ createLocalStream(userId, videoConfig) {
412
+ return client_async_to_generator(function*() {
413
+ this._roomUserId = userId;
414
+ const devices = yield getAudioDevices({
415
+ video: this._isSupportVideo
416
+ });
417
+ if (!devices.audioInputs.length) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, 'Failed to get audio devices');
418
+ if (this._isSupportVideo && !devices.videoInputs.length) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, 'Failed to get video devices');
419
+ yield this.engine.startAudioCapture(devices.audioInputs[0].deviceId);
420
+ if (this._isSupportVideo) try {
421
+ yield this.setVideoInputDevice((null == videoConfig ? void 0 : videoConfig.videoInputDeviceId) || devices.videoInputs[0].deviceId, null == videoConfig ? void 0 : videoConfig.videoOnDefault);
422
+ this.dispatch((null == videoConfig ? void 0 : videoConfig.videoOnDefault) ? event_names.VIDEO_ON : event_names.VIDEO_OFF, {});
423
+ } catch (e) {
424
+ this.dispatch(event_names.VIDEO_ERROR, e);
425
+ }
426
+ }).call(this);
427
+ }
428
+ disconnect() {
429
+ return client_async_to_generator(function*() {
430
+ try {
431
+ yield this.engine.leaveRoom();
432
+ this.removeEventListener();
433
+ this.clearEventHandlers();
434
+ rtc_default().destroyEngine(this.engine);
435
+ } catch (e) {
436
+ this.dispatch(event_names.ERROR, e);
437
+ throw e;
438
+ }
439
+ }).call(this);
440
+ }
441
+ changeAudioState(isMicOn) {
442
+ return client_async_to_generator(function*() {
443
+ try {
444
+ if (isMicOn) yield this.engine.publishStream(rtc_namespaceObject.MediaType.AUDIO);
445
+ else yield this.engine.unpublishStream(rtc_namespaceObject.MediaType.AUDIO);
446
+ } catch (e) {
447
+ this.dispatch(event_names.ERROR, e);
448
+ throw e;
449
+ }
450
+ }).call(this);
565
451
  }
566
- async changeVideoState(isVideoOn) {
567
- if (isVideoOn) {
568
- if (this._streamIndex === rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN) await this.engine.startVideoCapture();
452
+ changeVideoState(isVideoOn) {
453
+ return client_async_to_generator(function*() {
454
+ if (isVideoOn) if (this._streamIndex === rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN) yield this.engine.startVideoCapture();
569
455
  else {
570
456
  var _this__videoConfig;
571
457
  this.engine.setVideoSourceType(rtc_namespaceObject.StreamIndex.STREAM_INDEX_SCREEN, rtc_namespaceObject.VideoSourceType.VIDEO_SOURCE_TYPE_INTERNAL);
572
- await this.engine.startScreenCapture(null === (_this__videoConfig = this._videoConfig) || void 0 === _this__videoConfig ? void 0 : _this__videoConfig.screenConfig);
573
- await this.engine.publishScreen(rtc_namespaceObject.MediaType.VIDEO);
458
+ yield this.engine.startScreenCapture(null == (_this__videoConfig = this._videoConfig) ? void 0 : _this__videoConfig.screenConfig);
459
+ yield this.engine.publishScreen(rtc_namespaceObject.MediaType.VIDEO);
574
460
  }
575
- } else if (this._streamIndex === rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN) await this.engine.stopVideoCapture();
576
- else {
577
- await this.engine.stopScreenCapture();
578
- await this.engine.unpublishScreen(rtc_namespaceObject.MediaType.VIDEO);
579
- }
580
- }
581
- async stop() {
582
- try {
583
- const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify({
584
- id: 'event_1',
585
- event_type: 'conversation.chat.cancel',
586
- data: {}
587
- }));
588
- this._log(`interrupt ${this.joinUserId} ${result}`);
589
- } catch (e) {
590
- this.dispatch(event_names.ERROR, e);
591
- throw e;
592
- }
593
- }
594
- async sendMessage(message) {
595
- try {
596
- const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify(message));
597
- this._log(`sendMessage ${this.joinUserId} ${JSON.stringify(message)} ${result}`);
598
- } catch (e) {
599
- this.dispatch(event_names.ERROR, e);
600
- throw e;
601
- }
461
+ else if (this._streamIndex === rtc_namespaceObject.StreamIndex.STREAM_INDEX_MAIN) yield this.engine.stopVideoCapture();
462
+ else {
463
+ yield this.engine.stopScreenCapture();
464
+ yield this.engine.unpublishScreen(rtc_namespaceObject.MediaType.VIDEO);
465
+ }
466
+ }).call(this);
467
+ }
468
+ stop() {
469
+ return client_async_to_generator(function*() {
470
+ try {
471
+ const result = yield this.engine.sendUserMessage(this.joinUserId, JSON.stringify({
472
+ id: 'event_1',
473
+ event_type: 'conversation.chat.cancel',
474
+ data: {}
475
+ }));
476
+ this._log(`interrupt ${this.joinUserId} ${result}`);
477
+ } catch (e) {
478
+ this.dispatch(event_names.ERROR, e);
479
+ throw e;
480
+ }
481
+ }).call(this);
482
+ }
483
+ sendMessage(message) {
484
+ return client_async_to_generator(function*() {
485
+ try {
486
+ const result = yield this.engine.sendUserMessage(this.joinUserId, JSON.stringify(message));
487
+ this._log(`sendMessage ${this.joinUserId} ${JSON.stringify(message)} ${result}`);
488
+ } catch (e) {
489
+ this.dispatch(event_names.ERROR, e);
490
+ throw e;
491
+ }
492
+ }).call(this);
602
493
  }
603
494
  enableAudioPropertiesReport(config) {
604
495
  this.engine.enableAudioPropertiesReport(config);
605
496
  }
606
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
607
497
  handleLocalAudioPropertiesReport(event) {
608
498
  var _event__audioPropertiesInfo, _event_;
609
- if (this._debug && (null === (_event_ = event[0]) || void 0 === _event_ ? void 0 : null === (_event__audioPropertiesInfo = _event_.audioPropertiesInfo) || void 0 === _event__audioPropertiesInfo ? void 0 : _event__audioPropertiesInfo.linearVolume) > 0) console.log('handleLocalAudioPropertiesReport', event);
499
+ if (this._debug && (null == (_event_ = event[0]) ? void 0 : null == (_event__audioPropertiesInfo = _event_.audioPropertiesInfo) ? void 0 : _event__audioPropertiesInfo.linearVolume) > 0) console.log('handleLocalAudioPropertiesReport', event);
610
500
  }
611
501
  handleRemoteAudioPropertiesReport(event) {
612
502
  if (this._debug) console.log('handleRemoteAudioPropertiesReport', event);
613
503
  }
614
- async enableAudioNoiseReduction() {
615
- var _this_engine;
616
- await (null === (_this_engine = this.engine) || void 0 === _this_engine ? void 0 : _this_engine.setAudioCaptureConfig({
617
- noiseSuppression: true,
618
- echoCancellation: true,
619
- autoGainControl: true
620
- }));
504
+ enableAudioNoiseReduction() {
505
+ return client_async_to_generator(function*() {
506
+ var _this_engine;
507
+ yield null == (_this_engine = this.engine) ? void 0 : _this_engine.setAudioCaptureConfig({
508
+ noiseSuppression: true,
509
+ echoCancellation: true,
510
+ autoGainControl: true
511
+ });
512
+ }).call(this);
621
513
  }
622
- async initAIAnsExtension() {
623
- const AIAnsExtension = new (extension_ainr_default())();
624
- await this.engine.registerExtension(AIAnsExtension);
625
- this._AIAnsExtension = AIAnsExtension;
514
+ initAIAnsExtension() {
515
+ return client_async_to_generator(function*() {
516
+ const AIAnsExtension = new (extension_ainr_default())();
517
+ yield this.engine.registerExtension(AIAnsExtension);
518
+ this._AIAnsExtension = AIAnsExtension;
519
+ }).call(this);
626
520
  }
627
521
  changeAIAnsExtension(enable) {
628
522
  if (enable) {
629
523
  var _this__AIAnsExtension;
630
- null === (_this__AIAnsExtension = this._AIAnsExtension) || void 0 === _this__AIAnsExtension || _this__AIAnsExtension.enable();
524
+ null == (_this__AIAnsExtension = this._AIAnsExtension) || _this__AIAnsExtension.enable();
631
525
  } else {
632
526
  var _this__AIAnsExtension1;
633
- null === (_this__AIAnsExtension1 = this._AIAnsExtension) || void 0 === _this__AIAnsExtension1 || _this__AIAnsExtension1.disable();
527
+ null == (_this__AIAnsExtension1 = this._AIAnsExtension) || _this__AIAnsExtension1.disable();
634
528
  }
635
529
  }
636
- async startAudioPlaybackDeviceTest() {
637
- try {
638
- await this.engine.startAudioPlaybackDeviceTest('audio-test.wav', 200);
639
- } catch (e) {
640
- this.dispatch(event_names.ERROR, e);
641
- throw e;
642
- }
530
+ startAudioPlaybackDeviceTest() {
531
+ return client_async_to_generator(function*() {
532
+ try {
533
+ yield this.engine.startAudioPlaybackDeviceTest('audio-test.wav', 200);
534
+ } catch (e) {
535
+ this.dispatch(event_names.ERROR, e);
536
+ throw e;
537
+ }
538
+ }).call(this);
643
539
  }
644
540
  stopAudioPlaybackDeviceTest() {
645
541
  try {
@@ -652,7 +548,6 @@ class EngineClient extends RealtimeEventHandler {
652
548
  getRtcEngine() {
653
549
  return this.engine;
654
550
  }
655
- // eslint-disable-next-line max-params
656
551
  constructor(appId, debug = false, isTestEnv = false, isSupportVideo = false, videoConfig){
657
552
  super(debug), this.joinUserId = '', this._AIAnsExtension = null, this._isSupportVideo = false;
658
553
  if (isTestEnv) rtc_default().setParameter('ICE_CONFIG_REQUEST_URLS', [
@@ -667,283 +562,227 @@ class EngineClient extends RealtimeEventHandler {
667
562
  this.handlePlayerEvent = this.handlePlayerEvent.bind(this);
668
563
  this.handleNetworkQuality = this.handleNetworkQuality.bind(this);
669
564
  this.handleTrackEnded = this.handleTrackEnded.bind(this);
670
- // Debug only
671
565
  this.handleLocalAudioPropertiesReport = this.handleLocalAudioPropertiesReport.bind(this);
672
566
  this.handleRemoteAudioPropertiesReport = this.handleRemoteAudioPropertiesReport.bind(this);
673
567
  this._isSupportVideo = isSupportVideo;
674
568
  this._videoConfig = videoConfig;
675
569
  }
676
570
  }
677
- // Only use for test
571
+ function src_asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
572
+ try {
573
+ var info = gen[key](arg);
574
+ var value = info.value;
575
+ } catch (error) {
576
+ reject(error);
577
+ return;
578
+ }
579
+ if (info.done) resolve(value);
580
+ else Promise.resolve(value).then(_next, _throw);
581
+ }
582
+ function src_async_to_generator(fn) {
583
+ return function() {
584
+ var self = this, args = arguments;
585
+ return new Promise(function(resolve, reject) {
586
+ var gen = fn.apply(self, args);
587
+ function _next(value) {
588
+ src_asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
589
+ }
590
+ function _throw(err) {
591
+ src_asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
592
+ }
593
+ _next(void 0);
594
+ });
595
+ };
596
+ }
678
597
  const TEST_APP_ID = '6705332c79516e015e3e5f0c';
679
598
  class RealtimeClient extends RealtimeEventHandler {
680
- /**
681
- * en: Establish a connection to the Coze API and join the room
682
- *
683
- * zh: 建立与 Coze API 的连接并加入房间
684
- */ async connect() {
685
- var _this__config_videoConfig;
686
- const { botId, conversationId, voiceId, getRoomInfo } = this._config;
687
- this.dispatch(event_names.CONNECTING, {});
688
- let roomInfo;
689
- try {
690
- // Step1 get token
691
- if (getRoomInfo) roomInfo = await getRoomInfo();
692
- else {
693
- const config = {};
694
- if (this._config.prologueContent) config.prologue_content = this._config.prologueContent;
695
- if (void 0 !== this._config.roomMode && null !== this._config.roomMode) config.room_mode = this._config.roomMode || api_namespaceObject.RoomMode.Default;
696
- if (this._config.videoConfig) {
697
- if (isScreenShareDevice(this._config.videoConfig.videoInputDeviceId)) config.video_config = {
599
+ connect() {
600
+ return src_async_to_generator(function*() {
601
+ var _this__config_videoConfig;
602
+ const { botId, conversationId, voiceId, getRoomInfo } = this._config;
603
+ this.dispatch(event_names.CONNECTING, {});
604
+ let roomInfo;
605
+ try {
606
+ if (getRoomInfo) roomInfo = yield getRoomInfo();
607
+ else {
608
+ const config = {};
609
+ if (this._config.prologueContent) config.prologue_content = this._config.prologueContent;
610
+ if (void 0 !== this._config.roomMode && null !== this._config.roomMode) config.room_mode = this._config.roomMode || api_namespaceObject.RoomMode.Default;
611
+ if (this._config.videoConfig) if (isScreenShareDevice(this._config.videoConfig.videoInputDeviceId)) config.video_config = {
698
612
  stream_video_type: 'screen'
699
613
  };
700
614
  else config.video_config = {
701
615
  stream_video_type: 'main'
702
616
  };
617
+ if (this._config.translateConfig) config.translate_config = this._config.translateConfig;
618
+ if (this._config.turnDetection) config.turn_detection = this._config.turnDetection;
619
+ const params = {
620
+ bot_id: botId,
621
+ conversation_id: conversationId || void 0,
622
+ voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
623
+ connector_id: this._config.connectorId,
624
+ uid: this._config.userId || void 0,
625
+ workflow_id: this._config.workflowId || void 0,
626
+ config
627
+ };
628
+ roomInfo = yield this._api.audio.rooms.create(params);
703
629
  }
704
- if (this._config.translateConfig) config.translate_config = this._config.translateConfig;
705
- const params = {
706
- bot_id: botId,
707
- conversation_id: conversationId || void 0,
708
- voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
709
- connector_id: this._config.connectorId,
710
- uid: this._config.userId || void 0,
711
- workflow_id: this._config.workflowId || void 0,
712
- config
713
- };
714
- roomInfo = await this._api.audio.rooms.create(params);
630
+ } catch (error) {
631
+ this.dispatch(event_names.ERROR, error);
632
+ throw new RealtimeAPIError(error_RealtimeError.CREATE_ROOM_ERROR, error instanceof Error ? error.message : 'Unknown error', error);
715
633
  }
716
- } catch (error) {
717
- this.dispatch(event_names.ERROR, error);
718
- throw new RealtimeAPIError(error_RealtimeError.CREATE_ROOM_ERROR, error instanceof Error ? error.message : 'Unknown error', error);
719
- }
720
- this.dispatch(event_names.ROOM_INFO, {
721
- roomId: roomInfo.room_id,
722
- uid: roomInfo.uid,
723
- token: roomInfo.token,
724
- appId: roomInfo.app_id
725
- });
726
- this._isTestEnv = TEST_APP_ID === roomInfo.app_id;
727
- // Step2 create engine
728
- this._client = new EngineClient(roomInfo.app_id, this._config.debug, this._isTestEnv, this._isSupportVideo, this._config.videoConfig);
729
- // Step3 bind engine events
730
- this._client.bindEngineEvents();
731
- this._client.on(event_names.ALL, (eventName, data)=>{
732
- this.dispatch(eventName, data, false);
733
- });
734
- if (this._config.suppressStationaryNoise) {
735
- await this._client.enableAudioNoiseReduction();
736
- this.dispatch(event_names.SUPPRESS_STATIONARY_NOISE, {});
737
- }
738
- if (this._config.suppressNonStationaryNoise) try {
739
- await this._client.initAIAnsExtension();
740
- this._client.changeAIAnsExtension(true);
741
- this.dispatch(event_names.SUPPRESS_NON_STATIONARY_NOISE, {});
742
- } catch (error) {
743
- console.warn('Config suppressNonStationaryNoise is not supported', error);
744
- }
745
- var _this__config_audioMutedDefault, _this__config_videoConfig_videoOnDefault, _this__config_isAutoSubscribeAudio;
746
- // Step4 join room
747
- await this._client.joinRoom({
748
- token: roomInfo.token,
749
- roomId: roomInfo.room_id,
750
- uid: roomInfo.uid,
751
- audioMutedDefault: null !== (_this__config_audioMutedDefault = this._config.audioMutedDefault) && void 0 !== _this__config_audioMutedDefault && _this__config_audioMutedDefault,
752
- videoOnDefault: null === (_this__config_videoConfig_videoOnDefault = null === (_this__config_videoConfig = this._config.videoConfig) || void 0 === _this__config_videoConfig ? void 0 : _this__config_videoConfig.videoOnDefault) || void 0 === _this__config_videoConfig_videoOnDefault || _this__config_videoConfig_videoOnDefault,
753
- isAutoSubscribeAudio: null === (_this__config_isAutoSubscribeAudio = this._config.isAutoSubscribeAudio) || void 0 === _this__config_isAutoSubscribeAudio || _this__config_isAutoSubscribeAudio
754
- });
755
- // Step5 create local stream
756
- await this._client.createLocalStream(roomInfo.uid, this._config.videoConfig);
757
- // step6 set connected and dispatch connected event
758
- this.isConnected = true;
759
- this.dispatch(event_names.CONNECTED, {
760
- roomId: roomInfo.room_id,
761
- uid: roomInfo.uid,
762
- token: roomInfo.token,
763
- appId: roomInfo.app_id
764
- });
634
+ this.dispatch(event_names.ROOM_INFO, {
635
+ roomId: roomInfo.room_id,
636
+ uid: roomInfo.uid,
637
+ token: roomInfo.token,
638
+ appId: roomInfo.app_id
639
+ });
640
+ this._isTestEnv = TEST_APP_ID === roomInfo.app_id;
641
+ this._client = new EngineClient(roomInfo.app_id, this._config.debug, this._isTestEnv, this._isSupportVideo, this._config.videoConfig);
642
+ this._client.bindEngineEvents();
643
+ this._client.on(event_names.ALL, (eventName, data)=>{
644
+ this.dispatch(eventName, data, false);
645
+ });
646
+ if (this._config.suppressStationaryNoise) {
647
+ yield this._client.enableAudioNoiseReduction();
648
+ this.dispatch(event_names.SUPPRESS_STATIONARY_NOISE, {});
649
+ }
650
+ if (this._config.suppressNonStationaryNoise) try {
651
+ yield this._client.initAIAnsExtension();
652
+ this._client.changeAIAnsExtension(true);
653
+ this.dispatch(event_names.SUPPRESS_NON_STATIONARY_NOISE, {});
654
+ } catch (error) {
655
+ console.warn('Config suppressNonStationaryNoise is not supported', error);
656
+ }
657
+ var _this__config_audioMutedDefault, _this__config_videoConfig_videoOnDefault, _this__config_isAutoSubscribeAudio;
658
+ yield this._client.joinRoom({
659
+ token: roomInfo.token,
660
+ roomId: roomInfo.room_id,
661
+ uid: roomInfo.uid,
662
+ audioMutedDefault: null != (_this__config_audioMutedDefault = this._config.audioMutedDefault) ? _this__config_audioMutedDefault : false,
663
+ videoOnDefault: null != (_this__config_videoConfig_videoOnDefault = null == (_this__config_videoConfig = this._config.videoConfig) ? void 0 : _this__config_videoConfig.videoOnDefault) ? _this__config_videoConfig_videoOnDefault : true,
664
+ isAutoSubscribeAudio: null != (_this__config_isAutoSubscribeAudio = this._config.isAutoSubscribeAudio) ? _this__config_isAutoSubscribeAudio : true
665
+ });
666
+ yield this._client.createLocalStream(roomInfo.uid, this._config.videoConfig);
667
+ this.isConnected = true;
668
+ this.dispatch(event_names.CONNECTED, {
669
+ roomId: roomInfo.room_id,
670
+ uid: roomInfo.uid,
671
+ token: roomInfo.token,
672
+ appId: roomInfo.app_id
673
+ });
674
+ }).call(this);
765
675
  }
766
- /**
767
- * en: Interrupt the current conversation
768
- *
769
- * zh: 中断当前对话
770
- */ async interrupt() {
771
- var _this__client;
772
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.stop());
773
- this.dispatch(event_names.INTERRUPTED, {});
774
- }
775
- /**
776
- * en: Disconnect from the current session
777
- *
778
- * zh: 断开与当前会话的连接
779
- */ async disconnect() {
780
- var _this__client;
781
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.disconnect());
782
- this.isConnected = false;
783
- this._client = null;
784
- this.dispatch(event_names.DISCONNECTED, {});
785
- }
786
- /**
787
- * en: Send a message to the bot
788
- *
789
- * zh: 发送消息给Bot
790
- */ async sendMessage(message) {
791
- var _this__client;
792
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.sendMessage(message));
793
- const eventType = 'string' == typeof message.event_type ? message.event_type : 'unknown_event';
794
- this.dispatch(`client.${eventType}`, message);
795
- }
796
- /**
797
- * en: Enable or disable audio
798
- *
799
- * zh: 启用或禁用音频
800
- */ async setAudioEnable(isEnable) {
801
- var _this__client;
802
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.changeAudioState(isEnable));
803
- if (isEnable) this.dispatch(event_names.AUDIO_UNMUTED, {});
804
- else this.dispatch(event_names.AUDIO_MUTED, {});
676
+ interrupt() {
677
+ return src_async_to_generator(function*() {
678
+ var _this__client;
679
+ yield null == (_this__client = this._client) ? void 0 : _this__client.stop();
680
+ this.dispatch(event_names.INTERRUPTED, {});
681
+ }).call(this);
805
682
  }
806
- async setVideoEnable(isEnable) {
807
- try {
683
+ disconnect() {
684
+ return src_async_to_generator(function*() {
808
685
  var _this__client;
809
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.changeVideoState(isEnable));
810
- this.dispatch(isEnable ? event_names.VIDEO_ON : event_names.VIDEO_OFF, {});
811
- } catch (e) {
812
- this.dispatch(event_names.VIDEO_ERROR, e);
813
- throw e;
814
- }
686
+ yield null == (_this__client = this._client) ? void 0 : _this__client.disconnect();
687
+ this.isConnected = false;
688
+ this._client = null;
689
+ this.dispatch(event_names.DISCONNECTED, {});
690
+ }).call(this);
691
+ }
692
+ sendMessage(message) {
693
+ return src_async_to_generator(function*() {
694
+ var _this__client;
695
+ yield null == (_this__client = this._client) ? void 0 : _this__client.sendMessage(message);
696
+ const eventType = 'string' == typeof message.event_type ? message.event_type : 'unknown_event';
697
+ this.dispatch(`client.${eventType}`, message);
698
+ }).call(this);
815
699
  }
816
- /**
817
- * en: Enable audio properties reporting (debug mode only)
818
- *
819
- * zh: 启用音频属性报告(仅限调试模式)
820
- */ enableAudioPropertiesReport(config) {
700
+ setAudioEnable(isEnable) {
701
+ return src_async_to_generator(function*() {
702
+ var _this__client;
703
+ yield null == (_this__client = this._client) ? void 0 : _this__client.changeAudioState(isEnable);
704
+ if (isEnable) this.dispatch(event_names.AUDIO_UNMUTED, {});
705
+ else this.dispatch(event_names.AUDIO_MUTED, {});
706
+ }).call(this);
707
+ }
708
+ setVideoEnable(isEnable) {
709
+ return src_async_to_generator(function*() {
710
+ try {
711
+ var _this__client;
712
+ yield null == (_this__client = this._client) ? void 0 : _this__client.changeVideoState(isEnable);
713
+ this.dispatch(isEnable ? event_names.VIDEO_ON : event_names.VIDEO_OFF, {});
714
+ } catch (e) {
715
+ this.dispatch(event_names.VIDEO_ERROR, e);
716
+ throw e;
717
+ }
718
+ }).call(this);
719
+ }
720
+ enableAudioPropertiesReport(config) {
821
721
  if (this._config.debug) {
822
722
  var _this__client;
823
- null === (_this__client = this._client) || void 0 === _this__client || _this__client.enableAudioPropertiesReport(config);
723
+ null == (_this__client = this._client) || _this__client.enableAudioPropertiesReport(config);
824
724
  return true;
825
725
  }
826
726
  console.warn('enableAudioPropertiesReport is not supported in non-debug mode');
827
727
  return false;
828
728
  }
829
- /**
830
- * en: Start audio playback device test (debug mode only)
831
- *
832
- * zh: 开始音频播放设备测试(仅限调试模式)
833
- */ async startAudioPlaybackDeviceTest() {
834
- if (this._config.debug) {
835
- var _this__client;
836
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.startAudioPlaybackDeviceTest());
837
- } else console.warn('startAudioPlaybackDeviceTest is not supported in non-debug mode');
838
- }
839
- /**
840
- * en: Stop audio playback device test (debug mode only)
841
- *
842
- * zh: 停止音频播放设备测试(仅限调试模式)
843
- */ stopAudioPlaybackDeviceTest() {
729
+ startAudioPlaybackDeviceTest() {
730
+ return src_async_to_generator(function*() {
731
+ if (this._config.debug) {
732
+ var _this__client;
733
+ yield null == (_this__client = this._client) ? void 0 : _this__client.startAudioPlaybackDeviceTest();
734
+ } else console.warn('startAudioPlaybackDeviceTest is not supported in non-debug mode');
735
+ }).call(this);
736
+ }
737
+ stopAudioPlaybackDeviceTest() {
844
738
  if (this._config.debug) {
845
739
  var _this__client;
846
- null === (_this__client = this._client) || void 0 === _this__client || _this__client.stopAudioPlaybackDeviceTest();
740
+ null == (_this__client = this._client) || _this__client.stopAudioPlaybackDeviceTest();
847
741
  } else console.warn('stopAudioPlaybackDeviceTest is not supported in non-debug mode');
848
742
  }
849
- /**
850
- * en: Set the audio input device
851
- *
852
- * zh: 设置音频输入设备
853
- */ async setAudioInputDevice(deviceId) {
854
- var _this__client;
855
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioInputDevice(deviceId));
856
- this.dispatch(event_names.AUDIO_INPUT_DEVICE_CHANGED, {
857
- deviceId
858
- });
859
- }
860
- /**
861
- * en: Set the audio output device
862
- *
863
- * zh: 设置音频输出设备
864
- */ async setAudioOutputDevice(deviceId) {
865
- var _this__client;
866
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioOutputDevice(deviceId));
867
- this.dispatch(event_names.AUDIO_OUTPUT_DEVICE_CHANGED, {
868
- deviceId
869
- });
743
+ setAudioInputDevice(deviceId) {
744
+ return src_async_to_generator(function*() {
745
+ var _this__client;
746
+ yield null == (_this__client = this._client) ? void 0 : _this__client.setAudioInputDevice(deviceId);
747
+ this.dispatch(event_names.AUDIO_INPUT_DEVICE_CHANGED, {
748
+ deviceId
749
+ });
750
+ }).call(this);
870
751
  }
871
- /**
872
- * en: Set the video input device
873
- *
874
- * zh: 设置视频输入设备
875
- */ async setVideoInputDevice(deviceId) {
876
- try {
752
+ setAudioOutputDevice(deviceId) {
753
+ return src_async_to_generator(function*() {
877
754
  var _this__client;
878
- await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setVideoInputDevice(deviceId));
879
- this.dispatch(event_names.VIDEO_ON, {});
880
- } catch (e) {
881
- this.dispatch(event_names.VIDEO_ERROR, e);
882
- throw e;
883
- }
884
- this.dispatch(event_names.VIDEO_INPUT_DEVICE_CHANGED, {
885
- deviceId
886
- });
755
+ yield null == (_this__client = this._client) ? void 0 : _this__client.setAudioOutputDevice(deviceId);
756
+ this.dispatch(event_names.AUDIO_OUTPUT_DEVICE_CHANGED, {
757
+ deviceId
758
+ });
759
+ }).call(this);
760
+ }
761
+ setVideoInputDevice(deviceId) {
762
+ return src_async_to_generator(function*() {
763
+ try {
764
+ var _this__client;
765
+ yield null == (_this__client = this._client) ? void 0 : _this__client.setVideoInputDevice(deviceId);
766
+ this.dispatch(event_names.VIDEO_ON, {});
767
+ } catch (e) {
768
+ this.dispatch(event_names.VIDEO_ERROR, e);
769
+ throw e;
770
+ }
771
+ this.dispatch(event_names.VIDEO_INPUT_DEVICE_CHANGED, {
772
+ deviceId
773
+ });
774
+ }).call(this);
887
775
  }
888
- /**
889
- * en: Get the RTC engine instance, for detail visit https://www.volcengine.com/docs/6348/104481
890
- *
891
- * zh: 获取 RTC 引擎实例,详情请访问 https://www.volcengine.com/docs/6348/104481
892
- */ getRtcEngine() {
776
+ getRtcEngine() {
893
777
  var _this__client;
894
- return null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.getRtcEngine();
895
- }
896
- /**
897
- * Constructor for initializing a RealtimeClient instance.
898
- *
899
- * 构造函数,初始化RealtimeClient实例。
900
- *
901
- * @param config
902
- * @param config.accessToken - Required, Access Token. |
903
- * 必填,Access Token。
904
- * @param config.botId - Required, Bot Id. |
905
- * 必填,Bot Id。
906
- * @param config.voiceId - Optional, Voice Id. |
907
- * 可选,音色Id。
908
- * @param config.conversationId - Optional, Conversation Id. |
909
- * 可选,会话Id。
910
- * @param config.userId - Optional, User Id. |
911
- * 可选,用户Id。
912
- * @param config.baseURL - Optional, defaults to "https://api.coze.cn". |
913
- * 可选,默认值为 "https://api.coze.cn"。
914
- * @param config.debug - Optional, defaults to false.
915
- * 可选,默认值为 false。
916
- * @param config.allowPersonalAccessTokenInBrowser
917
- * - Optional, whether to allow personal access tokens in browser environment. |
918
- * 可选,是否允许在浏览器环境中使用个人访问令牌。
919
- * @param config.audioMutedDefault - Optional, whether audio is muted by default, defaults to false. |
920
- * 可选,默认是否静音,默认值为 false。
921
- * @param config.connectorId - Required, Connector Id. |
922
- * 必填,渠道 Id。
923
- * @param config.suppressStationaryNoise - Optional, suppress stationary noise, defaults to false. |
924
- * 可选,默认是否抑制静态噪声,默认值为 false。
925
- * @param config.suppressNonStationaryNoise - Optional, suppress non-stationary noise, defaults to false. |
926
- * 可选,默认是否抑制非静态噪声,默认值为 false。
927
- * @param config.isAutoSubscribeAudio - Optional, whether to automatically subscribe to bot reply audio streams, defaults to true. |
928
- * @param config.videoConfig - Optional, Video configuration. |
929
- * 可选,视频配置。
930
- * @param config.videoConfig.videoOnDefault - Optional, Whether to turn on video by default, defaults to true. |
931
- * 可选,默认是否开启视频,默认值为 true。
932
- * @param config.videoConfig.renderDom - Optional, The DOM element to render the video stream to. |
933
- * 可选,渲染视频流的 DOM 元素。
934
- * @param config.videoConfig.videoInputDeviceId - Optional, The device ID of the video input device to use. |
935
- * 可选,视频输入设备的设备 ID。
936
- * @param config.videoConfig.screenConfig - Optional, Screen share configuration if videoInputDeviceId is 'screenShare' see https://www.volcengine.com/docs/6348/104481#screenconfig for more details. |
937
- * 可选,屏幕共享配置,如果 videoInputDeviceId 是 'screenShare',请参考 https://www.volcengine.com/docs/6348/104481#screenconfig 了解更多详情。
938
- * @param config.prologueContent - Optional, Prologue content. | 可选,开场白内容。
939
- * @param config.roomMode - Optional, Room mode. | 可选,房间模式。
940
- */ constructor(config){
778
+ return null == (_this__client = this._client) ? void 0 : _this__client.getRtcEngine();
779
+ }
780
+ constructor(config){
941
781
  super(config.debug), this._client = null, this.isConnected = false, this._isTestEnv = false, this._isSupportVideo = false;
942
782
  this._config = config;
943
783
  var _this__config_baseURL;
944
- const defaultBaseURL = null !== (_this__config_baseURL = this._config.baseURL) && void 0 !== _this__config_baseURL ? _this__config_baseURL : 'https://api.coze.cn';
784
+ const defaultBaseURL = null != (_this__config_baseURL = this._config.baseURL) ? _this__config_baseURL : 'https://api.coze.cn';
945
785
  this._config.baseURL = defaultBaseURL;
946
- // init api
947
786
  this._api = new api_namespaceObject.CozeAPI({
948
787
  token: this._config.accessToken,
949
788
  baseURL: defaultBaseURL,
@@ -952,8 +791,18 @@ class RealtimeClient extends RealtimeEventHandler {
952
791
  this._isSupportVideo = !!config.videoConfig;
953
792
  }
954
793
  }
955
- var __webpack_export_target__ = exports;
956
- for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
957
- if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
794
+ exports.EventNames = __webpack_exports__.EventNames;
795
+ exports.RealtimeAPIError = __webpack_exports__.RealtimeAPIError;
796
+ exports.RealtimeClient = __webpack_exports__.RealtimeClient;
797
+ exports.RealtimeError = __webpack_exports__.RealtimeError;
798
+ exports.RealtimeUtils = __webpack_exports__.RealtimeUtils;
799
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
800
+ "EventNames",
801
+ "RealtimeAPIError",
802
+ "RealtimeClient",
803
+ "RealtimeError",
804
+ "RealtimeUtils"
805
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
806
+ Object.defineProperty(exports, '__esModule', {
958
807
  value: true
959
808
  });