UNPKG

@glodon-aiot/agent-cli-sdk

Version:

aiot agent client js sdk

302 lines (301 loc) 9.88 kB
var A = Object.defineProperty, O = Object.defineProperties; var D = Object.getOwnPropertyDescriptors; var v = Object.getOwnPropertySymbols; var P = Object.prototype.hasOwnProperty, $ = Object.prototype.propertyIsEnumerable; 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) => { for (var t in c || (c = {})) P.call(c, t) && C(i, t, c[t]); if (v) for (var t of v(c)) $.call(c, t) && C(i, t, c[t]); return i; }, R = (i, c) => O(i, D(c)); var k = (i, c, t) => (C(i, typeof c != "symbol" ? c + "" : c, t), t); var f = (i, c, t) => new Promise((o, g) => { var d = (e) => { try { m(t.next(e)); } catch (l) { g(l); } }, s = (e) => { try { m(t.throw(e)); } catch (l) { g(l); } }, m = (e) => e.done ? o(e.value) : Promise.resolve(e.value).then(d, s); m((t = t.apply(i, c)).next()); }); import { IHistoryRole as T, ChatMessageRole as _ } from "@glodon-aiot/apis"; import x from "./Logger.mjs"; import J from "./Session.mjs"; import N from "dayjs"; import { configuration as M } from "./Config.mjs"; import { fetchEventSource as p } from "./api/fetchEventSource/fetch.mjs"; const I = new x("[DialogSession] "); class H extends J { constructor(t, o = "", g, d, s, m) { super(t, o, g, d, s, m); k(this, "ctrl", new AbortController()); } prepareConnection() { return new Promise((t) => f(this, null, function* () { this._ready = !0, this.event.emit("ready"), this.frozen = !1, t(!0); })); } postChatV2EventSource(t, o, g, d, s, m) { return f(this, null, function* () { const e = { code: 0, // messageId: uuidv4(), content: "", role: T.Robot, sessionId: o, reference: [], searchReference: [], messageTime: "", llmModel: "", comment: { isAgree: 0 }, isReplying: !1 }, l = R(u({}, g), { stream: M.stream }); p(`${M.apiRoot}/chat/v2/chat?applicationId=${t}&sessionId=${o}`, { method: "POST", headers: { Authorization: `Bearer ${this.token}`, "Content-Type": "application/json" }, body: JSON.stringify(l), openWhenHidden: !0, signal: d, onopen(n) { return n.ok && s != null && s.onopen && s.onopen(), Promise.resolve(); }, onmessage(n) { if (!n.data.includes('{"code": ')) { let a = {}; try { a = JSON.parse(n.data); } catch (w) { } (a == null ? void 0 : a.mtype) === "search" ? e.searchReference.push(a) : e.content = e.content + n.data; const h = { content: e.content, role: 1, reference: e.reference, searchReference: e.searchReference, messageId: e.messageId, sessionId: o, messageTime: N(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"), comment: { isAgree: 0 }, isReplying: !0 }; return (s == null ? void 0 : s.onmessage) && s.onmessage(h); } const r = JSON.parse(n.data); 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) { const a = { code: e.code, content: e.content, role: 1, reference: e.reference, searchReference: e.searchReference, messageId: e.messageId, sessionId: o, messageTime: e.messageTime, isReplying: !0 }; s != null && s.onmessage && s.onmessage(a); } else return s != null && s.onclose && s.onclose(e), m && m({ code: e.code, message: e.message }); }, onerror() { s != null && s.onerror && s.onerror(); }, onclose() { const n = { code: e.code, content: e.content, role: 1, reference: e.reference, searchReference: e.searchReference, messageId: e.messageId, sessionId: o, messageTime: e.messageTime, comment: { isAgree: 0 }, isReplying: !1, index: -1 }; s != null && s.onclose && s.onclose(n); } }); }); } postChatV3EventSource(t, o, g, d, s, m) { return f(this, null, function* () { const e = { code: 0, // messageId: uuidv4(), content: "", role: T.Robot, sessionId: o, reference: [], searchReference: [], messageTime: "", llmModel: "", comment: { isAgree: 0 }, isReplying: !1, reasoningContent: "", thinkingSecs: "", thinkingStatus: "", searchingStatus: "" }, l = R(u({}, g), { stream: M.stream }); p(`${M.apiRoot}/chat/v3/chat?applicationId=${t}&sessionId=${o}`, { method: "POST", headers: { Authorization: `Bearer ${this.token}`, "Content-Type": "application/json" }, body: JSON.stringify(l), openWhenHidden: !0, signal: d, onopen(n) { return n.ok && s != null && s.onopen && s.onopen(), Promise.resolve(); }, onmessage(n) { if (!n.data && !n.event && !n.id && !n.retry) return; let r = {}; try { r = JSON.parse(n.data); } catch (y) { console.error("parse eventData to JSON failed. ", n.data); } if (r.code !== 200) return e.index = -1, e.code = r.code, e.message = r.message, s != null && s.onmessage && s.onmessage(e), m && m({ code: r.code, message: r.message }); const a = r.data.type, h = r.data.payload; e.isReplying = !0; const w = () => { if (!e.searchingStatus) { e.searchingStatus = "start"; return; } if (e.searchingStatus === "start") { e.searchingStatus = "end"; return; } }; if (a === "beforeSearch" && w(), a === "searchOver" && w(), a === "search" && e.searchReference.push(h), a === "thinking") { e.thinkingStatus = "start"; const S = h.choices[0]; e.reasoningContent = e.reasoningContent + S.delta.reasoningContent; } if (a === "thinkingOver") { e.thinkingStatus = "end"; const y = h.thinkingSecs; e.thinkingSecs = y; } if (a === "text") { const S = h.choices[0]; e.content = e.content + S.delta.content; } if (a === "reference" && (e.reference = h), a === "result") { const y = h.messageId, S = h.llmModel, b = h.messageTime; e.messageId = y, e.llmModel = S, e.messageTime = b, e.sessionId = o; } a === "over" ? e.index = -1 : (e.code = r.code, e.message = r.message), s != null && s.onmessage && s.onmessage(e); }, onerror() { s != null && s.onerror && s.onerror(); }, onclose() { const n = R(u({}, e), { isReplying: !1 }); s != null && s.onclose && s.onclose(n); } }); }); } sendQuestion(t, o) { return f(this, null, function* () { var l; const { content: g, fileIds: d, oldMessageId: s } = t, { stream: m, referenceFirst: e } = o; if (this.active || (yield this.activate()), this._id) { let n = { content: g, fileIds: d, referenceFirst: e, oldMessageId: s, promptVariables: this.promptVariables.reduce((r, a) => R(u({}, r), { [a.key]: a.value }), {}), knowledges: (l = this.knowledges) == null ? void 0 : l.map((r) => r.id) }; this.ctrl = new AbortController(), this.postChatV3EventSource(this._applicationId, this._id, n, this.ctrl.signal, { onopen: () => { I.log("open", this.ctrl); }, onmessage: (r) => { if (this._stopAnswer) return; let a = this.messages[this.messages.length - 1]; 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)); }, onclose: (r) => f(this, null, function* () { this._stopAnswer || (I.log(r, "onclose"), this.event.emit("answer:finish", r)); }), onerror: () => { this._stopAnswer || (this.event.emit("message:error", ""), this.frozen = !1); } }); } }); } stopAnswer() { this.ctrl.abort(); } registerMcpFromClientConfig() { return f(this, null, function* () { throw new Error("MCP registration is not supported by DialogSession"); }); } addMcpServers(t) { return f(this, null, function* () { throw new Error("MCP registration is not supported by DialogSession"); }); } listMcpServers() { return f(this, null, function* () { throw new Error("MCP registration is not supported by DialogSession"); }); } } export { H as default };