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 14KB

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