feat: enhance component picker and workflow variable block functionality

This commit is contained in:
zhsama 2026-01-08 18:17:09 +08:00
parent 872fd98eda
commit 8b0bc6937d
3 changed files with 28 additions and 8 deletions

View File

@ -21,7 +21,11 @@ import {
} from '@floating-ui/react' } from '@floating-ui/react'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { LexicalTypeaheadMenuPlugin } from '@lexical/react/LexicalTypeaheadMenuPlugin' import { LexicalTypeaheadMenuPlugin } from '@lexical/react/LexicalTypeaheadMenuPlugin'
import { KEY_ESCAPE_COMMAND } from 'lexical' import {
$getRoot,
$insertNodes,
KEY_ESCAPE_COMMAND,
} from 'lexical'
import { import {
Fragment, Fragment,
memo, memo,
@ -41,6 +45,7 @@ import { INSERT_ERROR_MESSAGE_BLOCK_COMMAND } from '../error-message-block'
import { INSERT_LAST_RUN_BLOCK_COMMAND } from '../last-run-block' import { INSERT_LAST_RUN_BLOCK_COMMAND } from '../last-run-block'
import { INSERT_VARIABLE_VALUE_BLOCK_COMMAND } from '../variable-block' import { INSERT_VARIABLE_VALUE_BLOCK_COMMAND } from '../variable-block'
import { INSERT_WORKFLOW_VARIABLE_BLOCK_COMMAND } from '../workflow-variable-block' import { INSERT_WORKFLOW_VARIABLE_BLOCK_COMMAND } from '../workflow-variable-block'
import { $createWorkflowVariableBlockNode } from '../workflow-variable-block/node'
import { useOptions } from './hooks' import { useOptions } from './hooks'
type ComponentPickerProps = { type ComponentPickerProps = {
@ -161,6 +166,16 @@ const ComponentPicker = ({
const needRemove = $splitNodeContainingQuery(checkForTriggerMatch(triggerString, editor)!) const needRemove = $splitNodeContainingQuery(checkForTriggerMatch(triggerString, editor)!)
if (needRemove) if (needRemove)
needRemove.remove() needRemove.remove()
const root = $getRoot()
const firstChild = root.getFirstChild()
if (firstChild) {
const selection = firstChild.selectStart()
if (selection) {
const workflowVariableBlockNode = $createWorkflowVariableBlockNode([agent.id, 'text'], {}, undefined)
$insertNodes([workflowVariableBlockNode])
}
}
}) })
agentBlock?.onSelect?.(agent) agentBlock?.onSelect?.(agent)
handleClose() handleClose()

View File

@ -21,6 +21,7 @@ import {
VariableLabelInEditor, VariableLabelInEditor,
} from '@/app/components/workflow/nodes/_base/components/variable/variable-label' } from '@/app/components/workflow/nodes/_base/components/variable/variable-label'
import { Type } from '@/app/components/workflow/nodes/llm/types' import { Type } from '@/app/components/workflow/nodes/llm/types'
import { BlockEnum } from '@/app/components/workflow/types'
import { isExceptionVariable } from '@/app/components/workflow/utils' import { isExceptionVariable } from '@/app/components/workflow/utils'
import { useSelectOrDelete } from '../../hooks' import { useSelectOrDelete } from '../../hooks'
import { import {
@ -66,6 +67,7 @@ const WorkflowVariableBlockComponent = ({
)() )()
const [localWorkflowNodesMap, setLocalWorkflowNodesMap] = useState<WorkflowNodesMap>(workflowNodesMap) const [localWorkflowNodesMap, setLocalWorkflowNodesMap] = useState<WorkflowNodesMap>(workflowNodesMap)
const node = localWorkflowNodesMap![variables[isRagVar ? 1 : 0]] const node = localWorkflowNodesMap![variables[isRagVar ? 1 : 0]]
const isAgentVariable = node?.type === BlockEnum.Agent
const isException = isExceptionVariable(varName, node?.type) const isException = isExceptionVariable(varName, node?.type)
const variableValid = useMemo(() => { const variableValid = useMemo(() => {
@ -134,6 +136,9 @@ const WorkflowVariableBlockComponent = ({
}) })
}, [node, reactflow, store]) }, [node, reactflow, store])
if (isAgentVariable)
return <span className="hidden" ref={ref} />
const Item = ( const Item = (
<VariableLabelInEditor <VariableLabelInEditor
nodeType={node?.type} nodeType={node?.type}

View File

@ -6,6 +6,7 @@ import type {
import { import {
memo, memo,
useCallback, useCallback,
useEffect,
useMemo, useMemo,
useState, useState,
} from 'react' } from 'react'
@ -78,6 +79,11 @@ const MixedVariableTextInput = ({
const [selectedAgent, setSelectedAgent] = useState<{ id: string, title: string } | null>(null) const [selectedAgent, setSelectedAgent] = useState<{ id: string, title: string } | null>(null)
useEffect(() => {
if (!detectedAgentFromValue && selectedAgent)
setSelectedAgent(null)
}, [detectedAgentFromValue, selectedAgent])
const agentNodes = useMemo(() => { const agentNodes = useMemo(() => {
return availableNodes return availableNodes
.filter(node => node.data.type === BlockEnum.Agent) .filter(node => node.data.type === BlockEnum.Agent)
@ -89,13 +95,7 @@ const MixedVariableTextInput = ({
const handleAgentSelect = useCallback((agent: { id: string, title: string }) => { const handleAgentSelect = useCallback((agent: { id: string, title: string }) => {
setSelectedAgent(agent) setSelectedAgent(agent)
if (onChange) { }, [])
const agentVar = `{{#${agent.id}.text#}}`
const newValue = value ? `${agentVar}${value}` : agentVar
onChange(newValue)
setControlPromptEditorRerenderKey(Date.now())
}
}, [onChange, value, setControlPromptEditorRerenderKey])
const handleAgentRemove = useCallback(() => { const handleAgentRemove = useCallback(() => {
const agentNodeId = detectedAgentFromValue?.nodeId || selectedAgent?.id const agentNodeId = detectedAgentFromValue?.nodeId || selectedAgent?.id