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