978 lines
26 KiB
JavaScript
978 lines
26 KiB
JavaScript
// @/composables/useFormBuilder.js
|
|
import { useNuxtApp } from "#app";
|
|
import elpApi from "@/apis/elpApi";
|
|
import repoApi from "@/apis/repoApi";
|
|
import { cleanTextUnpermittedChars } from "@/utils/generalUtil";
|
|
|
|
export function useElpService(props, emit) {
|
|
const { $http: httpService } = useNuxtApp();
|
|
const toast = useToast();
|
|
|
|
///-----------------------------------------------------
|
|
///---- اسکیمای صفحات برنامه
|
|
///-----------------------------------------------------
|
|
async function uepGetSchema(system = "search", organ = "monir") {
|
|
let payload = {
|
|
organ: organ,
|
|
system: system,
|
|
};
|
|
|
|
const url = elpApi.base.getSchema;
|
|
|
|
return await httpService
|
|
.postRequest(url, payload)
|
|
.then((res) => {
|
|
return res?.data;
|
|
})
|
|
.catch((err) => {
|
|
toast.add({
|
|
title: "خطا",
|
|
description: err?.message,
|
|
color: "red",
|
|
});
|
|
return {};
|
|
});
|
|
|
|
dataEntrySchema.value = res?.data || {};
|
|
}
|
|
|
|
///-----------------------------------------------------
|
|
///--- گرفتن اطلاعات سند (GET Document)
|
|
///-----------------------------------------------------
|
|
async function uepGetDocument(entityId, key = "") {
|
|
if (!entityId) {
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "شناسه وجود ندارد",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
if (!key) {
|
|
const config = useRuntimeConfig();
|
|
key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
let url = elpApi.base.get_document;
|
|
url = url.replace("{{type_name}}", key);
|
|
url = url.replace("{{doc_id}}", entityId);
|
|
|
|
return await httpService
|
|
.getRequest(url)
|
|
.then((res) => {
|
|
return res; // معمولاً شامل doc_id یا کل سند ایجاد شده است
|
|
})
|
|
.catch((error) => {
|
|
console.error("Error in 1 :", error);
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در گرفتن سند، دوباره تلاش کنید",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
});
|
|
}
|
|
|
|
async function myPostRequest(
|
|
url,
|
|
payload,
|
|
by_message = false,
|
|
message = "عملیات با موفقیت انجام شد"
|
|
) {
|
|
try {
|
|
const res = await httpService.postRequest(url, payload);
|
|
|
|
if (by_message && toast?.add) {
|
|
toast.add({
|
|
title: "موفق",
|
|
description: message,
|
|
icon: "i-lucide-file-plus",
|
|
});
|
|
}
|
|
|
|
return res;
|
|
} catch (error) {
|
|
console.error("Error in myPostRequest:", error);
|
|
|
|
if (toast?.add) {
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در عملیات، دوباره تلاش کنید",
|
|
color: "red",
|
|
});
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
///-----------------------------------------------------
|
|
///--- ایجاد سند جدید (Add Document)
|
|
///-- payload = {
|
|
///-- id: "" -- اختیاری
|
|
///-- docuemnt : {
|
|
///-- title: "..."
|
|
///-- ...
|
|
///-- sections: [ ... ]
|
|
///-- ...
|
|
///-- }
|
|
///-- }
|
|
|
|
///-----------------------------------------------------
|
|
async function uepAddDocument(payload, key = "", by_message = false) {
|
|
if (!payload) {
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "دادههای سند برای ایجاد الزامی است",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
let doc = {
|
|
document: { ...payload },
|
|
};
|
|
|
|
if (!key) {
|
|
const config = useRuntimeConfig();
|
|
key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
let url = elpApi.base.add_document;
|
|
url = url.replace("{{type_name}}", key);
|
|
|
|
return await httpService
|
|
.postRequest(url, doc)
|
|
.then((res) => {
|
|
if (by_message)
|
|
toast.add({
|
|
title: "موفق",
|
|
description: "سند جدید با موفقیت ایجاد شد.",
|
|
icon: "i-lucide-file-plus",
|
|
});
|
|
return res; // معمولاً شامل doc_id یا کل سند ایجاد شده است
|
|
})
|
|
.catch((error) => {
|
|
console.error("Error in 1 :", error);
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در ایجاد سند جدید، دوباره تلاش کنید",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
});
|
|
}
|
|
async function uepInsertTree(payload, typeName = "gntree") {
|
|
if (!payload) {
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "اطلاعات گره برای ایجاد الزامی است",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
const url = elpApi.Info.elp_insert_Tree.replace("{{type_name}}", typeName);
|
|
|
|
return await httpService.postRequest(url, payload).catch((error) => {
|
|
console.error("ساخت گرهٔ درخت ناموفق بود:", error);
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در ایجاد گرهٔ درخت، دوباره تلاش کنید",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
});
|
|
}
|
|
|
|
async function uepGetTree(payload, typeName = "gntree") {
|
|
const url = elpApi.Info.elp_get_Tree.replace("{{type_name}}", typeName);
|
|
|
|
return await httpService.postRequest(url, payload).catch((error) => {
|
|
console.error("خواندن درخت ناموفق بود:", error);
|
|
return [];
|
|
});
|
|
}
|
|
|
|
async function uepDeleteTree(nodeId, typeName = "gntree") {
|
|
const url = elpApi.Info.elp_delete_Tree
|
|
.replace("{{type_name}}", typeName)
|
|
.replace("{{id}}", nodeId);
|
|
|
|
return await httpService.postRequest(url).catch((error) => {
|
|
console.error("حذف گرهٔ درخت ناموفق بود:", error);
|
|
return undefined;
|
|
});
|
|
}
|
|
async function uepUpdateTree(nodeId, payload, typeName = "gntree") {
|
|
if (!nodeId) {
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "شناسه گره برای ویرایش الزامی است",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
if (!payload) {
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "اطلاعات گره برای ویرایش الزامی است",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
}
|
|
|
|
const url = elpApi.Info.elp_update_Tree
|
|
.replace("{{type_name}}", typeName)
|
|
.replace("{{id}}", nodeId);
|
|
|
|
try {
|
|
return await httpService.postRequest(url, payload);
|
|
} catch (error) {
|
|
console.error("ویرایش گره درخت ناموفق بود:", error);
|
|
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در ویرایش گره، دوباره تلاش کنید",
|
|
color: "red",
|
|
});
|
|
|
|
return undefined;
|
|
}
|
|
}
|
|
///-----------------------------------------------------
|
|
///--- آپدیت کامل داکیومنت (چند فیلد باهم)
|
|
///-- payload = {
|
|
///-- document : {
|
|
///-- title: "..."
|
|
///-- ...
|
|
///-- sections: [ ... ]
|
|
///-- ...
|
|
///-- }
|
|
///-- }
|
|
///-----------------------------------------------------
|
|
async function uepUpdateDocument(
|
|
doc_id,
|
|
payload,
|
|
key = "",
|
|
by_message = false
|
|
) {
|
|
if (!doc_id) return;
|
|
|
|
let doc = {
|
|
id: doc_id,
|
|
document: { ...payload },
|
|
};
|
|
|
|
if (!key) {
|
|
const config = useRuntimeConfig();
|
|
key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
let url = elpApi.base.update_document; // باید این route در بک وجود داشته باشد
|
|
url = url.replace("{{type_name}}", key);
|
|
url = url.replace("{{doc_id}}", doc_id);
|
|
|
|
return await httpService
|
|
.postRequest(url, doc)
|
|
.then((res) => {
|
|
if (by_message)
|
|
toast.add({
|
|
title: "موفق",
|
|
description: "سند با موفقیت بروزرسانی شد.",
|
|
icon: "i-lucide-check",
|
|
});
|
|
return res;
|
|
})
|
|
.catch((err) => {
|
|
console.error("Error in 2 :", err);
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در بروزرسانی سند " + err.message,
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
});
|
|
}
|
|
|
|
///-----------------------------------------------------
|
|
///--- یک فیلد را بروزرسانی می کند
|
|
///-- payload = {
|
|
///-- field_key: str
|
|
///-- field_value: Any
|
|
///-- action: str = 'edit' # edit, add, delete, full
|
|
///-- index_in_array: Optional[int] = None
|
|
///-- id_in_array: Optional[str] = None
|
|
///-- }
|
|
///-----------------------------------------------------
|
|
async function uepUpdateField(doc_id, payload, key = "", by_message = false) {
|
|
if (!doc_id) {
|
|
alert("شناسه uepUpdateField !!");
|
|
return undefined;
|
|
}
|
|
if (!key) {
|
|
const config = useRuntimeConfig();
|
|
key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
let url = elpApi.base.update_field;
|
|
url = url.replace("{{type_name}}", key);
|
|
url = url.replace("{{doc_id}}", doc_id);
|
|
return await httpService
|
|
.postRequest(url, payload)
|
|
.then(() => {
|
|
if (by_message)
|
|
toast.add({
|
|
title: "موفق",
|
|
description: "عملیات با موفقیت انجام شد.",
|
|
icon: "i-lucide-calendar-days",
|
|
});
|
|
return true;
|
|
})
|
|
.catch((err) => {
|
|
console.error("Error in 3 :", err);
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در انجام دوباره تلاش کنید",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
});
|
|
}
|
|
|
|
///-----------------------------------------------------
|
|
///- حذف کامل یک سند با شناسه مشخص
|
|
///-----------------------------------------------------
|
|
async function uepDeleteDocument(doc_id, key = "", by_message = false) {
|
|
if (!doc_id) return;
|
|
|
|
if (!key) {
|
|
const config = useRuntimeConfig();
|
|
key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
let url = elpApi.base.delete_document;
|
|
url = url.replace("{{type_name}}", key);
|
|
url = url.replace("{{doc_id}}", doc_id);
|
|
return await httpService
|
|
.postRequest(url, {})
|
|
.then(() => {
|
|
if (by_message)
|
|
toast.add({
|
|
title: "موفق",
|
|
description: "عملیات با موفقیت انجام شد.",
|
|
icon: "i-lucide-calendar-days",
|
|
});
|
|
return true;
|
|
})
|
|
.catch((err) => {
|
|
console.error("Error in 7 :", err);
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در انجام دوباره تلاش کنید",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
});
|
|
}
|
|
///-----------------------------------------------------
|
|
///--- با توجه به یک فیلد، مقدار محاسبه شده ای بر میگردند مثلا : بیشترین مقدار یا مجموع یا ...
|
|
///-----------------------------------------------------
|
|
async function uepGetComputeField(
|
|
key = "",
|
|
property_key = "meet_no",
|
|
compute_key = "max",
|
|
filters = {}
|
|
) {
|
|
let payload = {
|
|
compute_key,
|
|
property_key,
|
|
filters,
|
|
};
|
|
|
|
if (!key) {
|
|
const config = useRuntimeConfig();
|
|
key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
let url = elpApi.base.compute_field;
|
|
url = url.replace("{{type_name}}", key);
|
|
|
|
return await httpService
|
|
.postRequest(url, payload)
|
|
.then((res) => {
|
|
return res;
|
|
})
|
|
.catch(() => {
|
|
toast.add({
|
|
title: "خطا",
|
|
description: "خطا در انجام عملیات، دوباره تلاش کنید",
|
|
color: "red",
|
|
});
|
|
return undefined;
|
|
});
|
|
}
|
|
|
|
//-------------------------------------------
|
|
//-------------------------------------------
|
|
async function createSearchRequest(
|
|
index_key,
|
|
textSearch,
|
|
searchTypeKey,
|
|
mode_url = "normal",
|
|
filterPanel = "",
|
|
filterExtended = "",
|
|
offset = 0,
|
|
limit = 10,
|
|
field_collapse = "normal",
|
|
sortKey = "normal",
|
|
domainKey = "all",
|
|
domainSchema = {},
|
|
user_synonyms = {},
|
|
mirror_type = ""
|
|
) {
|
|
let _payload = {};
|
|
let domain_label = "";
|
|
let query_string = cleanTextUnpermittedChars(textSearch);
|
|
|
|
/* ===== Base URL ===== */
|
|
if (!index_key) {
|
|
const config = useRuntimeConfig();
|
|
index_key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
let baseUrl = "";
|
|
if (mode_url === "count") {
|
|
baseUrl = elpApi.search.elp_base_count;
|
|
baseUrl = baseUrl.replace("{{index_key}}", index_key);
|
|
}
|
|
if (mode_url === "elp_db") {
|
|
baseUrl = elpApi.search.elp_db_search;
|
|
baseUrl = baseUrl.replace("{{index_key}}", index_key);
|
|
} else if (mode_url === "elp_voice") {
|
|
baseUrl = elpApi.search.elp_voice_search;
|
|
baseUrl = baseUrl.replace("{{index_key}}", index_key);
|
|
} else if (mode_url === "mirror") {
|
|
baseUrl = repoApi.search.mirror_search;
|
|
baseUrl = baseUrl.replace("{{mirror_type}}", mirror_type);
|
|
} else if (mode_url === "elp_tree") {
|
|
baseUrl = elpApi.Info.elp_get_list_Tree;
|
|
baseUrl = baseUrl.replace("{{index_key}}", index_key);
|
|
} // if (mode_url === "elp" || mode_url == "")
|
|
else {
|
|
baseUrl = elpApi.search.elp_base_search;
|
|
baseUrl = baseUrl.replace("{{index_key}}", index_key);
|
|
}
|
|
|
|
// ====Synonym / Vector / Normal
|
|
let sysnonymSearchQuery = "";
|
|
|
|
if (!searchTypeKey) searchTypeKey = "normal";
|
|
|
|
if (searchTypeKey === "synonym") {
|
|
let newSynonym = {};
|
|
|
|
if (user_synonyms) {
|
|
Object.keys(user_synonyms).forEach((key) => {
|
|
if (
|
|
!(
|
|
"isStopWord" in user_synonyms[key] &&
|
|
user_synonyms[key].isStopWord
|
|
)
|
|
) {
|
|
newSynonym[key] = user_synonyms[key].value;
|
|
sysnonymSearchQuery += " " + key;
|
|
}
|
|
});
|
|
}
|
|
|
|
if (textSearch === "" || textSearch === undefined)
|
|
textSearch = sysnonymSearchQuery;
|
|
|
|
if (Object.keys(newSynonym).length) {
|
|
_payload = {
|
|
synonym: newSynonym,
|
|
};
|
|
}
|
|
} else if (searchTypeKey === "vector") {
|
|
/* ===== Vector ===== */
|
|
_payload = {
|
|
text: query_string,
|
|
};
|
|
} else if (!(textSearch === "" || textSearch === undefined)) {
|
|
/* ===== Normal ===== */
|
|
query_string = query_string;
|
|
|
|
if (!domainKey) domainKey = "all";
|
|
|
|
if (domainKey === "all") {
|
|
if (domainSchema?.type_action) {
|
|
let isNumberInput = /^\d+$/.test(query_string);
|
|
|
|
if (isNumberInput && domainSchema.type_action.number) {
|
|
let tag = domainSchema.type_action.number;
|
|
query_string = encodeURIComponent("#") + tag + " " + query_string;
|
|
}
|
|
}
|
|
} else if (domainKey !== "advance") {
|
|
let domainItem = domainSchema?.domain?.find(
|
|
(item) => item.key === domainKey
|
|
);
|
|
|
|
if (domainItem) {
|
|
domain_label = domainItem.label;
|
|
query_string =
|
|
encodeURIComponent("#") + domainItem.tag + " " + query_string;
|
|
}
|
|
}
|
|
}
|
|
|
|
// === Filters
|
|
let filterFull = "";
|
|
|
|
if (searchTypeKey !== "vector" && query_string) {
|
|
filterFull += "q=" + query_string;
|
|
}
|
|
|
|
filterFull += filterPanel + filterExtended;
|
|
|
|
if (!filterFull) filterFull = "none";
|
|
|
|
// ==== URL Replace
|
|
let url = baseUrl;
|
|
const buildName = import.meta.env.VITE_BUILD_NAME;
|
|
|
|
url = url.replace("{{appname}}", buildName);
|
|
url = url.replace("{{index_key}}", index_key);
|
|
url = url.replace("{{search_type}}", searchTypeKey);
|
|
url = url.replace("{{sortKey}}", sortKey);
|
|
url = url.replace("{{field_collapse}}", field_collapse);
|
|
url = url.replace("{{offset}}", offset);
|
|
url = url.replace("{{limit}}", limit);
|
|
url = url.replace("{{filter}}", filterFull);
|
|
|
|
// == Payload
|
|
let filters = (filterPanel + filterExtended)
|
|
.split("&")
|
|
.filter((e) => e.length);
|
|
|
|
let filters_obj = {};
|
|
filters.forEach((fl) => {
|
|
let fl_items = fl.split("=");
|
|
if (fl_items.length === 2) filters_obj[fl_items[0]] = fl_items[1];
|
|
});
|
|
|
|
let payload_full = {
|
|
track_total_hits: true,
|
|
query: query_string,
|
|
search_type: searchTypeKey,
|
|
filters: filters_obj,
|
|
sort: [sortKey],
|
|
from_: offset,
|
|
size: limit,
|
|
collapse_field: field_collapse,
|
|
};
|
|
|
|
if (_payload.synonym) {
|
|
payload_full.user_synonyms = _payload.synonym;
|
|
}
|
|
|
|
return {
|
|
url,
|
|
payload_full,
|
|
payload: _payload,
|
|
sysnonymSearchQuery,
|
|
domainLabel: domain_label,
|
|
};
|
|
}
|
|
|
|
//-------------------------------------------
|
|
async function uepGetDataSearch({
|
|
index_key,
|
|
textSearch,
|
|
searchTypeKey,
|
|
mode_url,
|
|
filterPanel,
|
|
filterExtended,
|
|
filterids,
|
|
offset,
|
|
limit,
|
|
field_collapse,
|
|
sortKey,
|
|
domainKey,
|
|
domainSchema,
|
|
user_synonyms,
|
|
mirror_type,
|
|
synonymData,
|
|
aggregations,
|
|
}) {
|
|
if (!index_key) {
|
|
const config = useRuntimeConfig();
|
|
index_key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
const request = await createSearchRequest(
|
|
index_key,
|
|
textSearch ?? "",
|
|
searchTypeKey ?? "normal",
|
|
mode_url ?? "elp",
|
|
filterPanel ?? "",
|
|
filterExtended ?? "",
|
|
offset ?? 0,
|
|
limit ?? 10,
|
|
field_collapse ?? "normal",
|
|
sortKey ?? "normal",
|
|
domainKey ?? "all",
|
|
domainSchema ?? {},
|
|
user_synonyms ?? {},
|
|
mirror_type ?? ""
|
|
);
|
|
|
|
let payload_full = request.payload_full;
|
|
if (filterids?.length) {
|
|
if (!"filters" in payload_full) payload_full["filters"] = {};
|
|
payload_full.filters["ids"] = filterids;
|
|
}
|
|
|
|
//------------------
|
|
if (aggregations && Object.keys(aggregations).length) {
|
|
request.payload_full.aggregation_fields = aggregations;
|
|
}
|
|
//------------------
|
|
if (synonymData && Object.keys(synonymData).length) {
|
|
if (synonymData.synonyms?.length > 0) {
|
|
request.payload_full.synonyms = synonymData.synonyms;
|
|
}
|
|
if (Object.keys(synonymData.user_synonyms)?.length) {
|
|
request.payload_full.user_synonyms = synonymData.user_synonyms;
|
|
}
|
|
}
|
|
|
|
try {
|
|
const res = await httpService.postRequest(request.url, payload_full);
|
|
|
|
return res;
|
|
} catch (err) {
|
|
console.error(err);
|
|
return {};
|
|
} finally {
|
|
}
|
|
}
|
|
|
|
async function uepLogSearchQuery(query) {
|
|
let key = "logsearch";
|
|
|
|
let doc = {
|
|
query,
|
|
};
|
|
|
|
let url = elpApi.base.add_document;
|
|
url = url.replace("{{type_name}}", key);
|
|
return await myPostRequest(url, doc, by_message);
|
|
}
|
|
|
|
//-------------------------------------------
|
|
//-------------------------------------------
|
|
|
|
// جستجوی معنایی از مسیر کارهای چت (chat job)
|
|
/*
|
|
offset و limit برای صفحهبندی است (مثل بقیهٔ متدهای جستجو)؛
|
|
در پیلود با نامهای from_ و size فرستاده میشوند که فیلدهای سرورند.
|
|
*/
|
|
async function uepChatJobSearchQuery(
|
|
query,
|
|
key,
|
|
filters = [],
|
|
offset = 0,
|
|
limit = 10,
|
|
lexical_weight = 0.5,
|
|
vector_weight = 0.5,
|
|
vector_reranker = false
|
|
) {
|
|
|
|
|
|
const url = elpApi.rag_chat.chat_job_search_query.replace(
|
|
"{{type_name}}",
|
|
key
|
|
);
|
|
|
|
let filters_obj = {};
|
|
filters.forEach((el) => {
|
|
let fls = el.split("&");
|
|
fls.forEach((fl) => {
|
|
let fl_items = fl.split("=");
|
|
if (fl_items.length === 2) filters_obj[fl_items[0]] = fl_items[1];
|
|
});
|
|
});
|
|
|
|
const payload = {
|
|
query: query ?? "",
|
|
from_: offset,
|
|
size: limit,
|
|
lexical_weight,
|
|
vector_weight,
|
|
vector_reranker,
|
|
filters: filters_obj,
|
|
};
|
|
|
|
try {
|
|
const res = await httpService.postRequest(url, payload);
|
|
|
|
return res;
|
|
} catch (err) {
|
|
console.error("uepChatJobSearchQuery error:", err);
|
|
return {};
|
|
} finally {
|
|
}
|
|
}
|
|
|
|
async function uepChatRag(query, key, filters = [], is_rerank = false) {
|
|
if (!key) {
|
|
const config = useRuntimeConfig();
|
|
key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
let filters_obj = {};
|
|
filters.forEach((fl) => {
|
|
let fl_items = fl.split("=");
|
|
if (fl_items.length === 2) filters_obj[fl_items[0]] = fl_items[1];
|
|
});
|
|
|
|
let payload = {
|
|
query,
|
|
filters: filters_obj,
|
|
sort: ["normal"],
|
|
size: 100,
|
|
is_rerank,
|
|
rerank_limit: 10,
|
|
};
|
|
|
|
let url = elpApi.rag_chat.elp_chat;
|
|
url = url.replace("{{type_name}}", key);
|
|
|
|
try {
|
|
const res = await httpService.postRequest(
|
|
url,
|
|
payload,
|
|
{ timeout: 180000 } // سه دقیقه
|
|
);
|
|
|
|
if (res.status == "success") return res.result;
|
|
else return res;
|
|
} catch (err) {
|
|
console.error(err);
|
|
return {};
|
|
} finally {
|
|
}
|
|
}
|
|
|
|
async function uepCahtLLM(user_prompt) {
|
|
let payload = {
|
|
user_prompt,
|
|
};
|
|
|
|
try {
|
|
const res = await httpService.postRequest(
|
|
elpApi.rag_chat.note_action,
|
|
payload
|
|
);
|
|
|
|
if (res.status == "success") return res.result;
|
|
else return res;
|
|
} catch (err) {
|
|
console.error(err);
|
|
return {};
|
|
} finally {
|
|
}
|
|
}
|
|
async function uepJobAdd(payload, note_id, key = "", onProgress) {
|
|
if (!key) {
|
|
const config = useRuntimeConfig();
|
|
key = config.public.NUXT_PUBLIC_INDEX_KEY;
|
|
}
|
|
|
|
const url = elpApi.rag_chat.chat_job_query
|
|
.replace("{{type_name}}", key)
|
|
.replace("{{note_id}}", note_id);
|
|
|
|
try {
|
|
const res = await httpService.postRequest(url, payload);
|
|
// console.log("add res ==> ", res);
|
|
|
|
if (!res?.id) return res;
|
|
|
|
return res;
|
|
} catch (err) {
|
|
console.error("uepJobAdd error:", err);
|
|
return {};
|
|
}
|
|
}
|
|
async function uepJobGet(job_id) {
|
|
if (!job_id) return { job_status: "error", result: {} };
|
|
|
|
const url = elpApi.rag_chat.chat_job_get.replace("{{job_id}}", job_id);
|
|
|
|
try {
|
|
const res = await httpService.getRequest(url);
|
|
// console.log("get res ==> ", res);
|
|
|
|
const body = res?.result ?? res ?? {};
|
|
|
|
return {
|
|
...body,
|
|
result: body,
|
|
job_status: body.job_status ?? res?.job_status ?? "wait",
|
|
};
|
|
} catch (err) {
|
|
console.error("uepJobGet error:", err);
|
|
|
|
return { job_status: "wait", result: {}, has_error: true };
|
|
}
|
|
}
|
|
async function uepJobWaitForFinish(job_id, onProgress, options = {}) {
|
|
const repeatTimeQuery = 5000;
|
|
const maxRetryOnError = 5;
|
|
const abortSignal = options.signal;
|
|
|
|
/*
|
|
اگر کاربر «توقف» را بزند، حلقه فوراً و بدون درخواست بعدی خارج میشود.
|
|
*/
|
|
function throwIfStopped() {
|
|
if (!abortSignal?.aborted) return;
|
|
|
|
const abortError = new Error("پاسخ متوقف شد");
|
|
abortError.name = "AbortError";
|
|
throw abortError;
|
|
}
|
|
|
|
/* انتظار بین دو درخواست، با قابلیت قطع فوری */
|
|
function waitForNextPoll() {
|
|
return new Promise((resolve) => {
|
|
if (!abortSignal) {
|
|
setTimeout(resolve, repeatTimeQuery);
|
|
return;
|
|
}
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
abortSignal.removeEventListener("abort", handleAbort);
|
|
resolve();
|
|
}, repeatTimeQuery);
|
|
|
|
function handleAbort() {
|
|
clearTimeout(timeoutId);
|
|
resolve();
|
|
}
|
|
|
|
abortSignal.addEventListener("abort", handleAbort, { once: true });
|
|
});
|
|
}
|
|
|
|
let retry = 0;
|
|
|
|
while (true) {
|
|
throwIfStopped();
|
|
|
|
const res = await uepJobGet(job_id);
|
|
|
|
throwIfStopped();
|
|
|
|
const result = res.result ?? {};
|
|
const status = res.job_status;
|
|
|
|
if (res.has_error) {
|
|
retry += 1;
|
|
|
|
if (retry > maxRetryOnError) {
|
|
throw new Error(`Job unreachable after ${retry} tries: ${job_id}`);
|
|
}
|
|
|
|
await waitForNextPoll();
|
|
continue;
|
|
}
|
|
|
|
retry = 0;
|
|
|
|
if (typeof onProgress === "function") {
|
|
try {
|
|
onProgress(result, { job_status: status });
|
|
} catch (err) {
|
|
console.error("onProgress error:", err);
|
|
}
|
|
}
|
|
|
|
if (status === "finish") {
|
|
return result;
|
|
}
|
|
|
|
if (status === "error" || status === "cancel") {
|
|
throw new Error(`Job ${status}: ${job_id}`);
|
|
}
|
|
|
|
await waitForNextPoll();
|
|
}
|
|
}
|
|
async function uepJobAction(note_id, payload) {
|
|
// console.log("note_id, payload ==> ", note_id, payload);
|
|
if (!note_id) return {};
|
|
|
|
const url = elpApi.rag_chat.note_job_action.replace("{{note_id}}", note_id);
|
|
|
|
try {
|
|
const res = await httpService.postRequest(url, payload);
|
|
// console.log("action res ==> ", res);
|
|
|
|
if (res?.status === "success") return res.result;
|
|
return res;
|
|
} catch (err) {
|
|
console.error("uepJobAction error:", err);
|
|
return {};
|
|
}
|
|
}
|
|
// ========== دریافت مترادف از سرور ==========
|
|
const getSynonymsFromServer = async (word) => {
|
|
try {
|
|
let payload = {
|
|
query: word,
|
|
};
|
|
|
|
const response = await httpService.postRequest(
|
|
elpApi.search.repo_synonym_words,
|
|
payload
|
|
);
|
|
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error("Error:", error);
|
|
return [];
|
|
}
|
|
};
|
|
///-----------------------------------------------------
|
|
///-----------------------------------------------------
|
|
return {
|
|
uepGetDocument,
|
|
uepAddDocument,
|
|
uepInsertTree,
|
|
uepGetTree,
|
|
uepDeleteTree,
|
|
uepUpdateDocument,
|
|
uepDeleteDocument,
|
|
uepGetDataSearch,
|
|
uepChatJobSearchQuery,
|
|
uepChatRag,
|
|
uepCahtLLM,
|
|
getSynonymsFromServer,
|
|
uepUpdateField,
|
|
uepJobAdd,
|
|
uepJobGet,
|
|
uepJobWaitForFinish,
|
|
uepGetSchema,
|
|
uepGetComputeField,
|
|
uepJobAction,
|
|
uepUpdateTree,
|
|
};
|
|
}
|
|
|
|
export default useElpService;
|