@coze/realtime-api 1.1.0 → 1.1.1-beta.2
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 +141 -92
- package/dist/esm/index.js +141 -92
- package/dist/types/client.d.ts +2 -1
- package/dist/types/event-handler.d.ts +1 -133
- package/dist/types/event-names.d.ts +142 -0
- package/dist/types/index.d.ts +4 -2
- package/dist/umd/index.js +141 -92
- package/package.json +2 -2
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-beta.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.mjs","types":"./dist/types/index.d.ts"},"./ws-tools":{"require":"./dist/cjs/ws-tools/index.js","import":"./dist/esm/ws-tools/index.mjs","types":"./dist/types/ws-tools/ws-tools/index.d.ts"}},"main":"dist/cjs/index.js","module":"dist/esm/index.mjs","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
|
@@ -39431,34 +39452,7 @@ const isScreenShareDevice = (deviceId)=>'screenShare' === deviceId;
|
|
39431
39452
|
var _navigator_mediaDevices, _navigator;
|
39432
39453
|
return !!(null === (_navigator = navigator) || void 0 === _navigator ? void 0 : null === (_navigator_mediaDevices = _navigator.mediaDevices) || void 0 === _navigator_mediaDevices ? void 0 : _navigator_mediaDevices.getDisplayMedia);
|
39433
39454
|
}
|
39434
|
-
var
|
39435
|
-
RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
|
39436
|
-
RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
|
39437
|
-
RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
|
39438
|
-
RealtimeError["DISCONNECTION_ERROR"] = "DISCONNECTION_ERROR";
|
39439
|
-
RealtimeError["INTERRUPT_ERROR"] = "INTERRUPT_ERROR";
|
39440
|
-
RealtimeError["EVENT_HANDLER_ERROR"] = "EVENT_HANDLER_ERROR";
|
39441
|
-
RealtimeError["PERMISSION_DENIED"] = "PERMISSION_DENIED";
|
39442
|
-
RealtimeError["NETWORK_ERROR"] = "NETWORK_ERROR";
|
39443
|
-
RealtimeError["INVALID_STATE"] = "INVALID_STATE";
|
39444
|
-
RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
|
39445
|
-
RealtimeError["PARSE_MESSAGE_ERROR"] = "PARSE_MESSAGE_ERROR";
|
39446
|
-
RealtimeError["HANDLER_MESSAGE_ERROR"] = "HANDLER_MESSAGE_ERROR";
|
39447
|
-
return RealtimeError;
|
39448
|
-
}({});
|
39449
|
-
class RealtimeAPIError extends Error {
|
39450
|
-
/**
|
39451
|
-
* @param code - Error code
|
39452
|
-
* @param message - Error message
|
39453
|
-
* @param error - Error object
|
39454
|
-
*/ constructor(code, message, error){
|
39455
|
-
super(`[${code}] ${message}`);
|
39456
|
-
this.name = 'RealtimeAPIError';
|
39457
|
-
this.code = code;
|
39458
|
-
this.error = error;
|
39459
|
-
}
|
39460
|
-
}
|
39461
|
-
var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
39455
|
+
var event_names_EventNames = /*#__PURE__*/ function(EventNames) {
|
39462
39456
|
/**
|
39463
39457
|
* en: All events
|
39464
39458
|
* zh: 所有事件
|
@@ -39476,6 +39470,10 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39476
39470
|
* zh: 客户端连接
|
39477
39471
|
*/ EventNames["CONNECTED"] = "client.connected";
|
39478
39472
|
/**
|
39473
|
+
* en: Client connecting
|
39474
|
+
* zh: 客户端连接中
|
39475
|
+
*/ EventNames["CONNECTING"] = "client.connecting";
|
39476
|
+
/**
|
39479
39477
|
* en: Client interrupted
|
39480
39478
|
* zh: 客户端中断
|
39481
39479
|
*/ EventNames["INTERRUPTED"] = "client.interrupted";
|
@@ -39528,6 +39526,10 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39528
39526
|
* zh: 视频输入设备改变
|
39529
39527
|
*/ EventNames["VIDEO_INPUT_DEVICE_CHANGED"] = "client.video.input.device.changed";
|
39530
39528
|
/**
|
39529
|
+
* en: Network quality changed
|
39530
|
+
* zh: 网络质量改变
|
39531
|
+
*/ EventNames["NETWORK_QUALITY"] = "client.network.quality";
|
39532
|
+
/**
|
39531
39533
|
* en: Bot joined
|
39532
39534
|
* zh: Bot 加入
|
39533
39535
|
*/ EventNames["BOT_JOIN"] = "server.bot.join";
|
@@ -39542,7 +39544,7 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39542
39544
|
/**
|
39543
39545
|
* en: Audio speech stopped
|
39544
39546
|
* zh: 停止说话
|
39545
|
-
*/ EventNames["
|
39547
|
+
*/ EventNames["AUDIO_AGENT_SPEECH_STOPPED"] = "server.audio.agent.speech_stopped";
|
39546
39548
|
/**
|
39547
39549
|
* en: Server error
|
39548
39550
|
* zh: 服务端错误
|
@@ -39565,6 +39567,33 @@ var event_handler_EventNames = /*#__PURE__*/ function(EventNames) {
|
|
39565
39567
|
*/ EventNames["SESSION_UPDATE"] = "server.session.update";
|
39566
39568
|
return EventNames;
|
39567
39569
|
}({});
|
39570
|
+
var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
|
39571
|
+
RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
|
39572
|
+
RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
|
39573
|
+
RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
|
39574
|
+
RealtimeError["DISCONNECTION_ERROR"] = "DISCONNECTION_ERROR";
|
39575
|
+
RealtimeError["INTERRUPT_ERROR"] = "INTERRUPT_ERROR";
|
39576
|
+
RealtimeError["EVENT_HANDLER_ERROR"] = "EVENT_HANDLER_ERROR";
|
39577
|
+
RealtimeError["PERMISSION_DENIED"] = "PERMISSION_DENIED";
|
39578
|
+
RealtimeError["NETWORK_ERROR"] = "NETWORK_ERROR";
|
39579
|
+
RealtimeError["INVALID_STATE"] = "INVALID_STATE";
|
39580
|
+
RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
|
39581
|
+
RealtimeError["PARSE_MESSAGE_ERROR"] = "PARSE_MESSAGE_ERROR";
|
39582
|
+
RealtimeError["HANDLER_MESSAGE_ERROR"] = "HANDLER_MESSAGE_ERROR";
|
39583
|
+
return RealtimeError;
|
39584
|
+
}({});
|
39585
|
+
class RealtimeAPIError extends Error {
|
39586
|
+
/**
|
39587
|
+
* @param code - Error code
|
39588
|
+
* @param message - Error message
|
39589
|
+
* @param error - Error object
|
39590
|
+
*/ constructor(code, message, error){
|
39591
|
+
super(`[${code}] ${message}`);
|
39592
|
+
this.name = 'RealtimeAPIError';
|
39593
|
+
this.code = code;
|
39594
|
+
this.error = error;
|
39595
|
+
}
|
39596
|
+
}
|
39568
39597
|
class RealtimeEventHandler {
|
39569
39598
|
clearEventHandlers() {
|
39570
39599
|
this.eventHandlers = {};
|
@@ -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,18 +39626,18 @@ 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
|
-
const allHandlers = (this.eventHandlers[
|
39632
|
+
const allHandlers = (this.eventHandlers[event_names_EventNames.ALL] || []).slice();
|
39601
39633
|
this._dispatchToHandlers(eventName, event, allHandlers);
|
39602
|
-
const allClientHandlers = (this.eventHandlers[
|
39634
|
+
const allClientHandlers = (this.eventHandlers[event_names_EventNames.ALL_CLIENT] || []).slice();
|
39603
39635
|
this._dispatchToHandlers(eventName, event, allClientHandlers, 'client.');
|
39604
|
-
const allServerHandlers = (this.eventHandlers[
|
39636
|
+
const allServerHandlers = (this.eventHandlers[event_names_EventNames.ALL_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);
|
@@ -42894,29 +42928,35 @@ class EngineClient extends RealtimeEventHandler {
|
|
42894
42928
|
this.dispatch(`server.${message.event_type}`, message);
|
42895
42929
|
} catch (e) {
|
42896
42930
|
if (e instanceof RealtimeAPIError) {
|
42897
|
-
if (e.code === error_RealtimeError.PARSE_MESSAGE_ERROR) this.dispatch(
|
42931
|
+
if (e.code === error_RealtimeError.PARSE_MESSAGE_ERROR) this.dispatch(event_names_EventNames.ERROR, {
|
42898
42932
|
message: `Failed to parse message: ${event.message}`,
|
42899
42933
|
error: e
|
42900
42934
|
});
|
42901
|
-
else if (e.code === error_RealtimeError.HANDLER_MESSAGE_ERROR) this.dispatch(
|
42935
|
+
else if (e.code === error_RealtimeError.HANDLER_MESSAGE_ERROR) this.dispatch(event_names_EventNames.ERROR, {
|
42902
42936
|
message: `Failed to handle message: ${event.message}`,
|
42903
42937
|
error: e
|
42904
42938
|
});
|
42905
|
-
} else this.dispatch(
|
42939
|
+
} else this.dispatch(event_names_EventNames.ERROR, e);
|
42906
42940
|
}
|
42907
42941
|
}
|
42908
42942
|
handleEventError(e) {
|
42909
|
-
this.dispatch(
|
42943
|
+
this.dispatch(event_names_EventNames.ERROR, e);
|
42910
42944
|
}
|
42911
42945
|
handleUserJoin(event) {
|
42912
42946
|
this.joinUserId = event.userInfo.userId;
|
42913
|
-
this.dispatch(
|
42947
|
+
this.dispatch(event_names_EventNames.BOT_JOIN, event);
|
42914
42948
|
}
|
42915
42949
|
handleUserLeave(event) {
|
42916
|
-
this.dispatch(
|
42950
|
+
this.dispatch(event_names_EventNames.BOT_LEAVE, event);
|
42917
42951
|
}
|
42918
42952
|
handlePlayerEvent(event) {
|
42919
|
-
this.dispatch(
|
42953
|
+
this.dispatch(event_names_EventNames.PLAYER_EVENT, event);
|
42954
|
+
}
|
42955
|
+
handleNetworkQuality(uplinkNetworkQuality, downlinkNetworkQuality) {
|
42956
|
+
this.dispatch(event_names_EventNames.NETWORK_QUALITY, {
|
42957
|
+
uplinkNetworkQuality,
|
42958
|
+
downlinkNetworkQuality
|
42959
|
+
});
|
42920
42960
|
}
|
42921
42961
|
async joinRoom(options) {
|
42922
42962
|
const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
|
@@ -42984,13 +43024,12 @@ 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
|
-
this.dispatch(
|
43032
|
+
this.dispatch(event_names_EventNames.ERROR, e);
|
42994
43033
|
throw e;
|
42995
43034
|
}
|
42996
43035
|
}
|
@@ -42999,7 +43038,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
42999
43038
|
if (isMicOn) await this.engine.publishStream(MediaType$1.AUDIO);
|
43000
43039
|
else await this.engine.unpublishStream(MediaType$1.AUDIO);
|
43001
43040
|
} catch (e) {
|
43002
|
-
this.dispatch(
|
43041
|
+
this.dispatch(event_names_EventNames.ERROR, e);
|
43003
43042
|
throw e;
|
43004
43043
|
}
|
43005
43044
|
}
|
@@ -43019,7 +43058,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
43019
43058
|
await this.engine.unpublishScreen(MediaType$1.VIDEO);
|
43020
43059
|
}
|
43021
43060
|
} catch (e) {
|
43022
|
-
this.dispatch(
|
43061
|
+
this.dispatch(event_names_EventNames.ERROR, e);
|
43023
43062
|
throw e;
|
43024
43063
|
}
|
43025
43064
|
}
|
@@ -43032,7 +43071,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
43032
43071
|
}));
|
43033
43072
|
this._log(`interrupt ${this.joinUserId} ${result}`);
|
43034
43073
|
} catch (e) {
|
43035
|
-
this.dispatch(
|
43074
|
+
this.dispatch(event_names_EventNames.ERROR, e);
|
43036
43075
|
throw e;
|
43037
43076
|
}
|
43038
43077
|
}
|
@@ -43041,7 +43080,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
43041
43080
|
const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify(message));
|
43042
43081
|
this._log(`sendMessage ${this.joinUserId} ${JSON.stringify(message)} ${result}`);
|
43043
43082
|
} catch (e) {
|
43044
|
-
this.dispatch(
|
43083
|
+
this.dispatch(event_names_EventNames.ERROR, e);
|
43045
43084
|
throw e;
|
43046
43085
|
}
|
43047
43086
|
}
|
@@ -43082,7 +43121,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
43082
43121
|
try {
|
43083
43122
|
await this.engine.startAudioPlaybackDeviceTest('audio-test.wav', 200);
|
43084
43123
|
} catch (e) {
|
43085
|
-
this.dispatch(
|
43124
|
+
this.dispatch(event_names_EventNames.ERROR, e);
|
43086
43125
|
throw e;
|
43087
43126
|
}
|
43088
43127
|
}
|
@@ -43090,7 +43129,7 @@ class EngineClient extends RealtimeEventHandler {
|
|
43090
43129
|
try {
|
43091
43130
|
this.engine.stopAudioPlaybackDeviceTest();
|
43092
43131
|
} catch (e) {
|
43093
|
-
this.dispatch(
|
43132
|
+
this.dispatch(event_names_EventNames.ERROR, e);
|
43094
43133
|
throw e;
|
43095
43134
|
}
|
43096
43135
|
}
|
@@ -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,48 +43165,55 @@ 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_names_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
|
-
this.dispatch(
|
43196
|
+
this.dispatch(event_names_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
|
43156
43203
|
this._client.bindEngineEvents();
|
43157
|
-
this._client.on(
|
43204
|
+
this._client.on(event_names_EventNames.ALL, (eventName, data)=>{
|
43158
43205
|
this.dispatch(eventName, data, false);
|
43159
43206
|
});
|
43160
43207
|
if (this._config.suppressStationaryNoise) {
|
43161
43208
|
await this._client.enableAudioNoiseReduction();
|
43162
|
-
this.dispatch(
|
43209
|
+
this.dispatch(event_names_EventNames.SUPPRESS_STATIONARY_NOISE, {});
|
43163
43210
|
}
|
43164
|
-
if (this._config.suppressNonStationaryNoise) {
|
43211
|
+
if (this._config.suppressNonStationaryNoise) try {
|
43165
43212
|
await this._client.initAIAnsExtension();
|
43166
43213
|
this._client.changeAIAnsExtension(true);
|
43167
|
-
this.dispatch(
|
43214
|
+
this.dispatch(event_names_EventNames.SUPPRESS_NON_STATIONARY_NOISE, {});
|
43215
|
+
} catch (error) {
|
43216
|
+
console.warn('Config suppressNonStationaryNoise is not supported', error);
|
43168
43217
|
}
|
43169
43218
|
var _this__config_audioMutedDefault, _this__config_videoConfig_videoOnDefault, _this__config_isAutoSubscribeAudio;
|
43170
43219
|
// Step4 join room
|
@@ -43180,7 +43229,7 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43180
43229
|
await this._client.createLocalStream(roomInfo.uid, this._config.videoConfig);
|
43181
43230
|
// step6 set connected and dispatch connected event
|
43182
43231
|
this.isConnected = true;
|
43183
|
-
this.dispatch(
|
43232
|
+
this.dispatch(event_names_EventNames.CONNECTED, {
|
43184
43233
|
roomId: roomInfo.room_id,
|
43185
43234
|
uid: roomInfo.uid,
|
43186
43235
|
token: roomInfo.token,
|
@@ -43194,7 +43243,7 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43194
43243
|
*/ async interrupt() {
|
43195
43244
|
var _this__client;
|
43196
43245
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.stop());
|
43197
|
-
this.dispatch(
|
43246
|
+
this.dispatch(event_names_EventNames.INTERRUPTED, {});
|
43198
43247
|
}
|
43199
43248
|
/**
|
43200
43249
|
* en: Disconnect from the current session
|
@@ -43204,7 +43253,8 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43204
43253
|
var _this__client;
|
43205
43254
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.disconnect());
|
43206
43255
|
this.isConnected = false;
|
43207
|
-
this.
|
43256
|
+
this._client = null;
|
43257
|
+
this.dispatch(event_names_EventNames.DISCONNECTED, {});
|
43208
43258
|
}
|
43209
43259
|
/**
|
43210
43260
|
* en: Send a message to the bot
|
@@ -43223,14 +43273,14 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43223
43273
|
*/ async setAudioEnable(isEnable) {
|
43224
43274
|
var _this__client;
|
43225
43275
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.changeAudioState(isEnable));
|
43226
|
-
if (isEnable) this.dispatch(
|
43227
|
-
else this.dispatch(
|
43276
|
+
if (isEnable) this.dispatch(event_names_EventNames.AUDIO_UNMUTED, {});
|
43277
|
+
else this.dispatch(event_names_EventNames.AUDIO_MUTED, {});
|
43228
43278
|
}
|
43229
43279
|
async setVideoEnable(isEnable) {
|
43230
43280
|
var _this__client;
|
43231
43281
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.changeVideoState(isEnable));
|
43232
|
-
if (isEnable) this.dispatch(
|
43233
|
-
else this.dispatch(
|
43282
|
+
if (isEnable) this.dispatch(event_names_EventNames.VIDEO_ON, {});
|
43283
|
+
else this.dispatch(event_names_EventNames.VIDEO_OFF, {});
|
43234
43284
|
}
|
43235
43285
|
/**
|
43236
43286
|
* en: Enable audio properties reporting (debug mode only)
|
@@ -43272,7 +43322,7 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43272
43322
|
*/ async setAudioInputDevice(deviceId) {
|
43273
43323
|
var _this__client;
|
43274
43324
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioInputDevice(deviceId));
|
43275
|
-
this.dispatch(
|
43325
|
+
this.dispatch(event_names_EventNames.AUDIO_INPUT_DEVICE_CHANGED, {
|
43276
43326
|
deviceId
|
43277
43327
|
});
|
43278
43328
|
}
|
@@ -43283,14 +43333,14 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43283
43333
|
*/ async setAudioOutputDevice(deviceId) {
|
43284
43334
|
var _this__client;
|
43285
43335
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setAudioOutputDevice(deviceId));
|
43286
|
-
this.dispatch(
|
43336
|
+
this.dispatch(event_names_EventNames.AUDIO_OUTPUT_DEVICE_CHANGED, {
|
43287
43337
|
deviceId
|
43288
43338
|
});
|
43289
43339
|
}
|
43290
43340
|
async setVideoInputDevice(deviceId) {
|
43291
43341
|
var _this__client;
|
43292
43342
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.setVideoInputDevice(deviceId));
|
43293
|
-
this.dispatch(
|
43343
|
+
this.dispatch(event_names_EventNames.VIDEO_INPUT_DEVICE_CHANGED, {
|
43294
43344
|
deviceId
|
43295
43345
|
});
|
43296
43346
|
}
|
@@ -43356,8 +43406,7 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
43356
43406
|
baseURL: defaultBaseURL,
|
43357
43407
|
allowPersonalAccessTokenInBrowser: this._config.allowPersonalAccessTokenInBrowser
|
43358
43408
|
});
|
43359
|
-
this._isTestEnv = 'https://api.coze.cn' !== defaultBaseURL;
|
43360
43409
|
this._isSupportVideo = !!config.videoConfig;
|
43361
43410
|
}
|
43362
43411
|
}
|
43363
|
-
export {
|
43412
|
+
export { event_names_EventNames as EventNames, RealtimeAPIError, RealtimeClient, error_RealtimeError as RealtimeError, src_utils_namespaceObject as RealtimeUtils };
|
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;
|