GeezerVison is showing github.com — fix: max completion tokens error for cloudflare (#7970) · coty/opencode@20088a8
File tree
Expand file treeCollapse file tree
Open diff view settings
packages/opencode/src/provider
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: packages/opencode/src/provider/provider.ts +17-2Lines changed: 17 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -419,11 +419,26 @@ export namespace Provider { |
419 | 419 | "HTTP-Referer": "https://opencode.ai/", |
420 | 420 | "X-Title": "opencode", |
421 | 421 | }, |
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 |
424 | 423 | fetch: async (input: RequestInfo | URL, init?: RequestInit) => { |
425 | 424 | const headers = new Headers(init?.headers) |
| 425 | + // Strip Authorization header - AI Gateway uses cf-aig-authorization instead |
426 | 426 | 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 | + |
427 | 442 | return fetch(input, { ...init, headers }) |
428 | 443 | }, |
429 | 444 | }, |
|