@coze/realtime-api 0.0.3 → 0.0.4

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 @@
1
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@coze/realtime-api",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Coze Realtime API",
5
5
  "homepage": "https://github.com/coze-dev/coze-js/packages/realtime-api",
6
6
  "repository": {
@@ -12,10 +12,15 @@
12
12
  "author": "jackshen <jackshen310@gmail.com>",
13
13
  "type": "module",
14
14
  "exports": {
15
- ".": "./dist/index.js"
15
+ ".": {
16
+ "require": "./dist/cjs/index.cjs",
17
+ "import": "./dist/esm/index.js",
18
+ "types": "./dist/types/index.d.ts"
19
+ }
16
20
  },
17
- "main": "dist/index.js",
18
- "types": "dist/index.d.ts",
21
+ "main": "dist/cjs/index.cjs",
22
+ "module": "dist/esm/index.js",
23
+ "typings": "dist/types/index.d.ts",
19
24
  "files": [
20
25
  "dist",
21
26
  "assets",
@@ -24,24 +29,25 @@
24
29
  "README.md"
25
30
  ],
26
31
  "scripts": {
27
- "build": "rm -rf dist && tsc -b tsconfig.build.json",
28
- "buildAll": "npm run build && cd examples/realtime-console && npm run build",
29
- "demo": "npm run build && cd examples/realtime-console && npm run start",
32
+ "build": "rm -rf ./dist && rslib build",
33
+ "buildAll": "npm run build && cd ../../examples/realtime-console && npm run build",
34
+ "demo": "npm run start & cd ../../examples/realtime-console && npm run start",
30
35
  "format": "prettier --write .",
31
36
  "lint": "eslint ./ --cache --quiet",
32
37
  "prepublishOnly": "npm run build",
33
- "start": "rm -rf dist && tsc -b -w tsconfig.build.json",
38
+ "start": "rm -rf dist && rslib build -w",
34
39
  "test": "vitest",
35
40
  "test:cov": "vitest --coverage --run"
36
41
  },
37
42
  "dependencies": {
38
- "@coze/api": "1.0.5",
43
+ "@coze/api": "1.0.8",
39
44
  "@volcengine/rtc": "^4.62.1"
40
45
  },
41
46
  "devDependencies": {
42
47
  "@coze-infra/eslint-config": "workspace:*",
43
48
  "@coze-infra/ts-config": "workspace:*",
44
49
  "@coze-infra/vitest-config": "workspace:*",
50
+ "@rslib/core": "0.0.18",
45
51
  "@swc/core": "^1.3.14",
46
52
  "@types/node": "^20",
47
53
  "@types/uuid": "^9.0.1",
package/dist/client.js DELETED
@@ -1,242 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.EngineClient = void 0;
30
- const extension_ainr_1 = __importDefault(require("@volcengine/rtc/extension-ainr"));
31
- const rtc_1 = __importStar(require("@volcengine/rtc"));
32
- const utils_1 = require("./utils");
33
- const event_handler_1 = require("./event-handler");
34
- const error_1 = require("./error");
35
- class EngineClient extends event_handler_1.RealtimeEventHandler {
36
- constructor(appId, debug = false, isTestEnv = false) {
37
- super(debug);
38
- this.joinUserId = '';
39
- this._AIAnsExtension = null;
40
- if (isTestEnv) {
41
- rtc_1.default.setParameter('ICE_CONFIG_REQUEST_URLS', ['rtc-test.bytedance.com']);
42
- }
43
- this.engine = rtc_1.default.createEngine(appId);
44
- this.handleMessage = this.handleMessage.bind(this);
45
- this.handleUserJoin = this.handleUserJoin.bind(this);
46
- this.handleUserLeave = this.handleUserLeave.bind(this);
47
- this.handleEventError = this.handleEventError.bind(this);
48
- // Debug only
49
- this.handleLocalAudioPropertiesReport =
50
- this.handleLocalAudioPropertiesReport.bind(this);
51
- this.handleRemoteAudioPropertiesReport =
52
- this.handleRemoteAudioPropertiesReport.bind(this);
53
- }
54
- bindEngineEvents() {
55
- this.engine.on(rtc_1.default.events.onUserMessageReceived, this.handleMessage);
56
- this.engine.on(rtc_1.default.events.onUserJoined, this.handleUserJoin);
57
- this.engine.on(rtc_1.default.events.onUserLeave, this.handleUserLeave);
58
- this.engine.on(rtc_1.default.events.onError, this.handleEventError);
59
- if (this._debug) {
60
- this.engine.on(rtc_1.default.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
61
- this.engine.on(rtc_1.default.events.onRemoteAudioPropertiesReport, this.handleRemoteAudioPropertiesReport);
62
- }
63
- }
64
- removeEventListener() {
65
- this.engine.off(rtc_1.default.events.onUserMessageReceived, this.handleMessage);
66
- this.engine.off(rtc_1.default.events.onUserJoined, this.handleUserJoin);
67
- this.engine.off(rtc_1.default.events.onUserLeave, this.handleUserLeave);
68
- this.engine.off(rtc_1.default.events.onError, this.handleEventError);
69
- if (this._debug) {
70
- this.engine.off(rtc_1.default.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
71
- this.engine.off(rtc_1.default.events.onRemoteAudioPropertiesReport, this.handleRemoteAudioPropertiesReport);
72
- }
73
- }
74
- handleMessage(event) {
75
- try {
76
- const message = JSON.parse(event.message);
77
- this.dispatch(`server.${message.event_type}`, message);
78
- }
79
- catch (e) {
80
- this.dispatch('client.error', {
81
- message: `Failed to parse message: ${event.message}`,
82
- error: e,
83
- });
84
- }
85
- }
86
- handleEventError(e) {
87
- this.dispatch('client.error', e);
88
- }
89
- handleUserJoin(event) {
90
- this.joinUserId = event.userInfo.userId;
91
- this.dispatch('server.bot.join', event);
92
- }
93
- handleUserLeave(event) {
94
- this.dispatch('server.bot.leave', event);
95
- }
96
- async joinRoom(options) {
97
- const { token, roomId, uid, audioMutedDefault = false } = options;
98
- try {
99
- await this.engine.joinRoom(token, roomId, {
100
- userId: uid,
101
- }, {
102
- isAutoPublish: !audioMutedDefault,
103
- isAutoSubscribeAudio: true,
104
- isAutoSubscribeVideo: false,
105
- });
106
- }
107
- catch (e) {
108
- if (e instanceof Error) {
109
- throw new error_1.RealtimeAPIError(error_1.RealtimeError.CONNECTION_ERROR, e.message);
110
- }
111
- throw new error_1.RealtimeAPIError(error_1.RealtimeError.CONNECTION_ERROR, 'Unknown error');
112
- }
113
- }
114
- async setAudioInputDevice(deviceId) {
115
- const devices = await (0, utils_1.getAudioDevices)();
116
- if (devices.audioInputs.findIndex(i => i.deviceId === deviceId) === -1) {
117
- throw new error_1.RealtimeAPIError(error_1.RealtimeError.DEVICE_ACCESS_ERROR, `Audio input device not found: ${deviceId}`);
118
- }
119
- this.engine.stopAudioCapture();
120
- await this.engine.startAudioCapture(deviceId);
121
- }
122
- async setAudioOutputDevice(deviceId) {
123
- const devices = await (0, utils_1.getAudioDevices)();
124
- if (devices.audioOutputs.findIndex(i => i.deviceId === deviceId) === -1) {
125
- throw new error_1.RealtimeAPIError(error_1.RealtimeError.DEVICE_ACCESS_ERROR, `Audio output device not found: ${deviceId}`);
126
- }
127
- await this.engine.setAudioPlaybackDevice(deviceId);
128
- }
129
- async createLocalStream() {
130
- const devices = await (0, utils_1.getAudioDevices)();
131
- if (!devices.audioInputs.length) {
132
- throw new error_1.RealtimeAPIError(error_1.RealtimeError.DEVICE_ACCESS_ERROR, 'Failed to get devices');
133
- }
134
- await this.engine.startAudioCapture(devices.audioInputs[0].deviceId);
135
- }
136
- async disconnect() {
137
- try {
138
- await this.engine.stopAudioCapture();
139
- await this.engine.unpublishStream(rtc_1.MediaType.AUDIO);
140
- await this.engine.leaveRoom();
141
- this.removeEventListener();
142
- }
143
- catch (e) {
144
- this.dispatch('client.error', e);
145
- throw e;
146
- }
147
- }
148
- async changeAudioState(isMicOn) {
149
- try {
150
- if (isMicOn) {
151
- await this.engine.publishStream(rtc_1.MediaType.AUDIO);
152
- }
153
- else {
154
- await this.engine.unpublishStream(rtc_1.MediaType.AUDIO);
155
- }
156
- }
157
- catch (e) {
158
- this.dispatch('client.error', e);
159
- throw e;
160
- }
161
- }
162
- async stop() {
163
- try {
164
- const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify({
165
- id: 'event_1',
166
- event_type: 'conversation.chat.cancel',
167
- data: {},
168
- }));
169
- this._log(`interrupt ${this.joinUserId} ${result}`);
170
- }
171
- catch (e) {
172
- this.dispatch('client.error', e);
173
- throw e;
174
- }
175
- }
176
- async sendMessage(message) {
177
- try {
178
- const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify(message));
179
- this._log(`sendMessage ${this.joinUserId} ${JSON.stringify(message)} ${result}`);
180
- }
181
- catch (e) {
182
- this.dispatch('client.error', e);
183
- throw e;
184
- }
185
- }
186
- enableAudioPropertiesReport(config) {
187
- this.engine.enableAudioPropertiesReport(config);
188
- }
189
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
190
- handleLocalAudioPropertiesReport(event) {
191
- var _a, _b;
192
- if (this._debug && ((_b = (_a = event[0]) === null || _a === void 0 ? void 0 : _a.audioPropertiesInfo) === null || _b === void 0 ? void 0 : _b.linearVolume) > 0) {
193
- console.log('handleLocalAudioPropertiesReport', event);
194
- }
195
- }
196
- handleRemoteAudioPropertiesReport(event) {
197
- if (this._debug) {
198
- console.log('handleRemoteAudioPropertiesReport', event);
199
- }
200
- }
201
- async enableAudioNoiseReduction() {
202
- var _a;
203
- await ((_a = this.engine) === null || _a === void 0 ? void 0 : _a.setAudioCaptureConfig({
204
- noiseSuppression: true,
205
- echoCancellation: true,
206
- autoGainControl: true,
207
- }));
208
- }
209
- async initAIAnsExtension() {
210
- const AIAnsExtension = new extension_ainr_1.default();
211
- await this.engine.registerExtension(AIAnsExtension);
212
- this._AIAnsExtension = AIAnsExtension;
213
- }
214
- changeAIAnsExtension(enable) {
215
- var _a, _b;
216
- if (enable) {
217
- (_a = this._AIAnsExtension) === null || _a === void 0 ? void 0 : _a.enable();
218
- }
219
- else {
220
- (_b = this._AIAnsExtension) === null || _b === void 0 ? void 0 : _b.disable();
221
- }
222
- }
223
- async startAudioPlaybackDeviceTest() {
224
- try {
225
- await this.engine.startAudioPlaybackDeviceTest('audio-test.wav', 200);
226
- }
227
- catch (e) {
228
- this.dispatch('client.error', e);
229
- throw e;
230
- }
231
- }
232
- stopAudioPlaybackDeviceTest() {
233
- try {
234
- this.engine.stopAudioPlaybackDeviceTest();
235
- }
236
- catch (e) {
237
- this.dispatch('client.error', e);
238
- throw e;
239
- }
240
- }
241
- }
242
- exports.EngineClient = EngineClient;
package/dist/error.js DELETED
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RealtimeAPIError = exports.ErrorMessages = exports.RealtimeError = void 0;
4
- var RealtimeError;
5
- (function (RealtimeError) {
6
- RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
7
- RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
8
- RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
9
- RealtimeError["DISCONNECTION_ERROR"] = "DISCONNECTION_ERROR";
10
- RealtimeError["INTERRUPT_ERROR"] = "INTERRUPT_ERROR";
11
- RealtimeError["EVENT_HANDLER_ERROR"] = "EVENT_HANDLER_ERROR";
12
- RealtimeError["PERMISSION_DENIED"] = "PERMISSION_DENIED";
13
- RealtimeError["NETWORK_ERROR"] = "NETWORK_ERROR";
14
- RealtimeError["INVALID_STATE"] = "INVALID_STATE";
15
- RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
16
- })(RealtimeError || (exports.RealtimeError = RealtimeError = {}));
17
- exports.ErrorMessages = {
18
- [RealtimeError.DEVICE_ACCESS_ERROR]: 'Failed to get devices',
19
- [RealtimeError.STREAM_CREATION_ERROR]: 'Failed to create local stream',
20
- [RealtimeError.CONNECTION_ERROR]: 'Failed to connect',
21
- [RealtimeError.DISCONNECTION_ERROR]: 'Failed to disconnect',
22
- [RealtimeError.INTERRUPT_ERROR]: 'Failed to interrupt',
23
- [RealtimeError.EVENT_HANDLER_ERROR]: 'Event handler not found',
24
- [RealtimeError.PERMISSION_DENIED]: 'Permission denied for requested operation',
25
- [RealtimeError.NETWORK_ERROR]: 'Network connection error occurred',
26
- [RealtimeError.INVALID_STATE]: 'Operation invalid in current state',
27
- [RealtimeError.CREATE_ROOM_ERROR]: 'Failed to create room',
28
- };
29
- class RealtimeAPIError extends Error {
30
- constructor(code, message) {
31
- super(`[${code}] ${message}`);
32
- this.code = code;
33
- this.name = 'RealtimeAPIError';
34
- }
35
- }
36
- exports.RealtimeAPIError = RealtimeAPIError;
@@ -1,126 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RealtimeEventHandler = exports.EventNames = void 0;
4
- const error_1 = require("./error");
5
- var EventNames;
6
- (function (EventNames) {
7
- /**
8
- * en: All events
9
- * zh: 所有事件
10
- */
11
- EventNames["ALL"] = "realtime.event";
12
- /**
13
- * en: All client events
14
- * zh: 所有客户端事件
15
- */
16
- EventNames["ALL_CLIENT"] = "client.*";
17
- /**
18
- * en: All server events
19
- * zh: 所有服务端事件
20
- */
21
- EventNames["ALL_SERVER"] = "server.*";
22
- /**
23
- * en: Client connected
24
- * zh: 客户端连接
25
- */
26
- EventNames["CONNECTED"] = "client.connected";
27
- /**
28
- * en: Client interrupted
29
- * zh: 客户端中断
30
- */
31
- EventNames["INTERRUPTED"] = "client.interrupted";
32
- /**
33
- * en: Client disconnected
34
- * zh: 客户端断开
35
- */
36
- EventNames["DISCONNECTED"] = "client.disconnected";
37
- /**
38
- * en: Client audio unmuted
39
- * zh: 客户端音频未静音
40
- */
41
- EventNames["AUDIO_UNMUTED"] = "client.audio.unmuted";
42
- /**
43
- * en: Client audio muted
44
- * zh: 客户端音频静音
45
- */
46
- EventNames["AUDIO_MUTED"] = "client.audio.muted";
47
- /**
48
- * en: Client error
49
- * zh: 客户端错误
50
- */
51
- EventNames["ERROR"] = "client.error";
52
- /**
53
- * en: Audio noise reduction enabled
54
- * zh: 抑制平稳噪声
55
- */
56
- EventNames["SUPPRESS_STATIONARY_NOISE"] = "client.suppress.stationary.noise";
57
- /**
58
- * en: Suppress non-stationary noise
59
- * zh: 抑制非平稳噪声
60
- */
61
- EventNames["SUPPRESS_NON_STATIONARY_NOISE"] = "client.suppress.non.stationary.noise";
62
- /**
63
- * en: Audio input device changed
64
- * zh: 音频输入设备改变
65
- */
66
- EventNames["AUDIO_INPUT_DEVICE_CHANGED"] = "client.input.device.changed";
67
- /**
68
- * en: Audio output device changed
69
- * zh: 音频输出设备改变
70
- */
71
- EventNames["AUDIO_OUTPUT_DEVICE_CHANGED"] = "client.output.device.changed";
72
- })(EventNames || (exports.EventNames = EventNames = {}));
73
- class RealtimeEventHandler {
74
- constructor(debug = false) {
75
- this.eventHandlers = {};
76
- this._debug = debug;
77
- }
78
- clearEventHandlers() {
79
- this.eventHandlers = {};
80
- }
81
- on(eventName, callback) {
82
- this._log(`on ${eventName} event`);
83
- this.eventHandlers[eventName] = this.eventHandlers[eventName] || [];
84
- this.eventHandlers[eventName].push(callback);
85
- return callback;
86
- }
87
- off(eventName, callback) {
88
- this._log(`off ${eventName} event`);
89
- const handlers = this.eventHandlers[eventName] || [];
90
- if (callback) {
91
- const index = handlers.indexOf(callback);
92
- if (index === -1) {
93
- throw new error_1.RealtimeAPIError(error_1.RealtimeError.EVENT_HANDLER_ERROR, `Could not turn off specified event listener for "${eventName}": not found as a listener`);
94
- }
95
- handlers.splice(index, 1);
96
- }
97
- else {
98
- delete this.eventHandlers[eventName];
99
- }
100
- }
101
- // eslint-disable-next-line max-params
102
- _dispatchToHandlers(eventName, event, handlers, prefix) {
103
- for (const handler of handlers) {
104
- if (!prefix || eventName.startsWith(prefix)) {
105
- handler(eventName, event);
106
- }
107
- }
108
- }
109
- dispatch(eventName, event) {
110
- this._log(`dispatch ${eventName} event`);
111
- const handlers = (this.eventHandlers[eventName] || []).slice();
112
- this._dispatchToHandlers(eventName, event, handlers);
113
- const allHandlers = (this.eventHandlers[EventNames.ALL] || []).slice();
114
- this._dispatchToHandlers(eventName, event, allHandlers);
115
- const allClientHandlers = (this.eventHandlers[EventNames.ALL_CLIENT] || []).slice();
116
- this._dispatchToHandlers(eventName, event, allClientHandlers, 'client.');
117
- const allServerHandlers = (this.eventHandlers[EventNames.ALL_SERVER] || []).slice();
118
- this._dispatchToHandlers(eventName, event, allServerHandlers, 'server.');
119
- }
120
- _log(message) {
121
- if (this._debug) {
122
- console.log(`[RealtimeClient] ${message}`);
123
- }
124
- }
125
- }
126
- exports.RealtimeEventHandler = RealtimeEventHandler;