@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.
@@ -1,57 +1,57 @@
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) => {
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
- g(t.next(e));
18
- } catch (l) {
19
- m(l);
17
+ l(a.next(r));
18
+ } catch (c) {
19
+ g(c);
20
20
  }
21
- }, s = (e) => {
21
+ }, e = (r) => {
22
22
  try {
23
- g(t.throw(e));
24
- } catch (l) {
25
- m(l);
23
+ l(a.throw(r));
24
+ } catch (c) {
25
+ g(c);
26
26
  }
27
- }, g = (e) => e.done ? o(e.value) : Promise.resolve(e.value).then(d, s);
28
- g((t = t.apply(i, c)).next());
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 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());
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((t) => f(this, null, function* () {
44
- this._ready = !0, this.event.emit("ready"), this.frozen = !1, t(!0);
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(t, o, m, d, s, g) {
48
- return f(this, null, function* () {
49
- const e = {
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: k.Robot,
54
- sessionId: o,
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
- }, l = R(u({}, m), {
64
- stream: !0
63
+ }, c = u(f({}, g), {
64
+ stream: y.stream
65
65
  });
66
- p(`${T.apiRoot}/chat/v2/chat?applicationId=${t}&sessionId=${o}`, {
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(l),
72
+ body: JSON.stringify(c),
73
73
  openWhenHidden: !0,
74
74
  signal: d,
75
- onopen(n) {
76
- return n.ok && s != null && s.onopen && s.onopen(), Promise.resolve();
75
+ onopen(i) {
76
+ return i.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
77
77
  },
78
- onmessage(n) {
79
- if (!n.data.includes('{"code": ')) {
80
- let a = {};
78
+ onmessage(i) {
79
+ if (!i.data.includes('{"code": ')) {
80
+ let t = {};
81
81
  try {
82
- a = JSON.parse(n.data);
83
- } catch (M) {
82
+ t = JSON.parse(i.data);
83
+ } catch (_) {
84
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,
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: 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"),
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 (s == null ? void 0 : s.onmessage) && s.onmessage(h);
99
+ return (e == null ? void 0 : e.onmessage) && e.onmessage(S);
100
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,
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: e.reference,
108
- searchReference: e.searchReference,
109
- messageId: e.messageId,
110
- sessionId: o,
111
- messageTime: e.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
- s != null && s.onmessage && s.onmessage(a);
114
+ e != null && e.onmessage && e.onmessage(t);
115
115
  } else
116
- return s != null && s.onclose && s.onclose(e), g && g({
117
- code: e.code,
118
- message: e.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
- s != null && s.onerror && s.onerror();
122
+ e != null && e.onerror && e.onerror();
123
123
  },
124
124
  onclose() {
125
- const n = {
126
- code: e.code,
127
- content: e.content,
125
+ const i = {
126
+ code: r.code,
127
+ content: r.content,
128
128
  role: 1,
129
- reference: e.reference,
130
- searchReference: e.searchReference,
131
- messageId: e.messageId,
132
- sessionId: o,
133
- messageTime: e.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
- s != null && s.onclose && s.onclose(n);
140
+ e != null && e.onclose && e.onclose(i);
141
141
  }
142
142
  });
143
143
  });
144
144
  }
145
- postChatV3EventSource(t, o, m, d, s, g) {
146
- return f(this, null, function* () {
147
- const e = {
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: k.Robot,
152
- sessionId: o,
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
- }, l = R(u({}, m), {
166
- stream: !0
165
+ }, i = u(f({}, g), {
166
+ stream: y.stream
167
167
  });
168
- p(`${T.apiRoot}/chat/v3/chat?applicationId=${t}&sessionId=${o}`, {
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(l),
174
+ body: JSON.stringify(i),
175
175
  openWhenHidden: !0,
176
176
  signal: d,
177
- onopen(n) {
178
- return n.ok && s != null && s.onopen && s.onopen(), Promise.resolve();
177
+ onopen(s) {
178
+ return s.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
179
179
  },
180
- onmessage(n) {
181
- if (!n.data && !n.event && !n.id && !n.retry)
180
+ onmessage(s) {
181
+ if (!s.data && !s.event && !s.id && !s.retry)
182
182
  return;
183
- let r = {};
183
+ let t = {};
184
184
  try {
185
- r = JSON.parse(n.data);
186
- } catch (y) {
187
- console.error("parse eventData to JSON failed. ", n.data);
185
+ t = JSON.parse(s.data);
186
+ } catch (S) {
187
+ console.error("parse eventData to JSON failed. ", s.data);
188
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
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
- 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);
194
+ r.handleStreamAnswer(t, c), e != null && e.onmessage && e.onmessage(c);
225
195
  },
226
196
  onerror() {
227
- s != null && s.onerror && s.onerror();
197
+ e != null && e.onerror && e.onerror();
228
198
  },
229
199
  onclose() {
230
- const n = R(u({}, e), {
200
+ const s = u(f({}, c), {
231
201
  isReplying: !1
232
202
  });
233
- s != null && s.onclose && s.onclose(n);
203
+ e != null && e.onclose && e.onclose(s);
234
204
  }
235
205
  });
236
206
  });
237
207
  }
238
- sendQuestion(t, o) {
239
- return f(this, null, function* () {
240
- var l;
208
+ sendQuestion(a, m) {
209
+ return h(this, null, function* () {
210
+ var c;
241
211
  const {
242
- content: m,
212
+ content: g,
243
213
  fileIds: d,
244
- oldMessageId: s
245
- } = t, {
246
- stream: g,
247
- referenceFirst: e
248
- } = o;
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 n = {
251
- content: m,
220
+ let i = {
221
+ content: g,
252
222
  fileIds: d,
253
- referenceFirst: e,
254
- oldMessageId: s,
255
- promptVariables: this.promptVariables.reduce((r, a) => R(u({}, r), {
256
- [a.key]: a.value
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: (l = this.knowledges) == null ? void 0 : l.map((r) => r.id)
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, n, this.ctrl.signal, {
233
+ this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, i, this.ctrl.signal, {
261
234
  onopen: () => {
262
- I.log("open", this.ctrl);
235
+ C.log("open", this.ctrl);
263
236
  },
264
- onmessage: (r) => {
237
+ onmessage: (s) => {
265
238
  if (this._stopAnswer)
266
239
  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));
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: (r) => f(this, null, function* () {
271
- this._stopAnswer || (I.log(r, "onclose"), this.event.emit("answer:finish", r));
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 f(this, null, function* () {
257
+ return h(this, null, function* () {
285
258
  throw new Error("MCP registration is not supported by DialogSession");
286
259
  });
287
260
  }
288
- addMcpServers(t) {
289
- return f(this, null, function* () {
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 f(this, null, function* () {
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
- H as default
273
+ B as default
301
274
  };