index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import {
  2. memo,
  3. useCallback,
  4. useState,
  5. } from 'react'
  6. import { useContext } from 'use-context-selector'
  7. import {
  8. useStoreApi,
  9. } from 'reactflow'
  10. import { RiBookOpenLine, RiCloseLine } from '@remixicon/react'
  11. import { useTranslation } from 'react-i18next'
  12. import { useStore } from '@/app/components/workflow/store'
  13. import ActionButton, { ActionButtonState } from '@/app/components/base/action-button'
  14. import { BubbleX, LongArrowLeft, LongArrowRight } from '@/app/components/base/icons/src/vender/line/others'
  15. import BlockIcon from '@/app/components/workflow/block-icon'
  16. import VariableModalTrigger from '@/app/components/workflow/panel/chat-variable-panel/components/variable-modal-trigger'
  17. import VariableItem from '@/app/components/workflow/panel/chat-variable-panel/components/variable-item'
  18. import RemoveEffectVarConfirm from '@/app/components/workflow/nodes/_base/components/remove-effect-var-confirm'
  19. import type {
  20. ConversationVariable,
  21. } from '@/app/components/workflow/types'
  22. import { findUsedVarNodes, updateNodeVars } from '@/app/components/workflow/nodes/_base/components/variable/utils'
  23. import { useNodesSyncDraft } from '@/app/components/workflow/hooks/use-nodes-sync-draft'
  24. import { BlockEnum } from '@/app/components/workflow/types'
  25. import I18n from '@/context/i18n'
  26. import { LanguagesSupported } from '@/i18n/language'
  27. import cn from '@/utils/classnames'
  28. const ChatVariablePanel = () => {
  29. const { t } = useTranslation()
  30. const { locale } = useContext(I18n)
  31. const store = useStoreApi()
  32. const setShowChatVariablePanel = useStore(s => s.setShowChatVariablePanel)
  33. const varList = useStore(s => s.conversationVariables) as ConversationVariable[]
  34. const updateChatVarList = useStore(s => s.setConversationVariables)
  35. const { doSyncWorkflowDraft } = useNodesSyncDraft()
  36. const [showTip, setShowTip] = useState(true)
  37. const [showVariableModal, setShowVariableModal] = useState(false)
  38. const [currentVar, setCurrentVar] = useState<ConversationVariable>()
  39. const [showRemoveVarConfirm, setShowRemoveConfirm] = useState(false)
  40. const [cacheForDelete, setCacheForDelete] = useState<ConversationVariable>()
  41. const getEffectedNodes = useCallback((chatVar: ConversationVariable) => {
  42. const { getNodes } = store.getState()
  43. const allNodes = getNodes()
  44. return findUsedVarNodes(
  45. ['conversation', chatVar.name],
  46. allNodes,
  47. )
  48. }, [store])
  49. const removeUsedVarInNodes = useCallback((chatVar: ConversationVariable) => {
  50. const { getNodes, setNodes } = store.getState()
  51. const effectedNodes = getEffectedNodes(chatVar)
  52. const newNodes = getNodes().map((node) => {
  53. if (effectedNodes.find(n => n.id === node.id))
  54. return updateNodeVars(node, ['conversation', chatVar.name], [])
  55. return node
  56. })
  57. setNodes(newNodes)
  58. }, [getEffectedNodes, store])
  59. const handleEdit = (chatVar: ConversationVariable) => {
  60. setCurrentVar(chatVar)
  61. setShowVariableModal(true)
  62. }
  63. const handleDelete = useCallback((chatVar: ConversationVariable) => {
  64. removeUsedVarInNodes(chatVar)
  65. updateChatVarList(varList.filter(v => v.id !== chatVar.id))
  66. setCacheForDelete(undefined)
  67. setShowRemoveConfirm(false)
  68. doSyncWorkflowDraft()
  69. }, [doSyncWorkflowDraft, removeUsedVarInNodes, updateChatVarList, varList])
  70. const deleteCheck = useCallback((chatVar: ConversationVariable) => {
  71. const effectedNodes = getEffectedNodes(chatVar)
  72. if (effectedNodes.length > 0) {
  73. setCacheForDelete(chatVar)
  74. setShowRemoveConfirm(true)
  75. }
  76. else {
  77. handleDelete(chatVar)
  78. }
  79. }, [getEffectedNodes, handleDelete])
  80. const handleSave = useCallback(async (chatVar: ConversationVariable) => {
  81. // add chatVar
  82. if (!currentVar) {
  83. const newList = [chatVar, ...varList]
  84. updateChatVarList(newList)
  85. doSyncWorkflowDraft()
  86. return
  87. }
  88. // edit chatVar
  89. const newList = varList.map(v => v.id === currentVar.id ? chatVar : v)
  90. updateChatVarList(newList)
  91. // side effects of rename env
  92. if (currentVar.name !== chatVar.name) {
  93. const { getNodes, setNodes } = store.getState()
  94. const effectedNodes = getEffectedNodes(currentVar)
  95. const newNodes = getNodes().map((node) => {
  96. if (effectedNodes.find(n => n.id === node.id))
  97. return updateNodeVars(node, ['conversation', currentVar.name], ['conversation', chatVar.name])
  98. return node
  99. })
  100. setNodes(newNodes)
  101. }
  102. doSyncWorkflowDraft()
  103. }, [currentVar, doSyncWorkflowDraft, getEffectedNodes, store, updateChatVarList, varList])
  104. return (
  105. <div
  106. className={cn(
  107. 'relative flex flex-col w-[420px] bg-components-panel-bg-alt rounded-l-2xl h-full border border-components-panel-border',
  108. )}
  109. >
  110. <div className='shrink-0 flex items-center justify-between p-4 pb-0 text-text-primary system-xl-semibold'>
  111. {t('workflow.chatVariable.panelTitle')}
  112. <div className='flex items-center gap-1'>
  113. <ActionButton state={showTip ? ActionButtonState.Active : undefined} onClick={() => setShowTip(!showTip)}>
  114. <RiBookOpenLine className='w-4 h-4' />
  115. </ActionButton>
  116. <div
  117. className='flex items-center justify-center w-6 h-6 cursor-pointer'
  118. onClick={() => setShowChatVariablePanel(false)}
  119. >
  120. <RiCloseLine className='w-4 h-4 text-text-tertiary' />
  121. </div>
  122. </div>
  123. </div>
  124. {showTip && (
  125. <div className='shrink-0 px-3 pt-2.5 pb-2'>
  126. <div className='relative p-3 radius-2xl bg-background-section-burn'>
  127. <div className='inline-block py-[3px] px-[5px] rounded-[5px] border border-divider-deep text-text-tertiary system-2xs-medium-uppercase'>TIPS</div>
  128. <div className='mt-1 mb-4 system-sm-regular text-text-secondary'>
  129. {t('workflow.chatVariable.panelDescription')}
  130. <a target='_blank' rel='noopener noreferrer' className='text-text-accent' href={locale !== LanguagesSupported[1] ? 'https://docs.dify.ai/guides/workflow/variables#conversation-variables' : `https://docs.dify.ai/${locale.toLowerCase()}/guides/workflow/variables#hui-hua-bian-liang`}>{t('workflow.chatVariable.docLink')}</a>
  131. </div>
  132. <div className='flex items-center gap-2'>
  133. <div className='flex flex-col p-3 pb-4 bg-workflow-block-bg radius-lg border border-workflow-block-border shadow-md'>
  134. <BubbleX className='shrink-0 mb-1 w-4 h-4 text-util-colors-teal-teal-700' />
  135. <div className='text-text-secondary system-xs-semibold'>conversation_var</div>
  136. <div className='text-text-tertiary system-2xs-regular'>String</div>
  137. </div>
  138. <div className='grow'>
  139. <div className='mb-2 flex items-center gap-2 py-1'>
  140. <div className='shrink-0 flex items-center gap-1 w-16 h-3 px-1'>
  141. <LongArrowLeft className='grow h-2 text-text-quaternary' />
  142. <div className='shrink-0 text-text-tertiary system-2xs-medium'>WRITE</div>
  143. </div>
  144. <BlockIcon className='shrink-0' type={BlockEnum.Assigner} />
  145. <div className='grow text-text-secondary system-xs-semibold truncate'>{t('workflow.blocks.assigner')}</div>
  146. </div>
  147. <div className='flex items-center gap-2 py-1'>
  148. <div className='shrink-0 flex items-center gap-1 w-16 h-3 px-1'>
  149. <div className='shrink-0 text-text-tertiary system-2xs-medium'>READ</div>
  150. <LongArrowRight className='grow h-2 text-text-quaternary' />
  151. </div>
  152. <BlockIcon className='shrink-0' type={BlockEnum.LLM} />
  153. <div className='grow text-text-secondary system-xs-semibold truncate'>{t('workflow.blocks.llm')}</div>
  154. </div>
  155. </div>
  156. </div>
  157. <div className='absolute z-10 top-[-4px] right-[38px] w-3 h-3 bg-background-section-burn rotate-45'/>
  158. </div>
  159. </div>
  160. )}
  161. <div className='shrink-0 px-4 pt-2 pb-3'>
  162. <VariableModalTrigger
  163. open={showVariableModal}
  164. setOpen={setShowVariableModal}
  165. showTip={showTip}
  166. chatVar={currentVar}
  167. onSave={handleSave}
  168. onClose={() => setCurrentVar(undefined)}
  169. />
  170. </div>
  171. <div className='grow px-4 rounded-b-2xl overflow-y-auto'>
  172. {varList.map(chatVar => (
  173. <VariableItem
  174. key={chatVar.id}
  175. item={chatVar}
  176. onEdit={handleEdit}
  177. onDelete={deleteCheck}
  178. />
  179. ))}
  180. </div>
  181. <RemoveEffectVarConfirm
  182. isShow={showRemoveVarConfirm}
  183. onCancel={() => setShowRemoveConfirm(false)}
  184. onConfirm={() => cacheForDelete && handleDelete(cacheForDelete)}
  185. />
  186. </div>
  187. )
  188. }
  189. export default memo(ChatVariablePanel)