From 81d63a2d1be5913d7a7320a363cdb1a05751259e Mon Sep 17 00:00:00 2001 From: Konstantin Meshcheryakov Date: Mon, 12 Aug 2024 17:56:06 +0200 Subject: [PATCH 1/2] feat: Add extended inputs for promts library variables --- .../Prompts/Groups/VariableForm.tsx | 113 ++++++++++--- .../src/components/ui/InputWithDropDown.tsx | 155 ++++++++++++++++++ client/src/components/ui/index.ts | 1 + 3 files changed, 246 insertions(+), 23 deletions(-) create mode 100644 client/src/components/ui/InputWithDropDown.tsx diff --git a/client/src/components/Prompts/Groups/VariableForm.tsx b/client/src/components/Prompts/Groups/VariableForm.tsx index d77d164f72b..54cfc6a215a 100644 --- a/client/src/components/Prompts/Groups/VariableForm.tsx +++ b/client/src/components/Prompts/Groups/VariableForm.tsx @@ -1,12 +1,56 @@ -import { useMemo } from 'react'; +import React, { useMemo, useState } from 'react'; import { useForm, useFieldArray, Controller, useWatch } from 'react-hook-form'; import type { TPromptGroup } from 'librechat-data-provider'; import { extractVariableInfo, wrapVariable, replaceSpecialVars } from '~/utils'; import { useAuthContext, useLocalize, useSubmitMessage } from '~/hooks'; -import { Textarea } from '~/components/ui'; +import { TextareaAutosize, Input, InputWithDropdown } from '~/components/ui'; + +type FieldType = 'text' | 'multiline' | 'select'; + +type FieldConfig = { + variable: string; + type: FieldType; + options?: string[]; +}; type FormValues = { - fields: { variable: string; value: string }[]; + fields: { variable: string; value: string; config: FieldConfig }[]; +}; + +/** + * Variable Format Guide: + * + * Variables in prompts should be enclosed in double curly braces: {{variable}} + * + * Simple text input: + * {{variable_name}} + * + * Multiline text input: + * {{variable_name:multiline}} + * + * Dropdown select with predefined options: + * {{variable_name:option1|option2|option3}} + * + * All dropdown selects allow custom input in addition to predefined options. + * + * Examples: + * {{name}} - Simple text input for a name + * {{email:multiline}} - Multiline input for an email + * {{tone:formal|casual|business casual}} - Dropdown for tone selection with custom input option + * + * Note: The order of variables in the prompt will be preserved in the input form. + */ + +const parseFieldConfig = (variable: string): FieldConfig => { + const content = variable; + if (content.includes(':')) { + const [name, options] = content.split(':'); + if (options && options.includes('|')) { + return { variable: name, type: 'select', options: options.split('|') }; + } + return { variable: name, type: options as FieldType }; + } + return { variable: content, type: 'text' }; }; export default function VariableForm({ @@ -30,9 +74,13 @@ export default function VariableForm({ ); const { submitPrompt } = useSubmitMessage(); - const { control, handleSubmit } = useForm({ + const { control, handleSubmit, setValue } = useForm({ defaultValues: { - fields: uniqueVariables.map((variable) => ({ variable: wrapVariable(variable), value: '' })), + fields: uniqueVariables.map((variable) => ({ + variable: wrapVariable(variable), + value: '', + config: parseFieldConfig(variable), + })), }, }); @@ -46,6 +94,8 @@ export default function VariableForm({ name: 'fields', }); + const [customOptions, setCustomOptions] = useState<{ [key: string]: string }>({}); + if (!uniqueVariables.length) { return null; } @@ -96,27 +146,44 @@ export default function VariableForm({

{generateHighlightedText()}

-
+
{fields.map((field, index) => ( -
+
( -