@glodon-aiot/agent-cli-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.
@@ -0,0 +1,252 @@
1
+ var w = Object.defineProperty, k = Object.defineProperties;
2
+ var A = Object.getOwnPropertyDescriptors;
3
+ var g = Object.getOwnPropertySymbols;
4
+ var x = Object.prototype.hasOwnProperty, I = Object.prototype.propertyIsEnumerable;
5
+ var u = (o, i, e) => i in o ? w(o, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[i] = e, p = (o, i) => {
6
+ for (var e in i || (i = {}))
7
+ x.call(i, e) && u(o, e, i[e]);
8
+ if (g)
9
+ for (var e of g(i))
10
+ I.call(i, e) && u(o, e, i[e]);
11
+ return o;
12
+ }, f = (o, i) => k(o, A(i));
13
+ var l = (o, i, e) => (u(o, typeof i != "symbol" ? i + "" : i, e), e);
14
+ var c = (o, i, e) => new Promise((t, s) => {
15
+ var n = (a) => {
16
+ try {
17
+ r(e.next(a));
18
+ } catch (h) {
19
+ s(h);
20
+ }
21
+ }, d = (a) => {
22
+ try {
23
+ r(e.throw(a));
24
+ } catch (h) {
25
+ s(h);
26
+ }
27
+ }, r = (a) => a.done ? t(a.value) : Promise.resolve(a.value).then(n, d);
28
+ r((e = e.apply(o, i)).next());
29
+ });
30
+ import { parseJWT as m } from "./utils.mjs";
31
+ import R from "./Logger.mjs";
32
+ import { getCVForceService as E } from "./api/index.mjs";
33
+ import { configure as S, configuration as y } from "./Config.mjs";
34
+ import { ApplicationTypes as v } from "@glodon-aiot/apis";
35
+ import L from "./AgentSession.mjs";
36
+ import M from "./DialogSession.mjs";
37
+ import C from "./EventBus.mjs";
38
+ const T = new R("[BotClient] ");
39
+ class z {
40
+ constructor(i, e, t = !0) {
41
+ l(this, "event", new C());
42
+ l(this, "cvforceApi");
43
+ //从token解析出来的用户信息
44
+ l(this, "_applicationId");
45
+ l(this, "_application", null);
46
+ //session列表,被加载过的session将会被缓存到这个列表中
47
+ l(this, "_sessions", []);
48
+ //活跃的session,只支持一个活跃的session
49
+ l(this, "_activeSession");
50
+ l(this, "_ready", !1);
51
+ //问题长度
52
+ l(this, "_questionContextMaxLength", 4e3);
53
+ l(this, "_promptVariables", []);
54
+ if (this._token = i, this.lazyload = t, S(e), this._applicationId = m(i).rid || y.applicationId, !this._applicationId)
55
+ throw "token is not vaild";
56
+ this.cvforceApi = this.getService(), this.loadApplication().then((s) => {
57
+ S(f(p({}, e), {
58
+ stream: e.stream === void 0 ? (s == null ? void 0 : s.type) === v.Dialog : e.stream
59
+ })), this.event.emit("ready");
60
+ });
61
+ }
62
+ checkReady() {
63
+ if (!this.ready)
64
+ throw Error("client is not ready.");
65
+ }
66
+ get ready() {
67
+ return this._ready;
68
+ }
69
+ set ready(i) {
70
+ this._ready = i;
71
+ }
72
+ get questionContextMaxLength() {
73
+ return this.checkReady(), this._questionContextMaxLength;
74
+ }
75
+ get promptVariables() {
76
+ return this.checkReady(), this._promptVariables;
77
+ }
78
+ get knowledges() {
79
+ var i;
80
+ return this.checkReady(), (i = this._application) == null ? void 0 : i.arrange[0].knowledges;
81
+ }
82
+ get sessions() {
83
+ return this.checkReady(), this._sessions;
84
+ }
85
+ set sessions(i) {
86
+ this.checkReady(), this._sessions = i, this.event.emit("sessions:updated", i);
87
+ }
88
+ loadApplication() {
89
+ return c(this, null, function* () {
90
+ var e, t, s;
91
+ this._application = yield this.cvforceApi.getApplicationId(this._applicationId).then((n) => (typeof n.arrange[0].recommendQuestionOpen != "boolean" && (n.arrange[0].recommendQuestionOpen = !0), f(p({}, n), {
92
+ arrange: [f(p({}, n.arrange[0]), {
93
+ recommendQuestionOpen: y.loadRelatedQuesions && n.arrange[0].recommendQuestionOpen
94
+ })]
95
+ })));
96
+ const i = (s = (t = (e = this._application) == null ? void 0 : e.arrange) == null ? void 0 : t[0]) == null ? void 0 : s.llmModel;
97
+ return i && (this._questionContextMaxLength = yield this.cvforceApi.getBaseModel(i).then((n) => n.maxModelLength)), this._promptVariables = yield this.cvforceApi.getPrompts(this._applicationId), this._ready = !0, this.event.emit("application:loaded"), this._application;
98
+ });
99
+ }
100
+ getService() {
101
+ return this.cvforceApi ? this.cvforceApi : E(y.apiRoot, this.token, (i, e) => T.error(e));
102
+ }
103
+ get token() {
104
+ return this._token;
105
+ }
106
+ set token(i) {
107
+ this.checkReady(), this._token = i, this.ready = !1;
108
+ const e = m(i).rid;
109
+ this._applicationId !== e && (this._applicationId = e, this._application = null, this.loadApplication().then(() => {
110
+ this.event.emit("ready");
111
+ }), this.clean());
112
+ }
113
+ get applicationId() {
114
+ return this._applicationId;
115
+ }
116
+ get application() {
117
+ return this.checkReady(), this._application;
118
+ }
119
+ get activeSession() {
120
+ var i;
121
+ return this.checkReady(), ((i = this._activeSession) == null ? void 0 : i.id) || "";
122
+ }
123
+ set activeSession(i) {
124
+ this.checkReady(), this.loadSession(i).then((e) => {
125
+ this._activeSession && this._activeSession.id !== i && this._activeSession.inactivate(), this._activeSession = e;
126
+ });
127
+ }
128
+ createSession(i, e, t, s, n) {
129
+ return c(this, null, function* () {
130
+ const d = this;
131
+ return new Promise((r, a) => {
132
+ const h = (_) => c(this, null, function* () {
133
+ if (_ === v.Agent)
134
+ r(new L(i, e, t, s, n, d));
135
+ else if (_ === v.Dialog)
136
+ r(new M(i, e, t, s, n, d));
137
+ else
138
+ throw Error(`不支持的应用类型:${_}`);
139
+ });
140
+ if (this.ready) {
141
+ if (!this._application)
142
+ throw Error("client is not ready");
143
+ h(this._application.type);
144
+ } else
145
+ this.event.on("ready", () => {
146
+ if (!this._application)
147
+ throw Error("_application is null");
148
+ h(this._application.type);
149
+ }, !0);
150
+ });
151
+ });
152
+ }
153
+ getSessions(i) {
154
+ return c(this, null, function* () {
155
+ return new Promise((e, t) => {
156
+ const s = () => this.cvforceApi.listSessions(this._applicationId, i).then((n) => {
157
+ const d = [];
158
+ return n.forEach((r) => c(this, null, function* () {
159
+ const a = this._sessions.find((h) => r.id === h.id);
160
+ if (a)
161
+ a.data = r, this._application ? a.application = this._application : a.loadApplicationInfo(), d.push(a);
162
+ else {
163
+ const h = yield this.createSession(this.token, r.id, r, this.lazyload, this._application || void 0);
164
+ d.push(h);
165
+ }
166
+ })), this.sessions = d, d;
167
+ });
168
+ if (this.ready) {
169
+ if (!this._application)
170
+ throw Error("_application is null");
171
+ e(s());
172
+ } else
173
+ this.event.on("ready", () => {
174
+ if (!this._application)
175
+ throw Error("_application is null");
176
+ e(s());
177
+ }, !0);
178
+ });
179
+ });
180
+ }
181
+ loadSession(i = "", e) {
182
+ return c(this, null, function* () {
183
+ this.checkReady();
184
+ let t = this.sessions.find((s) => s.id === i);
185
+ if (t || (t = yield this.createSession(this.token, i, e), this._sessions.push(t)), this._activeSession) {
186
+ if (this._activeSession === t)
187
+ return t;
188
+ this._activeSession.inactivate();
189
+ }
190
+ return t.id ? (yield t.activate(), this._activeSession = t) : t.addEventListener("data:loaded", () => {
191
+ this._activeSession !== t && (t == null || t.activate(), this._activeSession = t);
192
+ }, !0), t;
193
+ });
194
+ }
195
+ reload() {
196
+ return this.loadApplication().then(() => c(this, null, function* () {
197
+ return yield this.getSessions(), this.event.emit("ready"), this;
198
+ }));
199
+ }
200
+ reloadSession(i) {
201
+ return c(this, null, function* () {
202
+ yield this.reload();
203
+ let e = this._sessions.find((t) => t.id === i) || this._activeSession;
204
+ if (!e) {
205
+ console.warn("reloadSession session failed.");
206
+ return;
207
+ }
208
+ return yield e.reload(), e;
209
+ });
210
+ }
211
+ setTop(i, e, t = "top_time desc, sort_status desc, updated_at desc") {
212
+ return c(this, null, function* () {
213
+ let s = i ? this._sessions.find((n) => n.id === i) : this._activeSession;
214
+ if (!s) {
215
+ console.warn("setTop session failed.");
216
+ return;
217
+ }
218
+ return yield s.setTop(e), this.getSessions({
219
+ order: t
220
+ });
221
+ });
222
+ }
223
+ addEventListener(i, e, t = !1) {
224
+ if (typeof e != "function")
225
+ throw Error("event listener is not a function");
226
+ if (t) {
227
+ this.event.once(i, e);
228
+ return;
229
+ }
230
+ this.event.on(i, e);
231
+ }
232
+ removeEventListener(i, e) {
233
+ this.event.off(i, e);
234
+ }
235
+ removeSession(i, e) {
236
+ return c(this, null, function* () {
237
+ this.checkReady();
238
+ let t = -1;
239
+ if (typeof i == "string" ? t = this._sessions.findIndex((s) => s.id === i) : t = this._sessions.findIndex((s) => s.id === i.id), t > -1) {
240
+ const s = this._sessions[t];
241
+ e && (yield s.delete()), this._sessions.splice(t, 1);
242
+ } else
243
+ console.warn("session delete failed, session is not exist.");
244
+ });
245
+ }
246
+ clean() {
247
+ this._sessions.forEach((i) => i.destroy()), this.sessions = [];
248
+ }
249
+ }
250
+ export {
251
+ z as default
252
+ };
@@ -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
+ };
@@ -0,0 +1,301 @@
1
+ var A = Object.defineProperty, O = Object.defineProperties;
2
+ var D = Object.getOwnPropertyDescriptors;
3
+ var v = Object.getOwnPropertySymbols;
4
+ var P = Object.prototype.hasOwnProperty, $ = Object.prototype.propertyIsEnumerable;
5
+ var C = (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) && C(i, t, c[t]);
8
+ if (v)
9
+ for (var t of v(c))
10
+ $.call(c, t) && C(i, t, c[t]);
11
+ return i;
12
+ }, R = (i, c) => O(i, D(c));
13
+ var k = (i, c, t) => (C(i, typeof c != "symbol" ? c + "" : c, t), t);
14
+ var f = (i, c, t) => new Promise((o, g) => {
15
+ var d = (e) => {
16
+ try {
17
+ m(t.next(e));
18
+ } catch (l) {
19
+ g(l);
20
+ }
21
+ }, s = (e) => {
22
+ try {
23
+ m(t.throw(e));
24
+ } catch (l) {
25
+ g(l);
26
+ }
27
+ }, m = (e) => e.done ? o(e.value) : Promise.resolve(e.value).then(d, s);
28
+ m((t = t.apply(i, c)).next());
29
+ });
30
+ import { IHistoryRole as T, 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 M } 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 = "", g, d, s, m) {
39
+ super(t, o, g, d, s, m);
40
+ k(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, g, d, s, m) {
48
+ return f(this, null, function* () {
49
+ const e = {
50
+ code: 0,
51
+ // messageId: uuidv4(),
52
+ content: "",
53
+ role: T.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({}, g), {
64
+ stream: M.stream
65
+ });
66
+ p(`${M.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 (w) {
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), m && m({
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, g, d, s, m) {
146
+ return f(this, null, function* () {
147
+ const e = {
148
+ code: 0,
149
+ // messageId: uuidv4(),
150
+ content: "",
151
+ role: T.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({}, g), {
166
+ stream: M.stream
167
+ });
168
+ p(`${M.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), m && m({
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 w = () => {
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" && w(), a === "searchOver" && w(), 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: g,
243
+ fileIds: d,
244
+ oldMessageId: s
245
+ } = t, {
246
+ stream: m,
247
+ referenceFirst: e
248
+ } = o;
249
+ if (this.active || (yield this.activate()), this._id) {
250
+ let n = {
251
+ content: g,
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
+ };