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