From 1ff677c3000689e59568bf9164e55c1fbdbf33a3 Mon Sep 17 00:00:00 2001 From: zhsama Date: Thu, 15 Jan 2026 04:08:42 +0800 Subject: [PATCH] refactor: Remove unused sub-graph persistence and initialization hooks. Simplified sub-graph store by removing unused state fields and setters. --- web/app/components/sub-graph/hooks/index.ts | 3 - .../sub-graph/hooks/use-sub-graph-init.ts | 116 ------------------ .../hooks/use-sub-graph-persistence.ts | 116 ------------------ web/app/components/sub-graph/store/index.ts | 43 +------ web/app/components/sub-graph/types.ts | 51 +------- 5 files changed, 2 insertions(+), 327 deletions(-) delete mode 100644 web/app/components/sub-graph/hooks/use-sub-graph-init.ts delete mode 100644 web/app/components/sub-graph/hooks/use-sub-graph-persistence.ts diff --git a/web/app/components/sub-graph/hooks/index.ts b/web/app/components/sub-graph/hooks/index.ts index d67a22f2cc..71ef209f64 100644 --- a/web/app/components/sub-graph/hooks/index.ts +++ b/web/app/components/sub-graph/hooks/index.ts @@ -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' diff --git a/web/app/components/sub-graph/hooks/use-sub-graph-init.ts b/web/app/components/sub-graph/hooks/use-sub-graph-init.ts deleted file mode 100644 index 84ee9abd9a..0000000000 --- a/web/app/components/sub-graph/hooks/use-sub-graph-init.ts +++ /dev/null @@ -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 = { - 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 = { - 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, - } -} diff --git a/web/app/components/sub-graph/hooks/use-sub-graph-persistence.ts b/web/app/components/sub-graph/hooks/use-sub-graph-persistence.ts deleted file mode 100644 index ee54000b9c..0000000000 --- a/web/app/components/sub-graph/hooks/use-sub-graph-persistence.ts +++ /dev/null @@ -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(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, - ) => { - 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, - } -} diff --git a/web/app/components/sub-graph/store/index.ts b/web/app/components/sub-graph/store/index.ts index 3b314be72a..0701cdc3f2 100644 --- a/web/app/components/sub-graph/store/index.ts +++ b/web/app/components/sub-graph/store/index.ts @@ -1,53 +1,12 @@ import type { CreateSubGraphSlice, SubGraphSliceShape } from '../types' -const initialState: Omit = { - parentToolNodeId: '', - parameterKey: '', - sourceAgentNodeId: '', - sourceVariable: [], - subGraphReadOnly: true, - - subGraphNodes: [], - subGraphEdges: [], - - selectedOutputVar: [], - whenOutputNone: 'default', - defaultValue: '', - - showDebugPanel: false, - isRunning: false, - +const initialState: Omit = { 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 })), }) diff --git a/web/app/components/sub-graph/types.ts b/web/app/components/sub-graph/types.ts index 936ac87cde..fc98101a08 100644 --- a/web/app/components/sub-graph/types.ts +++ b/web/app/components/sub-graph/types.ts @@ -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