@coze/realtime-api 1.0.1 → 1.0.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/dist/cjs/index.cjs +129 -34
- package/dist/esm/index.js +129 -34
- package/dist/types/client.d.ts +1 -0
- package/dist/types/event-handler.d.ts +38 -4
- package/dist/types/index.d.ts +2 -0
- package/dist/types/utils.d.ts +3 -2
- package/dist/umd/index.js +129 -34
- package/package.json +2 -2
package/dist/umd/index.js
CHANGED
@@ -7,11 +7,11 @@
|
|
7
7
|
})(self, ()=>(()=>{
|
8
8
|
"use strict";
|
9
9
|
var __webpack_modules__ = {
|
10
|
-
"?
|
10
|
+
"?e272": function() {
|
11
11
|
/* (ignored) */ },
|
12
|
-
"?
|
12
|
+
"?5742": function() {
|
13
13
|
/* (ignored) */ },
|
14
|
-
"?
|
14
|
+
"?9caf": function() {
|
15
15
|
/* (ignored) */ }
|
16
16
|
};
|
17
17
|
/************************************************************************/ // The module cache
|
@@ -79,7 +79,7 @@
|
|
79
79
|
EventNames: ()=>/* reexport */ event_handler_EventNames,
|
80
80
|
RealtimeClient: ()=>/* binding */ RealtimeClient
|
81
81
|
});
|
82
|
-
// NAMESPACE OBJECT: ../../common/temp/default/node_modules/.pnpm/axios@1.7.7/node_modules/axios/lib/platform/common/utils.js
|
82
|
+
// NAMESPACE OBJECT: ../../common/temp/default/node_modules/.pnpm/axios@1.7.7_debug@4.3.7/node_modules/axios/lib/platform/common/utils.js
|
83
83
|
var common_utils_namespaceObject = {};
|
84
84
|
__webpack_require__.r(common_utils_namespaceObject);
|
85
85
|
__webpack_require__.d(common_utils_namespaceObject, {
|
@@ -2141,7 +2141,7 @@
|
|
2141
2141
|
}, {
|
2142
2142
|
highWaterMark: 2
|
2143
2143
|
});
|
2144
|
-
}; // CONCATENATED MODULE: ../../common/temp/default/node_modules/.pnpm/axios@1.7.7/node_modules/axios/lib/adapters/fetch.js
|
2144
|
+
}; // CONCATENATED MODULE: ../../common/temp/default/node_modules/.pnpm/axios@1.7.7_debug@4.3.7/node_modules/axios/lib/adapters/fetch.js
|
2145
2145
|
const isFetchSupported = 'function' == typeof fetch && 'function' == typeof Request && 'function' == typeof Response;
|
2146
2146
|
const isReadableStreamSupported = isFetchSupported && 'function' == typeof ReadableStream;
|
2147
2147
|
// used only inside the fetch adapter
|
@@ -2863,11 +2863,11 @@
|
|
2863
2863
|
// so that it can keep same with es module or cjs
|
2864
2864
|
const { Axios: axios_Axios, AxiosError: axios_AxiosError, CanceledError: axios_CanceledError, isCancel: axios_isCancel, CancelToken: axios_CancelToken, VERSION: axios_VERSION, all: axios_all, Cancel, isAxiosError: axios_isAxiosError, spread: axios_spread, toFormData: axios_toFormData, AxiosHeaders: axios_AxiosHeaders, HttpStatusCode: axios_HttpStatusCode, formToJSON, getAdapter, mergeConfig: axios_mergeConfig } = lib_axios;
|
2865
2865
|
// EXTERNAL MODULE: os (ignored)
|
2866
|
-
var os_ignored_ = __webpack_require__("?
|
2866
|
+
var os_ignored_ = __webpack_require__("?9caf");
|
2867
2867
|
// EXTERNAL MODULE: crypto (ignored)
|
2868
|
-
__webpack_require__("?
|
2868
|
+
__webpack_require__("?e272");
|
2869
2869
|
// EXTERNAL MODULE: jsonwebtoken (ignored)
|
2870
|
-
__webpack_require__("?
|
2870
|
+
__webpack_require__("?5742");
|
2871
2871
|
class APIResource {
|
2872
2872
|
constructor(client){
|
2873
2873
|
this._client = client;
|
@@ -3109,6 +3109,10 @@
|
|
3109
3109
|
}
|
3110
3110
|
}
|
3111
3111
|
const uuid = ()=>(Math.random() * new Date().getTime()).toString();
|
3112
|
+
const handleAdditionalMessages = (additional_messages)=>null == additional_messages ? void 0 : additional_messages.map((i)=>({
|
3113
|
+
...i,
|
3114
|
+
content: 'object' == typeof i.content ? JSON.stringify(i.content) : i.content
|
3115
|
+
}));
|
3112
3116
|
class Chat extends APIResource {
|
3113
3117
|
/**
|
3114
3118
|
* Call the Chat API to send messages to a published Coze agent. | 调用此接口发起一次对话,支持添加上下文
|
@@ -3130,6 +3134,7 @@
|
|
3130
3134
|
const apiUrl = `/v3/chat${conversation_id ? `?conversation_id=${conversation_id}` : ''}`;
|
3131
3135
|
const payload = {
|
3132
3136
|
...rest,
|
3137
|
+
additional_messages: handleAdditionalMessages(params.additional_messages),
|
3133
3138
|
stream: false
|
3134
3139
|
};
|
3135
3140
|
const result = await this._client.post(apiUrl, payload, false, options);
|
@@ -3155,6 +3160,7 @@
|
|
3155
3160
|
const apiUrl = `/v3/chat${conversation_id ? `?conversation_id=${conversation_id}` : ''}`;
|
3156
3161
|
const payload = {
|
3157
3162
|
...rest,
|
3163
|
+
additional_messages: handleAdditionalMessages(params.additional_messages),
|
3158
3164
|
stream: false
|
3159
3165
|
};
|
3160
3166
|
const result = await this._client.post(apiUrl, payload, false, options);
|
@@ -3192,6 +3198,7 @@
|
|
3192
3198
|
const apiUrl = `/v3/chat${conversation_id ? `?conversation_id=${conversation_id}` : ''}`;
|
3193
3199
|
const payload = {
|
3194
3200
|
...rest,
|
3201
|
+
additional_messages: handleAdditionalMessages(params.additional_messages),
|
3195
3202
|
stream: true
|
3196
3203
|
};
|
3197
3204
|
const result = await this._client.post(apiUrl, payload, true, options);
|
@@ -3366,6 +3373,7 @@
|
|
3366
3373
|
* @param params - Required The parameters for creating a conversation | 创建会话所需的参数
|
3367
3374
|
* @param params.messages - Optional Messages in the conversation. | 会话中的消息内容。
|
3368
3375
|
* @param params.meta_data - Optional Additional information when creating a message. | 创建消息时的附加消息。
|
3376
|
+
* @param params.bot_id - Optional Bind and isolate conversation on different bots. | 绑定和隔离不同Bot的会话。
|
3369
3377
|
* @returns Information about the created conversation. | 会话的基础信息。
|
3370
3378
|
*/ async create(params, options) {
|
3371
3379
|
const apiUrl = '/v1/conversation/create';
|
@@ -3383,6 +3391,27 @@
|
|
3383
3391
|
const response = await this._client.get(apiUrl, null, false, options);
|
3384
3392
|
return response.data;
|
3385
3393
|
}
|
3394
|
+
/**
|
3395
|
+
* List all conversations. | 列出 Bot 下所有会话。
|
3396
|
+
* @param params
|
3397
|
+
* @param params.bot_id - Required Bot ID. | Bot ID。
|
3398
|
+
* @param params.page_num - Optional The page number. | 页码,默认值为 1。
|
3399
|
+
* @param params.page_size - Optional The number of conversations per page. | 每页的会话数量,默认值为 50。
|
3400
|
+
* @returns Information about the conversations. | 会话的信息。
|
3401
|
+
*/ async list(params, options) {
|
3402
|
+
const apiUrl = '/v1/conversations';
|
3403
|
+
const response = await this._client.get(apiUrl, params, false, options);
|
3404
|
+
return response.data;
|
3405
|
+
}
|
3406
|
+
/**
|
3407
|
+
* Clear a conversation. | 清空会话。
|
3408
|
+
* @param conversation_id - Required The ID of the conversation. | Conversation ID,即会话的唯一标识。
|
3409
|
+
* @returns Information about the conversation session. | 会话的会话 ID。
|
3410
|
+
*/ async clear(conversation_id, options) {
|
3411
|
+
const apiUrl = `/v1/conversations/${conversation_id}/clear`;
|
3412
|
+
const response = await this._client.post(apiUrl, null, false, options);
|
3413
|
+
return response.data;
|
3414
|
+
}
|
3386
3415
|
constructor(...args){
|
3387
3416
|
super(...args), this.messages = new messages_Messages(this._client);
|
3388
3417
|
}
|
@@ -3421,6 +3450,9 @@
|
|
3421
3450
|
* @param params.bot_id - Optional The ID of the bot associated with the workflow. | 可选 与工作流关联的机器人 ID。
|
3422
3451
|
* @param params.parameters - Optional Parameters for the workflow execution. | 可选 工作流执行的参数。
|
3423
3452
|
* @param params.ext - Optional Additional information for the workflow execution. | 可选 工作流执行的附加信息。
|
3453
|
+
* @param params.execute_mode - Optional The mode in which to execute the workflow. | 可选 工作流执行的模式。
|
3454
|
+
* @param params.connector_id - Optional The ID of the connector to use for the workflow. | 可选 用于工作流的连接器 ID。
|
3455
|
+
* @param params.app_id - Optional The ID of the app. | 可选 要进行会话聊天的 App ID
|
3424
3456
|
* @returns RunWorkflowData | 工作流运行数据
|
3425
3457
|
*/ async create(params, options) {
|
3426
3458
|
const apiUrl = '/v1/workflow/run';
|
@@ -3435,6 +3467,9 @@
|
|
3435
3467
|
* @param params.bot_id - Optional The ID of the bot associated with the workflow. | 可选 与工作流关联的机器人 ID。
|
3436
3468
|
* @param params.parameters - Optional Parameters for the workflow execution. | 可选 工作流执行的参数。
|
3437
3469
|
* @param params.ext - Optional Additional information for the workflow execution. | 可选 工作流执行的附加信息。
|
3470
|
+
* @param params.execute_mode - Optional The mode in which to execute the workflow. | 可选 工作流执行的模式。
|
3471
|
+
* @param params.connector_id - Optional The ID of the connector to use for the workflow. | 可选 用于工作流的连接器 ID。
|
3472
|
+
* @param params.app_id - Optional The ID of the app. | 可选 要进行会话聊天的 App ID
|
3438
3473
|
* @returns Stream<WorkflowEvent, { id: string; event: string; data: string }> | 工作流事件流
|
3439
3474
|
*/ async *stream(params, options) {
|
3440
3475
|
const apiUrl = '/v1/workflow/stream_run';
|
@@ -3630,7 +3665,7 @@
|
|
3630
3665
|
super(...args), this.rooms = new Rooms(this._client), this.voices = new Voices(this._client), this.speech = new Speech(this._client);
|
3631
3666
|
}
|
3632
3667
|
}
|
3633
|
-
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.0.
|
3668
|
+
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.0.14","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>","type":"module","exports":{".":{"require":"./dist/cjs/index.cjs","import":"./dist/esm/index.js","types":"./dist/types/index.d.ts"}},"main":"dist/cjs/index.cjs","module":"dist/esm/index.js","browser":{"crypto":false,"os":false,"jsonwebtoken":false},"types":"dist/types/index.d.ts","files":["dist","LICENSE","README.md","!**/*.tsbuildinfo"],"scripts":{"build":"rm -rf dist && rslib build","format":"prettier --write .","lint":"eslint ./ --cache --quiet","prepublishOnly":"npm run build","start":"rm -rf dist && rslib build -w","test":"vitest","test:cov":"vitest --coverage --run"},"dependencies":{"jsonwebtoken":"^9.0.2"},"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/uuid":"^9.0.1","@types/whatwg-fetch":"^0.0.33","@vitest/coverage-v8":"~2.1.4","axios":"^1.7.7","typescript":"^5.5.3","vitest":"~2.1.4"},"peerDependencies":{"axios":"^1.7.1"}}'); // CONCATENATED MODULE: ./src/version.ts
|
3634
3669
|
const { version: esm_version } = package_namespaceObject;
|
3635
3670
|
const getEnv = ()=>{
|
3636
3671
|
const nodeVersion = process.version.slice(1); // Remove 'v' prefix
|
@@ -3693,6 +3728,11 @@
|
|
3693
3728
|
async function fetchAPI(url) {
|
3694
3729
|
let options = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
|
3695
3730
|
const axiosInstance = options.axiosInstance || lib_axios;
|
3731
|
+
// Add version check for streaming requests
|
3732
|
+
if (options.isStreaming && isAxiosStatic(axiosInstance)) {
|
3733
|
+
const axiosVersion = axiosInstance.VERSION || lib_axios.VERSION;
|
3734
|
+
if (!axiosVersion || compareVersions(axiosVersion, '1.7.1') < 0) throw new CozeError('Streaming requests require axios version 1.7.1 or higher. Please upgrade your axios version.');
|
3735
|
+
}
|
3696
3736
|
const response = await axiosInstance({
|
3697
3737
|
url,
|
3698
3738
|
responseType: options.isStreaming ? 'stream' : 'json',
|
@@ -3705,12 +3745,12 @@
|
|
3705
3745
|
async *stream () {
|
3706
3746
|
try {
|
3707
3747
|
const stream = response.data;
|
3708
|
-
const reader = stream[Symbol.asyncIterator]();
|
3748
|
+
const reader = stream[Symbol.asyncIterator] ? stream[Symbol.asyncIterator]() : stream.getReader();
|
3709
3749
|
const decoder = new TextDecoder();
|
3710
3750
|
const fieldValues = {};
|
3711
3751
|
let buffer = '';
|
3712
3752
|
while(true){
|
3713
|
-
const { done, value } = await reader.next();
|
3753
|
+
const { done, value } = await (reader.next ? reader.next() : reader.read());
|
3714
3754
|
if (done) {
|
3715
3755
|
if (buffer) {
|
3716
3756
|
// If the stream ends without a newline, it means an error occurred
|
@@ -3744,6 +3784,21 @@
|
|
3744
3784
|
response
|
3745
3785
|
};
|
3746
3786
|
}
|
3787
|
+
// Add version comparison utility
|
3788
|
+
function compareVersions(v1, v2) {
|
3789
|
+
const v1Parts = v1.split('.').map(Number);
|
3790
|
+
const v2Parts = v2.split('.').map(Number);
|
3791
|
+
for(let i = 0; i < 3; i++){
|
3792
|
+
const part1 = v1Parts[i] || 0;
|
3793
|
+
const part2 = v2Parts[i] || 0;
|
3794
|
+
if (part1 > part2) return 1;
|
3795
|
+
if (part1 < part2) return -1;
|
3796
|
+
}
|
3797
|
+
return 0;
|
3798
|
+
}
|
3799
|
+
function isAxiosStatic(instance) {
|
3800
|
+
return !!(null == instance ? void 0 : instance.Axios);
|
3801
|
+
}
|
3747
3802
|
/**
|
3748
3803
|
* default coze base URL is api.coze.com
|
3749
3804
|
*/ const COZE_COM_BASE_URL = 'https://api.coze.com';
|
@@ -3767,12 +3822,15 @@
|
|
3767
3822
|
const fullUrl = `${this.baseURL}${apiUrl}`;
|
3768
3823
|
const fetchOptions = this.buildOptions(method, body, options);
|
3769
3824
|
fetchOptions.isStreaming = isStream;
|
3825
|
+
fetchOptions.axiosInstance = this.axiosInstance;
|
3770
3826
|
this.debugLog(`--- request url: ${fullUrl}`);
|
3771
3827
|
this.debugLog('--- request options:', fetchOptions);
|
3772
3828
|
const { response, stream, json } = await fetchAPI(fullUrl, fetchOptions);
|
3773
3829
|
this.debugLog(`--- response status: ${response.status}`);
|
3774
3830
|
this.debugLog('--- response headers: ', response.headers);
|
3775
|
-
|
3831
|
+
var _response_headers;
|
3832
|
+
// Taro use `header`
|
3833
|
+
const contentType = (null !== (_response_headers = response.headers) && void 0 !== _response_headers ? _response_headers : response.header)['content-type'];
|
3776
3834
|
if (isStream) {
|
3777
3835
|
if (contentType && contentType.includes('application/json')) {
|
3778
3836
|
const result = await json();
|
@@ -3820,6 +3878,7 @@
|
|
3820
3878
|
this.baseURL = config.baseURL || COZE_COM_BASE_URL;
|
3821
3879
|
this.token = config.token;
|
3822
3880
|
this.axiosOptions = config.axiosOptions || {};
|
3881
|
+
this.axiosInstance = config.axiosInstance;
|
3823
3882
|
this.debug = config.debug || false;
|
3824
3883
|
this.allowPersonalAccessTokenInBrowser = config.allowPersonalAccessTokenInBrowser || false;
|
3825
3884
|
this.headers = config.headers;
|
@@ -12643,8 +12702,8 @@
|
|
12643
12702
|
}
|
12644
12703
|
var _navigator$userAgent$, isFirefox = "mozilla" === getBrowser(), isSafari = "safari" === getBrowser(), isChrome = "chrome-stable" === getBrowser(), isCriOS = !isSSR2() && /CriOS/i.test(userAgentString), isEdgeForDesktop = !isSSR2() && /Edg\//i.test(userAgentString), isEdgeForAndroid = !isSSR2() && /EdgA/i.test(userAgentString), isEdgeForIOS = !isSSR2() && /EdgiOS/i.test(userAgentString), isEdge = isEdgeForDesktop || isEdgeForAndroid || isEdgeForIOS, isDingTalk = !isSSR2() && /DingTalk/i.test(navigator.userAgent), isOpera = !isSSR2() && /OPR\//.test(navigator.userAgent), isIPad = !isSSR2() && (!!/(iPad)/i.exec(userAgentString) || /Macintosh/i.test(userAgentString) && "ontouchend" in document), isMac = !isSSR2() && /Macintosh/i.test(userAgentString), isWeChat = !isSSR2() && /MicroMessenger/i.test(userAgentString), isMobile = !isSSR2() && _includesInstanceProperty(_context$3 = userAgentString.toLowerCase()).call(_context$3, "mobile"), isIOS = !isSSR2() && !!/(iPhone|iPad|iPod)/i.exec(userAgentString), isAndroid = !isSSR2() && /Android/i.test(userAgentString), isWindows = !isSSR2() && /Windows/i.test(userAgentString), isOpenHarmony = !isSSR2() && /OpenHarmony/i.test(userAgentString), sv = 0, sv2 = "0", index_esm_min_v = !isSSR2() && (null === (_userAgentString$matc = userAgentString.match(/version\/(\d+)/i)) || void 0 === _userAgentString$matc ? void 0 : _userAgentString$matc[1]);
|
12645
12704
|
isSafari && index_esm_min_v && (sv = Number(index_esm_min_v), sv2 = null === (_navigator$userAgent$ = navigator.userAgent.match(/version\/(\d+\.\d+)/i)) || void 0 === _navigator$userAgent$ ? void 0 : _navigator$userAgent$[1]);
|
12646
|
-
var
|
12647
|
-
isFirefox &&
|
12705
|
+
var index_esm_min_v2 = !isSSR2() && (null === (_userAgentString$matc2 = userAgentString.match(/Firefox\/(\d+)/i)) || void 0 === _userAgentString$matc2 ? void 0 : _userAgentString$matc2[1]);
|
12706
|
+
isFirefox && index_esm_min_v2 && (sv = Number(index_esm_min_v2));
|
12648
12707
|
var safariVersion = sv, firefoxVersion = sv, safariMinorVersion = sv2, iOSVersion = null !== (_ref = !isSSR2() && (null === (_userAgentString$matc3 = userAgentString.match(/ ([\d_]+) like Mac OS X/i)) || void 0 === _userAgentString$matc3 || null === (_userAgentString$matc4 = _userAgentString$matc3[1]) || void 0 === _userAgentString$matc4 ? void 0 : _mapInstanceProperty(_context2 = _userAgentString$matc4.split("_")).call(_context2, function(e) {
|
12649
12708
|
return _parseInt$7(e);
|
12650
12709
|
}))) && void 0 !== _ref ? _ref : [], cv = 0, cvs = !isSSR2() && (null === (_userAgentString$matc5 = userAgentString.match(/Chrome\/(\d+)/i)) || void 0 === _userAgentString$matc5 ? void 0 : _userAgentString$matc5[1]);
|
@@ -38245,19 +38304,22 @@
|
|
38245
38304
|
/**
|
38246
38305
|
* Get audio devices
|
38247
38306
|
* @returns Promise<AudioDevices> Object containing arrays of audio input and output devices
|
38248
|
-
*/ const getAudioDevices = async ()
|
38249
|
-
|
38307
|
+
*/ const getAudioDevices = async function() {
|
38308
|
+
let { video = false } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
|
38309
|
+
let devices = [];
|
38310
|
+
devices = video ? await index_esm_min_index.enumerateDevices() : await [
|
38311
|
+
...await index_esm_min_index.enumerateAudioCaptureDevices(),
|
38312
|
+
...await index_esm_min_index.enumerateAudioPlaybackDevices()
|
38313
|
+
];
|
38250
38314
|
if (!(null == devices ? void 0 : devices.length)) return {
|
38251
38315
|
audioInputs: [],
|
38252
38316
|
audioOutputs: [],
|
38253
|
-
videoInputs: []
|
38254
|
-
videoOutputs: []
|
38317
|
+
videoInputs: []
|
38255
38318
|
};
|
38256
38319
|
return {
|
38257
38320
|
audioInputs: devices.filter((i)=>i.deviceId && 'audioinput' === i.kind),
|
38258
38321
|
audioOutputs: devices.filter((i)=>i.deviceId && 'audiooutput' === i.kind),
|
38259
|
-
videoInputs: devices.filter((i)=>i.deviceId && 'videoinput' === i.kind)
|
38260
|
-
videoOutputs: devices.filter((i)=>i.deviceId && 'videooutput' === i.kind)
|
38322
|
+
videoInputs: devices.filter((i)=>i.deviceId && 'videoinput' === i.kind)
|
38261
38323
|
};
|
38262
38324
|
};
|
38263
38325
|
var error_RealtimeError = /*#__PURE__*/ function(RealtimeError) {
|
@@ -38360,6 +38422,34 @@
|
|
38360
38422
|
* en: Bot left
|
38361
38423
|
* zh: Bot 离开
|
38362
38424
|
*/ EventNames["BOT_LEAVE"] = "server.bot.leave";
|
38425
|
+
/**
|
38426
|
+
* en: Audio speech started
|
38427
|
+
* zh: 开始说话
|
38428
|
+
*/ EventNames["AUDIO_AGENT_SPEECH_STARTED"] = "server.audio.agent.speech_started";
|
38429
|
+
/**
|
38430
|
+
* en: Audio speech stopped
|
38431
|
+
* zh: 停止说话
|
38432
|
+
*/ EventNames["AUDIO_SPEECH_STOPPED"] = "server.audio.speech_stopped";
|
38433
|
+
/**
|
38434
|
+
* en: Server error
|
38435
|
+
* zh: 服务端错误
|
38436
|
+
*/ EventNames["SERVER_ERROR"] = "server.error";
|
38437
|
+
/**
|
38438
|
+
* en: User speech started
|
38439
|
+
* zh: 用户开始说话
|
38440
|
+
*/ EventNames["AUDIO_USER_SPEECH_STARTED"] = "server.audio.user.speech_started";
|
38441
|
+
/**
|
38442
|
+
* en: User speech stopped
|
38443
|
+
* zh: 用户停止说话
|
38444
|
+
*/ EventNames["AUDIO_USER_SPEECH_STOPPED"] = "server.audio.user.speech_stopped";
|
38445
|
+
/**
|
38446
|
+
* en: User successfully enters the room
|
38447
|
+
* zh: 用户成功进入房间后,会收到该事件
|
38448
|
+
*/ EventNames["SESSION_CREATED"] = "server.session.created";
|
38449
|
+
/**
|
38450
|
+
* en: Session updated
|
38451
|
+
* zh: 会话更新
|
38452
|
+
*/ EventNames["SESSION_UPDATE"] = "server.session.update";
|
38363
38453
|
return EventNames;
|
38364
38454
|
}({});
|
38365
38455
|
class RealtimeEventHandler {
|
@@ -38390,7 +38480,8 @@
|
|
38390
38480
|
}
|
38391
38481
|
}
|
38392
38482
|
dispatch(eventName, event) {
|
38393
|
-
|
38483
|
+
let consoleLog = !(arguments.length > 2) || void 0 === arguments[2] || arguments[2];
|
38484
|
+
if (consoleLog) this._log(`dispatch ${eventName} event`);
|
38394
38485
|
const handlers = (this.eventHandlers[eventName] || []).slice();
|
38395
38486
|
this._dispatchToHandlers(eventName, event, handlers);
|
38396
38487
|
const allHandlers = (this.eventHandlers["realtime.event"] || []).slice();
|
@@ -41659,7 +41750,7 @@
|
|
41659
41750
|
this.engine.on(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
|
41660
41751
|
this.engine.on(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
|
41661
41752
|
this.engine.on(index_esm_min_index.events.onError, this.handleEventError);
|
41662
|
-
this.engine.on(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
41753
|
+
if (this._isSupportVideo) this.engine.on(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
41663
41754
|
if (this._debug) {
|
41664
41755
|
this.engine.on(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
41665
41756
|
this.engine.on(index_esm_min_index.events.onRemoteAudioPropertiesReport, this.handleRemoteAudioPropertiesReport);
|
@@ -41670,7 +41761,7 @@
|
|
41670
41761
|
this.engine.off(index_esm_min_index.events.onUserJoined, this.handleUserJoin);
|
41671
41762
|
this.engine.off(index_esm_min_index.events.onUserLeave, this.handleUserLeave);
|
41672
41763
|
this.engine.off(index_esm_min_index.events.onError, this.handleEventError);
|
41673
|
-
this.engine.off(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
41764
|
+
if (this._isSupportVideo) this.engine.off(index_esm_min_index.events.onPlayerEvent, this.handlePlayerEvent);
|
41674
41765
|
if (this._debug) {
|
41675
41766
|
this.engine.off(index_esm_min_index.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
41676
41767
|
this.engine.off(index_esm_min_index.events.onRemoteAudioPropertiesReport, this.handleRemoteAudioPropertiesReport);
|
@@ -41715,13 +41806,13 @@
|
|
41715
41806
|
this.dispatch(event_handler_EventNames.PLAYER_EVENT, event);
|
41716
41807
|
}
|
41717
41808
|
async joinRoom(options) {
|
41718
|
-
const { token, roomId, uid, audioMutedDefault, videoOnDefault } = options;
|
41809
|
+
const { token, roomId, uid, audioMutedDefault, videoOnDefault, isAutoSubscribeAudio } = options;
|
41719
41810
|
try {
|
41720
41811
|
await this.engine.joinRoom(token, roomId, {
|
41721
41812
|
userId: uid
|
41722
41813
|
}, {
|
41723
41814
|
isAutoPublish: !audioMutedDefault,
|
41724
|
-
isAutoSubscribeAudio
|
41815
|
+
isAutoSubscribeAudio,
|
41725
41816
|
isAutoSubscribeVideo: this._isSupportVideo && videoOnDefault
|
41726
41817
|
});
|
41727
41818
|
} catch (e) {
|
@@ -41735,14 +41826,18 @@
|
|
41735
41826
|
await this.engine.startAudioCapture(deviceId);
|
41736
41827
|
}
|
41737
41828
|
async setAudioOutputDevice(deviceId) {
|
41738
|
-
const devices = await getAudioDevices(
|
41829
|
+
const devices = await getAudioDevices({
|
41830
|
+
video: false
|
41831
|
+
});
|
41739
41832
|
if (-1 === devices.audioOutputs.findIndex((i)=>i.deviceId === deviceId)) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, `Audio output device not found: ${deviceId}`);
|
41740
41833
|
await this.engine.setAudioPlaybackDevice(deviceId);
|
41741
41834
|
}
|
41742
41835
|
async createLocalStream(userId, videoConfig) {
|
41743
|
-
const devices = await getAudioDevices(
|
41744
|
-
|
41745
|
-
|
41836
|
+
const devices = await getAudioDevices({
|
41837
|
+
video: this._isSupportVideo
|
41838
|
+
});
|
41839
|
+
if (!devices.audioInputs.length) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, 'Failed to get audio devices');
|
41840
|
+
if (this._isSupportVideo && !devices.videoInputs.length) throw new RealtimeAPIError(error_RealtimeError.DEVICE_ACCESS_ERROR, 'Failed to get video devices');
|
41746
41841
|
await this.engine.startAudioCapture(devices.audioInputs[0].deviceId);
|
41747
41842
|
if (this._isSupportVideo && (null == videoConfig ? void 0 : videoConfig.videoOnDefault)) await this.engine.startVideoCapture(devices.videoInputs[0].deviceId);
|
41748
41843
|
if (this._isSupportVideo) this.engine.setLocalVideoPlayer(StreamIndex$1.STREAM_INDEX_MAIN, {
|
@@ -41895,7 +41990,7 @@
|
|
41895
41990
|
// Step3 bind engine events
|
41896
41991
|
this._client.bindEngineEvents();
|
41897
41992
|
this._client.on(event_handler_EventNames.ALL, (eventName, data)=>{
|
41898
|
-
this.dispatch(eventName, data);
|
41993
|
+
this.dispatch(eventName, data, false);
|
41899
41994
|
});
|
41900
41995
|
if (this._config.suppressStationaryNoise) {
|
41901
41996
|
await this._client.enableAudioNoiseReduction();
|
@@ -41906,14 +42001,15 @@
|
|
41906
42001
|
this._client.changeAIAnsExtension(true);
|
41907
42002
|
this.dispatch(event_handler_EventNames.SUPPRESS_NON_STATIONARY_NOISE, {});
|
41908
42003
|
}
|
41909
|
-
var _this__config_audioMutedDefault, _this__config_videoConfig_videoOnDefault;
|
42004
|
+
var _this__config_audioMutedDefault, _this__config_videoConfig_videoOnDefault, _this__config_isAutoSubscribeAudio;
|
41910
42005
|
// Step4 join room
|
41911
42006
|
await this._client.joinRoom({
|
41912
42007
|
token: roomInfo.token,
|
41913
42008
|
roomId: roomInfo.room_id,
|
41914
42009
|
uid: roomInfo.uid,
|
41915
42010
|
audioMutedDefault: null !== (_this__config_audioMutedDefault = this._config.audioMutedDefault) && void 0 !== _this__config_audioMutedDefault && _this__config_audioMutedDefault,
|
41916
|
-
videoOnDefault: null === (_this__config_videoConfig_videoOnDefault = null === (_this__config_videoConfig = this._config.videoConfig) || void 0 === _this__config_videoConfig ? void 0 : _this__config_videoConfig.videoOnDefault) || void 0 === _this__config_videoConfig_videoOnDefault || _this__config_videoConfig_videoOnDefault
|
42011
|
+
videoOnDefault: null === (_this__config_videoConfig_videoOnDefault = null === (_this__config_videoConfig = this._config.videoConfig) || void 0 === _this__config_videoConfig ? void 0 : _this__config_videoConfig.videoOnDefault) || void 0 === _this__config_videoConfig_videoOnDefault || _this__config_videoConfig_videoOnDefault,
|
42012
|
+
isAutoSubscribeAudio: null === (_this__config_isAutoSubscribeAudio = this._config.isAutoSubscribeAudio) || void 0 === _this__config_isAutoSubscribeAudio || _this__config_isAutoSubscribeAudio
|
41917
42013
|
});
|
41918
42014
|
// Step5 create local stream
|
41919
42015
|
await this._client.createLocalStream(roomInfo.uid, this._config.videoConfig);
|
@@ -41925,7 +42021,6 @@
|
|
41925
42021
|
token: roomInfo.token,
|
41926
42022
|
appId: roomInfo.app_id
|
41927
42023
|
});
|
41928
|
-
this._log('dispatch client.connected event');
|
41929
42024
|
}
|
41930
42025
|
/**
|
41931
42026
|
* en: Interrupt the current conversation
|
@@ -41935,7 +42030,6 @@
|
|
41935
42030
|
var _this__client;
|
41936
42031
|
await (null === (_this__client = this._client) || void 0 === _this__client ? void 0 : _this__client.stop());
|
41937
42032
|
this.dispatch(event_handler_EventNames.INTERRUPTED, {});
|
41938
|
-
this._log('dispatch client.interrupted event');
|
41939
42033
|
}
|
41940
42034
|
/**
|
41941
42035
|
* en: Disconnect from the current session
|
@@ -42057,6 +42151,7 @@
|
|
42057
42151
|
* 可选,默认是否抑制静态噪声,默认值为 false。
|
42058
42152
|
* @param config.suppressNonStationaryNoise - Optional, suppress non-stationary noise, defaults to false. |
|
42059
42153
|
* 可选,默认是否抑制非静态噪声,默认值为 false。
|
42154
|
+
* @param config.isAutoSubscribeAudio - Optional, whether to automatically subscribe to bot reply audio streams, defaults to true. |
|
42060
42155
|
*/ constructor(config){
|
42061
42156
|
super(config.debug), this._client = null, this.isConnected = false, this._isTestEnv = false, this._isSupportVideo = false;
|
42062
42157
|
this._config = config;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@coze/realtime-api",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.2",
|
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",
|
@@ -51,7 +51,7 @@
|
|
51
51
|
"test:cov": "vitest --coverage --run"
|
52
52
|
},
|
53
53
|
"dependencies": {
|
54
|
-
"@coze/api": "1.0.
|
54
|
+
"@coze/api": "1.0.14",
|
55
55
|
"@volcengine/rtc": "^4.62.1"
|
56
56
|
},
|
57
57
|
"devDependencies": {
|