@glodon-aiot/agent-cli-sdk 3.25.0-alpha.9 → 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/AgentSession.mjs +68 -62
- package/dist/es/BotClient.mjs +75 -75
- package/dist/es/Config.mjs +16 -13
- package/dist/es/Session.mjs +183 -172
- package/dist/es/utils.mjs +16 -9
- package/dist/lib/index.js +5 -4
- 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 x = Object.defineProperty,
|
|
2
|
-
var
|
|
1
|
+
var x = Object.defineProperty, Q = Object.defineProperties;
|
|
2
|
+
var V = Object.getOwnPropertyDescriptors;
|
|
3
3
|
var S = Object.getOwnPropertySymbols;
|
|
4
|
-
var
|
|
5
|
-
var k = (
|
|
6
|
-
for (var
|
|
7
|
-
|
|
4
|
+
var R = Object.prototype.hasOwnProperty, T = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var k = (l, e, t) => e in l ? x(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t, f = (l, e) => {
|
|
6
|
+
for (var t in e || (e = {}))
|
|
7
|
+
R.call(e, t) && k(l, t, e[t]);
|
|
8
8
|
if (S)
|
|
9
|
-
for (var
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
}, I = (
|
|
13
|
-
var
|
|
14
|
-
var r = (
|
|
9
|
+
for (var t of S(e))
|
|
10
|
+
T.call(e, t) && k(l, t, e[t]);
|
|
11
|
+
return l;
|
|
12
|
+
}, I = (l, e) => Q(l, V(e));
|
|
13
|
+
var p = (l, e, t) => (k(l, typeof e != "symbol" ? e + "" : e, t), t);
|
|
14
|
+
var r = (l, e, t) => new Promise((i, n) => {
|
|
15
15
|
var o = (s) => {
|
|
16
16
|
try {
|
|
17
|
-
|
|
18
|
-
} catch (
|
|
19
|
-
n(
|
|
17
|
+
c(t.next(s));
|
|
18
|
+
} catch (d) {
|
|
19
|
+
n(d);
|
|
20
20
|
}
|
|
21
|
-
},
|
|
21
|
+
}, h = (s) => {
|
|
22
22
|
try {
|
|
23
|
-
|
|
24
|
-
} catch (
|
|
25
|
-
n(
|
|
23
|
+
c(t.throw(s));
|
|
24
|
+
} catch (d) {
|
|
25
|
+
n(d);
|
|
26
26
|
}
|
|
27
|
-
},
|
|
28
|
-
|
|
27
|
+
}, c = (s) => s.done ? i(s.value) : Promise.resolve(s.value).then(o, h);
|
|
28
|
+
c((t = t.apply(l, e)).next());
|
|
29
29
|
});
|
|
30
30
|
import { v4 as M } from "uuid";
|
|
31
|
-
import { parseJWT as z, removeNewlines as O, removeLeadingAndTrailingBackslashes as
|
|
32
|
-
import { ApplicationTypes as E, PromptStatus as
|
|
33
|
-
import
|
|
34
|
-
import { configuration as
|
|
35
|
-
import
|
|
36
|
-
import { getCVForceService as
|
|
31
|
+
import { parseJWT as z, resolveRelatedQuestionContent as C, removeNewlines as O, removeLeadingAndTrailingBackslashes as P } from "./utils.mjs";
|
|
32
|
+
import { ApplicationTypes as E, PromptStatus as q, PromptVariableType as b, ChatMessageRole as A } from "@glodon-aiot/apis";
|
|
33
|
+
import F from "./Logger.mjs";
|
|
34
|
+
import { configuration as L } from "./Config.mjs";
|
|
35
|
+
import N from "./EventBus.mjs";
|
|
36
|
+
import { getCVForceService as U } from "./api/index.mjs";
|
|
37
37
|
import B from "dayjs";
|
|
38
|
-
const u = new
|
|
39
|
-
var
|
|
40
|
-
class
|
|
38
|
+
const u = new F("[Session] ");
|
|
39
|
+
var D = /* @__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))(D || {});
|
|
40
|
+
class Z {
|
|
41
41
|
/**
|
|
42
42
|
* Session 类的构造函数,用于初始化会话实例。
|
|
43
43
|
* @param token - 用户的身份验证令牌,用于解析应用 ID。
|
|
@@ -47,69 +47,69 @@ class X {
|
|
|
47
47
|
* @param _application - 应用信息,可选参数。
|
|
48
48
|
* @param client - Bot 客户端实例,可选参数。
|
|
49
49
|
*/
|
|
50
|
-
constructor(e,
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
constructor(e, t = "", i, n, o, h) {
|
|
51
|
+
p(this, "_id");
|
|
52
|
+
p(this, "_frozen", !1);
|
|
53
53
|
//是否可发送新问题
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
p(this, "inactivating", !1);
|
|
55
|
+
p(this, "_ready", !1);
|
|
56
56
|
// ready = false;
|
|
57
|
-
|
|
57
|
+
p(this, "event", new N());
|
|
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 = n, this.client =
|
|
59
|
+
p(this, "_applicationId");
|
|
60
|
+
p(this, "_application", null);
|
|
61
|
+
p(this, "data", null);
|
|
62
|
+
p(this, "client", null);
|
|
63
|
+
p(this, "messages", []);
|
|
64
|
+
p(this, "_loadingMessages", !1);
|
|
65
|
+
p(this, "_loadingMessagesSize", 10);
|
|
66
|
+
p(this, "cvforceApi");
|
|
67
|
+
p(this, "socket", null);
|
|
68
|
+
p(this, "_promptVariables", []);
|
|
69
|
+
p(this, "active", !1);
|
|
70
|
+
p(this, "activing", !1);
|
|
71
|
+
p(this, "_stopAnswer", !1);
|
|
72
|
+
p(this, "_dataLoading", !1);
|
|
73
|
+
p(this, "_dataLoaded", !1);
|
|
74
|
+
p(this, "_mcpServers", null);
|
|
75
|
+
this.token = e, this.lazyload = n, this.client = h || null, this._applicationId = z(e).rid || L.applicationId, this.cvforceApi = this.getService(), o && (this._application = o), this.event.emit("init", {
|
|
76
76
|
applicationId: this._applicationId,
|
|
77
|
-
sessionId:
|
|
77
|
+
sessionId: t
|
|
78
78
|
}), this.loadApplicationInfo().then(() => {
|
|
79
|
-
var
|
|
80
|
-
if (
|
|
81
|
-
this._id =
|
|
82
|
-
this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId,
|
|
79
|
+
var c;
|
|
80
|
+
if (t)
|
|
81
|
+
this._id = t, this.event.emit("data:loading"), i ? (this.data = i, n || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((s) => r(this, null, function* () {
|
|
82
|
+
this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
83
83
|
})).then(() => r(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,
|
|
86
|
-
this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId,
|
|
85
|
+
})))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((s) => r(this, null, function* () {
|
|
86
|
+
this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
|
|
87
87
|
})).then(() => r(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 {
|
|
91
|
-
const s = (
|
|
91
|
+
const s = (i == null ? void 0 : i.name) || "新建会话";
|
|
92
92
|
this.event.emit("data:creating", {
|
|
93
93
|
applicationId: this._applicationId,
|
|
94
94
|
name: s
|
|
95
95
|
}), this.event.emit("data:loading"), this.cvforceApi.postSession(this.applicationId, {
|
|
96
96
|
name: s,
|
|
97
97
|
// @ts-ignore
|
|
98
|
-
netOpen:
|
|
99
|
-
knowledgeIdList: (
|
|
98
|
+
netOpen: i == null ? void 0 : i.netOpen,
|
|
99
|
+
knowledgeIdList: (c = i == null ? void 0 : i.knowledges) == null ? void 0 : c.map((d) => d.id),
|
|
100
100
|
applicationId: this._applicationId
|
|
101
|
-
}).then((
|
|
101
|
+
}).then((d) => r(this, null, function* () {
|
|
102
102
|
var g;
|
|
103
|
-
this._id =
|
|
104
|
-
knowledges:
|
|
103
|
+
this._id = d.id, this.data = I(f({}, d), {
|
|
104
|
+
knowledges: i == null ? void 0 : i.knowledges
|
|
105
105
|
}), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), yield (() => r(this, null, function* () {
|
|
106
|
-
if (
|
|
107
|
-
return this.setKnowledges((
|
|
106
|
+
if (i != null && i.knowledges)
|
|
107
|
+
return this.setKnowledges((i == null ? void 0 : i.knowledges) || void 0);
|
|
108
108
|
}))(), yield (() => {
|
|
109
|
-
if (
|
|
109
|
+
if (i != null && i.promptVariables) {
|
|
110
110
|
const y = this._promptVariables.map((m) => {
|
|
111
111
|
var w, v;
|
|
112
|
-
const a = ((v = (w =
|
|
112
|
+
const a = ((v = (w = i.promptVariables) == null ? void 0 : w.find((_) => _.key === m.key && _.type === m.type)) == null ? void 0 : v.value) || "";
|
|
113
113
|
return I(f({}, m), {
|
|
114
114
|
value: a
|
|
115
115
|
});
|
|
@@ -181,28 +181,28 @@ class X {
|
|
|
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((
|
|
184
|
+
const e = this.promptVariables.filter((t) => t.status === q.Quoted).filter((t) => !t.value);
|
|
185
185
|
if (e.length)
|
|
186
|
-
throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((
|
|
186
|
+
throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((t) => t.key).join(", ")}`);
|
|
187
187
|
}
|
|
188
188
|
get promptVariables() {
|
|
189
189
|
return this._promptVariables.filter((e) => !(e.type === b.User && e.key === "query") && !(e.type === b.System && e.key === "datetime"));
|
|
190
190
|
}
|
|
191
191
|
setPromptVariables(e) {
|
|
192
192
|
return r(this, null, function* () {
|
|
193
|
-
const
|
|
193
|
+
const t = this._promptVariables.map((i) => {
|
|
194
194
|
var o;
|
|
195
|
-
const n = ((o = e == null ? void 0 : e.find((
|
|
196
|
-
return I(f({},
|
|
195
|
+
const n = ((o = e == null ? void 0 : e.find((h) => h.key === i.key && h.type === i.type)) == null ? void 0 : o.value) || i.value;
|
|
196
|
+
return I(f({}, i), {
|
|
197
197
|
value: n
|
|
198
198
|
});
|
|
199
|
-
}).sort((
|
|
200
|
-
if (yield Promise.all(
|
|
199
|
+
}).sort((i, n) => i.key.localeCompare(n.key));
|
|
200
|
+
if (yield Promise.all(t.filter((i) => !(i.type === b.User && i.key === "query") && !(i.type === b.System && i.key === "datetime")).map((i) => {
|
|
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:
|
|
203
|
+
if (!(!i.id || !i.key))
|
|
204
|
+
return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id, i.id, {
|
|
205
|
+
value: i.value
|
|
206
206
|
});
|
|
207
207
|
})), !this.data)
|
|
208
208
|
throw Error("session data is not loaded");
|
|
@@ -223,8 +223,8 @@ class X {
|
|
|
223
223
|
*/
|
|
224
224
|
setNetOpen(e) {
|
|
225
225
|
return r(this, null, function* () {
|
|
226
|
-
var
|
|
227
|
-
if (u.log("setNetOpen:", e), u.log("application arrange setting value: ", (
|
|
226
|
+
var t, i, n;
|
|
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
|
}
|
|
@@ -243,27 +243,27 @@ class X {
|
|
|
243
243
|
});
|
|
244
244
|
}
|
|
245
245
|
getService() {
|
|
246
|
-
return
|
|
247
|
-
const
|
|
246
|
+
return U(L.apiRoot, this.token, (e, t) => {
|
|
247
|
+
const i = new Error("data request error", {
|
|
248
248
|
cause: {
|
|
249
249
|
status: e,
|
|
250
|
-
message:
|
|
250
|
+
message: t
|
|
251
251
|
}
|
|
252
252
|
});
|
|
253
|
-
u.error(
|
|
253
|
+
u.error(i), this.event.emit("error", i);
|
|
254
254
|
});
|
|
255
255
|
}
|
|
256
|
-
addEventListener(e,
|
|
257
|
-
if (typeof
|
|
256
|
+
addEventListener(e, t, i = !1) {
|
|
257
|
+
if (typeof t != "function")
|
|
258
258
|
throw Error("event listener is not a function");
|
|
259
|
-
if (
|
|
260
|
-
this.event.once(e,
|
|
259
|
+
if (i) {
|
|
260
|
+
this.event.once(e, t);
|
|
261
261
|
return;
|
|
262
262
|
}
|
|
263
|
-
this.event.on(e,
|
|
263
|
+
this.event.on(e, t);
|
|
264
264
|
}
|
|
265
|
-
removeEventListener(e,
|
|
266
|
-
this.event.off(e,
|
|
265
|
+
removeEventListener(e, t) {
|
|
266
|
+
this.event.off(e, t);
|
|
267
267
|
}
|
|
268
268
|
/**
|
|
269
269
|
* 获取对话记录
|
|
@@ -272,35 +272,35 @@ class X {
|
|
|
272
272
|
*/
|
|
273
273
|
fetchMessages() {
|
|
274
274
|
return r(this, arguments, function* (e = {}) {
|
|
275
|
-
var
|
|
276
|
-
const
|
|
275
|
+
var i, n;
|
|
276
|
+
const t = {
|
|
277
277
|
pageSize: this._loadingMessagesSize
|
|
278
278
|
};
|
|
279
279
|
if (this._id) {
|
|
280
|
-
e = f(f({},
|
|
281
|
-
const o = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e),
|
|
280
|
+
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;
|
|
281
|
+
const o = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), h = o.map((s, d) => {
|
|
282
282
|
var m, a;
|
|
283
283
|
let g = [], y;
|
|
284
|
-
return typeof ((m = s.agentHistoryList) == null ? void 0 : m.agentList) == "string" && (g = JSON.parse(s.agentHistoryList.agentList).reduce((v, _) => (v[_.data.step - 1] || (v[_.data.step - 1] = []), v[_.data.step - 1].push(_), v), [])),
|
|
284
|
+
return typeof ((m = s.agentHistoryList) == null ? void 0 : m.agentList) == "string" && (g = JSON.parse(s.agentHistoryList.agentList).reduce((v, _) => (v[_.data.step - 1] || (v[_.data.step - 1] = []), v[_.data.step - 1].push(_), v), [])), d === o.length - 1 && (y = (a = this.data) == null ? void 0 : a.lastRelatedQuestion), I(f({}, s), {
|
|
285
285
|
actions: g,
|
|
286
286
|
relatedQuesion: y
|
|
287
287
|
});
|
|
288
288
|
});
|
|
289
289
|
if (!e.endTime && !e.startTime) {
|
|
290
290
|
const s = this.messages[this.messages.length - 1];
|
|
291
|
-
s && !s.relatedQuesion && (s.relatedQuesion = (
|
|
291
|
+
s && !s.relatedQuesion && (s.relatedQuesion = (i = this.data) == null ? void 0 : i.lastRelatedQuestion), this.messages = [...h, ...this.messages];
|
|
292
292
|
} else if (e.endTime && !e.startTime) {
|
|
293
|
-
const s = this.messages.findIndex((
|
|
294
|
-
this.messages.splice(0, s + 1), this.messages = [...
|
|
293
|
+
const s = this.messages.findIndex((d) => d.ts === e.endTime);
|
|
294
|
+
this.messages.splice(0, s + 1), this.messages = [...h, ...this.messages];
|
|
295
295
|
} else if (!e.endTime && e.startTime) {
|
|
296
|
-
const s = this.messages.findLastIndex((
|
|
297
|
-
this.messages.splice(s, this.messages.length - s - 1), this.messages = [...this.messages, ...
|
|
296
|
+
const s = this.messages.findLastIndex((d) => d.ts === e.endTime);
|
|
297
|
+
this.messages.splice(s, this.messages.length - s - 1), this.messages = [...this.messages, ...h];
|
|
298
298
|
} else {
|
|
299
|
-
const s = this.messages.findIndex((g) => g.ts === e.endTime),
|
|
300
|
-
this.messages.splice(s,
|
|
299
|
+
const s = this.messages.findIndex((g) => g.ts === e.endTime), d = this.messages.findLastIndex((g) => g.ts === e.endTime);
|
|
300
|
+
this.messages.splice(s, d - s + 1, ...h);
|
|
301
301
|
}
|
|
302
|
-
const
|
|
303
|
-
return
|
|
302
|
+
const c = this.messages[this.messages.length - 1];
|
|
303
|
+
return c && !c.relatedQuesion && (c.relatedQuesion = (n = this.data) == null ? void 0 : n.lastRelatedQuestion), this.event.emit("history:moreloaded", h), u.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, h;
|
|
304
304
|
} else {
|
|
305
305
|
const o = new Error(
|
|
306
306
|
"session id is not defined"
|
|
@@ -365,17 +365,17 @@ class X {
|
|
|
365
365
|
activate() {
|
|
366
366
|
return r(this, null, function* () {
|
|
367
367
|
if (this.activing)
|
|
368
|
-
return new Promise((
|
|
368
|
+
return new Promise((t) => {
|
|
369
369
|
this.addEventListener("activated", () => {
|
|
370
|
-
|
|
370
|
+
t(!0);
|
|
371
371
|
}, !0);
|
|
372
372
|
});
|
|
373
373
|
if (!this._id)
|
|
374
374
|
throw Error("session is not avalible. id is undefined");
|
|
375
375
|
this.activing = !0;
|
|
376
376
|
const e = this._id;
|
|
377
|
-
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((
|
|
378
|
-
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e),
|
|
377
|
+
return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((t) => r(this, null, function* () {
|
|
378
|
+
return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e), t;
|
|
379
379
|
})), 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;
|
|
380
380
|
});
|
|
381
381
|
}
|
|
@@ -390,9 +390,9 @@ class X {
|
|
|
390
390
|
get messagesLoading() {
|
|
391
391
|
return this._loadingMessages;
|
|
392
392
|
}
|
|
393
|
-
send(
|
|
394
|
-
return r(this, arguments, function* (e,
|
|
395
|
-
stream:
|
|
393
|
+
send(i) {
|
|
394
|
+
return r(this, arguments, function* (e, t = {
|
|
395
|
+
stream: L.stream
|
|
396
396
|
}) {
|
|
397
397
|
var m;
|
|
398
398
|
try {
|
|
@@ -401,10 +401,10 @@ class X {
|
|
|
401
401
|
console.error(a);
|
|
402
402
|
}
|
|
403
403
|
const {
|
|
404
|
-
stream: n =
|
|
404
|
+
stream: n = L.stream,
|
|
405
405
|
referenceFirst: o = !1
|
|
406
|
-
} =
|
|
407
|
-
if (!
|
|
406
|
+
} = t, h = e.uuid || M(), c = ((m = e.text) == null ? void 0 : m.trim()) || "", s = e.fileIds;
|
|
407
|
+
if (!c && /*!files.length &&*/
|
|
408
408
|
!(s != null && s.length)) {
|
|
409
409
|
const a = new Error(
|
|
410
410
|
"failed at sending empty content"
|
|
@@ -436,24 +436,24 @@ class X {
|
|
|
436
436
|
return u.error(a), this.event.emit("error", a), !1;
|
|
437
437
|
}
|
|
438
438
|
this.frozen = !0, this._stopAnswer = !1;
|
|
439
|
-
const
|
|
439
|
+
const d = {
|
|
440
440
|
sessionId: this.id,
|
|
441
441
|
role: 2,
|
|
442
|
-
content:
|
|
443
|
-
messageId:
|
|
442
|
+
content: c,
|
|
443
|
+
messageId: h,
|
|
444
444
|
reference: [],
|
|
445
445
|
searchReference: [],
|
|
446
446
|
messageTime: ""
|
|
447
447
|
}, g = {
|
|
448
|
-
content:
|
|
448
|
+
content: c,
|
|
449
449
|
stream: n,
|
|
450
|
-
requestId:
|
|
450
|
+
requestId: h,
|
|
451
451
|
fileIds: s,
|
|
452
452
|
referenceFirst: o
|
|
453
453
|
};
|
|
454
|
-
this.pushMessage(
|
|
454
|
+
this.pushMessage(d), this.event.emit("question:content", c);
|
|
455
455
|
const y = (a) => r(this, null, function* () {
|
|
456
|
-
if (a.role !==
|
|
456
|
+
if (a.role !== A.Robot)
|
|
457
457
|
return;
|
|
458
458
|
switch (this.removeEventListener("answer:finish", y), this._id && a.content && a.index && a.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
459
459
|
finialMessage: a.content
|
|
@@ -474,77 +474,88 @@ class X {
|
|
|
474
474
|
}
|
|
475
475
|
this.getRelatedQuesion(a), a.isLoading = !1;
|
|
476
476
|
let w = this.messages[this.messages.length - 1];
|
|
477
|
-
if (w && w.role ===
|
|
477
|
+
if (w && w.role === A.Robot) {
|
|
478
478
|
const v = f(f({}, w), a);
|
|
479
479
|
this.messages[this.messages.length - 1] = v, this.event.emit("message:updated", v);
|
|
480
480
|
} else
|
|
481
481
|
this.pushMessage(a);
|
|
482
482
|
this.frozen = !1;
|
|
483
483
|
});
|
|
484
|
-
return this.addEventListener("answer:finish", y), this.sendQuestion(g, I(f({},
|
|
484
|
+
return this.addEventListener("answer:finish", y), this.sendQuestion(g, I(f({}, t), {
|
|
485
485
|
stream: n,
|
|
486
486
|
referenceFirst: o
|
|
487
|
-
})),
|
|
487
|
+
})), d;
|
|
488
488
|
});
|
|
489
489
|
}
|
|
490
|
+
currentUserQuestion() {
|
|
491
|
+
for (let e = this.messages.length - 1; e >= 0; e -= 1) {
|
|
492
|
+
const t = this.messages[e];
|
|
493
|
+
if (t.role === A.User)
|
|
494
|
+
return t.content || "";
|
|
495
|
+
}
|
|
496
|
+
return "";
|
|
497
|
+
}
|
|
490
498
|
getRelatedQuesion(e) {
|
|
491
499
|
return r(this, null, function* () {
|
|
492
|
-
var
|
|
493
|
-
if (!e.content || e.isSensitive || e.code !== 200
|
|
500
|
+
var c, s;
|
|
501
|
+
if (!e.content || e.isSensitive || e.code !== 200)
|
|
502
|
+
return e;
|
|
503
|
+
const t = (c = this.application) == null ? void 0 : c.arrange[0], i = C(t == null ? void 0 : t.recommendQuestionOpen, t == null ? void 0 : t.relatedQuestionApp, e.content, this.currentUserQuestion());
|
|
504
|
+
if (i === null)
|
|
494
505
|
return e;
|
|
495
506
|
this.event.emit("message:relatedQuesion:loading", e);
|
|
496
|
-
const
|
|
497
|
-
content:
|
|
507
|
+
const n = yield this.cvforceApi.postGeneques({
|
|
508
|
+
content: i
|
|
498
509
|
}).catch((d) => null);
|
|
499
|
-
if (!
|
|
500
|
-
const d = [],
|
|
510
|
+
if (!n) {
|
|
511
|
+
const d = [], g = I(f({}, e), {
|
|
501
512
|
relatedQuesion: d,
|
|
502
513
|
index: -3,
|
|
503
514
|
isLoading: !1
|
|
504
515
|
});
|
|
505
516
|
return this.event.emit("message:relatedQuesion:loaded", {
|
|
506
517
|
relatedQuesion: d
|
|
507
|
-
}), this.event.emit("message:updated",
|
|
518
|
+
}), this.event.emit("message:updated", g), g;
|
|
508
519
|
}
|
|
509
|
-
const
|
|
510
|
-
e.relatedQuesion =
|
|
511
|
-
const
|
|
512
|
-
relatedQuesion:
|
|
520
|
+
const o = (s = n.content) == null ? void 0 : s.split("*").map(O).map(P).filter((d) => !!d);
|
|
521
|
+
e.relatedQuesion = o, e.index = -3, e.isLoading = !1;
|
|
522
|
+
const h = I(f({}, e), {
|
|
523
|
+
relatedQuesion: o,
|
|
513
524
|
index: -3,
|
|
514
525
|
isLoading: !1
|
|
515
526
|
});
|
|
516
|
-
return this.event.emit("message:relatedQuesion:loaded",
|
|
517
|
-
lastRelatedQuestion:
|
|
518
|
-
}),
|
|
527
|
+
return this.event.emit("message:relatedQuesion:loaded", h), this.event.emit("message:updated", h), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
|
|
528
|
+
lastRelatedQuestion: o
|
|
529
|
+
}), h;
|
|
519
530
|
});
|
|
520
531
|
}
|
|
521
532
|
fillMessageReference(e) {
|
|
522
533
|
return r(this, null, function* () {
|
|
523
|
-
const
|
|
524
|
-
return e.reference = e.reference.map((
|
|
534
|
+
const t = yield Promise.all(e.reference.map((i) => this.cvforceApi.getKnowledgeIdFileId(i.knowledgeId, i.docId)));
|
|
535
|
+
return e.reference = e.reference.map((i, n) => f(f({}, i), t[n])), this.event.emit("message:updated", e), e;
|
|
525
536
|
});
|
|
526
537
|
}
|
|
527
538
|
answerAgain() {
|
|
528
539
|
return r(this, arguments, function* (e = {
|
|
529
|
-
stream:
|
|
540
|
+
stream: L.stream
|
|
530
541
|
}) {
|
|
531
|
-
const
|
|
532
|
-
if (!
|
|
542
|
+
const t = this.messages[this.messages.length - 1];
|
|
543
|
+
if (!t)
|
|
533
544
|
throw Error("message list is empty");
|
|
534
545
|
this.frozen = !0, this._stopAnswer = !1;
|
|
535
546
|
const {
|
|
536
|
-
stream:
|
|
547
|
+
stream: i = L.stream
|
|
537
548
|
} = e;
|
|
538
549
|
let n;
|
|
539
|
-
|
|
540
|
-
const o = this.messages[this.messages.length - 1],
|
|
550
|
+
t.role !== A.User && (n = e.oldMessageId || t.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", t), this.event.emit("history:remove", t));
|
|
551
|
+
const o = this.messages[this.messages.length - 1], h = {
|
|
541
552
|
content: o.content,
|
|
542
553
|
fileIds: o.fileIds,
|
|
543
554
|
requestId: o.messageId,
|
|
544
|
-
stream:
|
|
555
|
+
stream: i,
|
|
545
556
|
oldMessageId: n
|
|
546
|
-
},
|
|
547
|
-
if (s.role ===
|
|
557
|
+
}, c = (s) => r(this, null, function* () {
|
|
558
|
+
if (s.role === A.Robot && s.index === -1) {
|
|
548
559
|
switch (this._id && s.content && s.index && s.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
|
|
549
560
|
finialMessage: s.content
|
|
550
561
|
}), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
|
|
@@ -562,11 +573,11 @@ class X {
|
|
|
562
573
|
default:
|
|
563
574
|
s.content = s.message || "未知错误", s.isLoading = !1, s.index = -1, this.event.emit("message:error", s);
|
|
564
575
|
}
|
|
565
|
-
this.getRelatedQuesion(s), this.removeEventListener("message:content",
|
|
576
|
+
this.getRelatedQuesion(s), this.removeEventListener("message:content", c), this.frozen = !1;
|
|
566
577
|
}
|
|
567
578
|
});
|
|
568
|
-
this.addEventListener("message:content",
|
|
569
|
-
stream:
|
|
579
|
+
this.addEventListener("message:content", c), this.sendQuestion(h, I(f({}, e), {
|
|
580
|
+
stream: i
|
|
570
581
|
}));
|
|
571
582
|
});
|
|
572
583
|
}
|
|
@@ -588,10 +599,10 @@ class X {
|
|
|
588
599
|
* @returns 返回当前会话实例
|
|
589
600
|
*/
|
|
590
601
|
suspend() {
|
|
591
|
-
var
|
|
602
|
+
var i;
|
|
592
603
|
this.stopAnswer(), this.frozen = !1, this._stopAnswer = !0;
|
|
593
604
|
let e = this.messages[this.messages.length - 1];
|
|
594
|
-
const
|
|
605
|
+
const t = {
|
|
595
606
|
messageId: M(),
|
|
596
607
|
// 生成唯一的消息 ID
|
|
597
608
|
isLoading: !1,
|
|
@@ -602,7 +613,7 @@ class X {
|
|
|
602
613
|
// 消息时间
|
|
603
614
|
content: "",
|
|
604
615
|
// 消息内容
|
|
605
|
-
role:
|
|
616
|
+
role: A.System,
|
|
606
617
|
// 消息角色为系统消息
|
|
607
618
|
sessionId: this.id || "",
|
|
608
619
|
// 会话 ID
|
|
@@ -615,22 +626,22 @@ class X {
|
|
|
615
626
|
if (e.index === -2)
|
|
616
627
|
return;
|
|
617
628
|
if (e.actions) {
|
|
618
|
-
const n = e.actions[((
|
|
629
|
+
const n = e.actions[((i = e.actions) == null ? void 0 : i.length) - 1];
|
|
619
630
|
if (n) {
|
|
620
631
|
const o = n[n.length - 1];
|
|
621
632
|
if (o && (o.type === "BEFORE_CALL" || o.type === "FUNCTION_CALL")) {
|
|
622
633
|
const {
|
|
623
|
-
pluginName:
|
|
624
|
-
pluginCode:
|
|
634
|
+
pluginName: h,
|
|
635
|
+
pluginCode: c,
|
|
625
636
|
funcCode: s,
|
|
626
|
-
funcName:
|
|
637
|
+
funcName: d
|
|
627
638
|
} = o.data;
|
|
628
639
|
n.push({
|
|
629
640
|
data: {
|
|
630
|
-
pluginName:
|
|
631
|
-
pluginCode:
|
|
641
|
+
pluginName: h,
|
|
642
|
+
pluginCode: c,
|
|
632
643
|
funcCode: s,
|
|
633
|
-
funcName:
|
|
644
|
+
funcName: d,
|
|
634
645
|
step: o.data.step + 1
|
|
635
646
|
},
|
|
636
647
|
type: "ABORT"
|
|
@@ -641,9 +652,9 @@ class X {
|
|
|
641
652
|
}
|
|
642
653
|
}
|
|
643
654
|
}
|
|
644
|
-
e = f(f({}, e),
|
|
655
|
+
e = f(f({}, e), t), this.event.emit("message:content", e), this.event.emit("message:updated", e);
|
|
645
656
|
} else
|
|
646
|
-
this.pushMessage(
|
|
657
|
+
this.pushMessage(t), this.event.emit("message:content", t);
|
|
647
658
|
return this;
|
|
648
659
|
}
|
|
649
660
|
/**
|
|
@@ -655,9 +666,9 @@ class X {
|
|
|
655
666
|
return r(this, null, function* () {
|
|
656
667
|
if (!this._applicationId || !this._id)
|
|
657
668
|
throw Error("session is not avalible. id is undefined");
|
|
658
|
-
return this.cvforceApi.postComments(this._applicationId, this._id, e).then((
|
|
659
|
-
const
|
|
660
|
-
return
|
|
669
|
+
return this.cvforceApi.postComments(this._applicationId, this._id, e).then((t) => {
|
|
670
|
+
const i = this.messages.find((n) => n.messageId === e.messageId);
|
|
671
|
+
return i && (i.comment = e, this.event.emit("message:comment", i)), t;
|
|
661
672
|
});
|
|
662
673
|
});
|
|
663
674
|
}
|
|
@@ -684,6 +695,6 @@ class X {
|
|
|
684
695
|
}
|
|
685
696
|
}
|
|
686
697
|
export {
|
|
687
|
-
|
|
688
|
-
|
|
698
|
+
D as SessionErrors,
|
|
699
|
+
Z as default
|
|
689
700
|
};
|
package/dist/es/utils.mjs
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import { decode as
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
return (/* @__PURE__ */ new Date()).valueOf() / 1e3 >=
|
|
5
|
-
},
|
|
1
|
+
import { decode as o } from "js-base64";
|
|
2
|
+
const t = (n) => JSON.parse(o(n.split(".")[1])), w = (n) => {
|
|
3
|
+
const e = t(n).exp;
|
|
4
|
+
return (/* @__PURE__ */ new Date()).valueOf() / 1e3 >= e;
|
|
5
|
+
}, f = (n) => n == null ? void 0 : n.replace(/[\r\n]+/gm, ""), T = (n) => n.replace(/\\|\\/g, ""), g = (n, e, c, i) => {
|
|
6
|
+
if (n !== !0)
|
|
7
|
+
return null;
|
|
8
|
+
const s = (e == null ? void 0 : e.includeUserQuestion) === !0, u = (e == null ? void 0 : e.includeAiAnswer) === !0 || !s, r = [];
|
|
9
|
+
return s && r.push(`用户问题:${i}`), u && r.push(`AI回答:${c}`), r.join(`
|
|
10
|
+
`);
|
|
11
|
+
};
|
|
6
12
|
export {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
w as isExpired,
|
|
14
|
+
t as parseJWT,
|
|
15
|
+
T as removeLeadingAndTrailingBackslashes,
|
|
16
|
+
f as removeNewlines,
|
|
17
|
+
g as resolveRelatedQuestionContent
|
|
11
18
|
};
|