@glodon-aiot/apis
Version:
aiot apis
523 lines (522 loc) • 15.3 kB
JavaScript
var P = Object.defineProperty, K = Object.defineProperties;
var b = Object.getOwnPropertyDescriptors;
var T = Object.getOwnPropertySymbols;
var E = Object.prototype.hasOwnProperty, F = Object.prototype.propertyIsEnumerable;
var $ = (g, e, t) => e in g ? P(g, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : g[e] = t, f = (g, e) => {
for (var t in e || (e = {}))
E.call(e, t) && $(g, t, e[t]);
if (T)
for (var t of T(e))
F.call(e, t) && $(g, t, e[t]);
return g;
}, S = (g, e) => K(g, b(e));
var r = (g, e, t) => new Promise((o, n) => {
var s = (i) => {
try {
h(t.next(i));
} catch (c) {
n(c);
}
}, m = (i) => {
try {
h(t.throw(i));
} catch (c) {
n(c);
}
}, h = (i) => i.done ? o(i.value) : Promise.resolve(i.value).then(s, m);
h((t = t.apply(g, e)).next());
});
import O from "axios";
import { BaseApi as v } from "../base/index.mjs";
import { initMinio as C, putObjectBuffer as H } from "@glodon-aiot/minio";
import { IHistoryRole as k, AecKnowledgeFileStatus as R } from "./model.mjs";
import { IFileOrigin as W, ISourceType as X, IThinkingStatus as q, IversionInfo as Z } from "./model.mjs";
import { v4 as w } from "uuid";
import Q from "dayjs";
import { PROMPT_SEARCH_MAX_LENGTH as x } from "./cosntant.mjs";
import { PROMPT_TITLE_MAX_LENGTH as se } from "./cosntant.mjs";
import { fetchEventSource as A } from "../utils/fetchEventSource/fetch.mjs";
class z 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 = (m) => {
C({
endPoint: e.endpoints[0],
useSSL: e.useSSL,
accessKey: e.accessKey,
secretKey: e.secretKey
});
const h = new Uint8Array(m.target.result), i = {
"Content-Type": "application/octet-stream"
};
H(e.bucket, e.fileKeys[0], h, i, (c) => {
if (c)
throw console.error(c), Error(c);
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((m, h) => {
const i = () => {
if (!o()) {
clearInterval(c);
return;
}
t ? this.getSessionKnowledgeFile(e, t).then((a) => {
if (s == null || s(a), a.status === R.Success && (clearInterval(c), m(a)), a.status === R.Failed) {
clearInterval(c);
const l = "该文件无法读取,请上传可编辑pdf,doc,docx文档";
h(Error(l, {
cause: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
code: -101,
message: l,
data: a
}
}));
}
}).catch((a) => {
clearInterval(c), h(a);
}) : m(null);
}, c = 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: w(),
content: "",
role: k.Robot,
sessionId: "",
reference: [],
searchReference: [],
messageTime: "",
llmModel: "",
comment: {
isAgree: 0
},
isReplying: !1
}, m = yield this.getToken(), h = S(f({}, t), {
stream: !0
});
A(`${(i = this.axiosInstance) == null ? void 0 : i.defaults.baseURL}/chat?sessionId=${e}`, {
method: "POST",
headers: {
Authorization: `Bearer ${m}`,
"Content-Type": "application/json"
},
body: JSON.stringify(h),
openWhenHidden: !0,
signal: o,
onopen(c) {
return c.ok && n != null && n.onopen && n.onopen(), Promise.resolve();
},
onmessage(c) {
if (!c.data && !c.event && !c.id && !c.retry)
return;
if (!c.data.includes('{"code": ')) {
let a = {};
try {
a = JSON.parse(c.data);
} catch (u) {
}
(a == null ? void 0 : a.mtype) === "search" ? s.searchReference.push(a) : s.content = s.content + c.data;
const l = {
content: s.content,
role: 1,
reference: s.reference,
searchReference: s.searchReference,
messageId: s.messageId,
sessionId: e,
messageTime: Q(/* @__PURE__ */ new Date()).format("YYYY-MM-DD HH:mm:ss"),
comment: {
isAgree: 0
},
isReplying: !0
};
return (n == null ? void 0 : n.onmessage) && n.onmessage(l);
}
const d = JSON.parse(c.data);
if (s.reference = d.data.reference, s.messageTime = d.data.messageTime, s.messageId = d.data.messageId, s.llmModel = d.data.llmModel, s.code = d.code, d.code === 200) {
const a = {
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(a);
}
},
onerror() {
n != null && n.onerror && n.onerror();
},
onclose() {
const c = {
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(c);
}
});
});
}
postChatV3EventSource(e, t, o, n) {
return r(this, null, function* () {
var i, c;
const s = {
code: 0,
messageId: w(),
content: "",
role: k.Robot,
sessionId: "",
reference: [],
searchReference: [],
messageTime: "",
llmModel: "",
comment: {
isAgree: 0
},
isReplying: !1,
reasoningContent: "",
thinkingSecs: "",
thinkingStatus: "",
searchingStatus: ""
}, m = yield this.getToken(), h = S(f({}, t), {
stream: !0
});
A(`${(c = (i = this.axiosInstance) == null ? void 0 : i.defaults.baseURL) == null ? void 0 : c.replace("/v1", "/v3")}/chat?sessionId=${e}`, {
method: "POST",
headers: {
Authorization: `Bearer ${m}`,
"Content-Type": "application/json"
},
body: JSON.stringify(h),
openWhenHidden: !0,
signal: o,
onopen(d) {
return d.ok && n != null && n.onopen && n.onopen(), Promise.resolve();
},
onmessage(d) {
if (!d.data && !d.event && !d.id && !d.retry)
return;
let a = {};
try {
a = JSON.parse(d.data);
} catch (p) {
console.log("eventData", d.data);
}
const l = a.data.type, u = a.data.payload;
s.isReplying = !0, l === "error" && (s.code = a.code, s.message = a.message);
const I = () => {
if (!s.searchingStatus) {
s.searchingStatus = "start";
return;
}
if (s.searchingStatus === "start") {
s.searchingStatus = "end";
return;
}
};
if (l === "beforeSearch" && I(), l === "search" && (s.code = a.code, s.message = a.message, s.searchReference.push(u)), l === "thinking") {
I(), s.code = a.code, s.message = a.message, s.thinkingStatus = "start";
const y = u.choices[0];
s.reasoningContent = s.reasoningContent + y.delta.reasoningContent;
}
if (l === "thinkingOver") {
s.code = a.code, s.message = a.message, s.thinkingStatus = "end";
const p = u.thinkingSecs;
s.thinkingSecs = p;
}
if (l === "text") {
I(), s.code = a.code, s.message = a.message;
const y = u.choices[0];
s.content = s.content + y.delta.content;
}
if (l === "reference" && (s.code = a.code, s.message = a.message, s.reference = u), l === "result") {
s.code = a.code, s.message = a.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 d = S(f({}, s), {
isReplying: !1
});
n != null && n.onclose && n.onclose(d);
}
});
});
}
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 > x ? [] : 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 {
z as AECPilotApi,
R as AecKnowledgeFileStatus,
W as IFileOrigin,
k as IHistoryRole,
X as ISourceType,
q as IThinkingStatus,
Z as IversionInfo,
x as PROMPT_SEARCH_MAX_LENGTH,
se as PROMPT_TITLE_MAX_LENGTH
};