@glodon-aiot/agent-cli-sdk 3.2.2-beta.2 → 3.2.2-beta.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.
@@ -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,252 @@
1
+ var C = Object.defineProperty, b = Object.defineProperties;
2
+ var S = Object.getOwnPropertyDescriptors;
3
+ var A = Object.getOwnPropertySymbols;
4
+ var q = Object.prototype.hasOwnProperty, x = Object.prototype.propertyIsEnumerable;
5
+ var _ = (c, t, n) => t in c ? C(c, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : c[t] = n, h = (c, t) => {
6
+ for (var n in t || (t = {}))
7
+ q.call(t, n) && _(c, n, t[n]);
8
+ if (A)
9
+ for (var n of A(t))
10
+ x.call(t, n) && _(c, n, t[n]);
11
+ return c;
12
+ }, p = (c, t) => b(c, S(t));
13
+ var w = (c, t, n) => new Promise((d, o) => {
14
+ var s = (a) => {
15
+ try {
16
+ i(n.next(a));
17
+ } catch (m) {
18
+ o(m);
19
+ }
20
+ }, e = (a) => {
21
+ try {
22
+ i(n.throw(a));
23
+ } catch (m) {
24
+ o(m);
25
+ }
26
+ }, i = (a) => a.done ? d(a.value) : Promise.resolve(a.value).then(s, e);
27
+ i((n = n.apply(c, t)).next());
28
+ });
29
+ import { ChatMessageRole as k } from "@glodon-aiot/apis";
30
+ import E from "./Logger.mjs";
31
+ import { configuration as v } from "./Config.mjs";
32
+ import { io as L } from "socket.io-client";
33
+ import { UserMode as I } from "./PayloadTypes.mjs";
34
+ import N, { SessionErrors as y } from "./Session.mjs";
35
+ const u = new E("[AgentSession] ");
36
+ class B extends N {
37
+ constructor(t, n = "", d, o, s, e) {
38
+ super(t, n, d, o, s, e);
39
+ }
40
+ prepareConnection() {
41
+ return new Promise((t) => w(this, null, function* () {
42
+ this.socket && t(!0);
43
+ const n = {
44
+ version: "1.0",
45
+ stream: "false",
46
+ appId: this._applicationId,
47
+ sessionId: this.id,
48
+ Authorization: `${this.token}`,
49
+ userMode: v.debug ? I.Debug : I.Normal
50
+ }, d = new URL(v.apiRoot), o = yield L(d.origin, {
51
+ path: d.pathname.replace("api", "socketio") + "/copilot",
52
+ query: n,
53
+ transports: ["websocket"]
54
+ });
55
+ o.on("connect", () => {
56
+ u.log("socket:connect"), this.event.emit("socket:connect"), o.emit("init");
57
+ }), o.on("disconnect", () => {
58
+ u.log("socket:disconnect"), this.event.emit("socket:disconnect");
59
+ }), o.on("ready", (s) => {
60
+ if (s.code === 200) {
61
+ u.log("ready");
62
+ const e = v.debug ? I.Debug : I.Normal;
63
+ e === I.Debug && s.userMode, this._ready || (this._ready = !0, this.event.emit("ready")), this.frozen = !1, t(!0);
64
+ } else {
65
+ this._ready && (this._ready = !1);
66
+ const e = new Error(
67
+ y.SocketConnectionFailed
68
+ // , {
69
+ // cause: data,
70
+ // }
71
+ );
72
+ u.error("error", e), this.event.emit("error", e), t(!1);
73
+ }
74
+ }), o.on("action", (s, e) => w(this, null, function* () {
75
+ if (this._stopAnswer || !this.id)
76
+ return;
77
+ const {
78
+ type: i,
79
+ data: {
80
+ runnerIsClient: a,
81
+ pluginCode: m,
82
+ funcCode: g,
83
+ parameters: r,
84
+ requestBody: U
85
+ }
86
+ } = s, f = this.messages[this.messages.length - 1];
87
+ if (!f.messageId)
88
+ f.actions || (f.actions = []), f.actions[s.data.step - 1] || (f.actions[s.data.step - 1] = []), f.actions[s.data.step - 1].push(s), this.event.emit("message:action", {
89
+ messageId: f.messageId,
90
+ action: s
91
+ }), this.event.emit("message:updated", f);
92
+ else {
93
+ const l = {
94
+ messageId: "",
95
+ content: "",
96
+ role: k.Robot,
97
+ sessionId: this.id,
98
+ reference: [],
99
+ searchReference: [],
100
+ isLoading: !0,
101
+ messageTime: "",
102
+ actions: [[s]],
103
+ comment: {
104
+ isAgree: 0
105
+ }
106
+ };
107
+ this.pushMessage(l), this.event.emit("message:action", {
108
+ messageId: l.messageId,
109
+ action: s
110
+ });
111
+ }
112
+ if (this.inactivating) {
113
+ const l = new Error(
114
+ y.Inactive
115
+ // {
116
+ // cause: {
117
+ // code: -1,
118
+ // message:
119
+ // 'session is inactive, skipping plugin client function call',
120
+ // },
121
+ // }
122
+ );
123
+ u.warn("error", l), this.event.emit("error", l);
124
+ } else if (i === "FUNCTION_CALL" && a) {
125
+ if (u.log(`calling function ${g} of plugin ${m} with argument:
126
+ ${JSON.stringify(r)}`), !v.plugins[m]) {
127
+ e({
128
+ code: 404,
129
+ message: "对不起,没有找到插件。"
130
+ });
131
+ return;
132
+ }
133
+ const l = v.plugins[m][g];
134
+ typeof l != "function" && e({
135
+ code: 404,
136
+ message: "对不起,没有找到插件方法。"
137
+ });
138
+ let M = r == null ? void 0 : r.query;
139
+ try {
140
+ M = JSON.parse(r == null ? void 0 : r.query);
141
+ } catch (R) {
142
+ }
143
+ l(M, (...R) => {
144
+ this._stopAnswer || e(...R);
145
+ });
146
+ }
147
+ })), o.on("answer", (s) => {
148
+ if (!this._stopAnswer)
149
+ if (s.data.code !== 200 && (s.data.index = -1, this.event.emit("error", s.data)), s.data.index === -1) {
150
+ let e = this.messages[this.messages.length - 1];
151
+ if (e && !e.messageId)
152
+ e = h(h({}, e), s.data), this.event.emit("message:content", e), this.event.emit("message:updated", e), this.event.emit("answer:finish", e);
153
+ else {
154
+ const i = h({
155
+ messageId: "",
156
+ content: "",
157
+ role: k.Robot,
158
+ sessionId: this.id,
159
+ reference: [],
160
+ searchReference: [],
161
+ isLoading: !1,
162
+ messageTime: "",
163
+ actions: [],
164
+ comment: {
165
+ isAgree: 0
166
+ }
167
+ }, s.data);
168
+ this.event.emit("message:content", i), this.pushMessage(i), this.event.emit("answer:finish", i);
169
+ }
170
+ } else {
171
+ let e = this.messages[this.messages.length - 1];
172
+ e && !e.messageId ? (e = h(h({}, e), s.data), this.messages[this.messages.length - 1] = e, this.event.emit("message:content", e), this.event.emit("message:updated", e)) : (this.pushMessage(s.data), this.event.emit("message:content", s.data));
173
+ }
174
+ }), o.on("reference", (s) => w(this, null, function* () {
175
+ if (this._stopAnswer)
176
+ return;
177
+ if (!this.id) {
178
+ const r = new Error(
179
+ y.IdUndefined
180
+ // {
181
+ // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined],
182
+ // }
183
+ );
184
+ u.error(r), this.event.emit("error", r), this.frozen = !1;
185
+ return;
186
+ }
187
+ const {
188
+ type: e,
189
+ data: i,
190
+ requestId: a
191
+ } = s, m = this.messages.findIndex((r) => r.messageId === a && r.role === k.User);
192
+ if (m < 0)
193
+ return;
194
+ const g = m + 1;
195
+ if (g >= this.messages.length) {
196
+ const r = {
197
+ messageId: "",
198
+ content: "",
199
+ role: k.Robot,
200
+ sessionId: this.id,
201
+ reference: [],
202
+ searchReference: [],
203
+ isLoading: !0,
204
+ messageTime: "",
205
+ actions: [],
206
+ comment: {
207
+ isAgree: 0
208
+ }
209
+ };
210
+ this.pushMessage(r);
211
+ }
212
+ e === "KG" ? (this.messages[g] = p(h({}, this.messages[g]), {
213
+ reference: i.knowledgeReference
214
+ }), yield this.fillMessageReference(this.messages[g]), this.event.emit("message:reference", i.knowledgeReference), this.event.emit("message:updated", this.messages[g])) : e === "WEB_SEARCH" && (this.messages[g] = p(h({}, this.messages[g]), {
215
+ searchReference: i.knowledgeReference
216
+ }), this.event.emit("message:searchreference", i.searchReference), this.event.emit("message:updated", this.messages[g]));
217
+ })), this.socket = o, o.open();
218
+ }));
219
+ }
220
+ sendQuestion(t, n) {
221
+ return w(this, null, function* () {
222
+ var o, s, e;
223
+ let d = p(h({}, t), {
224
+ promptVariables: this.promptVariables.reduce((i, a) => p(h({}, i), {
225
+ [a.key]: a.value
226
+ }), {}),
227
+ knowledges: (o = this.knowledges) == null ? void 0 : o.map((i) => i.id)
228
+ });
229
+ if (!this.active) {
230
+ if (yield this.activate(), !this.socket) {
231
+ const i = new Error(
232
+ y.SocketConnectionFailed
233
+ // {
234
+ // cause: { code: -1, message: 'socket is null' },
235
+ // }
236
+ );
237
+ return u.warn("error", i), this.event.emit("error", i), !1;
238
+ }
239
+ (s = this.socket) == null || s.emit("question", d);
240
+ return;
241
+ }
242
+ (e = this.socket) == null || e.emit("question", d);
243
+ });
244
+ }
245
+ stopAnswer() {
246
+ var t;
247
+ (t = this.socket) == null || t.emit("stop_answer");
248
+ }
249
+ }
250
+ export {
251
+ B as default
252
+ };
@@ -0,0 +1,250 @@
1
+ var S = Object.defineProperty, w = Object.defineProperties;
2
+ var k = Object.getOwnPropertyDescriptors;
3
+ var v = Object.getOwnPropertySymbols;
4
+ var A = Object.prototype.hasOwnProperty, x = Object.prototype.propertyIsEnumerable;
5
+ var f = (o, i, e) => i in o ? S(o, i, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[i] = e, _ = (o, i) => {
6
+ for (var e in i || (i = {}))
7
+ A.call(i, e) && f(o, e, i[e]);
8
+ if (v)
9
+ for (var e of v(i))
10
+ x.call(i, e) && f(o, e, i[e]);
11
+ return o;
12
+ }, u = (o, i) => w(o, k(i));
13
+ var p = (o, i, e) => (f(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
+ }, l = (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, l);
28
+ r((e = e.apply(o, i)).next());
29
+ });
30
+ import { parseJWT as g } from "./utils.mjs";
31
+ import I from "./Logger.mjs";
32
+ import { getCVForceService as R } from "./api/index.mjs";
33
+ import { configure as E, configuration as y } from "./Config.mjs";
34
+ import { ApplicationTypes as m } 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 I("[BotClient] ");
39
+ class z {
40
+ constructor(i, e, t = !0) {
41
+ p(this, "event", new C());
42
+ p(this, "cvforceApi");
43
+ //从token解析出来的用户信息
44
+ p(this, "_applicationId");
45
+ p(this, "_application", null);
46
+ //session列表,被加载过的session将会被缓存到这个列表中
47
+ p(this, "_sessions", []);
48
+ //活跃的session,只支持一个活跃的session
49
+ p(this, "_activeSession");
50
+ p(this, "_ready", !1);
51
+ //问题长度
52
+ p(this, "_questionContextMaxLength", 4e3);
53
+ p(this, "_promptVariables", []);
54
+ if (this._token = i, this.lazyload = t, E(e), this._applicationId = g(i).rid || y.applicationId, !this._applicationId)
55
+ throw "token is not vaild";
56
+ this.cvforceApi = this.getService(), this.loadApplication().then(() => {
57
+ this.event.emit("ready");
58
+ });
59
+ }
60
+ checkReady() {
61
+ if (!this.ready)
62
+ throw Error("client is not ready.");
63
+ }
64
+ get ready() {
65
+ return this._ready;
66
+ }
67
+ set ready(i) {
68
+ this._ready = i;
69
+ }
70
+ get questionContextMaxLength() {
71
+ return this.checkReady(), this._questionContextMaxLength;
72
+ }
73
+ get promptVariables() {
74
+ return this.checkReady(), this._promptVariables;
75
+ }
76
+ get knowledges() {
77
+ var i;
78
+ return this.checkReady(), (i = this._application) == null ? void 0 : i.arrange[0].knowledges;
79
+ }
80
+ get sessions() {
81
+ return this.checkReady(), this._sessions;
82
+ }
83
+ set sessions(i) {
84
+ this.checkReady(), this._sessions = i, this.event.emit("sessions:updated", i);
85
+ }
86
+ loadApplication() {
87
+ return c(this, null, function* () {
88
+ var e, t, s;
89
+ this._application = yield this.cvforceApi.getApplicationId(this._applicationId).then((n) => (typeof n.arrange[0].recommendQuestionOpen != "boolean" && (n.arrange[0].recommendQuestionOpen = !0), u(_({}, n), {
90
+ arrange: [u(_({}, n.arrange[0]), {
91
+ recommendQuestionOpen: y.loadRelatedQuesions && n.arrange[0].recommendQuestionOpen
92
+ })]
93
+ })));
94
+ const i = (s = (t = (e = this._application) == null ? void 0 : e.arrange) == null ? void 0 : t[0]) == null ? void 0 : s.llmModel;
95
+ 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;
96
+ });
97
+ }
98
+ getService() {
99
+ return this.cvforceApi ? this.cvforceApi : R(y.apiRoot, this.token, (i, e) => T.error(e));
100
+ }
101
+ get token() {
102
+ return this._token;
103
+ }
104
+ set token(i) {
105
+ this.checkReady(), this._token = i, this.ready = !1;
106
+ const e = g(i).rid;
107
+ this._applicationId !== e && (this._applicationId = e, this._application = null, this.loadApplication().then(() => {
108
+ this.event.emit("ready");
109
+ }), this.clean());
110
+ }
111
+ get applicationId() {
112
+ return this._applicationId;
113
+ }
114
+ get application() {
115
+ return this.checkReady(), this._application;
116
+ }
117
+ get activeSession() {
118
+ var i;
119
+ return this.checkReady(), ((i = this._activeSession) == null ? void 0 : i.id) || "";
120
+ }
121
+ set activeSession(i) {
122
+ this.checkReady(), this.loadSession(i).then((e) => {
123
+ this._activeSession && this._activeSession.id !== i && this._activeSession.inactivate(), this._activeSession = e;
124
+ });
125
+ }
126
+ createSession(i, e, t, s, n) {
127
+ return c(this, null, function* () {
128
+ const l = this;
129
+ return new Promise((r, a) => {
130
+ const h = (d) => c(this, null, function* () {
131
+ if (d === m.Agent)
132
+ r(new L(i, e, t, s, n, l));
133
+ else if (d === m.Dialog)
134
+ r(new M(i, e, t, s, n, l));
135
+ else
136
+ throw Error(`不支持的应用类型:${d}`);
137
+ });
138
+ if (this.ready) {
139
+ if (!this._application)
140
+ throw Error("client is not ready");
141
+ h(this._application.type);
142
+ } else
143
+ this.event.on("ready", () => {
144
+ if (!this._application)
145
+ throw Error("_application is null");
146
+ h(this._application.type);
147
+ }, !0);
148
+ });
149
+ });
150
+ }
151
+ getSessions(i) {
152
+ return c(this, null, function* () {
153
+ return new Promise((e, t) => {
154
+ const s = () => this.cvforceApi.listSessions(this._applicationId, i).then((n) => {
155
+ const l = [];
156
+ return n.forEach((r) => c(this, null, function* () {
157
+ const a = this._sessions.find((h) => r.id === h.id);
158
+ if (a)
159
+ a.data = r, this._application ? a.application = this._application : a.loadApplicationInfo(), l.push(a);
160
+ else {
161
+ const h = yield this.createSession(this.token, r.id, r, this.lazyload, this._application || void 0);
162
+ l.push(h);
163
+ }
164
+ })), this.sessions = l, l;
165
+ });
166
+ if (this.ready) {
167
+ if (!this._application)
168
+ throw Error("_application is null");
169
+ e(s());
170
+ } else
171
+ this.event.on("ready", () => {
172
+ if (!this._application)
173
+ throw Error("_application is null");
174
+ e(s());
175
+ }, !0);
176
+ });
177
+ });
178
+ }
179
+ loadSession(i = "", e) {
180
+ return c(this, null, function* () {
181
+ this.checkReady();
182
+ let t = this.sessions.find((s) => s.id === i);
183
+ if (t || (t = yield this.createSession(this.token, i, e), this._sessions.push(t)), this._activeSession) {
184
+ if (this._activeSession === t)
185
+ return t;
186
+ this._activeSession.inactivate();
187
+ }
188
+ return t.id ? (yield t.activate(), this._activeSession = t) : t.addEventListener("data:loaded", () => {
189
+ this._activeSession !== t && (t == null || t.activate(), this._activeSession = t);
190
+ }, !0), t;
191
+ });
192
+ }
193
+ reload() {
194
+ return this.loadApplication().then(() => c(this, null, function* () {
195
+ return yield this.getSessions(), this.event.emit("ready"), this;
196
+ }));
197
+ }
198
+ reloadSession(i) {
199
+ return c(this, null, function* () {
200
+ yield this.reload();
201
+ let e = this._sessions.find((t) => t.id === i) || this._activeSession;
202
+ if (!e) {
203
+ console.warn("reloadSession session failed.");
204
+ return;
205
+ }
206
+ return yield e.reload(), e;
207
+ });
208
+ }
209
+ setTop(i, e, t = "top_time desc, sort_status desc, updated_at desc") {
210
+ return c(this, null, function* () {
211
+ let s = i ? this._sessions.find((n) => n.id === i) : this._activeSession;
212
+ if (!s) {
213
+ console.warn("setTop session failed.");
214
+ return;
215
+ }
216
+ return yield s.setTop(e), this.getSessions({
217
+ order: t
218
+ });
219
+ });
220
+ }
221
+ addEventListener(i, e, t = !1) {
222
+ if (typeof e != "function")
223
+ throw Error("event listener is not a function");
224
+ if (t) {
225
+ this.event.once(i, e);
226
+ return;
227
+ }
228
+ this.event.on(i, e);
229
+ }
230
+ removeEventListener(i, e) {
231
+ this.event.off(i, e);
232
+ }
233
+ removeSession(i, e) {
234
+ return c(this, null, function* () {
235
+ this.checkReady();
236
+ let t = -1;
237
+ if (typeof i == "string" ? t = this._sessions.findIndex((s) => s.id === i) : t = this._sessions.findIndex((s) => s.id === i.id), t > -1) {
238
+ const s = this._sessions[t];
239
+ e && (yield s.delete()), this._sessions.splice(t, 1);
240
+ } else
241
+ console.warn("session delete failed, session is not exist.");
242
+ });
243
+ }
244
+ clean() {
245
+ this._sessions.forEach((i) => i.destroy()), this.sessions = [];
246
+ }
247
+ }
248
+ export {
249
+ z as default
250
+ };
@@ -0,0 +1,35 @@
1
+ var s = Object.defineProperty, u = Object.defineProperties;
2
+ var v = 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) => u(o, v(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
+ loadRelatedQuesions: !0
25
+ };
26
+ function x(o) {
27
+ d.debug = (o == null ? void 0 : o.debug) || !1, c = l(i(i({}, c), o), {
28
+ apiRoot: (o == null ? void 0 : o.apiRoot) || c.apiRoot || (o != null && o.env ? f[o.env || "prod"] : c.apiRoot)
29
+ }), d.debug && R.log("updated", c);
30
+ }
31
+ export {
32
+ f as ApiUrl,
33
+ c as configuration,
34
+ x as configure
35
+ };