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.

panel.tsx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import type { FC } from 'react'
  2. import React, { useCallback } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import MemoryConfig from '../_base/components/memory-config'
  5. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  6. import ConfigVision from '../_base/components/config-vision'
  7. import useConfig from './use-config'
  8. import type { LLMNodeType } from './types'
  9. import ConfigPrompt from './components/config-prompt'
  10. import VarList from '@/app/components/workflow/nodes/_base/components/variable/var-list'
  11. import AddButton2 from '@/app/components/base/button/add-button'
  12. import Field from '@/app/components/workflow/nodes/_base/components/field'
  13. import Split from '@/app/components/workflow/nodes/_base/components/split'
  14. import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
  15. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  16. import type { NodePanelProps } from '@/app/components/workflow/types'
  17. import Tooltip from '@/app/components/base/tooltip'
  18. import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor'
  19. import StructureOutput from './components/structure-output'
  20. import ReasoningFormatConfig from './components/reasoning-format-config'
  21. import Switch from '@/app/components/base/switch'
  22. import { RiAlertFill, RiQuestionLine } from '@remixicon/react'
  23. import { fetchAndMergeValidCompletionParams } from '@/utils/completion-params'
  24. import Toast from '@/app/components/base/toast'
  25. const i18nPrefix = 'workflow.nodes.llm'
  26. const Panel: FC<NodePanelProps<LLMNodeType>> = ({
  27. id,
  28. data,
  29. }) => {
  30. const { t } = useTranslation()
  31. const {
  32. readOnly,
  33. inputs,
  34. isChatModel,
  35. isChatMode,
  36. isCompletionModel,
  37. shouldShowContextTip,
  38. isVisionModel,
  39. handleModelChanged,
  40. hasSetBlockStatus,
  41. handleCompletionParamsChange,
  42. handleContextVarChange,
  43. filterInputVar,
  44. filterVar,
  45. availableVars,
  46. availableNodesWithParent,
  47. isShowVars,
  48. handlePromptChange,
  49. handleAddEmptyVariable,
  50. handleAddVariable,
  51. handleVarListChange,
  52. handleVarNameChange,
  53. handleSyeQueryChange,
  54. handleMemoryChange,
  55. handleVisionResolutionEnabledChange,
  56. handleVisionResolutionChange,
  57. isModelSupportStructuredOutput,
  58. structuredOutputCollapsed,
  59. setStructuredOutputCollapsed,
  60. handleStructureOutputEnableChange,
  61. handleStructureOutputChange,
  62. filterJinja2InputVar,
  63. handleReasoningFormatChange,
  64. } = useConfig(id, data)
  65. const model = inputs.model
  66. const handleModelChange = useCallback((model: {
  67. provider: string
  68. modelId: string
  69. mode?: string
  70. }) => {
  71. (async () => {
  72. try {
  73. const { params: filtered, removedDetails } = await fetchAndMergeValidCompletionParams(
  74. model.provider,
  75. model.modelId,
  76. inputs.model.completion_params,
  77. true,
  78. )
  79. const keys = Object.keys(removedDetails)
  80. if (keys.length)
  81. Toast.notify({ type: 'warning', message: `${t('common.modelProvider.parametersInvalidRemoved')}: ${keys.map(k => `${k} (${removedDetails[k]})`).join(', ')}` })
  82. handleCompletionParamsChange(filtered)
  83. }
  84. catch {
  85. Toast.notify({ type: 'error', message: t('common.error') })
  86. handleCompletionParamsChange({})
  87. }
  88. finally {
  89. handleModelChanged(model)
  90. }
  91. })()
  92. }, [inputs.model.completion_params])
  93. return (
  94. <div className='mt-2'>
  95. <div className='space-y-4 px-4 pb-4'>
  96. <Field
  97. title={t(`${i18nPrefix}.model`)}
  98. required
  99. >
  100. <ModelParameterModal
  101. popupClassName='!w-[387px]'
  102. isInWorkflow
  103. isAdvancedMode={true}
  104. mode={model?.mode}
  105. provider={model?.provider}
  106. completionParams={model?.completion_params}
  107. modelId={model?.name}
  108. setModel={handleModelChange}
  109. onCompletionParamsChange={handleCompletionParamsChange}
  110. hideDebugWithMultipleModel
  111. debugWithMultipleModel={false}
  112. readonly={readOnly}
  113. />
  114. </Field>
  115. {/* knowledge */}
  116. <Field
  117. title={t(`${i18nPrefix}.context`)}
  118. tooltip={t(`${i18nPrefix}.contextTooltip`)!}
  119. >
  120. <>
  121. <VarReferencePicker
  122. readonly={readOnly}
  123. nodeId={id}
  124. isShowNodeName
  125. value={inputs.context?.variable_selector || []}
  126. onChange={handleContextVarChange}
  127. filterVar={filterVar}
  128. />
  129. {shouldShowContextTip && (
  130. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.notSetContextInPromptTip`)}</div>
  131. )}
  132. </>
  133. </Field>
  134. {/* Prompt */}
  135. {model.name && (
  136. <ConfigPrompt
  137. readOnly={readOnly}
  138. nodeId={id}
  139. filterVar={isShowVars ? filterJinja2InputVar : filterInputVar}
  140. isChatModel={isChatModel}
  141. isChatApp={isChatMode}
  142. isShowContext
  143. payload={inputs.prompt_template}
  144. onChange={handlePromptChange}
  145. hasSetBlockStatus={hasSetBlockStatus}
  146. varList={inputs.prompt_config?.jinja2_variables || []}
  147. handleAddVariable={handleAddVariable}
  148. modelConfig={model}
  149. />
  150. )}
  151. {isShowVars && (
  152. <Field
  153. title={t('workflow.nodes.templateTransform.inputVars')}
  154. operations={
  155. !readOnly ? <AddButton2 onClick={handleAddEmptyVariable} /> : undefined
  156. }
  157. >
  158. <VarList
  159. nodeId={id}
  160. readonly={readOnly}
  161. list={inputs.prompt_config?.jinja2_variables || []}
  162. onChange={handleVarListChange}
  163. onVarNameChange={handleVarNameChange}
  164. filterVar={filterJinja2InputVar}
  165. isSupportFileVar={false}
  166. />
  167. </Field>
  168. )}
  169. {/* Memory put place examples. */}
  170. {isChatMode && isChatModel && !!inputs.memory && (
  171. <div className='mt-4'>
  172. <div className='flex h-8 items-center justify-between rounded-lg bg-components-input-bg-normal pl-3 pr-2'>
  173. <div className='flex items-center space-x-1'>
  174. <div className='text-xs font-semibold uppercase text-text-secondary'>{t('workflow.nodes.common.memories.title')}</div>
  175. <Tooltip
  176. popupContent={t('workflow.nodes.common.memories.tip')}
  177. triggerClassName='w-4 h-4'
  178. />
  179. </div>
  180. <div className='flex h-[18px] items-center rounded-[5px] border border-divider-deep bg-components-badge-bg-dimm px-1 text-xs font-semibold uppercase text-text-tertiary'>{t('workflow.nodes.common.memories.builtIn')}</div>
  181. </div>
  182. {/* Readonly User Query */}
  183. <div className='mt-4'>
  184. <Editor
  185. title={<div className='flex items-center space-x-1'>
  186. <div className='text-xs font-semibold uppercase text-text-secondary'>user</div>
  187. <Tooltip
  188. popupContent={
  189. <div className='max-w-[180px]'>{t('workflow.nodes.llm.roleDescription.user')}</div>
  190. }
  191. triggerClassName='w-4 h-4'
  192. />
  193. </div>}
  194. value={inputs.memory.query_prompt_template || '{{#sys.query#}}'}
  195. onChange={handleSyeQueryChange}
  196. readOnly={readOnly}
  197. isShowContext={false}
  198. isChatApp
  199. isChatModel
  200. hasSetBlockStatus={hasSetBlockStatus}
  201. nodesOutputVars={availableVars}
  202. availableNodes={availableNodesWithParent}
  203. isSupportFileVar
  204. />
  205. {inputs.memory.query_prompt_template && !inputs.memory.query_prompt_template.includes('{{#sys.query#}}') && (
  206. <div className='text-xs font-normal leading-[18px] text-[#DC6803]'>{t(`${i18nPrefix}.sysQueryInUser`)}</div>
  207. )}
  208. </div>
  209. </div>
  210. )}
  211. {/* Memory */}
  212. {isChatMode && (
  213. <>
  214. <Split />
  215. <MemoryConfig
  216. readonly={readOnly}
  217. config={{ data: inputs.memory }}
  218. onChange={handleMemoryChange}
  219. canSetRoleName={isCompletionModel}
  220. />
  221. </>
  222. )}
  223. {/* Vision: GPT4-vision and so on */}
  224. <ConfigVision
  225. nodeId={id}
  226. readOnly={readOnly}
  227. isVisionModel={isVisionModel}
  228. enabled={inputs.vision?.enabled}
  229. onEnabledChange={handleVisionResolutionEnabledChange}
  230. config={inputs.vision?.configs}
  231. onConfigChange={handleVisionResolutionChange}
  232. />
  233. {/* Reasoning Format */}
  234. <ReasoningFormatConfig
  235. // Default to tagged for backward compatibility
  236. value={inputs.reasoning_format || 'tagged'}
  237. onChange={handleReasoningFormatChange}
  238. readonly={readOnly}
  239. />
  240. </div>
  241. <Split />
  242. <OutputVars
  243. collapsed={structuredOutputCollapsed}
  244. onCollapse={setStructuredOutputCollapsed}
  245. operations={
  246. <div className='mr-4 flex shrink-0 items-center'>
  247. {(!isModelSupportStructuredOutput && !!inputs.structured_output_enabled) && (
  248. <Tooltip noDecoration popupContent={
  249. <div className='w-[232px] rounded-xl border-[0.5px] border-components-panel-border bg-components-tooltip-bg px-4 py-3.5 shadow-lg backdrop-blur-[5px]'>
  250. <div className='title-xs-semi-bold text-text-primary'>{t('app.structOutput.modelNotSupported')}</div>
  251. <div className='body-xs-regular mt-1 text-text-secondary'>{t('app.structOutput.modelNotSupportedTip')}</div>
  252. </div>
  253. }>
  254. <div>
  255. <RiAlertFill className='mr-1 size-4 text-text-warning-secondary' />
  256. </div>
  257. </Tooltip>
  258. )}
  259. <div className='system-xs-medium-uppercase mr-0.5 text-text-tertiary'>{t('app.structOutput.structured')}</div>
  260. <Tooltip popupContent={
  261. <div className='max-w-[150px]'>{t('app.structOutput.structuredTip')}</div>
  262. }>
  263. <div>
  264. <RiQuestionLine className='size-3.5 text-text-quaternary' />
  265. </div>
  266. </Tooltip>
  267. <Switch
  268. className='ml-2'
  269. defaultValue={!!inputs.structured_output_enabled}
  270. onChange={handleStructureOutputEnableChange}
  271. size='md'
  272. disabled={readOnly}
  273. />
  274. </div>
  275. }
  276. >
  277. <>
  278. <VarItem
  279. name='text'
  280. type='string'
  281. description={t(`${i18nPrefix}.outputVars.output`)}
  282. />
  283. <VarItem
  284. name='usage'
  285. type='object'
  286. description={t(`${i18nPrefix}.outputVars.usage`)}
  287. />
  288. {inputs.structured_output_enabled && (
  289. <>
  290. <Split className='mt-3' />
  291. <StructureOutput
  292. className='mt-4'
  293. value={inputs.structured_output}
  294. onChange={handleStructureOutputChange}
  295. />
  296. </>
  297. )}
  298. </>
  299. </OutputVars>
  300. </div>
  301. )
  302. }
  303. export default React.memo(Panel)