UNPKG

@glodon-aiot/agent-cli-sdk

Version:

aiot agent client js sdk

253 lines (252 loc) 8.8 kB
var w = Object.defineProperty, k = Object.defineProperties; var A = Object.getOwnPropertyDescriptors; var g = Object.getOwnPropertySymbols; var x = Object.prototype.hasOwnProperty, I = Object.prototype.propertyIsEnumerable; 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) => { for (var e in i || (i = {})) x.call(i, e) && u(o, e, i[e]); if (g) for (var e of g(i)) I.call(i, e) && u(o, e, i[e]); return o; }, f = (o, i) => k(o, A(i)); var l = (o, i, e) => (u(o, typeof i != "symbol" ? i + "" : i, e), e); var c = (o, i, e) => new Promise((t, s) => { var n = (a) => { try { r(e.next(a)); } catch (h) { s(h); } }, d = (a) => { try { r(e.throw(a)); } catch (h) { s(h); } }, r = (a) => a.done ? t(a.value) : Promise.resolve(a.value).then(n, d); r((e = e.apply(o, i)).next()); }); import { parseJWT as m } from "./utils.mjs"; import R from "./Logger.mjs"; import { getCVForceService as E } from "./api/index.mjs"; import { configure as S, configuration as y } from "./Config.mjs"; import { ApplicationTypes as v } from "@glodon-aiot/apis"; import L from "./AgentSession.mjs"; import M from "./DialogSession.mjs"; import C from "./EventBus.mjs"; const T = new R("[BotClient] "); class z { constructor(i, e, t = !0) { l(this, "event", new C()); l(this, "cvforceApi"); //从token解析出来的用户信息 l(this, "_applicationId"); l(this, "_application", null); //session列表,被加载过的session将会被缓存到这个列表中 l(this, "_sessions", []); //活跃的session,只支持一个活跃的session l(this, "_activeSession"); l(this, "_ready", !1); //问题长度 l(this, "_questionContextMaxLength", 4e3); l(this, "_promptVariables", []); if (this._token = i, this.lazyload = t, S(e), this._applicationId = m(i).rid || y.applicationId, !this._applicationId) throw "token is not vaild"; this.cvforceApi = this.getService(), this.loadApplication().then((s) => { S(f(p({}, e), { stream: e.stream === void 0 ? (s == null ? void 0 : s.type) === v.Dialog : e.stream })), this.event.emit("ready"); }); } checkReady() { if (!this.ready) throw Error("client is not ready."); } get ready() { return this._ready; } set ready(i) { this._ready = i; } get questionContextMaxLength() { return this.checkReady(), this._questionContextMaxLength; } get promptVariables() { return this.checkReady(), this._promptVariables; } get knowledges() { var i; return this.checkReady(), (i = this._application) == null ? void 0 : i.arrange[0].knowledges; } get sessions() { return this.checkReady(), this._sessions; } set sessions(i) { this.checkReady(), this._sessions = i, this.event.emit("sessions:updated", i); } loadApplication() { return c(this, null, function* () { var e, t, s; this._application = yield this.cvforceApi.getApplicationId(this._applicationId).then((n) => (typeof n.arrange[0].recommendQuestionOpen != "boolean" && (n.arrange[0].recommendQuestionOpen = !0), f(p({}, n), { arrange: [f(p({}, n.arrange[0]), { recommendQuestionOpen: y.loadRelatedQuesions && n.arrange[0].recommendQuestionOpen })] }))); const i = (s = (t = (e = this._application) == null ? void 0 : e.arrange) == null ? void 0 : t[0]) == null ? void 0 : s.llmModel; 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; }); } getService() { return this.cvforceApi ? this.cvforceApi : E(y.apiRoot, this.token, (i, e) => T.error(e)); } get token() { return this._token; } set token(i) { this.checkReady(), this._token = i, this.ready = !1; const e = m(i).rid; this._applicationId !== e && (this._applicationId = e, this._application = null, this.loadApplication().then(() => { this.event.emit("ready"); }), this.clean()); } get applicationId() { return this._applicationId; } get application() { return this.checkReady(), this._application; } get activeSession() { var i; return this.checkReady(), ((i = this._activeSession) == null ? void 0 : i.id) || ""; } set activeSession(i) { this.checkReady(), this.loadSession(i).then((e) => { this._activeSession && this._activeSession.id !== i && this._activeSession.inactivate(), this._activeSession = e; }); } createSession(i, e, t, s, n) { return c(this, null, function* () { const d = this; return new Promise((r, a) => { const h = (_) => c(this, null, function* () { if (_ === v.Agent) r(new L(i, e, t, s, n, d)); else if (_ === v.Dialog) r(new M(i, e, t, s, n, d)); else throw Error(`不支持的应用类型:${_}`); }); if (this.ready) { if (!this._application) throw Error("client is not ready"); h(this._application.type); } else this.event.on("ready", () => { if (!this._application) throw Error("_application is null"); h(this._application.type); }, !0); }); }); } getSessions(i) { return c(this, null, function* () { return new Promise((e, t) => { const s = () => this.cvforceApi.listSessions(this._applicationId, i).then((n) => { const d = []; return n.forEach((r) => c(this, null, function* () { const a = this._sessions.find((h) => r.id === h.id); if (a) a.data = r, this._application ? a.application = this._application : a.loadApplicationInfo(), d.push(a); else { const h = yield this.createSession(this.token, r.id, r, this.lazyload, this._application || void 0); d.push(h); } })), this.sessions = d, d; }); if (this.ready) { if (!this._application) throw Error("_application is null"); e(s()); } else this.event.on("ready", () => { if (!this._application) throw Error("_application is null"); e(s()); }, !0); }); }); } loadSession(i = "", e) { return c(this, null, function* () { this.checkReady(); let t = this.sessions.find((s) => s.id === i); if (t || (t = yield this.createSession(this.token, i, e), this._sessions.push(t)), this._activeSession) { if (this._activeSession === t) return t; this._activeSession.inactivate(); } return t.id ? (yield t.activate(), this._activeSession = t) : t.addEventListener("data:loaded", () => { this._activeSession !== t && (t == null || t.activate(), this._activeSession = t); }, !0), t; }); } reload() { return this.loadApplication().then(() => c(this, null, function* () { return yield this.getSessions(), this.event.emit("ready"), this; })); } reloadSession(i) { return c(this, null, function* () { yield this.reload(); let e = this._sessions.find((t) => t.id === i) || this._activeSession; if (!e) { console.warn("reloadSession session failed."); return; } return yield e.reload(), e; }); } setTop(i, e, t = "top_time desc, sort_status desc, updated_at desc") { return c(this, null, function* () { let s = i ? this._sessions.find((n) => n.id === i) : this._activeSession; if (!s) { console.warn("setTop session failed."); return; } return yield s.setTop(e), this.getSessions({ order: t }); }); } addEventListener(i, e, t = !1) { if (typeof e != "function") throw Error("event listener is not a function"); if (t) { this.event.once(i, e); return; } this.event.on(i, e); } removeEventListener(i, e) { this.event.off(i, e); } removeSession(i, e) { return c(this, null, function* () { this.checkReady(); let t = -1; if (typeof i == "string" ? t = this._sessions.findIndex((s) => s.id === i) : t = this._sessions.findIndex((s) => s.id === i.id), t > -1) { const s = this._sessions[t]; e && (yield s.delete()), this._sessions.splice(t, 1); } else console.warn("session delete failed, session is not exist."); }); } clean() { this._sessions.forEach((i) => i.destroy()), this.sessions = []; } } export { z as default };