opencode-anthropic-fix 0.0.29 → 0.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +36 -3
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -2145,9 +2145,29 @@ export async function AnthropicAuthPlugin({ client, project, directory, worktree
|
|
|
2145
2145
|
try {
|
|
2146
2146
|
const parsedBody = JSON.parse(body);
|
|
2147
2147
|
if (Array.isArray(parsedBody.messages) && parsedBody.messages.length > 4) {
|
|
2148
|
-
// Keep first 2 messages (initial context) and last
|
|
2148
|
+
// Keep first 2 messages (initial context) and last N messages (recent work).
|
|
2149
|
+
// Ensure the trimmed array never ends with an assistant message (prefill),
|
|
2150
|
+
// which would cause "does not support assistant message prefill" errors.
|
|
2151
|
+
let tailCount = 2;
|
|
2152
|
+
const msgs = parsedBody.messages;
|
|
2153
|
+
// Expand tail if it would end with an assistant message
|
|
2154
|
+
while (
|
|
2155
|
+
tailCount < msgs.length - 2 &&
|
|
2156
|
+
msgs[msgs.length - tailCount]?.role !== "user" &&
|
|
2157
|
+
msgs.length - tailCount > 0
|
|
2158
|
+
) {
|
|
2159
|
+
tailCount++;
|
|
2160
|
+
}
|
|
2161
|
+
const tail = msgs.slice(-tailCount);
|
|
2162
|
+
// If tail still starts/ends badly, force a user wrapper
|
|
2163
|
+
if (tail.length > 0 && tail[tail.length - 1]?.role === "assistant") {
|
|
2164
|
+
tail.push({
|
|
2165
|
+
role: "user",
|
|
2166
|
+
content: [{ type: "text", text: "Continue." }],
|
|
2167
|
+
});
|
|
2168
|
+
}
|
|
2149
2169
|
const trimmed = [
|
|
2150
|
-
...
|
|
2170
|
+
...msgs.slice(0, 2),
|
|
2151
2171
|
{
|
|
2152
2172
|
role: "user",
|
|
2153
2173
|
content: [
|
|
@@ -2157,7 +2177,7 @@ export async function AnthropicAuthPlugin({ client, project, directory, worktree
|
|
|
2157
2177
|
},
|
|
2158
2178
|
],
|
|
2159
2179
|
},
|
|
2160
|
-
...
|
|
2180
|
+
...tail,
|
|
2161
2181
|
];
|
|
2162
2182
|
parsedBody.messages = trimmed;
|
|
2163
2183
|
requestInit.body = JSON.stringify(parsedBody);
|
|
@@ -4091,6 +4111,19 @@ function transformRequestBody(body, signature, runtime, betaHeader) {
|
|
|
4091
4111
|
if (fastModeEnabled && parsed.model && (isOpus46Model(parsed.model) || isSonnet46Model(parsed.model))) {
|
|
4092
4112
|
parsed.speed = "fast";
|
|
4093
4113
|
}
|
|
4114
|
+
|
|
4115
|
+
// Guard: ensure messages array never ends with an assistant message.
|
|
4116
|
+
// The API rejects this as "assistant message prefill" when extended thinking is active.
|
|
4117
|
+
if (Array.isArray(parsed.messages) && parsed.messages.length > 0) {
|
|
4118
|
+
const lastMsg = parsed.messages[parsed.messages.length - 1];
|
|
4119
|
+
if (lastMsg && lastMsg.role === "assistant") {
|
|
4120
|
+
parsed.messages.push({
|
|
4121
|
+
role: "user",
|
|
4122
|
+
content: [{ type: "text", text: "Continue." }],
|
|
4123
|
+
});
|
|
4124
|
+
}
|
|
4125
|
+
}
|
|
4126
|
+
|
|
4094
4127
|
return JSON.stringify(parsed);
|
|
4095
4128
|
} catch {
|
|
4096
4129
|
// ignore parse errors
|