| 
									
										
										
										
											2025-04-11 13:52:19 +08:00
										 |  |  | import type { ChangeEvent, FC, FormEvent } from 'react' | 
					
						
							| 
									
										
										
										
											2025-04-01 16:30:41 +08:00
										 |  |  | import { useEffect } from 'react' | 
					
						
							| 
									
										
										
										
											2024-11-12 15:35:12 +08:00
										 |  |  | import React, { useCallback } from 'react' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |   RiPlayLargeLine, | 
					
						
							|  |  |  | } from '@remixicon/react' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import Select from '@/app/components/base/select' | 
					
						
							|  |  |  | import type { SiteInfo } from '@/models/share' | 
					
						
							|  |  |  | import type { PromptConfig } from '@/models/debug' | 
					
						
							|  |  |  | import Button from '@/app/components/base/button' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import Textarea from '@/app/components/base/textarea' | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  | import Input from '@/app/components/base/input' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import { DEFAULT_VALUE_MAX_LEN } from '@/config' | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  | import TextGenerationImageUploader from '@/app/components/base/image-uploader/text-generation-image-uploader' | 
					
						
							|  |  |  | import type { VisionFile, VisionSettings } from '@/types/app' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader' | 
					
						
							|  |  |  | import { getProcessedFiles } from '@/app/components/base/file-uploader/utils' | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  | import useBreakpoints, { MediaType } from '@/hooks/use-breakpoints' | 
					
						
							|  |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-07 10:35:05 +08:00
										 |  |  | export type IRunOnceProps = { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   siteInfo: SiteInfo | 
					
						
							|  |  |  |   promptConfig: PromptConfig | 
					
						
							|  |  |  |   inputs: Record<string, any> | 
					
						
							| 
									
										
										
										
											2024-11-12 15:35:12 +08:00
										 |  |  |   inputsRef: React.MutableRefObject<Record<string, any>> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   onInputsChange: (inputs: Record<string, any>) => void | 
					
						
							|  |  |  |   onSend: () => void | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |   visionConfig: VisionSettings | 
					
						
							|  |  |  |   onVisionFilesChange: (files: VisionFile[]) => void | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-07 10:35:05 +08:00
										 |  |  | const RunOnce: FC<IRunOnceProps> = ({ | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   promptConfig, | 
					
						
							|  |  |  |   inputs, | 
					
						
							| 
									
										
										
										
											2024-11-12 15:35:12 +08:00
										 |  |  |   inputsRef, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   onInputsChange, | 
					
						
							|  |  |  |   onSend, | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |   visionConfig, | 
					
						
							|  |  |  |   onVisionFilesChange, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |   const media = useBreakpoints() | 
					
						
							|  |  |  |   const isPC = media === MediaType.pc | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |   const onClear = () => { | 
					
						
							|  |  |  |     const newInputs: Record<string, any> = {} | 
					
						
							|  |  |  |     promptConfig.prompt_variables.forEach((item) => { | 
					
						
							| 
									
										
										
										
											2025-04-11 14:40:57 +08:00
										 |  |  |       if (item.type === 'text-input' || item.type === 'paragraph') | 
					
						
							|  |  |  |         newInputs[item.key] = '' | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         newInputs[item.key] = undefined | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     }) | 
					
						
							|  |  |  |     onInputsChange(newInputs) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-15 18:09:47 +09:00
										 |  |  |   const onSubmit = (e: FormEvent<HTMLFormElement>) => { | 
					
						
							|  |  |  |     e.preventDefault() | 
					
						
							|  |  |  |     onSend() | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-12 15:35:12 +08:00
										 |  |  |   const handleInputsChange = useCallback((newInputs: Record<string, any>) => { | 
					
						
							|  |  |  |     onInputsChange(newInputs) | 
					
						
							|  |  |  |     inputsRef.current = newInputs | 
					
						
							|  |  |  |   }, [onInputsChange, inputsRef]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-01 16:30:41 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     const newInputs: Record<string, any> = {} | 
					
						
							|  |  |  |     promptConfig.prompt_variables.forEach((item) => { | 
					
						
							| 
									
										
										
										
											2025-04-11 14:40:57 +08:00
										 |  |  |       if (item.type === 'text-input' || item.type === 'paragraph') | 
					
						
							|  |  |  |         newInputs[item.key] = '' | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         newInputs[item.key] = undefined | 
					
						
							| 
									
										
										
										
											2025-04-01 16:30:41 +08:00
										 |  |  |     }) | 
					
						
							|  |  |  |     onInputsChange(newInputs) | 
					
						
							| 
									
										
										
										
											2025-04-11 13:52:19 +08:00
										 |  |  |   }, [promptConfig.prompt_variables, onInputsChange]) | 
					
						
							| 
									
										
										
										
											2025-04-01 16:30:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   return ( | 
					
						
							|  |  |  |     <div className=""> | 
					
						
							|  |  |  |       <section> | 
					
						
							|  |  |  |         {/* input form */} | 
					
						
							| 
									
										
										
										
											2024-09-15 18:09:47 +09:00
										 |  |  |         <form onSubmit={onSubmit}> | 
					
						
							| 
									
										
										
										
											2025-04-11 13:52:19 +08:00
										 |  |  |           {(inputs === null || inputs === undefined || Object.keys(inputs).length === 0) ? null | 
					
						
							|  |  |  |             : promptConfig.prompt_variables.map(item => ( | 
					
						
							|  |  |  |               <div className='mt-4 w-full' key={item.key}> | 
					
						
							|  |  |  |                 <label className='system-md-semibold flex h-6 items-center text-text-secondary'>{item.name}</label> | 
					
						
							|  |  |  |                 <div className='mt-1'> | 
					
						
							|  |  |  |                   {item.type === 'select' && ( | 
					
						
							|  |  |  |                     <Select | 
					
						
							|  |  |  |                       className='w-full' | 
					
						
							|  |  |  |                       defaultValue={inputs[item.key]} | 
					
						
							|  |  |  |                       onSelect={(i) => { handleInputsChange({ ...inputsRef.current, [item.key]: i.value }) }} | 
					
						
							|  |  |  |                       items={(item.options || []).map(i => ({ name: i, value: i }))} | 
					
						
							|  |  |  |                       allowSearch={false} | 
					
						
							|  |  |  |                     /> | 
					
						
							|  |  |  |                   )} | 
					
						
							|  |  |  |                   {item.type === 'string' && ( | 
					
						
							|  |  |  |                     <Input | 
					
						
							|  |  |  |                       type="text" | 
					
						
							|  |  |  |                       placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`} | 
					
						
							|  |  |  |                       value={inputs[item.key]} | 
					
						
							|  |  |  |                       onChange={(e: ChangeEvent<HTMLInputElement>) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }} | 
					
						
							|  |  |  |                       maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN} | 
					
						
							|  |  |  |                     /> | 
					
						
							|  |  |  |                   )} | 
					
						
							|  |  |  |                   {item.type === 'paragraph' && ( | 
					
						
							|  |  |  |                     <Textarea | 
					
						
							|  |  |  |                       className='h-[104px] sm:text-xs' | 
					
						
							|  |  |  |                       placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`} | 
					
						
							|  |  |  |                       value={inputs[item.key]} | 
					
						
							|  |  |  |                       onChange={(e: ChangeEvent<HTMLInputElement>) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }} | 
					
						
							|  |  |  |                     /> | 
					
						
							|  |  |  |                   )} | 
					
						
							|  |  |  |                   {item.type === 'number' && ( | 
					
						
							|  |  |  |                     <Input | 
					
						
							|  |  |  |                       type="number" | 
					
						
							|  |  |  |                       placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`} | 
					
						
							|  |  |  |                       value={inputs[item.key]} | 
					
						
							|  |  |  |                       onChange={(e: ChangeEvent<HTMLInputElement>) => { handleInputsChange({ ...inputsRef.current, [item.key]: e.target.value }) }} | 
					
						
							|  |  |  |                     /> | 
					
						
							|  |  |  |                   )} | 
					
						
							|  |  |  |                   {item.type === 'file' && ( | 
					
						
							|  |  |  |                     <FileUploaderInAttachmentWrapper | 
					
						
							|  |  |  |                       onChange={(files) => { handleInputsChange({ ...inputsRef.current, [item.key]: getProcessedFiles(files)[0] }) }} | 
					
						
							|  |  |  |                       fileConfig={{ | 
					
						
							|  |  |  |                         ...item.config, | 
					
						
							|  |  |  |                         fileUploadConfig: (visionConfig as any).fileUploadConfig, | 
					
						
							|  |  |  |                       }} | 
					
						
							|  |  |  |                     /> | 
					
						
							|  |  |  |                   )} | 
					
						
							|  |  |  |                   {item.type === 'file-list' && ( | 
					
						
							|  |  |  |                     <FileUploaderInAttachmentWrapper | 
					
						
							|  |  |  |                       onChange={(files) => { handleInputsChange({ ...inputsRef.current, [item.key]: getProcessedFiles(files) }) }} | 
					
						
							|  |  |  |                       fileConfig={{ | 
					
						
							|  |  |  |                         ...item.config, | 
					
						
							|  |  |  |                         fileUploadConfig: (visionConfig as any).fileUploadConfig, | 
					
						
							|  |  |  |                       }} | 
					
						
							|  |  |  |                     /> | 
					
						
							|  |  |  |                   )} | 
					
						
							|  |  |  |                 </div> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |               </div> | 
					
						
							| 
									
										
										
										
											2025-04-11 13:52:19 +08:00
										 |  |  |             ))} | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |           { | 
					
						
							|  |  |  |             visionConfig?.enabled && ( | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |               <div className="mt-4 w-full"> | 
					
						
							|  |  |  |                 <div className="system-md-semibold flex h-6 items-center text-text-secondary">{t('common.imageUploader.imageUpload')}</div> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |                 <div className='mt-1'> | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |                   <TextGenerationImageUploader | 
					
						
							|  |  |  |                     settings={visionConfig} | 
					
						
							|  |  |  |                     onFilesChange={files => onVisionFilesChange(files.filter(file => file.progress !== -1).map(fileItem => ({ | 
					
						
							|  |  |  |                       type: 'image', | 
					
						
							|  |  |  |                       transfer_method: fileItem.type, | 
					
						
							|  |  |  |                       url: fileItem.url, | 
					
						
							|  |  |  |                       upload_file_id: fileItem.fileId, | 
					
						
							|  |  |  |                     })))} | 
					
						
							|  |  |  |                   /> | 
					
						
							|  |  |  |                 </div> | 
					
						
							|  |  |  |               </div> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           <div className='mb-3 mt-6 w-full'> | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |             <div className="flex items-center justify-between gap-2"> | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |               <Button | 
					
						
							|  |  |  |                 onClick={onClear} | 
					
						
							|  |  |  |                 disabled={false} | 
					
						
							|  |  |  |               > | 
					
						
							|  |  |  |                 <span className='text-[13px]'>{t('common.operation.clear')}</span> | 
					
						
							|  |  |  |               </Button> | 
					
						
							|  |  |  |               <Button | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |                 className={cn(!isPC && 'grow')} | 
					
						
							| 
									
										
										
										
											2024-09-15 18:09:47 +09:00
										 |  |  |                 type='submit' | 
					
						
							| 
									
										
										
										
											2024-06-19 14:13:16 +08:00
										 |  |  |                 variant="primary" | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |                 disabled={false} | 
					
						
							|  |  |  |               > | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |                 <RiPlayLargeLine className="mr-1 h-4 w-4 shrink-0" aria-hidden="true" /> | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |                 <span className='text-[13px]'>{t('share.generation.run')}</span> | 
					
						
							|  |  |  |               </Button> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |             </div> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         </form> | 
					
						
							|  |  |  |       </section> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-07 10:35:05 +08:00
										 |  |  | export default React.memo(RunOnce) |