@glodon-aiot/bot-client-sdk 3.3.0-beta.13

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.
Files changed (37) hide show
  1. package/README.md +157 -0
  2. package/dist/agent-cli-sdk.js +33141 -0
  3. package/dist/agent-cli-sdk.umd.cjs +140 -0
  4. package/dist/es/AgentPlugin.mjs +20 -0
  5. package/dist/es/AgentSession.mjs +285 -0
  6. package/dist/es/BotClient.mjs +250 -0
  7. package/dist/es/Config.mjs +36 -0
  8. package/dist/es/DialogSession.mjs +301 -0
  9. package/dist/es/EventBus.mjs +64 -0
  10. package/dist/es/Logger.mjs +31 -0
  11. package/dist/es/PayloadTypes.mjs +4 -0
  12. package/dist/es/Session.mjs +683 -0
  13. package/dist/es/api/fetchEventSource/fetch.mjs +112 -0
  14. package/dist/es/api/fetchEventSource/parse.mjs +97 -0
  15. package/dist/es/api/index.mjs +41 -0
  16. package/dist/es/index.mjs +11 -0
  17. package/dist/es/utils.mjs +11 -0
  18. package/dist/lib/index.js +4 -0
  19. package/dist/src/AgentPlugin.d.ts +17 -0
  20. package/dist/src/AgentSession.d.ts +13 -0
  21. package/dist/src/BotClient.d.ts +48 -0
  22. package/dist/src/Config.d.ts +46 -0
  23. package/dist/src/DialogSession.d.ts +26 -0
  24. package/dist/src/EventBus.d.ts +23 -0
  25. package/dist/src/Logger.d.ts +12 -0
  26. package/dist/src/PayloadTypes.d.ts +84 -0
  27. package/dist/src/Session.d.ts +222 -0
  28. package/dist/src/__ tests __/AgentClient.spec.d.ts +1 -0
  29. package/dist/src/__ tests __/AgentSession.spec.d.ts +1 -0
  30. package/dist/src/__ tests __/DialogClient.spec.d.ts +1 -0
  31. package/dist/src/api/fetchEventSource/fetch.d.ts +44 -0
  32. package/dist/src/api/fetchEventSource/index.d.ts +1 -0
  33. package/dist/src/api/fetchEventSource/parse.d.ts +36 -0
  34. package/dist/src/api/index.d.ts +2 -0
  35. package/dist/src/index.d.ts +5 -0
  36. package/dist/src/utils.d.ts +4 -0
  37. package/package.json +90 -0
