@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,20 @@
1
+ var c = Object.defineProperty;
2
+ var a = (e, t, i) => t in e ? c(e, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : e[t] = i;
3
+ var s = (e, t, i) => (a(e, typeof t != "symbol" ? t + "" : t, i), i);
4
+ class d {
5
+ constructor(t, i = {}, o) {
6
+ s(this, "code");
7
+ s(this, "description", "");
8
+ s(this, "loaded", !1);
9
+ s(this, "functions", {});
10
+ this.code = t, i && (this.functions = i);
11
+ }
12
+ fetchData() {
13
+ this.description = "描述", this.loaded = !0;
14
+ }
15
+ checkFunctions() {
16
+ }
17
+ }
18
+ export {
19
+ d as default
20
+ };
@@ -0,0 +1,328 @@
1
+ var A = Object.defineProperty, C = Object.defineProperties;
2
+ var P = Object.getOwnPropertyDescriptors;
3
+ var b = Object.getOwnPropertySymbols;
4
+ var x = Object.prototype.hasOwnProperty, E = Object.prototype.propertyIsEnumerable;
5
+ var S = (d, i, r) => i in d ? A(d, i, { enumerable: !0, configurable: !0, writable: !0, value: r }) : d[i] = r, m = (d, i) => {
6
+ for (var r in i || (i = {}))
7
+ x.call(i, r) && S(d, r, i[r]);
8
+ if (b)
9
+ for (var r of b(i))
10
+ E.call(i, r) && S(d, r, i[r]);
11
+ return d;
12
+ }, w = (d, i) => C(d, P(i));
13
+ var v = (d, i, r) => new Promise((o, n) => {
14
+ var s = (a) => {
15
+ try {
16
+ t(r.next(a));
17
+ } catch (h) {
18
+ n(h);
19
+ }
20
+ }, e = (a) => {
21
+ try {
22
+ t(r.throw(a));
23
+ } catch (h) {
24
+ n(h);
25
+ }
26
+ }, t = (a) => a.done ? o(a.value) : Promise.resolve(a.value).then(s, e);
27
+ t((r = r.apply(d, i)).next());
28
+ });
29
+ import { ChatMessageRole as y, PromptVariableType as _ } from "@glodon-aiot/apis";
30
+ import L from "./Logger.mjs";
31
+ import { configuration as u } from "./Config.mjs";
32
+ import { io as T } from "socket.io-client";
33
+ import { UserMode as k } from "./PayloadTypes.mjs";
34
+ import q, { SessionErrors as I } from "./Session.mjs";
35
+ import N from "dayjs";
36
+ const p = new L("[AgentSession] ");
37
+ class H extends q {
38
+ constructor(i, r = "", o, n, s, e) {
39
+ super(i, r, o, n, s, e);
40
+ }
41
+ prepareConnection() {
42
+ return new Promise((i) => v(this, null, function* () {
43
+ this.socket && i(!0);
44
+ const r = {
45
+ version: "1.0",
46
+ stream: JSON.stringify(u.stream),
47
+ appId: this._applicationId,
48
+ sessionId: this.id,
49
+ Authorization: `${this.token}`,
50
+ userMode: u.debug ? k.Debug : k.Normal
51
+ }, o = new URL(u.apiRoot), n = yield T(o.origin, {
52
+ path: o.pathname.replace("api", "socketio") + "/copilot",
53
+ query: r,
54
+ transports: ["websocket"]
55
+ });
56
+ n.on("connect", () => {
57
+ p.log("socket:connect"), this.event.emit("socket:connect"), n.emit("init");
58
+ }), n.on("disconnect", () => {
59
+ p.log("socket:disconnect"), this.event.emit("socket:disconnect"), this._ready = !1;
60
+ }), n.on("ready", (s) => {
61
+ if (s.code === 200) {
62
+ p.log("ready");
63
+ const e = u.debug ? k.Debug : k.Normal;
64
+ e === k.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, this.registerMcpFromClientConfig(), i(!0);
65
+ } else {
66
+ this._ready && (this._ready = !1);
67
+ const e = new Error(
68
+ I.SocketConnectionFailed
69
+ // , {
70
+ // cause: data,
71
+ // }
72
+ );
73
+ p.error("error", e), this.event.emit("error", e), i(!1);
74
+ }
75
+ }), n.on("action", (s, e) => v(this, null, function* () {
76
+ if (this._stopAnswer || !this.id)
77
+ return;
78
+ const {
79
+ type: t,
80
+ data: {
81
+ runnerIsClient: a,
82
+ pluginCode: h,
83
+ funcCode: g,
84
+ parameters: c,
85
+ requestBody: U
86
+ }
87
+ } = s, l = this.messages[this.messages.length - 1];
88
+ if (!l.messageId)
89
+ l.actions || (l.actions = []), l.actions[s.data.step - 1] || (l.actions[s.data.step - 1] = []), l.actions[s.data.step - 1].push(s), this.event.emit("message:action", {
90
+ messageId: l.messageId,
91
+ action: s
92
+ }), this.event.emit("message:updated", l);
93
+ else {
94
+ const f = {
95
+ messageId: "",
96
+ content: "",
97
+ role: y.Robot,
98
+ sessionId: this.id,
99
+ reference: [],
100
+ searchReference: [],
101
+ isLoading: !0,
102
+ messageTime: "",
103
+ actions: [[s]],
104
+ comment: {
105
+ isAgree: 0
106
+ },
107
+ isReplying: !0
108
+ };
109
+ this.pushMessage(f), this.event.emit("message:action", {
110
+ messageId: f.messageId,
111
+ action: s
112
+ });
113
+ }
114
+ if (this.inactivating) {
115
+ const f = new Error(
116
+ I.Inactive
117
+ // {
118
+ // cause: {
119
+ // code: -1,
120
+ // message:
121
+ // 'session is inactive, skipping plugin client function call',
122
+ // },
123
+ // }
124
+ );
125
+ p.warn("error", f), this.event.emit("error", f);
126
+ } else if (t === "FUNCTION_CALL" && a) {
127
+ if (p.log(`calling function ${g} of plugin ${h} with argument:
128
+ ${JSON.stringify(c)}`), !u.plugins[h]) {
129
+ e({
130
+ code: 404,
131
+ message: "对不起,没有找到插件。"
132
+ });
133
+ return;
134
+ }
135
+ const f = u.plugins[h][g];
136
+ typeof f != "function" && e({
137
+ code: 404,
138
+ message: "对不起,没有找到插件方法。"
139
+ });
140
+ let M = c == null ? void 0 : c.query;
141
+ try {
142
+ M = JSON.parse(c == null ? void 0 : c.query);
143
+ } catch (R) {
144
+ }
145
+ f(M, (...R) => {
146
+ this._stopAnswer || e(...R);
147
+ });
148
+ }
149
+ })), n.on("answer", (s) => {
150
+ if (!this._stopAnswer)
151
+ if (s.data.code !== 200 && (s.data.index = -1), s.data.stream || s.data.stream === void 0) {
152
+ let e = this.messages[this.messages.length - 1];
153
+ if (e && e.role === y.Robot)
154
+ this.handleStreamAnswer(s.data, e), this.messages[this.messages.length - 1] = e, this.event.emit("message:content", e), this.event.emit("message:updated", e), e.index === -1 && (e.isReplying = !1, this.event.emit("answer:finish", e));
155
+ else {
156
+ const t = {
157
+ content: "",
158
+ role: y.Robot,
159
+ reference: [],
160
+ searchReference: [],
161
+ messageId: "",
162
+ sessionId: this.id,
163
+ messageTime: N(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
164
+ comment: {
165
+ isAgree: 0
166
+ },
167
+ isReplying: !0
168
+ };
169
+ this.handleStreamAnswer(s.data, t), this.pushMessage(t), this.event.emit("message:content", t), t.index === -1 && (t.isReplying = !1, this.event.emit("answer:finish", t));
170
+ }
171
+ } else if (s.data.updateSession && this.data && (this.data = m(m({}, this.data), s.data.updateSession), this.event.emit("data:updated", this.data)), s.data.index === -1) {
172
+ let e = this.messages[this.messages.length - 1];
173
+ if (e && !e.messageId)
174
+ e = m(m({}, e), s.data), this.event.emit("message:content", e), this.event.emit("message:updated", e), this.event.emit("answer:finish", e);
175
+ else {
176
+ const t = m({
177
+ messageId: "",
178
+ content: "",
179
+ role: y.Robot,
180
+ sessionId: this.id,
181
+ reference: [],
182
+ searchReference: [],
183
+ isLoading: !1,
184
+ messageTime: "",
185
+ actions: [],
186
+ comment: {
187
+ isAgree: 0
188
+ },
189
+ isReplying: !0
190
+ }, s.data);
191
+ this.event.emit("message:content", t), this.pushMessage(t), this.event.emit("answer:finish", t);
192
+ }
193
+ } else {
194
+ let e = this.messages[this.messages.length - 1];
195
+ e && !e.messageId ? (e = m(m({}, e), s.data), this.messages[this.messages.length - 1] = e, this.event.emit("message:content", e), this.event.emit("message:updated", e)) : (this.pushMessage(m({
196
+ messageId: "",
197
+ content: "",
198
+ role: y.Robot,
199
+ sessionId: this.id,
200
+ reference: [],
201
+ searchReference: [],
202
+ isLoading: !1,
203
+ messageTime: "",
204
+ actions: [],
205
+ comment: {
206
+ isAgree: 0
207
+ },
208
+ isReplying: !0
209
+ }, s.data)), this.event.emit("message:content", s.data));
210
+ }
211
+ }), n.on("reference", (s) => v(this, null, function* () {
212
+ if (this._stopAnswer)
213
+ return;
214
+ if (!this.id) {
215
+ const c = new Error(
216
+ I.IdUndefined
217
+ // {
218
+ // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
219
+ // }
220
+ );
221
+ p.error(c), this.event.emit("error", c), this.frozen = !1;
222
+ return;
223
+ }
224
+ const {
225
+ type: e,
226
+ data: t,
227
+ requestId: a
228
+ } = s, h = this.messages.findIndex((c) => c.messageId === a && c.role === y.User);
229
+ if (h < 0)
230
+ return;
231
+ const g = h + 1;
232
+ if (g >= this.messages.length) {
233
+ const c = {
234
+ messageId: "",
235
+ content: "",
236
+ role: y.Robot,
237
+ sessionId: this.id,
238
+ reference: [],
239
+ searchReference: [],
240
+ isLoading: !0,
241
+ messageTime: "",
242
+ actions: [],
243
+ comment: {
244
+ isAgree: 0
245
+ },
246
+ isReplying: !0
247
+ };
248
+ this.pushMessage(c);
249
+ }
250
+ e === "KG" ? (this.messages[g] = w(m({}, this.messages[g]), {
251
+ reference: t.knowledgeReference
252
+ }), yield this.fillMessageReference(this.messages[g]), this.event.emit("message:reference", t.knowledgeReference), this.event.emit("message:updated", this.messages[g])) : e === "WEB_SEARCH" && (this.messages[g] = w(m({}, this.messages[g]), {
253
+ searchReference: t.knowledgeReference
254
+ }), this.event.emit("message:searchreference", t.searchReference), this.event.emit("message:updated", this.messages[g]));
255
+ })), this.socket = n, n.open();
256
+ }));
257
+ }
258
+ registerMcpFromClientConfig() {
259
+ return u.mcp ? new Promise((i, r) => {
260
+ if (!this.socket) {
261
+ r("socket is undefined");
262
+ return;
263
+ }
264
+ this.socket.emit("add_mcp_servers", u.mcp, (o) => {
265
+ o.code === 200 ? (Object.keys(o.data).forEach((n) => {
266
+ o.data[n].code !== 200 && console.error(n, "MCP Server 注册失败。", o.data[n]);
267
+ }), i(!0)) : (console.error("MCP Server 注册失败。", o), r(o));
268
+ });
269
+ }) : Promise.resolve(!0);
270
+ }
271
+ addMcpServers(i) {
272
+ return v(this, null, function* () {
273
+ return this.ready ? this.socket ? new Promise((r, o) => {
274
+ this.socket && this.socket.emit("add_mcp_servers", i, (n) => {
275
+ n.code === 200 ? (Object.keys(n.data).forEach((s) => {
276
+ n.data[s].code !== 200 && console.error(s, "MCP Server 注册失败。", n.data[s]);
277
+ }), r(!0)) : (console.error("MCP Server 注册失败。", n), o(n));
278
+ });
279
+ }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
280
+ });
281
+ }
282
+ listMcpServers() {
283
+ return v(this, null, function* () {
284
+ return this.ready ? this.socket ? new Promise((i, r) => {
285
+ this.socket && this.socket.emit("list_mcp_servers", (o) => {
286
+ o.code === 200 ? i(o.data) : r(o);
287
+ });
288
+ }) : (console.error(" socket is undefined."), Promise.reject("socket is undefined")) : (console.error("session is not avalible."), Promise.reject("session is not avalible"));
289
+ });
290
+ }
291
+ sendQuestion(i, r) {
292
+ return v(this, null, function* () {
293
+ var n, s, e;
294
+ let o = w(m({}, i), {
295
+ promptVariables: this.promptVariables.filter((t) => t.type === _.User).reduce((t, a) => w(m({}, t), {
296
+ [a.key]: a.value
297
+ }), {}),
298
+ systemPromptVariables: this.promptVariables.filter((t) => t.type === _.System).reduce((t, a) => w(m({}, t), {
299
+ [a.key]: a.value
300
+ }), {}),
301
+ knowledges: (n = this.knowledges) == null ? void 0 : n.map((t) => t.id),
302
+ //开启内容摘要
303
+ enableTitleSummary: !0
304
+ });
305
+ if (!this.active) {
306
+ if (yield this.activate(), !this.socket) {
307
+ const t = new Error(
308
+ I.SocketConnectionFailed
309
+ // {
310
+ // cause: { code: -1, message: 'socket is null' },
311
+ // }
312
+ );
313
+ return p.warn("error", t), this.event.emit("error", t), !1;
314
+ }
315
+ (s = this.socket) == null || s.emit("question", o);
316
+ return;
317
+ }
318
+ (e = this.socket) == null || e.emit("question", o);
319
+ });
320
+ }
321
+ stopAnswer() {
322
+ var i;
323
+ (i = this.socket) == null || i.emit("stop_answer");
324
+ }
325
+ }
326
+ export {
327
+ H as default
328
+ };
@@ -0,0 +1,269 @@
1
+ var k = Object.defineProperty, A = Object.defineProperties;
2
+ var x = Object.getOwnPropertyDescriptors;
3
+ var g = Object.getOwnPropertySymbols;
4
+ var I = Object.prototype.hasOwnProperty, R = Object.prototype.propertyIsEnumerable;
5
+ var y = (r, i, e) => i in r ? k(r, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[i] = e, p = (r, i) => {
6
+ for (var e in i || (i = {}))
7
+ I.call(i, e) && y(r, e, i[e]);
8
+ if (g)
9
+ for (var e of g(i))
10
+ R.call(i, e) && y(r, e, i[e]);
11
+ return r;
12
+ }, f = (r, i) => A(r, x(i));
13
+ var d = (r, i, e) => (y(r, typeof i != "symbol" ? i + "" : i, e), e);
14
+ var h = (r, i, e) => new Promise((t, s) => {
15
+ var o = (a) => {
16
+ try {
17
+ n(e.next(a));
18
+ } catch (l) {
19
+ s(l);
20
+ }
21
+ }, c = (a) => {
22
+ try {
23
+ n(e.throw(a));
24
+ } catch (l) {
25
+ s(l);
26
+ }
27
+ }, n = (a) => a.done ? t(a.value) : Promise.resolve(a.value).then(o, c);
28
+ n((e = e.apply(r, i)).next());
29
+ });
30
+ import { parseJWT as m } from "./utils.mjs";
31
+ import E from "./Logger.mjs";
32
+ import { getCVForceService as L } from "./api/index.mjs";
33
+ import { configure as S, configuration as v } from "./Config.mjs";
34
+ import { ApplicationTypes as _, PromptVariableType as w } from "@glodon-aiot/apis";
35
+ import C from "./AgentSession.mjs";
36
+ import M from "./DialogSession.mjs";
37
+ import V from "./EventBus.mjs";
38
+ import T from "./package.json.mjs";
39
+ const b = new E("[BotClient] ");
40
+ class U {
41
+ constructor(i, e, t = !0, s = {
42
+ analytics: {
43
+ enabled: !1
44
+ }
45
+ }) {
46
+ d(this, "event", new V());
47
+ d(this, "cvforceApi");
48
+ //从token解析出来的用户信息
49
+ d(this, "_applicationId");
50
+ d(this, "_application", null);
51
+ //session列表,被加载过的session将会被缓存到这个列表中
52
+ d(this, "_sessions", []);
53
+ //活跃的session,只支持一个活跃的session
54
+ d(this, "_activeSession");
55
+ d(this, "_ready", !1);
56
+ //问题长度
57
+ d(this, "_questionContextMaxLength", 4e3);
58
+ d(this, "_promptVariables", []);
59
+ if (this._token = i, this.lazyload = t, this.sdkConfigs = s, S(e), this._applicationId = m(i).rid || v.applicationId, !this._applicationId)
60
+ throw "token is not vaild";
61
+ this.cvforceApi = this.getService(), this.loadApplication().then((o) => {
62
+ S(f(p({}, e), {
63
+ stream: e.stream === void 0 ? (o == null ? void 0 : o.type) === _.Dialog || (o == null ? void 0 : o.type) === _.Agent : e.stream
64
+ })), this.event.emit("ready");
65
+ });
66
+ }
67
+ checkReady() {
68
+ if (!this.ready)
69
+ throw Error("client is not ready.");
70
+ }
71
+ get ready() {
72
+ return this._ready;
73
+ }
74
+ set ready(i) {
75
+ this._ready = i;
76
+ }
77
+ get questionContextMaxLength() {
78
+ return this.checkReady(), this._questionContextMaxLength;
79
+ }
80
+ get promptVariables() {
81
+ return this.checkReady(), this._promptVariables;
82
+ }
83
+ get knowledges() {
84
+ var i;
85
+ return this.checkReady(), (i = this._application) == null ? void 0 : i.arrange[0].knowledges;
86
+ }
87
+ get sessions() {
88
+ return this.checkReady(), this._sessions;
89
+ }
90
+ set sessions(i) {
91
+ this.checkReady(), this._sessions = i, this.event.emit("sessions:updated", i);
92
+ }
93
+ loadApplication() {
94
+ return h(this, null, function* () {
95
+ var e, t, s, o, c;
96
+ this._application = yield this.cvforceApi.getApplicationId(
97
+ this._applicationId,
98
+ // 这里记录应用的名称和版本,以便后期数据分析
99
+ {
100
+ ui: (e = this.sdkConfigs.ui) == null ? void 0 : e.name,
101
+ uiVersion: (t = this.sdkConfigs.ui) == null ? void 0 : t.version,
102
+ sdkVersion: T.version
103
+ }
104
+ ).then((n) => (typeof n.arrange[0].recommendQuestionOpen != "boolean" && (n.arrange[0].recommendQuestionOpen = !0), f(p({}, n), {
105
+ arrange: [f(p({}, n.arrange[0]), {
106
+ recommendQuestionOpen: v.loadRelatedQuesions && n.arrange[0].recommendQuestionOpen
107
+ })]
108
+ })));
109
+ const i = (c = (o = (s = this._application) == null ? void 0 : s.arrange) == null ? void 0 : o[0]) == null ? void 0 : c.llmModel;
110
+ return i && (this._questionContextMaxLength = yield this.cvforceApi.getBaseModel(i).then((n) => n.maxModelLength)), this._promptVariables = yield Promise.all([this.cvforceApi.getPrompts(this._applicationId, {
111
+ type: w.User
112
+ }), this.cvforceApi.getPrompts(this._applicationId, {
113
+ type: w.System
114
+ })]).then(([n, a]) => [...n, ...a]), this._ready = !0, this.event.emit("application:loaded"), this._application;
115
+ });
116
+ }
117
+ getService() {
118
+ return this.cvforceApi ? this.cvforceApi : L(v.apiRoot, this.token, (i, e) => b.error(e));
119
+ }
120
+ get token() {
121
+ return this._token;
122
+ }
123
+ set token(i) {
124
+ this.checkReady(), this._token = i, this.ready = !1;
125
+ const e = m(i).rid;
126
+ this._applicationId !== e && (this._applicationId = e, this._application = null, this.loadApplication().then(() => {
127
+ this.event.emit("ready");
128
+ }), this.clean());
129
+ }
130
+ get applicationId() {
131
+ return this._applicationId;
132
+ }
133
+ get application() {
134
+ return this.checkReady(), this._application;
135
+ }
136
+ get activeSession() {
137
+ var i;
138
+ return this.checkReady(), ((i = this._activeSession) == null ? void 0 : i.id) || "";
139
+ }
140
+ set activeSession(i) {
141
+ this.checkReady(), this.loadSession(i).then((e) => {
142
+ this._activeSession && this._activeSession.id !== i && this._activeSession.inactivate(), this._activeSession = e;
143
+ });
144
+ }
145
+ createSession(i, e, t, s, o) {
146
+ return h(this, null, function* () {
147
+ const c = this;
148
+ return new Promise((n, a) => {
149
+ const l = (u) => h(this, null, function* () {
150
+ if (u === _.Agent)
151
+ n(new C(i, e, t, s, o, c));
152
+ else if (u === _.Dialog)
153
+ n(new M(i, e, t, s, o, c));
154
+ else
155
+ throw Error(`不支持的应用类型:${u}`);
156
+ });
157
+ if (this.ready) {
158
+ if (!this._application)
159
+ throw Error("client is not ready");
160
+ l(this._application.type);
161
+ } else
162
+ this.event.on("ready", () => {
163
+ if (!this._application)
164
+ throw Error("_application is null");
165
+ l(this._application.type);
166
+ }, !0);
167
+ });
168
+ });
169
+ }
170
+ getSessions(i) {
171
+ return h(this, null, function* () {
172
+ return new Promise((e, t) => {
173
+ const s = () => this.cvforceApi.listSessions(this._applicationId, i).then((o) => {
174
+ const c = [];
175
+ return o.forEach((n) => h(this, null, function* () {
176
+ const a = this._sessions.find((l) => n.id === l.id);
177
+ if (a)
178
+ a.data = n, this._application ? a.application = this._application : a.loadApplicationInfo(), c.push(a);
179
+ else {
180
+ const l = yield this.createSession(this.token, n.id, n, this.lazyload, this._application || void 0);
181
+ c.push(l);
182
+ }
183
+ })), this.sessions = c, c;
184
+ });
185
+ if (this.ready) {
186
+ if (!this._application)
187
+ throw Error("_application is null");
188
+ e(s());
189
+ } else
190
+ this.event.on("ready", () => {
191
+ if (!this._application)
192
+ throw Error("_application is null");
193
+ e(s());
194
+ }, !0);
195
+ });
196
+ });
197
+ }
198
+ loadSession(i = "", e) {
199
+ return h(this, null, function* () {
200
+ this.checkReady();
201
+ let t = this.sessions.find((s) => s.id === i);
202
+ if (t || (t = yield this.createSession(this.token, i, e), this._sessions.push(t)), this._activeSession) {
203
+ if (this._activeSession === t)
204
+ return t;
205
+ this._activeSession.inactivate();
206
+ }
207
+ return t.id ? (this._activeSession = t, yield t.activate()) : t.addEventListener("data:loaded", () => {
208
+ this._activeSession !== t && (t == null || t.activate(), this._activeSession = t);
209
+ }, !0), t;
210
+ });
211
+ }
212
+ reload() {
213
+ return this.loadApplication().then(() => h(this, null, function* () {
214
+ return yield this.getSessions(), this.event.emit("ready"), this;
215
+ }));
216
+ }
217
+ reloadSession(i) {
218
+ return h(this, null, function* () {
219
+ yield this.reload();
220
+ let e = this._sessions.find((t) => t.id === i) || this._activeSession;
221
+ if (!e) {
222
+ console.warn("reloadSession session failed.");
223
+ return;
224
+ }
225
+ return yield e.reload(), e;
226
+ });
227
+ }
228
+ setTop(i, e, t = "top_time desc, sort_status desc, updated_at desc") {
229
+ return h(this, null, function* () {
230
+ let s = i ? this._sessions.find((o) => o.id === i) : this._activeSession;
231
+ if (!s) {
232
+ console.warn("setTop session failed.");
233
+ return;
234
+ }
235
+ return yield s.setTop(e), this.getSessions({
236
+ order: t
237
+ });
238
+ });
239
+ }
240
+ addEventListener(i, e, t = !1) {
241
+ if (typeof e != "function")
242
+ throw Error("event listener is not a function");
243
+ if (t) {
244
+ this.event.once(i, e);
245
+ return;
246
+ }
247
+ this.event.on(i, e);
248
+ }
249
+ removeEventListener(i, e) {
250
+ this.event.off(i, e);
251
+ }
252
+ removeSession(i, e) {
253
+ return h(this, null, function* () {
254
+ this.checkReady();
255
+ let t = -1;
256
+ if (typeof i == "string" ? t = this._sessions.findIndex((s) => s.id === i) : t = this._sessions.findIndex((s) => s.id === i.id), t > -1) {
257
+ const s = this._sessions[t];
258
+ e && (yield s.delete()), this._sessions.splice(t, 1);
259
+ } else
260
+ console.warn("session delete failed, session is not exist.");
261
+ });
262
+ }
263
+ clean() {
264
+ this._sessions.forEach((i) => i.destroy()), this.sessions = [];
265
+ }
266
+ }
267
+ export {
268
+ U as default
269
+ };
@@ -0,0 +1,37 @@
1
+ var s = Object.defineProperty, v = Object.defineProperties;
2
+ var u = Object.getOwnPropertyDescriptors;
3
+ var n = Object.getOwnPropertySymbols;
4
+ var p = Object.prototype.hasOwnProperty, h = Object.prototype.propertyIsEnumerable;
5
+ var g = (o, t, e) => t in o ? s(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e, i = (o, t) => {
6
+ for (var e in t || (t = {}))
7
+ p.call(t, e) && g(o, e, t[e]);
8
+ if (n)
9
+ for (var e of n(t))
10
+ h.call(t, e) && g(o, e, t[e]);
11
+ return o;
12
+ }, l = (o, t) => v(o, u(t));
13
+ import d from "./Logger.mjs";
14
+ const R = new d("[Config] ");
15
+ var f = /* @__PURE__ */ ((o) => (o.dev = "https://aiot-dev.glodon.com/api/cvforcepd", o.test = "https://aiot-dev.glodon.com/api/cvforce", o.prod = "https://copilot.glodon.com/api/cvforce", o.local = "http://localhost:3000/api/cvforcepd", o))(f || {});
16
+ let c = {
17
+ debug: !1,
18
+ env: "prod",
19
+ //AIoT 环境, 与apiRoot配对使用
20
+ apiRoot: "https://copilot.glodon.com/api/cvforce",
21
+ //https://${hostname}/${PRODUCD_CODE}
22
+ applicationId: "",
23
+ plugins: {},
24
+ mcp: void 0,
25
+ loadRelatedQuesions: !0,
26
+ stream: !1
27
+ };
28
+ function w(o) {
29
+ d.debug = (o == null ? void 0 : o.debug) || !1, c = l(i(i({}, c), o), {
30
+ apiRoot: (o == null ? void 0 : o.apiRoot) || c.apiRoot || (o != null && o.env ? f[o.env || "prod"] : c.apiRoot)
31
+ }), d.debug && R.log("updated", c);
32
+ }
33
+ export {
34
+ f as ApiUrl,
35
+ c as configuration,
36
+ w as configure
37
+ };