better-opencode-openai-codex-auth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +37 -0
- package/README.md +99 -0
- package/assets/opencode-logo-ornate-dark.svg +18 -0
- package/assets/readme-hero.svg +31 -0
- package/config/README.md +103 -0
- package/config/minimal-opencode.json +12 -0
- package/config/opencode-legacy.json +571 -0
- package/config/opencode-modern.json +239 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +334 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/account-pool.d.ts +17 -0
- package/dist/lib/account-pool.d.ts.map +1 -0
- package/dist/lib/account-pool.js +243 -0
- package/dist/lib/account-pool.js.map +1 -0
- package/dist/lib/auth/auth.d.ts +43 -0
- package/dist/lib/auth/auth.d.ts.map +1 -0
- package/dist/lib/auth/auth.js +162 -0
- package/dist/lib/auth/auth.js.map +1 -0
- package/dist/lib/auth/browser.d.ts +17 -0
- package/dist/lib/auth/browser.d.ts.map +1 -0
- package/dist/lib/auth/browser.js +76 -0
- package/dist/lib/auth/browser.js.map +1 -0
- package/dist/lib/auth/server.d.ts +10 -0
- package/dist/lib/auth/server.d.ts.map +1 -0
- package/dist/lib/auth/server.js +78 -0
- package/dist/lib/auth/server.js.map +1 -0
- package/dist/lib/config.d.ts +17 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +53 -0
- package/dist/lib/config.js.map +1 -0
- package/dist/lib/constants.d.ts +67 -0
- package/dist/lib/constants.d.ts.map +1 -0
- package/dist/lib/constants.js +67 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/logger.d.ts +21 -0
- package/dist/lib/logger.d.ts.map +1 -0
- package/dist/lib/logger.js +77 -0
- package/dist/lib/logger.js.map +1 -0
- package/dist/lib/oauth-success.html +712 -0
- package/dist/lib/prompts/codex-opencode-bridge.d.ts +19 -0
- package/dist/lib/prompts/codex-opencode-bridge.d.ts.map +1 -0
- package/dist/lib/prompts/codex-opencode-bridge.js +152 -0
- package/dist/lib/prompts/codex-opencode-bridge.js.map +1 -0
- package/dist/lib/prompts/codex.d.ts +27 -0
- package/dist/lib/prompts/codex.d.ts.map +1 -0
- package/dist/lib/prompts/codex.js +252 -0
- package/dist/lib/prompts/codex.js.map +1 -0
- package/dist/lib/prompts/opencode-codex.d.ts +21 -0
- package/dist/lib/prompts/opencode-codex.d.ts.map +1 -0
- package/dist/lib/prompts/opencode-codex.js +91 -0
- package/dist/lib/prompts/opencode-codex.js.map +1 -0
- package/dist/lib/request/fetch-helpers.d.ts +73 -0
- package/dist/lib/request/fetch-helpers.d.ts.map +1 -0
- package/dist/lib/request/fetch-helpers.js +221 -0
- package/dist/lib/request/fetch-helpers.js.map +1 -0
- package/dist/lib/request/helpers/input-utils.d.ts +6 -0
- package/dist/lib/request/helpers/input-utils.d.ts.map +1 -0
- package/dist/lib/request/helpers/input-utils.js +174 -0
- package/dist/lib/request/helpers/input-utils.js.map +1 -0
- package/dist/lib/request/helpers/model-map.d.ts +28 -0
- package/dist/lib/request/helpers/model-map.d.ts.map +1 -0
- package/dist/lib/request/helpers/model-map.js +126 -0
- package/dist/lib/request/helpers/model-map.js.map +1 -0
- package/dist/lib/request/request-transformer.d.ts +93 -0
- package/dist/lib/request/request-transformer.d.ts.map +1 -0
- package/dist/lib/request/request-transformer.js +418 -0
- package/dist/lib/request/request-transformer.js.map +1 -0
- package/dist/lib/request/response-handler.d.ts +14 -0
- package/dist/lib/request/response-handler.d.ts.map +1 -0
- package/dist/lib/request/response-handler.js +150 -0
- package/dist/lib/request/response-handler.js.map +1 -0
- package/dist/lib/types.d.ts +183 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +2 -0
- package/dist/lib/types.js.map +1 -0
- package/package.json +74 -0
- package/scripts/install-opencode-codex-auth.js +433 -0
- package/scripts/test-all-models.sh +259 -0
- package/scripts/validate-model-map.sh +97 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://opencode.ai/config.json",
|
|
3
|
+
"plugin": [
|
|
4
|
+
"better-opencode-openai-codex-auth"
|
|
5
|
+
],
|
|
6
|
+
"provider": {
|
|
7
|
+
"openai": {
|
|
8
|
+
"options": {
|
|
9
|
+
"reasoningEffort": "medium",
|
|
10
|
+
"reasoningSummary": "auto",
|
|
11
|
+
"textVerbosity": "medium",
|
|
12
|
+
"include": [
|
|
13
|
+
"reasoning.encrypted_content"
|
|
14
|
+
],
|
|
15
|
+
"store": false
|
|
16
|
+
},
|
|
17
|
+
"models": {
|
|
18
|
+
"gpt-5.2": {
|
|
19
|
+
"name": "GPT 5.2 (OAuth)",
|
|
20
|
+
"limit": {
|
|
21
|
+
"context": 272000,
|
|
22
|
+
"output": 128000
|
|
23
|
+
},
|
|
24
|
+
"modalities": {
|
|
25
|
+
"input": [
|
|
26
|
+
"text",
|
|
27
|
+
"image"
|
|
28
|
+
],
|
|
29
|
+
"output": [
|
|
30
|
+
"text"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"variants": {
|
|
34
|
+
"none": {
|
|
35
|
+
"reasoningEffort": "none",
|
|
36
|
+
"reasoningSummary": "auto",
|
|
37
|
+
"textVerbosity": "medium"
|
|
38
|
+
},
|
|
39
|
+
"low": {
|
|
40
|
+
"reasoningEffort": "low",
|
|
41
|
+
"reasoningSummary": "auto",
|
|
42
|
+
"textVerbosity": "medium"
|
|
43
|
+
},
|
|
44
|
+
"medium": {
|
|
45
|
+
"reasoningEffort": "medium",
|
|
46
|
+
"reasoningSummary": "auto",
|
|
47
|
+
"textVerbosity": "medium"
|
|
48
|
+
},
|
|
49
|
+
"high": {
|
|
50
|
+
"reasoningEffort": "high",
|
|
51
|
+
"reasoningSummary": "detailed",
|
|
52
|
+
"textVerbosity": "medium"
|
|
53
|
+
},
|
|
54
|
+
"xhigh": {
|
|
55
|
+
"reasoningEffort": "xhigh",
|
|
56
|
+
"reasoningSummary": "detailed",
|
|
57
|
+
"textVerbosity": "medium"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"gpt-5.2-codex": {
|
|
62
|
+
"name": "GPT 5.2 Codex (OAuth)",
|
|
63
|
+
"limit": {
|
|
64
|
+
"context": 272000,
|
|
65
|
+
"output": 128000
|
|
66
|
+
},
|
|
67
|
+
"modalities": {
|
|
68
|
+
"input": [
|
|
69
|
+
"text",
|
|
70
|
+
"image"
|
|
71
|
+
],
|
|
72
|
+
"output": [
|
|
73
|
+
"text"
|
|
74
|
+
]
|
|
75
|
+
},
|
|
76
|
+
"variants": {
|
|
77
|
+
"low": {
|
|
78
|
+
"reasoningEffort": "low",
|
|
79
|
+
"reasoningSummary": "auto",
|
|
80
|
+
"textVerbosity": "medium"
|
|
81
|
+
},
|
|
82
|
+
"medium": {
|
|
83
|
+
"reasoningEffort": "medium",
|
|
84
|
+
"reasoningSummary": "auto",
|
|
85
|
+
"textVerbosity": "medium"
|
|
86
|
+
},
|
|
87
|
+
"high": {
|
|
88
|
+
"reasoningEffort": "high",
|
|
89
|
+
"reasoningSummary": "detailed",
|
|
90
|
+
"textVerbosity": "medium"
|
|
91
|
+
},
|
|
92
|
+
"xhigh": {
|
|
93
|
+
"reasoningEffort": "xhigh",
|
|
94
|
+
"reasoningSummary": "detailed",
|
|
95
|
+
"textVerbosity": "medium"
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
"gpt-5.1-codex-max": {
|
|
100
|
+
"name": "GPT 5.1 Codex Max (OAuth)",
|
|
101
|
+
"limit": {
|
|
102
|
+
"context": 272000,
|
|
103
|
+
"output": 128000
|
|
104
|
+
},
|
|
105
|
+
"modalities": {
|
|
106
|
+
"input": [
|
|
107
|
+
"text",
|
|
108
|
+
"image"
|
|
109
|
+
],
|
|
110
|
+
"output": [
|
|
111
|
+
"text"
|
|
112
|
+
]
|
|
113
|
+
},
|
|
114
|
+
"variants": {
|
|
115
|
+
"low": {
|
|
116
|
+
"reasoningEffort": "low",
|
|
117
|
+
"reasoningSummary": "detailed",
|
|
118
|
+
"textVerbosity": "medium"
|
|
119
|
+
},
|
|
120
|
+
"medium": {
|
|
121
|
+
"reasoningEffort": "medium",
|
|
122
|
+
"reasoningSummary": "detailed",
|
|
123
|
+
"textVerbosity": "medium"
|
|
124
|
+
},
|
|
125
|
+
"high": {
|
|
126
|
+
"reasoningEffort": "high",
|
|
127
|
+
"reasoningSummary": "detailed",
|
|
128
|
+
"textVerbosity": "medium"
|
|
129
|
+
},
|
|
130
|
+
"xhigh": {
|
|
131
|
+
"reasoningEffort": "xhigh",
|
|
132
|
+
"reasoningSummary": "detailed",
|
|
133
|
+
"textVerbosity": "medium"
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
"gpt-5.1-codex": {
|
|
138
|
+
"name": "GPT 5.1 Codex (OAuth)",
|
|
139
|
+
"limit": {
|
|
140
|
+
"context": 272000,
|
|
141
|
+
"output": 128000
|
|
142
|
+
},
|
|
143
|
+
"modalities": {
|
|
144
|
+
"input": [
|
|
145
|
+
"text",
|
|
146
|
+
"image"
|
|
147
|
+
],
|
|
148
|
+
"output": [
|
|
149
|
+
"text"
|
|
150
|
+
]
|
|
151
|
+
},
|
|
152
|
+
"variants": {
|
|
153
|
+
"low": {
|
|
154
|
+
"reasoningEffort": "low",
|
|
155
|
+
"reasoningSummary": "auto",
|
|
156
|
+
"textVerbosity": "medium"
|
|
157
|
+
},
|
|
158
|
+
"medium": {
|
|
159
|
+
"reasoningEffort": "medium",
|
|
160
|
+
"reasoningSummary": "auto",
|
|
161
|
+
"textVerbosity": "medium"
|
|
162
|
+
},
|
|
163
|
+
"high": {
|
|
164
|
+
"reasoningEffort": "high",
|
|
165
|
+
"reasoningSummary": "detailed",
|
|
166
|
+
"textVerbosity": "medium"
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
"gpt-5.1-codex-mini": {
|
|
171
|
+
"name": "GPT 5.1 Codex Mini (OAuth)",
|
|
172
|
+
"limit": {
|
|
173
|
+
"context": 272000,
|
|
174
|
+
"output": 128000
|
|
175
|
+
},
|
|
176
|
+
"modalities": {
|
|
177
|
+
"input": [
|
|
178
|
+
"text",
|
|
179
|
+
"image"
|
|
180
|
+
],
|
|
181
|
+
"output": [
|
|
182
|
+
"text"
|
|
183
|
+
]
|
|
184
|
+
},
|
|
185
|
+
"variants": {
|
|
186
|
+
"medium": {
|
|
187
|
+
"reasoningEffort": "medium",
|
|
188
|
+
"reasoningSummary": "auto",
|
|
189
|
+
"textVerbosity": "medium"
|
|
190
|
+
},
|
|
191
|
+
"high": {
|
|
192
|
+
"reasoningEffort": "high",
|
|
193
|
+
"reasoningSummary": "detailed",
|
|
194
|
+
"textVerbosity": "medium"
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
"gpt-5.1": {
|
|
199
|
+
"name": "GPT 5.1 (OAuth)",
|
|
200
|
+
"limit": {
|
|
201
|
+
"context": 272000,
|
|
202
|
+
"output": 128000
|
|
203
|
+
},
|
|
204
|
+
"modalities": {
|
|
205
|
+
"input": [
|
|
206
|
+
"text",
|
|
207
|
+
"image"
|
|
208
|
+
],
|
|
209
|
+
"output": [
|
|
210
|
+
"text"
|
|
211
|
+
]
|
|
212
|
+
},
|
|
213
|
+
"variants": {
|
|
214
|
+
"none": {
|
|
215
|
+
"reasoningEffort": "none",
|
|
216
|
+
"reasoningSummary": "auto",
|
|
217
|
+
"textVerbosity": "medium"
|
|
218
|
+
},
|
|
219
|
+
"low": {
|
|
220
|
+
"reasoningEffort": "low",
|
|
221
|
+
"reasoningSummary": "auto",
|
|
222
|
+
"textVerbosity": "low"
|
|
223
|
+
},
|
|
224
|
+
"medium": {
|
|
225
|
+
"reasoningEffort": "medium",
|
|
226
|
+
"reasoningSummary": "auto",
|
|
227
|
+
"textVerbosity": "medium"
|
|
228
|
+
},
|
|
229
|
+
"high": {
|
|
230
|
+
"reasoningEffort": "high",
|
|
231
|
+
"reasoningSummary": "detailed",
|
|
232
|
+
"textVerbosity": "high"
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI ChatGPT (Codex) OAuth Authentication Plugin for opencode
|
|
3
|
+
*
|
|
4
|
+
* COMPLIANCE NOTICE:
|
|
5
|
+
* This plugin uses OpenAI's official OAuth authentication flow (the same method
|
|
6
|
+
* used by OpenAI's official Codex CLI at https://github.com/openai/codex).
|
|
7
|
+
*
|
|
8
|
+
* INTENDED USE: Personal development and coding assistance with your own
|
|
9
|
+
* ChatGPT Plus/Pro subscription.
|
|
10
|
+
*
|
|
11
|
+
* NOT INTENDED FOR: Commercial resale, multi-user services, high-volume
|
|
12
|
+
* automated extraction, or any use that violates OpenAI's Terms of Service.
|
|
13
|
+
*
|
|
14
|
+
* Users are responsible for ensuring their usage complies with:
|
|
15
|
+
* - OpenAI Terms of Use: https://openai.com/policies/terms-of-use/
|
|
16
|
+
* - OpenAI Usage Policies: https://openai.com/policies/usage-policies/
|
|
17
|
+
*
|
|
18
|
+
* For production applications, use the OpenAI Platform API: https://platform.openai.com/
|
|
19
|
+
*
|
|
20
|
+
* @license MIT with Usage Disclaimer (see LICENSE file)
|
|
21
|
+
* @author ShivamB25 (fork maintainer); originally by numman-ali
|
|
22
|
+
* @repository https://github.com/ShivamB25/better-opencode-openai-codex-auth
|
|
23
|
+
*/
|
|
24
|
+
import type { Plugin } from "@opencode-ai/plugin";
|
|
25
|
+
/**
|
|
26
|
+
* OpenAI Codex OAuth authentication plugin for opencode
|
|
27
|
+
*
|
|
28
|
+
* This plugin enables opencode to use OpenAI's Codex backend via ChatGPT Plus/Pro
|
|
29
|
+
* OAuth authentication, allowing users to leverage their ChatGPT subscription
|
|
30
|
+
* instead of OpenAI Platform API credits.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```json
|
|
34
|
+
* {
|
|
35
|
+
* "plugin": ["better-opencode-openai-codex-auth"],
|
|
36
|
+
* "model": "openai/gpt-5-codex"
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare const OpenAIAuthPlugin: Plugin;
|
|
41
|
+
export default OpenAIAuthPlugin;
|
|
42
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAe,MAAM,qBAAqB,CAAC;AAmC/D;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,gBAAgB,EAAE,MA2V9B,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI ChatGPT (Codex) OAuth Authentication Plugin for opencode
|
|
3
|
+
*
|
|
4
|
+
* COMPLIANCE NOTICE:
|
|
5
|
+
* This plugin uses OpenAI's official OAuth authentication flow (the same method
|
|
6
|
+
* used by OpenAI's official Codex CLI at https://github.com/openai/codex).
|
|
7
|
+
*
|
|
8
|
+
* INTENDED USE: Personal development and coding assistance with your own
|
|
9
|
+
* ChatGPT Plus/Pro subscription.
|
|
10
|
+
*
|
|
11
|
+
* NOT INTENDED FOR: Commercial resale, multi-user services, high-volume
|
|
12
|
+
* automated extraction, or any use that violates OpenAI's Terms of Service.
|
|
13
|
+
*
|
|
14
|
+
* Users are responsible for ensuring their usage complies with:
|
|
15
|
+
* - OpenAI Terms of Use: https://openai.com/policies/terms-of-use/
|
|
16
|
+
* - OpenAI Usage Policies: https://openai.com/policies/usage-policies/
|
|
17
|
+
*
|
|
18
|
+
* For production applications, use the OpenAI Platform API: https://platform.openai.com/
|
|
19
|
+
*
|
|
20
|
+
* @license MIT with Usage Disclaimer (see LICENSE file)
|
|
21
|
+
* @author ShivamB25 (fork maintainer); originally by numman-ali
|
|
22
|
+
* @repository https://github.com/ShivamB25/better-opencode-openai-codex-auth
|
|
23
|
+
*/
|
|
24
|
+
import { createAuthorizationFlow, decodeJWT, exchangeAuthorizationCode, parseAuthorizationInput, refreshAccessToken, REDIRECT_URI, } from "./lib/auth/auth.js";
|
|
25
|
+
import { openBrowserUrl } from "./lib/auth/browser.js";
|
|
26
|
+
import { startLocalOAuthServer } from "./lib/auth/server.js";
|
|
27
|
+
import { getCodexMode, loadPluginConfig } from "./lib/config.js";
|
|
28
|
+
import { AUTH_LABELS, CODEX_BASE_URL, DUMMY_API_KEY, ERROR_MESSAGES, JWT_CLAIM_PATH, LOG_STAGES, PLUGIN_NAME, PROVIDER_ID, } from "./lib/constants.js";
|
|
29
|
+
import { logRequest, logDebug, logWarn } from "./lib/logger.js";
|
|
30
|
+
import { createCodexHeaders, extractRequestUrl, handleErrorResponse, handleSuccessResponse, rewriteUrlForCodex, transformRequestForCodex, } from "./lib/request/fetch-helpers.js";
|
|
31
|
+
import { AccountPool } from "./lib/account-pool.js";
|
|
32
|
+
/**
|
|
33
|
+
* OpenAI Codex OAuth authentication plugin for opencode
|
|
34
|
+
*
|
|
35
|
+
* This plugin enables opencode to use OpenAI's Codex backend via ChatGPT Plus/Pro
|
|
36
|
+
* OAuth authentication, allowing users to leverage their ChatGPT subscription
|
|
37
|
+
* instead of OpenAI Platform API credits.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```json
|
|
41
|
+
* {
|
|
42
|
+
* "plugin": ["better-opencode-openai-codex-auth"],
|
|
43
|
+
* "model": "openai/gpt-5-codex"
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export const OpenAIAuthPlugin = async ({ client }) => {
|
|
48
|
+
const buildManualOAuthFlow = (pkce, url) => ({
|
|
49
|
+
url,
|
|
50
|
+
method: "code",
|
|
51
|
+
instructions: AUTH_LABELS.INSTRUCTIONS_MANUAL,
|
|
52
|
+
callback: async (input) => {
|
|
53
|
+
const parsed = parseAuthorizationInput(input);
|
|
54
|
+
if (!parsed.code) {
|
|
55
|
+
return { type: "failed" };
|
|
56
|
+
}
|
|
57
|
+
const tokens = await exchangeAuthorizationCode(parsed.code, pkce.verifier, REDIRECT_URI);
|
|
58
|
+
return tokens?.type === "success" ? tokens : { type: "failed" };
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
auth: {
|
|
63
|
+
provider: PROVIDER_ID,
|
|
64
|
+
/**
|
|
65
|
+
* Loader function that configures OAuth authentication and request handling
|
|
66
|
+
*
|
|
67
|
+
* This function:
|
|
68
|
+
* 1. Validates OAuth authentication
|
|
69
|
+
* 2. Extracts ChatGPT account ID from access token
|
|
70
|
+
* 3. Loads user configuration from opencode.json
|
|
71
|
+
* 4. Fetches Codex system instructions from GitHub (cached)
|
|
72
|
+
* 5. Returns SDK configuration with custom fetch implementation
|
|
73
|
+
*
|
|
74
|
+
* @param getAuth - Function to retrieve current auth state
|
|
75
|
+
* @param provider - Provider configuration from opencode.json
|
|
76
|
+
* @returns SDK configuration object or empty object for non-OAuth auth
|
|
77
|
+
*/
|
|
78
|
+
async loader(getAuth, provider) {
|
|
79
|
+
const auth = await getAuth();
|
|
80
|
+
// Only handle OAuth auth type, skip API key auth
|
|
81
|
+
if (auth.type !== "oauth") {
|
|
82
|
+
return {};
|
|
83
|
+
}
|
|
84
|
+
// Extract ChatGPT account ID from JWT access token
|
|
85
|
+
const decoded = decodeJWT(auth.access);
|
|
86
|
+
const accountId = decoded?.[JWT_CLAIM_PATH]?.chatgpt_account_id;
|
|
87
|
+
if (!accountId) {
|
|
88
|
+
logDebug(`[${PLUGIN_NAME}] ${ERROR_MESSAGES.NO_ACCOUNT_ID} (skipping plugin)`);
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
// Extract user configuration (global + per-model options)
|
|
92
|
+
const providerConfig = provider;
|
|
93
|
+
const userConfig = {
|
|
94
|
+
global: providerConfig?.options || {},
|
|
95
|
+
models: providerConfig?.models || {},
|
|
96
|
+
};
|
|
97
|
+
// Load plugin configuration and determine CODEX_MODE
|
|
98
|
+
// Priority: CODEX_MODE env var > config file > default (true)
|
|
99
|
+
const pluginConfig = loadPluginConfig();
|
|
100
|
+
const codexMode = getCodexMode(pluginConfig);
|
|
101
|
+
const accountSelectionStrategy = pluginConfig.accountSelectionStrategy === "sticky" ? "sticky" : "round-robin";
|
|
102
|
+
const rateLimitCooldownMs = pluginConfig.rateLimitCooldownMs;
|
|
103
|
+
const accountPool = AccountPool.load();
|
|
104
|
+
const savePool = () => {
|
|
105
|
+
try {
|
|
106
|
+
accountPool.save();
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
logWarn("Failed to persist account pool", error);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
accountPool.upsert({
|
|
113
|
+
accountId,
|
|
114
|
+
access: auth.access,
|
|
115
|
+
refresh: auth.refresh,
|
|
116
|
+
expires: auth.expires,
|
|
117
|
+
email: typeof decoded?.email === "string"
|
|
118
|
+
? decoded.email
|
|
119
|
+
: undefined,
|
|
120
|
+
});
|
|
121
|
+
savePool();
|
|
122
|
+
// Return SDK configuration
|
|
123
|
+
return {
|
|
124
|
+
apiKey: DUMMY_API_KEY,
|
|
125
|
+
baseURL: CODEX_BASE_URL,
|
|
126
|
+
/**
|
|
127
|
+
* Custom fetch implementation for Codex API
|
|
128
|
+
*
|
|
129
|
+
* Handles:
|
|
130
|
+
* - Per-account token refresh when expired (inside retry loop)
|
|
131
|
+
* - URL rewriting for Codex backend
|
|
132
|
+
* - Request body transformation
|
|
133
|
+
* - OAuth header injection per selected account
|
|
134
|
+
* - Automatic 429 rotation across account pool
|
|
135
|
+
* - SSE to JSON conversion for non-tool requests
|
|
136
|
+
* - Error handling and logging
|
|
137
|
+
*
|
|
138
|
+
* @param input - Request URL or Request object
|
|
139
|
+
* @param init - Request options
|
|
140
|
+
* @returns Response from Codex API
|
|
141
|
+
*/
|
|
142
|
+
async fetch(input, init) {
|
|
143
|
+
const latestAuth = await getAuth();
|
|
144
|
+
let latestAccountIdFromAuth;
|
|
145
|
+
if (latestAuth.type === "oauth") {
|
|
146
|
+
const latestDecoded = decodeJWT(latestAuth.access);
|
|
147
|
+
const latestAccountId = latestDecoded?.[JWT_CLAIM_PATH]?.chatgpt_account_id;
|
|
148
|
+
latestAccountIdFromAuth = latestAccountId;
|
|
149
|
+
if (latestAccountId) {
|
|
150
|
+
accountPool.upsert({
|
|
151
|
+
accountId: latestAccountId,
|
|
152
|
+
access: latestAuth.access,
|
|
153
|
+
refresh: latestAuth.refresh,
|
|
154
|
+
expires: latestAuth.expires,
|
|
155
|
+
email: typeof latestDecoded?.email === "string"
|
|
156
|
+
? latestDecoded.email
|
|
157
|
+
: undefined,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// Step 1: Extract and rewrite URL for Codex backend
|
|
162
|
+
const originalUrl = extractRequestUrl(input);
|
|
163
|
+
const url = rewriteUrlForCodex(originalUrl);
|
|
164
|
+
// Step 2: Transform request body with model-specific Codex instructions
|
|
165
|
+
// Instructions are fetched per model family (codex-max, codex, gpt-5.1)
|
|
166
|
+
// Capture original stream value before transformation
|
|
167
|
+
// generateText() sends no stream field, streamText() sends stream=true
|
|
168
|
+
let originalBody = {};
|
|
169
|
+
if (typeof init?.body === "string") {
|
|
170
|
+
try {
|
|
171
|
+
originalBody = JSON.parse(init.body);
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
originalBody = {};
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
const isStreaming = originalBody.stream === true;
|
|
178
|
+
const transformation = await transformRequestForCodex(init, url, userConfig, codexMode);
|
|
179
|
+
const requestInit = transformation?.updatedInit ?? init;
|
|
180
|
+
const attempts = Math.max(accountPool.count(), 1);
|
|
181
|
+
let lastRateLimitResponse = null;
|
|
182
|
+
for (let i = 0; i < attempts; i++) {
|
|
183
|
+
const selected = accountPool.next(accountSelectionStrategy);
|
|
184
|
+
if (!selected) {
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
if (selected.expires < Date.now()) {
|
|
188
|
+
const selectedRefreshBefore = selected.refresh;
|
|
189
|
+
const refreshed = await refreshAccessToken(selected.refresh);
|
|
190
|
+
if (refreshed.type === "failed") {
|
|
191
|
+
// Auth failure (revoked/invalid token) is NOT a rate limit.
|
|
192
|
+
// Calling markRateLimited here would retry the same dead
|
|
193
|
+
// token after cooldown, looping forever. Skip this account
|
|
194
|
+
// for the current request without penalizing it.
|
|
195
|
+
logWarn("Token refresh failed for account, skipping", selected.accountId);
|
|
196
|
+
savePool();
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
accountPool.replaceAuth(selected.accountId, refreshed.access, refreshed.refresh, refreshed.expires);
|
|
200
|
+
if (latestAuth.type === "oauth" &&
|
|
201
|
+
(latestAuth.refresh === selectedRefreshBefore ||
|
|
202
|
+
latestAccountIdFromAuth === selected.accountId)) {
|
|
203
|
+
try {
|
|
204
|
+
await client.auth.set({
|
|
205
|
+
path: { id: "openai" },
|
|
206
|
+
body: {
|
|
207
|
+
type: "oauth",
|
|
208
|
+
access: refreshed.access,
|
|
209
|
+
refresh: refreshed.refresh,
|
|
210
|
+
expires: refreshed.expires,
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
logWarn("Failed to persist refreshed auth", error);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const headers = createCodexHeaders(requestInit, selected.accountId, selected.access, {
|
|
220
|
+
model: transformation?.body.model,
|
|
221
|
+
promptCacheKey: transformation?.body
|
|
222
|
+
?.prompt_cache_key,
|
|
223
|
+
});
|
|
224
|
+
const response = await fetch(url, {
|
|
225
|
+
...requestInit,
|
|
226
|
+
headers,
|
|
227
|
+
});
|
|
228
|
+
logRequest(LOG_STAGES.RESPONSE, {
|
|
229
|
+
status: response.status,
|
|
230
|
+
ok: response.ok,
|
|
231
|
+
statusText: response.statusText,
|
|
232
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
233
|
+
accountId: selected.accountId,
|
|
234
|
+
attempt: i + 1,
|
|
235
|
+
totalAttempts: attempts,
|
|
236
|
+
});
|
|
237
|
+
if (!response.ok) {
|
|
238
|
+
const mapped = await handleErrorResponse(response);
|
|
239
|
+
if (mapped.status === 429) {
|
|
240
|
+
accountPool.markRateLimited(selected.accountId, mapped.headers, rateLimitCooldownMs);
|
|
241
|
+
savePool();
|
|
242
|
+
lastRateLimitResponse = mapped;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
savePool();
|
|
246
|
+
return mapped;
|
|
247
|
+
}
|
|
248
|
+
savePool();
|
|
249
|
+
return await handleSuccessResponse(response, isStreaming);
|
|
250
|
+
}
|
|
251
|
+
savePool();
|
|
252
|
+
if (lastRateLimitResponse) {
|
|
253
|
+
return lastRateLimitResponse;
|
|
254
|
+
}
|
|
255
|
+
const retryAfterMs = accountPool.getMinRetryAfterMs();
|
|
256
|
+
const retryAfterSeconds = retryAfterMs ? Math.max(1, Math.ceil(retryAfterMs / 1000)) : null;
|
|
257
|
+
return new Response(JSON.stringify({
|
|
258
|
+
error: {
|
|
259
|
+
code: "usage_limit_reached",
|
|
260
|
+
message: "All ChatGPT accounts are temporarily rate-limited",
|
|
261
|
+
},
|
|
262
|
+
}), {
|
|
263
|
+
status: 429,
|
|
264
|
+
headers: {
|
|
265
|
+
"content-type": "application/json",
|
|
266
|
+
...(retryAfterSeconds
|
|
267
|
+
? { "retry-after": String(retryAfterSeconds) }
|
|
268
|
+
: {}),
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
},
|
|
274
|
+
methods: [
|
|
275
|
+
{
|
|
276
|
+
label: AUTH_LABELS.OAUTH,
|
|
277
|
+
type: "oauth",
|
|
278
|
+
/**
|
|
279
|
+
* OAuth authorization flow
|
|
280
|
+
*
|
|
281
|
+
* Steps:
|
|
282
|
+
* 1. Generate PKCE challenge and state for security
|
|
283
|
+
* 2. Start local OAuth callback server on port 1455
|
|
284
|
+
* 3. Open browser to OpenAI authorization page
|
|
285
|
+
* 4. Wait for user to complete login
|
|
286
|
+
* 5. Exchange authorization code for tokens
|
|
287
|
+
*
|
|
288
|
+
* @returns Authorization flow configuration
|
|
289
|
+
*/
|
|
290
|
+
authorize: async () => {
|
|
291
|
+
const { pkce, state, url } = await createAuthorizationFlow();
|
|
292
|
+
const serverInfo = await startLocalOAuthServer({ state });
|
|
293
|
+
// Attempt to open browser automatically
|
|
294
|
+
openBrowserUrl(url);
|
|
295
|
+
if (!serverInfo.ready) {
|
|
296
|
+
serverInfo.close();
|
|
297
|
+
return buildManualOAuthFlow(pkce, url);
|
|
298
|
+
}
|
|
299
|
+
return {
|
|
300
|
+
url,
|
|
301
|
+
method: "auto",
|
|
302
|
+
instructions: AUTH_LABELS.INSTRUCTIONS,
|
|
303
|
+
callback: async () => {
|
|
304
|
+
const result = await serverInfo.waitForCode(state);
|
|
305
|
+
serverInfo.close();
|
|
306
|
+
if (!result) {
|
|
307
|
+
return { type: "failed" };
|
|
308
|
+
}
|
|
309
|
+
const tokens = await exchangeAuthorizationCode(result.code, pkce.verifier, REDIRECT_URI);
|
|
310
|
+
return tokens?.type === "success"
|
|
311
|
+
? tokens
|
|
312
|
+
: { type: "failed" };
|
|
313
|
+
},
|
|
314
|
+
};
|
|
315
|
+
},
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
label: AUTH_LABELS.OAUTH_MANUAL,
|
|
319
|
+
type: "oauth",
|
|
320
|
+
authorize: async () => {
|
|
321
|
+
const { pkce, url } = await createAuthorizationFlow();
|
|
322
|
+
return buildManualOAuthFlow(pkce, url);
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
label: AUTH_LABELS.API_KEY,
|
|
327
|
+
type: "api",
|
|
328
|
+
},
|
|
329
|
+
],
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
};
|
|
333
|
+
export default OpenAIAuthPlugin;
|
|
334
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAIH,OAAO,EACN,uBAAuB,EACvB,SAAS,EACT,yBAAyB,EACzB,uBAAuB,EACvB,kBAAkB,EAClB,YAAY,GACZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EACN,WAAW,EACX,cAAc,EACd,aAAa,EACb,cAAc,EACd,cAAc,EACd,UAAU,EACV,WAAW,EACX,WAAW,GACX,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EACN,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,EAClB,wBAAwB,GACxB,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAW,KAAK,EAAE,EAAE,MAAM,EAAe,EAAE,EAAE;IACzE,MAAM,oBAAoB,GAAG,CAAC,IAA0B,EAAE,GAAW,EAAE,E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BAAoB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;oBACxC,CAAC;iBACD;gBACD;oBACC,KAAK,EAAE,WAAW,CAAC,OAAO;oBAC1B,IAAI,EAAE,KAAc;iBACpB;aACF;SACD;KACD,CAAC;AACH,CAAC,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AccountPoolEntry } from "./types.js";
|
|
2
|
+
export declare class AccountPool {
|
|
3
|
+
private accounts;
|
|
4
|
+
private activeIndex;
|
|
5
|
+
static load(): AccountPool;
|
|
6
|
+
save(): void;
|
|
7
|
+
upsert(entry: AccountPoolEntry): void;
|
|
8
|
+
count(): number;
|
|
9
|
+
getAvailableCount(): number;
|
|
10
|
+
getMinRetryAfterMs(): number | null;
|
|
11
|
+
markRateLimited(accountId: string, headers: Headers, cooldownMs?: number): void;
|
|
12
|
+
next(strategy: "sticky" | "round-robin"): AccountPoolEntry | null;
|
|
13
|
+
replaceAuth(accountId: string, access: string, refresh: string, expires: number): void;
|
|
14
|
+
private isLimited;
|
|
15
|
+
private clearExpiredLimits;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=account-pool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"account-pool.d.ts","sourceRoot":"","sources":["../../lib/account-pool.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAsB,MAAM,YAAY,CAAC;AA8FvE,qBAAa,WAAW;IACvB,OAAO,CAAC,QAAQ,CAA0B;IAC1C,OAAO,CAAC,WAAW,CAAK;IAExB,MAAM,CAAC,IAAI,IAAI,WAAW;IAgB1B,IAAI,IAAI,IAAI;IAuBZ,MAAM,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI;IA8BrC,KAAK,IAAI,MAAM;IAIf,iBAAiB,IAAI,MAAM;IAK3B,kBAAkB,IAAI,MAAM,GAAG,IAAI;IAanC,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI;IAQ/E,IAAI,CAAC,QAAQ,EAAE,QAAQ,GAAG,aAAa,GAAG,gBAAgB,GAAG,IAAI;IAuBjE,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAQtF,OAAO,CAAC,SAAS;IAIjB,OAAO,CAAC,kBAAkB;CAQ1B"}
|