2024-05-15 11:34:57 +08:00
|
|
|
import { Authorization } from '@/constants/authorization';
|
2024-04-12 11:41:00 +08:00
|
|
|
import { LanguageTranslationMap } from '@/constants/common';
|
2024-04-25 19:06:24 +08:00
|
|
|
import { Pagination } from '@/interfaces/common';
|
2024-05-16 20:15:02 +08:00
|
|
|
import { IAnswer } from '@/interfaces/database/chat';
|
2024-03-27 17:56:34 +08:00
|
|
|
import { IKnowledgeFile } from '@/interfaces/database/knowledge';
|
|
|
|
import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
|
2024-05-15 11:34:57 +08:00
|
|
|
import api from '@/utils/api';
|
2024-05-16 20:15:02 +08:00
|
|
|
import { getAuthorization } from '@/utils/authorizationUtil';
|
2024-04-25 19:06:24 +08:00
|
|
|
import { PaginationProps } from 'antd';
|
2024-05-11 16:03:07 +08:00
|
|
|
import axios from 'axios';
|
2024-05-16 20:15:02 +08:00
|
|
|
import { EventSourceParserStream } from 'eventsource-parser/stream';
|
2024-06-06 19:29:36 +08:00
|
|
|
import {
|
|
|
|
ChangeEventHandler,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'react';
|
2024-04-07 17:41:29 +08:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-25 19:06:24 +08:00
|
|
|
import { useDispatch } from 'umi';
|
|
|
|
import { useSetModalState, useTranslate } from './commonHooks';
|
2024-03-27 17:56:34 +08:00
|
|
|
import { useSetDocumentParser } from './documentHooks';
|
|
|
|
import { useOneNamespaceEffectsLoading } from './storeHooks';
|
2024-04-07 17:41:29 +08:00
|
|
|
import { useSaveSetting } from './userSettingHook';
|
2024-03-27 17:56:34 +08:00
|
|
|
|
|
|
|
export const useChangeDocumentParser = (documentId: string) => {
|
|
|
|
const setDocumentParser = useSetDocumentParser();
|
|
|
|
|
|
|
|
const {
|
|
|
|
visible: changeParserVisible,
|
|
|
|
hideModal: hideChangeParserModal,
|
|
|
|
showModal: showChangeParserModal,
|
|
|
|
} = useSetModalState();
|
|
|
|
const loading = useOneNamespaceEffectsLoading('kFModel', [
|
|
|
|
'document_change_parser',
|
|
|
|
]);
|
|
|
|
|
|
|
|
const onChangeParserOk = useCallback(
|
|
|
|
async (parserId: string, parserConfig: IChangeParserConfigRequestBody) => {
|
|
|
|
const ret = await setDocumentParser(parserId, documentId, parserConfig);
|
|
|
|
if (ret === 0) {
|
|
|
|
hideChangeParserModal();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[hideChangeParserModal, setDocumentParser, documentId],
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
changeParserLoading: loading,
|
|
|
|
onChangeParserOk,
|
|
|
|
changeParserVisible,
|
|
|
|
hideChangeParserModal,
|
|
|
|
showChangeParserModal,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useSetSelectedRecord = <T = IKnowledgeFile>() => {
|
|
|
|
const [currentRecord, setCurrentRecord] = useState<T>({} as T);
|
|
|
|
|
|
|
|
const setRecord = (record: T) => {
|
|
|
|
setCurrentRecord(record);
|
|
|
|
};
|
|
|
|
|
|
|
|
return { currentRecord, setRecord };
|
|
|
|
};
|
2024-04-07 17:41:29 +08:00
|
|
|
|
|
|
|
export const useChangeLanguage = () => {
|
|
|
|
const { i18n } = useTranslation();
|
|
|
|
const saveSetting = useSaveSetting();
|
|
|
|
|
|
|
|
const changeLanguage = (lng: string) => {
|
2024-04-12 11:41:00 +08:00
|
|
|
i18n.changeLanguage(
|
|
|
|
LanguageTranslationMap[lng as keyof typeof LanguageTranslationMap],
|
|
|
|
);
|
2024-04-07 17:41:29 +08:00
|
|
|
saveSetting({ language: lng });
|
|
|
|
};
|
|
|
|
|
|
|
|
return changeLanguage;
|
|
|
|
};
|
2024-04-25 19:06:24 +08:00
|
|
|
|
|
|
|
export const useGetPagination = (
|
|
|
|
total: number,
|
|
|
|
page: number,
|
|
|
|
pageSize: number,
|
|
|
|
onPageChange: PaginationProps['onChange'],
|
|
|
|
) => {
|
|
|
|
const { t } = useTranslate('common');
|
|
|
|
|
|
|
|
const pagination: PaginationProps = useMemo(() => {
|
|
|
|
return {
|
|
|
|
showQuickJumper: true,
|
|
|
|
total,
|
|
|
|
showSizeChanger: true,
|
|
|
|
current: page,
|
|
|
|
pageSize: pageSize,
|
|
|
|
pageSizeOptions: [1, 2, 10, 20, 50, 100],
|
|
|
|
onChange: onPageChange,
|
|
|
|
showTotal: (total) => `${t('total')} ${total}`,
|
|
|
|
};
|
|
|
|
}, [t, onPageChange, page, pageSize, total]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
pagination,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useSetPagination = (namespace: string) => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
const setPagination = useCallback(
|
|
|
|
(pageNumber = 1, pageSize?: number) => {
|
|
|
|
const pagination: Pagination = {
|
|
|
|
current: pageNumber,
|
|
|
|
} as Pagination;
|
|
|
|
if (pageSize) {
|
|
|
|
pagination.pageSize = pageSize;
|
|
|
|
}
|
|
|
|
dispatch({
|
|
|
|
type: `${namespace}/setPagination`,
|
|
|
|
payload: pagination,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch, namespace],
|
|
|
|
);
|
|
|
|
|
|
|
|
return setPagination;
|
|
|
|
};
|
2024-05-11 16:03:07 +08:00
|
|
|
|
|
|
|
export interface AppConf {
|
|
|
|
appName: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useFetchAppConf = () => {
|
|
|
|
const [appConf, setAppConf] = useState<AppConf>({} as AppConf);
|
|
|
|
const fetchAppConf = useCallback(async () => {
|
|
|
|
const ret = await axios.get('/conf.json');
|
|
|
|
|
|
|
|
setAppConf(ret.data);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchAppConf();
|
|
|
|
}, [fetchAppConf]);
|
|
|
|
|
|
|
|
return appConf;
|
|
|
|
};
|
2024-05-15 11:34:57 +08:00
|
|
|
|
2024-05-16 20:15:02 +08:00
|
|
|
export const useSendMessageWithSse = (
|
|
|
|
url: string = api.completeConversation,
|
|
|
|
) => {
|
|
|
|
const [answer, setAnswer] = useState<IAnswer>({} as IAnswer);
|
|
|
|
const [done, setDone] = useState(true);
|
2024-05-15 11:34:57 +08:00
|
|
|
|
|
|
|
const send = useCallback(
|
|
|
|
async (body: any) => {
|
2024-05-16 20:15:02 +08:00
|
|
|
try {
|
|
|
|
setDone(false);
|
|
|
|
const response = await fetch(url, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[Authorization]: getAuthorization(),
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
});
|
|
|
|
|
|
|
|
const reader = response?.body
|
|
|
|
?.pipeThrough(new TextDecoderStream())
|
|
|
|
.pipeThrough(new EventSourceParserStream())
|
|
|
|
.getReader();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const x = await reader?.read();
|
|
|
|
if (x) {
|
|
|
|
const { done, value } = x;
|
|
|
|
try {
|
|
|
|
const val = JSON.parse(value?.data || '');
|
|
|
|
const d = val?.data;
|
|
|
|
if (typeof d !== 'boolean') {
|
|
|
|
console.info('data:', d);
|
|
|
|
setAnswer(d);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
if (done) {
|
|
|
|
console.info('done');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.info('done?');
|
|
|
|
setDone(true);
|
|
|
|
return response;
|
|
|
|
} catch (e) {
|
|
|
|
setDone(true);
|
|
|
|
console.warn(e);
|
2024-05-15 11:34:57 +08:00
|
|
|
}
|
|
|
|
},
|
2024-05-16 20:15:02 +08:00
|
|
|
[url],
|
2024-05-15 11:34:57 +08:00
|
|
|
);
|
|
|
|
|
2024-05-16 20:15:02 +08:00
|
|
|
return { send, answer, done };
|
2024-05-15 11:34:57 +08:00
|
|
|
};
|
2024-06-06 19:29:36 +08:00
|
|
|
|
|
|
|
//#region chat hooks
|
|
|
|
|
|
|
|
export const useScrollToBottom = (id?: string) => {
|
|
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const scrollToBottom = useCallback(() => {
|
|
|
|
if (id) {
|
|
|
|
ref.current?.scrollIntoView({ behavior: 'instant' });
|
|
|
|
}
|
|
|
|
}, [id]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
scrollToBottom();
|
|
|
|
}, [scrollToBottom]);
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useHandleMessageInputChange = () => {
|
|
|
|
const [value, setValue] = useState('');
|
|
|
|
|
|
|
|
const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {
|
|
|
|
const value = e.target.value;
|
|
|
|
const nextValue = value.replaceAll('\\n', '\n').replaceAll('\\t', '\t');
|
|
|
|
setValue(nextValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
handleInputChange,
|
|
|
|
value,
|
|
|
|
setValue,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// #endregion
|