Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

4.5 KB
Raw Blame
/*
 *  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import { cn } from '@/lib/utils';
import { forwardRef } from 'react';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { DelimiterPreview } from './delimiter-preview';
import {
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from './ui/form';
import { Input, InputProps } from './ui/input';
import { Switch } from './ui/switch';

interface IProps {
  value?: string | undefined;
  onChange?: (val: string | undefined) => void;
}

export const DelimiterInput = forwardRef<HTMLInputElement, InputProps & IProps>(
  function DelimiterInput(
    { value, onChange, maxLength, defaultValue, ...props },
    ref,
  ) {
    const nextValue = value
      ?.replaceAll('\n', '\\n')
      .replaceAll('\t', '\\t')
      .replaceAll('\r', '\\r');
    const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
      const val = e.target.value;
      const nextValue = val
        .replaceAll('\\n', '\n')
        .replaceAll('\\t', '\t')
        .replaceAll('\\r', '\r');
      onChange?.(nextValue);
    };
    return (
      <Input
        value={nextValue}
        onChange={handleInputChange}
        maxLength={maxLength}
        defaultValue={defaultValue}
        ref={ref}
        className={cn('bg-bg-base', props.className)}
        {...props}
      />
    );
  },
);

export function ChildrenDelimiterForm() {
  const { t } = useTranslation();
  const form = useFormContext();

  const delimiterValue = form.watch('parser_config.children_delimiter');
  const enableChildren = form.watch('parser_config.enable_children');

  return (
    <fieldset className="space-y-2">
      <FormField
        control={form.control}
        name="parser_config.enable_children"
        render={({ field: { value, onChange, ...restProps } }) => (
          <FormItem className="items-center space-y-0 ">
            <div className="flex items-center justify-between gap-1">
              <FormLabel>
                {t('knowledgeDetails.enableChildrenDelimiter')}
              </FormLabel>

              <div className="flex-none">
                <FormControl>
                  <Switch
                    checked={value}
                    onCheckedChange={(checked) => {
                      if (checked && !delimiterValue) {
                        form.setValue('parser_config.children_delimiter', '\n');
                      }

                      onChange(checked);
                    }}
                    {...restProps}
                    data-testid="ds-settings-parser-child-chunk-switch"
                  />
                </FormControl>
              </div>
            </div>
          </FormItem>
        )}
      />

      {enableChildren && (
        <FormField
          control={form.control}
          name="parser_config.children_delimiter"
          render={({ field }) => (
            <FormItem className="items-center space-y-0 ">
              <div className="flex items-center gap-1 pr-[1px]">
                <FormLabel
                  required
                  tooltip={t('knowledgeDetails.childrenDelimiterTip')}
                  className="text-sm text-text-secondary whitespace-break-spaces w-1/4"
                >
                  {t('knowledgeDetails.childrenDelimiter')}
                </FormLabel>
                <div className="w-3/4">
                  <FormControl>
                    <DelimiterInput
                      {...field}
                      data-testid="ds-settings-parser-child-chunk-delimiter-input"
                    />
                  </FormControl>
                  <DelimiterPreview value={field.value} />
                </div>
              </div>
              <div className="flex pt-1">
                <div className="w-1/4"></div>
                <FormMessage />
              </div>
            </FormItem>
          )}
        />
      )}
    </fieldset>
  );
}