@@ -0,0 +1,301 @@
1
+ var A = Object.defineProperty, O = Object.defineProperties;
2
+ var D = Object.getOwnPropertyDescriptors;
3
+ var C = Object.getOwnPropertySymbols;
4
+ var P = Object.prototype.hasOwnProperty, $ = Object.prototype.propertyIsEnumerable;
5
+ var w = (i, c, t) => c in i ? A(i, c, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[c] = t, u = (i, c) => {
6
+ for (var t in c || (c = {}))
7
+ P.call(c, t) && w(i, t, c[t]);
8
+ if (C)
9
+ for (var t of C(c))
10
+ $.call(c, t) && w(i, t, c[t]);
11
+ return i;
12
+ }, R = (i, c) => O(i, D(c));
13
+ var v = (i, c, t) => (w(i, typeof c != "symbol" ? c + "" : c, t), t);
14
+ var f = (i, c, t) => new Promise((o, m) => {
15
+ var d = (e) => {
16
+ try {
17
+ g(t.next(e));
18
+ } catch (l) {
19
+ m(l);
20
+ }
21
+ }, s = (e) => {
22
+ try {
23
+ g(t.throw(e));
24
+ } catch (l) {
25
+ m(l);
26
+ }
27
+ }, g = (e) => e.done ? o(e.value) : Promise.resolve(e.value).then(d, s);
28
+ g((t = t.apply(i, c)).next());
29
+ });
30
+ import { IHistoryRole as k, ChatMessageRole as _ } from "@glodon-aiot/apis";
31
+ import x from "./Logger.mjs";
32
+ import J from "./Session.mjs";
33
+ import N from "dayjs";
34
+ import { configuration as T } from "./Config.mjs";
35
+ import { fetchEventSource as p } from "./api/fetchEventSource/fetch.mjs";
36
+ const I = new x("[DialogSession] ");
37
+ class H extends J {
38
+ constructor(t, o = "", m, d, s, g) {
39
+ super(t, o, m, d, s, g);
40
+ v(this, "ctrl", new AbortController());
41
+ }
42
+ prepareConnection() {
43
+ return new Promise((t) => f(this, null, function* () {
44
+ this._ready = !0, this.event.emit("ready"), this.frozen = !1, t(!0);
45
+ }));
46
+ }
47
+ postChatV2EventSource(t, o, m, d, s, g) {
48
+ return f(this, null, function* () {
49
+ const e = {
50
+ code: 0,
51
+ // messageId: uuidv4(),
52
+ content: "",
53
+ role: k.Robot,
54
+ sessionId: o,
55
+ reference: [],
56
+ searchReference: [],
57
+ messageTime: "",
58
+ llmModel: "",
59
+ comment: {
60
+ isAgree: 0
61
+ },
62
+ isReplying: !1
63
+ }, l = R(u({}, m), {
64
+ stream: !0
65
+ });
66
+ p(`${T.apiRoot}/chat/v2/chat?applicationId=${t}&sessionId=${o}`, {
67
+ method: "POST",
68
+ headers: {
69
+ Authorization: `Bearer ${this.token}`,
70
+ "Content-Type": "application/json"
71
+ },
72
+ body: JSON.stringify(l),
73
+ openWhenHidden: !0,
74
+ signal: d,
75
+ onopen(n) {
76
+ return n.ok && s != null && s.onopen && s.onopen(), Promise.resolve();
77
+ },
78
+ onmessage(n) {
79
+ if (!n.data.includes('{"code": ')) {
80
+ let a = {};
81
+ try {
82
+ a = JSON.parse(n.data);
83
+ } catch (M) {
84
+ }
85
+ (a == null ? void 0 : a.mtype) === "search" ? e.searchReference.push(a) : e.content = e.content + n.data;
86
+ const h = {
87
+ content: e.content,
88
+ role: 1,
89
+ reference: e.reference,
90
+ searchReference: e.searchReference,
91
+ messageId: e.messageId,
92
+ sessionId: o,
93
+ messageTime: N(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
94
+ comment: {
95
+ isAgree: 0
96
+ },
97
+ isReplying: !0
98
+ };
99
+ return (s == null ? void 0 : s.onmessage) && s.onmessage(h);
100
+ }
101
+ const r = JSON.parse(n.data);
102
+ if (e.reference = r.data.reference, e.searchReference = r.data.searchReference, e.messageTime = r.data.messageTime, e.messageId = r.data.messageId, e.llmModel = r.data.llmModel, e.content = r.data.content, e.code = r.code, r.code === 200) {
103
+ const a = {
104
+ code: e.code,
105
+ content: e.content,
106
+ role: 1,
107
+ reference: e.reference,
108
+ searchReference: e.searchReference,
109
+ messageId: e.messageId,
110
+ sessionId: o,
111
+ messageTime: e.messageTime,
112
+ isReplying: !0
113
+ };
114
+ s != null && s.onmessage && s.onmessage(a);
115
+ } else
116
+ return s != null && s.onclose && s.onclose(e), g && g({
117
+ code: e.code,
118
+ message: e.message
119
+ });
120
+ },
121
+ onerror() {
122
+ s != null && s.onerror && s.onerror();
123
+ },
124
+ onclose() {
125
+ const n = {
126
+ code: e.code,
127
+ content: e.content,
128
+ role: 1,
129
+ reference: e.reference,
130
+ searchReference: e.searchReference,
131
+ messageId: e.messageId,
132
+ sessionId: o,
133
+ messageTime: e.messageTime,
134
+ comment: {
135
+ isAgree: 0
136
+ },
137
+ isReplying: !1,
138
+ index: -1
139
+ };
140
+ s != null && s.onclose && s.onclose(n);
141
+ }
142
+ });
143
+ });
144
+ }
145
+ postChatV3EventSource(t, o, m, d, s, g) {
146
+ return f(this, null, function* () {
147
+ const e = {
148
+ code: 0,
149
+ // messageId: uuidv4(),
150
+ content: "",
151
+ role: k.Robot,
152
+ sessionId: o,
153
+ reference: [],
154
+ searchReference: [],
155
+ messageTime: "",
156
+ llmModel: "",
157
+ comment: {
158
+ isAgree: 0
159
+ },
160
+ isReplying: !1,
161
+ reasoningContent: "",
162
+ thinkingSecs: "",
163
+ thinkingStatus: "",
164
+ searchingStatus: ""
165
+ }, l = R(u({}, m), {
166
+ stream: !0
167
+ });
168
+ p(`${T.apiRoot}/chat/v3/chat?applicationId=${t}&sessionId=${o}`, {
169
+ method: "POST",
170
+ headers: {
171
+ Authorization: `Bearer ${this.token}`,
172
+ "Content-Type": "application/json"
173
+ },
174
+ body: JSON.stringify(l),
175
+ openWhenHidden: !0,
176
+ signal: d,
177
+ onopen(n) {
178
+ return n.ok && s != null && s.onopen && s.onopen(), Promise.resolve();
179
+ },
180
+ onmessage(n) {
181
+ if (!n.data && !n.event && !n.id && !n.retry)
182
+ return;
183
+ let r = {};
184
+ try {
185
+ r = JSON.parse(n.data);
186
+ } catch (y) {
187
+ console.error("parse eventData to JSON failed. ", n.data);
188
+ }
189
+ if (r.code !== 200)
190
+ return e.index = -1, e.code = r.code, e.message = r.message, s != null && s.onmessage && s.onmessage(e), g && g({
191
+ code: r.code,
192
+ message: r.message
193
+ });
194
+ const a = r.data.type, h = r.data.payload;
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);
225
+ },
226
+ onerror() {
227
+ s != null && s.onerror && s.onerror();
228
+ },
229
+ onclose() {
230
+ const n = R(u({}, e), {
231
+ isReplying: !1
232
+ });
233
+ s != null && s.onclose && s.onclose(n);
234
+ }
235
+ });
236
+ });
237
+ }
238
+ sendQuestion(t, o) {
239
+ return f(this, null, function* () {
240
+ var l;
241
+ const {
242
+ content: m,
243
+ fileIds: d,
244
+ oldMessageId: s
245
+ } = t, {
246
+ stream: g,
247
+ referenceFirst: e
248
+ } = o;
249
+ if (this.active || (yield this.activate()), this._id) {
250
+ let n = {
251
+ content: m,
252
+ fileIds: d,
253
+ referenceFirst: e,
254
+ oldMessageId: s,
255
+ promptVariables: this.promptVariables.reduce((r, a) => R(u({}, r), {
256
+ [a.key]: a.value
257
+ }), {}),
258
+ knowledges: (l = this.knowledges) == null ? void 0 : l.map((r) => r.id)
259
+ };
260
+ this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, n, this.ctrl.signal, {
261
+ onopen: () => {
262
+ I.log("open", this.ctrl);
263
+ },
264
+ onmessage: (r) => {
265
+ if (this._stopAnswer)
266
+ return;
267
+ let a = this.messages[this.messages.length - 1];
268
+ a && a.role === _.Robot ? (a = u(u({}, a), r), this.messages[this.messages.length - 1] = a, this.event.emit("message:content", a), this.event.emit("message:updated", a)) : (this.pushMessage(r), this.event.emit("message:content", r));
269
+ },
270
+ onclose: (r) => f(this, null, function* () {
271
+ this._stopAnswer || (I.log(r, "onclose"), this.event.emit("answer:finish", r));
272
+ }),
273
+ onerror: () => {
274
+ this._stopAnswer || (this.event.emit("message:error", ""), this.frozen = !1);
275
+ }
276
+ });
277
+ }
278
+ });
279
+ }
280
+ stopAnswer() {
281
+ this.ctrl.abort();
282
+ }
283
+ registerMcpFromClientConfig() {
284
+ return f(this, null, function* () {
285
+ throw new Error("MCP registration is not supported by DialogSession");
286
+ });
287
+ }
288
+ addMcpServers(t) {
289
+ return f(this, null, function* () {
290
+ throw new Error("MCP registration is not supported by DialogSession");
291
+ });
292
+ }
293
+ listMcpServers() {
294
+ return f(this, null, function* () {
295
+ throw new Error("MCP registration is not supported by DialogSession");
296
+ });
297
+ }
298
+ }
299
+ export {
300
+ H as default
301
+ };
@@ -0,0 +1,64 @@
1
+ var u = Object.defineProperty;
2
+ var v = Object.getOwnPropertySymbols;
3
+ var o = Object.prototype.hasOwnProperty, L = Object.prototype.propertyIsEnumerable;
4
+ var l = (s, e, n) => e in s ? u(s, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : s[e] = n, a = (s, e) => {
5
+ for (var n in e || (e = {}))
6
+ o.call(e, n) && l(s, n, e[n]);
7
+ if (v)
8
+ for (var n of v(e))
9
+ L.call(e, n) && l(s, n, e[n]);
10
+ return s;
11
+ };
12
+ var h = (s, e, n) => (l(s, typeof e != "symbol" ? e + "" : e, n), n);
13
+ import M from "./Logger.mjs";
14
+ const i = new M("[EventBus] ");
15
+ class $ {
16
+ constructor(e = !1) {
17
+ h(this, "eventListenerMap", {});
18
+ e && i.disable();
19
+ }
20
+ getEventListenerMap() {
21
+ return a({}, this.eventListenerMap);
22
+ }
23
+ on(e, n, r = !1) {
24
+ this.eventListenerMap[e] || (this.eventListenerMap[e] = []), r ? this.eventListenerMap[e].push({
25
+ fn: n,
26
+ once: !0
27
+ }) : this.eventListenerMap[e].push(n), i.log(`add event ${e} listener ${r ? ", call once" : ""}`);
28
+ }
29
+ once(e, n) {
30
+ this.on(e, n, !0);
31
+ }
32
+ off(e, n) {
33
+ if (this.eventListenerMap[e]) {
34
+ const r = this.eventListenerMap[e].findIndex((t) => {
35
+ if (typeof t == "function" && n === t)
36
+ return !0;
37
+ if (t = t, t.fn === n)
38
+ return !0;
39
+ });
40
+ r < 0 ? i.log(`event ${e} listener ${n.name} is not found`) : (this.eventListenerMap[e].splice(r, 1), i.log(`remove event ${e} listener`));
41
+ } else
42
+ i.log(`event ${e} listeners is empty`);
43
+ }
44
+ emit(e, n) {
45
+ if (!this.eventListenerMap[e]) {
46
+ i.log(`event ${e} listeners is empty`);
47
+ return;
48
+ }
49
+ const r = [];
50
+ for (let t = 0; t < this.eventListenerMap[e].length; t++) {
51
+ const f = this.eventListenerMap[e][t];
52
+ typeof f == "function" ? f(n) : r.push(f);
53
+ }
54
+ r.forEach((t) => {
55
+ t && (t.fn(n), this.off(e, t.fn));
56
+ }), i.log(`event ${e} is fired, has ${this.eventListenerMap[e].length} listeners`);
57
+ }
58
+ removeAllListeners() {
59
+ this.eventListenerMap = {};
60
+ }
61
+ }
62
+ export {
63
+ $ as default
64
+ };
@@ -0,0 +1,31 @@
1
+ var n = Object.defineProperty;
2
+ var f = (s, e, i) => e in s ? n(s, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : s[e] = i;
3
+ var r = (s, e, i) => (f(s, typeof e != "symbol" ? e + "" : e, i), i);
4
+ const o = class o {
5
+ constructor(e, i = !1) {
6
+ this.prefix = e, this.open = i;
7
+ }
8
+ disable() {
9
+ this.open = !1;
10
+ }
11
+ enable() {
12
+ this.open = !0;
13
+ }
14
+ log(...e) {
15
+ !this.open && !o.debug || console.log(this.prefix, ...e);
16
+ }
17
+ info(...e) {
18
+ !this.open && !o.debug || console.info(`%c ${this.prefix} %o`, "color:#0f0;", ...e);
19
+ }
20
+ error(...e) {
21
+ !this.open && !o.debug || console.error(this.prefix, ...e);
22
+ }
23
+ warn(...e) {
24
+ !this.open && !o.debug || console.warn(this.prefix, ...e);
25
+ }
26
+ };
27
+ r(o, "debug", !1);
28
+ let t = o;
29
+ export {
30
+ t as default
31
+ };
@@ -0,0 +1,4 @@
1
+ var r = /* @__PURE__ */ ((a) => (a[a.Normal = 0] = "Normal", a[a.Debug = 1] = "Debug", a))(r || {});
2
+ export {
3
+ r as UserMode
4
+ };