import { Authorization } from '@/constants/authorization'; import { useSetModalState } from '@/hooks/common-hooks'; import { useSetSelectedRecord } from '@/hooks/logic-hooks'; import { useHandleSubmittable } from '@/hooks/login-hooks'; import { IModalProps } from '@/interfaces/common'; import api from '@/utils/api'; import { getAuthorization } from '@/utils/authorization-util'; import { UploadOutlined } from '@ant-design/icons'; import { Button, Drawer, Form, FormItemProps, Input, InputNumber, Select, Switch, Upload, } from 'antd'; import { pick } from 'lodash'; import React, { useCallback, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { BeginQueryType } from '../constant'; import { useGetBeginNodeDataQuery, useSaveGraphBeforeOpeningDebugDrawer, } from '../hooks'; import { BeginQuery } from '../interface'; import useGraphStore from '../store'; import { getDrawerWidth } from '../utils'; import { PopoverForm } from './popover-form'; import { UploadChangeParam, UploadFile } from 'antd/es/upload'; import { Link } from 'lucide-react'; import styles from './index.less'; const RunDrawer = ({ hideModal, showModal: showChatModal, }: IModalProps) => { const { t } = useTranslation(); const [form] = Form.useForm(); const updateNodeForm = useGraphStore((state) => state.updateNodeForm); const { visible, hideModal: hidePopover, switchVisible, showModal: showPopover, } = useSetModalState(); const { setRecord, currentRecord } = useSetSelectedRecord(); const { submittable } = useHandleSubmittable(form); const [isUploading, setIsUploading] = useState(false); const handleShowPopover = useCallback( (idx: number) => () => { setRecord(idx); showPopover(); }, [setRecord, showPopover], ); const getBeginNodeDataQuery = useGetBeginNodeDataQuery(); const query: BeginQuery[] = getBeginNodeDataQuery(); const normFile = (e: any) => { if (Array.isArray(e)) { return e; } return e?.fileList; }; const onChange = useCallback( (optional: boolean) => ({ fileList }: UploadChangeParam) => { if (!optional) { setIsUploading(fileList.some((x) => x.status === 'uploading')); } }, [], ); const renderWidget = useCallback( (q: BeginQuery, idx: number) => { const props: FormItemProps & { key: number } = { key: idx, label: q.name, name: idx, }; if (q.optional === false) { props.rules = [{ required: true }]; } const urlList: { url: string; result: string }[] = form.getFieldValue(idx) || []; const BeginQueryTypeMap = { [BeginQueryType.Line]: ( ), [BeginQueryType.Paragraph]: ( ), [BeginQueryType.Options]: ( ), [BeginQueryType.File]: (
0 ? 'mb-1' : ''} noStyle >
), [BeginQueryType.Integer]: ( ), [BeginQueryType.Boolean]: ( ), }; return BeginQueryTypeMap[q.type as BeginQueryType]; }, [form, handleShowPopover, onChange, switchVisible, t, visible], ); const { handleRun } = useSaveGraphBeforeOpeningDebugDrawer(showChatModal!); const handleRunAgent = useCallback( (nextValues: Record) => { const currentNodes = updateNodeForm('begin', nextValues, ['query']); handleRun(currentNodes); hideModal?.(); }, [handleRun, hideModal, updateNodeForm], ); const onOk = useCallback(async () => { const values = await form.validateFields(); const nextValues = Object.entries(values).map(([key, value]) => { const item = query[Number(key)]; let nextValue = value; if (Array.isArray(value)) { nextValue = ``; value.forEach((x) => { nextValue += x?.originFileObj instanceof File ? `${x.name}\n${x.response?.data}\n----\n` : `${x.url}\n${x.result}\n----\n`; }); } return { ...item, value: nextValue }; }); handleRunAgent(nextValues); }, [form, handleRunAgent, query]); return (
{ if (name === 'urlForm') { const { basicForm } = forms; const urlInfo = basicForm.getFieldValue(currentRecord) || []; basicForm.setFieldsValue({ [currentRecord]: [...urlInfo, { ...values, name: values.url }], }); hidePopover(); } }} >
{query.map((x, idx) => { return renderWidget(x, idx); })}
); }; export default RunDrawer;