Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/components/nodes/agentflow/Agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,32 @@ class Agent_Agentflow implements INode {
response.tool_calls.splice(response.tool_calls.indexOf(toolCall), 1)
}

// Ensure raw OpenAI-compatible tool_calls are preserved for providers
// that require provider-specific fields (e.g. thought_signature) on follow-up turns.
if (response.tool_calls?.length) {
const existingRawToolCalls = (response.additional_kwargs as any)?.tool_calls
const mergedRawToolCalls = response.tool_calls.map((toolCall: any) => {
const existing = Array.isArray(existingRawToolCalls)
? existingRawToolCalls.find((raw: any) => raw?.id && toolCall?.id && raw.id === toolCall.id)
: undefined
if (existing) return existing
return {
id: toolCall.id,
type: 'function',
function: {
name: toolCall.name,
arguments: JSON.stringify(toolCall.args ?? {}),
...(toolCall?.thought_signature ? { thought_signature: toolCall.thought_signature } : {}),
...(toolCall?.thoughtSignature ? { thought_signature: toolCall.thoughtSignature } : {})
}
}
})
response.additional_kwargs = {
...(response.additional_kwargs ?? {}),
tool_calls: mergedRawToolCalls
}
}
Comment on lines +2230 to +2254
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for preserving and rebuilding raw tool_calls metadata is duplicated here and in handleResumedToolCalls (lines 2637-2661). This repetition makes the code harder to maintain and increases the risk of bugs if the logic needs to be updated in the future.

Consider refactoring this into a private helper method within the Agent_Agentflow class to centralize the logic for OpenAI-compatible tool call preservation.


// Add LLM response with tool calls to messages
messages.push(response)

Expand Down Expand Up @@ -2608,6 +2634,32 @@ class Agent_Agentflow implements INode {
response.tool_calls.splice(response.tool_calls.indexOf(toolCall), 1)
}

// Ensure raw OpenAI-compatible tool_calls are preserved for providers
// that require provider-specific fields (e.g. thought_signature) on follow-up turns.
if (response.tool_calls?.length) {
const existingRawToolCalls = (response.additional_kwargs as any)?.tool_calls
const mergedRawToolCalls = response.tool_calls.map((toolCall: any) => {
const existing = Array.isArray(existingRawToolCalls)
? existingRawToolCalls.find((raw: any) => raw?.id && toolCall?.id && raw.id === toolCall.id)
: undefined
if (existing) return existing
return {
id: toolCall.id,
type: 'function',
function: {
name: toolCall.name,
arguments: JSON.stringify(toolCall.args ?? {}),
...(toolCall?.thought_signature ? { thought_signature: toolCall.thought_signature } : {}),
...(toolCall?.thoughtSignature ? { thought_signature: toolCall.thoughtSignature } : {})
}
}
})
response.additional_kwargs = {
...(response.additional_kwargs ?? {}),
tool_calls: mergedRawToolCalls
}
}

// Add LLM response with tool calls to messages
messages.push(response)

Expand Down