-
-
Notifications
You must be signed in to change notification settings - Fork 24.5k
feat: expose tool node artifacts to downstream nodes in Agentflow v2 #6440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| const { nodeClass: DirectReply_Agentflow } = require('./DirectReply') | ||
|
|
||
| describe('DirectReply_Agentflow', () => { | ||
| const createNode = () => new DirectReply_Agentflow() | ||
| const createOptions = () => ({ | ||
| agentflowRuntime: { state: { existing: 'state' } }, | ||
| chatId: 'chat-1', | ||
| isLastNode: true, | ||
| sseStreamer: { | ||
| streamTokenEvent: jest.fn(), | ||
| streamArtifactsEvent: jest.fn() | ||
| } | ||
| }) | ||
|
|
||
| it('returns text only when no artifacts are provided', async () => { | ||
| const node = createNode() | ||
| const options = createOptions() | ||
|
|
||
| const result = await node.run( | ||
| { | ||
| id: 'directReplyAgentflow_0', | ||
| inputs: { directReplyMessage: 'done' } | ||
| }, | ||
| '', | ||
| options | ||
| ) | ||
|
|
||
| expect(result.output).toEqual({ content: 'done' }) | ||
| expect(options.sseStreamer.streamTokenEvent).toHaveBeenCalledWith('chat-1', 'done') | ||
| expect(options.sseStreamer.streamArtifactsEvent).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('attaches resolved artifacts from the artifacts input', async () => { | ||
| const node = createNode() | ||
| const options = createOptions() | ||
| const artifacts = [ | ||
| { type: 'png', data: 'FILE-STORAGE::image.png' }, | ||
| { type: 'pdf', data: 'FILE-STORAGE::report.pdf' } | ||
| ] | ||
|
|
||
| const result = await node.run( | ||
| { | ||
| id: 'directReplyAgentflow_0', | ||
| inputs: { | ||
| directReplyMessage: 'generated files', | ||
| directReplyArtifacts: JSON.stringify(artifacts) | ||
| } | ||
| }, | ||
| '', | ||
| options | ||
| ) | ||
|
|
||
| expect(result.output).toEqual({ | ||
| content: 'generated files', | ||
| artifacts | ||
| }) | ||
| expect(options.sseStreamer.streamTokenEvent).toHaveBeenCalledWith('chat-1', 'generated files') | ||
| expect(options.sseStreamer.streamArtifactsEvent).toHaveBeenCalledWith('chat-1', artifacts) | ||
| }) | ||
|
|
||
| it('detects artifact-only messages without returning artifact JSON as content', async () => { | ||
| const node = createNode() | ||
| const options = createOptions() | ||
| const artifacts = [{ type: 'png', data: 'FILE-STORAGE::image.png' }] | ||
|
|
||
| const result = await node.run( | ||
| { | ||
| id: 'directReplyAgentflow_0', | ||
| inputs: { directReplyMessage: JSON.stringify(artifacts) } | ||
| }, | ||
| '', | ||
| options | ||
| ) | ||
|
|
||
| expect(result.output).toEqual({ | ||
| content: ' ', | ||
| artifacts | ||
| }) | ||
| expect(options.sseStreamer.streamTokenEvent).not.toHaveBeenCalled() | ||
| expect(options.sseStreamer.streamArtifactsEvent).toHaveBeenCalledWith('chat-1', artifacts) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,29 @@ | ||||||||||||||||||||||||||||||||
| import { ICommonObject, INode, INodeData, INodeParams, IServerSideEventStreamer } from '../../../src/Interface' | ||||||||||||||||||||||||||||||||
| import { flatten } from 'lodash' | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const parseArtifacts = (value: unknown): ICommonObject[] => { | ||||||||||||||||||||||||||||||||
| let artifacts = value | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (typeof artifacts === 'string') { | ||||||||||||||||||||||||||||||||
| const trimmedValue = artifacts.trim() | ||||||||||||||||||||||||||||||||
| if (!trimmedValue) return [] | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||
| artifacts = JSON.parse(trimmedValue) | ||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const flattenedArtifacts = Array.isArray(artifacts) ? flatten(artifacts) : [artifacts] | ||||||||||||||||||||||||||||||||
| return flattenedArtifacts.filter( | ||||||||||||||||||||||||||||||||
| (artifact): artifact is ICommonObject => | ||||||||||||||||||||||||||||||||
| typeof artifact === 'object' && | ||||||||||||||||||||||||||||||||
| artifact !== null && | ||||||||||||||||||||||||||||||||
| typeof (artifact as ICommonObject).type === 'string' && | ||||||||||||||||||||||||||||||||
| typeof (artifact as ICommonObject).data === 'string' | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| class DirectReply_Agentflow implements INode { | ||||||||||||||||||||||||||||||||
| label: string | ||||||||||||||||||||||||||||||||
|
|
@@ -32,30 +57,50 @@ class DirectReply_Agentflow implements INode { | |||||||||||||||||||||||||||||||
| name: 'directReplyMessage', | ||||||||||||||||||||||||||||||||
| type: 'string', | ||||||||||||||||||||||||||||||||
| rows: 4, | ||||||||||||||||||||||||||||||||
| acceptVariable: true | ||||||||||||||||||||||||||||||||
| acceptVariable: true, | ||||||||||||||||||||||||||||||||
| acceptNodeOutputAsVariable: true | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| label: 'Artifacts', | ||||||||||||||||||||||||||||||||
| name: 'directReplyArtifacts', | ||||||||||||||||||||||||||||||||
| type: 'string', | ||||||||||||||||||||||||||||||||
| optional: true, | ||||||||||||||||||||||||||||||||
| acceptVariable: true, | ||||||||||||||||||||||||||||||||
| acceptNodeOutputAsVariable: true | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| async run(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { | ||||||||||||||||||||||||||||||||
| const directReplyMessage = nodeData.inputs?.directReplyMessage as string | ||||||||||||||||||||||||||||||||
| const directReplyArtifacts = nodeData.inputs?.directReplyArtifacts | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const state = options.agentflowRuntime?.state as ICommonObject | ||||||||||||||||||||||||||||||||
| const chatId = options.chatId as string | ||||||||||||||||||||||||||||||||
| const isLastNode = options.isLastNode as boolean | ||||||||||||||||||||||||||||||||
| const isStreamable = isLastNode && options.sseStreamer !== undefined | ||||||||||||||||||||||||||||||||
| const artifacts = parseArtifacts(directReplyArtifacts) | ||||||||||||||||||||||||||||||||
| const messageArtifacts = artifacts.length > 0 ? [] : parseArtifacts(directReplyMessage) | ||||||||||||||||||||||||||||||||
| const resolvedArtifacts = artifacts.length > 0 ? artifacts : messageArtifacts | ||||||||||||||||||||||||||||||||
| const outputContent = messageArtifacts.length > 0 && directReplyMessage?.trim() ? ' ' : directReplyMessage | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (isStreamable) { | ||||||||||||||||||||||||||||||||
| const sseStreamer: IServerSideEventStreamer = options.sseStreamer | ||||||||||||||||||||||||||||||||
| sseStreamer.streamTokenEvent(chatId, directReplyMessage) | ||||||||||||||||||||||||||||||||
| if (outputContent?.trim()) { | ||||||||||||||||||||||||||||||||
| sseStreamer.streamTokenEvent(chatId, outputContent) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+85
to
+91
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling We can make this safer and simpler by:
Suggested change
|
||||||||||||||||||||||||||||||||
| if (resolvedArtifacts.length > 0) { | ||||||||||||||||||||||||||||||||
| sseStreamer.streamArtifactsEvent(chatId, resolvedArtifacts) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const returnOutput = { | ||||||||||||||||||||||||||||||||
| id: nodeData.id, | ||||||||||||||||||||||||||||||||
| name: this.name, | ||||||||||||||||||||||||||||||||
| input: {}, | ||||||||||||||||||||||||||||||||
| output: { | ||||||||||||||||||||||||||||||||
| content: directReplyMessage | ||||||||||||||||||||||||||||||||
| content: outputContent, | ||||||||||||||||||||||||||||||||
| ...(resolvedArtifacts.length > 0 && { artifacts: resolvedArtifacts }) | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| state | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid throwing and catching exceptions on every plain text message (which is the most common case for
directReplyMessage), we can add a quick heuristic check to see if the string looks like JSON (e.g., starts with[or{) before callingJSON.parse. This improves performance and prevents debugger disruption when 'Pause on caught exceptions' is enabled. This heuristic-based approach is acceptable here as the risk of false positives is very low.References