@coze/realtime-api 1.0.4-beta.2 → 1.0.5
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 +761 -9
- package/dist/cjs/index.cjs.LICENSE.txt +22 -0
- package/dist/esm/index.js +761 -9
- package/dist/esm/index.js.LICENSE.txt +22 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/umd/index.js +761 -9
- package/dist/umd/index.js.LICENSE.txt +22 -0
- package/package.json +4 -4
package/dist/esm/index.js
CHANGED
@@ -1,9 +1,16 @@
|
|
1
1
|
/*! For license information please see index.js.LICENSE.txt */
|
2
2
|
var __webpack_modules__ = {
|
3
|
+
"../../common/temp/default/node_modules/.pnpm/ws@8.18.0/node_modules/ws/browser.js": function(module) {
|
4
|
+
module.exports = function() {
|
5
|
+
throw new Error("ws does not work in the browser. Browser clients must use the native WebSocket object");
|
6
|
+
};
|
7
|
+
},
|
3
8
|
"?666e": function() {
|
4
9
|
/* (ignored) */ },
|
5
10
|
"?79fd": function() {
|
6
11
|
/* (ignored) */ },
|
12
|
+
"?8dee": function() {
|
13
|
+
/* (ignored) */ },
|
7
14
|
"?9050": function() {
|
8
15
|
/* (ignored) */ }
|
9
16
|
};
|
@@ -229,8 +236,6 @@ class error_APIError extends CozeError {
|
|
229
236
|
if ((null == error ? void 0 : error.detail) && msg !== error.detail) list.push(`detail: ${error.detail}`);
|
230
237
|
const logId = (null == error ? void 0 : error.logid) || (null == headers ? void 0 : headers['x-tt-logid']);
|
231
238
|
if (logId) list.push(`logid: ${logId}`);
|
232
|
-
const help_doc = null == error ? void 0 : error.help_doc;
|
233
|
-
if (help_doc) list.push(`help doc: ${help_doc}`);
|
234
239
|
return list.join(', ');
|
235
240
|
}
|
236
241
|
if (status) return `http status code: ${status} (no body)`;
|
@@ -253,16 +258,15 @@ class error_APIError extends CozeError {
|
|
253
258
|
return new error_APIError(status, error, message, headers);
|
254
259
|
}
|
255
260
|
constructor(status, error, message, headers){
|
256
|
-
var
|
261
|
+
var _error_detail, _error_error;
|
257
262
|
super(`${error_APIError.makeMessage(status, error, message, headers)}`);
|
258
263
|
this.status = status;
|
259
264
|
this.headers = headers;
|
260
|
-
this.logid = null == headers ? void 0 : headers['x-tt-logid'];
|
265
|
+
this.logid = (null == error ? void 0 : null === (_error_detail = error.detail) || void 0 === _error_detail ? void 0 : _error_detail.logid) || (null == headers ? void 0 : headers['x-tt-logid']);
|
261
266
|
// this.error = error;
|
262
267
|
this.code = null == error ? void 0 : error.code;
|
263
268
|
this.msg = null == error ? void 0 : error.msg;
|
264
269
|
this.detail = null == error ? void 0 : null === (_error_error = error.error) || void 0 === _error_error ? void 0 : _error_error.detail;
|
265
|
-
this.help_doc = null == error ? void 0 : null === (_error_error1 = error.error) || void 0 === _error_error1 ? void 0 : _error_error1.help_doc;
|
266
270
|
this.rawError = error;
|
267
271
|
}
|
268
272
|
}
|
@@ -3914,10 +3918,705 @@ class Templates extends APIResource {
|
|
3914
3918
|
return response.data;
|
3915
3919
|
}
|
3916
3920
|
}
|
3921
|
+
class chat_Chat extends APIResource {
|
3922
|
+
async create(botId, options) {
|
3923
|
+
const apiUrl = `/v1/chat?bot_id=${botId}`;
|
3924
|
+
return await this._client.makeWebsocket(apiUrl, options);
|
3925
|
+
}
|
3926
|
+
}
|
3927
|
+
class transcriptions_Transcriptions extends APIResource {
|
3928
|
+
async create(options) {
|
3929
|
+
const apiUrl = '/v1/audio/transcriptions';
|
3930
|
+
return await this._client.makeWebsocket(apiUrl, options);
|
3931
|
+
}
|
3932
|
+
}
|
3933
|
+
class speech_Speech extends APIResource {
|
3934
|
+
async create(options) {
|
3935
|
+
const apiUrl = '/v1/audio/speech';
|
3936
|
+
return await this._client.makeWebsocket(apiUrl, options);
|
3937
|
+
}
|
3938
|
+
}
|
3939
|
+
class websockets_audio_Audio extends APIResource {
|
3940
|
+
constructor(...args){
|
3941
|
+
super(...args), this.speech = new speech_Speech(this._client), this.transcriptions = new transcriptions_Transcriptions(this._client);
|
3942
|
+
}
|
3943
|
+
}
|
3944
|
+
// Common types (not exported)
|
3945
|
+
// Keep all existing exports but use the base types where applicable
|
3946
|
+
var types_WebsocketsEventType = /*#__PURE__*/ function(WebsocketsEventType) {
|
3947
|
+
// Common
|
3948
|
+
/** SDK error */ WebsocketsEventType["CLIENT_ERROR"] = "client_error";
|
3949
|
+
/** Connection closed */ WebsocketsEventType["CLOSED"] = "closed";
|
3950
|
+
// Error
|
3951
|
+
/** Received error event */ WebsocketsEventType["ERROR"] = "error";
|
3952
|
+
// v1/audio/speech
|
3953
|
+
/** Send text to server */ WebsocketsEventType["INPUT_TEXT_BUFFER_APPEND"] = "input_text_buffer.append";
|
3954
|
+
/** No text to send, after audio all received, can close connection */ WebsocketsEventType["INPUT_TEXT_BUFFER_COMPLETE"] = "input_text_buffer.complete";
|
3955
|
+
/** Send speech config to server */ WebsocketsEventType["SPEECH_UPDATE"] = "speech.update";
|
3956
|
+
/** Received `speech.updated` event */ WebsocketsEventType["SPEECH_UPDATED"] = "speech.updated";
|
3957
|
+
/** After speech created */ WebsocketsEventType["SPEECH_CREATED"] = "speech.created";
|
3958
|
+
/** Received `input_text_buffer.complete` event */ WebsocketsEventType["INPUT_TEXT_BUFFER_COMPLETED"] = "input_text_buffer.completed";
|
3959
|
+
/** Received `speech.update` event */ WebsocketsEventType["SPEECH_AUDIO_UPDATE"] = "speech.audio.update";
|
3960
|
+
/** All audio received, can close connection */ WebsocketsEventType["SPEECH_AUDIO_COMPLETED"] = "speech.audio.completed";
|
3961
|
+
// v1/audio/transcriptions
|
3962
|
+
/** Send audio to server */ WebsocketsEventType["INPUT_AUDIO_BUFFER_APPEND"] = "input_audio_buffer.append";
|
3963
|
+
/** No audio to send, after text all received, can close connection */ WebsocketsEventType["INPUT_AUDIO_BUFFER_COMPLETE"] = "input_audio_buffer.complete";
|
3964
|
+
/** Send transcriptions config to server */ WebsocketsEventType["TRANSCRIPTIONS_UPDATE"] = "transcriptions.update";
|
3965
|
+
/** Send `input_audio_buffer.clear` event */ WebsocketsEventType["INPUT_AUDIO_BUFFER_CLEAR"] = "input_audio_buffer.clear";
|
3966
|
+
/** After transcriptions created */ WebsocketsEventType["TRANSCRIPTIONS_CREATED"] = "transcriptions.created";
|
3967
|
+
/** Received `input_audio_buffer.complete` event */ WebsocketsEventType["INPUT_AUDIO_BUFFER_COMPLETED"] = "input_audio_buffer.completed";
|
3968
|
+
/** Received `transcriptions.update` event */ WebsocketsEventType["TRANSCRIPTIONS_MESSAGE_UPDATE"] = "transcriptions.message.update";
|
3969
|
+
/** All audio received, can close connection */ WebsocketsEventType["TRANSCRIPTIONS_MESSAGE_COMPLETED"] = "transcriptions.message.completed";
|
3970
|
+
/** Received `input_audio_buffer.cleared` event */ WebsocketsEventType["INPUT_AUDIO_BUFFER_CLEARED"] = "input_audio_buffer.cleared";
|
3971
|
+
/** Received `transcriptions.updated` event */ WebsocketsEventType["TRANSCRIPTIONS_UPDATED"] = "transcriptions.updated";
|
3972
|
+
// v1/chat
|
3973
|
+
/** Send chat config to server */ WebsocketsEventType["CHAT_UPDATE"] = "chat.update";
|
3974
|
+
/** Send tool outputs to server */ WebsocketsEventType["CONVERSATION_CHAT_SUBMIT_TOOL_OUTPUTS"] = "conversation.chat.submit_tool_outputs";
|
3975
|
+
/** After chat created */ WebsocketsEventType["CHAT_CREATED"] = "chat.created";
|
3976
|
+
/** After chat updated */ WebsocketsEventType["CHAT_UPDATED"] = "chat.updated";
|
3977
|
+
/** Audio AST completed, chat started */ WebsocketsEventType["CONVERSATION_CHAT_CREATED"] = "conversation.chat.created";
|
3978
|
+
/** Message created */ WebsocketsEventType["CONVERSATION_MESSAGE_CREATE"] = "conversation.message.create";
|
3979
|
+
/** Clear conversation */ WebsocketsEventType["CONVERSATION_CLEAR"] = "conversation.clear";
|
3980
|
+
/** Chat in progress */ WebsocketsEventType["CONVERSATION_CHAT_IN_PROGRESS"] = "conversation.chat.in_progress";
|
3981
|
+
/** Get agent text message update */ WebsocketsEventType["CONVERSATION_MESSAGE_DELTA"] = "conversation.message.delta";
|
3982
|
+
/** Need plugin submit */ WebsocketsEventType["CONVERSATION_CHAT_REQUIRES_ACTION"] = "conversation.chat.requires_action";
|
3983
|
+
/** Message completed */ WebsocketsEventType["CONVERSATION_MESSAGE_COMPLETED"] = "conversation.message.completed";
|
3984
|
+
/** Get agent audio message update */ WebsocketsEventType["CONVERSATION_AUDIO_DELTA"] = "conversation.audio.delta";
|
3985
|
+
/** Audio message completed */ WebsocketsEventType["CONVERSATION_AUDIO_COMPLETED"] = "conversation.audio.completed";
|
3986
|
+
/** All message received, can close connection */ WebsocketsEventType["CONVERSATION_CHAT_COMPLETED"] = "conversation.chat.completed";
|
3987
|
+
/** Chat failed */ WebsocketsEventType["CONVERSATION_CHAT_FAILED"] = "conversation.chat.failed";
|
3988
|
+
/** Received `conversation.cleared` event */ WebsocketsEventType["CONVERSATION_CLEARED"] = "conversation.cleared";
|
3989
|
+
return WebsocketsEventType;
|
3990
|
+
}({});
|
3991
|
+
class Websockets extends APIResource {
|
3992
|
+
constructor(...args){
|
3993
|
+
super(...args), this.audio = new websockets_audio_Audio(this._client), this.chat = new chat_Chat(this._client);
|
3994
|
+
}
|
3995
|
+
}
|
3996
|
+
// EXTERNAL MODULE: ../../common/temp/default/node_modules/.pnpm/ws@8.18.0/node_modules/ws/browser.js
|
3997
|
+
var ws_browser = __webpack_require__("../../common/temp/default/node_modules/.pnpm/ws@8.18.0/node_modules/ws/browser.js");
|
3998
|
+
var browser_default = /*#__PURE__*/ __webpack_require__.n(ws_browser);
|
3999
|
+
/*! *****************************************************************************
|
4000
|
+
Copyright (c) Microsoft Corporation. All rights reserved.
|
4001
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
4002
|
+
this file except in compliance with the License. You may obtain a copy of the
|
4003
|
+
License at http://www.apache.org/licenses/LICENSE-2.0
|
4004
|
+
|
4005
|
+
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
4006
|
+
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
4007
|
+
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
4008
|
+
MERCHANTABLITY OR NON-INFRINGEMENT.
|
4009
|
+
|
4010
|
+
See the Apache Version 2.0 License for specific language governing permissions
|
4011
|
+
and limitations under the License.
|
4012
|
+
***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(d, b) {
|
4013
|
+
extendStatics = Object.setPrototypeOf || ({
|
4014
|
+
__proto__: []
|
4015
|
+
}) instanceof Array && function(d, b) {
|
4016
|
+
d.__proto__ = b;
|
4017
|
+
} || function(d, b) {
|
4018
|
+
for(var p in b)if (b.hasOwnProperty(p)) d[p] = b[p];
|
4019
|
+
};
|
4020
|
+
return extendStatics(d, b);
|
4021
|
+
};
|
4022
|
+
function __extends(d, b) {
|
4023
|
+
extendStatics(d, b);
|
4024
|
+
function __() {
|
4025
|
+
this.constructor = d;
|
4026
|
+
}
|
4027
|
+
d.prototype = null === b ? Object.create(b) : (__.prototype = b.prototype, new __());
|
4028
|
+
}
|
4029
|
+
function __values(o) {
|
4030
|
+
var m = "function" == typeof Symbol && o[Symbol.iterator], i = 0;
|
4031
|
+
if (m) return m.call(o);
|
4032
|
+
return {
|
4033
|
+
next: function() {
|
4034
|
+
if (o && i >= o.length) o = void 0;
|
4035
|
+
return {
|
4036
|
+
value: o && o[i++],
|
4037
|
+
done: !o
|
4038
|
+
};
|
4039
|
+
}
|
4040
|
+
};
|
4041
|
+
}
|
4042
|
+
function __read(o, n) {
|
4043
|
+
var m = "function" == typeof Symbol && o[Symbol.iterator];
|
4044
|
+
if (!m) return o;
|
4045
|
+
var i = m.call(o), r, ar = [], e;
|
4046
|
+
try {
|
4047
|
+
while((void 0 === n || n-- > 0) && !(r = i.next()).done)ar.push(r.value);
|
4048
|
+
} catch (error) {
|
4049
|
+
e = {
|
4050
|
+
error: error
|
4051
|
+
};
|
4052
|
+
} finally{
|
4053
|
+
try {
|
4054
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
4055
|
+
} finally{
|
4056
|
+
if (e) throw e.error;
|
4057
|
+
}
|
4058
|
+
}
|
4059
|
+
return ar;
|
4060
|
+
}
|
4061
|
+
function __spread() {
|
4062
|
+
for(var ar = [], i = 0; i < arguments.length; i++)ar = ar.concat(__read(arguments[i]));
|
4063
|
+
return ar;
|
4064
|
+
}
|
4065
|
+
var reconnecting_websocket_mjs_Event = /** @class */ function() {
|
4066
|
+
function Event1(type, target) {
|
4067
|
+
this.target = target;
|
4068
|
+
this.type = type;
|
4069
|
+
}
|
4070
|
+
return Event1;
|
4071
|
+
}();
|
4072
|
+
var reconnecting_websocket_mjs_ErrorEvent = /** @class */ function(_super) {
|
4073
|
+
__extends(ErrorEvent, _super);
|
4074
|
+
function ErrorEvent(error, target) {
|
4075
|
+
var _this = _super.call(this, 'error', target) || this;
|
4076
|
+
_this.message = error.message;
|
4077
|
+
_this.error = error;
|
4078
|
+
return _this;
|
4079
|
+
}
|
4080
|
+
return ErrorEvent;
|
4081
|
+
}(reconnecting_websocket_mjs_Event);
|
4082
|
+
var reconnecting_websocket_mjs_CloseEvent = /** @class */ function(_super) {
|
4083
|
+
__extends(CloseEvent, _super);
|
4084
|
+
function CloseEvent(code, reason, target) {
|
4085
|
+
if (void 0 === code) code = 1000;
|
4086
|
+
if (void 0 === reason) reason = '';
|
4087
|
+
var _this = _super.call(this, 'close', target) || this;
|
4088
|
+
_this.wasClean = true;
|
4089
|
+
_this.code = code;
|
4090
|
+
_this.reason = reason;
|
4091
|
+
return _this;
|
4092
|
+
}
|
4093
|
+
return CloseEvent;
|
4094
|
+
}(reconnecting_websocket_mjs_Event);
|
4095
|
+
/*!
|
4096
|
+
* Reconnecting WebSocket
|
4097
|
+
* by Pedro Ladaria <pedro.ladaria@gmail.com>
|
4098
|
+
* https://github.com/pladaria/reconnecting-websocket
|
4099
|
+
* License MIT
|
4100
|
+
*/ var getGlobalWebSocket = function() {
|
4101
|
+
if ('undefined' != typeof WebSocket) // @ts-ignore
|
4102
|
+
return WebSocket;
|
4103
|
+
};
|
4104
|
+
/**
|
4105
|
+
* Returns true if given argument looks like a WebSocket class
|
4106
|
+
*/ var isWebSocket = function(w) {
|
4107
|
+
return void 0 !== w && !!w && 2 === w.CLOSING;
|
4108
|
+
};
|
4109
|
+
var DEFAULT = {
|
4110
|
+
maxReconnectionDelay: 10000,
|
4111
|
+
minReconnectionDelay: 1000 + 4000 * Math.random(),
|
4112
|
+
minUptime: 5000,
|
4113
|
+
reconnectionDelayGrowFactor: 1.3,
|
4114
|
+
connectionTimeout: 4000,
|
4115
|
+
maxRetries: 1 / 0,
|
4116
|
+
maxEnqueuedMessages: 1 / 0,
|
4117
|
+
startClosed: false,
|
4118
|
+
debug: false
|
4119
|
+
};
|
4120
|
+
var reconnecting_websocket_mjs_ReconnectingWebSocket = /** @class */ function() {
|
4121
|
+
function ReconnectingWebSocket(url, protocols, options) {
|
4122
|
+
var _this = this;
|
4123
|
+
if (void 0 === options) options = {};
|
4124
|
+
this._listeners = {
|
4125
|
+
error: [],
|
4126
|
+
message: [],
|
4127
|
+
open: [],
|
4128
|
+
close: []
|
4129
|
+
};
|
4130
|
+
this._retryCount = -1;
|
4131
|
+
this._shouldReconnect = true;
|
4132
|
+
this._connectLock = false;
|
4133
|
+
this._binaryType = 'blob';
|
4134
|
+
this._closeCalled = false;
|
4135
|
+
this._messageQueue = [];
|
4136
|
+
/**
|
4137
|
+
* An event listener to be called when the WebSocket connection's readyState changes to CLOSED
|
4138
|
+
*/ this.onclose = null;
|
4139
|
+
/**
|
4140
|
+
* An event listener to be called when an error occurs
|
4141
|
+
*/ this.onerror = null;
|
4142
|
+
/**
|
4143
|
+
* An event listener to be called when a message is received from the server
|
4144
|
+
*/ this.onmessage = null;
|
4145
|
+
/**
|
4146
|
+
* An event listener to be called when the WebSocket connection's readyState changes to OPEN;
|
4147
|
+
* this indicates that the connection is ready to send and receive data
|
4148
|
+
*/ this.onopen = null;
|
4149
|
+
this._handleOpen = function(event) {
|
4150
|
+
_this._debug('open event');
|
4151
|
+
var _a = _this._options.minUptime, minUptime = void 0 === _a ? DEFAULT.minUptime : _a;
|
4152
|
+
clearTimeout(_this._connectTimeout);
|
4153
|
+
_this._uptimeTimeout = setTimeout(function() {
|
4154
|
+
return _this._acceptOpen();
|
4155
|
+
}, minUptime);
|
4156
|
+
_this._ws.binaryType = _this._binaryType;
|
4157
|
+
// send enqueued messages (messages sent before websocket open event)
|
4158
|
+
_this._messageQueue.forEach(function(message) {
|
4159
|
+
return _this._ws.send(message);
|
4160
|
+
});
|
4161
|
+
_this._messageQueue = [];
|
4162
|
+
if (_this.onopen) _this.onopen(event);
|
4163
|
+
_this._listeners.open.forEach(function(listener) {
|
4164
|
+
return _this._callEventListener(event, listener);
|
4165
|
+
});
|
4166
|
+
};
|
4167
|
+
this._handleMessage = function(event) {
|
4168
|
+
_this._debug('message event');
|
4169
|
+
if (_this.onmessage) _this.onmessage(event);
|
4170
|
+
_this._listeners.message.forEach(function(listener) {
|
4171
|
+
return _this._callEventListener(event, listener);
|
4172
|
+
});
|
4173
|
+
};
|
4174
|
+
this._handleError = function(event) {
|
4175
|
+
_this._debug('error event', event.message);
|
4176
|
+
_this._disconnect(void 0, 'TIMEOUT' === event.message ? 'timeout' : void 0);
|
4177
|
+
if (_this.onerror) _this.onerror(event);
|
4178
|
+
_this._debug('exec error listeners');
|
4179
|
+
_this._listeners.error.forEach(function(listener) {
|
4180
|
+
return _this._callEventListener(event, listener);
|
4181
|
+
});
|
4182
|
+
_this._connect();
|
4183
|
+
};
|
4184
|
+
this._handleClose = function(event) {
|
4185
|
+
_this._debug('close event');
|
4186
|
+
_this._clearTimeouts();
|
4187
|
+
if (_this._shouldReconnect) _this._connect();
|
4188
|
+
if (_this.onclose) _this.onclose(event);
|
4189
|
+
_this._listeners.close.forEach(function(listener) {
|
4190
|
+
return _this._callEventListener(event, listener);
|
4191
|
+
});
|
4192
|
+
};
|
4193
|
+
this._url = url;
|
4194
|
+
this._protocols = protocols;
|
4195
|
+
this._options = options;
|
4196
|
+
if (this._options.startClosed) this._shouldReconnect = false;
|
4197
|
+
this._connect();
|
4198
|
+
}
|
4199
|
+
Object.defineProperty(ReconnectingWebSocket, "CONNECTING", {
|
4200
|
+
get: function() {
|
4201
|
+
return 0;
|
4202
|
+
},
|
4203
|
+
enumerable: true,
|
4204
|
+
configurable: true
|
4205
|
+
});
|
4206
|
+
Object.defineProperty(ReconnectingWebSocket, "OPEN", {
|
4207
|
+
get: function() {
|
4208
|
+
return 1;
|
4209
|
+
},
|
4210
|
+
enumerable: true,
|
4211
|
+
configurable: true
|
4212
|
+
});
|
4213
|
+
Object.defineProperty(ReconnectingWebSocket, "CLOSING", {
|
4214
|
+
get: function() {
|
4215
|
+
return 2;
|
4216
|
+
},
|
4217
|
+
enumerable: true,
|
4218
|
+
configurable: true
|
4219
|
+
});
|
4220
|
+
Object.defineProperty(ReconnectingWebSocket, "CLOSED", {
|
4221
|
+
get: function() {
|
4222
|
+
return 3;
|
4223
|
+
},
|
4224
|
+
enumerable: true,
|
4225
|
+
configurable: true
|
4226
|
+
});
|
4227
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "CONNECTING", {
|
4228
|
+
get: function() {
|
4229
|
+
return ReconnectingWebSocket.CONNECTING;
|
4230
|
+
},
|
4231
|
+
enumerable: true,
|
4232
|
+
configurable: true
|
4233
|
+
});
|
4234
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "OPEN", {
|
4235
|
+
get: function() {
|
4236
|
+
return ReconnectingWebSocket.OPEN;
|
4237
|
+
},
|
4238
|
+
enumerable: true,
|
4239
|
+
configurable: true
|
4240
|
+
});
|
4241
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "CLOSING", {
|
4242
|
+
get: function() {
|
4243
|
+
return ReconnectingWebSocket.CLOSING;
|
4244
|
+
},
|
4245
|
+
enumerable: true,
|
4246
|
+
configurable: true
|
4247
|
+
});
|
4248
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "CLOSED", {
|
4249
|
+
get: function() {
|
4250
|
+
return ReconnectingWebSocket.CLOSED;
|
4251
|
+
},
|
4252
|
+
enumerable: true,
|
4253
|
+
configurable: true
|
4254
|
+
});
|
4255
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "binaryType", {
|
4256
|
+
get: function() {
|
4257
|
+
return this._ws ? this._ws.binaryType : this._binaryType;
|
4258
|
+
},
|
4259
|
+
set: function(value) {
|
4260
|
+
this._binaryType = value;
|
4261
|
+
if (this._ws) this._ws.binaryType = value;
|
4262
|
+
},
|
4263
|
+
enumerable: true,
|
4264
|
+
configurable: true
|
4265
|
+
});
|
4266
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "retryCount", {
|
4267
|
+
/**
|
4268
|
+
* Returns the number or connection retries
|
4269
|
+
*/ get: function() {
|
4270
|
+
return Math.max(this._retryCount, 0);
|
4271
|
+
},
|
4272
|
+
enumerable: true,
|
4273
|
+
configurable: true
|
4274
|
+
});
|
4275
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "bufferedAmount", {
|
4276
|
+
/**
|
4277
|
+
* The number of bytes of data that have been queued using calls to send() but not yet
|
4278
|
+
* transmitted to the network. This value resets to zero once all queued data has been sent.
|
4279
|
+
* This value does not reset to zero when the connection is closed; if you keep calling send(),
|
4280
|
+
* this will continue to climb. Read only
|
4281
|
+
*/ get: function() {
|
4282
|
+
var bytes = this._messageQueue.reduce(function(acc, message) {
|
4283
|
+
if ('string' == typeof message) acc += message.length; // not byte size
|
4284
|
+
else if (message instanceof Blob) acc += message.size;
|
4285
|
+
else acc += message.byteLength;
|
4286
|
+
return acc;
|
4287
|
+
}, 0);
|
4288
|
+
return bytes + (this._ws ? this._ws.bufferedAmount : 0);
|
4289
|
+
},
|
4290
|
+
enumerable: true,
|
4291
|
+
configurable: true
|
4292
|
+
});
|
4293
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "extensions", {
|
4294
|
+
/**
|
4295
|
+
* The extensions selected by the server. This is currently only the empty string or a list of
|
4296
|
+
* extensions as negotiated by the connection
|
4297
|
+
*/ get: function() {
|
4298
|
+
return this._ws ? this._ws.extensions : '';
|
4299
|
+
},
|
4300
|
+
enumerable: true,
|
4301
|
+
configurable: true
|
4302
|
+
});
|
4303
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "protocol", {
|
4304
|
+
/**
|
4305
|
+
* A string indicating the name of the sub-protocol the server selected;
|
4306
|
+
* this will be one of the strings specified in the protocols parameter when creating the
|
4307
|
+
* WebSocket object
|
4308
|
+
*/ get: function() {
|
4309
|
+
return this._ws ? this._ws.protocol : '';
|
4310
|
+
},
|
4311
|
+
enumerable: true,
|
4312
|
+
configurable: true
|
4313
|
+
});
|
4314
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "readyState", {
|
4315
|
+
/**
|
4316
|
+
* The current state of the connection; this is one of the Ready state constants
|
4317
|
+
*/ get: function() {
|
4318
|
+
if (this._ws) return this._ws.readyState;
|
4319
|
+
return this._options.startClosed ? ReconnectingWebSocket.CLOSED : ReconnectingWebSocket.CONNECTING;
|
4320
|
+
},
|
4321
|
+
enumerable: true,
|
4322
|
+
configurable: true
|
4323
|
+
});
|
4324
|
+
Object.defineProperty(ReconnectingWebSocket.prototype, "url", {
|
4325
|
+
/**
|
4326
|
+
* The URL as resolved by the constructor
|
4327
|
+
*/ get: function() {
|
4328
|
+
return this._ws ? this._ws.url : '';
|
4329
|
+
},
|
4330
|
+
enumerable: true,
|
4331
|
+
configurable: true
|
4332
|
+
});
|
4333
|
+
/**
|
4334
|
+
* Closes the WebSocket connection or connection attempt, if any. If the connection is already
|
4335
|
+
* CLOSED, this method does nothing
|
4336
|
+
*/ ReconnectingWebSocket.prototype.close = function(code, reason) {
|
4337
|
+
if (void 0 === code) code = 1000;
|
4338
|
+
this._closeCalled = true;
|
4339
|
+
this._shouldReconnect = false;
|
4340
|
+
this._clearTimeouts();
|
4341
|
+
if (!this._ws) {
|
4342
|
+
this._debug('close enqueued: no ws instance');
|
4343
|
+
return;
|
4344
|
+
}
|
4345
|
+
if (this._ws.readyState === this.CLOSED) {
|
4346
|
+
this._debug('close: already closed');
|
4347
|
+
return;
|
4348
|
+
}
|
4349
|
+
this._ws.close(code, reason);
|
4350
|
+
};
|
4351
|
+
/**
|
4352
|
+
* Closes the WebSocket connection or connection attempt and connects again.
|
4353
|
+
* Resets retry counter;
|
4354
|
+
*/ ReconnectingWebSocket.prototype.reconnect = function(code, reason) {
|
4355
|
+
this._shouldReconnect = true;
|
4356
|
+
this._closeCalled = false;
|
4357
|
+
this._retryCount = -1;
|
4358
|
+
if (this._ws && this._ws.readyState !== this.CLOSED) {
|
4359
|
+
this._disconnect(code, reason);
|
4360
|
+
this._connect();
|
4361
|
+
} else this._connect();
|
4362
|
+
};
|
4363
|
+
/**
|
4364
|
+
* Enqueue specified data to be transmitted to the server over the WebSocket connection
|
4365
|
+
*/ ReconnectingWebSocket.prototype.send = function(data) {
|
4366
|
+
if (this._ws && this._ws.readyState === this.OPEN) {
|
4367
|
+
this._debug('send', data);
|
4368
|
+
this._ws.send(data);
|
4369
|
+
} else {
|
4370
|
+
var _a = this._options.maxEnqueuedMessages, maxEnqueuedMessages = void 0 === _a ? DEFAULT.maxEnqueuedMessages : _a;
|
4371
|
+
if (this._messageQueue.length < maxEnqueuedMessages) {
|
4372
|
+
this._debug('enqueue', data);
|
4373
|
+
this._messageQueue.push(data);
|
4374
|
+
}
|
4375
|
+
}
|
4376
|
+
};
|
4377
|
+
/**
|
4378
|
+
* Register an event handler of a specific event type
|
4379
|
+
*/ ReconnectingWebSocket.prototype.addEventListener = function(type, listener) {
|
4380
|
+
if (this._listeners[type]) // @ts-ignore
|
4381
|
+
this._listeners[type].push(listener);
|
4382
|
+
};
|
4383
|
+
ReconnectingWebSocket.prototype.dispatchEvent = function(event) {
|
4384
|
+
var e_1, _a;
|
4385
|
+
var listeners = this._listeners[event.type];
|
4386
|
+
if (listeners) try {
|
4387
|
+
for(var listeners_1 = __values(listeners), listeners_1_1 = listeners_1.next(); !listeners_1_1.done; listeners_1_1 = listeners_1.next()){
|
4388
|
+
var listener = listeners_1_1.value;
|
4389
|
+
this._callEventListener(event, listener);
|
4390
|
+
}
|
4391
|
+
} catch (e_1_1) {
|
4392
|
+
e_1 = {
|
4393
|
+
error: e_1_1
|
4394
|
+
};
|
4395
|
+
} finally{
|
4396
|
+
try {
|
4397
|
+
if (listeners_1_1 && !listeners_1_1.done && (_a = listeners_1.return)) _a.call(listeners_1);
|
4398
|
+
} finally{
|
4399
|
+
if (e_1) throw e_1.error;
|
4400
|
+
}
|
4401
|
+
}
|
4402
|
+
return true;
|
4403
|
+
};
|
4404
|
+
/**
|
4405
|
+
* Removes an event listener
|
4406
|
+
*/ ReconnectingWebSocket.prototype.removeEventListener = function(type, listener) {
|
4407
|
+
if (this._listeners[type]) // @ts-ignore
|
4408
|
+
this._listeners[type] = this._listeners[type].filter(function(l) {
|
4409
|
+
return l !== listener;
|
4410
|
+
});
|
4411
|
+
};
|
4412
|
+
ReconnectingWebSocket.prototype._debug = function() {
|
4413
|
+
var args = [];
|
4414
|
+
for(var _i = 0; _i < arguments.length; _i++)args[_i] = arguments[_i];
|
4415
|
+
if (this._options.debug) // not using spread because compiled version uses Symbols
|
4416
|
+
// tslint:disable-next-line
|
4417
|
+
console.log.apply(console, __spread([
|
4418
|
+
'RWS>'
|
4419
|
+
], args));
|
4420
|
+
};
|
4421
|
+
ReconnectingWebSocket.prototype._getNextDelay = function() {
|
4422
|
+
var _a = this._options, _b = _a.reconnectionDelayGrowFactor, reconnectionDelayGrowFactor = void 0 === _b ? DEFAULT.reconnectionDelayGrowFactor : _b, _c = _a.minReconnectionDelay, minReconnectionDelay = void 0 === _c ? DEFAULT.minReconnectionDelay : _c, _d = _a.maxReconnectionDelay, maxReconnectionDelay = void 0 === _d ? DEFAULT.maxReconnectionDelay : _d;
|
4423
|
+
var delay = 0;
|
4424
|
+
if (this._retryCount > 0) {
|
4425
|
+
delay = minReconnectionDelay * Math.pow(reconnectionDelayGrowFactor, this._retryCount - 1);
|
4426
|
+
if (delay > maxReconnectionDelay) delay = maxReconnectionDelay;
|
4427
|
+
}
|
4428
|
+
this._debug('next delay', delay);
|
4429
|
+
return delay;
|
4430
|
+
};
|
4431
|
+
ReconnectingWebSocket.prototype._wait = function() {
|
4432
|
+
var _this = this;
|
4433
|
+
return new Promise(function(resolve) {
|
4434
|
+
setTimeout(resolve, _this._getNextDelay());
|
4435
|
+
});
|
4436
|
+
};
|
4437
|
+
ReconnectingWebSocket.prototype._getNextUrl = function(urlProvider) {
|
4438
|
+
if ('string' == typeof urlProvider) return Promise.resolve(urlProvider);
|
4439
|
+
if ('function' == typeof urlProvider) {
|
4440
|
+
var url = urlProvider();
|
4441
|
+
if ('string' == typeof url) return Promise.resolve(url);
|
4442
|
+
if (!!url.then) return url;
|
4443
|
+
}
|
4444
|
+
throw Error('Invalid URL');
|
4445
|
+
};
|
4446
|
+
ReconnectingWebSocket.prototype._connect = function() {
|
4447
|
+
var _this = this;
|
4448
|
+
if (this._connectLock || !this._shouldReconnect) return;
|
4449
|
+
this._connectLock = true;
|
4450
|
+
var _a = this._options, _b = _a.maxRetries, maxRetries = void 0 === _b ? DEFAULT.maxRetries : _b, _c = _a.connectionTimeout, connectionTimeout = void 0 === _c ? DEFAULT.connectionTimeout : _c, _d = _a.WebSocket, WebSocket1 = void 0 === _d ? getGlobalWebSocket() : _d;
|
4451
|
+
if (this._retryCount >= maxRetries) {
|
4452
|
+
this._debug('max retries reached', this._retryCount, '>=', maxRetries);
|
4453
|
+
return;
|
4454
|
+
}
|
4455
|
+
this._retryCount++;
|
4456
|
+
this._debug('connect', this._retryCount);
|
4457
|
+
this._removeListeners();
|
4458
|
+
if (!isWebSocket(WebSocket1)) throw Error('No valid WebSocket class provided');
|
4459
|
+
this._wait().then(function() {
|
4460
|
+
return _this._getNextUrl(_this._url);
|
4461
|
+
}).then(function(url) {
|
4462
|
+
// close could be called before creating the ws
|
4463
|
+
if (_this._closeCalled) return;
|
4464
|
+
_this._debug('connect', {
|
4465
|
+
url: url,
|
4466
|
+
protocols: _this._protocols
|
4467
|
+
});
|
4468
|
+
_this._ws = _this._protocols ? new WebSocket1(url, _this._protocols) : new WebSocket1(url);
|
4469
|
+
_this._ws.binaryType = _this._binaryType;
|
4470
|
+
_this._connectLock = false;
|
4471
|
+
_this._addListeners();
|
4472
|
+
_this._connectTimeout = setTimeout(function() {
|
4473
|
+
return _this._handleTimeout();
|
4474
|
+
}, connectionTimeout);
|
4475
|
+
});
|
4476
|
+
};
|
4477
|
+
ReconnectingWebSocket.prototype._handleTimeout = function() {
|
4478
|
+
this._debug('timeout event');
|
4479
|
+
this._handleError(new reconnecting_websocket_mjs_ErrorEvent(Error('TIMEOUT'), this));
|
4480
|
+
};
|
4481
|
+
ReconnectingWebSocket.prototype._disconnect = function(code, reason) {
|
4482
|
+
if (void 0 === code) code = 1000;
|
4483
|
+
this._clearTimeouts();
|
4484
|
+
if (!this._ws) return;
|
4485
|
+
this._removeListeners();
|
4486
|
+
try {
|
4487
|
+
this._ws.close(code, reason);
|
4488
|
+
this._handleClose(new reconnecting_websocket_mjs_CloseEvent(code, reason, this));
|
4489
|
+
} catch (error) {
|
4490
|
+
// ignore
|
4491
|
+
}
|
4492
|
+
};
|
4493
|
+
ReconnectingWebSocket.prototype._acceptOpen = function() {
|
4494
|
+
this._debug('accept open');
|
4495
|
+
this._retryCount = 0;
|
4496
|
+
};
|
4497
|
+
ReconnectingWebSocket.prototype._callEventListener = function(event, listener) {
|
4498
|
+
if ('handleEvent' in listener) // @ts-ignore
|
4499
|
+
listener.handleEvent(event);
|
4500
|
+
else // @ts-ignore
|
4501
|
+
listener(event);
|
4502
|
+
};
|
4503
|
+
ReconnectingWebSocket.prototype._removeListeners = function() {
|
4504
|
+
if (!this._ws) return;
|
4505
|
+
this._debug('removeListeners');
|
4506
|
+
this._ws.removeEventListener('open', this._handleOpen);
|
4507
|
+
this._ws.removeEventListener('close', this._handleClose);
|
4508
|
+
this._ws.removeEventListener('message', this._handleMessage);
|
4509
|
+
// @ts-ignore
|
4510
|
+
this._ws.removeEventListener('error', this._handleError);
|
4511
|
+
};
|
4512
|
+
ReconnectingWebSocket.prototype._addListeners = function() {
|
4513
|
+
if (!this._ws) return;
|
4514
|
+
this._debug('addListeners');
|
4515
|
+
this._ws.addEventListener('open', this._handleOpen);
|
4516
|
+
this._ws.addEventListener('close', this._handleClose);
|
4517
|
+
this._ws.addEventListener('message', this._handleMessage);
|
4518
|
+
// @ts-ignore
|
4519
|
+
this._ws.addEventListener('error', this._handleError);
|
4520
|
+
};
|
4521
|
+
ReconnectingWebSocket.prototype._clearTimeouts = function() {
|
4522
|
+
clearTimeout(this._connectTimeout);
|
4523
|
+
clearTimeout(this._uptimeTimeout);
|
4524
|
+
};
|
4525
|
+
return ReconnectingWebSocket;
|
4526
|
+
}();
|
4527
|
+
/* ESM default export */ const reconnecting_websocket_mjs = reconnecting_websocket_mjs_ReconnectingWebSocket;
|
4528
|
+
class WebSocketAPI {
|
4529
|
+
// Standard WebSocket properties
|
4530
|
+
get readyState() {
|
4531
|
+
return this.rws.readyState;
|
4532
|
+
}
|
4533
|
+
// Standard WebSocket methods
|
4534
|
+
send(data) {
|
4535
|
+
return this.rws.send(JSON.stringify(data));
|
4536
|
+
}
|
4537
|
+
close(code, reason) {
|
4538
|
+
return this.rws.close(code, reason);
|
4539
|
+
}
|
4540
|
+
reconnect(code, reason) {
|
4541
|
+
return this.rws.reconnect(code, reason);
|
4542
|
+
}
|
4543
|
+
// Event listener methods
|
4544
|
+
addEventListener(type, listener) {
|
4545
|
+
this.rws.addEventListener(type, listener);
|
4546
|
+
}
|
4547
|
+
removeEventListener(type, listener) {
|
4548
|
+
this.rws.removeEventListener(type, listener);
|
4549
|
+
}
|
4550
|
+
constructor(url, options = {}){
|
4551
|
+
// Event handler methods
|
4552
|
+
this.onmessage = null;
|
4553
|
+
this.onopen = null;
|
4554
|
+
this.onclose = null;
|
4555
|
+
this.onerror = null;
|
4556
|
+
const separator = url.includes('?') ? '&' : '?';
|
4557
|
+
const { authorization } = options.headers || {};
|
4558
|
+
this.rws = new reconnecting_websocket_mjs(`${url}${separator}authorization=${authorization}`, [], {
|
4559
|
+
WebSocket: utils_isBrowser() ? window.WebSocket : class extends browser_default() {
|
4560
|
+
constructor(url2, protocols){
|
4561
|
+
super(url2, protocols, {
|
4562
|
+
headers: options.headers
|
4563
|
+
});
|
4564
|
+
}
|
4565
|
+
},
|
4566
|
+
...options
|
4567
|
+
});
|
4568
|
+
this.rws.addEventListener('message', (event)=>{
|
4569
|
+
try {
|
4570
|
+
var _this_onmessage, _this;
|
4571
|
+
const data = JSON.parse(event.data);
|
4572
|
+
null === (_this_onmessage = (_this = this).onmessage) || void 0 === _this_onmessage || _this_onmessage.call(_this, data, event);
|
4573
|
+
} catch (error) {
|
4574
|
+
console.error('WebSocketAPI onmessage error', error);
|
4575
|
+
}
|
4576
|
+
});
|
4577
|
+
this.rws.addEventListener('open', (event)=>{
|
4578
|
+
var _this_onopen, _this;
|
4579
|
+
null === (_this_onopen = (_this = this).onopen) || void 0 === _this_onopen || _this_onopen.call(_this, event);
|
4580
|
+
});
|
4581
|
+
this.rws.addEventListener('close', (event)=>{
|
4582
|
+
var _this_onclose, _this;
|
4583
|
+
null === (_this_onclose = (_this = this).onclose) || void 0 === _this_onclose || _this_onclose.call(_this, event);
|
4584
|
+
});
|
4585
|
+
this.rws.addEventListener('error', (event)=>{
|
4586
|
+
var _event_target__req_res, _event_target__req, _event_target, _event_target__req_res1, _event_target__req1, _event_target1, _this_onerror, _this;
|
4587
|
+
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;
|
4588
|
+
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) || [];
|
4589
|
+
const logidIndex = rawHeaders.findIndex((header)=>'X-Tt-Logid' === header);
|
4590
|
+
const logid = -1 !== logidIndex ? rawHeaders[logidIndex + 1] : void 0;
|
4591
|
+
const error = {
|
4592
|
+
id: '0',
|
4593
|
+
event_type: types_WebsocketsEventType.ERROR,
|
4594
|
+
data: {
|
4595
|
+
code: -1,
|
4596
|
+
msg: 'WebSocket error'
|
4597
|
+
},
|
4598
|
+
detail: {
|
4599
|
+
logid
|
4600
|
+
}
|
4601
|
+
};
|
4602
|
+
if (401 === statusCode) {
|
4603
|
+
error.data.code = 401;
|
4604
|
+
error.data.msg = 'Unauthorized';
|
4605
|
+
} else if (403 === statusCode) {
|
4606
|
+
error.data.code = 403;
|
4607
|
+
error.data.msg = 'Forbidden';
|
4608
|
+
} else {
|
4609
|
+
error.data.code = 500;
|
4610
|
+
error.data.msg = String(null == event ? void 0 : event.error) || 'WebSocket error';
|
4611
|
+
}
|
4612
|
+
null === (_this_onerror = (_this = this).onerror) || void 0 === _this_onerror || _this_onerror.call(_this, error, event);
|
4613
|
+
});
|
4614
|
+
}
|
4615
|
+
}
|
3917
4616
|
// EXTERNAL MODULE: os (ignored)
|
3918
4617
|
var os_ignored_ = __webpack_require__("?9050");
|
3919
4618
|
var os_ignored_default = /*#__PURE__*/ __webpack_require__.n(os_ignored_);
|
3920
|
-
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.0.
|
4619
|
+
var package_namespaceObject = JSON.parse('{"name":"@coze/api","version":"1.0.20","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":{".":"./src/index.ts"},"main":"src/index.ts","module":"src/index.ts","browser":{"crypto":false,"os":false,"jsonwebtoken":false,"node-fetch":false},"types":"src/index.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","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.cjs","import":"./dist/esm/index.js","types":"./dist/types/index.d.ts"}},"main":"dist/cjs/index.cjs","module":"dist/esm/index.js","types":"dist/types/index.d.ts"}}'); // CONCATENATED MODULE: ../coze-js/src/version.ts
|
3921
4620
|
const { version: version_version } = package_namespaceObject;
|
3922
4621
|
const getEnv = ()=>{
|
3923
4622
|
const nodeVersion = process.version.slice(1); // Remove 'v' prefix
|
@@ -4016,6 +4715,9 @@ const getBrowserClientUserAgent = ()=>{
|
|
4016
4715
|
};
|
4017
4716
|
return JSON.stringify(ua);
|
4018
4717
|
};
|
4718
|
+
// EXTERNAL MODULE: node-fetch (ignored)
|
4719
|
+
var node_fetch_ignored_ = __webpack_require__("?8dee");
|
4720
|
+
var node_fetch_ignored_default = /*#__PURE__*/ __webpack_require__.n(node_fetch_ignored_);
|
4019
4721
|
/* eslint-disable @typescript-eslint/no-explicit-any */ const fetcher_handleError = (error)=>{
|
4020
4722
|
if (!error.isAxiosError && (!error.code || !error.message)) return new CozeError(`Unexpected error: ${error.message}`);
|
4021
4723
|
if ('ECONNABORTED' === error.code && error.message.includes('timeout') || 'ETIMEDOUT' === error.code) {
|
@@ -4028,6 +4730,23 @@ const getBrowserClientUserAgent = ()=>{
|
|
4028
4730
|
return error_APIError.generate((null === (_error_response1 = error.response) || void 0 === _error_response1 ? void 0 : _error_response1.status) || 500, null === (_error_response2 = error.response) || void 0 === _error_response2 ? void 0 : _error_response2.data, error.message, null === (_error_response3 = error.response) || void 0 === _error_response3 ? void 0 : _error_response3.headers);
|
4029
4731
|
}
|
4030
4732
|
};
|
4733
|
+
// node-fetch is used for streaming requests
|
4734
|
+
const adapterFetch = async (options)=>{
|
4735
|
+
const response = await node_fetch_ignored_default()(options.url, {
|
4736
|
+
body: options.data,
|
4737
|
+
...options
|
4738
|
+
});
|
4739
|
+
return {
|
4740
|
+
data: response.body,
|
4741
|
+
...response
|
4742
|
+
};
|
4743
|
+
};
|
4744
|
+
const isSupportNativeFetch = ()=>{
|
4745
|
+
if (utils_isBrowser()) return true;
|
4746
|
+
// native fetch is supported in node 18.0.0 or higher
|
4747
|
+
const version = process.version.slice(1);
|
4748
|
+
return compareVersions(version, '18.0.0') >= 0;
|
4749
|
+
};
|
4031
4750
|
async function fetchAPI(url) {
|
4032
4751
|
let options = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
|
4033
4752
|
const axiosInstance = options.axiosInstance || lib_axios;
|
@@ -4039,7 +4758,7 @@ async function fetchAPI(url) {
|
|
4039
4758
|
const response = await axiosInstance({
|
4040
4759
|
url,
|
4041
4760
|
responseType: options.isStreaming ? 'stream' : 'json',
|
4042
|
-
adapter: options.isStreaming ? 'fetch' : void 0,
|
4761
|
+
adapter: options.isStreaming ? isSupportNativeFetch() ? 'fetch' : adapterFetch : void 0,
|
4043
4762
|
...options
|
4044
4763
|
}).catch((error)=>{
|
4045
4764
|
throw fetcher_handleError(error);
|
@@ -4105,6 +4824,9 @@ function isAxiosStatic(instance) {
|
|
4105
4824
|
/**
|
4106
4825
|
* default coze base URL is api.coze.com
|
4107
4826
|
*/ const constant_COZE_COM_BASE_URL = 'https://api.coze.com';
|
4827
|
+
/**
|
4828
|
+
* default base websocket URL is wss://ws.coze.com
|
4829
|
+
*/ const COZE_COM_BASE_WS_URL = 'wss://ws.coze.com';
|
4108
4830
|
/* eslint-disable max-params */ class core_APIClient {
|
4109
4831
|
async getToken() {
|
4110
4832
|
if ('function' == typeof this.token) return await this.token();
|
@@ -4129,6 +4851,26 @@ function isAxiosStatic(instance) {
|
|
4129
4851
|
config.data = body;
|
4130
4852
|
return config;
|
4131
4853
|
}
|
4854
|
+
async buildWebsocketOptions(options) {
|
4855
|
+
const token = await this.getToken();
|
4856
|
+
const headers = {
|
4857
|
+
authorization: `Bearer ${token}`
|
4858
|
+
};
|
4859
|
+
if (utils_isBrowser()) headers['X-Coze-Client-User-Agent'] = getBrowserClientUserAgent();
|
4860
|
+
else {
|
4861
|
+
headers['User-Agent'] = getUserAgent();
|
4862
|
+
headers['X-Coze-Client-User-Agent'] = getNodeClientUserAgent();
|
4863
|
+
}
|
4864
|
+
var _this__config_debug;
|
4865
|
+
const config = mergeConfig({
|
4866
|
+
debug: null !== (_this__config_debug = this._config.debug) && void 0 !== _this__config_debug && _this__config_debug
|
4867
|
+
}, this._config.websocketOptions, options, {
|
4868
|
+
headers
|
4869
|
+
}, {
|
4870
|
+
headers: this.headers || {}
|
4871
|
+
});
|
4872
|
+
return config;
|
4873
|
+
}
|
4132
4874
|
async makeRequest(apiUrl, method, body, isStream, options) {
|
4133
4875
|
const fullUrl = `${this.baseURL}${apiUrl}`;
|
4134
4876
|
const fetchOptions = await this.buildOptions(method, body, options);
|
@@ -4176,6 +4918,14 @@ function isAxiosStatic(instance) {
|
|
4176
4918
|
async delete(apiUrl, isStream, options) {
|
4177
4919
|
return this.makeRequest(apiUrl, 'DELETE', void 0, isStream, options);
|
4178
4920
|
}
|
4921
|
+
async makeWebsocket(apiUrl, options) {
|
4922
|
+
const fullUrl = `${this.baseWsURL}${apiUrl}`;
|
4923
|
+
const websocketOptions = await this.buildWebsocketOptions(options);
|
4924
|
+
this.debugLog(null == options ? void 0 : options.debug, `--- websocket url: ${fullUrl}`);
|
4925
|
+
this.debugLog(null == options ? void 0 : options.debug, '--- websocket options:', websocketOptions);
|
4926
|
+
const ws = new WebSocketAPI(fullUrl, websocketOptions);
|
4927
|
+
return ws;
|
4928
|
+
}
|
4179
4929
|
getConfig() {
|
4180
4930
|
return this._config;
|
4181
4931
|
}
|
@@ -4188,6 +4938,7 @@ function isAxiosStatic(instance) {
|
|
4188
4938
|
constructor(config){
|
4189
4939
|
this._config = config;
|
4190
4940
|
this.baseURL = config.baseURL || constant_COZE_COM_BASE_URL;
|
4941
|
+
this.baseWsURL = config.baseWsURL || COZE_COM_BASE_WS_URL;
|
4191
4942
|
this.token = config.token;
|
4192
4943
|
this.axiosOptions = config.axiosOptions || {};
|
4193
4944
|
this.axiosInstance = config.axiosInstance;
|
@@ -4215,7 +4966,7 @@ class CozeAPI extends core_APIClient {
|
|
4215
4966
|
constructor(...args){
|
4216
4967
|
super(...args), this.bots = new Bots(this), this.chat = new Chat(this), this.conversations = new Conversations(this), this.files = new Files(this), /**
|
4217
4968
|
* @deprecated
|
4218
|
-
*/ this.knowledge = new Knowledge(this), this.datasets = new Datasets(this), this.workflows = new Workflows(this), this.workspaces = new WorkSpaces(this), this.audio = new audio_Audio(this), this.templates = new Templates(this);
|
4969
|
+
*/ this.knowledge = new Knowledge(this), this.datasets = new Datasets(this), this.workflows = new Workflows(this), this.workspaces = new WorkSpaces(this), this.audio = new audio_Audio(this), this.templates = new Templates(this), this.websockets = new Websockets(this);
|
4219
4970
|
}
|
4220
4971
|
}
|
4221
4972
|
/**
|
@@ -42370,7 +43121,8 @@ class RealtimeClient extends RealtimeEventHandler {
|
|
42370
43121
|
conversation_id: conversationId || void 0,
|
42371
43122
|
voice_id: voiceId && voiceId.length > 0 ? voiceId : void 0,
|
42372
43123
|
connector_id: this._config.connectorId,
|
42373
|
-
uid: this._config.userId || void 0
|
43124
|
+
uid: this._config.userId || void 0,
|
43125
|
+
workflow_id: this._config.workflowId || void 0
|
42374
43126
|
});
|
42375
43127
|
} catch (error) {
|
42376
43128
|
this.dispatch(event_handler_EventNames.ERROR, error);
|