@glodon-aiot/bot-client-sdk 3.25.1-alpha.7 → 3.26.0-alpha.5

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 (96) hide show
  1. package/README.md +572 -98
  2. package/dist/es/AgentSession.mjs +230 -165
  3. package/dist/es/BotClient.mjs +186 -136
  4. package/dist/es/Config.mjs +89 -26
  5. package/dist/es/Session.mjs +12 -0
  6. package/dist/es/api/index.mjs +42 -33
  7. package/dist/es/builtinTools/browserRuntime/BrowserCommandRunner.mjs +201 -0
  8. package/dist/es/builtinTools/browserRuntime/PythonRunner.mjs +356 -0
  9. package/dist/es/builtinTools/browserRuntime/WorkspaceBashFs.mjs +350 -0
  10. package/dist/es/builtinTools/browserRuntime/assets.mjs +9 -0
  11. package/dist/es/builtinTools/browserRuntime/loadBash.mjs +17 -0
  12. package/dist/es/builtinTools/browserRuntime/pyodideRunner.py.mjs +96 -0
  13. package/dist/es/builtinTools/browserRuntime/pyodideWorker.mjs +242 -0
  14. package/dist/es/builtinTools/command.mjs +56 -0
  15. package/dist/es/builtinTools/constants.mjs +10 -0
  16. package/dist/es/builtinTools/createBuiltinToolsPlugin.mjs +236 -0
  17. package/dist/es/builtinTools/overview.mjs +77 -0
  18. package/dist/es/builtinTools/paths.mjs +63 -0
  19. package/dist/es/builtinTools/schemas.mjs +216 -0
  20. package/dist/es/builtinTools/tools.mjs +471 -0
  21. package/dist/es/index.mjs +69 -9
  22. package/dist/es/localFiles/adapters/FileSystemAccessAdapter.mjs +264 -0
  23. package/dist/es/localFiles/adapters/HostBridgeAdapter.mjs +233 -0
  24. package/dist/es/localFiles/adapters/MemoryWorkspaceAdapter.mjs +197 -0
  25. package/dist/es/localFiles/adapters/WorkspaceFileAdapter.mjs +54 -0
  26. package/dist/es/localFiles/adapters/fsaTypes.mjs +9 -0
  27. package/dist/es/localFiles/constants.mjs +47 -0
  28. package/dist/es/localFiles/createLocalFilesPlugin.mjs +255 -0
  29. package/dist/es/localFiles/errors.mjs +57 -0
  30. package/dist/es/localFiles/host/connectHostBridge.mjs +54 -0
  31. package/dist/es/localFiles/host/protocol.mjs +27 -0
  32. package/dist/es/localFiles/limits.mjs +22 -0
  33. package/dist/es/localFiles/schemas.mjs +312 -0
  34. package/dist/es/localFiles/security/downloadUrlPolicy.mjs +39 -0
  35. package/dist/es/localFiles/security/glob.mjs +28 -0
  36. package/dist/es/localFiles/security/pathPolicy.mjs +59 -0
  37. package/dist/es/localFiles/security/sensitiveFiles.mjs +115 -0
  38. package/dist/es/localFiles/tools/readTools.mjs +333 -0
  39. package/dist/es/localFiles/tools/runtime.mjs +108 -0
  40. package/dist/es/localFiles/tools/transferTools.mjs +228 -0
  41. package/dist/es/localFiles/tools/writeTools.mjs +173 -0
  42. package/dist/es/localFiles/transfer/SessionFileTransfer.mjs +111 -0
  43. package/dist/es/localFiles/validate.mjs +66 -0
  44. package/dist/es/localFiles/workspace/LocalWorkspace.mjs +358 -0
  45. package/dist/es/localFiles/workspace/handleStore.mjs +79 -0
  46. package/dist/es/localFiles/workspace/watchDirectory.mjs +38 -0
  47. package/dist/es/package.json.mjs +38 -29
  48. package/dist/lib/index.js +373 -5
  49. package/dist/src/AgentSession.d.ts +9 -0
  50. package/dist/src/BotClient.d.ts +12 -2
  51. package/dist/src/Config.d.ts +22 -0
  52. package/dist/src/Session.d.ts +5 -1
  53. package/dist/src/api/index.d.ts +3 -1
  54. package/dist/src/builtinTools/browserRuntime/BrowserCommandRunner.d.ts +26 -0
  55. package/dist/src/builtinTools/browserRuntime/PythonRunner.d.ts +67 -0
  56. package/dist/src/builtinTools/browserRuntime/WorkspaceBashFs.d.ts +51 -0
  57. package/dist/src/builtinTools/browserRuntime/assets.d.ts +13 -0
  58. package/dist/src/builtinTools/browserRuntime/loadBash.d.ts +3 -0
  59. package/dist/src/builtinTools/browserRuntime/pyodideWorker.d.ts +44 -0
  60. package/dist/src/builtinTools/command.d.ts +26 -0
  61. package/dist/src/builtinTools/constants.d.ts +17 -0
  62. package/dist/src/builtinTools/createBuiltinToolsPlugin.d.ts +11 -0
  63. package/dist/src/builtinTools/overview.d.ts +5 -0
  64. package/dist/src/builtinTools/paths.d.ts +15 -0
  65. package/dist/src/builtinTools/schemas.d.ts +3 -0
  66. package/dist/src/builtinTools/tools.d.ts +60 -0
  67. package/dist/src/index.d.ts +4 -2
  68. package/dist/src/localFiles/adapters/FileSystemAccessAdapter.d.ts +20 -0
  69. package/dist/src/localFiles/adapters/HostBridgeAdapter.d.ts +43 -0
  70. package/dist/src/localFiles/adapters/MemoryWorkspaceAdapter.d.ts +21 -0
  71. package/dist/src/localFiles/adapters/WorkspaceFileAdapter.d.ts +40 -0
  72. package/dist/src/localFiles/adapters/fsaTypes.d.ts +41 -0
  73. package/dist/src/localFiles/constants.d.ts +51 -0
  74. package/dist/src/localFiles/createLocalFilesPlugin.d.ts +11 -0
  75. package/dist/src/localFiles/createLocalPythonPlugin.d.ts +12 -0
  76. package/dist/src/localFiles/errors.d.ts +34 -0
  77. package/dist/src/localFiles/host/connectHostBridge.d.ts +6 -0
  78. package/dist/src/localFiles/host/protocol.d.ts +46 -0
  79. package/dist/src/localFiles/index.d.ts +22 -0
  80. package/dist/src/localFiles/limits.d.ts +3 -0
  81. package/dist/src/localFiles/schemas.d.ts +29 -0
  82. package/dist/src/localFiles/security/downloadUrlPolicy.d.ts +4 -0
  83. package/dist/src/localFiles/security/glob.d.ts +3 -0
  84. package/dist/src/localFiles/security/pathPolicy.d.ts +14 -0
  85. package/dist/src/localFiles/security/sensitiveFiles.d.ts +23 -0
  86. package/dist/src/localFiles/tools/readTools.d.ts +7 -0
  87. package/dist/src/localFiles/tools/runtime.d.ts +43 -0
  88. package/dist/src/localFiles/tools/transferTools.d.ts +4 -0
  89. package/dist/src/localFiles/tools/writeTools.d.ts +4 -0
  90. package/dist/src/localFiles/transfer/SessionFileTransfer.d.ts +40 -0
  91. package/dist/src/localFiles/types.d.ts +319 -0
  92. package/dist/src/localFiles/validate.d.ts +2 -0
  93. package/dist/src/localFiles/workspace/LocalWorkspace.d.ts +111 -0
  94. package/dist/src/localFiles/workspace/handleStore.d.ts +11 -0
  95. package/dist/src/localFiles/workspace/watchDirectory.d.ts +7 -0
  96. package/package.json +13 -4
