next-dynamic-input-variable.tsx


Overview

This file defines React components that provide a dynamic, user-interactive form interface to manage input variables in a "RAG" (Retrieval-Augmented Generation) flow node context. It primarily focuses on rendering and managing a list of variables that can be either references to other components or direct text inputs.

The components utilize the react-hook-form library for form state management and validation, combined with custom UI components for form controls and collapsible sections. The user can add, remove, and select variable types and their corresponding values dynamically, with options populated based on the current node context.


Components and Functions

Enum: VariableType

Defines the two types of variables that the form supports:

Member

Value

Description

Reference

'reference'

Variable referring to a component ID

Input

'input'

Variable holding user input text


Function: getVariableName(type: string): string

Returns the form field name suffix corresponding to the variable type.

Usage example:

const fieldName = getVariableName(VariableType.Reference);  // returns 'component_id'

Component: DynamicVariableForm

A form component that renders dynamic input fields for variables associated with a given flow node.

Usage example:

<DynamicVariableForm node={someNode} />

Component: DynamicInputVariable

A collapsible UI component that wraps DynamicVariableForm, providing a toggleable section for input variables.

Usage example:

<DynamicInputVariable node={someNode} />

Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

componentDiagram
    direction TB
    DynamicInputVariable --> Collapsible
    Collapsible --> CollapsibleTrigger
    Collapsible --> CollapsibleContent
    CollapsibleContent --> DynamicVariableForm
    DynamicVariableForm --> useFormContext
    DynamicVariableForm --> useFieldArray
    DynamicVariableForm --> useBuildVariableOptions
    DynamicVariableForm --> RAGFlowSelect
    DynamicVariableForm --> Input
    DynamicVariableForm --> Button
    DynamicVariableForm --> Trash2 (icon)
    DynamicVariableForm --> FormField
    DynamicVariableForm --> FormItem
    DynamicVariableForm --> FormControl
    DynamicVariableForm --> FormDescription
    DynamicVariableForm --> FormMessage

Summary

This file provides a reusable and dynamic React UI for managing input variables in a RAG flow editor. It handles complex form states with dynamic fields, conditional rendering based on variable types, and integrates with the broader system via hooks and context. Its collapsible design enhances user experience by organizing input variables into a neat, expandable section.


Example Integration

import { useForm, FormProvider } from 'react-hook-form';
import { DynamicInputVariable } from './next-dynamic-input-variable';

function FlowNodeEditor({ node }) {
  const methods = useForm({
    defaultValues: {
      query: [
        { type: 'input', value: '' },
      ],
    },
  });

  return (
    <FormProvider {...methods}>
      <form>
        <DynamicInputVariable node={node} />
        {/* Other form fields and submit button */}
      </form>
    </FormProvider>
  );
}

This example demonstrates how DynamicInputVariable fits inside a form context, managing variable inputs dynamically for a given flow node.