mirror of
https://github.com/langgenius/dify.git
synced 2026-05-29 05:07:55 +08:00
fix(web): sys variables not supported in snippet
This commit is contained in:
@ -41,6 +41,31 @@ const outputVars: NodeOutPutVar[] = [{
|
||||
}] satisfies Var[],
|
||||
}]
|
||||
|
||||
const outputVarsWithSystemVars: NodeOutPutVar[] = [
|
||||
{
|
||||
nodeId: 'vars-node',
|
||||
title: 'Vars',
|
||||
vars: [
|
||||
{
|
||||
variable: 'answer',
|
||||
type: VarType.string,
|
||||
},
|
||||
{
|
||||
variable: 'sys.files',
|
||||
type: VarType.arrayFile,
|
||||
},
|
||||
] satisfies Var[],
|
||||
},
|
||||
{
|
||||
nodeId: 'global',
|
||||
title: 'SYSTEM',
|
||||
vars: [{
|
||||
variable: 'sys.user_id',
|
||||
type: VarType.string,
|
||||
}] satisfies Var[],
|
||||
},
|
||||
]
|
||||
|
||||
describe('useNodesAvailableVarList', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
@ -121,6 +146,38 @@ describe('useNodesAvailableVarList', () => {
|
||||
}))
|
||||
})
|
||||
|
||||
it('filters system variables on snippet canvases', () => {
|
||||
globalThis.history.pushState({}, '', '/snippets/snippet-1/orchestrate')
|
||||
mockGetNodeAvailableVars.mockReturnValue(outputVarsWithSystemVars)
|
||||
|
||||
const currentNode = createNode({ id: 'node-a' })
|
||||
|
||||
const { result } = renderHook(() => useNodesAvailableVarList([currentNode], {
|
||||
filterVar: () => true,
|
||||
}))
|
||||
|
||||
expect(result.current['node-a']?.availableVars).toEqual([{
|
||||
nodeId: 'vars-node',
|
||||
title: 'Vars',
|
||||
vars: [{
|
||||
variable: 'answer',
|
||||
type: VarType.string,
|
||||
}],
|
||||
}])
|
||||
})
|
||||
|
||||
it('keeps system variables outside snippet canvases', () => {
|
||||
mockGetNodeAvailableVars.mockReturnValue(outputVarsWithSystemVars)
|
||||
|
||||
const currentNode = createNode({ id: 'node-a' })
|
||||
|
||||
const { result } = renderHook(() => useNodesAvailableVarList([currentNode], {
|
||||
filterVar: () => true,
|
||||
}))
|
||||
|
||||
expect(result.current['node-a']?.availableVars).toEqual(outputVarsWithSystemVars)
|
||||
})
|
||||
|
||||
it('returns a callback version that can use leaf nodes or caller-provided nodes', () => {
|
||||
const firstNode = createNode({ id: 'node-a' })
|
||||
const secondNode = createNode({ id: 'node-b' })
|
||||
|
||||
@ -9,6 +9,7 @@ import {
|
||||
} from '@/app/components/workflow/hooks'
|
||||
import {
|
||||
appendSnippetInputFieldVars,
|
||||
isSnippetCanvas,
|
||||
} from '@/app/components/workflow/nodes/_base/hooks/snippet-input-field-vars'
|
||||
import { BlockEnum } from '@/app/components/workflow/types'
|
||||
|
||||
@ -35,6 +36,18 @@ const getNodeInfo = (nodeId: string, nodes: Node[]) => {
|
||||
}
|
||||
}
|
||||
|
||||
const filterSystemVarsForSnippet = (availableVars: NodeOutPutVar[]) => {
|
||||
if (!isSnippetCanvas())
|
||||
return availableVars
|
||||
|
||||
return availableVars
|
||||
.map(nodeVar => ({
|
||||
...nodeVar,
|
||||
vars: nodeVar.vars.filter(variable => !variable.variable.startsWith('sys.')),
|
||||
}))
|
||||
.filter(nodeVar => nodeVar.vars.length > 0)
|
||||
}
|
||||
|
||||
// TODO: loop type?
|
||||
const useNodesAvailableVarList = (nodes: Node[], {
|
||||
onlyLeafNodeVar,
|
||||
@ -69,7 +82,7 @@ const useNodesAvailableVarList = (nodes: Node[], {
|
||||
parentNode: iterationNode,
|
||||
} = getNodeInfo(nodeId, nodes)
|
||||
|
||||
const availableVars = [
|
||||
const availableVars = filterSystemVarsForSnippet([
|
||||
...snippetInputFieldAvailability.availableVars,
|
||||
...getNodeAvailableVars({
|
||||
parentNode: iterationNode,
|
||||
@ -79,7 +92,7 @@ const useNodesAvailableVarList = (nodes: Node[], {
|
||||
hideEnv,
|
||||
hideChatVar,
|
||||
}),
|
||||
]
|
||||
])
|
||||
const result = {
|
||||
node,
|
||||
availableVars,
|
||||
@ -123,7 +136,7 @@ export const useGetNodesAvailableVarList = () => {
|
||||
parentNode: iterationNode,
|
||||
} = getNodeInfo(nodeId, nodes)
|
||||
|
||||
const availableVars = [
|
||||
const availableVars = filterSystemVarsForSnippet([
|
||||
...snippetInputFieldAvailability.availableVars,
|
||||
...getNodeAvailableVars({
|
||||
parentNode: iterationNode,
|
||||
@ -133,7 +146,7 @@ export const useGetNodesAvailableVarList = () => {
|
||||
hideEnv,
|
||||
hideChatVar,
|
||||
}),
|
||||
]
|
||||
])
|
||||
const result = {
|
||||
node,
|
||||
availableVars,
|
||||
|
||||
Reference in New Issue
Block a user