Feature/Update Loop Agentflow (#4957)
* Feature: Update Loop Agentflow to include fallback message and version increment to 1.1 - Added a new input parameter 'fallbackMessage' to the Loop Agentflow for displaying a message when the loop count is exceeded. - Incremented the version of Loop Agentflow from 1.0 to 1.1. - Updated the processing logic to handle the fallback message appropriately when the maximum loop count is reached. * - Introduced a new input parameter 'loopUpdateState' to allow updating the runtime state during workflow execution. - Added a method to list runtime state keys for dynamic state management. - Implemented logic to retrieve and utilize the current loop count in variable resolution. - Updated the Loop Agentflow output to reflect the new state and final output content.
This commit is contained in:
parent
31434e52ce
commit
0065e8f1a0
|
|
@ -1,4 +1,5 @@
|
||||||
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
|
import { ICommonObject, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
|
||||||
|
import { updateFlowState } from '../utils'
|
||||||
|
|
||||||
class Loop_Agentflow implements INode {
|
class Loop_Agentflow implements INode {
|
||||||
label: string
|
label: string
|
||||||
|
|
@ -19,7 +20,7 @@ class Loop_Agentflow implements INode {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.label = 'Loop'
|
this.label = 'Loop'
|
||||||
this.name = 'loopAgentflow'
|
this.name = 'loopAgentflow'
|
||||||
this.version = 1.0
|
this.version = 1.1
|
||||||
this.type = 'Loop'
|
this.type = 'Loop'
|
||||||
this.category = 'Agent Flows'
|
this.category = 'Agent Flows'
|
||||||
this.description = 'Loop back to a previous node'
|
this.description = 'Loop back to a previous node'
|
||||||
|
|
@ -40,6 +41,40 @@ class Loop_Agentflow implements INode {
|
||||||
name: 'maxLoopCount',
|
name: 'maxLoopCount',
|
||||||
type: 'number',
|
type: 'number',
|
||||||
default: 5
|
default: 5
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Fallback Message',
|
||||||
|
name: 'fallbackMessage',
|
||||||
|
type: 'string',
|
||||||
|
description: 'Message to display if the loop count is exceeded',
|
||||||
|
placeholder: 'Enter your fallback message here',
|
||||||
|
rows: 4,
|
||||||
|
acceptVariable: true,
|
||||||
|
optional: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Update Flow State',
|
||||||
|
name: 'loopUpdateState',
|
||||||
|
description: 'Update runtime state during the execution of the workflow',
|
||||||
|
type: 'array',
|
||||||
|
optional: true,
|
||||||
|
acceptVariable: true,
|
||||||
|
array: [
|
||||||
|
{
|
||||||
|
label: 'Key',
|
||||||
|
name: 'key',
|
||||||
|
type: 'asyncOptions',
|
||||||
|
loadMethod: 'listRuntimeStateKeys',
|
||||||
|
freeSolo: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Value',
|
||||||
|
name: 'value',
|
||||||
|
type: 'string',
|
||||||
|
acceptVariable: true,
|
||||||
|
acceptNodeOutputAsVariable: true
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
@ -58,12 +93,20 @@ class Loop_Agentflow implements INode {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return returnOptions
|
return returnOptions
|
||||||
|
},
|
||||||
|
async listRuntimeStateKeys(_: INodeData, options: ICommonObject): Promise<INodeOptionsValue[]> {
|
||||||
|
const previousNodes = options.previousNodes as ICommonObject[]
|
||||||
|
const startAgentflowNode = previousNodes.find((node) => node.name === 'startAgentflow')
|
||||||
|
const state = startAgentflowNode?.inputs?.startState as ICommonObject[]
|
||||||
|
return state.map((item) => ({ label: item.key, name: item.key }))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
async run(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
|
||||||
const loopBackToNode = nodeData.inputs?.loopBackToNode as string
|
const loopBackToNode = nodeData.inputs?.loopBackToNode as string
|
||||||
const _maxLoopCount = nodeData.inputs?.maxLoopCount as string
|
const _maxLoopCount = nodeData.inputs?.maxLoopCount as string
|
||||||
|
const fallbackMessage = nodeData.inputs?.fallbackMessage as string
|
||||||
|
const _loopUpdateState = nodeData.inputs?.loopUpdateState
|
||||||
|
|
||||||
const state = options.agentflowRuntime?.state as ICommonObject
|
const state = options.agentflowRuntime?.state as ICommonObject
|
||||||
|
|
||||||
|
|
@ -75,16 +118,34 @@ class Loop_Agentflow implements INode {
|
||||||
maxLoopCount: _maxLoopCount ? parseInt(_maxLoopCount) : 5
|
maxLoopCount: _maxLoopCount ? parseInt(_maxLoopCount) : 5
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const finalOutput = 'Loop back to ' + `${loopBackToNodeLabel} (${loopBackToNodeId})`
|
||||||
|
|
||||||
|
// Update flow state if needed
|
||||||
|
let newState = { ...state }
|
||||||
|
if (_loopUpdateState && Array.isArray(_loopUpdateState) && _loopUpdateState.length > 0) {
|
||||||
|
newState = updateFlowState(state, _loopUpdateState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process template variables in state
|
||||||
|
if (newState && Object.keys(newState).length > 0) {
|
||||||
|
for (const key in newState) {
|
||||||
|
if (newState[key].toString().includes('{{ output }}')) {
|
||||||
|
newState[key] = finalOutput
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const returnOutput = {
|
const returnOutput = {
|
||||||
id: nodeData.id,
|
id: nodeData.id,
|
||||||
name: this.name,
|
name: this.name,
|
||||||
input: data,
|
input: data,
|
||||||
output: {
|
output: {
|
||||||
content: 'Loop back to ' + `${loopBackToNodeLabel} (${loopBackToNodeId})`,
|
content: finalOutput,
|
||||||
nodeID: loopBackToNodeId,
|
nodeID: loopBackToNodeId,
|
||||||
maxLoopCount: _maxLoopCount ? parseInt(_maxLoopCount) : 5
|
maxLoopCount: _maxLoopCount ? parseInt(_maxLoopCount) : 5,
|
||||||
|
fallbackMessage
|
||||||
},
|
},
|
||||||
state
|
state: newState
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnOutput
|
return returnOutput
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,8 @@ import {
|
||||||
QUESTION_VAR_PREFIX,
|
QUESTION_VAR_PREFIX,
|
||||||
CURRENT_DATE_TIME_VAR_PREFIX,
|
CURRENT_DATE_TIME_VAR_PREFIX,
|
||||||
_removeCredentialId,
|
_removeCredentialId,
|
||||||
validateHistorySchema
|
validateHistorySchema,
|
||||||
|
LOOP_COUNT_VAR_PREFIX
|
||||||
} from '.'
|
} from '.'
|
||||||
import { ChatFlow } from '../database/entities/ChatFlow'
|
import { ChatFlow } from '../database/entities/ChatFlow'
|
||||||
import { Variable } from '../database/entities/Variable'
|
import { Variable } from '../database/entities/Variable'
|
||||||
|
|
@ -84,6 +85,8 @@ interface IProcessNodeOutputsParams {
|
||||||
waitingNodes: Map<string, IWaitingNode>
|
waitingNodes: Map<string, IWaitingNode>
|
||||||
loopCounts: Map<string, number>
|
loopCounts: Map<string, number>
|
||||||
abortController?: AbortController
|
abortController?: AbortController
|
||||||
|
sseStreamer?: IServerSideEventStreamer
|
||||||
|
chatId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IAgentFlowRuntime {
|
interface IAgentFlowRuntime {
|
||||||
|
|
@ -130,6 +133,7 @@ interface IExecuteNodeParams {
|
||||||
parentExecutionId?: string
|
parentExecutionId?: string
|
||||||
isRecursive?: boolean
|
isRecursive?: boolean
|
||||||
iterationContext?: ICommonObject
|
iterationContext?: ICommonObject
|
||||||
|
loopCounts?: Map<string, number>
|
||||||
orgId: string
|
orgId: string
|
||||||
workspaceId: string
|
workspaceId: string
|
||||||
subscriptionId: string
|
subscriptionId: string
|
||||||
|
|
@ -218,7 +222,8 @@ export const resolveVariables = async (
|
||||||
chatHistory: IMessage[],
|
chatHistory: IMessage[],
|
||||||
componentNodes: IComponentNodes,
|
componentNodes: IComponentNodes,
|
||||||
agentFlowExecutedData?: IAgentflowExecutedData[],
|
agentFlowExecutedData?: IAgentflowExecutedData[],
|
||||||
iterationContext?: ICommonObject
|
iterationContext?: ICommonObject,
|
||||||
|
loopCounts?: Map<string, number>
|
||||||
): Promise<INodeData> => {
|
): Promise<INodeData> => {
|
||||||
let flowNodeData = cloneDeep(reactFlowNodeData)
|
let flowNodeData = cloneDeep(reactFlowNodeData)
|
||||||
const types = 'inputs'
|
const types = 'inputs'
|
||||||
|
|
@ -285,6 +290,20 @@ export const resolveVariables = async (
|
||||||
resolvedValue = resolvedValue.replace(match, flowConfig?.runtimeChatHistoryLength ?? 0)
|
resolvedValue = resolvedValue.replace(match, flowConfig?.runtimeChatHistoryLength ?? 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (variableFullPath === LOOP_COUNT_VAR_PREFIX) {
|
||||||
|
// Get the current loop count from the most recent loopAgentflow node execution
|
||||||
|
let currentLoopCount = 0
|
||||||
|
if (loopCounts && agentFlowExecutedData) {
|
||||||
|
// Find the most recent loopAgentflow node execution to get its loop count
|
||||||
|
const loopNodes = [...agentFlowExecutedData].reverse().filter((data) => data.data?.name === 'loopAgentflow')
|
||||||
|
if (loopNodes.length > 0) {
|
||||||
|
const latestLoopNode = loopNodes[0]
|
||||||
|
currentLoopCount = loopCounts.get(latestLoopNode.nodeId) || 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resolvedValue = resolvedValue.replace(match, currentLoopCount.toString())
|
||||||
|
}
|
||||||
|
|
||||||
if (variableFullPath === CURRENT_DATE_TIME_VAR_PREFIX) {
|
if (variableFullPath === CURRENT_DATE_TIME_VAR_PREFIX) {
|
||||||
resolvedValue = resolvedValue.replace(match, new Date().toISOString())
|
resolvedValue = resolvedValue.replace(match, new Date().toISOString())
|
||||||
}
|
}
|
||||||
|
|
@ -742,7 +761,9 @@ async function processNodeOutputs({
|
||||||
edges,
|
edges,
|
||||||
nodeExecutionQueue,
|
nodeExecutionQueue,
|
||||||
waitingNodes,
|
waitingNodes,
|
||||||
loopCounts
|
loopCounts,
|
||||||
|
sseStreamer,
|
||||||
|
chatId
|
||||||
}: IProcessNodeOutputsParams): Promise<{ humanInput?: IHumanInput }> {
|
}: IProcessNodeOutputsParams): Promise<{ humanInput?: IHumanInput }> {
|
||||||
logger.debug(`\n🔄 Processing outputs from node: ${nodeId}`)
|
logger.debug(`\n🔄 Processing outputs from node: ${nodeId}`)
|
||||||
|
|
||||||
|
|
@ -823,6 +844,11 @@ async function processNodeOutputs({
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.debug(` ⚠️ Maximum loop count (${maxLoop}) reached, stopping loop`)
|
logger.debug(` ⚠️ Maximum loop count (${maxLoop}) reached, stopping loop`)
|
||||||
|
const fallbackMessage = result.output.fallbackMessage || `Loop completed after reaching maximum iteration count of ${maxLoop}.`
|
||||||
|
if (sseStreamer) {
|
||||||
|
sseStreamer.streamTokenEvent(chatId, fallbackMessage)
|
||||||
|
}
|
||||||
|
result.output = { ...result.output, content: fallbackMessage }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -967,6 +993,7 @@ const executeNode = async ({
|
||||||
isInternal,
|
isInternal,
|
||||||
isRecursive,
|
isRecursive,
|
||||||
iterationContext,
|
iterationContext,
|
||||||
|
loopCounts,
|
||||||
orgId,
|
orgId,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
subscriptionId,
|
subscriptionId,
|
||||||
|
|
@ -1045,7 +1072,8 @@ const executeNode = async ({
|
||||||
chatHistory,
|
chatHistory,
|
||||||
componentNodes,
|
componentNodes,
|
||||||
agentFlowExecutedData,
|
agentFlowExecutedData,
|
||||||
iterationContext
|
iterationContext,
|
||||||
|
loopCounts
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handle human input if present
|
// Handle human input if present
|
||||||
|
|
@ -1889,6 +1917,7 @@ export const executeAgentFlow = async ({
|
||||||
analyticHandlers,
|
analyticHandlers,
|
||||||
isRecursive,
|
isRecursive,
|
||||||
iterationContext,
|
iterationContext,
|
||||||
|
loopCounts,
|
||||||
orgId,
|
orgId,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
subscriptionId,
|
subscriptionId,
|
||||||
|
|
@ -1957,7 +1986,8 @@ export const executeAgentFlow = async ({
|
||||||
nodeExecutionQueue,
|
nodeExecutionQueue,
|
||||||
waitingNodes,
|
waitingNodes,
|
||||||
loopCounts,
|
loopCounts,
|
||||||
abortController
|
sseStreamer,
|
||||||
|
chatId
|
||||||
})
|
})
|
||||||
|
|
||||||
// Update humanInput if it was changed
|
// Update humanInput if it was changed
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ export const QUESTION_VAR_PREFIX = 'question'
|
||||||
export const FILE_ATTACHMENT_PREFIX = 'file_attachment'
|
export const FILE_ATTACHMENT_PREFIX = 'file_attachment'
|
||||||
export const CHAT_HISTORY_VAR_PREFIX = 'chat_history'
|
export const CHAT_HISTORY_VAR_PREFIX = 'chat_history'
|
||||||
export const RUNTIME_MESSAGES_LENGTH_VAR_PREFIX = 'runtime_messages_length'
|
export const RUNTIME_MESSAGES_LENGTH_VAR_PREFIX = 'runtime_messages_length'
|
||||||
|
export const LOOP_COUNT_VAR_PREFIX = 'loop_count'
|
||||||
export const CURRENT_DATE_TIME_VAR_PREFIX = 'current_date_time'
|
export const CURRENT_DATE_TIME_VAR_PREFIX = 'current_date_time'
|
||||||
export const REDACTED_CREDENTIAL_VALUE = '_FLOWISE_BLANK_07167752-1a71-43b1-bf8f-4f32252165db'
|
export const REDACTED_CREDENTIAL_VALUE = '_FLOWISE_BLANK_07167752-1a71-43b1-bf8f-4f32252165db'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,12 @@ export const suggestionOptions = (
|
||||||
description: 'Total messsages between LLM and Agent',
|
description: 'Total messsages between LLM and Agent',
|
||||||
category: 'Chat Context'
|
category: 'Chat Context'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'loop_count',
|
||||||
|
mentionLabel: 'loop_count',
|
||||||
|
description: 'Current loop count',
|
||||||
|
category: 'Chat Context'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
id: 'file_attachment',
|
id: 'file_attachment',
|
||||||
mentionLabel: 'file_attachment',
|
mentionLabel: 'file_attachment',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue