openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.98 kB
JavaScript
import{t as createOpenAIProviderMetadata,u as isSIWCAuthFlow}from"./provider-metadata-BCF2N0es.mjs";import{i as resolveServiceTiers,r as resolveFastModeSupport}from"./provider-policy-api-CA5XJ6P0.mjs";import{asNonArrayRecord,normalizeOptionalString}from"openclaw/plugin-sdk/string-coerce-runtime";import{decodeOpenAICodexJwtPayload,isFailedOAuthRefreshFence,resolveOpenAICodexAuthIdentity}from"openclaw/plugin-sdk/provider-oauth-runtime";
//!extensions/openai/provider-contract-api.ts
const matchesTokenSharingAccount=(e,t)=>{if(e.type!==`oauth`||t.type!==`oauth`||e.provider!==`openai`||t.provider!==`openai`||!isSIWCAuthFlow(e.authFlow)||!isSIWCAuthFlow(t.authFlow)||!e.idToken||!t.idToken||!e.clientId||e.clientId!==t.clientId)return false;const n=decodeOpenAICodexJwtPayload(e.idToken);const r=decodeOpenAICodexJwtPayload(t.idToken);return Boolean(n?.sub&&n.sub===r?.sub&&n.iss===r?.iss)};function accountSubject(e){const t=asNonArrayRecord(e&&decodeOpenAICodexJwtPayload(e)?.[`https://api.openai.com/auth`]);return{accountId:normalizeOptionalString(t.chatgpt_account_id),userId:normalizeOptionalString(t.chatgpt_user_id)??normalizeOptionalString(t.user_id)}}const matchesPersonalAccount=(e,t)=>{if(e.type!==`oauth`||t.type!==`oauth`||e.provider!==`openai`||t.provider!==e.provider)return false;const n=accountSubject(e.access);const r=accountSubject(t.access);return Boolean(n.accountId&&n.userId&&r.accountId===n.accountId&&r.userId===n.userId)};const matchesSharedAccount=(e,t)=>{if(e.type!==`oauth`||t.type!==`oauth`||e.provider!==`openai`||t.provider!==e.provider||isSIWCAuthFlow(e.authFlow)||isSIWCAuthFlow(t.authFlow))return false;const n=accountSubject(e.access);const r=accountSubject(t.access);const i=accountSubject(t.idToken);const a=[r.accountId,i.accountId,normalizeOptionalString(t.accountId)];const o=[r.userId,i.userId];return Boolean(n.accountId&&n.userId&&a.includes(n.accountId)&&a.every(e=>!e||e===n.accountId)&&o.includes(n.userId)&&o.every(e=>!e||e===n.userId))};const canReplaceGeneratedProfile=(e,t)=>{if(e.type!==`oauth`||t.type!==`oauth`||t.provider!==e.provider||isSIWCAuthFlow(t.authFlow))return false;if(matchesSharedAccount(e,t))return true;if(!isFailedOAuthRefreshFence(t))return false;const n=resolveOpenAICodexAuthIdentity({access:e.access});const r=asNonArrayRecord(decodeOpenAICodexJwtPayload(e.access)?.[`https://api.openai.com/profile`]);return Boolean(n.accountId&&n.accountId===normalizeOptionalString(t.accountId)&&r.email_verified===true&&n.email&&n.email===normalizeOptionalString(t.email))};function createOpenAIProvider(){const e=createOpenAIProviderMetadata();for(const t of e.auth)if(t.kind===`oauth`||t.kind===`device_code`){t.matchesPersonalAccount=t.id===`siwc`?matchesTokenSharingAccount:matchesPersonalAccount;if(t.id!==`siwc`){t.matchesSharedAccount=matchesSharedAccount;t.canReplaceGeneratedProfile=canReplaceGeneratedProfile}}return{...e,resolveFastModeSupport,resolveServiceTiers}}export{createOpenAIProvider as t};