mirror of
https://github.com/langgenius/dify.git
synced 2026-05-03 08:58:09 +08:00
refactor: Update variable syntax to support agent context markers
Extend variable pattern matching to support both `#` and `@` markers, with `@` specifically used for agent context variables. Update regex patterns, text processing logic, and add sub-graph persistence for agent variable handling.
This commit is contained in:
@ -1,86 +1,97 @@
|
||||
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 } from '@/app/components/workflow/types'
|
||||
import type { Edge, Node, ValueSelector } from '@/app/components/workflow/types'
|
||||
import { useMemo } from 'react'
|
||||
import { BlockEnum, PromptRole } from '@/app/components/workflow/types'
|
||||
import { AppModeEnum } from '@/types/app'
|
||||
|
||||
const SUBGRAPH_SOURCE_NODE_ID = 'subgraph-source'
|
||||
const SUBGRAPH_LLM_NODE_ID = 'subgraph-llm'
|
||||
export const SUBGRAPH_SOURCE_NODE_ID = 'subgraph-source'
|
||||
export const SUBGRAPH_LLM_NODE_ID = 'subgraph-llm'
|
||||
|
||||
export const getSubGraphInitialNodes = (
|
||||
sourceVariable: ValueSelector,
|
||||
agentName: 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,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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 } = props
|
||||
|
||||
const initialNodes = useMemo((): 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,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return [startNode, llmNode]
|
||||
return getSubGraphInitialNodes(sourceVariable, agentName)
|
||||
}, [sourceVariable, agentName])
|
||||
|
||||
const initialEdges = useMemo((): 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,
|
||||
},
|
||||
},
|
||||
]
|
||||
return getSubGraphInitialEdges()
|
||||
}, [])
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user