@glodon-aiot/bot-client-sdk 0.0.0-20251106104234

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 (39) 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 +328 -0
  6. package/dist/es/BotClient.mjs +269 -0
  7. package/dist/es/Config.mjs +37 -0
  8. package/dist/es/DialogSession.mjs +276 -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 +732 -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/package.json.mjs +110 -0
  18. package/dist/es/utils.mjs +11 -0
  19. package/dist/lib/index.js +4 -0
  20. package/dist/src/AgentPlugin.d.ts +17 -0
  21. package/dist/src/AgentSession.d.ts +13 -0
  22. package/dist/src/BotClient.d.ts +58 -0
  23. package/dist/src/Config.d.ts +48 -0
  24. package/dist/src/DialogSession.d.ts +26 -0
  25. package/dist/src/EventBus.d.ts +23 -0
  26. package/dist/src/Logger.d.ts +12 -0
  27. package/dist/src/PayloadTypes.d.ts +84 -0
  28. package/dist/src/Session.d.ts +230 -0
  29. package/dist/src/__ tests __/AgentClient.spec.d.ts +1 -0
  30. package/dist/src/__ tests __/AgentSession.spec.d.ts +1 -0
  31. package/dist/src/__ tests __/DialogClient.spec.d.ts +1 -0
  32. package/dist/src/api/fetchEventSource/fetch.d.ts +44 -0
  33. package/dist/src/api/fetchEventSource/index.d.ts +1 -0
  34. package/dist/src/api/fetchEventSource/parse.d.ts +36 -0
  35. package/dist/src/api/index.d.ts +2 -0
  36. package/dist/src/constant.d.ts +13 -0
  37. package/dist/src/index.d.ts +5 -0
  38. package/dist/src/utils.d.ts +4 -0
  39. package/package.json +90 -0
