@glodon-aiot/bot-client-sdk 3.3.1 → 3.3.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,66 +1,67 @@
1
- var R = Object.defineProperty, b = Object.defineProperties;
2
- var S = Object.getOwnPropertyDescriptors;
3
- var C = Object.getOwnPropertySymbols;
4
- var A = Object.prototype.hasOwnProperty, E = Object.prototype.propertyIsEnumerable;
5
- var P = (d, t, n) => t in d ? R(d, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : d[t] = n, h = (d, t) => {
1
+ var S = Object.defineProperty, A = Object.defineProperties;
2
+ var b = Object.getOwnPropertyDescriptors;
3
+ var _ = Object.getOwnPropertySymbols;
4
+ var P = Object.prototype.hasOwnProperty, x = Object.prototype.propertyIsEnumerable;
5
+ var C = (m, t, n) => t in m ? S(m, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : m[t] = n, g = (m, t) => {
6
6
  for (var n in t || (t = {}))
7
- A.call(t, n) && P(d, n, t[n]);
8
- if (C)
9
- for (var n of C(t))
10
- E.call(t, n) && P(d, n, t[n]);
11
- return d;
12
- }, k = (d, t) => b(d, S(t));
13
- var p = (d, t, n) => new Promise((r, i) => {
7
+ P.call(t, n) && C(m, n, t[n]);
8
+ if (_)
9
+ for (var n of _(t))
10
+ x.call(t, n) && C(m, n, t[n]);
11
+ return m;
12
+ }, w = (m, t) => A(m, b(t));
13
+ var v = (m, t, n) => new Promise((o, i) => {
14
14
  var s = (c) => {
15
15
  try {
16
- o(n.next(c));
17
- } catch (g) {
18
- i(g);
16
+ r(n.next(c));
17
+ } catch (h) {
18
+ i(h);
19
19
  }
20
20
  }, e = (c) => {
21
21
  try {
22
- o(n.throw(c));
23
- } catch (g) {
24
- i(g);
22
+ r(n.throw(c));
23
+ } catch (h) {
24
+ i(h);
25
25
  }
26
- }, o = (c) => c.done ? r(c.value) : Promise.resolve(c.value).then(s, e);
27
- o((n = n.apply(d, t)).next());
26
+ }, r = (c) => c.done ? o(c.value) : Promise.resolve(c.value).then(s, e);
27
+ r((n = n.apply(m, t)).next());
28
28
  });
29
- import { ChatMessageRole as I } from "@glodon-aiot/apis";
30
- import q from "./Logger.mjs";
31
- import { configuration as v } from "./Config.mjs";
32
- import { io as x } from "socket.io-client";
33
- import { UserMode as w } from "./PayloadTypes.mjs";
29
+ import { ChatMessageRole as k } from "@glodon-aiot/apis";
30
+ import E from "./Logger.mjs";
31
+ import { configuration as u } from "./Config.mjs";
32
+ import { io as q } from "socket.io-client";
33
+ import { UserMode as M } from "./PayloadTypes.mjs";
34
34
  import L, { SessionErrors as y } from "./Session.mjs";
35
- const u = new q("[AgentSession] ");
36
- class D extends L {
37
- constructor(t, n = "", r, i, s, e) {
38
- super(t, n, r, i, s, e);
35
+ import N from "dayjs";
36
+ const p = new E("[AgentSession] ");
37
+ class H extends L {
38
+ constructor(t, n = "", o, i, s, e) {
39
+ super(t, n, o, i, s, e);
39
40
  }
40
41
  prepareConnection() {
41
- return new Promise((t) => p(this, null, function* () {
42
+ return new Promise((t) => v(this, null, function* () {
42
43
  this.socket && t(!0);
43
44
  const n = {
44
45
  version: "1.0",
45
- stream: "false",
46
+ stream: JSON.stringify(u.stream),
46
47
  appId: this._applicationId,
47
48
  sessionId: this.id,
48
49
  Authorization: `${this.token}`,
49
- userMode: v.debug ? w.Debug : w.Normal
50
- }, r = new URL(v.apiRoot), i = yield x(r.origin, {
51
- path: r.pathname.replace("api", "socketio") + "/copilot",
50
+ userMode: u.debug ? M.Debug : M.Normal
51
+ }, o = new URL(u.apiRoot), i = yield q(o.origin, {
52
+ path: o.pathname.replace("api", "socketio") + "/copilot",
52
53
  query: n,
53
54
  transports: ["websocket"]
54
55
  });
55
56
  i.on("connect", () => {
56
- u.log("socket:connect"), this.event.emit("socket:connect"), i.emit("init");
57
+ p.log("socket:connect"), this.event.emit("socket:connect"), i.emit("init");
57
58
  }), i.on("disconnect", () => {
58
- u.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
59
+ p.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
59
60
  }), i.on("ready", (s) => {
60
61
  if (s.code === 200) {
61
- u.log("ready");
62
- const e = v.debug ? w.Debug : w.Normal;
63
- e === w.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), t(!0);
62
+ p.log("ready");
63
+ const e = u.debug ? M.Debug : M.Normal;
64
+ e === M.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), t(!0);
64
65
  } else {
65
66
  this._ready && (this._ready = !1);
66
67
  const e = new Error(
@@ -69,19 +70,19 @@ class D extends L {
69
70
  // cause: data,
70
71
  // }
71
72
  );
72
- u.error("error", e), this.event.emit("error", e), t(!1);
73
+ p.error("error", e), this.event.emit("error", e), t(!1);
73
74
  }
74
- }), i.on("action", (s, e) => p(this, null, function* () {
75
+ }), i.on("action", (s, e) => v(this, null, function* () {
75
76
  if (this._stopAnswer || !this.id)
76
77
  return;
77
78
  const {
78
- type: o,
79
+ type: r,
79
80
  data: {
80
81
  runnerIsClient: c,
81
- pluginCode: g,
82
- funcCode: m,
82
+ pluginCode: h,
83
+ funcCode: d,
83
84
  parameters: a,
84
- requestBody: N
85
+ requestBody: j
85
86
  }
86
87
  } = s, f = this.messages[this.messages.length - 1];
87
88
  if (!f.messageId)
@@ -93,7 +94,7 @@ class D extends L {
93
94
  const l = {
94
95
  messageId: "",
95
96
  content: "",
96
- role: I.Robot,
97
+ role: k.Robot,
97
98
  sessionId: this.id,
98
99
  reference: [],
99
100
  searchReference: [],
@@ -120,41 +121,61 @@ class D extends L {
120
121
  // },
121
122
  // }
122
123
  );
123
- u.warn("error", l), this.event.emit("error", l);
124
- } else if (o === "FUNCTION_CALL" && c) {
125
- if (u.log(`calling function ${m} of plugin ${g} with argument:
126
- ${JSON.stringify(a)}`), !v.plugins[g]) {
124
+ p.warn("error", l), this.event.emit("error", l);
125
+ } else if (r === "FUNCTION_CALL" && c) {
126
+ if (p.log(`calling function ${d} of plugin ${h} with argument:
127
+ ${JSON.stringify(a)}`), !u.plugins[h]) {
127
128
  e({
128
129
  code: 404,
129
130
  message: "对不起,没有找到插件。"
130
131
  });
131
132
  return;
132
133
  }
133
- const l = v.plugins[g][m];
134
+ const l = u.plugins[h][d];
134
135
  typeof l != "function" && e({
135
136
  code: 404,
136
137
  message: "对不起,没有找到插件方法。"
137
138
  });
138
- let M = a == null ? void 0 : a.query;
139
+ let I = a == null ? void 0 : a.query;
139
140
  try {
140
- M = JSON.parse(a == null ? void 0 : a.query);
141
- } catch (_) {
141
+ I = JSON.parse(a == null ? void 0 : a.query);
142
+ } catch (R) {
142
143
  }
143
- l(M, (..._) => {
144
- this._stopAnswer || e(..._);
144
+ l(I, (...R) => {
145
+ this._stopAnswer || e(...R);
145
146
  });
146
147
  }
147
148
  })), i.on("answer", (s) => {
148
149
  if (!this._stopAnswer)
149
- if (s.data.code !== 200 && (s.data.index = -1, this.event.emit("error", s.data)), s.data.index === -1) {
150
+ if (s.data.code !== 200 && (s.data.index = -1), s.data.stream || s.data.stream === void 0) {
151
+ let e = this.messages[this.messages.length - 1];
152
+ if (e && e.role === k.Robot)
153
+ 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 && this.event.emit("answer:finish", e);
154
+ else {
155
+ const r = {
156
+ content: "",
157
+ role: 1,
158
+ reference: [],
159
+ searchReference: [],
160
+ messageId: "",
161
+ sessionId: this.id,
162
+ messageTime: N(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
163
+ comment: {
164
+ isAgree: 0
165
+ },
166
+ isReplying: !0
167
+ };
168
+ this.handleStreamAnswer(s.data, r), this.pushMessage(r), this.event.emit("message:content", r), e.index === -1 && this.event.emit("answer:finish", e);
169
+ }
170
+ } else if (s.data.index === -1) {
150
171
  let e = this.messages[this.messages.length - 1];
151
172
  if (e && !e.messageId)
152
- e = h(h({}, e), s.data), this.event.emit("message:content", e), this.event.emit("message:updated", e), this.event.emit("answer:finish", e);
173
+ e = g(g({}, e), s.data), this.event.emit("message:content", e), this.event.emit("message:updated", e), this.event.emit("answer:finish", e);
153
174
  else {
154
- const o = h({
175
+ const r = g({
155
176
  messageId: "",
156
177
  content: "",
157
- role: I.Robot,
178
+ role: k.Robot,
158
179
  sessionId: this.id,
159
180
  reference: [],
160
181
  searchReference: [],
@@ -165,13 +186,13 @@ class D extends L {
165
186
  isAgree: 0
166
187
  }
167
188
  }, s.data);
168
- this.event.emit("message:content", o), this.pushMessage(o), this.event.emit("answer:finish", o);
189
+ this.event.emit("message:content", r), this.pushMessage(r), this.event.emit("answer:finish", r);
169
190
  }
170
191
  } else {
171
192
  let e = this.messages[this.messages.length - 1];
172
- e && !e.messageId ? (e = h(h({}, e), s.data), this.messages[this.messages.length - 1] = e, this.event.emit("message:content", e), this.event.emit("message:updated", e)) : (this.pushMessage(s.data), this.event.emit("message:content", s.data));
193
+ e && !e.messageId ? (e = g(g({}, e), s.data), this.messages[this.messages.length - 1] = e, this.event.emit("message:content", e), this.event.emit("message:updated", e)) : (this.pushMessage(s.data), this.event.emit("message:content", s.data));
173
194
  }
174
- }), i.on("reference", (s) => p(this, null, function* () {
195
+ }), i.on("reference", (s) => v(this, null, function* () {
175
196
  if (this._stopAnswer)
176
197
  return;
177
198
  if (!this.id) {
@@ -181,22 +202,22 @@ class D extends L {
181
202
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
182
203
  // }
183
204
  );
184
- u.error(a), this.event.emit("error", a), this.frozen = !1;
205
+ p.error(a), this.event.emit("error", a), this.frozen = !1;
185
206
  return;
186
207
  }
187
208
  const {
188
209
  type: e,
189
- data: o,
210
+ data: r,
190
211
  requestId: c
191
- } = s, g = this.messages.findIndex((a) => a.messageId === c && a.role === I.User);
192
- if (g < 0)
212
+ } = s, h = this.messages.findIndex((a) => a.messageId === c && a.role === k.User);
213
+ if (h < 0)
193
214
  return;
194
- const m = g + 1;
195
- if (m >= this.messages.length) {
215
+ const d = h + 1;
216
+ if (d >= this.messages.length) {
196
217
  const a = {
197
218
  messageId: "",
198
219
  content: "",
199
- role: I.Robot,
220
+ role: k.Robot,
200
221
  sessionId: this.id,
201
222
  reference: [],
202
223
  searchReference: [],
@@ -209,70 +230,70 @@ class D extends L {
209
230
  };
210
231
  this.pushMessage(a);
211
232
  }
212
- e === "KG" ? (this.messages[m] = k(h({}, this.messages[m]), {
213
- reference: o.knowledgeReference
214
- }), yield this.fillMessageReference(this.messages[m]), this.event.emit("message:reference", o.knowledgeReference), this.event.emit("message:updated", this.messages[m])) : e === "WEB_SEARCH" && (this.messages[m] = k(h({}, this.messages[m]), {
215
- searchReference: o.knowledgeReference
216
- }), this.event.emit("message:searchreference", o.searchReference), this.event.emit("message:updated", this.messages[m]));
233
+ e === "KG" ? (this.messages[d] = w(g({}, this.messages[d]), {
234
+ reference: r.knowledgeReference
235
+ }), yield this.fillMessageReference(this.messages[d]), this.event.emit("message:reference", r.knowledgeReference), this.event.emit("message:updated", this.messages[d])) : e === "WEB_SEARCH" && (this.messages[d] = w(g({}, this.messages[d]), {
236
+ searchReference: r.knowledgeReference
237
+ }), this.event.emit("message:searchreference", r.searchReference), this.event.emit("message:updated", this.messages[d]));
217
238
  })), this.socket = i, i.open();
218
239
  }));
219
240
  }
220
241
  registerMcpFromClientConfig() {
221
- return v.mcp ? new Promise((t, n) => {
242
+ return u.mcp ? new Promise((t, n) => {
222
243
  if (!this.socket) {
223
244
  n("socket is undefined");
224
245
  return;
225
246
  }
226
- this.socket.emit("add_mcp_servers", v.mcp, (r) => {
227
- r.code === 200 ? (Object.keys(r.data).forEach((i) => {
228
- r.data[i].code !== 200 && console.error(i, "MCP Server 注册失败。", r.data[i]);
229
- }), t(!0)) : (console.error("MCP Server 注册失败。", r), n(r));
247
+ this.socket.emit("add_mcp_servers", u.mcp, (o) => {
248
+ o.code === 200 ? (Object.keys(o.data).forEach((i) => {
249
+ o.data[i].code !== 200 && console.error(i, "MCP Server 注册失败。", o.data[i]);
250
+ }), t(!0)) : (console.error("MCP Server 注册失败。", o), n(o));
230
251
  });
231
252
  }) : Promise.resolve(!0);
232
253
  }
233
254
  addMcpServers(t) {
234
- return p(this, null, function* () {
235
- return this.ready ? this.socket ? new Promise((n, r) => {
255
+ return v(this, null, function* () {
256
+ return this.ready ? this.socket ? new Promise((n, o) => {
236
257
  this.socket && this.socket.emit("add_mcp_servers", t, (i) => {
237
258
  i.code === 200 ? (Object.keys(i.data).forEach((s) => {
238
259
  i.data[s].code !== 200 && console.error(s, "MCP Server 注册失败。", i.data[s]);
239
- }), n(!0)) : (console.error("MCP Server 注册失败。", i), r(i));
260
+ }), n(!0)) : (console.error("MCP Server 注册失败。", i), o(i));
240
261
  });
241
262
  }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
242
263
  });
243
264
  }
244
265
  listMcpServers() {
245
- return p(this, null, function* () {
266
+ return v(this, null, function* () {
246
267
  return this.ready ? this.socket ? new Promise((t, n) => {
247
- this.socket && this.socket.emit("list_mcp_servers", (r) => {
248
- r.code === 200 ? t(r.data) : n(r);
268
+ this.socket && this.socket.emit("list_mcp_servers", (o) => {
269
+ o.code === 200 ? t(o.data) : n(o);
249
270
  });
250
271
  }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
251
272
  });
252
273
  }
253
274
  sendQuestion(t, n) {
254
- return p(this, null, function* () {
275
+ return v(this, null, function* () {
255
276
  var i, s, e;
256
- let r = k(h({}, t), {
257
- promptVariables: this.promptVariables.reduce((o, c) => k(h({}, o), {
277
+ let o = w(g({}, t), {
278
+ promptVariables: this.promptVariables.reduce((r, c) => w(g({}, r), {
258
279
  [c.key]: c.value
259
280
  }), {}),
260
- knowledges: (i = this.knowledges) == null ? void 0 : i.map((o) => o.id)
281
+ knowledges: (i = this.knowledges) == null ? void 0 : i.map((r) => r.id)
261
282
  });
262
283
  if (!this.active) {
263
284
  if (yield this.activate(), !this.socket) {
264
- const o = new Error(
285
+ const r = new Error(
265
286
  y.SocketConnectionFailed
266
287
  // {
267
288
  // cause: { code: -1, message: 'socket is null' },
268
289
  // }
269
290
  );
270
- return u.warn("error", o), this.event.emit("error", o), !1;
291
+ return p.warn("error", r), this.event.emit("error", r), !1;
271
292
  }
272
- (s = this.socket) == null || s.emit("question", r);
293
+ (s = this.socket) == null || s.emit("question", o);
273
294
  return;
274
295
  }
275
- (e = this.socket) == null || e.emit("question", r);
296
+ (e = this.socket) == null || e.emit("question", o);
276
297
  });
277
298
  }
278
299
  stopAnswer() {
@@ -281,5 +302,5 @@ class D extends L {
281
302
  }
282
303
  }
283
304
  export {
284
- D as default
305
+ H as default
285
306
  };
@@ -1,15 +1,15 @@
1
- var v = Object.defineProperty, s = Object.defineProperties;
2
- var u = Object.getOwnPropertyDescriptors;
1
+ var s = Object.defineProperty, u = Object.defineProperties;
2
+ var v = Object.getOwnPropertyDescriptors;
3
3
  var n = Object.getOwnPropertySymbols;
4
4
  var p = Object.prototype.hasOwnProperty, h = Object.prototype.propertyIsEnumerable;
5
- var g = (o, t, e) => t in o ? v(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e, i = (o, t) => {
5
+ var g = (o, t, e) => t in o ? s(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e, i = (o, t) => {
6
6
  for (var e in t || (t = {}))
7
7
  p.call(t, e) && g(o, e, t[e]);
8
8
  if (n)
9
9
  for (var e of n(t))
10
10
  h.call(t, e) && g(o, e, t[e]);
11
11
  return o;
12
- }, l = (o, t) => s(o, u(t));
12
+ }, l = (o, t) => u(o, v(t));
13
13
  import d from "./Logger.mjs";
14
14
  const R = new d("[Config] ");
15
15
  var f = /* @__PURE__ */ ((o) => (o.dev = "https://aiot-dev.glodon.com/api/cvforcepd", o.test = "https://aiot-dev.glodon.com/api/cvforce", o.prod = "https://copilot.glodon.com/api/cvforce", o.local = "http://localhost:3000/api/cvforcepd", o))(f || {});
@@ -22,9 +22,10 @@ let c = {
22
22
  applicationId: "",
23
23
  plugins: {},
24
24
  mcp: void 0,
25
- loadRelatedQuesions: !0
25
+ loadRelatedQuesions: !0,
26
+ stream: !0
26
27
  };
27
- function x(o) {
28
+ function w(o) {
28
29
  d.debug = (o == null ? void 0 : o.debug) || !1, c = l(i(i({}, c), o), {
29
30
  apiRoot: (o == null ? void 0 : o.apiRoot) || c.apiRoot || (o != null && o.env ? f[o.env || "prod"] : c.apiRoot)
30
31
  }), d.debug && R.log("updated", c);
@@ -32,5 +33,5 @@ function x(o) {
32
33
  export {
33
34
  f as ApiUrl,
34
35
  c as configuration,
35
- x as configure
36
+ w as configure
36
37
  };