@glodon-aiot/bot-client-sdk 3.7.0-alpha.2 → 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.
- package/dist/es/AgentSession.mjs +113 -104
- package/dist/es/DialogSession.mjs +101 -95
- package/dist/es/Session.mjs +96 -87
- package/dist/es/package.json.mjs +2 -2
- package/dist/lib/index.js +3 -3
- package/dist/src/AgentSession.d.ts +1 -0
- package/dist/src/DialogSession.d.ts +1 -0
- package/dist/src/Session.d.ts +1 -0
- package/package.json +2 -2
package/dist/es/AgentSession.mjs
CHANGED
|
@@ -1,88 +1,88 @@
|
|
|
1
|
-
var A = Object.defineProperty,
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
for (var
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
}, w = (
|
|
13
|
-
var v = (
|
|
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
|
-
|
|
17
|
-
} catch (
|
|
18
|
-
|
|
16
|
+
n(i.next(a));
|
|
17
|
+
} catch (g) {
|
|
18
|
+
r(g);
|
|
19
19
|
}
|
|
20
20
|
}, e = (a) => {
|
|
21
21
|
try {
|
|
22
|
-
|
|
23
|
-
} catch (
|
|
24
|
-
|
|
22
|
+
n(i.throw(a));
|
|
23
|
+
} catch (g) {
|
|
24
|
+
r(g);
|
|
25
25
|
}
|
|
26
|
-
},
|
|
27
|
-
|
|
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
|
|
30
|
+
import E from "./Logger.mjs";
|
|
31
31
|
import { configuration as u } from "./Config.mjs";
|
|
32
|
-
import { io as
|
|
33
|
-
import { UserMode as
|
|
34
|
-
import q, { SessionErrors as
|
|
35
|
-
import
|
|
36
|
-
const p = new
|
|
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(
|
|
39
|
-
super(
|
|
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((
|
|
43
|
-
this.socket &&
|
|
44
|
-
const
|
|
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 ?
|
|
51
|
-
}, o = new URL(u.apiRoot),
|
|
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:
|
|
53
|
+
query: i,
|
|
54
54
|
transports: ["websocket"]
|
|
55
55
|
});
|
|
56
|
-
|
|
57
|
-
p.log("socket:connect"), this.event.emit("socket:connect"),
|
|
58
|
-
}),
|
|
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
|
-
}),
|
|
60
|
+
}), r.on("ready", (s) => {
|
|
61
61
|
if (s.code === 200) {
|
|
62
62
|
p.log("ready");
|
|
63
|
-
const e = u.debug ?
|
|
64
|
-
e ===
|
|
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
|
-
|
|
68
|
+
M.SocketConnectionFailed
|
|
69
69
|
// , {
|
|
70
70
|
// cause: data,
|
|
71
71
|
// }
|
|
72
72
|
);
|
|
73
|
-
p.error("error", e), this.event.emit("error", e),
|
|
73
|
+
p.error("error", e), this.event.emit("error", e), t(!1);
|
|
74
74
|
}
|
|
75
|
-
}),
|
|
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:
|
|
79
|
+
type: n,
|
|
80
80
|
data: {
|
|
81
81
|
runnerIsClient: a,
|
|
82
|
-
pluginCode:
|
|
83
|
-
funcCode:
|
|
82
|
+
pluginCode: g,
|
|
83
|
+
funcCode: h,
|
|
84
84
|
parameters: c,
|
|
85
|
-
requestBody:
|
|
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
|
-
|
|
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 (
|
|
127
|
-
if (p.log(`calling function ${
|
|
128
|
-
${JSON.stringify(c)}`), !u.plugins[
|
|
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[
|
|
135
|
+
const f = u.plugins[g][h];
|
|
136
136
|
typeof f != "function" && e({
|
|
137
137
|
code: 404,
|
|
138
138
|
message: "对不起,没有找到插件方法。"
|
|
139
139
|
});
|
|
140
|
-
let
|
|
140
|
+
let R = c == null ? void 0 : c.query;
|
|
141
141
|
try {
|
|
142
|
-
|
|
143
|
-
} catch (
|
|
142
|
+
R = JSON.parse(c == null ? void 0 : c.query);
|
|
143
|
+
} catch (k) {
|
|
144
144
|
}
|
|
145
|
-
f(
|
|
146
|
-
this._stopAnswer || e(...
|
|
145
|
+
f(R, (...k) => {
|
|
146
|
+
this._stopAnswer || e(...k);
|
|
147
147
|
});
|
|
148
148
|
}
|
|
149
|
-
})),
|
|
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
|
|
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:
|
|
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,
|
|
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 =
|
|
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 =
|
|
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
|
|
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",
|
|
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 =
|
|
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
|
-
}),
|
|
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
|
-
|
|
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:
|
|
226
|
+
data: n,
|
|
227
227
|
requestId: a
|
|
228
|
-
} = s,
|
|
229
|
-
if (
|
|
228
|
+
} = s, g = this.messages.findIndex((c) => c.messageId === a && c.role === y.User);
|
|
229
|
+
if (g < 0)
|
|
230
230
|
return;
|
|
231
|
-
const
|
|
232
|
-
if (
|
|
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[
|
|
251
|
-
reference:
|
|
252
|
-
}), yield this.fillMessageReference(this.messages[
|
|
253
|
-
searchReference:
|
|
254
|
-
}), this.event.emit("message:searchreference",
|
|
255
|
-
})), this.socket =
|
|
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((
|
|
259
|
+
return u.mcp ? new Promise((t, i) => {
|
|
260
260
|
if (!this.socket) {
|
|
261
|
-
|
|
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((
|
|
266
|
-
o.data[
|
|
267
|
-
}),
|
|
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(
|
|
271
|
+
addMcpServers(t) {
|
|
272
272
|
return v(this, null, function* () {
|
|
273
|
-
return this.ready ? this.socket ? new Promise((
|
|
274
|
-
this.socket && this.socket.emit("add_mcp_servers",
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
}),
|
|
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((
|
|
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 ?
|
|
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(
|
|
291
|
+
sendQuestion(t, i) {
|
|
292
292
|
return v(this, null, function* () {
|
|
293
|
-
var
|
|
294
|
-
|
|
295
|
-
|
|
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((
|
|
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: (
|
|
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
|
|
308
|
-
|
|
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",
|
|
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
|
|
323
|
-
(
|
|
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 {
|