@glodon-aiot/bot-client-sdk 3.7.0-alpha.3 → 3.8.0-alpha.4

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,88 +1,88 @@
1
- var A = Object.defineProperty, C = Object.defineProperties;
2
- var P = Object.getOwnPropertyDescriptors;
3
- var b = Object.getOwnPropertySymbols;
4
- var x = Object.prototype.hasOwnProperty, E = Object.prototype.propertyIsEnumerable;
5
- var S = (d, i, r) => i in d ? A(d, i, { enumerable: !0, configurable: !0, writable: !0, value: r }) : d[i] = r, m = (d, i) => {
6
- for (var r in i || (i = {}))
7
- x.call(i, r) && S(d, r, i[r]);
8
- if (b)
9
- for (var r of b(i))
10
- E.call(i, r) && S(d, r, i[r]);
11
- return d;
12
- }, w = (d, i) => C(d, P(i));
13
- var v = (d, i, r) => new Promise((o, n) => {
1
+ var A = Object.defineProperty, P = Object.defineProperties;
2
+ var b = Object.getOwnPropertyDescriptors;
3
+ var S = Object.getOwnPropertySymbols;
4
+ var T = Object.prototype.hasOwnProperty, x = Object.prototype.propertyIsEnumerable;
5
+ var C = (m, t, i) => t in m ? A(m, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : m[t] = i, d = (m, t) => {
6
+ for (var i in t || (t = {}))
7
+ T.call(t, i) && C(m, i, t[i]);
8
+ if (S)
9
+ for (var i of S(t))
10
+ x.call(t, i) && C(m, i, t[i]);
11
+ return m;
12
+ }, w = (m, t) => P(m, b(t));
13
+ var v = (m, t, i) => new Promise((o, r) => {
14
14
  var s = (a) => {
15
15
  try {
16
- t(r.next(a));
17
- } catch (h) {
18
- n(h);
16
+ n(i.next(a));
17
+ } catch (g) {
18
+ r(g);
19
19
  }
20
20
  }, e = (a) => {
21
21
  try {
22
- t(r.throw(a));
23
- } catch (h) {
24
- n(h);
22
+ n(i.throw(a));
23
+ } catch (g) {
24
+ r(g);
25
25
  }
26
- }, t = (a) => a.done ? o(a.value) : Promise.resolve(a.value).then(s, e);
27
- t((r = r.apply(d, i)).next());
26
+ }, n = (a) => a.done ? o(a.value) : Promise.resolve(a.value).then(s, e);
27
+ n((i = i.apply(m, t)).next());
28
28
  });
29
29
  import { ChatMessageRole as y, PromptVariableType as _ } from "@glodon-aiot/apis";
30
- import L from "./Logger.mjs";
30
+ import E from "./Logger.mjs";
31
31
  import { configuration as u } from "./Config.mjs";
32
- import { io as T } from "socket.io-client";
33
- import { UserMode as k } from "./PayloadTypes.mjs";
34
- import q, { SessionErrors as I } from "./Session.mjs";
35
- import N from "dayjs";
36
- const p = new L("[AgentSession] ");
32
+ import { io as L } from "socket.io-client";
33
+ import { UserMode as I } from "./PayloadTypes.mjs";
34
+ import q, { SessionErrors as M } from "./Session.mjs";
35
+ import F from "dayjs";
36
+ const p = new E("[AgentSession] ");
37
37
  class H extends q {
38
- constructor(i, r = "", o, n, s, e) {
39
- super(i, r, o, n, s, e);
38
+ constructor(t, i = "", o, r, s, e) {
39
+ super(t, i, o, r, s, e);
40
40
  }
41
41
  prepareConnection() {
42
- return new Promise((i) => v(this, null, function* () {
43
- this.socket && i(!0);
44
- const r = {
42
+ return new Promise((t) => v(this, null, function* () {
43
+ this.socket && t(!0);
44
+ const i = {
45
45
  version: "1.0",
46
46
  stream: JSON.stringify(u.stream),
47
47
  appId: this._applicationId,
48
48
  sessionId: this.id,
49
49
  Authorization: `${this.token}`,
50
- userMode: u.debug ? k.Debug : k.Normal
51
- }, o = new URL(u.apiRoot), n = yield T(o.origin, {
50
+ userMode: u.debug ? I.Debug : I.Normal
51
+ }, o = new URL(u.apiRoot), r = yield L(o.origin, {
52
52
  path: o.pathname.replace("api", "socketio") + "/copilot",
53
- query: r,
53
+ query: i,
54
54
  transports: ["websocket"]
55
55
  });
56
- n.on("connect", () => {
57
- p.log("socket:connect"), this.event.emit("socket:connect"), n.emit("init");
58
- }), n.on("disconnect", () => {
56
+ r.on("connect", () => {
57
+ p.log("socket:connect"), this.event.emit("socket:connect"), r.emit("init");
58
+ }), r.on("disconnect", () => {
59
59
  p.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
60
- }), n.on("ready", (s) => {
60
+ }), r.on("ready", (s) => {
61
61
  if (s.code === 200) {
62
62
  p.log("ready");
63
- const e = u.debug ? k.Debug : k.Normal;
64
- e === k.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), i(!0);
63
+ const e = u.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);
65
65
  } else {
66
66
  this._ready && (this._ready = !1);
67
67
  const e = new Error(
68
- I.SocketConnectionFailed
68
+ M.SocketConnectionFailed
69
69
  // , {
70
70
  // cause: data,
71
71
  // }
72
72
  );
73
- p.error("error", e), this.event.emit("error", e), i(!1);
73
+ p.error("error", e), this.event.emit("error", e), t(!1);
74
74
  }
75
- }), n.on("action", (s, e) => v(this, null, function* () {
75
+ }), r.on("action", (s, e) => v(this, null, function* () {
76
76
  if (this._stopAnswer || !this.id)
77
77
  return;
78
78
  const {
79
- type: t,
79
+ type: n,
80
80
  data: {
81
81
  runnerIsClient: a,
82
- pluginCode: h,
83
- funcCode: g,
82
+ pluginCode: g,
83
+ funcCode: h,
84
84
  parameters: c,
85
- requestBody: U
85
+ requestBody: N
86
86
  }
87
87
  } = s, l = this.messages[this.messages.length - 1];
88
88
  if (!l.messageId)
@@ -113,7 +113,7 @@ class H extends q {
113
113
  }
114
114
  if (this.inactivating) {
115
115
  const f = new Error(
116
- I.Inactive
116
+ M.Inactive
117
117
  // {
118
118
  // cause: {
119
119
  // code: -1,
@@ -123,57 +123,57 @@ class H extends q {
123
123
  // }
124
124
  );
125
125
  p.warn("error", f), this.event.emit("error", f);
126
- } else if (t === "FUNCTION_CALL" && a) {
127
- if (p.log(`calling function ${g} of plugin ${h} with argument:
128
- ${JSON.stringify(c)}`), !u.plugins[h]) {
126
+ } else if (n === "FUNCTION_CALL" && a) {
127
+ if (p.log(`calling function ${h} of plugin ${g} with argument:
128
+ ${JSON.stringify(c)}`), !u.plugins[g]) {
129
129
  e({
130
130
  code: 404,
131
131
  message: "对不起,没有找到插件。"
132
132
  });
133
133
  return;
134
134
  }
135
- const f = u.plugins[h][g];
135
+ const f = u.plugins[g][h];
136
136
  typeof f != "function" && e({
137
137
  code: 404,
138
138
  message: "对不起,没有找到插件方法。"
139
139
  });
140
- let M = c == null ? void 0 : c.query;
140
+ let R = c == null ? void 0 : c.query;
141
141
  try {
142
- M = JSON.parse(c == null ? void 0 : c.query);
143
- } catch (R) {
142
+ R = JSON.parse(c == null ? void 0 : c.query);
143
+ } catch (k) {
144
144
  }
145
- f(M, (...R) => {
146
- this._stopAnswer || e(...R);
145
+ f(R, (...k) => {
146
+ this._stopAnswer || e(...k);
147
147
  });
148
148
  }
149
- })), n.on("answer", (s) => {
149
+ })), r.on("answer", (s) => {
150
150
  if (!this._stopAnswer)
151
151
  if (s.data.code !== 200 && (s.data.index = -1), s.data.stream || s.data.stream === void 0) {
152
152
  let e = this.messages[this.messages.length - 1];
153
153
  if (e && e.role === y.Robot)
154
154
  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));
155
155
  else {
156
- const t = {
156
+ const n = {
157
157
  content: "",
158
158
  role: y.Robot,
159
159
  reference: [],
160
160
  searchReference: [],
161
161
  messageId: "",
162
162
  sessionId: this.id,
163
- messageTime: N(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
163
+ messageTime: F(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
164
164
  comment: {
165
165
  isAgree: 0
166
166
  },
167
167
  isReplying: !0
168
168
  };
169
- this.handleStreamAnswer(s.data, t), this.pushMessage(t), this.event.emit("message:content", t), t.index === -1 && (t.isReplying = !1, this.event.emit("answer:finish", t));
169
+ 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));
170
170
  }
171
- } else if (s.data.updateSession && this.data && (this.data = m(m({}, this.data), s.data.updateSession), this.event.emit("data:updated", this.data)), s.data.index === -1) {
171
+ } 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) {
172
172
  let e = this.messages[this.messages.length - 1];
173
173
  if (e && !e.messageId)
174
- e = m(m({}, e), s.data), this.event.emit("message:content", e), this.event.emit("message:updated", e), this.event.emit("answer:finish", e);
174
+ e = d(d({}, e), s.data), this.event.emit("message:content", e), this.event.emit("message:updated", e), this.event.emit("answer:finish", e);
175
175
  else {
176
- const t = m({
176
+ const n = d({
177
177
  messageId: "",
178
178
  content: "",
179
179
  role: y.Robot,
@@ -188,11 +188,11 @@ class H extends q {
188
188
  },
189
189
  isReplying: !0
190
190
  }, s.data);
191
- this.event.emit("message:content", t), this.pushMessage(t), this.event.emit("answer:finish", t);
191
+ this.event.emit("message:content", n), this.pushMessage(n), this.event.emit("answer:finish", n);
192
192
  }
193
193
  } else {
194
194
  let e = this.messages[this.messages.length - 1];
195
- e && !e.messageId ? (e = m(m({}, e), s.data), this.messages[this.messages.length - 1] = e, this.event.emit("message:content", e), this.event.emit("message:updated", e)) : (this.pushMessage(m({
195
+ 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({
196
196
  messageId: "",
197
197
  content: "",
198
198
  role: y.Robot,
@@ -208,12 +208,12 @@ class H extends q {
208
208
  isReplying: !0
209
209
  }, s.data)), this.event.emit("message:content", s.data));
210
210
  }
211
- }), n.on("reference", (s) => v(this, null, function* () {
211
+ }), r.on("reference", (s) => v(this, null, function* () {
212
212
  if (this._stopAnswer)
213
213
  return;
214
214
  if (!this.id) {
215
215
  const c = new Error(
216
- I.IdUndefined
216
+ M.IdUndefined
217
217
  // {
218
218
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
219
219
  // }
@@ -223,13 +223,13 @@ class H extends q {
223
223
  }
224
224
  const {
225
225
  type: e,
226
- data: t,
226
+ data: n,
227
227
  requestId: a
228
- } = s, h = this.messages.findIndex((c) => c.messageId === a && c.role === y.User);
229
- if (h < 0)
228
+ } = s, g = this.messages.findIndex((c) => c.messageId === a && c.role === y.User);
229
+ if (g < 0)
230
230
  return;
231
- const g = h + 1;
232
- if (g >= this.messages.length) {
231
+ const h = g + 1;
232
+ if (h >= this.messages.length) {
233
233
  const c = {
234
234
  messageId: "",
235
235
  content: "",
@@ -247,70 +247,73 @@ class H extends q {
247
247
  };
248
248
  this.pushMessage(c);
249
249
  }
250
- e === "KG" ? (this.messages[g] = w(m({}, this.messages[g]), {
251
- reference: t.knowledgeReference
252
- }), yield this.fillMessageReference(this.messages[g]), this.event.emit("message:reference", t.knowledgeReference), this.event.emit("message:updated", this.messages[g])) : e === "WEB_SEARCH" && (this.messages[g] = w(m({}, this.messages[g]), {
253
- searchReference: t.knowledgeReference
254
- }), this.event.emit("message:searchreference", t.searchReference), this.event.emit("message:updated", this.messages[g]));
255
- })), this.socket = n, n.open();
250
+ e === "KG" ? (this.messages[h] = w(d({}, this.messages[h]), {
251
+ reference: n.knowledgeReference
252
+ }), yield this.fillMessageReference(this.messages[h]), this.event.emit("message:reference", n.knowledgeReference), this.event.emit("message:updated", this.messages[h])) : e === "WEB_SEARCH" && (this.messages[h] = w(d({}, this.messages[h]), {
253
+ searchReference: n.knowledgeReference
254
+ }), this.event.emit("message:searchreference", n.searchReference), this.event.emit("message:updated", this.messages[h]));
255
+ })), this.socket = r, r.open();
256
256
  }));
257
257
  }
258
258
  registerMcpFromClientConfig() {
259
- return u.mcp ? new Promise((i, r) => {
259
+ return u.mcp ? new Promise((t, i) => {
260
260
  if (!this.socket) {
261
- r("socket is undefined");
261
+ i("socket is undefined");
262
262
  return;
263
263
  }
264
264
  this.socket.emit("add_mcp_servers", u.mcp, (o) => {
265
- o.code === 200 ? (Object.keys(o.data).forEach((n) => {
266
- o.data[n].code !== 200 && console.error(n, "MCP Server 注册失败。", o.data[n]);
267
- }), i(!0)) : (console.error("MCP Server 注册失败。", o), r(o));
265
+ o.code === 200 ? (Object.keys(o.data).forEach((r) => {
266
+ o.data[r].code !== 200 && console.error(r, "MCP Server 注册失败。", o.data[r]);
267
+ }), t(!0)) : (console.error("MCP Server 注册失败。", o), i(o));
268
268
  });
269
269
  }) : Promise.resolve(!0);
270
270
  }
271
- addMcpServers(i) {
271
+ addMcpServers(t) {
272
272
  return v(this, null, function* () {
273
- return this.ready ? this.socket ? new Promise((r, o) => {
274
- this.socket && this.socket.emit("add_mcp_servers", i, (n) => {
275
- n.code === 200 ? (Object.keys(n.data).forEach((s) => {
276
- n.data[s].code !== 200 && console.error(s, "MCP Server 注册失败。", n.data[s]);
277
- }), r(!0)) : (console.error("MCP Server 注册失败。", n), o(n));
273
+ return this.ready ? this.socket ? new Promise((i, o) => {
274
+ this.socket && this.socket.emit("add_mcp_servers", t, (r) => {
275
+ r.code === 200 ? (Object.keys(r.data).forEach((s) => {
276
+ r.data[s].code !== 200 && console.error(s, "MCP Server 注册失败。", r.data[s]);
277
+ }), i(!0)) : (console.error("MCP Server 注册失败。", r), o(r));
278
278
  });
279
279
  }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
280
280
  });
281
281
  }
282
282
  listMcpServers() {
283
283
  return v(this, null, function* () {
284
- return this.ready ? this.socket ? new Promise((i, r) => {
284
+ return this.ready ? this.socket ? new Promise((t, i) => {
285
285
  this.socket && this.socket.emit("list_mcp_servers", (o) => {
286
- o.code === 200 ? i(o.data) : r(o);
286
+ o.code === 200 ? t(o.data) : i(o);
287
287
  });
288
288
  }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
289
289
  });
290
290
  }
291
- sendQuestion(i, r) {
291
+ sendQuestion(t, i) {
292
292
  return v(this, null, function* () {
293
- var n, s, e;
294
- let o = w(m({}, i), {
295
- promptVariables: this.promptVariables.filter((t) => t.type === _.User).reduce((t, a) => w(m({}, t), {
293
+ var r, s, e;
294
+ this.convertKnowledgesToChatFormat(this.knowledges);
295
+ let o = w(d({}, t), {
296
+ promptVariables: this.promptVariables.filter((n) => n.type === _.User).reduce((n, a) => w(d({}, n), {
296
297
  [a.key]: a.value
297
298
  }), {}),
298
- systemPromptVariables: this.promptVariables.filter((t) => t.type === _.System).reduce((t, a) => w(m({}, t), {
299
+ systemPromptVariables: this.promptVariables.filter((n) => n.type === _.System).reduce((n, a) => w(d({}, n), {
299
300
  [a.key]: a.value
300
301
  }), {}),
301
- knowledges: (n = this.knowledges) == null ? void 0 : n.map((t) => t.id),
302
+ knowledges: (r = this.knowledges) == null ? void 0 : r.map((n) => n.id),
303
+ //agent支持指定文件时再放开
304
+ // knowledges: knowledgesForChat,
302
305
  //开启内容摘要
303
306
  enableTitleSummary: !0
304
307
  });
305
308
  if (!this.active) {
306
309
  if (yield this.activate(), !this.socket) {
307
- const t = new Error(
308
- I.SocketConnectionFailed
310
+ const n = new Error(
311
+ M.SocketConnectionFailed
309
312
  // {
310
313
  // cause: { code: -1, message: 'socket is null' },
311
314
  // }
312
315
  );
313
- return p.warn("error", t), this.event.emit("error", t), !1;
316
+ return p.warn("error", n), this.event.emit("error", n), !1;
314
317
  }
315
318
  (s = this.socket) == null || s.emit("question", o);
316
319
  return;
@@ -319,8 +322,14 @@ class H extends q {
319
322
  });
320
323
  }
321
324
  stopAnswer() {
322
- var i;
323
- (i = this.socket) == null || i.emit("stop_answer");
325
+ var t;
326
+ (t = this.socket) == null || t.emit("stop_answer");
327
+ }
328
+ convertKnowledgesToChatFormat(t) {
329
+ return t ? t.map((i) => i != null && i.fileIds ? {
330
+ id: i.id,
331
+ fileIds: i.fileIds
332
+ } : i.id) : [];
324
333
  }
325
334
  }
326
335
  export {