Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

conversation-variable-modal.tsx 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 'use client'
  2. import React, { useCallback } from 'react'
  3. import { useMount } from 'ahooks'
  4. import { useTranslation } from 'react-i18next'
  5. import { capitalize } from 'lodash-es'
  6. import copy from 'copy-to-clipboard'
  7. import { RiCloseLine } from '@remixicon/react'
  8. import Modal from '@/app/components/base/modal'
  9. import { BubbleX } from '@/app/components/base/icons/src/vender/line/others'
  10. import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
  11. import {
  12. Clipboard,
  13. ClipboardCheck,
  14. } from '@/app/components/base/icons/src/vender/line/files'
  15. import { useStore } from '@/app/components/workflow/store'
  16. import type {
  17. ConversationVariable,
  18. } from '@/app/components/workflow/types'
  19. import { ChatVarType } from '@/app/components/workflow/panel/chat-variable-panel/type'
  20. import { CodeLanguage } from '@/app/components/workflow/nodes/code/types'
  21. import useTimestamp from '@/hooks/use-timestamp'
  22. import { fetchCurrentValueOfConversationVariable } from '@/service/workflow'
  23. import cn from '@/utils/classnames'
  24. import { noop } from 'lodash-es'
  25. export type Props = {
  26. conversationID: string
  27. onHide: () => void
  28. }
  29. const ConversationVariableModal = ({
  30. conversationID,
  31. onHide,
  32. }: Props) => {
  33. const { t } = useTranslation()
  34. const { formatTime } = useTimestamp()
  35. const varList = useStore(s => s.conversationVariables) as ConversationVariable[]
  36. const appID = useStore(s => s.appId)
  37. const [currentVar, setCurrentVar] = React.useState<ConversationVariable>(varList[0])
  38. const [latestValueMap, setLatestValueMap] = React.useState<Record<string, string>>({})
  39. const [latestValueTimestampMap, setLatestValueTimestampMap] = React.useState<Record<string, number>>({})
  40. const getChatVarLatestValues = useCallback(async () => {
  41. if (conversationID && varList.length > 0) {
  42. const res = await fetchCurrentValueOfConversationVariable({
  43. url: `/apps/${appID}/conversation-variables`,
  44. params: { conversation_id: conversationID },
  45. })
  46. if (res.data.length > 0) {
  47. const valueMap = res.data.reduce((acc: any, cur) => {
  48. acc[cur.id] = cur.value
  49. return acc
  50. }, {})
  51. setLatestValueMap(valueMap)
  52. const timestampMap = res.data.reduce((acc: any, cur) => {
  53. acc[cur.id] = cur.updated_at
  54. return acc
  55. }, {})
  56. setLatestValueTimestampMap(timestampMap)
  57. }
  58. }
  59. }, [appID, conversationID, varList.length])
  60. const [isCopied, setIsCopied] = React.useState(false)
  61. const handleCopy = useCallback(() => {
  62. copy(currentVar.value)
  63. setIsCopied(true)
  64. setTimeout(() => {
  65. setIsCopied(false)
  66. }, 2000)
  67. }, [currentVar.value])
  68. useMount(() => {
  69. getChatVarLatestValues()
  70. })
  71. return (
  72. <Modal
  73. isShow
  74. onClose={noop}
  75. className={cn('h-[640px] w-[920px] max-w-[920px] p-0')}
  76. >
  77. <div className='absolute right-4 top-4 cursor-pointer p-2' onClick={onHide}>
  78. <RiCloseLine className='h-4 w-4 text-text-tertiary' />
  79. </div>
  80. <div className='flex h-full w-full'>
  81. {/* LEFT */}
  82. <div className='flex h-full w-[224px] shrink-0 flex-col border-r border-divider-burn bg-background-sidenav-bg'>
  83. <div className='system-xl-semibold shrink-0 pb-3 pl-5 pr-4 pt-5 text-text-primary'>{t('workflow.chatVariable.panelTitle')}</div>
  84. <div className='grow overflow-y-auto px-3 py-2'>
  85. {varList.map(chatVar => (
  86. <div key={chatVar.id} className={cn('radius-md group mb-0.5 flex cursor-pointer items-center p-2 hover:bg-state-base-hover', currentVar.id === chatVar.id && 'bg-state-base-hover')} onClick={() => setCurrentVar(chatVar)}>
  87. <BubbleX className={cn('mr-1 h-4 w-4 shrink-0 text-text-tertiary group-hover:text-util-colors-teal-teal-700', currentVar.id === chatVar.id && 'text-util-colors-teal-teal-700')} />
  88. <div title={chatVar.name} className={cn('system-sm-medium truncate text-text-tertiary group-hover:text-util-colors-teal-teal-700', currentVar.id === chatVar.id && 'text-util-colors-teal-teal-700')}>{chatVar.name}</div>
  89. </div>
  90. ))}
  91. </div>
  92. </div>
  93. {/* RIGHT */}
  94. <div className='flex h-full w-0 grow flex-col bg-components-panel-bg'>
  95. <div className='shrink-0 p-4 pb-2'>
  96. <div className='flex items-center gap-1 py-1'>
  97. <div className='system-xl-semibold text-text-primary'>{currentVar.name}</div>
  98. <div className='system-xs-medium text-text-tertiary'>{capitalize(currentVar.value_type)}</div>
  99. </div>
  100. </div>
  101. <div className='flex h-0 grow flex-col p-4 pt-2'>
  102. <div className='mb-2 flex shrink-0 items-center gap-2'>
  103. <div className='system-xs-medium-uppercase shrink-0 text-text-tertiary'>{t('workflow.chatVariable.storedContent').toLocaleUpperCase()}</div>
  104. <div className='h-[1px] grow' style={{
  105. background: 'linear-gradient(to right, rgba(16, 24, 40, 0.08) 0%, rgba(255, 255, 255) 100%)',
  106. }}></div>
  107. {latestValueTimestampMap[currentVar.id] && (
  108. <div className='system-xs-regular shrink-0 text-text-tertiary'>{t('workflow.chatVariable.updatedAt')}{formatTime(latestValueTimestampMap[currentVar.id], t('appLog.dateTimeFormat') as string)}</div>
  109. )}
  110. </div>
  111. <div className='grow overflow-y-auto'>
  112. {currentVar.value_type !== ChatVarType.Number && currentVar.value_type !== ChatVarType.String && (
  113. <div className='flex h-full flex-col rounded-lg bg-components-input-bg-normal px-2 pb-2'>
  114. <div className='flex h-7 shrink-0 items-center justify-between pl-3 pr-2 pt-1'>
  115. <div className='system-xs-semibold text-text-secondary'>JSON</div>
  116. <div className='flex items-center p-1'>
  117. {!isCopied
  118. ? (
  119. <Clipboard className='h-4 w-4 cursor-pointer text-text-tertiary' onClick={handleCopy} />
  120. )
  121. : (
  122. <ClipboardCheck className='h-4 w-4 text-text-tertiary' />
  123. )
  124. }
  125. </div>
  126. </div>
  127. <div className='grow pl-4'>
  128. <CodeEditor
  129. readOnly
  130. noWrapper
  131. isExpand
  132. language={CodeLanguage.json}
  133. value={latestValueMap[currentVar.id] || ''}
  134. isJSONStringifyBeauty
  135. />
  136. </div>
  137. </div>
  138. )}
  139. {(currentVar.value_type === ChatVarType.Number || currentVar.value_type === ChatVarType.String) && (
  140. <div className='system-md-regular h-full overflow-y-auto overflow-x-hidden rounded-lg bg-components-input-bg-normal px-4 py-3 text-components-input-text-filled'>{latestValueMap[currentVar.id] || ''}</div>
  141. )}
  142. </div>
  143. </div>
  144. </div>
  145. </div>
  146. </Modal>
  147. )
  148. }
  149. export default ConversationVariableModal