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.

value-content.tsx 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import { useEffect, useMemo, useRef, useState } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { useDebounceFn } from 'ahooks'
  4. import { RiBracesLine, RiEyeLine } from '@remixicon/react'
  5. import Textarea from '@/app/components/base/textarea'
  6. import { Markdown } from '@/app/components/base/markdown'
  7. import SchemaEditor from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/schema-editor'
  8. import { FileUploaderInAttachmentWrapper } from '@/app/components/base/file-uploader'
  9. import ErrorMessage from '@/app/components/workflow/nodes/llm/components/json-schema-config-modal/error-message'
  10. import {
  11. checkJsonSchemaDepth,
  12. getValidationErrorMessage,
  13. validateSchemaAgainstDraft7,
  14. } from '@/app/components/workflow/nodes/llm/utils'
  15. import {
  16. validateJSONSchema,
  17. } from '@/app/components/workflow/variable-inspect/utils'
  18. import { getProcessedFiles, getProcessedFilesFromResponse } from '@/app/components/base/file-uploader/utils'
  19. import { SegmentedControl } from '@/app/components/base/segmented-control'
  20. import { JSON_SCHEMA_MAX_DEPTH } from '@/config'
  21. import { TransferMethod } from '@/types/app'
  22. import { FILE_EXTS } from '@/app/components/base/prompt-editor/constants'
  23. import { SupportUploadFileTypes } from '@/app/components/workflow/types'
  24. import type { VarInInspect } from '@/types/workflow'
  25. import { VarInInspectType } from '@/types/workflow'
  26. import cn from '@/utils/classnames'
  27. import { useStore } from '@/app/components/workflow/store'
  28. import { ChunkCardList, type ChunkInfo } from '@/app/components/rag-pipeline/components/chunk-card-list'
  29. import { PreviewMode } from '../../base/features/types'
  30. enum ViewMode {
  31. Code = 'code',
  32. Preview = 'preview',
  33. }
  34. enum ContentType {
  35. Markdown = 'markdown',
  36. Chunks = 'chunks',
  37. }
  38. type DisplayContentProps = {
  39. type: ContentType
  40. mdString?: string
  41. jsonString?: string
  42. readonly: boolean
  43. handleTextChange?: (value: string) => void
  44. handleEditorChange?: (value: string) => void
  45. }
  46. const DisplayContent = (props: DisplayContentProps) => {
  47. const { type, mdString, jsonString, readonly, handleTextChange, handleEditorChange } = props
  48. const [viewMode, setViewMode] = useState<ViewMode>(ViewMode.Code)
  49. const [isFocused, setIsFocused] = useState(false)
  50. const { t } = useTranslation()
  51. return (
  52. <div className={cn('flex h-full flex-col rounded-[10px] bg-components-input-bg-normal', isFocused && 'bg-components-input-bg-active outline outline-1 outline-components-input-border-active')}>
  53. <div className='flex shrink-0 items-center justify-between p-1'>
  54. <div className='system-xs-semibold-uppercase flex items-center px-2 py-0.5 text-text-secondary'>
  55. {type.toUpperCase()}
  56. </div>
  57. <SegmentedControl
  58. options={[
  59. { value: ViewMode.Code, text: t('workflow.nodes.templateTransform.code'), Icon: RiBracesLine },
  60. { value: ViewMode.Preview, text: t('workflow.common.preview'), Icon: RiEyeLine },
  61. ]}
  62. value={viewMode}
  63. onChange={setViewMode}
  64. size='small'
  65. padding='with'
  66. activeClassName='!text-text-accent-light-mode-only'
  67. btnClassName='!pl-1.5 !pr-0.5 gap-[3px]'
  68. />
  69. </div>
  70. <div className='flex flex-1 overflow-auto rounded-b-[10px] pb-1 pl-3 pr-1'>
  71. {viewMode === ViewMode.Code && (
  72. type === ContentType.Markdown
  73. ? <Textarea
  74. readOnly={readonly}
  75. disabled={readonly}
  76. className='h-full border-none bg-transparent p-0 text-text-secondary hover:bg-transparent focus:bg-transparent focus:shadow-none'
  77. value={mdString as any}
  78. onChange={e => handleTextChange?.(e.target.value)}
  79. onFocus={() => setIsFocused(true)}
  80. onBlur={() => setIsFocused(false)}
  81. />
  82. : <SchemaEditor
  83. readonly={readonly}
  84. className='overflow-y-auto bg-transparent'
  85. hideTopMenu
  86. schema={jsonString!}
  87. onUpdate={handleEditorChange!}
  88. onFocus={() => setIsFocused(true)}
  89. onBlur={() => setIsFocused(false)}
  90. />
  91. )}
  92. {viewMode === ViewMode.Preview && (
  93. type === ContentType.Markdown
  94. ? <Markdown className='grow overflow-auto rounded-lg !bg-white px-4 py-3' content={(mdString ?? '') as string} />
  95. : <ChunkCardList chunkInfo={JSON.parse(jsonString!) as ChunkInfo} />
  96. )}
  97. </div>
  98. </div>
  99. )
  100. }
  101. type Props = {
  102. currentVar: VarInInspect
  103. handleValueChange: (varId: string, value: any) => void
  104. }
  105. const ValueContent = ({
  106. currentVar,
  107. handleValueChange,
  108. }: Props) => {
  109. const contentContainerRef = useRef<HTMLDivElement>(null)
  110. const errorMessageRef = useRef<HTMLDivElement>(null)
  111. const [editorHeight, setEditorHeight] = useState(0)
  112. const showTextEditor = currentVar.value_type === 'secret' || currentVar.value_type === 'string' || currentVar.value_type === 'number'
  113. const isSysFiles = currentVar.type === VarInInspectType.system && currentVar.name === 'files'
  114. const showJSONEditor = !isSysFiles && (currentVar.value_type === 'object' || currentVar.value_type === 'array[string]' || currentVar.value_type === 'array[number]' || currentVar.value_type === 'array[object]' || currentVar.value_type === 'array[any]')
  115. const showFileEditor = isSysFiles || currentVar.value_type === 'file' || currentVar.value_type === 'array[file]'
  116. const textEditorDisabled = currentVar.type === VarInInspectType.environment || (currentVar.type === VarInInspectType.system && currentVar.name !== 'query' && currentVar.name !== 'files')
  117. const JSONEditorDisabled = currentVar.value_type === 'array[any]'
  118. const fileUploadConfig = useStore(s => s.fileUploadConfig)
  119. const hasChunks = useMemo(() => {
  120. return currentVar.value_type === 'object'
  121. && currentVar.value
  122. && typeof currentVar.value === 'object'
  123. && ['parent_child_chunks', 'general_chunks', 'qa_chunks'].some(key => key in currentVar.value)
  124. }, [currentVar.value_type, currentVar.value])
  125. const formatFileValue = (value: VarInInspect) => {
  126. if (value.value_type === 'file')
  127. return value.value ? getProcessedFilesFromResponse([value.value]) : []
  128. if (value.value_type === 'array[file]' || (value.type === VarInInspectType.system && currentVar.name === 'files'))
  129. return value.value && value.value.length > 0 ? getProcessedFilesFromResponse(value.value) : []
  130. return []
  131. }
  132. const [value, setValue] = useState<any>()
  133. const [json, setJson] = useState('')
  134. const [parseError, setParseError] = useState<Error | null>(null)
  135. const [validationError, setValidationError] = useState<string>('')
  136. const [fileValue, setFileValue] = useState<any>(formatFileValue(currentVar))
  137. const { run: debounceValueChange } = useDebounceFn(handleValueChange, { wait: 500 })
  138. // update default value when id changed
  139. useEffect(() => {
  140. if (showTextEditor) {
  141. if (currentVar.value_type === 'number')
  142. return setValue(JSON.stringify(currentVar.value))
  143. if (!currentVar.value)
  144. return setValue('')
  145. setValue(currentVar.value)
  146. }
  147. if (showJSONEditor)
  148. setJson(currentVar.value ? JSON.stringify(currentVar.value, null, 2) : '')
  149. if (showFileEditor)
  150. setFileValue(formatFileValue(currentVar))
  151. }, [currentVar.id, currentVar.value])
  152. const handleTextChange = (value: string) => {
  153. if (currentVar.value_type === 'string')
  154. setValue(value)
  155. if (currentVar.value_type === 'number') {
  156. if (/^-?\d+(\.)?(\d+)?$/.test(value))
  157. setValue(Number.parseFloat(value))
  158. }
  159. const newValue = currentVar.value_type === 'number' ? Number.parseFloat(value) : value
  160. debounceValueChange(currentVar.id, newValue)
  161. }
  162. const jsonValueValidate = (value: string, type: string) => {
  163. try {
  164. const newJSONSchema = JSON.parse(value)
  165. setParseError(null)
  166. const result = validateJSONSchema(newJSONSchema, type)
  167. if (!result.success) {
  168. setValidationError(result.error.message)
  169. return false
  170. }
  171. if (type === 'object' || type === 'array[object]') {
  172. const schemaDepth = checkJsonSchemaDepth(newJSONSchema)
  173. if (schemaDepth > JSON_SCHEMA_MAX_DEPTH) {
  174. setValidationError(`Schema exceeds maximum depth of ${JSON_SCHEMA_MAX_DEPTH}.`)
  175. return false
  176. }
  177. const validationErrors = validateSchemaAgainstDraft7(newJSONSchema)
  178. if (validationErrors.length > 0) {
  179. setValidationError(getValidationErrorMessage(validationErrors))
  180. return false
  181. }
  182. }
  183. setValidationError('')
  184. return true
  185. }
  186. catch (error) {
  187. setValidationError('')
  188. if (error instanceof Error) {
  189. setParseError(error)
  190. return false
  191. }
  192. else {
  193. setParseError(new Error('Invalid JSON'))
  194. return false
  195. }
  196. }
  197. }
  198. const handleEditorChange = (value: string) => {
  199. setJson(value)
  200. if (jsonValueValidate(value, currentVar.value_type)) {
  201. const parsed = JSON.parse(value)
  202. debounceValueChange(currentVar.id, parsed)
  203. }
  204. }
  205. const fileValueValidate = (fileList: any[]) => fileList.every(file => file.upload_file_id)
  206. const handleFileChange = (value: any[]) => {
  207. setFileValue(value)
  208. // check every file upload progress
  209. // invoke update api after every file uploaded
  210. if (!fileValueValidate(value))
  211. return
  212. if (currentVar.value_type === 'file')
  213. debounceValueChange(currentVar.id, value[0])
  214. if (currentVar.value_type === 'array[file]' || isSysFiles)
  215. debounceValueChange(currentVar.id, value)
  216. }
  217. // get editor height
  218. useEffect(() => {
  219. if (contentContainerRef.current && errorMessageRef.current) {
  220. const errorMessageObserver = new ResizeObserver((entries) => {
  221. for (const entry of entries) {
  222. const { inlineSize } = entry.borderBoxSize[0]
  223. const height = (contentContainerRef.current as any).clientHeight - inlineSize
  224. setEditorHeight(height)
  225. }
  226. })
  227. errorMessageObserver.observe(errorMessageRef.current)
  228. return () => {
  229. errorMessageObserver.disconnect()
  230. }
  231. }
  232. }, [setEditorHeight])
  233. return (
  234. <div
  235. ref={contentContainerRef}
  236. className='flex h-full flex-col'
  237. >
  238. <div className={cn('grow')} style={{ height: `${editorHeight}px` }}>
  239. {showTextEditor && (
  240. currentVar.value_type === 'string' ? (
  241. <DisplayContent
  242. type={ContentType.Markdown}
  243. mdString={value as any}
  244. readonly={textEditorDisabled}
  245. handleTextChange={handleTextChange}
  246. />
  247. ) : <Textarea
  248. readOnly={textEditorDisabled}
  249. disabled={textEditorDisabled}
  250. className='h-full'
  251. value={value as any}
  252. onChange={e => handleTextChange(e.target.value)}
  253. />
  254. )}
  255. {showJSONEditor && (
  256. hasChunks
  257. ? <DisplayContent
  258. type={ContentType.Chunks}
  259. jsonString={json ?? '{}'}
  260. readonly={JSONEditorDisabled}
  261. handleEditorChange={handleEditorChange}
  262. />
  263. : <SchemaEditor
  264. readonly={JSONEditorDisabled}
  265. className='overflow-y-auto'
  266. hideTopMenu
  267. schema={json}
  268. onUpdate={handleEditorChange}
  269. />
  270. )}
  271. {showFileEditor && (
  272. <div className='max-w-[460px]'>
  273. <FileUploaderInAttachmentWrapper
  274. value={fileValue}
  275. onChange={files => handleFileChange(getProcessedFiles(files))}
  276. fileConfig={{
  277. allowed_file_types: [
  278. SupportUploadFileTypes.image,
  279. SupportUploadFileTypes.document,
  280. SupportUploadFileTypes.audio,
  281. SupportUploadFileTypes.video,
  282. ],
  283. allowed_file_extensions: [
  284. ...FILE_EXTS[SupportUploadFileTypes.image],
  285. ...FILE_EXTS[SupportUploadFileTypes.document],
  286. ...FILE_EXTS[SupportUploadFileTypes.audio],
  287. ...FILE_EXTS[SupportUploadFileTypes.video],
  288. ],
  289. allowed_file_upload_methods: [TransferMethod.local_file, TransferMethod.remote_url],
  290. number_limits: currentVar.value_type === 'file' ? 1 : fileUploadConfig?.workflow_file_upload_limit || 5,
  291. fileUploadConfig,
  292. preview_config: {
  293. mode: PreviewMode.NewPage,
  294. file_type_list: ['application/pdf'],
  295. },
  296. }}
  297. isDisabled={textEditorDisabled}
  298. />
  299. </div>
  300. )}
  301. </div>
  302. <div ref={errorMessageRef} className='shrink-0'>
  303. {parseError && <ErrorMessage className='mt-1' message={parseError.message} />}
  304. {validationError && <ErrorMessage className='mt-1' message={validationError} />}
  305. </div>
  306. </div>
  307. )
  308. }
  309. export default ValueContent