mirror of
https://github.com/openai/codex.git
synced 2026-04-29 02:41:12 +03:00
The previous config approach had a few issues: 1. It is part of the config but not designed to be used externally 2. It had to be wired through many places (look at the +/- on this PR 3. It wasn't guaranteed to be set consistently everywhere because we don't have a super well defined way that configs stack. For example, the extension would configure during newConversation but anything that happened outside of that (like login) wouldn't get it. This env var approach is cleaner and also creates one less thing we have to deal with when coming up with a better holistic story around configs. One downside is that I removed the unit test testing for the override because I don't want to deal with setting the global env or spawning child processes and figuring out how to introspect their originator header. The new code is sufficiently simple and I tested it e2e that I feel as if this is still worth it.
30 lines
879 B
Rust
30 lines
879 B
Rust
use codex_core::CodexAuth;
|
|
use codex_protocol::mcp_protocol::AuthMode;
|
|
use std::path::Path;
|
|
use std::sync::LazyLock;
|
|
use std::sync::RwLock;
|
|
|
|
use codex_core::token_data::TokenData;
|
|
|
|
static CHATGPT_TOKEN: LazyLock<RwLock<Option<TokenData>>> = LazyLock::new(|| RwLock::new(None));
|
|
|
|
pub fn get_chatgpt_token_data() -> Option<TokenData> {
|
|
CHATGPT_TOKEN.read().ok()?.clone()
|
|
}
|
|
|
|
pub fn set_chatgpt_token_data(value: TokenData) {
|
|
if let Ok(mut guard) = CHATGPT_TOKEN.write() {
|
|
*guard = Some(value);
|
|
}
|
|
}
|
|
|
|
/// Initialize the ChatGPT token from auth.json file
|
|
pub async fn init_chatgpt_token_from_auth(codex_home: &Path) -> std::io::Result<()> {
|
|
let auth = CodexAuth::from_codex_home(codex_home, AuthMode::ChatGPT)?;
|
|
if let Some(auth) = auth {
|
|
let token_data = auth.get_token_data().await?;
|
|
set_chatgpt_token_data(token_data);
|
|
}
|
|
Ok(())
|
|
}
|