Skip to content

Commit 6026a00

Browse files
roerohansauerdaniel
authored andcommitted
fix: max completion tokens error for cloudflare (anomalyco#7970)
1 parent aff4441 commit 6026a00

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

packages/opencode/src/provider/provider.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,26 @@ export namespace Provider {
419419
"HTTP-Referer": "https://opencode.ai/",
420420
"X-Title": "opencode",
421421
},
422-
// Custom fetch to strip Authorization header - AI Gateway uses cf-aig-authorization instead
423-
// Sending Authorization header with invalid value causes auth errors
422+
// Custom fetch to handle parameter transformation and auth
424423
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
425424
const headers = new Headers(init?.headers)
425+
// Strip Authorization header - AI Gateway uses cf-aig-authorization instead
426426
headers.delete("Authorization")
427+
428+
// Transform max_tokens to max_completion_tokens for newer models
429+
if (init?.body && init.method === "POST") {
430+
try {
431+
const body = JSON.parse(init.body as string)
432+
if (body.max_tokens !== undefined && !body.max_completion_tokens) {
433+
body.max_completion_tokens = body.max_tokens
434+
delete body.max_tokens
435+
init = { ...init, body: JSON.stringify(body) }
436+
}
437+
} catch (e) {
438+
// If body parsing fails, continue with original request
439+
}
440+
}
441+
427442
return fetch(input, { ...init, headers })
428443
},
429444
},

0 commit comments

Comments
 (0)