@glodon-aiot/bot-client-sdk 3.3.2-beta.0 → 3.3.2-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/AgentSession.mjs +116 -95
- package/dist/es/Config.mjs +8 -7
- package/dist/es/DialogSession.mjs +137 -167
- package/dist/es/Session.mjs +211 -171
- package/dist/es/package.json.mjs +2 -2
- package/dist/lib/index.js +4 -4
- package/dist/src/Config.d.ts +2 -0
- package/dist/src/Session.d.ts +6 -0
- package/package.json +2 -2
package/dist/es/Session.mjs
CHANGED
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
1
|
+
var V = Object.defineProperty, x = Object.defineProperties;
|
|
2
|
+
var Q = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var k = Object.getOwnPropertySymbols;
|
|
4
|
+
var M = Object.prototype.hasOwnProperty, T = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var S = (c, e, t) => e in c ? V(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t, f = (c, e) => {
|
|
6
6
|
for (var t in e || (e = {}))
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var t of
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
}, I = (
|
|
13
|
-
var
|
|
14
|
-
var
|
|
7
|
+
M.call(e, t) && S(c, t, e[t]);
|
|
8
|
+
if (k)
|
|
9
|
+
for (var t of k(e))
|
|
10
|
+
T.call(e, t) && S(c, t, e[t]);
|
|
11
|
+
return c;
|
|
12
|
+
}, I = (c, e) => x(c, Q(e));
|
|
13
|
+
var l = (c, e, t) => (S(c, typeof e != "symbol" ? e + "" : e, t), t);
|
|
14
|
+
var d = (c, e, t) => new Promise((i, a) => {
|
|
15
15
|
var o = (s) => {
|
|
16
16
|
try {
|
|
17
|
-
|
|
17
|
+
r(t.next(s));
|
|
18
18
|
} catch (p) {
|
|
19
|
-
|
|
19
|
+
a(p);
|
|
20
20
|
}
|
|
21
|
-
},
|
|
21
|
+
}, h = (s) => {
|
|
22
22
|
try {
|
|
23
|
-
|
|
23
|
+
r(t.throw(s));
|
|
24
24
|
} catch (p) {
|
|
25
|
-
|
|
25
|
+
a(p);
|
|
26
26
|
}
|
|
27
|
-
},
|
|
28
|
-
|
|
27
|
+
}, r = (s) => s.done ? i(s.value) : Promise.resolve(s.value).then(o, h);
|
|
28
|
+
r((t = t.apply(c, e)).next());
|
|
29
29
|
});
|
|
30
|
-
import { v4 as
|
|
31
|
-
import { parseJWT as
|
|
32
|
-
import { ApplicationTypes as
|
|
30
|
+
import { v4 as b } from "uuid";
|
|
31
|
+
import { parseJWT as R, removeNewlines as C, removeLeadingAndTrailingBackslashes as O } from "./utils.mjs";
|
|
32
|
+
import { ApplicationTypes as E, PromptStatus as z, ChatMessageRole as L } from "@glodon-aiot/apis";
|
|
33
33
|
import P from "./Logger.mjs";
|
|
34
|
-
import { configuration as
|
|
34
|
+
import { configuration as A } from "./Config.mjs";
|
|
35
35
|
import q from "./EventBus.mjs";
|
|
36
36
|
import { getCVForceService as F } from "./api/index.mjs";
|
|
37
37
|
import N from "dayjs";
|
|
38
38
|
const u = new P("[Session] ");
|
|
39
|
-
var B = /* @__PURE__ */ ((
|
|
39
|
+
var B = /* @__PURE__ */ ((c) => (c.DataRequestFailed = "data request error", c.SocketConnectionFailed = "socket error", c.IdUndefined = "session id is not defined", c.SendingEmptyContent = "failed at sending empty content", c.Frozen = "session is frozen", c.UserModeError = "failed at setting socket user mode", c.Inactive = "session is inactive", c))(B || {});
|
|
40
40
|
class $ {
|
|
41
41
|
/**
|
|
42
42
|
* Session 类的构造函数,用于初始化会话实例。
|
|
@@ -47,44 +47,44 @@ class $ {
|
|
|
47
47
|
* @param _application - 应用信息,可选参数。
|
|
48
48
|
* @param client - Bot 客户端实例,可选参数。
|
|
49
49
|
*/
|
|
50
|
-
constructor(e, t = "", i,
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
constructor(e, t = "", i, a, o, h) {
|
|
51
|
+
l(this, "_id");
|
|
52
|
+
l(this, "_frozen", !1);
|
|
53
53
|
//是否可发送新问题
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
l(this, "inactivating", !1);
|
|
55
|
+
l(this, "_ready", !1);
|
|
56
56
|
// ready = false;
|
|
57
|
-
|
|
57
|
+
l(this, "event", new q());
|
|
58
58
|
//从token解析出来的用户信息
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
this.token = e, this.lazyload =
|
|
59
|
+
l(this, "_applicationId");
|
|
60
|
+
l(this, "_application", null);
|
|
61
|
+
l(this, "data", null);
|
|
62
|
+
l(this, "client", null);
|
|
63
|
+
l(this, "messages", []);
|
|
64
|
+
l(this, "_loadingMessages", !1);
|
|
65
|
+
l(this, "_loadingMessagesSize", 10);
|
|
66
|
+
l(this, "cvforceApi");
|
|
67
|
+
l(this, "socket", null);
|
|
68
|
+
l(this, "_promptVariables", []);
|
|
69
|
+
l(this, "active", !1);
|
|
70
|
+
l(this, "activing", !1);
|
|
71
|
+
l(this, "_stopAnswer", !1);
|
|
72
|
+
l(this, "_dataLoading", !1);
|
|
73
|
+
l(this, "_dataLoaded", !1);
|
|
74
|
+
l(this, "_mcpServers", null);
|
|
75
|
+
this.token = e, this.lazyload = a, this.client = h || null, this._applicationId = R(e).rid || A.applicationId, this.cvforceApi = this.getService(), o && (this._application = o), this.event.emit("init", {
|
|
76
76
|
applicationId: this._applicationId,
|
|
77
77
|
sessionId: t
|
|
78
78
|
}), this.loadApplicationInfo().then(() => {
|
|
79
|
-
var
|
|
79
|
+
var r;
|
|
80
80
|
if (t)
|
|
81
|
-
this._id = t, this.event.emit("data:loading"), i ? (this.data = i,
|
|
81
|
+
this._id = t, this.event.emit("data:loading"), i ? (this.data = i, a || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((s) => d(this, null, function* () {
|
|
82
82
|
this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
83
|
-
})).then(() =>
|
|
83
|
+
})).then(() => d(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((s) =>
|
|
85
|
+
})))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((s) => d(this, null, function* () {
|
|
86
86
|
this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
87
|
-
})).then(() =>
|
|
87
|
+
})).then(() => d(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,37 +96,37 @@ class $ {
|
|
|
96
96
|
name: s,
|
|
97
97
|
// @ts-ignore
|
|
98
98
|
netOpen: i == null ? void 0 : i.netOpen,
|
|
99
|
-
knowledgeIdList: (
|
|
99
|
+
knowledgeIdList: (r = i == null ? void 0 : i.knowledges) == null ? void 0 : r.map((p) => p.id),
|
|
100
100
|
applicationId: this._applicationId
|
|
101
|
-
}).then((p) =>
|
|
102
|
-
var
|
|
101
|
+
}).then((p) => d(this, null, function* () {
|
|
102
|
+
var m;
|
|
103
103
|
this._id = p.id, this.data = I(f({}, p), {
|
|
104
104
|
knowledges: i == null ? void 0 : i.knowledges
|
|
105
|
-
}), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), yield (() =>
|
|
105
|
+
}), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), yield (() => d(this, null, function* () {
|
|
106
106
|
if (i != null && i.knowledges)
|
|
107
107
|
return this.setKnowledges((i == null ? void 0 : i.knowledges) || void 0);
|
|
108
108
|
}))(), yield (() => {
|
|
109
109
|
if (i != null && i.promptVariables) {
|
|
110
|
-
const w = this._promptVariables.map((
|
|
110
|
+
const w = this._promptVariables.map((g) => {
|
|
111
111
|
var y, v;
|
|
112
|
-
const
|
|
113
|
-
return I(f({},
|
|
114
|
-
value:
|
|
112
|
+
const n = ((v = (y = i.promptVariables) == null ? void 0 : y.find((_) => _.key === g.key)) == null ? void 0 : v.value) || "";
|
|
113
|
+
return I(f({}, g), {
|
|
114
|
+
value: n
|
|
115
115
|
});
|
|
116
|
-
}).sort((
|
|
116
|
+
}).sort((g, n) => g.key.localeCompare(n.key));
|
|
117
117
|
return this.setPromptVariables(w || []);
|
|
118
118
|
} else
|
|
119
119
|
return;
|
|
120
|
-
})(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((
|
|
120
|
+
})(), 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) !== E.Agent && (this._ready = !0, this.event.emit("ready"));
|
|
121
121
|
}));
|
|
122
122
|
}
|
|
123
123
|
});
|
|
124
124
|
}
|
|
125
125
|
fetchData() {
|
|
126
|
-
return
|
|
126
|
+
return d(this, null, function* () {
|
|
127
127
|
if (!this.data)
|
|
128
128
|
throw Error("session data is not loaded");
|
|
129
|
-
return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) =>
|
|
129
|
+
return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) => d(this, null, function* () {
|
|
130
130
|
return this.data = e, this.event.emit("data:updated", this.data), e;
|
|
131
131
|
}));
|
|
132
132
|
});
|
|
@@ -166,7 +166,7 @@ class $ {
|
|
|
166
166
|
return this.data.knowledges || void 0;
|
|
167
167
|
}
|
|
168
168
|
setKnowledges(e) {
|
|
169
|
-
return
|
|
169
|
+
return d(this, null, function* () {
|
|
170
170
|
if (!this.data)
|
|
171
171
|
throw Error("session data is not loaded");
|
|
172
172
|
return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
|
|
@@ -181,7 +181,7 @@ class $ {
|
|
|
181
181
|
throw Error("session id is not defined");
|
|
182
182
|
if (!this.data)
|
|
183
183
|
throw Error("session data is not loaded");
|
|
184
|
-
const e = this.promptVariables.filter((t) => t.status ===
|
|
184
|
+
const e = this.promptVariables.filter((t) => t.status === z.Quoted).filter((t) => !t.value);
|
|
185
185
|
if (e.length)
|
|
186
186
|
throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((t) => t.key).join(", ")}`);
|
|
187
187
|
}
|
|
@@ -189,14 +189,14 @@ class $ {
|
|
|
189
189
|
return this._promptVariables.filter((e) => e.key !== "query");
|
|
190
190
|
}
|
|
191
191
|
setPromptVariables(e) {
|
|
192
|
-
return
|
|
192
|
+
return d(this, null, function* () {
|
|
193
193
|
const t = this._promptVariables.map((i) => {
|
|
194
194
|
var o;
|
|
195
|
-
const
|
|
195
|
+
const a = ((o = e == null ? void 0 : e.find((h) => h.key === i.key)) == null ? void 0 : o.value) || i.value;
|
|
196
196
|
return I(f({}, i), {
|
|
197
|
-
value:
|
|
197
|
+
value: a
|
|
198
198
|
});
|
|
199
|
-
}).sort((i,
|
|
199
|
+
}).sort((i, a) => i.key.localeCompare(a.key));
|
|
200
200
|
if (yield Promise.all(t.filter((i) => i.key !== "query").map((i) => {
|
|
201
201
|
if (!this.data)
|
|
202
202
|
throw Error("session data is not loaded");
|
|
@@ -222,13 +222,13 @@ class $ {
|
|
|
222
222
|
* @returns 如果设置成功,返回最终的网络访问开关状态;若不满足设置条件,则提前返回 undefined
|
|
223
223
|
*/
|
|
224
224
|
setNetOpen(e) {
|
|
225
|
-
return
|
|
226
|
-
var t, i,
|
|
227
|
-
if (u.log("setNetOpen:", e), u.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((i = this._application) == null ? void 0 : i.type) ===
|
|
225
|
+
return d(this, null, function* () {
|
|
226
|
+
var t, i, a;
|
|
227
|
+
if (u.log("setNetOpen:", e), u.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((i = this._application) == null ? void 0 : i.type) === E.Agent) {
|
|
228
228
|
console.warn("Agent Application Sessions are disabled to setNetOpen.");
|
|
229
229
|
return;
|
|
230
230
|
}
|
|
231
|
-
if (((
|
|
231
|
+
if (((a = this._application) == null ? void 0 : a.arrange[0].enableNetOpen) === !1) {
|
|
232
232
|
console.warn("This Application is disabled to change netOpen.");
|
|
233
233
|
return;
|
|
234
234
|
}
|
|
@@ -243,7 +243,7 @@ class $ {
|
|
|
243
243
|
});
|
|
244
244
|
}
|
|
245
245
|
getService() {
|
|
246
|
-
return F(
|
|
246
|
+
return F(A.apiRoot, this.token, (e, t) => {
|
|
247
247
|
const i = new Error(
|
|
248
248
|
"data request error"
|
|
249
249
|
/* DataRequestFailed */
|
|
@@ -272,36 +272,36 @@ class $ {
|
|
|
272
272
|
* @returns Promise<false|Message[]>
|
|
273
273
|
*/
|
|
274
274
|
fetchMessages() {
|
|
275
|
-
return
|
|
276
|
-
var i,
|
|
275
|
+
return d(this, arguments, function* (e = {}) {
|
|
276
|
+
var i, a;
|
|
277
277
|
const t = {
|
|
278
278
|
pageSize: this._loadingMessagesSize
|
|
279
279
|
};
|
|
280
280
|
if (this._id) {
|
|
281
281
|
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;
|
|
282
|
-
const o = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e),
|
|
283
|
-
var
|
|
284
|
-
let
|
|
285
|
-
return typeof ((
|
|
286
|
-
actions:
|
|
282
|
+
const o = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), h = o.map((s, p) => {
|
|
283
|
+
var g, n;
|
|
284
|
+
let m = [], w;
|
|
285
|
+
return typeof ((g = s.agentHistoryList) == null ? void 0 : g.agentList) == "string" && (m = JSON.parse(s.agentHistoryList.agentList).reduce((v, _) => (v[_.data.step - 1] || (v[_.data.step - 1] = []), v[_.data.step - 1].push(_), v), [])), p === o.length - 1 && (w = (n = this.data) == null ? void 0 : n.lastRelatedQuestion), I(f({}, s), {
|
|
286
|
+
actions: m,
|
|
287
287
|
relatedQuesion: w
|
|
288
288
|
});
|
|
289
289
|
});
|
|
290
290
|
if (!e.endTime && !e.startTime) {
|
|
291
291
|
const s = this.messages[this.messages.length - 1];
|
|
292
|
-
s && !s.relatedQuesion && (s.relatedQuesion = (i = this.data) == null ? void 0 : i.lastRelatedQuestion), this.messages = [...
|
|
292
|
+
s && !s.relatedQuesion && (s.relatedQuesion = (i = this.data) == null ? void 0 : i.lastRelatedQuestion), this.messages = [...h, ...this.messages];
|
|
293
293
|
} else if (e.endTime && !e.startTime) {
|
|
294
294
|
const s = this.messages.findIndex((p) => p.ts === e.endTime);
|
|
295
|
-
this.messages.splice(0, s + 1), this.messages = [...
|
|
295
|
+
this.messages.splice(0, s + 1), this.messages = [...h, ...this.messages];
|
|
296
296
|
} else if (!e.endTime && e.startTime) {
|
|
297
297
|
const s = this.messages.findLastIndex((p) => p.ts === e.endTime);
|
|
298
|
-
this.messages.splice(s, this.messages.length - s - 1), this.messages = [...this.messages, ...
|
|
298
|
+
this.messages.splice(s, this.messages.length - s - 1), this.messages = [...this.messages, ...h];
|
|
299
299
|
} else {
|
|
300
|
-
const s = this.messages.findIndex((
|
|
301
|
-
this.messages.splice(s, p - s + 1, ...
|
|
300
|
+
const s = this.messages.findIndex((m) => m.ts === e.endTime), p = this.messages.findLastIndex((m) => m.ts === e.endTime);
|
|
301
|
+
this.messages.splice(s, p - s + 1, ...h);
|
|
302
302
|
}
|
|
303
|
-
const
|
|
304
|
-
return
|
|
303
|
+
const r = this.messages[this.messages.length - 1];
|
|
304
|
+
return r && !r.relatedQuesion && (r.relatedQuesion = (a = this.data) == null ? void 0 : a.lastRelatedQuestion), this.event.emit("history:moreloaded", h), u.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, h;
|
|
305
305
|
} else {
|
|
306
306
|
const o = new Error(
|
|
307
307
|
"session id is not defined"
|
|
@@ -315,7 +315,7 @@ class $ {
|
|
|
315
315
|
});
|
|
316
316
|
}
|
|
317
317
|
rename(e) {
|
|
318
|
-
return
|
|
318
|
+
return d(this, null, function* () {
|
|
319
319
|
if (!this._id)
|
|
320
320
|
throw Error("session is not created yet");
|
|
321
321
|
if (e.trim() === "")
|
|
@@ -328,28 +328,28 @@ class $ {
|
|
|
328
328
|
});
|
|
329
329
|
}
|
|
330
330
|
pushMessage(e) {
|
|
331
|
-
return
|
|
331
|
+
return d(this, null, function* () {
|
|
332
332
|
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);
|
|
333
333
|
});
|
|
334
334
|
}
|
|
335
335
|
reloadSessionInfo() {
|
|
336
336
|
if (!this._id)
|
|
337
337
|
throw Error("session is not created yet");
|
|
338
|
-
return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) =>
|
|
338
|
+
return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => d(this, null, function* () {
|
|
339
339
|
if (this.data = e, !this._id)
|
|
340
340
|
throw Error("session is not created yet");
|
|
341
341
|
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;
|
|
342
|
-
})).then(() =>
|
|
342
|
+
})).then(() => d(this, null, function* () {
|
|
343
343
|
return this.messages = [], yield this.fetchMessages(), this._dataLoading = !1, this._dataLoaded = !0, this.event.emit("data:loaded", this.data), this;
|
|
344
344
|
}));
|
|
345
345
|
}
|
|
346
346
|
reload() {
|
|
347
|
-
return
|
|
347
|
+
return d(this, null, function* () {
|
|
348
348
|
return yield this.reloadSessionInfo(), this.socket ? (this.inactivate(!0), this.activate()) : (this.inactivate(!0), this.activate());
|
|
349
349
|
});
|
|
350
350
|
}
|
|
351
351
|
delete() {
|
|
352
|
-
return
|
|
352
|
+
return d(this, null, function* () {
|
|
353
353
|
if (!this.id) {
|
|
354
354
|
const e = new Error(
|
|
355
355
|
"session id is not defined"
|
|
@@ -364,7 +364,7 @@ class $ {
|
|
|
364
364
|
});
|
|
365
365
|
}
|
|
366
366
|
activate() {
|
|
367
|
-
return
|
|
367
|
+
return d(this, null, function* () {
|
|
368
368
|
if (this.activing)
|
|
369
369
|
return new Promise((t) => {
|
|
370
370
|
this.addEventListener("activated", () => {
|
|
@@ -375,7 +375,7 @@ class $ {
|
|
|
375
375
|
throw Error("session is not avalible. id is undefined");
|
|
376
376
|
this.activing = !0;
|
|
377
377
|
const e = this._id;
|
|
378
|
-
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((t) =>
|
|
378
|
+
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((t) => d(this, null, function* () {
|
|
379
379
|
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e), t;
|
|
380
380
|
})), 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;
|
|
381
381
|
});
|
|
@@ -392,157 +392,157 @@ class $ {
|
|
|
392
392
|
return this._loadingMessages;
|
|
393
393
|
}
|
|
394
394
|
send(i) {
|
|
395
|
-
return
|
|
396
|
-
stream:
|
|
395
|
+
return d(this, arguments, function* (e, t = {
|
|
396
|
+
stream: A.stream
|
|
397
397
|
}) {
|
|
398
|
-
var
|
|
398
|
+
var g;
|
|
399
399
|
try {
|
|
400
400
|
this.validateConfigs();
|
|
401
|
-
} catch (
|
|
402
|
-
console.error(
|
|
401
|
+
} catch (n) {
|
|
402
|
+
console.error(n);
|
|
403
403
|
}
|
|
404
404
|
const {
|
|
405
|
-
stream:
|
|
405
|
+
stream: a = A.stream,
|
|
406
406
|
referenceFirst: o = !1
|
|
407
|
-
} = t,
|
|
408
|
-
if (!
|
|
407
|
+
} = t, h = e.uuid || b(), r = ((g = e.text) == null ? void 0 : g.trim()) || "", s = e.fileIds;
|
|
408
|
+
if (!r && /*!files.length &&*/
|
|
409
409
|
!(s != null && s.length)) {
|
|
410
|
-
const
|
|
410
|
+
const n = new Error(
|
|
411
411
|
"failed at sending empty content"
|
|
412
412
|
/* SendingEmptyContent */
|
|
413
413
|
// {
|
|
414
414
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.SendingEmptyContent],
|
|
415
415
|
// }
|
|
416
416
|
);
|
|
417
|
-
return u.error(
|
|
417
|
+
return u.error(n), this.event.emit("error", n), !1;
|
|
418
418
|
}
|
|
419
419
|
if (!this._id) {
|
|
420
|
-
const
|
|
420
|
+
const n = new Error(
|
|
421
421
|
"session id is not defined"
|
|
422
422
|
/* IdUndefined */
|
|
423
423
|
// {
|
|
424
424
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
425
425
|
// }
|
|
426
426
|
);
|
|
427
|
-
return u.error(
|
|
427
|
+
return u.error(n), this.event.emit("error", n), !1;
|
|
428
428
|
}
|
|
429
429
|
if (this.frozen) {
|
|
430
|
-
const
|
|
430
|
+
const n = new Error(
|
|
431
431
|
"session is frozen"
|
|
432
432
|
/* Frozen */
|
|
433
433
|
// {
|
|
434
434
|
// cause: { code: -1, message: 'session is frozen.' },
|
|
435
435
|
// }
|
|
436
436
|
);
|
|
437
|
-
return u.error(
|
|
437
|
+
return u.error(n), this.event.emit("error", n), !1;
|
|
438
438
|
}
|
|
439
439
|
this.frozen = !0, this._stopAnswer = !1;
|
|
440
440
|
const p = {
|
|
441
441
|
sessionId: this.id,
|
|
442
442
|
role: 2,
|
|
443
|
-
content:
|
|
444
|
-
messageId:
|
|
443
|
+
content: r,
|
|
444
|
+
messageId: h,
|
|
445
445
|
reference: [],
|
|
446
446
|
searchReference: [],
|
|
447
447
|
messageTime: ""
|
|
448
|
-
},
|
|
449
|
-
content:
|
|
450
|
-
stream:
|
|
451
|
-
requestId:
|
|
448
|
+
}, m = {
|
|
449
|
+
content: r,
|
|
450
|
+
stream: a,
|
|
451
|
+
requestId: h,
|
|
452
452
|
fileIds: s,
|
|
453
453
|
referenceFirst: o
|
|
454
454
|
};
|
|
455
|
-
this.pushMessage(p), this.event.emit("question:content",
|
|
456
|
-
const w = (
|
|
457
|
-
if (
|
|
455
|
+
this.pushMessage(p), this.event.emit("question:content", r);
|
|
456
|
+
const w = (n) => d(this, null, function* () {
|
|
457
|
+
if (n.role !== L.Robot)
|
|
458
458
|
return;
|
|
459
|
-
switch (this.removeEventListener("answer:finish", w), this._id &&
|
|
460
|
-
finialMessage:
|
|
459
|
+
switch (this.removeEventListener("answer:finish", w), this._id && n.content && n.index && n.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
460
|
+
finialMessage: n.content
|
|
461
461
|
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
462
|
-
finialMessage:
|
|
463
|
-
})), yield this.fillMessageReference(
|
|
462
|
+
finialMessage: n.content
|
|
463
|
+
})), yield this.fillMessageReference(n), n.code) {
|
|
464
464
|
case 11915:
|
|
465
|
-
|
|
465
|
+
n.content = "请您换个问题问我吧~", n.index = -1, n.isSensitive = !0, n.isLoading = !1, n.sessionId = this._id || "", this.event.emit("message:error", n);
|
|
466
466
|
break;
|
|
467
467
|
case 200:
|
|
468
|
-
|
|
468
|
+
n.index = -1, n.isLoading = !1;
|
|
469
469
|
break;
|
|
470
470
|
default:
|
|
471
|
-
|
|
471
|
+
n.content = n.message || "未知错误", n.isLoading = !1, n.index = -1, this.event.emit("message:error", n);
|
|
472
472
|
}
|
|
473
|
-
this.getRelatedQuesion(
|
|
473
|
+
this.getRelatedQuesion(n), n.isLoading = !1;
|
|
474
474
|
let y = this.messages[this.messages.length - 1];
|
|
475
|
-
if (y && y.role ===
|
|
476
|
-
const v = f(f({}, y),
|
|
475
|
+
if (y && y.role === L.Robot) {
|
|
476
|
+
const v = f(f({}, y), n);
|
|
477
477
|
this.messages[this.messages.length - 1] = v, this.event.emit("message:updated", v);
|
|
478
478
|
} else
|
|
479
|
-
this.pushMessage(
|
|
479
|
+
this.pushMessage(n);
|
|
480
480
|
this.frozen = !1;
|
|
481
481
|
});
|
|
482
|
-
return this.addEventListener("answer:finish", w), this.sendQuestion(
|
|
483
|
-
stream:
|
|
482
|
+
return this.addEventListener("answer:finish", w), this.sendQuestion(m, I(f({}, t), {
|
|
483
|
+
stream: a,
|
|
484
484
|
referenceFirst: o
|
|
485
485
|
})), p;
|
|
486
486
|
});
|
|
487
487
|
}
|
|
488
488
|
getRelatedQuesion(e) {
|
|
489
|
-
return
|
|
490
|
-
var o,
|
|
489
|
+
return d(this, null, function* () {
|
|
490
|
+
var o, h;
|
|
491
491
|
if (!e.content || e.isSensitive || e.code !== 200 || !((o = this.application) != null && o.arrange[0].recommendQuestionOpen))
|
|
492
492
|
return e;
|
|
493
493
|
this.event.emit("message:relatedQuesion:loading", e);
|
|
494
494
|
const t = yield this.cvforceApi.postGeneques({
|
|
495
495
|
content: e.content
|
|
496
|
-
}).catch((
|
|
496
|
+
}).catch((r) => null);
|
|
497
497
|
if (!t) {
|
|
498
|
-
const
|
|
499
|
-
relatedQuesion:
|
|
498
|
+
const r = [], s = I(f({}, e), {
|
|
499
|
+
relatedQuesion: r,
|
|
500
500
|
index: -3,
|
|
501
501
|
isLoading: !1
|
|
502
502
|
});
|
|
503
503
|
return this.event.emit("message:relatedQuesion:loaded", {
|
|
504
|
-
relatedQuesion:
|
|
504
|
+
relatedQuesion: r
|
|
505
505
|
}), this.event.emit("message:updated", s), s;
|
|
506
506
|
}
|
|
507
|
-
const i = (
|
|
507
|
+
const i = (h = t.content) == null ? void 0 : h.split("*").map(C).map(O).filter((r) => !!r);
|
|
508
508
|
e.relatedQuesion = i, e.index = -3, e.isLoading = !1;
|
|
509
|
-
const
|
|
509
|
+
const a = I(f({}, e), {
|
|
510
510
|
relatedQuesion: i,
|
|
511
511
|
index: -3,
|
|
512
512
|
isLoading: !1
|
|
513
513
|
});
|
|
514
|
-
return this.event.emit("message:relatedQuesion:loaded",
|
|
514
|
+
return this.event.emit("message:relatedQuesion:loaded", a), this.event.emit("message:updated", a), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
515
515
|
lastRelatedQuestion: i
|
|
516
|
-
}),
|
|
516
|
+
}), a;
|
|
517
517
|
});
|
|
518
518
|
}
|
|
519
519
|
fillMessageReference(e) {
|
|
520
|
-
return
|
|
520
|
+
return d(this, null, function* () {
|
|
521
521
|
const t = yield Promise.all(e.reference.map((i) => this.cvforceApi.getKnowledgeIdFileId(i.knowledgeId, i.docId)));
|
|
522
|
-
return e.reference = e.reference.map((i,
|
|
522
|
+
return e.reference = e.reference.map((i, a) => f(f({}, i), t[a])), this.event.emit("message:updated", e), e;
|
|
523
523
|
});
|
|
524
524
|
}
|
|
525
525
|
answerAgain() {
|
|
526
|
-
return
|
|
527
|
-
stream:
|
|
526
|
+
return d(this, arguments, function* (e = {
|
|
527
|
+
stream: A.stream
|
|
528
528
|
}) {
|
|
529
529
|
const t = this.messages[this.messages.length - 1];
|
|
530
530
|
if (!t)
|
|
531
531
|
throw Error("message list is empty");
|
|
532
532
|
this.frozen = !0, this._stopAnswer = !1;
|
|
533
533
|
const {
|
|
534
|
-
stream: i =
|
|
534
|
+
stream: i = A.stream
|
|
535
535
|
} = e;
|
|
536
|
-
let
|
|
537
|
-
t.role !==
|
|
538
|
-
const o = this.messages[this.messages.length - 1],
|
|
536
|
+
let a;
|
|
537
|
+
t.role !== L.User && (a = e.oldMessageId || t.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", t), this.event.emit("history:remove", t));
|
|
538
|
+
const o = this.messages[this.messages.length - 1], h = {
|
|
539
539
|
content: o.content,
|
|
540
540
|
fileIds: o.fileIds,
|
|
541
541
|
requestId: o.messageId,
|
|
542
542
|
stream: i,
|
|
543
|
-
oldMessageId:
|
|
544
|
-
},
|
|
545
|
-
if (s.role ===
|
|
543
|
+
oldMessageId: a
|
|
544
|
+
}, r = (s) => d(this, null, function* () {
|
|
545
|
+
if (s.role === L.Robot && s.index === -1) {
|
|
546
546
|
switch (this._id && s.content && s.index && s.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
547
547
|
finialMessage: s.content
|
|
548
548
|
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
@@ -557,16 +557,16 @@ class $ {
|
|
|
557
557
|
default:
|
|
558
558
|
s.content = s.message || "未知错误", s.isLoading = !1, s.index = -1, this.event.emit("message:error", s);
|
|
559
559
|
}
|
|
560
|
-
this.getRelatedQuesion(s), this.removeEventListener("message:content",
|
|
560
|
+
this.getRelatedQuesion(s), this.removeEventListener("message:content", r), this.frozen = !1;
|
|
561
561
|
}
|
|
562
562
|
});
|
|
563
|
-
this.addEventListener("message:content",
|
|
563
|
+
this.addEventListener("message:content", r), this.sendQuestion(h, I(f({}, e), {
|
|
564
564
|
stream: i
|
|
565
565
|
}));
|
|
566
566
|
});
|
|
567
567
|
}
|
|
568
568
|
setTop(e) {
|
|
569
|
-
return
|
|
569
|
+
return d(this, null, function* () {
|
|
570
570
|
if (!this._id)
|
|
571
571
|
throw Error("session is not avalible. id is undefined");
|
|
572
572
|
this.data = yield this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
@@ -587,7 +587,7 @@ class $ {
|
|
|
587
587
|
this.stopAnswer(), this.frozen = !1, this._stopAnswer = !0;
|
|
588
588
|
let e = this.messages[this.messages.length - 1];
|
|
589
589
|
const t = {
|
|
590
|
-
messageId:
|
|
590
|
+
messageId: b(),
|
|
591
591
|
// 生成唯一的消息 ID
|
|
592
592
|
isLoading: !1,
|
|
593
593
|
// 标记消息已停止加载
|
|
@@ -597,7 +597,7 @@ class $ {
|
|
|
597
597
|
// 消息时间
|
|
598
598
|
content: "",
|
|
599
599
|
// 消息内容
|
|
600
|
-
role:
|
|
600
|
+
role: L.System,
|
|
601
601
|
// 消息角色为系统消息
|
|
602
602
|
sessionId: this.id || "",
|
|
603
603
|
// 会话 ID
|
|
@@ -610,20 +610,20 @@ class $ {
|
|
|
610
610
|
if (e.index === -2)
|
|
611
611
|
return;
|
|
612
612
|
if (e.actions) {
|
|
613
|
-
const
|
|
614
|
-
if (
|
|
615
|
-
const o =
|
|
613
|
+
const a = e.actions[((i = e.actions) == null ? void 0 : i.length) - 1];
|
|
614
|
+
if (a) {
|
|
615
|
+
const o = a[a.length - 1];
|
|
616
616
|
if (o && (o.type === "BEFORE_CALL" || o.type === "FUNCTION_CALL")) {
|
|
617
617
|
const {
|
|
618
|
-
pluginName:
|
|
619
|
-
pluginCode:
|
|
618
|
+
pluginName: h,
|
|
619
|
+
pluginCode: r,
|
|
620
620
|
funcCode: s,
|
|
621
621
|
funcName: p
|
|
622
622
|
} = o.data;
|
|
623
|
-
|
|
623
|
+
a.push({
|
|
624
624
|
data: {
|
|
625
|
-
pluginName:
|
|
626
|
-
pluginCode:
|
|
625
|
+
pluginName: h,
|
|
626
|
+
pluginCode: r,
|
|
627
627
|
funcCode: s,
|
|
628
628
|
funcName: p,
|
|
629
629
|
step: o.data.step + 1
|
|
@@ -631,7 +631,7 @@ class $ {
|
|
|
631
631
|
type: "ABORT"
|
|
632
632
|
}), this.event.emit("message:action", {
|
|
633
633
|
messageId: e.messageId,
|
|
634
|
-
action:
|
|
634
|
+
action: a
|
|
635
635
|
});
|
|
636
636
|
}
|
|
637
637
|
}
|
|
@@ -641,17 +641,57 @@ class $ {
|
|
|
641
641
|
this.pushMessage(t), this.event.emit("message:content", t);
|
|
642
642
|
return this;
|
|
643
643
|
}
|
|
644
|
+
/**
|
|
645
|
+
* 处理流式回答事件,更新目标消息的状态和内容。
|
|
646
|
+
* @param event - 包含流式回答数据的事件对象,通常为服务器发送的事件。
|
|
647
|
+
* @param targetMessage - 要更新的目标聊天消息对象。
|
|
648
|
+
*/
|
|
649
|
+
handleStreamAnswer(e, t) {
|
|
650
|
+
if (!e.data)
|
|
651
|
+
return;
|
|
652
|
+
const i = e.data.type, a = e.data.payload;
|
|
653
|
+
t.isReplying = !0;
|
|
654
|
+
const o = () => {
|
|
655
|
+
if (!t.searchingStatus) {
|
|
656
|
+
t.searchingStatus = "start";
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
if (t.searchingStatus === "start") {
|
|
660
|
+
t.searchingStatus = "end";
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
if (i === "beforeSearch" && o(), i === "searchOver" && o(), i === "search" && t.searchReference.push(a), i === "thinking") {
|
|
665
|
+
t.thinkingStatus = "start";
|
|
666
|
+
const r = a.choices[0];
|
|
667
|
+
t.reasoningContent = t.reasoningContent + r.delta.reasoningContent;
|
|
668
|
+
}
|
|
669
|
+
if (i === "thinkingOver") {
|
|
670
|
+
t.thinkingStatus = "end";
|
|
671
|
+
const h = a.thinkingSecs;
|
|
672
|
+
t.thinkingSecs = h;
|
|
673
|
+
}
|
|
674
|
+
if (i === "text") {
|
|
675
|
+
const r = a.choices[0];
|
|
676
|
+
t.content = t.content + r.delta.content;
|
|
677
|
+
}
|
|
678
|
+
if (i === "reference" && (t.reference = a), i === "result") {
|
|
679
|
+
const h = a.messageId, r = a.llmModel, s = a.messageTime;
|
|
680
|
+
t.messageId = h, t.llmModel = r, t.messageTime = s;
|
|
681
|
+
}
|
|
682
|
+
i === "over" ? t.index = -1 : (t.code = e.code, t.message = e.message);
|
|
683
|
+
}
|
|
644
684
|
/**
|
|
645
685
|
* 异步提交评论信息
|
|
646
686
|
* @param params - 评论参数,包含评论相关的信息,如消息 ID 等
|
|
647
687
|
* @returns 返回调用 API 提交评论后的响应结果
|
|
648
688
|
*/
|
|
649
689
|
postMessageComment(e) {
|
|
650
|
-
return
|
|
690
|
+
return d(this, null, function* () {
|
|
651
691
|
if (!this._applicationId || !this._id)
|
|
652
692
|
throw Error("session is not avalible. id is undefined");
|
|
653
693
|
return this.cvforceApi.postComments(this._applicationId, this._id, e).then((t) => {
|
|
654
|
-
const i = this.messages.find((
|
|
694
|
+
const i = this.messages.find((a) => a.messageId === e.messageId);
|
|
655
695
|
return i && (i.comment = e, this.event.emit("message:comment", i)), t;
|
|
656
696
|
});
|
|
657
697
|
});
|