fix: address Codex review for structured output (#36) (#38)

- 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:
JackChen 2026-04-03 14:08:27 +08:00 committed by GitHub
parent fbc5546fa1
commit 99b028dc1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 15 deletions

View File

@ -335,29 +335,33 @@ export class Agent {
? firstAttemptError.message
: 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[] = [
...originalMessages,
...result.messages,
{
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'),
}],
},
errorFeedbackMessage,
]
const retryResult = await runner.run(retryMessages, runOptions)
this.state.tokenUsage = addUsage(this.state.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]
try {

View File

@ -845,7 +845,7 @@ export class OpenMultiAgent {
messages: [...existing.messages, ...result.messages],
tokenUsage: addUsage(existing.tokenUsage, result.tokenUsage),
toolCalls: [...existing.toolCalls, ...result.toolCalls],
structured: result.structured ?? existing.structured,
structured: result.structured !== undefined ? result.structured : existing.structured,
})
}