ragflow/web/src/pages/agent/form/code-form/index.tsx
balibabu 558b252c5a
Feat: Switching the programming language of the code operator will switch the corresponding language template #3221 (#7770)
### What problem does this PR solve?

Feat: Switching the programming language of the code operator will
switch the corresponding language template #3221

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
2025-05-21 18:22:06 +08:00

78 lines
2.1 KiB
TypeScript

import Editor, { loader } from '@monaco-editor/react';
import { INextOperatorForm } from '../../interface';
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/components/ui/form';
import { RAGFlowSelect } from '@/components/ui/select';
import { CodeTemplateStrMap, ProgrammingLanguage } from '@/constants/agent';
import { ICodeForm } from '@/interfaces/database/flow';
import { useEffect } from 'react';
import { DynamicInputVariable } from './next-variable';
loader.config({ paths: { vs: '/vs' } });
const options = [
ProgrammingLanguage.Python,
ProgrammingLanguage.Javascript,
].map((x) => ({ value: x, label: x }));
const CodeForm = ({ form, node }: INextOperatorForm) => {
const formData = node?.data.form as ICodeForm;
useEffect(() => {
// TODO: Direct operation zustand is more elegant
form?.setValue(
'script',
CodeTemplateStrMap[formData.lang as ProgrammingLanguage],
);
}, [form, formData.lang]);
return (
<Form {...form}>
<DynamicInputVariable node={node}></DynamicInputVariable>
<FormField
control={form.control}
name="script"
render={({ field }) => (
<FormItem>
<FormLabel>
<FormField
control={form.control}
name="lang"
render={({ field }) => (
<FormItem>
<FormControl>
<RAGFlowSelect {...field} options={options} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</FormLabel>
<FormControl>
<Editor
height={600}
theme="vs-dark"
language={formData.lang}
options={{
minimap: { enabled: false },
automaticLayout: true,
}}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</Form>
);
};
export default CodeForm;