@glodon-aiot/bot-client-sdk 3.3.2-beta.9 → 3.3.5-alpha.16
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 +115 -112
- package/dist/es/BotClient.mjs +69 -65
- package/dist/es/DialogSession.mjs +50 -47
- package/dist/es/Session.mjs +53 -53
- package/dist/es/package.json.mjs +11 -11
- package/dist/lib/index.js +3 -3
- package/dist/src/constant.d.ts +13 -0
- package/package.json +2 -2
package/dist/es/AgentSession.mjs
CHANGED
|
@@ -1,87 +1,87 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
for (var
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var
|
|
10
|
-
|
|
1
|
+
var C = Object.defineProperty, P = Object.defineProperties;
|
|
2
|
+
var A = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var R = Object.getOwnPropertySymbols;
|
|
4
|
+
var x = Object.prototype.hasOwnProperty, E = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var S = (m, i, r) => i in m ? C(m, i, { enumerable: !0, configurable: !0, writable: !0, value: r }) : m[i] = r, h = (m, i) => {
|
|
6
|
+
for (var r in i || (i = {}))
|
|
7
|
+
x.call(i, r) && S(m, r, i[r]);
|
|
8
|
+
if (R)
|
|
9
|
+
for (var r of R(i))
|
|
10
|
+
E.call(i, r) && S(m, r, i[r]);
|
|
11
11
|
return m;
|
|
12
|
-
}, w = (m,
|
|
13
|
-
var v = (m,
|
|
14
|
-
var s = (
|
|
12
|
+
}, w = (m, i) => P(m, A(i));
|
|
13
|
+
var v = (m, i, r) => new Promise((o, n) => {
|
|
14
|
+
var s = (a) => {
|
|
15
15
|
try {
|
|
16
|
-
r
|
|
17
|
-
} catch (
|
|
18
|
-
|
|
16
|
+
t(r.next(a));
|
|
17
|
+
} catch (g) {
|
|
18
|
+
n(g);
|
|
19
19
|
}
|
|
20
|
-
}, e = (
|
|
20
|
+
}, e = (a) => {
|
|
21
21
|
try {
|
|
22
|
-
r
|
|
23
|
-
} catch (
|
|
24
|
-
|
|
22
|
+
t(r.throw(a));
|
|
23
|
+
} catch (g) {
|
|
24
|
+
n(g);
|
|
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 k } from "@glodon-aiot/apis";
|
|
30
|
-
import
|
|
29
|
+
import { ChatMessageRole as k, PromptVariableType as _ } from "@glodon-aiot/apis";
|
|
30
|
+
import q from "./Logger.mjs";
|
|
31
31
|
import { configuration as u } from "./Config.mjs";
|
|
32
|
-
import { io as
|
|
33
|
-
import { UserMode as
|
|
34
|
-
import
|
|
35
|
-
import
|
|
36
|
-
const p = new
|
|
37
|
-
class H extends
|
|
38
|
-
constructor(
|
|
39
|
-
super(
|
|
32
|
+
import { io as L } from "socket.io-client";
|
|
33
|
+
import { UserMode as y } from "./PayloadTypes.mjs";
|
|
34
|
+
import N, { SessionErrors as M } from "./Session.mjs";
|
|
35
|
+
import U from "dayjs";
|
|
36
|
+
const p = new q("[AgentSession] ");
|
|
37
|
+
class H extends N {
|
|
38
|
+
constructor(i, r = "", o, n, s, e) {
|
|
39
|
+
super(i, r, o, n, s, e);
|
|
40
40
|
}
|
|
41
41
|
prepareConnection() {
|
|
42
|
-
return new Promise((
|
|
43
|
-
this.socket &&
|
|
44
|
-
const
|
|
42
|
+
return new Promise((i) => v(this, null, function* () {
|
|
43
|
+
this.socket && i(!0);
|
|
44
|
+
const r = {
|
|
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 ? y.Debug : y.Normal
|
|
51
|
+
}, o = new URL(u.apiRoot), n = yield L(o.origin, {
|
|
52
52
|
path: o.pathname.replace("api", "socketio") + "/copilot",
|
|
53
|
-
query:
|
|
53
|
+
query: r,
|
|
54
54
|
transports: ["websocket"]
|
|
55
55
|
});
|
|
56
|
-
|
|
57
|
-
p.log("socket:connect"), this.event.emit("socket:connect"),
|
|
58
|
-
}),
|
|
56
|
+
n.on("connect", () => {
|
|
57
|
+
p.log("socket:connect"), this.event.emit("socket:connect"), n.emit("init");
|
|
58
|
+
}), n.on("disconnect", () => {
|
|
59
59
|
p.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
|
|
60
|
-
}),
|
|
60
|
+
}), n.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 ? y.Debug : y.Normal;
|
|
64
|
+
e === y.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), i(!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), i(!1);
|
|
74
74
|
}
|
|
75
|
-
}),
|
|
75
|
+
}), n.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: t,
|
|
80
80
|
data: {
|
|
81
|
-
runnerIsClient:
|
|
82
|
-
pluginCode:
|
|
81
|
+
runnerIsClient: a,
|
|
82
|
+
pluginCode: g,
|
|
83
83
|
funcCode: d,
|
|
84
|
-
parameters:
|
|
84
|
+
parameters: c,
|
|
85
85
|
requestBody: j
|
|
86
86
|
}
|
|
87
87
|
} = s, f = this.messages[this.messages.length - 1];
|
|
@@ -112,7 +112,7 @@ class H extends L {
|
|
|
112
112
|
}
|
|
113
113
|
if (this.inactivating) {
|
|
114
114
|
const l = new Error(
|
|
115
|
-
|
|
115
|
+
M.Inactive
|
|
116
116
|
// {
|
|
117
117
|
// cause: {
|
|
118
118
|
// code: -1,
|
|
@@ -122,57 +122,57 @@ class H extends L {
|
|
|
122
122
|
// }
|
|
123
123
|
);
|
|
124
124
|
p.warn("error", l), this.event.emit("error", l);
|
|
125
|
-
} else if (
|
|
126
|
-
if (p.log(`calling function ${d} of plugin ${
|
|
127
|
-
${JSON.stringify(
|
|
125
|
+
} else if (t === "FUNCTION_CALL" && a) {
|
|
126
|
+
if (p.log(`calling function ${d} of plugin ${g} with argument:
|
|
127
|
+
${JSON.stringify(c)}`), !u.plugins[g]) {
|
|
128
128
|
e({
|
|
129
129
|
code: 404,
|
|
130
130
|
message: "对不起,没有找到插件。"
|
|
131
131
|
});
|
|
132
132
|
return;
|
|
133
133
|
}
|
|
134
|
-
const l = u.plugins[
|
|
134
|
+
const l = u.plugins[g][d];
|
|
135
135
|
typeof l != "function" && e({
|
|
136
136
|
code: 404,
|
|
137
137
|
message: "对不起,没有找到插件方法。"
|
|
138
138
|
});
|
|
139
|
-
let I =
|
|
139
|
+
let I = c == null ? void 0 : c.query;
|
|
140
140
|
try {
|
|
141
|
-
I = JSON.parse(
|
|
142
|
-
} catch (
|
|
141
|
+
I = JSON.parse(c == null ? void 0 : c.query);
|
|
142
|
+
} catch (b) {
|
|
143
143
|
}
|
|
144
|
-
l(I, (...
|
|
145
|
-
this._stopAnswer || e(...
|
|
144
|
+
l(I, (...b) => {
|
|
145
|
+
this._stopAnswer || e(...b);
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
|
-
})),
|
|
148
|
+
})), n.on("answer", (s) => {
|
|
149
149
|
if (!this._stopAnswer)
|
|
150
|
-
if (s.data.code !== 200 && (s.data.index = -1), s.data.stream || s.data.stream === void 0) {
|
|
150
|
+
if (s.data.code !== 200 && (s.data.index = -1, this.event.emit("error", s.data)), s.data.stream || s.data.stream === void 0) {
|
|
151
151
|
let e = this.messages[this.messages.length - 1];
|
|
152
152
|
if (e && e.role === k.Robot)
|
|
153
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
154
|
else {
|
|
155
|
-
const
|
|
155
|
+
const t = {
|
|
156
156
|
content: "",
|
|
157
|
-
role:
|
|
157
|
+
role: k.Robot,
|
|
158
158
|
reference: [],
|
|
159
159
|
searchReference: [],
|
|
160
160
|
messageId: "",
|
|
161
161
|
sessionId: this.id,
|
|
162
|
-
messageTime:
|
|
162
|
+
messageTime: U(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
|
|
163
163
|
comment: {
|
|
164
164
|
isAgree: 0
|
|
165
165
|
},
|
|
166
166
|
isReplying: !0
|
|
167
167
|
};
|
|
168
|
-
this.handleStreamAnswer(s.data,
|
|
168
|
+
this.handleStreamAnswer(s.data, t), this.pushMessage(t), this.event.emit("message:content", t), t.index === -1 && this.event.emit("answer:finish", t);
|
|
169
169
|
}
|
|
170
170
|
} else if (s.data.index === -1) {
|
|
171
171
|
let e = this.messages[this.messages.length - 1];
|
|
172
172
|
if (e && !e.messageId)
|
|
173
|
-
e =
|
|
173
|
+
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
174
|
else {
|
|
175
|
-
const
|
|
175
|
+
const t = h({
|
|
176
176
|
messageId: "",
|
|
177
177
|
content: "",
|
|
178
178
|
role: k.Robot,
|
|
@@ -186,35 +186,35 @@ class H extends L {
|
|
|
186
186
|
isAgree: 0
|
|
187
187
|
}
|
|
188
188
|
}, s.data);
|
|
189
|
-
this.event.emit("message:content",
|
|
189
|
+
this.event.emit("message:content", t), this.pushMessage(t), this.event.emit("answer:finish", t);
|
|
190
190
|
}
|
|
191
191
|
} else {
|
|
192
192
|
let e = this.messages[this.messages.length - 1];
|
|
193
|
-
e && !e.messageId ? (e =
|
|
193
|
+
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));
|
|
194
194
|
}
|
|
195
|
-
}),
|
|
195
|
+
}), n.on("reference", (s) => v(this, null, function* () {
|
|
196
196
|
if (this._stopAnswer)
|
|
197
197
|
return;
|
|
198
198
|
if (!this.id) {
|
|
199
|
-
const
|
|
200
|
-
|
|
199
|
+
const c = new Error(
|
|
200
|
+
M.IdUndefined
|
|
201
201
|
// {
|
|
202
202
|
// cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
|
|
203
203
|
// }
|
|
204
204
|
);
|
|
205
|
-
p.error(
|
|
205
|
+
p.error(c), this.event.emit("error", c), this.frozen = !1;
|
|
206
206
|
return;
|
|
207
207
|
}
|
|
208
208
|
const {
|
|
209
209
|
type: e,
|
|
210
|
-
data:
|
|
211
|
-
requestId:
|
|
212
|
-
} = s,
|
|
213
|
-
if (
|
|
210
|
+
data: t,
|
|
211
|
+
requestId: a
|
|
212
|
+
} = s, g = this.messages.findIndex((c) => c.messageId === a && c.role === k.User);
|
|
213
|
+
if (g < 0)
|
|
214
214
|
return;
|
|
215
|
-
const d =
|
|
215
|
+
const d = g + 1;
|
|
216
216
|
if (d >= this.messages.length) {
|
|
217
|
-
const
|
|
217
|
+
const c = {
|
|
218
218
|
messageId: "",
|
|
219
219
|
content: "",
|
|
220
220
|
role: k.Robot,
|
|
@@ -228,67 +228,70 @@ class H extends L {
|
|
|
228
228
|
isAgree: 0
|
|
229
229
|
}
|
|
230
230
|
};
|
|
231
|
-
this.pushMessage(
|
|
231
|
+
this.pushMessage(c);
|
|
232
232
|
}
|
|
233
|
-
e === "KG" ? (this.messages[d] = w(
|
|
234
|
-
reference:
|
|
235
|
-
}), yield this.fillMessageReference(this.messages[d]), this.event.emit("message:reference",
|
|
236
|
-
searchReference:
|
|
237
|
-
}), this.event.emit("message:searchreference",
|
|
238
|
-
})), this.socket =
|
|
233
|
+
e === "KG" ? (this.messages[d] = w(h({}, this.messages[d]), {
|
|
234
|
+
reference: t.knowledgeReference
|
|
235
|
+
}), yield this.fillMessageReference(this.messages[d]), this.event.emit("message:reference", t.knowledgeReference), this.event.emit("message:updated", this.messages[d])) : e === "WEB_SEARCH" && (this.messages[d] = w(h({}, this.messages[d]), {
|
|
236
|
+
searchReference: t.knowledgeReference
|
|
237
|
+
}), this.event.emit("message:searchreference", t.searchReference), this.event.emit("message:updated", this.messages[d]));
|
|
238
|
+
})), this.socket = n, n.open();
|
|
239
239
|
}));
|
|
240
240
|
}
|
|
241
241
|
registerMcpFromClientConfig() {
|
|
242
|
-
return u.mcp ? new Promise((
|
|
242
|
+
return u.mcp ? new Promise((i, r) => {
|
|
243
243
|
if (!this.socket) {
|
|
244
|
-
|
|
244
|
+
r("socket is undefined");
|
|
245
245
|
return;
|
|
246
246
|
}
|
|
247
247
|
this.socket.emit("add_mcp_servers", u.mcp, (o) => {
|
|
248
|
-
o.code === 200 ? (Object.keys(o.data).forEach((
|
|
249
|
-
o.data[
|
|
250
|
-
}),
|
|
248
|
+
o.code === 200 ? (Object.keys(o.data).forEach((n) => {
|
|
249
|
+
o.data[n].code !== 200 && console.error(n, "MCP Server 注册失败。", o.data[n]);
|
|
250
|
+
}), i(!0)) : (console.error("MCP Server 注册失败。", o), r(o));
|
|
251
251
|
});
|
|
252
252
|
}) : Promise.resolve(!0);
|
|
253
253
|
}
|
|
254
|
-
addMcpServers(
|
|
254
|
+
addMcpServers(i) {
|
|
255
255
|
return v(this, null, function* () {
|
|
256
|
-
return this.ready ? this.socket ? new Promise((
|
|
257
|
-
this.socket && this.socket.emit("add_mcp_servers",
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}),
|
|
256
|
+
return this.ready ? this.socket ? new Promise((r, o) => {
|
|
257
|
+
this.socket && this.socket.emit("add_mcp_servers", i, (n) => {
|
|
258
|
+
n.code === 200 ? (Object.keys(n.data).forEach((s) => {
|
|
259
|
+
n.data[s].code !== 200 && console.error(s, "MCP Server 注册失败。", n.data[s]);
|
|
260
|
+
}), r(!0)) : (console.error("MCP Server 注册失败。", n), o(n));
|
|
261
261
|
});
|
|
262
262
|
}) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
|
|
263
263
|
});
|
|
264
264
|
}
|
|
265
265
|
listMcpServers() {
|
|
266
266
|
return v(this, null, function* () {
|
|
267
|
-
return this.ready ? this.socket ? new Promise((
|
|
267
|
+
return this.ready ? this.socket ? new Promise((i, r) => {
|
|
268
268
|
this.socket && this.socket.emit("list_mcp_servers", (o) => {
|
|
269
|
-
o.code === 200 ?
|
|
269
|
+
o.code === 200 ? i(o.data) : r(o);
|
|
270
270
|
});
|
|
271
271
|
}) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
|
|
272
272
|
});
|
|
273
273
|
}
|
|
274
|
-
sendQuestion(
|
|
274
|
+
sendQuestion(i, r) {
|
|
275
275
|
return v(this, null, function* () {
|
|
276
|
-
var
|
|
277
|
-
let o = w(
|
|
278
|
-
promptVariables: this.promptVariables.reduce((
|
|
279
|
-
[
|
|
276
|
+
var n, s, e;
|
|
277
|
+
let o = w(h({}, i), {
|
|
278
|
+
promptVariables: this.promptVariables.filter((t) => t.type === _.User).reduce((t, a) => w(h({}, t), {
|
|
279
|
+
[a.key]: a.value
|
|
280
|
+
}), {}),
|
|
281
|
+
systemPromptVariables: this.promptVariables.filter((t) => t.type === _.System).reduce((t, a) => w(h({}, t), {
|
|
282
|
+
[a.key]: a.value
|
|
280
283
|
}), {}),
|
|
281
|
-
knowledges: (
|
|
284
|
+
knowledges: (n = this.knowledges) == null ? void 0 : n.map((t) => t.id)
|
|
282
285
|
});
|
|
283
286
|
if (!this.active) {
|
|
284
287
|
if (yield this.activate(), !this.socket) {
|
|
285
|
-
const
|
|
286
|
-
|
|
288
|
+
const t = new Error(
|
|
289
|
+
M.SocketConnectionFailed
|
|
287
290
|
// {
|
|
288
291
|
// cause: { code: -1, message: 'socket is null' },
|
|
289
292
|
// }
|
|
290
293
|
);
|
|
291
|
-
return p.warn("error",
|
|
294
|
+
return p.warn("error", t), this.event.emit("error", t), !1;
|
|
292
295
|
}
|
|
293
296
|
(s = this.socket) == null || s.emit("question", o);
|
|
294
297
|
return;
|
|
@@ -297,8 +300,8 @@ class H extends L {
|
|
|
297
300
|
});
|
|
298
301
|
}
|
|
299
302
|
stopAnswer() {
|
|
300
|
-
var
|
|
301
|
-
(
|
|
303
|
+
var i;
|
|
304
|
+
(i = this.socket) == null || i.emit("stop_answer");
|
|
302
305
|
}
|
|
303
306
|
}
|
|
304
307
|
export {
|
package/dist/es/BotClient.mjs
CHANGED
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var u = (
|
|
1
|
+
var k = Object.defineProperty, A = Object.defineProperties;
|
|
2
|
+
var x = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var m = Object.getOwnPropertySymbols;
|
|
4
|
+
var I = Object.prototype.hasOwnProperty, R = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var u = (r, i, e) => i in r ? k(r, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[i] = e, d = (r, i) => {
|
|
6
6
|
for (var e in i || (i = {}))
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var e of
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
}, f = (
|
|
13
|
-
var
|
|
14
|
-
var h = (
|
|
15
|
-
var
|
|
7
|
+
I.call(i, e) && u(r, e, i[e]);
|
|
8
|
+
if (m)
|
|
9
|
+
for (var e of m(i))
|
|
10
|
+
R.call(i, e) && u(r, e, i[e]);
|
|
11
|
+
return r;
|
|
12
|
+
}, f = (r, i) => A(r, x(i));
|
|
13
|
+
var p = (r, i, e) => (u(r, typeof i != "symbol" ? i + "" : i, e), e);
|
|
14
|
+
var h = (r, i, e) => new Promise((t, s) => {
|
|
15
|
+
var a = (o) => {
|
|
16
16
|
try {
|
|
17
|
-
n(e.next(
|
|
17
|
+
n(e.next(o));
|
|
18
18
|
} catch (l) {
|
|
19
19
|
s(l);
|
|
20
20
|
}
|
|
21
|
-
}, c = (
|
|
21
|
+
}, c = (o) => {
|
|
22
22
|
try {
|
|
23
|
-
n(e.throw(
|
|
23
|
+
n(e.throw(o));
|
|
24
24
|
} catch (l) {
|
|
25
25
|
s(l);
|
|
26
26
|
}
|
|
27
|
-
}, n = (
|
|
28
|
-
n((e = e.apply(
|
|
27
|
+
}, n = (o) => o.done ? t(o.value) : Promise.resolve(o.value).then(a, c);
|
|
28
|
+
n((e = e.apply(r, i)).next());
|
|
29
29
|
});
|
|
30
|
-
import { parseJWT as
|
|
31
|
-
import
|
|
32
|
-
import { getCVForceService as
|
|
33
|
-
import { configure as S, configuration as
|
|
34
|
-
import { ApplicationTypes as
|
|
35
|
-
import
|
|
36
|
-
import
|
|
37
|
-
import
|
|
38
|
-
import
|
|
39
|
-
const
|
|
40
|
-
class
|
|
30
|
+
import { parseJWT as g } from "./utils.mjs";
|
|
31
|
+
import E from "./Logger.mjs";
|
|
32
|
+
import { getCVForceService as L } from "./api/index.mjs";
|
|
33
|
+
import { configure as S, configuration as y } from "./Config.mjs";
|
|
34
|
+
import { ApplicationTypes as v, PromptVariableType as w } from "@glodon-aiot/apis";
|
|
35
|
+
import C from "./AgentSession.mjs";
|
|
36
|
+
import M from "./DialogSession.mjs";
|
|
37
|
+
import V from "./EventBus.mjs";
|
|
38
|
+
import T from "./package.json.mjs";
|
|
39
|
+
const b = new E("[BotClient] ");
|
|
40
|
+
class U {
|
|
41
41
|
constructor(i, e, t = !0, s = {
|
|
42
42
|
analytics: {
|
|
43
43
|
enabled: !1
|
|
44
44
|
}
|
|
45
45
|
}) {
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
p(this, "event", new V());
|
|
47
|
+
p(this, "cvforceApi");
|
|
48
48
|
//从token解析出来的用户信息
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
p(this, "_applicationId");
|
|
50
|
+
p(this, "_application", null);
|
|
51
51
|
//session列表,被加载过的session将会被缓存到这个列表中
|
|
52
|
-
|
|
52
|
+
p(this, "_sessions", []);
|
|
53
53
|
//活跃的session,只支持一个活跃的session
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
p(this, "_activeSession");
|
|
55
|
+
p(this, "_ready", !1);
|
|
56
56
|
//问题长度
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (this._token = i, this.lazyload = t, this.sdkConfigs = s, S(e), this._applicationId =
|
|
57
|
+
p(this, "_questionContextMaxLength", 4e3);
|
|
58
|
+
p(this, "_promptVariables", []);
|
|
59
|
+
if (this._token = i, this.lazyload = t, this.sdkConfigs = s, S(e), this._applicationId = g(i).rid || y.applicationId, !this._applicationId)
|
|
60
60
|
throw "token is not vaild";
|
|
61
|
-
this.cvforceApi = this.getService(), this.loadApplication().then((
|
|
62
|
-
S(f(
|
|
63
|
-
stream: e.stream === void 0 ? (
|
|
61
|
+
this.cvforceApi = this.getService(), this.loadApplication().then((a) => {
|
|
62
|
+
S(f(d({}, e), {
|
|
63
|
+
stream: e.stream === void 0 ? (a == null ? void 0 : a.type) === v.Dialog : e.stream
|
|
64
64
|
})), this.event.emit("ready");
|
|
65
65
|
});
|
|
66
66
|
}
|
|
@@ -92,33 +92,37 @@ class F {
|
|
|
92
92
|
}
|
|
93
93
|
loadApplication() {
|
|
94
94
|
return h(this, null, function* () {
|
|
95
|
-
var e, t, s,
|
|
95
|
+
var e, t, s, a, c;
|
|
96
96
|
this._application = yield this.cvforceApi.getApplicationId(
|
|
97
97
|
this._applicationId,
|
|
98
98
|
// 这里记录应用的名称和版本,以便后期数据分析
|
|
99
99
|
{
|
|
100
100
|
ui: (e = this.sdkConfigs.ui) == null ? void 0 : e.name,
|
|
101
101
|
uiVersion: (t = this.sdkConfigs.ui) == null ? void 0 : t.version,
|
|
102
|
-
sdkVersion:
|
|
102
|
+
sdkVersion: T.version
|
|
103
103
|
}
|
|
104
|
-
).then((n) => (typeof n.arrange[0].recommendQuestionOpen != "boolean" && (n.arrange[0].recommendQuestionOpen = !0), f(
|
|
105
|
-
arrange: [f(
|
|
106
|
-
recommendQuestionOpen:
|
|
104
|
+
).then((n) => (typeof n.arrange[0].recommendQuestionOpen != "boolean" && (n.arrange[0].recommendQuestionOpen = !0), f(d({}, n), {
|
|
105
|
+
arrange: [f(d({}, n.arrange[0]), {
|
|
106
|
+
recommendQuestionOpen: y.loadRelatedQuesions && n.arrange[0].recommendQuestionOpen
|
|
107
107
|
})]
|
|
108
108
|
})));
|
|
109
|
-
const i = (c = (
|
|
110
|
-
return i && (this._questionContextMaxLength = yield this.cvforceApi.getBaseModel(i).then((n) => n.maxModelLength)), this._promptVariables = yield this.cvforceApi.getPrompts(this._applicationId
|
|
109
|
+
const i = (c = (a = (s = this._application) == null ? void 0 : s.arrange) == null ? void 0 : a[0]) == null ? void 0 : c.llmModel;
|
|
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
|
+
type: w.User
|
|
112
|
+
}), this.cvforceApi.getPrompts(this._applicationId, {
|
|
113
|
+
type: w.System
|
|
114
|
+
})]).then(([n, o]) => [...n, ...o]), this._ready = !0, this.event.emit("application:loaded"), this._application;
|
|
111
115
|
});
|
|
112
116
|
}
|
|
113
117
|
getService() {
|
|
114
|
-
return this.cvforceApi ? this.cvforceApi :
|
|
118
|
+
return this.cvforceApi ? this.cvforceApi : L(y.apiRoot, this.token, (i, e) => b.error(e));
|
|
115
119
|
}
|
|
116
120
|
get token() {
|
|
117
121
|
return this._token;
|
|
118
122
|
}
|
|
119
123
|
set token(i) {
|
|
120
124
|
this.checkReady(), this._token = i, this.ready = !1;
|
|
121
|
-
const e =
|
|
125
|
+
const e = g(i).rid;
|
|
122
126
|
this._applicationId !== e && (this._applicationId = e, this._application = null, this.loadApplication().then(() => {
|
|
123
127
|
this.event.emit("ready");
|
|
124
128
|
}), this.clean());
|
|
@@ -138,15 +142,15 @@ class F {
|
|
|
138
142
|
this._activeSession && this._activeSession.id !== i && this._activeSession.inactivate(), this._activeSession = e;
|
|
139
143
|
});
|
|
140
144
|
}
|
|
141
|
-
createSession(i, e, t, s,
|
|
145
|
+
createSession(i, e, t, s, a) {
|
|
142
146
|
return h(this, null, function* () {
|
|
143
147
|
const c = this;
|
|
144
|
-
return new Promise((n,
|
|
148
|
+
return new Promise((n, o) => {
|
|
145
149
|
const l = (_) => h(this, null, function* () {
|
|
146
|
-
if (_ ===
|
|
147
|
-
n(new
|
|
148
|
-
else if (_ ===
|
|
149
|
-
n(new
|
|
150
|
+
if (_ === v.Agent)
|
|
151
|
+
n(new C(i, e, t, s, a, c));
|
|
152
|
+
else if (_ === v.Dialog)
|
|
153
|
+
n(new M(i, e, t, s, a, c));
|
|
150
154
|
else
|
|
151
155
|
throw Error(`不支持的应用类型:${_}`);
|
|
152
156
|
});
|
|
@@ -166,12 +170,12 @@ class F {
|
|
|
166
170
|
getSessions(i) {
|
|
167
171
|
return h(this, null, function* () {
|
|
168
172
|
return new Promise((e, t) => {
|
|
169
|
-
const s = () => this.cvforceApi.listSessions(this._applicationId, i).then((
|
|
173
|
+
const s = () => this.cvforceApi.listSessions(this._applicationId, i).then((a) => {
|
|
170
174
|
const c = [];
|
|
171
|
-
return
|
|
172
|
-
const
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
+
return a.forEach((n) => h(this, null, function* () {
|
|
176
|
+
const o = this._sessions.find((l) => n.id === l.id);
|
|
177
|
+
if (o)
|
|
178
|
+
o.data = n, this._application ? o.application = this._application : o.loadApplicationInfo(), c.push(o);
|
|
175
179
|
else {
|
|
176
180
|
const l = yield this.createSession(this.token, n.id, n, this.lazyload, this._application || void 0);
|
|
177
181
|
c.push(l);
|
|
@@ -223,7 +227,7 @@ class F {
|
|
|
223
227
|
}
|
|
224
228
|
setTop(i, e, t = "top_time desc, sort_status desc, updated_at desc") {
|
|
225
229
|
return h(this, null, function* () {
|
|
226
|
-
let s = i ? this._sessions.find((
|
|
230
|
+
let s = i ? this._sessions.find((a) => a.id === i) : this._activeSession;
|
|
227
231
|
if (!s) {
|
|
228
232
|
console.warn("setTop session failed.");
|
|
229
233
|
return;
|
|
@@ -261,5 +265,5 @@ class F {
|
|
|
261
265
|
}
|
|
262
266
|
}
|
|
263
267
|
export {
|
|
264
|
-
|
|
268
|
+
U as default
|
|
265
269
|
};
|