- Include error feedback user turn in mergedMessages to maintain alternating user/assistant roles required by Anthropic API - Use explicit undefined check instead of ?? for structured merge to preserve null as a valid structured output value
This commit is contained in:
parent
fbc5546fa1
commit
99b028dc1d
|
|
@ -335,29 +335,33 @@ export class Agent {
|
||||||
? firstAttemptError.message
|
? firstAttemptError.message
|
||||||
: String(firstAttemptError)
|
: String(firstAttemptError)
|
||||||
|
|
||||||
|
const errorFeedbackMessage: LLMMessage = {
|
||||||
|
role: 'user' as const,
|
||||||
|
content: [{
|
||||||
|
type: 'text' as const,
|
||||||
|
text: [
|
||||||
|
'Your previous response did not produce valid JSON matching the required schema.',
|
||||||
|
'',
|
||||||
|
`Error: ${errorMsg}`,
|
||||||
|
'',
|
||||||
|
'Please try again. Respond with ONLY valid JSON, no other text.',
|
||||||
|
].join('\n'),
|
||||||
|
}],
|
||||||
|
}
|
||||||
|
|
||||||
const retryMessages: LLMMessage[] = [
|
const retryMessages: LLMMessage[] = [
|
||||||
...originalMessages,
|
...originalMessages,
|
||||||
...result.messages,
|
...result.messages,
|
||||||
{
|
errorFeedbackMessage,
|
||||||
role: 'user' as const,
|
|
||||||
content: [{
|
|
||||||
type: 'text' as const,
|
|
||||||
text: [
|
|
||||||
'Your previous response did not produce valid JSON matching the required schema.',
|
|
||||||
'',
|
|
||||||
`Error: ${errorMsg}`,
|
|
||||||
'',
|
|
||||||
'Please try again. Respond with ONLY valid JSON, no other text.',
|
|
||||||
].join('\n'),
|
|
||||||
}],
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
const retryResult = await runner.run(retryMessages, runOptions)
|
const retryResult = await runner.run(retryMessages, runOptions)
|
||||||
this.state.tokenUsage = addUsage(this.state.tokenUsage, retryResult.tokenUsage)
|
this.state.tokenUsage = addUsage(this.state.tokenUsage, retryResult.tokenUsage)
|
||||||
|
|
||||||
const mergedTokenUsage = addUsage(result.tokenUsage, retryResult.tokenUsage)
|
const mergedTokenUsage = addUsage(result.tokenUsage, retryResult.tokenUsage)
|
||||||
const mergedMessages = [...result.messages, ...retryResult.messages]
|
// Include the error feedback turn to maintain alternating user/assistant roles,
|
||||||
|
// which is required by Anthropic's API for subsequent prompt() calls.
|
||||||
|
const mergedMessages = [...result.messages, errorFeedbackMessage, ...retryResult.messages]
|
||||||
const mergedToolCalls = [...result.toolCalls, ...retryResult.toolCalls]
|
const mergedToolCalls = [...result.toolCalls, ...retryResult.toolCalls]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -845,7 +845,7 @@ export class OpenMultiAgent {
|
||||||
messages: [...existing.messages, ...result.messages],
|
messages: [...existing.messages, ...result.messages],
|
||||||
tokenUsage: addUsage(existing.tokenUsage, result.tokenUsage),
|
tokenUsage: addUsage(existing.tokenUsage, result.tokenUsage),
|
||||||
toolCalls: [...existing.toolCalls, ...result.toolCalls],
|
toolCalls: [...existing.toolCalls, ...result.toolCalls],
|
||||||
structured: result.structured ?? existing.structured,
|
structured: result.structured !== undefined ? result.structured : existing.structured,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue