@glodon-aiot/bot-client-sdk 3.3.2-beta.0 → 3.3.2-beta.2

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