Skip to content

Commit 0527333

Browse files
fix: preserve sourceDocuments and metadata in ExecuteFlow node (#5972)
* fix: preserve sourceDocuments and metadata in ExecuteFlow node The Execute Flow node in AgentFlow V2 was only extracting the text content from chatflow responses, discarding sourceDocuments, usedTools, artifacts, and fileAnnotations metadata. This fix preserves all metadata fields in the return output and streams them via SSE when the node is the last in the flow. Fixes #5949 Signed-off-by: majiayu000 <1835304752@qq.com> * fix: address review feedback on PR #5972 - Use object destructuring for response.data metadata extraction - Extend SSE streaming test to cover artifacts and fileAnnotations Signed-off-by: majiayu000 <1835304752@qq.com> * Delete packages/components/nodes/agentflow/ExecuteFlow/ExecuteFlow.test.ts * Update ExecuteFlow.ts * Update ExecuteFlow.ts --------- Signed-off-by: majiayu000 <1835304752@qq.com> Co-authored-by: Henry Heng <henryheng@flowiseai.com>
1 parent b93ccf3 commit 0527333

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

packages/components/nodes/agentflow/ExecuteFlow/ExecuteFlow.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getCredentialData, getCredentialParam, processTemplateVariables, parseJ
1313
import { DataSource } from 'typeorm'
1414
import { BaseMessageLike } from '@langchain/core/messages'
1515
import { updateFlowState } from '../utils'
16+
import { flatten } from 'lodash'
1617

1718
class ExecuteFlow_Agentflow implements INode {
1819
label: string
@@ -205,12 +206,28 @@ class ExecuteFlow_Agentflow implements INode {
205206
const response = await secureAxiosRequest(requestConfig)
206207

207208
let resultText = ''
209+
const { sourceDocuments, usedTools, artifacts, fileAnnotations } = response.data
210+
const flattenedSourceDocuments = Array.isArray(sourceDocuments) ? flatten(sourceDocuments) : []
211+
const flattenedUsedTools = Array.isArray(usedTools) ? flatten(usedTools) : []
212+
const flattenedArtifacts = Array.isArray(artifacts) ? flatten(artifacts) : []
208213
if (response.data.text) resultText = response.data.text
209214
else if (response.data.json) resultText = '```json\n' + JSON.stringify(response.data.json, null, 2)
210215
else resultText = JSON.stringify(response.data, null, 2)
211216

212217
if (isLastNode && sseStreamer) {
213218
sseStreamer.streamTokenEvent(options.chatId, resultText)
219+
if (flattenedSourceDocuments.length > 0) {
220+
sseStreamer.streamSourceDocumentsEvent(options.chatId, flattenedSourceDocuments)
221+
}
222+
if (flattenedUsedTools.length > 0) {
223+
sseStreamer.streamUsedToolsEvent(options.chatId, flattenedUsedTools)
224+
}
225+
if (flattenedArtifacts.length > 0) {
226+
sseStreamer.streamArtifactsEvent(options.chatId, flattenedArtifacts)
227+
}
228+
if (fileAnnotations) {
229+
sseStreamer.streamFileAnnotationsEvent(options.chatId, fileAnnotations)
230+
}
214231
}
215232

216233
// Update flow state if needed
@@ -245,15 +262,28 @@ class ExecuteFlow_Agentflow implements INode {
245262
]
246263
},
247264
output: {
248-
content: resultText
265+
content: resultText,
266+
...(flattenedSourceDocuments.length > 0 && { sourceDocuments: flattenedSourceDocuments }),
267+
...(flattenedUsedTools.length > 0 && { usedTools: flattenedUsedTools }),
268+
...(flattenedArtifacts.length > 0 && { artifacts: flattenedArtifacts }),
269+
...(fileAnnotations && fileAnnotations.length > 0 && { fileAnnotations })
249270
},
250271
state: newState,
251272
chatHistory: [
252273
...inputMessages,
253274
{
254275
role: returnRole,
255276
content: resultText,
256-
name: nodeData?.label ? nodeData?.label.toLowerCase().replace(/\s/g, '_').trim() : nodeData?.id
277+
name: nodeData?.label ? nodeData?.label.toLowerCase().replace(/\s/g, '_').trim() : nodeData?.id,
278+
...((flattenedArtifacts.length > 0 ||
279+
(fileAnnotations && fileAnnotations.length > 0) ||
280+
flattenedUsedTools.length > 0) && {
281+
additional_kwargs: {
282+
...(flattenedArtifacts.length > 0 && { artifacts: flattenedArtifacts }),
283+
...(fileAnnotations && fileAnnotations.length > 0 && { fileAnnotations }),
284+
...(flattenedUsedTools.length > 0 && { usedTools: flattenedUsedTools })
285+
}
286+
})
257287
}
258288
]
259289
}

0 commit comments

Comments
 (0)