@glodon-aiot/bot-client-sdk 3.3.3 → 3.3.4-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 +172 -131
- package/dist/es/BotClient.mjs +83 -77
- package/dist/es/Config.mjs +7 -6
- package/dist/es/DialogSession.mjs +142 -169
- package/dist/es/Session.mjs +252 -211
- package/dist/es/package.json.mjs +11 -11
- package/dist/lib/index.js +4 -4
- package/dist/src/Config.d.ts +2 -0
- package/dist/src/Session.d.ts +7 -1
- package/package.json +2 -2
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
var P = Object.prototype.hasOwnProperty,
|
|
5
|
-
var
|
|
6
|
-
for (var
|
|
7
|
-
P.call(
|
|
8
|
-
if (
|
|
9
|
-
for (var
|
|
10
|
-
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
var
|
|
14
|
-
var
|
|
15
|
-
var d = (
|
|
1
|
+
var T = Object.defineProperty, A = Object.defineProperties;
|
|
2
|
+
var I = Object.getOwnPropertyDescriptors;
|
|
3
|
+
var M = Object.getOwnPropertySymbols;
|
|
4
|
+
var P = Object.prototype.hasOwnProperty, k = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var R = (o, n, a) => n in o ? T(o, n, { enumerable: !0, configurable: !0, writable: !0, value: a }) : o[n] = a, f = (o, n) => {
|
|
6
|
+
for (var a in n || (n = {}))
|
|
7
|
+
P.call(n, a) && R(o, a, n[a]);
|
|
8
|
+
if (M)
|
|
9
|
+
for (var a of M(n))
|
|
10
|
+
k.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, g) => {
|
|
15
|
+
var d = (r) => {
|
|
16
16
|
try {
|
|
17
|
-
|
|
18
|
-
} catch (
|
|
19
|
-
|
|
17
|
+
l(a.next(r));
|
|
18
|
+
} catch (c) {
|
|
19
|
+
g(c);
|
|
20
20
|
}
|
|
21
|
-
},
|
|
21
|
+
}, e = (r) => {
|
|
22
22
|
try {
|
|
23
|
-
|
|
24
|
-
} catch (
|
|
25
|
-
|
|
23
|
+
l(a.throw(r));
|
|
24
|
+
} catch (c) {
|
|
25
|
+
g(c);
|
|
26
26
|
}
|
|
27
|
-
},
|
|
28
|
-
|
|
27
|
+
}, l = (r) => r.done ? m(r.value) : Promise.resolve(r.value).then(d, e);
|
|
28
|
+
l((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 b, PromptVariableType as p, ChatMessageRole as D } from "@glodon-aiot/apis";
|
|
31
|
+
import O from "./Logger.mjs";
|
|
32
|
+
import V 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 C = new O("[DialogSession] ");
|
|
37
|
+
class B extends V {
|
|
38
|
+
constructor(a, m = "", g, d, e, l) {
|
|
39
|
+
super(a, m, g, d, e, l);
|
|
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, g, d, e, l) {
|
|
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: b.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({}, g), {
|
|
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(i) {
|
|
76
|
+
return i.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
|
|
77
77
|
},
|
|
78
|
-
onmessage(
|
|
79
|
-
if (!
|
|
80
|
-
let
|
|
78
|
+
onmessage(i) {
|
|
79
|
+
if (!i.data.includes('{"code": ')) {
|
|
80
|
+
let t = {};
|
|
81
81
|
try {
|
|
82
|
-
|
|
83
|
-
} catch (
|
|
82
|
+
t = JSON.parse(i.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 + i.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(i.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), l && l({
|
|
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 i = {
|
|
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(i);
|
|
141
141
|
}
|
|
142
142
|
});
|
|
143
143
|
});
|
|
144
144
|
}
|
|
145
|
-
postChatV3EventSource(
|
|
146
|
-
return
|
|
147
|
-
const
|
|
145
|
+
postChatV3EventSource(a, m, g, d, e, l) {
|
|
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: b.Robot,
|
|
152
|
+
sessionId: m,
|
|
153
153
|
reference: [],
|
|
154
154
|
searchReference: [],
|
|
155
155
|
messageTime: "",
|
|
@@ -162,113 +162,86 @@ class H extends J {
|
|
|
162
162
|
thinkingSecs: "",
|
|
163
163
|
thinkingStatus: "",
|
|
164
164
|
searchingStatus: ""
|
|
165
|
-
},
|
|
166
|
-
stream:
|
|
165
|
+
}, i = u(f({}, g), {
|
|
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}`,
|
|
172
172
|
"Content-Type": "application/json"
|
|
173
173
|
},
|
|
174
|
-
body: JSON.stringify(
|
|
174
|
+
body: JSON.stringify(i),
|
|
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), l && l({
|
|
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: g,
|
|
243
213
|
fileIds: d,
|
|
244
|
-
oldMessageId:
|
|
245
|
-
} =
|
|
246
|
-
stream:
|
|
247
|
-
referenceFirst:
|
|
248
|
-
} =
|
|
214
|
+
oldMessageId: e
|
|
215
|
+
} = a, {
|
|
216
|
+
stream: l,
|
|
217
|
+
referenceFirst: r
|
|
218
|
+
} = m;
|
|
249
219
|
if (this.active || (yield this.activate()), this._id) {
|
|
250
|
-
let
|
|
251
|
-
content:
|
|
220
|
+
let i = {
|
|
221
|
+
content: g,
|
|
252
222
|
fileIds: d,
|
|
253
|
-
referenceFirst:
|
|
254
|
-
oldMessageId:
|
|
255
|
-
promptVariables: this.promptVariables.reduce((
|
|
256
|
-
[
|
|
223
|
+
referenceFirst: r,
|
|
224
|
+
oldMessageId: e,
|
|
225
|
+
promptVariables: this.promptVariables.filter((s) => s.type === p.User).reduce((s, t) => u(f({}, s), {
|
|
226
|
+
[t.key]: t.value
|
|
227
|
+
}), {}),
|
|
228
|
+
systemPromptVariables: this.promptVariables.filter((s) => s.type === p.System).reduce((s, t) => u(f({}, s), {
|
|
229
|
+
[t.key]: t.value
|
|
257
230
|
}), {}),
|
|
258
|
-
knowledges: (
|
|
231
|
+
knowledges: (c = this.knowledges) == null ? void 0 : c.map((s) => s.id)
|
|
259
232
|
};
|
|
260
|
-
this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id,
|
|
233
|
+
this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, i, this.ctrl.signal, {
|
|
261
234
|
onopen: () => {
|
|
262
|
-
|
|
235
|
+
C.log("open", this.ctrl);
|
|
263
236
|
},
|
|
264
|
-
onmessage: (
|
|
237
|
+
onmessage: (s) => {
|
|
265
238
|
if (this._stopAnswer)
|
|
266
239
|
return;
|
|
267
|
-
let
|
|
268
|
-
|
|
240
|
+
let t = this.messages[this.messages.length - 1];
|
|
241
|
+
t && t.role === D.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
242
|
},
|
|
270
|
-
onclose: (
|
|
271
|
-
this._stopAnswer || (
|
|
243
|
+
onclose: (s) => h(this, null, function* () {
|
|
244
|
+
this._stopAnswer || (C.log(s, "onclose"), this.event.emit("answer:finish", s));
|
|
272
245
|
}),
|
|
273
246
|
onerror: () => {
|
|
274
247
|
this._stopAnswer || (this.event.emit("message:error", ""), this.frozen = !1);
|
|
@@ -281,21 +254,21 @@ class H extends J {
|
|
|
281
254
|
this.ctrl.abort();
|
|
282
255
|
}
|
|
283
256
|
registerMcpFromClientConfig() {
|
|
284
|
-
return
|
|
257
|
+
return h(this, null, function* () {
|
|
285
258
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
286
259
|
});
|
|
287
260
|
}
|
|
288
|
-
addMcpServers(
|
|
289
|
-
return
|
|
261
|
+
addMcpServers(a) {
|
|
262
|
+
return h(this, null, function* () {
|
|
290
263
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
291
264
|
});
|
|
292
265
|
}
|
|
293
266
|
listMcpServers() {
|
|
294
|
-
return
|
|
267
|
+
return h(this, null, function* () {
|
|
295
268
|
throw new Error("MCP registration is not supported by DialogSession");
|
|
296
269
|
});
|
|
297
270
|
}
|
|
298
271
|
}
|
|
299
272
|
export {
|
|
300
|
-
|
|
273
|
+
B as default
|
|
301
274
|
};
|