panel.tsx 5.4 KB

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