@@ -1,98 +1,122 @@
1
- var b = Object.defineProperty, T = Object.defineProperties;
2
- var x = Object.getOwnPropertyDescriptors;
3
- var _ = Object.getOwnPropertySymbols;
4
- var E = Object.prototype.hasOwnProperty, L = Object.prototype.propertyIsEnumerable;
5
- var A = (m, t, i) => t in m ? b(m, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : m[t] = i, d = (m, t) => {
6
- for (var i in t || (t = {}))
7
- E.call(t, i) && A(m, i, t[i]);
8
- if (_)
9
- for (var i of _(t))
10
- L.call(t, i) && A(m, i, t[i]);
11
- return m;
12
- }, w = (m, t) => T(m, x(t));
13
- var p = (m, t, i) => new Promise((o, r) => {
14
- var s = (a) => {
1
+ var T = Object.defineProperty, j = Object.defineProperties;
2
+ var L = Object.getOwnPropertyDescriptors;
3
+ var S = Object.getOwnPropertySymbols;
4
+ var F = Object.prototype.hasOwnProperty, N = Object.prototype.propertyIsEnumerable;
5
+ var M = (r, c, e) => c in r ? T(r, c, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[c] = e, h = (r, c) => {
6
+ for (var e in c || (c = {}))
7
+ F.call(c, e) && M(r, e, c[e]);
8
+ if (S)
9
+ for (var e of S(c))
10
+ N.call(c, e) && M(r, e, c[e]);
11
+ return r;
12
+ }, w = (r, c) => j(r, L(c));
13
+ var b = (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) => {
15
16
  try {
16
- n(i.next(a));
17
- } catch (h) {
18
- r(h);
17
+ s(e.next(i));
18
+ } catch (d) {
19
+ a(d);
19
20
  }
20
- }, e = (a) => {
21
+ }, t = (i) => {
21
22
  try {
22
- n(i.throw(a));
23
- } catch (h) {
24
- r(h);
23
+ s(e.throw(i));
24
+ } catch (d) {
25
+ a(d);
25
26
  }
26
- }, n = (a) => a.done ? o(a.value) : Promise.resolve(a.value).then(s, e);
27
- n((i = i.apply(m, t)).next());
27
+ }, s = (i) => i.done ? o(i.value) : Promise.resolve(i.value).then(n, t);
28
+ s((e = e.apply(r, c)).next());
28
29
  });
29
- import { ChatMessageRole as v, PromptVariableType as P } from "@glodon-aiot/apis";
30
- import q from "./Logger.mjs";
31
- import { configuration as y } from "./Config.mjs";
32
- import { io as F } from "socket.io-client";
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";
33
34
  import { UserMode as I } from "./PayloadTypes.mjs";
34
- import N, { SessionErrors as M } from "./Session.mjs";
35
- import U from "dayjs";
36
- const u = new q("[AgentSession] ");
37
- class K extends N {
38
- constructor(t, i = "", o, r, s, e) {
39
- super(t, i, o, r, s, e);
35
+ import D, { SessionErrors as k } from "./Session.mjs";
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)
44
+ try {
45
+ return JSON.parse(c);
46
+ } catch (e) {
47
+ return r;
48
+ }
49
+ }
50
+ function x(r) {
51
+ return r == null || r === "" || typeof r == "object" && !Array.isArray(r) && Object.keys(r).length === 0;
52
+ }
53
+ function Y(r, c) {
54
+ const e = _(r == null ? void 0 : r.query);
55
+ if (!x(e))
56
+ return e;
57
+ const o = _(c);
58
+ return x(o) && e != null ? e : o;
59
+ }
60
+ class Z extends D {
61
+ constructor(e, o = "", a, n, t, s) {
62
+ super(e, o, a, n, t, s);
63
+ b(this, "inflightCalls", /* @__PURE__ */ new Set());
40
64
  }
41
65
  prepareConnection() {
42
- return new Promise((t) => p(this, null, function* () {
43
- this.socket && t(!0);
44
- const i = {
66
+ return new Promise((e) => y(this, null, function* () {
67
+ this.socket && e(!0);
68
+ const o = {
45
69
  version: "1.0",
46
- stream: JSON.stringify(y.stream),
70
+ stream: JSON.stringify(C.stream),
47
71
  appId: this._applicationId,
48
72
  sessionId: this.id,
49
73
  Authorization: `${this.token}`,
50
- userMode: y.debug ? I.Debug : I.Normal
51
- }, o = new URL(y.apiRoot), r = yield F(o.origin, {
52
- path: o.pathname.replace("api", "socketio") + "/copilot",
53
- query: i,
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,
54
78
  transports: ["websocket"]
55
79
  });
56
- r.on("connect", () => {
57
- u.log("socket:connect"), this.event.emit("socket:connect"), r.emit("init");
58
- }), r.on("disconnect", () => {
59
- u.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
60
- }), r.on("ready", (s) => {
61
- if (s.code === 200) {
62
- u.log("ready");
63
- const e = y.debug ? I.Debug : I.Normal;
64
- e === I.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), t(!0);
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);
65
89
  } else {
66
90
  this._ready && (this._ready = !1);
67
- const e = new Error(
68
- M.SocketConnectionFailed
91
+ const s = new Error(
92
+ k.SocketConnectionFailed
69
93
  // , {
70
94
  // cause: data,
71
95
  // }
72
96
  );
73
- u.error("error", e), this.event.emit("error", e), t(!1);
97
+ f.error("error", s), this.event.emit("error", s), e(!1);
74
98
  }
75
- }), r.on("action", (s, e) => p(this, null, function* () {
76
- var k;
99
+ }), n.on("action", (t, s) => y(this, null, function* () {
100
+ var R;
77
101
  if (this._stopAnswer || !this.id)
78
102
  return;
79
103
  const {
80
- type: n,
104
+ type: i,
81
105
  data: {
82
- runnerIsClient: a,
83
- pluginCode: h,
106
+ runnerIsClient: d,
107
+ pluginCode: l,
84
108
  funcCode: g,
85
- parameters: c,
86
- requestBody: j
109
+ parameters: m,
110
+ requestBody: E
87
111
  }
88
- } = s, l = this.messages[this.messages.length - 1];
89
- if (!l.messageId)
90
- l.actions || (l.actions = []), l.actions[s.data.step - 1] || (l.actions[s.data.step - 1] = []), l.actions[s.data.step - 1].push(s), this.event.emit("message:action", {
91
- messageId: l.messageId,
92
- action: s
93
- }), this.event.emit("message:updated", l);
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);
94
118
  else {
95
- const f = {
119
+ const p = {
96
120
  messageId: "",
97
121
  content: "",
98
122
  role: v.Robot,
@@ -101,20 +125,20 @@ class K extends N {
101
125
  searchReference: [],
102
126
  isLoading: !0,
103
127
  messageTime: "",
104
- actions: [[s]],
128
+ actions: [[t]],
105
129
  comment: {
106
130
  isAgree: 0
107
131
  },
108
132
  isReplying: !0
109
133
  };
110
- this.pushMessage(f), this.event.emit("message:action", {
111
- messageId: f.messageId,
112
- action: s
134
+ this.pushMessage(p), this.event.emit("message:action", {
135
+ messageId: p.messageId,
136
+ action: t
113
137
  });
114
138
  }
115
139
  if (this.inactivating) {
116
- const f = new Error(
117
- M.Inactive
140
+ const p = new Error(
141
+ k.Inactive
118
142
  // {
119
143
  // cause: {
120
144
  // code: -1,
@@ -123,50 +147,48 @@ class K extends N {
123
147
  // },
124
148
  // }
125
149
  );
126
- u.warn("error", f), this.event.emit("error", f);
127
- } else if (n === "FUNCTION_CALL" && a) {
128
- u.log(`calling function ${g} of plugin ${h} with argument:
129
- ${JSON.stringify(c)}`);
130
- const f = (k = y.plugins) == null ? void 0 : k[h];
131
- if (!f) {
132
- e({
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(m)}`);
154
+ const p = (R = C.plugins) == null ? void 0 : R[l];
155
+ if (!p) {
156
+ s({
133
157
  code: 404,
134
158
  message: "没有找到插件。"
135
159
  });
136
160
  return;
137
161
  }
138
- const R = f[g];
139
- if (typeof R != "function") {
140
- e({
162
+ const P = p[g];
163
+ if (typeof P != "function") {
164
+ s({
141
165
  code: 404,
142
166
  message: "没有找到插件方法。"
143
167
  });
144
168
  return;
145
169
  }
146
- let S = c == null ? void 0 : c.query;
147
- try {
148
- S = JSON.parse(c == null ? void 0 : c.query);
149
- } catch (C) {
150
- }
151
- R(S, (...C) => {
152
- this._stopAnswer || e(...C);
170
+ const q = Y(m, E);
171
+ this.invokeClientPlugin(P, q, s, {
172
+ requestId: t.requestId,
173
+ pluginCode: l,
174
+ functionCode: g
153
175
  });
154
176
  }
155
- })), r.on("answer", (s) => {
177
+ })), n.on("answer", (t) => {
156
178
  if (!this._stopAnswer)
157
- if (s.data.code !== 200 && (s.data.index = -1), s.data.stream || s.data.stream === void 0) {
158
- let e = this.messages[this.messages.length - 1];
159
- if (e && e.role === v.Robot)
160
- this.handleStreamAnswer(s.data, e), this.messages[this.messages.length - 1] = e, this.event.emit("message:content", e), this.event.emit("message:updated", e), e.index === -1 && (e.isReplying = !1, this.event.emit("answer:finish", e));
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));
161
183
  else {
162
- const n = {
184
+ const i = {
163
185
  content: "",
164
186
  role: v.Robot,
165
187
  reference: [],
166
188
  searchReference: [],
167
189
  messageId: "",
168
190
  sessionId: this.id,
169
- messageTime: U(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
191
+ messageTime: V(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
170
192
  comment: {
171
193
  isAgree: 0
172
194
  },
@@ -181,14 +203,14 @@ class K extends N {
181
203
  thinkingStatus: "",
182
204
  searchingStatus: ""
183
205
  };
184
- 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));
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));
185
207
  }
186
- } else if (s.data.updateSession && this.data && (this.data = d(d({}, this.data), s.data.updateSession), this.event.emit("data:updated", this.data)), s.data.index === -1) {
187
- let e = this.messages[this.messages.length - 1];
188
- if (e && !e.messageId)
189
- e = d(d({}, e), s.data), this.event.emit("message:content", e), this.event.emit("message:updated", e), this.event.emit("answer:finish", e);
208
+ } 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) {
209
+ let s = this.messages[this.messages.length - 1];
210
+ if (s && !s.messageId)
211
+ s = h(h({}, s), t.data), this.event.emit("message:content", s), this.event.emit("message:updated", s), this.event.emit("answer:finish", s);
190
212
  else {
191
- const n = d({
213
+ const i = h({
192
214
  messageId: "",
193
215
  content: "",
194
216
  role: v.Robot,
@@ -202,12 +224,12 @@ class K extends N {
202
224
  isAgree: 0
203
225
  },
204
226
  isReplying: !0
205
- }, s.data);
206
- this.event.emit("message:content", n), this.pushMessage(n), this.event.emit("answer:finish", n);
227
+ }, t.data);
228
+ this.event.emit("message:content", i), this.pushMessage(i), this.event.emit("answer:finish", i);
207
229
  }
208
230
  } else {
209
- let e = this.messages[this.messages.length - 1];
210
- e && !e.messageId ? (e = d(d({}, e), s.data), this.messages[this.messages.length - 1] = e, this.event.emit("message:content", e), this.event.emit("message:updated", e)) : (this.pushMessage(d({
231
+ let s = this.messages[this.messages.length - 1];
232
+ 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({
211
233
  messageId: "",
212
234
  content: "",
213
235
  role: v.Robot,
@@ -221,31 +243,31 @@ class K extends N {
221
243
  isAgree: 0
222
244
  },
223
245
  isReplying: !0
224
- }, s.data)), this.event.emit("message:content", s.data));
246
+ }, t.data)), this.event.emit("message:content", t.data));
225
247
  }
226
- }), r.on("reference", (s) => p(this, null, function* () {
248
+ }), n.on("reference", (t) => y(this, null, function* () {
227
249
  if (this._stopAnswer)
228
250
  return;
229
251
  if (!this.id) {
230
- const c = new Error(
231
- M.IdUndefined
252
+ const m = new Error(
253
+ k.IdUndefined
232
254
  // {
233
255
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
234
256
  // }
235
257
  );
236
- u.error(c), this.event.emit("error", c), this.frozen = !1;
258
+ f.error(m), this.event.emit("error", m), this.frozen = !1;
237
259
  return;
238
260
  }
239
261
  const {
240
- type: e,
241
- data: n,
242
- requestId: a
243
- } = s, h = this.messages.findIndex((c) => c.messageId === a && c.role === v.User);
244
- if (h < 0)
262
+ type: s,
263
+ data: i,
264
+ requestId: d
265
+ } = t, l = this.messages.findIndex((m) => m.messageId === d && m.role === v.User);
266
+ if (l < 0)
245
267
  return;
246
- const g = h + 1;
268
+ const g = l + 1;
247
269
  if (g >= this.messages.length) {
248
- const c = {
270
+ const m = {
249
271
  messageId: "",
250
272
  content: "",
251
273
  role: v.Robot,
@@ -260,61 +282,61 @@ class K extends N {
260
282
  },
261
283
  isReplying: !0
262
284
  };
263
- this.pushMessage(c);
285
+ this.pushMessage(m);
264
286
  }
265
- e === "KG" ? (this.messages[g] = w(d({}, this.messages[g]), {
266
- reference: n.knowledgeReference
267
- }), yield this.fillMessageReference(this.messages[g]), this.event.emit("message:reference", n.knowledgeReference), this.event.emit("message:updated", this.messages[g])) : e === "WEB_SEARCH" && (this.messages[g] = w(d({}, this.messages[g]), {
268
- searchReference: n.knowledgeReference
269
- }), this.event.emit("message:searchreference", n.searchReference), this.event.emit("message:updated", this.messages[g]));
270
- })), this.socket = r, r.open();
287
+ s === "KG" ? (this.messages[g] = w(h({}, 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(h({}, 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();
271
293
  }));
272
294
  }
273
295
  registerMcpFromClientConfig() {
274
- return y.mcp ? new Promise((t, i) => {
296
+ return C.mcp ? new Promise((e, o) => {
275
297
  if (!this.socket) {
276
- i("socket is undefined");
298
+ o("socket is undefined");
277
299
  return;
278
300
  }
279
- this.socket.emit("add_mcp_servers", y.mcp, (o) => {
280
- o.code === 200 ? (Object.keys(o.data).forEach((r) => {
281
- o.data[r].code !== 200 && console.error(r, "MCP Server 注册失败。", o.data[r]);
282
- }), t(!0)) : (console.error("MCP Server 注册失败。", o), i(o));
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));
283
305
  });
284
306
  }) : Promise.resolve(!0);
285
307
  }
286
- addMcpServers(t) {
287
- return p(this, null, function* () {
288
- return this.ready ? this.socket ? new Promise((i, o) => {
289
- this.socket && this.socket.emit("add_mcp_servers", t, (r) => {
290
- r.code === 200 ? (Object.keys(r.data).forEach((s) => {
291
- r.data[s].code !== 200 && console.error(s, "MCP Server 注册失败。", r.data[s]);
292
- }), i(!0)) : (console.error("MCP Server 注册失败。", r), o(r));
308
+ 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));
293
315
  });
294
316
  }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
295
317
  });
296
318
  }
297
319
  listMcpServers() {
298
- return p(this, null, function* () {
299
- return this.ready ? this.socket ? new Promise((t, i) => {
300
- this.socket && this.socket.emit("list_mcp_servers", (o) => {
301
- o.code === 200 ? t(o.data) : i(o);
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);
302
324
  });
303
325
  }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
304
326
  });
305
327
  }
306
- sendQuestion(t, i) {
307
- return p(this, null, function* () {
308
- var r, s, e;
328
+ sendQuestion(e, o) {
329
+ return y(this, null, function* () {
330
+ var n, t, s;
309
331
  this.convertKnowledgesToChatFormat(this.knowledges);
310
- let o = w(d({}, t), {
311
- promptVariables: this.promptVariables.filter((n) => n.type === P.User).reduce((n, a) => w(d({}, n), {
312
- [a.key]: a.value
332
+ let a = w(h({}, e), {
333
+ promptVariables: this.promptVariables.filter((i) => i.type === A.User).reduce((i, d) => w(h({}, i), {
334
+ [d.key]: d.value
313
335
  }), {}),
314
- systemPromptVariables: this.promptVariables.filter((n) => n.type === P.System).reduce((n, a) => w(d({}, n), {
315
- [a.key]: a.value
336
+ systemPromptVariables: this.promptVariables.filter((i) => i.type === A.System).reduce((i, d) => w(h({}, i), {
337
+ [d.key]: d.value
316
338
  }), {}),
317
- knowledges: (r = this.knowledges) == null ? void 0 : r.map((n) => n.id),
339
+ knowledges: (n = this.knowledges) == null ? void 0 : n.map((i) => i.id),
318
340
  //agent支持指定文件时再放开
319
341
  // knowledges: knowledgesForChat,
320
342
  //开启内容摘要
@@ -322,31 +344,74 @@ class K extends N {
322
344
  });
323
345
  if (!this.active) {
324
346
  if (yield this.activate(), !this.socket) {
325
- const n = new Error(
326
- M.SocketConnectionFailed
347
+ const i = new Error(
348
+ k.SocketConnectionFailed
327
349
  // {
328
350
  // cause: { code: -1, message: 'socket is null' },
329
351
  // }
330
352
  );
331
- return u.warn("error", n), this.event.emit("error", n), !1;
353
+ return f.warn("error", i), this.event.emit("error", i), !1;
332
354
  }
333
- (s = this.socket) == null || s.emit("question", o);
355
+ (t = this.socket) == null || t.emit("question", a);
334
356
  return;
335
357
  }
336
- (e = this.socket) == null || e.emit("question", o);
358
+ (s = this.socket) == null || s.emit("question", a);
337
359
  });
338
360
  }
361
+ /**
362
+ * 调用客户端插件:传入第三个参数 context(旧插件可忽略),结果最多回传一次,
363
+ * 停止回答或调用被取消后不再回传。
364
+ */
365
+ invokeClientPlugin(e, o, a, n) {
366
+ const t = new AbortController();
367
+ this.inflightCalls.add(t);
368
+ let s = !1;
369
+ const i = (...l) => {
370
+ s || (s = !0, this.inflightCalls.delete(t), !(this._stopAnswer || t.signal.aborted) && a(...l));
371
+ }, d = {
372
+ requestId: n.requestId || J(),
373
+ applicationId: this._applicationId,
374
+ sessionId: this.id,
375
+ pluginCode: n.pluginCode,
376
+ functionCode: n.functionCode,
377
+ signal: t.signal
378
+ };
379
+ try {
380
+ const l = e(o, i, d);
381
+ l && typeof l.catch == "function" && l.catch((g) => {
382
+ f.error("client plugin failed", g), i({
383
+ code: 500,
384
+ message: "客户端插件执行失败。"
385
+ });
386
+ });
387
+ } catch (l) {
388
+ f.error("client plugin failed", l), i({
389
+ code: 500,
390
+ message: "客户端插件执行失败。"
391
+ });
392
+ }
393
+ }
394
+ abortClientPluginCalls() {
395
+ const e = Array.from(this.inflightCalls);
396
+ this.inflightCalls.clear(), e.forEach((o) => o.abort());
397
+ }
339
398
  stopAnswer() {
340
- var t;
341
- (t = this.socket) == null || t.emit("stop_answer");
399
+ var e;
400
+ this.abortClientPluginCalls(), (e = this.socket) == null || e.emit("stop_answer");
401
+ }
402
+ inactivate(e = !1) {
403
+ return this.abortClientPluginCalls(), super.inactivate(e);
404
+ }
405
+ destroy() {
406
+ return this.abortClientPluginCalls(), super.destroy();
342
407
  }
343
- convertKnowledgesToChatFormat(t) {
344
- return t ? t.map((i) => i != null && i.fileIds ? {
345
- id: i.id,
346
- fileIds: i.fileIds
347
- } : i.id) : [];
408
+ convertKnowledgesToChatFormat(e) {
409
+ return e ? e.map((o) => o != null && o.fileIds ? {
410
+ id: o.id,
411
+ fileIds: o.fileIds
412
+ } : o.id) : [];
348
413
  }
349
414
  }
350
415
  export {
351
- K as default
416
+ Z as default
352
417
  };