| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import { useEffect } from 'react' | 
					
						
							|  |  |  | import type { | 
					
						
							|  |  |  |   EditorState, | 
					
						
							|  |  |  | } from 'lexical' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   $getRoot, | 
					
						
							|  |  |  |   TextNode, | 
					
						
							|  |  |  | } from 'lexical' | 
					
						
							| 
									
										
										
										
											2024-03-29 21:01:04 +08:00
										 |  |  | import { CodeNode } from '@lexical/code' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | import { LexicalComposer } from '@lexical/react/LexicalComposer' | 
					
						
							|  |  |  | import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin' | 
					
						
							|  |  |  | import { ContentEditable } from '@lexical/react/LexicalContentEditable' | 
					
						
							|  |  |  | import LexicalErrorBoundary from '@lexical/react/LexicalErrorBoundary' | 
					
						
							|  |  |  | import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin' | 
					
						
							| 
									
										
										
										
											2024-04-09 18:18:53 +08:00
										 |  |  | import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | // import TreeView from './plugins/tree-view'
 | 
					
						
							|  |  |  | import Placeholder from './plugins/placeholder' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import ComponentPickerBlock from './plugins/component-picker-block' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   ContextBlock, | 
					
						
							|  |  |  |   ContextBlockNode, | 
					
						
							|  |  |  |   ContextBlockReplacementBlock, | 
					
						
							|  |  |  | } from './plugins/context-block' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   QueryBlock, | 
					
						
							|  |  |  |   QueryBlockNode, | 
					
						
							|  |  |  |   QueryBlockReplacementBlock, | 
					
						
							|  |  |  | } from './plugins/query-block' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   HistoryBlock, | 
					
						
							|  |  |  |   HistoryBlockNode, | 
					
						
							|  |  |  |   HistoryBlockReplacementBlock, | 
					
						
							|  |  |  | } from './plugins/history-block' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   WorkflowVariableBlock, | 
					
						
							|  |  |  |   WorkflowVariableBlockNode, | 
					
						
							|  |  |  |   WorkflowVariableBlockReplacementBlock, | 
					
						
							|  |  |  | } from './plugins/workflow-variable-block' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | import VariableBlock from './plugins/variable-block' | 
					
						
							|  |  |  | import VariableValueBlock from './plugins/variable-value-block' | 
					
						
							|  |  |  | import { VariableValueBlockNode } from './plugins/variable-value-block/node' | 
					
						
							|  |  |  | import { CustomTextNode } from './plugins/custom-text/node' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import OnBlurBlock from './plugins/on-blur-or-focus-block' | 
					
						
							| 
									
										
										
										
											2024-03-09 18:36:58 +08:00
										 |  |  | import UpdateBlock from './plugins/update-block' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | import { textToEditorState } from './utils' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { | 
					
						
							|  |  |  |   ContextBlockType, | 
					
						
							|  |  |  |   ExternalToolBlockType, | 
					
						
							|  |  |  |   HistoryBlockType, | 
					
						
							|  |  |  |   QueryBlockType, | 
					
						
							|  |  |  |   VariableBlockType, | 
					
						
							|  |  |  |   WorkflowVariableBlockType, | 
					
						
							|  |  |  | } from './types' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   UPDATE_DATASETS_EVENT_EMITTER, | 
					
						
							|  |  |  |   UPDATE_HISTORY_EVENT_EMITTER, | 
					
						
							|  |  |  | } from './constants' | 
					
						
							|  |  |  | import { useEventEmitterContextContext } from '@/context/event-emitter' | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  | import cn from '@/utils/classnames' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export type PromptEditorProps = { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   instanceId?: string | 
					
						
							|  |  |  |   compact?: boolean | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   className?: string | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   placeholder?: string | 
					
						
							|  |  |  |   placeholderClassName?: string | 
					
						
							|  |  |  |   style?: React.CSSProperties | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   value?: string | 
					
						
							|  |  |  |   editable?: boolean | 
					
						
							|  |  |  |   onChange?: (text: string) => void | 
					
						
							|  |  |  |   onBlur?: () => void | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   onFocus?: () => void | 
					
						
							|  |  |  |   contextBlock?: ContextBlockType | 
					
						
							|  |  |  |   queryBlock?: QueryBlockType | 
					
						
							|  |  |  |   historyBlock?: HistoryBlockType | 
					
						
							|  |  |  |   variableBlock?: VariableBlockType | 
					
						
							|  |  |  |   externalToolBlock?: ExternalToolBlockType | 
					
						
							|  |  |  |   workflowVariableBlock?: WorkflowVariableBlockType | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   isSupportFileVar?: boolean | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const PromptEditor: FC<PromptEditorProps> = ({ | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   instanceId, | 
					
						
							|  |  |  |   compact, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   className, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   placeholder, | 
					
						
							|  |  |  |   placeholderClassName, | 
					
						
							|  |  |  |   style, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   value, | 
					
						
							|  |  |  |   editable = true, | 
					
						
							|  |  |  |   onChange, | 
					
						
							|  |  |  |   onBlur, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   onFocus, | 
					
						
							|  |  |  |   contextBlock, | 
					
						
							|  |  |  |   queryBlock, | 
					
						
							|  |  |  |   historyBlock, | 
					
						
							|  |  |  |   variableBlock, | 
					
						
							|  |  |  |   externalToolBlock, | 
					
						
							|  |  |  |   workflowVariableBlock, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   isSupportFileVar, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | }) => { | 
					
						
							|  |  |  |   const { eventEmitter } = useEventEmitterContextContext() | 
					
						
							|  |  |  |   const initialConfig = { | 
					
						
							|  |  |  |     namespace: 'prompt-editor', | 
					
						
							|  |  |  |     nodes: [ | 
					
						
							| 
									
										
										
										
											2024-03-29 21:01:04 +08:00
										 |  |  |       CodeNode, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |       CustomTextNode, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         replace: TextNode, | 
					
						
							|  |  |  |         with: (node: TextNode) => new CustomTextNode(node.__text), | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       ContextBlockNode, | 
					
						
							|  |  |  |       HistoryBlockNode, | 
					
						
							|  |  |  |       QueryBlockNode, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       WorkflowVariableBlockNode, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |       VariableValueBlockNode, | 
					
						
							|  |  |  |     ], | 
					
						
							| 
									
										
										
										
											2024-04-30 14:25:21 +08:00
										 |  |  |     editorState: textToEditorState(value || ''), | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |     onError: (error: Error) => { | 
					
						
							|  |  |  |       throw error | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleEditorChange = (editorState: EditorState) => { | 
					
						
							| 
									
										
										
										
											2024-07-16 11:23:26 +08:00
										 |  |  |     const text = editorState.read(() => { | 
					
						
							|  |  |  |       return $getRoot().getChildren().map(p => p.getTextContent()).join('\n') | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |     if (onChange) | 
					
						
							| 
									
										
										
										
											2024-07-16 11:23:26 +08:00
										 |  |  |       onChange(text) | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     eventEmitter?.emit({ | 
					
						
							|  |  |  |       type: UPDATE_DATASETS_EVENT_EMITTER, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       payload: contextBlock?.datasets, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |     } as any) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   }, [eventEmitter, contextBlock?.datasets]) | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     eventEmitter?.emit({ | 
					
						
							|  |  |  |       type: UPDATE_HISTORY_EVENT_EMITTER, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       payload: historyBlock?.history, | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |     } as any) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   }, [eventEmitter, historyBlock?.history]) | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <LexicalComposer initialConfig={{ ...initialConfig, editable }}> | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |       <div className='relative min-h-5'> | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |         <RichTextPlugin | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |           contentEditable={<ContentEditable className={`${className} outline-none ${compact ? 'text-[13px] leading-5' : 'text-sm leading-6'} text-text-secondary`} style={style || {}} />} | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |           placeholder={<Placeholder value={placeholder} className={cn('truncate', placeholderClassName)} compact={compact} />} | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |           ErrorBoundary={LexicalErrorBoundary} | 
					
						
							|  |  |  |         /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         <ComponentPickerBlock | 
					
						
							|  |  |  |           triggerString='/' | 
					
						
							|  |  |  |           contextBlock={contextBlock} | 
					
						
							|  |  |  |           historyBlock={historyBlock} | 
					
						
							|  |  |  |           queryBlock={queryBlock} | 
					
						
							|  |  |  |           variableBlock={variableBlock} | 
					
						
							|  |  |  |           externalToolBlock={externalToolBlock} | 
					
						
							|  |  |  |           workflowVariableBlock={workflowVariableBlock} | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |           isSupportFileVar={isSupportFileVar} | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |         /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         <ComponentPickerBlock | 
					
						
							|  |  |  |           triggerString='{' | 
					
						
							|  |  |  |           contextBlock={contextBlock} | 
					
						
							|  |  |  |           historyBlock={historyBlock} | 
					
						
							|  |  |  |           queryBlock={queryBlock} | 
					
						
							|  |  |  |           variableBlock={variableBlock} | 
					
						
							|  |  |  |           externalToolBlock={externalToolBlock} | 
					
						
							|  |  |  |           workflowVariableBlock={workflowVariableBlock} | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |           isSupportFileVar={isSupportFileVar} | 
					
						
							| 
									
										
										
										
											2023-11-06 19:36:32 +08:00
										 |  |  |         /> | 
					
						
							| 
									
										
										
										
											2023-10-19 17:52:14 +08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           contextBlock?.show && ( | 
					
						
							|  |  |  |             <> | 
					
						
							|  |  |  |               <ContextBlock {...contextBlock} /> | 
					
						
							|  |  |  |               <ContextBlockReplacementBlock {...contextBlock} /> | 
					
						
							|  |  |  |             </> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           queryBlock?.show && ( | 
					
						
							| 
									
										
										
										
											2023-10-19 17:52:14 +08:00
										 |  |  |             <> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               <QueryBlock {...queryBlock} /> | 
					
						
							|  |  |  |               <QueryBlockReplacementBlock /> | 
					
						
							| 
									
										
										
										
											2023-10-19 17:52:14 +08:00
										 |  |  |             </> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           historyBlock?.show && ( | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |             <> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               <HistoryBlock {...historyBlock} /> | 
					
						
							|  |  |  |               <HistoryBlockReplacementBlock {...historyBlock} /> | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |             </> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |           (variableBlock?.show || externalToolBlock?.show) && ( | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |             <> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |               <VariableBlock /> | 
					
						
							|  |  |  |               <VariableValueBlock /> | 
					
						
							|  |  |  |             </> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           workflowVariableBlock?.show && ( | 
					
						
							|  |  |  |             <> | 
					
						
							|  |  |  |               <WorkflowVariableBlock {...workflowVariableBlock} /> | 
					
						
							|  |  |  |               <WorkflowVariableBlockReplacementBlock {...workflowVariableBlock} /> | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |             </> | 
					
						
							|  |  |  |           ) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         <OnChangePlugin onChange={handleEditorChange} /> | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         <OnBlurBlock onBlur={onBlur} onFocus={onFocus} /> | 
					
						
							|  |  |  |         <UpdateBlock instanceId={instanceId} /> | 
					
						
							| 
									
										
										
										
											2024-04-09 18:18:53 +08:00
										 |  |  |         <HistoryPlugin /> | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |         {/* <TreeView /> */} | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |     </LexicalComposer> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default PromptEditor |