import * as plugins from './plugins.js'; import type { IOpenAiChatGptAuthCredentials, IOpenAiChatGptDynamicModelConnectionOptions, } from './interfaces.js'; import { createOpenAiChatGptProviderSettings, OPENAI_CHATGPT_CODEX_BASE_URL, OpenAiChatGptAuthError, } from './smartai.auth.openai.js'; const codexBaseUrl = new URL(OPENAI_CHATGPT_CODEX_BASE_URL); function assertCodexRequestUrl(normalizedUrlArg: string): void { const url = new URL(normalizedUrlArg); const isCodexPath = url.pathname === codexBaseUrl.pathname || url.pathname.startsWith(`${codexBaseUrl.pathname}/`); if ( url.origin !== codexBaseUrl.origin || url.username !== '' || url.password !== '' || !isCodexPath ) { throw new OpenAiChatGptAuthError('OpenAI ChatGPT inference URL is outside the Codex endpoint.'); } } function requireExpectedIdentity(optionsArg: IOpenAiChatGptDynamicModelConnectionOptions): { accountId: string | undefined; isFedrampAccount: boolean; } { const expectedIdentity = optionsArg.expectedIdentity; if ( !expectedIdentity || ( expectedIdentity.accountId !== undefined && (typeof expectedIdentity.accountId !== 'string' || expectedIdentity.accountId.length === 0) ) || typeof expectedIdentity.isFedrampAccount !== 'boolean' ) { throw new OpenAiChatGptAuthError('OpenAI ChatGPT expected access identity is invalid.'); } return Object.freeze({ accountId: expectedIdentity.accountId, isFedrampAccount: expectedIdentity.isFedrampAccount, }); } function assertExpectedIdentity( settingsArg: ReturnType, expectedIdentityArg: Readonly<{ accountId: string | undefined; isFedrampAccount: boolean; }>, ): void { const accountId = settingsArg.headers['ChatGPT-Account-ID']; const isFedrampAccount = settingsArg.headers['X-OpenAI-Fedramp'] === 'true'; if ( accountId !== expectedIdentityArg.accountId || isFedrampAccount !== expectedIdentityArg.isFedrampAccount ) { throw new OpenAiChatGptAuthError( 'OpenAI ChatGPT resolved access identity does not match the connection.', ); } } function applyAccessHeaders( headersArg: Headers, settingsArg: ReturnType, ): void { headersArg.set('Authorization', `Bearer ${settingsArg.apiKey}`); headersArg.set('originator', settingsArg.headers.originator); for (const name of ['ChatGPT-Account-ID', 'X-OpenAI-Fedramp']) { const value = settingsArg.headers[name]; if (value === undefined) { headersArg.delete(name); } else { headersArg.set(name, value); } } } export const createOpenAiChatGptModelConnection = ( credentialsArg: IOpenAiChatGptAuthCredentials, ): plugins.IOpenAiModelConnection => ({ settings: createOpenAiChatGptProviderSettings(credentialsArg), middleware: plugins.createOpenAiChatGptInstructionsMiddleware(), // the ChatGPT Codex backend serves the Responses API only apis: ['responses'], classifyLimit: plugins.classifyOpenAiChatGptLimit, }); export const createOpenAiChatGptDynamicModelConnection = ( optionsArg: IOpenAiChatGptDynamicModelConnectionOptions, ): plugins.IOpenAiModelConnection => { if (!optionsArg || typeof optionsArg.resolveAccess !== 'function') { throw new OpenAiChatGptAuthError('OpenAI ChatGPT access resolver is required.'); } const expectedIdentity = requireExpectedIdentity(optionsArg); const resolveAccess = optionsArg.resolveAccess; const originator = optionsArg.originator; const transport = optionsArg.fetch ?? globalThis.fetch; if (typeof transport !== 'function') { throw new OpenAiChatGptAuthError('fetch is not available for OpenAI ChatGPT inference.'); } const fetchWithCurrentAccess: typeof fetch = async (inputArg, initArg) => { const request = new Request(inputArg, { ...initArg, redirect: 'error', }); assertCodexRequestUrl(request.url); request.signal.throwIfAborted(); const access = await resolveAccess({ signal: request.signal }); request.signal.throwIfAborted(); const settings = createOpenAiChatGptProviderSettings({ accessToken: access.accessToken, originator, }); if (settings.baseURL !== OPENAI_CHATGPT_CODEX_BASE_URL) { throw new OpenAiChatGptAuthError('OpenAI ChatGPT inference endpoint is invalid.'); } assertExpectedIdentity(settings, expectedIdentity); const headers = new Headers(request.headers); applyAccessHeaders(headers, settings); const authenticatedRequest = new Request(request, { headers, }); authenticatedRequest.signal.throwIfAborted(); return transport(authenticatedRequest); }; return { settings: { apiKey: '', baseURL: OPENAI_CHATGPT_CODEX_BASE_URL, fetch: fetchWithCurrentAccess, }, middleware: plugins.createOpenAiChatGptInstructionsMiddleware(), // the ChatGPT Codex backend serves the Responses API only apis: ['responses'], classifyLimit: plugins.classifyOpenAiChatGptLimit, }; };