@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.
- package/dist/es/AgentSession.mjs +116 -95
- package/dist/es/Config.mjs +8 -7
- package/dist/es/DialogSession.mjs +137 -167
- package/dist/es/Session.mjs +211 -171
- package/dist/es/package.json.mjs +11 -11
- package/dist/lib/index.js +4 -4
- package/dist/src/Config.d.ts +2 -0
- package/dist/src/Session.d.ts +6 -0
- package/package.json +2 -2
package/dist/es/AgentSession.mjs
CHANGED
|
@@ -1,66 +1,67 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
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
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var n of
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
var
|
|
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
|
-
|
|
17
|
-
} catch (
|
|
18
|
-
i(
|
|
16
|
+
r(n.next(c));
|
|
17
|
+
} catch (h) {
|
|
18
|
+
i(h);
|
|
19
19
|
}
|
|
20
20
|
}, e = (c) => {
|
|
21
21
|
try {
|
|
22
|
-
|
|
23
|
-
} catch (
|
|
24
|
-
i(
|
|
22
|
+
r(n.throw(c));
|
|
23
|
+
} catch (h) {
|
|
24
|
+
i(h);
|
|
25
25
|
}
|
|
26
|
-
},
|
|
27
|
-
|
|
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
|
|
30
|
-
import
|
|
31
|
-
import { configuration as
|
|
32
|
-
import { io as
|
|
33
|
-
import { UserMode as
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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) =>
|
|
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:
|
|
46
|
+
stream: JSON.stringify(u.stream),
|
|
46
47
|
appId: this._applicationId,
|
|
47
48
|
sessionId: this.id,
|
|
48
49
|
Authorization: `${this.token}`,
|
|
49
|
-
userMode:
|
|
50
|
-
},
|
|
51
|
-
path:
|
|
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
|
-
|
|
57
|
+
p.log("socket:connect"), this.event.emit("socket:connect"), i.emit("init");
|
|
57
58
|
}), i.on("disconnect", () => {
|
|
58
|
-
|
|
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
|
-
|
|
62
|
-
const e =
|
|
63
|
-
e ===
|
|
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
|
-
|
|
73
|
+
p.error("error", e), this.event.emit("error", e), t(!1);
|
|
73
74
|
}
|
|
74
|
-
}), i.on("action", (s, e) =>
|
|
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:
|
|
79
|
+
type: r,
|
|
79
80
|
data: {
|
|
80
81
|
runnerIsClient: c,
|
|
81
|
-
pluginCode:
|
|
82
|
-
funcCode:
|
|
82
|
+
pluginCode: h,
|
|
83
|
+
funcCode: d,
|
|
83
84
|
parameters: a,
|
|
84
|
-
requestBody:
|
|
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:
|
|
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
|
-
|
|
124
|
-
} else if (
|
|
125
|
-
if (
|
|
126
|
-
${JSON.stringify(a)}`), !
|
|
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 =
|
|
134
|
+
const l = u.plugins[h][d];
|
|
134
135
|
typeof l != "function" && e({
|
|
135
136
|
code: 404,
|
|
136
137
|
message: "对不起,没有找到插件方法。"
|
|
137
138
|
});
|
|
138
|
-
let
|
|
139
|
+
let I = a == null ? void 0 : a.query;
|
|
139
140
|
try {
|
|
140
|
-
|
|
141
|
-
} catch (
|
|
141
|
+
I = JSON.parse(a == null ? void 0 : a.query);
|
|
142
|
+
} catch (R) {
|
|
142
143
|
}
|
|
143
|
-
l(
|
|
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,
|
|
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 =
|
|
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
|
|
175
|
+
const r = g({
|
|
155
176
|
messageId: "",
|
|
156
177
|
content: "",
|
|
157
|
-
role:
|
|
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",
|
|
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 =
|
|
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) =>
|
|
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
|
-
|
|
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:
|
|
210
|
+
data: r,
|
|
190
211
|
requestId: c
|
|
191
|
-
} = s,
|
|
192
|
-
if (
|
|
212
|
+
} = s, h = this.messages.findIndex((a) => a.messageId === c && a.role === k.User);
|
|
213
|
+
if (h < 0)
|
|
193
214
|
return;
|
|
194
|
-
const
|
|
195
|
-
if (
|
|
215
|
+
const d = h + 1;
|
|
216
|
+
if (d >= this.messages.length) {
|
|
196
217
|
const a = {
|
|
197
218
|
messageId: "",
|
|
198
219
|
content: "",
|
|
199
|
-
role:
|
|
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[
|
|
213
|
-
reference:
|
|
214
|
-
}), yield this.fillMessageReference(this.messages[
|
|
215
|
-
searchReference:
|
|
216
|
-
}), this.event.emit("message:searchreference",
|
|
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
|
|
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",
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}), t(!0)) : (console.error("MCP Server 注册失败。",
|
|
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
|
|
235
|
-
return this.ready ? this.socket ? new Promise((n,
|
|
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),
|
|
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
|
|
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", (
|
|
248
|
-
|
|
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
|
|
275
|
+
return v(this, null, function* () {
|
|
255
276
|
var i, s, e;
|
|
256
|
-
let
|
|
257
|
-
promptVariables: this.promptVariables.reduce((
|
|
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((
|
|
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
|
|
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
|
|
291
|
+
return p.warn("error", r), this.event.emit("error", r), !1;
|
|
271
292
|
}
|
|
272
|
-
(s = this.socket) == null || s.emit("question",
|
|
293
|
+
(s = this.socket) == null || s.emit("question", o);
|
|
273
294
|
return;
|
|
274
295
|
}
|
|
275
|
-
(e = this.socket) == null || e.emit("question",
|
|
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
|
-
|
|
305
|
+
H as default
|
|
285
306
|
};
|
package/dist/es/Config.mjs
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
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 ?
|
|
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) =>
|
|
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
|
|
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
|
-
|
|
36
|
+
w as configure
|
|
36
37
|
};
|