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

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