diff --git a/web/app/components/workflow/nodes/_base/hooks/use-available-var-list.ts b/web/app/components/workflow/nodes/_base/hooks/use-available-var-list.ts new file mode 100644 index 0000000000..ca53014948 --- /dev/null +++ b/web/app/components/workflow/nodes/_base/hooks/use-available-var-list.ts @@ -0,0 +1,27 @@ +import { + useIsChatMode, + useWorkflow, +} from '@/app/components/workflow/hooks' +import { toNodeOutputVars } from '@/app/components/workflow/nodes/_base/components/variable/utils' +import type { ValueSelector, Var } from '@/app/components/workflow/types' + +type Params = { + onlyLeafNodeVar?: boolean + filterVar: (payload: Var, selector: ValueSelector) => boolean +} +const useAvailableVarList = (nodeId: string, { + onlyLeafNodeVar, + filterVar, +}: Params = { + onlyLeafNodeVar: false, + filterVar: () => true, +}) => { + const { getTreeLeafNodes, getBeforeNodesInSameBranch } = useWorkflow() + const isChatMode = useIsChatMode() + + const availableNodes = onlyLeafNodeVar ? getTreeLeafNodes(nodeId) : getBeforeNodesInSameBranch(nodeId) + const availableVars = toNodeOutputVars(availableNodes, isChatMode, filterVar).map(item => item.vars) + return availableVars +} + +export default useAvailableVarList diff --git a/web/app/components/workflow/nodes/llm/components/config-prompt.tsx b/web/app/components/workflow/nodes/llm/components/config-prompt.tsx index 51d8b5ef9c..63ced99be4 100644 --- a/web/app/components/workflow/nodes/llm/components/config-prompt.tsx +++ b/web/app/components/workflow/nodes/llm/components/config-prompt.tsx @@ -3,8 +3,9 @@ import type { FC } from 'react' import React, { useCallback } from 'react' import { useTranslation } from 'react-i18next' import produce from 'immer' -import type { PromptItem } from '../../../types' +import type { PromptItem, ValueSelector, Var } from '../../../types' import { PromptRole } from '../../../types' +import useAvailableVarList from '../../_base/hooks/use-available-var-list' import Editor from '@/app/components/workflow/nodes/_base/components/prompt/editor' import AddButton from '@/app/components/workflow/nodes/_base/components/add-button' import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector' @@ -15,6 +16,8 @@ const i18nPrefix = 'workflow.nodes.llm' type Props = { readOnly: boolean + nodeId: string + filterVar: (payload: Var, selector: ValueSelector) => boolean isChatModel: boolean isChatApp: boolean payload: PromptItem | PromptItem[] @@ -30,6 +33,8 @@ type Props = { const ConfigPrompt: FC = ({ readOnly, + nodeId, + filterVar, isChatModel, isChatApp, payload, @@ -39,6 +44,11 @@ const ConfigPrompt: FC = ({ hasSetBlockStatus, }) => { const { t } = useTranslation() + const availableVarList = useAvailableVarList(nodeId, { + onlyLeafNodeVar: false, + filterVar, + }) + console.log('availableVarList', availableVarList) const handleChatModePromptChange = useCallback((index: number) => { return (prompt: string) => { diff --git a/web/app/components/workflow/nodes/llm/panel.tsx b/web/app/components/workflow/nodes/llm/panel.tsx index 6405435d68..581e4bcc48 100644 --- a/web/app/components/workflow/nodes/llm/panel.tsx +++ b/web/app/components/workflow/nodes/llm/panel.tsx @@ -171,6 +171,8 @@ const Panel: FC> = ({ {model.name && (