@glodon-aiot/bot-client-sdk 3.7.0-alpha.2 → 3.8.0-alpha.4

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 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 = (m, n, a) => n in m ? T(m, n, { enumerable: !0, configurable: !0, writable: !0, value: a }) : m[n] = a, u = (m, n) => {
6
- for (var a in n || (n = {}))
7
- P.call(n, a) && R(m, a, n[a]);
8
- if (M)
9
- for (var a of M(n))
10
- k.call(n, a) && R(m, a, n[a]);
11
- return m;
12
- }, y = (m, n) => A(m, I(n));
13
- var w = (m, n, a) => (R(m, typeof n != "symbol" ? n + "" : n, a), a);
14
- var f = (m, n, a) => new Promise((l, d) => {
1
+ var I = Object.defineProperty, A = Object.defineProperties;
2
+ var P = Object.getOwnPropertyDescriptors;
3
+ var w = Object.getOwnPropertySymbols;
4
+ var b = Object.prototype.hasOwnProperty, D = Object.prototype.propertyIsEnumerable;
5
+ var R = (l, c, s) => c in l ? I(l, c, { enumerable: !0, configurable: !0, writable: !0, value: s }) : l[c] = s, u = (l, c) => {
6
+ for (var s in c || (c = {}))
7
+ b.call(c, s) && R(l, s, c[s]);
8
+ if (w)
9
+ for (var s of w(c))
10
+ D.call(c, s) && R(l, s, c[s]);
11
+ return l;
12
+ }, y = (l, c) => A(l, P(c));
13
+ var M = (l, c, s) => (R(l, typeof c != "symbol" ? c + "" : c, s), s);
14
+ var f = (l, c, s) => new Promise((a, d) => {
15
15
  var h = (r) => {
16
16
  try {
17
- i(a.next(r));
18
- } catch (o) {
19
- d(o);
17
+ i(s.next(r));
18
+ } catch (m) {
19
+ d(m);
20
20
  }
21
21
  }, e = (r) => {
22
22
  try {
23
- i(a.throw(r));
24
- } catch (o) {
25
- d(o);
23
+ i(s.throw(r));
24
+ } catch (m) {
25
+ d(m);
26
26
  }
27
- }, i = (r) => r.done ? l(r.value) : Promise.resolve(r.value).then(h, e);
28
- i((a = a.apply(m, n)).next());
27
+ }, i = (r) => r.done ? a(r.value) : Promise.resolve(r.value).then(h, e);
28
+ i((s = s.apply(l, c)).next());
29
29
  });
30
- import { IHistoryRole as b, PromptVariableType as C, ChatMessageRole as D } from "@glodon-aiot/apis";
31
- import O from "./Logger.mjs";
32
- import V from "./Session.mjs";
33
- import $ from "dayjs";
30
+ import { IHistoryRole as C, PromptVariableType as v, ChatMessageRole as O } from "@glodon-aiot/apis";
31
+ import V from "./Logger.mjs";
32
+ import $ from "./Session.mjs";
33
+ import _ from "dayjs";
34
34
  import { configuration as S } from "./Config.mjs";
35
- import { fetchEventSource as p } from "./api/fetchEventSource/fetch.mjs";
36
- const v = new O("[DialogSession] ");
37
- class B extends V {
38
- constructor(a, l = "", d, h, e, i) {
39
- super(a, l, d, h, e, i);
40
- w(this, "ctrl", new AbortController());
35
+ import { fetchEventSource as T } from "./api/fetchEventSource/fetch.mjs";
36
+ const p = new V("[DialogSession] ");
37
+ class k extends $ {
38
+ constructor(s, a = "", d, h, e, i) {
39
+ super(s, a, d, h, e, i);
40
+ M(this, "ctrl", new AbortController());
41
41
  }
42
42
  prepareConnection() {
43
- return new Promise((a) => f(this, null, function* () {
44
- this._ready = !0, this.event.emit("ready"), this.frozen = !1, a(!0);
43
+ return new Promise((s) => f(this, null, function* () {
44
+ this._ready = !0, this.event.emit("ready"), this.frozen = !1, s(!0);
45
45
  }));
46
46
  }
47
- postChatV2EventSource(a, l, d, h, e, i) {
47
+ postChatV2EventSource(s, a, d, h, e, i) {
48
48
  return f(this, null, function* () {
49
49
  const r = {
50
50
  code: 0,
51
51
  // messageId: uuidv4(),
52
52
  content: "",
53
- role: b.Robot,
54
- sessionId: l,
53
+ role: C.Robot,
54
+ sessionId: a,
55
55
  reference: [],
56
56
  searchReference: [],
57
57
  messageTime: "",
@@ -60,16 +60,16 @@ class B extends V {
60
60
  isAgree: 0
61
61
  },
62
62
  isReplying: !1
63
- }, o = y(u({}, d), {
63
+ }, m = y(u({}, d), {
64
64
  stream: S.stream
65
65
  });
66
- p(`${S.apiRoot}/chat/v2/chat?applicationId=${a}&sessionId=${l}`, {
66
+ T(`${S.apiRoot}/chat/v2/chat?applicationId=${s}&sessionId=${a}`, {
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(o),
72
+ body: JSON.stringify(m),
73
73
  openWhenHidden: !0,
74
74
  signal: h,
75
75
  onopen(g) {
@@ -77,41 +77,41 @@ class B extends V {
77
77
  },
78
78
  onmessage(g) {
79
79
  if (!g.data.includes('{"code": ')) {
80
- let s = {};
80
+ let t = {};
81
81
  try {
82
- s = JSON.parse(g.data);
83
- } catch (_) {
82
+ t = JSON.parse(g.data);
83
+ } catch (J) {
84
84
  }
85
- (s == null ? void 0 : s.mtype) === "search" ? r.searchReference.push(s) : r.content = r.content + g.data;
86
- const c = {
85
+ (t == null ? void 0 : t.mtype) === "search" ? r.searchReference.push(t) : r.content = r.content + g.data;
86
+ const o = {
87
87
  content: r.content,
88
88
  role: 1,
89
89
  reference: r.reference,
90
90
  searchReference: r.searchReference,
91
91
  messageId: r.messageId,
92
- sessionId: l,
93
- messageTime: $(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
92
+ sessionId: a,
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 (e == null ? void 0 : e.onmessage) && e.onmessage(c);
99
+ return (e == null ? void 0 : e.onmessage) && e.onmessage(o);
100
100
  }
101
- const t = JSON.parse(g.data);
102
- if (r.reference = t.data.reference, r.searchReference = t.data.searchReference, r.messageTime = t.data.messageTime, r.messageId = t.data.messageId, r.llmModel = t.data.llmModel, r.content = t.data.content, r.code = t.code, t.code === 200) {
103
- const s = {
101
+ const n = JSON.parse(g.data);
102
+ if (r.reference = n.data.reference, r.searchReference = n.data.searchReference, r.messageTime = n.data.messageTime, r.messageId = n.data.messageId, r.llmModel = n.data.llmModel, r.content = n.data.content, r.code = n.code, n.code === 200) {
103
+ const t = {
104
104
  code: r.code,
105
105
  content: r.content,
106
106
  role: 1,
107
107
  reference: r.reference,
108
108
  searchReference: r.searchReference,
109
109
  messageId: r.messageId,
110
- sessionId: l,
110
+ sessionId: a,
111
111
  messageTime: r.messageTime,
112
112
  isReplying: !0
113
113
  };
114
- e != null && e.onmessage && e.onmessage(s);
114
+ e != null && e.onmessage && e.onmessage(t);
115
115
  } else
116
116
  return e != null && e.onclose && e.onclose(r), i && i({
117
117
  code: r.code,
@@ -129,7 +129,7 @@ class B extends V {
129
129
  reference: r.reference,
130
130
  searchReference: r.searchReference,
131
131
  messageId: r.messageId,
132
- sessionId: l,
132
+ sessionId: a,
133
133
  messageTime: r.messageTime,
134
134
  comment: {
135
135
  isAgree: 0
@@ -142,14 +142,14 @@ class B extends V {
142
142
  });
143
143
  });
144
144
  }
145
- postChatV3EventSource(a, l, d, h, e, i) {
145
+ postChatV3EventSource(s, a, d, h, e, i) {
146
146
  return f(this, null, function* () {
147
- const r = this, o = {
147
+ const r = this, m = {
148
148
  code: 0,
149
149
  // messageId: uuidv4(),
150
150
  content: "",
151
- role: b.Robot,
152
- sessionId: l,
151
+ role: C.Robot,
152
+ sessionId: a,
153
153
  reference: [],
154
154
  searchReference: [],
155
155
  messageTime: "",
@@ -165,7 +165,7 @@ class B extends V {
165
165
  }, g = y(u({}, d), {
166
166
  stream: S.stream
167
167
  });
168
- p(`${S.apiRoot}/chat/v3/chat?applicationId=${a}&sessionId=${l}`, {
168
+ T(`${S.apiRoot}/chat/v3/chat?applicationId=${s}&sessionId=${a}`, {
169
169
  method: "POST",
170
170
  headers: {
171
171
  Authorization: `Bearer ${this.token}`,
@@ -174,78 +174,78 @@ class B extends V {
174
174
  body: JSON.stringify(g),
175
175
  openWhenHidden: !0,
176
176
  signal: h,
177
- onopen(t) {
178
- return t.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
177
+ onopen(n) {
178
+ return n.ok && e != null && e.onopen && e.onopen(), Promise.resolve();
179
179
  },
180
- onmessage(t) {
181
- if (!t.data && !t.event && !t.id && !t.retry)
180
+ onmessage(n) {
181
+ if (!n.data && !n.event && !n.id && !n.retry)
182
182
  return;
183
- let s = {};
183
+ let t = {};
184
184
  try {
185
- s = JSON.parse(t.data);
186
- } catch (c) {
187
- console.error("parse eventData to JSON failed. ", t.data);
185
+ t = JSON.parse(n.data);
186
+ } catch (o) {
187
+ console.error("parse eventData to JSON failed. ", n.data);
188
188
  }
189
- if (s.code !== 200)
190
- return o.index = -1, o.code = s.code, o.message = s.message, e != null && e.onmessage && e.onmessage(o), i && i({
191
- code: s.code,
192
- message: s.message
189
+ if (t.code !== 200)
190
+ return m.index = -1, m.code = t.code, m.message = t.message, e != null && e.onmessage && e.onmessage(m), i && i({
191
+ code: t.code,
192
+ message: t.message
193
193
  });
194
- r.handleStreamAnswer(s, o), e != null && e.onmessage && e.onmessage(o);
194
+ r.handleStreamAnswer(t, m), e != null && e.onmessage && e.onmessage(m);
195
195
  },
196
196
  onerror() {
197
197
  e != null && e.onerror && e.onerror();
198
198
  },
199
199
  onclose() {
200
- const t = y(u({}, o), {
200
+ const n = y(u({}, m), {
201
201
  isReplying: !1
202
202
  });
203
- e != null && e.onclose && e.onclose(t);
203
+ e != null && e.onclose && e.onclose(n);
204
204
  }
205
205
  });
206
206
  });
207
207
  }
208
- sendQuestion(a, l) {
208
+ sendQuestion(s, a) {
209
209
  return f(this, null, function* () {
210
- var g;
211
210
  const {
212
211
  content: d,
213
212
  fileIds: h,
214
213
  oldMessageId: e,
215
214
  imageContent: i
216
- } = a, {
215
+ } = s, {
217
216
  stream: r,
218
- referenceFirst: o
219
- } = l;
217
+ referenceFirst: m
218
+ } = a;
220
219
  if (this.active || (yield this.activate()), this._id) {
221
- let t = {
220
+ const g = this.convertKnowledgesToChatFormat(this.knowledges);
221
+ let n = {
222
222
  content: d,
223
223
  fileIds: h,
224
- referenceFirst: o,
224
+ referenceFirst: m,
225
225
  oldMessageId: e,
226
- promptVariables: this.promptVariables.filter((s) => s.type === C.User).reduce((s, c) => y(u({}, s), {
227
- [c.key]: c.value
226
+ promptVariables: this.promptVariables.filter((t) => t.type === v.User).reduce((t, o) => y(u({}, t), {
227
+ [o.key]: o.value
228
228
  }), {}),
229
- systemPromptVariables: this.promptVariables.filter((s) => s.type === C.System).reduce((s, c) => y(u({}, s), {
230
- [c.key]: c.value
229
+ systemPromptVariables: this.promptVariables.filter((t) => t.type === v.System).reduce((t, o) => y(u({}, t), {
230
+ [o.key]: o.value
231
231
  }), {}),
232
- knowledges: (g = this.knowledges) == null ? void 0 : g.map((s) => s.id),
232
+ knowledges: g,
233
233
  //开启内容摘要
234
234
  enableTitleSummary: !0,
235
235
  imageContent: i
236
236
  };
237
- this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, t, this.ctrl.signal, {
237
+ this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, n, this.ctrl.signal, {
238
238
  onopen: () => {
239
- v.log("open", this.ctrl);
239
+ p.log("open", this.ctrl);
240
240
  },
241
- onmessage: (s) => {
241
+ onmessage: (t) => {
242
242
  if (this._stopAnswer)
243
243
  return;
244
- let c = this.messages[this.messages.length - 1];
245
- c && c.role === D.Robot ? (c = u(u({}, c), s), this.messages[this.messages.length - 1] = c, this.event.emit("message:content", c), this.event.emit("message:updated", c)) : (this.pushMessage(s), this.event.emit("message:content", s));
244
+ let o = this.messages[this.messages.length - 1];
245
+ o && o.role === O.Robot ? (o = u(u({}, o), t), this.messages[this.messages.length - 1] = o, this.event.emit("message:content", o), this.event.emit("message:updated", o)) : (this.pushMessage(t), this.event.emit("message:content", t));
246
246
  },
247
- onclose: (s) => f(this, null, function* () {
248
- this._stopAnswer || (v.log(s, "onclose"), this.event.emit("answer:finish", s));
247
+ onclose: (t) => f(this, null, function* () {
248
+ this._stopAnswer || (p.log(t, "onclose"), this.event.emit("answer:finish", t));
249
249
  }),
250
250
  onerror: () => {
251
251
  this._stopAnswer || (this.event.emit("message:error", ""), this.frozen = !1);
@@ -262,7 +262,7 @@ class B extends V {
262
262
  throw new Error("MCP registration is not supported by DialogSession");
263
263
  });
264
264
  }
265
- addMcpServers(a) {
265
+ addMcpServers(s) {
266
266
  return f(this, null, function* () {
267
267
  throw new Error("MCP registration is not supported by DialogSession");
268
268
  });
@@ -272,7 +272,13 @@ class B extends V {
272
272
  throw new Error("MCP registration is not supported by DialogSession");
273
273
  });
274
274
  }
275
+ convertKnowledgesToChatFormat(s) {
276
+ return s ? s.map((a) => a != null && a.fileIds ? {
277
+ id: a.id,
278
+ fileIds: a.fileIds
279
+ } : a.id) : [];
280
+ }
275
281
  }
276
282
  export {
277
- B as default
283
+ k as default
278
284
  };