strategy-form.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { IChunkStrategyResponse } from '@/types/knowledge';
  2. import { Alert, Checkbox, Form, FormListFieldData, Input, InputNumber, Radio, RadioChangeEvent } from 'antd';
  3. import { useState } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. const { TextArea } = Input;
  6. type IProps = {
  7. strategies: Array<IChunkStrategyResponse>;
  8. docType: string;
  9. field: FormListFieldData;
  10. fileName: string;
  11. };
  12. /**
  13. * render strategies by doc type and file suffix
  14. */
  15. export default function StrategyForm({ strategies, docType, fileName, field }: IProps) {
  16. const [selectedStrategy, setSelectedStrategy] = useState<string>();
  17. let filleSuffix = '';
  18. if (docType === 'DOCUMENT') {
  19. // filter strategy by file suffix
  20. const arr = fileName.split('.');
  21. filleSuffix = arr[arr.length - 1];
  22. }
  23. const ableStrategies = filleSuffix ? strategies.filter(i => i.suffix.indexOf(filleSuffix) > -1) : strategies;
  24. const { t } = useTranslation();
  25. const DEFAULT_STRATEGY = {
  26. strategy: 'Automatic',
  27. name: t('Automatic'),
  28. desc: t('Automatic_desc'),
  29. };
  30. function radioChange(e: RadioChangeEvent) {
  31. setSelectedStrategy(e.target.value);
  32. }
  33. function renderStrategyParamForm() {
  34. if (!selectedStrategy) {
  35. return null;
  36. }
  37. if (selectedStrategy === DEFAULT_STRATEGY.name) {
  38. return <p className='my-4'>{DEFAULT_STRATEGY.desc}</p>;
  39. }
  40. const parameters = ableStrategies?.filter(i => i.strategy === selectedStrategy)[0]?.parameters;
  41. if (!parameters || !parameters.length) {
  42. return <Alert className='my-2' type='warning' message={t('No_parameter')} />;
  43. }
  44. return (
  45. <div className='mt-2'>
  46. {parameters?.map(param => (
  47. <Form.Item
  48. key={`param_${param.param_name}`}
  49. label={param.param_name}
  50. name={[field!.name, 'chunk_parameters', param.param_name]}
  51. rules={[{ required: true, message: t('Please_input_the_name') }]}
  52. initialValue={param.default_value}
  53. valuePropName={param.param_type === 'boolean' ? 'checked' : 'value'}
  54. tooltip={param.description}
  55. >
  56. {renderParamByType(param.param_type)}
  57. </Form.Item>
  58. ))}
  59. </div>
  60. );
  61. }
  62. function renderParamByType(type: string) {
  63. switch (type) {
  64. case 'int':
  65. return <InputNumber className='w-full' min={1} />;
  66. case 'string':
  67. return <TextArea className='w-full' rows={2} />;
  68. case 'boolean':
  69. return <Checkbox />;
  70. }
  71. }
  72. return (
  73. <>
  74. <Form.Item name={[field!.name, 'chunk_parameters', 'chunk_strategy']} initialValue={DEFAULT_STRATEGY.strategy}>
  75. <Radio.Group style={{ marginTop: 16 }} onChange={radioChange}>
  76. <Radio value={DEFAULT_STRATEGY.strategy}>{DEFAULT_STRATEGY.name}</Radio>
  77. {ableStrategies.map(strategy => (
  78. <Radio key={`strategy_radio_${strategy.strategy}`} value={strategy.strategy}>
  79. {strategy.name}
  80. </Radio>
  81. ))}
  82. </Radio.Group>
  83. </Form.Item>
  84. {renderStrategyParamForm()}
  85. </>
  86. );
  87. }