Skip to content

Commit

Permalink
fix: auto prompt (#2768)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxhlyh authored Mar 9, 2024
1 parent 88145ef commit 9beefd7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ArrowNarrowRight } from '@/app/components/base/icons/src/vender/line/ar
import { useEventEmitterContextContext } from '@/context/event-emitter'
import { ADD_EXTERNAL_DATA_TOOL } from '@/app/components/app/configuration/config-var'
import { INSERT_VARIABLE_VALUE_BLOCK_COMMAND } from '@/app/components/base/prompt-editor/plugins/variable-block'
import { PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER } from '@/app/components/base/prompt-editor/plugins/update-block'

export type ISimplePromptInput = {
mode: AppType
Expand Down Expand Up @@ -125,6 +126,10 @@ const Prompt: FC<ISimplePromptInput> = ({
if (mode === AppType.chat)
setIntroduction(res.opening_statement)
showAutomaticFalse()
eventEmitter?.emit({
type: PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER,
payload: res.prompt,
} as any)
}
const minHeight = 228
const [editorHeight, setEditorHeight] = useState(minHeight)
Expand Down
2 changes: 2 additions & 0 deletions web/app/components/base/prompt-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import VariableValueBlock from './plugins/variable-value-block'
import { VariableValueBlockNode } from './plugins/variable-value-block/node'
import { CustomTextNode } from './plugins/custom-text/node'
import OnBlurBlock from './plugins/on-blur-block'
import UpdateBlock from './plugins/update-block'
import { textToEditorState } from './utils'
import type { Dataset } from './plugins/context-block'
import type { RoleName } from './plugins/history-block'
Expand Down Expand Up @@ -222,6 +223,7 @@ const PromptEditor: FC<PromptEditorProps> = ({
<VariableValueBlock />
<OnChangePlugin onChange={handleEditorChange} />
<OnBlurBlock onBlur={onBlur} />
<UpdateBlock />
{/* <TreeView /> */}
</div>
</LexicalComposer>
Expand Down
21 changes: 21 additions & 0 deletions web/app/components/base/prompt-editor/plugins/update-block.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { textToEditorState } from '../utils'
import { useEventEmitterContextContext } from '@/context/event-emitter'

export const PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER = 'PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER'

const UpdateBlock = () => {
const { eventEmitter } = useEventEmitterContextContext()
const [editor] = useLexicalComposerContext()

eventEmitter?.useSubscription((v: any) => {
if (v.type === PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER) {
const editorState = editor.parseEditorState(textToEditorState(v.payload))
editor.setEditorState(editorState)
}
})

return null
}

export default UpdateBlock

0 comments on commit 9beefd7

Please sign in to comment.