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,10 +335,7 @@ export class Agent {
? firstAttemptError.message ? firstAttemptError.message
: String(firstAttemptError) : String(firstAttemptError)
const retryMessages: LLMMessage[] = [ const errorFeedbackMessage: LLMMessage = {
...originalMessages,
...result.messages,
{
role: 'user' as const, role: 'user' as const,
content: [{ content: [{
type: 'text' as const, type: 'text' as const,
@ -350,14 +347,21 @@ export class Agent {
'Please try again. Respond with ONLY valid JSON, no other text.', 'Please try again. Respond with ONLY valid JSON, no other text.',
].join('\n'), ].join('\n'),
}], }],
}, }
const retryMessages: LLMMessage[] = [
...originalMessages,
...result.messages,
errorFeedbackMessage,
] ]
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 {

View File

@ -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,
}) })
} }