reasoning-config-form.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import { useCallback, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import produce from 'immer'
  4. import {
  5. RiArrowRightUpLine,
  6. } from '@remixicon/react'
  7. import Tooltip from '@/app/components/base/tooltip'
  8. import Switch from '@/app/components/base/switch'
  9. import Input from '@/app/components/workflow/nodes/_base/components/input-support-select-var'
  10. import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker'
  11. import AppSelector from '@/app/components/plugins/plugin-detail-panel/app-selector'
  12. import ModelParameterModal from '@/app/components/plugins/plugin-detail-panel/model-selector'
  13. import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
  14. import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  15. import type { Node } from 'reactflow'
  16. import type {
  17. NodeOutPutVar,
  18. ValueSelector,
  19. Var,
  20. } from '@/app/components/workflow/types'
  21. import type { ToolVarInputs } from '@/app/components/workflow/nodes/tool/types'
  22. import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
  23. import { VarType } from '@/app/components/workflow/types'
  24. import cn from '@/utils/classnames'
  25. type Props = {
  26. value: Record<string, any>
  27. onChange: (val: Record<string, any>) => void
  28. schemas: any[]
  29. nodeOutputVars: NodeOutPutVar[],
  30. availableNodes: Node[],
  31. nodeId: string
  32. }
  33. const ReasoningConfigForm: React.FC<Props> = ({
  34. value,
  35. onChange,
  36. schemas,
  37. nodeOutputVars,
  38. availableNodes,
  39. nodeId,
  40. }) => {
  41. const { t } = useTranslation()
  42. const language = useLanguage()
  43. const handleAutomatic = (key: string, val: any) => {
  44. onChange({
  45. ...value,
  46. [key]: {
  47. value: val ? null : value[key]?.value,
  48. auto: val ? 1 : 0,
  49. },
  50. })
  51. }
  52. const [inputsIsFocus, setInputsIsFocus] = useState<Record<string, boolean>>({})
  53. const handleInputFocus = useCallback((variable: string) => {
  54. return (value: boolean) => {
  55. setInputsIsFocus((prev) => {
  56. return {
  57. ...prev,
  58. [variable]: value,
  59. }
  60. })
  61. }
  62. }, [])
  63. const handleNotMixedTypeChange = useCallback((variable: string) => {
  64. return (varValue: ValueSelector | string, varKindType: VarKindType) => {
  65. const newValue = produce(value, (draft: ToolVarInputs) => {
  66. const target = draft[variable].value
  67. if (target) {
  68. target.type = varKindType
  69. target.value = varValue
  70. }
  71. else {
  72. draft[variable].value = {
  73. type: varKindType,
  74. value: varValue,
  75. }
  76. }
  77. })
  78. onChange(newValue)
  79. }
  80. }, [value, onChange])
  81. const handleMixedTypeChange = useCallback((variable: string) => {
  82. return (itemValue: string) => {
  83. const newValue = produce(value, (draft: ToolVarInputs) => {
  84. const target = draft[variable].value
  85. if (target) {
  86. target.value = itemValue
  87. }
  88. else {
  89. draft[variable].value = {
  90. type: VarKindType.mixed,
  91. value: itemValue,
  92. }
  93. }
  94. })
  95. onChange(newValue)
  96. }
  97. }, [value, onChange])
  98. const handleFileChange = useCallback((variable: string) => {
  99. return (varValue: ValueSelector | string) => {
  100. const newValue = produce(value, (draft: ToolVarInputs) => {
  101. draft[variable].value = {
  102. type: VarKindType.variable,
  103. value: varValue,
  104. }
  105. })
  106. onChange(newValue)
  107. }
  108. }, [value, onChange])
  109. const handleAppChange = useCallback((variable: string) => {
  110. return (app: {
  111. app_id: string
  112. inputs: Record<string, any>
  113. files?: any[]
  114. }) => {
  115. const newValue = produce(value, (draft: ToolVarInputs) => {
  116. draft[variable].value = app as any
  117. })
  118. onChange(newValue)
  119. }
  120. }, [onChange, value])
  121. const handleModelChange = useCallback((variable: string) => {
  122. return (model: any) => {
  123. const newValue = produce(value, (draft: ToolVarInputs) => {
  124. draft[variable].value = {
  125. ...draft[variable].value,
  126. ...model,
  127. } as any
  128. })
  129. onChange(newValue)
  130. }
  131. }, [onChange, value])
  132. const renderField = (schema: any) => {
  133. const {
  134. variable,
  135. label,
  136. required,
  137. tooltip,
  138. type,
  139. scope,
  140. url,
  141. } = schema
  142. const auto = value[variable]?.auto
  143. const tooltipContent = (tooltip && (
  144. <Tooltip
  145. popupContent={<div className='w-[200px]'>
  146. {tooltip[language] || tooltip.en_US}
  147. </div>}
  148. triggerClassName='ml-1 w-4 h-4'
  149. asChild={false} />
  150. ))
  151. const varInput = value[variable].value
  152. const isNumber = type === FormTypeEnum.textNumber
  153. const isSelect = type === FormTypeEnum.select
  154. const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files
  155. const isAppSelector = type === FormTypeEnum.appSelector
  156. const isModelSelector = type === FormTypeEnum.modelSelector
  157. // const isToolSelector = type === FormTypeEnum.toolSelector
  158. const isString = !isNumber && !isSelect && !isFile && !isAppSelector && !isModelSelector
  159. return (
  160. <div key={variable} className='space-y-1'>
  161. <div className='flex items-center justify-between py-2 system-sm-semibold text-text-secondary'>
  162. <div className='flex items-center space-x-2'>
  163. <span className={cn('text-text-secondary code-sm-semibold')}>{label[language] || label.en_US}</span>
  164. {required && (
  165. <span className='ml-1 text-red-500'>*</span>
  166. )}
  167. {tooltipContent}
  168. </div>
  169. <div className='flex items-center gap-1 px-2 py-1 rounded-[6px] border border-divider-subtle bg-background-default-lighter cursor-pointer hover:bg-state-base-hover' onClick={() => handleAutomatic(variable, !auto)}>
  170. <span className='text-text-secondary system-xs-medium'>{t('plugin.detailPanel.toolSelector.auto')}</span>
  171. <Switch
  172. size='xs'
  173. defaultValue={!!auto}
  174. onChange={val => handleAutomatic(variable, val)}
  175. />
  176. </div>
  177. </div>
  178. {auto === 0 && (
  179. <>
  180. {isString && (
  181. <Input
  182. className={cn(inputsIsFocus[variable] ? 'shadow-xs bg-gray-50 border-gray-300' : 'bg-gray-100 border-gray-100', 'rounded-lg px-3 py-[6px] border')}
  183. value={varInput?.value as string || ''}
  184. onChange={handleMixedTypeChange(variable)}
  185. nodesOutputVars={nodeOutputVars}
  186. availableNodes={availableNodes}
  187. onFocusChange={handleInputFocus(variable)}
  188. placeholder={t('workflow.nodes.http.insertVarPlaceholder')!}
  189. placeholderClassName='!leading-[21px]'
  190. />
  191. )}
  192. {/* {isString && (
  193. <VarReferencePicker
  194. zIndex={1001}
  195. readonly={false}
  196. isShowNodeName
  197. nodeId={nodeId}
  198. value={varInput?.value || ''}
  199. onChange={handleNotMixedTypeChange(variable)}
  200. defaultVarKindType={VarKindType.variable}
  201. filterVar={(varPayload: Var) => varPayload.type === VarType.number || varPayload.type === VarType.secret || varPayload.type === VarType.string}
  202. />
  203. )} */}
  204. {(isNumber || isSelect) && (
  205. <VarReferencePicker
  206. zIndex={1001}
  207. readonly={false}
  208. isShowNodeName
  209. nodeId={nodeId}
  210. value={varInput?.type === VarKindType.constant ? (varInput?.value ?? '') : (varInput?.value ?? [])}
  211. onChange={handleNotMixedTypeChange(variable)}
  212. defaultVarKindType={varInput?.type || (isNumber ? VarKindType.constant : VarKindType.variable)}
  213. isSupportConstantValue
  214. filterVar={isNumber ? (varPayload: Var) => varPayload.type === schema._type : undefined}
  215. availableVars={isSelect ? nodeOutputVars : undefined}
  216. schema={schema}
  217. />
  218. )}
  219. {isFile && (
  220. <VarReferencePicker
  221. zIndex={1001}
  222. readonly={false}
  223. isShowNodeName
  224. nodeId={nodeId}
  225. value={varInput?.value || []}
  226. onChange={handleFileChange(variable)}
  227. defaultVarKindType={VarKindType.variable}
  228. filterVar={(varPayload: Var) => varPayload.type === VarType.file || varPayload.type === VarType.arrayFile}
  229. />
  230. )}
  231. {isAppSelector && (
  232. <AppSelector
  233. disabled={false}
  234. scope={scope || 'all'}
  235. value={varInput as any}
  236. onSelect={handleAppChange(variable)}
  237. />
  238. )}
  239. {isModelSelector && (
  240. <ModelParameterModal
  241. popupClassName='!w-[387px]'
  242. isAdvancedMode
  243. isInWorkflow
  244. value={varInput as any}
  245. setModel={handleModelChange(variable)}
  246. scope={scope}
  247. />
  248. )}
  249. </>
  250. )}
  251. {url && (
  252. <a
  253. href={url}
  254. target='_blank' rel='noopener noreferrer'
  255. className='inline-flex items-center text-xs text-text-accent'
  256. >
  257. {t('tools.howToGet')}
  258. <RiArrowRightUpLine className='ml-1 w-3 h-3' />
  259. </a>
  260. )}
  261. </div>
  262. )
  263. }
  264. return (
  265. <div className='px-4 py-2 space-y-3'>
  266. {schemas.map(schema => renderField(schema))}
  267. </div>
  268. )
  269. }
  270. export default ReasoningConfigForm