features.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {
  2. memo,
  3. useCallback,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import { RiCloseLine } from '@remixicon/react'
  7. import { useNodes } from 'reactflow'
  8. import { useStore } from './store'
  9. import {
  10. useIsChatMode,
  11. useNodesReadOnly,
  12. useNodesSyncDraft,
  13. } from './hooks'
  14. import { type CommonNodeType, type InputVar, InputVarType, type Node } from './types'
  15. import useConfig from './nodes/start/use-config'
  16. import type { StartNodeType } from './nodes/start/types'
  17. import {
  18. FeaturesChoose,
  19. FeaturesPanel,
  20. } from '@/app/components/base/features'
  21. import type { PromptVariable } from '@/models/debug'
  22. const Features = () => {
  23. const { t } = useTranslation()
  24. const isChatMode = useIsChatMode()
  25. const setShowFeaturesPanel = useStore(s => s.setShowFeaturesPanel)
  26. const { nodesReadOnly } = useNodesReadOnly()
  27. const { handleSyncWorkflowDraft } = useNodesSyncDraft()
  28. const nodes = useNodes<CommonNodeType>()
  29. const startNode = nodes.find(node => node.data.type === 'start')
  30. const { id, data } = startNode as Node<StartNodeType>
  31. const { handleAddVariable } = useConfig(id, data)
  32. const handleAddOpeningStatementVariable = (variables: PromptVariable[]) => {
  33. const newVariable = variables[0]
  34. const startNodeVariable: InputVar = {
  35. variable: newVariable.key,
  36. label: newVariable.name,
  37. type: InputVarType.textInput,
  38. max_length: newVariable.max_length,
  39. required: newVariable.required || false,
  40. options: [],
  41. }
  42. handleAddVariable(startNodeVariable)
  43. }
  44. const handleFeaturesChange = useCallback(() => {
  45. handleSyncWorkflowDraft()
  46. }, [handleSyncWorkflowDraft])
  47. return (
  48. <div className='fixed top-16 left-2 bottom-2 w-[600px] rounded-2xl border-[0.5px] border-gray-200 bg-white shadow-xl z-10'>
  49. <div className='flex items-center justify-between px-4 pt-3'>
  50. {t('workflow.common.features')}
  51. <div className='flex items-center'>
  52. {
  53. isChatMode && (
  54. <>
  55. <FeaturesChoose
  56. disabled={nodesReadOnly}
  57. onChange={handleFeaturesChange}
  58. />
  59. <div className='mx-3 w-[1px] h-[14px] bg-gray-200'></div>
  60. </>
  61. )
  62. }
  63. <div
  64. className='flex items-center justify-center w-6 h-6 cursor-pointer'
  65. onClick={() => setShowFeaturesPanel(false)}
  66. >
  67. <RiCloseLine className='w-4 h-4 text-gray-500' />
  68. </div>
  69. </div>
  70. </div>
  71. <div className='p-4'>
  72. <FeaturesPanel
  73. disabled={nodesReadOnly}
  74. onChange={handleFeaturesChange}
  75. openingStatementProps={{
  76. onAutoAddPromptVariable: handleAddOpeningStatementVariable,
  77. }}
  78. workflowVariables={data.variables}
  79. />
  80. </div>
  81. </div>
  82. )
  83. }
  84. export default memo(Features)