You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reasoning-config-form.tsx 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import { useCallback, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import produce from 'immer'
  4. import {
  5. RiArrowRightUpLine,
  6. RiBracesLine,
  7. } from '@remixicon/react'
  8. import Tooltip from '@/app/components/base/tooltip'
  9. import Switch from '@/app/components/base/switch'
  10. import MixedVariableTextInput from '@/app/components/workflow/nodes/tool/components/mixed-variable-text-input'
  11. import Input from '@/app/components/base/input'
  12. import FormInputTypeSwitch from '@/app/components/workflow/nodes/_base/components/form-input-type-switch'
  13. import FormInputBoolean from '@/app/components/workflow/nodes/_base/components/form-input-boolean'
  14. import { SimpleSelect } from '@/app/components/base/select'
  15. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  16. import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker'
  17. import AppSelector from '@/app/components/plugins/plugin-detail-panel/app-selector'
  18. import ModelParameterModal from '@/app/components/plugins/plugin-detail-panel/model-selector'
  19. import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
  20. import { useLanguage } from '@/app/components/header/account-setting/model-provider-page/hooks'
  21. import { FormTypeEnum } from '@/app/components/header/account-setting/model-provider-page/declarations'
  22. import type { Node } from 'reactflow'
  23. import type {
  24. NodeOutPutVar,
  25. ValueSelector,
  26. } from '@/app/components/workflow/types'
  27. import type { ToolVarInputs } from '@/app/components/workflow/nodes/tool/types'
  28. import { VarType as VarKindType } from '@/app/components/workflow/nodes/tool/types'
  29. import { VarType } from '@/app/components/workflow/types'
  30. import cn from '@/utils/classnames'
  31. import { useBoolean } from 'ahooks'
  32. import SchemaModal from './schema-modal'
  33. import type { SchemaRoot } from '@/app/components/workflow/nodes/llm/types'
  34. type Props = {
  35. value: Record<string, any>
  36. onChange: (val: Record<string, any>) => void
  37. schemas: any[]
  38. nodeOutputVars: NodeOutPutVar[],
  39. availableNodes: Node[],
  40. nodeId: string
  41. }
  42. const ReasoningConfigForm: React.FC<Props> = ({
  43. value,
  44. onChange,
  45. schemas,
  46. nodeOutputVars,
  47. availableNodes,
  48. nodeId,
  49. }) => {
  50. const { t } = useTranslation()
  51. const language = useLanguage()
  52. const getVarKindType = (type: FormTypeEnum) => {
  53. if (type === FormTypeEnum.file || type === FormTypeEnum.files)
  54. return VarKindType.variable
  55. if (type === FormTypeEnum.select || type === FormTypeEnum.boolean || type === FormTypeEnum.textNumber || type === FormTypeEnum.array || type === FormTypeEnum.object)
  56. return VarKindType.constant
  57. if (type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput)
  58. return VarKindType.mixed
  59. }
  60. const handleAutomatic = (key: string, val: any, type: FormTypeEnum) => {
  61. onChange({
  62. ...value,
  63. [key]: {
  64. value: val ? null : { type: getVarKindType(type), value: null },
  65. auto: val ? 1 : 0,
  66. },
  67. })
  68. }
  69. const handleTypeChange = useCallback((variable: string, defaultValue: any) => {
  70. return (newType: VarKindType) => {
  71. const res = produce(value, (draft: ToolVarInputs) => {
  72. draft[variable].value = {
  73. type: newType,
  74. value: newType === VarKindType.variable ? '' : defaultValue,
  75. }
  76. })
  77. onChange(res)
  78. }
  79. }, [onChange, value])
  80. const handleValueChange = useCallback((variable: string, varType: FormTypeEnum) => {
  81. return (newValue: any) => {
  82. const res = produce(value, (draft: ToolVarInputs) => {
  83. draft[variable].value = {
  84. type: getVarKindType(varType),
  85. value: newValue,
  86. }
  87. })
  88. onChange(res)
  89. }
  90. }, [onChange, value])
  91. const handleAppChange = useCallback((variable: string) => {
  92. return (app: {
  93. app_id: string
  94. inputs: Record<string, any>
  95. files?: any[]
  96. }) => {
  97. const newValue = produce(value, (draft: ToolVarInputs) => {
  98. draft[variable].value = app as any
  99. })
  100. onChange(newValue)
  101. }
  102. }, [onChange, value])
  103. const handleModelChange = useCallback((variable: string) => {
  104. return (model: any) => {
  105. const newValue = produce(value, (draft: ToolVarInputs) => {
  106. draft[variable].value = {
  107. ...draft[variable].value,
  108. ...model,
  109. } as any
  110. })
  111. onChange(newValue)
  112. }
  113. }, [onChange, value])
  114. const handleVariableSelectorChange = useCallback((variable: string) => {
  115. return (newValue: ValueSelector | string) => {
  116. const res = produce(value, (draft: ToolVarInputs) => {
  117. draft[variable].value = {
  118. type: VarKindType.variable,
  119. value: newValue,
  120. }
  121. })
  122. onChange(res)
  123. }
  124. }, [onChange, value])
  125. const [isShowSchema, {
  126. setTrue: showSchema,
  127. setFalse: hideSchema,
  128. }] = useBoolean(false)
  129. const [schema, setSchema] = useState<SchemaRoot | null>(null)
  130. const [schemaRootName, setSchemaRootName] = useState<string>('')
  131. const renderField = (schema: any, showSchema: (schema: SchemaRoot, rootName: string) => void) => {
  132. const {
  133. default: defaultValue,
  134. variable,
  135. label,
  136. required,
  137. tooltip,
  138. type,
  139. scope,
  140. url,
  141. input_schema,
  142. placeholder,
  143. options,
  144. } = schema
  145. const auto = value[variable]?.auto
  146. const tooltipContent = (tooltip && (
  147. <Tooltip
  148. popupContent={<div className='w-[200px]'>
  149. {tooltip[language] || tooltip.en_US}
  150. </div>}
  151. triggerClassName='ml-0.5 w-4 h-4'
  152. asChild={false} />
  153. ))
  154. const varInput = value[variable].value
  155. const isString = type === FormTypeEnum.textInput || type === FormTypeEnum.secretInput
  156. const isNumber = type === FormTypeEnum.textNumber
  157. const isObject = type === FormTypeEnum.object
  158. const isArray = type === FormTypeEnum.array
  159. const isShowJSONEditor = isObject || isArray
  160. const isFile = type === FormTypeEnum.file || type === FormTypeEnum.files
  161. const isBoolean = type === FormTypeEnum.boolean
  162. const isSelect = type === FormTypeEnum.select
  163. const isAppSelector = type === FormTypeEnum.appSelector
  164. const isModelSelector = type === FormTypeEnum.modelSelector
  165. const showTypeSwitch = isNumber || isObject || isArray
  166. const isConstant = varInput?.type === VarKindType.constant || !varInput?.type
  167. const showVariableSelector = isFile || varInput?.type === VarKindType.variable
  168. const targetVarType = () => {
  169. if (isString)
  170. return VarType.string
  171. else if (isNumber)
  172. return VarType.number
  173. else if (type === FormTypeEnum.files)
  174. return VarType.arrayFile
  175. else if (type === FormTypeEnum.file)
  176. return VarType.file
  177. else if (isBoolean)
  178. return VarType.boolean
  179. else if (isObject)
  180. return VarType.object
  181. else if (isArray)
  182. return VarType.arrayObject
  183. else
  184. return VarType.string
  185. }
  186. const getFilterVar = () => {
  187. if (isNumber)
  188. return (varPayload: any) => varPayload.type === VarType.number
  189. else if (isString)
  190. return (varPayload: any) => [VarType.string, VarType.number, VarType.secret].includes(varPayload.type)
  191. else if (isFile)
  192. return (varPayload: any) => [VarType.file, VarType.arrayFile].includes(varPayload.type)
  193. else if (isBoolean)
  194. return (varPayload: any) => varPayload.type === VarType.boolean
  195. else if (isObject)
  196. return (varPayload: any) => varPayload.type === VarType.object
  197. else if (isArray)
  198. return (varPayload: any) => [VarType.array, VarType.arrayString, VarType.arrayNumber, VarType.arrayObject].includes(varPayload.type)
  199. return undefined
  200. }
  201. return (
  202. <div key={variable} className='space-y-0.5'>
  203. <div className='system-sm-semibold flex items-center justify-between py-2 text-text-secondary'>
  204. <div className='flex items-center'>
  205. <span className={cn('code-sm-semibold max-w-[140px] truncate text-text-secondary')} title={label[language] || label.en_US}>{label[language] || label.en_US}</span>
  206. {required && (
  207. <span className='ml-1 text-red-500'>*</span>
  208. )}
  209. {tooltipContent}
  210. <span className='system-xs-regular mx-1 text-text-quaternary'>·</span>
  211. <span className='system-xs-regular text-text-tertiary'>{targetVarType()}</span>
  212. {isShowJSONEditor && (
  213. <Tooltip
  214. popupContent={<div className='system-xs-medium text-text-secondary'>
  215. {t('workflow.nodes.agent.clickToViewParameterSchema')}
  216. </div>}
  217. asChild={false}>
  218. <div
  219. className='ml-0.5 cursor-pointer rounded-[4px] p-px text-text-tertiary hover:bg-state-base-hover hover:text-text-secondary'
  220. onClick={() => showSchema(input_schema as SchemaRoot, label[language] || label.en_US)}
  221. >
  222. <RiBracesLine className='size-3.5'/>
  223. </div>
  224. </Tooltip>
  225. )}
  226. </div>
  227. <div className='flex cursor-pointer items-center gap-1 rounded-[6px] border border-divider-subtle bg-background-default-lighter px-2 py-1 hover:bg-state-base-hover' onClick={() => handleAutomatic(variable, !auto, type)}>
  228. <span className='system-xs-medium text-text-secondary'>{t('plugin.detailPanel.toolSelector.auto')}</span>
  229. <Switch
  230. size='xs'
  231. defaultValue={!!auto}
  232. onChange={val => handleAutomatic(variable, val, type)}
  233. />
  234. </div>
  235. </div>
  236. {auto === 0 && (
  237. <div className={cn('gap-1', !(isShowJSONEditor && isConstant) && 'flex')}>
  238. {showTypeSwitch && (
  239. <FormInputTypeSwitch value={varInput?.type || VarKindType.constant} onChange={handleTypeChange(variable, defaultValue)}/>
  240. )}
  241. {isString && (
  242. <MixedVariableTextInput
  243. value={varInput?.value as string || ''}
  244. onChange={handleValueChange(variable, type)}
  245. nodesOutputVars={nodeOutputVars}
  246. availableNodes={availableNodes}
  247. />
  248. )}
  249. {isNumber && isConstant && (
  250. <Input
  251. className='h-8 grow'
  252. type='number'
  253. value={varInput?.value || ''}
  254. onChange={handleValueChange(variable, type)}
  255. placeholder={placeholder?.[language] || placeholder?.en_US}
  256. />
  257. )}
  258. {isBoolean && (
  259. <FormInputBoolean
  260. value={varInput?.value as boolean}
  261. onChange={handleValueChange(variable, type)}
  262. />
  263. )}
  264. {isSelect && (
  265. <SimpleSelect
  266. wrapperClassName='h-8 grow'
  267. defaultValue={varInput?.value}
  268. items={options.filter((option: { show_on: any[] }) => {
  269. if (option.show_on.length)
  270. return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)
  271. return true
  272. }).map((option: { value: any; label: { [x: string]: any; en_US: any } }) => ({ value: option.value, name: option.label[language] || option.label.en_US }))}
  273. onSelect={item => handleValueChange(variable, type)(item.value as string)}
  274. placeholder={placeholder?.[language] || placeholder?.en_US}
  275. />
  276. )}
  277. {isShowJSONEditor && isConstant && (
  278. <div className='mt-1 w-full'>
  279. <CodeEditor
  280. title='JSON'
  281. value={varInput?.value as any}
  282. isExpand
  283. isInNode
  284. height={100}
  285. language={CodeLanguage.json}
  286. onChange={handleValueChange(variable, type)}
  287. className='w-full'
  288. placeholder={<div className='whitespace-pre'>{placeholder?.[language] || placeholder?.en_US}</div>}
  289. />
  290. </div>
  291. )}
  292. {isAppSelector && (
  293. <AppSelector
  294. disabled={false}
  295. scope={scope || 'all'}
  296. value={varInput as any}
  297. onSelect={handleAppChange(variable)}
  298. />
  299. )}
  300. {isModelSelector && (
  301. <ModelParameterModal
  302. popupClassName='!w-[387px]'
  303. isAdvancedMode
  304. isInWorkflow
  305. value={varInput}
  306. setModel={handleModelChange(variable)}
  307. scope={scope}
  308. />
  309. )}
  310. {showVariableSelector && (
  311. <VarReferencePicker
  312. zIndex={1001}
  313. className='h-8 grow'
  314. readonly={false}
  315. isShowNodeName
  316. nodeId={nodeId}
  317. value={varInput?.value || []}
  318. onChange={handleVariableSelectorChange(variable)}
  319. filterVar={getFilterVar()}
  320. schema={schema}
  321. valueTypePlaceHolder={targetVarType()}
  322. />
  323. )}
  324. </div>
  325. )}
  326. {url && (
  327. <a
  328. href={url}
  329. target='_blank' rel='noopener noreferrer'
  330. className='inline-flex items-center text-xs text-text-accent'
  331. >
  332. {t('tools.howToGet')}
  333. <RiArrowRightUpLine className='ml-1 h-3 w-3' />
  334. </a>
  335. )}
  336. </div>
  337. )
  338. }
  339. return (
  340. <div className='space-y-3 px-4 py-2'>
  341. {!isShowSchema && schemas.map(schema => renderField(schema, (s: SchemaRoot, rootName: string) => {
  342. setSchema(s)
  343. setSchemaRootName(rootName)
  344. showSchema()
  345. }))}
  346. {isShowSchema && (
  347. <SchemaModal
  348. isShow={isShowSchema}
  349. schema={schema!}
  350. rootName={schemaRootName}
  351. onClose={hideSchema}
  352. />
  353. )}
  354. </div>
  355. )
  356. }
  357. export default ReasoningConfigForm