( clientEnv: ClientEnv = env, )
| 97 | * Environment variable takes precedence. |
| 98 | */ |
| 99 | export const getChatGptOAuthCredentials = ( |
| 100 | clientEnv: ClientEnv = env, |
| 101 | ): ChatGptOAuthCredentials | null => { |
| 102 | // 1. Environment variable takes highest precedence |
| 103 | const envToken = getChatGptOAuthTokenFromEnv() |
| 104 | if (envToken) { |
| 105 | return { |
| 106 | accessToken: envToken, |
| 107 | refreshToken: '', |
| 108 | expiresAt: Date.now() + 365 * 24 * 60 * 60 * 1000, |
| 109 | connectedAt: Date.now(), |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // 2. Codebuff's own stored credentials |
| 114 | const credentialsPath = getCredentialsPath(clientEnv) |
| 115 | if (fs.existsSync(credentialsPath)) { |
| 116 | try { |
| 117 | const credentialsFile = fs.readFileSync(credentialsPath, 'utf8') |
| 118 | const parsed = credentialsFileSchema.safeParse(JSON.parse(credentialsFile)) |
| 119 | if (parsed.success && parsed.data.chatgptOAuth) { |
| 120 | return parsed.data.chatgptOAuth |
| 121 | } |
| 122 | } catch { |
| 123 | // Fall through |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return null |
| 128 | } |
| 129 | |
| 130 | export const saveChatGptOAuthCredentials = ( |
| 131 | credentials: ChatGptOAuthCredentials, |
no test coverage detected