2024-04-02 15:44:09 +08:00
|
|
|
import { useShowDeleteConfirm } from '@/hooks/commonHooks';
|
2024-03-06 19:17:45 +08:00
|
|
|
import { IKnowledge } from '@/interfaces/database/knowledge';
|
2024-02-18 18:18:20 +08:00
|
|
|
import { useCallback, useEffect, useMemo } from 'react';
|
2024-02-05 12:01:27 +08:00
|
|
|
import { useDispatch, useSearchParams, useSelector } from 'umi';
|
2024-03-20 11:13:51 +08:00
|
|
|
import { useGetKnowledgeSearchParams } from './routeHook';
|
|
|
|
import { useOneNamespaceEffectsLoading } from './storeHooks';
|
2024-02-02 18:49:54 +08:00
|
|
|
|
|
|
|
export const useKnowledgeBaseId = (): string => {
|
|
|
|
const [searchParams] = useSearchParams();
|
|
|
|
const knowledgeBaseId = searchParams.get('id');
|
|
|
|
|
|
|
|
return knowledgeBaseId || '';
|
|
|
|
};
|
2024-02-05 12:01:27 +08:00
|
|
|
|
|
|
|
export const useDeleteDocumentById = (): {
|
|
|
|
removeDocument: (documentId: string) => Promise<number>;
|
|
|
|
} => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const knowledgeBaseId = useKnowledgeBaseId();
|
2024-04-02 15:44:09 +08:00
|
|
|
const showDeleteConfirm = useShowDeleteConfirm();
|
2024-02-05 12:01:27 +08:00
|
|
|
|
|
|
|
const removeDocument = (documentId: string) => () => {
|
|
|
|
return dispatch({
|
|
|
|
type: 'kFModel/document_rm',
|
|
|
|
payload: {
|
|
|
|
doc_id: documentId,
|
|
|
|
kb_id: knowledgeBaseId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onRmDocument = (documentId: string): Promise<number> => {
|
|
|
|
return showDeleteConfirm({ onOk: removeDocument(documentId) });
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
removeDocument: onRmDocument,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-03-06 19:17:45 +08:00
|
|
|
export const useFetchKnowledgeDetail = () => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { knowledgeId } = useGetKnowledgeSearchParams();
|
|
|
|
|
|
|
|
const fetchKnowledgeDetail = useCallback(
|
|
|
|
(knowledgeId: string) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'knowledgeModel/getKnowledgeDetail',
|
|
|
|
payload: { kb_id: knowledgeId },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchKnowledgeDetail(knowledgeId);
|
|
|
|
}, [fetchKnowledgeDetail, knowledgeId]);
|
|
|
|
|
|
|
|
return fetchKnowledgeDetail;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useSelectKnowledgeDetail = () => {
|
|
|
|
const knowledge: IKnowledge = useSelector(
|
|
|
|
(state: any) => state.knowledgeModel.knowledge,
|
2024-02-05 12:01:27 +08:00
|
|
|
);
|
|
|
|
|
2024-03-06 19:17:45 +08:00
|
|
|
return knowledge;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useGetDocumentDefaultParser = () => {
|
|
|
|
const item = useSelectKnowledgeDetail();
|
2024-02-05 12:01:27 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
defaultParserId: item?.parser_id ?? '',
|
|
|
|
parserConfig: item?.parser_config ?? '',
|
|
|
|
};
|
|
|
|
};
|
2024-02-07 18:04:25 +08:00
|
|
|
|
|
|
|
export const useDeleteChunkByIds = (): {
|
|
|
|
removeChunk: (chunkIds: string[], documentId: string) => Promise<number>;
|
|
|
|
} => {
|
|
|
|
const dispatch = useDispatch();
|
2024-04-02 15:44:09 +08:00
|
|
|
const showDeleteConfirm = useShowDeleteConfirm();
|
2024-02-07 18:04:25 +08:00
|
|
|
|
|
|
|
const removeChunk = useCallback(
|
|
|
|
(chunkIds: string[], documentId: string) => () => {
|
|
|
|
return dispatch({
|
|
|
|
type: 'chunkModel/rm_chunk',
|
|
|
|
payload: {
|
|
|
|
chunk_ids: chunkIds,
|
|
|
|
doc_id: documentId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch],
|
|
|
|
);
|
|
|
|
|
|
|
|
const onRemoveChunk = useCallback(
|
|
|
|
(chunkIds: string[], documentId: string): Promise<number> => {
|
|
|
|
return showDeleteConfirm({ onOk: removeChunk(chunkIds, documentId) });
|
|
|
|
},
|
2024-04-02 15:44:09 +08:00
|
|
|
[removeChunk, showDeleteConfirm],
|
2024-02-07 18:04:25 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
removeChunk: onRemoveChunk,
|
|
|
|
};
|
|
|
|
};
|
2024-02-18 18:18:20 +08:00
|
|
|
|
|
|
|
export const useFetchKnowledgeBaseConfiguration = () => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const knowledgeBaseId = useKnowledgeBaseId();
|
|
|
|
|
|
|
|
const fetchKnowledgeBaseConfiguration = useCallback(() => {
|
|
|
|
dispatch({
|
|
|
|
type: 'kSModel/getKbDetail',
|
|
|
|
payload: {
|
|
|
|
kb_id: knowledgeBaseId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}, [dispatch, knowledgeBaseId]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchKnowledgeBaseConfiguration();
|
|
|
|
}, [fetchKnowledgeBaseConfiguration]);
|
|
|
|
};
|
2024-02-20 18:10:20 +08:00
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
export const useFetchKnowledgeList = (
|
|
|
|
shouldFilterListWithoutDocument: boolean = false,
|
2024-03-20 11:13:51 +08:00
|
|
|
): { list: IKnowledge[]; loading: boolean } => {
|
2024-02-20 18:10:20 +08:00
|
|
|
const dispatch = useDispatch();
|
2024-03-20 11:13:51 +08:00
|
|
|
const loading = useOneNamespaceEffectsLoading('knowledgeModel', ['getList']);
|
2024-02-20 18:10:20 +08:00
|
|
|
|
|
|
|
const knowledgeModel = useSelector((state: any) => state.knowledgeModel);
|
|
|
|
const { data = [] } = knowledgeModel;
|
2024-02-22 17:14:25 +08:00
|
|
|
const list = useMemo(() => {
|
|
|
|
return shouldFilterListWithoutDocument
|
|
|
|
? data.filter((x: IKnowledge) => x.doc_num > 0)
|
|
|
|
: data;
|
|
|
|
}, [data, shouldFilterListWithoutDocument]);
|
2024-02-20 18:10:20 +08:00
|
|
|
|
|
|
|
const fetchList = useCallback(() => {
|
|
|
|
dispatch({
|
|
|
|
type: 'knowledgeModel/getList',
|
|
|
|
});
|
|
|
|
}, [dispatch]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchList();
|
|
|
|
}, [fetchList]);
|
|
|
|
|
2024-03-20 11:13:51 +08:00
|
|
|
return { list, loading };
|
2024-02-20 18:10:20 +08:00
|
|
|
};
|
2024-02-28 16:28:33 +08:00
|
|
|
|
|
|
|
export const useSelectFileThumbnails = () => {
|
|
|
|
const fileThumbnails: Record<string, string> = useSelector(
|
|
|
|
(state: any) => state.kFModel.fileThumbnails,
|
|
|
|
);
|
|
|
|
|
|
|
|
return fileThumbnails;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useFetchFileThumbnails = (docIds?: Array<string>) => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const fileThumbnails = useSelectFileThumbnails();
|
|
|
|
|
|
|
|
const fetchFileThumbnails = useCallback(
|
|
|
|
(docIds: Array<string>) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'kFModel/fetch_document_thumbnails',
|
|
|
|
payload: { doc_ids: docIds.join(',') },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (docIds) {
|
|
|
|
fetchFileThumbnails(docIds);
|
|
|
|
}
|
|
|
|
}, [docIds, fetchFileThumbnails]);
|
|
|
|
|
|
|
|
return { fileThumbnails, fetchFileThumbnails };
|
|
|
|
};
|
2024-03-20 11:13:51 +08:00
|
|
|
|
|
|
|
//#region knowledge configuration
|
|
|
|
|
|
|
|
export const useUpdateKnowledge = () => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
const saveKnowledgeConfiguration = useCallback(
|
|
|
|
(payload: any) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'kSModel/updateKb',
|
|
|
|
payload,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch],
|
|
|
|
);
|
|
|
|
|
|
|
|
return saveKnowledgeConfiguration;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useSelectKnowledgeDetails = () => {
|
|
|
|
const knowledgeDetails: IKnowledge = useSelector(
|
|
|
|
(state: any) => state.kSModel.knowledgeDetails,
|
|
|
|
);
|
|
|
|
return knowledgeDetails;
|
|
|
|
};
|
|
|
|
//#endregion
|
2024-03-28 16:18:16 +08:00
|
|
|
|
|
|
|
//#region Retrieval testing
|
|
|
|
|
|
|
|
export const useTestChunkRetrieval = () => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const knowledgeBaseId = useKnowledgeBaseId();
|
|
|
|
|
|
|
|
const testChunk = useCallback(
|
|
|
|
(values: any) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'testingModel/testDocumentChunk',
|
|
|
|
payload: {
|
|
|
|
...values,
|
|
|
|
kb_id: knowledgeBaseId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch, knowledgeBaseId],
|
|
|
|
);
|
|
|
|
|
|
|
|
return testChunk;
|
|
|
|
};
|
|
|
|
//#endregion
|