Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

right.tsx 12KB

2 månader sedan
2 månader sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. console.log(currentNodeVar?.var)
  58. // const isTruncated = true
  59. // const fullContent = {
  60. // size_bytes: 11289600,
  61. // download_url: 'https://upload.dify.ai/files/222bc6e7-40bd-4433-9ba8-4b9ecda88b14/file-preview?timestamp=1754976824&nonce=d970eb39b119f76ec94a9b026f2825b3&sign=ltJO4vS0jrwxuBl4GU74E1Sg_Tia2Y4g2LoBoPh3970=&as_attachment=true',
  62. // }
  63. const {
  64. resetConversationVar,
  65. resetToLastRunVar,
  66. editInspectVarValue,
  67. } = useCurrentVars()
  68. const handleValueChange = (varId: string, value: any) => {
  69. if (!currentNodeVar) return
  70. editInspectVarValue(currentNodeVar.nodeId, varId, value)
  71. }
  72. const resetValue = () => {
  73. if (!currentNodeVar) return
  74. resetToLastRunVar(currentNodeVar.nodeId, currentNodeVar.var.id)
  75. }
  76. const handleClose = () => {
  77. setShowVariableInspectPanel(false)
  78. setCurrentFocusNodeId('')
  79. }
  80. const handleClear = () => {
  81. if (!currentNodeVar) return
  82. resetConversationVar(currentNodeVar.var.id)
  83. }
  84. const getCopyContent = () => {
  85. const value = currentNodeVar?.var.value
  86. if (value === null || value === undefined)
  87. return ''
  88. if (typeof value === 'object')
  89. return JSON.stringify(value)
  90. return String(value)
  91. }
  92. const configsMap = useHooksStore(s => s.configsMap)
  93. const { eventEmitter } = useEventEmitterContextContext()
  94. const { handleNodeSelect } = useNodesInteractions()
  95. const { node } = useNodeInfo(nodeId)
  96. const { setInputs } = useNodeCrud(nodeId, node?.data)
  97. const blockType = node?.data?.type
  98. const isCodeBlock = blockType === BlockEnum.Code
  99. const canShowPromptGenerator = [BlockEnum.LLM, BlockEnum.Code].includes(blockType)
  100. const currentPrompt = useMemo(() => {
  101. if (!canShowPromptGenerator)
  102. return ''
  103. if (blockType === BlockEnum.LLM)
  104. return node?.data?.prompt_template?.text || node?.data?.prompt_template?.[0].text
  105. // if (blockType === BlockEnum.Agent) {
  106. // return node?.data?.agent_parameters?.instruction?.value
  107. // }
  108. if (blockType === BlockEnum.Code)
  109. return node?.data?.code
  110. }, [canShowPromptGenerator])
  111. const [isShowPromptGenerator, {
  112. setTrue: doShowPromptGenerator,
  113. setFalse: handleHidePromptGenerator,
  114. }] = useBoolean(false)
  115. const handleShowPromptGenerator = useCallback(() => {
  116. handleNodeSelect(nodeId)
  117. doShowPromptGenerator()
  118. }, [doShowPromptGenerator, handleNodeSelect, nodeId])
  119. const handleUpdatePrompt = useCallback((res: GenRes) => {
  120. const newInputs = produce(node?.data, (draft: any) => {
  121. switch (blockType) {
  122. case BlockEnum.LLM:
  123. if (draft?.prompt_template) {
  124. if (Array.isArray(draft.prompt_template))
  125. draft.prompt_template[0].text = res.modified
  126. else
  127. draft.prompt_template.text = res.modified
  128. }
  129. break
  130. // Agent is a plugin, may has many instructions, can not locate which one to update
  131. // case BlockEnum.Agent:
  132. // if (draft?.agent_parameters?.instruction) {
  133. // draft.agent_parameters.instruction.value = res.modified
  134. // }
  135. // break
  136. case BlockEnum.Code:
  137. draft.code = res.modified
  138. break
  139. }
  140. })
  141. setInputs(newInputs)
  142. eventEmitter?.emit({
  143. type: PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER,
  144. instanceId: `${nodeId}-chat-workflow-llm-prompt-editor`,
  145. payload: res.modified,
  146. } as any)
  147. handleHidePromptGenerator()
  148. }, [setInputs, blockType, nodeId, node?.data, handleHidePromptGenerator])
  149. const displaySchemaType = currentNodeVar?.var.schemaType ? (`(${currentNodeVar.var.schemaType})`) : ''
  150. return (
  151. <div className={cn('flex h-full flex-col')}>
  152. {/* header */}
  153. <div className='flex shrink-0 items-center justify-between gap-1 px-2 pt-2'>
  154. {bottomPanelWidth < 488 && (
  155. <ActionButton className='shrink-0' onClick={handleOpenMenu}>
  156. <RiMenuLine className='h-4 w-4' />
  157. </ActionButton>
  158. )}
  159. <div className='flex w-0 grow items-center gap-1'>
  160. {currentNodeVar && (
  161. <>
  162. {
  163. [VarInInspectType.environment, VarInInspectType.conversation, VarInInspectType.system].includes(currentNodeVar.nodeType as VarInInspectType) && (
  164. <VariableIconWithColor
  165. variableCategory={currentNodeVar.nodeType as VarInInspectType}
  166. className='size-4'
  167. />
  168. )
  169. }
  170. {currentNodeVar.nodeType !== VarInInspectType.environment
  171. && currentNodeVar.nodeType !== VarInInspectType.conversation
  172. && currentNodeVar.nodeType !== VarInInspectType.system
  173. && (
  174. <>
  175. <BlockIcon
  176. className='shrink-0'
  177. type={currentNodeVar.nodeType as BlockEnum}
  178. size='xs'
  179. toolIcon={toolIcon}
  180. />
  181. <div className='system-sm-regular shrink-0 text-text-secondary'>{currentNodeVar.title}</div>
  182. <div className='system-sm-regular shrink-0 text-text-quaternary'>/</div>
  183. </>
  184. )}
  185. <div title={currentNodeVar.var.name} className='system-sm-semibold truncate text-text-secondary'>{currentNodeVar.var.name}</div>
  186. <div className='system-xs-medium ml-1 shrink-0 space-x-2 text-text-tertiary'>
  187. <span>{`${currentNodeVar.var.value_type}${displaySchemaType}`}</span>
  188. {isTruncated && (
  189. <>
  190. <span>·</span>
  191. <span>{((fullContent?.size_bytes || 0) / 1024 / 1024).toFixed(1)}MB</span>
  192. </>
  193. )}
  194. </div>
  195. </>
  196. )}
  197. </div>
  198. <div className='flex shrink-0 items-center gap-1'>
  199. {currentNodeVar && (
  200. <>
  201. {canShowPromptGenerator && (
  202. <Tooltip popupContent={t('appDebug.generate.optimizePromptTooltip')}>
  203. <div
  204. className='cursor-pointer rounded-md p-1 hover:bg-state-accent-active'
  205. onClick={handleShowPromptGenerator}
  206. >
  207. <RiSparklingFill className='size-4 text-components-input-border-active-prompt-1' />
  208. </div>
  209. </Tooltip>
  210. )}
  211. {isTruncated && (
  212. <Tooltip popupContent={t('workflow.debug.variableInspect.exportToolTip')}>
  213. <ActionButton>
  214. <a
  215. href={fullContent?.download_url}
  216. target='_blank'
  217. >
  218. <RiFileDownloadFill className='size-4' />
  219. </a>
  220. </ActionButton>
  221. </Tooltip>
  222. )}
  223. {!isTruncated && currentNodeVar.var.edited && (
  224. <Badge>
  225. <span className='ml-[2.5px] mr-[4.5px] h-[3px] w-[3px] rounded bg-text-accent-secondary'></span>
  226. <span className='system-2xs-semibold-uupercase'>{t('workflow.debug.variableInspect.edited')}</span>
  227. </Badge>
  228. )}
  229. {!isTruncated && currentNodeVar.var.edited && currentNodeVar.var.type !== VarInInspectType.conversation && (
  230. <Tooltip popupContent={t('workflow.debug.variableInspect.reset')}>
  231. <ActionButton onClick={resetValue}>
  232. <RiArrowGoBackLine className='h-4 w-4' />
  233. </ActionButton>
  234. </Tooltip>
  235. )}
  236. {!isTruncated && currentNodeVar.var.edited && currentNodeVar.var.type === VarInInspectType.conversation && (
  237. <Tooltip popupContent={t('workflow.debug.variableInspect.resetConversationVar')}>
  238. <ActionButton onClick={handleClear}>
  239. <RiArrowGoBackLine className='h-4 w-4' />
  240. </ActionButton>
  241. </Tooltip>
  242. )}
  243. {currentNodeVar.var.value_type !== 'secret' && (
  244. <CopyFeedback content={getCopyContent()} />
  245. )}
  246. </>
  247. )}
  248. <ActionButton onClick={handleClose}>
  249. <RiCloseLine className='h-4 w-4' />
  250. </ActionButton>
  251. </div>
  252. </div>
  253. {/* content */}
  254. <div className='grow p-2'>
  255. {!currentNodeVar && <Empty />}
  256. {isValueFetching && (
  257. <div className='flex h-full items-center justify-center'>
  258. <Loading />
  259. </div>
  260. )}
  261. {currentNodeVar && !isValueFetching && (
  262. <ValueContent
  263. currentVar={currentNodeVar.var}
  264. handleValueChange={handleValueChange}
  265. isTruncated={!!isTruncated}
  266. />
  267. )}
  268. </div>
  269. {isShowPromptGenerator && (
  270. isCodeBlock
  271. ? <GetCodeGeneratorResModal
  272. isShow
  273. mode={AppType.chat}
  274. onClose={handleHidePromptGenerator}
  275. flowId={configsMap?.flowId || ''}
  276. nodeId={nodeId}
  277. currentCode={currentPrompt}
  278. codeLanguages={node?.data?.code_languages || CodeLanguage.python3}
  279. onFinished={handleUpdatePrompt}
  280. />
  281. : <GetAutomaticResModal
  282. mode={AppType.chat}
  283. isShow
  284. onClose={handleHidePromptGenerator}
  285. onFinished={handleUpdatePrompt}
  286. flowId={configsMap?.flowId || ''}
  287. nodeId={nodeId}
  288. currentPrompt={currentPrompt}
  289. />
  290. )}
  291. </div>
  292. )
  293. }
  294. export default Right