File size: 15,831 Bytes
809dc5c 8e222fd 7c07000 11e3284 68ed806 7ba250b 68ed806 7d8f5e6 eedc75d 8a4bb0c 7d8f5e6 e68f488 8e222fd 809dc5c 11e3284 3726b19 7043f18 11e3284 058cd84 8c4ec99 809dc5c f30b544 11e3284 058cd84 11e3284 058cd84 11e3284 058cd84 11e3284 058cd84 7c07000 e441caf 058cd84 11e3284 058cd84 11e3284 058cd84 6a6f6eb f30b544 11e3284 f30b544 64dd66b f30b544 64dd66b f30b544 64dd66b f30b544 11e3284 196c662 f30b544 64dd66b f30b544 058cd84 7c07000 11e3284 aa396c5 11e3284 aa396c5 809dc5c 11e3284 aa396c5 809dc5c ef0f1be 809dc5c ef0f1be 809dc5c 38eebf9 ef0f1be 809dc5c ef0f1be 809dc5c ef0f1be aa396c5 809dc5c aa396c5 809dc5c aa396c5 11e3284 aa396c5 058cd84 11e3284 058cd84 414b2e9 809dc5c 7c07000 809dc5c 7c07000 414b2e9 809dc5c 414b2e9 7c07000 414b2e9 058cd84 8a4bb0c 809dc5c 8a4bb0c 809dc5c 8a4bb0c 414b2e9 809dc5c 8a4bb0c 414b2e9 809dc5c 8a4bb0c 809dc5c 414b2e9 8a4bb0c 414b2e9 7043f18 dee8078 7043f18 058cd84 809dc5c 8a4bb0c 809dc5c 8a4bb0c 809dc5c 8a4bb0c 809dc5c 8a4bb0c 809dc5c 8a4bb0c 196c662 8a4bb0c 809dc5c 8a4bb0c 809dc5c 8a4bb0c 809dc5c 196c662 809dc5c 8a4bb0c 809dc5c 5281167 8a4bb0c 809dc5c 8a4bb0c 809dc5c 8a4bb0c 809dc5c 8a4bb0c 8e222fd 7c07000 e441caf 11e3284 6a6f6eb 7c07000 11e3284 809dc5c 6a6f6eb 7c07000 6a6f6eb 11e3284 6a6f6eb 11e3284 6a6f6eb 7c07000 809dc5c 6a6f6eb 196c662 6a6f6eb 7c07000 6a6f6eb 11e3284 196c662 6a6f6eb e55650e 809dc5c e55650e 985fa34 3726b19 985fa34 1ea3843 a4c4dfd 1ea3843 a4c4dfd 809dc5c a4c4dfd 1ea3843 a4c4dfd 1ea3843 058cd84 |
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
import { ChatSearchParams, MessageType } from '@/constants/chat';
import { fileIconMap } from '@/constants/common';
import {
useFetchManualConversation,
useFetchManualDialog,
useFetchNextConversation,
useFetchNextConversationList,
useFetchNextDialog,
useGetChatSearchParams,
useRemoveNextConversation,
useRemoveNextDialog,
useSetNextDialog,
useUpdateNextConversation,
} from '@/hooks/chat-hooks';
import {
useSetModalState,
useShowDeleteConfirm,
useTranslate,
} from '@/hooks/common-hooks';
import {
useRegenerateMessage,
useSelectDerivedMessages,
useSendMessageWithSse,
} from '@/hooks/logic-hooks';
import { IConversation, IDialog, Message } from '@/interfaces/database/chat';
import { getFileExtension } from '@/utils';
import api from '@/utils/api';
import { getConversationId } from '@/utils/chat';
import { useMutationState } from '@tanstack/react-query';
import { get } from 'lodash';
import trim from 'lodash/trim';
import {
ChangeEventHandler,
useCallback,
useEffect,
useMemo,
useState,
} from 'react';
import { useSearchParams } from 'umi';
import { v4 as uuid } from 'uuid';
import {
IClientConversation,
IMessage,
VariableTableDataType,
} from './interface';
export const useSetChatRouteParams = () => {
const [currentQueryParameters, setSearchParams] = useSearchParams();
const newQueryParameters: URLSearchParams = useMemo(
() => new URLSearchParams(currentQueryParameters.toString()),
[currentQueryParameters],
);
const setConversationIsNew = useCallback(
(value: string) => {
newQueryParameters.set(ChatSearchParams.isNew, value);
setSearchParams(newQueryParameters);
},
[newQueryParameters, setSearchParams],
);
const getConversationIsNew = useCallback(() => {
return newQueryParameters.get(ChatSearchParams.isNew);
}, [newQueryParameters]);
return { setConversationIsNew, getConversationIsNew };
};
export const useSetNewConversationRouteParams = () => {
const [currentQueryParameters, setSearchParams] = useSearchParams();
const newQueryParameters: URLSearchParams = useMemo(
() => new URLSearchParams(currentQueryParameters.toString()),
[currentQueryParameters],
);
const setNewConversationRouteParams = useCallback(
(conversationId: string, isNew: string) => {
newQueryParameters.set(ChatSearchParams.ConversationId, conversationId);
newQueryParameters.set(ChatSearchParams.isNew, isNew);
setSearchParams(newQueryParameters);
},
[newQueryParameters, setSearchParams],
);
return { setNewConversationRouteParams };
};
export const useSelectCurrentDialog = () => {
const data = useMutationState({
filters: { mutationKey: ['fetchDialog'] },
select: (mutation) => {
return get(mutation, 'state.data.data', {});
},
});
return (data.at(-1) ?? {}) as IDialog;
};
export const useSelectPromptConfigParameters = (): VariableTableDataType[] => {
const { data: currentDialog } = useFetchNextDialog();
const finalParameters: VariableTableDataType[] = useMemo(() => {
const parameters = currentDialog?.prompt_config?.parameters ?? [];
if (!currentDialog.id) {
// The newly created chat has a default parameter
return [{ key: uuid(), variable: 'knowledge', optional: false }];
}
return parameters.map((x) => ({
key: uuid(),
variable: x.key,
optional: x.optional,
}));
}, [currentDialog]);
return finalParameters;
};
export const useDeleteDialog = () => {
const showDeleteConfirm = useShowDeleteConfirm();
const { removeDialog } = useRemoveNextDialog();
const onRemoveDialog = (dialogIds: Array<string>) => {
showDeleteConfirm({ onOk: () => removeDialog(dialogIds) });
};
return { onRemoveDialog };
};
export const useHandleItemHover = () => {
const [activated, setActivated] = useState<string>('');
const handleItemEnter = (id: string) => {
setActivated(id);
};
const handleItemLeave = () => {
setActivated('');
};
return {
activated,
handleItemEnter,
handleItemLeave,
};
};
export const useEditDialog = () => {
const [dialog, setDialog] = useState<IDialog>({} as IDialog);
const { fetchDialog } = useFetchManualDialog();
const { setDialog: submitDialog, loading } = useSetNextDialog();
const {
visible: dialogEditVisible,
hideModal: hideDialogEditModal,
showModal: showDialogEditModal,
} = useSetModalState();
const hideModal = useCallback(() => {
setDialog({} as IDialog);
hideDialogEditModal();
}, [hideDialogEditModal]);
const onDialogEditOk = useCallback(
async (dialog: IDialog) => {
const ret = await submitDialog(dialog);
if (ret === 0) {
hideModal();
}
},
[submitDialog, hideModal],
);
const handleShowDialogEditModal = useCallback(
async (dialogId?: string) => {
if (dialogId) {
const ret = await fetchDialog(dialogId);
if (ret.code === 0) {
setDialog(ret.data);
}
}
showDialogEditModal();
},
[showDialogEditModal, fetchDialog],
);
const clearDialog = useCallback(() => {
setDialog({} as IDialog);
}, []);
return {
dialogSettingLoading: loading,
initialDialog: dialog,
onDialogEditOk,
dialogEditVisible,
hideDialogEditModal: hideModal,
showDialogEditModal: handleShowDialogEditModal,
clearDialog,
};
};
//#region conversation
export const useSelectDerivedConversationList = () => {
const { t } = useTranslate('chat');
const [list, setList] = useState<Array<IConversation>>([]);
const { data: currentDialog } = useFetchNextDialog();
const { data: conversationList, loading } = useFetchNextConversationList();
const { dialogId } = useGetChatSearchParams();
const prologue = currentDialog?.prompt_config?.prologue ?? '';
const { setNewConversationRouteParams } = useSetNewConversationRouteParams();
const addTemporaryConversation = useCallback(() => {
const conversationId = getConversationId();
setList((pre) => {
if (dialogId) {
setNewConversationRouteParams(conversationId, 'true');
const nextList = [
{
id: conversationId,
name: t('newConversation'),
dialog_id: dialogId,
is_new: true,
message: [
{
content: prologue,
role: MessageType.Assistant,
},
],
} as any,
...conversationList,
];
return nextList;
}
return pre;
});
}, [conversationList, dialogId, prologue, t, setNewConversationRouteParams]);
// When you first enter the page, select the top conversation card
useEffect(() => {
setList([...conversationList]);
}, [conversationList]);
return { list, addTemporaryConversation, loading };
};
export const useSetConversation = () => {
const { dialogId } = useGetChatSearchParams();
const { updateConversation } = useUpdateNextConversation();
const setConversation = useCallback(
async (
message: string,
isNew: boolean = false,
conversationId?: string,
) => {
const data = await updateConversation({
dialog_id: dialogId,
name: message,
is_new: isNew,
conversation_id: conversationId,
message: [
{
role: MessageType.Assistant,
content: message,
},
],
});
return data;
},
[updateConversation, dialogId],
);
return { setConversation };
};
export const useSelectNextMessages = () => {
const {
ref,
setDerivedMessages,
derivedMessages,
addNewestAnswer,
addNewestQuestion,
removeLatestMessage,
removeMessageById,
removeMessagesAfterCurrentMessage,
} = useSelectDerivedMessages();
const { data: conversation, loading } = useFetchNextConversation();
const { data: dialog } = useFetchNextDialog();
const { conversationId, dialogId, isNew } = useGetChatSearchParams();
const addPrologue = useCallback(() => {
if (dialogId !== '' && isNew === 'true') {
const prologue = dialog.prompt_config?.prologue;
const nextMessage = {
role: MessageType.Assistant,
content: prologue,
id: uuid(),
} as IMessage;
setDerivedMessages([nextMessage]);
}
}, [isNew, dialog, dialogId, setDerivedMessages]);
useEffect(() => {
addPrologue();
}, [addPrologue]);
useEffect(() => {
if (
conversationId &&
isNew !== 'true' &&
conversation.message?.length > 0
) {
setDerivedMessages(conversation.message);
}
if (!conversationId) {
setDerivedMessages([]);
}
}, [conversation.message, conversationId, setDerivedMessages, isNew]);
return {
ref,
derivedMessages,
loading,
addNewestAnswer,
addNewestQuestion,
removeLatestMessage,
removeMessageById,
removeMessagesAfterCurrentMessage,
};
};
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,
};
};
export const useSendNextMessage = (controller: AbortController) => {
const { setConversation } = useSetConversation();
const { conversationId, isNew } = useGetChatSearchParams();
const { handleInputChange, value, setValue } = useHandleMessageInputChange();
const { send, answer, done } = useSendMessageWithSse(
api.completeConversation,
);
const {
ref,
derivedMessages,
loading,
addNewestAnswer,
addNewestQuestion,
removeLatestMessage,
removeMessageById,
removeMessagesAfterCurrentMessage,
} = useSelectNextMessages();
const { setConversationIsNew, getConversationIsNew } =
useSetChatRouteParams();
const sendMessage = useCallback(
async ({
message,
currentConversationId,
messages,
}: {
message: Message;
currentConversationId?: string;
messages?: Message[];
}) => {
const res = await send(
{
conversation_id: currentConversationId ?? conversationId,
messages: [...(messages ?? derivedMessages ?? []), message],
},
controller,
);
if (res && (res?.response.status !== 200 || res?.data?.code !== 0)) {
// cancel loading
setValue(message.content);
console.info('removeLatestMessage111');
removeLatestMessage();
}
},
[
derivedMessages,
conversationId,
removeLatestMessage,
setValue,
send,
controller,
],
);
const handleSendMessage = useCallback(
async (message: Message) => {
const isNew = getConversationIsNew();
if (isNew !== 'true') {
sendMessage({ message });
} else {
const data = await setConversation(
message.content,
true,
conversationId,
);
if (data.code === 0) {
setConversationIsNew('');
const id = data.data.id;
// currentConversationIdRef.current = id;
sendMessage({
message,
currentConversationId: id,
messages: data.data.message,
});
}
}
},
[
setConversation,
sendMessage,
setConversationIsNew,
getConversationIsNew,
conversationId,
],
);
const { regenerateMessage } = useRegenerateMessage({
removeMessagesAfterCurrentMessage,
sendMessage,
messages: derivedMessages,
});
useEffect(() => {
// #1289
if (answer.answer && conversationId && isNew !== 'true') {
addNewestAnswer(answer);
}
}, [answer, addNewestAnswer, conversationId, isNew]);
const handlePressEnter = useCallback(
(documentIds: string[]) => {
if (trim(value) === '') return;
const id = uuid();
addNewestQuestion({
content: value,
doc_ids: documentIds,
id,
role: MessageType.User,
});
if (done) {
setValue('');
handleSendMessage({
id,
content: value.trim(),
role: MessageType.User,
doc_ids: documentIds,
});
}
},
[addNewestQuestion, handleSendMessage, done, setValue, value],
);
return {
handlePressEnter,
handleInputChange,
value,
setValue,
regenerateMessage,
sendLoading: !done,
loading,
ref,
derivedMessages,
removeMessageById,
};
};
export const useGetFileIcon = () => {
const getFileIcon = (filename: string) => {
const ext: string = getFileExtension(filename);
const iconPath = fileIconMap[ext as keyof typeof fileIconMap];
return `@/assets/svg/file-icon/${iconPath}`;
};
return getFileIcon;
};
export const useDeleteConversation = () => {
const showDeleteConfirm = useShowDeleteConfirm();
const { removeConversation } = useRemoveNextConversation();
const deleteConversation = (conversationIds: Array<string>) => async () => {
const ret = await removeConversation(conversationIds);
return ret;
};
const onRemoveConversation = (conversationIds: Array<string>) => {
showDeleteConfirm({ onOk: deleteConversation(conversationIds) });
};
return { onRemoveConversation };
};
export const useRenameConversation = () => {
const [conversation, setConversation] = useState<IClientConversation>(
{} as IClientConversation,
);
const { fetchConversation } = useFetchManualConversation();
const {
visible: conversationRenameVisible,
hideModal: hideConversationRenameModal,
showModal: showConversationRenameModal,
} = useSetModalState();
const { updateConversation, loading } = useUpdateNextConversation();
const onConversationRenameOk = useCallback(
async (name: string) => {
const ret = await updateConversation({
...conversation,
conversation_id: conversation.id,
name,
is_new: false,
});
if (ret.code === 0) {
hideConversationRenameModal();
}
},
[updateConversation, conversation, hideConversationRenameModal],
);
const handleShowConversationRenameModal = useCallback(
async (conversationId: string) => {
const ret = await fetchConversation(conversationId);
if (ret.code === 0) {
setConversation(ret.data);
}
showConversationRenameModal();
},
[showConversationRenameModal, fetchConversation],
);
return {
conversationRenameLoading: loading,
initialConversationName: conversation.name,
onConversationRenameOk,
conversationRenameVisible,
hideConversationRenameModal,
showConversationRenameModal: handleShowConversationRenameModal,
};
};
export const useGetSendButtonDisabled = () => {
const { dialogId, conversationId } = useGetChatSearchParams();
return dialogId === '' || conversationId === '';
};
export const useSendButtonDisabled = (value: string) => {
return trim(value) === '';
};
export const useCreateConversationBeforeUploadDocument = () => {
const { setConversation } = useSetConversation();
const { dialogId } = useGetChatSearchParams();
const { getConversationIsNew } = useSetChatRouteParams();
const createConversationBeforeUploadDocument = useCallback(
async (message: string) => {
const isNew = getConversationIsNew();
if (isNew === 'true') {
const data = await setConversation(message, true);
return data;
}
},
[setConversation, getConversationIsNew],
);
return {
createConversationBeforeUploadDocument,
dialogId,
};
};
//#endregion
|