* Loader function that configures OAuth authentication and request handling * * This function: * 1. Validates OAuth authentication * 2. Extracts ChatGPT account ID from access token * 3. Loads user configuration from opencode.json * 4. Fetches Codex system instructions from G
(getAuth: () => Promise<Auth.Info>, provider: unknown)
| 92 | * @returns SDK configuration object or empty object for non-OAuth auth |
| 93 | */ |
| 94 | async loader(getAuth: () => Promise<Auth.Info>, provider: unknown) { |
| 95 | const auth = await getAuth(); |
| 96 | |
| 97 | // Support both standard oauth and internal codex auth types |
| 98 | if (auth.type !== "oauth" && auth.type !== "codex") { |
| 99 | return {}; |
| 100 | } |
| 101 | |
| 102 | // Normalize token access (codex uses accessToken, oauth uses access) |
| 103 | const accessToken = (auth as any).accessToken ?? (auth as any).access; |
| 104 | |
| 105 | // Extract ChatGPT account ID from JWT access token |
| 106 | const decoded = decodeJWT(accessToken); |
| 107 | const accountId = decoded?.[JWT_CLAIM_PATH]?.chatgpt_account_id; |
| 108 | |
| 109 | if (!accountId) { |
| 110 | console.error(`[${PLUGIN_NAME}] ${ERROR_MESSAGES.NO_ACCOUNT_ID}`); |
| 111 | return {}; |
| 112 | } |
| 113 | // Extract user configuration (global + per-model options) |
| 114 | const providerConfig = provider as |
| 115 | | { options?: Record<string, unknown>; models?: UserConfig["models"] } |
| 116 | | undefined; |
| 117 | const userConfig: UserConfig = { |
| 118 | global: providerConfig?.options || {}, |
| 119 | models: providerConfig?.models || {}, |
| 120 | }; |
| 121 | |
| 122 | // Load plugin configuration and determine CODEX_MODE |
| 123 | // Priority: CODEX_MODE env var > config file > default (true) |
| 124 | const pluginConfig = loadPluginConfig(); |
| 125 | const codexMode = getCodexMode(pluginConfig); |
| 126 | |
| 127 | // Return SDK configuration |
| 128 | return { |
| 129 | apiKey: DUMMY_API_KEY, |
| 130 | baseURL: CODEX_BASE_URL, |
| 131 | /** |
| 132 | * Custom fetch implementation for Codex API |
| 133 | * |
| 134 | * Handles: |
| 135 | * - Token refresh when expired |
| 136 | * - URL rewriting for Codex backend |
| 137 | * - Request body transformation |
| 138 | * - OAuth header injection |
| 139 | * - SSE to JSON conversion for non-tool requests |
| 140 | * - Error handling and logging |
| 141 | * |
| 142 | * @param input - Request URL or Request object |
| 143 | * @param init - Request options |
| 144 | * @returns Response from Codex API |
| 145 | */ |
| 146 | async fetch( |
| 147 | input: Request | string | URL, |
| 148 | init?: RequestInit, |
| 149 | ): Promise<Response> { |
| 150 | // Step 1: Check and refresh token if needed |
| 151 | let currentAuth = await getAuth(); |
nothing calls this directly
no test coverage detected