panel.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import type { FC } from 'react'
  2. import {
  3. memo,
  4. useCallback,
  5. } from 'react'
  6. import { useTranslation } from 'react-i18next'
  7. import VarReferencePicker from '../_base/components/variable/var-reference-picker'
  8. import useConfig from './use-config'
  9. import RetrievalConfig from './components/retrieval-config'
  10. import AddKnowledge from './components/add-dataset'
  11. import DatasetList from './components/dataset-list'
  12. import type { KnowledgeRetrievalNodeType } from './types'
  13. import Field from '@/app/components/workflow/nodes/_base/components/field'
  14. import Split from '@/app/components/workflow/nodes/_base/components/split'
  15. import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
  16. import { InputVarType, type NodePanelProps } from '@/app/components/workflow/types'
  17. import BeforeRunForm from '@/app/components/workflow/nodes/_base/components/before-run-form'
  18. import ResultPanel from '@/app/components/workflow/run/result-panel'
  19. const i18nPrefix = 'workflow.nodes.knowledgeRetrieval'
  20. const Panel: FC<NodePanelProps<KnowledgeRetrievalNodeType>> = ({
  21. id,
  22. data,
  23. }) => {
  24. const { t } = useTranslation()
  25. const {
  26. readOnly,
  27. inputs,
  28. handleQueryVarChange,
  29. filterVar,
  30. handleModelChanged,
  31. handleCompletionParamsChange,
  32. handleRetrievalModeChange,
  33. handleMultipleRetrievalConfigChange,
  34. selectedDatasets,
  35. handleOnDatasetsChange,
  36. isShowSingleRun,
  37. hideSingleRun,
  38. runningStatus,
  39. handleRun,
  40. handleStop,
  41. query,
  42. setQuery,
  43. runResult,
  44. rerankModelOpen,
  45. setRerankModelOpen,
  46. } = useConfig(id, data)
  47. const handleOpenFromPropsChange = useCallback((openFromProps: boolean) => {
  48. setRerankModelOpen(openFromProps)
  49. }, [setRerankModelOpen])
  50. return (
  51. <div className='mt-2'>
  52. <div className='px-4 pb-4 space-y-4'>
  53. {/* {JSON.stringify(inputs, null, 2)} */}
  54. <Field
  55. title={t(`${i18nPrefix}.queryVariable`)}
  56. >
  57. <VarReferencePicker
  58. nodeId={id}
  59. readonly={readOnly}
  60. isShowNodeName
  61. value={inputs.query_variable_selector}
  62. onChange={handleQueryVarChange}
  63. filterVar={filterVar}
  64. />
  65. </Field>
  66. <Field
  67. title={t(`${i18nPrefix}.knowledge`)}
  68. operations={
  69. <div className='flex items-center space-x-1'>
  70. <RetrievalConfig
  71. payload={{
  72. retrieval_mode: inputs.retrieval_mode,
  73. multiple_retrieval_config: inputs.multiple_retrieval_config,
  74. single_retrieval_config: inputs.single_retrieval_config,
  75. }}
  76. onRetrievalModeChange={handleRetrievalModeChange}
  77. onMultipleRetrievalConfigChange={handleMultipleRetrievalConfigChange}
  78. singleRetrievalModelConfig={inputs.single_retrieval_config?.model}
  79. onSingleRetrievalModelChange={handleModelChanged as any}
  80. onSingleRetrievalModelParamsChange={handleCompletionParamsChange}
  81. readonly={readOnly || !selectedDatasets.length}
  82. openFromProps={rerankModelOpen}
  83. onOpenFromPropsChange={handleOpenFromPropsChange}
  84. selectedDatasets={selectedDatasets}
  85. />
  86. {!readOnly && (<div className='w-px h-3 bg-gray-200'></div>)}
  87. {!readOnly && (
  88. <AddKnowledge
  89. selectedIds={inputs.dataset_ids}
  90. onChange={handleOnDatasetsChange}
  91. />
  92. )}
  93. </div>
  94. }
  95. >
  96. <DatasetList
  97. list={selectedDatasets}
  98. onChange={handleOnDatasetsChange}
  99. readonly={readOnly}
  100. />
  101. </Field>
  102. </div>
  103. <Split />
  104. <div className='px-4 pt-4 pb-2'>
  105. <OutputVars>
  106. <>
  107. <VarItem
  108. name='result'
  109. type='Array[Object]'
  110. description={t(`${i18nPrefix}.outputVars.output`)}
  111. subItems={[
  112. {
  113. name: 'content',
  114. type: 'string',
  115. description: t(`${i18nPrefix}.outputVars.content`),
  116. },
  117. // url, title, link like bing search reference result: link, link page title, link page icon
  118. {
  119. name: 'title',
  120. type: 'string',
  121. description: t(`${i18nPrefix}.outputVars.title`),
  122. },
  123. {
  124. name: 'url',
  125. type: 'string',
  126. description: t(`${i18nPrefix}.outputVars.url`),
  127. },
  128. {
  129. name: 'icon',
  130. type: 'string',
  131. description: t(`${i18nPrefix}.outputVars.icon`),
  132. },
  133. {
  134. name: 'metadata',
  135. type: 'object',
  136. description: t(`${i18nPrefix}.outputVars.metadata`),
  137. },
  138. ]}
  139. />
  140. </>
  141. </OutputVars>
  142. {isShowSingleRun && (
  143. <BeforeRunForm
  144. nodeName={inputs.title}
  145. onHide={hideSingleRun}
  146. forms={[
  147. {
  148. inputs: [{
  149. label: t(`${i18nPrefix}.queryVariable`)!,
  150. variable: 'query',
  151. type: InputVarType.paragraph,
  152. required: true,
  153. }],
  154. values: { query },
  155. onChange: keyValue => setQuery((keyValue as any).query),
  156. },
  157. ]}
  158. runningStatus={runningStatus}
  159. onRun={handleRun}
  160. onStop={handleStop}
  161. result={<ResultPanel {...runResult} showSteps={false} />}
  162. />
  163. )}
  164. </div>
  165. </div>
  166. )
  167. }
  168. export default memo(Panel)