2024-02-19 19:16:23 +08:00
|
|
|
import { ReactComponent as ChatConfigurationAtom } from '@/assets/svg/chat-configuration-atom.svg';
|
|
|
|
import { IModalManagerChildrenProps } from '@/components/modal-manager';
|
2024-03-04 17:03:23 +08:00
|
|
|
import { Divider, Flex, Form, Modal, Segmented, UploadFile } from 'antd';
|
2024-02-19 19:16:23 +08:00
|
|
|
import { SegmentedValue } from 'antd/es/segmented';
|
2024-02-20 18:10:20 +08:00
|
|
|
import omit from 'lodash/omit';
|
2024-02-22 17:14:25 +08:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2024-02-19 19:16:23 +08:00
|
|
|
import AssistantSetting from './assistant-setting';
|
|
|
|
import ModelSetting from './model-setting';
|
|
|
|
import PromptEngine from './prompt-engine';
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
import { useOneNamespaceEffectsLoading } from '@/hooks/storeHooks';
|
|
|
|
import { variableEnabledFieldMap } from '../constants';
|
|
|
|
import { useFetchDialog, useResetCurrentDialog, useSetDialog } from '../hooks';
|
|
|
|
import { IPromptConfigParameters } from '../interface';
|
|
|
|
import { excludeUnEnabledVariables } from '../utils';
|
2024-02-19 19:16:23 +08:00
|
|
|
import styles from './index.less';
|
|
|
|
|
|
|
|
enum ConfigurationSegmented {
|
|
|
|
AssistantSetting = 'Assistant Setting',
|
|
|
|
PromptEngine = 'Prompt Engine',
|
2024-02-20 18:10:20 +08:00
|
|
|
ModelSetting = 'Model Setting',
|
2024-02-19 19:16:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const segmentedMap = {
|
|
|
|
[ConfigurationSegmented.AssistantSetting]: AssistantSetting,
|
|
|
|
[ConfigurationSegmented.ModelSetting]: ModelSetting,
|
|
|
|
[ConfigurationSegmented.PromptEngine]: PromptEngine,
|
|
|
|
};
|
|
|
|
|
|
|
|
const layout = {
|
2024-03-05 12:01:48 +08:00
|
|
|
labelCol: { span: 7 },
|
|
|
|
wrapperCol: { span: 17 },
|
2024-02-19 19:16:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const validateMessages = {
|
|
|
|
required: '${label} is required!',
|
|
|
|
types: {
|
|
|
|
email: '${label} is not a valid email!',
|
|
|
|
number: '${label} is not a valid number!',
|
|
|
|
},
|
|
|
|
number: {
|
|
|
|
range: '${label} must be between ${min} and ${max}',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
interface IProps extends IModalManagerChildrenProps {
|
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ChatConfigurationModal = ({ visible, hideModal, id }: IProps) => {
|
2024-02-19 19:16:23 +08:00
|
|
|
const [form] = Form.useForm();
|
|
|
|
const [value, setValue] = useState<ConfigurationSegmented>(
|
|
|
|
ConfigurationSegmented.AssistantSetting,
|
|
|
|
);
|
2024-02-22 17:14:25 +08:00
|
|
|
const promptEngineRef = useRef<Array<IPromptConfigParameters>>([]);
|
|
|
|
const loading = useOneNamespaceEffectsLoading('chatModel', ['setDialog']);
|
2024-02-20 18:10:20 +08:00
|
|
|
|
|
|
|
const setDialog = useSetDialog();
|
2024-02-22 17:14:25 +08:00
|
|
|
const currentDialog = useFetchDialog(id, visible);
|
|
|
|
const { resetCurrentDialog } = useResetCurrentDialog();
|
2024-02-19 19:16:23 +08:00
|
|
|
|
|
|
|
const handleOk = async () => {
|
2024-02-20 18:10:20 +08:00
|
|
|
const values = await form.validateFields();
|
2024-02-22 17:14:25 +08:00
|
|
|
const nextValues: any = omit(values, [
|
|
|
|
...Object.keys(variableEnabledFieldMap),
|
|
|
|
'parameters',
|
|
|
|
...excludeUnEnabledVariables(values),
|
|
|
|
]);
|
|
|
|
const emptyResponse = nextValues.prompt_config?.empty_response ?? '';
|
2024-03-04 17:03:23 +08:00
|
|
|
|
|
|
|
const fileList = values.icon;
|
|
|
|
let icon;
|
|
|
|
|
|
|
|
if (Array.isArray(fileList) && fileList.length > 0) {
|
|
|
|
icon = fileList[0].thumbUrl;
|
|
|
|
}
|
|
|
|
|
2024-02-20 18:10:20 +08:00
|
|
|
const finalValues = {
|
2024-02-22 17:14:25 +08:00
|
|
|
dialog_id: id,
|
2024-02-20 18:10:20 +08:00
|
|
|
...nextValues,
|
|
|
|
prompt_config: {
|
|
|
|
...nextValues.prompt_config,
|
|
|
|
parameters: promptEngineRef.current,
|
2024-02-22 17:14:25 +08:00
|
|
|
empty_response: emptyResponse,
|
2024-02-20 18:10:20 +08:00
|
|
|
},
|
2024-03-04 17:03:23 +08:00
|
|
|
icon,
|
2024-02-20 18:10:20 +08:00
|
|
|
};
|
|
|
|
console.info(promptEngineRef.current);
|
|
|
|
console.info(nextValues);
|
|
|
|
console.info(finalValues);
|
2024-02-22 17:14:25 +08:00
|
|
|
const retcode: number = await setDialog(finalValues);
|
|
|
|
if (retcode === 0) {
|
|
|
|
hideModal();
|
|
|
|
}
|
2024-02-19 19:16:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
hideModal();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSegmentedChange = (val: SegmentedValue) => {
|
|
|
|
setValue(val as ConfigurationSegmented);
|
|
|
|
};
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
const handleModalAfterClose = () => {
|
|
|
|
resetCurrentDialog();
|
|
|
|
form.resetFields();
|
|
|
|
};
|
|
|
|
|
2024-02-19 19:16:23 +08:00
|
|
|
const title = (
|
|
|
|
<Flex gap={16}>
|
|
|
|
<ChatConfigurationAtom></ChatConfigurationAtom>
|
|
|
|
<div>
|
|
|
|
<b>Chat Configuration</b>
|
|
|
|
<div className={styles.chatConfigurationDescription}>
|
|
|
|
Here, dress up a dedicated assistant for your special knowledge bases!
|
|
|
|
💕
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
useEffect(() => {
|
2024-03-05 19:28:44 +08:00
|
|
|
if (visible) {
|
|
|
|
const icon = currentDialog.icon;
|
|
|
|
let fileList: UploadFile[] = [];
|
|
|
|
|
|
|
|
if (icon) {
|
|
|
|
fileList = [{ uid: '1', name: 'file', thumbUrl: icon, status: 'done' }];
|
|
|
|
}
|
|
|
|
form.setFieldsValue({ ...currentDialog, icon: fileList });
|
2024-03-04 17:03:23 +08:00
|
|
|
}
|
2024-03-05 19:28:44 +08:00
|
|
|
}, [currentDialog, form, visible]);
|
2024-02-22 17:14:25 +08:00
|
|
|
|
2024-02-19 19:16:23 +08:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title={title}
|
|
|
|
width={688}
|
|
|
|
open={visible}
|
|
|
|
onOk={handleOk}
|
|
|
|
onCancel={handleCancel}
|
2024-02-22 17:14:25 +08:00
|
|
|
confirmLoading={loading}
|
|
|
|
destroyOnClose
|
|
|
|
afterClose={handleModalAfterClose}
|
2024-02-19 19:16:23 +08:00
|
|
|
>
|
|
|
|
<Segmented
|
|
|
|
size={'large'}
|
|
|
|
value={value}
|
|
|
|
onChange={handleSegmentedChange}
|
|
|
|
options={Object.values(ConfigurationSegmented)}
|
|
|
|
block
|
|
|
|
/>
|
|
|
|
<Divider></Divider>
|
|
|
|
<Form
|
|
|
|
{...layout}
|
|
|
|
name="nest-messages"
|
|
|
|
form={form}
|
|
|
|
style={{ maxWidth: 600 }}
|
|
|
|
validateMessages={validateMessages}
|
|
|
|
colon={false}
|
|
|
|
>
|
|
|
|
{Object.entries(segmentedMap).map(([key, Element]) => (
|
2024-02-20 18:10:20 +08:00
|
|
|
<Element
|
|
|
|
key={key}
|
|
|
|
show={key === value}
|
|
|
|
form={form}
|
|
|
|
{...(key === ConfigurationSegmented.PromptEngine
|
|
|
|
? { ref: promptEngineRef }
|
|
|
|
: {})}
|
|
|
|
></Element>
|
2024-02-19 19:16:23 +08:00
|
|
|
))}
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChatConfigurationModal;
|