RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Feat: Fixed the issue where the prompt always displayed the initial value when switching between different generate operators #4764 (#4808)
balibabua357190eff57
diff --git a/web/src/components/prompt-editor/constant.ts b/web/src/components/prompt-editor/constant.tsnew file mode 100644index 0000000000..b6cf30ed9c--- /dev/null+++ b/web/src/components/prompt-editor/constant.ts@@ -0,0 +1 @@+export const ProgrammaticTag = 'programmatic';diff --git a/web/src/components/prompt-editor/index.tsx b/web/src/components/prompt-editor/index.tsxindex 695d100474..ba20e62f23 100644--- a/web/src/components/prompt-editor/index.tsx+++ b/web/src/components/prompt-editor/index.tsx@@ -1,13 +1,10 @@import { CodeHighlightNode, CodeNode } from '@lexical/code';-import { AutoFocusPlugin } from '@lexical/react/LexicalAutoFocusPlugin';import {InitialConfigType,LexicalComposer,} from '@lexical/react/LexicalComposer';import { ContentEditable } from '@lexical/react/LexicalContentEditable';import { LexicalErrorBoundary } from '@lexical/react/LexicalErrorBoundary';-import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';-import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';import { RichTextPlugin } from '@lexical/react/LexicalRichTextPlugin';import { HeadingNode, QuoteNode } from '@lexical/rich-text';import {@@ -18,9 +15,11 @@ import {LexicalNode,} from 'lexical';+import { useCallback } from 'react';import { useTranslation } from 'react-i18next';import theme from './theme';import { VariableNode } from './variable-node';+import { VariableOnChangePlugin } from './variable-on-change-plugin';import VariablePickerMenuPlugin from './variable-picker-plugin';// Catch any errors that occur during Lexical updates and log them@@ -52,16 +51,20 @@ export function PromptEditor({ value, onChange }: IProps) {nodes: Nodes,};- function onValueChange(editorState: EditorState) {- editorState?.read(() => {- const listNodes = $nodesOfType(VariableNode); // to be removed- // const allNodes = $dfs();- console.log('π ~ onChange ~ allNodes:', listNodes);+ const onValueChange = useCallback(+ (editorState: EditorState) => {+ editorState?.read(() => {+ const listNodes = $nodesOfType(VariableNode); // to be removed+ // const allNodes = $dfs();+ console.log('π ~ onChange ~ allNodes:', listNodes);- const text = $getRoot().getTextContent();- onChange?.(text);- });- }+ const text = $getRoot().getTextContent();++ onChange?.(text);+ });+ },+ [onChange],+ );return (<LexicalComposer initialConfig={initialConfig}>@@ -74,10 +77,8 @@ export function PromptEditor({ value, onChange }: IProps) {}ErrorBoundary={LexicalErrorBoundary}/>- <HistoryPlugin />- <AutoFocusPlugin /><VariablePickerMenuPlugin value={value}></VariablePickerMenuPlugin>- <OnChangePlugin onChange={onValueChange}></OnChangePlugin>+ <VariableOnChangePlugin onChange={onValueChange}></VariableOnChangePlugin></LexicalComposer>);}diff --git a/web/src/components/prompt-editor/variable-on-change-plugin.tsx b/web/src/components/prompt-editor/variable-on-change-plugin.tsxnew file mode 100644index 0000000000..86fa66db4f--- /dev/null+++ b/web/src/components/prompt-editor/variable-on-change-plugin.tsx@@ -0,0 +1,35 @@+import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';+import { EditorState, LexicalEditor } from 'lexical';+import { useEffect } from 'react';+import { ProgrammaticTag } from './constant';++interface IProps {+ onChange: (+ editorState: EditorState,+ editor?: LexicalEditor,+ tags?: Set<string>,+ ) => void;+}++export function VariableOnChangePlugin({ onChange }: IProps) {+ // Access the editor through the LexicalComposerContext+ const [editor] = useLexicalComposerContext();+ // Wrap our listener in useEffect to handle the teardown and avoid stale references.+ useEffect(() => {+ // most listeners return a teardown function that can be called to clean them up.+ return editor.registerUpdateListener(+ ({ editorState, tags, dirtyElements }) => {+ // Check if there is a "programmatic" tag+ const isProgrammaticUpdate = tags.has(ProgrammaticTag);++ // The onchange event is only triggered when the data is manually updated+ // Otherwise, the content will be displayed incorrectly.+ if (dirtyElements.size > 0 && !isProgrammaticUpdate) {+ onChange(editorState);+ }+ },+ );+ }, [editor, onChange]);++ return null;+}diff --git a/web/src/components/prompt-editor/variable-picker-plugin.tsx b/web/src/components/prompt-editor/variable-picker-plugin.tsxindex 4a30ad3bcd..b50e540ee3 100644--- a/web/src/components/prompt-editor/variable-picker-plugin.tsx+++ b/web/src/components/prompt-editor/variable-picker-plugin.tsx@@ -33,6 +33,7 @@ import { FlowFormContext } from '@/pages/flow/context';import { useBuildComponentIdSelectOptions } from '@/pages/flow/hooks/use-get-begin-query';import { $createVariableNode } from './variable-node';+import { ProgrammaticTag } from './constant';import './index.css';class VariableInnerOption extends MenuOption {label: string;@@ -215,9 +216,12 @@ export default function VariablePickerMenuPlugin({useEffect(() => {if (editor && value && isFirstRender.current) {isFirstRender.current = false;- editor.update(() => {- parseTextToVariableNodes(value);- });+ editor.update(+ () => {+ parseTextToVariableNodes(value);+ },+ { tag: ProgrammaticTag },+ );}}, [parseTextToVariableNodes, editor, value]);diff --git a/web/src/hooks/flow-hooks.ts b/web/src/hooks/flow-hooks.tsindex e733e9be15..5a0964dc08 100644--- a/web/src/hooks/flow-hooks.ts+++ b/web/src/hooks/flow-hooks.ts@@ -3,6 +3,7 @@ import { DSL, IFlow, IFlowTemplate } from '@/interfaces/database/flow';import { IDebugSingleRequestBody } from '@/interfaces/request/flow';import i18n from '@/locales/config';import { useGetSharedChatSearchParams } from '@/pages/chat/shared-hooks';+import { BeginId } from '@/pages/flow/constant';import flowService from '@/services/flow-service';import { buildMessageListWithUuid } from '@/utils/chat';import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';@@ -16,7 +17,7 @@ export const EmptyDsl = {graph: {nodes: [{- id: 'begin',+ id: BeginId,type: 'beginNode',position: {x: 50,diff --git a/web/src/pages/flow/form/generate-form/index.tsx b/web/src/pages/flow/form/generate-form/index.tsxindex 076ed25180..f21ce1035a 100644--- a/web/src/pages/flow/form/generate-form/index.tsx+++ b/web/src/pages/flow/form/generate-form/index.tsx@@ -35,6 +35,7 @@ const GenerateForm = ({ onValuesChange, form }: IOperatorForm) => {},]}>+ {/* <Input.TextArea rows={8}></Input.TextArea> */}<PromptEditor></PromptEditor></Form.Item><Form.Itemdiff --git a/web/src/pages/flow/hooks/use-save-graph.ts b/web/src/pages/flow/hooks/use-save-graph.tsindex eb102d1a2c..9567b56f29 100644--- a/web/src/pages/flow/hooks/use-save-graph.ts+++ b/web/src/pages/flow/hooks/use-save-graph.ts@@ -73,7 +73,7 @@ export const useWatchAgentChange = (chatDrawerVisible: boolean) => {useDebounceEffect(() => {- saveAgent();+ // saveAgent();},[nodes, edges],{