UNPKG

@gaiot/apis

Version:

aiot apis

511 lines (510 loc) 15 kB
var P = Object.defineProperty, K = Object.defineProperties; var E = Object.getOwnPropertyDescriptors; var I = Object.getOwnPropertySymbols; var b = Object.prototype.hasOwnProperty, F = Object.prototype.propertyIsEnumerable; var T = (m, e, t) => e in m ? P(m, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : m[e] = t, f = (m, e) => { for (var t in e || (e = {})) b.call(e, t) && T(m, t, e[t]); if (I) for (var t of I(e)) F.call(e, t) && T(m, t, e[t]); return m; }, S = (m, e) => K(m, E(e)); var r = (m, e, t) => new Promise((o, n) => { var s = (i) => { try { g(t.next(i)); } catch (a) { n(a); } }, h = (i) => { try { g(t.throw(i)); } catch (a) { n(a); } }, g = (i) => i.done ? o(i.value) : Promise.resolve(i.value).then(s, h); g((t = t.apply(m, e)).next()); }); import O from "axios"; import { BaseApi as v } from "../base/index.mjs"; import { m as $ } from "../node_modules/@gaiot/minio/dist/minio-zj.mjs"; import { IHistoryRole as k, AecKnowledgeFileStatus as R } from "./model.mjs"; import { IFileOrigin as G, ISourceType as V, IThinkingStatus as W, IversionInfo as X } from "./model.mjs"; import { fetchEventSource as w } from "../utils/fetchEventSource/fetch.mjs"; import C from "dayjs"; import { PROMPT_SEARCH_MAX_LENGTH as H } from "./cosntant.mjs"; import { PROMPT_TITLE_MAX_LENGTH as q } from "./cosntant.mjs"; import A from "../node_modules/uuid/dist/esm-browser/v4.mjs"; class J extends v { constructor(e, t, o) { super(e != null ? e : O.create(), o), this.axiosInstance = e, this.getToken = t; } // 会话 getSessions() { return r(this, null, function* () { return this.get("/chatSession"); }); } postSession(e) { return r(this, null, function* () { return this.post("/chatSession", e); }); } getSession(e) { return r(this, null, function* () { return this.get(`/chatSession/${e}`); }); } updateSession(e, t) { return r(this, null, function* () { return this.patch(`/chatSession/${e}`, t); }); } deleteSession(e) { return r(this, null, function* () { return this.delete(`/chatSession/${e}`); }); } // 会话文件 getSessionKnowledgeFiles(e, t) { return r(this, null, function* () { return this.get(`chatSession/${e}/file`, t); }); } postSessionKnowledgeFile(e, t) { return r(this, null, function* () { return this.post(`chatSession/${e}/file`, t); }); } deleteSessionKnowledgeFiles(e, t) { return r(this, null, function* () { return this.delete(`chatSession/${e}/file`, t); }); } getSessionKnowledgeFile(e, t) { return r(this, null, function* () { return this.get(`chatSession/${e}/file/${t}`); }); } deleteSessionKnowledgeFile(e, t) { return r(this, null, function* () { return this.delete(`chatSession/${e}/file/${t}`); }); } // 会话历史 getSessionChatHistory(e, t) { return r(this, null, function* () { return this.get(`/chatSession/${e}/history`, t); }); } // 聊天 postMessage(e, t, o) { return r(this, null, function* () { return this.post(`/config/tenantApplication?applicationId=${e}&sessionId=${t}`, o); }); } // 评价 postComments(e, t) { return r(this, null, function* () { return this.post(`/chatSession/${e}/comments`, t); }); } // 租户应用 listApplications() { return r(this, null, function* () { return this.list("/applications"); }); } getApplication() { return r(this, null, function* () { return this.get("/config/tenantApplication"); }); } postApplication(e) { return r(this, null, function* () { return this.post("/config/tenantApplication", { applicationId: e }); }); } // 推荐问题 getRecommendQues() { return r(this, null, function* () { return this.get("/recommendQues"); }); } postRecommendQues(e) { return r(this, null, function* () { return this.post("/recommendQues", e); }); } patchRecommendQues(e, t) { return r(this, null, function* () { return this.patch(`/recommendQues/${e}`, t); }); } delRecommendQues(e) { return r(this, null, function* () { return this.delete(`/recommendQues/${e}`); }); } // 文件上传 getUploadTicket(e) { return r(this, null, function* () { return this.get("/upload/ticket", e); }); } uploadMinioFile(e, t) { return r(this, null, function* () { return new Promise((o, n) => { const s = new FileReader(); s.readAsArrayBuffer(t), s.onload = (h) => { $.initMinio({ endPoint: e.endpoints[0], useSSL: e.useSSL, accessKey: e.accessKey, secretKey: e.secretKey }); const g = Buffer.from(h.target.result), i = { "Content-Type": "application/octet-stream" }; $.putObjectBuffer(e.bucket, e.fileKeys[0], g, i, (a) => { if (a) throw console.error(a), Error(a); o(e.fileKeys[0]); }); }; }); }); } uploadFile(e, t) { return r(this, null, function* () { const o = { files: [t.name], isv: "minio", knowledgeId: e }; return this.postUploadTicket(o).then((n) => this.uploadMinioFile(n, t)).catch((n) => { throw Error(n); }); }); } uploadFiles(e, t) { return r(this, null, function* () { const o = { files: t.map((n) => n.name), isv: "minio", knowledgeId: e }; return this.postUploadTicket(o).then((n) => Promise.all(t.map((s) => this.uploadMinioFile(n, s)))).catch((n) => { throw Error(n); }); }); } getKnowledge(e, t) { return r(this, null, function* () { return this.get("/knowledge", { docId: e, name: t }); }); } // getKnowledgeIdFileId(e, t) { return r(this, null, function* () { return this.get(`/knowledge/${e}/file/${t}`); }); } updateKnowledgeIdFileId(e, t, o) { return r(this, null, function* () { return this.patch(`/chatSession/${e}/file/${t}`, o); }); } getKnowledgeIdFileIdData(e, t, o) { return r(this, null, function* () { return this.get(`/knowledge/${e}/file/${t}/data/${o}`); }); } // 文件详情 checkFileReady(e, t, o = () => !0, n = 1e4, s) { return r(this, null, function* () { return new Promise((h, g) => { const i = () => { if (!o()) { clearInterval(a); return; } t ? this.getSessionKnowledgeFile(e, t).then((c) => { if (s == null || s(c), c.status === R.Success && (clearInterval(a), h(c)), c.status === R.Failed) { clearInterval(a); const d = "该文件无法读取,请上传可编辑pdf,doc,docx文档"; g(Error(d, { cause: { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore code: -101, message: d, data: c } })); } }).catch((c) => { clearInterval(a), g(c); }) : h(null); }, a = setInterval(i, n); i(); }); }); } postChat(e, t) { return r(this, null, function* () { return this.post("/chat", e, t); }); } // IChatParam postChatV2EventSource(e, t, o, n) { return r(this, null, function* () { var i; const s = { code: 0, messageId: A(), content: "", role: k.Robot, sessionId: "", reference: [], searchReference: [], messageTime: "", llmModel: "", comment: { isAgree: 0 }, isReplying: !1 }, h = yield this.getToken(), g = S(f({}, t), { stream: !0 }); w(`${(i = this.axiosInstance) == null ? void 0 : i.defaults.baseURL}/chat?sessionId=${e}`, { method: "POST", headers: { Authorization: `Bearer ${h}`, "Content-Type": "application/json" }, body: JSON.stringify(g), openWhenHidden: !0, signal: o, onopen(a) { return console.log("onopen"), a.ok && n != null && n.onopen && n.onopen(), Promise.resolve(); }, onmessage(a) { if (!a.data && !a.event && !a.id && !a.retry) return; if (!a.data.includes('{"code": ')) { let c = {}; try { c = JSON.parse(a.data); } catch (u) { } (c == null ? void 0 : c.mtype) === "search" ? s.searchReference.push(c) : s.content = s.content + a.data; const d = { content: s.content, role: 1, reference: s.reference, searchReference: s.searchReference, messageId: s.messageId, sessionId: e, messageTime: C(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"), comment: { isAgree: 0 }, isReplying: !0 }; return (n == null ? void 0 : n.onmessage) && n.onmessage(d); } const l = JSON.parse(a.data); if (s.reference = l.data.reference, s.messageTime = l.data.messageTime, s.messageId = l.data.messageId, s.llmModel = l.data.llmModel, s.code = l.code, l.code === 200) { const c = { code: s.code, content: s.content, role: 1, reference: s.reference, searchReference: s.searchReference, messageId: s.messageId, sessionId: e, messageTime: s.messageTime, isReplying: !0 }; n != null && n.onmessage && n.onmessage(c); } }, onerror() { n != null && n.onerror && n.onerror(); }, onclose() { const a = { code: s.code, content: s.content, role: 1, reference: s.reference, searchReference: s.searchReference, messageId: s.messageId, sessionId: e, messageTime: s.messageTime, comment: { isAgree: 0 }, isReplying: !1 }; n != null && n.onclose && n.onclose(a); } }); }); } postChatV3EventSource(e, t, o, n) { return r(this, null, function* () { var i, a; const s = { code: 0, messageId: A(), content: "", role: k.Robot, sessionId: "", reference: [], searchReference: [], messageTime: "", llmModel: "", comment: { isAgree: 0 }, isReplying: !1, reasoningContent: "", thinkingSecs: "", thinkingStatus: "" }, h = yield this.getToken(), g = S(f({}, t), { stream: !0 }); w(`${(a = (i = this.axiosInstance) == null ? void 0 : i.defaults.baseURL) == null ? void 0 : a.replace("/v1", "/v3")}/chat?sessionId=${e}`, { method: "POST", headers: { Authorization: `Bearer ${h}`, "Content-Type": "application/json" }, body: JSON.stringify(g), openWhenHidden: !0, signal: o, onopen(l) { return console.log("onopen"), l.ok && n != null && n.onopen && n.onopen(), Promise.resolve(); }, onmessage(l) { if (!l.data && !l.event && !l.id && !l.retry) return; let c = {}; try { c = JSON.parse(l.data); } catch (p) { console.log("eventData", l.data); } const d = c.data.type, u = c.data.payload; if (s.isReplying = !0, d === "error" && (s.code = c.code, s.message = c.message), d === "search" && (s.code = c.code, s.message = c.message, s.searchReference.push(u)), d === "thinking") { s.code = c.code, s.message = c.message, s.thinkingStatus = "start"; const y = u.choices[0]; s.reasoningContent = s.reasoningContent + y.delta.reasoningContent; } if (d === "thinkingOver") { s.code = c.code, s.message = c.message, s.thinkingStatus = "end"; const p = u.thinkingSecs; s.thinkingSecs = p; } if (d === "text") { s.code = c.code, s.message = c.message; const y = u.choices[0]; s.content = s.content + y.delta.content; } if (d === "reference" && (s.code = c.code, s.message = c.message, s.reference = u), d === "result") { s.code = c.code, s.message = c.message; const p = u.messageId, y = u.llmModel, M = u.messageTime; s.messageId = p, s.llmModel = y, s.messageTime = M, s.sessionId = e; } n != null && n.onmessage && n.onmessage(s); }, onerror() { n != null && n.onerror && n.onerror(); }, onclose() { const l = S(f({}, s), { isReplying: !1 }); n != null && n.onclose && n.onclose(l); } }); }); } getResourceToken(e) { return r(this, null, function* () { return this.get("/resourceToken", e); }); } postUploadTicket(e) { return r(this, null, function* () { return this.post("/upload/ticket", e); }); } //常用提示词 postPrompts(e) { return r(this, null, function* () { return this.post("/prompts", e); }); } listPrompt(e) { return r(this, null, function* () { return e != null && e.title && e.title.length > H ? [] : this.get("/prompts", e); }); } getPrompt(e) { return r(this, null, function* () { return this.get(`/prompts/${e}`); }); } updatePrompt(e, t) { return r(this, null, function* () { return this.put(`/prompts/${e}`, t); }); } deletePrompt(e) { return r(this, null, function* () { return this.delete(`/prompts/${e}`); }); } chatSessionIdContext(e) { return r(this, null, function* () { return this.patch(`/chatSession/${e}/context`, {}); }); } getBaseModelInfo(e) { return r(this, null, function* () { return this.get("/data/v1/baseModelInfo", { serviceId: e }); }); } getApplicationId(e) { return r(this, null, function* () { return this.get(`/applications/${e}`); }); } getAppService() { return r(this, null, function* () { return this.get("/appservice"); }); } } export { J as AECPilotApi, R as AecKnowledgeFileStatus, G as IFileOrigin, k as IHistoryRole, V as ISourceType, W as IThinkingStatus, X as IversionInfo, H as PROMPT_SEARCH_MAX_LENGTH, q as PROMPT_TITLE_MAX_LENGTH };