@glodon-aiot/bot-client-sdk 3.3.2 → 3.3.4-beta.1

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