@@ -0,0 +1,276 @@
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 S = (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) && S(o, a, n[a]);
8
+ if (M)
9
+ for (var a of M(n))
10
+ k.call(n, a) && S(o, a, n[a]);
11
+ return o;
12
+ }, u = (o, n) => A(o, I(n));
13
+ var w = (o, n, a) => (S(o, typeof n != "symbol" ? n + "" : n, a), a);
14
+ var h = (o, n, a) => new Promise((m, g) => {
15
+ var d = (r) => {
16
+ try {
17
+ l(a.next(r));
18
+ } catch (c) {
19
+ g(c);
20
+ }
21
+ }, e = (r) => {
22
+ try {
23
+ l(a.throw(r));
24
+ } catch (c) {
25
+ g(c);
26
+ }
27
+ }, l = (r) => r.done ? m(r.value) : Promise.resolve(r.value).then(d, e);
28
+ l((a = a.apply(o, n)).next());
29
+ });
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
+ }
42
+ prepareConnection() {
43
+ return new Promise((a) => h(this, null, function* () {
44
+ this._ready = !0, this.event.emit("ready"), this.frozen = !1, a(!0);
45
+ }));
46
+ }
47
+ postChatV2EventSource(a, m, g, d, e, l) {
48
+ return h(this, null, function* () {
49
+ const r = {
50
+ code: 0,
51
+ // messageId: uuidv4(),
52
+ content: "",
53
+ role: b.Robot,
54
+ sessionId: m,
55
+ reference: [],
56
+ searchReference: [],
57
+ messageTime: "",
58
+ llmModel: "",
59
+ comment: {
60
+ isAgree: 0
61
+ },
62
+ isReplying: !1
63
+ }, c = u(f({}, g), {
64
+ stream: y.stream
65
+ });
66
+ v(`${y.apiRoot}/chat/v2/chat?applicationId=${a}&sessionId=${m}`, {
67
+ method: "POST",
68
+ headers: {
69
+ Authorization: `Bearer ${this.token}`,
70
+ "Content-Type": "application/json"
71
+ },
72
+ body: JSON.stringify(c),
73
+ openWhenHidden: !0,
74
+ signal: d,
75
+ onopen(i) {
76
+ return i.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
77
+ },
78
+ onmessage(i) {
79
+ if (!i.data.includes('{"code": ')) {
80
+ let t = {};
81
+ try {
82
+ t = JSON.parse(i.data);
83
+ } catch (_) {
84
+ }
85
+ (t == null ? void 0 : t.mtype) === "search" ? r.searchReference.push(t) : r.content = r.content + i.data;
86
+ const R = {
87
+ content: r.content,
88
+ role: 1,
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
+ comment: {
95
+ isAgree: 0
96
+ },
97
+ isReplying: !0
98
+ };
99
+ return (e == null ? void 0 : e.onmessage) && e.onmessage(R);
100
+ }
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
+ role: 1,
107
+ reference: r.reference,
108
+ searchReference: r.searchReference,
109
+ messageId: r.messageId,
110
+ sessionId: m,
111
+ messageTime: r.messageTime,
112
+ isReplying: !0
113
+ };
114
+ e != null && e.onmessage && e.onmessage(t);
115
+ } else
116
+ return e != null && e.onclose && e.onclose(r), l && l({
117
+ code: r.code,
118
+ message: r.message
119
+ });
120
+ },
121
+ onerror() {
122
+ e != null && e.onerror && e.onerror();
123
+ },
124
+ onclose() {
125
+ const i = {
126
+ code: r.code,
127
+ content: r.content,
128
+ role: 1,
129
+ reference: r.reference,
130
+ searchReference: r.searchReference,
131
+ messageId: r.messageId,
132
+ sessionId: m,
133
+ messageTime: r.messageTime,
134
+ comment: {
135
+ isAgree: 0
136
+ },
137
+ isReplying: !1,
138
+ index: -1
139
+ };
140
+ e != null && e.onclose && e.onclose(i);
141
+ }
142
+ });
143
+ });
144
+ }
145
+ postChatV3EventSource(a, m, g, d, e, l) {
146
+ return h(this, null, function* () {
147
+ const r = this, c = {
148
+ code: 0,
149
+ // messageId: uuidv4(),
150
+ content: "",
151
+ role: b.Robot,
152
+ sessionId: m,
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
+ }, i = u(f({}, g), {
166
+ stream: y.stream
167
+ });
168
+ v(`${y.apiRoot}/chat/v3/chat?applicationId=${a}&sessionId=${m}`, {
169
+ method: "POST",
170
+ headers: {
171
+ Authorization: `Bearer ${this.token}`,
172
+ "Content-Type": "application/json"
173
+ },
174
+ body: JSON.stringify(i),
175
+ openWhenHidden: !0,
176
+ signal: d,
177
+ onopen(s) {
178
+ return s.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
179
+ },
180
+ onmessage(s) {
181
+ if (!s.data && !s.event && !s.id && !s.retry)
182
+ return;
183
+ let t = {};
184
+ try {
185
+ t = JSON.parse(s.data);
186
+ } catch (R) {
187
+ console.error("parse eventData to JSON failed. ", s.data);
188
+ }
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
+ });
194
+ r.handleStreamAnswer(t, c), e != null && e.onmessage && e.onmessage(c);
195
+ },
196
+ onerror() {
197
+ e != null && e.onerror && e.onerror();
198
+ },
199
+ onclose() {
200
+ const s = u(f({}, c), {
201
+ isReplying: !1
202
+ });
203
+ e != null && e.onclose && e.onclose(s);
204
+ }
205
+ });
206
+ });
207
+ }
208
+ sendQuestion(a, m) {
209
+ return h(this, null, function* () {
210
+ var c;
211
+ const {
212
+ content: g,
213
+ fileIds: d,
214
+ oldMessageId: e
215
+ } = a, {
216
+ stream: l,
217
+ referenceFirst: r
218
+ } = m;
219
+ if (this.active || (yield this.activate()), this._id) {
220
+ let i = {
221
+ content: g,
222
+ fileIds: d,
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
230
+ }), {}),
231
+ knowledges: (c = this.knowledges) == null ? void 0 : c.map((s) => s.id),
232
+ //开启内容摘要
233
+ enableTitleSummary: !0
234
+ };
235
+ this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, i, this.ctrl.signal, {
236
+ onopen: () => {
237
+ C.log("open", this.ctrl);
238
+ },
239
+ onmessage: (s) => {
240
+ if (this._stopAnswer)
241
+ return;
242
+ let t = this.messages[this.messages.length - 1];
243
+ 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));
244
+ },
245
+ onclose: (s) => h(this, null, function* () {
246
+ this._stopAnswer || (C.log(s, "onclose"), this.event.emit("answer:finish", s));
247
+ }),
248
+ onerror: () => {
249
+ this._stopAnswer || (this.event.emit("message:error", ""), this.frozen = !1);
250
+ }
251
+ });
252
+ }
253
+ });
254
+ }
255
+ stopAnswer() {
256
+ this.ctrl.abort();
257
+ }
258
+ registerMcpFromClientConfig() {
259
+ return h(this, null, function* () {
260
+ throw new Error("MCP registration is not supported by DialogSession");
261
+ });
262
+ }
263
+ addMcpServers(a) {
264
+ return h(this, null, function* () {
265
+ throw new Error("MCP registration is not supported by DialogSession");
266
+ });
267
+ }
268
+ listMcpServers() {
269
+ return h(this, null, function* () {
270
+ throw new Error("MCP registration is not supported by DialogSession");
271
+ });
272
+ }
273
+ }
274
+ export {
275
+ B as default
276
+ };
@@ -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
+ };