@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,189 +1,203 @@
1
- var q = Object.defineProperty, j = Object.defineProperties;
2
- var L = Object.getOwnPropertyDescriptors;
3
- var b = Object.getOwnPropertySymbols;
4
- var F = Object.prototype.hasOwnProperty, N = Object.prototype.propertyIsEnumerable;
5
- var M = (r, c, e) => c in r ? q(r, c, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[c] = e, m = (r, c) => {
6
- for (var e in c || (c = {}))
7
- F.call(c, e) && M(r, e, c[e]);
8
- if (b)
9
- for (var e of b(c))
10
- N.call(c, e) && M(r, e, c[e]);
11
- return r;
12
- }, w = (r, c) => j(r, L(c));
13
- var P = (r, c, e) => (M(r, typeof c != "symbol" ? c + "" : c, e), e);
14
- var y = (r, c, e) => new Promise((o, a) => {
15
- var n = (i) => {
1
+ var E = Object.defineProperty, x = Object.defineProperties;
2
+ var j = Object.getOwnPropertyDescriptors;
3
+ var P = Object.getOwnPropertySymbols;
4
+ var O = Object.prototype.hasOwnProperty, F = Object.prototype.propertyIsEnumerable;
5
+ var S = (a, l, e) => l in a ? E(a, l, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[l] = e, h = (a, l) => {
6
+ for (var e in l || (l = {}))
7
+ O.call(l, e) && S(a, e, l[e]);
8
+ if (P)
9
+ for (var e of P(l))
10
+ F.call(l, e) && S(a, e, l[e]);
11
+ return a;
12
+ }, y = (a, l) => x(a, j(l));
13
+ var w = (a, l, e) => (S(a, typeof l != "symbol" ? l + "" : l, e), e);
14
+ var v = (a, l, e) => new Promise((i, c) => {
15
+ var o = (t) => {
16
16
  try {
17
- s(e.next(i));
18
- } catch (d) {
19
- a(d);
17
+ s(e.next(t));
18
+ } catch (n) {
19
+ c(n);
20
20
  }
21
- }, t = (i) => {
21
+ }, d = (t) => {
22
22
  try {
23
- s(e.throw(i));
24
- } catch (d) {
25
- a(d);
23
+ s(e.throw(t));
24
+ } catch (n) {
25
+ c(n);
26
26
  }
27
- }, s = (i) => i.done ? o(i.value) : Promise.resolve(i.value).then(n, t);
28
- s((e = e.apply(r, c)).next());
27
+ }, s = (t) => t.done ? i(t.value) : Promise.resolve(t.value).then(o, d);
28
+ s((e = e.apply(a, l)).next());
29
29
  });
30
- import { ChatMessageRole as v, PromptVariableType as A } from "@glodon-aiot/apis";
31
- import O from "./Logger.mjs";
32
- import { configuration as C } from "./Config.mjs";
33
- import { io as U } from "socket.io-client";
30
+ import { ChatMessageRole as C, PromptVariableType as T } from "@glodon-aiot/apis";
31
+ import N from "./Logger.mjs";
32
+ import { configuration as p } from "./Config.mjs";
33
+ import { io as D } from "socket.io-client";
34
34
  import { UserMode as I } from "./PayloadTypes.mjs";
35
- import D, { SessionErrors as k } from "./Session.mjs";
35
+ import z, { SessionErrors as R } from "./Session.mjs";
36
36
  import V from "dayjs";
37
- import { v4 as J } from "uuid";
38
- const f = new O("[AgentSession] ");
39
- function _(r) {
40
- if (typeof r != "string")
41
- return r;
42
- const c = r.trim();
43
- if (c)
37
+ import { v4 as $ } from "uuid";
38
+ import { connectLocalMcpBridge as B, subscribeLocalMcpEndpoints as J } from "./localMcp/endpoints.mjs";
39
+ import { registerLocalMcp as Q } from "./localMcp/register.mjs";
40
+ const r = new N("[AgentSession] ");
41
+ function L(a) {
42
+ if (typeof a != "string")
43
+ return a;
44
+ const l = a.trim();
45
+ if (l)
44
46
  try {
45
- return JSON.parse(c);
47
+ return JSON.parse(l);
46
48
  } catch (e) {
47
- return r;
49
+ return a;
48
50
  }
49
51
  }
50
- function E(r) {
51
- return r == null || r === "" || typeof r == "object" && !Array.isArray(r) && Object.keys(r).length === 0;
52
+ function Y(a) {
53
+ const l = a.imageContent;
54
+ return Array.isArray(l) ? y(h({}, a), {
55
+ imageContent: `[${l.length}]`
56
+ }) : a;
52
57
  }
53
- function Y(r, c) {
54
- const e = _(r == null ? void 0 : r.query);
55
- if (!E(e))
58
+ function U(a) {
59
+ return a == null || a === "" || typeof a == "object" && !Array.isArray(a) && Object.keys(a).length === 0;
60
+ }
61
+ function H(a, l) {
62
+ const e = L(a == null ? void 0 : a.query);
63
+ if (!U(e))
56
64
  return e;
57
- const o = _(c);
58
- return E(o) && e != null ? e : o;
65
+ const i = L(l);
66
+ return U(i) && e != null ? e : i;
59
67
  }
60
- class Z extends D {
61
- constructor(e, o = "", a, n, t, s) {
62
- super(e, o, a, n, t, s);
63
- P(this, "inflightCalls", /* @__PURE__ */ new Set());
68
+ class re extends z {
69
+ constructor(e, i = "", c, o, d, s) {
70
+ super(e, i, c, o, d, s);
71
+ w(this, "inflightCalls", /* @__PURE__ */ new Set());
72
+ w(this, "localMcpTicket", 0);
73
+ w(this, "stopLocalMcpUpdates");
74
+ /** 已发出 `update`、尚未收到结果;下一条 ready / ack 按换票结果处理,不拆连接。 */
75
+ w(this, "pendingTokenUpdate", !1);
64
76
  }
65
77
  prepareConnection() {
66
- return new Promise((e) => y(this, null, function* () {
67
- this.socket && e(!0);
68
- const o = {
78
+ return new Promise((e) => v(this, null, function* () {
79
+ var d;
80
+ if ((d = this.socket) != null && d.connected) {
81
+ r.log("socket:reuse", this.id), e(!0);
82
+ return;
83
+ }
84
+ this.socket && (this.socket.removeAllListeners(), this.socket.close(), this.socket = null);
85
+ const i = {
69
86
  version: "1.0",
70
- stream: JSON.stringify(C.stream),
87
+ stream: JSON.stringify(p.stream),
71
88
  appId: this._applicationId,
72
89
  sessionId: this.id,
73
- Authorization: `${this.token}`,
74
- userMode: C.debug ? I.Debug : I.Normal
75
- }, a = new URL(C.apiRoot), n = yield U(a.origin, {
76
- path: a.pathname.replace("api", "socketio") + "/copilot",
77
- query: o,
90
+ Authorization: `${this.requestToken}`,
91
+ userMode: p.debug ? I.Debug : I.Normal
92
+ }, c = new URL(p.apiRoot);
93
+ r.log("socket:connecting", {
94
+ sessionId: this.id,
95
+ origin: c.origin
96
+ });
97
+ const o = yield D(c.origin, {
98
+ path: c.pathname.replace("api", "socketio") + "/copilot",
99
+ query: i,
78
100
  transports: ["websocket"]
79
101
  });
80
- n.on("connect", () => {
81
- f.log("socket:connect"), this.event.emit("socket:connect"), n.emit("init");
82
- }), n.on("disconnect", () => {
83
- f.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
84
- }), n.on("ready", (t) => {
85
- if (t.code === 200) {
86
- f.log("ready");
87
- const s = C.debug ? I.Debug : I.Normal;
88
- s === I.Debug && t.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), e(!0);
102
+ o.on("connect", () => {
103
+ r.log("socket:connect"), this.event.emit("socket:connect"), o.emit("init");
104
+ }), o.on("disconnect", () => {
105
+ r.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1, this.pendingTokenUpdate = !1;
106
+ }), o.on("ready", (s) => {
107
+ if (this.pendingTokenUpdate) {
108
+ this.handleResourceTokenUpdateResult(s);
109
+ return;
110
+ }
111
+ if (s.code === 200) {
112
+ r.log("ready");
113
+ const t = p.debug ? I.Debug : I.Normal;
114
+ t === I.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), this.registerLocalMcp(), e(!0);
89
115
  } else {
90
116
  this._ready && (this._ready = !1);
91
- const s = new Error(
92
- k.SocketConnectionFailed
117
+ const t = new Error(
118
+ R.SocketConnectionFailed
93
119
  // , {
94
120
  // cause: data,
95
121
  // }
96
122
  );
97
- f.error("error", s), this.event.emit("error", s), e(!1);
123
+ r.error("error", t), this.event.emit("error", t), e(!1);
98
124
  }
99
- }), n.on("action", (t, s) => y(this, null, function* () {
100
- var R;
125
+ }), o.on("action", (s, t) => v(this, null, function* () {
126
+ var A;
101
127
  if (this._stopAnswer || !this.id)
102
128
  return;
103
129
  const {
104
- type: i,
130
+ type: n,
105
131
  data: {
106
- runnerIsClient: d,
107
- pluginCode: l,
108
- funcCode: g,
109
- parameters: h,
110
- requestBody: x
132
+ runnerIsClient: g,
133
+ pluginCode: f,
134
+ funcCode: m,
135
+ parameters: u,
136
+ requestBody: _
111
137
  }
112
- } = t, u = this.messages[this.messages.length - 1];
113
- if (!u.messageId)
114
- u.actions || (u.actions = []), u.actions[t.data.step - 1] || (u.actions[t.data.step - 1] = []), u.actions[t.data.step - 1].push(t), this.event.emit("message:action", {
115
- messageId: u.messageId,
116
- action: t
117
- }), this.event.emit("message:updated", u);
138
+ } = s, k = this.messages[this.messages.length - 1];
139
+ if (!k.messageId)
140
+ k.actions || (k.actions = []), k.actions[s.data.step - 1] || (k.actions[s.data.step - 1] = []), k.actions[s.data.step - 1].push(s), this.event.emit("message:action", {
141
+ messageId: k.messageId,
142
+ action: s
143
+ }), this.event.emit("message:updated", k);
118
144
  else {
119
- const p = {
145
+ const M = {
120
146
  messageId: "",
121
147
  content: "",
122
- role: v.Robot,
148
+ role: C.Robot,
123
149
  sessionId: this.id,
124
150
  reference: [],
125
151
  searchReference: [],
126
152
  isLoading: !0,
127
153
  messageTime: "",
128
- actions: [[t]],
154
+ actions: [[s]],
129
155
  comment: {
130
156
  isAgree: 0
131
157
  },
132
158
  isReplying: !0
133
159
  };
134
- this.pushMessage(p), this.event.emit("message:action", {
135
- messageId: p.messageId,
136
- action: t
160
+ this.pushMessage(M), this.event.emit("message:action", {
161
+ messageId: M.messageId,
162
+ action: s
137
163
  });
138
164
  }
139
- if (this.inactivating) {
140
- const p = new Error(
141
- k.Inactive
142
- // {
143
- // cause: {
144
- // code: -1,
145
- // message:
146
- // 'session is inactive, skipping plugin client function call',
147
- // },
148
- // }
149
- );
150
- f.warn("error", p), this.event.emit("error", p);
151
- } else if (i === "FUNCTION_CALL" && d) {
152
- f.log(`calling function ${g} of plugin ${l} with argument:
153
- ${JSON.stringify(h)}`);
154
- const p = (R = C.plugins) == null ? void 0 : R[l];
155
- if (!p) {
156
- s({
165
+ if (n === "FUNCTION_CALL" && !g && r.log("server function", f, m), n === "FUNCTION_CALL" && g) {
166
+ r.log(`calling function ${m} of plugin ${f} with argument:
167
+ ${JSON.stringify(u)}`);
168
+ const M = (A = p.plugins) == null ? void 0 : A[f];
169
+ if (!M) {
170
+ r.error("plugin not found", f, m), t({
157
171
  code: 404,
158
172
  message: "没有找到插件。"
159
173
  });
160
174
  return;
161
175
  }
162
- const S = p[g];
163
- if (typeof S != "function") {
164
- s({
176
+ const b = M[m];
177
+ if (typeof b != "function") {
178
+ r.error("plugin function not found", f, m), t({
165
179
  code: 404,
166
180
  message: "没有找到插件方法。"
167
181
  });
168
182
  return;
169
183
  }
170
- const T = Y(h, x);
171
- this.invokeClientPlugin(S, T, s, {
172
- requestId: t.requestId,
173
- pluginCode: l,
174
- functionCode: g
184
+ const q = H(u, _);
185
+ this.invokeClientPlugin(b, q, t, {
186
+ requestId: s.requestId,
187
+ pluginCode: f,
188
+ functionCode: m
175
189
  });
176
190
  }
177
- })), n.on("answer", (t) => {
178
- if (!this._stopAnswer)
179
- if (t.data.code !== 200 && (t.data.index = -1), t.data.stream || t.data.stream === void 0) {
180
- let s = this.messages[this.messages.length - 1];
181
- if (s && s.role === v.Robot)
182
- this.handleStreamAnswer(t.data, s), this.messages[this.messages.length - 1] = s, this.event.emit("message:content", s), this.event.emit("message:updated", s), s.index === -1 && (s.isReplying = !1, this.event.emit("answer:finish", s));
191
+ })), o.on("answer", (s) => {
192
+ if (r.log("answer", s == null ? void 0 : s.data), !this._stopAnswer)
193
+ if (s.data.code !== 200 && (s.data.index = -1), s.data.stream || s.data.stream === void 0) {
194
+ let t = this.messages[this.messages.length - 1];
195
+ if (t && t.role === C.Robot)
196
+ this.handleStreamAnswer(s.data, t), this.messages[this.messages.length - 1] = t, this.event.emit("message:content", t), this.event.emit("message:updated", t), t.index === -1 && (t.isReplying = !1, this.event.emit("answer:finish", t));
183
197
  else {
184
- const i = {
198
+ const n = {
185
199
  content: "",
186
- role: v.Robot,
200
+ role: C.Robot,
187
201
  reference: [],
188
202
  searchReference: [],
189
203
  messageId: "",
@@ -203,17 +217,38 @@ class Z extends D {
203
217
  thinkingStatus: "",
204
218
  searchingStatus: ""
205
219
  };
206
- this.handleStreamAnswer(t.data, i), this.pushMessage(i), this.event.emit("message:content", i), i.index === -1 && (i.isReplying = !1, this.event.emit("answer:finish", i));
220
+ this.handleStreamAnswer(s.data, n), this.pushMessage(n), this.event.emit("message:content", n), n.index === -1 && (n.isReplying = !1, this.event.emit("answer:finish", n));
207
221
  }
208
- } else if (t.data.updateSession && this.data && (this.data = m(m({}, this.data), t.data.updateSession), this.event.emit("data:updated", this.data)), t.data.index === -1) {
209
- let s = this.messages[this.messages.length - 1];
210
- if (s && !s.messageId)
211
- s = m(m({}, s), t.data), this.event.emit("message:content", s), this.event.emit("message:updated", s), this.event.emit("answer:finish", s);
212
- else {
213
- const i = m({
222
+ } else {
223
+ const t = h({}, s.data);
224
+ if (t.usageExtension == null && delete t.usageExtension, t.updateSession && this.data && (this.data = h(h({}, this.data), t.updateSession), this.event.emit("data:updated", this.data)), t.index === -1) {
225
+ let n = this.messages[this.messages.length - 1];
226
+ if (n && !n.messageId)
227
+ n = h(h({}, n), t), this.messages[this.messages.length - 1] = n, this.event.emit("message:content", n), this.event.emit("message:updated", n), this.event.emit("answer:finish", n);
228
+ else {
229
+ const g = h({
230
+ messageId: "",
231
+ content: "",
232
+ role: C.Robot,
233
+ sessionId: this.id,
234
+ reference: [],
235
+ searchReference: [],
236
+ isLoading: !1,
237
+ messageTime: "",
238
+ actions: [],
239
+ comment: {
240
+ isAgree: 0
241
+ },
242
+ isReplying: !0
243
+ }, t);
244
+ this.event.emit("message:content", g), this.pushMessage(g), this.event.emit("answer:finish", g);
245
+ }
246
+ } else {
247
+ let n = this.messages[this.messages.length - 1];
248
+ n && !n.messageId ? (n = h(h({}, n), t), this.messages[this.messages.length - 1] = n, this.event.emit("message:content", n), this.event.emit("message:updated", n)) : (this.pushMessage(h({
214
249
  messageId: "",
215
250
  content: "",
216
- role: v.Robot,
251
+ role: C.Robot,
217
252
  sessionId: this.id,
218
253
  reference: [],
219
254
  searchReference: [],
@@ -224,53 +259,41 @@ class Z extends D {
224
259
  isAgree: 0
225
260
  },
226
261
  isReplying: !0
227
- }, t.data);
228
- this.event.emit("message:content", i), this.pushMessage(i), this.event.emit("answer:finish", i);
262
+ }, t)), this.event.emit("message:content", t));
229
263
  }
230
- } else {
231
- let s = this.messages[this.messages.length - 1];
232
- s && !s.messageId ? (s = m(m({}, s), t.data), this.messages[this.messages.length - 1] = s, this.event.emit("message:content", s), this.event.emit("message:updated", s)) : (this.pushMessage(m({
233
- messageId: "",
234
- content: "",
235
- role: v.Robot,
236
- sessionId: this.id,
237
- reference: [],
238
- searchReference: [],
239
- isLoading: !1,
240
- messageTime: "",
241
- actions: [],
242
- comment: {
243
- isAgree: 0
244
- },
245
- isReplying: !0
246
- }, t.data)), this.event.emit("message:content", t.data));
247
264
  }
248
- }), n.on("reference", (t) => y(this, null, function* () {
265
+ }), o.on("reference", (s) => v(this, null, function* () {
249
266
  if (this._stopAnswer)
250
267
  return;
251
268
  if (!this.id) {
252
- const h = new Error(
253
- k.IdUndefined
269
+ const u = new Error(
270
+ R.IdUndefined
254
271
  // {
255
272
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
256
273
  // }
257
274
  );
258
- f.error(h), this.event.emit("error", h), this.frozen = !1;
275
+ r.error(u), this.event.emit("error", u), this.frozen = !1;
259
276
  return;
260
277
  }
261
278
  const {
262
- type: s,
263
- data: i,
264
- requestId: d
265
- } = t, l = this.messages.findIndex((h) => h.messageId === d && h.role === v.User);
266
- if (l < 0)
279
+ type: t,
280
+ data: n,
281
+ requestId: g
282
+ } = s;
283
+ r.log("reference", {
284
+ type: t,
285
+ requestId: g,
286
+ data: n
287
+ });
288
+ const f = this.messages.findIndex((u) => u.messageId === g && u.role === C.User);
289
+ if (f < 0)
267
290
  return;
268
- const g = l + 1;
269
- if (g >= this.messages.length) {
270
- const h = {
291
+ const m = f + 1;
292
+ if (m >= this.messages.length) {
293
+ const u = {
271
294
  messageId: "",
272
295
  content: "",
273
- role: v.Robot,
296
+ role: C.Robot,
274
297
  sessionId: this.id,
275
298
  reference: [],
276
299
  searchReference: [],
@@ -282,109 +305,133 @@ class Z extends D {
282
305
  },
283
306
  isReplying: !0
284
307
  };
285
- this.pushMessage(h);
308
+ this.pushMessage(u);
286
309
  }
287
- s === "KG" ? (this.messages[g] = w(m({}, this.messages[g]), {
288
- reference: i.knowledgeReference
289
- }), yield this.fillMessageReference(this.messages[g]), this.event.emit("message:reference", i.knowledgeReference), this.event.emit("message:updated", this.messages[g])) : s === "WEB_SEARCH" && (this.messages[g] = w(m({}, this.messages[g]), {
290
- searchReference: i.knowledgeReference
291
- }), this.event.emit("message:searchreference", i.searchReference), this.event.emit("message:updated", this.messages[g]));
292
- })), this.socket = n, n.open();
310
+ t === "KG" ? (this.messages[m] = y(h({}, this.messages[m]), {
311
+ reference: n.knowledgeReference
312
+ }), yield this.fillMessageReference(this.messages[m]), this.event.emit("message:reference", n.knowledgeReference), this.event.emit("message:updated", this.messages[m])) : t === "WEB_SEARCH" && (this.messages[m] = y(h({}, this.messages[m]), {
313
+ searchReference: n.knowledgeReference
314
+ }), this.event.emit("message:searchreference", n.searchReference), this.event.emit("message:updated", this.messages[m]));
315
+ })), this.socket = o, o.open();
293
316
  }));
294
317
  }
295
318
  registerMcpFromClientConfig() {
296
- return C.mcp ? new Promise((e, o) => {
319
+ return p.mcp ? new Promise((e, i) => {
297
320
  if (!this.socket) {
298
- o("socket is undefined");
321
+ i("socket is undefined");
299
322
  return;
300
323
  }
301
- this.socket.emit("add_mcp_servers", C.mcp, (a) => {
302
- a.code === 200 ? (Object.keys(a.data).forEach((n) => {
303
- a.data[n].code !== 200 && console.error(n, "MCP Server 注册失败。", a.data[n]);
304
- }), e(!0)) : (console.error("MCP Server 注册失败。", a), o(a));
324
+ this.socket.emit("add_mcp_servers", p.mcp, (c) => {
325
+ c.code === 200 ? (Object.keys(c.data).forEach((o) => {
326
+ c.data[o].code !== 200 && r.report(o, "MCP Server 注册失败。", c.data[o]);
327
+ }), r.log("mcp:registered", Object.keys(c.data)), e(!0)) : (r.error("MCP Server 注册失败。", c), i(c));
305
328
  });
306
- }) : Promise.resolve(!0);
329
+ }) : (r.log("mcp:skip"), Promise.resolve(!0));
330
+ }
331
+ registerLocalMcp() {
332
+ var c, o;
333
+ const e = this.socket;
334
+ if (!e)
335
+ return;
336
+ B(), this.stopLocalMcpUpdates || (this.stopLocalMcpUpdates = J(() => this.registerLocalMcp()));
337
+ const i = ++this.localMcpTicket;
338
+ Q({
339
+ arrange: (o = (c = this.application) == null ? void 0 : c.arrange) == null ? void 0 : o[0],
340
+ initMcp: p.mcp,
341
+ apiRoot: p.apiRoot,
342
+ token: this.requestToken,
343
+ socket: e,
344
+ isCurrent: () => i === this.localMcpTicket
345
+ }).catch((d) => {
346
+ r.report("local mcp", d instanceof Error ? d.message : String(d));
347
+ });
307
348
  }
308
349
  addMcpServers(e) {
309
- return y(this, null, function* () {
310
- return this.ready ? this.socket ? new Promise((o, a) => {
311
- this.socket && this.socket.emit("add_mcp_servers", e, (n) => {
312
- n.code === 200 ? (Object.keys(n.data).forEach((t) => {
313
- n.data[t].code !== 200 && console.error(t, "MCP Server 注册失败。", n.data[t]);
314
- }), o(!0)) : (console.error("MCP Server 注册失败。", n), a(n));
350
+ return v(this, null, function* () {
351
+ return this.ready ? this.socket ? new Promise((i, c) => {
352
+ this.socket && this.socket.emit("add_mcp_servers", e, (o) => {
353
+ o.code === 200 ? (Object.keys(o.data).forEach((d) => {
354
+ o.data[d].code !== 200 && r.report(d, "MCP Server 注册失败。", o.data[d]);
355
+ }), r.log("mcp:registered", Object.keys(o.data)), i(!0)) : (r.error("MCP Server 注册失败。", o), c(o));
315
356
  });
316
- }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
357
+ }) : (r.error("socket is undefined."), Promise.reject("socket is undefined")) : (r.error("session is not avalible."), Promise.reject("session is not avalible"));
317
358
  });
318
359
  }
319
360
  listMcpServers() {
320
- return y(this, null, function* () {
321
- return this.ready ? this.socket ? new Promise((e, o) => {
322
- this.socket && this.socket.emit("list_mcp_servers", (a) => {
323
- a.code === 200 ? e(a.data) : o(a);
361
+ return v(this, null, function* () {
362
+ return this.ready ? this.socket ? new Promise((e, i) => {
363
+ this.socket && this.socket.emit("list_mcp_servers", (c) => {
364
+ c.code === 200 ? e(c.data) : i(c);
324
365
  });
325
- }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
366
+ }) : (r.error("socket is undefined."), Promise.reject("socket is undefined")) : (r.error("session is not avalible."), Promise.reject("session is not avalible"));
326
367
  });
327
368
  }
328
- sendQuestion(e, o) {
329
- return y(this, null, function* () {
330
- var n, t, s;
369
+ sendQuestion(e, i) {
370
+ return v(this, null, function* () {
371
+ var o;
331
372
  this.convertKnowledgesToChatFormat(this.knowledges);
332
- let a = w(m({}, e), {
333
- promptVariables: this.promptVariables.filter((i) => i.type === A.User).reduce((i, d) => w(m({}, i), {
334
- [d.key]: d.value
373
+ let c = y(h({}, e), {
374
+ promptVariables: this.promptVariables.filter((d) => d.type === T.User).reduce((d, s) => y(h({}, d), {
375
+ [s.key]: s.value
335
376
  }), {}),
336
- systemPromptVariables: this.promptVariables.filter((i) => i.type === A.System).reduce((i, d) => w(m({}, i), {
337
- [d.key]: d.value
377
+ systemPromptVariables: this.promptVariables.filter((d) => d.type === T.System).reduce((d, s) => y(h({}, d), {
378
+ [s.key]: s.value
338
379
  }), {}),
339
- knowledges: (n = this.knowledges) == null ? void 0 : n.map((i) => i.id),
380
+ knowledges: (o = this.knowledges) == null ? void 0 : o.map((d) => d.id),
340
381
  //agent支持指定文件时再放开
341
382
  // knowledges: knowledgesForChat,
342
383
  enableTitleSummary: this.resolveEnableTitleSummary()
343
384
  });
344
385
  if (!this.active) {
345
386
  if (yield this.activate(), !this.socket) {
346
- const i = new Error(
347
- k.SocketConnectionFailed
387
+ const d = new Error(
388
+ R.SocketConnectionFailed
348
389
  // {
349
390
  // cause: { code: -1, message: 'socket is null' },
350
391
  // }
351
392
  );
352
- return f.warn("error", i), this.event.emit("error", i), !1;
393
+ return r.warn("error", d), this.event.emit("error", d), !1;
353
394
  }
354
- (t = this.socket) == null || t.emit("question", a);
395
+ this.emitQuestion(c);
355
396
  return;
356
397
  }
357
- (s = this.socket) == null || s.emit("question", a);
398
+ this.emitQuestion(c);
399
+ });
400
+ }
401
+ emitQuestion(e) {
402
+ var i;
403
+ r.log("question:request", Y(e)), (i = this.socket) == null || i.emit("question", e), r.log("question:sent", {
404
+ sessionId: this.id
358
405
  });
359
406
  }
360
407
  /**
361
408
  * 调用客户端插件:传入第三个参数 context(旧插件可忽略),结果最多回传一次,
362
409
  * 停止回答或调用被取消后不再回传。
363
410
  */
364
- invokeClientPlugin(e, o, a, n) {
365
- const t = new AbortController();
366
- this.inflightCalls.add(t);
411
+ invokeClientPlugin(e, i, c, o) {
412
+ const d = new AbortController();
413
+ this.inflightCalls.add(d);
367
414
  let s = !1;
368
- const i = (...l) => {
369
- s || (s = !0, this.inflightCalls.delete(t), !(this._stopAnswer || t.signal.aborted) && a(...l));
370
- }, d = {
371
- requestId: n.requestId || J(),
415
+ const t = (...g) => {
416
+ s || (s = !0, this.inflightCalls.delete(d), !(this._stopAnswer || d.signal.aborted) && c(...g));
417
+ }, n = {
418
+ requestId: o.requestId || $(),
372
419
  applicationId: this._applicationId,
373
420
  sessionId: this.id,
374
- pluginCode: n.pluginCode,
375
- functionCode: n.functionCode,
376
- signal: t.signal
421
+ pluginCode: o.pluginCode,
422
+ functionCode: o.functionCode,
423
+ signal: d.signal
377
424
  };
378
425
  try {
379
- const l = e(o, i, d);
380
- l && typeof l.catch == "function" && l.catch((g) => {
381
- f.error("client plugin failed", g), i({
426
+ const g = e(i, t, n);
427
+ g && typeof g.catch == "function" && g.catch((f) => {
428
+ r.error("client plugin failed", f), t({
382
429
  code: 500,
383
430
  message: "客户端插件执行失败。"
384
431
  });
385
432
  });
386
- } catch (l) {
387
- f.error("client plugin failed", l), i({
433
+ } catch (g) {
434
+ r.error("client plugin failed", g), t({
388
435
  code: 500,
389
436
  message: "客户端插件执行失败。"
390
437
  });
@@ -392,25 +439,51 @@ class Z extends D {
392
439
  }
393
440
  abortClientPluginCalls() {
394
441
  const e = Array.from(this.inflightCalls);
395
- this.inflightCalls.clear(), e.forEach((o) => o.abort());
442
+ this.inflightCalls.clear(), e.forEach((i) => i.abort());
443
+ }
444
+ /**
445
+ * 长连接静默换票:发 `update`,body 为 `{ resourceToken }`(无 Bearer,与建连一致)。
446
+ * 结果走 ack 或下一条 `ready`;失败只报错,不拆连接(服务端仍用旧 token)。
447
+ * 断线期间不发 `update`,只更新重连握手用的 token。
448
+ */
449
+ updateSocketResourceToken(e) {
450
+ if (!this.socket)
451
+ return;
452
+ const i = this.socket.io.opts.query;
453
+ i && (i.Authorization = e), this.socket.connected && (this.pendingTokenUpdate = !0, r.log("socket:update", this.id), this.socket.emit("update", {
454
+ resourceToken: e
455
+ }, (c) => {
456
+ this.pendingTokenUpdate && this.handleResourceTokenUpdateResult(c);
457
+ }));
458
+ }
459
+ handleResourceTokenUpdateResult(e) {
460
+ if (this.pendingTokenUpdate = !1, !e || e.code === 200) {
461
+ r.log("socket:update:ok", this.id);
462
+ return;
463
+ }
464
+ const i = new Error(R.SocketConnectionFailed, {
465
+ cause: e
466
+ });
467
+ r.error("socket:update:failed", e), this.event.emit("error", i);
396
468
  }
397
469
  stopAnswer() {
398
470
  var e;
399
- this.abortClientPluginCalls(), (e = this.socket) == null || e.emit("stop_answer");
471
+ r.log("stopAnswer", this.id), this.abortClientPluginCalls(), (e = this.socket) == null || e.emit("stop_answer");
400
472
  }
401
473
  inactivate(e = !1) {
402
- return this.abortClientPluginCalls(), super.inactivate(e);
474
+ return !e && this.frozen && !!this.socket || this.abortClientPluginCalls(), super.inactivate(e);
403
475
  }
404
476
  destroy() {
405
- return this.abortClientPluginCalls(), super.destroy();
477
+ var e;
478
+ return (e = this.stopLocalMcpUpdates) == null || e.call(this), this.stopLocalMcpUpdates = void 0, this.abortClientPluginCalls(), super.destroy();
406
479
  }
407
480
  convertKnowledgesToChatFormat(e) {
408
- return e ? e.map((o) => o != null && o.fileIds ? {
409
- id: o.id,
410
- fileIds: o.fileIds
411
- } : o.id) : [];
481
+ return e ? e.map((i) => i != null && i.fileIds ? {
482
+ id: i.id,
483
+ fileIds: i.fileIds
484
+ } : i.id) : [];
412
485
  }
413
486
  }
414
487
  export {
415
- Z as default
488
+ re as default
416
489
  };