@glodon-aiot/bot-client-sdk 3.3.3 → 3.3.4-beta.2

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,87 +1,88 @@
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) => {
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) => {
14
- var s = (c) => {
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 = (m, i, r) => i in m ? A(m, i, { enumerable: !0, configurable: !0, writable: !0, value: r }) : m[i] = r, d = (m, i) => {
6
+ for (var r in i || (i = {}))
7
+ x.call(i, r) && S(m, r, i[r]);
8
+ if (b)
9
+ for (var r of b(i))
10
+ E.call(i, r) && S(m, r, i[r]);
11
+ return m;
12
+ }, y = (m, i) => C(m, P(i));
13
+ var v = (m, i, r) => new Promise((o, n) => {
14
+ var s = (a) => {
15
15
  try {
16
- o(n.next(c));
17
- } catch (g) {
18
- i(g);
16
+ t(r.next(a));
17
+ } catch (h) {
18
+ n(h);
19
19
  }
20
- }, e = (c) => {
20
+ }, e = (a) => {
21
21
  try {
22
- o(n.throw(c));
23
- } catch (g) {
24
- i(g);
22
+ t(r.throw(a));
23
+ } catch (h) {
24
+ n(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
+ }, t = (a) => a.done ? o(a.value) : Promise.resolve(a.value).then(s, e);
27
+ t((r = r.apply(m, i)).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";
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);
29
+ import { ChatMessageRole as w, PromptVariableType as _ } from "@glodon-aiot/apis";
30
+ import L from "./Logger.mjs";
31
+ import { configuration as u } from "./Config.mjs";
32
+ import { io as q } from "socket.io-client";
33
+ import { UserMode as k } from "./PayloadTypes.mjs";
34
+ import N, { SessionErrors as I } from "./Session.mjs";
35
+ import T from "dayjs";
36
+ const p = new L("[AgentSession] ");
37
+ class H extends N {
38
+ constructor(i, r = "", o, n, s, e) {
39
+ super(i, r, o, n, s, e);
39
40
  }
40
41
  prepareConnection() {
41
- return new Promise((t) => p(this, null, function* () {
42
- this.socket && t(!0);
43
- const n = {
42
+ return new Promise((i) => v(this, null, function* () {
43
+ this.socket && i(!0);
44
+ const r = {
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",
52
- query: n,
50
+ userMode: u.debug ? k.Debug : k.Normal
51
+ }, o = new URL(u.apiRoot), n = yield q(o.origin, {
52
+ path: o.pathname.replace("api", "socketio") + "/copilot",
53
+ query: r,
53
54
  transports: ["websocket"]
54
55
  });
55
- i.on("connect", () => {
56
- u.log("socket:connect"), this.event.emit("socket:connect"), i.emit("init");
57
- }), i.on("disconnect", () => {
58
- u.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
59
- }), i.on("ready", (s) => {
56
+ n.on("connect", () => {
57
+ p.log("socket:connect"), this.event.emit("socket:connect"), n.emit("init");
58
+ }), n.on("disconnect", () => {
59
+ p.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
60
+ }), n.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 ? 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);
64
65
  } else {
65
66
  this._ready && (this._ready = !1);
66
67
  const e = new Error(
67
- y.SocketConnectionFailed
68
+ I.SocketConnectionFailed
68
69
  // , {
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), i(!1);
73
74
  }
74
- }), i.on("action", (s, e) => p(this, null, function* () {
75
+ }), n.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: t,
79
80
  data: {
80
- runnerIsClient: c,
81
- pluginCode: g,
82
- funcCode: m,
83
- parameters: a,
84
- requestBody: N
81
+ runnerIsClient: a,
82
+ pluginCode: h,
83
+ funcCode: g,
84
+ parameters: c,
85
+ requestBody: U
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: w.Robot,
97
98
  sessionId: this.id,
98
99
  reference: [],
99
100
  searchReference: [],
@@ -102,7 +103,8 @@ class D extends L {
102
103
  actions: [[s]],
103
104
  comment: {
104
105
  isAgree: 0
105
- }
106
+ },
107
+ isReplying: !0
106
108
  };
107
109
  this.pushMessage(l), this.event.emit("message:action", {
108
110
  messageId: l.messageId,
@@ -111,7 +113,7 @@ class D extends L {
111
113
  }
112
114
  if (this.inactivating) {
113
115
  const l = new Error(
114
- y.Inactive
116
+ I.Inactive
115
117
  // {
116
118
  // cause: {
117
119
  // code: -1,
@@ -120,41 +122,61 @@ class D extends L {
120
122
  // },
121
123
  // }
122
124
  );
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]) {
125
+ p.warn("error", l), this.event.emit("error", l);
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]) {
127
129
  e({
128
130
  code: 404,
129
131
  message: "对不起,没有找到插件。"
130
132
  });
131
133
  return;
132
134
  }
133
- const l = v.plugins[g][m];
135
+ const l = u.plugins[h][g];
134
136
  typeof l != "function" && e({
135
137
  code: 404,
136
138
  message: "对不起,没有找到插件方法。"
137
139
  });
138
- let M = a == null ? void 0 : a.query;
140
+ let M = c == null ? void 0 : c.query;
139
141
  try {
140
- M = JSON.parse(a == null ? void 0 : a.query);
141
- } catch (_) {
142
+ M = JSON.parse(c == null ? void 0 : c.query);
143
+ } catch (R) {
142
144
  }
143
- l(M, (..._) => {
144
- this._stopAnswer || e(..._);
145
+ l(M, (...R) => {
146
+ this._stopAnswer || e(...R);
145
147
  });
146
148
  }
147
- })), i.on("answer", (s) => {
149
+ })), n.on("answer", (s) => {
148
150
  if (!this._stopAnswer)
149
- if (s.data.code !== 200 && (s.data.index = -1, this.event.emit("error", s.data)), s.data.index === -1) {
151
+ if (s.data.code !== 200 && (s.data.index = -1, this.event.emit("error", s.data)), s.data.stream || s.data.stream === void 0) {
152
+ let e = this.messages[this.messages.length - 1];
153
+ if (e && e.role === w.Robot)
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 && this.event.emit("answer:finish", e);
155
+ else {
156
+ const t = {
157
+ content: "",
158
+ role: w.Robot,
159
+ reference: [],
160
+ searchReference: [],
161
+ messageId: "",
162
+ sessionId: this.id,
163
+ messageTime: T(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
164
+ comment: {
165
+ isAgree: 0
166
+ },
167
+ isReplying: !0
168
+ };
169
+ this.handleStreamAnswer(s.data, t), this.pushMessage(t), this.event.emit("message:content", t), t.index === -1 && this.event.emit("answer:finish", t);
170
+ }
171
+ } else if (s.data.index === -1) {
150
172
  let e = this.messages[this.messages.length - 1];
151
173
  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);
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);
153
175
  else {
154
- const o = h({
176
+ const t = d({
155
177
  messageId: "",
156
178
  content: "",
157
- role: I.Robot,
179
+ role: w.Robot,
158
180
  sessionId: this.id,
159
181
  reference: [],
160
182
  searchReference: [],
@@ -163,40 +185,55 @@ class D extends L {
163
185
  actions: [],
164
186
  comment: {
165
187
  isAgree: 0
166
- }
188
+ },
189
+ isReplying: !0
167
190
  }, s.data);
168
- this.event.emit("message:content", o), this.pushMessage(o), this.event.emit("answer:finish", o);
191
+ this.event.emit("message:content", t), this.pushMessage(t), this.event.emit("answer:finish", t);
169
192
  }
170
193
  } else {
171
194
  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));
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
+ messageId: "",
197
+ content: "",
198
+ role: w.Robot,
199
+ sessionId: this.id,
200
+ reference: [],
201
+ searchReference: [],
202
+ isLoading: !1,
203
+ messageTime: "",
204
+ actions: [],
205
+ comment: {
206
+ isAgree: 0
207
+ },
208
+ isReplying: !0
209
+ }, s.data)), this.event.emit("message:content", s.data));
173
210
  }
174
- }), i.on("reference", (s) => p(this, null, function* () {
211
+ }), n.on("reference", (s) => v(this, null, function* () {
175
212
  if (this._stopAnswer)
176
213
  return;
177
214
  if (!this.id) {
178
- const a = new Error(
179
- y.IdUndefined
215
+ const c = new Error(
216
+ I.IdUndefined
180
217
  // {
181
218
  // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
182
219
  // }
183
220
  );
184
- u.error(a), this.event.emit("error", a), this.frozen = !1;
221
+ p.error(c), this.event.emit("error", c), this.frozen = !1;
185
222
  return;
186
223
  }
187
224
  const {
188
225
  type: e,
189
- data: o,
190
- requestId: c
191
- } = s, g = this.messages.findIndex((a) => a.messageId === c && a.role === I.User);
192
- if (g < 0)
226
+ data: t,
227
+ requestId: a
228
+ } = s, h = this.messages.findIndex((c) => c.messageId === a && c.role === w.User);
229
+ if (h < 0)
193
230
  return;
194
- const m = g + 1;
195
- if (m >= this.messages.length) {
196
- const a = {
231
+ const g = h + 1;
232
+ if (g >= this.messages.length) {
233
+ const c = {
197
234
  messageId: "",
198
235
  content: "",
199
- role: I.Robot,
236
+ role: w.Robot,
200
237
  sessionId: this.id,
201
238
  reference: [],
202
239
  searchReference: [],
@@ -205,81 +242,85 @@ class D extends L {
205
242
  actions: [],
206
243
  comment: {
207
244
  isAgree: 0
208
- }
245
+ },
246
+ isReplying: !0
209
247
  };
210
- this.pushMessage(a);
248
+ this.pushMessage(c);
211
249
  }
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]));
217
- })), this.socket = i, i.open();
250
+ e === "KG" ? (this.messages[g] = y(d({}, 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] = y(d({}, 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();
218
256
  }));
219
257
  }
220
258
  registerMcpFromClientConfig() {
221
- return v.mcp ? new Promise((t, n) => {
259
+ return u.mcp ? new Promise((i, r) => {
222
260
  if (!this.socket) {
223
- n("socket is undefined");
261
+ r("socket is undefined");
224
262
  return;
225
263
  }
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));
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));
230
268
  });
231
269
  }) : Promise.resolve(!0);
232
270
  }
233
- addMcpServers(t) {
234
- return p(this, null, function* () {
235
- return this.ready ? this.socket ? new Promise((n, r) => {
236
- this.socket && this.socket.emit("add_mcp_servers", t, (i) => {
237
- i.code === 200 ? (Object.keys(i.data).forEach((s) => {
238
- i.data[s].code !== 200 && console.error(s, "MCP Server 注册失败。", i.data[s]);
239
- }), n(!0)) : (console.error("MCP Server 注册失败。", i), r(i));
271
+ addMcpServers(i) {
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));
240
278
  });
241
279
  }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
242
280
  });
243
281
  }
244
282
  listMcpServers() {
245
- return p(this, null, function* () {
246
- 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);
283
+ return v(this, null, function* () {
284
+ return this.ready ? this.socket ? new Promise((i, r) => {
285
+ this.socket && this.socket.emit("list_mcp_servers", (o) => {
286
+ o.code === 200 ? i(o.data) : r(o);
249
287
  });
250
288
  }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
251
289
  });
252
290
  }
253
- sendQuestion(t, n) {
254
- return p(this, null, function* () {
255
- var i, s, e;
256
- let r = k(h({}, t), {
257
- promptVariables: this.promptVariables.reduce((o, c) => k(h({}, o), {
258
- [c.key]: c.value
291
+ sendQuestion(i, r) {
292
+ return v(this, null, function* () {
293
+ var n, s, e;
294
+ let o = y(d({}, i), {
295
+ promptVariables: this.promptVariables.filter((t) => t.type === _.User).reduce((t, a) => y(d({}, t), {
296
+ [a.key]: a.value
297
+ }), {}),
298
+ systemPromptVariables: this.promptVariables.filter((t) => t.type === _.System).reduce((t, a) => y(d({}, t), {
299
+ [a.key]: a.value
259
300
  }), {}),
260
- knowledges: (i = this.knowledges) == null ? void 0 : i.map((o) => o.id)
301
+ knowledges: (n = this.knowledges) == null ? void 0 : n.map((t) => t.id)
261
302
  });
262
303
  if (!this.active) {
263
304
  if (yield this.activate(), !this.socket) {
264
- const o = new Error(
265
- y.SocketConnectionFailed
305
+ const t = new Error(
306
+ I.SocketConnectionFailed
266
307
  // {
267
308
  // cause: { code: -1, message: 'socket is null' },
268
309
  // }
269
310
  );
270
- return u.warn("error", o), this.event.emit("error", o), !1;
311
+ return p.warn("error", t), this.event.emit("error", t), !1;
271
312
  }
272
- (s = this.socket) == null || s.emit("question", r);
313
+ (s = this.socket) == null || s.emit("question", o);
273
314
  return;
274
315
  }
275
- (e = this.socket) == null || e.emit("question", r);
316
+ (e = this.socket) == null || e.emit("question", o);
276
317
  });
277
318
  }
278
319
  stopAnswer() {
279
- var t;
280
- (t = this.socket) == null || t.emit("stop_answer");
320
+ var i;
321
+ (i = this.socket) == null || i.emit("stop_answer");
281
322
  }
282
323
  }
283
324
  export {
284
- D as default
325
+ H as default
285
326
  };