@@ -26,6 +26,7 @@ import { isMcpTool } from "../../utils/mcp-name"
2626import { sanitizeOpenAiCallId } from "../../utils/tool-id"
2727import { openAiCodexOAuthManager } from "../../integrations/openai-codex/oauth"
2828import { t } from "../../i18n"
29+ import { ContextProxy } from "../../core/config/ContextProxy"
2930
3031export type OpenAiCodexModel = ReturnType < OpenAiCodexHandler [ "getModel" ] >
3132
@@ -64,6 +65,20 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
6465 */
6566 private pendingToolCallId : string | undefined
6667 private pendingToolCallName : string | undefined
68+ private resolveProfileId ( ) : string | undefined {
69+ try {
70+ const contextProxy = ContextProxy . instance
71+ const currentApiConfigName = contextProxy . getValue ( "currentApiConfigName" )
72+ const listApiConfigMeta = contextProxy . getValue ( "listApiConfigMeta" )
73+ if ( ! Array . isArray ( listApiConfigMeta ) ) {
74+ return undefined
75+ }
76+ const match = listApiConfigMeta . find ( ( profile ) => profile ?. name === currentApiConfigName )
77+ return typeof match ?. id === "string" ? match . id : undefined
78+ } catch {
79+ return undefined
80+ }
81+ }
6782
6883 // Event types handled by the shared event processor
6984 private readonly coreHandledEventTypes = new Set < string > ( [
@@ -151,7 +166,8 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
151166 this . pendingToolCallName = undefined
152167
153168 // Get access token from OAuth manager
154- let accessToken = await openAiCodexOAuthManager . getAccessToken ( )
169+ const profileId = this . resolveProfileId ( )
170+ let accessToken = await openAiCodexOAuthManager . getAccessToken ( profileId )
155171 if ( ! accessToken ) {
156172 throw new Error (
157173 t ( "common:errors.openAiCodex.notAuthenticated" , {
@@ -183,7 +199,7 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
183199
184200 if ( attempt === 0 && isAuthFailure ) {
185201 // Force refresh the token for retry
186- const refreshed = await openAiCodexOAuthManager . forceRefreshAccessToken ( )
202+ const refreshed = await openAiCodexOAuthManager . forceRefreshAccessToken ( profileId )
187203 if ( ! refreshed ) {
188204 throw new Error (
189205 t ( "common:errors.openAiCodex.notAuthenticated" , {
@@ -341,7 +357,7 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
341357 // is consistent across providers.
342358 try {
343359 // Get ChatGPT account ID for organization subscriptions
344- const accountId = await openAiCodexOAuthManager . getAccountId ( )
360+ const accountId = await openAiCodexOAuthManager . getAccountId ( this . resolveProfileId ( ) )
345361
346362 // Build Codex-specific headers. Authorization is provided by the SDK apiKey.
347363 const codexHeaders : Record < string , string > = {
@@ -481,7 +497,7 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
481497 const url = `${ CODEX_API_BASE_URL } /responses`
482498
483499 // Get ChatGPT account ID for organization subscriptions
484- const accountId = await openAiCodexOAuthManager . getAccountId ( )
500+ const accountId = await openAiCodexOAuthManager . getAccountId ( this . resolveProfileId ( ) )
485501
486502 // Build headers with required Codex-specific fields
487503 const headers : Record < string , string > = {
@@ -1008,7 +1024,7 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
10081024 const model = this . getModel ( )
10091025
10101026 // Get access token
1011- const accessToken = await openAiCodexOAuthManager . getAccessToken ( )
1027+ const accessToken = await openAiCodexOAuthManager . getAccessToken ( this . resolveProfileId ( ) )
10121028 if ( ! accessToken ) {
10131029 throw new Error (
10141030 t ( "common:errors.openAiCodex.notAuthenticated" , {
@@ -1043,7 +1059,7 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
10431059 const url = `${ CODEX_API_BASE_URL } /responses`
10441060
10451061 // Get ChatGPT account ID for organization subscriptions
1046- const accountId = await openAiCodexOAuthManager . getAccountId ( )
1062+ const accountId = await openAiCodexOAuthManager . getAccountId ( this . resolveProfileId ( ) )
10471063
10481064 // Build headers with required Codex-specific fields
10491065 const headers : Record < string , string > = {
0 commit comments