@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.
@@ -1,43 +1,43 @@
1
- var x = Object.defineProperty, V = Object.defineProperties;
2
- var Q = Object.getOwnPropertyDescriptors;
1
+ var x = Object.defineProperty, Q = Object.defineProperties;
2
+ var V = Object.getOwnPropertyDescriptors;
3
3
  var S = Object.getOwnPropertySymbols;
4
- var T = Object.prototype.hasOwnProperty, R = Object.prototype.propertyIsEnumerable;
5
- var k = (h, e, i) => e in h ? x(h, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : h[e] = i, f = (h, e) => {
6
- for (var i in e || (e = {}))
7
- T.call(e, i) && k(h, i, e[i]);
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 i of S(e))
10
- R.call(e, i) && k(h, i, e[i]);
11
- return h;
12
- }, I = (h, e) => V(h, Q(e));
13
- var c = (h, e, i) => (k(h, typeof e != "symbol" ? e + "" : e, i), i);
14
- var r = (h, e, i) => new Promise((t, n) => {
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
- d(i.next(s));
18
- } catch (p) {
19
- n(p);
17
+ c(t.next(s));
18
+ } catch (d) {
19
+ n(d);
20
20
  }
21
- }, l = (s) => {
21
+ }, h = (s) => {
22
22
  try {
23
- d(i.throw(s));
24
- } catch (p) {
25
- n(p);
23
+ c(t.throw(s));
24
+ } catch (d) {
25
+ n(d);
26
26
  }
27
- }, d = (s) => s.done ? t(s.value) : Promise.resolve(s.value).then(o, l);
28
- d((i = i.apply(h, e)).next());
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 C } from "./utils.mjs";
32
- import { ApplicationTypes as E, PromptStatus as P, PromptVariableType as b, ChatMessageRole as L } 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";
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 q("[Session] ");
39
- var U = /* @__PURE__ */ ((h) => (h.DataRequestFailed = "data request error", h.SocketConnectionFailed = "socket error", h.IdUndefined = "session id is not defined", h.SendingEmptyContent = "failed at sending empty content", h.Frozen = "session is frozen", h.UserModeError = "failed at setting socket user mode", h.Inactive = "session is inactive", h))(U || {});
40
- class X {
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, i = "", t, n, o, l) {
51
- c(this, "_id");
52
- c(this, "_frozen", !1);
50
+ constructor(e, t = "", i, n, o, h) {
51
+ p(this, "_id");
52
+ p(this, "_frozen", !1);
53
53
  //是否可发送新问题
54
- c(this, "inactivating", !1);
55
- c(this, "_ready", !1);
54
+ p(this, "inactivating", !1);
55
+ p(this, "_ready", !1);
56
56
  // ready = false;
57
- c(this, "event", new F());
57
+ p(this, "event", new N());
58
58
  //从token解析出来的用户信息
59
- c(this, "_applicationId");
60
- c(this, "_application", null);
61
- c(this, "data", null);
62
- c(this, "client", null);
63
- c(this, "messages", []);
64
- c(this, "_loadingMessages", !1);
65
- c(this, "_loadingMessagesSize", 10);
66
- c(this, "cvforceApi");
67
- c(this, "socket", null);
68
- c(this, "_promptVariables", []);
69
- c(this, "active", !1);
70
- c(this, "activing", !1);
71
- c(this, "_stopAnswer", !1);
72
- c(this, "_dataLoading", !1);
73
- c(this, "_dataLoaded", !1);
74
- c(this, "_mcpServers", null);
75
- this.token = e, this.lazyload = n, this.client = l || null, this._applicationId = z(e).rid || A.applicationId, this.cvforceApi = this.getService(), o && (this._application = o), this.event.emit("init", {
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: i
77
+ sessionId: t
78
78
  }), this.loadApplicationInfo().then(() => {
79
- var d;
80
- if (i)
81
- this._id = i, this.event.emit("data:loading"), t ? (this.data = t, n || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, i).then((s) => r(this, null, function* () {
82
- this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, i);
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, i).then((s) => r(this, null, function* () {
86
- this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, i);
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 = (t == null ? void 0 : t.name) || "新建会话";
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: t == null ? void 0 : t.netOpen,
99
- knowledgeIdList: (d = t == null ? void 0 : t.knowledges) == null ? void 0 : d.map((p) => p.id),
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((p) => r(this, null, function* () {
101
+ }).then((d) => r(this, null, function* () {
102
102
  var g;
103
- this._id = p.id, this.data = I(f({}, p), {
104
- knowledges: t == null ? void 0 : t.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 (t != null && t.knowledges)
107
- return this.setKnowledges((t == null ? void 0 : t.knowledges) || void 0);
106
+ if (i != null && i.knowledges)
107
+ return this.setKnowledges((i == null ? void 0 : i.knowledges) || void 0);
108
108
  }))(), yield (() => {
109
- if (t != null && t.promptVariables) {
109
+ if (i != null && i.promptVariables) {
110
110
  const y = this._promptVariables.map((m) => {
111
111
  var w, v;
112
- const a = ((v = (w = t.promptVariables) == null ? void 0 : w.find((_) => _.key === m.key && _.type === m.type)) == null ? void 0 : v.value) || "";
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((i) => i.status === P.Quoted).filter((i) => !i.value);
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((i) => i.key).join(", ")}`);
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 i = this._promptVariables.map((t) => {
193
+ const t = this._promptVariables.map((i) => {
194
194
  var o;
195
- const n = ((o = e == null ? void 0 : e.find((l) => l.key === t.key && l.type === t.type)) == null ? void 0 : o.value) || t.value;
196
- return I(f({}, t), {
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((t, n) => t.key.localeCompare(n.key));
200
- if (yield Promise.all(i.filter((t) => !(t.type === b.User && t.key === "query") && !(t.type === b.System && t.key === "datetime")).map((t) => {
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 (!(!t.id || !t.key))
204
- return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id, t.id, {
205
- value: t.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 i, t, n;
227
- if (u.log("setNetOpen:", e), u.log("application arrange setting value: ", (i = this._application) == null ? void 0 : i.arrange[0].enableNetOpen), ((t = this._application) == null ? void 0 : t.type) === E.Agent) {
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 N(A.apiRoot, this.token, (e, i) => {
247
- const t = new Error("data request error", {
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: i
250
+ message: t
251
251
  }
252
252
  });
253
- u.error(t), this.event.emit("error", t);
253
+ u.error(i), this.event.emit("error", i);
254
254
  });
255
255
  }
256
- addEventListener(e, i, t = !1) {
257
- if (typeof i != "function")
256
+ addEventListener(e, t, i = !1) {
257
+ if (typeof t != "function")
258
258
  throw Error("event listener is not a function");
259
- if (t) {
260
- this.event.once(e, i);
259
+ if (i) {
260
+ this.event.once(e, t);
261
261
  return;
262
262
  }
263
- this.event.on(e, i);
263
+ this.event.on(e, t);
264
264
  }
265
- removeEventListener(e, i) {
266
- this.event.off(e, i);
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 t, n;
276
- const i = {
275
+ var i, n;
276
+ const t = {
277
277
  pageSize: this._loadingMessagesSize
278
278
  };
279
279
  if (this._id) {
280
- e = f(f({}, i), 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), l = o.map((s, p) => {
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), [])), p === o.length - 1 && (y = (a = this.data) == null ? void 0 : a.lastRelatedQuestion), I(f({}, s), {
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 = (t = this.data) == null ? void 0 : t.lastRelatedQuestion), this.messages = [...l, ...this.messages];
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((p) => p.ts === e.endTime);
294
- this.messages.splice(0, s + 1), this.messages = [...l, ...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((p) => p.ts === e.endTime);
297
- this.messages.splice(s, this.messages.length - s - 1), this.messages = [...this.messages, ...l];
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), p = this.messages.findLastIndex((g) => g.ts === e.endTime);
300
- this.messages.splice(s, p - s + 1, ...l);
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 d = this.messages[this.messages.length - 1];
303
- return d && !d.relatedQuesion && (d.relatedQuesion = (n = this.data) == null ? void 0 : n.lastRelatedQuestion), this.event.emit("history:moreloaded", l), u.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, l;
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((i) => {
368
+ return new Promise((t) => {
369
369
  this.addEventListener("activated", () => {
370
- i(!0);
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((i) => r(this, null, function* () {
378
- return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e), i;
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(t) {
394
- return r(this, arguments, function* (e, i = {
395
- stream: A.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 = A.stream,
404
+ stream: n = L.stream,
405
405
  referenceFirst: o = !1
406
- } = i, l = e.uuid || M(), d = ((m = e.text) == null ? void 0 : m.trim()) || "", s = e.fileIds;
407
- if (!d && /*!files.length &&*/
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 p = {
439
+ const d = {
440
440
  sessionId: this.id,
441
441
  role: 2,
442
- content: d,
443
- messageId: l,
442
+ content: c,
443
+ messageId: h,
444
444
  reference: [],
445
445
  searchReference: [],
446
446
  messageTime: ""
447
447
  }, g = {
448
- content: d,
448
+ content: c,
449
449
  stream: n,
450
- requestId: l,
450
+ requestId: h,
451
451
  fileIds: s,
452
452
  referenceFirst: o
453
453
  };
454
- this.pushMessage(p), this.event.emit("question:content", d);
454
+ this.pushMessage(d), this.event.emit("question:content", c);
455
455
  const y = (a) => r(this, null, function* () {
456
- if (a.role !== L.Robot)
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 === L.Robot) {
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({}, i), {
484
+ return this.addEventListener("answer:finish", y), this.sendQuestion(g, I(f({}, t), {
485
485
  stream: n,
486
486
  referenceFirst: o
487
- })), p;
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 o, l;
493
- if (!e.content || e.isSensitive || e.code !== 200 || !((o = this.application) != null && o.arrange[0].recommendQuestionOpen))
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 i = yield this.cvforceApi.postGeneques({
497
- content: e.content
507
+ const n = yield this.cvforceApi.postGeneques({
508
+ content: i
498
509
  }).catch((d) => null);
499
- if (!i) {
500
- const d = [], s = I(f({}, e), {
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", s), s;
518
+ }), this.event.emit("message:updated", g), g;
508
519
  }
509
- const t = (l = i.content) == null ? void 0 : l.split("*").map(O).map(C).filter((d) => !!d);
510
- e.relatedQuesion = t, e.index = -3, e.isLoading = !1;
511
- const n = I(f({}, e), {
512
- relatedQuesion: t,
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", n), this.event.emit("message:updated", n), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
517
- lastRelatedQuestion: t
518
- }), n;
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 i = yield Promise.all(e.reference.map((t) => this.cvforceApi.getKnowledgeIdFileId(t.knowledgeId, t.docId)));
524
- return e.reference = e.reference.map((t, n) => f(f({}, t), i[n])), this.event.emit("message:updated", e), e;
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: A.stream
540
+ stream: L.stream
530
541
  }) {
531
- const i = this.messages[this.messages.length - 1];
532
- if (!i)
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: t = A.stream
547
+ stream: i = L.stream
537
548
  } = e;
538
549
  let n;
539
- i.role !== L.User && (n = e.oldMessageId || i.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", i), this.event.emit("history:remove", i));
540
- const o = this.messages[this.messages.length - 1], l = {
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: t,
555
+ stream: i,
545
556
  oldMessageId: n
546
- }, d = (s) => r(this, null, function* () {
547
- if (s.role === L.Robot && s.index === -1) {
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", d), this.frozen = !1;
576
+ this.getRelatedQuesion(s), this.removeEventListener("message:content", c), this.frozen = !1;
566
577
  }
567
578
  });
568
- this.addEventListener("message:content", d), this.sendQuestion(l, I(f({}, e), {
569
- stream: t
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 t;
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 i = {
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: L.System,
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[((t = e.actions) == null ? void 0 : t.length) - 1];
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: l,
624
- pluginCode: d,
634
+ pluginName: h,
635
+ pluginCode: c,
625
636
  funcCode: s,
626
- funcName: p
637
+ funcName: d
627
638
  } = o.data;
628
639
  n.push({
629
640
  data: {
630
- pluginName: l,
631
- pluginCode: d,
641
+ pluginName: h,
642
+ pluginCode: c,
632
643
  funcCode: s,
633
- funcName: p,
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), i), this.event.emit("message:content", e), this.event.emit("message:updated", 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(i), this.event.emit("message:content", i);
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((i) => {
659
- const t = this.messages.find((n) => n.messageId === e.messageId);
660
- return t && (t.comment = e, this.event.emit("message:comment", t)), i;
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
- U as SessionErrors,
688
- X as default
698
+ D as SessionErrors,
699
+ Z as default
689
700
  };
package/dist/es/utils.mjs CHANGED
@@ -1,11 +1,18 @@
1
- import { decode as n } from "js-base64";
2
- const c = (e) => JSON.parse(n(e.split(".")[1])), i = (e) => {
3
- const r = c(e).exp;
4
- return (/* @__PURE__ */ new Date()).valueOf() / 1e3 >= r;
5
- }, a = (e) => e == null ? void 0 : e.replace(/[\r\n]+/gm, ""), p = (e) => e.replace(/\\|\\/g, "");
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
- i as isExpired,
8
- c as parseJWT,
9
- p as removeLeadingAndTrailingBackslashes,
10
- a as removeNewlines
13
+ w as isExpired,
14
+ t as parseJWT,
15
+ T as removeLeadingAndTrailingBackslashes,
16
+ f as removeNewlines,
17
+ g as resolveRelatedQuestionContent
11
18
  };