@glodon-aiot/bot-client-sdk 3.26.0-alpha.8 → 3.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +284 -254
  2. package/dist/es/AgentSession.mjs +267 -197
  3. package/dist/es/BotClient.mjs +200 -123
  4. package/dist/es/Config.mjs +55 -88
  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/Session.mjs +338 -284
  9. package/dist/es/api/index.mjs +31 -30
  10. package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +201 -0
  11. package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +356 -0
  12. package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +350 -0
  13. package/dist/es/builtinTools/browserRuntime/assets.mjs +9 -0
  14. package/dist/es/builtinTools/browserRuntime/loadBash.mjs +17 -0
  15. package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +96 -0
  16. package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +242 -0
  17. package/dist/es/builtinTools/command.mjs +56 -0
  18. package/dist/es/builtinTools/constants.mjs +17 -0
  19. package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +266 -0
  20. package/dist/es/builtinTools/overview.mjs +77 -0
  21. package/dist/es/builtinTools/paths.mjs +70 -0
  22. package/dist/es/builtinTools/platformSkills.mjs +248 -0
  23. package/dist/es/builtinTools/schemas.mjs +261 -0
  24. package/dist/es/builtinTools/tools.mjs +595 -0
  25. package/dist/es/index.mjs +66 -64
  26. package/dist/es/localFiles/adapters/FileSnapshotAdapter.mjs +160 -0
  27. package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +185 -113
  28. package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +127 -137
  29. package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +132 -97
  30. package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +32 -28
  31. package/dist/es/localFiles/constants.mjs +24 -28
  32. package/dist/es/localFiles/createLocalFilesPlugin.mjs +53 -52
  33. package/dist/es/localFiles/errors.mjs +44 -35
  34. package/dist/es/localFiles/host/connectHostBridge.mjs +43 -35
  35. package/dist/es/localFiles/host/protocol.mjs +27 -13
  36. package/dist/es/localFiles/host/sandboxClient.mjs +494 -0
  37. package/dist/es/localFiles/host/sandboxFrames.mjs +21 -0
  38. package/dist/es/localFiles/preview.mjs +33 -0
  39. package/dist/es/localFiles/schemas.mjs +40 -86
  40. package/dist/es/localFiles/validate.mjs +37 -37
  41. package/dist/es/localFiles/workspace/LocalWorkspace.mjs +176 -90
  42. package/dist/es/localFiles/workspace/WorkspaceFiles.mjs +246 -0
  43. package/dist/es/localMcp/client.mjs +234 -0
  44. package/dist/es/localMcp/endpoints.mjs +94 -0
  45. package/dist/es/localMcp/register.mjs +207 -0
  46. package/dist/es/localMcp/registry.mjs +43 -0
  47. package/dist/es/package.json.mjs +49 -60
  48. package/dist/es/sessionUpload.mjs +49 -0
  49. package/dist/es/utils.mjs +24 -11
  50. package/dist/lib/index.js +382 -10
  51. package/dist/src/AgentSession.d.ts +13 -0
  52. package/dist/src/BotClient.d.ts +13 -1
  53. package/dist/src/Config.d.ts +7 -8
  54. package/dist/src/EventBus.d.ts +3 -0
  55. package/dist/src/Logger.d.ts +15 -8
  56. package/dist/src/PayloadTypes.d.ts +2 -0
  57. package/dist/src/Session.d.ts +19 -2
  58. package/dist/src/api/index.d.ts +2 -2
  59. package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +26 -0
  60. package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +67 -0
  61. package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +51 -0
  62. package/dist/src/builtinTools/browserRuntime/assets.d.ts +13 -0
  63. package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +3 -0
  64. package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +44 -0
  65. package/dist/src/builtinTools/command.d.ts +35 -0
  66. package/dist/src/builtinTools/constants.d.ts +25 -0
  67. package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +11 -0
  68. package/dist/src/builtinTools/overview.d.ts +5 -0
  69. package/dist/src/builtinTools/paths.d.ts +16 -0
  70. package/dist/src/builtinTools/platformSkills.d.ts +92 -0
  71. package/dist/src/builtinTools/schemas.d.ts +3 -0
  72. package/dist/src/builtinTools/tools.d.ts +73 -0
  73. package/dist/src/constant.d.ts +1 -1
  74. package/dist/src/index.d.ts +2 -1
  75. package/dist/src/localFiles/adapters/FileSnapshotAdapter.d.ts +24 -0
  76. package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +4 -1
  77. package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +30 -14
  78. package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +4 -1
  79. package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +22 -0
  80. package/dist/src/localFiles/adapters/fsaTypes.d.ts +2 -0
  81. package/dist/src/localFiles/constants.d.ts +5 -13
  82. package/dist/src/localFiles/errors.d.ts +1 -1
  83. package/dist/src/localFiles/host/protocol.d.ts +34 -16
  84. package/dist/src/localFiles/host/sandboxClient.d.ts +112 -0
  85. package/dist/src/localFiles/host/sandboxFrames.d.ts +9 -0
  86. package/dist/src/localFiles/index.d.ts +11 -6
  87. package/dist/src/localFiles/preview.d.ts +2 -0
  88. package/dist/src/localFiles/schemas.d.ts +2 -5
  89. package/dist/src/localFiles/types.d.ts +16 -23
  90. package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +35 -1
  91. package/dist/src/localFiles/workspace/WorkspaceFiles.d.ts +63 -0
  92. package/dist/src/localMcp/client.d.ts +15 -0
  93. package/dist/src/localMcp/endpoints.d.ts +19 -0
  94. package/dist/src/localMcp/register.d.ts +34 -0
  95. package/dist/src/localMcp/registry.d.ts +27 -0
  96. package/dist/src/sessionUpload.d.ts +14 -0
  97. package/dist/src/utils.d.ts +6 -0
  98. package/package.json +13 -24
  99. package/dist/es/localFiles/createLocalPythonPlugin.mjs +0 -196
