fix(Agent Node): Thinking model issue - undefined .map error (#22046)

This commit is contained in:
Michael Drury 2025-11-21 09:01:25 +00:00 committed by GitHub
parent 69e092a263
commit 231e4eff57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 6 deletions

View File

@ -206,18 +206,35 @@ export function handleAgentFinishOutput(
if (agentFinishSteps.returnValues) {
const isMultiOutput = Array.isArray(agentFinishSteps.returnValues?.output);
if (isMultiOutput) {
// If all items in the multi-output array are of type 'text', merge them into a single string
const multiOutputSteps = agentFinishSteps.returnValues.output as Array<{
index: number;
type: string;
text: string;
text?: string;
thinking?: string;
}>;
const isTextOnly = multiOutputSteps.every((output) => 'text' in output);
if (isTextOnly) {
agentFinishSteps.returnValues.output = multiOutputSteps
.map((output) => output.text)
// Filter out thinking blocks and join text blocks
const textOutputs = multiOutputSteps
.filter((output) => output.type === 'text' && output.text)
.map((output) => output.text)
.join('\n')
.trim();
if (textOutputs) {
agentFinishSteps.returnValues.output = textOutputs;
} else {
const thinkingOutputs = multiOutputSteps
.filter((output) => output.type === 'thinking' && output.thinking)
.map((output) => output.thinking)
.join('\n')
.trim();
if (thinkingOutputs) {
agentFinishSteps.returnValues.output = thinkingOutputs;
} else {
// no output was found
agentFinishSteps.returnValues.output = '';
}
}
return agentFinishSteps;
}

View File

@ -831,4 +831,49 @@ describe('handleAgentFinishOutput', () => {
expect(result).toEqual(steps);
});
it('should filter out thinking blocks and return only text blocks', () => {
const steps: AgentFinish = {
returnValues: {
output: [
{ index: 0, type: 'thinking', thinking: 'Internal reasoning...' },
{ index: 1, type: 'text', text: 'User-facing output' },
],
},
log: '',
};
const result = handleAgentFinishOutput(steps) as AgentFinish;
expect(result.returnValues.output).toBe('User-facing output');
});
it('should return thinking content when no text blocks exist', () => {
const steps: AgentFinish = {
returnValues: {
output: [
{ index: 0, type: 'thinking', thinking: 'Only thinking content' },
{ index: 1, type: 'thinking', thinking: 'More thinking' },
],
},
log: '',
};
const result = handleAgentFinishOutput(steps) as AgentFinish;
expect(result.returnValues.output).toBe('Only thinking content\nMore thinking');
});
it('should return empty string when no text or thinking blocks exist', () => {
const steps: AgentFinish = {
returnValues: {
output: [{ index: 0, type: 'unknown' }],
},
log: '',
};
const result = handleAgentFinishOutput(steps) as AgentFinish;
expect(result.returnValues.output).toBe('');
});
});