@glodon-aiot/bot-client-sdk 3.7.0-alpha.3 → 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
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var R = (
|
|
6
|
-
for (var
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
}, y = (
|
|
13
|
-
var
|
|
14
|
-
var f = (
|
|
1
|
+
var I = Object.defineProperty, A = Object.defineProperties;
|
|
2
|
+
var P = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var w = Object.getOwnPropertySymbols;
|
|
4
|
+
var b = Object.prototype.hasOwnProperty, D = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var R = (l, c, s) => c in l ? I(l, c, { enumerable: !0, configurable: !0, writable: !0, value: s }) : l[c] = s, u = (l, c) => {
|
|
6
|
+
for (var s in c || (c = {}))
|
|
7
|
+
b.call(c, s) && R(l, s, c[s]);
|
|
8
|
+
if (w)
|
|
9
|
+
for (var s of w(c))
|
|
10
|
+
D.call(c, s) && R(l, s, c[s]);
|
|
11
|
+
return l;
|
|
12
|
+
}, y = (l, c) => A(l, P(c));
|
|
13
|
+
var M = (l, c, s) => (R(l, typeof c != "symbol" ? c + "" : c, s), s);
|
|
14
|
+
var f = (l, c, s) => new Promise((a, d) => {
|
|
15
15
|
var h = (r) => {
|
|
16
16
|
try {
|
|
17
|
-
i(
|
|
18
|
-
} catch (
|
|
19
|
-
d(
|
|
17
|
+
i(s.next(r));
|
|
18
|
+
} catch (m) {
|
|
19
|
+
d(m);
|
|
20
20
|
}
|
|
21
21
|
}, e = (r) => {
|
|
22
22
|
try {
|
|
23
|
-
i(
|
|
24
|
-
} catch (
|
|
25
|
-
d(
|
|
23
|
+
i(s.throw(r));
|
|
24
|
+
} catch (m) {
|
|
25
|
+
d(m);
|
|
26
26
|
}
|
|
27
|
-
}, i = (r) => r.done ?
|
|
28
|
-
i((
|
|
27
|
+
}, i = (r) => r.done ? a(r.value) : Promise.resolve(r.value).then(h, e);
|
|
28
|
+
i((s = s.apply(l, c)).next());
|
|
29
29
|
});
|
|
30
|
-
import { IHistoryRole as
|
|
31
|
-
import
|
|
32
|
-
import
|
|
33
|
-
import
|
|
30
|
+
import { IHistoryRole as C, PromptVariableType as v, ChatMessageRole as O } from "@glodon-aiot/apis";
|
|
31
|
+
import V from "./Logger.mjs";
|
|
32
|
+
import $ from "./Session.mjs";
|
|
33
|
+
import _ from "dayjs";
|
|
34
34
|
import { configuration as S } from "./Config.mjs";
|
|
35
|
-
import { fetchEventSource as
|
|
36
|
-
const
|
|
37
|
-
class
|
|
38
|
-
constructor(
|
|
39
|
-
super(
|
|
40
|
-
|
|
35
|
+
import { fetchEventSource as T } from "./api/fetchEventSource/fetch.mjs";
|
|
36
|
+
const p = new V("[DialogSession] ");
|
|
37
|
+
class k extends $ {
|
|
38
|
+
constructor(s, a = "", d, h, e, i) {
|
|
39
|
+
super(s, a, d, h, e, i);
|
|
40
|
+
M(this, "ctrl", new AbortController());
|
|
41
41
|
}
|
|
42
42
|
prepareConnection() {
|
|
43
|
-
return new Promise((
|
|
44
|
-
this._ready = !0, this.event.emit("ready"), this.frozen = !1,
|
|
43
|
+
return new Promise((s) => f(this, null, function* () {
|
|
44
|
+
this._ready = !0, this.event.emit("ready"), this.frozen = !1, s(!0);
|
|
45
45
|
}));
|
|
46
46
|
}
|
|
47
|
-
postChatV2EventSource(
|
|
47
|
+
postChatV2EventSource(s, a, d, h, e, i) {
|
|
48
48
|
return f(this, null, function* () {
|
|
49
49
|
const r = {
|
|
50
50
|
code: 0,
|
|
51
51
|
// messageId: uuidv4(),
|
|
52
52
|
content: "",
|
|
53
|
-
role:
|
|
54
|
-
sessionId:
|
|
53
|
+
role: C.Robot,
|
|
54
|
+
sessionId: a,
|
|
55
55
|
reference: [],
|
|
56
56
|
searchReference: [],
|
|
57
57
|
messageTime: "",
|
|
@@ -60,16 +60,16 @@ class B extends V {
|
|
|
60
60
|
isAgree: 0
|
|
61
61
|
},
|
|
62
62
|
isReplying: !1
|
|
63
|
-
},
|
|
63
|
+
}, m = y(u({}, d), {
|
|
64
64
|
stream: S.stream
|
|
65
65
|
});
|
|
66
|
-
|
|
66
|
+
T(`${S.apiRoot}/chat/v2/chat?applicationId=${s}&sessionId=${a}`, {
|
|
67
67
|
method: "POST",
|
|
68
68
|
headers: {
|
|
69
69
|
Authorization: `Bearer ${this.token}`,
|
|
70
70
|
"Content-Type": "application/json"
|
|
71
71
|
},
|
|
72
|
-
body: JSON.stringify(
|
|
72
|
+
body: JSON.stringify(m),
|
|
73
73
|
openWhenHidden: !0,
|
|
74
74
|
signal: h,
|
|
75
75
|
onopen(g) {
|
|
@@ -77,41 +77,41 @@ class B extends V {
|
|
|
77
77
|
},
|
|
78
78
|
onmessage(g) {
|
|
79
79
|
if (!g.data.includes('{"code": ')) {
|
|
80
|
-
let
|
|
80
|
+
let t = {};
|
|
81
81
|
try {
|
|
82
|
-
|
|
83
|
-
} catch (
|
|
82
|
+
t = JSON.parse(g.data);
|
|
83
|
+
} catch (J) {
|
|
84
84
|
}
|
|
85
|
-
(
|
|
86
|
-
const
|
|
85
|
+
(t == null ? void 0 : t.mtype) === "search" ? r.searchReference.push(t) : r.content = r.content + g.data;
|
|
86
|
+
const o = {
|
|
87
87
|
content: r.content,
|
|
88
88
|
role: 1,
|
|
89
89
|
reference: r.reference,
|
|
90
90
|
searchReference: r.searchReference,
|
|
91
91
|
messageId: r.messageId,
|
|
92
|
-
sessionId:
|
|
93
|
-
messageTime:
|
|
92
|
+
sessionId: a,
|
|
93
|
+
messageTime: _(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
|
|
94
94
|
comment: {
|
|
95
95
|
isAgree: 0
|
|
96
96
|
},
|
|
97
97
|
isReplying: !0
|
|
98
98
|
};
|
|
99
|
-
return (e == null ? void 0 : e.onmessage) && e.onmessage(
|
|
99
|
+
return (e == null ? void 0 : e.onmessage) && e.onmessage(o);
|
|
100
100
|
}
|
|
101
|
-
const
|
|
102
|
-
if (r.reference =
|
|
103
|
-
const
|
|
101
|
+
const n = JSON.parse(g.data);
|
|
102
|
+
if (r.reference = n.data.reference, r.searchReference = n.data.searchReference, r.messageTime = n.data.messageTime, r.messageId = n.data.messageId, r.llmModel = n.data.llmModel, r.content = n.data.content, r.code = n.code, n.code === 200) {
|
|
103
|
+
const t = {
|
|
104
104
|
code: r.code,
|
|
105
105
|
content: r.content,
|
|
106
106
|
role: 1,
|
|
107
107
|
reference: r.reference,
|
|
108
108
|
searchReference: r.searchReference,
|
|
109
109
|
messageId: r.messageId,
|
|
110
|
-
sessionId:
|
|
110
|
+
sessionId: a,
|
|
111
111
|
messageTime: r.messageTime,
|
|
112
112
|
isReplying: !0
|
|
113
113
|
};
|
|
114
|
-
e != null && e.onmessage && e.onmessage(
|
|
114
|
+
e != null && e.onmessage && e.onmessage(t);
|
|
115
115
|
} else
|
|
116
116
|
return e != null && e.onclose && e.onclose(r), i && i({
|
|
117
117
|
code: r.code,
|
|
@@ -129,7 +129,7 @@ class B extends V {
|
|
|
129
129
|
reference: r.reference,
|
|
130
130
|
searchReference: r.searchReference,
|
|
131
131
|
messageId: r.messageId,
|
|
132
|
-
sessionId:
|
|
132
|
+
sessionId: a,
|
|
133
133
|
messageTime: r.messageTime,
|
|
134
134
|
comment: {
|
|
135
135
|
isAgree: 0
|
|
@@ -142,14 +142,14 @@ class B extends V {
|
|
|
142
142
|
});
|
|
143
143
|
});
|
|
144
144
|
}
|
|
145
|
-
postChatV3EventSource(
|
|
145
|
+
postChatV3EventSource(s, a, d, h, e, i) {
|
|
146
146
|
return f(this, null, function* () {
|
|
147
|
-
const r = this,
|
|
147
|
+
const r = this, m = {
|
|
148
148
|
code: 0,
|
|
149
149
|
// messageId: uuidv4(),
|
|
150
150
|
content: "",
|
|
151
|
-
role:
|
|
152
|
-
sessionId:
|
|
151
|
+
role: C.Robot,
|
|
152
|
+
sessionId: a,
|
|
153
153
|
reference: [],
|
|
154
154
|
searchReference: [],
|
|
155
155
|
messageTime: "",
|
|
@@ -165,7 +165,7 @@ class B extends V {
|
|
|
165
165
|
}, g = y(u({}, d), {
|
|
166
166
|
stream: S.stream
|
|
167
167
|
});
|
|
168
|
-
|
|
168
|
+
T(`${S.apiRoot}/chat/v3/chat?applicationId=${s}&sessionId=${a}`, {
|
|
169
169
|
method: "POST",
|
|
170
170
|
headers: {
|
|
171
171
|
Authorization: `Bearer ${this.token}`,
|
|
@@ -174,78 +174,78 @@ class B extends V {
|
|
|
174
174
|
body: JSON.stringify(g),
|
|
175
175
|
openWhenHidden: !0,
|
|
176
176
|
signal: h,
|
|
177
|
-
onopen(
|
|
178
|
-
return
|
|
177
|
+
onopen(n) {
|
|
178
|
+
return n.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
|
|
179
179
|
},
|
|
180
|
-
onmessage(
|
|
181
|
-
if (!
|
|
180
|
+
onmessage(n) {
|
|
181
|
+
if (!n.data && !n.event && !n.id && !n.retry)
|
|
182
182
|
return;
|
|
183
|
-
let
|
|
183
|
+
let t = {};
|
|
184
184
|
try {
|
|
185
|
-
|
|
186
|
-
} catch (
|
|
187
|
-
console.error("parse eventData to JSON failed. ",
|
|
185
|
+
t = JSON.parse(n.data);
|
|
186
|
+
} catch (o) {
|
|
187
|
+
console.error("parse eventData to JSON failed. ", n.data);
|
|
188
188
|
}
|
|
189
|
-
if (
|
|
190
|
-
return
|
|
191
|
-
code:
|
|
192
|
-
message:
|
|
189
|
+
if (t.code !== 200)
|
|
190
|
+
return m.index = -1, m.code = t.code, m.message = t.message, e != null && e.onmessage && e.onmessage(m), i && i({
|
|
191
|
+
code: t.code,
|
|
192
|
+
message: t.message
|
|
193
193
|
});
|
|
194
|
-
r.handleStreamAnswer(
|
|
194
|
+
r.handleStreamAnswer(t, m), e != null && e.onmessage && e.onmessage(m);
|
|
195
195
|
},
|
|
196
196
|
onerror() {
|
|
197
197
|
e != null && e.onerror && e.onerror();
|
|
198
198
|
},
|
|
199
199
|
onclose() {
|
|
200
|
-
const
|
|
200
|
+
const n = y(u({}, m), {
|
|
201
201
|
isReplying: !1
|
|
202
202
|
});
|
|
203
|
-
e != null && e.onclose && e.onclose(
|
|
203
|
+
e != null && e.onclose && e.onclose(n);
|
|
204
204
|
}
|
|
205
205
|
});
|
|
206
206
|
});
|
|
207
207
|
}
|
|
208
|
-
sendQuestion(
|
|
208
|
+
sendQuestion(s, a) {
|
|
209
209
|
return f(this, null, function* () {
|
|
210
|
-
var g;
|
|
211
210
|
const {
|
|
212
211
|
content: d,
|
|
213
212
|
fileIds: h,
|
|
214
213
|
oldMessageId: e,
|
|
215
214
|
imageContent: i
|
|
216
|
-
} =
|
|
215
|
+
} = s, {
|
|
217
216
|
stream: r,
|
|
218
|
-
referenceFirst:
|
|
219
|
-
} =
|
|
217
|
+
referenceFirst: m
|
|
218
|
+
} = a;
|
|
220
219
|
if (this.active || (yield this.activate()), this._id) {
|
|
221
|
-
|
|
220
|
+
const g = this.convertKnowledgesToChatFormat(this.knowledges);
|
|
221
|
+
let n = {
|
|
222
222
|
content: d,
|
|
223
223
|
fileIds: h,
|
|
224
|
-
referenceFirst:
|
|
224
|
+
referenceFirst: m,
|
|
225
225
|
oldMessageId: e,
|
|
226
|
-
promptVariables: this.promptVariables.filter((
|
|
227
|
-
[
|
|
226
|
+
promptVariables: this.promptVariables.filter((t) => t.type === v.User).reduce((t, o) => y(u({}, t), {
|
|
227
|
+
[o.key]: o.value
|
|
228
228
|
}), {}),
|
|
229
|
-
systemPromptVariables: this.promptVariables.filter((
|
|
230
|
-
[
|
|
229
|
+
systemPromptVariables: this.promptVariables.filter((t) => t.type === v.System).reduce((t, o) => y(u({}, t), {
|
|
230
|
+
[o.key]: o.value
|
|
231
231
|
}), {}),
|
|
232
|
-
knowledges:
|
|
232
|
+
knowledges: g,
|
|
233
233
|
//开启内容摘要
|
|
234
234
|
enableTitleSummary: !0,
|
|
235
235
|
imageContent: i
|
|
236
236
|
};
|
|
237
|
-
this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id,
|
|
237
|
+
this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, n, this.ctrl.signal, {
|
|
238
238
|
onopen: () => {
|
|
239
|
-
|
|
239
|
+
p.log("open", this.ctrl);
|
|
240
240
|
},
|
|
241
|
-
onmessage: (
|
|
241
|
+
onmessage: (t) => {
|
|
242
242
|
if (this._stopAnswer)
|
|
243
243
|
return;
|
|
244
|
-
let
|
|
245
|
-
|
|
244
|
+
let o = this.messages[this.messages.length - 1];
|
|
245
|
+
o && o.role === O.Robot ? (o = u(u({}, o), t), this.messages[this.messages.length - 1] = o, this.event.emit("message:content", o), this.event.emit("message:updated", o)) : (this.pushMessage(t), this.event.emit("message:content", t));
|
|
246
246
|
},
|
|
247
|
-
onclose: (
|
|
248
|
-
this._stopAnswer || (
|
|
247
|
+
onclose: (t) => f(this, null, function* () {
|
|
248
|
+
this._stopAnswer || (p.log(t, "onclose"), this.event.emit("answer:finish", t));
|
|
249
249
|
}),
|
|
250
250
|
onerror: () => {
|
|
251
251
|
this._stopAnswer || (this.event.emit("message:error", ""), this.frozen = !1);
|
|
@@ -262,7 +262,7 @@ class B extends V {
|
|
|
262
262
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
263
263
|
});
|
|
264
264
|
}
|
|
265
|
-
addMcpServers(
|
|
265
|
+
addMcpServers(s) {
|
|
266
266
|
return f(this, null, function* () {
|
|
267
267
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
268
268
|
});
|
|
@@ -272,7 +272,13 @@ class B extends V {
|
|
|
272
272
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
273
273
|
});
|
|
274
274
|
}
|
|
275
|
+
convertKnowledgesToChatFormat(s) {
|
|
276
|
+
return s ? s.map((a) => a != null && a.fileIds ? {
|
|
277
|
+
id: a.id,
|
|
278
|
+
fileIds: a.fileIds
|
|
279
|
+
} : a.id) : [];
|
|
280
|
+
}
|
|
275
281
|
}
|
|
276
282
|
export {
|
|
277
|
-
|
|
283
|
+
k as default
|
|
278
284
|
};
|