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