dynamic-prompt.tsx


Overview

dynamic-prompt.tsx defines a React functional component named DynamicPrompt for dynamically managing a list of prompt messages in a form. Each prompt consists of a role (either "User" or "Assistant") and associated content that can be edited with a rich text editor (PromptEditor). The component utilizes React Hook Form for form state management and validation, allowing users to add, edit, and remove prompts interactively within a form context.

This component is typically used in applications where multi-turn conversational prompts or messages need to be constructed dynamically, such as chatbots, AI assistants, or interactive dialog flows.


Detailed Explanation

Component: DynamicPrompt

Description

DynamicPrompt renders a section of a form that allows users to dynamically add, edit, and remove conversational prompts. Each prompt includes:

The component shows all prompts currently in the form state, and a button to append a new prompt initialized with the role User and empty content.

Libraries and Hooks Used


Constants

const options = [
  { label: 'User', value: PromptRole.User },
  { label: 'Assistant', value: PromptRole.Assistant },
];

Internal Variables


Component Render Structure

<FormItem>
  <FormLabel tooltip={t('flow.msgTip')}>{t('flow.msg')}</FormLabel>
  <div className="space-y-4">
    {fields.map((field, index) => (
      <div key={field.id} className="flex">
        <div className="space-y-2 flex-1">
          {/* Role Selector */}
          <FormField ... >
            <FormItem className="w-1/3">
              <FormLabel />
              <FormControl>
                <RAGFlowSelect ... />
              </FormControl>
              <FormMessage />
            </FormItem>
          </FormField>

          {/* Prompt Content Editor */}
          <FormField ... >
            <FormItem className="flex-1">
              <FormControl>
                <section>
                  <PromptEditor ... />
                </section>
              </FormControl>
            </FormItem>
          </FormField>
        </div>

        {/* Remove Button */}
        <Button type="button" variant="ghost" onClick={() => remove(index)}>
          <X />
        </Button>
      </div>
    ))}
  </div>
  <FormMessage />
  <BlockButton onClick={() => append({ content: '', role: PromptRole.User })}>
    Add
  </BlockButton>
</FormItem>

Props and Parameters

DynamicPrompt does not accept any external props directly; it relies on being rendered within a react-hook-form context.


Usage Example

import { useForm, FormProvider } from 'react-hook-form';
import DynamicPrompt from './dynamic-prompt';

function MyForm() {
  const methods = useForm({
    defaultValues: {
      prompts: [{ role: 'User', content: '' }],
    },
  });

  const onSubmit = data => {
    console.log(data.prompts);
  };

  return (
    <FormProvider {...methods}>
      <form onSubmit={methods.handleSubmit(onSubmit)}>
        <DynamicPrompt />
        <button type="submit">Submit</button>
      </form>
    </FormProvider>
  );
}

Important Implementation Details


Interactions with Other Parts of the System


Mermaid Component Diagram

componentDiagram
    component DynamicPrompt {
        +useFormContext()
        +useFieldArray(name: "prompts")
        +useTranslation()
        +render()
    }
    component FormField
    component FormItem
    component FormLabel
    component FormControl
    component FormMessage
    component RAGFlowSelect
    component PromptEditor
    component Button
    component BlockButton
    component XIcon

    DynamicPrompt --> FormItem
    DynamicPrompt --> FormLabel
    DynamicPrompt --> FormField
    DynamicPrompt --> FormControl
    DynamicPrompt --> FormMessage
    DynamicPrompt --> RAGFlowSelect
    DynamicPrompt --> PromptEditor
    DynamicPrompt --> Button
    DynamicPrompt --> BlockButton
    Button --> XIcon

Summary

This component is a critical building block for any feature requiring flexible, user-editable conversation flows or prompt sequences.