| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  | import type { UserInputFormItem } from '@/types/app' | 
					
						
							|  |  |  | import type { PromptVariable } from '@/models/debug' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-27 14:53:22 +08:00
										 |  |  | export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null, dataset_query_variable?: string) => { | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |   if (!useInputs) | 
					
						
							|  |  |  |     return [] | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   const promptVariables: PromptVariable[] = [] | 
					
						
							|  |  |  |   useInputs.forEach((item: any) => { | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     const isParagraph = !!item.paragraph | 
					
						
							| 
									
										
										
										
											2023-09-27 14:53:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     const [type, content] = (() => { | 
					
						
							|  |  |  |       if (isParagraph) | 
					
						
							|  |  |  |         return ['paragraph', item.paragraph] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (item['text-input']) | 
					
						
							|  |  |  |         return ['string', item['text-input']] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-09 18:33:47 +08:00
										 |  |  |       if (item.number) | 
					
						
							|  |  |  |         return ['number', item.number] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |       if (item.file) | 
					
						
							|  |  |  |         return ['file', item.file] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (item['file-list']) | 
					
						
							|  |  |  |         return ['file-list', item['file-list']] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       if (item.external_data_tool) | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |         return [item.external_data_tool.type, item.external_data_tool] | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       return ['select', item.select || {}] | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     })() | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     const is_context_var = dataset_query_variable === content?.variable | 
					
						
							| 
									
										
										
										
											2023-09-27 14:53:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     if (type === 'string' || type === 'paragraph') { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       promptVariables.push({ | 
					
						
							|  |  |  |         key: content.variable, | 
					
						
							|  |  |  |         name: content.label, | 
					
						
							|  |  |  |         required: content.required, | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |         type, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         max_length: content.max_length, | 
					
						
							|  |  |  |         options: [], | 
					
						
							| 
									
										
										
										
											2023-09-27 14:53:22 +08:00
										 |  |  |         is_context_var, | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |         hide: content.hide, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       }) | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     else if (type === 'number') { | 
					
						
							|  |  |  |       promptVariables.push({ | 
					
						
							|  |  |  |         key: content.variable, | 
					
						
							|  |  |  |         name: content.label, | 
					
						
							|  |  |  |         required: content.required, | 
					
						
							|  |  |  |         type, | 
					
						
							|  |  |  |         options: [], | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |         hide: content.hide, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |     else if (type === 'select') { | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       promptVariables.push({ | 
					
						
							|  |  |  |         key: content.variable, | 
					
						
							|  |  |  |         name: content.label, | 
					
						
							|  |  |  |         required: content.required, | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |         type: 'select', | 
					
						
							|  |  |  |         options: content.options, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |         is_context_var, | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |         hide: content.hide, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |     else if (type === 'file') { | 
					
						
							|  |  |  |       promptVariables.push({ | 
					
						
							|  |  |  |         key: content.variable, | 
					
						
							|  |  |  |         name: content.label, | 
					
						
							|  |  |  |         required: content.required, | 
					
						
							|  |  |  |         type, | 
					
						
							|  |  |  |         config: { | 
					
						
							|  |  |  |           allowed_file_types: content.allowed_file_types, | 
					
						
							|  |  |  |           allowed_file_extensions: content.allowed_file_extensions, | 
					
						
							|  |  |  |           allowed_file_upload_methods: content.allowed_file_upload_methods, | 
					
						
							|  |  |  |           number_limits: 1, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |         hide: content.hide, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (type === 'file-list') { | 
					
						
							|  |  |  |       promptVariables.push({ | 
					
						
							|  |  |  |         key: content.variable, | 
					
						
							|  |  |  |         name: content.label, | 
					
						
							|  |  |  |         required: content.required, | 
					
						
							|  |  |  |         type, | 
					
						
							|  |  |  |         config: { | 
					
						
							|  |  |  |           allowed_file_types: content.allowed_file_types, | 
					
						
							|  |  |  |           allowed_file_extensions: content.allowed_file_extensions, | 
					
						
							|  |  |  |           allowed_file_upload_methods: content.allowed_file_upload_methods, | 
					
						
							|  |  |  |           number_limits: content.max_length, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |         hide: content.hide, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       promptVariables.push({ | 
					
						
							|  |  |  |         key: content.variable, | 
					
						
							|  |  |  |         name: content.label, | 
					
						
							|  |  |  |         required: content.required, | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |         type: content.type, | 
					
						
							|  |  |  |         enabled: content.enabled, | 
					
						
							|  |  |  |         config: content.config, | 
					
						
							|  |  |  |         icon: content.icon, | 
					
						
							|  |  |  |         icon_background: content.icon_background, | 
					
						
							| 
									
										
										
										
											2023-09-27 14:53:22 +08:00
										 |  |  |         is_context_var, | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |         hide: content.hide, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   return promptVariables | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const promptVariablesToUserInputsForm = (promptVariables: PromptVariable[]) => { | 
					
						
							|  |  |  |   const userInputs: UserInputFormItem[] = [] | 
					
						
							|  |  |  |   promptVariables.filter(({ key, name }) => { | 
					
						
							| 
									
										
										
										
											2025-04-14 15:28:20 +08:00
										 |  |  |     return key && key.trim() && name && name.trim() | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   }).forEach((item: any) => { | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     if (item.type === 'string' || item.type === 'paragraph') { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       userInputs.push({ | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |         [item.type === 'string' ? 'text-input' : 'paragraph']: { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           label: item.name, | 
					
						
							|  |  |  |           variable: item.key, | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |           required: item.required !== false, // default true
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           max_length: item.max_length, | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |           default: '', | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |           hide: item.hide, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |       } as any) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       return | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (item.type === 'number') { | 
					
						
							|  |  |  |       userInputs.push({ | 
					
						
							|  |  |  |         number: { | 
					
						
							|  |  |  |           label: item.name, | 
					
						
							|  |  |  |           variable: item.key, | 
					
						
							|  |  |  |           required: item.required !== false, // default true
 | 
					
						
							|  |  |  |           default: '', | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |           hide: item.hide, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |       } as any) | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |     else if (item.type === 'select') { | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       userInputs.push({ | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |         select: { | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |           label: item.name, | 
					
						
							|  |  |  |           variable: item.key, | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |           required: item.required !== false, // default true
 | 
					
						
							|  |  |  |           options: item.options, | 
					
						
							|  |  |  |           default: '', | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |           hide: item.hide, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |       } as any) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-09-10 00:12:34 +08:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       userInputs.push({ | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |         external_data_tool: { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |           label: item.name, | 
					
						
							|  |  |  |           variable: item.key, | 
					
						
							| 
									
										
										
										
											2024-02-20 14:49:00 +08:00
										 |  |  |           enabled: item.enabled, | 
					
						
							|  |  |  |           type: item.type, | 
					
						
							|  |  |  |           config: item.config, | 
					
						
							|  |  |  |           required: item.required, | 
					
						
							|  |  |  |           icon: item.icon, | 
					
						
							|  |  |  |           icon_background: item.icon_background, | 
					
						
							| 
									
										
										
										
											2025-06-18 11:30:30 +08:00
										 |  |  |           hide: item.hide, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |       } as any) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   return userInputs | 
					
						
							|  |  |  | } |