UNPKG

@glodon-aiot/agent-cli-sdk

Version:

aiot agent client js sdk

690 lines (689 loc) 29.6 kB
var x = Object.defineProperty, V = Object.defineProperties; var Q = Object.getOwnPropertyDescriptors; var S = Object.getOwnPropertySymbols; var T = Object.prototype.hasOwnProperty, R = Object.prototype.propertyIsEnumerable; var k = (h, e, i) => e in h ? x(h, e, { enumerable: !0, configurable: !0, writable: !0, value: i }) : h[e] = i, f = (h, e) => { for (var i in e || (e = {})) T.call(e, i) && k(h, i, e[i]); if (S) for (var i of S(e)) R.call(e, i) && k(h, i, e[i]); return h; }, I = (h, e) => V(h, Q(e)); var c = (h, e, i) => (k(h, typeof e != "symbol" ? e + "" : e, i), i); var r = (h, e, i) => new Promise((t, n) => { var o = (s) => { try { d(i.next(s)); } catch (p) { n(p); } }, l = (s) => { try { d(i.throw(s)); } catch (p) { n(p); } }, d = (s) => s.done ? t(s.value) : Promise.resolve(s.value).then(o, l); d((i = i.apply(h, e)).next()); }); import { v4 as M } from "uuid"; import { parseJWT as z, removeNewlines as O, removeLeadingAndTrailingBackslashes as C } from "./utils.mjs"; import { ApplicationTypes as E, PromptStatus as P, PromptVariableType as b, ChatMessageRole as L } from "@glodon-aiot/apis"; import q from "./Logger.mjs"; import { configuration as A } from "./Config.mjs"; import F from "./EventBus.mjs"; import { getCVForceService as N } from "./api/index.mjs"; import B from "dayjs"; const u = new q("[Session] "); var U = /* @__PURE__ */ ((h) => (h.DataRequestFailed = "data request error", h.SocketConnectionFailed = "socket error", h.IdUndefined = "session id is not defined", h.SendingEmptyContent = "failed at sending empty content", h.Frozen = "session is frozen", h.UserModeError = "failed at setting socket user mode", h.Inactive = "session is inactive", h))(U || {}); class X { /** * Session 类的构造函数,用于初始化会话实例。 * @param token - 用户的身份验证令牌,用于解析应用 ID。 * @param _id - 会话的 ID,可选参数,默认为空字符串。 * @param _data - 会话的初始数据,可选参数,包含提示变量、知识库等信息。 * @param lazyload - 是否懒加载数据,可选参数,默认为 undefined。 * @param _application - 应用信息,可选参数。 * @param client - Bot 客户端实例,可选参数。 */ constructor(e, i = "", t, n, o, l) { c(this, "_id"); c(this, "_frozen", !1); //是否可发送新问题 c(this, "inactivating", !1); c(this, "_ready", !1); // ready = false; c(this, "event", new F()); //从token解析出来的用户信息 c(this, "_applicationId"); c(this, "_application", null); c(this, "data", null); c(this, "client", null); c(this, "messages", []); c(this, "_loadingMessages", !1); c(this, "_loadingMessagesSize", 10); c(this, "cvforceApi"); c(this, "socket", null); c(this, "_promptVariables", []); c(this, "active", !1); c(this, "activing", !1); c(this, "_stopAnswer", !1); c(this, "_dataLoading", !1); c(this, "_dataLoaded", !1); c(this, "_mcpServers", null); this.token = e, this.lazyload = n, this.client = l || null, this._applicationId = z(e).rid || A.applicationId, this.cvforceApi = this.getService(), o && (this._application = o), this.event.emit("init", { applicationId: this._applicationId, sessionId: i }), this.loadApplicationInfo().then(() => { var d; if (i) this._id = i, this.event.emit("data:loading"), t ? (this.data = t, n || (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, i).then((s) => r(this, null, function* () { this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, i); })).then(() => r(this, null, function* () { this.messages = [], yield this.fetchMessages(), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data); })))) : (this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, i).then((s) => r(this, null, function* () { this.data = s, this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, i); })).then(() => r(this, null, function* () { this.messages = [], yield this.fetchMessages(), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), this._dataLoading = !1, this._dataLoaded = !0; }))); else { const s = (t == null ? void 0 : t.name) || "新建会话"; this.event.emit("data:creating", { applicationId: this._applicationId, name: s }), this.event.emit("data:loading"), this.cvforceApi.postSession(this.applicationId, { name: s, // @ts-ignore netOpen: t == null ? void 0 : t.netOpen, knowledgeIdList: (d = t == null ? void 0 : t.knowledges) == null ? void 0 : d.map((p) => p.id), applicationId: this._applicationId }).then((p) => r(this, null, function* () { var g; this._id = p.id, this.data = I(f({}, p), { knowledges: t == null ? void 0 : t.knowledges }), this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), yield (() => r(this, null, function* () { if (t != null && t.knowledges) return this.setKnowledges((t == null ? void 0 : t.knowledges) || void 0); }))(), yield (() => { if (t != null && t.promptVariables) { const y = this._promptVariables.map((m) => { var w, v; const a = ((v = (w = t.promptVariables) == null ? void 0 : w.find((_) => _.key === m.key && _.type === m.type)) == null ? void 0 : v.value) || ""; return I(f({}, m), { value: a }); }).sort((m, a) => m.key.localeCompare(a.key)); return this.setPromptVariables(y || []); } else return; })(), this.event.emit("data:created", this.data), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), ((g = this.application) == null ? void 0 : g.type) !== E.Agent && (this._ready = !0, this.event.emit("ready")); })); } }); } fetchData() { return r(this, null, function* () { if (!this.data) throw Error("session data is not loaded"); return yield this.cvforceApi.getSession(this.applicationId, this.data.id).then((e) => r(this, null, function* () { return this.data = e, this.event.emit("data:updated", this.data), e; })); }); } get id() { var e; return this._id || ((e = this.data) == null ? void 0 : e.id) || ""; } get applicationId() { return this._applicationId; } get application() { return this._application; } set application(e) { this._application = e; } loadApplicationInfo() { if (this._application) return Promise.resolve(this._application); if (this._applicationId && !this._application) return this.event.emit("application:data:loading"), this.cvforceApi.getApplicationId(this._applicationId).then((e) => (this._application = e, this.event.emit("application:data:loaded", e), e)); throw Error("application id is not defined"); } get ready() { return this._ready; } get frozen() { return this._frozen; } set frozen(e) { e === !1 && this.inactivating && this.socket && (this.socket.disconnect(), this.socket = null, this.event.emit("inactivated")), this._frozen = e, this.event.emit("frozen", e); } get knowledges() { if (!this.data) throw Error("session data is not loaded"); return this.data.knowledges || void 0; } setKnowledges(e) { return r(this, null, function* () { if (!this.data) throw Error("session data is not loaded"); return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, { knowledges: e }), this.data.knowledges = e, this.event.emit("data:updated", this.data), this.event.emit("data:updated:knowledges", { knowledges: e }), e; }); } validateConfigs() { if (!this._id) throw Error("session id is not defined"); if (!this.data) throw Error("session data is not loaded"); const e = this.promptVariables.filter((i) => i.status === P.Quoted).filter((i) => !i.value); if (e.length) throw Error(`在提示词中被引用的变量值不能为空, 请检查以下变量: ${e.map((i) => i.key).join(", ")}`); } get promptVariables() { return this._promptVariables.filter((e) => !(e.type === b.User && e.key === "query") && !(e.type === b.System && e.key === "datetime")); } setPromptVariables(e) { return r(this, null, function* () { const i = this._promptVariables.map((t) => { var o; const n = ((o = e == null ? void 0 : e.find((l) => l.key === t.key && l.type === t.type)) == null ? void 0 : o.value) || t.value; return I(f({}, t), { value: n }); }).sort((t, n) => t.key.localeCompare(n.key)); if (yield Promise.all(i.filter((t) => !(t.type === b.User && t.key === "query") && !(t.type === b.System && t.key === "datetime")).map((t) => { if (!this.data) throw Error("session data is not loaded"); if (!(!t.id || !t.key)) return this.cvforceApi.patchApplicationIdSessionpromptVariablesId(this._applicationId, this.data.id, t.id, { value: t.value }); })), !this.data) throw Error("session data is not loaded"); return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this.data.id), this.event.emit("data:updated", this.data), this.event.emit("data:updated:promptVariables", { promptVariables: this._promptVariables }), this._promptVariables; }); } get netOpen() { if (!this.data) throw Error("session data is not loaded"); return this.data.netOpen || !1; } /** * 异步设置会话的网络访问开关状态 * @param v - 布尔值,表示是否开启网络访问,true 为开启,false 为关闭 * @returns 如果设置成功,返回最终的网络访问开关状态;若不满足设置条件,则提前返回 undefined */ setNetOpen(e) { return r(this, null, function* () { var i, t, n; if (u.log("setNetOpen:", e), u.log("application arrange setting value: ", (i = this._application) == null ? void 0 : i.arrange[0].enableNetOpen), ((t = this._application) == null ? void 0 : t.type) === E.Agent) { console.warn("Agent Application Sessions are disabled to setNetOpen."); return; } if (((n = this._application) == null ? void 0 : n.arrange[0].enableNetOpen) === !1) { console.warn("This Application is disabled to change netOpen."); return; } if (!this.data) throw Error("session data is not loaded"); return yield this.cvforceApi.patchSessionsId(this._applicationId, this.data.id, { netOpen: e ? 1 : 2 /* disable */ }), this.data.netOpen = e, u.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.event.emit("data:updated:netOpen", { netOpen: e }), e; }); } getService() { return N(A.apiRoot, this.token, (e, i) => { const t = new Error("data request error", { cause: { status: e, message: i } }); u.error(t), this.event.emit("error", t); }); } addEventListener(e, i, t = !1) { if (typeof i != "function") throw Error("event listener is not a function"); if (t) { this.event.once(e, i); return; } this.event.on(e, i); } removeEventListener(e, i) { this.event.off(e, i); } /** * 获取对话记录 * @param params ChatHistoryParams * @returns Promise<false|Message[]> */ fetchMessages() { return r(this, arguments, function* (e = {}) { var t, n; const i = { pageSize: this._loadingMessagesSize }; if (this._id) { e = f(f({}, i), e), this.event.emit("history:loading"), u.log("history:loading"), (e.endTime || e.startTime) && (this.event.emit("history:moreloading", e), u.log("history:moreloading", e)), this._loadingMessages = !0; const o = yield this.cvforceApi.getChatHistorySessionId(this._applicationId, this._id, e), l = o.map((s, p) => { var m, a; let g = [], y; return typeof ((m = s.agentHistoryList) == null ? void 0 : m.agentList) == "string" && (g = JSON.parse(s.agentHistoryList.agentList).reduce((v, _) => (v[_.data.step - 1] || (v[_.data.step - 1] = []), v[_.data.step - 1].push(_), v), [])), p === o.length - 1 && (y = (a = this.data) == null ? void 0 : a.lastRelatedQuestion), I(f({}, s), { actions: g, relatedQuesion: y }); }); if (!e.endTime && !e.startTime) { const s = this.messages[this.messages.length - 1]; s && !s.relatedQuesion && (s.relatedQuesion = (t = this.data) == null ? void 0 : t.lastRelatedQuestion), this.messages = [...l, ...this.messages]; } else if (e.endTime && !e.startTime) { const s = this.messages.findIndex((p) => p.ts === e.endTime); this.messages.splice(0, s + 1), this.messages = [...l, ...this.messages]; } else if (!e.endTime && e.startTime) { const s = this.messages.findLastIndex((p) => p.ts === e.endTime); this.messages.splice(s, this.messages.length - s - 1), this.messages = [...this.messages, ...l]; } else { const s = this.messages.findIndex((g) => g.ts === e.endTime), p = this.messages.findLastIndex((g) => g.ts === e.endTime); this.messages.splice(s, p - s + 1, ...l); } const d = this.messages[this.messages.length - 1]; return d && !d.relatedQuesion && (d.relatedQuesion = (n = this.data) == null ? void 0 : n.lastRelatedQuestion), this.event.emit("history:moreloaded", l), u.log("history:loaded", this.messages), this.event.emit("history:loaded", this.messages), this._loadingMessages = !1, l; } else { const o = new Error( "session id is not defined" /* IdUndefined */ // { // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined], // } ); return u.error(o), this.event.emit("error", o), !1; } }); } rename(e) { return r(this, null, function* () { if (!this._id) throw Error("session is not created yet"); if (e.trim() === "") throw Error("name is empty"); return this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, { name: e }), u.log("data:updated", this.messages), this.event.emit("data:updated", this.data), this.event.emit("data:updated:name", { name: e }), !0; }); } pushMessage(e) { return r(this, null, function* () { this.messages.push(e), u.log("history:add", e), this.event.emit("history:add", e), this.event.emit("message:new", e), this.event.emit("message:updated", e); }); } reloadSessionInfo() { if (!this._id) throw Error("session is not created yet"); return this._dataLoading = !0, this.cvforceApi.getSession(this.applicationId, this._id).then((e) => r(this, null, function* () { if (this.data = e, !this._id) throw Error("session is not created yet"); return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, this._id), u.log("data:updated", this.data), this.event.emit("data:updated", this.data), this.data; })).then(() => r(this, null, function* () { return this.messages = [], yield this.fetchMessages(), this._dataLoading = !1, this._dataLoaded = !0, this.event.emit("data:loaded", this.data), this; })); } reload() { return r(this, null, function* () { return yield this.reloadSessionInfo(), this.socket ? (this.inactivate(!0), this.activate()) : (this.inactivate(!0), this.activate()); }); } delete() { return r(this, null, function* () { if (!this.id) { const e = new Error( "session id is not defined" /* IdUndefined */ // { // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined], // } ); return u.error(e), this.event.emit("error", e), !1; } return this.event.emit("data:deleting"), yield this.cvforceApi.delSessionsId(this.applicationId, this.id), this.event.emit("data:deleted"), this.destroy(); }); } activate() { return r(this, null, function* () { if (this.activing) return new Promise((i) => { this.addEventListener("activated", () => { i(!0); }, !0); }); if (!this._id) throw Error("session is not avalible. id is undefined"); this.activing = !0; const e = this._id; return this._dataLoading = !0, this._dataLoaded = !1, this.data = yield this.cvforceApi.getSession(this.applicationId, e).then((i) => r(this, null, function* () { return this._promptVariables = yield this.cvforceApi.getApplicationIdSessionIdPromptVariables(this.applicationId, e), i; })), this.messages.length || (this.messages = [], yield this.fetchMessages()), this.event.emit("data:loaded", this.data), this.event.emit("data:updated", this.data), this._dataLoaded = !0, this._dataLoading = !1, this.inactivating = !1, this.frozen = !0, yield this.prepareConnection(), this.event.emit("activated", this.socket), this.active = !0, this.activing = !1, this; }); } inactivate(e = !1) { return this.socket ? this._frozen ? (e || (this.inactivating = !0), this.activing = !1, !0) : (this.socket.close(), this.socket = null, this.event.emit("inactivated"), this.active = !1, this.activing = !1, !0) : (this.activing = !1, !0); } //TODO: remove mcp server // abstract removeMcpServers(mcpServers: MCPConfig): Promise<boolean>; get dataLoaded() { return this._dataLoaded; } get messagesLoading() { return this._loadingMessages; } send(t) { return r(this, arguments, function* (e, i = { stream: A.stream }) { var m; try { this.validateConfigs(); } catch (a) { console.error(a); } const { stream: n = A.stream, referenceFirst: o = !1 } = i, l = e.uuid || M(), d = ((m = e.text) == null ? void 0 : m.trim()) || "", s = e.fileIds; if (!d && /*!files.length &&*/ !(s != null && s.length)) { const a = new Error( "failed at sending empty content" /* SendingEmptyContent */ // { // cause: SESSION_ERROR_CAUSE[SessionErrors.SendingEmptyContent], // } ); return u.error(a), this.event.emit("error", a), !1; } if (!this._id) { const a = new Error( "session id is not defined" /* IdUndefined */ // { // cause: SESSION_ERROR_CAUSE[SessionErrors.IdUndefined], // } ); return u.error(a), this.event.emit("error", a), !1; } if (this.frozen) { const a = new Error( "session is frozen" /* Frozen */ // { // cause: { code: -1, message: 'session is frozen.' }, // } ); return u.error(a), this.event.emit("error", a), !1; } this.frozen = !0, this._stopAnswer = !1; const p = { sessionId: this.id, role: 2, content: d, messageId: l, reference: [], searchReference: [], messageTime: "" }, g = { content: d, stream: n, requestId: l, fileIds: s, referenceFirst: o }; this.pushMessage(p), this.event.emit("question:content", d); const y = (a) => r(this, null, function* () { if (a.role !== L.Robot) return; switch (this.removeEventListener("answer:finish", y), this._id && a.content && a.index && a.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, { finialMessage: a.content }), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", { finialMessage: a.content })), yield this.fillMessageReference(a), a.code) { case 11915: a.content = "请您换个问题问我吧~", a.index = -1, a.isSensitive = !0, a.isLoading = !1, a.sessionId = this._id || "", this.event.emit("message:error", a); break; case 11916: a.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", a.index = -1, a.isSensitive = !0, a.isLoading = !1, a.sessionId = this._id || "", this.event.emit("message:error", a); break; case 200: a.index = -1, a.isLoading = !1; break; default: a.content = a.message || "未知错误", a.isLoading = !1, a.index = -1, this.event.emit("message:error", a); } this.getRelatedQuesion(a), a.isLoading = !1; let w = this.messages[this.messages.length - 1]; if (w && w.role === L.Robot) { const v = f(f({}, w), a); this.messages[this.messages.length - 1] = v, this.event.emit("message:updated", v); } else this.pushMessage(a); this.frozen = !1; }); return this.addEventListener("answer:finish", y), this.sendQuestion(g, I(f({}, i), { stream: n, referenceFirst: o })), p; }); } getRelatedQuesion(e) { return r(this, null, function* () { var o, l; if (!e.content || e.isSensitive || e.code !== 200 || !((o = this.application) != null && o.arrange[0].recommendQuestionOpen)) return e; this.event.emit("message:relatedQuesion:loading", e); const i = yield this.cvforceApi.postGeneques({ content: e.content }).catch((d) => null); if (!i) { const d = [], s = I(f({}, e), { relatedQuesion: d, index: -3, isLoading: !1 }); return this.event.emit("message:relatedQuesion:loaded", { relatedQuesion: d }), this.event.emit("message:updated", s), s; } const t = (l = i.content) == null ? void 0 : l.split("*").map(O).map(C).filter((d) => !!d); e.relatedQuesion = t, e.index = -3, e.isLoading = !1; const n = I(f({}, e), { relatedQuesion: t, index: -3, isLoading: !1 }); return this.event.emit("message:relatedQuesion:loaded", n), this.event.emit("message:updated", n), this._id && this.cvforceApi.patchSessionsId(this._applicationId, this._id, { lastRelatedQuestion: t }), n; }); } fillMessageReference(e) { return r(this, null, function* () { const i = yield Promise.all(e.reference.map((t) => this.cvforceApi.getKnowledgeIdFileId(t.knowledgeId, t.docId))); return e.reference = e.reference.map((t, n) => f(f({}, t), i[n])), this.event.emit("message:updated", e), e; }); } answerAgain() { return r(this, arguments, function* (e = { stream: A.stream }) { const i = this.messages[this.messages.length - 1]; if (!i) throw Error("message list is empty"); this.frozen = !0, this._stopAnswer = !1; const { stream: t = A.stream } = e; let n; i.role !== L.User && (n = e.oldMessageId || i.messageId, this.messages.splice(this.messages.length - 1, 1), this.event.emit("message:remove", i), this.event.emit("history:remove", i)); const o = this.messages[this.messages.length - 1], l = { content: o.content, fileIds: o.fileIds, requestId: o.messageId, stream: t, oldMessageId: n }, d = (s) => r(this, null, function* () { if (s.role === L.Robot && s.index === -1) { switch (this._id && s.content && s.index && s.index < 0 && (this.data = yield this.cvforceApi.patchSessionsId(this.applicationId, this._id, { finialMessage: s.content }), this.event.emit("data:updated", this.data), this.event.emit("data:updated:finialMessage", { finialMessage: s.content })), yield this.fillMessageReference(s), s.code) { case 11915: s.content = "请您换个问题问我吧~", s.index = -1, s.isSensitive = !0, s.isLoading = !1, s.sessionId = this._id || "", this.event.emit("message:error", s); break; case 11916: s.content = "尊敬的用户您好,让我们换个话题再聊聊吧。", s.index = -1, s.isSensitive = !0, s.isLoading = !1, s.sessionId = this._id || "", this.event.emit("message:error", s); break; case 200: s.index = -1, s.isLoading = !1; break; default: s.content = s.message || "未知错误", s.isLoading = !1, s.index = -1, this.event.emit("message:error", s); } this.getRelatedQuesion(s), this.removeEventListener("message:content", d), this.frozen = !1; } }); this.addEventListener("message:content", d), this.sendQuestion(l, I(f({}, e), { stream: t })); }); } setTop(e) { return r(this, null, function* () { if (!this._id) throw Error("session is not avalible. id is undefined"); this.data = yield this.cvforceApi.patchSessionsId(this._applicationId, this._id, { top: e }), this.event.emit("data:updated", this.data), this.event.emit("data:updated:top", { top: e }); }); } /** * 暂停当前会话的回答过程 * 该方法会停止当前回答,标记回答停止状态,处理正在进行的消息和操作, * 并根据情况更新消息内容或添加新的系统消息。 * @returns 返回当前会话实例 */ suspend() { var t; this.stopAnswer(), this.frozen = !1, this._stopAnswer = !0; let e = this.messages[this.messages.length - 1]; const i = { messageId: M(), // 生成唯一的消息 ID isLoading: !1, // 标记消息已停止加载 index: -2, // 消息索引,用于标识特殊状态 messageTime: B(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:ss:mm"), // 消息时间 content: "", // 消息内容 role: L.System, // 消息角色为系统消息 sessionId: this.id || "", // 会话 ID reference: [], // 引用列表 searchReference: [] // 搜索引用列表 }; if (e && !e.messageId) { if (e.index === -2) return; if (e.actions) { const n = e.actions[((t = e.actions) == null ? void 0 : t.length) - 1]; if (n) { const o = n[n.length - 1]; if (o && (o.type === "BEFORE_CALL" || o.type === "FUNCTION_CALL")) { const { pluginName: l, pluginCode: d, funcCode: s, funcName: p } = o.data; n.push({ data: { pluginName: l, pluginCode: d, funcCode: s, funcName: p, step: o.data.step + 1 }, type: "ABORT" }), this.event.emit("message:action", { messageId: e.messageId, action: n }); } } } e = f(f({}, e), i), this.event.emit("message:content", e), this.event.emit("message:updated", e); } else this.pushMessage(i), this.event.emit("message:content", i); return this; } /** * 异步提交评论信息 * @param params - 评论参数,包含评论相关的信息,如消息 ID 等 * @returns 返回调用 API 提交评论后的响应结果 */ postMessageComment(e) { return r(this, null, function* () { if (!this._applicationId || !this._id) throw Error("session is not avalible. id is undefined"); return this.cvforceApi.postComments(this._applicationId, this._id, e).then((i) => { const t = this.messages.find((n) => n.messageId === e.messageId); return t && (t.comment = e, this.event.emit("message:comment", t)), i; }); }); } /** * 清除当前会话的上下文信息 * 该方法会调用 API 清除指定会话的上下文,并在操作成功后触发相应事件 * @returns 如果会话 ID 存在且清除操作成功,返回一个解析为 true 的 Promise;若会话 ID 不存在,抛出错误 */ clear() { if (!this._id) throw Error("session is not avalible. id is undefined"); return this.cvforceApi.patchApplicationIdSessionsIdContext(this._applicationId, this._id).then(() => (this.event.emit("context:clear"), !0)); } /** * 销毁当前会话实例,执行清理操作并触发销毁事件 * @returns 返回当前会话的 ID,如果会话 ID 不存在则返回空字符串 */ destroy() { var e; return this._ready = !1, this.event.removeAllListeners(), (e = this.socket) == null || e.disconnect(), this.socket = null, this.event.emit("destroy", { applicationId: this._applicationId, sessionId: this._id }), this._id || ""; } } export { U as SessionErrors, X as default };