feat: add account removal to proxypilot-rs

This commit is contained in:
RCD
2026-04-05 14:51:34 +07:00
parent 0b80a392e7
commit 469fd38420
6 changed files with 99 additions and 4 deletions
+3 -2
View File
@@ -17,8 +17,8 @@ This branch carries the Rust rewrite line for ProxyPilot.
- a `tui` command that opens a minimal terminal operator view
- a TOML config model plus a separate local state file for saved accounts
- explicit Codex-facing routes for `/v1/models`, `/v1/chat/completions`, and `/v1/responses`
- CLI account commands for adding, importing, device-login, refresh, listing, and activating Codex accounts
- a TUI account/operator panel that shows models, local accounts, active account state, terminal actions for activate/refresh, and selected-account token expiry health
- CLI account commands for adding, importing, device-login, refresh, listing, activating, and removing Codex accounts
- a TUI account/operator panel that shows models, local accounts, active account state, terminal actions for activate/refresh/delete, and selected-account token expiry health
- runtime credential resolution that prefers the active saved account over the config fallback key
- end-to-end tests against a mocked upstream server
@@ -39,6 +39,7 @@ cargo run -p proxypilot-rs -- account add-codex --name primary --api-key sk-...
cargo run -p proxypilot-rs -- account import-codex --file ../auths/codex-example.json --activate
cargo run -p proxypilot-rs -- account login-codex-device --activate
cargo run -p proxypilot-rs -- account refresh-codex
cargo run -p proxypilot-rs -- account remove --name old-account
cargo run -p proxypilot-rs -- run --config proxypilot-rs.toml
```
+15
View File
@@ -68,6 +68,21 @@ pub fn activate_account(config: &AppConfig, config_path: &Path, name: String) ->
Ok(())
}
pub fn remove_account(config: &AppConfig, config_path: &Path, name: String) -> Result<()> {
let state_path = config.resolve_state_path(config_path);
let mut state = AccountState::load_or_default(&state_path)?;
state.remove_account(&name)?;
state.save(&state_path)?;
println!("removed account `{}`", name);
match state.active_account.as_deref() {
Some(active) => println!("active account is now `{}`", active),
None => println!("no active account remains"),
}
println!("state file: {}", state_path.display());
Ok(())
}
pub fn import_codex_account(
config: &AppConfig,
config_path: &Path,
+7
View File
@@ -97,4 +97,11 @@ pub enum AccountCommand {
#[arg(long)]
name: String,
},
/// Remove a saved account from the local state file.
Remove {
#[command(flatten)]
shared: SharedConfig,
#[arg(long)]
name: String,
},
}
+5
View File
@@ -84,6 +84,11 @@ async fn main() -> Result<()> {
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::activate_account(&config, &path, name)?;
}
AccountCommand::Remove { shared, name } => {
let path = resolve_config_path(shared.config);
let config = AppConfig::load(&path)?;
proxypilot_rs::accounts::remove_account(&config, &path, name)?;
}
},
}
+41
View File
@@ -118,6 +118,30 @@ impl AccountState {
}
}
pub fn remove_account(&mut self, name: &str) -> Result<()> {
let trimmed = name.trim();
if trimmed.is_empty() {
bail!("account name cannot be empty");
}
let original_len = self.accounts.len();
self.accounts.retain(|account| account.name != trimmed);
if self.accounts.len() == original_len {
bail!("no saved account named {}", trimmed);
}
if self.active_account.as_deref() == Some(trimmed) {
self.active_account = self
.accounts
.iter()
.find(|account| account.provider == "codex")
.map(|account| account.name.clone())
.or_else(|| self.accounts.first().map(|account| account.name.clone()));
}
Ok(())
}
pub fn active_codex_account(&self) -> Option<ActiveCodexAccount> {
let active_name = self.active_account.as_deref()?;
self.accounts
@@ -357,4 +381,21 @@ mod tests {
assert_eq!(account.expires_at.as_deref(), Some("2026-04-06T00:00:00Z"));
assert_eq!(account.source.as_deref(), Some("file"));
}
#[test]
fn removing_active_account_promotes_next_saved_account() {
let mut state = AccountState::default();
state
.add_or_replace_codex_account("primary".to_string(), "key-1".to_string(), true)
.unwrap();
state
.add_or_replace_codex_account("backup".to_string(), "key-2".to_string(), false)
.unwrap();
state.remove_account("primary").unwrap();
assert_eq!(state.active_account.as_deref(), Some("backup"));
assert_eq!(state.accounts.len(), 1);
assert_eq!(state.accounts[0].name, "backup");
}
}
+28 -2
View File
@@ -60,6 +60,7 @@ async fn run_loop(
KeyCode::Up => app.move_selection_up(),
KeyCode::Down => app.move_selection_down(),
KeyCode::Char('a') => app.activate_selected_account(),
KeyCode::Char('d') => app.delete_selected_account(),
KeyCode::Char('f') => app.refresh_selected_account().await,
_ => {}
}
@@ -97,7 +98,7 @@ impl TuiApp {
account_count: 0,
models: Vec::new(),
feedback: "Use arrows to select an account, `a` to activate, `f` to refresh."
.to_string(),
.replace("`f` to refresh.", "`f` to refresh, `d` to delete."),
feedback_color: Color::DarkGray,
last_poll: Instant::now() - Duration::from_secs(3),
}
@@ -244,6 +245,31 @@ impl TuiApp {
}
}
fn delete_selected_account(&mut self) {
let Some(selected) = self.accounts.get(self.selected_account_idx) else {
self.feedback = "No saved Codex account to delete.".to_string();
self.feedback_color = Color::Yellow;
return;
};
let state_path = self.config.resolve_state_path(Path::new(&self.config_path));
match AccountState::load_or_default(&state_path).and_then(|mut state| {
state.remove_account(&selected.name)?;
state.save(&state_path)?;
Ok(())
}) {
Ok(()) => {
self.feedback = format!("Deleted account `{}`.", selected.name);
self.feedback_color = Color::Green;
self.refresh_accounts();
}
Err(err) => {
self.feedback = format!("Failed to delete account: {err}");
self.feedback_color = Color::Red;
}
}
}
async fn refresh_selected_account(&mut self) {
let Some(selected) = self.accounts.get(self.selected_account_idx) else {
self.feedback = "No saved Codex account to refresh.".to_string();
@@ -420,7 +446,7 @@ fn render(frame: &mut ratatui::Frame<'_>, app: &TuiApp) {
Style::default().fg(app.feedback_color),
)]),
Line::from(
"q quit | r refresh health | arrows move | a activate | f refresh account",
"q quit | r refresh health | arrows move | a activate | f refresh | d delete",
),
])
.block(Block::default().borders(Borders::ALL).title("Keys"));