@larksuiteoapi/node-sdk 1.30.0 → 1.31.0
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 +5 -0
- package/README.zh.md +5 -0
- package/es/index.js +414 -1
- package/lib/index.js +414 -0
- package/package.json +2 -2
- package/types/index.d.ts +126 -1
package/README.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Feishu open interface SDK
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
|
|
3
8
|
[中文](https://github.com/larksuite/node-sdk/blob/main/README.zh.md)
|
|
4
9
|
## Overview
|
|
5
10
|
[Feishu Open Platform](https://open.feishu.cn/document/ukTMukTMukTM/uITNz4iM1MjLyUzM) provides a series of atomic APIs on the server side to realize diversified functions, but the actual coding process is not very smooth: when using these APIs to complete operation, a lot of extra work needs to be considered, such as token acquisition and maintenance, data encryption and decryption, request signature verification, etc.; in the actual coding process, the semantics of function calls are missing, cause mental burden.
|
package/README.zh.md
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
# 飞书开放接口SDK
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
2
7
|
[English](https://github.com/larksuite/node-sdk/blob/main/README.md)
|
|
3
8
|
## 概述
|
|
4
9
|
[飞书开放平台](https://open.feishu.cn/document/ukTMukTMukTM/uITNz4iM1MjLyUzM)提供了一系列服务端的原子api来实现多元化的功能,但在实际编码过程中感受不是很顺畅,原因在于使用这些api完成功能时,需要考虑很多额外的工作,如token的获取及其维护、数据加解密、请求的验签等等;再者,在实际编码过程中,少了函数调用的语义化描述,类型系统的支持,使得心智负担过重。
|
package/es/index.js
CHANGED
|
@@ -58,6 +58,18 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
function __values(o) {
|
|
62
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
63
|
+
if (m) return m.call(o);
|
|
64
|
+
if (o && typeof o.length === "number") return {
|
|
65
|
+
next: function () {
|
|
66
|
+
if (o && i >= o.length) o = void 0;
|
|
67
|
+
return { value: o && o[i++], done: !o };
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
71
|
+
}
|
|
72
|
+
|
|
61
73
|
function __await(v) {
|
|
62
74
|
return this instanceof __await ? (this.v = v, this) : new __await(v);
|
|
63
75
|
}
|
|
@@ -74,6 +86,14 @@ function __asyncGenerator(thisArg, _arguments, generator) {
|
|
|
74
86
|
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
75
87
|
}
|
|
76
88
|
|
|
89
|
+
function __asyncValues(o) {
|
|
90
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
91
|
+
var m = o[Symbol.asyncIterator], i;
|
|
92
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
93
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
94
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
95
|
+
}
|
|
96
|
+
|
|
77
97
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
78
98
|
var e = new Error(message);
|
|
79
99
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
@@ -114,6 +134,8 @@ const CAppTicket = Symbol('app-ticket');
|
|
|
114
134
|
const CTenantAccessToken = Symbol('tenant-access-token');
|
|
115
135
|
const CWithHelpdeskAuthorization = Symbol('with-helpdesk-authorization');
|
|
116
136
|
const CWithUserAccessToken = Symbol('with-user-access-token');
|
|
137
|
+
const CUserAccessToken = Symbol('user-access-token');
|
|
138
|
+
const CAilySessionRecord = Symbol('aily-session-record');
|
|
117
139
|
|
|
118
140
|
const string2Base64 = (content) => Buffer.from(content).toString('base64');
|
|
119
141
|
|
|
@@ -61111,6 +61133,135 @@ class TokenManager {
|
|
|
61111
61133
|
}
|
|
61112
61134
|
}
|
|
61113
61135
|
|
|
61136
|
+
const mergeObject = (obj1, obj2) => {
|
|
61137
|
+
const mergedObject = Object.assign({}, obj1);
|
|
61138
|
+
for (let [key, value] of Object.entries(obj2)) {
|
|
61139
|
+
if (value !== undefined) {
|
|
61140
|
+
mergedObject[key] = value;
|
|
61141
|
+
}
|
|
61142
|
+
}
|
|
61143
|
+
return mergedObject;
|
|
61144
|
+
};
|
|
61145
|
+
|
|
61146
|
+
class UserAccessToken {
|
|
61147
|
+
constructor(params) {
|
|
61148
|
+
this.client = params.client;
|
|
61149
|
+
}
|
|
61150
|
+
getCacheKey(key, options) {
|
|
61151
|
+
const namespace = get(options, 'namespace', this.client.appId);
|
|
61152
|
+
return `${namespace}/${CUserAccessToken.toString()}/${key}`;
|
|
61153
|
+
}
|
|
61154
|
+
// the unit of time is seconds
|
|
61155
|
+
calibrateTime(time) {
|
|
61156
|
+
// Due to the time-consuming network, the time needs to be 3 minutes earlier
|
|
61157
|
+
return new Date().getTime() + (time || 0) * 1000 - 3 * 60 * 1000;
|
|
61158
|
+
}
|
|
61159
|
+
initWithCode(key2Code, options) {
|
|
61160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61161
|
+
const key2Info = {};
|
|
61162
|
+
for (const [key, code] of Object.entries(key2Code)) {
|
|
61163
|
+
const oidcAccessInfo = yield this.client.authen.oidcAccessToken.create({
|
|
61164
|
+
data: {
|
|
61165
|
+
grant_type: 'authorization_code',
|
|
61166
|
+
code
|
|
61167
|
+
}
|
|
61168
|
+
});
|
|
61169
|
+
if (oidcAccessInfo.code !== 0) {
|
|
61170
|
+
// @ts-ignore
|
|
61171
|
+
this.client.logger.error('init user access token error', key, oidcAccessInfo.msg || oidcAccessInfo.message);
|
|
61172
|
+
continue;
|
|
61173
|
+
}
|
|
61174
|
+
// code expired
|
|
61175
|
+
if (!oidcAccessInfo.data) {
|
|
61176
|
+
this.client.logger.error('user access code expired', key, code);
|
|
61177
|
+
continue;
|
|
61178
|
+
}
|
|
61179
|
+
key2Info[key] = {
|
|
61180
|
+
code,
|
|
61181
|
+
token: oidcAccessInfo.data.access_token,
|
|
61182
|
+
refreshToken: oidcAccessInfo.data.refresh_token,
|
|
61183
|
+
expiredTime: this.calibrateTime(oidcAccessInfo.data.expires_in)
|
|
61184
|
+
};
|
|
61185
|
+
}
|
|
61186
|
+
yield this.update(key2Info, { namespace: get(options, 'namespace') });
|
|
61187
|
+
return key2Info;
|
|
61188
|
+
});
|
|
61189
|
+
}
|
|
61190
|
+
update(key2Info, options) {
|
|
61191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61192
|
+
for (const [key, info] of Object.entries(key2Info)) {
|
|
61193
|
+
const cacheKey = this.getCacheKey(key, { namespace: get(options, 'namespace') });
|
|
61194
|
+
const cacheValue = (yield this.client.cache.get(cacheKey)) || {};
|
|
61195
|
+
const { code, token, refreshToken, expiredTime } = info;
|
|
61196
|
+
const targetValue = mergeObject(cacheValue, { code, token, refreshToken, expiredTime });
|
|
61197
|
+
yield this.client.cache.set(cacheKey, targetValue, Infinity);
|
|
61198
|
+
}
|
|
61199
|
+
});
|
|
61200
|
+
}
|
|
61201
|
+
get(key, options) {
|
|
61202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61203
|
+
const cacheKey = this.getCacheKey(key, { namespace: get(options, 'namespace') });
|
|
61204
|
+
const cacheInfo = yield this.client.cache.get(cacheKey);
|
|
61205
|
+
// cacheInfo是否存在
|
|
61206
|
+
if (!cacheInfo) {
|
|
61207
|
+
this.client.logger.error('user access token needs to be initialized or updated first');
|
|
61208
|
+
return;
|
|
61209
|
+
}
|
|
61210
|
+
const { token, code, refreshToken, expiredTime } = cacheInfo;
|
|
61211
|
+
// step1 token存在且未过期
|
|
61212
|
+
if (token && expiredTime && expiredTime - new Date().getTime() > 0) {
|
|
61213
|
+
return token;
|
|
61214
|
+
}
|
|
61215
|
+
// step2 refresh token存在,刷新token
|
|
61216
|
+
if (refreshToken) {
|
|
61217
|
+
const refreshAccessInfo = yield this.client.authen.oidcRefreshAccessToken.create({
|
|
61218
|
+
data: {
|
|
61219
|
+
grant_type: 'refresh_token',
|
|
61220
|
+
refresh_token: refreshToken
|
|
61221
|
+
}
|
|
61222
|
+
});
|
|
61223
|
+
if (refreshAccessInfo.code === 0 && refreshAccessInfo.data) {
|
|
61224
|
+
yield this.update({
|
|
61225
|
+
key: {
|
|
61226
|
+
token: refreshAccessInfo.data.access_token,
|
|
61227
|
+
refreshToken: refreshAccessInfo.data.refresh_token,
|
|
61228
|
+
expiredTime: this.calibrateTime(refreshAccessInfo.data.expires_in)
|
|
61229
|
+
}
|
|
61230
|
+
});
|
|
61231
|
+
return refreshAccessInfo.data.access_token;
|
|
61232
|
+
}
|
|
61233
|
+
else {
|
|
61234
|
+
this.client.logger.error('get user access token by refresh token failed.', refreshAccessInfo.msg);
|
|
61235
|
+
return;
|
|
61236
|
+
}
|
|
61237
|
+
}
|
|
61238
|
+
// step3 code存在的话,用code重新获取
|
|
61239
|
+
if (code) {
|
|
61240
|
+
const oidcAccessInfo = yield this.client.authen.oidcAccessToken.create({
|
|
61241
|
+
data: {
|
|
61242
|
+
grant_type: "authorization_code",
|
|
61243
|
+
code: code
|
|
61244
|
+
}
|
|
61245
|
+
});
|
|
61246
|
+
if (oidcAccessInfo.code === 0 && oidcAccessInfo.data) {
|
|
61247
|
+
yield this.update({
|
|
61248
|
+
key: {
|
|
61249
|
+
token: oidcAccessInfo.data.access_token,
|
|
61250
|
+
refreshToken: oidcAccessInfo.data.refresh_token,
|
|
61251
|
+
expiredTime: this.calibrateTime(oidcAccessInfo.data.expires_in)
|
|
61252
|
+
}
|
|
61253
|
+
});
|
|
61254
|
+
}
|
|
61255
|
+
else {
|
|
61256
|
+
this.client.logger.error('get user access token by code failed.', oidcAccessInfo.msg);
|
|
61257
|
+
}
|
|
61258
|
+
}
|
|
61259
|
+
// step4 重试完毕没结果后,返回undefine
|
|
61260
|
+
return;
|
|
61261
|
+
});
|
|
61262
|
+
}
|
|
61263
|
+
}
|
|
61264
|
+
|
|
61114
61265
|
class Client extends Client$1 {
|
|
61115
61266
|
constructor(params) {
|
|
61116
61267
|
super();
|
|
@@ -61141,6 +61292,7 @@ class Client extends Client$1 {
|
|
|
61141
61292
|
appType: this.appType,
|
|
61142
61293
|
httpInstance: this.httpInstance,
|
|
61143
61294
|
});
|
|
61295
|
+
this.userAccessToken = new UserAccessToken({ client: this });
|
|
61144
61296
|
this.logger.info('client ready');
|
|
61145
61297
|
}
|
|
61146
61298
|
formatPayload(payload, options) {
|
|
@@ -62744,4 +62896,265 @@ class WSClient {
|
|
|
62744
62896
|
}
|
|
62745
62897
|
}
|
|
62746
62898
|
|
|
62747
|
-
|
|
62899
|
+
class SessionCache {
|
|
62900
|
+
constructor() {
|
|
62901
|
+
this.sessions = new Map();
|
|
62902
|
+
}
|
|
62903
|
+
set(key, value) {
|
|
62904
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62905
|
+
const sessions = this.sessions.get(key.toString()) || {};
|
|
62906
|
+
const mergedSessions = mergeObject(sessions, value);
|
|
62907
|
+
this.sessions.set(key.toString(), mergedSessions);
|
|
62908
|
+
return true;
|
|
62909
|
+
});
|
|
62910
|
+
}
|
|
62911
|
+
get(key) {
|
|
62912
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62913
|
+
return this.sessions.get(key.toString());
|
|
62914
|
+
});
|
|
62915
|
+
}
|
|
62916
|
+
}
|
|
62917
|
+
|
|
62918
|
+
var EExecStatus;
|
|
62919
|
+
(function (EExecStatus) {
|
|
62920
|
+
EExecStatus[EExecStatus["ERROR"] = -1] = "ERROR";
|
|
62921
|
+
EExecStatus[EExecStatus["SUCCESS"] = 0] = "SUCCESS";
|
|
62922
|
+
EExecStatus[EExecStatus["EXPIRED"] = 1] = "EXPIRED";
|
|
62923
|
+
EExecStatus[EExecStatus["CANCELLED"] = 2] = "CANCELLED";
|
|
62924
|
+
EExecStatus[EExecStatus["FAILED"] = 3] = "FAILED";
|
|
62925
|
+
EExecStatus[EExecStatus["OTHER"] = 4] = "OTHER";
|
|
62926
|
+
})(EExecStatus || (EExecStatus = {}));
|
|
62927
|
+
class Aily {
|
|
62928
|
+
constructor(params) {
|
|
62929
|
+
this.completions = {
|
|
62930
|
+
create: this.create.bind(this),
|
|
62931
|
+
createWithStream: this.createWithStream.bind(this),
|
|
62932
|
+
sessionRecords: {
|
|
62933
|
+
get: this.getRecords.bind(this),
|
|
62934
|
+
update: this.updateRecords.bind(this)
|
|
62935
|
+
}
|
|
62936
|
+
};
|
|
62937
|
+
this.client = params.client;
|
|
62938
|
+
this.logger = params.client.logger;
|
|
62939
|
+
this.cache = params.cache || new SessionCache();
|
|
62940
|
+
}
|
|
62941
|
+
getSessionId(params) {
|
|
62942
|
+
var _a;
|
|
62943
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62944
|
+
const { sessionId, payload, options } = params;
|
|
62945
|
+
const records = yield this.getRecords();
|
|
62946
|
+
if (sessionId && sessionId in records) {
|
|
62947
|
+
return records[sessionId];
|
|
62948
|
+
}
|
|
62949
|
+
const ailySession = yield this.client.aily.v1.ailySession.create({ data: payload }, options);
|
|
62950
|
+
if (ailySession.code === 0 && ailySession.data) {
|
|
62951
|
+
const ailySessionId = (_a = ailySession.data.session) === null || _a === void 0 ? void 0 : _a.id;
|
|
62952
|
+
if (sessionId) {
|
|
62953
|
+
yield this.updateRecords({ [sessionId]: ailySessionId });
|
|
62954
|
+
}
|
|
62955
|
+
return ailySessionId;
|
|
62956
|
+
}
|
|
62957
|
+
else {
|
|
62958
|
+
this.logger.error('get aily session id error', ailySession.msg);
|
|
62959
|
+
return undefined;
|
|
62960
|
+
}
|
|
62961
|
+
});
|
|
62962
|
+
}
|
|
62963
|
+
waitReply(params, options) {
|
|
62964
|
+
var _a, _b;
|
|
62965
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62966
|
+
const { sessionId, ailyAppId, message, sessionInfo, messageInfo, runInfo, skillId } = params;
|
|
62967
|
+
// step1 get aily session id
|
|
62968
|
+
const ailySessionId = yield this.getSessionId({ sessionId, payload: sessionInfo, options });
|
|
62969
|
+
if (!ailySessionId) {
|
|
62970
|
+
throw EExecStatus.ERROR;
|
|
62971
|
+
}
|
|
62972
|
+
// step2 create message and run
|
|
62973
|
+
const ailySessionMessage = yield this.client.aily.v1.ailySessionAilyMessage.create({
|
|
62974
|
+
path: {
|
|
62975
|
+
aily_session_id: ailySessionId
|
|
62976
|
+
},
|
|
62977
|
+
data: Object.assign(Object.assign({}, messageInfo), { content: message, idempotent_id: `${Date.now()}`, content_type: 'MDX' })
|
|
62978
|
+
}, options);
|
|
62979
|
+
if (!(ailySessionMessage.code === 0 && ailySessionMessage.data)) {
|
|
62980
|
+
this.logger.error('create aily message error', ailySessionMessage.msg);
|
|
62981
|
+
throw EExecStatus.ERROR;
|
|
62982
|
+
}
|
|
62983
|
+
const messageId = (_a = ailySessionMessage.data.message) === null || _a === void 0 ? void 0 : _a.id;
|
|
62984
|
+
const ailySessionRun = yield this.client.aily.v1.ailySessionRun.create({
|
|
62985
|
+
path: {
|
|
62986
|
+
aily_session_id: ailySessionId
|
|
62987
|
+
},
|
|
62988
|
+
data: Object.assign({ app_id: ailyAppId, skill_id: skillId }, runInfo)
|
|
62989
|
+
}, options);
|
|
62990
|
+
if (!(ailySessionRun.code === 0 && ailySessionRun.data)) {
|
|
62991
|
+
this.logger.error('create aily session run error', ailySessionRun.msg);
|
|
62992
|
+
throw EExecStatus.ERROR;
|
|
62993
|
+
}
|
|
62994
|
+
const runId = (_b = ailySessionRun.data.run) === null || _b === void 0 ? void 0 : _b.id;
|
|
62995
|
+
if (!runId) {
|
|
62996
|
+
this.logger.error('run id is empty');
|
|
62997
|
+
throw EExecStatus.ERROR;
|
|
62998
|
+
}
|
|
62999
|
+
// step3 wait run complete
|
|
63000
|
+
const polling = () => __awaiter(this, void 0, void 0, function* () {
|
|
63001
|
+
return new Promise((resolve) => {
|
|
63002
|
+
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
|
63003
|
+
var _a, _b;
|
|
63004
|
+
const runStatusInfo = yield this.client.aily.v1.ailySessionRun.get({
|
|
63005
|
+
path: {
|
|
63006
|
+
aily_session_id: ailySessionId,
|
|
63007
|
+
run_id: runId
|
|
63008
|
+
}
|
|
63009
|
+
}, options);
|
|
63010
|
+
if (!(runStatusInfo.code === 0 && runStatusInfo.data)) {
|
|
63011
|
+
resolve(3);
|
|
63012
|
+
return;
|
|
63013
|
+
}
|
|
63014
|
+
const status = (_b = (_a = runStatusInfo.data) === null || _a === void 0 ? void 0 : _a.run) === null || _b === void 0 ? void 0 : _b.status;
|
|
63015
|
+
switch (status) {
|
|
63016
|
+
case 'QUEUED':
|
|
63017
|
+
case 'IN_PROGRESS': yield (() => __awaiter(this, void 0, void 0, function* () {
|
|
63018
|
+
const ret = yield polling();
|
|
63019
|
+
resolve(ret);
|
|
63020
|
+
}))();
|
|
63021
|
+
case 'COMPLETED': resolve(EExecStatus.SUCCESS);
|
|
63022
|
+
case 'EXPIRED': resolve(EExecStatus.EXPIRED);
|
|
63023
|
+
case 'CANCELLED': resolve(EExecStatus.CANCELLED);
|
|
63024
|
+
case 'FAILED': resolve(EExecStatus.FAILED);
|
|
63025
|
+
default: resolve(EExecStatus.OTHER);
|
|
63026
|
+
}
|
|
63027
|
+
}), 500);
|
|
63028
|
+
});
|
|
63029
|
+
});
|
|
63030
|
+
const pollingRet = yield polling();
|
|
63031
|
+
if (pollingRet !== EExecStatus.SUCCESS) {
|
|
63032
|
+
this.logger.error('aily run error');
|
|
63033
|
+
throw pollingRet;
|
|
63034
|
+
}
|
|
63035
|
+
return {
|
|
63036
|
+
ailySessionId,
|
|
63037
|
+
runId,
|
|
63038
|
+
messageId
|
|
63039
|
+
};
|
|
63040
|
+
});
|
|
63041
|
+
}
|
|
63042
|
+
create(params, options) {
|
|
63043
|
+
var _a, e_1, _b, _c;
|
|
63044
|
+
var _d;
|
|
63045
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63046
|
+
const { ailySessionId, runId } = yield this.waitReply(params, options);
|
|
63047
|
+
let reply;
|
|
63048
|
+
try {
|
|
63049
|
+
for (var _e = true, _f = __asyncValues(yield this.client.aily.v1.ailySessionAilyMessage.listWithIterator({
|
|
63050
|
+
path: {
|
|
63051
|
+
aily_session_id: ailySessionId,
|
|
63052
|
+
},
|
|
63053
|
+
params: {
|
|
63054
|
+
run_id: runId,
|
|
63055
|
+
with_partial_message: false,
|
|
63056
|
+
}
|
|
63057
|
+
}, options)), _g; _g = yield _f.next(), _a = _g.done, !_a;) {
|
|
63058
|
+
_c = _g.value;
|
|
63059
|
+
_e = false;
|
|
63060
|
+
try {
|
|
63061
|
+
const items = _c;
|
|
63062
|
+
if (!items) {
|
|
63063
|
+
continue;
|
|
63064
|
+
}
|
|
63065
|
+
(_d = items === null || items === void 0 ? void 0 : items.messages) === null || _d === void 0 ? void 0 : _d.forEach(message => {
|
|
63066
|
+
var _a;
|
|
63067
|
+
if (((_a = message.sender) === null || _a === void 0 ? void 0 : _a.sender_type) === 'ASSISTANT') {
|
|
63068
|
+
reply = message;
|
|
63069
|
+
}
|
|
63070
|
+
});
|
|
63071
|
+
}
|
|
63072
|
+
finally {
|
|
63073
|
+
_e = true;
|
|
63074
|
+
}
|
|
63075
|
+
}
|
|
63076
|
+
}
|
|
63077
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
63078
|
+
finally {
|
|
63079
|
+
try {
|
|
63080
|
+
if (!_e && !_a && (_b = _f.return)) yield _b.call(_f);
|
|
63081
|
+
}
|
|
63082
|
+
finally { if (e_1) throw e_1.error; }
|
|
63083
|
+
}
|
|
63084
|
+
if (!reply) {
|
|
63085
|
+
this.logger.error('no aily reply');
|
|
63086
|
+
throw EExecStatus.ERROR;
|
|
63087
|
+
}
|
|
63088
|
+
return reply;
|
|
63089
|
+
});
|
|
63090
|
+
}
|
|
63091
|
+
createWithStream(params, options) {
|
|
63092
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63093
|
+
const { ailySessionId, runId, messageId } = yield this.waitReply(params, options);
|
|
63094
|
+
const listMessages = () => this.client.aily.v1.ailySessionAilyMessage.listWithIterator({
|
|
63095
|
+
path: {
|
|
63096
|
+
aily_session_id: ailySessionId,
|
|
63097
|
+
},
|
|
63098
|
+
params: {
|
|
63099
|
+
run_id: runId,
|
|
63100
|
+
with_partial_message: true,
|
|
63101
|
+
}
|
|
63102
|
+
}, options);
|
|
63103
|
+
let startOutput = false;
|
|
63104
|
+
const Iterable = {
|
|
63105
|
+
[Symbol.asyncIterator]() {
|
|
63106
|
+
return __asyncGenerator(this, arguments, function* _a() {
|
|
63107
|
+
var _b, e_2, _c, _d;
|
|
63108
|
+
try {
|
|
63109
|
+
for (var _e = true, _f = __asyncValues(yield __await(listMessages())), _g; _g = yield __await(_f.next()), _b = _g.done, !_b;) {
|
|
63110
|
+
_d = _g.value;
|
|
63111
|
+
_e = false;
|
|
63112
|
+
try {
|
|
63113
|
+
const items = _d;
|
|
63114
|
+
if (!items) {
|
|
63115
|
+
continue;
|
|
63116
|
+
}
|
|
63117
|
+
for (const message of items.messages || []) {
|
|
63118
|
+
if (startOutput) {
|
|
63119
|
+
yield yield __await(message);
|
|
63120
|
+
if (message.status === 'COMPLETED') {
|
|
63121
|
+
return yield __await(void 0);
|
|
63122
|
+
}
|
|
63123
|
+
}
|
|
63124
|
+
else if (message.id === messageId && message.status === 'COMPLETED') {
|
|
63125
|
+
startOutput = true;
|
|
63126
|
+
}
|
|
63127
|
+
}
|
|
63128
|
+
}
|
|
63129
|
+
finally {
|
|
63130
|
+
_e = true;
|
|
63131
|
+
}
|
|
63132
|
+
}
|
|
63133
|
+
}
|
|
63134
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
63135
|
+
finally {
|
|
63136
|
+
try {
|
|
63137
|
+
if (!_e && !_b && (_c = _f.return)) yield __await(_c.call(_f));
|
|
63138
|
+
}
|
|
63139
|
+
finally { if (e_2) throw e_2.error; }
|
|
63140
|
+
}
|
|
63141
|
+
});
|
|
63142
|
+
}
|
|
63143
|
+
};
|
|
63144
|
+
return Iterable;
|
|
63145
|
+
});
|
|
63146
|
+
}
|
|
63147
|
+
getRecords() {
|
|
63148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63149
|
+
const sessions = (yield this.cache.get(CAilySessionRecord)) || {};
|
|
63150
|
+
return sessions;
|
|
63151
|
+
});
|
|
63152
|
+
}
|
|
63153
|
+
updateRecords(records) {
|
|
63154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63155
|
+
yield this.cache.set(CAilySessionRecord, records);
|
|
63156
|
+
});
|
|
63157
|
+
}
|
|
63158
|
+
}
|
|
63159
|
+
|
|
63160
|
+
export { AESCipher, Aily, AppType, CAppTicket, CTenantAccessToken, CardActionHandler, Client, Domain, EventDispatcher, LoggerLevel, WSClient, adaptDefault, adaptExpress, adaptKoa, adaptKoaRouter, defaultHttpInstance, generateChallenge, messageCard, withAll, withHelpDeskCredential, withTenantKey, withTenantToken, withUserAccessToken };
|
package/lib/index.js
CHANGED
|
@@ -75,6 +75,18 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
75
75
|
});
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
function __values(o) {
|
|
79
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
80
|
+
if (m) return m.call(o);
|
|
81
|
+
if (o && typeof o.length === "number") return {
|
|
82
|
+
next: function () {
|
|
83
|
+
if (o && i >= o.length) o = void 0;
|
|
84
|
+
return { value: o && o[i++], done: !o };
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
88
|
+
}
|
|
89
|
+
|
|
78
90
|
function __await(v) {
|
|
79
91
|
return this instanceof __await ? (this.v = v, this) : new __await(v);
|
|
80
92
|
}
|
|
@@ -91,6 +103,14 @@ function __asyncGenerator(thisArg, _arguments, generator) {
|
|
|
91
103
|
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
92
104
|
}
|
|
93
105
|
|
|
106
|
+
function __asyncValues(o) {
|
|
107
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
108
|
+
var m = o[Symbol.asyncIterator], i;
|
|
109
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
110
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
111
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
112
|
+
}
|
|
113
|
+
|
|
94
114
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
95
115
|
var e = new Error(message);
|
|
96
116
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
@@ -131,6 +151,8 @@ const CAppTicket = Symbol('app-ticket');
|
|
|
131
151
|
const CTenantAccessToken = Symbol('tenant-access-token');
|
|
132
152
|
const CWithHelpdeskAuthorization = Symbol('with-helpdesk-authorization');
|
|
133
153
|
const CWithUserAccessToken = Symbol('with-user-access-token');
|
|
154
|
+
const CUserAccessToken = Symbol('user-access-token');
|
|
155
|
+
const CAilySessionRecord = Symbol('aily-session-record');
|
|
134
156
|
|
|
135
157
|
const string2Base64 = (content) => Buffer.from(content).toString('base64');
|
|
136
158
|
|
|
@@ -61128,6 +61150,135 @@ class TokenManager {
|
|
|
61128
61150
|
}
|
|
61129
61151
|
}
|
|
61130
61152
|
|
|
61153
|
+
const mergeObject = (obj1, obj2) => {
|
|
61154
|
+
const mergedObject = Object.assign({}, obj1);
|
|
61155
|
+
for (let [key, value] of Object.entries(obj2)) {
|
|
61156
|
+
if (value !== undefined) {
|
|
61157
|
+
mergedObject[key] = value;
|
|
61158
|
+
}
|
|
61159
|
+
}
|
|
61160
|
+
return mergedObject;
|
|
61161
|
+
};
|
|
61162
|
+
|
|
61163
|
+
class UserAccessToken {
|
|
61164
|
+
constructor(params) {
|
|
61165
|
+
this.client = params.client;
|
|
61166
|
+
}
|
|
61167
|
+
getCacheKey(key, options) {
|
|
61168
|
+
const namespace = get__default["default"](options, 'namespace', this.client.appId);
|
|
61169
|
+
return `${namespace}/${CUserAccessToken.toString()}/${key}`;
|
|
61170
|
+
}
|
|
61171
|
+
// the unit of time is seconds
|
|
61172
|
+
calibrateTime(time) {
|
|
61173
|
+
// Due to the time-consuming network, the time needs to be 3 minutes earlier
|
|
61174
|
+
return new Date().getTime() + (time || 0) * 1000 - 3 * 60 * 1000;
|
|
61175
|
+
}
|
|
61176
|
+
initWithCode(key2Code, options) {
|
|
61177
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61178
|
+
const key2Info = {};
|
|
61179
|
+
for (const [key, code] of Object.entries(key2Code)) {
|
|
61180
|
+
const oidcAccessInfo = yield this.client.authen.oidcAccessToken.create({
|
|
61181
|
+
data: {
|
|
61182
|
+
grant_type: 'authorization_code',
|
|
61183
|
+
code
|
|
61184
|
+
}
|
|
61185
|
+
});
|
|
61186
|
+
if (oidcAccessInfo.code !== 0) {
|
|
61187
|
+
// @ts-ignore
|
|
61188
|
+
this.client.logger.error('init user access token error', key, oidcAccessInfo.msg || oidcAccessInfo.message);
|
|
61189
|
+
continue;
|
|
61190
|
+
}
|
|
61191
|
+
// code expired
|
|
61192
|
+
if (!oidcAccessInfo.data) {
|
|
61193
|
+
this.client.logger.error('user access code expired', key, code);
|
|
61194
|
+
continue;
|
|
61195
|
+
}
|
|
61196
|
+
key2Info[key] = {
|
|
61197
|
+
code,
|
|
61198
|
+
token: oidcAccessInfo.data.access_token,
|
|
61199
|
+
refreshToken: oidcAccessInfo.data.refresh_token,
|
|
61200
|
+
expiredTime: this.calibrateTime(oidcAccessInfo.data.expires_in)
|
|
61201
|
+
};
|
|
61202
|
+
}
|
|
61203
|
+
yield this.update(key2Info, { namespace: get__default["default"](options, 'namespace') });
|
|
61204
|
+
return key2Info;
|
|
61205
|
+
});
|
|
61206
|
+
}
|
|
61207
|
+
update(key2Info, options) {
|
|
61208
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61209
|
+
for (const [key, info] of Object.entries(key2Info)) {
|
|
61210
|
+
const cacheKey = this.getCacheKey(key, { namespace: get__default["default"](options, 'namespace') });
|
|
61211
|
+
const cacheValue = (yield this.client.cache.get(cacheKey)) || {};
|
|
61212
|
+
const { code, token, refreshToken, expiredTime } = info;
|
|
61213
|
+
const targetValue = mergeObject(cacheValue, { code, token, refreshToken, expiredTime });
|
|
61214
|
+
yield this.client.cache.set(cacheKey, targetValue, Infinity);
|
|
61215
|
+
}
|
|
61216
|
+
});
|
|
61217
|
+
}
|
|
61218
|
+
get(key, options) {
|
|
61219
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
61220
|
+
const cacheKey = this.getCacheKey(key, { namespace: get__default["default"](options, 'namespace') });
|
|
61221
|
+
const cacheInfo = yield this.client.cache.get(cacheKey);
|
|
61222
|
+
// cacheInfo是否存在
|
|
61223
|
+
if (!cacheInfo) {
|
|
61224
|
+
this.client.logger.error('user access token needs to be initialized or updated first');
|
|
61225
|
+
return;
|
|
61226
|
+
}
|
|
61227
|
+
const { token, code, refreshToken, expiredTime } = cacheInfo;
|
|
61228
|
+
// step1 token存在且未过期
|
|
61229
|
+
if (token && expiredTime && expiredTime - new Date().getTime() > 0) {
|
|
61230
|
+
return token;
|
|
61231
|
+
}
|
|
61232
|
+
// step2 refresh token存在,刷新token
|
|
61233
|
+
if (refreshToken) {
|
|
61234
|
+
const refreshAccessInfo = yield this.client.authen.oidcRefreshAccessToken.create({
|
|
61235
|
+
data: {
|
|
61236
|
+
grant_type: 'refresh_token',
|
|
61237
|
+
refresh_token: refreshToken
|
|
61238
|
+
}
|
|
61239
|
+
});
|
|
61240
|
+
if (refreshAccessInfo.code === 0 && refreshAccessInfo.data) {
|
|
61241
|
+
yield this.update({
|
|
61242
|
+
key: {
|
|
61243
|
+
token: refreshAccessInfo.data.access_token,
|
|
61244
|
+
refreshToken: refreshAccessInfo.data.refresh_token,
|
|
61245
|
+
expiredTime: this.calibrateTime(refreshAccessInfo.data.expires_in)
|
|
61246
|
+
}
|
|
61247
|
+
});
|
|
61248
|
+
return refreshAccessInfo.data.access_token;
|
|
61249
|
+
}
|
|
61250
|
+
else {
|
|
61251
|
+
this.client.logger.error('get user access token by refresh token failed.', refreshAccessInfo.msg);
|
|
61252
|
+
return;
|
|
61253
|
+
}
|
|
61254
|
+
}
|
|
61255
|
+
// step3 code存在的话,用code重新获取
|
|
61256
|
+
if (code) {
|
|
61257
|
+
const oidcAccessInfo = yield this.client.authen.oidcAccessToken.create({
|
|
61258
|
+
data: {
|
|
61259
|
+
grant_type: "authorization_code",
|
|
61260
|
+
code: code
|
|
61261
|
+
}
|
|
61262
|
+
});
|
|
61263
|
+
if (oidcAccessInfo.code === 0 && oidcAccessInfo.data) {
|
|
61264
|
+
yield this.update({
|
|
61265
|
+
key: {
|
|
61266
|
+
token: oidcAccessInfo.data.access_token,
|
|
61267
|
+
refreshToken: oidcAccessInfo.data.refresh_token,
|
|
61268
|
+
expiredTime: this.calibrateTime(oidcAccessInfo.data.expires_in)
|
|
61269
|
+
}
|
|
61270
|
+
});
|
|
61271
|
+
}
|
|
61272
|
+
else {
|
|
61273
|
+
this.client.logger.error('get user access token by code failed.', oidcAccessInfo.msg);
|
|
61274
|
+
}
|
|
61275
|
+
}
|
|
61276
|
+
// step4 重试完毕没结果后,返回undefine
|
|
61277
|
+
return;
|
|
61278
|
+
});
|
|
61279
|
+
}
|
|
61280
|
+
}
|
|
61281
|
+
|
|
61131
61282
|
class Client extends Client$1 {
|
|
61132
61283
|
constructor(params) {
|
|
61133
61284
|
super();
|
|
@@ -61158,6 +61309,7 @@ class Client extends Client$1 {
|
|
|
61158
61309
|
appType: this.appType,
|
|
61159
61310
|
httpInstance: this.httpInstance,
|
|
61160
61311
|
});
|
|
61312
|
+
this.userAccessToken = new UserAccessToken({ client: this });
|
|
61161
61313
|
this.logger.info('client ready');
|
|
61162
61314
|
}
|
|
61163
61315
|
formatPayload(payload, options) {
|
|
@@ -62761,7 +62913,269 @@ class WSClient {
|
|
|
62761
62913
|
}
|
|
62762
62914
|
}
|
|
62763
62915
|
|
|
62916
|
+
class SessionCache {
|
|
62917
|
+
constructor() {
|
|
62918
|
+
this.sessions = new Map();
|
|
62919
|
+
}
|
|
62920
|
+
set(key, value) {
|
|
62921
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62922
|
+
const sessions = this.sessions.get(key.toString()) || {};
|
|
62923
|
+
const mergedSessions = mergeObject(sessions, value);
|
|
62924
|
+
this.sessions.set(key.toString(), mergedSessions);
|
|
62925
|
+
return true;
|
|
62926
|
+
});
|
|
62927
|
+
}
|
|
62928
|
+
get(key) {
|
|
62929
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62930
|
+
return this.sessions.get(key.toString());
|
|
62931
|
+
});
|
|
62932
|
+
}
|
|
62933
|
+
}
|
|
62934
|
+
|
|
62935
|
+
var EExecStatus;
|
|
62936
|
+
(function (EExecStatus) {
|
|
62937
|
+
EExecStatus[EExecStatus["ERROR"] = -1] = "ERROR";
|
|
62938
|
+
EExecStatus[EExecStatus["SUCCESS"] = 0] = "SUCCESS";
|
|
62939
|
+
EExecStatus[EExecStatus["EXPIRED"] = 1] = "EXPIRED";
|
|
62940
|
+
EExecStatus[EExecStatus["CANCELLED"] = 2] = "CANCELLED";
|
|
62941
|
+
EExecStatus[EExecStatus["FAILED"] = 3] = "FAILED";
|
|
62942
|
+
EExecStatus[EExecStatus["OTHER"] = 4] = "OTHER";
|
|
62943
|
+
})(EExecStatus || (EExecStatus = {}));
|
|
62944
|
+
class Aily {
|
|
62945
|
+
constructor(params) {
|
|
62946
|
+
this.completions = {
|
|
62947
|
+
create: this.create.bind(this),
|
|
62948
|
+
createWithStream: this.createWithStream.bind(this),
|
|
62949
|
+
sessionRecords: {
|
|
62950
|
+
get: this.getRecords.bind(this),
|
|
62951
|
+
update: this.updateRecords.bind(this)
|
|
62952
|
+
}
|
|
62953
|
+
};
|
|
62954
|
+
this.client = params.client;
|
|
62955
|
+
this.logger = params.client.logger;
|
|
62956
|
+
this.cache = params.cache || new SessionCache();
|
|
62957
|
+
}
|
|
62958
|
+
getSessionId(params) {
|
|
62959
|
+
var _a;
|
|
62960
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62961
|
+
const { sessionId, payload, options } = params;
|
|
62962
|
+
const records = yield this.getRecords();
|
|
62963
|
+
if (sessionId && sessionId in records) {
|
|
62964
|
+
return records[sessionId];
|
|
62965
|
+
}
|
|
62966
|
+
const ailySession = yield this.client.aily.v1.ailySession.create({ data: payload }, options);
|
|
62967
|
+
if (ailySession.code === 0 && ailySession.data) {
|
|
62968
|
+
const ailySessionId = (_a = ailySession.data.session) === null || _a === void 0 ? void 0 : _a.id;
|
|
62969
|
+
if (sessionId) {
|
|
62970
|
+
yield this.updateRecords({ [sessionId]: ailySessionId });
|
|
62971
|
+
}
|
|
62972
|
+
return ailySessionId;
|
|
62973
|
+
}
|
|
62974
|
+
else {
|
|
62975
|
+
this.logger.error('get aily session id error', ailySession.msg);
|
|
62976
|
+
return undefined;
|
|
62977
|
+
}
|
|
62978
|
+
});
|
|
62979
|
+
}
|
|
62980
|
+
waitReply(params, options) {
|
|
62981
|
+
var _a, _b;
|
|
62982
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62983
|
+
const { sessionId, ailyAppId, message, sessionInfo, messageInfo, runInfo, skillId } = params;
|
|
62984
|
+
// step1 get aily session id
|
|
62985
|
+
const ailySessionId = yield this.getSessionId({ sessionId, payload: sessionInfo, options });
|
|
62986
|
+
if (!ailySessionId) {
|
|
62987
|
+
throw EExecStatus.ERROR;
|
|
62988
|
+
}
|
|
62989
|
+
// step2 create message and run
|
|
62990
|
+
const ailySessionMessage = yield this.client.aily.v1.ailySessionAilyMessage.create({
|
|
62991
|
+
path: {
|
|
62992
|
+
aily_session_id: ailySessionId
|
|
62993
|
+
},
|
|
62994
|
+
data: Object.assign(Object.assign({}, messageInfo), { content: message, idempotent_id: `${Date.now()}`, content_type: 'MDX' })
|
|
62995
|
+
}, options);
|
|
62996
|
+
if (!(ailySessionMessage.code === 0 && ailySessionMessage.data)) {
|
|
62997
|
+
this.logger.error('create aily message error', ailySessionMessage.msg);
|
|
62998
|
+
throw EExecStatus.ERROR;
|
|
62999
|
+
}
|
|
63000
|
+
const messageId = (_a = ailySessionMessage.data.message) === null || _a === void 0 ? void 0 : _a.id;
|
|
63001
|
+
const ailySessionRun = yield this.client.aily.v1.ailySessionRun.create({
|
|
63002
|
+
path: {
|
|
63003
|
+
aily_session_id: ailySessionId
|
|
63004
|
+
},
|
|
63005
|
+
data: Object.assign({ app_id: ailyAppId, skill_id: skillId }, runInfo)
|
|
63006
|
+
}, options);
|
|
63007
|
+
if (!(ailySessionRun.code === 0 && ailySessionRun.data)) {
|
|
63008
|
+
this.logger.error('create aily session run error', ailySessionRun.msg);
|
|
63009
|
+
throw EExecStatus.ERROR;
|
|
63010
|
+
}
|
|
63011
|
+
const runId = (_b = ailySessionRun.data.run) === null || _b === void 0 ? void 0 : _b.id;
|
|
63012
|
+
if (!runId) {
|
|
63013
|
+
this.logger.error('run id is empty');
|
|
63014
|
+
throw EExecStatus.ERROR;
|
|
63015
|
+
}
|
|
63016
|
+
// step3 wait run complete
|
|
63017
|
+
const polling = () => __awaiter(this, void 0, void 0, function* () {
|
|
63018
|
+
return new Promise((resolve) => {
|
|
63019
|
+
setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
|
63020
|
+
var _a, _b;
|
|
63021
|
+
const runStatusInfo = yield this.client.aily.v1.ailySessionRun.get({
|
|
63022
|
+
path: {
|
|
63023
|
+
aily_session_id: ailySessionId,
|
|
63024
|
+
run_id: runId
|
|
63025
|
+
}
|
|
63026
|
+
}, options);
|
|
63027
|
+
if (!(runStatusInfo.code === 0 && runStatusInfo.data)) {
|
|
63028
|
+
resolve(3);
|
|
63029
|
+
return;
|
|
63030
|
+
}
|
|
63031
|
+
const status = (_b = (_a = runStatusInfo.data) === null || _a === void 0 ? void 0 : _a.run) === null || _b === void 0 ? void 0 : _b.status;
|
|
63032
|
+
switch (status) {
|
|
63033
|
+
case 'QUEUED':
|
|
63034
|
+
case 'IN_PROGRESS': yield (() => __awaiter(this, void 0, void 0, function* () {
|
|
63035
|
+
const ret = yield polling();
|
|
63036
|
+
resolve(ret);
|
|
63037
|
+
}))();
|
|
63038
|
+
case 'COMPLETED': resolve(EExecStatus.SUCCESS);
|
|
63039
|
+
case 'EXPIRED': resolve(EExecStatus.EXPIRED);
|
|
63040
|
+
case 'CANCELLED': resolve(EExecStatus.CANCELLED);
|
|
63041
|
+
case 'FAILED': resolve(EExecStatus.FAILED);
|
|
63042
|
+
default: resolve(EExecStatus.OTHER);
|
|
63043
|
+
}
|
|
63044
|
+
}), 500);
|
|
63045
|
+
});
|
|
63046
|
+
});
|
|
63047
|
+
const pollingRet = yield polling();
|
|
63048
|
+
if (pollingRet !== EExecStatus.SUCCESS) {
|
|
63049
|
+
this.logger.error('aily run error');
|
|
63050
|
+
throw pollingRet;
|
|
63051
|
+
}
|
|
63052
|
+
return {
|
|
63053
|
+
ailySessionId,
|
|
63054
|
+
runId,
|
|
63055
|
+
messageId
|
|
63056
|
+
};
|
|
63057
|
+
});
|
|
63058
|
+
}
|
|
63059
|
+
create(params, options) {
|
|
63060
|
+
var _a, e_1, _b, _c;
|
|
63061
|
+
var _d;
|
|
63062
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63063
|
+
const { ailySessionId, runId } = yield this.waitReply(params, options);
|
|
63064
|
+
let reply;
|
|
63065
|
+
try {
|
|
63066
|
+
for (var _e = true, _f = __asyncValues(yield this.client.aily.v1.ailySessionAilyMessage.listWithIterator({
|
|
63067
|
+
path: {
|
|
63068
|
+
aily_session_id: ailySessionId,
|
|
63069
|
+
},
|
|
63070
|
+
params: {
|
|
63071
|
+
run_id: runId,
|
|
63072
|
+
with_partial_message: false,
|
|
63073
|
+
}
|
|
63074
|
+
}, options)), _g; _g = yield _f.next(), _a = _g.done, !_a;) {
|
|
63075
|
+
_c = _g.value;
|
|
63076
|
+
_e = false;
|
|
63077
|
+
try {
|
|
63078
|
+
const items = _c;
|
|
63079
|
+
if (!items) {
|
|
63080
|
+
continue;
|
|
63081
|
+
}
|
|
63082
|
+
(_d = items === null || items === void 0 ? void 0 : items.messages) === null || _d === void 0 ? void 0 : _d.forEach(message => {
|
|
63083
|
+
var _a;
|
|
63084
|
+
if (((_a = message.sender) === null || _a === void 0 ? void 0 : _a.sender_type) === 'ASSISTANT') {
|
|
63085
|
+
reply = message;
|
|
63086
|
+
}
|
|
63087
|
+
});
|
|
63088
|
+
}
|
|
63089
|
+
finally {
|
|
63090
|
+
_e = true;
|
|
63091
|
+
}
|
|
63092
|
+
}
|
|
63093
|
+
}
|
|
63094
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
63095
|
+
finally {
|
|
63096
|
+
try {
|
|
63097
|
+
if (!_e && !_a && (_b = _f.return)) yield _b.call(_f);
|
|
63098
|
+
}
|
|
63099
|
+
finally { if (e_1) throw e_1.error; }
|
|
63100
|
+
}
|
|
63101
|
+
if (!reply) {
|
|
63102
|
+
this.logger.error('no aily reply');
|
|
63103
|
+
throw EExecStatus.ERROR;
|
|
63104
|
+
}
|
|
63105
|
+
return reply;
|
|
63106
|
+
});
|
|
63107
|
+
}
|
|
63108
|
+
createWithStream(params, options) {
|
|
63109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63110
|
+
const { ailySessionId, runId, messageId } = yield this.waitReply(params, options);
|
|
63111
|
+
const listMessages = () => this.client.aily.v1.ailySessionAilyMessage.listWithIterator({
|
|
63112
|
+
path: {
|
|
63113
|
+
aily_session_id: ailySessionId,
|
|
63114
|
+
},
|
|
63115
|
+
params: {
|
|
63116
|
+
run_id: runId,
|
|
63117
|
+
with_partial_message: true,
|
|
63118
|
+
}
|
|
63119
|
+
}, options);
|
|
63120
|
+
let startOutput = false;
|
|
63121
|
+
const Iterable = {
|
|
63122
|
+
[Symbol.asyncIterator]() {
|
|
63123
|
+
return __asyncGenerator(this, arguments, function* _a() {
|
|
63124
|
+
var _b, e_2, _c, _d;
|
|
63125
|
+
try {
|
|
63126
|
+
for (var _e = true, _f = __asyncValues(yield __await(listMessages())), _g; _g = yield __await(_f.next()), _b = _g.done, !_b;) {
|
|
63127
|
+
_d = _g.value;
|
|
63128
|
+
_e = false;
|
|
63129
|
+
try {
|
|
63130
|
+
const items = _d;
|
|
63131
|
+
if (!items) {
|
|
63132
|
+
continue;
|
|
63133
|
+
}
|
|
63134
|
+
for (const message of items.messages || []) {
|
|
63135
|
+
if (startOutput) {
|
|
63136
|
+
yield yield __await(message);
|
|
63137
|
+
if (message.status === 'COMPLETED') {
|
|
63138
|
+
return yield __await(void 0);
|
|
63139
|
+
}
|
|
63140
|
+
}
|
|
63141
|
+
else if (message.id === messageId && message.status === 'COMPLETED') {
|
|
63142
|
+
startOutput = true;
|
|
63143
|
+
}
|
|
63144
|
+
}
|
|
63145
|
+
}
|
|
63146
|
+
finally {
|
|
63147
|
+
_e = true;
|
|
63148
|
+
}
|
|
63149
|
+
}
|
|
63150
|
+
}
|
|
63151
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
63152
|
+
finally {
|
|
63153
|
+
try {
|
|
63154
|
+
if (!_e && !_b && (_c = _f.return)) yield __await(_c.call(_f));
|
|
63155
|
+
}
|
|
63156
|
+
finally { if (e_2) throw e_2.error; }
|
|
63157
|
+
}
|
|
63158
|
+
});
|
|
63159
|
+
}
|
|
63160
|
+
};
|
|
63161
|
+
return Iterable;
|
|
63162
|
+
});
|
|
63163
|
+
}
|
|
63164
|
+
getRecords() {
|
|
63165
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63166
|
+
const sessions = (yield this.cache.get(CAilySessionRecord)) || {};
|
|
63167
|
+
return sessions;
|
|
63168
|
+
});
|
|
63169
|
+
}
|
|
63170
|
+
updateRecords(records) {
|
|
63171
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63172
|
+
yield this.cache.set(CAilySessionRecord, records);
|
|
63173
|
+
});
|
|
63174
|
+
}
|
|
63175
|
+
}
|
|
63176
|
+
|
|
62764
63177
|
exports.AESCipher = AESCipher;
|
|
63178
|
+
exports.Aily = Aily;
|
|
62765
63179
|
exports.CAppTicket = CAppTicket;
|
|
62766
63180
|
exports.CTenantAccessToken = CTenantAccessToken;
|
|
62767
63181
|
exports.CardActionHandler = CardActionHandler;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@larksuiteoapi/node-sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.31.0",
|
|
4
4
|
"description": "larksuite open sdk for nodejs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"feishu",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"scripts": {
|
|
23
23
|
"build": "rm -r lib es types & rollup -c",
|
|
24
24
|
"test": "jest",
|
|
25
|
-
"test:watch": "jest --watch --testPathPattern=
|
|
25
|
+
"test:watch": "jest --watch --testPathPattern=utils/__tests__"
|
|
26
26
|
},
|
|
27
27
|
"author": "mazhe.nerd",
|
|
28
28
|
"license": "MIT",
|
package/types/index.d.ts
CHANGED
|
@@ -166065,6 +166065,30 @@ declare class TokenManager {
|
|
|
166065
166065
|
}): Promise<any>;
|
|
166066
166066
|
}
|
|
166067
166067
|
|
|
166068
|
+
interface ITokenInfo {
|
|
166069
|
+
code?: string;
|
|
166070
|
+
token?: string;
|
|
166071
|
+
refreshToken?: string;
|
|
166072
|
+
expiredTime?: number;
|
|
166073
|
+
}
|
|
166074
|
+
declare class UserAccessToken {
|
|
166075
|
+
client: Client;
|
|
166076
|
+
constructor(params: {
|
|
166077
|
+
client: Client;
|
|
166078
|
+
});
|
|
166079
|
+
private getCacheKey;
|
|
166080
|
+
private calibrateTime;
|
|
166081
|
+
initWithCode(key2Code: Record<string, string>, options?: {
|
|
166082
|
+
namespace?: string;
|
|
166083
|
+
}): Promise<Record<string, ITokenInfo>>;
|
|
166084
|
+
update(key2Info: Record<string, ITokenInfo>, options?: {
|
|
166085
|
+
namespace?: string;
|
|
166086
|
+
}): Promise<void>;
|
|
166087
|
+
get(key: string, options?: {
|
|
166088
|
+
namespace?: string;
|
|
166089
|
+
}): Promise<any>;
|
|
166090
|
+
}
|
|
166091
|
+
|
|
166068
166092
|
declare class Client extends Client$1 {
|
|
166069
166093
|
appId: string;
|
|
166070
166094
|
appSecret: string;
|
|
@@ -166077,6 +166101,7 @@ declare class Client extends Client$1 {
|
|
|
166077
166101
|
appType: AppType;
|
|
166078
166102
|
domain: string;
|
|
166079
166103
|
httpInstance: HttpInstance;
|
|
166104
|
+
userAccessToken: UserAccessToken;
|
|
166080
166105
|
constructor(params: IClientParams);
|
|
166081
166106
|
formatPayload(payload?: IPayload, options?: IRequestOptions): Promise<Required<IPayload>>;
|
|
166082
166107
|
request<T = any>(payload: AxiosRequestConfig, options?: IRequestOptions): Promise<T>;
|
|
@@ -171519,4 +171544,104 @@ declare class WSClient {
|
|
|
171519
171544
|
}): Promise<void>;
|
|
171520
171545
|
}
|
|
171521
171546
|
|
|
171522
|
-
|
|
171547
|
+
type TClientInstance = typeof Client extends new (...args: any[]) => infer R ? R : any;
|
|
171548
|
+
type TPickParams<T extends (...args: any) => any> = Exclude<Parameters<T>['0'], undefined>;
|
|
171549
|
+
type TCreateSessionParams = TPickParams<TClientInstance['aily']['v1']['ailySession']['create']>;
|
|
171550
|
+
type TCreateMessageParams = TPickParams<TClientInstance['aily']['v1']['ailySessionAilyMessage']['create']>;
|
|
171551
|
+
type TCreateRunParams = TPickParams<TClientInstance['aily']['v1']['ailySessionRun']['create']>;
|
|
171552
|
+
interface ICreateParams {
|
|
171553
|
+
sessionId?: string;
|
|
171554
|
+
sessionInfo?: TCreateSessionParams['data'];
|
|
171555
|
+
message: string;
|
|
171556
|
+
messageInfo?: Omit<TCreateMessageParams['data'], 'idempotent_id' | 'content_type' | 'content'>;
|
|
171557
|
+
ailyAppId: string;
|
|
171558
|
+
skillId?: string;
|
|
171559
|
+
runInfo?: Omit<TCreateRunParams['data'], 'app_id' | 'skill_id'>;
|
|
171560
|
+
}
|
|
171561
|
+
declare class Aily {
|
|
171562
|
+
client: Client;
|
|
171563
|
+
cache: Cache;
|
|
171564
|
+
logger: Logger;
|
|
171565
|
+
constructor(params: {
|
|
171566
|
+
client: Client;
|
|
171567
|
+
cache?: Cache;
|
|
171568
|
+
});
|
|
171569
|
+
private getSessionId;
|
|
171570
|
+
private waitReply;
|
|
171571
|
+
private create;
|
|
171572
|
+
private createWithStream;
|
|
171573
|
+
private getRecords;
|
|
171574
|
+
private updateRecords;
|
|
171575
|
+
completions: {
|
|
171576
|
+
create: (params: ICreateParams, options?: IRequestOptions) => Promise<{
|
|
171577
|
+
id?: string | undefined;
|
|
171578
|
+
session_id?: string | undefined;
|
|
171579
|
+
run_id?: string | undefined;
|
|
171580
|
+
content_type?: string | undefined;
|
|
171581
|
+
content?: string | undefined;
|
|
171582
|
+
files?: {
|
|
171583
|
+
id?: string | undefined;
|
|
171584
|
+
mime_type?: string | undefined;
|
|
171585
|
+
file_name?: string | undefined;
|
|
171586
|
+
metadata?: string | undefined;
|
|
171587
|
+
created_at?: string | undefined;
|
|
171588
|
+
}[] | undefined;
|
|
171589
|
+
quote_message_id?: string | undefined;
|
|
171590
|
+
sender?: {
|
|
171591
|
+
entity_id?: string | undefined;
|
|
171592
|
+
identity_provider?: string | undefined;
|
|
171593
|
+
sender_type?: string | undefined;
|
|
171594
|
+
aily_id?: string | undefined;
|
|
171595
|
+
} | undefined;
|
|
171596
|
+
mentions?: {
|
|
171597
|
+
entity_id?: string | undefined;
|
|
171598
|
+
identity_provider?: string | undefined;
|
|
171599
|
+
key?: string | undefined;
|
|
171600
|
+
name?: string | undefined;
|
|
171601
|
+
aily_id?: string | undefined;
|
|
171602
|
+
}[] | undefined;
|
|
171603
|
+
plain_text?: string | undefined;
|
|
171604
|
+
created_at?: string | undefined;
|
|
171605
|
+
status?: string | undefined;
|
|
171606
|
+
}>;
|
|
171607
|
+
createWithStream: (params: ICreateParams, options?: IRequestOptions) => Promise<{
|
|
171608
|
+
[Symbol.asyncIterator](): AsyncGenerator<{
|
|
171609
|
+
id?: string | undefined;
|
|
171610
|
+
session_id?: string | undefined;
|
|
171611
|
+
run_id?: string | undefined;
|
|
171612
|
+
content_type?: string | undefined;
|
|
171613
|
+
content?: string | undefined;
|
|
171614
|
+
files?: {
|
|
171615
|
+
id?: string | undefined;
|
|
171616
|
+
mime_type?: string | undefined;
|
|
171617
|
+
file_name?: string | undefined;
|
|
171618
|
+
metadata?: string | undefined;
|
|
171619
|
+
created_at?: string | undefined;
|
|
171620
|
+
}[] | undefined;
|
|
171621
|
+
quote_message_id?: string | undefined;
|
|
171622
|
+
sender?: {
|
|
171623
|
+
entity_id?: string | undefined;
|
|
171624
|
+
identity_provider?: string | undefined;
|
|
171625
|
+
sender_type?: string | undefined;
|
|
171626
|
+
aily_id?: string | undefined;
|
|
171627
|
+
} | undefined;
|
|
171628
|
+
mentions?: {
|
|
171629
|
+
entity_id?: string | undefined;
|
|
171630
|
+
identity_provider?: string | undefined;
|
|
171631
|
+
key?: string | undefined;
|
|
171632
|
+
name?: string | undefined;
|
|
171633
|
+
aily_id?: string | undefined;
|
|
171634
|
+
}[] | undefined;
|
|
171635
|
+
plain_text?: string | undefined;
|
|
171636
|
+
created_at?: string | undefined;
|
|
171637
|
+
status?: string | undefined;
|
|
171638
|
+
}, void, unknown>;
|
|
171639
|
+
}>;
|
|
171640
|
+
sessionRecords: {
|
|
171641
|
+
get: () => Promise<any>;
|
|
171642
|
+
update: (records: Record<string, string>) => Promise<void>;
|
|
171643
|
+
};
|
|
171644
|
+
};
|
|
171645
|
+
}
|
|
171646
|
+
|
|
171647
|
+
export { AESCipher, Aily, AppType, CAppTicket, CTenantAccessToken, Cache, CardActionHandler, Client, Domain, EventDispatcher, IHandles as EventHandles, HttpInstance, HttpRequestOptions, InteractiveCard, InteractiveCardActionEvent, InteractiveCardActionItem, InteractiveCardButtonActionItem, InteractiveCardDatePickerActionItem, InteractiveCardDivElement, InteractiveCardDividerElement, InteractiveCardElement, InteractiveCardField, InteractiveCardImageElement, InteractiveCardImageItem, InteractiveCardLarkMdItem, InteractiveCardMarkdownElement, InteractiveCardNoteElement, InteractiveCardOverflowActionItem, InteractiveCardPlainTextItem, InteractiveCardSelectMenuActionItem, InteractiveCardTextItem, InteractiveCardTitle, InteractiveCardUrlItem, InterfaceCardActionElement, LoggerLevel, WSClient, adaptDefault, adaptExpress, adaptKoa, adaptKoaRouter, defaultHttpInstance, generateChallenge, messageCard, withAll, withHelpDeskCredential, withTenantKey, withTenantToken, withUserAccessToken };
|