You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

right.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { useTranslation } from 'react-i18next'
  2. import {
  3. RiArrowGoBackLine,
  4. RiCloseLine,
  5. RiFileDownloadFill,
  6. RiMenuLine,
  7. RiSparklingFill,
  8. } from '@remixicon/react'
  9. import { useStore } from '../store'
  10. import { BlockEnum } from '../types'
  11. import useCurrentVars from '../hooks/use-inspect-vars-crud'
  12. import Empty from './empty'
  13. import ValueContent from './value-content'
  14. import ActionButton from '@/app/components/base/action-button'
  15. import Badge from '@/app/components/base/badge'
  16. import CopyFeedback from '@/app/components/base/copy-feedback'
  17. import Tooltip from '@/app/components/base/tooltip'
  18. import BlockIcon from '@/app/components/workflow/block-icon'
  19. import Loading from '@/app/components/base/loading'
  20. import type { currentVarType } from './panel'
  21. import { VarInInspectType } from '@/types/workflow'
  22. import cn from '@/utils/classnames'
  23. import useNodeInfo from '../nodes/_base/hooks/use-node-info'
  24. import { useBoolean } from 'ahooks'
  25. import GetAutomaticResModal from '@/app/components/app/configuration/config/automatic/get-automatic-res'
  26. import GetCodeGeneratorResModal from '../../app/configuration/config/code-generator/get-code-generator-res'
  27. import { AppType } from '@/types/app'
  28. import { useHooksStore } from '../hooks-store'
  29. import { useCallback, useMemo } from 'react'
  30. import { useNodesInteractions, useToolIcon } from '../hooks'
  31. import { CodeLanguage } from '../nodes/code/types'
  32. import useNodeCrud from '../nodes/_base/hooks/use-node-crud'
  33. import type { GenRes } from '@/service/debug'
  34. import produce from 'immer'
  35. import { PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER } from '../../base/prompt-editor/plugins/update-block'
  36. import { useEventEmitterContextContext } from '@/context/event-emitter'
  37. import { VariableIconWithColor } from '@/app/components/workflow/nodes/_base/components/variable/variable-label'
  38. type Props = {
  39. nodeId: string
  40. currentNodeVar?: currentVarType
  41. handleOpenMenu: () => void
  42. isValueFetching?: boolean
  43. }
  44. const Right = ({
  45. nodeId,
  46. currentNodeVar,
  47. handleOpenMenu,
  48. isValueFetching,
  49. }: Props) => {
  50. const { t } = useTranslation()
  51. const bottomPanelWidth = useStore(s => s.bottomPanelWidth)
  52. const setShowVariableInspectPanel = useStore(s => s.setShowVariableInspectPanel)
  53. const setCurrentFocusNodeId = useStore(s => s.setCurrentFocusNodeId)
  54. const toolIcon = useToolIcon(currentNodeVar?.nodeData)
  55. const isTruncated = currentNodeVar?.var.is_truncated
  56. const fullContent = currentNodeVar?.var.full_content
  57. // const isTruncated = true
  58. // const fullContent = {
  59. // size_bytes: 11289600,
  60. // download_url: 'https://upload.dify.ai/files/222bc6e7-40bd-4433-9ba8-4b9ecda88b14/file-preview?timestamp=1754976824&nonce=d970eb39b119f76ec94a9b026f2825b3&sign=ltJO4vS0jrwxuBl4GU74E1Sg_Tia2Y4g2LoBoPh3970=&as_attachment=true',
  61. // }
  62. const {
  63. resetConversationVar,
  64. resetToLastRunVar,
  65. editInspectVarValue,
  66. } = useCurrentVars()
  67. const handleValueChange = (varId: string, value: any) => {
  68. if (!currentNodeVar) return
  69. editInspectVarValue(currentNodeVar.nodeId, varId, value)
  70. }
  71. const resetValue = () => {
  72. if (!currentNodeVar) return
  73. resetToLastRunVar(currentNodeVar.nodeId, currentNodeVar.var.id)
  74. }
  75. const handleClose = () => {
  76. setShowVariableInspectPanel(false)
  77. setCurrentFocusNodeId('')
  78. }
  79. const handleClear = () => {
  80. if (!currentNodeVar) return
  81. resetConversationVar(currentNodeVar.var.id)
  82. }
  83. const getCopyContent = () => {
  84. const value = currentNodeVar?.var.value
  85. if (value === null || value === undefined)
  86. return ''
  87. if (typeof value === 'object')
  88. return JSON.stringify(value)
  89. return String(value)
  90. }
  91. const configsMap = useHooksStore(s => s.configsMap)
  92. const { eventEmitter } = useEventEmitterContextContext()
  93. const { handleNodeSelect } = useNodesInteractions()
  94. const { node } = useNodeInfo(nodeId)
  95. const { setInputs } = useNodeCrud(nodeId, node?.data)
  96. const blockType = node?.data?.type
  97. const isCodeBlock = blockType === BlockEnum.Code
  98. const canShowPromptGenerator = [BlockEnum.LLM, BlockEnum.Code].includes(blockType)
  99. const currentPrompt = useMemo(() => {
  100. if (!canShowPromptGenerator)
  101. return ''
  102. if (blockType === BlockEnum.LLM)
  103. return node?.data?.prompt_template?.text || node?.data?.prompt_template?.[0].text
  104. // if (blockType === BlockEnum.Agent) {
  105. // return node?.data?.agent_parameters?.instruction?.value
  106. // }
  107. if (blockType === BlockEnum.Code)
  108. return node?.data?.code
  109. }, [canShowPromptGenerator])
  110. const [isShowPromptGenerator, {
  111. setTrue: doShowPromptGenerator,
  112. setFalse: handleHidePromptGenerator,
  113. }] = useBoolean(false)
  114. const handleShowPromptGenerator = useCallback(() => {
  115. handleNodeSelect(nodeId)
  116. doShowPromptGenerator()
  117. }, [doShowPromptGenerator, handleNodeSelect, nodeId])
  118. const handleUpdatePrompt = useCallback((res: GenRes) => {
  119. const newInputs = produce(node?.data, (draft: any) => {
  120. switch (blockType) {
  121. case BlockEnum.LLM:
  122. if (draft?.prompt_template) {
  123. if (Array.isArray(draft.prompt_template))
  124. draft.prompt_template[0].text = res.modified
  125. else
  126. draft.prompt_template.text = res.modified
  127. }
  128. break
  129. // Agent is a plugin, may has many instructions, can not locate which one to update
  130. // case BlockEnum.Agent:
  131. // if (draft?.agent_parameters?.instruction) {
  132. // draft.agent_parameters.instruction.value = res.modified
  133. // }
  134. // break
  135. case BlockEnum.Code:
  136. draft.code = res.modified
  137. break
  138. }
  139. })
  140. setInputs(newInputs)
  141. eventEmitter?.emit({
  142. type: PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER,
  143. instanceId: `${nodeId}-chat-workflow-llm-prompt-editor`,
  144. payload: res.modified,
  145. } as any)
  146. handleHidePromptGenerator()
  147. }, [setInputs, blockType, nodeId, node?.data, handleHidePromptGenerator])
  148. return (
  149. <div className={cn('flex h-full flex-col')}>
  150. {/* header */}
  151. <div className='flex shrink-0 items-center justify-between gap-1 px-2 pt-2'>
  152. {bottomPanelWidth < 488 && (
  153. <ActionButton className='shrink-0' onClick={handleOpenMenu}>
  154. <RiMenuLine className='h-4 w-4' />
  155. </ActionButton>
  156. )}
  157. <div className='flex w-0 grow items-center gap-1'>
  158. {currentNodeVar && (
  159. <>
  160. {
  161. [VarInInspectType.environment, VarInInspectType.conversation, VarInInspectType.system].includes(currentNodeVar.nodeType as VarInInspectType) && (
  162. <VariableIconWithColor
  163. variableCategory={currentNodeVar.nodeType as VarInInspectType}
  164. className='size-4'
  165. />
  166. )
  167. }
  168. {currentNodeVar.nodeType !== VarInInspectType.environment && currentNodeVar.nodeType !== VarInInspectType.conversation && currentNodeVar.nodeType !== VarInInspectType.system && (
  169. <>
  170. <BlockIcon
  171. className='shrink-0'
  172. type={currentNodeVar.nodeType as BlockEnum}
  173. size='xs'
  174. toolIcon={toolIcon}
  175. />
  176. <div className='system-sm-regular shrink-0 text-text-secondary'>{currentNodeVar.title}</div>
  177. <div className='system-sm-regular shrink-0 text-text-quaternary'>/</div>
  178. </>
  179. )}
  180. <div title={currentNodeVar.var.name} className='system-sm-semibold truncate text-text-secondary'>{currentNodeVar.var.name}</div>
  181. <div className='system-xs-medium ml-1 shrink-0 space-x-2 text-text-tertiary'>
  182. <span>{currentNodeVar.var.value_type}</span>
  183. {isTruncated && (
  184. <>
  185. <span>·</span>
  186. <span>{((fullContent?.size_bytes || 0) / 1024 / 1024).toFixed(1)}MB</span>
  187. </>
  188. )}
  189. </div>
  190. </>
  191. )}
  192. </div>
  193. <div className='flex shrink-0 items-center gap-1'>
  194. {currentNodeVar && (
  195. <>
  196. {canShowPromptGenerator && (
  197. <Tooltip popupContent={t('appDebug.generate.optimizePromptTooltip')}>
  198. <div
  199. className='cursor-pointer rounded-md p-1 hover:bg-state-accent-active'
  200. onClick={handleShowPromptGenerator}
  201. >
  202. <RiSparklingFill className='size-4 text-components-input-border-active-prompt-1' />
  203. </div>
  204. </Tooltip>
  205. )}
  206. {isTruncated && (
  207. <Tooltip popupContent={t('workflow.debug.variableInspect.exportToolTip')}>
  208. <ActionButton>
  209. <a
  210. href={fullContent?.download_url}
  211. target='_blank'
  212. >
  213. <RiFileDownloadFill className='size-4' />
  214. </a>
  215. </ActionButton>
  216. </Tooltip>
  217. )}
  218. {!isTruncated && currentNodeVar.var.edited && (
  219. <Badge>
  220. <span className='ml-[2.5px] mr-[4.5px] h-[3px] w-[3px] rounded bg-text-accent-secondary'></span>
  221. <span className='system-2xs-semibold-uupercase'>{t('workflow.debug.variableInspect.edited')}</span>
  222. </Badge>
  223. )}
  224. {!isTruncated && currentNodeVar.var.edited && currentNodeVar.var.type !== VarInInspectType.conversation && (
  225. <Tooltip popupContent={t('workflow.debug.variableInspect.reset')}>
  226. <ActionButton onClick={resetValue}>
  227. <RiArrowGoBackLine className='h-4 w-4' />
  228. </ActionButton>
  229. </Tooltip>
  230. )}
  231. {!isTruncated && currentNodeVar.var.edited && currentNodeVar.var.type === VarInInspectType.conversation && (
  232. <Tooltip popupContent={t('workflow.debug.variableInspect.resetConversationVar')}>
  233. <ActionButton onClick={handleClear}>
  234. <RiArrowGoBackLine className='h-4 w-4' />
  235. </ActionButton>
  236. </Tooltip>
  237. )}
  238. {currentNodeVar.var.value_type !== 'secret' && (
  239. <CopyFeedback content={getCopyContent()} />
  240. )}
  241. </>
  242. )}
  243. <ActionButton onClick={handleClose}>
  244. <RiCloseLine className='h-4 w-4' />
  245. </ActionButton>
  246. </div>
  247. </div>
  248. {/* content */}
  249. <div className='grow p-2'>
  250. {!currentNodeVar && <Empty />}
  251. {isValueFetching && (
  252. <div className='flex h-full items-center justify-center'>
  253. <Loading />
  254. </div>
  255. )}
  256. {currentNodeVar && !isValueFetching && <ValueContent currentVar={currentNodeVar.var} handleValueChange={handleValueChange} isTruncated={!!isTruncated} />}
  257. </div>
  258. {isShowPromptGenerator && (
  259. isCodeBlock
  260. ? <GetCodeGeneratorResModal
  261. isShow
  262. mode={AppType.chat}
  263. onClose={handleHidePromptGenerator}
  264. flowId={configsMap?.flowId || ''}
  265. nodeId={nodeId}
  266. currentCode={currentPrompt}
  267. codeLanguages={node?.data?.code_languages || CodeLanguage.python3}
  268. onFinished={handleUpdatePrompt}
  269. />
  270. : <GetAutomaticResModal
  271. mode={AppType.chat}
  272. isShow
  273. onClose={handleHidePromptGenerator}
  274. onFinished={handleUpdatePrompt}
  275. flowId={configsMap?.flowId || ''}
  276. nodeId={nodeId}
  277. currentPrompt={currentPrompt}
  278. />
  279. )}
  280. </div>
  281. )
  282. }
  283. export default Right