@glodon-aiot/bot-client-sdk 3.3.2-beta.0 → 3.3.2-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 +116 -95
- package/dist/es/Config.mjs +8 -7
- package/dist/es/DialogSession.mjs +137 -167
- package/dist/es/Session.mjs +211 -171
- package/dist/es/package.json.mjs +2 -2
- package/dist/lib/index.js +4 -4
- package/dist/src/Config.d.ts +2 -0
- package/dist/src/Session.d.ts +6 -0
- package/package.json +2 -2
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
for (var
|
|
7
|
-
|
|
8
|
-
if (
|
|
9
|
-
for (var
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
var
|
|
14
|
-
var
|
|
15
|
-
var d = (
|
|
1
|
+
var b = Object.defineProperty, A = Object.defineProperties;
|
|
2
|
+
var I = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var M = Object.getOwnPropertySymbols;
|
|
4
|
+
var k = Object.prototype.hasOwnProperty, D = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var R = (o, n, a) => n in o ? b(o, n, { enumerable: !0, configurable: !0, writable: !0, value: a }) : o[n] = a, f = (o, n) => {
|
|
6
|
+
for (var a in n || (n = {}))
|
|
7
|
+
k.call(n, a) && R(o, a, n[a]);
|
|
8
|
+
if (M)
|
|
9
|
+
for (var a of M(n))
|
|
10
|
+
D.call(n, a) && R(o, a, n[a]);
|
|
11
|
+
return o;
|
|
12
|
+
}, u = (o, n) => A(o, I(n));
|
|
13
|
+
var w = (o, n, a) => (R(o, typeof n != "symbol" ? n + "" : n, a), a);
|
|
14
|
+
var h = (o, n, a) => new Promise((m, i) => {
|
|
15
|
+
var d = (r) => {
|
|
16
16
|
try {
|
|
17
|
-
g(
|
|
18
|
-
} catch (
|
|
19
|
-
|
|
17
|
+
g(a.next(r));
|
|
18
|
+
} catch (c) {
|
|
19
|
+
i(c);
|
|
20
20
|
}
|
|
21
|
-
},
|
|
21
|
+
}, e = (r) => {
|
|
22
22
|
try {
|
|
23
|
-
g(
|
|
24
|
-
} catch (
|
|
25
|
-
|
|
23
|
+
g(a.throw(r));
|
|
24
|
+
} catch (c) {
|
|
25
|
+
i(c);
|
|
26
26
|
}
|
|
27
|
-
}, g = (
|
|
28
|
-
g((
|
|
27
|
+
}, g = (r) => r.done ? m(r.value) : Promise.resolve(r.value).then(d, e);
|
|
28
|
+
g((a = a.apply(o, n)).next());
|
|
29
29
|
});
|
|
30
|
-
import { IHistoryRole as
|
|
31
|
-
import
|
|
32
|
-
import
|
|
33
|
-
import
|
|
34
|
-
import { configuration as
|
|
35
|
-
import { fetchEventSource as
|
|
36
|
-
const
|
|
37
|
-
class
|
|
38
|
-
constructor(
|
|
39
|
-
super(
|
|
40
|
-
|
|
30
|
+
import { IHistoryRole as C, ChatMessageRole as P } from "@glodon-aiot/apis";
|
|
31
|
+
import p from "./Logger.mjs";
|
|
32
|
+
import O from "./Session.mjs";
|
|
33
|
+
import $ from "dayjs";
|
|
34
|
+
import { configuration as y } from "./Config.mjs";
|
|
35
|
+
import { fetchEventSource as v } from "./api/fetchEventSource/fetch.mjs";
|
|
36
|
+
const T = new p("[DialogSession] ");
|
|
37
|
+
class j extends O {
|
|
38
|
+
constructor(a, m = "", i, d, e, g) {
|
|
39
|
+
super(a, m, i, d, e, g);
|
|
40
|
+
w(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((a) => h(this, null, function* () {
|
|
44
|
+
this._ready = !0, this.event.emit("ready"), this.frozen = !1, a(!0);
|
|
45
45
|
}));
|
|
46
46
|
}
|
|
47
|
-
postChatV2EventSource(
|
|
48
|
-
return
|
|
49
|
-
const
|
|
47
|
+
postChatV2EventSource(a, m, i, d, e, g) {
|
|
48
|
+
return h(this, null, function* () {
|
|
49
|
+
const r = {
|
|
50
50
|
code: 0,
|
|
51
51
|
// messageId: uuidv4(),
|
|
52
52
|
content: "",
|
|
53
|
-
role:
|
|
54
|
-
sessionId:
|
|
53
|
+
role: C.Robot,
|
|
54
|
+
sessionId: m,
|
|
55
55
|
reference: [],
|
|
56
56
|
searchReference: [],
|
|
57
57
|
messageTime: "",
|
|
@@ -60,96 +60,96 @@ class H extends J {
|
|
|
60
60
|
isAgree: 0
|
|
61
61
|
},
|
|
62
62
|
isReplying: !1
|
|
63
|
-
},
|
|
64
|
-
stream:
|
|
63
|
+
}, c = u(f({}, i), {
|
|
64
|
+
stream: y.stream
|
|
65
65
|
});
|
|
66
|
-
|
|
66
|
+
v(`${y.apiRoot}/chat/v2/chat?applicationId=${a}&sessionId=${m}`, {
|
|
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(c),
|
|
73
73
|
openWhenHidden: !0,
|
|
74
74
|
signal: d,
|
|
75
|
-
onopen(
|
|
76
|
-
return
|
|
75
|
+
onopen(l) {
|
|
76
|
+
return l.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
|
|
77
77
|
},
|
|
78
|
-
onmessage(
|
|
79
|
-
if (!
|
|
80
|
-
let
|
|
78
|
+
onmessage(l) {
|
|
79
|
+
if (!l.data.includes('{"code": ')) {
|
|
80
|
+
let t = {};
|
|
81
81
|
try {
|
|
82
|
-
|
|
83
|
-
} catch (
|
|
82
|
+
t = JSON.parse(l.data);
|
|
83
|
+
} catch (_) {
|
|
84
84
|
}
|
|
85
|
-
(
|
|
86
|
-
const
|
|
87
|
-
content:
|
|
85
|
+
(t == null ? void 0 : t.mtype) === "search" ? r.searchReference.push(t) : r.content = r.content + l.data;
|
|
86
|
+
const S = {
|
|
87
|
+
content: r.content,
|
|
88
88
|
role: 1,
|
|
89
|
-
reference:
|
|
90
|
-
searchReference:
|
|
91
|
-
messageId:
|
|
92
|
-
sessionId:
|
|
93
|
-
messageTime:
|
|
89
|
+
reference: r.reference,
|
|
90
|
+
searchReference: r.searchReference,
|
|
91
|
+
messageId: r.messageId,
|
|
92
|
+
sessionId: m,
|
|
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 (
|
|
99
|
+
return (e == null ? void 0 : e.onmessage) && e.onmessage(S);
|
|
100
100
|
}
|
|
101
|
-
const
|
|
102
|
-
if (
|
|
103
|
-
const
|
|
104
|
-
code:
|
|
105
|
-
content:
|
|
101
|
+
const s = JSON.parse(l.data);
|
|
102
|
+
if (r.reference = s.data.reference, r.searchReference = s.data.searchReference, r.messageTime = s.data.messageTime, r.messageId = s.data.messageId, r.llmModel = s.data.llmModel, r.content = s.data.content, r.code = s.code, s.code === 200) {
|
|
103
|
+
const t = {
|
|
104
|
+
code: r.code,
|
|
105
|
+
content: r.content,
|
|
106
106
|
role: 1,
|
|
107
|
-
reference:
|
|
108
|
-
searchReference:
|
|
109
|
-
messageId:
|
|
110
|
-
sessionId:
|
|
111
|
-
messageTime:
|
|
107
|
+
reference: r.reference,
|
|
108
|
+
searchReference: r.searchReference,
|
|
109
|
+
messageId: r.messageId,
|
|
110
|
+
sessionId: m,
|
|
111
|
+
messageTime: r.messageTime,
|
|
112
112
|
isReplying: !0
|
|
113
113
|
};
|
|
114
|
-
|
|
114
|
+
e != null && e.onmessage && e.onmessage(t);
|
|
115
115
|
} else
|
|
116
|
-
return
|
|
117
|
-
code:
|
|
118
|
-
message:
|
|
116
|
+
return e != null && e.onclose && e.onclose(r), g && g({
|
|
117
|
+
code: r.code,
|
|
118
|
+
message: r.message
|
|
119
119
|
});
|
|
120
120
|
},
|
|
121
121
|
onerror() {
|
|
122
|
-
|
|
122
|
+
e != null && e.onerror && e.onerror();
|
|
123
123
|
},
|
|
124
124
|
onclose() {
|
|
125
|
-
const
|
|
126
|
-
code:
|
|
127
|
-
content:
|
|
125
|
+
const l = {
|
|
126
|
+
code: r.code,
|
|
127
|
+
content: r.content,
|
|
128
128
|
role: 1,
|
|
129
|
-
reference:
|
|
130
|
-
searchReference:
|
|
131
|
-
messageId:
|
|
132
|
-
sessionId:
|
|
133
|
-
messageTime:
|
|
129
|
+
reference: r.reference,
|
|
130
|
+
searchReference: r.searchReference,
|
|
131
|
+
messageId: r.messageId,
|
|
132
|
+
sessionId: m,
|
|
133
|
+
messageTime: r.messageTime,
|
|
134
134
|
comment: {
|
|
135
135
|
isAgree: 0
|
|
136
136
|
},
|
|
137
137
|
isReplying: !1,
|
|
138
138
|
index: -1
|
|
139
139
|
};
|
|
140
|
-
|
|
140
|
+
e != null && e.onclose && e.onclose(l);
|
|
141
141
|
}
|
|
142
142
|
});
|
|
143
143
|
});
|
|
144
144
|
}
|
|
145
|
-
postChatV3EventSource(
|
|
146
|
-
return
|
|
147
|
-
const
|
|
145
|
+
postChatV3EventSource(a, m, i, d, e, g) {
|
|
146
|
+
return h(this, null, function* () {
|
|
147
|
+
const r = this, c = {
|
|
148
148
|
code: 0,
|
|
149
149
|
// messageId: uuidv4(),
|
|
150
150
|
content: "",
|
|
151
|
-
role:
|
|
152
|
-
sessionId:
|
|
151
|
+
role: C.Robot,
|
|
152
|
+
sessionId: m,
|
|
153
153
|
reference: [],
|
|
154
154
|
searchReference: [],
|
|
155
155
|
messageTime: "",
|
|
@@ -162,10 +162,10 @@ class H extends J {
|
|
|
162
162
|
thinkingSecs: "",
|
|
163
163
|
thinkingStatus: "",
|
|
164
164
|
searchingStatus: ""
|
|
165
|
-
}, l =
|
|
166
|
-
stream:
|
|
165
|
+
}, l = u(f({}, i), {
|
|
166
|
+
stream: y.stream
|
|
167
167
|
});
|
|
168
|
-
|
|
168
|
+
v(`${y.apiRoot}/chat/v3/chat?applicationId=${a}&sessionId=${m}`, {
|
|
169
169
|
method: "POST",
|
|
170
170
|
headers: {
|
|
171
171
|
Authorization: `Bearer ${this.token}`,
|
|
@@ -174,101 +174,71 @@ class H extends J {
|
|
|
174
174
|
body: JSON.stringify(l),
|
|
175
175
|
openWhenHidden: !0,
|
|
176
176
|
signal: d,
|
|
177
|
-
onopen(
|
|
178
|
-
return
|
|
177
|
+
onopen(s) {
|
|
178
|
+
return s.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
|
|
179
179
|
},
|
|
180
|
-
onmessage(
|
|
181
|
-
if (!
|
|
180
|
+
onmessage(s) {
|
|
181
|
+
if (!s.data && !s.event && !s.id && !s.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(s.data);
|
|
186
|
+
} catch (S) {
|
|
187
|
+
console.error("parse eventData to JSON failed. ", s.data);
|
|
188
188
|
}
|
|
189
|
-
if (
|
|
190
|
-
return
|
|
191
|
-
code:
|
|
192
|
-
message:
|
|
189
|
+
if (t.code !== 200)
|
|
190
|
+
return c.index = -1, c.code = t.code, c.message = t.message, e != null && e.onmessage && e.onmessage(c), g && g({
|
|
191
|
+
code: t.code,
|
|
192
|
+
message: t.message
|
|
193
193
|
});
|
|
194
|
-
|
|
195
|
-
e.isReplying = !0;
|
|
196
|
-
const M = () => {
|
|
197
|
-
if (!e.searchingStatus) {
|
|
198
|
-
e.searchingStatus = "start";
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
if (e.searchingStatus === "start") {
|
|
202
|
-
e.searchingStatus = "end";
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
};
|
|
206
|
-
if (a === "beforeSearch" && M(), a === "searchOver" && M(), a === "search" && e.searchReference.push(h), a === "thinking") {
|
|
207
|
-
e.thinkingStatus = "start";
|
|
208
|
-
const S = h.choices[0];
|
|
209
|
-
e.reasoningContent = e.reasoningContent + S.delta.reasoningContent;
|
|
210
|
-
}
|
|
211
|
-
if (a === "thinkingOver") {
|
|
212
|
-
e.thinkingStatus = "end";
|
|
213
|
-
const y = h.thinkingSecs;
|
|
214
|
-
e.thinkingSecs = y;
|
|
215
|
-
}
|
|
216
|
-
if (a === "text") {
|
|
217
|
-
const S = h.choices[0];
|
|
218
|
-
e.content = e.content + S.delta.content;
|
|
219
|
-
}
|
|
220
|
-
if (a === "reference" && (e.reference = h), a === "result") {
|
|
221
|
-
const y = h.messageId, S = h.llmModel, b = h.messageTime;
|
|
222
|
-
e.messageId = y, e.llmModel = S, e.messageTime = b, e.sessionId = o;
|
|
223
|
-
}
|
|
224
|
-
a === "over" ? e.index = -1 : (e.code = r.code, e.message = r.message), s != null && s.onmessage && s.onmessage(e);
|
|
194
|
+
r.handleStreamAnswer(t, c), e != null && e.onmessage && e.onmessage(c);
|
|
225
195
|
},
|
|
226
196
|
onerror() {
|
|
227
|
-
|
|
197
|
+
e != null && e.onerror && e.onerror();
|
|
228
198
|
},
|
|
229
199
|
onclose() {
|
|
230
|
-
const
|
|
200
|
+
const s = u(f({}, c), {
|
|
231
201
|
isReplying: !1
|
|
232
202
|
});
|
|
233
|
-
|
|
203
|
+
e != null && e.onclose && e.onclose(s);
|
|
234
204
|
}
|
|
235
205
|
});
|
|
236
206
|
});
|
|
237
207
|
}
|
|
238
|
-
sendQuestion(
|
|
239
|
-
return
|
|
240
|
-
var
|
|
208
|
+
sendQuestion(a, m) {
|
|
209
|
+
return h(this, null, function* () {
|
|
210
|
+
var c;
|
|
241
211
|
const {
|
|
242
|
-
content:
|
|
212
|
+
content: i,
|
|
243
213
|
fileIds: d,
|
|
244
|
-
oldMessageId:
|
|
245
|
-
} =
|
|
214
|
+
oldMessageId: e
|
|
215
|
+
} = a, {
|
|
246
216
|
stream: g,
|
|
247
|
-
referenceFirst:
|
|
248
|
-
} =
|
|
217
|
+
referenceFirst: r
|
|
218
|
+
} = m;
|
|
249
219
|
if (this.active || (yield this.activate()), this._id) {
|
|
250
|
-
let
|
|
251
|
-
content:
|
|
220
|
+
let l = {
|
|
221
|
+
content: i,
|
|
252
222
|
fileIds: d,
|
|
253
|
-
referenceFirst:
|
|
254
|
-
oldMessageId:
|
|
255
|
-
promptVariables: this.promptVariables.reduce((
|
|
256
|
-
[
|
|
223
|
+
referenceFirst: r,
|
|
224
|
+
oldMessageId: e,
|
|
225
|
+
promptVariables: this.promptVariables.reduce((s, t) => u(f({}, s), {
|
|
226
|
+
[t.key]: t.value
|
|
257
227
|
}), {}),
|
|
258
|
-
knowledges: (
|
|
228
|
+
knowledges: (c = this.knowledges) == null ? void 0 : c.map((s) => s.id)
|
|
259
229
|
};
|
|
260
|
-
this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id,
|
|
230
|
+
this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, l, this.ctrl.signal, {
|
|
261
231
|
onopen: () => {
|
|
262
|
-
|
|
232
|
+
T.log("open", this.ctrl);
|
|
263
233
|
},
|
|
264
|
-
onmessage: (
|
|
234
|
+
onmessage: (s) => {
|
|
265
235
|
if (this._stopAnswer)
|
|
266
236
|
return;
|
|
267
|
-
let
|
|
268
|
-
|
|
237
|
+
let t = this.messages[this.messages.length - 1];
|
|
238
|
+
t && t.role === P.Robot ? (t = f(f({}, t), s), this.messages[this.messages.length - 1] = t, this.event.emit("message:content", t), this.event.emit("message:updated", t)) : (this.pushMessage(s), this.event.emit("message:content", s));
|
|
269
239
|
},
|
|
270
|
-
onclose: (
|
|
271
|
-
this._stopAnswer || (
|
|
240
|
+
onclose: (s) => h(this, null, function* () {
|
|
241
|
+
this._stopAnswer || (T.log(s, "onclose"), this.event.emit("answer:finish", s));
|
|
272
242
|
}),
|
|
273
243
|
onerror: () => {
|
|
274
244
|
this._stopAnswer || (this.event.emit("message:error", ""), this.frozen = !1);
|
|
@@ -281,21 +251,21 @@ class H extends J {
|
|
|
281
251
|
this.ctrl.abort();
|
|
282
252
|
}
|
|
283
253
|
registerMcpFromClientConfig() {
|
|
284
|
-
return
|
|
254
|
+
return h(this, null, function* () {
|
|
285
255
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
286
256
|
});
|
|
287
257
|
}
|
|
288
|
-
addMcpServers(
|
|
289
|
-
return
|
|
258
|
+
addMcpServers(a) {
|
|
259
|
+
return h(this, null, function* () {
|
|
290
260
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
291
261
|
});
|
|
292
262
|
}
|
|
293
263
|
listMcpServers() {
|
|
294
|
-
return
|
|
264
|
+
return h(this, null, function* () {
|
|
295
265
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
296
266
|
});
|
|
297
267
|
}
|
|
298
268
|
}
|
|
299
269
|
export {
|
|
300
|
-
|
|
270
|
+
j as default
|
|
301
271
|
};
|