@glodon-aiot/bot-client-sdk 3.25.0 → 3.25.1-alpha.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/dist/es/BotClient.mjs +73 -73
- package/dist/es/Session.mjs +163 -152
- package/dist/es/package.json.mjs +2 -2
- package/dist/es/utils.mjs +16 -9
- package/dist/lib/index.js +4 -3
- package/dist/src/Session.d.ts +1 -0
- package/dist/src/utils.d.ts +5 -0
- package/package.json +2 -2
package/dist/es/Session.mjs
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
var
|
|
1
|
+
var C = Object.defineProperty, T = Object.defineProperties;
|
|
2
2
|
var M = Object.getOwnPropertyDescriptors;
|
|
3
3
|
var E = Object.getOwnPropertySymbols;
|
|
4
|
-
var
|
|
5
|
-
var b = (l, e, t) => e in l ?
|
|
4
|
+
var R = Object.prototype.hasOwnProperty, O = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var b = (l, e, t) => e in l ? C(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t, f = (l, e) => {
|
|
6
6
|
for (var t in e || (e = {}))
|
|
7
|
-
|
|
7
|
+
R.call(e, t) && b(l, t, e[t]);
|
|
8
8
|
if (E)
|
|
9
9
|
for (var t of E(e))
|
|
10
|
-
|
|
10
|
+
O.call(e, t) && b(l, t, e[t]);
|
|
11
11
|
return l;
|
|
12
12
|
}, I = (l, e) => T(l, M(e));
|
|
13
13
|
var p = (l, e, t) => (b(l, typeof e != "symbol" ? e + "" : e, t), t);
|
|
14
|
-
var
|
|
15
|
-
var
|
|
14
|
+
var c = (l, e, t) => new Promise((s, o) => {
|
|
15
|
+
var n = (i) => {
|
|
16
16
|
try {
|
|
17
17
|
r(t.next(i));
|
|
18
|
-
} catch (
|
|
19
|
-
o(
|
|
18
|
+
} catch (h) {
|
|
19
|
+
o(h);
|
|
20
20
|
}
|
|
21
21
|
}, d = (i) => {
|
|
22
22
|
try {
|
|
23
23
|
r(t.throw(i));
|
|
24
|
-
} catch (
|
|
25
|
-
o(
|
|
24
|
+
} catch (h) {
|
|
25
|
+
o(h);
|
|
26
26
|
}
|
|
27
|
-
}, r = (i) => i.done ? s(i.value) : Promise.resolve(i.value).then(
|
|
27
|
+
}, r = (i) => i.done ? s(i.value) : Promise.resolve(i.value).then(n, d);
|
|
28
28
|
r((t = t.apply(l, e)).next());
|
|
29
29
|
});
|
|
30
30
|
import { v4 as x } from "uuid";
|
|
31
|
-
import { parseJWT as z,
|
|
32
|
-
import { ApplicationTypes as
|
|
33
|
-
import
|
|
34
|
-
import { configuration as
|
|
35
|
-
import
|
|
36
|
-
import { getCVForceService as
|
|
37
|
-
import
|
|
38
|
-
const
|
|
39
|
-
var
|
|
40
|
-
class
|
|
31
|
+
import { parseJWT as z, resolveRelatedQuestionContent as P, removeNewlines as F, removeLeadingAndTrailingBackslashes as N } from "./utils.mjs";
|
|
32
|
+
import { ApplicationTypes as Q, PromptStatus as q, PromptVariableType as L, ChatMessageRole as A } from "@glodon-aiot/apis";
|
|
33
|
+
import U from "./Logger.mjs";
|
|
34
|
+
import { configuration as S } from "./Config.mjs";
|
|
35
|
+
import B from "./EventBus.mjs";
|
|
36
|
+
import { getCVForceService as H } from "./api/index.mjs";
|
|
37
|
+
import K from "dayjs";
|
|
38
|
+
const V = 100, u = new U("[Session] ");
|
|
39
|
+
var Y = /* @__PURE__ */ ((l) => (l.DataRequestFailed = "data request error", l.SocketConnectionFailed = "socket error", l.IdUndefined = "session id is not defined", l.SendingEmptyContent = "failed at sending empty content", l.Frozen = "session is frozen", l.UserModeError = "failed at setting socket user mode", l.Inactive = "session is inactive", l))(Y || {});
|
|
40
|
+
class te {
|
|
41
41
|
/**
|
|
42
42
|
* Session 类的构造函数,用于初始化会话实例。
|
|
43
43
|
* @param token - 用户的身份验证令牌,用于解析应用 ID。
|
|
@@ -47,14 +47,14 @@ class ee {
|
|
|
47
47
|
* @param _application - 应用信息,可选参数。
|
|
48
48
|
* @param client - Bot 客户端实例,可选参数。
|
|
49
49
|
*/
|
|
50
|
-
constructor(e, t = "", s, o,
|
|
50
|
+
constructor(e, t = "", s, o, n, d) {
|
|
51
51
|
p(this, "_id");
|
|
52
52
|
p(this, "_frozen", !1);
|
|
53
53
|
//是否可发送新问题
|
|
54
54
|
p(this, "inactivating", !1);
|
|
55
55
|
p(this, "_ready", !1);
|
|
56
56
|
// ready = false;
|
|
57
|
-
p(this, "event", new
|
|
57
|
+
p(this, "event", new B());
|
|
58
58
|
//从token解析出来的用户信息
|
|
59
59
|
p(this, "_applicationId");
|
|
60
60
|
p(this, "_application", null);
|
|
@@ -72,19 +72,19 @@ class ee {
|
|
|
72
72
|
p(this, "_dataLoading", !1);
|
|
73
73
|
p(this, "_dataLoaded", !1);
|
|
74
74
|
p(this, "_mcpServers", null);
|
|
75
|
-
this.token = e, this.lazyload = o, this.client = d || null, this._applicationId = z(e).rid ||
|
|
75
|
+
this.token = e, this.lazyload = o, this.client = d || null, this._applicationId = z(e).rid || S.applicationId, this.cvforceApi = this.getService(), n && (this._application = n), this.event.emit("init", {
|
|
76
76
|
applicationId: this._applicationId,
|
|
77
77
|
sessionId: t
|
|
78
78
|
}), this.loadApplicationInfo().then(() => {
|
|
79
79
|
var r;
|
|
80
80
|
if (t)
|
|
81
|
-
this._id = t, this.event.emit("data:loading"), s ? (this.data = s, o || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((i) =>
|
|
81
|
+
this._id = t, this.event.emit("data:loading"), s ? (this.data = s, o || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((i) => c(this, null, function* () {
|
|
82
82
|
this.data = i, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
83
|
-
})).then(() =>
|
|
83
|
+
})).then(() => c(this, null, function* () {
|
|
84
84
|
this.messages = [], yield this.fetchMessages(), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data);
|
|
85
|
-
})))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((i) =>
|
|
85
|
+
})))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((i) => c(this, null, function* () {
|
|
86
86
|
this.data = i, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
87
|
-
})).then(() =>
|
|
87
|
+
})).then(() => c(this, null, function* () {
|
|
88
88
|
this.messages = [], yield this.fetchMessages(), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), this._dataLoading = !1, this._dataLoaded = !0;
|
|
89
89
|
})));
|
|
90
90
|
else {
|
|
@@ -96,15 +96,15 @@ class ee {
|
|
|
96
96
|
name: i,
|
|
97
97
|
// @ts-ignore
|
|
98
98
|
netOpen: s == null ? void 0 : s.netOpen,
|
|
99
|
-
knowledgeIdList: (r = s == null ? void 0 : s.knowledges) == null ? void 0 : r.map((
|
|
99
|
+
knowledgeIdList: (r = s == null ? void 0 : s.knowledges) == null ? void 0 : r.map((h) => h.id),
|
|
100
100
|
applicationId: this._applicationId
|
|
101
|
-
}).then((
|
|
101
|
+
}).then((h) => c(this, null, function* () {
|
|
102
102
|
var m;
|
|
103
|
-
this._id =
|
|
103
|
+
this._id = h.id, this.data = I(f({}, h), {
|
|
104
104
|
knowledges: s == null ? void 0 : s.knowledges
|
|
105
105
|
}), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id);
|
|
106
106
|
try {
|
|
107
|
-
yield (() =>
|
|
107
|
+
yield (() => c(this, null, function* () {
|
|
108
108
|
if (s != null && s.knowledges)
|
|
109
109
|
return this.setKnowledges((s == null ? void 0 : s.knowledges) || void 0);
|
|
110
110
|
}))();
|
|
@@ -114,8 +114,8 @@ class ee {
|
|
|
114
114
|
yield (() => {
|
|
115
115
|
if (s != null && s.promptVariables) {
|
|
116
116
|
const y = this._promptVariables.map((v) => {
|
|
117
|
-
var _,
|
|
118
|
-
const w = ((
|
|
117
|
+
var _, a;
|
|
118
|
+
const w = ((a = (_ = s.promptVariables) == null ? void 0 : _.find((g) => g.key === v.key && g.type === v.type)) == null ? void 0 : a.value) || "";
|
|
119
119
|
return I(f({}, v), {
|
|
120
120
|
value: w
|
|
121
121
|
});
|
|
@@ -123,16 +123,16 @@ class ee {
|
|
|
123
123
|
return this.setPromptVariables(y || []);
|
|
124
124
|
} else
|
|
125
125
|
return;
|
|
126
|
-
})(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((m = this.application) == null ? void 0 : m.type) !==
|
|
126
|
+
})(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((m = this.application) == null ? void 0 : m.type) !== Q.Agent && (this._ready = !0, this.event.emit("ready"));
|
|
127
127
|
}));
|
|
128
128
|
}
|
|
129
129
|
});
|
|
130
130
|
}
|
|
131
131
|
fetchData() {
|
|
132
|
-
return
|
|
132
|
+
return c(this, null, function* () {
|
|
133
133
|
if (!this.data)
|
|
134
134
|
throw Error("session data is not loaded");
|
|
135
|
-
return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) =>
|
|
135
|
+
return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) => c(this, null, function* () {
|
|
136
136
|
return this.data = e, this.event.emit("data:updated", this.data), e;
|
|
137
137
|
}));
|
|
138
138
|
});
|
|
@@ -172,17 +172,17 @@ class ee {
|
|
|
172
172
|
return this.data.knowledges || void 0;
|
|
173
173
|
}
|
|
174
174
|
setKnowledges(e) {
|
|
175
|
-
return
|
|
175
|
+
return c(this, null, function* () {
|
|
176
176
|
if (!this.data)
|
|
177
177
|
throw Error("session data is not loaded");
|
|
178
178
|
if ((e || []).some((o) => Array.isArray(o.fileIds) && o.fileIds.length === 0))
|
|
179
179
|
throw Error("fileIds cannot be an empty array");
|
|
180
|
-
if ((e || []).reduce((o,
|
|
180
|
+
if ((e || []).reduce((o, n) => {
|
|
181
181
|
var r, i;
|
|
182
|
-
const d = (i = (r =
|
|
182
|
+
const d = (i = (r = n.fileIds) == null ? void 0 : r.length) != null ? i : 0;
|
|
183
183
|
return o + (d > 0 ? d : 1);
|
|
184
|
-
}, 0) >
|
|
185
|
-
throw Error(`knowledges and fileIds total count cannot exceed ${
|
|
184
|
+
}, 0) > V)
|
|
185
|
+
throw Error(`knowledges and fileIds total count cannot exceed ${V}`);
|
|
186
186
|
return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
|
|
187
187
|
knowledges: e
|
|
188
188
|
}), this.data.knowledges = e, this.event.emit("data:updated", this.data), this.event.emit("data:updated:knowledges", {
|
|
@@ -195,7 +195,7 @@ class ee {
|
|
|
195
195
|
throw Error("session id is not defined");
|
|
196
196
|
if (!this.data)
|
|
197
197
|
throw Error("session data is not loaded");
|
|
198
|
-
const e = this.promptVariables.filter((t) => t.status ===
|
|
198
|
+
const e = this.promptVariables.filter((t) => t.status === q.Quoted).filter((t) => !t.value);
|
|
199
199
|
if (e.length)
|
|
200
200
|
throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((t) => t.key).join(", ")}`);
|
|
201
201
|
}
|
|
@@ -203,24 +203,24 @@ class ee {
|
|
|
203
203
|
return this._promptVariables.filter((e) => !(e.type === L.User && e.key === "query") && !(e.type === L.System && e.key === "datetime"));
|
|
204
204
|
}
|
|
205
205
|
setPromptVariables(e) {
|
|
206
|
-
return
|
|
207
|
-
const t = e == null ? void 0 : e.map((
|
|
206
|
+
return c(this, null, function* () {
|
|
207
|
+
const t = e == null ? void 0 : e.map((n) => n.type ? n : I(f({}, n), {
|
|
208
208
|
type: "instruction"
|
|
209
|
-
})), s = this._promptVariables.map((
|
|
209
|
+
})), s = this._promptVariables.map((n) => {
|
|
210
210
|
var r;
|
|
211
|
-
const d = ((r = t == null ? void 0 : t.find((i) => i.key ===
|
|
212
|
-
return I(f({},
|
|
211
|
+
const d = ((r = t == null ? void 0 : t.find((i) => i.key === n.key && i.type === n.type)) == null ? void 0 : r.value) || n.value;
|
|
212
|
+
return I(f({}, n), {
|
|
213
213
|
value: d
|
|
214
214
|
});
|
|
215
|
-
}).sort((
|
|
216
|
-
if (!(yield Promise.all(s.filter((
|
|
215
|
+
}).sort((n, d) => n.key.localeCompare(d.key));
|
|
216
|
+
if (!(yield Promise.all(s.filter((n) => !(n.type === L.User && n.key === "query") && !(n.type === L.System && n.key === "datetime")).map((n) => {
|
|
217
217
|
if (!this.data)
|
|
218
218
|
throw Error("session data is not loaded");
|
|
219
|
-
if (!(!
|
|
220
|
-
return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id,
|
|
221
|
-
value:
|
|
219
|
+
if (!(!n.id || !n.key))
|
|
220
|
+
return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id, n.id, {
|
|
221
|
+
value: n.value
|
|
222
222
|
});
|
|
223
|
-
})).then(() => !0).catch((
|
|
223
|
+
})).then(() => !0).catch((n) => !1)))
|
|
224
224
|
return !1;
|
|
225
225
|
if (!this.data)
|
|
226
226
|
throw Error("session data is not loaded");
|
|
@@ -240,9 +240,9 @@ class ee {
|
|
|
240
240
|
* @returns 如果设置成功,返回最终的网络访问开关状态;若不满足设置条件,则提前返回 undefined
|
|
241
241
|
*/
|
|
242
242
|
setNetOpen(e) {
|
|
243
|
-
return
|
|
243
|
+
return c(this, null, function* () {
|
|
244
244
|
var t, s, o;
|
|
245
|
-
if (u.log("setNetOpen:", e), u.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((s = this._application) == null ? void 0 : s.type) ===
|
|
245
|
+
if (u.log("setNetOpen:", e), u.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((s = this._application) == null ? void 0 : s.type) === Q.Agent) {
|
|
246
246
|
console.warn("Agent Application Sessions are disabled to setNetOpen.");
|
|
247
247
|
return;
|
|
248
248
|
}
|
|
@@ -261,7 +261,7 @@ class ee {
|
|
|
261
261
|
});
|
|
262
262
|
}
|
|
263
263
|
getService() {
|
|
264
|
-
return
|
|
264
|
+
return H(S.apiRoot, this.token, (e, t) => {
|
|
265
265
|
const s = new Error("data request error", {
|
|
266
266
|
cause: {
|
|
267
267
|
status: e,
|
|
@@ -289,17 +289,17 @@ class ee {
|
|
|
289
289
|
* @returns Promise<false|Message[]>
|
|
290
290
|
*/
|
|
291
291
|
fetchMessages() {
|
|
292
|
-
return
|
|
292
|
+
return c(this, arguments, function* (e = {}) {
|
|
293
293
|
var s, o;
|
|
294
294
|
const t = {
|
|
295
295
|
pageSize: this._loadingMessagesSize
|
|
296
296
|
};
|
|
297
297
|
if (this._id) {
|
|
298
298
|
e = f(f({}, t), e), this.event.emit("history:loading"), u.log("history:loading"), (e.endTime || e.startTime) && (this.event.emit("history:moreloading", e), u.log("history:moreloading", e)), this._loadingMessages = !0;
|
|
299
|
-
const
|
|
299
|
+
const n = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), d = n.map((i, h) => {
|
|
300
300
|
var v, w;
|
|
301
301
|
let m = [], y;
|
|
302
|
-
return typeof ((v = i.agentHistoryList) == null ? void 0 : v.agentList) == "string" && (m = JSON.parse(i.agentHistoryList.agentList).reduce((
|
|
302
|
+
return typeof ((v = i.agentHistoryList) == null ? void 0 : v.agentList) == "string" && (m = JSON.parse(i.agentHistoryList.agentList).reduce((a, g) => (a[g.data.step - 1] || (a[g.data.step - 1] = []), a[g.data.step - 1].push(g), a), [])), h === n.length - 1 && (y = (w = this.data) == null ? void 0 : w.lastRelatedQuestion), I(f({}, i), {
|
|
303
303
|
actions: m,
|
|
304
304
|
relatedQuesion: y,
|
|
305
305
|
fromHistory: !0
|
|
@@ -309,31 +309,31 @@ class ee {
|
|
|
309
309
|
const i = this.messages[this.messages.length - 1];
|
|
310
310
|
i && !i.relatedQuesion && (i.relatedQuesion = (s = this.data) == null ? void 0 : s.lastRelatedQuestion), this.messages = [...d, ...this.messages];
|
|
311
311
|
} else if (e.endTime && !e.startTime) {
|
|
312
|
-
const i = this.messages.findIndex((
|
|
312
|
+
const i = this.messages.findIndex((h) => h.ts === e.endTime);
|
|
313
313
|
this.messages.splice(0, i + 1), this.messages = [...d, ...this.messages];
|
|
314
314
|
} else if (!e.endTime && e.startTime) {
|
|
315
|
-
const i = this.messages.findLastIndex((
|
|
315
|
+
const i = this.messages.findLastIndex((h) => h.ts === e.endTime);
|
|
316
316
|
this.messages.splice(i, this.messages.length - i - 1), this.messages = [...this.messages, ...d];
|
|
317
317
|
} else {
|
|
318
|
-
const i = this.messages.findIndex((m) => m.ts === e.endTime),
|
|
319
|
-
this.messages.splice(i,
|
|
318
|
+
const i = this.messages.findIndex((m) => m.ts === e.endTime), h = this.messages.findLastIndex((m) => m.ts === e.endTime);
|
|
319
|
+
this.messages.splice(i, h - i + 1, ...d);
|
|
320
320
|
}
|
|
321
321
|
const r = this.messages[this.messages.length - 1];
|
|
322
322
|
return r && !r.relatedQuesion && (r.relatedQuesion = (o = this.data) == null ? void 0 : o.lastRelatedQuestion), this.event.emit("history:moreloaded", d), u.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, d;
|
|
323
323
|
} else {
|
|
324
|
-
const
|
|
324
|
+
const n = new Error(
|
|
325
325
|
"session id is not defined"
|
|
326
326
|
/* IdUndefined */
|
|
327
327
|
// {
|
|
328
328
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
329
329
|
// }
|
|
330
330
|
);
|
|
331
|
-
return u.error(
|
|
331
|
+
return u.error(n), this.event.emit("error", n), !1;
|
|
332
332
|
}
|
|
333
333
|
});
|
|
334
334
|
}
|
|
335
335
|
rename(e) {
|
|
336
|
-
return
|
|
336
|
+
return c(this, null, function* () {
|
|
337
337
|
if (!this._id)
|
|
338
338
|
throw Error("session is not created yet");
|
|
339
339
|
if (e.trim() === "")
|
|
@@ -346,28 +346,28 @@ class ee {
|
|
|
346
346
|
});
|
|
347
347
|
}
|
|
348
348
|
pushMessage(e) {
|
|
349
|
-
return
|
|
349
|
+
return c(this, null, function* () {
|
|
350
350
|
this.messages.push(e), u.log("history:add", e), this.event.emit("history:add", e), this.event.emit("message:new", e), this.event.emit("message:updated", e);
|
|
351
351
|
});
|
|
352
352
|
}
|
|
353
353
|
reloadSessionInfo() {
|
|
354
354
|
if (!this._id)
|
|
355
355
|
throw Error("session is not created yet");
|
|
356
|
-
return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) =>
|
|
356
|
+
return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => c(this, null, function* () {
|
|
357
357
|
if (this.data = e, !this._id)
|
|
358
358
|
throw Error("session is not created yet");
|
|
359
359
|
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), u.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.data;
|
|
360
|
-
})).then(() =>
|
|
360
|
+
})).then(() => c(this, null, function* () {
|
|
361
361
|
return this.messages = [], yield this.fetchMessages(), this._dataLoading = !1, this._dataLoaded = !0, this.event.emit("data:loaded", this.data), this;
|
|
362
362
|
}));
|
|
363
363
|
}
|
|
364
364
|
reload() {
|
|
365
|
-
return
|
|
365
|
+
return c(this, null, function* () {
|
|
366
366
|
return yield this.reloadSessionInfo(), this.socket ? (this.inactivate(!0), this.activate()) : (this.inactivate(!0), this.activate());
|
|
367
367
|
});
|
|
368
368
|
}
|
|
369
369
|
delete() {
|
|
370
|
-
return
|
|
370
|
+
return c(this, null, function* () {
|
|
371
371
|
if (!this.id) {
|
|
372
372
|
const e = new Error(
|
|
373
373
|
"session id is not defined"
|
|
@@ -382,7 +382,7 @@ class ee {
|
|
|
382
382
|
});
|
|
383
383
|
}
|
|
384
384
|
activate() {
|
|
385
|
-
return
|
|
385
|
+
return c(this, null, function* () {
|
|
386
386
|
if (this.activing)
|
|
387
387
|
return new Promise((t) => {
|
|
388
388
|
this.addEventListener("activated", () => {
|
|
@@ -393,7 +393,7 @@ class ee {
|
|
|
393
393
|
throw Error("session is not avalible. id is undefined");
|
|
394
394
|
this.activing = !0;
|
|
395
395
|
const e = this._id;
|
|
396
|
-
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((t) =>
|
|
396
|
+
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((t) => c(this, null, function* () {
|
|
397
397
|
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e), t;
|
|
398
398
|
})), this.messages.length || (this.messages = [], yield this.fetchMessages()), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), this._dataLoaded = !0, this._dataLoading = !1, this.inactivating = !1, this.frozen = !0, yield this.prepareConnection(), this.event.emit("activated", this.socket), this.active = !0, this.activing = !1, this;
|
|
399
399
|
});
|
|
@@ -410,48 +410,48 @@ class ee {
|
|
|
410
410
|
return this._loadingMessages;
|
|
411
411
|
}
|
|
412
412
|
send(s) {
|
|
413
|
-
return
|
|
414
|
-
stream:
|
|
413
|
+
return c(this, arguments, function* (e, t = {
|
|
414
|
+
stream: S.stream
|
|
415
415
|
}) {
|
|
416
416
|
var _;
|
|
417
417
|
try {
|
|
418
418
|
this.validateConfigs();
|
|
419
|
-
} catch (
|
|
420
|
-
console.error(
|
|
419
|
+
} catch (a) {
|
|
420
|
+
console.error(a);
|
|
421
421
|
}
|
|
422
422
|
const {
|
|
423
|
-
stream: o =
|
|
424
|
-
referenceFirst:
|
|
425
|
-
} = t, d = e.uuid || x(), r = ((_ = e.text) == null ? void 0 : _.trim()) || "", i = e.files || [],
|
|
426
|
-
if (!r && !i.length && !(
|
|
427
|
-
const
|
|
423
|
+
stream: o = S.stream,
|
|
424
|
+
referenceFirst: n = !1
|
|
425
|
+
} = t, d = e.uuid || x(), r = ((_ = e.text) == null ? void 0 : _.trim()) || "", i = e.files || [], h = e.fileIds, m = e.imageContent;
|
|
426
|
+
if (!r && !i.length && !(h != null && h.length)) {
|
|
427
|
+
const a = new Error(
|
|
428
428
|
"failed at sending empty content"
|
|
429
429
|
/* SendingEmptyContent */
|
|
430
430
|
// {
|
|
431
431
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.SendingEmptyContent],
|
|
432
432
|
// }
|
|
433
433
|
);
|
|
434
|
-
return u.error(
|
|
434
|
+
return u.error(a), this.event.emit("error", a), !1;
|
|
435
435
|
}
|
|
436
436
|
if (!this._id) {
|
|
437
|
-
const
|
|
437
|
+
const a = new Error(
|
|
438
438
|
"session id is not defined"
|
|
439
439
|
/* IdUndefined */
|
|
440
440
|
// {
|
|
441
441
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
442
442
|
// }
|
|
443
443
|
);
|
|
444
|
-
return u.error(
|
|
444
|
+
return u.error(a), this.event.emit("error", a), !1;
|
|
445
445
|
}
|
|
446
446
|
if (this.frozen) {
|
|
447
|
-
const
|
|
447
|
+
const a = new Error(
|
|
448
448
|
"session is frozen"
|
|
449
449
|
/* Frozen */
|
|
450
450
|
// {
|
|
451
451
|
// cause: { code: -1, message: 'session is frozen.' },
|
|
452
452
|
// }
|
|
453
453
|
);
|
|
454
|
-
return u.error(
|
|
454
|
+
return u.error(a), this.event.emit("error", a), !1;
|
|
455
455
|
}
|
|
456
456
|
this.frozen = !0, this._stopAnswer = !1;
|
|
457
457
|
const y = {
|
|
@@ -468,79 +468,90 @@ class ee {
|
|
|
468
468
|
content: r,
|
|
469
469
|
stream: o,
|
|
470
470
|
requestId: d,
|
|
471
|
-
fileIds:
|
|
472
|
-
referenceFirst:
|
|
471
|
+
fileIds: h,
|
|
472
|
+
referenceFirst: n,
|
|
473
473
|
imageContent: m
|
|
474
474
|
};
|
|
475
475
|
this.pushMessage(y), this.event.emit("question:content", r);
|
|
476
|
-
const w = (
|
|
477
|
-
if (
|
|
476
|
+
const w = (a) => c(this, null, function* () {
|
|
477
|
+
if (a.role !== A.Robot)
|
|
478
478
|
return;
|
|
479
|
-
switch (this.removeEventListener("answer:finish", w), this._id &&
|
|
480
|
-
finialMessage:
|
|
479
|
+
switch (this.removeEventListener("answer:finish", w), this._id && a.content && a.index && a.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
480
|
+
finialMessage: a.content
|
|
481
481
|
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
482
|
-
finialMessage:
|
|
483
|
-
})), yield this.fillMessageReference(
|
|
482
|
+
finialMessage: a.content
|
|
483
|
+
})), yield this.fillMessageReference(a), a.code) {
|
|
484
484
|
case 11915:
|
|
485
|
-
|
|
485
|
+
a.content = "请您换个问题问我吧~", a.index = -1, a.isSensitive = !0, a.isLoading = !1, a.sessionId = this._id || "", this.event.emit("message:error", a);
|
|
486
486
|
break;
|
|
487
487
|
case 11916:
|
|
488
|
-
|
|
488
|
+
a.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", a.index = -1, a.isSensitive = !0, a.isLoading = !1, a.sessionId = this._id || "", this.event.emit("message:error", a);
|
|
489
489
|
break;
|
|
490
490
|
case 200:
|
|
491
|
-
|
|
491
|
+
a.index = -1, a.isLoading = !1;
|
|
492
492
|
break;
|
|
493
493
|
default:
|
|
494
|
-
|
|
494
|
+
a.content = a.message || "未知错误", a.isLoading = !1, a.index = -1, this.event.emit("message:error", a);
|
|
495
495
|
}
|
|
496
|
-
this.getRelatedQuesion(
|
|
496
|
+
this.getRelatedQuesion(a), a.isLoading = !1;
|
|
497
497
|
let g = this.messages[this.messages.length - 1];
|
|
498
|
-
if (g && g.role ===
|
|
499
|
-
const k = f(f({}, g),
|
|
498
|
+
if (g && g.role === A.Robot) {
|
|
499
|
+
const k = f(f({}, g), a);
|
|
500
500
|
this.messages[this.messages.length - 1] = k, this.event.emit("message:updated", k);
|
|
501
501
|
} else
|
|
502
|
-
this.pushMessage(
|
|
502
|
+
this.pushMessage(a);
|
|
503
503
|
this.frozen = !1;
|
|
504
504
|
});
|
|
505
505
|
return this.addEventListener("answer:finish", w), this.sendQuestion(v, I(f({}, t), {
|
|
506
506
|
stream: o,
|
|
507
|
-
referenceFirst:
|
|
507
|
+
referenceFirst: n
|
|
508
508
|
})), y;
|
|
509
509
|
});
|
|
510
510
|
}
|
|
511
|
+
currentUserQuestion() {
|
|
512
|
+
for (let e = this.messages.length - 1; e >= 0; e -= 1) {
|
|
513
|
+
const t = this.messages[e];
|
|
514
|
+
if (t.role === A.User)
|
|
515
|
+
return t.content || "";
|
|
516
|
+
}
|
|
517
|
+
return "";
|
|
518
|
+
}
|
|
511
519
|
getRelatedQuesion(e) {
|
|
512
|
-
return
|
|
513
|
-
var
|
|
514
|
-
if (!e.content || e.isSensitive || e.code !== 200
|
|
520
|
+
return c(this, null, function* () {
|
|
521
|
+
var r, i;
|
|
522
|
+
if (!e.content || e.isSensitive || e.code !== 200)
|
|
523
|
+
return e;
|
|
524
|
+
const t = (r = this.application) == null ? void 0 : r.arrange[0], s = P(t == null ? void 0 : t.recommendQuestionOpen, t == null ? void 0 : t.relatedQuestionApp, e.content, this.currentUserQuestion());
|
|
525
|
+
if (s === null)
|
|
515
526
|
return e;
|
|
516
527
|
this.event.emit("message:relatedQuesion:loading", e);
|
|
517
|
-
const
|
|
518
|
-
content:
|
|
519
|
-
}).catch((
|
|
520
|
-
if (!
|
|
521
|
-
const
|
|
522
|
-
relatedQuesion:
|
|
528
|
+
const o = yield this.cvforceApi.postGeneques({
|
|
529
|
+
content: s
|
|
530
|
+
}).catch((h) => null);
|
|
531
|
+
if (!o) {
|
|
532
|
+
const h = [], m = I(f({}, e), {
|
|
533
|
+
relatedQuesion: h,
|
|
523
534
|
index: -3,
|
|
524
535
|
isLoading: !1
|
|
525
536
|
});
|
|
526
537
|
return this.event.emit("message:relatedQuesion:loaded", {
|
|
527
|
-
relatedQuesion:
|
|
528
|
-
}), this.event.emit("message:updated",
|
|
538
|
+
relatedQuesion: h
|
|
539
|
+
}), this.event.emit("message:updated", m), m;
|
|
529
540
|
}
|
|
530
|
-
const
|
|
531
|
-
e.relatedQuesion =
|
|
532
|
-
const
|
|
533
|
-
relatedQuesion:
|
|
541
|
+
const n = (i = o.content) == null ? void 0 : i.split("*").map(F).map(N).filter((h) => !!h);
|
|
542
|
+
e.relatedQuesion = n, e.index = -3, e.isLoading = !1;
|
|
543
|
+
const d = I(f({}, e), {
|
|
544
|
+
relatedQuesion: n,
|
|
534
545
|
index: -3,
|
|
535
546
|
isLoading: !1
|
|
536
547
|
});
|
|
537
|
-
return this.event.emit("message:relatedQuesion:loaded",
|
|
538
|
-
lastRelatedQuestion:
|
|
539
|
-
}),
|
|
548
|
+
return this.event.emit("message:relatedQuesion:loaded", d), this.event.emit("message:updated", d), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
549
|
+
lastRelatedQuestion: n
|
|
550
|
+
}), d;
|
|
540
551
|
});
|
|
541
552
|
}
|
|
542
553
|
fillMessageReference(e) {
|
|
543
|
-
return
|
|
554
|
+
return c(this, null, function* () {
|
|
544
555
|
try {
|
|
545
556
|
const t = yield Promise.all(e.reference.map((s) => this.cvforceApi.getKnowledgeIdFileId(s.knowledgeId, s.docId)));
|
|
546
557
|
e.reference = e.reference.map((s, o) => f(f({}, s), t[o]));
|
|
@@ -551,26 +562,26 @@ class ee {
|
|
|
551
562
|
});
|
|
552
563
|
}
|
|
553
564
|
answerAgain() {
|
|
554
|
-
return
|
|
555
|
-
stream:
|
|
565
|
+
return c(this, arguments, function* (e = {
|
|
566
|
+
stream: S.stream
|
|
556
567
|
}) {
|
|
557
568
|
const t = this.messages[this.messages.length - 1];
|
|
558
569
|
if (!t)
|
|
559
570
|
throw Error("message list is empty");
|
|
560
571
|
this.frozen = !0, this._stopAnswer = !1;
|
|
561
572
|
const {
|
|
562
|
-
stream: s =
|
|
573
|
+
stream: s = S.stream
|
|
563
574
|
} = e;
|
|
564
575
|
let o;
|
|
565
|
-
t.role !==
|
|
566
|
-
const
|
|
567
|
-
content:
|
|
568
|
-
fileIds:
|
|
569
|
-
requestId:
|
|
576
|
+
t.role !== A.User && (o = e.oldMessageId || t.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", t), this.event.emit("history:remove", t));
|
|
577
|
+
const n = this.messages[this.messages.length - 1], d = {
|
|
578
|
+
content: n.content,
|
|
579
|
+
fileIds: n.fileIds,
|
|
580
|
+
requestId: n.messageId,
|
|
570
581
|
stream: s,
|
|
571
582
|
oldMessageId: o
|
|
572
|
-
}, r = (i) =>
|
|
573
|
-
if (i.role ===
|
|
583
|
+
}, r = (i) => c(this, null, function* () {
|
|
584
|
+
if (i.role === A.Robot && i.index === -1) {
|
|
574
585
|
switch (this._id && i.content && i.index && i.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
575
586
|
finialMessage: i.content
|
|
576
587
|
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
@@ -597,7 +608,7 @@ class ee {
|
|
|
597
608
|
});
|
|
598
609
|
}
|
|
599
610
|
setTop(e) {
|
|
600
|
-
return
|
|
611
|
+
return c(this, null, function* () {
|
|
601
612
|
if (!this._id)
|
|
602
613
|
throw Error("session is not avalible. id is undefined");
|
|
603
614
|
this.data = yield this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
@@ -624,11 +635,11 @@ class ee {
|
|
|
624
635
|
// 标记消息已停止加载
|
|
625
636
|
index: -2,
|
|
626
637
|
// 消息索引,用于标识特殊状态
|
|
627
|
-
messageTime:
|
|
638
|
+
messageTime: K(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:ss:mm"),
|
|
628
639
|
// 消息时间
|
|
629
640
|
content: "",
|
|
630
641
|
// 消息内容
|
|
631
|
-
role:
|
|
642
|
+
role: A.System,
|
|
632
643
|
// 消息角色为系统消息
|
|
633
644
|
sessionId: this.id || "",
|
|
634
645
|
// 会话 ID
|
|
@@ -643,21 +654,21 @@ class ee {
|
|
|
643
654
|
if (e.actions) {
|
|
644
655
|
const o = e.actions[((s = e.actions) == null ? void 0 : s.length) - 1];
|
|
645
656
|
if (o) {
|
|
646
|
-
const
|
|
647
|
-
if (
|
|
657
|
+
const n = o[o.length - 1];
|
|
658
|
+
if (n && (n.type === "BEFORE_CALL" || n.type === "FUNCTION_CALL")) {
|
|
648
659
|
const {
|
|
649
660
|
pluginName: d,
|
|
650
661
|
pluginCode: r,
|
|
651
662
|
funcCode: i,
|
|
652
|
-
funcName:
|
|
653
|
-
} =
|
|
663
|
+
funcName: h
|
|
664
|
+
} = n.data;
|
|
654
665
|
o.push({
|
|
655
666
|
data: {
|
|
656
667
|
pluginName: d,
|
|
657
668
|
pluginCode: r,
|
|
658
669
|
funcCode: i,
|
|
659
|
-
funcName:
|
|
660
|
-
step:
|
|
670
|
+
funcName: h,
|
|
671
|
+
step: n.data.step + 1
|
|
661
672
|
},
|
|
662
673
|
type: "ABORT"
|
|
663
674
|
}), this.event.emit("message:action", {
|
|
@@ -683,7 +694,7 @@ class ee {
|
|
|
683
694
|
return;
|
|
684
695
|
const s = e.data.type, o = e.data.payload;
|
|
685
696
|
t.isReplying = !0;
|
|
686
|
-
const
|
|
697
|
+
const n = () => {
|
|
687
698
|
if (!t.searchingStatus) {
|
|
688
699
|
t.searchingStatus = "start";
|
|
689
700
|
return;
|
|
@@ -693,7 +704,7 @@ class ee {
|
|
|
693
704
|
return;
|
|
694
705
|
}
|
|
695
706
|
};
|
|
696
|
-
if (s === "error" && (t.index = -1), s === "beforeSearch" &&
|
|
707
|
+
if (s === "error" && (t.index = -1), s === "beforeSearch" && n(), s === "searchOver" && n(), s === "search" && t.searchReference.push(o), s === "thinking") {
|
|
697
708
|
t.thinkingStatus = "start";
|
|
698
709
|
const i = o.choices[0];
|
|
699
710
|
t.reasoningContent = (t.reasoningContent || "") + ((d = i.delta.reasoningContent) != null ? d : "");
|
|
@@ -708,8 +719,8 @@ class ee {
|
|
|
708
719
|
t.content = (t.content || "") + i.delta.content;
|
|
709
720
|
}
|
|
710
721
|
if (s === "reference" && (t.reference = o), s === "updateSession" && o && this.data && (this.data = f(f({}, this.data), o), this.event.emit("data:updated", this.data)), s === "result") {
|
|
711
|
-
const r = o.messageId, i = o.llmModel,
|
|
712
|
-
t.messageId = r, t.llmModel = i, t.messageTime =
|
|
722
|
+
const r = o.messageId, i = o.llmModel, h = o.messageTime;
|
|
723
|
+
t.messageId = r, t.llmModel = i, t.messageTime = h;
|
|
713
724
|
}
|
|
714
725
|
s === "over" ? t.index = -1 : (t.code = e.code, t.message = e.message);
|
|
715
726
|
}
|
|
@@ -719,7 +730,7 @@ class ee {
|
|
|
719
730
|
* @returns 返回调用 API 提交评论后的响应结果
|
|
720
731
|
*/
|
|
721
732
|
postMessageComment(e) {
|
|
722
|
-
return
|
|
733
|
+
return c(this, null, function* () {
|
|
723
734
|
if (!this._applicationId || !this._id)
|
|
724
735
|
throw Error("session is not avalible. id is undefined");
|
|
725
736
|
return this.cvforceApi.postComments(this._applicationId, this._id, e).then((t) => {
|
|
@@ -751,7 +762,7 @@ class ee {
|
|
|
751
762
|
}
|
|
752
763
|
}
|
|
753
764
|
export {
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
765
|
+
V as MAX_KNOWLEDGES_AND_FILES_COUNT,
|
|
766
|
+
Y as SessionErrors,
|
|
767
|
+
te as default
|
|
757
768
|
};
|