@@ -1,43 +1,44 @@
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 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) => {
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
+ 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) => {
16
16
  try {
17
- r(t.next(i));
18
- } catch (h) {
19
- a(h);
17
+ d(t.next(a));
18
+ } catch (s) {
19
+ o(s);
20
20
  }
21
- }, d = (i) => {
21
+ }, h = (a) => {
22
22
  try {
23
- r(t.throw(i));
24
- } catch (h) {
25
- a(h);
23
+ d(t.throw(a));
24
+ } catch (s) {
25
+ o(s);
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
+ }, d = (a) => a.done ? i(a.value) : Promise.resolve(a.value).then(n, h);
28
+ d((t = t.apply(p, 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";
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 {
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";
36
+ import { getCVForceService as K } from "./api/index.mjs";
37
+ 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 {
41
42
  /**
42
43
  * Session 类的构造函数,用于初始化会话实例。
43
44
  * @param token - 用户的身份验证令牌,用于解析应用 ID。
@@ -47,87 +48,91 @@ class te {
47
48
  * @param _application - 应用信息,可选参数。
48
49
  * @param client - Bot 客户端实例,可选参数。
49
50
  */
50
- constructor(e, t = "", s, a, n, d) {
51
- p(this, "_id");
52
- p(this, "_frozen", !1);
51
+ constructor(e, t = "", i, o, n, h) {
52
+ f(this, "_id");
53
+ f(this, "_frozen", !1);
53
54
  //是否可发送新问题
54
- p(this, "inactivating", !1);
55
- p(this, "_ready", !1);
55
+ f(this, "inactivating", !1);
56
+ f(this, "_ready", !1);
56
57
  // ready = false;
57
- p(this, "event", new B());
58
+ f(this, "event", new H());
58
59
  //从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", {
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", {
76
77
  applicationId: this._applicationId,
77
78
  sessionId: t
78
79
  }), this.loadApplicationInfo().then(() => {
79
- var r;
80
+ var d;
80
81
  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);
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);
83
84
  })).then(() => c(this, null, function* () {
84
85
  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);
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);
87
88
  })).then(() => c(this, null, function* () {
88
89
  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
90
  })));
