mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 17:38:04 +08:00
refactor: Remove unused sub-graph persistence and initialization hooks.
Simplified sub-graph store by removing unused state fields and setters.
This commit is contained in:
@ -1,5 +1,2 @@
|
||||
export { useAvailableNodesMetaData } from './use-available-nodes-meta-data'
|
||||
export { useSubGraphInit } from './use-sub-graph-init'
|
||||
export { useSubGraphNodes } from './use-sub-graph-nodes'
|
||||
export { useSubGraphPersistence } from './use-sub-graph-persistence'
|
||||
export type { SubGraphData } from './use-sub-graph-persistence'
|
||||
|
||||
@ -1,116 +0,0 @@
|
||||
import type { SubGraphProps } from '../types'
|
||||
import type { LLMNodeType } from '@/app/components/workflow/nodes/llm/types'
|
||||
import type { StartNodeType } from '@/app/components/workflow/nodes/start/types'
|
||||
import type { Edge, Node, ValueSelector } from '@/app/components/workflow/types'
|
||||
import { useMemo } from 'react'
|
||||
import { Type } from '@/app/components/workflow/nodes/llm/types'
|
||||
import { BlockEnum, PromptRole } from '@/app/components/workflow/types'
|
||||
import { AppModeEnum } from '@/types/app'
|
||||
|
||||
export const SUBGRAPH_SOURCE_NODE_ID = 'subgraph-source'
|
||||
export const SUBGRAPH_LLM_NODE_ID = 'subgraph-llm'
|
||||
|
||||
export const getSubGraphInitialNodes = (
|
||||
sourceVariable: ValueSelector,
|
||||
agentName: string,
|
||||
paramKey: string,
|
||||
): Node[] => {
|
||||
const sourceVarName = sourceVariable.length > 1
|
||||
? sourceVariable.slice(1).join('.')
|
||||
: 'output'
|
||||
|
||||
const startNode: Node<StartNodeType> = {
|
||||
id: SUBGRAPH_SOURCE_NODE_ID,
|
||||
type: 'custom',
|
||||
position: { x: 100, y: 150 },
|
||||
data: {
|
||||
type: BlockEnum.Start,
|
||||
title: `${agentName}: ${sourceVarName}`,
|
||||
desc: 'Source variable from agent',
|
||||
_connectedSourceHandleIds: ['source'],
|
||||
_connectedTargetHandleIds: [],
|
||||
variables: [],
|
||||
},
|
||||
}
|
||||
|
||||
const llmNode: Node<LLMNodeType> = {
|
||||
id: SUBGRAPH_LLM_NODE_ID,
|
||||
type: 'custom',
|
||||
position: { x: 450, y: 150 },
|
||||
data: {
|
||||
type: BlockEnum.LLM,
|
||||
title: 'LLM',
|
||||
desc: 'Transform the output',
|
||||
_connectedSourceHandleIds: [],
|
||||
_connectedTargetHandleIds: ['target'],
|
||||
model: {
|
||||
provider: '',
|
||||
name: '',
|
||||
mode: AppModeEnum.CHAT,
|
||||
completion_params: {
|
||||
temperature: 0.7,
|
||||
},
|
||||
},
|
||||
prompt_template: [{
|
||||
role: PromptRole.system,
|
||||
text: '',
|
||||
}],
|
||||
context: {
|
||||
enabled: false,
|
||||
variable_selector: [],
|
||||
},
|
||||
vision: {
|
||||
enabled: false,
|
||||
},
|
||||
structured_output_enabled: true,
|
||||
structured_output: {
|
||||
schema: {
|
||||
type: Type.object,
|
||||
properties: {
|
||||
[paramKey]: {
|
||||
type: Type.string,
|
||||
},
|
||||
},
|
||||
required: [paramKey],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return [startNode, llmNode]
|
||||
}
|
||||
|
||||
export const getSubGraphInitialEdges = (): Edge[] => {
|
||||
return [
|
||||
{
|
||||
id: `${SUBGRAPH_SOURCE_NODE_ID}-${SUBGRAPH_LLM_NODE_ID}`,
|
||||
source: SUBGRAPH_SOURCE_NODE_ID,
|
||||
sourceHandle: 'source',
|
||||
target: SUBGRAPH_LLM_NODE_ID,
|
||||
targetHandle: 'target',
|
||||
type: 'custom',
|
||||
data: {
|
||||
sourceType: BlockEnum.Start,
|
||||
targetType: BlockEnum.LLM,
|
||||
},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
export const useSubGraphInit = (props: SubGraphProps) => {
|
||||
const { sourceVariable, agentName, paramKey } = props
|
||||
|
||||
const initialNodes = useMemo((): Node[] => {
|
||||
return getSubGraphInitialNodes(sourceVariable, agentName, paramKey)
|
||||
}, [sourceVariable, agentName, paramKey])
|
||||
|
||||
const initialEdges = useMemo((): Edge[] => {
|
||||
return getSubGraphInitialEdges()
|
||||
}, [])
|
||||
|
||||
return {
|
||||
initialNodes,
|
||||
initialEdges,
|
||||
}
|
||||
}
|
||||
@ -1,116 +0,0 @@
|
||||
import type { SubGraphConfig } from '../types'
|
||||
import type { ToolNodeType } from '@/app/components/workflow/nodes/tool/types'
|
||||
import type { Edge, Node } from '@/app/components/workflow/types'
|
||||
import { useCallback } from 'react'
|
||||
import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
|
||||
import { VarKindType } from '@/app/components/workflow/nodes/_base/types'
|
||||
|
||||
type SubGraphPersistenceProps = {
|
||||
toolNodeId: string
|
||||
paramKey: string
|
||||
}
|
||||
|
||||
export type SubGraphData = {
|
||||
nodes: Node[]
|
||||
edges: Edge[]
|
||||
config: SubGraphConfig
|
||||
}
|
||||
|
||||
const SUB_GRAPH_DATA_PREFIX = '__subgraph__'
|
||||
|
||||
export const useSubGraphPersistence = ({
|
||||
toolNodeId,
|
||||
paramKey,
|
||||
}: SubGraphPersistenceProps) => {
|
||||
const { inputs, setInputs } = useNodeCrud<ToolNodeType>(toolNodeId, {} as ToolNodeType)
|
||||
|
||||
const getSubGraphDataKey = useCallback(() => {
|
||||
return `${SUB_GRAPH_DATA_PREFIX}${paramKey}`
|
||||
}, [paramKey])
|
||||
|
||||
const loadSubGraphData = useCallback((): SubGraphData | null => {
|
||||
const dataKey = getSubGraphDataKey()
|
||||
const toolParameters = inputs.tool_parameters || {}
|
||||
const storedData = toolParameters[dataKey]
|
||||
|
||||
if (!storedData || storedData.type !== VarKindType.constant) {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = typeof storedData.value === 'string'
|
||||
? JSON.parse(storedData.value)
|
||||
: storedData.value
|
||||
|
||||
return parsed as SubGraphData
|
||||
}
|
||||
catch {
|
||||
return null
|
||||
}
|
||||
}, [getSubGraphDataKey, inputs.tool_parameters])
|
||||
|
||||
const saveSubGraphData = useCallback((data: SubGraphData) => {
|
||||
const dataKey = getSubGraphDataKey()
|
||||
const newToolParameters = {
|
||||
...inputs.tool_parameters,
|
||||
[dataKey]: {
|
||||
type: VarKindType.constant,
|
||||
value: JSON.stringify(data),
|
||||
},
|
||||
}
|
||||
|
||||
setInputs({
|
||||
...inputs,
|
||||
tool_parameters: newToolParameters,
|
||||
})
|
||||
}, [getSubGraphDataKey, inputs, setInputs])
|
||||
|
||||
const hasSubGraphData = useCallback(() => {
|
||||
const dataKey = getSubGraphDataKey()
|
||||
const toolParameters = inputs.tool_parameters || {}
|
||||
return !!toolParameters[dataKey]
|
||||
}, [getSubGraphDataKey, inputs.tool_parameters])
|
||||
|
||||
const updateSubGraphConfig = useCallback((
|
||||
config: Partial<SubGraphConfig>,
|
||||
) => {
|
||||
const existingData = loadSubGraphData()
|
||||
if (!existingData)
|
||||
return
|
||||
|
||||
saveSubGraphData({
|
||||
...existingData,
|
||||
config: {
|
||||
...existingData.config,
|
||||
...config,
|
||||
},
|
||||
})
|
||||
}, [loadSubGraphData, saveSubGraphData])
|
||||
|
||||
const updateSubGraphNodes = useCallback((
|
||||
nodes: Node[],
|
||||
edges: Edge[],
|
||||
) => {
|
||||
const existingData = loadSubGraphData()
|
||||
const defaultConfig: SubGraphConfig = {
|
||||
enabled: true,
|
||||
startNodeId: nodes[0]?.id || '',
|
||||
selectedOutputVar: [],
|
||||
whenOutputNone: 'default',
|
||||
}
|
||||
|
||||
saveSubGraphData({
|
||||
nodes,
|
||||
edges,
|
||||
config: existingData?.config || defaultConfig,
|
||||
})
|
||||
}, [loadSubGraphData, saveSubGraphData])
|
||||
|
||||
return {
|
||||
loadSubGraphData,
|
||||
saveSubGraphData,
|
||||
hasSubGraphData,
|
||||
updateSubGraphConfig,
|
||||
updateSubGraphNodes,
|
||||
}
|
||||
}
|
||||
@ -1,53 +1,12 @@
|
||||
import type { CreateSubGraphSlice, SubGraphSliceShape } from '../types'
|
||||
|
||||
const initialState: Omit<SubGraphSliceShape, 'setSubGraphContext' | 'setSubGraphNodes' | 'setSubGraphEdges' | 'setSelectedOutputVar' | 'setWhenOutputNone' | 'setDefaultValue' | 'setShowDebugPanel' | 'setIsRunning' | 'setParentAvailableVars' | 'setParentAvailableNodes' | 'resetSubGraph'> = {
|
||||
parentToolNodeId: '',
|
||||
parameterKey: '',
|
||||
sourceAgentNodeId: '',
|
||||
sourceVariable: [],
|
||||
subGraphReadOnly: true,
|
||||
|
||||
subGraphNodes: [],
|
||||
subGraphEdges: [],
|
||||
|
||||
selectedOutputVar: [],
|
||||
whenOutputNone: 'default',
|
||||
defaultValue: '',
|
||||
|
||||
showDebugPanel: false,
|
||||
isRunning: false,
|
||||
|
||||
const initialState: Omit<SubGraphSliceShape, 'setParentAvailableVars' | 'setParentAvailableNodes'> = {
|
||||
parentAvailableVars: [],
|
||||
parentAvailableNodes: [],
|
||||
}
|
||||
|
||||
export const createSubGraphSlice: CreateSubGraphSlice = set => ({
|
||||
...initialState,
|
||||
|
||||
setSubGraphContext: context => set(() => ({
|
||||
parentToolNodeId: context.parentToolNodeId,
|
||||
parameterKey: context.parameterKey,
|
||||
sourceAgentNodeId: context.sourceAgentNodeId,
|
||||
sourceVariable: context.sourceVariable,
|
||||
})),
|
||||
|
||||
setSubGraphNodes: nodes => set(() => ({ subGraphNodes: nodes })),
|
||||
|
||||
setSubGraphEdges: edges => set(() => ({ subGraphEdges: edges })),
|
||||
|
||||
setSelectedOutputVar: selector => set(() => ({ selectedOutputVar: selector })),
|
||||
|
||||
setWhenOutputNone: option => set(() => ({ whenOutputNone: option })),
|
||||
|
||||
setDefaultValue: value => set(() => ({ defaultValue: value })),
|
||||
|
||||
setShowDebugPanel: show => set(() => ({ showDebugPanel: show })),
|
||||
|
||||
setIsRunning: running => set(() => ({ isRunning: running })),
|
||||
|
||||
setParentAvailableVars: vars => set(() => ({ parentAvailableVars: vars })),
|
||||
|
||||
setParentAvailableNodes: nodes => set(() => ({ parentAvailableNodes: nodes })),
|
||||
|
||||
resetSubGraph: () => set(() => ({ ...initialState })),
|
||||
})
|
||||
|
||||
@ -1,25 +1,7 @@
|
||||
import type { StateCreator } from 'zustand'
|
||||
import type { MentionConfig } from '@/app/components/workflow/nodes/_base/types'
|
||||
import type { LLMNodeType } from '@/app/components/workflow/nodes/llm/types'
|
||||
import type { Edge, Node, NodeOutPutVar, ValueSelector, VarType } from '@/app/components/workflow/types'
|
||||
|
||||
export type WhenOutputNoneOption = 'error' | 'default'
|
||||
|
||||
export type SubGraphConfig = {
|
||||
enabled: boolean
|
||||
startNodeId: string
|
||||
selectedOutputVar: ValueSelector
|
||||
whenOutputNone: WhenOutputNoneOption
|
||||
defaultValue?: string
|
||||
}
|
||||
|
||||
export type SubGraphOutputVariable = {
|
||||
nodeId: string
|
||||
nodeName: string
|
||||
variable: string
|
||||
type: VarType
|
||||
description?: string
|
||||
}
|
||||
import type { Edge, Node, NodeOutPutVar, ValueSelector } from '@/app/components/workflow/types'
|
||||
|
||||
export type SubGraphProps = {
|
||||
toolNodeId: string
|
||||
@ -37,41 +19,10 @@ export type SubGraphProps = {
|
||||
}
|
||||
|
||||
export type SubGraphSliceShape = {
|
||||
parentToolNodeId: string
|
||||
parameterKey: string
|
||||
sourceAgentNodeId: string
|
||||
sourceVariable: ValueSelector
|
||||
subGraphReadOnly: boolean
|
||||
|
||||
subGraphNodes: Node[]
|
||||
subGraphEdges: Edge[]
|
||||
|
||||
selectedOutputVar: ValueSelector
|
||||
whenOutputNone: WhenOutputNoneOption
|
||||
defaultValue: string
|
||||
|
||||
showDebugPanel: boolean
|
||||
isRunning: boolean
|
||||
|
||||
parentAvailableVars: NodeOutPutVar[]
|
||||
parentAvailableNodes: Node[]
|
||||
|
||||
setSubGraphContext: (context: {
|
||||
parentToolNodeId: string
|
||||
parameterKey: string
|
||||
sourceAgentNodeId: string
|
||||
sourceVariable: ValueSelector
|
||||
}) => void
|
||||
setSubGraphNodes: (nodes: Node[]) => void
|
||||
setSubGraphEdges: (edges: Edge[]) => void
|
||||
setSelectedOutputVar: (selector: ValueSelector) => void
|
||||
setWhenOutputNone: (option: WhenOutputNoneOption) => void
|
||||
setDefaultValue: (value: string) => void
|
||||
setShowDebugPanel: (show: boolean) => void
|
||||
setIsRunning: (running: boolean) => void
|
||||
setParentAvailableVars: (vars: NodeOutPutVar[]) => void
|
||||
setParentAvailableNodes: (nodes: Node[]) => void
|
||||
resetSubGraph: () => void
|
||||
}
|
||||
|
||||
export type CreateSubGraphSlice = StateCreator<SubGraphSliceShape>
|
||||
|
||||
Reference in New Issue
Block a user