fix: max completion tokens error for cloudflare (#7970)
This commit is contained in:
@@ -419,11 +419,26 @@ export namespace Provider {
|
|||||||
"HTTP-Referer": "https://opencode.ai/",
|
"HTTP-Referer": "https://opencode.ai/",
|
||||||
"X-Title": "opencode",
|
"X-Title": "opencode",
|
||||||
},
|
},
|
||||||
// Custom fetch to strip Authorization header - AI Gateway uses cf-aig-authorization instead
|
// Custom fetch to handle parameter transformation and auth
|
||||||
// Sending Authorization header with invalid value causes auth errors
|
|
||||||
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
|
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
const headers = new Headers(init?.headers)
|
const headers = new Headers(init?.headers)
|
||||||
|
// Strip Authorization header - AI Gateway uses cf-aig-authorization instead
|
||||||
headers.delete("Authorization")
|
headers.delete("Authorization")
|
||||||
|
|
||||||
|
// Transform max_tokens to max_completion_tokens for newer models
|
||||||
|
if (init?.body && init.method === "POST") {
|
||||||
|
try {
|
||||||
|
const body = JSON.parse(init.body as string)
|
||||||
|
if (body.max_tokens !== undefined && !body.max_completion_tokens) {
|
||||||
|
body.max_completion_tokens = body.max_tokens
|
||||||
|
delete body.max_tokens
|
||||||
|
init = { ...init, body: JSON.stringify(body) }
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// If body parsing fails, continue with original request
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return fetch(input, { ...init, headers })
|
return fetch(input, { ...init, headers })
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user