90
91
  else {
91
- const i = (s == null ? void 0 : s.name) || "新建会话";
92
+ const a = (i == null ? void 0 : i.name) || "新建会话";
92
93
  this.event.emit("data:creating", {
93
94
  applicationId: this._applicationId,
94
- name: i
95
+ name: a
95
96
  }), this.event.emit("data:loading"), this.cvforceApi.postSession(this.applicationId, {
96
- name: i,
97
+ name: a,
97
98
  // @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),
99
+ netOpen: i == null ? void 0 : i.netOpen,
100
+ knowledgeIdList: (d = i == null ? void 0 : i.knowledges) == null ? void 0 : d.map((s) => s.id),
100
101
  applicationId: this._applicationId
101
- }).then((h) => c(this, null, function* () {
102
+ }).then((s) => c(this, null, function* () {
102
103
  var m;
103
- this._id = h.id, this.data = I(f({}, h), {
104
- knowledges: s == null ? void 0 : s.knowledges
104
+ this._id = s.id, this.data = y(u({}, s), {
105
+ knowledges: i == null ? void 0 : i.knowledges
105
106
  }), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id);
106
107
  try {
107
108
  yield (() => c(this, null, function* () {
108
- if (s != null && s.knowledges)
109
- return this.setKnowledges((s == null ? void 0 : s.knowledges) || void 0);
109
+ if (i != null && i.knowledges)
110
+ return this.setKnowledges((i == null ? void 0 : i.knowledges) || void 0);
110
111
  }))();
111
- } catch (y) {
112
- u.error(y);
112
+ } catch (I) {
113
+ l.error(I);
113
114
  }
114
115
  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), {
116
+ 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), {
120
121
  value: w
121
122
  });
122
- }).sort((v, w) => v.key.localeCompare(w.key));
123
- return this.setPromptVariables(y || []);
123
+ }).sort((g, w) => g.key.localeCompare(w.key));
124
+ return this.setPromptVariables(I || []);
124
125
  } else
125
126
  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"));
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"));
127
128
  }));
128
129
  }
129
130
  });
130
131
  }
