@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.
- package/dist/es/AgentSession.mjs +172 -131
- package/dist/es/BotClient.mjs +83 -77
- package/dist/es/Config.mjs +7 -6
- package/dist/es/DialogSession.mjs +142 -169
- package/dist/es/Session.mjs +252 -211
- 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 +7 -1
- package/package.json +2 -2
package/dist/es/AgentSession.mjs
CHANGED
|
@@ -1,87 +1,88 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
for (var
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var
|
|
10
|
-
E.call(
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
var
|
|
14
|
-
var s = (
|
|
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
|
-
|
|
17
|
-
} catch (
|
|
18
|
-
|
|
16
|
+
t(r.next(a));
|
|
17
|
+
} catch (h) {
|
|
18
|
+
n(h);
|
|
19
19
|
}
|
|
20
|
-
}, e = (
|
|
20
|
+
}, e = (a) => {
|
|
21
21
|
try {
|
|
22
|
-
|
|
23
|
-
} catch (
|
|
24
|
-
|
|
22
|
+
t(r.throw(a));
|
|
23
|
+
} catch (h) {
|
|
24
|
+
n(h);
|
|
25
25
|
}
|
|
26
|
-
},
|
|
27
|
-
|
|
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
|
|
30
|
-
import
|
|
31
|
-
import { configuration as
|
|
32
|
-
import { io as
|
|
33
|
-
import { UserMode as
|
|
34
|
-
import
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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((
|
|
42
|
-
this.socket &&
|
|
43
|
-
const
|
|
42
|
+
return new Promise((i) => v(this, null, function* () {
|
|
43
|
+
this.socket && i(!0);
|
|
44
|
+
const r = {
|
|
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:
|
|
52
|
-
query:
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
}),
|
|
58
|
-
|
|
59
|
-
}),
|
|
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
|
-
|
|
62
|
-
const e =
|
|
63
|
-
e ===
|
|
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
|
-
|
|
68
|
+
I.SocketConnectionFailed
|
|
68
69
|
// , {
|
|
69
70
|
// cause: data,
|
|
70
71
|
// }
|
|
71
72
|
);
|
|
72
|
-
|
|
73
|
+
p.error("error", e), this.event.emit("error", e), i(!1);
|
|
73
74
|
}
|
|
74
|
-
}),
|
|
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:
|
|
79
|
+
type: t,
|
|
79
80
|
data: {
|
|
80
|
-
runnerIsClient:
|
|
81
|
-
pluginCode:
|
|
82
|
-
funcCode:
|
|
83
|
-
parameters:
|
|
84
|
-
requestBody:
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
124
|
-
} else if (
|
|
125
|
-
if (
|
|
126
|
-
${JSON.stringify(
|
|
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 =
|
|
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 =
|
|
140
|
+
let M = c == null ? void 0 : c.query;
|
|
139
141
|
try {
|
|
140
|
-
M = JSON.parse(
|
|
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
|
-
})),
|
|
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.
|
|
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 =
|
|
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
|
|
176
|
+
const t = d({
|
|
155
177
|
messageId: "",
|
|
156
178
|
content: "",
|
|
157
|
-
role:
|
|
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",
|
|
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 =
|
|
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
|
-
}),
|
|
211
|
+
}), n.on("reference", (s) => v(this, null, function* () {
|
|
175
212
|
if (this._stopAnswer)
|
|
176
213
|
return;
|
|
177
214
|
if (!this.id) {
|
|
178
|
-
const
|
|
179
|
-
|
|
215
|
+
const c = new Error(
|
|
216
|
+
I.IdUndefined
|
|
180
217
|
// {
|
|
181
218
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
182
219
|
// }
|
|
183
220
|
);
|
|
184
|
-
|
|
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:
|
|
190
|
-
requestId:
|
|
191
|
-
} = s,
|
|
192
|
-
if (
|
|
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
|
|
195
|
-
if (
|
|
196
|
-
const
|
|
231
|
+
const g = h + 1;
|
|
232
|
+
if (g >= this.messages.length) {
|
|
233
|
+
const c = {
|
|
197
234
|
messageId: "",
|
|
198
235
|
content: "",
|
|
199
|
-
role:
|
|
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(
|
|
248
|
+
this.pushMessage(c);
|
|
211
249
|
}
|
|
212
|
-
e === "KG" ? (this.messages[
|
|
213
|
-
reference:
|
|
214
|
-
}), yield this.fillMessageReference(this.messages[
|
|
215
|
-
searchReference:
|
|
216
|
-
}), this.event.emit("message:searchreference",
|
|
217
|
-
})), this.socket =
|
|
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
|
|
259
|
+
return u.mcp ? new Promise((i, r) => {
|
|
222
260
|
if (!this.socket) {
|
|
223
|
-
|
|
261
|
+
r("socket is undefined");
|
|
224
262
|
return;
|
|
225
263
|
}
|
|
226
|
-
this.socket.emit("add_mcp_servers",
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}),
|
|
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(
|
|
234
|
-
return
|
|
235
|
-
return this.ready ? this.socket ? new Promise((
|
|
236
|
-
this.socket && this.socket.emit("add_mcp_servers",
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}),
|
|
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
|
|
246
|
-
return this.ready ? this.socket ? new Promise((
|
|
247
|
-
this.socket && this.socket.emit("list_mcp_servers", (
|
|
248
|
-
|
|
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(
|
|
254
|
-
return
|
|
255
|
-
var
|
|
256
|
-
let
|
|
257
|
-
promptVariables: this.promptVariables.reduce((
|
|
258
|
-
[
|
|
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: (
|
|
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
|
|
265
|
-
|
|
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
|
|
311
|
+
return p.warn("error", t), this.event.emit("error", t), !1;
|
|
271
312
|
}
|
|
272
|
-
(s = this.socket) == null || s.emit("question",
|
|
313
|
+
(s = this.socket) == null || s.emit("question", o);
|
|
273
314
|
return;
|
|
274
315
|
}
|
|
275
|
-
(e = this.socket) == null || e.emit("question",
|
|
316
|
+
(e = this.socket) == null || e.emit("question", o);
|
|
276
317
|
});
|
|
277
318
|
}
|
|
278
319
|
stopAnswer() {
|
|
279
|
-
var
|
|
280
|
-
(
|
|
320
|
+
var i;
|
|
321
|
+
(i = this.socket) == null || i.emit("stop_answer");
|
|
281
322
|
}
|
|
282
323
|
}
|
|
283
324
|
export {
|
|
284
|
-
|
|
325
|
+
H as default
|
|
285
326
|
};
|