@glodon-aiot/bot-client-sdk 3.26.0 → 3.26.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,44 +1,45 @@
1
- var V = Object.defineProperty, M = Object.defineProperties;
2
- var O = Object.getOwnPropertyDescriptors;
3
- var x = Object.getOwnPropertySymbols;
4
- var R = Object.prototype.hasOwnProperty, z = Object.prototype.propertyIsEnumerable;
5
- var b = (p, e, t) => e in p ? V(p, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : p[e] = t, u = (p, e) => {
1
+ var O = Object.defineProperty, M = Object.defineProperties;
2
+ var z = Object.getOwnPropertyDescriptors;
3
+ var C = Object.getOwnPropertySymbols;
4
+ var F = Object.prototype.hasOwnProperty, P = Object.prototype.propertyIsEnumerable;
5
+ var b = (f, e, t) => e in f ? O(f, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : f[e] = t, u = (f, e) => {
6
6
  for (var t in e || (e = {}))
7
- R.call(e, t) && b(p, t, e[t]);
8
- if (x)
9
- for (var t of x(e))
10
- z.call(e, t) && b(p, t, e[t]);
11
- return p;
12
- }, y = (p, e) => M(p, O(e));
13
- var f = (p, e, t) => (b(p, typeof e != "symbol" ? e + "" : e, t), t);
14
- var c = (p, e, t) => new Promise((i, o) => {
15
- var n = (a) => {
7
+ F.call(e, t) && b(f, t, e[t]);
8
+ if (C)
9
+ for (var t of C(e))
10
+ P.call(e, t) && b(f, t, e[t]);
11
+ return f;
12
+ }, y = (f, e) => M(f, z(e));
13
+ var m = (f, e, t) => (b(f, typeof e != "symbol" ? e + "" : e, t), t);
14
+ var l = (f, e, t) => new Promise((i, s) => {
15
+ var a = (n) => {
16
16
  try {
17
- d(t.next(a));
18
- } catch (s) {
19
- o(s);
17
+ r(t.next(n));
18
+ } catch (o) {
19
+ s(o);
20
20
  }
21
- }, h = (a) => {
21
+ }, c = (n) => {
22
22
  try {
23
- d(t.throw(a));
24
- } catch (s) {
25
- o(s);
23
+ r(t.throw(n));
24
+ } catch (o) {
25
+ s(o);
26
26
  }
27
- }, d = (a) => a.done ? i(a.value) : Promise.resolve(a.value).then(n, h);
28
- d((t = t.apply(p, e)).next());
27
+ }, r = (n) => n.done ? i(n.value) : Promise.resolve(n.value).then(a, c);
28
+ r((t = t.apply(f, e)).next());
29
29
  });
30
30
  import { v4 as Q } from "uuid";
31
- import { parseJWT as P, resolveRelatedQuestionContent as q, removeNewlines as F, removeLeadingAndTrailingBackslashes as N } from "./utils.mjs";
32
- import { ApplicationTypes as T, PromptStatus as U, PromptVariableType as L, ChatMessageRole as S } from "@glodon-aiot/apis";
33
- import B from "./Logger.mjs";
34
- import { configuration as _ } from "./Config.mjs";
35
- import H from "./EventBus.mjs";
31
+ import { parseJWT as q, resolveRelatedQuestionContent as N, removeNewlines as U, removeLeadingAndTrailingBackslashes as B } from "./utils.mjs";
32
+ import { ApplicationTypes as R, PromptStatus as H, PromptVariableType as L, ChatMessageRole as A } from "@glodon-aiot/apis";
33
+ import j from "./Logger.mjs";
34
+ import { configuration as S } from "./Config.mjs";
35
+ import G from "./EventBus.mjs";
36
+ import { UsageErrorCode as E, USAGE_ERROR_DEFAULT_MESSAGES as T } from "./PayloadTypes.mjs";
36
37
  import { getCVForceService as K } from "./api/index.mjs";
37
38
  import Y from "dayjs";
38
- import { uploadSessionFile as G } from "./sessionUpload.mjs";
39
- const C = 100, l = new B("[Session] ");
40
- var j = /* @__PURE__ */ ((p) => (p.DataRequestFailed = "data request error", p.SocketConnectionFailed = "socket error", p.IdUndefined = "session id is not defined", p.SendingEmptyContent = "failed at sending empty content", p.Frozen = "session is frozen", p.UserModeError = "failed at setting socket user mode", p.Inactive = "session is inactive", p))(j || {});
41
- class ne {
39
+ import { uploadSessionFile as J } from "./sessionUpload.mjs";
40
+ const V = 100, h = new j("[Session] ");
41
+ var W = /* @__PURE__ */ ((f) => (f.DataRequestFailed = "data request error", f.SocketConnectionFailed = "socket error", f.IdUndefined = "session id is not defined", f.SendingEmptyContent = "failed at sending empty content", f.Frozen = "session is frozen", f.UserModeError = "failed at setting socket user mode", f.Inactive = "session is inactive", f.UsageError = "usage error", f))(W || {});
42
+ class re {
42
43
  /**
43
44
  * Session 类的构造函数,用于初始化会话实例。
44
45
  * @param token - 用户的身份验证令牌,用于解析应用 ID。
@@ -48,83 +49,83 @@ class ne {
48
49
  * @param _application - 应用信息,可选参数。
49
50
  * @param client - Bot 客户端实例,可选参数。
50
51
  */
51
- constructor(e, t = "", i, o, n, h) {
52
- f(this, "_id");
53
- f(this, "_frozen", !1);
52
+ constructor(e, t = "", i, s, a, c) {
53
+ m(this, "_id");
54
+ m(this, "_frozen", !1);
54
55
  //是否可发送新问题
55
- f(this, "inactivating", !1);
56
- f(this, "_ready", !1);
56
+ m(this, "inactivating", !1);
57
+ m(this, "_ready", !1);
57
58
  // ready = false;
58
- f(this, "event", new H());
59
+ m(this, "event", new G());
59
60
  //从token解析出来的用户信息
60
- f(this, "_applicationId");
61
- f(this, "_application", null);
62
- f(this, "data", null);
63
- f(this, "client", null);
64
- f(this, "messages", []);
65
- f(this, "_loadingMessages", !1);
66
- f(this, "_loadingMessagesSize", 10);
67
- f(this, "cvforceApi");
68
- f(this, "socket", null);
69
- f(this, "_promptVariables", []);
70
- f(this, "_sessionActive", !1);
71
- f(this, "activing", !1);
72
- f(this, "_stopAnswer", !1);
73
- f(this, "_dataLoading", !1);
74
- f(this, "_dataLoaded", !1);
75
- f(this, "_mcpServers", null);
76
- this.token = e, this.lazyload = o, this.client = h || null, this._applicationId = P(e).rid || _.applicationId, this.cvforceApi = this.getService(), n && (this._application = n), this.event.emit("init", {
61
+ m(this, "_applicationId");
62
+ m(this, "_application", null);
63
+ m(this, "data", null);
64
+ m(this, "client", null);
65
+ m(this, "messages", []);
66
+ m(this, "_loadingMessages", !1);
67
+ m(this, "_loadingMessagesSize", 10);
68
+ m(this, "cvforceApi");
69
+ m(this, "socket", null);
70
+ m(this, "_promptVariables", []);
71
+ m(this, "_sessionActive", !1);
72
+ m(this, "activing", !1);
73
+ m(this, "_stopAnswer", !1);
74
+ m(this, "_dataLoading", !1);
75
+ m(this, "_dataLoaded", !1);
76
+ m(this, "_mcpServers", null);
77
+ this.token = e, this.lazyload = s, this.client = c || null, this._applicationId = q(e).rid || S.applicationId, this.cvforceApi = this.getService(), a && (this._application = a), this.event.emit("init", {
77
78
  applicationId: this._applicationId,
78
79
  sessionId: t
79
80
  }), this.loadApplicationInfo().then(() => {
80
- var d;
81
+ var r;
81
82
  if (t)
82
- this._id = t, this.event.emit("data:loading"), i ? (this.data = i, o || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((a) => c(this, null, function* () {
83
- this.data = a, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
84
- })).then(() => c(this, null, function* () {
83
+ this._id = t, this.event.emit("data:loading"), i ? (this.data = i, s || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((n) => l(this, null, function* () {
84
+ this.data = n, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
85
+ })).then(() => l(this, null, function* () {
85
86
  this.messages = [], yield this.fetchMessages(), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data);
86
- })))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((a) => c(this, null, function* () {
87
- this.data = a, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
88
- })).then(() => c(this, null, function* () {
87
+ })))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, t).then((n) => l(this, null, function* () {
88
+ this.data = n, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, t);
89
+ })).then(() => l(this, null, function* () {
89
90
  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;
90
91
  })));
91
92
  else {
92
- const a = (i == null ? void 0 : i.name) || "新建会话";
93
+ const n = (i == null ? void 0 : i.name) || "新建会话";
93
94
  this.event.emit("data:creating", {
94
95
  applicationId: this._applicationId,
95
- name: a
96
+ name: n
96
97
  }), this.event.emit("data:loading"), this.cvforceApi.postSession(this.applicationId, {
97
- name: a,
98
+ name: n,
98
99
  // @ts-ignore
99
100
  netOpen: i == null ? void 0 : i.netOpen,
100
- knowledgeIdList: (d = i == null ? void 0 : i.knowledges) == null ? void 0 : d.map((s) => s.id),
101
+ knowledgeIdList: (r = i == null ? void 0 : i.knowledges) == null ? void 0 : r.map((o) => o.id),
101
102
  applicationId: this._applicationId
102
- }).then((s) => c(this, null, function* () {
103
- var m;
104
- this._id = s.id, this.data = y(u({}, s), {
103
+ }).then((o) => l(this, null, function* () {
104
+ var p;
105
+ this._id = o.id, this.data = y(u({}, o), {
105
106
  knowledges: i == null ? void 0 : i.knowledges
106
107
  }), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id);
107
108
  try {
108
- yield (() => c(this, null, function* () {
109
+ yield (() => l(this, null, function* () {
109
110
  if (i != null && i.knowledges)
110
111
  return this.setKnowledges((i == null ? void 0 : i.knowledges) || void 0);
111
112
  }))();
112
- } catch (I) {
113
- l.error(I);
113
+ } catch (g) {
114
+ h.error(g);
114
115
  }
115
116
  yield (() => {
116
117
  if (i != null && i.promptVariables) {
117
- const I = this._promptVariables.map((g) => {
118
- var A, v;
119
- const w = ((v = (A = i.promptVariables) == null ? void 0 : A.find((r) => r.key === g.key && r.type === g.type)) == null ? void 0 : v.value) || "";
120
- return y(u({}, g), {
118
+ const g = this._promptVariables.map((v) => {
119
+ var k, I;
120
+ const w = ((I = (k = i.promptVariables) == null ? void 0 : k.find((d) => d.key === v.key && d.type === v.type)) == null ? void 0 : I.value) || "";
121
+ return y(u({}, v), {
121
122
  value: w
122
123
  });
123
- }).sort((g, w) => g.key.localeCompare(w.key));
124
- return this.setPromptVariables(I || []);
124
+ }).sort((v, w) => v.key.localeCompare(w.key));
125
+ return this.setPromptVariables(g || []);
125
126
  } else
126
127
  return;
127
- })(), 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) !== T.Agent && (this._ready = !0, this.event.emit("ready"));
128
+ })(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((p = this.application) == null ? void 0 : p.type) !== R.Agent && (this._ready = !0, this.event.emit("ready"));
128
129
  }));
129
130
  }
130
131
  });
@@ -134,10 +135,10 @@ class ne {
134
135
  return this._sessionActive;
135
136
  }
136
137
  fetchData() {
137
- return c(this, null, function* () {
138
+ return l(this, null, function* () {
138
139
  if (!this.data)
139
140
  throw Error("session data is not loaded");
140
- return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) => c(this, null, function* () {
141
+ return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) => l(this, null, function* () {
141
142
  return this.data = e, this.event.emit("data:updated", this.data), e;
142
143
  }));
143
144
  });
@@ -177,17 +178,17 @@ class ne {
177
178
  return this.data.knowledges || void 0;
178
179
  }
179
180
  setKnowledges(e) {
180
- return c(this, null, function* () {
181
+ return l(this, null, function* () {
181
182
  if (!this.data)
182
183
  throw Error("session data is not loaded");
183
- if ((e || []).some((o) => Array.isArray(o.fileIds) && o.fileIds.length === 0))
184
+ if ((e || []).some((s) => Array.isArray(s.fileIds) && s.fileIds.length === 0))
184
185
  throw Error("fileIds cannot be an empty array");
185
- if ((e || []).reduce((o, n) => {
186
- var d, a;
187
- const h = (a = (d = n.fileIds) == null ? void 0 : d.length) != null ? a : 0;
188
- return o + (h > 0 ? h : 1);
189
- }, 0) > C)
190
- throw Error(`knowledges and fileIds total count cannot exceed ${C}`);
186
+ if ((e || []).reduce((s, a) => {
187
+ var r, n;
188
+ const c = (n = (r = a.fileIds) == null ? void 0 : r.length) != null ? n : 0;
189
+ return s + (c > 0 ? c : 1);
190
+ }, 0) > V)
191
+ throw Error(`knowledges and fileIds total count cannot exceed ${V}`);
191
192
  return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
192
193
  knowledges: e
193
194
  }), this.data.knowledges = e, this.event.emit("data:updated", this.data), this.event.emit("data:updated:knowledges", {
@@ -200,7 +201,7 @@ class ne {
200
201
  throw Error("session id is not defined");
201
202
  if (!this.data)
202
203
  throw Error("session data is not loaded");
203
- const e = this.promptVariables.filter((t) => t.status === U.Quoted).filter((t) => !t.value);
204
+ const e = this.promptVariables.filter((t) => t.status === H.Quoted).filter((t) => !t.value);
204
205
  if (e.length)
205
206
  throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((t) => t.key).join(", ")}`);
206
207
  }
@@ -208,24 +209,24 @@ class ne {
208
209
  return this._promptVariables.filter((e) => !(e.type === L.User && e.key === "query") && !(e.type === L.System && e.key === "datetime"));
209
210
  }
210
211
  setPromptVariables(e) {
211
- return c(this, null, function* () {
212
- const t = e == null ? void 0 : e.map((n) => n.type ? n : y(u({}, n), {
212
+ return l(this, null, function* () {
213
+ const t = e == null ? void 0 : e.map((a) => a.type ? a : y(u({}, a), {
213
214
  type: "instruction"
214
- })), i = this._promptVariables.map((n) => {
215
- var d;
216
- const h = ((d = t == null ? void 0 : t.find((a) => a.key === n.key && a.type === n.type)) == null ? void 0 : d.value) || n.value;
217
- return y(u({}, n), {
218
- value: h
215
+ })), i = this._promptVariables.map((a) => {
216
+ var r;
217
+ const c = ((r = t == null ? void 0 : t.find((n) => n.key === a.key && n.type === a.type)) == null ? void 0 : r.value) || a.value;
218
+ return y(u({}, a), {
219
+ value: c
219
220
  });
220
- }).sort((n, h) => n.key.localeCompare(h.key));
221
- if (!(yield Promise.all(i.filter((n) => !(n.type === L.User && n.key === "query") && !(n.type === L.System && n.key === "datetime")).map((n) => {
221
+ }).sort((a, c) => a.key.localeCompare(c.key));
222
+ if (!(yield Promise.all(i.filter((a) => !(a.type === L.User && a.key === "query") && !(a.type === L.System && a.key === "datetime")).map((a) => {
222
223
  if (!this.data)
223
224
  throw Error("session data is not loaded");
224
- if (!(!n.id || !n.key))
225
- return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id, n.id, {
226
- value: n.value
225
+ if (!(!a.id || !a.key))
226
+ return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id, a.id, {
227
+ value: a.value
227
228
  });
228
- })).then(() => !0).catch((n) => !1)))
229
+ })).then(() => !0).catch((a) => !1)))
229
230
  return !1;
230
231
  if (!this.data)
231
232
  throw Error("session data is not loaded");
@@ -245,14 +246,14 @@ class ne {
245
246
  * @returns 如果设置成功,返回最终的网络访问开关状态;若不满足设置条件,则提前返回 undefined
246
247
  */
247
248
  setNetOpen(e) {
248
- return c(this, null, function* () {
249
- var t, i, o;
250
- if (l.log("setNetOpen:", e), l.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((i = this._application) == null ? void 0 : i.type) === T.Agent) {
251
- l.warn("Agent Application Sessions are disabled to setNetOpen.");
249
+ return l(this, null, function* () {
250
+ var t, i, s;
251
+ if (h.log("setNetOpen:", e), h.log("application arrange setting value: ", (t = this._application) == null ? void 0 : t.arrange[0].enableNetOpen), ((i = this._application) == null ? void 0 : i.type) === R.Agent) {
252
+ h.warn("Agent Application Sessions are disabled to setNetOpen.");
252
253
  return;
253
254
  }
254
- if (((o = this._application) == null ? void 0 : o.arrange[0].enableNetOpen) === !1) {
255
- l.warn("This Application is disabled to change netOpen.");
255
+ if (((s = this._application) == null ? void 0 : s.arrange[0].enableNetOpen) === !1) {
256
+ h.warn("This Application is disabled to change netOpen.");
256
257
  return;
257
258
  }
258
259
  if (!this.data)
@@ -260,7 +261,7 @@ class ne {
260
261
  return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
261
262
  netOpen: e ? 1 : 2
262
263
  /* disable */
263
- }), this.data.netOpen = e, l.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.event.emit("data:updated:netOpen", {
264
+ }), this.data.netOpen = e, h.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.event.emit("data:updated:netOpen", {
264
265
  netOpen: e
265
266
  }), e;
266
267
  });
@@ -279,14 +280,14 @@ class ne {
279
280
  updateSocketResourceToken(e) {
280
281
  }
281
282
  getService() {
282
- return K(_.apiRoot, () => this.requestToken, (e, t) => {
283
+ return K(S.apiRoot, () => this.requestToken, (e, t) => {
283
284
  const i = new Error("data request error", {
284
285
  cause: {
285
286
  status: e,
286
287
  message: t
287
288
  }
288
289
  });
289
- l.error(i), this.event.emit("error", i);
290
+ h.error(i), this.event.emit("error", i);
290
291
  });
291
292
  }
292
293
  addEventListener(e, t, i = !1) {
@@ -307,85 +308,85 @@ class ne {
307
308
  * @returns Promise<false|Message[]>
308
309
  */
309
310
  fetchMessages() {
310
- return c(this, arguments, function* (e = {}) {
311
- var i, o;
311
+ return l(this, arguments, function* (e = {}) {
312
+ var i, s;
312
313
  const t = {
313
314
  pageSize: this._loadingMessagesSize
314
315
  };
315
316
  if (this._id) {
316
- e = u(u({}, t), e), this.event.emit("history:loading"), l.log("history:loading"), (e.endTime || e.startTime) && (this.event.emit("history:moreloading", e), l.log("history:moreloading", e)), this._loadingMessages = !0;
317
- const n = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), h = n.map((a, s) => {
318
- var g, w;
319
- let m = [], I;
320
- return typeof ((g = a.agentHistoryList) == null ? void 0 : g.agentList) == "string" && (m = JSON.parse(a.agentHistoryList.agentList).reduce((v, r) => (v[r.data.step - 1] || (v[r.data.step - 1] = []), v[r.data.step - 1].push(r), v), [])), s === n.length - 1 && (I = (w = this.data) == null ? void 0 : w.lastRelatedQuestion), y(u({}, a), {
321
- actions: m,
322
- relatedQuesion: I,
317
+ e = u(u({}, t), e), this.event.emit("history:loading"), h.log("history:loading"), (e.endTime || e.startTime) && (this.event.emit("history:moreloading", e), h.log("history:moreloading", e)), this._loadingMessages = !0;
318
+ const a = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), c = a.map((n, o) => {
319
+ var v, w;
320
+ let p = [], g;
321
+ return typeof ((v = n.agentHistoryList) == null ? void 0 : v.agentList) == "string" && (p = JSON.parse(n.agentHistoryList.agentList).reduce((I, d) => (I[d.data.step - 1] || (I[d.data.step - 1] = []), I[d.data.step - 1].push(d), I), [])), o === a.length - 1 && (g = (w = this.data) == null ? void 0 : w.lastRelatedQuestion), y(u({}, n), {
322
+ actions: p,
323
+ relatedQuesion: g,
323
324
  fromHistory: !0
324
325
  });
325
326
  });
326
327
  if (!e.endTime && !e.startTime) {
327
- const a = this.messages[this.messages.length - 1];
328
- a && !a.relatedQuesion && (a.relatedQuesion = (i = this.data) == null ? void 0 : i.lastRelatedQuestion), this.messages = [...h, ...this.messages];
328
+ const n = this.messages[this.messages.length - 1];
329
+ n && !n.relatedQuesion && (n.relatedQuesion = (i = this.data) == null ? void 0 : i.lastRelatedQuestion), this.messages = [...c, ...this.messages];
329
330
  } else if (e.endTime && !e.startTime) {
330
- const a = this.messages.findIndex((s) => s.ts === e.endTime);
331
- this.messages.splice(0, a + 1), this.messages = [...h, ...this.messages];
331
+ const n = this.messages.findIndex((o) => o.ts === e.endTime);
332
+ this.messages.splice(0, n + 1), this.messages = [...c, ...this.messages];
332
333
  } else if (!e.endTime && e.startTime) {
333
- const a = this.messages.findLastIndex((s) => s.ts === e.endTime);
334
- this.messages.splice(a, this.messages.length - a - 1), this.messages = [...this.messages, ...h];
334
+ const n = this.messages.findLastIndex((o) => o.ts === e.endTime);
335
+ this.messages.splice(n, this.messages.length - n - 1), this.messages = [...this.messages, ...c];
335
336
  } else {
336
- const a = this.messages.findIndex((m) => m.ts === e.endTime), s = this.messages.findLastIndex((m) => m.ts === e.endTime);
337
- this.messages.splice(a, s - a + 1, ...h);
337
+ const n = this.messages.findIndex((p) => p.ts === e.endTime), o = this.messages.findLastIndex((p) => p.ts === e.endTime);
338
+ this.messages.splice(n, o - n + 1, ...c);
338
339
  }
339
- const d = this.messages[this.messages.length - 1];
340
- return d && !d.relatedQuesion && (d.relatedQuesion = (o = this.data) == null ? void 0 : o.lastRelatedQuestion), this.event.emit("history:moreloaded", h), l.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, h;
340
+ const r = this.messages[this.messages.length - 1];
341
+ return r && !r.relatedQuesion && (r.relatedQuesion = (s = this.data) == null ? void 0 : s.lastRelatedQuestion), this.event.emit("history:moreloaded", c), h.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, c;
341
342
  } else {
342
- const n = new Error(
343
+ const a = new Error(
343
344
  "session id is not defined"
344
345
  /* IdUndefined */
345
346
  // {
346
347
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
347
348
  // }
348
349
  );
349
- return l.error(n), this.event.emit("error", n), !1;
350
+ return h.error(a), this.event.emit("error", a), !1;
350
351
  }
351
352
  });
352
353
  }
353
354
  rename(e) {
354
- return c(this, null, function* () {
355
+ return l(this, null, function* () {
355
356
  if (!this._id)
356
357
  throw Error("session is not created yet");
357
358
  if (e.trim() === "")
358
359
  throw Error("name is empty");
359
360
  return this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
360
361
  name: e
361
- }), l.log("data:updated", this.messages), this.event.emit("data:updated", this.data), this.event.emit("data:updated:name", {
362
+ }), h.log("data:updated", this.messages), this.event.emit("data:updated", this.data), this.event.emit("data:updated:name", {
362
363
  name: e
363
364
  }), !0;
364
365
  });
365
366
  }
366
367
  pushMessage(e) {
367
- return c(this, null, function* () {
368
- this.messages.push(e), l.log("history:add", e), this.event.emit("history:add", e), this.event.emit("message:new", e), this.event.emit("message:updated", e);
368
+ return l(this, null, function* () {
369
+ this.messages.push(e), h.log("history:add", e), this.event.emit("history:add", e), this.event.emit("message:new", e), this.event.emit("message:updated", e);
369
370
  });
370
371
  }
371
372
  reloadSessionInfo() {
372
373
  if (!this._id)
373
374
  throw Error("session is not created yet");
374
- return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => c(this, null, function* () {
375
+ return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => l(this, null, function* () {
375
376
  if (this.data = e, !this._id)
376
377
  throw Error("session is not created yet");
377
- return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), l.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.data;
378
- })).then(() => c(this, null, function* () {
378
+ return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), h.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.data;
379
+ })).then(() => l(this, null, function* () {
379
380
  return this.messages = [], yield this.fetchMessages(), this._dataLoading = !1, this._dataLoaded = !0, this.event.emit("data:loaded", this.data), this;
380
381
  }));
381
382
  }
382
383
  reload() {
383
- return c(this, null, function* () {
384
+ return l(this, null, function* () {
384
385
  return yield this.reloadSessionInfo(), this.socket ? (this.inactivate(!0), this.activate()) : (this.inactivate(!0), this.activate());
385
386
  });
386
387
  }
387
388
  delete() {
388
- return c(this, null, function* () {
389
+ return l(this, null, function* () {
389
390
  if (!this.id) {
390
391
  const e = new Error(
391
392
  "session id is not defined"
@@ -394,13 +395,13 @@ class ne {
394
395
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
395
396
  // }
396
397
  );
397
- return l.error(e), this.event.emit("error", e), !1;
398
+ return h.error(e), this.event.emit("error", e), !1;
398
399
  }
399
400
  return this.event.emit("data:deleting"), yield this.cvforceApi.delSessionsId(this.applicationId, this.id), this.event.emit("data:deleted"), this.destroy();
400
401
  });
401
402
  }
402
403
  activate() {
403
- return c(this, null, function* () {
404
+ return l(this, null, function* () {
404
405
  var t;
405
406
  if (this.activing)
406
407
  return new Promise((i) => {
@@ -410,22 +411,22 @@ class ne {
410
411
  });
411
412
  if (!this._id)
412
413
  throw Error("session is not avalible. id is undefined");
413
- l.log("activate", this._id), this.activing = !0;
414
+ h.log("activate", this._id), this.activing = !0;
414
415
  const e = this._id;
415
- return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((i) => c(this, null, function* () {
416
+ return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((i) => l(this, null, function* () {
416
417
  return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e), i;
417
- })), 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, (t = this.socket) != null && t.connected || (this.frozen = !0), yield this.prepareConnection(), this.event.emit("activated", this.socket), l.log("activated", this._id), this._sessionActive = !0, this.activing = !1, this;
418
+ })), 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, (t = this.socket) != null && t.connected || (this.frozen = !0), yield this.prepareConnection(), this.event.emit("activated", this.socket), h.log("activated", this._id), this._sessionActive = !0, this.activing = !1, this;
418
419
  });
419
420
  }
420
421
  inactivate(e = !1) {
421
- return this.socket ? (l.log("inactivate", this.id), this._frozen ? (e || (this.inactivating = !0), this.activing = !1, !0) : (this.socket.close(), this.socket = null, this.event.emit("inactivated"), this._sessionActive = !1, this.activing = !1, !0)) : (this.activing = !1, !0);
422
+ return this.socket ? (h.log("inactivate", this.id), this._frozen ? (e || (this.inactivating = !0), this.activing = !1, !0) : (this.socket.close(), this.socket = null, this.event.emit("inactivated"), this._sessionActive = !1, this.activing = !1, !0)) : (this.activing = !1, !0);
422
423
  }
423
424
  /**
424
425
  * 会话标题摘要开关。应用详情返回布尔值时用该值,字段缺失时默认开启。
425
426
  */
426
427
  resolveEnableTitleSummary() {
427
- var t, i, o;
428
- const e = (o = (i = (t = this.application) == null ? void 0 : t.arrange) == null ? void 0 : i[0]) == null ? void 0 : o.autoGenerateSessionTitle;
428
+ var t, i, s;
429
+ const e = (s = (i = (t = this.application) == null ? void 0 : t.arrange) == null ? void 0 : i[0]) == null ? void 0 : s.autoGenerateSessionTitle;
429
430
  return typeof e == "boolean" ? e : !0;
430
431
  }
431
432
  //TODO: remove mcp server
@@ -441,236 +442,269 @@ class ne {
441
442
  * 图片地址放进 send 的 imageContent。
442
443
  */
443
444
  upload(e) {
444
- return c(this, null, function* () {
445
+ return l(this, null, function* () {
445
446
  if (!this._id)
446
447
  throw Error("session is not avalible. id is undefined");
447
- return G(this.cvforceApi, this._id, e);
448
+ return J(this.cvforceApi, this._id, e);
448
449
  });
449
450
  }
450
451
  send(i) {
451
- return c(this, arguments, function* (e, t = {
452
- stream: _.stream
452
+ return l(this, arguments, function* (e, t = {
453
+ stream: S.stream
453
454
  }) {
454
- var A, v;
455
+ var k, I;
455
456
  try {
456
457
  this.validateConfigs();
457
- } catch (r) {
458
- l.report(r);
458
+ } catch (d) {
459
+ h.report(d);
459
460
  }
460
461
  const {
461
- stream: o = _.stream,
462
- referenceFirst: n = !1
463
- } = t, h = e.uuid || Q(), d = ((A = e.text) == null ? void 0 : A.trim()) || "", a = e.files || [], s = e.fileIds, m = e.imageContent;
464
- if (!d && !a.length && !(s != null && s.length)) {
465
- const r = new Error(
462
+ stream: s = S.stream,
463
+ referenceFirst: a = !1
464
+ } = t, c = e.uuid || Q(), r = ((k = e.text) == null ? void 0 : k.trim()) || "", n = e.files || [], o = e.fileIds, p = e.imageContent;
465
+ if (!r && !n.length && !(o != null && o.length)) {
466
+ const d = new Error(
466
467
  "failed at sending empty content"
467
468
  /* SendingEmptyContent */
468
469
  // {
469
470
  // cause: SESSION_ERROR_CAUSE[SessionErrors.SendingEmptyContent],
470
471
  // }
471
472
  );
472
- return l.error(r), this.event.emit("error", r), !1;
473
+ return h.error(d), this.event.emit("error", d), !1;
473
474
  }
474
475
  if (!this._id) {
475
- const r = new Error(
476
+ const d = new Error(
476
477
  "session id is not defined"
477
478
  /* IdUndefined */
478
479
  // {
479
480
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
480
481
  // }
481
482
  );
482
- return l.error(r), this.event.emit("error", r), !1;
483
+ return h.error(d), this.event.emit("error", d), !1;
483
484
  }
484
485
  if (this.frozen) {
485
- const r = new Error(
486
+ const d = new Error(
486
487
  "session is frozen"
487
488
  /* Frozen */
488
489
  // {
489
490
  // cause: { code: -1, message: 'session is frozen.' },
490
491
  // }
491
492
  );
492
- return l.error(r), this.event.emit("error", r), !1;
493
+ return h.error(d), this.event.emit("error", d), !1;
493
494
  }
494
- this.frozen = !0, this._stopAnswer = !1, l.log("send", {
495
+ this.frozen = !0, this._stopAnswer = !1, h.log("send", {
495
496
  sessionId: this._id,
496
- stream: o,
497
- contentLength: d.length,
498
- fileCount: a.length,
499
- fileIdCount: (v = s == null ? void 0 : s.length) != null ? v : 0
497
+ stream: s,
498
+ contentLength: r.length,
499
+ fileCount: n.length,
500
+ fileIdCount: (I = o == null ? void 0 : o.length) != null ? I : 0
500
501
  });
501
- const I = {
502
+ const g = {
502
503
  sessionId: this.id,
503
504
  role: 2,
504
- content: d,
505
- messageId: h,
505
+ content: r,
506
+ messageId: c,
506
507
  reference: [],
507
508
  searchReference: [],
508
509
  messageTime: "",
509
- files: a,
510
- imageContent: m
511
- }, g = {
512
- content: d,
513
- stream: o,
514
- requestId: h,
515
- fileIds: s,
516
- referenceFirst: n,
517
- imageContent: m
510
+ files: n,
511
+ imageContent: p
512
+ }, v = {
513
+ content: r,
514
+ stream: s,
515
+ requestId: c,
516
+ fileIds: o,
517
+ referenceFirst: a,
518
+ imageContent: p
518
519
  };
519
- this.pushMessage(I), this.event.emit("question:content", d);
520
- const w = (r) => c(this, null, function* () {
521
- if (r.role === S.Robot) {
520
+ this.pushMessage(g), this.event.emit("question:content", r);
521
+ const w = (d) => l(this, null, function* () {
522
+ if (d.role === A.Robot) {
522
523
  this.removeEventListener("answer:finish", w);
523
524
  try {
524
- switch (this._id && r.content && r.index && r.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
525
- finialMessage: r.content
525
+ if (this._id && d.content && d.index && d.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
526
+ finialMessage: d.content
526
527
  }), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
527
- finialMessage: r.content
528
- })), yield this.fillMessageReference(r), r.code) {
529
- case 11915:
530
- r.content = "请您换个问题问我吧~", r.index = -1, r.isSensitive = !0, r.isLoading = !1, r.sessionId = this._id || "", this.event.emit("message:error", r);
531
- break;
532
- case 11916:
533
- r.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", r.index = -1, r.isSensitive = !0, r.isLoading = !1, r.sessionId = this._id || "", this.event.emit("message:error", r);
534
- break;
535
- case 200:
536
- r.index = -1, r.isLoading = !1;
537
- break;
538
- default:
539
- r.content = r.message || "未知错误", r.isLoading = !1, r.index = -1, this.event.emit("message:error", r);
528
+ finialMessage: d.content
529
+ })), yield this.fillMessageReference(d), this.finalizeAnswerMessage(d), this.getRelatedQuesion(d), d.isLoading = !1, this.isEmptyQuotaRejection(d)) {
530
+ const _ = this.messages[this.messages.length - 1];
531
+ _ && _.role === A.Robot && !_.content && !(_.actions && _.actions.length > 0) && this.messages.pop(), this.event.emit("message:updated", d);
532
+ } else {
533
+ let _ = this.messages[this.messages.length - 1];
534
+ if (_ && _.role === A.Robot) {
535
+ const x = u(u({}, _), d);
536
+ this.messages[this.messages.length - 1] = x, this.event.emit("message:updated", x);
537
+ } else
538
+ this.pushMessage(d);
540
539
  }
541
- this.getRelatedQuesion(r), r.isLoading = !1;
542
- let k = this.messages[this.messages.length - 1];
543
- if (k && k.role === S.Robot) {
544
- const E = u(u({}, k), r);
545
- this.messages[this.messages.length - 1] = E, this.event.emit("message:updated", E);
546
- } else
547
- this.pushMessage(r);
548
540
  } finally {
549
541
  this.frozen = !1;
550
542
  }
551
543
  }
552
544
  });
553
- return this.addEventListener("answer:finish", w), this.sendQuestion(g, y(u({}, t), {
554
- stream: o,
555
- referenceFirst: n
556
- })), I;
545
+ return this.addEventListener("answer:finish", w), this.sendQuestion(v, y(u({}, t), {
546
+ stream: s,
547
+ referenceFirst: a
548
+ })), g;
557
549
  });
558
550
  }
551
+ isEmptyQuotaRejection(e) {
552
+ return (e.code === E.InsufficientBalance || e.code === E.QuotaCheckFailed) && !e.content && !(e.actions && e.actions.length > 0);
553
+ }
554
+ finalizeAnswerMessage(e) {
555
+ switch (e.code) {
556
+ case 11915:
557
+ e.content = "请您换个问题问我吧~", e.index = -1, e.isSensitive = !0, e.isLoading = !1, e.sessionId = this._id || "", this.event.emit("message:error", e);
558
+ break;
559
+ case 11916:
560
+ e.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", e.index = -1, e.isSensitive = !0, e.isLoading = !1, e.sessionId = this._id || "", this.event.emit("message:error", e);
561
+ break;
562
+ case E.ConversionFailed: {
563
+ e.index = -1, e.isLoading = !1, e.sessionId = e.sessionId || this._id || "", this.event.emit("message:error", e);
564
+ const t = {
565
+ code: E.ConversionFailed,
566
+ message: e.message || T[E.ConversionFailed],
567
+ usageExtension: e.usageExtension,
568
+ messageId: e.messageId || void 0,
569
+ sessionId: this._id || e.sessionId || void 0
570
+ }, i = new Error("usage error", {
571
+ cause: t
572
+ });
573
+ h.error("usage error", t), this.event.emit("error", i);
574
+ break;
575
+ }
576
+ case E.InsufficientBalance:
577
+ case E.QuotaCheckFailed: {
578
+ e.index = -1, e.isLoading = !1, e.sessionId = e.sessionId || this._id || "", this.event.emit("message:error", e);
579
+ const t = e.code, i = {
580
+ code: t,
581
+ message: e.message || T[t],
582
+ messageId: e.messageId || void 0,
583
+ sessionId: this._id || e.sessionId || void 0
584
+ }, s = new Error("usage error", {
585
+ cause: i
586
+ });
587
+ h.error("usage error", i), this.event.emit("error", s);
588
+ break;
589
+ }
590
+ case 200:
591
+ e.index = -1, e.isLoading = !1;
592
+ break;
593
+ default:
594
+ e.content = e.message || "未知错误", e.isLoading = !1, e.index = -1, this.event.emit("message:error", e);
595
+ }
596
+ }
559
597
  currentUserQuestion() {
560
598
  for (let e = this.messages.length - 1; e >= 0; e -= 1) {
561
599
  const t = this.messages[e];
562
- if (t.role === S.User)
600
+ if (t.role === A.User)
563
601
  return t.content || "";
564
602
  }
565
603
  return "";
566
604
  }
567
605
  getRelatedQuesion(e) {
568
- return c(this, null, function* () {
569
- var d, a;
570
- const t = (d = this.application) == null ? void 0 : d.arrange[0];
606
+ return l(this, null, function* () {
607
+ var r, n;
608
+ const t = (r = this.application) == null ? void 0 : r.arrange[0];
571
609
  if (!e.content || e.isSensitive || e.code !== 200)
572
- return l.log("related:skip", {
610
+ return h.log("related:skip", {
573
611
  code: e.code,
574
612
  sensitive: !!e.isSensitive,
575
613
  hasContent: !!e.content
576
614
  }), e;
577
- const i = q(t == null ? void 0 : t.recommendQuestionOpen, t == null ? void 0 : t.relatedQuestionApp, e.content, this.currentUserQuestion());
615
+ const i = N(t == null ? void 0 : t.recommendQuestionOpen, t == null ? void 0 : t.relatedQuestionApp, e.content, this.currentUserQuestion());
578
616
  if (i === null)
579
- return l.log("related:skip", {
617
+ return h.log("related:skip", {
580
618
  recommendQuestionOpen: t == null ? void 0 : t.recommendQuestionOpen
581
619
  }), e;
582
- l.log("related:request", i), this.event.emit("message:relatedQuesion:loading", e);
583
- const o = yield this.cvforceApi.postGeneques({
620
+ h.log("related:request", i), this.event.emit("message:relatedQuesion:loading", e);
621
+ const s = yield this.cvforceApi.postGeneques({
584
622
  content: i
585
- }).catch((s) => (l.error("related:failed", s), null));
586
- if (l.log("related:response", o), !o) {
587
- const s = [], m = y(u({}, e), {
588
- relatedQuesion: s,
623
+ }).catch((o) => (h.error("related:failed", o), null));
624
+ if (h.log("related:response", s), !s) {
625
+ const o = [], p = y(u({}, e), {
626
+ relatedQuesion: o,
589
627
  index: -3,
590
628
  isLoading: !1
591
629
  });
592
630
  return this.event.emit("message:relatedQuesion:loaded", {
593
- relatedQuesion: s
594
- }), this.event.emit("message:updated", m), m;
631
+ relatedQuesion: o
632
+ }), this.event.emit("message:updated", p), p;
595
633
  }
596
- const n = (a = o.content) == null ? void 0 : a.split("*").map(F).map(N).filter((s) => !!s);
597
- e.relatedQuesion = n, e.index = -3, e.isLoading = !1;
598
- const h = y(u({}, e), {
599
- relatedQuesion: n,
634
+ const a = (n = s.content) == null ? void 0 : n.split("*").map(U).map(B).filter((o) => !!o);
635
+ e.relatedQuesion = a, e.index = -3, e.isLoading = !1;
636
+ const c = y(u({}, e), {
637
+ relatedQuesion: a,
600
638
  index: -3,
601
639
  isLoading: !1
602
640
  });
603
- return this.event.emit("message:relatedQuesion:loaded", h), this.event.emit("message:updated", h), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
604
- lastRelatedQuestion: n
605
- }), h;
641
+ return this.event.emit("message:relatedQuesion:loaded", c), this.event.emit("message:updated", c), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
642
+ lastRelatedQuestion: a
643
+ }), c;
606
644
  });
607
645
  }
608
646
  fillMessageReference(e) {
609
- return c(this, null, function* () {
647
+ return l(this, null, function* () {
610
648
  try {
611
649
  const t = yield Promise.all(e.reference.map((i) => this.cvforceApi.getKnowledgeIdFileId(i.knowledgeId, i.docId)));
612
- e.reference = e.reference.map((i, o) => u(u({}, i), t[o]));
650
+ e.reference = e.reference.map((i, s) => u(u({}, i), t[s]));
613
651
  } catch (t) {
614
- l.error("Failed to fill message reference:", t);
652
+ h.error("Failed to fill message reference:", t);
615
653
  }
616
654
  return this.event.emit("message:updated", e), e;
617
655
  });
618
656
  }
619
657
  answerAgain() {
620
- return c(this, arguments, function* (e = {
621
- stream: _.stream
658
+ return l(this, arguments, function* (e = {
659
+ stream: S.stream
622
660
  }) {
623
- var a;
661
+ var n;
624
662
  const t = this.messages[this.messages.length - 1];
625
663
  if (!t)
626
664
  throw Error("message list is empty");
627
- this.frozen = !0, this._stopAnswer = !1, l.log("answerAgain", {
665
+ this.frozen = !0, this._stopAnswer = !1, h.log("answerAgain", {
628
666
  sessionId: this._id,
629
- stream: (a = e.stream) != null ? a : _.stream
667
+ stream: (n = e.stream) != null ? n : S.stream
630
668
  });
631
669
  const {
632
- stream: i = _.stream
670
+ stream: i = S.stream
633
671
  } = e;
634
- let o;
635
- t.role !== S.User && (o = e.oldMessageId || t.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", t), this.event.emit("history:remove", t));
636
- const n = this.messages[this.messages.length - 1], h = {
637
- content: n.content,
638
- fileIds: n.fileIds,
639
- requestId: n.messageId,
672
+ let s;
673
+ t.role !== A.User && (s = e.oldMessageId || t.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", t), this.event.emit("history:remove", t));
674
+ const a = this.messages[this.messages.length - 1], c = {
675
+ content: a.content,
676
+ fileIds: a.fileIds,
677
+ requestId: a.messageId,
640
678
  stream: i,
641
- oldMessageId: o
642
- }, d = (s) => c(this, null, function* () {
643
- if (s.role === S.Robot && s.index === -1)
679
+ oldMessageId: s
680
+ }, r = (o) => l(this, null, function* () {
681
+ if (o.role === A.Robot && o.index === -1)
644
682
  try {
645
- switch (this._id && s.content && s.index && s.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
646
- finialMessage: s.content
683
+ if (this._id && o.content && o.index && o.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
684
+ finialMessage: o.content
647
685
  }), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", {
648
- finialMessage: s.content
649
- })), yield this.fillMessageReference(s), s.code) {
650
- case 11915:
651
- s.content = "请您换个问题问我吧~", s.index = -1, s.isSensitive = !0, s.isLoading = !1, s.sessionId = this._id || "", this.event.emit("message:error", s);
652
- break;
653
- case 11916:
654
- s.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", s.index = -1, s.isSensitive = !0, s.isLoading = !1, s.sessionId = this._id || "", this.event.emit("message:error", s);
655
- break;
656
- case 200:
657
- s.index = -1, s.isLoading = !1;
658
- break;
659
- default:
660
- s.content = s.message || "未知错误", s.isLoading = !1, s.index = -1, this.event.emit("message:error", s);
686
+ finialMessage: o.content
687
+ })), yield this.fillMessageReference(o), this.finalizeAnswerMessage(o), this.getRelatedQuesion(o), this.isEmptyQuotaRejection(o)) {
688
+ const p = this.messages[this.messages.length - 1];
689
+ p && p.role === A.Robot && !p.content && !(p.actions && p.actions.length > 0) && this.messages.pop(), this.event.emit("message:updated", o);
690
+ } else {
691
+ const p = this.messages[this.messages.length - 1];
692
+ if (p && p.role === A.Robot) {
693
+ const g = u(u({}, p), o);
694
+ this.messages[this.messages.length - 1] = g, this.event.emit("message:updated", g);
695
+ }
661
696
  }
662
- this.getRelatedQuesion(s);
663
697
  } finally {
664
- this.removeEventListener("message:content", d), this.frozen = !1;
698
+ this.removeEventListener("message:content", r), this.frozen = !1;
665
699
  }
666
700
  });
667
- this.addEventListener("message:content", d), this.sendQuestion(h, y(u({}, e), {
701
+ this.addEventListener("message:content", r), this.sendQuestion(c, y(u({}, e), {
668
702
  stream: i
669
703
  }));
670
704
  });
671
705
  }
672
706
  setTop(e) {
673
- return c(this, null, function* () {
707
+ return l(this, null, function* () {
674
708
  if (!this._id)
675
709
  throw Error("session is not avalible. id is undefined");
676
710
  this.data = yield this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
@@ -701,7 +735,7 @@ class ne {
701
735
  // 消息时间
702
736
  content: "",
703
737
  // 消息内容
704
- role: S.System,
738
+ role: A.System,
705
739
  // 消息角色为系统消息
706
740
  sessionId: this.id || "",
707
741
  // 会话 ID
@@ -714,28 +748,28 @@ class ne {
714
748
  if (e.index === -2)
715
749
  return;
716
750
  if (e.actions) {
717
- const o = e.actions[((i = e.actions) == null ? void 0 : i.length) - 1];
718
- if (o) {
719
- const n = o[o.length - 1];
720
- if (n && (n.type === "BEFORE_CALL" || n.type === "FUNCTION_CALL")) {
751
+ const s = e.actions[((i = e.actions) == null ? void 0 : i.length) - 1];
752
+ if (s) {
753
+ const a = s[s.length - 1];
754
+ if (a && (a.type === "BEFORE_CALL" || a.type === "FUNCTION_CALL")) {
721
755
  const {
722
- pluginName: h,
723
- pluginCode: d,
724
- funcCode: a,
725
- funcName: s
726
- } = n.data;
727
- o.push({
756
+ pluginName: c,
757
+ pluginCode: r,
758
+ funcCode: n,
759
+ funcName: o
760
+ } = a.data;
761
+ s.push({
728
762
  data: {
729
- pluginName: h,
730
- pluginCode: d,
731
- funcCode: a,
732
- funcName: s,
733
- step: n.data.step + 1
763
+ pluginName: c,
764
+ pluginCode: r,
765
+ funcCode: n,
766
+ funcName: o,
767
+ step: a.data.step + 1
734
768
  },
735
769
  type: "ABORT"
736
770
  }), this.event.emit("message:action", {
737
771
  messageId: e.messageId,
738
- action: o
772
+ action: s
739
773
  });
740
774
  }
741
775
  }
@@ -751,12 +785,14 @@ class ne {
751
785
  * @param targetMessage - 要更新的目标聊天消息对象。
752
786
  */
753
787
  handleStreamAnswer(e, t) {
754
- var h;
755
- if (!e.data)
788
+ var c;
789
+ if (!e.data) {
790
+ e.code != null && e.code !== 200 && (t.code = e.code, t.message = e.message, t.index = -1);
756
791
  return;
757
- const i = e.data.type, o = e.data.payload;
792
+ }
793
+ const i = e.data.type, s = e.data.payload;
758
794
  t.isReplying = !0;
759
- const n = () => {
795
+ const a = () => {
760
796
  if (!t.searchingStatus) {
761
797
  t.searchingStatus = "start";
762
798
  return;
@@ -766,25 +802,28 @@ class ne {
766
802
  return;
767
803
  }
768
804
  };
769
- if (i === "error" && (t.index = -1), i === "beforeSearch" && n(), i === "searchOver" && n(), i === "search" && t.searchReference.push(o), i === "thinking") {
805
+ if (i === "error" && (t.index = -1), i === "beforeSearch" && a(), i === "searchOver" && a(), i === "search" && t.searchReference.push(s), i === "thinking") {
770
806
  t.thinkingStatus = "start";
771
- const a = o.choices[0];
772
- t.reasoningContent = (t.reasoningContent || "") + ((h = a.delta.reasoningContent) != null ? h : "");
807
+ const n = s.choices[0];
808
+ t.reasoningContent = (t.reasoningContent || "") + ((c = n.delta.reasoningContent) != null ? c : "");
773
809
  }
774
810
  if (i === "thinkingOver") {
775
811
  t.thinkingStatus = "end";
776
- const d = o.thinkingSecs;
777
- t.thinkingSecs = d;
812
+ const r = s.thinkingSecs;
813
+ t.thinkingSecs = r;
778
814
  }
779
815
  if (i === "text") {
780
- const a = o.choices[0];
781
- t.content = (t.content || "") + a.delta.content;
816
+ const n = s.choices[0];
817
+ t.content = (t.content || "") + n.delta.content;
818
+ }
819
+ if (i === "reference" && (t.reference = s), i === "updateSession" && s && this.data && (this.data = u(u({}, this.data), s), this.event.emit("data:updated", this.data)), i === "result") {
820
+ const r = s == null ? void 0 : s.messageId, n = s == null ? void 0 : s.llmModel, o = s == null ? void 0 : s.messageTime;
821
+ r !== void 0 && (t.messageId = r), n !== void 0 && (t.llmModel = n), o !== void 0 && (t.messageTime = o), (s == null ? void 0 : s.usageExtension) != null && (t.usageExtension = s.usageExtension);
782
822
  }
783
- if (i === "reference" && (t.reference = o), i === "updateSession" && o && this.data && (this.data = u(u({}, this.data), o), this.event.emit("data:updated", this.data)), i === "result") {
784
- const d = o.messageId, a = o.llmModel, s = o.messageTime;
785
- t.messageId = d, t.llmModel = a, t.messageTime = s;
823
+ if (i === "over" && (t.index = -1), e.code != null) {
824
+ const r = t.code != null && t.code !== 200;
825
+ (e.code !== 200 || !r) && (t.code = e.code, t.message = e.message);
786
826
  }
787
- i === "over" && (t.index = -1), e.code != null && (t.code = e.code, t.message = e.message);
788
827
  }
789
828
  /**
790
829
  * 异步提交评论信息
@@ -795,7 +834,7 @@ class ne {
795
834
  * 获取当前会话的产出物。没有产出物时返回空数组。
796
835
  */
797
836
  getArtifacts() {
798
- return c(this, null, function* () {
837
+ return l(this, null, function* () {
799
838
  var t;
800
839
  if (!this._applicationId || !this._id)
801
840
  throw Error("session is not avalible. id is undefined");
@@ -804,11 +843,11 @@ class ne {
804
843
  });
805
844
  }
806
845
  postMessageComment(e) {
807
- return c(this, null, function* () {
846
+ return l(this, null, function* () {
808
847
  if (!this._applicationId || !this._id)
809
848
  throw Error("session is not avalible. id is undefined");
810
849
  return this.cvforceApi.postComments(this._applicationId, this._id, e).then((t) => {
811
- const i = this.messages.find((o) => o.messageId === e.messageId);
850
+ const i = this.messages.find((s) => s.messageId === e.messageId);
812
851
  return i && (i.comment = e, this.event.emit("message:comment", i)), t;
813
852
  });
814
853
  });
@@ -836,7 +875,7 @@ class ne {
836
875
  }
837
876
  }
838
877
  export {
839
- C as MAX_KNOWLEDGES_AND_FILES_COUNT,
840
- j as SessionErrors,
841
- ne as default
878
+ V as MAX_KNOWLEDGES_AND_FILES_COUNT,
879
+ W as SessionErrors,
880
+ re as default
842
881
  };