132
+ /** 是否为当前正在使用的会话。 */
133
+ get active() {
134
+ return this._sessionActive;
135
+ }
131
136
  fetchData() {
132
137
  return c(this, null, function* () {
133
138
  if (!this.data)
@@ -175,14 +180,14 @@ class te {
175
180
  return c(this, null, function* () {
176
181
  if (!this.data)
177
182
  throw Error("session data is not loaded");
178
- if ((e || []).some((a) => Array.isArray(a.fileIds) && a.fileIds.length === 0))
183
+ if ((e || []).some((o) => Array.isArray(o.fileIds) && o.fileIds.length === 0))
179
184
  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);
184
- }, 0) > V)
185
- throw Error(`knowledges and fileIds total count cannot exceed ${V}`);
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
191
  return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
187
192
  knowledges: e
188
193
  }), this.data.knowledges = e, this.event.emit("data:updated", this.data), this.event.emit("data:updated:knowledges", {
@@ -195,7 +200,7 @@ class te {
195
200
  throw Error("session id is not defined");
196
201
  if (!this.data)
197
202
  throw Error("session data is not loaded");
198
- const e = this.promptVariables.filter((t) => t.status === q.Quoted).filter((t) => !t.value);
203
+ const e = this.promptVariables.filter((t) => t.status === U.Quoted).filter((t) => !t.value);
199
204
  if (e.length)
200
205
  throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((t) => t.key).join(", ")}`);
201
206
  }
@@ -204,16 +209,16 @@ class te {
204
209
  }
205
210
  setPromptVariables(e) {
206
211
  return c(this, null, function* () {
207
- const t = e == null ? void 0 : e.map((n) => n.type ? n : I(f({}, n), {
212
+ const t = e == null ? void 0 : e.map((n) => n.type ? n : y(u({}, n), {
208
213
  type: "instruction"
209
- })), s = this._promptVariables.map((n) => {
210
- 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
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
214
219
  });
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) => {
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) => {
217
222
  if (!this.data)
218
223
  throw Error("session data is not loaded");
219
224
  if (!(!n.id || !n.key))
@@ -241,13 +246,13 @@ class te {
241
246
  */
242
247
  setNetOpen(e) {
243
248
  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
+ 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.");
247
252
  return;
248
253
  }
249
- if (((a = this._application) == null ? void 0 : a.arrange[0].enableNetOpen) === !1) {
250
- console.warn("This Application is disabled to change netOpen.");
254
+ if (((o = this._application) == null ? void 0 : o.arrange[0].enableNetOpen) === !1) {
255
+ l.warn("This Application is disabled to change netOpen.");
251
256
  return;
252
257
  }
253
258
  if (!this.data)
@@ -255,26 +260,39 @@ class te {
255
260
  return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, {
256
261
  netOpen: e ? 1 : 2
257
262
  /* 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", {
263
+ }), this.data.netOpen = e, l.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.event.emit("data:updated:netOpen", {
259
264
  netOpen: e
260
265
  }), e;
261
266
  });
262
267
  }
268
+ /**
269
+ * 发请求用的 token。有 BotClient 时跟它同步,便于同 payload 静默换票。
270
+ */
271
+ get requestToken() {
272
+ var e, t;
273
+ return (t = (e = this.client) == null ? void 0 : e.token) != null ? t : this.token;
274
+ }
275
+ /**
276
+ * 同 payload 静默换票时通知长连接。
277
+ * Agent 发 socket `update`;Dialog / 无连接默认空操作(HTTP 已现读 requestToken)。
278
+ */
279
+ updateSocketResourceToken(e) {
280
+ }
263
281
  getService() {
264
- return H(S.apiRoot, this.token, (e, t) => {
265
- const s = new Error("data request error", {
282
+ return K(_.apiRoot, () => this.requestToken, (e, t) => {
283
+ const i = new Error("data request error", {
266
284
  cause: {
267
285
  status: e,
268
286
  message: t
269
287
  }
270
288
  });
271
- u.error(s), this.event.emit("error", s);
289
+ l.error(i), this.event.emit("error", i);
272
290
  });
273
291
  }
274
- addEventListener(e, t, s = !1) {
292
+ addEventListener(e, t, i = !1) {
275
293
  if (typeof t != "function")
276
294
  throw Error("event listener is not a function");
277
- if (s) {
295
+ if (i) {
278
296
  this.event.once(e, t);
279
297
  return;
280
298
  }
@@ -290,36 +308,36 @@ class te {
290
308
  */
291
309
  fetchMessages() {
292
310
  return c(this, arguments, function* (e = {}) {
293
- var s, a;
311
+ var i, o;
294
312
  const t = {
295
313
  pageSize: this._loadingMessagesSize
296
314
  };
297
315
  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) => {
300
- 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), {
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), {
303
321
  actions: m,
304
- relatedQuesion: y,
322
+ relatedQuesion: I,
305
323
  fromHistory: !0
306
324
  });
307
325
  });
308
326
  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];
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];
311
329
  } 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];
330
+ const a = this.messages.findIndex((s) => s.ts === e.endTime);
331
+ this.messages.splice(0, a + 1), this.messages = [...h, ...this.messages];
314
332
  } 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];
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];
317
335
  } 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);
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);
320
338
  }
321
- 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;
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;
323
341
  } else {
324
342
  const n = new Error(
325
343
  "session id is not defined"
@@ -328,7 +346,7 @@ class te {
328
346
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
329
347
  // }
330
348
  );
331
- return u.error(n), this.event.emit("error", n), !1;
349
+ return l.error(n), this.event.emit("error", n), !1;
332
350
  }
333
351
  });
334
352
  }
@@ -340,14 +358,14 @@ class te {
340
358
  throw Error("name is empty");
341
359
  return this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, {
342
360
  name: e
343
- }), u.log("data:updated", this.messages), this.event.emit("data:updated", this.data), this.event.emit("data:updated:name", {
361
+ }), l.log("data:updated", this.messages), this.event.emit("data:updated", this.data), this.event.emit("data:updated:name", {
344
362
  name: e
345
363
  }), !0;
346
364
  });
347
365
  }
348
366
  pushMessage(e) {
349
367
  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
+ 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);
351
369
  });
352
370
  }
353
371
  reloadSessionInfo() {
@@ -356,7 +374,7 @@ class te {
356
374
  return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => c(this, null, function* () {
357
375
  if (this.data = e, !this._id)
358
376
  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;
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;
360
378
  })).then(() => c(this, null, function* () {
361
379
  return this.messages = [], yield this.fetchMessages(), this._dataLoading = !1, this._dataLoaded = !0, this.event.emit("data:loaded", this.data), this;
362
380
  }));
@@ -376,37 +394,38 @@ class te {
376
394
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
377
395
  // }
378
396
  );
379
- return u.error(e), this.event.emit("error", e), !1;
397
+ return l.error(e), this.event.emit("error", e), !1;
380
398
  }
381
399
  return this.event.emit("data:deleting"), yield this.cvforceApi.delSessionsId(this.applicationId, this.id), this.event.emit("data:deleted"), this.destroy();
382
400
  });
383
401
  }
384
402
  activate() {
385
403
  return c(this, null, function* () {
404
+ var t;
386
405
  if (this.activing)
387
- return new Promise((t) => {
406
+ return new Promise((i) => {
388
407
  this.addEventListener("activated", () => {
389
- t(!0);
408
+ i(!0);
390
409
  }, !0);
391
410
  });
392
411
  if (!this._id)
393
412
  throw Error("session is not avalible. id is undefined");
394
- this.activing = !0;
413
+ l.log("activate", this._id), this.activing = !0;
395
414
  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;
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._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;
399
418
  });
400
419
  }
401
420
  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);
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);
403
422
  }
404
423
  /**
405
424
  * 会话标题摘要开关。应用详情返回布尔值时用该值,字段缺失时默认开启。
406
425
  */
407
426
  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;
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;
410
429
  return typeof e == "boolean" ? e : !0;
411
430
  }
412
431
  //TODO: remove mcp server
@@ -417,201 +436,236 @@ class te {
417
436
  get messagesLoading() {
418
437
  return this._loadingMessages;
419
438
  }
420
- send(s) {
439
+ /**
440
+ * 把文件上传到当前会话的对象存储,返回可访问的地址。
441
+ * 图片地址放进 send 的 imageContent。
442
+ */
443
+ upload(e) {
444
+ return c(this, null, function* () {
445
+ if (!this._id)
446
+ throw Error("session is not avalible. id is undefined");
447
+ return G(this.cvforceApi, this._id, e);
448
+ });
449
+ }
450
+ send(i) {
421
451
  return c(this, arguments, function* (e, t = {
422
- stream: S.stream
452
+ stream: _.stream
423
453
  }) {
424
- var _;
454
+ var A, v;
425
455
  try {
426
456
  this.validateConfigs();
427
- } catch (o) {
428
- console.error(o);
457
+ } catch (r) {
458
+ l.report(r);
429
459
  }
430
460
  const {
431
- stream: a = S.stream,
461
+ stream: o = _.stream,
432
462
  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(
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(
436
466
  "failed at sending empty content"
437
467
  /* SendingEmptyContent */
438
468
  // {
439
469
  // cause: SESSION_ERROR_CAUSE[SessionErrors.SendingEmptyContent],
440
470
  // }
441
471
  );
442
- return u.error(o), this.event.emit("error", o), !1;
472
+ return l.error(r), this.event.emit("error", r), !1;
443
473
  }
444
474
  if (!this._id) {
445
- const o = new Error(
475
+ const r = new Error(
446
476
  "session id is not defined"
447
477
  /* IdUndefined */
448
478
  // {
449
479
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
450
480
  // }
451
481
  );
452
- return u.error(o), this.event.emit("error", o), !1;
482
+ return l.error(r), this.event.emit("error", r), !1;
453
483
  }
454
484
  if (this.frozen) {
455
- const o = new Error(
485
+ const r = new Error(
456
486
  "session is frozen"
457
487
  /* Frozen */
458
488
  // {
459
489
  // cause: { code: -1, message: 'session is frozen.' },
460
490
  // }
461
491
  );
462
- return u.error(o), this.event.emit("error", o), !1;
492
+ return l.error(r), this.event.emit("error", r), !1;
463
493
  }
464
- this.frozen = !0, this._stopAnswer = !1;
465
- const y = {
494
+ this.frozen = !0, this._stopAnswer = !1, l.log("send", {
495
+ 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
500
+ });
501
+ const I = {
466
502
  sessionId: this.id,
467
503
  role: 2,
468
- content: r,
469
- messageId: d,
504
+ content: d,
505
+ messageId: h,
470
506
  reference: [],
471
507
  searchReference: [],
472
508
  messageTime: "",
473
- files: i,
509
+ files: a,
474
510
  imageContent: m
475
- }, v = {
476
- content: r,
477
- stream: a,
478
- requestId: d,
479
- fileIds: h,
511
+ }, g = {
512
+ content: d,
513
+ stream: o,
514
+ requestId: h,
515
+ fileIds: s,
480
516
  referenceFirst: n,
481
517
  imageContent: m
482
518
  };
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);
519
+ this.pushMessage(I), this.event.emit("question:content", d);
520
+ const w = (r) => c(this, null, function* () {
521
+ if (r.role === S.Robot) {
522
+ this.removeEventListener("answer:finish", w);
523
+ 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
526
+ }), 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);
540
+ }
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
+ } finally {
549
+ this.frozen = !1;
550
+ }
503
551
  }
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
552
  });
513
- return this.addEventListener("answer:finish", w), this.sendQuestion(v, I(f({}, t), {
514
- stream: a,
553
+ return this.addEventListener("answer:finish", w), this.sendQuestion(g, y(u({}, t), {
554
+ stream: o,
515
555
  referenceFirst: n
516
- })), y;
556
+ })), I;
517
557
  });
518
558
  }
519
559
  currentUserQuestion() {
520
560
  for (let e = this.messages.length - 1; e >= 0; e -= 1) {
521
561
  const t = this.messages[e];
522
- if (t.role === A.User)
562
+ if (t.role === S.User)
523
563
  return t.content || "";
524
564
  }
525
565
  return "";
526
566
  }
527
567
  getRelatedQuesion(e) {
528
568
  return c(this, null, function* () {
529
- var r, i;
569
+ var d, a;
570
+ const t = (d = this.application) == null ? void 0 : d.arrange[0];
530
571
  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,
572
+ return l.log("related:skip", {
573
+ code: e.code,
574
+ sensitive: !!e.isSensitive,
575
+ hasContent: !!e.content
576
+ }), e;
577
+ const i = q(t == null ? void 0 : t.recommendQuestionOpen, t == null ? void 0 : t.relatedQuestionApp, e.content, this.currentUserQuestion());
578
+ if (i === null)
579
+ return l.log("related:skip", {
580
+ recommendQuestionOpen: t == null ? void 0 : t.recommendQuestionOpen
581
+ }), e;
582
+ l.log("related:request", i), this.event.emit("message:relatedQuesion:loading", e);
583
+ const o = yield this.cvforceApi.postGeneques({
584
+ 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,
542
589
  index: -3,
543
590
  isLoading: !1
544
591
  });
545
592
  return this.event.emit("message:relatedQuesion:loaded", {
546
- relatedQuesion: h
593
+ relatedQuesion: s
547
594
  }), this.event.emit("message:updated", m), m;
548
595
  }
549
- const n = (i = a.content) == null ? void 0 : i.split("*").map(F).map(N).filter((h) => !!h);
596
+ const n = (a = o.content) == null ? void 0 : a.split("*").map(F).map(N).filter((s) => !!s);
550
597
  e.relatedQuesion = n, e.index = -3, e.isLoading = !1;
551
- const d = I(f({}, e), {
598
+ const h = y(u({}, e), {
552
599
  relatedQuesion: n,
553
600
  index: -3,
554
601
  isLoading: !1
555
602
  });
556
- return this.event.emit("message:relatedQuesion:loaded", d), this.event.emit("message:updated", d), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
603
+ return this.event.emit("message:relatedQuesion:loaded", h), this.event.emit("message:updated", h), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, {
557
604
  lastRelatedQuestion: n
558
- }), d;
605
+ }), h;
559
606
  });
560
607
  }
561
608
  fillMessageReference(e) {
562
609
  return c(this, null, function* () {
563
610
  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]));
611
+ 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]));
566
613
  } catch (t) {
567
- console.error("Failed to fill message reference:", t);
614
+ l.error("Failed to fill message reference:", t);
568
615
  }
569
616
  return this.event.emit("message:updated", e), e;
570
617
  });
571
618
  }
572
619
  answerAgain() {
573
620
  return c(this, arguments, function* (e = {
574
- stream: S.stream
621
+ stream: _.stream
575
622
  }) {
623
+ var a;
576
624
  const t = this.messages[this.messages.length - 1];
577
625
  if (!t)
578
626
  throw Error("message list is empty");
579
- this.frozen = !0, this._stopAnswer = !1;
627
+ this.frozen = !0, this._stopAnswer = !1, l.log("answerAgain", {
628
+ sessionId: this._id,
629
+ stream: (a = e.stream) != null ? a : _.stream
630
+ });
580
631
  const {
581
- stream: s = S.stream
632
+ stream: i = _.stream
582
633
  } = 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 = {
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 = {
586
637
  content: n.content,
587
638
  fileIds: n.fileIds,
588
639
  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);
640
+ stream: i,
641
+ oldMessageId: o
642
+ }, d = (s) => c(this, null, function* () {
643
+ if (s.role === S.Robot && s.index === -1)
644
+ 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
647
+ }), 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);
661
+ }
662
+ this.getRelatedQuesion(s);
663
+ } finally {
664
+ this.removeEventListener("message:content", d), this.frozen = !1;
609
665
  }
610
- this.getRelatedQuesion(i), this.removeEventListener("message:content", r), this.frozen = !1;
611
- }
612
666
  });
613
- this.addEventListener("message:content", r), this.sendQuestion(d, I(f({}, e), {
614
- stream: s
667
+ this.addEventListener("message:content", d), this.sendQuestion(h, y(u({}, e), {
668
+ stream: i
615
669
  }));
616
670
  });
617
671
  }
@@ -633,21 +687,21 @@ class te {
633
687
  * @returns 返回当前会话实例
634
688
  */
635
689
  suspend() {
636
- var s;
690
+ var i;
637
691
  this.stopAnswer(), this.frozen = !1, this._stopAnswer = !0;
638
692
  let e = this.messages[this.messages.length - 1];
639
693
  const t = {
640
- messageId: x(),
694
+ messageId: Q(),
641
695
  // 生成唯一的消息 ID
642
696
  isLoading: !1,
643
697
  // 标记消息已停止加载
644
698
  index: -2,
645
699
  // 消息索引,用于标识特殊状态
646
- messageTime: K(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:ss:mm"),
700
+ messageTime: Y(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:ss:mm"),
647
701
  // 消息时间
648
702
  content: "",
649
703
  // 消息内容
650
- role: A.System,
704
+ role: S.System,
651
705
  // 消息角色为系统消息
652
706
  sessionId: this.id || "",
653
707
  // 会话 ID
@@ -660,33 +714,33 @@ class te {
660
714
  if (e.index === -2)
661
715
  return;
662
716
  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];
717
+ const o = e.actions[((i = e.actions) == null ? void 0 : i.length) - 1];
718
+ if (o) {
719
+ const n = o[o.length - 1];
666
720
  if (n && (n.type === "BEFORE_CALL" || n.type === "FUNCTION_CALL")) {
667
721
  const {
668
- pluginName: d,
669
- pluginCode: r,
670
- funcCode: i,
671
- funcName: h
722
+ pluginName: h,
723
+ pluginCode: d,
724
+ funcCode: a,
725
+ funcName: s
672
726
  } = n.data;
673
- a.push({
727
+ o.push({
674
728
  data: {
675
- pluginName: d,
676
- pluginCode: r,
677
- funcCode: i,
678
- funcName: h,
729
+ pluginName: h,
730
+ pluginCode: d,
731
+ funcCode: a,
732
+ funcName: s,
679
733
  step: n.data.step + 1
680
734
  },
681
735
  type: "ABORT"
682
736
  }), this.event.emit("message:action", {
683
737
  messageId: e.messageId,
684
- action: a
738
+ action: o
685
739
  });
686
740
  }
687
741
  }
688
742
  }
689
- e = f(f({}, e), t), this.event.emit("message:content", e), this.event.emit("message:updated", e);
743
+ e = u(u({}, e), t), this.event.emit("message:content", e), this.event.emit("message:updated", e);
690
744
  } else
691
745
  this.pushMessage(t), this.event.emit("message:content", t);
692
746
  return this;
@@ -697,10 +751,10 @@ class te {
697
751
  * @param targetMessage - 要更新的目标聊天消息对象。
698
752
  */
699
753
  handleStreamAnswer(e, t) {
700
- var d;
754
+ var h;
701
755
  if (!e.data)
702
756
  return;
703
- const s = e.data.type, a = e.data.payload;
757
+ const i = e.data.type, o = e.data.payload;
704
758
  t.isReplying = !0;
705
759
  const n = () => {
706
760
  if (!t.searchingStatus) {
@@ -712,25 +766,25 @@ class te {
712
766
  return;
713
767
  }
714
768
  };
715
- if (s === "error" && (t.index = -1), s === "beforeSearch" && n(), s === "searchOver" && n(), s === "search" && t.searchReference.push(a), s === "thinking") {
769
+ if (i === "error" && (t.index = -1), i === "beforeSearch" && n(), i === "searchOver" && n(), i === "search" && t.searchReference.push(o), i === "thinking") {
716
770
  t.thinkingStatus = "start";
717
- const i = a.choices[0];
718
- t.reasoningContent = (t.reasoningContent || "") + ((d = i.delta.reasoningContent) != null ? d : "");
771
+ const a = o.choices[0];
772
+ t.reasoningContent = (t.reasoningContent || "") + ((h = a.delta.reasoningContent) != null ? h : "");
719
773
  }
720
- if (s === "thinkingOver") {
774
+ if (i === "thinkingOver") {
721
775
  t.thinkingStatus = "end";
722
- const r = a.thinkingSecs;
723
- t.thinkingSecs = r;
776
+ const d = o.thinkingSecs;
777
+ t.thinkingSecs = d;
724
778
  }
725
- if (s === "text") {
726
- const i = a.choices[0];
727
- t.content = (t.content || "") + i.delta.content;
779
+ if (i === "text") {
780
+ const a = o.choices[0];
781
+ t.content = (t.content || "") + a.delta.content;
728
782
  }
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;
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;
732
786
  }
733
- s === "over" ? t.index = -1 : (t.code = e.code, t.message = e.message);
787
+ i === "over" && (t.index = -1), e.code != null && (t.code = e.code, t.message = e.message);
734
788
  }
735
789
  /**
736
790
  * 异步提交评论信息
@@ -754,8 +808,8 @@ class te {
754
808
  if (!this._applicationId || !this._id)
755
809
  throw Error("session is not avalible. id is undefined");
756
810
  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;
811
+ const i = this.messages.find((o) => o.messageId === e.messageId);
812
+ return i && (i.comment = e, this.event.emit("message:comment", i)), t;
759
813
  });
760
814
  });
761
815
  }
@@ -782,7 +836,7 @@ class te {
782
836
  }
783
837
  }
784
838
  export {
785
- V as MAX_KNOWLEDGES_AND_FILES_COUNT,
786
- Y as SessionErrors,
787
- te as default
839
+ C as MAX_KNOWLEDGES_AND_FILES_COUNT,
840
+ j as SessionErrors,
841
+ ne as default
788
842
  };