@coze/realtime-api 1.3.0 → 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/README.md +3 -0
- package/README.zh-CN.md +3 -0
- package/dist/cjs/event-names/index.js +220 -210
- package/dist/cjs/index.js +762 -624
- package/dist/cjs/live/index.js +289 -0
- package/dist/esm/event-names/index.js +216 -169
- package/dist/esm/index.js +757 -591
- package/dist/esm/live/index.js +287 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/event-names/event-names.d.ts +8 -2
- package/dist/types/index.d.ts +330 -9
- package/dist/types/live/live/index.d.ts +91 -0
- package/package.json +26 -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 -163
- package/dist/types/event-names/utils.d.ts +0 -49
- package/dist/types/event-names.d.ts +0 -208
- package/dist/types/utils.d.ts +0 -49
- package/dist/umd/index.js +0 -987
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,225 +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
|
-
|
|
193
|
-
|
|
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
|
-
|
|
209
|
+
* en: Bot joined
|
|
210
|
+
* zh: Bot 加入
|
|
211
|
+
*/
|
|
212
|
+
EventNames["BOT_JOIN"] = "server.bot.join";
|
|
213
|
+
/**
|
|
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) {
|
|
293
318
|
RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
|
|
294
319
|
RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
|
|
295
320
|
RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
|
|
@@ -302,21 +327,40 @@ var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
|
|
|
302
327
|
RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
|
|
303
328
|
RealtimeError["PARSE_MESSAGE_ERROR"] = "PARSE_MESSAGE_ERROR";
|
|
304
329
|
RealtimeError["HANDLER_MESSAGE_ERROR"] = "HANDLER_MESSAGE_ERROR";
|
|
305
|
-
|
|
306
|
-
|
|
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
|
+
});
|
|
307
345
|
class RealtimeAPIError extends Error {
|
|
308
346
|
/**
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
347
|
+
* @param code - Error code
|
|
348
|
+
* @param message - Error message
|
|
349
|
+
* @param error - Error object
|
|
350
|
+
*/
|
|
351
|
+
constructor(code, message, error) {
|
|
313
352
|
super(`[${code}] ${message}`);
|
|
314
353
|
this.name = 'RealtimeAPIError';
|
|
315
354
|
this.code = code;
|
|
316
355
|
this.error = error;
|
|
317
356
|
}
|
|
318
357
|
}
|
|
358
|
+
|
|
319
359
|
class RealtimeEventHandler {
|
|
360
|
+
constructor(debug = false) {
|
|
361
|
+
this.eventHandlers = {};
|
|
362
|
+
this._debug = debug;
|
|
363
|
+
}
|
|
320
364
|
clearEventHandlers() {
|
|
321
365
|
this.eventHandlers = {};
|
|
322
366
|
}
|
|
@@ -331,183 +375,254 @@ class RealtimeEventHandler {
|
|
|
331
375
|
const handlers = this.eventHandlers[eventName] || [];
|
|
332
376
|
if (callback) {
|
|
333
377
|
const index = handlers.indexOf(callback);
|
|
334
|
-
if (
|
|
378
|
+
if (index === -1) {
|
|
335
379
|
console.warn(`Could not turn off specified event listener for "${eventName}": not found as a listener`);
|
|
336
380
|
return;
|
|
337
381
|
}
|
|
338
382
|
handlers.splice(index, 1);
|
|
339
|
-
}
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
delete this.eventHandlers[eventName];
|
|
386
|
+
}
|
|
340
387
|
}
|
|
341
388
|
// eslint-disable-next-line max-params
|
|
342
389
|
_dispatchToHandlers(eventName, event, handlers, prefix) {
|
|
343
|
-
for (const handler of handlers)
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
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
|
+
}
|
|
347
399
|
}
|
|
348
400
|
}
|
|
349
|
-
dispatch(eventName, event) {
|
|
350
|
-
|
|
351
|
-
|
|
401
|
+
dispatch(eventName, event, consoleLog = true) {
|
|
402
|
+
if (consoleLog) {
|
|
403
|
+
this._log(`dispatch ${eventName} event`, event);
|
|
404
|
+
}
|
|
352
405
|
const handlers = (this.eventHandlers[eventName] || []).slice();
|
|
353
406
|
this._dispatchToHandlers(eventName, event, handlers);
|
|
354
|
-
const allHandlers = (this.eventHandlers[
|
|
407
|
+
const allHandlers = (this.eventHandlers[EventNames$1.ALL] || []).slice();
|
|
355
408
|
this._dispatchToHandlers(eventName, event, allHandlers);
|
|
356
|
-
const allClientHandlers = (this.eventHandlers[
|
|
409
|
+
const allClientHandlers = (this.eventHandlers[EventNames$1.ALL_CLIENT] || []).slice();
|
|
357
410
|
this._dispatchToHandlers(eventName, event, allClientHandlers, 'client.');
|
|
358
|
-
const allServerHandlers = (this.eventHandlers[
|
|
411
|
+
const allServerHandlers = (this.eventHandlers[EventNames$1.ALL_SERVER] || []).slice();
|
|
359
412
|
this._dispatchToHandlers(eventName, event, allServerHandlers, 'server.');
|
|
360
413
|
}
|
|
361
414
|
_log(message, event) {
|
|
362
|
-
if (this._debug)
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
this.eventHandlers = {};
|
|
366
|
-
this._debug = debug;
|
|
415
|
+
if (this._debug) {
|
|
416
|
+
console.log(`[RealtimeClient] ${message}`, event);
|
|
417
|
+
}
|
|
367
418
|
}
|
|
368
419
|
}
|
|
420
|
+
|
|
369
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
|
+
}
|
|
370
450
|
bindEngineEvents() {
|
|
371
|
-
this.engine.on(
|
|
372
|
-
this.engine.on(
|
|
373
|
-
this.engine.on(
|
|
374
|
-
this.engine.on(
|
|
375
|
-
this.engine.on(
|
|
376
|
-
this.engine.on(
|
|
377
|
-
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
|
+
}
|
|
378
460
|
if (this._debug) {
|
|
379
|
-
this.engine.on(
|
|
380
|
-
this.engine.on(
|
|
461
|
+
this.engine.on(VERTC.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
|
462
|
+
this.engine.on(VERTC.events.onRemoteAudioPropertiesReport, this.handleRemoteAudioPropertiesReport);
|
|
381
463
|
}
|
|
382
464
|
}
|
|
383
465
|
removeEventListener() {
|
|
384
|
-
this.engine.off(
|
|
385
|
-
this.engine.off(
|
|
386
|
-
this.engine.off(
|
|
387
|
-
this.engine.off(
|
|
388
|
-
this.engine.off(
|
|
389
|
-
this.engine.off(
|
|
390
|
-
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
|
+
}
|
|
391
475
|
if (this._debug) {
|
|
392
|
-
this.engine.off(
|
|
393
|
-
this.engine.off(
|
|
476
|
+
this.engine.off(VERTC.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
|
477
|
+
this.engine.off(VERTC.events.onRemoteAudioPropertiesReport, this.handleRemoteAudioPropertiesReport);
|
|
394
478
|
}
|
|
395
479
|
}
|
|
396
480
|
_parseMessage(event) {
|
|
397
481
|
try {
|
|
398
482
|
return JSON.parse(event.message);
|
|
399
|
-
|
|
400
|
-
}
|
|
401
|
-
|
|
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');
|
|
402
487
|
}
|
|
403
488
|
}
|
|
404
489
|
handleMessage(event) {
|
|
405
490
|
try {
|
|
406
491
|
const message = this._parseMessage(event);
|
|
407
492
|
this.dispatch(`server.${message.event_type}`, message);
|
|
408
|
-
}
|
|
493
|
+
}
|
|
494
|
+
catch (e) {
|
|
409
495
|
if (e instanceof RealtimeAPIError) {
|
|
410
|
-
if (e.code ===
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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
|
+
}
|
|
419
512
|
}
|
|
420
513
|
}
|
|
421
514
|
handleEventError(e) {
|
|
422
|
-
this.dispatch(
|
|
515
|
+
this.dispatch(EventNames$1.ERROR, e);
|
|
423
516
|
}
|
|
424
517
|
handleUserJoin(event) {
|
|
425
518
|
this.joinUserId = event.userInfo.userId;
|
|
426
|
-
this.dispatch(
|
|
519
|
+
this.dispatch(EventNames$1.BOT_JOIN, event);
|
|
427
520
|
}
|
|
428
521
|
handleUserLeave(event) {
|
|
429
|
-
this.dispatch(
|
|
522
|
+
this.dispatch(EventNames$1.BOT_LEAVE, event);
|
|
430
523
|
}
|
|
431
524
|
handlePlayerEvent(event) {
|
|
432
|
-
this.dispatch(
|
|
525
|
+
this.dispatch(EventNames$1.PLAYER_EVENT, event);
|
|
433
526
|
}
|
|
434
527
|
handleNetworkQuality(uplinkNetworkQuality, downlinkNetworkQuality) {
|
|
435
|
-
this.dispatch(
|
|
528
|
+
this.dispatch(EventNames$1.NETWORK_QUALITY, {
|
|
436
529
|
uplinkNetworkQuality,
|
|
437
|
-
downlinkNetworkQuality
|
|
530
|
+
downlinkNetworkQuality,
|
|
438
531
|
});
|
|
439
532
|
}
|
|
440
533
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
441
534
|
handleTrackEnded(event) {
|
|
442
|
-
if ((null
|
|
535
|
+
if ((event === null || event === void 0 ? void 0 : event.kind) === 'video') {
|
|
536
|
+
this.dispatch(EventNames$1.VIDEO_OFF, event);
|
|
537
|
+
}
|
|
443
538
|
}
|
|
444
539
|
async joinRoom(options) {
|
|
445
|
-
const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
|
|
540
|
+
const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio, } = options;
|
|
446
541
|
try {
|
|
447
542
|
await this.engine.joinRoom(token, roomId, {
|
|
448
|
-
userId: uid
|
|
543
|
+
userId: uid,
|
|
449
544
|
}, {
|
|
450
545
|
isAutoPublish: !audioMutedDefault,
|
|
451
546
|
isAutoSubscribeAudio,
|
|
452
|
-
isAutoSubscribeVideo: this._isSupportVideo && videoOnDefault
|
|
547
|
+
isAutoSubscribeVideo: this._isSupportVideo && videoOnDefault,
|
|
453
548
|
});
|
|
454
|
-
}
|
|
455
|
-
|
|
549
|
+
}
|
|
550
|
+
catch (e) {
|
|
551
|
+
if (e instanceof Error) {
|
|
552
|
+
throw new RealtimeAPIError(RealtimeError.CONNECTION_ERROR, e.message);
|
|
553
|
+
}
|
|
456
554
|
}
|
|
457
555
|
}
|
|
458
556
|
async setAudioInputDevice(deviceId) {
|
|
459
557
|
const devices = await getAudioDevices();
|
|
460
|
-
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
|
+
}
|
|
461
561
|
this.engine.stopAudioCapture();
|
|
462
562
|
await this.engine.startAudioCapture(deviceId);
|
|
463
563
|
}
|
|
464
564
|
async setAudioOutputDevice(deviceId) {
|
|
465
|
-
const devices = await getAudioDevices({
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
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
|
+
}
|
|
469
569
|
await this.engine.setAudioPlaybackDevice(deviceId);
|
|
470
570
|
}
|
|
471
|
-
async setVideoInputDevice(deviceId) {
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
this.engine.setLocalVideoPlayer(isScreenShareDevice(deviceId)
|
|
479
|
-
|
|
480
|
-
|
|
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,
|
|
481
583
|
});
|
|
482
584
|
await this.changeVideoState(false);
|
|
483
585
|
if (isScreenShareDevice(deviceId)) {
|
|
484
|
-
if (this._streamIndex ===
|
|
586
|
+
if (this._streamIndex === StreamIndex.STREAM_INDEX_MAIN) {
|
|
587
|
+
this.engine.setLocalVideoPlayer(StreamIndex.STREAM_INDEX_MAIN);
|
|
588
|
+
}
|
|
485
589
|
if (isAutoCapture) {
|
|
486
|
-
|
|
487
|
-
this.engine.
|
|
488
|
-
await this.engine.
|
|
489
|
-
await this.engine.publishScreen(__WEBPACK_EXTERNAL_MODULE__volcengine_rtc__.MediaType.VIDEO);
|
|
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);
|
|
490
593
|
}
|
|
491
|
-
this._streamIndex =
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
if (
|
|
495
|
-
|
|
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
|
+
}
|
|
600
|
+
if (isAutoCapture) {
|
|
601
|
+
await this.engine.startVideoCapture(deviceId);
|
|
602
|
+
}
|
|
603
|
+
this._streamIndex = StreamIndex.STREAM_INDEX_MAIN;
|
|
496
604
|
}
|
|
497
605
|
}
|
|
498
606
|
async createLocalStream(userId, videoConfig) {
|
|
499
607
|
this._roomUserId = userId;
|
|
500
|
-
const devices = await getAudioDevices({
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
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
|
+
}
|
|
505
615
|
await this.engine.startAudioCapture(devices.audioInputs[0].deviceId);
|
|
506
|
-
if (this._isSupportVideo)
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
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
|
+
}
|
|
511
626
|
}
|
|
512
627
|
}
|
|
513
628
|
async disconnect() {
|
|
@@ -515,34 +630,47 @@ class EngineClient extends RealtimeEventHandler {
|
|
|
515
630
|
await this.engine.leaveRoom();
|
|
516
631
|
this.removeEventListener();
|
|
517
632
|
this.clearEventHandlers();
|
|
518
|
-
|
|
519
|
-
}
|
|
520
|
-
|
|
633
|
+
VERTC.destroyEngine(this.engine);
|
|
634
|
+
}
|
|
635
|
+
catch (e) {
|
|
636
|
+
this.dispatch(EventNames$1.ERROR, e);
|
|
521
637
|
throw e;
|
|
522
638
|
}
|
|
523
639
|
}
|
|
524
640
|
async changeAudioState(isMicOn) {
|
|
525
641
|
try {
|
|
526
|
-
if (isMicOn)
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
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);
|
|
530
651
|
throw e;
|
|
531
652
|
}
|
|
532
653
|
}
|
|
533
654
|
async changeVideoState(isVideoOn) {
|
|
655
|
+
var _a;
|
|
534
656
|
if (isVideoOn) {
|
|
535
|
-
if (this._streamIndex ===
|
|
657
|
+
if (this._streamIndex === StreamIndex.STREAM_INDEX_MAIN) {
|
|
658
|
+
await this.engine.startVideoCapture();
|
|
659
|
+
}
|
|
536
660
|
else {
|
|
537
|
-
|
|
538
|
-
this.engine.
|
|
539
|
-
await this.engine.
|
|
540
|
-
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);
|
|
541
664
|
}
|
|
542
|
-
}
|
|
665
|
+
}
|
|
543
666
|
else {
|
|
544
|
-
|
|
545
|
-
|
|
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
|
+
}
|
|
546
674
|
}
|
|
547
675
|
}
|
|
548
676
|
async stop() {
|
|
@@ -550,11 +678,12 @@ class EngineClient extends RealtimeEventHandler {
|
|
|
550
678
|
const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify({
|
|
551
679
|
id: 'event_1',
|
|
552
680
|
event_type: 'conversation.chat.cancel',
|
|
553
|
-
data: {}
|
|
681
|
+
data: {},
|
|
554
682
|
}));
|
|
555
683
|
this._log(`interrupt ${this.joinUserId} ${result}`);
|
|
556
|
-
}
|
|
557
|
-
|
|
684
|
+
}
|
|
685
|
+
catch (e) {
|
|
686
|
+
this.dispatch(EventNames$1.ERROR, e);
|
|
558
687
|
throw e;
|
|
559
688
|
}
|
|
560
689
|
}
|
|
@@ -562,8 +691,9 @@ class EngineClient extends RealtimeEventHandler {
|
|
|
562
691
|
try {
|
|
563
692
|
const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify(message));
|
|
564
693
|
this._log(`sendMessage ${this.joinUserId} ${JSON.stringify(message)} ${result}`);
|
|
565
|
-
}
|
|
566
|
-
|
|
694
|
+
}
|
|
695
|
+
catch (e) {
|
|
696
|
+
this.dispatch(EventNames$1.ERROR, e);
|
|
567
697
|
throw e;
|
|
568
698
|
}
|
|
569
699
|
}
|
|
@@ -572,348 +702,384 @@ class EngineClient extends RealtimeEventHandler {
|
|
|
572
702
|
}
|
|
573
703
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
574
704
|
handleLocalAudioPropertiesReport(event) {
|
|
575
|
-
var
|
|
576
|
-
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
|
+
}
|
|
577
709
|
}
|
|
578
710
|
handleRemoteAudioPropertiesReport(event) {
|
|
579
|
-
if (this._debug)
|
|
711
|
+
if (this._debug) {
|
|
712
|
+
console.log('handleRemoteAudioPropertiesReport', event);
|
|
713
|
+
}
|
|
580
714
|
}
|
|
581
715
|
async enableAudioNoiseReduction() {
|
|
582
|
-
var
|
|
583
|
-
await (
|
|
716
|
+
var _a;
|
|
717
|
+
await ((_a = this.engine) === null || _a === void 0 ? void 0 : _a.setAudioCaptureConfig({
|
|
584
718
|
noiseSuppression: true,
|
|
585
719
|
echoCancellation: true,
|
|
586
|
-
autoGainControl: true
|
|
720
|
+
autoGainControl: true,
|
|
587
721
|
}));
|
|
588
722
|
}
|
|
589
723
|
async initAIAnsExtension() {
|
|
590
|
-
const AIAnsExtension = new
|
|
724
|
+
const AIAnsExtension = new RTCAIAnsExtension();
|
|
591
725
|
await this.engine.registerExtension(AIAnsExtension);
|
|
592
726
|
this._AIAnsExtension = AIAnsExtension;
|
|
593
727
|
}
|
|
594
728
|
changeAIAnsExtension(enable) {
|
|
729
|
+
var _a, _b;
|
|
595
730
|
if (enable) {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
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();
|
|
601
735
|
}
|
|
602
736
|
}
|
|
603
737
|
async startAudioPlaybackDeviceTest() {
|
|
604
738
|
try {
|
|
605
739
|
await this.engine.startAudioPlaybackDeviceTest('audio-test.wav', 200);
|
|
606
|
-
}
|
|
607
|
-
|
|
740
|
+
}
|
|
741
|
+
catch (e) {
|
|
742
|
+
this.dispatch(EventNames$1.ERROR, e);
|
|
608
743
|
throw e;
|
|
609
744
|
}
|
|
610
745
|
}
|
|
611
746
|
stopAudioPlaybackDeviceTest() {
|
|
612
747
|
try {
|
|
613
748
|
this.engine.stopAudioPlaybackDeviceTest();
|
|
614
|
-
}
|
|
615
|
-
|
|
749
|
+
}
|
|
750
|
+
catch (e) {
|
|
751
|
+
this.dispatch(EventNames$1.ERROR, e);
|
|
616
752
|
throw e;
|
|
617
753
|
}
|
|
618
754
|
}
|
|
619
755
|
getRtcEngine() {
|
|
620
756
|
return this.engine;
|
|
621
757
|
}
|
|
622
|
-
// eslint-disable-next-line max-params
|
|
623
|
-
constructor(appId, debug = false, isTestEnv = false, isSupportVideo = false, videoConfig){
|
|
624
|
-
super(debug), this.joinUserId = '', this._AIAnsExtension = null, this._isSupportVideo = false;
|
|
625
|
-
if (isTestEnv) __WEBPACK_EXTERNAL_MODULE__volcengine_rtc__["default"].setParameter('ICE_CONFIG_REQUEST_URLS', [
|
|
626
|
-
'rtc-test.bytedance.com'
|
|
627
|
-
]);
|
|
628
|
-
this.engine = __WEBPACK_EXTERNAL_MODULE__volcengine_rtc__["default"].createEngine(appId);
|
|
629
|
-
this.handleMessage = this.handleMessage.bind(this);
|
|
630
|
-
this.handleUserJoin = this.handleUserJoin.bind(this);
|
|
631
|
-
this.handleUserLeave = this.handleUserLeave.bind(this);
|
|
632
|
-
this.handleEventError = this.handleEventError.bind(this);
|
|
633
|
-
this.handlePlayerEvent = this.handlePlayerEvent.bind(this);
|
|
634
|
-
this.handleNetworkQuality = this.handleNetworkQuality.bind(this);
|
|
635
|
-
this.handleTrackEnded = this.handleTrackEnded.bind(this);
|
|
636
|
-
// Debug only
|
|
637
|
-
this.handleLocalAudioPropertiesReport = this.handleLocalAudioPropertiesReport.bind(this);
|
|
638
|
-
this.handleRemoteAudioPropertiesReport = this.handleRemoteAudioPropertiesReport.bind(this);
|
|
639
|
-
this._isSupportVideo = isSupportVideo;
|
|
640
|
-
this._videoConfig = videoConfig;
|
|
641
|
-
}
|
|
642
758
|
}
|
|
759
|
+
|
|
643
760
|
// Only use for test
|
|
644
761
|
const TEST_APP_ID = '6705332c79516e015e3e5f0c';
|
|
645
762
|
class RealtimeClient extends RealtimeEventHandler {
|
|
646
763
|
/**
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
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;
|
|
652
833
|
const { botId, conversationId, voiceId, getRoomInfo } = this._config;
|
|
653
|
-
this.dispatch(
|
|
834
|
+
this.dispatch(EventNames$1.CONNECTING, {});
|
|
654
835
|
let roomInfo;
|
|
655
836
|
try {
|
|
656
837
|
// Step1 get token
|
|
657
|
-
if (getRoomInfo)
|
|
838
|
+
if (getRoomInfo) {
|
|
839
|
+
roomInfo = await getRoomInfo();
|
|
840
|
+
}
|
|
658
841
|
else {
|
|
659
842
|
const config = {};
|
|
660
|
-
if (this._config.prologueContent)
|
|
661
|
-
|
|
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
|
+
}
|
|
662
850
|
if (this._config.videoConfig) {
|
|
663
|
-
if (isScreenShareDevice(this._config.videoConfig.videoInputDeviceId))
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
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;
|
|
669
867
|
}
|
|
670
|
-
|
|
868
|
+
const params = {
|
|
671
869
|
bot_id: botId,
|
|
672
|
-
conversation_id: conversationId ||
|
|
673
|
-
voice_id: voiceId && voiceId.length > 0 ? voiceId :
|
|
870
|
+
conversation_id: conversationId || undefined,
|
|
871
|
+
voice_id: voiceId && voiceId.length > 0 ? voiceId : undefined,
|
|
674
872
|
connector_id: this._config.connectorId,
|
|
675
|
-
uid: this._config.userId ||
|
|
676
|
-
workflow_id: this._config.workflowId ||
|
|
677
|
-
config
|
|
678
|
-
}
|
|
873
|
+
uid: this._config.userId || undefined,
|
|
874
|
+
workflow_id: this._config.workflowId || undefined,
|
|
875
|
+
config,
|
|
876
|
+
};
|
|
877
|
+
roomInfo = await this._api.audio.rooms.create(params);
|
|
679
878
|
}
|
|
680
|
-
} catch (error) {
|
|
681
|
-
this.dispatch(event_names.ERROR, error);
|
|
682
|
-
throw new RealtimeAPIError(error_RealtimeError.CREATE_ROOM_ERROR, error instanceof Error ? error.message : 'Unknown error', error);
|
|
683
879
|
}
|
|
684
|
-
|
|
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, {
|
|
685
885
|
roomId: roomInfo.room_id,
|
|
686
886
|
uid: roomInfo.uid,
|
|
687
887
|
token: roomInfo.token,
|
|
688
|
-
appId: roomInfo.app_id
|
|
888
|
+
appId: roomInfo.app_id,
|
|
689
889
|
});
|
|
690
890
|
this._isTestEnv = TEST_APP_ID === roomInfo.app_id;
|
|
691
891
|
// Step2 create engine
|
|
692
892
|
this._client = new EngineClient(roomInfo.app_id, this._config.debug, this._isTestEnv, this._isSupportVideo, this._config.videoConfig);
|
|
693
893
|
// Step3 bind engine events
|
|
694
894
|
this._client.bindEngineEvents();
|
|
695
|
-
this._client.on(
|
|
895
|
+
this._client.on(EventNames$1.ALL, (eventName, data) => {
|
|
696
896
|
this.dispatch(eventName, data, false);
|
|
697
897
|
});
|
|
698
898
|
if (this._config.suppressStationaryNoise) {
|
|
699
899
|
await this._client.enableAudioNoiseReduction();
|
|
700
|
-
this.dispatch(
|
|
900
|
+
this.dispatch(EventNames$1.SUPPRESS_STATIONARY_NOISE, {});
|
|
701
901
|
}
|
|
702
|
-
if (this._config.suppressNonStationaryNoise)
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
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
|
+
}
|
|
708
911
|
}
|
|
709
|
-
var _this__config_audioMutedDefault, _this__config_videoConfig_videoOnDefault, _this__config_isAutoSubscribeAudio;
|
|
710
912
|
// Step4 join room
|
|
711
913
|
await this._client.joinRoom({
|
|
712
914
|
token: roomInfo.token,
|
|
713
915
|
roomId: roomInfo.room_id,
|
|
714
916
|
uid: roomInfo.uid,
|
|
715
|
-
audioMutedDefault:
|
|
716
|
-
videoOnDefault:
|
|
717
|
-
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,
|
|
718
920
|
});
|
|
719
921
|
// Step5 create local stream
|
|
720
922
|
await this._client.createLocalStream(roomInfo.uid, this._config.videoConfig);
|
|
721
923
|
// step6 set connected and dispatch connected event
|
|
722
924
|
this.isConnected = true;
|
|
723
|
-
this.dispatch(
|
|
925
|
+
this.dispatch(EventNames$1.CONNECTED, {
|
|
724
926
|
roomId: roomInfo.room_id,
|
|
725
927
|
uid: roomInfo.uid,
|
|
726
928
|
token: roomInfo.token,
|
|
727
|
-
appId: roomInfo.app_id
|
|
929
|
+
appId: roomInfo.app_id,
|
|
728
930
|
});
|
|
729
931
|
}
|
|
730
932
|
/**
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
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, {});
|
|
738
941
|
}
|
|
739
942
|
/**
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
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());
|
|
746
950
|
this.isConnected = false;
|
|
747
951
|
this._client = null;
|
|
748
|
-
this.dispatch(
|
|
952
|
+
this.dispatch(EventNames$1.DISCONNECTED, {});
|
|
749
953
|
}
|
|
750
954
|
/**
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
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';
|
|
758
965
|
this.dispatch(`client.${eventType}`, message);
|
|
759
966
|
}
|
|
760
967
|
/**
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
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
|
+
}
|
|
769
981
|
}
|
|
770
982
|
async setVideoEnable(isEnable) {
|
|
983
|
+
var _a;
|
|
771
984
|
try {
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
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);
|
|
777
990
|
throw e;
|
|
778
991
|
}
|
|
779
992
|
}
|
|
780
993
|
/**
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
994
|
+
* en: Enable audio properties reporting (debug mode only)
|
|
995
|
+
*
|
|
996
|
+
* zh: 启用音频属性报告(仅限调试模式)
|
|
997
|
+
*/
|
|
998
|
+
enableAudioPropertiesReport(config) {
|
|
999
|
+
var _a;
|
|
785
1000
|
if (this._config.debug) {
|
|
786
|
-
|
|
787
|
-
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);
|
|
788
1002
|
return true;
|
|
789
1003
|
}
|
|
790
|
-
|
|
791
|
-
|
|
1004
|
+
else {
|
|
1005
|
+
console.warn('enableAudioPropertiesReport is not supported in non-debug mode');
|
|
1006
|
+
return false;
|
|
1007
|
+
}
|
|
792
1008
|
}
|
|
793
1009
|
/**
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
1010
|
+
* en: Start audio playback device test (debug mode only)
|
|
1011
|
+
*
|
|
1012
|
+
* zh: 开始音频播放设备测试(仅限调试模式)
|
|
1013
|
+
*/
|
|
1014
|
+
async startAudioPlaybackDeviceTest() {
|
|
1015
|
+
var _a;
|
|
798
1016
|
if (this._config.debug) {
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
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
|
+
}
|
|
802
1022
|
}
|
|
803
1023
|
/**
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
1024
|
+
* en: Stop audio playback device test (debug mode only)
|
|
1025
|
+
*
|
|
1026
|
+
* zh: 停止音频播放设备测试(仅限调试模式)
|
|
1027
|
+
*/
|
|
1028
|
+
stopAudioPlaybackDeviceTest() {
|
|
1029
|
+
var _a;
|
|
808
1030
|
if (this._config.debug) {
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
* en: Set the audio input device
|
|
815
|
-
*
|
|
816
|
-
* zh: 设置音频输入设备
|
|
817
|
-
*/ async setAudioInputDevice(deviceId) {
|
|
818
|
-
var _this__client;
|
|
819
|
-
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioInputDevice(deviceId));
|
|
820
|
-
this.dispatch(event_names.AUDIO_INPUT_DEVICE_CHANGED, {
|
|
821
|
-
deviceId
|
|
822
|
-
});
|
|
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
|
+
}
|
|
823
1036
|
}
|
|
824
1037
|
/**
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
this.
|
|
832
|
-
|
|
833
|
-
|
|
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 });
|
|
834
1056
|
}
|
|
835
1057
|
/**
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
1058
|
+
* en: Set the video input device
|
|
1059
|
+
*
|
|
1060
|
+
* zh: 设置视频输入设备
|
|
1061
|
+
*/
|
|
1062
|
+
async setVideoInputDevice(deviceId) {
|
|
1063
|
+
var _a;
|
|
840
1064
|
try {
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
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);
|
|
846
1070
|
throw e;
|
|
847
1071
|
}
|
|
848
|
-
this.dispatch(
|
|
849
|
-
deviceId
|
|
850
|
-
});
|
|
1072
|
+
this.dispatch(EventNames$1.VIDEO_INPUT_DEVICE_CHANGED, { deviceId });
|
|
851
1073
|
}
|
|
852
1074
|
/**
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
/**
|
|
861
|
-
* Constructor for initializing a RealtimeClient instance.
|
|
862
|
-
*
|
|
863
|
-
* 构造函数,初始化RealtimeClient实例。
|
|
864
|
-
*
|
|
865
|
-
* @param config
|
|
866
|
-
* @param config.accessToken - Required, Access Token. |
|
|
867
|
-
* 必填,Access Token。
|
|
868
|
-
* @param config.botId - Required, Bot Id. |
|
|
869
|
-
* 必填,Bot Id。
|
|
870
|
-
* @param config.voiceId - Optional, Voice Id. |
|
|
871
|
-
* 可选,音色Id。
|
|
872
|
-
* @param config.conversationId - Optional, Conversation Id. |
|
|
873
|
-
* 可选,会话Id。
|
|
874
|
-
* @param config.userId - Optional, User Id. |
|
|
875
|
-
* 可选,用户Id。
|
|
876
|
-
* @param config.baseURL - Optional, defaults to "https://api.coze.cn". |
|
|
877
|
-
* 可选,默认值为 "https://api.coze.cn"。
|
|
878
|
-
* @param config.debug - Optional, defaults to false.
|
|
879
|
-
* 可选,默认值为 false。
|
|
880
|
-
* @param config.allowPersonalAccessTokenInBrowser
|
|
881
|
-
* - Optional, whether to allow personal access tokens in browser environment. |
|
|
882
|
-
* 可选,是否允许在浏览器环境中使用个人访问令牌。
|
|
883
|
-
* @param config.audioMutedDefault - Optional, whether audio is muted by default, defaults to false. |
|
|
884
|
-
* 可选,默认是否静音,默认值为 false。
|
|
885
|
-
* @param config.connectorId - Required, Connector Id. |
|
|
886
|
-
* 必填,渠道 Id。
|
|
887
|
-
* @param config.suppressStationaryNoise - Optional, suppress stationary noise, defaults to false. |
|
|
888
|
-
* 可选,默认是否抑制静态噪声,默认值为 false。
|
|
889
|
-
* @param config.suppressNonStationaryNoise - Optional, suppress non-stationary noise, defaults to false. |
|
|
890
|
-
* 可选,默认是否抑制非静态噪声,默认值为 false。
|
|
891
|
-
* @param config.isAutoSubscribeAudio - Optional, whether to automatically subscribe to bot reply audio streams, defaults to true. |
|
|
892
|
-
* @param config.videoConfig - Optional, Video configuration. |
|
|
893
|
-
* 可选,视频配置。
|
|
894
|
-
* @param config.videoConfig.videoOnDefault - Optional, Whether to turn on video by default, defaults to true. |
|
|
895
|
-
* 可选,默认是否开启视频,默认值为 true。
|
|
896
|
-
* @param config.videoConfig.renderDom - Optional, The DOM element to render the video stream to. |
|
|
897
|
-
* 可选,渲染视频流的 DOM 元素。
|
|
898
|
-
* @param config.videoConfig.videoInputDeviceId - Optional, The device ID of the video input device to use. |
|
|
899
|
-
* 可选,视频输入设备的设备 ID。
|
|
900
|
-
* @param config.videoConfig.screenConfig - Optional, Screen share configuration if videoInputDeviceId is 'screenShare' see https://www.volcengine.com/docs/6348/104481#screenconfig for more details. |
|
|
901
|
-
* 可选,屏幕共享配置,如果 videoInputDeviceId 是 'screenShare',请参考 https://www.volcengine.com/docs/6348/104481#screenconfig 了解更多详情。
|
|
902
|
-
* @param config.prologueContent - Optional, Prologue content. | 可选,开场白内容。
|
|
903
|
-
* @param config.roomMode - Optional, Room mode. | 可选,房间模式。
|
|
904
|
-
*/ constructor(config){
|
|
905
|
-
super(config.debug), this._client = null, this.isConnected = false, this._isTestEnv = false, this._isSupportVideo = false;
|
|
906
|
-
this._config = config;
|
|
907
|
-
var _this__config_baseURL;
|
|
908
|
-
const defaultBaseURL = null !== (_this__config_baseURL = this._config.baseURL) && void 0 !== _this__config_baseURL ? _this__config_baseURL : 'https://api.coze.cn';
|
|
909
|
-
this._config.baseURL = defaultBaseURL;
|
|
910
|
-
// init api
|
|
911
|
-
this._api = new __WEBPACK_EXTERNAL_MODULE__coze_api__.CozeAPI({
|
|
912
|
-
token: this._config.accessToken,
|
|
913
|
-
baseURL: defaultBaseURL,
|
|
914
|
-
allowPersonalAccessTokenInBrowser: this._config.allowPersonalAccessTokenInBrowser
|
|
915
|
-
});
|
|
916
|
-
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();
|
|
917
1082
|
}
|
|
918
1083
|
}
|
|
919
|
-
|
|
1084
|
+
|
|
1085
|
+
export { EventNames$1 as EventNames, RealtimeAPIError, RealtimeClient, RealtimeError, utils as RealtimeUtils };
|