Last active
January 11, 2026 12:49
-
-
Save zsnmwy/4e9bf0dfaf3dc658a4df56b3986c8f49 to your computer and use it in GitHub Desktop.
File Location ~/.config/opencode/plugin/codex-proxy-plugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { Plugin } from '@opencode-ai/plugin'; | |
| /** | |
| * Custom fetch wrapper that removes max_output_tokens and max_completion_tokens | |
| * from the request body before sending to the API. | |
| * | |
| * @param input - The request URL or Request object | |
| * @param init - Optional request init options | |
| * @returns The fetch response | |
| */ | |
| async function codexFetch(input: Request | string | URL, init?: RequestInit): Promise<Response> { | |
| if (!init?.body || typeof init.body !== 'string') { | |
| return globalThis.fetch(input, init); | |
| } | |
| try { | |
| const originalBody = JSON.parse(init.body); | |
| // Remove token limit parameters that may cause issues | |
| delete originalBody.max_output_tokens; | |
| delete originalBody.max_completion_tokens; | |
| const modifiedInit: RequestInit = { | |
| ...init, | |
| body: JSON.stringify(originalBody), | |
| }; | |
| return globalThis.fetch(input, modifiedInit); | |
| } catch { | |
| // If JSON parsing fails, proceed with original request | |
| return globalThis.fetch(input, init); | |
| } | |
| } | |
| /** | |
| * Codex Proxy Plugin | |
| * | |
| * Intercepts requests to OpenCode providers and applies custom fetch handling | |
| * to modify request parameters before they are sent to the API. | |
| */ | |
| export const CodexProxyPlugin: Plugin = async () => { | |
| return { | |
| config: async (input) => { | |
| // const provider = input.provider?.['openai-duckcoding']; | |
| const provider = input.provider?.['openai']; | |
| if (provider) { | |
| if (!provider.options) { | |
| provider.options = {}; | |
| } | |
| provider.options.fetch = codexFetch; | |
| } | |
| }, | |
| }; | |
| }; | |
| export default CodexProxyPlugin; |
Author
zsnmwy
commented
Jan 11, 2026
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment