@glodon-aiot/bot-client-sdk 3.7.0-alpha.3 → 3.9.0-alpha.5
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/BotClient.mjs +62 -55
- 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 {
|
package/dist/es/BotClient.mjs
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
1
|
var k = Object.defineProperty, A = Object.defineProperties;
|
|
2
2
|
var x = Object.getOwnPropertyDescriptors;
|
|
3
|
-
var
|
|
3
|
+
var m = Object.getOwnPropertySymbols;
|
|
4
4
|
var I = Object.prototype.hasOwnProperty, R = Object.prototype.propertyIsEnumerable;
|
|
5
|
-
var
|
|
5
|
+
var g = (r, i, e) => i in r ? k(r, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[i] = e, p = (r, i) => {
|
|
6
6
|
for (var e in i || (i = {}))
|
|
7
|
-
I.call(i, e) &&
|
|
8
|
-
if (
|
|
9
|
-
for (var e of
|
|
10
|
-
R.call(i, e) &&
|
|
7
|
+
I.call(i, e) && g(r, e, i[e]);
|
|
8
|
+
if (m)
|
|
9
|
+
for (var e of m(i))
|
|
10
|
+
R.call(i, e) && g(r, e, i[e]);
|
|
11
11
|
return r;
|
|
12
12
|
}, f = (r, i) => A(r, x(i));
|
|
13
|
-
var d = (r, i, e) => (
|
|
14
|
-
var
|
|
15
|
-
var
|
|
13
|
+
var d = (r, i, e) => (g(r, typeof i != "symbol" ? i + "" : i, e), e);
|
|
14
|
+
var l = (r, i, e) => new Promise((t, s) => {
|
|
15
|
+
var a = (o) => {
|
|
16
16
|
try {
|
|
17
|
-
n(e.next(
|
|
18
|
-
} catch (
|
|
19
|
-
s(
|
|
17
|
+
n(e.next(o));
|
|
18
|
+
} catch (h) {
|
|
19
|
+
s(h);
|
|
20
20
|
}
|
|
21
|
-
}, c = (
|
|
21
|
+
}, c = (o) => {
|
|
22
22
|
try {
|
|
23
|
-
n(e.throw(
|
|
24
|
-
} catch (
|
|
25
|
-
s(
|
|
23
|
+
n(e.throw(o));
|
|
24
|
+
} catch (h) {
|
|
25
|
+
s(h);
|
|
26
26
|
}
|
|
27
|
-
}, n = (
|
|
27
|
+
}, n = (o) => o.done ? t(o.value) : Promise.resolve(o.value).then(a, c);
|
|
28
28
|
n((e = e.apply(r, i)).next());
|
|
29
29
|
});
|
|
30
|
-
import { parseJWT as
|
|
30
|
+
import { parseJWT as y } from "./utils.mjs";
|
|
31
31
|
import E from "./Logger.mjs";
|
|
32
|
-
import { getCVForceService as
|
|
32
|
+
import { getCVForceService as C } from "./api/index.mjs";
|
|
33
33
|
import { configure as S, configuration as v } from "./Config.mjs";
|
|
34
34
|
import { ApplicationTypes as _, PromptVariableType as w } from "@glodon-aiot/apis";
|
|
35
|
-
import
|
|
35
|
+
import L from "./AgentSession.mjs";
|
|
36
36
|
import M from "./DialogSession.mjs";
|
|
37
37
|
import V from "./EventBus.mjs";
|
|
38
38
|
import T from "./package.json.mjs";
|
|
39
39
|
const b = new E("[BotClient] ");
|
|
40
|
-
class
|
|
40
|
+
class W {
|
|
41
41
|
constructor(i, e, t = !0, s = {
|
|
42
42
|
analytics: {
|
|
43
43
|
enabled: !1
|
|
@@ -56,11 +56,11 @@ class U {
|
|
|
56
56
|
//问题长度
|
|
57
57
|
d(this, "_questionContextMaxLength", 4e3);
|
|
58
58
|
d(this, "_promptVariables", []);
|
|
59
|
-
if (this._token = i, this.lazyload = t, this.sdkConfigs = s, S(e), this._applicationId =
|
|
59
|
+
if (this._token = i, this.lazyload = t, this.sdkConfigs = s, S(e), this._applicationId = y(i).rid || v.applicationId, !this._applicationId)
|
|
60
60
|
throw "token is not vaild";
|
|
61
|
-
this.cvforceApi = this.getService(), this.loadApplication().then((
|
|
61
|
+
this.cvforceApi = this.getService(), this.loadApplication().then((a) => {
|
|
62
62
|
S(f(p({}, e), {
|
|
63
|
-
stream: e.stream === void 0 ? (
|
|
63
|
+
stream: e.stream === void 0 ? (a == null ? void 0 : a.type) === _.Dialog || (a == null ? void 0 : a.type) === _.Agent : e.stream
|
|
64
64
|
})), this.event.emit("ready");
|
|
65
65
|
});
|
|
66
66
|
}
|
|
@@ -91,8 +91,8 @@ class U {
|
|
|
91
91
|
this.checkReady(), this._sessions = i, this.event.emit("sessions:updated", i);
|
|
92
92
|
}
|
|
93
93
|
loadApplication() {
|
|
94
|
-
return
|
|
95
|
-
var e, t, s,
|
|
94
|
+
return l(this, null, function* () {
|
|
95
|
+
var e, t, s, a, c;
|
|
96
96
|
this._application = yield this.cvforceApi.getApplicationId(
|
|
97
97
|
this._applicationId,
|
|
98
98
|
// 这里记录应用的名称和版本,以便后期数据分析
|
|
@@ -106,23 +106,23 @@ class U {
|
|
|
106
106
|
recommendQuestionOpen: v.loadRelatedQuesions && n.arrange[0].recommendQuestionOpen
|
|
107
107
|
})]
|
|
108
108
|
})));
|
|
109
|
-
const i = (c = (
|
|
109
|
+
const i = (c = (a = (s = this._application) == null ? void 0 : s.arrange) == null ? void 0 : a[0]) == null ? void 0 : c.llmModel;
|
|
110
110
|
return i && (this._questionContextMaxLength = yield this.cvforceApi.getBaseModel(i).then((n) => n.maxModelLength)), this._promptVariables = yield Promise.all([this.cvforceApi.getPrompts(this._applicationId, {
|
|
111
111
|
type: w.User
|
|
112
112
|
}), this.cvforceApi.getPrompts(this._applicationId, {
|
|
113
113
|
type: w.System
|
|
114
|
-
})]).then(([n,
|
|
114
|
+
})]).then(([n, o]) => [...n, ...o]), this._ready = !0, this.event.emit("application:loaded"), this._application;
|
|
115
115
|
});
|
|
116
116
|
}
|
|
117
117
|
getService() {
|
|
118
|
-
return this.cvforceApi ? this.cvforceApi :
|
|
118
|
+
return this.cvforceApi ? this.cvforceApi : C(v.apiRoot, this.token, (i, e) => b.error(e));
|
|
119
119
|
}
|
|
120
120
|
get token() {
|
|
121
121
|
return this._token;
|
|
122
122
|
}
|
|
123
123
|
set token(i) {
|
|
124
124
|
this.checkReady(), this._token = i, this.ready = !1;
|
|
125
|
-
const e =
|
|
125
|
+
const e = y(i).rid;
|
|
126
126
|
this._applicationId !== e && (this._applicationId = e, this._application = null, this.loadApplication().then(() => {
|
|
127
127
|
this.event.emit("ready");
|
|
128
128
|
}), this.clean());
|
|
@@ -142,46 +142,53 @@ class U {
|
|
|
142
142
|
this._activeSession && this._activeSession.id !== i && this._activeSession.inactivate(), this._activeSession = e;
|
|
143
143
|
});
|
|
144
144
|
}
|
|
145
|
-
createSession(i, e, t, s,
|
|
146
|
-
return
|
|
145
|
+
createSession(i, e, t, s, a) {
|
|
146
|
+
return l(this, null, function* () {
|
|
147
147
|
const c = this;
|
|
148
|
-
return new Promise((n,
|
|
149
|
-
const
|
|
148
|
+
return new Promise((n, o) => {
|
|
149
|
+
const h = (u) => l(this, null, function* () {
|
|
150
150
|
if (u === _.Agent)
|
|
151
|
-
n(new
|
|
151
|
+
n(new L(i, e, t, s, a, c));
|
|
152
152
|
else if (u === _.Dialog)
|
|
153
|
-
n(new M(i, e, t, s,
|
|
153
|
+
n(new M(i, e, t, s, a, c));
|
|
154
154
|
else
|
|
155
155
|
throw Error(`不支持的应用类型:${u}`);
|
|
156
156
|
});
|
|
157
157
|
if (this.ready) {
|
|
158
158
|
if (!this._application)
|
|
159
159
|
throw Error("client is not ready");
|
|
160
|
-
|
|
160
|
+
h(this._application.type);
|
|
161
161
|
} else
|
|
162
162
|
this.event.on("ready", () => {
|
|
163
163
|
if (!this._application)
|
|
164
164
|
throw Error("_application is null");
|
|
165
|
-
|
|
165
|
+
h(this._application.type);
|
|
166
166
|
}, !0);
|
|
167
167
|
});
|
|
168
168
|
});
|
|
169
169
|
}
|
|
170
170
|
getSessions(i) {
|
|
171
|
-
return
|
|
171
|
+
return l(this, null, function* () {
|
|
172
172
|
return new Promise((e, t) => {
|
|
173
|
-
const s = () => this.cvforceApi.listSessions(this._applicationId, i).then((
|
|
173
|
+
const s = () => this.cvforceApi.listSessions(this._applicationId, i).then((a) => l(this, null, function* () {
|
|
174
174
|
const c = [];
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
if (
|
|
178
|
-
|
|
175
|
+
for (const n of a) {
|
|
176
|
+
const o = this._sessions.find((h) => h.id === n.id);
|
|
177
|
+
if (o)
|
|
178
|
+
o.data = n, this._application ? o.application = this._application : yield o.loadApplicationInfo(), c.push(o);
|
|
179
179
|
else {
|
|
180
|
-
const
|
|
181
|
-
c.push(
|
|
180
|
+
const h = yield this.createSession(this.token, n.id, n, this.lazyload, this._application || void 0);
|
|
181
|
+
c.push(h);
|
|
182
182
|
}
|
|
183
|
-
}
|
|
184
|
-
|
|
183
|
+
}
|
|
184
|
+
if (i != null && i.pageSize) {
|
|
185
|
+
const n = [...this._sessions, ...c], o = /* @__PURE__ */ new Map();
|
|
186
|
+
for (const h of n)
|
|
187
|
+
o.set(h.id, h);
|
|
188
|
+
return this.sessions = Array.from(o.values()), this.sessions;
|
|
189
|
+
} else
|
|
190
|
+
return this.sessions = c, c;
|
|
191
|
+
}));
|
|
185
192
|
if (this.ready) {
|
|
186
193
|
if (!this._application)
|
|
187
194
|
throw Error("_application is null");
|
|
@@ -196,7 +203,7 @@ class U {
|
|
|
196
203
|
});
|
|
197
204
|
}
|
|
198
205
|
loadSession(i = "", e) {
|
|
199
|
-
return
|
|
206
|
+
return l(this, null, function* () {
|
|
200
207
|
this.checkReady();
|
|
201
208
|
let t = this.sessions.find((s) => s.id === i);
|
|
202
209
|
if (t || (t = yield this.createSession(this.token, i, e), this._sessions.push(t)), this._activeSession) {
|
|
@@ -210,12 +217,12 @@ class U {
|
|
|
210
217
|
});
|
|
211
218
|
}
|
|
212
219
|
reload() {
|
|
213
|
-
return this.loadApplication().then(() =>
|
|
220
|
+
return this.loadApplication().then(() => l(this, null, function* () {
|
|
214
221
|
return yield this.getSessions(), this.event.emit("ready"), this;
|
|
215
222
|
}));
|
|
216
223
|
}
|
|
217
224
|
reloadSession(i) {
|
|
218
|
-
return
|
|
225
|
+
return l(this, null, function* () {
|
|
219
226
|
yield this.reload();
|
|
220
227
|
let e = this._sessions.find((t) => t.id === i) || this._activeSession;
|
|
221
228
|
if (!e) {
|
|
@@ -226,8 +233,8 @@ class U {
|
|
|
226
233
|
});
|
|
227
234
|
}
|
|
228
235
|
setTop(i, e, t = "top_time desc, sort_status desc, updated_at desc") {
|
|
229
|
-
return
|
|
230
|
-
let s = i ? this._sessions.find((
|
|
236
|
+
return l(this, null, function* () {
|
|
237
|
+
let s = i ? this._sessions.find((a) => a.id === i) : this._activeSession;
|
|
231
238
|
if (!s) {
|
|
232
239
|
console.warn("setTop session failed.");
|
|
233
240
|
return;
|
|
@@ -250,7 +257,7 @@ class U {
|
|
|
250
257
|
this.event.off(i, e);
|
|
251
258
|
}
|
|
252
259
|
removeSession(i, e) {
|
|
253
|
-
return
|
|
260
|
+
return l(this, null, function* () {
|
|
254
261
|
this.checkReady();
|
|
255
262
|
let t = -1;
|
|
256
263
|
if (typeof i == "string" ? t = this._sessions.findIndex((s) => s.id === i) : t = this._sessions.findIndex((s) => s.id === i.id), t > -1) {
|
|
@@ -265,5 +272,5 @@ class U {
|
|
|
265
272
|
}
|
|
266
273
|
}
|
|
267
274
|
export {
|
|
268
|
-
|
|
275
|
+
W as default
|
|
269
276
|
};
|