mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 12:37:20 +08:00
workflow template
This commit is contained in:
parent
7c64f2cfe0
commit
df9e2e478f
73
web/app/components/workflow/hooks/use-workflow-template.ts
Normal file
73
web/app/components/workflow/hooks/use-workflow-template.ts
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import { generateNewNode } from '../utils'
|
||||||
|
import {
|
||||||
|
NODE_WIDTH_X_OFFSET,
|
||||||
|
START_INITIAL_POSITION,
|
||||||
|
} from '../constants'
|
||||||
|
import { useIsChatMode } from './use-workflow'
|
||||||
|
import { useNodesInitialData } from './use-nodes-data'
|
||||||
|
|
||||||
|
export const useWorkflowTemplate = () => {
|
||||||
|
const isChatMode = useIsChatMode()
|
||||||
|
const nodesInitialData = useNodesInitialData()
|
||||||
|
|
||||||
|
const startNode = generateNewNode({
|
||||||
|
data: nodesInitialData.start,
|
||||||
|
position: START_INITIAL_POSITION,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (isChatMode) {
|
||||||
|
const llmNode = generateNewNode({
|
||||||
|
id: 'llm',
|
||||||
|
data: {
|
||||||
|
...nodesInitialData.llm,
|
||||||
|
memory: {
|
||||||
|
window: { enabled: false, size: 10 },
|
||||||
|
},
|
||||||
|
selected: true,
|
||||||
|
},
|
||||||
|
position: {
|
||||||
|
x: START_INITIAL_POSITION.x + NODE_WIDTH_X_OFFSET,
|
||||||
|
y: START_INITIAL_POSITION.y,
|
||||||
|
},
|
||||||
|
} as any)
|
||||||
|
|
||||||
|
const answerNode = generateNewNode({
|
||||||
|
id: 'answer',
|
||||||
|
data: {
|
||||||
|
...nodesInitialData.answer,
|
||||||
|
answer: `{{#${llmNode.id}.text#}}`,
|
||||||
|
},
|
||||||
|
position: {
|
||||||
|
x: START_INITIAL_POSITION.x + NODE_WIDTH_X_OFFSET * 2,
|
||||||
|
y: START_INITIAL_POSITION.y,
|
||||||
|
},
|
||||||
|
} as any)
|
||||||
|
|
||||||
|
const startToLlmEdge = {
|
||||||
|
id: `${startNode.id}-${llmNode.id}`,
|
||||||
|
source: startNode.id,
|
||||||
|
sourceHandle: 'source',
|
||||||
|
target: llmNode.id,
|
||||||
|
targetHandle: 'target',
|
||||||
|
}
|
||||||
|
|
||||||
|
const llmToAnswerEdge = {
|
||||||
|
id: `${llmNode.id}-${answerNode.id}`,
|
||||||
|
source: llmNode.id,
|
||||||
|
sourceHandle: 'source',
|
||||||
|
target: answerNode.id,
|
||||||
|
targetHandle: 'target',
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
nodes: [startNode, llmNode, answerNode],
|
||||||
|
edges: [startToLlmEdge, llmToAnswerEdge],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return {
|
||||||
|
nodes: [startNode],
|
||||||
|
edges: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -19,7 +19,6 @@ import type {
|
|||||||
Viewport,
|
Viewport,
|
||||||
} from 'reactflow'
|
} from 'reactflow'
|
||||||
import {
|
import {
|
||||||
generateNewNode,
|
|
||||||
getLayoutByDagre,
|
getLayoutByDagre,
|
||||||
initialEdges,
|
initialEdges,
|
||||||
initialNodes,
|
initialNodes,
|
||||||
@ -39,14 +38,11 @@ import {
|
|||||||
} from '../store'
|
} from '../store'
|
||||||
import {
|
import {
|
||||||
AUTO_LAYOUT_OFFSET,
|
AUTO_LAYOUT_OFFSET,
|
||||||
START_INITIAL_POSITION,
|
|
||||||
SUPPORT_OUTPUT_VARS_NODE,
|
SUPPORT_OUTPUT_VARS_NODE,
|
||||||
} from '../constants'
|
} from '../constants'
|
||||||
import { findUsedVarNodes, getNodeOutputVars, updateNodeVars } from '../nodes/_base/components/variable/utils'
|
import { findUsedVarNodes, getNodeOutputVars, updateNodeVars } from '../nodes/_base/components/variable/utils'
|
||||||
import {
|
import { useNodesExtraData } from './use-nodes-data'
|
||||||
useNodesExtraData,
|
import { useWorkflowTemplate } from './use-workflow-template'
|
||||||
useNodesInitialData,
|
|
||||||
} from './use-nodes-data'
|
|
||||||
import { useNodesSyncDraft } from './use-nodes-sync-draft'
|
import { useNodesSyncDraft } from './use-nodes-sync-draft'
|
||||||
import { useStore as useAppStore } from '@/app/components/app/store'
|
import { useStore as useAppStore } from '@/app/components/app/store'
|
||||||
import {
|
import {
|
||||||
@ -393,7 +389,10 @@ export const useFetchToolsData = () => {
|
|||||||
|
|
||||||
export const useWorkflowInit = () => {
|
export const useWorkflowInit = () => {
|
||||||
const workflowStore = useWorkflowStore()
|
const workflowStore = useWorkflowStore()
|
||||||
const nodesInitialData = useNodesInitialData()
|
const {
|
||||||
|
nodes: nodesTemplate,
|
||||||
|
edges: edgesTemplate,
|
||||||
|
} = useWorkflowTemplate()
|
||||||
const { handleFetchAllTools } = useFetchToolsData()
|
const { handleFetchAllTools } = useFetchToolsData()
|
||||||
const appDetail = useAppStore(state => state.appDetail)!
|
const appDetail = useAppStore(state => state.appDetail)!
|
||||||
const { data, isLoading, error, mutate } = useSWR(`/apps/${appDetail.id}/workflows/draft`, fetchWorkflowDraft)
|
const { data, isLoading, error, mutate } = useSWR(`/apps/${appDetail.id}/workflows/draft`, fetchWorkflowDraft)
|
||||||
@ -435,14 +434,8 @@ export const useWorkflowInit = () => {
|
|||||||
url: `/apps/${appDetail.id}/workflows/draft`,
|
url: `/apps/${appDetail.id}/workflows/draft`,
|
||||||
params: {
|
params: {
|
||||||
graph: {
|
graph: {
|
||||||
nodes: [generateNewNode({
|
nodes: nodesTemplate,
|
||||||
data: {
|
edges: edgesTemplate,
|
||||||
...nodesInitialData.start,
|
|
||||||
selected: true,
|
|
||||||
},
|
|
||||||
position: START_INITIAL_POSITION,
|
|
||||||
})],
|
|
||||||
edges: [],
|
|
||||||
},
|
},
|
||||||
features: {},
|
features: {},
|
||||||
},
|
},
|
||||||
|
|||||||
@ -152,9 +152,9 @@ export const getNodesConnectedSourceOrTargetHandleIdsMap = (changes: ConnectedSo
|
|||||||
return nodesConnectedSourceOrTargetHandleIdsMap
|
return nodesConnectedSourceOrTargetHandleIdsMap
|
||||||
}
|
}
|
||||||
|
|
||||||
export const generateNewNode = ({ data, position }: Pick<Node, 'data' | 'position'>) => {
|
export const generateNewNode = ({ data, position, id }: Pick<Node, 'data' | 'position'> & { id?: string }) => {
|
||||||
return {
|
return {
|
||||||
id: `${Date.now()}`,
|
id: id || `${Date.now()}`,
|
||||||
type: 'custom',
|
type: 'custom',
|
||||||
data,
|
data,
|
||||||
position,
|
position,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user