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.

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