panel.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import type { FC } from 'react'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  5. import ConfigVision from '../_base/components/config-vision'
  6. import { findVariableWhenOnLLMVision } from '../utils'
  7. import useConfig from './use-config'
  8. import ClassList from './components/class-list'
  9. import AdvancedSetting from './components/advanced-setting'
  10. import type { QuestionClassifierNodeType } from './types'
  11. import Field from '@/app/components/workflow/nodes/_base/components/field'
  12. import ModelParameterModal from '@/app/components/header/account-setting/model-provider-page/model-parameter-modal'
  13. import { InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
  14. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  15. import ResultPanel from '@/app/components/workflow/run/result-panel'
  16. import Split from '@/app/components/workflow/nodes/_base/components/split'
  17. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  18. import { FieldCollapse } from '@/app/components/workflow/nodes/_base/components/collapse'
  19. import type { Props as FormProps } from '@/app/components/workflow/nodes/_base/components/before-run-form/form'
  20. const i18nPrefix = 'workflow.nodes.questionClassifiers'
  21. const Panel: FC<NodePanelProps<QuestionClassifierNodeType>> = ({
  22. id,
  23. data,
  24. }) => {
  25. const { t } = useTranslation()
  26. const {
  27. readOnly,
  28. inputs,
  29. handleModelChanged,
  30. isChatMode,
  31. isChatModel,
  32. handleCompletionParamsChange,
  33. handleQueryVarChange,
  34. handleTopicsChange,
  35. hasSetBlockStatus,
  36. availableVars,
  37. availableNodesWithParent,
  38. availableVisionVars,
  39. handleInstructionChange,
  40. inputVarValues,
  41. varInputs,
  42. setInputVarValues,
  43. handleMemoryChange,
  44. isVisionModel,
  45. handleVisionResolutionChange,
  46. handleVisionResolutionEnabledChange,
  47. isShowSingleRun,
  48. hideSingleRun,
  49. runningStatus,
  50. handleRun,
  51. handleStop,
  52. runResult,
  53. filterVar,
  54. visionFiles,
  55. setVisionFiles,
  56. } = useConfig(id, data)
  57. const model = inputs.model
  58. const singleRunForms = (() => {
  59. const forms: FormProps[] = []
  60. forms.push(
  61. {
  62. label: t('workflow.nodes.llm.singleRun.variable')!,
  63. inputs: [{
  64. label: t(`${i18nPrefix}.inputVars`)!,
  65. variable: 'query',
  66. type: InputVarType.paragraph,
  67. required: true,
  68. }, ...varInputs],
  69. values: inputVarValues,
  70. onChange: setInputVarValues,
  71. },
  72. )
  73. if (isVisionModel && data.vision?.enabled && data.vision?.configs?.variable_selector) {
  74. const currentVariable = findVariableWhenOnLLMVision(data.vision.configs.variable_selector, availableVisionVars)
  75. forms.push(
  76. {
  77. label: t('workflow.nodes.llm.vision')!,
  78. inputs: [{
  79. label: currentVariable?.variable as any,
  80. variable: '#files#',
  81. type: currentVariable?.formType as any,
  82. required: false,
  83. }],
  84. values: { '#files#': visionFiles },
  85. onChange: keyValue => setVisionFiles((keyValue as any)['#files#']),
  86. },
  87. )
  88. }
  89. return forms
  90. })()
  91. return (
  92. <div className='pt-2'>
  93. <div className='px-4 space-y-4'>
  94. <Field
  95. title={t(`${i18nPrefix}.model`)}
  96. >
  97. <ModelParameterModal
  98. popupClassName='!w-[387px]'
  99. isInWorkflow
  100. isAdvancedMode={true}
  101. mode={model?.mode}
  102. provider={model?.provider}
  103. completionParams={model.completion_params}
  104. modelId={model.name}
  105. setModel={handleModelChanged}
  106. onCompletionParamsChange={handleCompletionParamsChange}
  107. hideDebugWithMultipleModel
  108. debugWithMultipleModel={false}
  109. readonly={readOnly}
  110. />
  111. </Field>
  112. <Field
  113. title={t(`${i18nPrefix}.inputVars`)}
  114. >
  115. <VarReferencePicker
  116. readonly={readOnly}
  117. isShowNodeName
  118. nodeId={id}
  119. value={inputs.query_variable_selector}
  120. onChange={handleQueryVarChange}
  121. filterVar={filterVar}
  122. />
  123. </Field>
  124. <Split />
  125. <ConfigVision
  126. nodeId={id}
  127. readOnly={readOnly}
  128. isVisionModel={isVisionModel}
  129. enabled={inputs.vision?.enabled}
  130. onEnabledChange={handleVisionResolutionEnabledChange}
  131. config={inputs.vision?.configs}
  132. onConfigChange={handleVisionResolutionChange}
  133. />
  134. <Field
  135. title={t(`${i18nPrefix}.class`)}
  136. >
  137. <ClassList
  138. id={id}
  139. list={inputs.classes}
  140. onChange={handleTopicsChange}
  141. readonly={readOnly}
  142. />
  143. </Field>
  144. <Split />
  145. </div>
  146. <FieldCollapse
  147. title={t(`${i18nPrefix}.advancedSetting`)}
  148. >
  149. <AdvancedSetting
  150. hideMemorySetting={!isChatMode}
  151. instruction={inputs.instruction}
  152. onInstructionChange={handleInstructionChange}
  153. memory={inputs.memory}
  154. onMemoryChange={handleMemoryChange}
  155. readonly={readOnly}
  156. isChatApp={isChatMode}
  157. isChatModel={isChatModel}
  158. hasSetBlockStatus={hasSetBlockStatus}
  159. nodesOutputVars={availableVars}
  160. availableNodes={availableNodesWithParent}
  161. />
  162. </FieldCollapse>
  163. <Split />
  164. <div>
  165. <OutputVars>
  166. <>
  167. <VarItem
  168. name='class_name'
  169. type='string'
  170. description={t(`${i18nPrefix}.outputVars.className`)}
  171. />
  172. </>
  173. </OutputVars>
  174. </div>
  175. {isShowSingleRun && (
  176. <BeforeRunForm
  177. nodeName={inputs.title}
  178. onHide={hideSingleRun}
  179. forms={singleRunForms}
  180. runningStatus={runningStatus}
  181. onRun={handleRun}
  182. onStop={handleStop}
  183. result={<ResultPanel {...runResult} showSteps={false} />}
  184. />
  185. )}
  186. </div>
  187. )
  188. }
  189. export default React.memo(Panel)