UNPKG

@gaiot/apis

Version:

aiot apis

608 lines (607 loc) 19.9 kB
var S = Object.defineProperty, A = Object.defineProperties; var w = Object.getOwnPropertyDescriptors; var h = Object.getOwnPropertySymbols; var R = Object.prototype.hasOwnProperty, K = Object.prototype.propertyIsEnumerable; var f = (p, e, t) => e in p ? S(p, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : p[e] = t, y = (p, e) => { for (var t in e || (e = {})) R.call(e, t) && f(p, t, e[t]); if (h) for (var t of h(e)) K.call(e, t) && f(p, t, e[t]); return p; }, $ = (p, e) => A(p, w(e)); var s = (p, e, t) => new Promise((a, i) => { var r = (d) => { try { m(t.next(d)); } catch (l) { i(l); } }, n = (d) => { try { m(t.throw(d)); } catch (l) { i(l); } }, m = (d) => d.done ? a(d.value) : Promise.resolve(d.value).then(r, n); m((t = t.apply(p, e)).next()); }); import T from "axios"; import { BaseApi as b } from "../base/index.mjs"; import { m as I } from "../node_modules/@gaiot/minio/dist/minio-zj.mjs"; import { IHistoryRole as P, AecKnowledgeFileStatus as v } from "../aecpilot/model.mjs"; import { fetchEventSource as k } from "../utils/fetchEventSource/fetch.mjs"; import M from "dayjs"; import { Buffer as F } from "buffer"; import { PROMPT_SEARCH_MAX_LENGTH as B } from "../aecpilot/cosntant.mjs"; import E from "../node_modules/uuid/dist/esm-browser/v4.mjs"; globalThis.Buffer = F; class Y extends b { constructor(e, t, a) { super(e != null ? e : T.create(), a), this.axiosInstance = e, this.getToken = t; } // async postChatV2EventSource1( // applicationId: string, // sessionId: string, // params: any, // signal: AbortSignal, // options: { onopen?: any; onmessage?: any; onerror?: any; onclose?: any } // ) { // const tempMessage: any = { // code: 0, // messageId: uuidv4(), // content: '', // role: IHistoryRole.Robot, // sessionId: '', // reference: [], // searchReference: [], // messageTime: '', // llmModel: '', // comment: { // isAgree: 0, // }, // isReplying: false, // }; // const token = await this.getToken(); // const body = { ...params, stream: true }; // fetchEventSource( // `https://aiot-dev.glodon.com/api/cvforcepd/chat/v2/chat?applicationId=${applicationId}&sessionId=${sessionId}`, // { // method: 'POST', // headers: { // Authorization: `Bearer ${token}`, // 'Content-Type': 'application/json', // }, // body: JSON.stringify(body), // openWhenHidden: true, // signal: signal, // onopen(response) { // console.log('onopen'); // if (response.ok) { // options?.onopen && options.onopen(); // } // return Promise.resolve(); // }, // onmessage(event) { // if (!event.data.includes('{"code": ')) { // let eventData: any = {}; // try { // eventData = JSON.parse(event.data); // } catch (error) {} // if (eventData?.mtype === 'search') { // tempMessage.searchReference.push(eventData); // } else { // tempMessage.content = tempMessage.content + event.data; // } // const responseMsg: any = { // content: tempMessage.content, // role: 1, // reference: tempMessage.reference, // searchReference: tempMessage.searchReference, // messageId: tempMessage.messageId, // sessionId: sessionId, // messageTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'), // comment: { // isAgree: 0, // }, // isReplying: true, // }; // return options?.onmessage && options.onmessage(responseMsg); // } // const structure = JSON.parse(event.data); // tempMessage.reference = structure.data.reference; // tempMessage.searchReference = structure.data.searchReference; // tempMessage.messageTime = structure.data.messageTime; // tempMessage.messageId = structure.data.messageId; // tempMessage.llmModel = structure.data.llmModel; // tempMessage.content = structure.data.content; // tempMessage.code = structure.code; // if (structure.code === 200) { // const responseMsg: any = { // code: tempMessage.code, // content: tempMessage.content, // role: 1, // reference: tempMessage.reference, // searchReference: tempMessage.searchReference, // messageId: tempMessage.messageId, // sessionId: sessionId, // messageTime: tempMessage.messageTime, // isReplying: true, // }; // options?.onmessage && options.onmessage(responseMsg); // } else { // return notification.error({ // message: structure.message, // }); // } // }, // onerror() { // options?.onerror && options.onerror(); // }, // onclose() { // const responseMsg: any = { // code: tempMessage.code, // content: tempMessage.content, // role: 1, // reference: tempMessage.reference, // searchReference: tempMessage.searchReference, // messageId: tempMessage.messageId, // sessionId: sessionId, // messageTime: tempMessage.messageTime, // comment: { // isAgree: 0, // }, // isReplying: false, // }; // options?.onclose && options.onclose(responseMsg); // }, // } // ); // } // async getKnowledgeIdFileIdData(id: string, fid: string, did: string) { // return this.get<IFileDataParam[]>( // `/knowledge/${id}/file/${fid}/data/${did}` // ); // } // async getKnowledgeIdFileId(id: string, fid: string) { // return this.get<IFile>(`/knowledge/${id}/file/${fid}`); // } // async getKnowledgeId(id: string) { // return this.get<IKnowledge>(`/knowledge/${id}`); // } // async getSessionKnowledgeFile(sessionId: string, fileId: string) { // return this.get<KnowledgeFileMetadata>( // `chatSession/${sessionId}/file/${fileId}` // ); // } // async sleep(ms: number) { // return new Promise((reslove) => { // setTimeout(() => { // reslove(''); // }, ms); // }); // } // async uploadFiles(path: string, files: File[]) { // const parmas = { // files: files.map((i) => i.name), // isv: 'minio', // knowledgeId: path, // }; // return this.postUploadTicket(parmas) // .then((data) => // Promise.all(files.map((file) => this.uploadMinioFile(data.minio, file))) // ) // .catch((e) => { // throw Error(e); // }); // } // async postFile(path: string, file: File, type?: number) { // const parmas = { // files: [file.name], // isv: 'minio', // knowledgeId: path, // type: type, // }; // return this.postTicket(parmas) // .then((data) => this.uploadMinioFile(data.minio, file)) // .catch((e) => { // throw Error(e); // }); // } // async postTicket(params: IUploadParam) { // return this.post<IUploadTicket>(`/ticket`, params); // } // async uploadFile(path: string, file: File) { // const parmas = { // files: [file.name], // isv: 'minio', // knowledgeId: path, // }; // return this.postUploadTicket(parmas) // .then((data) => this.uploadMinioFile(data.minio, file)) // .catch((e) => { // throw Error(e); // }); // } // async postUploadTicket(params: IUploadParam) { // return this.post<IUploadTicket>(`/upload/ticket`, params); // } // async uploadMinioFile(minioConfig: MinioConfig, file: File) { // return new Promise<string>((resolve, reject) => { // const fr = new FileReader(); // fr.readAsArrayBuffer(file); // fr.onload = (e: any) => { // initMinio({ // endPoint: minioConfig.endpoints[0], // useSSL: minioConfig.useSSL, // accessKey: minioConfig.accessKey, // secretKey: minioConfig.secretKey, // }); // const buffer = Buffer.from(e.target.result); // const metaData = { // 'Content-Type': 'application/octet-stream', // }; // putObjectBuffer( // minioConfig.bucket, // minioConfig.fileKeys[0], // buffer, // metaData, // (err) => { // if (err) { // console.error(err); // throw Error(err); // } else { // resolve(minioConfig.fileKeys[0]); // } // } // ); // }; // }); // } // 接口 getBaseModelInfo(e) { return s(this, null, function* () { return this.get("/data/v1/baseModelInfo", { serviceId: e }); }); } postGeneques(e) { return s(this, null, function* () { return this.post("/data/v1/geneques", e); }); } getApplicationId(e) { return s(this, null, function* () { return this.get(`/data/v1/applications/${e}`); }); } getApplicationIdSessions(e, t) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/sessionsa`, t); }); } postApplicationIdSession(e) { return s(this, null, function* () { return this.post(`/data/v1/application/${e.applicationId}/sessions`, e); }); } getApplicationIdSessionsId(e, t) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/sessions/${t}`); }); } patchApplicationIdSessionsId(e, t, a) { return s(this, null, function* () { return this.patch(`/data/v1/application/${e}/sessions/${t}`, a); }); } delApplicationIdSessionsId(e, t) { return s(this, null, function* () { return this.delete(`/data/v1/application/${e}/sessions/${t}`); }); } getApplicationIdPromptVariables(e, t) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/promptVariables`, t); }); } getApplicationIdSessionIdPromptVariables(e, t) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/session/${t}/promptVariables`); }); } patchApplicationIdSessionpromptVariablesId(e, t, a, i) { return s(this, null, function* () { return this.patch(`/data/v1/application/${e}/session/${t}/promptVariables/${a}`, i); }); } deleteSessionKnowledgeFiles(e, t) { return s(this, null, function* () { return this.delete(`/data/v1/chatSession/${e}/file`, t); }); } getRecommendQues(e) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/recommendQues`); }); } postRecommendQues(e, t) { return s(this, null, function* () { return this.post(`/data/v1/application/${e}/recommendQues`, t); }); } delRecommendQues(e, t) { return s(this, null, function* () { return this.delete(`/data/v1/application/${e}/recommendQues/${t}`); }); } getChatHistorySessionId(e, t) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/chatHistory/${t}`); }); } //常用提示词 postPrompts(e, t) { return s(this, null, function* () { return this.post(`/data/v1/application/${e}/prompts`, t); }); } listPrompt(e, t) { return s(this, null, function* () { return t != null && t.title && t.title.length > B ? [] : this.get(`/data/v1/application/${e}/prompts`, t); }); } getPrompt(e, t) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/prompts/${t}`); }); } updatePrompt(e, t, a) { return s(this, null, function* () { return this.put(`/data/v1/application/${e}/prompts/${t}`, a); }); } deletePrompt(e, t) { return s(this, null, function* () { return this.delete(`/data/v1/application/${e}/prompts/${t}`); }); } patchApplicationIdSessionsIdContext(e, t) { return s(this, null, function* () { return this.patch(`/data/v1/application/${e}/sessions/${t}/context`, {}); }); } getApplicationIdSessionKnowledgeFiles(e, t, a) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/session/${t}/file`, a); }); } postApplicationIdSessionKnowledgeFile(e, t, a) { return s(this, null, function* () { return this.post(`/data/v1/application/${e}/session/${t}/file`, a); }); } postApplicationIdSessionKnowledgeSessionFile(e, t, a) { return s(this, null, function* () { return this.post(`/data/v1/application/${e}/session/${t}/sessionfile`, a); }); } delApplicationIdSessionKnowledgeFile(e, t, a) { return s(this, null, function* () { return this.delete(`/data/v1/application/${e}/session/${t}/file`, a); }); } getApplicationIdSessionKnowledgeFileId(e, t, a, i, r) { return s(this, null, function* () { return this.get(`/data/v1/application/${e}/session/${t}/file/${a}`, i, r); }); } delApplicationIdSessionKnowledgeFileId(e, t, a) { return s(this, null, function* () { return this.delete(`/data/v1/application/${e}/session/${t}/file/${a}`); }); } patchApplicationIdKnowledgeIdFileId(e, t, a, i) { return s(this, null, function* () { return this.patch(`/data/v1/application/${e}/session/${t}/file/${a}`, i); }); } checkFileReady(e, t, a, i = () => !0, r = 1e4, n) { return s(this, null, function* () { return new Promise((m, d) => { const l = () => { if (!i()) { clearInterval(o); return; } a ? this.getApplicationIdSessionKnowledgeFileId(e, t, a, {}, !0).then((c) => { if (n == null || n(c), c.status === v.Success && (clearInterval(o), m(c)), c.status === v.Failed) { clearInterval(o); const g = "该文件无法读取,请上传可编辑pdf,doc,docx文档"; d(Error(g, { cause: { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore code: -101, message: g, data: c } })); } }).catch((c) => { clearInterval(o), d(c); }) : m(null); }, o = setInterval(l, r); l(); }); }); } uploadFile(e, t) { return s(this, null, function* () { const a = { files: [t.name], isv: "minio", knowledgeId: e }; return this.postUploadTicket(a).then((i) => this.uploadMinioFile(i.minio, t)).catch((i) => { throw Error(i); }); }); } postUploadTicket(e) { return s(this, null, function* () { return this.post("/data/v1/ticket", e); }); } uploadMinioFile(e, t) { return s(this, null, function* () { return new Promise((a, i) => { const r = new FileReader(); r.readAsArrayBuffer(t), r.onload = (n) => { I.initMinio({ endPoint: e.endpoints[0], useSSL: e.useSSL, accessKey: e.accessKey, secretKey: e.secretKey }); const m = Buffer.from(n.target.result), d = { "Content-Type": "application/octet-stream" }; I.putObjectBuffer(e.bucket, e.fileKeys[0], m, d, (l) => { if (l) throw console.error(l), Error(l); a(e.fileKeys[0]); }); }; }); }); } postComments(e, t, a) { return s(this, null, function* () { return this.post(`/data/v1/application/${e}/sessions/${t}/comments`, a); }); } getKnowledgeId(e) { return s(this, null, function* () { return this.get(`/data/v1/knowledge/${e}`); }); } getKnowledgeIdFileId(e, t) { return s(this, null, function* () { return this.get(`/data/v1/knowledge/${e}/file/${t}`); }); } getKnowledgeIdFileIdData(e, t, a) { return s(this, null, function* () { return this.get(`/data/v1/knowledge/${e}/file/${t}/data/${a}`); }); } postChatV2EventSource(e, t, a, i, r) { return s(this, null, function* () { var l; const n = { code: 0, messageId: E(), content: "", role: P.Robot, sessionId: "", reference: [], searchReference: [], messageTime: "", llmModel: "", comment: { isAgree: 0 }, isReplying: !1, message: "" }, m = yield this.getToken(), d = $(y({}, a), { stream: !0 }); k(`${(l = this.axiosInstance) == null ? void 0 : l.defaults.baseURL}/chat/v2/chat?applicationId=${e}&sessionId=${t}`, { method: "POST", headers: { Authorization: `Bearer ${m}`, "Content-Type": "application/json" }, body: JSON.stringify(d), openWhenHidden: !0, signal: i, onopen(o) { return console.log("onopen"), o.ok && r != null && r.onopen && r.onopen(), Promise.resolve(); }, onmessage(o) { if (!o.data && !o.event && !o.id && !o.retry) return; if (!o.data.includes('{"code": ')) { let c = {}; try { c = JSON.parse(o.data); } catch (H) { } (c == null ? void 0 : c.mtype) === "search" ? n.searchReference.push(c) : n.content = n.content + o.data; const g = { content: n.content, role: 1, reference: n.reference, searchReference: n.searchReference, messageId: n.messageId, sessionId: t, messageTime: M(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"), comment: { isAgree: 0 }, isReplying: !0 }; return (r == null ? void 0 : r.onmessage) && r.onmessage(g); } const u = JSON.parse(o.data); if (n.reference = u.data.reference, n.messageTime = u.data.messageTime, n.messageId = u.data.messageId, n.llmModel = u.data.llmModel, n.code = u.code, n.message = u.message, u.code === 200) { const c = { code: n.code, content: n.content, role: 1, reference: n.reference, searchReference: n.searchReference, messageId: n.messageId, sessionId: t, messageTime: n.messageTime, isReplying: !0 }; r != null && r.onmessage && r.onmessage(c); } }, onerror() { r != null && r.onerror && r.onerror(); }, onclose() { const o = { code: n.code, content: n.content, role: 1, reference: n.reference, searchReference: n.searchReference, messageId: n.messageId, sessionId: t, messageTime: n.messageTime, comment: { isAgree: 0 }, isReplying: !1, message: n.message }; r != null && r.onclose && r.onclose(o); } }); }); } postChat(e, t) { return s(this, null, function* () { var a; return this.post(`${(a = this.axiosInstance) == null ? void 0 : a.defaults.baseURL}/chat/v2/chat`, e, t); }); } } export { Y as AecpilotUIApi };