File size: 7,479 Bytes
1a0fce6 74ade83 bd39551 09502ac cb33b9e 71b7e06 1a0fce6 cb33b9e bd39551 15052fd cb33b9e 8207a08 88e5a61 bd39551 71b7e06 b2b0160 71b7e06 b2b0160 71b7e06 88e5a61 74ade83 88e5a61 bd39551 15052fd 1a0fce6 cb33b9e 1a0fce6 09502ac cb33b9e 09502ac cb33b9e 09502ac cb33b9e 1a0fce6 cb33b9e 1a0fce6 cb33b9e 1a0fce6 8207a08 8441328 8207a08 8441328 8207a08 8441328 8207a08 9ee6e3a b2b0160 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
import { Authorization } from '@/constants/authorization';
import { LanguageTranslationMap } from '@/constants/common';
import { Pagination } from '@/interfaces/common';
import { ResponseType } from '@/interfaces/database/base';
import { IAnswer } from '@/interfaces/database/chat';
import { IKnowledgeFile } from '@/interfaces/database/knowledge';
import { IChangeParserConfigRequestBody } from '@/interfaces/request/document';
import api from '@/utils/api';
import { getAuthorization } from '@/utils/authorizationUtil';
import { PaginationProps } from 'antd';
import axios from 'axios';
import { EventSourceParserStream } from 'eventsource-parser/stream';
import {
ChangeEventHandler,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch } from 'umi';
import { useSetModalState, useTranslate } from './commonHooks';
import { useSetDocumentParser } from './documentHooks';
import { useFetchLlmList } from './llmHooks';
import { useOneNamespaceEffectsLoading } from './storeHooks';
import {
useFetchTenantInfo,
useSaveSetting,
useSelectTenantInfo,
} from './userSettingHook';
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 };
};
export const useChangeLanguage = () => {
const { i18n } = useTranslation();
const saveSetting = useSaveSetting();
const changeLanguage = (lng: string) => {
i18n.changeLanguage(
LanguageTranslationMap[lng as keyof typeof LanguageTranslationMap],
);
saveSetting({ language: lng });
};
return changeLanguage;
};
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;
};
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;
};
export const useSendMessageWithSse = (
url: string = api.completeConversation,
) => {
const [answer, setAnswer] = useState<IAnswer>({} as IAnswer);
const [done, setDone] = useState(true);
const send = useCallback(
async (
body: any,
): Promise<{ response: Response; data: ResponseType } | undefined> => {
try {
setDone(false);
const response = await fetch(url, {
method: 'POST',
headers: {
[Authorization]: getAuthorization(),
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
const res = response.clone().json();
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 { data: await res, response };
} catch (e) {
setDone(true);
console.warn(e);
}
},
[url],
);
return { send, answer, done };
};
//#region chat hooks
export const useScrollToBottom = (messages?: unknown) => {
const ref = useRef<HTMLDivElement>(null);
const scrollToBottom = useCallback(() => {
if (messages) {
ref.current?.scrollIntoView({ behavior: 'instant' });
}
}, [messages]); // If the message changes, scroll to the bottom
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
/**
*
* @param defaultId
* used to switch between different items, similar to radio
* @returns
*/
export const useSelectItem = (defaultId?: string) => {
const [selectedId, setSelectedId] = useState('');
const handleItemClick = useCallback(
(id: string) => () => {
setSelectedId(id);
},
[],
);
useEffect(() => {
if (defaultId) {
setSelectedId(defaultId);
}
}, [defaultId]);
return { selectedId, handleItemClick };
};
export const useFetchModelId = (visible: boolean) => {
const fetchTenantInfo = useFetchTenantInfo(false);
const tenantInfo = useSelectTenantInfo();
useEffect(() => {
if (visible) {
fetchTenantInfo();
}
}, [visible, fetchTenantInfo]);
return tenantInfo?.llm_id ?? '';
};
export const useFetchLlmModelOnVisible = (visible: boolean) => {
const fetchLlmList = useFetchLlmList();
useEffect(() => {
if (visible) {
fetchLlmList();
}
}, [fetchLlmList, visible]);
};
|