-
Notifications
You must be signed in to change notification settings - Fork 160
fix: normalize trailing assistant messages #222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,7 +197,17 @@ export function transformBody( | |
| if (typeof firstUser.content === "string") { | ||
| firstUser.content = prefix + "\n\n" + firstUser.content | ||
| } else if (Array.isArray(firstUser.content)) { | ||
| firstUser.content.unshift({ type: "text", text: prefix }) | ||
| const insertAt = firstUser.content.findIndex( | ||
| (block) => block.type !== "tool_result", | ||
| ) | ||
| firstUser.content.splice( | ||
| insertAt === -1 ? firstUser.content.length : insertAt, | ||
| 0, | ||
| { | ||
| type: "text", | ||
| text: prefix, | ||
| }, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -253,6 +263,19 @@ export function transformBody( | |
|
|
||
| if (Array.isArray(parsed.messages)) { | ||
| parsed.messages = repairToolPairs(parsed.messages) | ||
|
|
||
| const lastMessage = parsed.messages.at(-1) | ||
| if (lastMessage?.role === "assistant") { | ||
| parsed.messages.push({ | ||
| role: "user", | ||
| content: [ | ||
| { | ||
| type: "text", | ||
| text: "Please pause and wait for further instructions.", | ||
| }, | ||
| ], | ||
| }) | ||
| } | ||
|
Comment on lines
+267
to
+278
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
After Prompt To Fix With AIThis is a comment left during a code review.
Path: src/transforms.ts
Line: 267-278
Comment:
**Trailing-assistant guard does not check for active `tool_use` blocks**
After `repairToolPairs`, a last assistant message can still contain `tool_use` blocks if those blocks are considered non-orphaned (i.e. there happens to be a matching `tool_result` earlier in the (already broken) conversation). In that case the code appends a plain-text user turn, producing a conversation where the `tool_use` call is never answered by a `tool_result` — a structure the API rejects with a different error than the one this PR targets. Adding a check that the last assistant message contains no `tool_use` content before appending the synthetic turn would make the guard more defensive against unusual compaction outputs.
How can I resolve this? If you propose a fix, please make it concise. |
||
| } | ||
|
|
||
| return JSON.stringify(parsed) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Opus 4.7 test (above) verifies the exact shape of the appended content block with
deepEqual, but this model-agnostic variant only checksmessages.lengthandmessages[2].role. Adding adeepEqualonmessages[2].contenthere would confirm the injected text is consistent across models and guard against a future regression where the content accidentally diverges per model.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!