2025-08-20 13:47:24 -07:00
|
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
|
|
|
2025-08-22 13:10:11 -07:00
|
|
|
|
use codex_login::AuthManager;
|
2025-08-14 19:42:14 -07:00
|
|
|
|
use codex_login::CLIENT_ID;
|
|
|
|
|
|
use codex_login::ServerOptions;
|
2025-08-18 17:32:03 -07:00
|
|
|
|
use codex_login::ShutdownHandle;
|
2025-08-14 19:42:14 -07:00
|
|
|
|
use codex_login::run_login_server;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
use crossterm::event::KeyCode;
|
|
|
|
|
|
use crossterm::event::KeyEvent;
|
|
|
|
|
|
use ratatui::buffer::Buffer;
|
|
|
|
|
|
use ratatui::layout::Rect;
|
|
|
|
|
|
use ratatui::prelude::Widget;
|
|
|
|
|
|
use ratatui::style::Color;
|
|
|
|
|
|
use ratatui::style::Modifier;
|
|
|
|
|
|
use ratatui::style::Style;
|
fix: clean up styles & colors and define in styles.md (#2401)
New style guide:
# Headers, primary, and secondary text
- **Headers:** Use `bold`. For markdown with various header levels,
leave in the `#` signs.
- **Primary text:** Default.
- **Secondary text:** Use `dim`.
# Foreground colors
- **Default:** Most of the time, just use the default foreground color.
`reset` can help get it back.
- **Selection:** Use ANSI `blue`. (Ed & AE want to make this cyan too,
but we'll do that in a followup since it's riskier in different themes.)
- **User input tips and status indicators:** Use ANSI `cyan`.
- **Success and additions:** Use ANSI `green`.
- **Errors, failures and deletions:** Use ANSI `red`.
- **Codex:** Use ANSI `magenta`.
# Avoid
- Avoid custom colors because there's no guarantee that they'll contrast
well or look good on various terminal color themes.
- Avoid ANSI `black`, `white`, `yellow` as foreground colors because the
terminal theme will do a better job. (Use `reset` if you need to in
order to get those.) The exception is if you need contrast rendering
over a manually colored background.
(There are some rules to try to catch this in `clippy.toml`.)
# Testing
Tested in a variety of light and dark color themes in Terminal, iTerm2, and Ghostty.
2025-08-18 08:26:29 -07:00
|
|
|
|
use ratatui::style::Stylize;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
use ratatui::text::Line;
|
|
|
|
|
|
use ratatui::text::Span;
|
|
|
|
|
|
use ratatui::widgets::Paragraph;
|
|
|
|
|
|
use ratatui::widgets::WidgetRef;
|
|
|
|
|
|
use ratatui::widgets::Wrap;
|
|
|
|
|
|
|
|
|
|
|
|
use codex_login::AuthMode;
|
2025-08-20 13:47:24 -07:00
|
|
|
|
use std::sync::RwLock;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
|
2025-08-18 20:22:48 -07:00
|
|
|
|
use crate::LoginStatus;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
use crate::onboarding::onboarding_screen::KeyboardHandler;
|
2025-08-06 19:39:07 -07:00
|
|
|
|
use crate::onboarding::onboarding_screen::StepStateProvider;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
use crate::shimmer::shimmer_spans;
|
2025-08-20 13:47:24 -07:00
|
|
|
|
use crate::tui::FrameRequester;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
use std::path::PathBuf;
|
2025-08-20 13:47:24 -07:00
|
|
|
|
use std::sync::Arc;
|
2025-08-06 19:39:07 -07:00
|
|
|
|
|
|
|
|
|
|
use super::onboarding_screen::StepState;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
|
#[derive(Clone)]
|
2025-08-06 15:22:14 -07:00
|
|
|
|
pub(crate) enum SignInState {
|
|
|
|
|
|
PickMode,
|
2025-08-08 18:30:34 -07:00
|
|
|
|
ChatGptContinueInBrowser(ContinueInBrowserState),
|
2025-08-06 19:39:07 -07:00
|
|
|
|
ChatGptSuccessMessage,
|
2025-08-06 15:22:14 -07:00
|
|
|
|
ChatGptSuccess,
|
2025-08-06 19:39:07 -07:00
|
|
|
|
EnvVarMissing,
|
|
|
|
|
|
EnvVarFound,
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
|
#[derive(Clone)]
|
2025-08-14 16:59:47 -04:00
|
|
|
|
/// Used to manage the lifecycle of SpawnedLogin and ensure it gets cleaned up.
|
2025-08-06 15:22:14 -07:00
|
|
|
|
pub(crate) struct ContinueInBrowserState {
|
2025-08-14 19:42:14 -07:00
|
|
|
|
auth_url: String,
|
2025-08-20 13:47:24 -07:00
|
|
|
|
shutdown_flag: Option<ShutdownHandle>,
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
2025-08-18 17:32:03 -07:00
|
|
|
|
|
2025-08-06 15:22:14 -07:00
|
|
|
|
impl Drop for ContinueInBrowserState {
|
|
|
|
|
|
fn drop(&mut self) {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
if let Some(handle) = &self.shutdown_flag {
|
|
|
|
|
|
handle.shutdown();
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl KeyboardHandler for AuthModeWidget {
|
2025-08-06 19:39:07 -07:00
|
|
|
|
fn handle_key_event(&mut self, key_event: KeyEvent) {
|
2025-08-06 15:22:14 -07:00
|
|
|
|
match key_event.code {
|
|
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => {
|
2025-08-06 19:39:07 -07:00
|
|
|
|
self.highlighted_mode = AuthMode::ChatGPT;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
KeyCode::Down | KeyCode::Char('j') => {
|
2025-08-06 19:39:07 -07:00
|
|
|
|
self.highlighted_mode = AuthMode::ApiKey;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
KeyCode::Char('1') => {
|
|
|
|
|
|
self.start_chatgpt_login();
|
|
|
|
|
|
}
|
2025-08-06 19:39:07 -07:00
|
|
|
|
KeyCode::Char('2') => self.verify_api_key(),
|
2025-08-20 13:47:24 -07:00
|
|
|
|
KeyCode::Enter => {
|
|
|
|
|
|
let sign_in_state = { (*self.sign_in_state.read().unwrap()).clone() };
|
|
|
|
|
|
match sign_in_state {
|
|
|
|
|
|
SignInState::PickMode => match self.highlighted_mode {
|
|
|
|
|
|
AuthMode::ChatGPT => {
|
|
|
|
|
|
self.start_chatgpt_login();
|
|
|
|
|
|
}
|
|
|
|
|
|
AuthMode::ApiKey => {
|
|
|
|
|
|
self.verify_api_key();
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
SignInState::EnvVarMissing => {
|
|
|
|
|
|
*self.sign_in_state.write().unwrap() = SignInState::PickMode;
|
|
|
|
|
|
}
|
|
|
|
|
|
SignInState::ChatGptSuccessMessage => {
|
|
|
|
|
|
*self.sign_in_state.write().unwrap() = SignInState::ChatGptSuccess;
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {}
|
2025-08-06 19:39:07 -07:00
|
|
|
|
}
|
2025-08-20 13:47:24 -07:00
|
|
|
|
}
|
2025-08-06 15:22:14 -07:00
|
|
|
|
KeyCode::Esc => {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
tracing::info!("Esc pressed");
|
|
|
|
|
|
let sign_in_state = { (*self.sign_in_state.read().unwrap()).clone() };
|
|
|
|
|
|
if matches!(sign_in_state, SignInState::ChatGptContinueInBrowser(_)) {
|
|
|
|
|
|
*self.sign_in_state.write().unwrap() = SignInState::PickMode;
|
|
|
|
|
|
self.request_frame.schedule_frame();
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-06 19:39:07 -07:00
|
|
|
|
_ => {}
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
|
#[derive(Clone)]
|
2025-08-06 15:22:14 -07:00
|
|
|
|
pub(crate) struct AuthModeWidget {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
pub request_frame: FrameRequester,
|
2025-08-06 19:39:07 -07:00
|
|
|
|
pub highlighted_mode: AuthMode,
|
2025-08-06 15:22:14 -07:00
|
|
|
|
pub error: Option<String>,
|
2025-08-20 13:47:24 -07:00
|
|
|
|
pub sign_in_state: Arc<RwLock<SignInState>>,
|
2025-08-06 15:22:14 -07:00
|
|
|
|
pub codex_home: PathBuf,
|
2025-08-18 20:22:48 -07:00
|
|
|
|
pub login_status: LoginStatus,
|
|
|
|
|
|
pub preferred_auth_method: AuthMode,
|
2025-08-22 13:10:11 -07:00
|
|
|
|
pub auth_manager: Arc<AuthManager>,
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl AuthModeWidget {
|
|
|
|
|
|
fn render_pick_mode(&self, area: Rect, buf: &mut Buffer) {
|
|
|
|
|
|
let mut lines: Vec<Line> = vec![
|
|
|
|
|
|
Line::from(vec![
|
|
|
|
|
|
Span::raw("> "),
|
|
|
|
|
|
Span::styled(
|
2025-08-07 03:29:33 -07:00
|
|
|
|
"Sign in with ChatGPT to use Codex as part of your paid plan",
|
|
|
|
|
|
Style::default().add_modifier(Modifier::BOLD),
|
|
|
|
|
|
),
|
|
|
|
|
|
]),
|
|
|
|
|
|
Line::from(vec![
|
|
|
|
|
|
Span::raw(" "),
|
|
|
|
|
|
Span::styled(
|
|
|
|
|
|
"or connect an API key for usage-based billing",
|
2025-08-06 15:22:14 -07:00
|
|
|
|
Style::default().add_modifier(Modifier::BOLD),
|
|
|
|
|
|
),
|
|
|
|
|
|
]),
|
|
|
|
|
|
Line::from(""),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2025-08-18 20:22:48 -07:00
|
|
|
|
// If the user is already authenticated but the method differs from their
|
|
|
|
|
|
// preferred auth method, show a brief explanation.
|
2025-08-19 13:22:02 -07:00
|
|
|
|
if let LoginStatus::AuthMode(current) = self.login_status
|
|
|
|
|
|
&& current != self.preferred_auth_method
|
|
|
|
|
|
{
|
|
|
|
|
|
let to_label = |mode: AuthMode| match mode {
|
|
|
|
|
|
AuthMode::ApiKey => "API key",
|
|
|
|
|
|
AuthMode::ChatGPT => "ChatGPT",
|
|
|
|
|
|
};
|
|
|
|
|
|
let msg = format!(
|
|
|
|
|
|
" You’re currently using {} while your preferred method is {}.",
|
|
|
|
|
|
to_label(current),
|
|
|
|
|
|
to_label(self.preferred_auth_method)
|
|
|
|
|
|
);
|
|
|
|
|
|
lines.push(Line::from(msg).style(Style::default()));
|
|
|
|
|
|
lines.push(Line::from(""));
|
2025-08-18 20:22:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 15:22:14 -07:00
|
|
|
|
let create_mode_item = |idx: usize,
|
|
|
|
|
|
selected_mode: AuthMode,
|
|
|
|
|
|
text: &str,
|
|
|
|
|
|
description: &str|
|
|
|
|
|
|
-> Vec<Line<'static>> {
|
2025-08-06 19:39:07 -07:00
|
|
|
|
let is_selected = self.highlighted_mode == selected_mode;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
let caret = if is_selected { ">" } else { " " };
|
|
|
|
|
|
|
|
|
|
|
|
let line1 = if is_selected {
|
|
|
|
|
|
Line::from(vec![
|
2025-08-18 09:02:25 -07:00
|
|
|
|
format!("{} {}. ", caret, idx + 1).cyan().dim(),
|
|
|
|
|
|
text.to_string().cyan(),
|
2025-08-06 15:22:14 -07:00
|
|
|
|
])
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Line::from(format!(" {}. {text}", idx + 1))
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let line2 = if is_selected {
|
|
|
|
|
|
Line::from(format!(" {description}"))
|
2025-08-18 09:02:25 -07:00
|
|
|
|
.fg(Color::Cyan)
|
fix: clean up styles & colors and define in styles.md (#2401)
New style guide:
# Headers, primary, and secondary text
- **Headers:** Use `bold`. For markdown with various header levels,
leave in the `#` signs.
- **Primary text:** Default.
- **Secondary text:** Use `dim`.
# Foreground colors
- **Default:** Most of the time, just use the default foreground color.
`reset` can help get it back.
- **Selection:** Use ANSI `blue`. (Ed & AE want to make this cyan too,
but we'll do that in a followup since it's riskier in different themes.)
- **User input tips and status indicators:** Use ANSI `cyan`.
- **Success and additions:** Use ANSI `green`.
- **Errors, failures and deletions:** Use ANSI `red`.
- **Codex:** Use ANSI `magenta`.
# Avoid
- Avoid custom colors because there's no guarantee that they'll contrast
well or look good on various terminal color themes.
- Avoid ANSI `black`, `white`, `yellow` as foreground colors because the
terminal theme will do a better job. (Use `reset` if you need to in
order to get those.) The exception is if you need contrast rendering
over a manually colored background.
(There are some rules to try to catch this in `clippy.toml`.)
# Testing
Tested in a variety of light and dark color themes in Terminal, iTerm2, and Ghostty.
2025-08-18 08:26:29 -07:00
|
|
|
|
.add_modifier(Modifier::DIM)
|
2025-08-06 15:22:14 -07:00
|
|
|
|
} else {
|
|
|
|
|
|
Line::from(format!(" {description}"))
|
|
|
|
|
|
.style(Style::default().add_modifier(Modifier::DIM))
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
vec![line1, line2]
|
|
|
|
|
|
};
|
2025-08-18 20:22:48 -07:00
|
|
|
|
let chatgpt_label = if matches!(self.login_status, LoginStatus::AuthMode(AuthMode::ChatGPT))
|
|
|
|
|
|
{
|
|
|
|
|
|
"Continue using ChatGPT"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
"Sign in with ChatGPT"
|
|
|
|
|
|
};
|
2025-08-06 15:22:14 -07:00
|
|
|
|
|
|
|
|
|
|
lines.extend(create_mode_item(
|
|
|
|
|
|
0,
|
|
|
|
|
|
AuthMode::ChatGPT,
|
2025-08-18 20:22:48 -07:00
|
|
|
|
chatgpt_label,
|
2025-08-07 03:29:33 -07:00
|
|
|
|
"Usage included with Plus, Pro, and Team plans",
|
2025-08-06 15:22:14 -07:00
|
|
|
|
));
|
2025-08-18 20:22:48 -07:00
|
|
|
|
let api_key_label = if matches!(self.login_status, LoginStatus::AuthMode(AuthMode::ApiKey))
|
|
|
|
|
|
{
|
|
|
|
|
|
"Continue using API key"
|
|
|
|
|
|
} else {
|
|
|
|
|
|
"Provide your own API key"
|
|
|
|
|
|
};
|
2025-08-06 15:22:14 -07:00
|
|
|
|
lines.extend(create_mode_item(
|
|
|
|
|
|
1,
|
|
|
|
|
|
AuthMode::ApiKey,
|
2025-08-18 20:22:48 -07:00
|
|
|
|
api_key_label,
|
2025-08-07 03:29:33 -07:00
|
|
|
|
"Pay for what you use",
|
2025-08-06 15:22:14 -07:00
|
|
|
|
));
|
|
|
|
|
|
lines.push(Line::from(""));
|
|
|
|
|
|
lines.push(
|
2025-08-18 09:02:25 -07:00
|
|
|
|
// AE: Following styles.md, this should probably be Cyan because it's a user input tip.
|
|
|
|
|
|
// But leaving this for a future cleanup.
|
2025-08-07 03:29:33 -07:00
|
|
|
|
Line::from(" Press Enter to continue")
|
2025-08-06 15:22:14 -07:00
|
|
|
|
.style(Style::default().add_modifier(Modifier::DIM)),
|
|
|
|
|
|
);
|
|
|
|
|
|
if let Some(err) = &self.error {
|
|
|
|
|
|
lines.push(Line::from(""));
|
|
|
|
|
|
lines.push(Line::from(Span::styled(
|
|
|
|
|
|
err.as_str(),
|
|
|
|
|
|
Style::default().fg(Color::Red),
|
|
|
|
|
|
)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Paragraph::new(lines)
|
|
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn render_continue_in_browser(&self, area: Rect, buf: &mut Buffer) {
|
|
|
|
|
|
let mut spans = vec![Span::from("> ")];
|
2025-08-14 16:59:47 -04:00
|
|
|
|
// Schedule a follow-up frame to keep the shimmer animation going.
|
2025-08-20 13:47:24 -07:00
|
|
|
|
self.request_frame
|
|
|
|
|
|
.schedule_frame_in(std::time::Duration::from_millis(100));
|
2025-08-14 16:59:47 -04:00
|
|
|
|
spans.extend(shimmer_spans("Finish signing in via your browser"));
|
2025-08-08 18:30:34 -07:00
|
|
|
|
let mut lines = vec![Line::from(spans), Line::from("")];
|
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
|
let sign_in_state = self.sign_in_state.read().unwrap();
|
|
|
|
|
|
if let SignInState::ChatGptContinueInBrowser(state) = &*sign_in_state
|
2025-08-19 13:22:02 -07:00
|
|
|
|
&& !state.auth_url.is_empty()
|
|
|
|
|
|
{
|
|
|
|
|
|
lines.push(Line::from(" If the link doesn't open automatically, open the following link to authenticate:"));
|
|
|
|
|
|
lines.push(Line::from(vec![
|
|
|
|
|
|
Span::raw(" "),
|
|
|
|
|
|
state.auth_url.as_str().cyan().underlined(),
|
|
|
|
|
|
]));
|
|
|
|
|
|
lines.push(Line::from(""));
|
2025-08-08 18:30:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lines.push(
|
2025-08-07 03:29:33 -07:00
|
|
|
|
Line::from(" Press Esc to cancel").style(Style::default().add_modifier(Modifier::DIM)),
|
2025-08-08 18:30:34 -07:00
|
|
|
|
);
|
2025-08-06 15:22:14 -07:00
|
|
|
|
Paragraph::new(lines)
|
|
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 19:39:07 -07:00
|
|
|
|
fn render_chatgpt_success_message(&self, area: Rect, buf: &mut Buffer) {
|
2025-08-06 15:22:14 -07:00
|
|
|
|
let lines = vec![
|
fix: clean up styles & colors and define in styles.md (#2401)
New style guide:
# Headers, primary, and secondary text
- **Headers:** Use `bold`. For markdown with various header levels,
leave in the `#` signs.
- **Primary text:** Default.
- **Secondary text:** Use `dim`.
# Foreground colors
- **Default:** Most of the time, just use the default foreground color.
`reset` can help get it back.
- **Selection:** Use ANSI `blue`. (Ed & AE want to make this cyan too,
but we'll do that in a followup since it's riskier in different themes.)
- **User input tips and status indicators:** Use ANSI `cyan`.
- **Success and additions:** Use ANSI `green`.
- **Errors, failures and deletions:** Use ANSI `red`.
- **Codex:** Use ANSI `magenta`.
# Avoid
- Avoid custom colors because there's no guarantee that they'll contrast
well or look good on various terminal color themes.
- Avoid ANSI `black`, `white`, `yellow` as foreground colors because the
terminal theme will do a better job. (Use `reset` if you need to in
order to get those.) The exception is if you need contrast rendering
over a manually colored background.
(There are some rules to try to catch this in `clippy.toml`.)
# Testing
Tested in a variety of light and dark color themes in Terminal, iTerm2, and Ghostty.
2025-08-18 08:26:29 -07:00
|
|
|
|
Line::from("✓ Signed in with your ChatGPT account").fg(Color::Green),
|
2025-08-06 15:22:14 -07:00
|
|
|
|
Line::from(""),
|
|
|
|
|
|
Line::from("> Before you start:"),
|
|
|
|
|
|
Line::from(""),
|
2025-08-07 03:29:33 -07:00
|
|
|
|
Line::from(" Decide how much autonomy you want to grant Codex"),
|
|
|
|
|
|
Line::from(vec![
|
|
|
|
|
|
Span::raw(" For more details see the "),
|
|
|
|
|
|
Span::styled(
|
|
|
|
|
|
"\u{1b}]8;;https://github.com/openai/codex\u{7}Codex docs\u{1b}]8;;\u{7}",
|
|
|
|
|
|
Style::default().add_modifier(Modifier::UNDERLINED),
|
|
|
|
|
|
),
|
|
|
|
|
|
])
|
|
|
|
|
|
.style(Style::default().add_modifier(Modifier::DIM)),
|
2025-08-06 15:22:14 -07:00
|
|
|
|
Line::from(""),
|
fix: clean up styles & colors and define in styles.md (#2401)
New style guide:
# Headers, primary, and secondary text
- **Headers:** Use `bold`. For markdown with various header levels,
leave in the `#` signs.
- **Primary text:** Default.
- **Secondary text:** Use `dim`.
# Foreground colors
- **Default:** Most of the time, just use the default foreground color.
`reset` can help get it back.
- **Selection:** Use ANSI `blue`. (Ed & AE want to make this cyan too,
but we'll do that in a followup since it's riskier in different themes.)
- **User input tips and status indicators:** Use ANSI `cyan`.
- **Success and additions:** Use ANSI `green`.
- **Errors, failures and deletions:** Use ANSI `red`.
- **Codex:** Use ANSI `magenta`.
# Avoid
- Avoid custom colors because there's no guarantee that they'll contrast
well or look good on various terminal color themes.
- Avoid ANSI `black`, `white`, `yellow` as foreground colors because the
terminal theme will do a better job. (Use `reset` if you need to in
order to get those.) The exception is if you need contrast rendering
over a manually colored background.
(There are some rules to try to catch this in `clippy.toml`.)
# Testing
Tested in a variety of light and dark color themes in Terminal, iTerm2, and Ghostty.
2025-08-18 08:26:29 -07:00
|
|
|
|
Line::from(" Codex can make mistakes"),
|
2025-08-07 03:29:33 -07:00
|
|
|
|
Line::from(" Review the code it writes and commands it runs")
|
2025-08-06 15:22:14 -07:00
|
|
|
|
.style(Style::default().add_modifier(Modifier::DIM)),
|
|
|
|
|
|
Line::from(""),
|
|
|
|
|
|
Line::from(" Powered by your ChatGPT account"),
|
2025-08-07 03:29:33 -07:00
|
|
|
|
Line::from(vec![
|
|
|
|
|
|
Span::raw(" Uses your plan's rate limits and "),
|
|
|
|
|
|
Span::styled(
|
|
|
|
|
|
"\u{1b}]8;;https://chatgpt.com/#settings\u{7}training data preferences\u{1b}]8;;\u{7}",
|
|
|
|
|
|
Style::default().add_modifier(Modifier::UNDERLINED),
|
|
|
|
|
|
),
|
|
|
|
|
|
])
|
|
|
|
|
|
.style(Style::default().add_modifier(Modifier::DIM)),
|
2025-08-06 15:22:14 -07:00
|
|
|
|
Line::from(""),
|
2025-08-18 09:02:25 -07:00
|
|
|
|
Line::from(" Press Enter to continue").fg(Color::Cyan),
|
2025-08-06 15:22:14 -07:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
Paragraph::new(lines)
|
|
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 19:39:07 -07:00
|
|
|
|
fn render_chatgpt_success(&self, area: Rect, buf: &mut Buffer) {
|
fix: clean up styles & colors and define in styles.md (#2401)
New style guide:
# Headers, primary, and secondary text
- **Headers:** Use `bold`. For markdown with various header levels,
leave in the `#` signs.
- **Primary text:** Default.
- **Secondary text:** Use `dim`.
# Foreground colors
- **Default:** Most of the time, just use the default foreground color.
`reset` can help get it back.
- **Selection:** Use ANSI `blue`. (Ed & AE want to make this cyan too,
but we'll do that in a followup since it's riskier in different themes.)
- **User input tips and status indicators:** Use ANSI `cyan`.
- **Success and additions:** Use ANSI `green`.
- **Errors, failures and deletions:** Use ANSI `red`.
- **Codex:** Use ANSI `magenta`.
# Avoid
- Avoid custom colors because there's no guarantee that they'll contrast
well or look good on various terminal color themes.
- Avoid ANSI `black`, `white`, `yellow` as foreground colors because the
terminal theme will do a better job. (Use `reset` if you need to in
order to get those.) The exception is if you need contrast rendering
over a manually colored background.
(There are some rules to try to catch this in `clippy.toml`.)
# Testing
Tested in a variety of light and dark color themes in Terminal, iTerm2, and Ghostty.
2025-08-18 08:26:29 -07:00
|
|
|
|
let lines = vec![Line::from("✓ Signed in with your ChatGPT account").fg(Color::Green)];
|
2025-08-06 19:39:07 -07:00
|
|
|
|
|
|
|
|
|
|
Paragraph::new(lines)
|
|
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn render_env_var_found(&self, area: Rect, buf: &mut Buffer) {
|
fix: clean up styles & colors and define in styles.md (#2401)
New style guide:
# Headers, primary, and secondary text
- **Headers:** Use `bold`. For markdown with various header levels,
leave in the `#` signs.
- **Primary text:** Default.
- **Secondary text:** Use `dim`.
# Foreground colors
- **Default:** Most of the time, just use the default foreground color.
`reset` can help get it back.
- **Selection:** Use ANSI `blue`. (Ed & AE want to make this cyan too,
but we'll do that in a followup since it's riskier in different themes.)
- **User input tips and status indicators:** Use ANSI `cyan`.
- **Success and additions:** Use ANSI `green`.
- **Errors, failures and deletions:** Use ANSI `red`.
- **Codex:** Use ANSI `magenta`.
# Avoid
- Avoid custom colors because there's no guarantee that they'll contrast
well or look good on various terminal color themes.
- Avoid ANSI `black`, `white`, `yellow` as foreground colors because the
terminal theme will do a better job. (Use `reset` if you need to in
order to get those.) The exception is if you need contrast rendering
over a manually colored background.
(There are some rules to try to catch this in `clippy.toml`.)
# Testing
Tested in a variety of light and dark color themes in Terminal, iTerm2, and Ghostty.
2025-08-18 08:26:29 -07:00
|
|
|
|
let lines = vec![Line::from("✓ Using OPENAI_API_KEY").fg(Color::Green)];
|
2025-08-06 19:39:07 -07:00
|
|
|
|
|
|
|
|
|
|
Paragraph::new(lines)
|
|
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn render_env_var_missing(&self, area: Rect, buf: &mut Buffer) {
|
|
|
|
|
|
let lines = vec![
|
2025-08-07 03:29:33 -07:00
|
|
|
|
Line::from(
|
|
|
|
|
|
" To use Codex with the OpenAI API, set OPENAI_API_KEY in your environment",
|
|
|
|
|
|
)
|
2025-08-18 09:02:25 -07:00
|
|
|
|
.style(Style::default().fg(Color::Cyan)),
|
2025-08-06 19:39:07 -07:00
|
|
|
|
Line::from(""),
|
|
|
|
|
|
Line::from(" Press Enter to return")
|
|
|
|
|
|
.style(Style::default().add_modifier(Modifier::DIM)),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
Paragraph::new(lines)
|
|
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
|
|
.render(area, buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn start_chatgpt_login(&mut self) {
|
2025-08-18 20:22:48 -07:00
|
|
|
|
// If we're already authenticated with ChatGPT, don't start a new login –
|
|
|
|
|
|
// just proceed to the success message flow.
|
|
|
|
|
|
if matches!(self.login_status, LoginStatus::AuthMode(AuthMode::ChatGPT)) {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
*self.sign_in_state.write().unwrap() = SignInState::ChatGptSuccess;
|
|
|
|
|
|
self.request_frame.schedule_frame();
|
2025-08-18 20:22:48 -07:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 15:22:14 -07:00
|
|
|
|
self.error = None;
|
2025-08-14 19:42:14 -07:00
|
|
|
|
let opts = ServerOptions::new(self.codex_home.clone(), CLIENT_ID.to_string());
|
2025-08-20 13:47:24 -07:00
|
|
|
|
match run_login_server(opts) {
|
2025-08-06 15:22:14 -07:00
|
|
|
|
Ok(child) => {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
let sign_in_state = self.sign_in_state.clone();
|
|
|
|
|
|
let request_frame = self.request_frame.clone();
|
2025-08-22 13:10:11 -07:00
|
|
|
|
let auth_manager = self.auth_manager.clone();
|
2025-08-20 13:47:24 -07:00
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
|
let auth_url = child.auth_url.clone();
|
|
|
|
|
|
{
|
|
|
|
|
|
*sign_in_state.write().unwrap() =
|
|
|
|
|
|
SignInState::ChatGptContinueInBrowser(ContinueInBrowserState {
|
|
|
|
|
|
auth_url,
|
|
|
|
|
|
shutdown_flag: Some(child.cancel_handle()),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
request_frame.schedule_frame();
|
|
|
|
|
|
let r = child.block_until_done().await;
|
|
|
|
|
|
match r {
|
|
|
|
|
|
Ok(()) => {
|
2025-08-22 13:10:11 -07:00
|
|
|
|
// Force the auth manager to reload the new auth information.
|
|
|
|
|
|
auth_manager.reload();
|
|
|
|
|
|
|
2025-08-20 13:47:24 -07:00
|
|
|
|
*sign_in_state.write().unwrap() = SignInState::ChatGptSuccessMessage;
|
|
|
|
|
|
request_frame.schedule_frame();
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {
|
|
|
|
|
|
*sign_in_state.write().unwrap() = SignInState::PickMode;
|
|
|
|
|
|
// self.error = Some(e.to_string());
|
|
|
|
|
|
request_frame.schedule_frame();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-18 17:23:40 -07:00
|
|
|
|
});
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
Err(e) => {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
*self.sign_in_state.write().unwrap() = SignInState::PickMode;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
self.error = Some(e.to_string());
|
2025-08-20 13:47:24 -07:00
|
|
|
|
self.request_frame.schedule_frame();
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// TODO: Read/write from the correct hierarchy config overrides + auth json + OPENAI_API_KEY.
|
2025-08-06 19:39:07 -07:00
|
|
|
|
fn verify_api_key(&mut self) {
|
2025-08-18 20:22:48 -07:00
|
|
|
|
if matches!(self.login_status, LoginStatus::AuthMode(AuthMode::ApiKey)) {
|
|
|
|
|
|
// We already have an API key configured (e.g., from auth.json or env),
|
|
|
|
|
|
// so mark this step complete immediately.
|
2025-08-20 13:47:24 -07:00
|
|
|
|
*self.sign_in_state.write().unwrap() = SignInState::EnvVarFound;
|
2025-08-18 20:22:48 -07:00
|
|
|
|
} else {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
*self.sign_in_state.write().unwrap() = SignInState::EnvVarMissing;
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
2025-08-20 13:47:24 -07:00
|
|
|
|
self.request_frame.schedule_frame();
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
2025-08-18 17:23:40 -07:00
|
|
|
|
}
|
2025-08-06 15:22:14 -07:00
|
|
|
|
|
2025-08-06 19:39:07 -07:00
|
|
|
|
impl StepStateProvider for AuthModeWidget {
|
|
|
|
|
|
fn get_step_state(&self) -> StepState {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
let sign_in_state = self.sign_in_state.read().unwrap();
|
|
|
|
|
|
match &*sign_in_state {
|
2025-08-06 19:39:07 -07:00
|
|
|
|
SignInState::PickMode
|
|
|
|
|
|
| SignInState::EnvVarMissing
|
|
|
|
|
|
| SignInState::ChatGptContinueInBrowser(_)
|
|
|
|
|
|
| SignInState::ChatGptSuccessMessage => StepState::InProgress,
|
|
|
|
|
|
SignInState::ChatGptSuccess | SignInState::EnvVarFound => StepState::Complete,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 15:22:14 -07:00
|
|
|
|
impl WidgetRef for AuthModeWidget {
|
|
|
|
|
|
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
|
2025-08-20 13:47:24 -07:00
|
|
|
|
let sign_in_state = self.sign_in_state.read().unwrap();
|
|
|
|
|
|
match &*sign_in_state {
|
2025-08-06 15:22:14 -07:00
|
|
|
|
SignInState::PickMode => {
|
|
|
|
|
|
self.render_pick_mode(area, buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
SignInState::ChatGptContinueInBrowser(_) => {
|
|
|
|
|
|
self.render_continue_in_browser(area, buf);
|
|
|
|
|
|
}
|
2025-08-06 19:39:07 -07:00
|
|
|
|
SignInState::ChatGptSuccessMessage => {
|
|
|
|
|
|
self.render_chatgpt_success_message(area, buf);
|
|
|
|
|
|
}
|
2025-08-06 15:22:14 -07:00
|
|
|
|
SignInState::ChatGptSuccess => {
|
|
|
|
|
|
self.render_chatgpt_success(area, buf);
|
|
|
|
|
|
}
|
2025-08-06 19:39:07 -07:00
|
|
|
|
SignInState::EnvVarMissing => {
|
|
|
|
|
|
self.render_env_var_missing(area, buf);
|
|
|
|
|
|
}
|
|
|
|
|
|
SignInState::EnvVarFound => {
|
|
|
|
|
|
self.render_env_var_found(area, buf);
|
|
|
|
|
|
}
|
2025-08-06 15:22:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|