| 
									
										
										
										
											2024-09-15 18:09:47 +09:00
										 |  |  | import type { FC, FormEvent } from 'react' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import React from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   PlayIcon, | 
					
						
							|  |  |  | } from '@heroicons/react/24/solid' | 
					
						
							|  |  |  | 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' | 
					
						
							| 
									
										
										
										
											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' | 
					
						
							| 
									
										
										
										
											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> | 
					
						
							|  |  |  |   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, | 
					
						
							|  |  |  |   onInputsChange, | 
					
						
							|  |  |  |   onSend, | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |   visionConfig, | 
					
						
							|  |  |  |   onVisionFilesChange, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | }) => { | 
					
						
							|  |  |  |   const { t } = useTranslation() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |   const onClear = () => { | 
					
						
							|  |  |  |     const newInputs: Record<string, any> = {} | 
					
						
							|  |  |  |     promptConfig.prompt_variables.forEach((item) => { | 
					
						
							|  |  |  |       newInputs[item.key] = '' | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     onInputsChange(newInputs) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-15 18:09:47 +09:00
										 |  |  |   const onSubmit = (e: FormEvent<HTMLFormElement>) => { | 
					
						
							|  |  |  |     e.preventDefault() | 
					
						
							|  |  |  |     onSend() | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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}> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           {promptConfig.prompt_variables.map(item => ( | 
					
						
							|  |  |  |             <div className='w-full mt-4' key={item.key}> | 
					
						
							|  |  |  |               <label className='text-gray-900 text-sm font-medium'>{item.name}</label> | 
					
						
							|  |  |  |               <div className='mt-2'> | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |                 {item.type === 'select' && ( | 
					
						
							|  |  |  |                   <Select | 
					
						
							|  |  |  |                     className='w-full' | 
					
						
							|  |  |  |                     defaultValue={inputs[item.key]} | 
					
						
							|  |  |  |                     onSelect={(i) => { onInputsChange({ ...inputs, [item.key]: i.value }) }} | 
					
						
							|  |  |  |                     items={(item.options || []).map(i => ({ name: i, value: i }))} | 
					
						
							|  |  |  |                     allowSearch={false} | 
					
						
							|  |  |  |                     bgClassName='bg-gray-50' | 
					
						
							|  |  |  |                   /> | 
					
						
							|  |  |  |                 )} | 
					
						
							|  |  |  |                 {item.type === 'string' && ( | 
					
						
							|  |  |  |                   <input | 
					
						
							|  |  |  |                     type="text" | 
					
						
							|  |  |  |                     className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 " | 
					
						
							|  |  |  |                     placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`} | 
					
						
							|  |  |  |                     value={inputs[item.key]} | 
					
						
							|  |  |  |                     onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }} | 
					
						
							|  |  |  |                     maxLength={item.max_length || DEFAULT_VALUE_MAX_LEN} | 
					
						
							|  |  |  |                   /> | 
					
						
							|  |  |  |                 )} | 
					
						
							|  |  |  |                 {item.type === 'paragraph' && ( | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |                   <Textarea | 
					
						
							|  |  |  |                     className='h-[104px] sm:text-xs' | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |                     placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |                     value={inputs[item.key]} | 
					
						
							|  |  |  |                     onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }} | 
					
						
							|  |  |  |                   /> | 
					
						
							|  |  |  |                 )} | 
					
						
							|  |  |  |                 {item.type === 'number' && ( | 
					
						
							|  |  |  |                   <input | 
					
						
							|  |  |  |                     type="number" | 
					
						
							|  |  |  |                     className="block w-full p-2 text-gray-900 border border-gray-300 rounded-lg bg-gray-50 sm:text-xs focus:ring-blue-500 focus:border-blue-500 " | 
					
						
							|  |  |  |                     placeholder={`${item.name}${!item.required ? `(${t('appDebug.variableTable.optional')})` : ''}`} | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |                     value={inputs[item.key]} | 
					
						
							|  |  |  |                     onChange={(e) => { onInputsChange({ ...inputs, [item.key]: e.target.value }) }} | 
					
						
							|  |  |  |                   /> | 
					
						
							|  |  |  |                 )} | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |                 {item.type === 'file' && ( | 
					
						
							|  |  |  |                   <FileUploaderInAttachmentWrapper | 
					
						
							|  |  |  |                     onChange={(files) => { onInputsChange({ ...inputs, [item.key]: getProcessedFiles(files)[0] }) }} | 
					
						
							| 
									
										
										
										
											2024-10-24 22:55:17 +08:00
										 |  |  |                     fileConfig={{ | 
					
						
							|  |  |  |                       ...item.config, | 
					
						
							|  |  |  |                       fileUploadConfig: (visionConfig as any).fileUploadConfig, | 
					
						
							|  |  |  |                     }} | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |                   /> | 
					
						
							|  |  |  |                 )} | 
					
						
							|  |  |  |                 {item.type === 'file-list' && ( | 
					
						
							|  |  |  |                   <FileUploaderInAttachmentWrapper | 
					
						
							|  |  |  |                     onChange={(files) => { onInputsChange({ ...inputs, [item.key]: getProcessedFiles(files) }) }} | 
					
						
							| 
									
										
										
										
											2024-10-24 22:55:17 +08:00
										 |  |  |                     fileConfig={{ | 
					
						
							|  |  |  |                       ...item.config, | 
					
						
							|  |  |  |                       fileUploadConfig: (visionConfig as any).fileUploadConfig, | 
					
						
							|  |  |  |                     }} | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |                   /> | 
					
						
							|  |  |  |                 )} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |               </div> | 
					
						
							|  |  |  |             </div> | 
					
						
							|  |  |  |           ))} | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |           { | 
					
						
							|  |  |  |             visionConfig?.enabled && ( | 
					
						
							|  |  |  |               <div className="w-full mt-4"> | 
					
						
							| 
									
										
										
										
											2023-11-16 11:56:11 +08:00
										 |  |  |                 <div className="text-gray-900 text-sm font-medium">{t('common.imageUploader.imageUpload')}</div> | 
					
						
							| 
									
										
										
										
											2023-11-13 22:32:39 +08:00
										 |  |  |                 <div className='mt-2'> | 
					
						
							|  |  |  |                   <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> | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2023-05-29 14:35:21 +08:00
										 |  |  |           {promptConfig.prompt_variables.length > 0 && ( | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |             <div className='mt-4 h-[1px] bg-gray-100'></div> | 
					
						
							| 
									
										
										
										
											2023-05-29 14:35:21 +08:00
										 |  |  |           )} | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |           <div className='w-full mt-4'> | 
					
						
							|  |  |  |             <div className="flex items-center justify-between"> | 
					
						
							|  |  |  |               <Button | 
					
						
							|  |  |  |                 onClick={onClear} | 
					
						
							|  |  |  |                 disabled={false} | 
					
						
							|  |  |  |               > | 
					
						
							|  |  |  |                 <span className='text-[13px]'>{t('common.operation.clear')}</span> | 
					
						
							|  |  |  |               </Button> | 
					
						
							|  |  |  |               <Button | 
					
						
							| 
									
										
										
										
											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} | 
					
						
							|  |  |  |               > | 
					
						
							|  |  |  |                 <PlayIcon className="shrink-0 w-4 h-4 mr-1" aria-hidden="true" /> | 
					
						
							|  |  |  |                 <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) |