mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-24 07:26:47 +08:00
### What problem does this PR solve? Feat: Add the JS code (or other) executor component to Agent. #4977 ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
66
web/src/pages/flow/form/code-form/dynamic-input-variable.tsx
Normal file
66
web/src/pages/flow/form/code-form/dynamic-input-variable.tsx
Normal file
@ -0,0 +1,66 @@
|
||||
import { RAGFlowNodeType } from '@/interfaces/database/flow';
|
||||
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
|
||||
import { Button, Form, Input, Select } from 'antd';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useBuildComponentIdSelectOptions } from '../../hooks/use-get-begin-query';
|
||||
import { FormCollapse } from '../components/dynamic-input-variable';
|
||||
|
||||
type DynamicInputVariableProps = {
|
||||
name?: string;
|
||||
node?: RAGFlowNodeType;
|
||||
};
|
||||
|
||||
export const DynamicInputVariable = ({
|
||||
name = 'arguments',
|
||||
node,
|
||||
}: DynamicInputVariableProps) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const valueOptions = useBuildComponentIdSelectOptions(
|
||||
node?.id,
|
||||
node?.parentId,
|
||||
);
|
||||
|
||||
return (
|
||||
<FormCollapse title={t('flow.inputVariables')}>
|
||||
<Form.List name={name}>
|
||||
{(fields, { add, remove }) => (
|
||||
<>
|
||||
{fields.map(({ key, name, ...restField }) => (
|
||||
<div key={key} className="flex items-center gap-2 pb-4">
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, 'name']}
|
||||
className="m-0 flex-1"
|
||||
>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, 'component_id']}
|
||||
className="m-0 flex-1"
|
||||
>
|
||||
<Select
|
||||
placeholder={t('common.pleaseSelect')}
|
||||
options={valueOptions}
|
||||
></Select>
|
||||
</Form.Item>
|
||||
<MinusCircleOutlined onClick={() => remove(name)} />
|
||||
</div>
|
||||
))}
|
||||
<Form.Item>
|
||||
<Button
|
||||
type="dashed"
|
||||
onClick={() => add()}
|
||||
block
|
||||
icon={<PlusOutlined />}
|
||||
>
|
||||
{t('flow.addVariable')}
|
||||
</Button>
|
||||
</Form.Item>
|
||||
</>
|
||||
)}
|
||||
</Form.List>
|
||||
</FormCollapse>
|
||||
);
|
||||
};
|
||||
@ -0,0 +1,66 @@
|
||||
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
|
||||
import { Button, Form, Input, Select } from 'antd';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { FormCollapse } from '../components/dynamic-input-variable';
|
||||
|
||||
type DynamicOutputVariableProps = {
|
||||
name?: string;
|
||||
};
|
||||
|
||||
const options = [
|
||||
'String',
|
||||
'Number',
|
||||
'Boolean',
|
||||
'Array[String]',
|
||||
'Array[Number]',
|
||||
'Object',
|
||||
].map((x) => ({ label: x, value: x }));
|
||||
|
||||
export const DynamicOutputVariable = ({
|
||||
name = 'output',
|
||||
}: DynamicOutputVariableProps) => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return (
|
||||
<FormCollapse title={t('flow.output')}>
|
||||
<Form.List name={name}>
|
||||
{(fields, { add, remove }) => (
|
||||
<>
|
||||
{fields.map(({ key, name, ...restField }) => (
|
||||
<div key={key} className="flex items-center gap-2 pb-4">
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, 'first']}
|
||||
className="m-0 flex-1"
|
||||
>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, 'last']}
|
||||
className="m-0 flex-1"
|
||||
>
|
||||
<Select
|
||||
placeholder={t('common.pleaseSelect')}
|
||||
options={options}
|
||||
></Select>
|
||||
</Form.Item>
|
||||
<MinusCircleOutlined onClick={() => remove(name)} />
|
||||
</div>
|
||||
))}
|
||||
<Form.Item>
|
||||
<Button
|
||||
type="dashed"
|
||||
onClick={() => add()}
|
||||
block
|
||||
icon={<PlusOutlined />}
|
||||
>
|
||||
{t('flow.addVariable')}
|
||||
</Button>
|
||||
</Form.Item>
|
||||
</>
|
||||
)}
|
||||
</Form.List>
|
||||
</FormCollapse>
|
||||
);
|
||||
};
|
||||
16
web/src/pages/flow/form/code-form/index.less
Normal file
16
web/src/pages/flow/form/code-form/index.less
Normal file
@ -0,0 +1,16 @@
|
||||
.languageItem {
|
||||
margin: 0;
|
||||
:global(.ant-select-selector) {
|
||||
background: transparent !important;
|
||||
border: none !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
:global(.ant-select-selector:hover) {
|
||||
border: none !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
:global(.ant-select-focused .ant-select-selector) {
|
||||
border: none !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
}
|
||||
67
web/src/pages/flow/form/code-form/index.tsx
Normal file
67
web/src/pages/flow/form/code-form/index.tsx
Normal file
@ -0,0 +1,67 @@
|
||||
import Editor, { loader } from '@monaco-editor/react';
|
||||
import { Form, Select } from 'antd';
|
||||
import { IOperatorForm } from '../../interface';
|
||||
import { DynamicInputVariable } from './dynamic-input-variable';
|
||||
|
||||
import { CodeTemplateStrMap, ProgrammingLanguage } from '@/constants/agent';
|
||||
import { ICodeForm } from '@/interfaces/database/flow';
|
||||
import { useEffect } from 'react';
|
||||
import styles from './index.less';
|
||||
|
||||
loader.config({ paths: { vs: '/vs' } });
|
||||
|
||||
const options = [
|
||||
ProgrammingLanguage.Python,
|
||||
ProgrammingLanguage.Javascript,
|
||||
].map((x) => ({ value: x, label: x }));
|
||||
|
||||
const CodeForm = ({ onValuesChange, form, node }: IOperatorForm) => {
|
||||
const formData = node?.data.form as ICodeForm;
|
||||
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
// TODO: Direct operation zustand is more elegant
|
||||
form?.setFieldValue(
|
||||
'script',
|
||||
CodeTemplateStrMap[formData.lang as ProgrammingLanguage],
|
||||
);
|
||||
}, 0);
|
||||
}, [form, formData.lang]);
|
||||
|
||||
return (
|
||||
<Form
|
||||
name="basic"
|
||||
autoComplete="off"
|
||||
form={form}
|
||||
onValuesChange={onValuesChange}
|
||||
layout={'vertical'}
|
||||
>
|
||||
<DynamicInputVariable node={node}></DynamicInputVariable>
|
||||
<Form.Item
|
||||
name={'script'}
|
||||
label={
|
||||
<Form.Item name={'lang'} className={styles.languageItem}>
|
||||
<Select
|
||||
defaultValue={'python'}
|
||||
popupMatchSelectWidth={false}
|
||||
options={options}
|
||||
/>
|
||||
</Form.Item>
|
||||
}
|
||||
className="bg-gray-100 rounded dark:bg-gray-800"
|
||||
>
|
||||
<Editor
|
||||
height={200}
|
||||
theme="vs-dark"
|
||||
language={formData.lang}
|
||||
options={{
|
||||
minimap: { enabled: false },
|
||||
automaticLayout: true,
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
|
||||
export default CodeForm;
|
||||
@ -22,7 +22,7 @@ const getVariableName = (type: string) =>
|
||||
type === VariableType.Reference ? 'component_id' : 'value';
|
||||
|
||||
const DynamicVariableForm = ({ name: formName, node }: IProps) => {
|
||||
formName = formName || 'query';
|
||||
const nextFormName = formName || 'query';
|
||||
const { t } = useTranslation();
|
||||
const valueOptions = useBuildComponentIdSelectOptions(
|
||||
node?.id,
|
||||
@ -38,15 +38,15 @@ const DynamicVariableForm = ({ name: formName, node }: IProps) => {
|
||||
const handleTypeChange = useCallback(
|
||||
(name: number) => () => {
|
||||
setTimeout(() => {
|
||||
form.setFieldValue([formName, name, 'component_id'], undefined);
|
||||
form.setFieldValue([formName, name, 'value'], undefined);
|
||||
form.setFieldValue([nextFormName, name, 'component_id'], undefined);
|
||||
form.setFieldValue([nextFormName, name, 'value'], undefined);
|
||||
}, 0);
|
||||
},
|
||||
[form],
|
||||
[form, nextFormName],
|
||||
);
|
||||
|
||||
return (
|
||||
<Form.List name={formName}>
|
||||
<Form.List name={nextFormName}>
|
||||
{(fields, { add, remove }) => (
|
||||
<>
|
||||
{fields.map(({ key, name, ...restField }) => (
|
||||
@ -63,7 +63,7 @@ const DynamicVariableForm = ({ name: formName, node }: IProps) => {
|
||||
</Form.Item>
|
||||
<Form.Item noStyle dependencies={[name, 'type']}>
|
||||
{({ getFieldValue }) => {
|
||||
const type = getFieldValue([formName, name, 'type']);
|
||||
const type = getFieldValue([nextFormName, name, 'type']);
|
||||
return (
|
||||
<Form.Item
|
||||
{...restField}
|
||||
|
||||
Reference in New Issue
Block a user