@coze/realtime-api 1.1.0 → 1.1.1
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 +9 -0
- package/README.zh-CN.md +9 -0
- package/dist/cjs/index.cjs +78 -31
- package/dist/esm/index.js +78 -31
- package/dist/types/client.d.ts +2 -1
- package/dist/types/event-handler.d.ts +12 -2
- package/dist/types/index.d.ts +2 -1
- package/dist/umd/index.js +78 -31
- package/package.json +2 -2
package/README.md
CHANGED
@@ -56,6 +56,15 @@ const client = new RealtimeClient({
|
|
56
56
|
voiceId: "your_voice_id", // Optional: Specify voice ID
|
57
57
|
conversationId: "conversation_id", // Optional: For conversation continuity
|
58
58
|
debug: true, // Optional: Enable debug logging
|
59
|
+
getRoomInfo: async () => {
|
60
|
+
// Customize get room info
|
61
|
+
return {
|
62
|
+
token: "your_token",
|
63
|
+
uid: "your_uid",
|
64
|
+
room_id: "your_room_id",
|
65
|
+
app_id: "your_app_id",
|
66
|
+
};
|
67
|
+
},
|
59
68
|
allowPersonalAccessTokenInBrowser: true, // Optional: Enable PAT token usage in browser
|
60
69
|
audioMutedDefault: false, // Optional: Initial audio state (default: false)
|
61
70
|
suppressStationaryNoise: false, // Optional: Enable stationary noise suppression(default: false)
|
package/README.zh-CN.md
CHANGED
@@ -56,6 +56,15 @@ const client = new RealtimeClient({
|
|
56
56
|
voiceId: "your_voice_id", // 可选:指定音色 ID
|
57
57
|
conversationId: "conversation_id", // 可选:用于对话连续性
|
58
58
|
debug: true, // 可选:启用调试日志
|
59
|
+
getRoomInfo: async () => {
|
60
|
+
// 自定义获取房间信息
|
61
|
+
return {
|
62
|
+
token: "your_token",
|
63
|
+
uid: "your_uid",
|
64
|
+
room_id: "your_room_id",
|
65
|
+
app_id: "your_app_id",
|
66
|
+
};
|
67
|
+
},
|
59
68
|
allowPersonalAccessTokenInBrowser: true, // 可选:在浏览器中启用 PAT 令牌使用
|
60
69
|
audioMutedDefault: false, // 可选:初始音频状态(默认:false)
|
61
70
|
suppressStationaryNoise: false, // 可选:启用静态噪声抑制(默认:false)
|
package/dist/cjs/index.cjs
CHANGED
@@ -358,6 +358,12 @@ const handleAdditionalMessages = (additional_messages)=>null == additional_messa
|
|
358
358
|
...i,
|
359
359
|
content: 'object' == typeof i.content ? JSON.stringify(i.content) : i.content
|
360
360
|
}));
|
361
|
+
const handleParameters = (parameters)=>{
|
362
|
+
if (parameters) {
|
363
|
+
for (const [key, value] of Object.entries(parameters))if ('object' == typeof value) parameters[key] = JSON.stringify(value);
|
364
|
+
}
|
365
|
+
return parameters;
|
366
|
+
};
|
361
367
|
class Chat extends APIResource {
|
362
368
|
/**
|
363
369
|
* Call the Chat API to send messages to a published Coze agent. | 调用此接口发起一次对话,支持添加上下文
|
@@ -380,6 +386,10 @@ class Chat extends APIResource {
|
|
380
386
|
const payload = {
|
381
387
|
...rest,
|
382
388
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
389
|
+
shortcut_command: params.shortcut_command ? {
|
390
|
+
...params.shortcut_command,
|
391
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
392
|
+
} : void 0,
|
383
393
|
stream: false
|
384
394
|
};
|
385
395
|
const result = await this._client.post(apiUrl, payload, false, options);
|
@@ -406,6 +416,10 @@ class Chat extends APIResource {
|
|
406
416
|
const payload = {
|
407
417
|
...rest,
|
408
418
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
419
|
+
shortcut_command: params.shortcut_command ? {
|
420
|
+
...params.shortcut_command,
|
421
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
422
|
+
} : void 0,
|
409
423
|
stream: false
|
410
424
|
};
|
411
425
|
const result = await this._client.post(apiUrl, payload, false, options);
|
@@ -444,6 +458,10 @@ class Chat extends APIResource {
|
|
444
458
|
const payload = {
|
445
459
|
...rest,
|
446
460
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
461
|
+
shortcut_command: params.shortcut_command ? {
|
462
|
+
...params.shortcut_command,
|
463
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
464
|
+
} : void 0,
|
447
465
|
stream: true
|
448
466
|
};
|
449
467
|
const result = await this._client.post(apiUrl, payload, true, options);
|
@@ -4607,6 +4625,8 @@ class WebSocketAPI {
|
|
4607
4625
|
});
|
4608
4626
|
this.rws.addEventListener('error', (event)=>{
|
4609
4627
|
var _event_target__req_res, _event_target__req, _event_target, _event_target__req_res1, _event_target__req1, _event_target1, _this_onerror, _this;
|
4628
|
+
const { readyState } = this.rws;
|
4629
|
+
if (3 === readyState) return;
|
4610
4630
|
const statusCode = null === (_event_target = event.target) || void 0 === _event_target ? void 0 : null === (_event_target__req = _event_target._req) || void 0 === _event_target__req ? void 0 : null === (_event_target__req_res = _event_target__req.res) || void 0 === _event_target__req_res ? void 0 : _event_target__req_res.statusCode;
|
4611
4631
|
const rawHeaders = (null === (_event_target1 = event.target) || void 0 === _event_target1 ? void 0 : null === (_event_target__req1 = _event_target1._req) || void 0 === _event_target__req1 ? void 0 : null === (_event_target__req_res1 = _event_target__req1.res) || void 0 === _event_target__req_res1 ? void 0 : _event_target__req_res1.rawHeaders) || [];
|
4612
4632
|
const logidIndex = rawHeaders.findIndex((header)=>'X-Tt-Logid' === header);
|
@@ -4630,7 +4650,8 @@ class WebSocketAPI {
|
|
4630
4650
|
error.data.msg = 'Forbidden';
|
4631
4651
|
} else {
|
4632
4652
|
error.data.code = 500;
|
4633
|
-
|
4653
|
+
var _event_error;
|
4654
|
+
error.data.msg = String(null !== (_event_error = null == event ? void 0 : event.error) && void 0 !== _event_error ? _event_error : '') || 'WebSocket error';
|
4634
4655
|
}
|
4635
4656
|
null === (_this_onerror = (_this = this).onerror) || void 0 === _this_onerror || _this_onerror.call(_this, error, event);
|
4636
4657
|
});
|
@@ -4639,7 +4660,7 @@ class WebSocketAPI {
|
|
4639
4660
|
// EXTERNAL MODULE: os (ignored)
|
4640
4661
|
var os_ignored_ = __webpack_require__("?9050");
|
4641
4662
|
var os_ignored_default = /*#__PURE__*/ __webpack_require__.n(os_ignored_);
|
4642
|
-
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.1.
|
4663
|
+
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.1.1","description":"Official Coze Node.js SDK for seamless AI integration into your applications | 扣子官方 Node.js SDK,助您轻松集成 AI 能力到应用中","keywords":["coze","ai","nodejs","sdk","chatbot","typescript"],"homepage":"https://github.com/coze-dev/coze-js/tree/main/packages/coze-js","bugs":{"url":"https://github.com/coze-dev/coze-js/issues"},"repository":{"type":"git","url":"https://github.com/coze-dev/coze-js.git","directory":"packages/coze-js"},"license":"MIT","author":"Leeight <leeight@gmail.com>","exports":{".":"./src/index.ts","./ws-tools":"./src/ws-tools/index.ts"},"main":"src/index.ts","module":"src/index.ts","browser":{"crypto":false,"os":false,"jsonwebtoken":false,"node-fetch":false},"typesVersions":{"*":{".":["dist/types/index.d.ts"],"ws-tools":["dist/types/ws-tools/ws-tools/index.d.ts"]}},"files":["dist","LICENSE","README.md","README.zh-CN.md"],"scripts":{"build":"rslib build","format":"prettier --write .","lint":"eslint ./ --cache --quiet","start":"rslib build -w","test":"vitest","test:cov":"vitest --coverage --run"},"dependencies":{"jsonwebtoken":"^9.0.2","node-fetch":"^2.x","reconnecting-websocket":"^4.4.0","uuid":"^10.0.0","ws":"^8.11.0"},"devDependencies":{"@coze-infra/eslint-config":"workspace:*","@coze-infra/ts-config":"workspace:*","@coze-infra/vitest-config":"workspace:*","@rslib/core":"0.0.18","@swc/core":"^1.3.14","@types/jsonwebtoken":"^9.0.0","@types/node":"^20","@types/node-fetch":"^2.x","@types/uuid":"^9.0.1","@types/whatwg-fetch":"^0.0.33","@types/ws":"^8.5.1","@vitest/coverage-v8":"~2.1.4","axios":"^1.7.7","typescript":"^5.5.3","vitest":"~2.1.4"},"peerDependencies":{"axios":"^1.7.1"},"cozePublishConfig":{"exports":{".":{"require":"./dist/cjs/index.js","import":"./dist/esm/index.js","types":"./dist/types/index.d.ts"},"./ws-tools":{"require":"./dist/cjs/ws-tools/index.js","import":"./dist/esm/ws-tools/index.js","types":"./dist/types/ws-tools/ws-tools/index.d.ts"}},"main":"dist/cjs/index.js","module":"dist/esm/index.js","types":"dist/types/index.d.ts"}}'); // CONCATENATED MODULE: ../coze-js/src/version.ts
|
4643
4664
|
const { version: version_version } = package_namespaceObject;
|
4644
4665
|
const getEnv = ()=>{
|
4645
4666
|
const nodeVersion = process.version.slice(1); // Remove 'v' prefix
|
@@ -39488,6 +39509,10 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39488
39509
|
* zh: 客户端连接
|
39489
39510
|
*/ EventNames["CONNECTED"] = "client.connected";
|
39490
39511
|
/**
|
39512
|
+
* en: Client connecting
|
39513
|
+
* zh: 客户端连接中
|
39514
|
+
*/ EventNames["CONNECTING"] = "client.connecting";
|
39515
|
+
/**
|
39491
39516
|
* en: Client interrupted
|
39492
39517
|
* zh: 客户端中断
|
39493
39518
|
*/ EventNames["INTERRUPTED"] = "client.interrupted";
|
@@ -39540,6 +39565,10 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39540
39565
|
* zh: 视频输入设备改变
|
39541
39566
|
*/ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
|
39542
39567
|
/**
|
39568
|
+
* en: Network quality changed
|
39569
|
+
* zh: 网络质量改变
|
39570
|
+
*/ EventNames["NETWORK_QUALITY"] = "client.network.quality";
|
39571
|
+
/**
|
39543
39572
|
* en: Bot joined
|
39544
39573
|
* zh: Bot 加入
|
39545
39574
|
*/ EventNames["BOT_JOIN"] = "server.bot.join";
|
@@ -39554,7 +39583,7 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39554
39583
|
/**
|
39555
39584
|
* en: Audio speech stopped
|
39556
39585
|
* zh: 停止说话
|
39557
|
-
*/ EventNames["
|
39586
|
+
*/ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
|
39558
39587
|
/**
|
39559
39588
|
* en: Server error
|
39560
39589
|
* zh: 服务端错误
|
@@ -39592,7 +39621,10 @@ class RealtimeEventHandler {
|
|
39592
39621
|
const handlers = this.eventHandlers[eventName] || [];
|
39593
39622
|
if (callback) {
|
39594
39623
|
const index = handlers.indexOf(callback);
|
39595
|
-
if (-1 === index)
|
39624
|
+
if (-1 === index) {
|
39625
|
+
console.warn(`Could not turn off specified event listener for "${eventName}": not found as a listener`);
|
39626
|
+
return;
|
39627
|
+
}
|
39596
39628
|
handlers.splice(index, 1);
|
39597
39629
|
} else delete this.eventHandlers[eventName];
|
39598
39630
|
}
|
@@ -39606,7 +39638,7 @@ class RealtimeEventHandler {
|
|
39606
39638
|
}
|
39607
39639
|
dispatch(eventName, event) {
|
39608
39640
|
let consoleLog = !(arguments.length > 2) || void 0 === arguments[2] || arguments[2];
|
39609
|
-
if (consoleLog) this._log(`dispatch ${eventName} event
|
39641
|
+
if (consoleLog) this._log(`dispatch ${eventName} event`, event);
|
39610
39642
|
const handlers = (this.eventHandlers[eventName] || []).slice();
|
39611
39643
|
this._dispatchToHandlers(eventName, event, handlers);
|
39612
39644
|
const allHandlers = (this.eventHandlers["realtime.event"] || []).slice();
|
@@ -39616,8 +39648,8 @@ class RealtimeEventHandler {
|
|
39616
39648
|
const allServerHandlers = (this.eventHandlers["server.*"] || []).slice();
|
39617
39649
|
this._dispatchToHandlers(eventName, event, allServerHandlers, 'server.');
|
39618
39650
|
}
|
39619
|
-
_log(message) {
|
39620
|
-
if (this._debug) console.log(`[RealtimeClient] ${message}
|
39651
|
+
_log(message, event) {
|
39652
|
+
if (this._debug) console.log(`[RealtimeClient] ${message}`, event);
|
39621
39653
|
}
|
39622
39654
|
constructor(debug = false){
|
39623
39655
|
this.eventHandlers = {};
|
@@ -42875,6 +42907,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
42875
42907
|
this.engine.on(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
|
42876
42908
|
this.engine.on(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
|
42877
42909
|
this.engine.on(index_esm_min_index.events.onError, this.handleEventError);
|
42910
|
+
this.engine.on(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
|
42878
42911
|
if (this._isSupportVideo) this.engine.on(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
42879
42912
|
if (this._debug) {
|
42880
42913
|
this.engine.on(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
@@ -42886,6 +42919,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
42886
42919
|
this.engine.off(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
|
42887
42920
|
this.engine.off(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
|
42888
42921
|
this.engine.off(index_esm_min_index.events.onError, this.handleEventError);
|
42922
|
+
this.engine.off(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
|
42889
42923
|
if (this._isSupportVideo) this.engine.off(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
42890
42924
|
if (this._debug) {
|
42891
42925
|
this.engine.off(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
@@ -42930,6 +42964,12 @@ class EngineClient extends RealtimeEventHandler {
|
|
42930
42964
|
handlePlayerEvent(event) {
|
42931
42965
|
this.dispatch(event_handler_EventNames.PLAYER_EVENT, event);
|
42932
42966
|
}
|
42967
|
+
handleNetworkQuality(uplinkNetworkQuality, downlinkNetworkQuality) {
|
42968
|
+
this.dispatch(event_handler_EventNames.NETWORK_QUALITY, {
|
42969
|
+
uplinkNetworkQuality,
|
42970
|
+
downlinkNetworkQuality
|
42971
|
+
});
|
42972
|
+
}
|
42933
42973
|
async joinRoom(options) {
|
42934
42974
|
const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
|
42935
42975
|
try {
|
@@ -42996,11 +43036,10 @@ class EngineClient extends RealtimeEventHandler {
|
|
42996
43036
|
}
|
42997
43037
|
async disconnect() {
|
42998
43038
|
try {
|
42999
|
-
if (this._isSupportVideo) await this.changeVideoState(false);
|
43000
|
-
await this.changeAudioState(false);
|
43001
|
-
await this.engine.unpublishStream(MediaType$1.AUDIO);
|
43002
43039
|
await this.engine.leaveRoom();
|
43003
43040
|
this.removeEventListener();
|
43041
|
+
this.clearEventHandlers();
|
43042
|
+
index_esm_min_index.destroyEngine(this.engine);
|
43004
43043
|
} catch (e) {
|
43005
43044
|
this.dispatch(event_handler_EventNames.ERROR, e);
|
43006
43045
|
throw e;
|
@@ -43121,6 +43160,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
43121
43160
|
this.handleUserLeave = this.handleUserLeave.bind(this);
|
43122
43161
|
this.handleEventError = this.handleEventError.bind(this);
|
43123
43162
|
this.handlePlayerEvent = this.handlePlayerEvent.bind(this);
|
43163
|
+
this.handleNetworkQuality = this.handleNetworkQuality.bind(this);
|
43124
43164
|
// Debug only
|
43125
43165
|
this.handleLocalAudioPropertiesReport = this.handleLocalAudioPropertiesReport.bind(this);
|
43126
43166
|
this.handleRemoteAudioPropertiesReport = this.handleRemoteAudioPropertiesReport.bind(this);
|
@@ -43128,6 +43168,8 @@ class EngineClient extends RealtimeEventHandler {
|
|
43128
43168
|
this._videoConfig = videoConfig;
|
43129
43169
|
}
|
43130
43170
|
}
|
43171
|
+
// Only use for test
|
43172
|
+
const TEST_APP_ID = '6705332c79516e015e3e5f0c';
|
43131
43173
|
class RealtimeClient extends RealtimeEventHandler {
|
43132
43174
|
/**
|
43133
43175
|
* en: Establish a connection to the Coze API and join the room
|
@@ -43135,33 +43177,38 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43135
43177
|
* zh: 建立与 Coze API 的连接并加入房间
|
43136
43178
|
*/ async connect() {
|
43137
43179
|
var _this__config_videoConfig;
|
43138
|
-
const { botId, conversationId, voiceId } = this._config;
|
43180
|
+
const { botId, conversationId, voiceId, getRoomInfo } = this._config;
|
43181
|
+
this.dispatch(event_handler_EventNames.CONNECTING, {});
|
43139
43182
|
let roomInfo;
|
43140
43183
|
try {
|
43141
43184
|
// Step1 get token
|
43142
|
-
|
43143
|
-
|
43144
|
-
|
43145
|
-
|
43146
|
-
|
43147
|
-
|
43148
|
-
|
43149
|
-
|
43150
|
-
|
43151
|
-
|
43152
|
-
|
43153
|
-
|
43154
|
-
|
43155
|
-
|
43156
|
-
|
43157
|
-
|
43158
|
-
|
43159
|
-
|
43160
|
-
|
43185
|
+
if (getRoomInfo) roomInfo = await getRoomInfo();
|
43186
|
+
else {
|
43187
|
+
let config;
|
43188
|
+
if (this._config.videoConfig) config = isScreenShareDevice(this._config.videoConfig.videoInputDeviceId) ? {
|
43189
|
+
video_config: {
|
43190
|
+
stream_video_type: 'screen'
|
43191
|
+
}
|
43192
|
+
} : {
|
43193
|
+
video_config: {
|
43194
|
+
stream_video_type: 'main'
|
43195
|
+
}
|
43196
|
+
};
|
43197
|
+
roomInfo = await this._api.audio.rooms.create({
|
43198
|
+
bot_id: botId,
|
43199
|
+
conversation_id: conversationId || void 0,
|
43200
|
+
voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
|
43201
|
+
connector_id: this._config.connectorId,
|
43202
|
+
uid: this._config.userId || void 0,
|
43203
|
+
workflow_id: this._config.workflowId || void 0,
|
43204
|
+
config
|
43205
|
+
});
|
43206
|
+
}
|
43161
43207
|
} catch (error) {
|
43162
43208
|
this.dispatch(event_handler_EventNames.ERROR, error);
|
43163
43209
|
throw new RealtimeAPIError(error_RealtimeError.CREATE_ROOM_ERROR, error instanceof Error ? error.message : 'Unknown error', error);
|
43164
43210
|
}
|
43211
|
+
this._isTestEnv = TEST_APP_ID === roomInfo.app_id;
|
43165
43212
|
// Step2 create engine
|
43166
43213
|
this._client = new EngineClient(roomInfo.app_id, this._config.debug, this._isTestEnv, this._isSupportVideo, this._config.videoConfig);
|
43167
43214
|
// Step3 bind engine events
|
@@ -43216,6 +43263,7 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43216
43263
|
var _this__client;
|
43217
43264
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.disconnect());
|
43218
43265
|
this.isConnected = false;
|
43266
|
+
this._client = null;
|
43219
43267
|
this.dispatch(event_handler_EventNames.DISCONNECTED, {});
|
43220
43268
|
}
|
43221
43269
|
/**
|
@@ -43368,7 +43416,6 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43368
43416
|
baseURL: defaultBaseURL,
|
43369
43417
|
allowPersonalAccessTokenInBrowser: this._config.allowPersonalAccessTokenInBrowser
|
43370
43418
|
});
|
43371
|
-
this._isTestEnv = 'https://api.coze.cn' !== defaultBaseURL;
|
43372
43419
|
this._isSupportVideo = !!config.videoConfig;
|
43373
43420
|
}
|
43374
43421
|
}
|
package/dist/esm/index.js
CHANGED
@@ -346,6 +346,12 @@ const handleAdditionalMessages = (additional_messages)=>null == additional_messa
|
|
346
346
|
...i,
|
347
347
|
content: 'object' == typeof i.content ? JSON.stringify(i.content) : i.content
|
348
348
|
}));
|
349
|
+
const handleParameters = (parameters)=>{
|
350
|
+
if (parameters) {
|
351
|
+
for (const [key, value] of Object.entries(parameters))if ('object' == typeof value) parameters[key] = JSON.stringify(value);
|
352
|
+
}
|
353
|
+
return parameters;
|
354
|
+
};
|
349
355
|
class Chat extends APIResource {
|
350
356
|
/**
|
351
357
|
* Call the Chat API to send messages to a published Coze agent. | 调用此接口发起一次对话,支持添加上下文
|
@@ -368,6 +374,10 @@ class Chat extends APIResource {
|
|
368
374
|
const payload = {
|
369
375
|
...rest,
|
370
376
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
377
|
+
shortcut_command: params.shortcut_command ? {
|
378
|
+
...params.shortcut_command,
|
379
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
380
|
+
} : void 0,
|
371
381
|
stream: false
|
372
382
|
};
|
373
383
|
const result = await this._client.post(apiUrl, payload, false, options);
|
@@ -394,6 +404,10 @@ class Chat extends APIResource {
|
|
394
404
|
const payload = {
|
395
405
|
...rest,
|
396
406
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
407
|
+
shortcut_command: params.shortcut_command ? {
|
408
|
+
...params.shortcut_command,
|
409
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
410
|
+
} : void 0,
|
397
411
|
stream: false
|
398
412
|
};
|
399
413
|
const result = await this._client.post(apiUrl, payload, false, options);
|
@@ -432,6 +446,10 @@ class Chat extends APIResource {
|
|
432
446
|
const payload = {
|
433
447
|
...rest,
|
434
448
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
449
|
+
shortcut_command: params.shortcut_command ? {
|
450
|
+
...params.shortcut_command,
|
451
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
452
|
+
} : void 0,
|
435
453
|
stream: true
|
436
454
|
};
|
437
455
|
const result = await this._client.post(apiUrl, payload, true, options);
|
@@ -4595,6 +4613,8 @@ class WebSocketAPI {
|
|
4595
4613
|
});
|
4596
4614
|
this.rws.addEventListener('error', (event)=>{
|
4597
4615
|
var _event_target__req_res, _event_target__req, _event_target, _event_target__req_res1, _event_target__req1, _event_target1, _this_onerror, _this;
|
4616
|
+
const { readyState } = this.rws;
|
4617
|
+
if (3 === readyState) return;
|
4598
4618
|
const statusCode = null === (_event_target = event.target) || void 0 === _event_target ? void 0 : null === (_event_target__req = _event_target._req) || void 0 === _event_target__req ? void 0 : null === (_event_target__req_res = _event_target__req.res) || void 0 === _event_target__req_res ? void 0 : _event_target__req_res.statusCode;
|
4599
4619
|
const rawHeaders = (null === (_event_target1 = event.target) || void 0 === _event_target1 ? void 0 : null === (_event_target__req1 = _event_target1._req) || void 0 === _event_target__req1 ? void 0 : null === (_event_target__req_res1 = _event_target__req1.res) || void 0 === _event_target__req_res1 ? void 0 : _event_target__req_res1.rawHeaders) || [];
|
4600
4620
|
const logidIndex = rawHeaders.findIndex((header)=>'X-Tt-Logid' === header);
|
@@ -4618,7 +4638,8 @@ class WebSocketAPI {
|
|
4618
4638
|
error.data.msg = 'Forbidden';
|
4619
4639
|
} else {
|
4620
4640
|
error.data.code = 500;
|
4621
|
-
|
4641
|
+
var _event_error;
|
4642
|
+
error.data.msg = String(null !== (_event_error = null == event ? void 0 : event.error) && void 0 !== _event_error ? _event_error : '') || 'WebSocket error';
|
4622
4643
|
}
|
4623
4644
|
null === (_this_onerror = (_this = this).onerror) || void 0 === _this_onerror || _this_onerror.call(_this, error, event);
|
4624
4645
|
});
|
@@ -4627,7 +4648,7 @@ class WebSocketAPI {
|
|
4627
4648
|
// EXTERNAL MODULE: os (ignored)
|
4628
4649
|
var os_ignored_ = __webpack_require__("?9050");
|
4629
4650
|
var os_ignored_default = /*#__PURE__*/ __webpack_require__.n(os_ignored_);
|
4630
|
-
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.1.
|
4651
|
+
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.1.1","description":"Official Coze Node.js SDK for seamless AI integration into your applications | 扣子官方 Node.js SDK,助您轻松集成 AI 能力到应用中","keywords":["coze","ai","nodejs","sdk","chatbot","typescript"],"homepage":"https://github.com/coze-dev/coze-js/tree/main/packages/coze-js","bugs":{"url":"https://github.com/coze-dev/coze-js/issues"},"repository":{"type":"git","url":"https://github.com/coze-dev/coze-js.git","directory":"packages/coze-js"},"license":"MIT","author":"Leeight <leeight@gmail.com>","exports":{".":"./src/index.ts","./ws-tools":"./src/ws-tools/index.ts"},"main":"src/index.ts","module":"src/index.ts","browser":{"crypto":false,"os":false,"jsonwebtoken":false,"node-fetch":false},"typesVersions":{"*":{".":["dist/types/index.d.ts"],"ws-tools":["dist/types/ws-tools/ws-tools/index.d.ts"]}},"files":["dist","LICENSE","README.md","README.zh-CN.md"],"scripts":{"build":"rslib build","format":"prettier --write .","lint":"eslint ./ --cache --quiet","start":"rslib build -w","test":"vitest","test:cov":"vitest --coverage --run"},"dependencies":{"jsonwebtoken":"^9.0.2","node-fetch":"^2.x","reconnecting-websocket":"^4.4.0","uuid":"^10.0.0","ws":"^8.11.0"},"devDependencies":{"@coze-infra/eslint-config":"workspace:*","@coze-infra/ts-config":"workspace:*","@coze-infra/vitest-config":"workspace:*","@rslib/core":"0.0.18","@swc/core":"^1.3.14","@types/jsonwebtoken":"^9.0.0","@types/node":"^20","@types/node-fetch":"^2.x","@types/uuid":"^9.0.1","@types/whatwg-fetch":"^0.0.33","@types/ws":"^8.5.1","@vitest/coverage-v8":"~2.1.4","axios":"^1.7.7","typescript":"^5.5.3","vitest":"~2.1.4"},"peerDependencies":{"axios":"^1.7.1"},"cozePublishConfig":{"exports":{".":{"require":"./dist/cjs/index.js","import":"./dist/esm/index.js","types":"./dist/types/index.d.ts"},"./ws-tools":{"require":"./dist/cjs/ws-tools/index.js","import":"./dist/esm/ws-tools/index.js","types":"./dist/types/ws-tools/ws-tools/index.d.ts"}},"main":"dist/cjs/index.js","module":"dist/esm/index.js","types":"dist/types/index.d.ts"}}'); // CONCATENATED MODULE: ../coze-js/src/version.ts
|
4631
4652
|
const { version: version_version } = package_namespaceObject;
|
4632
4653
|
const getEnv = ()=>{
|
4633
4654
|
const nodeVersion = process.version.slice(1); // Remove 'v' prefix
|
@@ -39476,6 +39497,10 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39476
39497
|
* zh: 客户端连接
|
39477
39498
|
*/ EventNames["CONNECTED"] = "client.connected";
|
39478
39499
|
/**
|
39500
|
+
* en: Client connecting
|
39501
|
+
* zh: 客户端连接中
|
39502
|
+
*/ EventNames["CONNECTING"] = "client.connecting";
|
39503
|
+
/**
|
39479
39504
|
* en: Client interrupted
|
39480
39505
|
* zh: 客户端中断
|
39481
39506
|
*/ EventNames["INTERRUPTED"] = "client.interrupted";
|
@@ -39528,6 +39553,10 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39528
39553
|
* zh: 视频输入设备改变
|
39529
39554
|
*/ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
|
39530
39555
|
/**
|
39556
|
+
* en: Network quality changed
|
39557
|
+
* zh: 网络质量改变
|
39558
|
+
*/ EventNames["NETWORK_QUALITY"] = "client.network.quality";
|
39559
|
+
/**
|
39531
39560
|
* en: Bot joined
|
39532
39561
|
* zh: Bot 加入
|
39533
39562
|
*/ EventNames["BOT_JOIN"] = "server.bot.join";
|
@@ -39542,7 +39571,7 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39542
39571
|
/**
|
39543
39572
|
* en: Audio speech stopped
|
39544
39573
|
* zh: 停止说话
|
39545
|
-
*/ EventNames["
|
39574
|
+
*/ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
|
39546
39575
|
/**
|
39547
39576
|
* en: Server error
|
39548
39577
|
* zh: 服务端错误
|
@@ -39580,7 +39609,10 @@ class RealtimeEventHandler {
|
|
39580
39609
|
const handlers = this.eventHandlers[eventName] || [];
|
39581
39610
|
if (callback) {
|
39582
39611
|
const index = handlers.indexOf(callback);
|
39583
|
-
if (-1 === index)
|
39612
|
+
if (-1 === index) {
|
39613
|
+
console.warn(`Could not turn off specified event listener for "${eventName}": not found as a listener`);
|
39614
|
+
return;
|
39615
|
+
}
|
39584
39616
|
handlers.splice(index, 1);
|
39585
39617
|
} else delete this.eventHandlers[eventName];
|
39586
39618
|
}
|
@@ -39594,7 +39626,7 @@ class RealtimeEventHandler {
|
|
39594
39626
|
}
|
39595
39627
|
dispatch(eventName, event) {
|
39596
39628
|
let consoleLog = !(arguments.length > 2) || void 0 === arguments[2] || arguments[2];
|
39597
|
-
if (consoleLog) this._log(`dispatch ${eventName} event
|
39629
|
+
if (consoleLog) this._log(`dispatch ${eventName} event`, event);
|
39598
39630
|
const handlers = (this.eventHandlers[eventName] || []).slice();
|
39599
39631
|
this._dispatchToHandlers(eventName, event, handlers);
|
39600
39632
|
const allHandlers = (this.eventHandlers["realtime.event"] || []).slice();
|
@@ -39604,8 +39636,8 @@ class RealtimeEventHandler {
|
|
39604
39636
|
const allServerHandlers = (this.eventHandlers["server.*"] || []).slice();
|
39605
39637
|
this._dispatchToHandlers(eventName, event, allServerHandlers, 'server.');
|
39606
39638
|
}
|
39607
|
-
_log(message) {
|
39608
|
-
if (this._debug) console.log(`[RealtimeClient] ${message}
|
39639
|
+
_log(message, event) {
|
39640
|
+
if (this._debug) console.log(`[RealtimeClient] ${message}`, event);
|
39609
39641
|
}
|
39610
39642
|
constructor(debug = false){
|
39611
39643
|
this.eventHandlers = {};
|
@@ -42863,6 +42895,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
42863
42895
|
this.engine.on(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
|
42864
42896
|
this.engine.on(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
|
42865
42897
|
this.engine.on(index_esm_min_index.events.onError, this.handleEventError);
|
42898
|
+
this.engine.on(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
|
42866
42899
|
if (this._isSupportVideo) this.engine.on(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
42867
42900
|
if (this._debug) {
|
42868
42901
|
this.engine.on(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
@@ -42874,6 +42907,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
42874
42907
|
this.engine.off(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
|
42875
42908
|
this.engine.off(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
|
42876
42909
|
this.engine.off(index_esm_min_index.events.onError, this.handleEventError);
|
42910
|
+
this.engine.off(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
|
42877
42911
|
if (this._isSupportVideo) this.engine.off(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
42878
42912
|
if (this._debug) {
|
42879
42913
|
this.engine.off(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
@@ -42918,6 +42952,12 @@ class EngineClient extends RealtimeEventHandler {
|
|
42918
42952
|
handlePlayerEvent(event) {
|
42919
42953
|
this.dispatch(event_handler_EventNames.PLAYER_EVENT, event);
|
42920
42954
|
}
|
42955
|
+
handleNetworkQuality(uplinkNetworkQuality, downlinkNetworkQuality) {
|
42956
|
+
this.dispatch(event_handler_EventNames.NETWORK_QUALITY, {
|
42957
|
+
uplinkNetworkQuality,
|
42958
|
+
downlinkNetworkQuality
|
42959
|
+
});
|
42960
|
+
}
|
42921
42961
|
async joinRoom(options) {
|
42922
42962
|
const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
|
42923
42963
|
try {
|
@@ -42984,11 +43024,10 @@ class EngineClient extends RealtimeEventHandler {
|
|
42984
43024
|
}
|
42985
43025
|
async disconnect() {
|
42986
43026
|
try {
|
42987
|
-
if (this._isSupportVideo) await this.changeVideoState(false);
|
42988
|
-
await this.changeAudioState(false);
|
42989
|
-
await this.engine.unpublishStream(MediaType$1.AUDIO);
|
42990
43027
|
await this.engine.leaveRoom();
|
42991
43028
|
this.removeEventListener();
|
43029
|
+
this.clearEventHandlers();
|
43030
|
+
index_esm_min_index.destroyEngine(this.engine);
|
42992
43031
|
} catch (e) {
|
42993
43032
|
this.dispatch(event_handler_EventNames.ERROR, e);
|
42994
43033
|
throw e;
|
@@ -43109,6 +43148,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
43109
43148
|
this.handleUserLeave = this.handleUserLeave.bind(this);
|
43110
43149
|
this.handleEventError = this.handleEventError.bind(this);
|
43111
43150
|
this.handlePlayerEvent = this.handlePlayerEvent.bind(this);
|
43151
|
+
this.handleNetworkQuality = this.handleNetworkQuality.bind(this);
|
43112
43152
|
// Debug only
|
43113
43153
|
this.handleLocalAudioPropertiesReport = this.handleLocalAudioPropertiesReport.bind(this);
|
43114
43154
|
this.handleRemoteAudioPropertiesReport = this.handleRemoteAudioPropertiesReport.bind(this);
|
@@ -43116,6 +43156,8 @@ class EngineClient extends RealtimeEventHandler {
|
|
43116
43156
|
this._videoConfig = videoConfig;
|
43117
43157
|
}
|
43118
43158
|
}
|
43159
|
+
// Only use for test
|
43160
|
+
const TEST_APP_ID = '6705332c79516e015e3e5f0c';
|
43119
43161
|
class RealtimeClient extends RealtimeEventHandler {
|
43120
43162
|
/**
|
43121
43163
|
* en: Establish a connection to the Coze API and join the room
|
@@ -43123,33 +43165,38 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43123
43165
|
* zh: 建立与 Coze API 的连接并加入房间
|
43124
43166
|
*/ async connect() {
|
43125
43167
|
var _this__config_videoConfig;
|
43126
|
-
const { botId, conversationId, voiceId } = this._config;
|
43168
|
+
const { botId, conversationId, voiceId, getRoomInfo } = this._config;
|
43169
|
+
this.dispatch(event_handler_EventNames.CONNECTING, {});
|
43127
43170
|
let roomInfo;
|
43128
43171
|
try {
|
43129
43172
|
// Step1 get token
|
43130
|
-
|
43131
|
-
|
43132
|
-
|
43133
|
-
|
43134
|
-
|
43135
|
-
|
43136
|
-
|
43137
|
-
|
43138
|
-
|
43139
|
-
|
43140
|
-
|
43141
|
-
|
43142
|
-
|
43143
|
-
|
43144
|
-
|
43145
|
-
|
43146
|
-
|
43147
|
-
|
43148
|
-
|
43173
|
+
if (getRoomInfo) roomInfo = await getRoomInfo();
|
43174
|
+
else {
|
43175
|
+
let config;
|
43176
|
+
if (this._config.videoConfig) config = isScreenShareDevice(this._config.videoConfig.videoInputDeviceId) ? {
|
43177
|
+
video_config: {
|
43178
|
+
stream_video_type: 'screen'
|
43179
|
+
}
|
43180
|
+
} : {
|
43181
|
+
video_config: {
|
43182
|
+
stream_video_type: 'main'
|
43183
|
+
}
|
43184
|
+
};
|
43185
|
+
roomInfo = await this._api.audio.rooms.create({
|
43186
|
+
bot_id: botId,
|
43187
|
+
conversation_id: conversationId || void 0,
|
43188
|
+
voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
|
43189
|
+
connector_id: this._config.connectorId,
|
43190
|
+
uid: this._config.userId || void 0,
|
43191
|
+
workflow_id: this._config.workflowId || void 0,
|
43192
|
+
config
|
43193
|
+
});
|
43194
|
+
}
|
43149
43195
|
} catch (error) {
|
43150
43196
|
this.dispatch(event_handler_EventNames.ERROR, error);
|
43151
43197
|
throw new RealtimeAPIError(error_RealtimeError.CREATE_ROOM_ERROR, error instanceof Error ? error.message : 'Unknown error', error);
|
43152
43198
|
}
|
43199
|
+
this._isTestEnv = TEST_APP_ID === roomInfo.app_id;
|
43153
43200
|
// Step2 create engine
|
43154
43201
|
this._client = new EngineClient(roomInfo.app_id, this._config.debug, this._isTestEnv, this._isSupportVideo, this._config.videoConfig);
|
43155
43202
|
// Step3 bind engine events
|
@@ -43204,6 +43251,7 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43204
43251
|
var _this__client;
|
43205
43252
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.disconnect());
|
43206
43253
|
this.isConnected = false;
|
43254
|
+
this._client = null;
|
43207
43255
|
this.dispatch(event_handler_EventNames.DISCONNECTED, {});
|
43208
43256
|
}
|
43209
43257
|
/**
|
@@ -43356,7 +43404,6 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43356
43404
|
baseURL: defaultBaseURL,
|
43357
43405
|
allowPersonalAccessTokenInBrowser: this._config.allowPersonalAccessTokenInBrowser
|
43358
43406
|
});
|
43359
|
-
this._isTestEnv = 'https://api.coze.cn' !== defaultBaseURL;
|
43360
43407
|
this._isSupportVideo = !!config.videoConfig;
|
43361
43408
|
}
|
43362
43409
|
}
|
package/dist/types/client.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type AudioPropertiesConfig, type IRTCEngine, type onUserJoinedEvent, type onUserLeaveEvent, type UserMessageEvent } from '@volcengine/rtc';
|
1
|
+
import { type AudioPropertiesConfig, type IRTCEngine, type NetworkQuality, type onUserJoinedEvent, type onUserLeaveEvent, type UserMessageEvent } from '@volcengine/rtc';
|
2
2
|
import { RealtimeEventHandler } from './event-handler';
|
3
3
|
import { type VideoConfig } from '.';
|
4
4
|
export declare class EngineClient extends RealtimeEventHandler {
|
@@ -18,6 +18,7 @@ export declare class EngineClient extends RealtimeEventHandler {
|
|
18
18
|
handleUserJoin(event: onUserJoinedEvent): void;
|
19
19
|
handleUserLeave(event: onUserLeaveEvent): void;
|
20
20
|
handlePlayerEvent(event: unknown): void;
|
21
|
+
handleNetworkQuality(uplinkNetworkQuality: NetworkQuality, downlinkNetworkQuality: NetworkQuality): void;
|
21
22
|
joinRoom(options: {
|
22
23
|
token: string;
|
23
24
|
roomId: string;
|
@@ -19,6 +19,11 @@ export declare enum EventNames {
|
|
19
19
|
* zh: 客户端连接
|
20
20
|
*/
|
21
21
|
CONNECTED = "client.connected",
|
22
|
+
/**
|
23
|
+
* en: Client connecting
|
24
|
+
* zh: 客户端连接中
|
25
|
+
*/
|
26
|
+
CONNECTING = "client.connecting",
|
22
27
|
/**
|
23
28
|
* en: Client interrupted
|
24
29
|
* zh: 客户端中断
|
@@ -84,6 +89,11 @@ export declare enum EventNames {
|
|
84
89
|
* zh: 视频输入设备改变
|
85
90
|
*/
|
86
91
|
VIDEO_INPUT_DEVICE_CHANGED = "client.video.input.device.changed",
|
92
|
+
/**
|
93
|
+
* en: Network quality changed
|
94
|
+
* zh: 网络质量改变
|
95
|
+
*/
|
96
|
+
NETWORK_QUALITY = "client.network.quality",
|
87
97
|
/**
|
88
98
|
* en: Bot joined
|
89
99
|
* zh: Bot 加入
|
@@ -103,7 +113,7 @@ export declare enum EventNames {
|
|
103
113
|
* en: Audio speech stopped
|
104
114
|
* zh: 停止说话
|
105
115
|
*/
|
106
|
-
|
116
|
+
AUDIO_AGENT_SPEECH_STOPPED = "server.audio.agent.speech_stopped",
|
107
117
|
/**
|
108
118
|
* en: Server error
|
109
119
|
* zh: 服务端错误
|
@@ -140,5 +150,5 @@ export declare class RealtimeEventHandler {
|
|
140
150
|
off(eventName: string, callback: EventCallback): void;
|
141
151
|
private _dispatchToHandlers;
|
142
152
|
dispatch(eventName: string, event: unknown, consoleLog?: boolean): void;
|
143
|
-
_log(message: string): void;
|
153
|
+
_log(message: string, event?: unknown): void;
|
144
154
|
}
|
package/dist/types/index.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { type ScreenConfig, type AudioPropertiesConfig, type IRTCEngine } from '@volcengine/rtc';
|
2
|
-
import { type GetToken } from '@coze/api';
|
2
|
+
import { type CreateRoomData, type GetToken } from '@coze/api';
|
3
3
|
import * as RealtimeUtils from './utils';
|
4
4
|
import { RealtimeEventHandler, EventNames } from './event-handler';
|
5
5
|
import { RealtimeAPIError, RealtimeError } from './error';
|
@@ -18,6 +18,7 @@ export interface RealtimeClientConfig {
|
|
18
18
|
workflowId?: string /** optional, Workflow Id */;
|
19
19
|
baseURL?: string /** optional, defaults to "https://api.coze.cn" */;
|
20
20
|
debug?: boolean /** optional, defaults to false */;
|
21
|
+
getRoomInfo?: () => Promise<CreateRoomData> /** optional, Customize get room info */;
|
21
22
|
/** Whether Personal Access Tokens (PAT) are allowed in browser environments */
|
22
23
|
allowPersonalAccessTokenInBrowser?: boolean;
|
23
24
|
/** Whether to mute by default, defaults to false
|
package/dist/umd/index.js
CHANGED
@@ -364,6 +364,12 @@
|
|
364
364
|
...i,
|
365
365
|
content: 'object' == typeof i.content ? JSON.stringify(i.content) : i.content
|
366
366
|
}));
|
367
|
+
const handleParameters = (parameters)=>{
|
368
|
+
if (parameters) {
|
369
|
+
for (const [key, value] of Object.entries(parameters))if ('object' == typeof value) parameters[key] = JSON.stringify(value);
|
370
|
+
}
|
371
|
+
return parameters;
|
372
|
+
};
|
367
373
|
class Chat extends APIResource {
|
368
374
|
/**
|
369
375
|
* Call the Chat API to send messages to a published Coze agent. | 调用此接口发起一次对话,支持添加上下文
|
@@ -386,6 +392,10 @@
|
|
386
392
|
const payload = {
|
387
393
|
...rest,
|
388
394
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
395
|
+
shortcut_command: params.shortcut_command ? {
|
396
|
+
...params.shortcut_command,
|
397
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
398
|
+
} : void 0,
|
389
399
|
stream: false
|
390
400
|
};
|
391
401
|
const result = await this._client.post(apiUrl, payload, false, options);
|
@@ -412,6 +422,10 @@
|
|
412
422
|
const payload = {
|
413
423
|
...rest,
|
414
424
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
425
|
+
shortcut_command: params.shortcut_command ? {
|
426
|
+
...params.shortcut_command,
|
427
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
428
|
+
} : void 0,
|
415
429
|
stream: false
|
416
430
|
};
|
417
431
|
const result = await this._client.post(apiUrl, payload, false, options);
|
@@ -450,6 +464,10 @@
|
|
450
464
|
const payload = {
|
451
465
|
...rest,
|
452
466
|
additional_messages: handleAdditionalMessages(params.additional_messages),
|
467
|
+
shortcut_command: params.shortcut_command ? {
|
468
|
+
...params.shortcut_command,
|
469
|
+
parameters: handleParameters(params.shortcut_command.parameters)
|
470
|
+
} : void 0,
|
453
471
|
stream: true
|
454
472
|
};
|
455
473
|
const result = await this._client.post(apiUrl, payload, true, options);
|
@@ -4613,6 +4631,8 @@ and limitations under the License.
|
|
4613
4631
|
});
|
4614
4632
|
this.rws.addEventListener('error', (event)=>{
|
4615
4633
|
var _event_target__req_res, _event_target__req, _event_target, _event_target__req_res1, _event_target__req1, _event_target1, _this_onerror, _this;
|
4634
|
+
const { readyState } = this.rws;
|
4635
|
+
if (3 === readyState) return;
|
4616
4636
|
const statusCode = null === (_event_target = event.target) || void 0 === _event_target ? void 0 : null === (_event_target__req = _event_target._req) || void 0 === _event_target__req ? void 0 : null === (_event_target__req_res = _event_target__req.res) || void 0 === _event_target__req_res ? void 0 : _event_target__req_res.statusCode;
|
4617
4637
|
const rawHeaders = (null === (_event_target1 = event.target) || void 0 === _event_target1 ? void 0 : null === (_event_target__req1 = _event_target1._req) || void 0 === _event_target__req1 ? void 0 : null === (_event_target__req_res1 = _event_target__req1.res) || void 0 === _event_target__req_res1 ? void 0 : _event_target__req_res1.rawHeaders) || [];
|
4618
4638
|
const logidIndex = rawHeaders.findIndex((header)=>'X-Tt-Logid' === header);
|
@@ -4636,7 +4656,8 @@ and limitations under the License.
|
|
4636
4656
|
error.data.msg = 'Forbidden';
|
4637
4657
|
} else {
|
4638
4658
|
error.data.code = 500;
|
4639
|
-
|
4659
|
+
var _event_error;
|
4660
|
+
error.data.msg = String(null !== (_event_error = null == event ? void 0 : event.error) && void 0 !== _event_error ? _event_error : '') || 'WebSocket error';
|
4640
4661
|
}
|
4641
4662
|
null === (_this_onerror = (_this = this).onerror) || void 0 === _this_onerror || _this_onerror.call(_this, error, event);
|
4642
4663
|
});
|
@@ -4645,7 +4666,7 @@ and limitations under the License.
|
|
4645
4666
|
// EXTERNAL MODULE: os (ignored)
|
4646
4667
|
var os_ignored_ = __webpack_require__("?9050");
|
4647
4668
|
var os_ignored_default = /*#__PURE__*/ __webpack_require__.n(os_ignored_);
|
4648
|
-
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.1.
|
4669
|
+
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.1.1","description":"Official Coze Node.js SDK for seamless AI integration into your applications | 扣子官方 Node.js SDK,助您轻松集成 AI 能力到应用中","keywords":["coze","ai","nodejs","sdk","chatbot","typescript"],"homepage":"https://github.com/coze-dev/coze-js/tree/main/packages/coze-js","bugs":{"url":"https://github.com/coze-dev/coze-js/issues"},"repository":{"type":"git","url":"https://github.com/coze-dev/coze-js.git","directory":"packages/coze-js"},"license":"MIT","author":"Leeight <leeight@gmail.com>","exports":{".":"./src/index.ts","./ws-tools":"./src/ws-tools/index.ts"},"main":"src/index.ts","module":"src/index.ts","browser":{"crypto":false,"os":false,"jsonwebtoken":false,"node-fetch":false},"typesVersions":{"*":{".":["dist/types/index.d.ts"],"ws-tools":["dist/types/ws-tools/ws-tools/index.d.ts"]}},"files":["dist","LICENSE","README.md","README.zh-CN.md"],"scripts":{"build":"rslib build","format":"prettier --write .","lint":"eslint ./ --cache --quiet","start":"rslib build -w","test":"vitest","test:cov":"vitest --coverage --run"},"dependencies":{"jsonwebtoken":"^9.0.2","node-fetch":"^2.x","reconnecting-websocket":"^4.4.0","uuid":"^10.0.0","ws":"^8.11.0"},"devDependencies":{"@coze-infra/eslint-config":"workspace:*","@coze-infra/ts-config":"workspace:*","@coze-infra/vitest-config":"workspace:*","@rslib/core":"0.0.18","@swc/core":"^1.3.14","@types/jsonwebtoken":"^9.0.0","@types/node":"^20","@types/node-fetch":"^2.x","@types/uuid":"^9.0.1","@types/whatwg-fetch":"^0.0.33","@types/ws":"^8.5.1","@vitest/coverage-v8":"~2.1.4","axios":"^1.7.7","typescript":"^5.5.3","vitest":"~2.1.4"},"peerDependencies":{"axios":"^1.7.1"},"cozePublishConfig":{"exports":{".":{"require":"./dist/cjs/index.js","import":"./dist/esm/index.js","types":"./dist/types/index.d.ts"},"./ws-tools":{"require":"./dist/cjs/ws-tools/index.js","import":"./dist/esm/ws-tools/index.js","types":"./dist/types/ws-tools/ws-tools/index.d.ts"}},"main":"dist/cjs/index.js","module":"dist/esm/index.js","types":"dist/types/index.d.ts"}}'); // CONCATENATED MODULE: ../coze-js/src/version.ts
|
4649
4670
|
const { version: version_version } = package_namespaceObject;
|
4650
4671
|
const getEnv = ()=>{
|
4651
4672
|
const nodeVersion = process.version.slice(1); // Remove 'v' prefix
|
@@ -39494,6 +39515,10 @@ and limitations under the License.
|
|
39494
39515
|
* zh: 客户端连接
|
39495
39516
|
*/ EventNames["CONNECTED"] = "client.connected";
|
39496
39517
|
/**
|
39518
|
+
* en: Client connecting
|
39519
|
+
* zh: 客户端连接中
|
39520
|
+
*/ EventNames["CONNECTING"] = "client.connecting";
|
39521
|
+
/**
|
39497
39522
|
* en: Client interrupted
|
39498
39523
|
* zh: 客户端中断
|
39499
39524
|
*/ EventNames["INTERRUPTED"] = "client.interrupted";
|
@@ -39546,6 +39571,10 @@ and limitations under the License.
|
|
39546
39571
|
* zh: 视频输入设备改变
|
39547
39572
|
*/ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
|
39548
39573
|
/**
|
39574
|
+
* en: Network quality changed
|
39575
|
+
* zh: 网络质量改变
|
39576
|
+
*/ EventNames["NETWORK_QUALITY"] = "client.network.quality";
|
39577
|
+
/**
|
39549
39578
|
* en: Bot joined
|
39550
39579
|
* zh: Bot 加入
|
39551
39580
|
*/ EventNames["BOT_JOIN"] = "server.bot.join";
|
@@ -39560,7 +39589,7 @@ and limitations under the License.
|
|
39560
39589
|
/**
|
39561
39590
|
* en: Audio speech stopped
|
39562
39591
|
* zh: 停止说话
|
39563
|
-
*/ EventNames["
|
39592
|
+
*/ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
|
39564
39593
|
/**
|
39565
39594
|
* en: Server error
|
39566
39595
|
* zh: 服务端错误
|
@@ -39598,7 +39627,10 @@ and limitations under the License.
|
|
39598
39627
|
const handlers = this.eventHandlers[eventName] || [];
|
39599
39628
|
if (callback) {
|
39600
39629
|
const index = handlers.indexOf(callback);
|
39601
|
-
if (-1 === index)
|
39630
|
+
if (-1 === index) {
|
39631
|
+
console.warn(`Could not turn off specified event listener for "${eventName}": not found as a listener`);
|
39632
|
+
return;
|
39633
|
+
}
|
39602
39634
|
handlers.splice(index, 1);
|
39603
39635
|
} else delete this.eventHandlers[eventName];
|
39604
39636
|
}
|
@@ -39612,7 +39644,7 @@ and limitations under the License.
|
|
39612
39644
|
}
|
39613
39645
|
dispatch(eventName, event) {
|
39614
39646
|
let consoleLog = !(arguments.length > 2) || void 0 === arguments[2] || arguments[2];
|
39615
|
-
if (consoleLog) this._log(`dispatch ${eventName} event
|
39647
|
+
if (consoleLog) this._log(`dispatch ${eventName} event`, event);
|
39616
39648
|
const handlers = (this.eventHandlers[eventName] || []).slice();
|
39617
39649
|
this._dispatchToHandlers(eventName, event, handlers);
|
39618
39650
|
const allHandlers = (this.eventHandlers["realtime.event"] || []).slice();
|
@@ -39622,8 +39654,8 @@ and limitations under the License.
|
|
39622
39654
|
const allServerHandlers = (this.eventHandlers["server.*"] || []).slice();
|
39623
39655
|
this._dispatchToHandlers(eventName, event, allServerHandlers, 'server.');
|
39624
39656
|
}
|
39625
|
-
_log(message) {
|
39626
|
-
if (this._debug) console.log(`[RealtimeClient] ${message}
|
39657
|
+
_log(message, event) {
|
39658
|
+
if (this._debug) console.log(`[RealtimeClient] ${message}`, event);
|
39627
39659
|
}
|
39628
39660
|
constructor(debug = false){
|
39629
39661
|
this.eventHandlers = {};
|
@@ -42881,6 +42913,7 @@ and limitations under the License.
|
|
42881
42913
|
this.engine.on(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
|
42882
42914
|
this.engine.on(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
|
42883
42915
|
this.engine.on(index_esm_min_index.events.onError, this.handleEventError);
|
42916
|
+
this.engine.on(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
|
42884
42917
|
if (this._isSupportVideo) this.engine.on(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
42885
42918
|
if (this._debug) {
|
42886
42919
|
this.engine.on(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
@@ -42892,6 +42925,7 @@ and limitations under the License.
|
|
42892
42925
|
this.engine.off(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
|
42893
42926
|
this.engine.off(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
|
42894
42927
|
this.engine.off(index_esm_min_index.events.onError, this.handleEventError);
|
42928
|
+
this.engine.off(index_esm_min_index.events.onNetworkQuality, this.handleNetworkQuality);
|
42895
42929
|
if (this._isSupportVideo) this.engine.off(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
42896
42930
|
if (this._debug) {
|
42897
42931
|
this.engine.off(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
@@ -42936,6 +42970,12 @@ and limitations under the License.
|
|
42936
42970
|
handlePlayerEvent(event) {
|
42937
42971
|
this.dispatch(event_handler_EventNames.PLAYER_EVENT, event);
|
42938
42972
|
}
|
42973
|
+
handleNetworkQuality(uplinkNetworkQuality, downlinkNetworkQuality) {
|
42974
|
+
this.dispatch(event_handler_EventNames.NETWORK_QUALITY, {
|
42975
|
+
uplinkNetworkQuality,
|
42976
|
+
downlinkNetworkQuality
|
42977
|
+
});
|
42978
|
+
}
|
42939
42979
|
async joinRoom(options) {
|
42940
42980
|
const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
|
42941
42981
|
try {
|
@@ -43002,11 +43042,10 @@ and limitations under the License.
|
|
43002
43042
|
}
|
43003
43043
|
async disconnect() {
|
43004
43044
|
try {
|
43005
|
-
if (this._isSupportVideo) await this.changeVideoState(false);
|
43006
|
-
await this.changeAudioState(false);
|
43007
|
-
await this.engine.unpublishStream(MediaType$1.AUDIO);
|
43008
43045
|
await this.engine.leaveRoom();
|
43009
43046
|
this.removeEventListener();
|
43047
|
+
this.clearEventHandlers();
|
43048
|
+
index_esm_min_index.destroyEngine(this.engine);
|
43010
43049
|
} catch (e) {
|
43011
43050
|
this.dispatch(event_handler_EventNames.ERROR, e);
|
43012
43051
|
throw e;
|
@@ -43127,6 +43166,7 @@ and limitations under the License.
|
|
43127
43166
|
this.handleUserLeave = this.handleUserLeave.bind(this);
|
43128
43167
|
this.handleEventError = this.handleEventError.bind(this);
|
43129
43168
|
this.handlePlayerEvent = this.handlePlayerEvent.bind(this);
|
43169
|
+
this.handleNetworkQuality = this.handleNetworkQuality.bind(this);
|
43130
43170
|
// Debug only
|
43131
43171
|
this.handleLocalAudioPropertiesReport = this.handleLocalAudioPropertiesReport.bind(this);
|
43132
43172
|
this.handleRemoteAudioPropertiesReport = this.handleRemoteAudioPropertiesReport.bind(this);
|
@@ -43134,6 +43174,8 @@ and limitations under the License.
|
|
43134
43174
|
this._videoConfig = videoConfig;
|
43135
43175
|
}
|
43136
43176
|
}
|
43177
|
+
// Only use for test
|
43178
|
+
const TEST_APP_ID = '6705332c79516e015e3e5f0c';
|
43137
43179
|
class RealtimeClient extends RealtimeEventHandler {
|
43138
43180
|
/**
|
43139
43181
|
* en: Establish a connection to the Coze API and join the room
|
@@ -43141,33 +43183,38 @@ and limitations under the License.
|
|
43141
43183
|
* zh: 建立与 Coze API 的连接并加入房间
|
43142
43184
|
*/ async connect() {
|
43143
43185
|
var _this__config_videoConfig;
|
43144
|
-
const { botId, conversationId, voiceId } = this._config;
|
43186
|
+
const { botId, conversationId, voiceId, getRoomInfo } = this._config;
|
43187
|
+
this.dispatch(event_handler_EventNames.CONNECTING, {});
|
43145
43188
|
let roomInfo;
|
43146
43189
|
try {
|
43147
43190
|
// Step1 get token
|
43148
|
-
|
43149
|
-
|
43150
|
-
|
43151
|
-
|
43152
|
-
|
43153
|
-
|
43154
|
-
|
43155
|
-
|
43156
|
-
|
43157
|
-
|
43158
|
-
|
43159
|
-
|
43160
|
-
|
43161
|
-
|
43162
|
-
|
43163
|
-
|
43164
|
-
|
43165
|
-
|
43166
|
-
|
43191
|
+
if (getRoomInfo) roomInfo = await getRoomInfo();
|
43192
|
+
else {
|
43193
|
+
let config;
|
43194
|
+
if (this._config.videoConfig) config = isScreenShareDevice(this._config.videoConfig.videoInputDeviceId) ? {
|
43195
|
+
video_config: {
|
43196
|
+
stream_video_type: 'screen'
|
43197
|
+
}
|
43198
|
+
} : {
|
43199
|
+
video_config: {
|
43200
|
+
stream_video_type: 'main'
|
43201
|
+
}
|
43202
|
+
};
|
43203
|
+
roomInfo = await this._api.audio.rooms.create({
|
43204
|
+
bot_id: botId,
|
43205
|
+
conversation_id: conversationId || void 0,
|
43206
|
+
voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
|
43207
|
+
connector_id: this._config.connectorId,
|
43208
|
+
uid: this._config.userId || void 0,
|
43209
|
+
workflow_id: this._config.workflowId || void 0,
|
43210
|
+
config
|
43211
|
+
});
|
43212
|
+
}
|
43167
43213
|
} catch (error) {
|
43168
43214
|
this.dispatch(event_handler_EventNames.ERROR, error);
|
43169
43215
|
throw new RealtimeAPIError(error_RealtimeError.CREATE_ROOM_ERROR, error instanceof Error ? error.message : 'Unknown error', error);
|
43170
43216
|
}
|
43217
|
+
this._isTestEnv = TEST_APP_ID === roomInfo.app_id;
|
43171
43218
|
// Step2 create engine
|
43172
43219
|
this._client = new EngineClient(roomInfo.app_id, this._config.debug, this._isTestEnv, this._isSupportVideo, this._config.videoConfig);
|
43173
43220
|
// Step3 bind engine events
|
@@ -43222,6 +43269,7 @@ and limitations under the License.
|
|
43222
43269
|
var _this__client;
|
43223
43270
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.disconnect());
|
43224
43271
|
this.isConnected = false;
|
43272
|
+
this._client = null;
|
43225
43273
|
this.dispatch(event_handler_EventNames.DISCONNECTED, {});
|
43226
43274
|
}
|
43227
43275
|
/**
|
@@ -43374,7 +43422,6 @@ and limitations under the License.
|
|
43374
43422
|
baseURL: defaultBaseURL,
|
43375
43423
|
allowPersonalAccessTokenInBrowser: this._config.allowPersonalAccessTokenInBrowser
|
43376
43424
|
});
|
43377
|
-
this._isTestEnv = 'https://api.coze.cn' !== defaultBaseURL;
|
43378
43425
|
this._isSupportVideo = !!config.videoConfig;
|
43379
43426
|
}
|
43380
43427
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@coze/realtime-api",
|
3
|
-
"version": "1.1.
|
3
|
+
"version": "1.1.1",
|
4
4
|
"description": "A powerful real-time communication SDK for voice interactions with Coze AI bots | 扣子官方实时通信 SDK,用于与 Coze AI bots 进行语音交互",
|
5
5
|
"keywords": [
|
6
6
|
"coze",
|
@@ -49,7 +49,7 @@
|
|
49
49
|
"test:cov": "vitest --coverage --run"
|
50
50
|
},
|
51
51
|
"dependencies": {
|
52
|
-
"@coze/api": "1.1.
|
52
|
+
"@coze/api": "1.1.1",
|
53
53
|
"@volcengine/rtc": "^4.62.1"
|
54
54
|
},
|
55
55
|
"devDependencies": {
|