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