feat: pass the var into it

This commit is contained in:
Joel 2025-03-14 18:06:58 +08:00
parent 183edf0fd5
commit 7ea0a972d5
7 changed files with 80 additions and 11 deletions

View File

@ -29,18 +29,24 @@ import { isConversationVar, isENV, isSystemVar } from '@/app/components/workflow
import Tooltip from '@/app/components/base/tooltip' import Tooltip from '@/app/components/base/tooltip'
import { isExceptionVariable } from '@/app/components/workflow/utils' import { isExceptionVariable } from '@/app/components/workflow/utils'
import VarFullPathPanel from '@/app/components/workflow/nodes/_base/components/variable/var-full-path-panel' import VarFullPathPanel from '@/app/components/workflow/nodes/_base/components/variable/var-full-path-panel'
import { Type } from '@/app/components/workflow/nodes/llm/types' import type { Type } from '@/app/components/workflow/nodes/llm/types'
import type { ValueSelector } from '@/app/components/workflow/types'
type WorkflowVariableBlockComponentProps = { type WorkflowVariableBlockComponentProps = {
nodeKey: string nodeKey: string
variables: string[] variables: string[]
workflowNodesMap: WorkflowNodesMap workflowNodesMap: WorkflowNodesMap
getVarType: (payload: {
nodeId: string,
valueSelector: ValueSelector,
}) => Type
} }
const WorkflowVariableBlockComponent = ({ const WorkflowVariableBlockComponent = ({
nodeKey, nodeKey,
variables, variables,
workflowNodesMap = {}, workflowNodesMap = {},
getVarType,
}: WorkflowVariableBlockComponentProps) => { }: WorkflowVariableBlockComponentProps) => {
const { t } = useTranslation() const { t } = useTranslation()
const [editor] = useLexicalComposerContext() const [editor] = useLexicalComposerContext()
@ -144,7 +150,10 @@ const WorkflowVariableBlockComponent = ({
<VarFullPathPanel <VarFullPathPanel
nodeName={node.title} nodeName={node.title}
path={variables.slice(1)} path={variables.slice(1)}
varType={Type.string} varType={getVarType({
nodeId: variables[0],
valueSelector: variables,
})}
nodeType={node?.type} nodeType={node?.type}
/>} />}
disabled={!isShowAPart} disabled={!isShowAPart}

View File

@ -25,11 +25,13 @@ export type WorkflowVariableBlockProps = {
getWorkflowNode: (nodeId: string) => Node getWorkflowNode: (nodeId: string) => Node
onInsert?: () => void onInsert?: () => void
onDelete?: () => void onDelete?: () => void
getVarType: any
} }
const WorkflowVariableBlock = memo(({ const WorkflowVariableBlock = memo(({
workflowNodesMap, workflowNodesMap,
onInsert, onInsert,
onDelete, onDelete,
getVarType,
}: WorkflowVariableBlockType) => { }: WorkflowVariableBlockType) => {
const [editor] = useLexicalComposerContext() const [editor] = useLexicalComposerContext()
@ -48,7 +50,7 @@ const WorkflowVariableBlock = memo(({
INSERT_WORKFLOW_VARIABLE_BLOCK_COMMAND, INSERT_WORKFLOW_VARIABLE_BLOCK_COMMAND,
(variables: string[]) => { (variables: string[]) => {
editor.dispatchCommand(CLEAR_HIDE_MENU_TIMEOUT, undefined) editor.dispatchCommand(CLEAR_HIDE_MENU_TIMEOUT, undefined)
const workflowVariableBlockNode = $createWorkflowVariableBlockNode(variables, workflowNodesMap) const workflowVariableBlockNode = $createWorkflowVariableBlockNode(variables, workflowNodesMap, getVarType)
$insertNodes([workflowVariableBlockNode]) $insertNodes([workflowVariableBlockNode])
if (onInsert) if (onInsert)
@ -69,7 +71,7 @@ const WorkflowVariableBlock = memo(({
COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_EDITOR,
), ),
) )
}, [editor, onInsert, onDelete, workflowNodesMap]) }, [editor, onInsert, onDelete, workflowNodesMap, getVarType])
return null return null
}) })

View File

@ -7,29 +7,32 @@ export type WorkflowNodesMap = WorkflowVariableBlockType['workflowNodesMap']
export type SerializedNode = SerializedLexicalNode & { export type SerializedNode = SerializedLexicalNode & {
variables: string[] variables: string[]
workflowNodesMap: WorkflowNodesMap workflowNodesMap: WorkflowNodesMap
getVarType: any
} }
export class WorkflowVariableBlockNode extends DecoratorNode<JSX.Element> { export class WorkflowVariableBlockNode extends DecoratorNode<JSX.Element> {
__variables: string[] __variables: string[]
__workflowNodesMap: WorkflowNodesMap __workflowNodesMap: WorkflowNodesMap
__getVarType: any
static getType(): string { static getType(): string {
return 'workflow-variable-block' return 'workflow-variable-block'
} }
static clone(node: WorkflowVariableBlockNode): WorkflowVariableBlockNode { static clone(node: WorkflowVariableBlockNode): WorkflowVariableBlockNode {
return new WorkflowVariableBlockNode(node.__variables, node.__workflowNodesMap, node.__key) return new WorkflowVariableBlockNode(node.__variables, node.__workflowNodesMap, node.__getVarType, node.__key)
} }
isInline(): boolean { isInline(): boolean {
return true return true
} }
constructor(variables: string[], workflowNodesMap: WorkflowNodesMap, key?: NodeKey) { constructor(variables: string[], workflowNodesMap: WorkflowNodesMap, getVarType: any, key?: NodeKey) {
super(key) super(key)
this.__variables = variables this.__variables = variables
this.__workflowNodesMap = workflowNodesMap this.__workflowNodesMap = workflowNodesMap
this.__getVarType = getVarType
} }
createDOM(): HTMLElement { createDOM(): HTMLElement {
@ -48,12 +51,13 @@ export class WorkflowVariableBlockNode extends DecoratorNode<JSX.Element> {
nodeKey={this.getKey()} nodeKey={this.getKey()}
variables={this.__variables} variables={this.__variables}
workflowNodesMap={this.__workflowNodesMap} workflowNodesMap={this.__workflowNodesMap}
getVarType={this.__getVarType!}
/> />
) )
} }
static importJSON(serializedNode: SerializedNode): WorkflowVariableBlockNode { static importJSON(serializedNode: SerializedNode): WorkflowVariableBlockNode {
const node = $createWorkflowVariableBlockNode(serializedNode.variables, serializedNode.workflowNodesMap) const node = $createWorkflowVariableBlockNode(serializedNode.variables, serializedNode.workflowNodesMap, serializedNode.getVarType)
return node return node
} }
@ -77,12 +81,17 @@ export class WorkflowVariableBlockNode extends DecoratorNode<JSX.Element> {
return self.__workflowNodesMap return self.__workflowNodesMap
} }
getVarType(): any {
const self = this.getLatest()
return self.__getVarType
}
getTextContent(): string { getTextContent(): string {
return `{{#${this.getVariables().join('.')}#}}` return `{{#${this.getVariables().join('.')}#}}`
} }
} }
export function $createWorkflowVariableBlockNode(variables: string[], workflowNodesMap: WorkflowNodesMap): WorkflowVariableBlockNode { export function $createWorkflowVariableBlockNode(variables: string[], workflowNodesMap: WorkflowNodesMap, getVarType: any): WorkflowVariableBlockNode {
return new WorkflowVariableBlockNode(variables, workflowNodesMap) return new WorkflowVariableBlockNode(variables, workflowNodesMap, getVarType)
} }
export function $isWorkflowVariableBlockNode( export function $isWorkflowVariableBlockNode(

View File

@ -16,6 +16,7 @@ import { VAR_REGEX as REGEX, resetReg } from '@/config'
const WorkflowVariableBlockReplacementBlock = ({ const WorkflowVariableBlockReplacementBlock = ({
workflowNodesMap, workflowNodesMap,
getVarType,
onInsert, onInsert,
}: WorkflowVariableBlockType) => { }: WorkflowVariableBlockType) => {
const [editor] = useLexicalComposerContext() const [editor] = useLexicalComposerContext()
@ -30,8 +31,8 @@ const WorkflowVariableBlockReplacementBlock = ({
onInsert() onInsert()
const nodePathString = textNode.getTextContent().slice(3, -3) const nodePathString = textNode.getTextContent().slice(3, -3)
return $applyNodeReplacement($createWorkflowVariableBlockNode(nodePathString.split('.'), workflowNodesMap)) return $applyNodeReplacement($createWorkflowVariableBlockNode(nodePathString.split('.'), workflowNodesMap, getVarType))
}, [onInsert, workflowNodesMap]) }, [onInsert, workflowNodesMap, getVarType])
const getMatch = useCallback((text: string) => { const getMatch = useCallback((text: string) => {
const matchArr = REGEX.exec(text) const matchArr = REGEX.exec(text)

View File

@ -3,6 +3,7 @@ import type { RoleName } from './plugins/history-block'
import type { import type {
Node, Node,
NodeOutPutVar, NodeOutPutVar,
ValueSelector,
} from '@/app/components/workflow/types' } from '@/app/components/workflow/types'
export type Option = { export type Option = {
@ -60,6 +61,10 @@ export type WorkflowVariableBlockType = {
workflowNodesMap?: Record<string, Pick<Node['data'], 'title' | 'type'>> workflowNodesMap?: Record<string, Pick<Node['data'], 'title' | 'type'>>
onInsert?: () => void onInsert?: () => void
onDelete?: () => void onDelete?: () => void
getVarType?: (payload: {
nodeId: string,
valueSelector: ValueSelector,
}) => string
} }
export type MenuTextMatch = { export type MenuTextMatch = {

View File

@ -8,6 +8,8 @@ import type {
ValueSelector, ValueSelector,
Var, Var,
} from '@/app/components/workflow/types' } from '@/app/components/workflow/types'
import { useIsChatMode, useWorkflow } from './use-workflow'
import { useStoreApi } from 'reactflow'
export const useWorkflowVariables = () => { export const useWorkflowVariables = () => {
const { t } = useTranslation() const { t } = useTranslation()
@ -72,3 +74,40 @@ export const useWorkflowVariables = () => {
getCurrentVariableType, getCurrentVariableType,
} }
} }
export const useWorkflowVariableType = () => {
const store = useStoreApi()
const {
getNodes,
} = store.getState()
const { getBeforeNodesInSameBranch } = useWorkflow()
const { getCurrentVariableType } = useWorkflowVariables()
const isChatMode = useIsChatMode()
const getVarType = ({
nodeId,
valueSelector,
}: {
nodeId: string,
valueSelector: ValueSelector,
}) => {
// debugger
const node = getNodes().find(n => n.id === nodeId)
console.log(nodeId, valueSelector)
const isInIteration = !!node?.data.isInIteration
const iterationNode = isInIteration ? getNodes().find(n => n.id === node.parentId) : null
const availableNodes = getBeforeNodesInSameBranch(nodeId)
const type = getCurrentVariableType({
parentNode: iterationNode,
valueSelector,
availableNodes,
isChatMode,
isConstant: false,
})
return type
}
return getVarType
}

View File

@ -36,6 +36,7 @@ import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/
import Switch from '@/app/components/base/switch' import Switch from '@/app/components/base/switch'
import { Jinja } from '@/app/components/base/icons/src/vender/workflow' import { Jinja } from '@/app/components/base/icons/src/vender/workflow'
import { useStore } from '@/app/components/workflow/store' import { useStore } from '@/app/components/workflow/store'
import { useWorkflowVariableType } from '@/app/components/workflow/hooks'
type Props = { type Props = {
className?: string className?: string
@ -143,6 +144,8 @@ const Editor: FC<Props> = ({
eventEmitter?.emit({ type: PROMPT_EDITOR_INSERT_QUICKLY, instanceId } as any) eventEmitter?.emit({ type: PROMPT_EDITOR_INSERT_QUICKLY, instanceId } as any)
} }
const getVarType = useWorkflowVariableType()
return ( return (
<Wrap className={cn(className, wrapClassName)} style={wrapStyle} isInNode isExpand={isExpand}> <Wrap className={cn(className, wrapClassName)} style={wrapStyle} isInNode isExpand={isExpand}>
<div ref={ref} className={cn(isFocus ? (gradientBorder && s.gradientBorder) : 'bg-gray-100', isExpand && 'h-full', '!rounded-[9px] p-0.5', containerClassName)}> <div ref={ref} className={cn(isFocus ? (gradientBorder && s.gradientBorder) : 'bg-gray-100', isExpand && 'h-full', '!rounded-[9px] p-0.5', containerClassName)}>
@ -249,6 +252,7 @@ const Editor: FC<Props> = ({
workflowVariableBlock={{ workflowVariableBlock={{
show: true, show: true,
variables: nodesOutputVars || [], variables: nodesOutputVars || [],
getVarType,
workflowNodesMap: availableNodes.reduce((acc, node) => { workflowNodesMap: availableNodes.reduce((acc, node) => {
acc[node.id] = { acc[node.id] = {
title: node.data.title, title: node.data.title,