| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  | import type { ToolCredential, ToolParameter } from '../types' | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | export const toType = (type: string) => { | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |   switch (type) { | 
					
						
							|  |  |  |     case 'string': | 
					
						
							|  |  |  |       return 'text-input' | 
					
						
							|  |  |  |     case 'number': | 
					
						
							|  |  |  |       return 'number-input' | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       return type | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | export const toolParametersToFormSchemas = (parameters: ToolParameter[]) => { | 
					
						
							|  |  |  |   if (!parameters) | 
					
						
							|  |  |  |     return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const formSchemas = parameters.map((parameter) => { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       ...parameter, | 
					
						
							|  |  |  |       variable: parameter.name, | 
					
						
							|  |  |  |       type: toType(parameter.type), | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       _type: parameter.type, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       show_on: [], | 
					
						
							|  |  |  |       options: parameter.options?.map((option) => { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           ...option, | 
					
						
							|  |  |  |           show_on: [], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |       tooltip: parameter.human_description, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   return formSchemas | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const toolCredentialToFormSchemas = (parameters: ToolCredential[]) => { | 
					
						
							|  |  |  |   if (!parameters) | 
					
						
							|  |  |  |     return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const formSchemas = parameters.map((parameter) => { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       ...parameter, | 
					
						
							|  |  |  |       variable: parameter.name, | 
					
						
							|  |  |  |       label: parameter.label, | 
					
						
							|  |  |  |       tooltip: parameter.help, | 
					
						
							|  |  |  |       show_on: [], | 
					
						
							|  |  |  |       options: parameter.options?.map((option) => { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |           ...option, | 
					
						
							|  |  |  |           show_on: [], | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   return formSchemas | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const addDefaultValue = (value: Record<string, any>, formSchemas: { variable: string; default?: any }[]) => { | 
					
						
							|  |  |  |   const newValues = { ...value } | 
					
						
							|  |  |  |   formSchemas.forEach((formSchema) => { | 
					
						
							|  |  |  |     const itemValue = value[formSchema.variable] | 
					
						
							| 
									
										
										
										
											2024-02-22 13:06:55 +08:00
										 |  |  |     if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined)) | 
					
						
							| 
									
										
										
										
											2024-01-23 19:31:56 +08:00
										 |  |  |       newValues[formSchema.variable] = formSchema.default | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   return newValues | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const generateFormValue = (value: Record<string, any>, formSchemas: { variable: string; default?: any }[], isReasoning = false) => { | 
					
						
							|  |  |  |   const newValues = {} as any | 
					
						
							|  |  |  |   formSchemas.forEach((formSchema) => { | 
					
						
							|  |  |  |     const itemValue = value[formSchema.variable] | 
					
						
							|  |  |  |     if ((formSchema.default !== undefined) && (value === undefined || itemValue === null || itemValue === '' || itemValue === undefined)) { | 
					
						
							|  |  |  |       newValues[formSchema.variable] = { | 
					
						
							|  |  |  |         ...(isReasoning ? { value: null, auto: 1 } : { value: formSchema.default }), | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   return newValues | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const getPlainValue = (value: Record<string, any>) => { | 
					
						
							| 
									
										
										
										
											2025-04-14 16:06:10 +08:00
										 |  |  |   const plainValue = { ...value } | 
					
						
							| 
									
										
										
										
											2025-02-17 17:05:13 +08:00
										 |  |  |   Object.keys(plainValue).forEach((key) => { | 
					
						
							|  |  |  |     plainValue[key] = value[key].value | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   return plainValue | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const getStructureValue = (value: Record<string, any>) => { | 
					
						
							|  |  |  |   const newValue = { ...value } as any | 
					
						
							|  |  |  |   Object.keys(newValue).forEach((key) => { | 
					
						
							|  |  |  |     newValue[key] = { | 
					
						
							|  |  |  |       value: value[key], | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   return newValue | 
					
						
							|  |  |  | } |