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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,253 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ WebLiveClient: ()=>WebLiveClient,
28
+ ResourceStatus: ()=>live_ResourceStatus
29
+ });
30
+ const api_namespaceObject = require("@coze/api");
31
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
32
+ try {
33
+ var info = gen[key](arg);
34
+ var value = info.value;
35
+ } catch (error) {
36
+ reject(error);
37
+ return;
38
+ }
39
+ if (info.done) resolve(value);
40
+ else Promise.resolve(value).then(_next, _throw);
41
+ }
42
+ function _async_to_generator(fn) {
43
+ return function() {
44
+ var self = this, args = arguments;
45
+ return new Promise(function(resolve, reject) {
46
+ var gen = fn.apply(self, args);
47
+ function _next(value) {
48
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
49
+ }
50
+ function _throw(err) {
51
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
52
+ }
53
+ _next(void 0);
54
+ });
55
+ };
56
+ }
57
+ const WTN_BASE_URL = 'https://wtn.volcvideo.com';
58
+ var live_ResourceStatus = /*#__PURE__*/ function(ResourceStatus) {
59
+ ResourceStatus["IDLE"] = "idle";
60
+ ResourceStatus["CONNECTING"] = "connecting";
61
+ ResourceStatus["CONNECTED"] = "connected";
62
+ ResourceStatus["FAILED"] = "failed";
63
+ ResourceStatus["CLOSING"] = "closing";
64
+ ResourceStatus["CLOSED"] = "closed";
65
+ return ResourceStatus;
66
+ }({});
67
+ class WebLiveClient {
68
+ getStatus() {
69
+ return this.status;
70
+ }
71
+ onStatusChange(callback) {
72
+ this.statusListeners.push(callback);
73
+ }
74
+ offStatusChange(callback) {
75
+ this.removeStatusListener(callback);
76
+ }
77
+ removeStatusListener(callback) {
78
+ const index = this.statusListeners.indexOf(callback);
79
+ if (-1 !== index) this.statusListeners.splice(index, 1);
80
+ }
81
+ subscribe(appId, streamId, clientId = '') {
82
+ return _async_to_generator(function*() {
83
+ try {
84
+ var _pc_localDescription;
85
+ if (this.peerConnection) {
86
+ this.peerConnection.close();
87
+ this.peerConnection = null;
88
+ }
89
+ this.setStatus("connecting");
90
+ const rtcConfig = {};
91
+ const pc = new RTCPeerConnection(rtcConfig);
92
+ pc.ontrack = (event)=>{
93
+ this.player.onloadeddata = ()=>{
94
+ this.player.play();
95
+ };
96
+ this.player.srcObject = event.streams[0];
97
+ };
98
+ this.peerConnection = pc;
99
+ this.setupPeerConnectionListeners();
100
+ pc.addTransceiver('audio', {
101
+ direction: 'recvonly'
102
+ });
103
+ const offer = yield pc.createOffer();
104
+ yield pc.setLocalDescription(offer);
105
+ yield this.waitForIceGathering(pc);
106
+ if (!(null == (_pc_localDescription = pc.localDescription) ? void 0 : _pc_localDescription.sdp)) throw new Error('Failed to create SDP offer');
107
+ let subscribeUrl = `${WTN_BASE_URL}/sub/${appId}/${streamId}?MuteVideo=true`;
108
+ if (clientId) subscribeUrl += `&clientid=${clientId}`;
109
+ const response = yield fetch(subscribeUrl, {
110
+ method: 'POST',
111
+ headers: {
112
+ 'Content-Type': 'application/sdp'
113
+ },
114
+ body: offer.sdp
115
+ });
116
+ if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
117
+ this.resourceUrl = response.headers.get('location') || '';
118
+ const answerSdp = yield response.text();
119
+ const answer = new RTCSessionDescription({
120
+ type: 'answer',
121
+ sdp: answerSdp
122
+ });
123
+ yield this.peerConnection.setRemoteDescription(answer);
124
+ return {
125
+ status: this.status,
126
+ peerConnection: this.peerConnection
127
+ };
128
+ } catch (error) {
129
+ this.status = "failed";
130
+ console.error('Failed to subscribe WebRTC stream:', error);
131
+ return Promise.reject(error);
132
+ }
133
+ }).call(this);
134
+ }
135
+ unsubscribe() {
136
+ return _async_to_generator(function*() {
137
+ try {
138
+ if (!this.resourceUrl) throw new Error("No valid subscription resource URL to unsubscribe");
139
+ this.setStatus("closing");
140
+ const response = yield fetch(this.resourceUrl, {
141
+ method: 'DELETE'
142
+ });
143
+ if (!response.ok) throw new Error(`Failed to unsubscribe: ${response.status} ${response.statusText}`);
144
+ if (this.peerConnection) {
145
+ this.peerConnection.close();
146
+ this.peerConnection = null;
147
+ }
148
+ this.resourceUrl = '';
149
+ this.status = "closed";
150
+ return true;
151
+ } catch (error) {
152
+ console.error('Error unsubscribing resource:', error);
153
+ this.status = "failed";
154
+ return Promise.reject(error);
155
+ }
156
+ }).call(this);
157
+ }
158
+ setMuted(muted) {
159
+ this.player.muted = muted;
160
+ }
161
+ close() {
162
+ this.closePeerConnection();
163
+ if (this.player) {
164
+ this.player.pause();
165
+ this.player.srcObject = null;
166
+ this.player.remove();
167
+ }
168
+ this.resourceUrl = '';
169
+ this.setStatus("idle");
170
+ }
171
+ waitForIceGathering(pc) {
172
+ return new Promise((resolve)=>{
173
+ if ('complete' === pc.iceGatheringState) return void resolve();
174
+ const checkState = ()=>{
175
+ if ('complete' === pc.iceGatheringState) {
176
+ pc.removeEventListener('icegatheringstatechange', checkState);
177
+ resolve();
178
+ }
179
+ };
180
+ pc.addEventListener('icegatheringstatechange', checkState);
181
+ setTimeout(()=>resolve(), 5000);
182
+ });
183
+ }
184
+ setupPeerConnectionListeners() {
185
+ if (!this.peerConnection) return;
186
+ this.peerConnection.oniceconnectionstatechange = ()=>{
187
+ var _this_peerConnection, _this_peerConnection1;
188
+ console.log('ICE connection state changed:', null == (_this_peerConnection = this.peerConnection) ? void 0 : _this_peerConnection.iceConnectionState);
189
+ switch(null == (_this_peerConnection1 = this.peerConnection) ? void 0 : _this_peerConnection1.iceConnectionState){
190
+ case 'connected':
191
+ case 'completed':
192
+ this.setStatus("connected");
193
+ break;
194
+ case 'failed':
195
+ case 'disconnected':
196
+ this.setStatus("failed");
197
+ break;
198
+ case 'closed':
199
+ this.setStatus("closed");
200
+ break;
201
+ default:
202
+ var _this_peerConnection2;
203
+ console.log('ICE connection state changed:', null == (_this_peerConnection2 = this.peerConnection) ? void 0 : _this_peerConnection2.iceConnectionState);
204
+ break;
205
+ }
206
+ };
207
+ this.peerConnection.onicecandidate = (event)=>{
208
+ if (event.candidate) console.log('New ICE candidate:', event.candidate);
209
+ };
210
+ }
211
+ closePeerConnection() {
212
+ if (this.peerConnection) {
213
+ this.peerConnection.close();
214
+ this.peerConnection = null;
215
+ }
216
+ }
217
+ setStatus(newStatus) {
218
+ const oldStatus = this.status;
219
+ if (oldStatus !== newStatus) {
220
+ this.status = newStatus;
221
+ for (const listener of this.statusListeners)try {
222
+ listener(newStatus);
223
+ } catch (error) {
224
+ console.error('Error in status listener:', error);
225
+ }
226
+ }
227
+ }
228
+ constructor(liveId){
229
+ this.peerConnection = null;
230
+ this.resourceUrl = '';
231
+ this.status = "idle";
232
+ this.statusListeners = [];
233
+ this.getLiveData = ()=>_async_to_generator(function*() {
234
+ const api = new api_namespaceObject.CozeAPI({
235
+ baseURL: api_namespaceObject.COZE_CN_BASE_URL,
236
+ token: ''
237
+ });
238
+ return yield api.audio.live.retrieve(this.liveId);
239
+ }).call(this);
240
+ this.setupPeerConnectionListeners();
241
+ this.player = document.createElement('audio');
242
+ this.liveId = liveId;
243
+ }
244
+ }
245
+ exports.ResourceStatus = __webpack_exports__.ResourceStatus;
246
+ exports.WebLiveClient = __webpack_exports__.WebLiveClient;
247
+ for(var __webpack_i__ in __webpack_exports__)if (-1 === [
248
+ "ResourceStatus",
249
+ "WebLiveClient"
250
+ ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
251
+ Object.defineProperty(exports, '__esModule', {
252
+ value: true
253
+ });
@@ -1,169 +1,47 @@
1
1
  var event_names_EventNames = /*#__PURE__*/ function(EventNames) {
2
- /**
3
- * en: All events
4
- * zh: 所有事件
5
- */ EventNames["ALL"] = "realtime.event";
6
- /**
7
- * en: All client events
8
- * zh: 所有客户端事件
9
- */ EventNames["ALL_CLIENT"] = "client.*";
10
- /**
11
- * en: All server events
12
- * zh: 所有服务端事件
13
- */ EventNames["ALL_SERVER"] = "server.*";
14
- /**
15
- * en: Room info
16
- * zh: 房间信息
17
- */ EventNames["ROOM_INFO"] = "client.room.info";
18
- /**
19
- * en: Client connected
20
- * zh: 客户端连接
21
- */ EventNames["CONNECTED"] = "client.connected";
22
- /**
23
- * en: Client connecting
24
- * zh: 客户端连接中
25
- */ EventNames["CONNECTING"] = "client.connecting";
26
- /**
27
- * en: Client interrupted
28
- * zh: 客户端中断
29
- */ EventNames["INTERRUPTED"] = "client.interrupted";
30
- /**
31
- * en: Client disconnected
32
- * zh: 客户端断开
33
- */ EventNames["DISCONNECTED"] = "client.disconnected";
34
- /**
35
- * en: Client audio unmuted
36
- * zh: 客户端音频未静音
37
- */ EventNames["AUDIO_UNMUTED"] = "client.audio.unmuted";
38
- /**
39
- * en: Client audio muted
40
- * zh: 客户端音频静音
41
- */ EventNames["AUDIO_MUTED"] = "client.audio.muted";
42
- /**
43
- * en: Client video on
44
- * zh: 客户端视频开启
45
- */ EventNames["VIDEO_ON"] = "client.video.on";
46
- /**
47
- * en: Client video off
48
- * zh: 客户端视频关闭
49
- */ EventNames["VIDEO_OFF"] = "client.video.off";
50
- /**
51
- * en: Client video error
52
- * zh: 客户端视频(或屏幕共享)错误
53
- */ EventNames["VIDEO_ERROR"] = "client.video.error";
54
- /**
55
- * en: Client video event
56
- * zh: 客户端视频事件
57
- */ EventNames["PLAYER_EVENT"] = "client.video.event";
58
- /**
59
- * en: Client error
60
- * zh: 客户端错误
61
- */ EventNames["ERROR"] = "client.error";
62
- /**
63
- * en: Audio noise reduction enabled
64
- * zh: 抑制平稳噪声
65
- */ EventNames["SUPPRESS_STATIONARY_NOISE"] = "client.suppress.stationary.noise";
66
- /**
67
- * en: Suppress non-stationary noise
68
- * zh: 抑制非平稳噪声
69
- */ EventNames["SUPPRESS_NON_STATIONARY_NOISE"] = "client.suppress.non.stationary.noise";
70
- /**
71
- * en: Audio input device changed
72
- * zh: 音频输入设备改变
73
- */ EventNames["AUDIO_INPUT_DEVICE_CHANGED"] = "client.input.device.changed";
74
- /**
75
- * en: Audio output device changed
76
- * zh: 音频输出设备改变
77
- */ EventNames["AUDIO_OUTPUT_DEVICE_CHANGED"] = "client.output.device.changed";
78
- /**
79
- * en: Video input device changed
80
- * zh: 视频输入设备改变
81
- */ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
82
- /**
83
- * en: Network quality changed
84
- * zh: 网络质量改变
85
- */ EventNames["NETWORK_QUALITY"] = "client.network.quality";
86
- /**
87
- * en: Bot joined
88
- * zh: Bot 加入
89
- */ EventNames["BOT_JOIN"] = "server.bot.join";
90
- /**
91
- * en: Bot left
92
- * zh: Bot 离开
93
- */ EventNames["BOT_LEAVE"] = "server.bot.leave";
94
- /**
95
- * en: Audio speech started
96
- * zh: 开始说话
97
- */ EventNames["AUDIO_AGENT_SPEECH_STARTED"] = "server.audio.agent.speech_started";
98
- /**
99
- * en: Audio speech stopped
100
- * zh: 停止说话
101
- */ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
102
- /**
103
- * en: Server error
104
- * zh: 服务端错误
105
- */ EventNames["SERVER_ERROR"] = "server.error";
106
- /**
107
- * en: User speech started
108
- * zh: 用户开始说话
109
- */ EventNames["AUDIO_USER_SPEECH_STARTED"] = "server.audio.user.speech_started";
110
- /**
111
- * en: User speech stopped
112
- * zh: 用户停止说话
113
- */ EventNames["AUDIO_USER_SPEECH_STOPPED"] = "server.audio.user.speech_stopped";
114
- /**
115
- * en: User successfully enters the room
116
- * zh: 用户成功进入房间后,会收到该事件
117
- */ EventNames["SESSION_CREATED"] = "server.session.created";
118
- /**
119
- * en: Session updated
120
- * zh: 会话更新
121
- */ EventNames["SESSION_UPDATED"] = "server.session.updated";
122
- /**
123
- * en: Conversation created
124
- * zh: 会话创建
125
- */ EventNames["CONVERSATION_CREATED"] = "server.conversation.created";
126
- /**
127
- * en: Conversation chat created
128
- * zh: 会话对话创建
129
- */ EventNames["CONVERSATION_CHAT_CREATED"] = "server.conversation.chat.created";
130
- /**
131
- * en: Conversation chat in progress
132
- * zh: 对话正在处理中
133
- */ EventNames["CONVERSATION_CHAT_IN_PROGRESS"] = "server.conversation.chat.in_progress";
134
- /**
135
- * en: Conversation message delta received
136
- * zh: 文本消息增量返回
137
- */ EventNames["CONVERSATION_MESSAGE_DELTA"] = "server.conversation.message.delta";
138
- /**
139
- * en: Conversation message completed
140
- * zh: 文本消息完成
141
- */ EventNames["CONVERSATION_MESSAGE_COMPLETED"] = "server.conversation.message.completed";
142
- /**
143
- * en: Conversation chat completed
144
- * zh: 对话完成
145
- */ EventNames["CONVERSATION_CHAT_COMPLETED"] = "server.conversation.chat.completed";
146
- /**
147
- * en: Conversation chat requires action
148
- * zh: 对话需要插件
149
- */ EventNames["CONVERSATION_CHAT_REQUIRES_ACTION"] = "server.conversation.chat.requires_action";
150
- /**
151
- * en: Conversation chat failed
152
- * zh: 对话失败
153
- */ EventNames["CONVERSATION_CHAT_FAILED"] = "server.conversation.chat.failed";
154
- /**
155
- * en: Session pre answer updated
156
- * zh: 安抚配置更新成功
157
- */ EventNames["SESSION_PRE_ANSWER_UPDATED"] = "server.session.pre_answer.updated";
158
- /**
159
- * en: Conversation audio transcript delta
160
- * zh: 用户语音识别字幕
161
- */ EventNames["CONVERSATION_AUDIO_TRANSCRIPT_DELTA"] = "server.conversation.audio_transcript.delta";
162
- /**
163
- * en: Mode updated
164
- * zh: 更新房间模式成功
165
- */ EventNames["MODE_UPDATED"] = "server.mode.updated";
2
+ EventNames["ALL"] = "realtime.event";
3
+ EventNames["ALL_CLIENT"] = "client.*";
4
+ EventNames["ALL_SERVER"] = "server.*";
5
+ EventNames["ROOM_INFO"] = "client.room.info";
6
+ EventNames["CONNECTED"] = "client.connected";
7
+ EventNames["CONNECTING"] = "client.connecting";
8
+ EventNames["INTERRUPTED"] = "client.interrupted";
9
+ EventNames["DISCONNECTED"] = "client.disconnected";
10
+ EventNames["AUDIO_UNMUTED"] = "client.audio.unmuted";
11
+ EventNames["AUDIO_MUTED"] = "client.audio.muted";
12
+ EventNames["VIDEO_ON"] = "client.video.on";
13
+ EventNames["VIDEO_OFF"] = "client.video.off";
14
+ EventNames["VIDEO_ERROR"] = "client.video.error";
15
+ EventNames["PLAYER_EVENT"] = "client.video.event";
16
+ EventNames["ERROR"] = "client.error";
17
+ EventNames["SUPPRESS_STATIONARY_NOISE"] = "client.suppress.stationary.noise";
18
+ EventNames["SUPPRESS_NON_STATIONARY_NOISE"] = "client.suppress.non.stationary.noise";
19
+ EventNames["AUDIO_INPUT_DEVICE_CHANGED"] = "client.input.device.changed";
20
+ EventNames["AUDIO_OUTPUT_DEVICE_CHANGED"] = "client.output.device.changed";
21
+ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
22
+ EventNames["NETWORK_QUALITY"] = "client.network.quality";
23
+ EventNames["BOT_JOIN"] = "server.bot.join";
24
+ EventNames["BOT_LEAVE"] = "server.bot.leave";
25
+ EventNames["AUDIO_AGENT_SPEECH_STARTED"] = "server.audio.agent.speech_started";
26
+ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
27
+ EventNames["SERVER_ERROR"] = "server.error";
28
+ EventNames["AUDIO_USER_SPEECH_STARTED"] = "server.audio.user.speech_started";
29
+ EventNames["AUDIO_USER_SPEECH_STOPPED"] = "server.audio.user.speech_stopped";
30
+ EventNames["SESSION_CREATED"] = "server.session.created";
31
+ EventNames["SESSION_UPDATED"] = "server.session.updated";
32
+ EventNames["CONVERSATION_CREATED"] = "server.conversation.created";
33
+ EventNames["CONVERSATION_CHAT_CREATED"] = "server.conversation.chat.created";
34
+ EventNames["CONVERSATION_CHAT_IN_PROGRESS"] = "server.conversation.chat.in_progress";
35
+ EventNames["CONVERSATION_MESSAGE_DELTA"] = "server.conversation.message.delta";
36
+ EventNames["CONVERSATION_MESSAGE_COMPLETED"] = "server.conversation.message.completed";
37
+ EventNames["CONVERSATION_CHAT_COMPLETED"] = "server.conversation.chat.completed";
38
+ EventNames["CONVERSATION_CHAT_REQUIRES_ACTION"] = "server.conversation.chat.requires_action";
39
+ EventNames["CONVERSATION_CHAT_FAILED"] = "server.conversation.chat.failed";
40
+ EventNames["SESSION_PRE_ANSWER_UPDATED"] = "server.session.pre_answer.updated";
41
+ EventNames["CONVERSATION_AUDIO_TRANSCRIPT_DELTA"] = "server.conversation.audio_transcript.delta";
42
+ EventNames["MODE_UPDATED"] = "server.mode.updated";
43
+ EventNames["LIVE_CREATED"] = "server.live.created";
166
44
  return EventNames;
167
45
  }(event_names_EventNames || {});
168
- /* ESM default export */ const event_names = event_names_EventNames;
46
+ const event_names = event_names_EventNames;
169
47
  export { event_names as default };