Files
codex/codex-rs/tui/src/chatwidget/rate_limits.rs
T
Eric Traut 1698314bb3 Scope TUI rate limits to the active thread's provider (#63838)
## Why

A resumed thread can use a different provider from the server default.
A successful account quota read can then show warnings and banners for
quota the thread does not use.

## What changed

Apply account quota reads and prefetch quota only when the active
provider uses account quota. Preserve remote provider definitions when
reloading local preferences, refresh custom provider definitions on
remote resume, and clear cached quota when the provider or its
definition changes.

For providers with independent quota, use turn updates for warnings and
composer notices. Keep omitted windows from sparse updates, clear the
notice when reported usage falls below the warning threshold, and omit
the `/status` hint. Limit the model-switch prompt to the built-in OpenAI
provider.

## Testing

Extend coverage for starting and resuming sessions across providers,
including an authentication change under the same provider ID. Verify
that custom-provider turn updates can show and clear a quota notice
without triggering a model-switch prompt.

GitOrigin-RevId: 4e6c95efe46ec420cee7b09d5f5a94862307530c
2026-11-25 06:20:31 +00:00

762 lines
29 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Rate-limit warning, prompt, and notice surfaces, plus quota-aware account refresh cadence.
use super::*;
pub(super) const WORKSPACE_NUDGE_VIEW_ID: &str = "workspace-usage-nudge";
use crate::bottom_pane::ActionableBanner;
use crate::model_catalog::LUNA_RESERVE_MODEL;
use codex_app_server_protocol::CodexErrorInfo as AppServerCodexErrorInfo;
use codex_model_provider_info::OPENAI_PROVIDER_ID;
use uuid::Uuid;
pub(super) struct PendingCreditsNudge {
request_id: Uuid,
credit_type: AddCreditsNudgeCreditType,
}
pub(super) const NUDGE_MODEL_SLUG: &str = crate::model_catalog::LUNA_MODEL;
pub(super) const RATE_LIMIT_SWITCH_PROMPT_THRESHOLD: i32 = 90;
pub(super) const RATE_LIMIT_SWITCH_PROMPT_VIEW_ID: &str = "rate-limit-switch-prompt";
const RATE_LIMIT_WARNING_THRESHOLDS: [f64; 4] = [50.0, 75.0, 90.0, 95.0];
const PRIMARY_LIMIT_FALLBACK_LABEL: &str = "usage";
const SECONDARY_LIMIT_FALLBACK_LABEL: &str = "secondary usage";
#[derive(Default)]
pub(super) struct RateLimitWarningState {
pub(super) secondary_index: usize,
pub(super) primary_index: usize,
}
impl RateLimitWarningState {
pub(super) fn take_warnings(
&mut self,
plan_type: Option<PlanType>,
secondary_used_percent: Option<f64>,
secondary_window_minutes: Option<i64>,
primary_used_percent: Option<f64>,
primary_window_minutes: Option<i64>,
) -> Vec<String> {
let reached_secondary_cap =
matches!(secondary_used_percent, Some(percent) if percent == 100.0);
let reached_primary_cap = matches!(primary_used_percent, Some(percent) if percent == 100.0);
if reached_secondary_cap || reached_primary_cap {
return Vec::new();
}
let mut warnings = Vec::new();
if let Some(secondary_used_percent) = secondary_used_percent {
let mut highest_secondary: Option<f64> = None;
while self.secondary_index < RATE_LIMIT_WARNING_THRESHOLDS.len()
&& secondary_used_percent >= RATE_LIMIT_WARNING_THRESHOLDS[self.secondary_index]
{
let threshold = RATE_LIMIT_WARNING_THRESHOLDS[self.secondary_index];
if threshold
>= f64::from(usage_notice::warning_threshold(
plan_type,
secondary_window_minutes,
))
{
highest_secondary = Some(threshold);
}
self.secondary_index += 1;
}
if let Some(threshold) = highest_secondary {
let limit_label =
limit_label_for_window(secondary_window_minutes, /*is_secondary*/ true);
let remaining_percent = 100.0 - threshold;
warnings.push(format!(
"Heads up, you have less than {remaining_percent:.0}% of your {limit_label} limit left."
));
}
}
if let Some(primary_used_percent) = primary_used_percent {
let mut highest_primary: Option<f64> = None;
while self.primary_index < RATE_LIMIT_WARNING_THRESHOLDS.len()
&& primary_used_percent >= RATE_LIMIT_WARNING_THRESHOLDS[self.primary_index]
{
let threshold = RATE_LIMIT_WARNING_THRESHOLDS[self.primary_index];
if threshold
>= f64::from(usage_notice::warning_threshold(
plan_type,
primary_window_minutes,
))
{
highest_primary = Some(threshold);
}
self.primary_index += 1;
}
if let Some(threshold) = highest_primary {
let limit_label =
limit_label_for_window(primary_window_minutes, /*is_secondary*/ false);
let remaining_percent = 100.0 - threshold;
warnings.push(format!(
"Heads up, you have less than {remaining_percent:.0}% of your {limit_label} limit left."
));
}
}
warnings
}
}
pub(crate) fn limit_label_for_window(window_minutes: Option<i64>, is_secondary: bool) -> String {
window_minutes
.and_then(get_limits_duration)
.unwrap_or_else(|| fallback_limit_label(is_secondary).to_string())
}
pub(crate) fn get_limits_duration(windows_minutes: i64) -> Option<String> {
const MINUTES_PER_HOUR: i64 = 60;
const MINUTES_PER_5_HOURS: i64 = 5 * MINUTES_PER_HOUR;
const MINUTES_PER_DAY: i64 = 24 * MINUTES_PER_HOUR;
const MINUTES_PER_WEEK: i64 = 7 * MINUTES_PER_DAY;
const MINUTES_PER_MONTH: i64 = 30 * MINUTES_PER_DAY;
const MINUTES_PER_YEAR: i64 = 365 * MINUTES_PER_DAY;
let windows_minutes = windows_minutes.max(0);
if is_approximate_window(windows_minutes, MINUTES_PER_5_HOURS) {
Some("5h".to_string())
} else if is_approximate_window(windows_minutes, MINUTES_PER_DAY) {
Some("daily".to_string())
} else if is_approximate_window(windows_minutes, MINUTES_PER_WEEK) {
Some("weekly".to_string())
} else if is_approximate_window(windows_minutes, MINUTES_PER_MONTH) {
Some("monthly".to_string())
} else if is_approximate_window(windows_minutes, MINUTES_PER_YEAR) {
Some("annual".to_string())
} else {
None
}
}
pub(crate) fn fallback_limit_label(is_secondary: bool) -> &'static str {
if is_secondary {
SECONDARY_LIMIT_FALLBACK_LABEL
} else {
PRIMARY_LIMIT_FALLBACK_LABEL
}
}
pub(super) fn is_approximate_window(minutes: i64, expected_minutes: i64) -> bool {
let minutes = minutes as f64;
let expected_minutes = expected_minutes as f64;
minutes >= expected_minutes * 0.95 && minutes <= expected_minutes * 1.05
}
#[derive(Default)]
pub(super) enum RateLimitSwitchPromptState {
#[default]
Idle,
Pending,
Shown,
Recovering {
deadline: Instant,
seconds_shown: u64,
},
}
#[derive(Debug)]
pub(super) enum RateLimitErrorKind {
ServerOverloaded,
UsageLimit,
Generic,
}
pub(super) fn app_server_rate_limit_error_kind(
info: &AppServerCodexErrorInfo,
) -> Option<RateLimitErrorKind> {
match info {
AppServerCodexErrorInfo::ServerOverloaded
| AppServerCodexErrorInfo::PolicyCapacityUnavailable => {
Some(RateLimitErrorKind::ServerOverloaded)
}
AppServerCodexErrorInfo::UsageLimitExceeded
| AppServerCodexErrorInfo::ApiKeySpendLimitExceeded
| AppServerCodexErrorInfo::ProjectSpendLimitExceeded
| AppServerCodexErrorInfo::OrganizationSpendLimitExceeded => {
Some(RateLimitErrorKind::UsageLimit)
}
AppServerCodexErrorInfo::ResponseTooManyFailedAttempts {
http_status_code: Some(429),
} => Some(RateLimitErrorKind::Generic),
_ => None,
}
}
pub(super) fn is_app_server_cyber_policy_error(info: &AppServerCodexErrorInfo) -> bool {
matches!(info, AppServerCodexErrorInfo::CyberPolicy)
}
#[derive(Clone, Copy)]
pub(super) enum RateLimitSnapshotSource {
AccountUsage,
RollingUpdate,
ProviderTurn,
}
fn has_usable_workspace_credits(credits: &CreditsSnapshot) -> bool {
credits.unlimited || credits.has_credits
}
impl ChatWidget {
pub(crate) fn quota_provider_id(&self) -> &str {
if self.thread_id().is_none()
&& let Some(provider) = &self.inherited_quota_provider
{
return provider;
}
&self.config.model_provider_id
}
pub(crate) fn forget_inherited_quota_provider(&mut self) {
self.inherited_quota_provider = None;
}
/// Poll more often near exhaustion for every ChatGPT account, independently of experiments.
pub(crate) fn rate_limit_refresh_interval(&self) -> Option<std::time::Duration> {
if !self.should_prefetch_rate_limits() {
return None;
}
// Ignore unrelated model buckets; watch ordinary usage and the selected model's bucket.
let used_percent = self
.rate_limit_snapshots_by_limit_id
.iter()
.filter(|(limit_id, snapshot)| {
limit_id.as_str() == "codex" || snapshot.limit_name == self.current_model()
})
.flat_map(|(_, snapshot)| snapshot.primary.iter().chain(snapshot.secondary.iter()))
.map(|window| window.used_percent)
.filter(|percent| percent.is_finite())
.max_by(f64::total_cmp)
.unwrap_or_default();
let seconds = if used_percent >= 99.0 {
5
} else if used_percent >= 90.0 {
15
} else if used_percent >= 75.0 {
30
} else {
60
};
Some(std::time::Duration::from_secs(seconds))
}
pub(crate) fn hold_rate_limit_recovery(&mut self) -> bool {
std::mem::replace(&mut self.input_queue.rate_limit_recovery_pending, true)
}
pub(crate) fn finish_rate_limit_recovery(&mut self) {
if self.waiting_for_luna_reserve() {
return;
}
if std::mem::take(&mut self.input_queue.rate_limit_recovery_pending) {
self.submit_initial_user_message_if_pending();
self.maybe_send_next_queued_input();
}
}
pub(crate) fn on_rate_limit_snapshot(&mut self, snapshot: Option<RateLimitSnapshot>) {
if let Some(snapshot) = &snapshot {
self.observe_quota_plan(snapshot);
}
self.on_rate_limit_snapshot_from(snapshot, RateLimitSnapshotSource::AccountUsage);
}
pub(crate) fn observe_quota_plan(&mut self, snapshot: &RateLimitSnapshot) {
if snapshot
.limit_id
.as_deref()
.is_none_or(|id| id.eq_ignore_ascii_case("codex"))
&& let Some(plan) = snapshot.plan_type.filter(|plan| *plan != PlanType::Unknown)
{
self.quota_plan = Some(plan);
if self.plan_type != Some(plan) {
self.refresh_billing_plan();
self.refresh_status_surfaces();
}
}
}
pub(crate) fn on_rolling_rate_limit_snapshot(&mut self, snapshot: RateLimitSnapshot) {
if let (Some(previous), Some(current)) = (
self.codex_rate_limit_reached_type,
snapshot.rate_limit_reached_type,
) && previous != current
{
// Do not keep a credits CTA when a newer notification requires a different remedy.
// An absent aggregate limit type is not a contradiction of model-scoped recovery.
self.clear_backend_banner();
}
// Rolling app-server notifications are sparse. Preserve metadata learned from the full read.
let source = if self.uses_account_quota() {
RateLimitSnapshotSource::RollingUpdate
} else {
RateLimitSnapshotSource::ProviderTurn
};
self.on_rate_limit_snapshot_from(Some(snapshot), source);
}
fn on_rate_limit_snapshot_from(
&mut self,
snapshot: Option<RateLimitSnapshot>,
source: RateLimitSnapshotSource,
) {
let usage_notice_blocked = self.codex_rate_limit_reached_type.is_some()
|| self.codex_spend_control_reached == Some(true);
if let Some(mut snapshot) = snapshot {
let limit_id = snapshot
.limit_id
.clone()
.unwrap_or_else(|| "codex".to_string());
let is_codex_limit = limit_id.eq_ignore_ascii_case("codex");
if is_codex_limit
&& self
.usage_notice_state
.update(&snapshot, source, self.plan_type)
{
self.request_redraw();
}
if matches!(source, RateLimitSnapshotSource::RollingUpdate)
&& snapshot.credits.is_none()
{
snapshot.credits = self
.rate_limit_snapshots_by_limit_id
.get(&limit_id)
.and_then(|display| display.credits.as_ref())
.map(|credits| CreditsSnapshot {
has_credits: credits.has_credits,
unlimited: credits.unlimited,
balance: credits.balance.clone(),
});
}
if is_codex_limit
&& (matches!(source, RateLimitSnapshotSource::AccountUsage)
|| snapshot.spend_control_reached.is_some())
{
self.codex_spend_control_reached = snapshot.spend_control_reached;
}
if (is_codex_limit && matches!(source, RateLimitSnapshotSource::AccountUsage))
|| snapshot.rate_limit_reached_type.is_some()
{
self.codex_rate_limit_reached_type = snapshot.rate_limit_reached_type;
}
let workspace_limit_reached = self.codex_spend_control_reached == Some(true)
|| matches!(
self.codex_rate_limit_reached_type,
Some(
RateLimitReachedType::WorkspaceOwnerCreditsDepleted
| RateLimitReachedType::WorkspaceMemberCreditsDepleted
| RateLimitReachedType::WorkspaceOwnerUsageLimitReached
| RateLimitReachedType::WorkspaceMemberUsageLimitReached
)
);
let has_workspace_credits = !workspace_limit_reached
&& snapshot
.credits
.as_ref()
.is_some_and(has_usable_workspace_credits);
let used_percent = snapshot
.primary
.iter()
.chain(&snapshot.secondary)
.map(|window| window.used_percent)
.max();
let replenished = matches!(source, RateLimitSnapshotSource::AccountUsage)
&& !self.usage_notice_state.applying_stale_read
&& used_percent.is_some_and(|used| used < RATE_LIMIT_SWITCH_PROMPT_THRESHOLD);
if is_codex_limit && (has_workspace_credits || replenished) {
match self.rate_limit_switch_prompt {
RateLimitSwitchPromptState::Pending => {
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Idle;
}
RateLimitSwitchPromptState::Shown if replenished => {
let now = Instant::now();
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Recovering {
deadline: now + Duration::from_secs(/*secs*/ 5),
seconds_shown: 0,
};
self.tick_rate_limit_recovery_prompt(now);
}
RateLimitSwitchPromptState::Shown => {
self.bottom_pane
.dismiss_view_by_id(RATE_LIMIT_SWITCH_PROMPT_VIEW_ID);
}
RateLimitSwitchPromptState::Idle
| RateLimitSwitchPromptState::Recovering { .. } => {}
}
}
let should_warn_about_rate_limit_usage = is_codex_limit && !has_workspace_credits;
let warnings = if should_warn_about_rate_limit_usage {
self.rate_limit_warnings.take_warnings(
self.plan_type,
snapshot
.secondary
.as_ref()
.map(|window| f64::from(window.used_percent)),
snapshot
.secondary
.as_ref()
.and_then(|window| window.window_duration_mins),
snapshot
.primary
.as_ref()
.map(|window| f64::from(window.used_percent)),
snapshot
.primary
.as_ref()
.and_then(|window| window.window_duration_mins),
)
} else {
vec![]
};
if used_percent.is_some_and(|used| used >= RATE_LIMIT_SWITCH_PROMPT_THRESHOLD)
&& should_warn_about_rate_limit_usage
&& self.quota_provider_id() == OPENAI_PROVIDER_ID
&& !self.rate_limit_switch_prompt_hidden()
&& self.current_model() != NUDGE_MODEL_SLUG
&& matches!(
self.rate_limit_switch_prompt,
RateLimitSwitchPromptState::Idle
)
{
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Pending;
}
// /wham/usage identifies ordinary and additional model limits separately. Streamed
// updates still drive warnings/recovery above, but must not overwrite status data.
if matches!(source, RateLimitSnapshotSource::AccountUsage) {
let limit_label = snapshot
.limit_name
.clone()
.unwrap_or_else(|| limit_id.clone());
let display = rate_limit_snapshot_display_for_limit(
&snapshot,
limit_label,
Local::now(),
self.clock_format,
);
self.rate_limit_snapshots_by_limit_id
.insert(limit_id, display);
}
if !warnings.is_empty() {
for mut warning in warnings {
if !matches!(source, RateLimitSnapshotSource::ProviderTurn) {
warning.push_str(" Run /status for a breakdown.");
}
self.add_to_history(history_cell::new_visible_warning_event(warning));
}
self.request_redraw();
}
} else {
self.rate_limit_snapshots_by_limit_id.clear();
self.quota_plan = None;
self.refresh_billing_plan();
self.codex_rate_limit_reached_type = None;
self.codex_spend_control_reached = None;
}
if usage_notice_blocked
!= (self.codex_rate_limit_reached_type.is_some()
|| self.codex_spend_control_reached == Some(true))
{
self.request_redraw();
}
self.refresh_status_line();
}
pub(crate) fn uses_account_quota(&self) -> bool {
if self.thread_params_mode == crate::ThreadParamsMode::Cloud {
return self.requires_openai_auth;
}
// The server authenticated the quota read, but its default provider may differ
// from the one serving this thread.
self.config
.model_providers
.get(self.quota_provider_id())
.is_some_and(codex_model_provider::provider_uses_first_party_auth_path)
}
pub(crate) fn should_prefetch_rate_limits(&self) -> bool {
self.requires_openai_auth && self.has_chatgpt_account && self.uses_account_quota()
}
fn lower_cost_preset(&self) -> Option<ModelPreset> {
let models = self.model_catalog.try_list_models().ok()?;
models
.iter()
.find(|preset| preset.show_in_picker && preset.model == NUDGE_MODEL_SLUG)
.cloned()
}
fn rate_limit_switch_prompt_hidden(&self) -> bool {
self.local_settings
.notices
.hide_rate_limit_model_nudge
.unwrap_or(false)
}
pub(super) fn maybe_show_pending_rate_limit_prompt(&mut self) {
if self.has_applicable_backend_banner()
|| matches!(
self.rate_limit_switch_prompt,
RateLimitSwitchPromptState::Recovering { .. }
)
{
return;
}
if self.rate_limit_switch_prompt_hidden()
|| matches!(self.current_model(), NUDGE_MODEL_SLUG | LUNA_RESERVE_MODEL)
{
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Idle;
return;
}
if !matches!(
self.rate_limit_switch_prompt,
RateLimitSwitchPromptState::Pending
) {
return;
}
if let Some(preset) = self.lower_cost_preset() {
self.open_rate_limit_switch_prompt(preset);
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Shown;
} else {
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Idle;
}
}
pub(super) fn tick_rate_limit_recovery_prompt(&mut self, now: Instant) {
let RateLimitSwitchPromptState::Recovering {
deadline,
seconds_shown,
} = &mut self.rate_limit_switch_prompt
else {
return;
};
let remaining = deadline.saturating_duration_since(now);
if remaining.is_zero() {
self.bottom_pane
.dismiss_view_by_id(RATE_LIMIT_SWITCH_PROMPT_VIEW_ID);
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Shown;
return;
}
let seconds = remaining.as_secs() + u64::from(remaining.subsec_nanos() != 0);
if *seconds_shown != seconds {
let unit = if seconds == 1 { "second" } else { "seconds" };
let params = SelectionViewParams {
view_id: Some(RATE_LIMIT_SWITCH_PROMPT_VIEW_ID),
title: Some("You’re no longer close to your usage limit.".into()),
subtitle: Some(format!("Dismissing in {seconds} {unit}…")),
items: vec![SelectionItem {
name: "Dismiss".into(),
dismiss_on_select: true,
..Default::default()
}],
..SelectionViewParams::picker()
};
if !self
.bottom_pane
.replace_selection_view_if_present(RATE_LIMIT_SWITCH_PROMPT_VIEW_ID, params)
{
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Shown;
return;
}
*seconds_shown = seconds;
}
self.bottom_pane
.request_redraw_in(remaining - Duration::from_secs(seconds - 1));
}
fn open_rate_limit_switch_prompt(&mut self, preset: ModelPreset) {
let switch_model = preset.model;
let switch_model_for_events = switch_model.clone();
let default_effort: ReasoningEffortConfig = preset.default_reasoning_effort;
let switch_actions: Vec<SelectionAction> = vec![Box::new(move |tx| {
tx.send(AppEvent::CodexOp(AppCommand::override_turn_context(
/*cwd*/ None,
/*approval_policy*/ None,
/*approvals_reviewer*/ None,
/*permission_profile*/ None,
/*active_permission_profile*/ None,
Some(switch_model_for_events.clone()),
Some(Some(default_effort.clone())),
/*summary*/ None,
/*service_tier*/ None,
/*collaboration_mode*/ None,
)));
tx.send(AppEvent::UpdateModel(switch_model_for_events.clone()));
tx.send(AppEvent::UpdateReasoningEffort(Some(
default_effort.clone(),
)));
})];
let keep_actions: Vec<SelectionAction> = Vec::new();
let never_actions: Vec<SelectionAction> = vec![Box::new(|tx| {
tx.send(AppEvent::UpdateRateLimitSwitchPromptHidden(true));
tx.send(AppEvent::PersistRateLimitSwitchPromptHidden);
})];
let description = if preset.description.is_empty() {
Some("Uses fewer credits for upcoming turns".to_string())
} else {
Some(preset.description)
};
let items = vec![
SelectionItem {
name: format!("Switch to {switch_model}"),
description,
selected_description: None,
is_current: false,
actions: switch_actions,
dismiss_on_select: true,
..Default::default()
},
SelectionItem {
name: "Keep current model".to_string(),
description: None,
selected_description: None,
is_current: false,
actions: keep_actions,
dismiss_on_select: true,
..Default::default()
},
SelectionItem {
name: "Keep current model (never show again)".to_string(),
description: Some(
"Hide future rate limit reminders about switching models".to_string(),
),
selected_description: None,
is_current: false,
actions: never_actions,
dismiss_on_select: true,
..Default::default()
},
];
self.bottom_pane.show_selection_view(SelectionViewParams {
view_id: Some(RATE_LIMIT_SWITCH_PROMPT_VIEW_ID),
title: Some("Approaching rate limits".to_string()),
subtitle: Some(format!("Switch to {switch_model} for lower credit usage?")),
footer_hint: Some(standard_popup_hint_line()),
items,
..SelectionViewParams::picker()
});
}
pub(super) fn open_workspace_owner_nudge_prompt(
&mut self,
credit_type: AddCreditsNudgeCreditType,
) {
if self.add_credits_nudge_email_in_flight.is_some() {
return;
}
let (title, prompt) = match credit_type {
AddCreditsNudgeCreditType::Credits => (
"You've reached your workspace credit limit",
"Your workspace is out of credits. Ask your workspace owner to add more. Notify owner?",
),
AddCreditsNudgeCreditType::UsageLimit => (
"Usage limit reached",
"Request a limit increase from your owner to continue using codex. Request increase?",
),
};
let send_actions: Vec<SelectionAction> = vec![Box::new(move |tx| {
tx.send(AppEvent::SendAddCreditsNudgeEmail { credit_type });
})];
let items = vec![
SelectionItem {
name: "Yes".to_string(),
display_shortcut: Some(key_hint::plain(KeyCode::Char('y')).into()),
actions: send_actions,
dismiss_on_select: true,
..Default::default()
},
SelectionItem {
name: "No (default)".to_string(),
display_shortcut: Some(key_hint::plain(KeyCode::Char('n')).into()),
dismiss_on_select: true,
..Default::default()
},
];
self.bottom_pane.show_actionable_banner(ActionableBanner {
view_id: Some(WORKSPACE_NUDGE_VIEW_ID),
title: title.to_string(),
description: prompt.to_string(),
actions: items,
initial_selected_idx: Some(1),
..Default::default()
});
}
pub(crate) fn start_add_credits_nudge_email_request(
&mut self,
credit_type: AddCreditsNudgeCreditType,
) -> Option<Uuid> {
if self.add_credits_nudge_email_in_flight.is_some() {
return None;
}
let request_id = Uuid::new_v4();
self.add_credits_nudge_email_in_flight = Some(PendingCreditsNudge {
request_id,
credit_type,
});
Some(request_id)
}
pub(crate) fn finish_add_credits_nudge_email_request(
&mut self,
request_id: Uuid,
result: Result<AddCreditsNudgeEmailStatus, String>,
) {
let Some(pending) = self
.add_credits_nudge_email_in_flight
.as_ref()
.filter(|pending| pending.request_id == request_id)
else {
return;
};
let credit_type = pending.credit_type;
self.add_credits_nudge_email_in_flight = None;
let message = match (credit_type, result) {
(AddCreditsNudgeCreditType::Credits, Ok(AddCreditsNudgeEmailStatus::Sent)) => {
"Workspace owner notified."
}
(
AddCreditsNudgeCreditType::Credits,
Ok(AddCreditsNudgeEmailStatus::CooldownActive),
) => "Workspace owner was already notified recently.",
(AddCreditsNudgeCreditType::Credits, Err(_)) => {
"Could not notify your workspace owner. Please try again."
}
(AddCreditsNudgeCreditType::UsageLimit, Ok(AddCreditsNudgeEmailStatus::Sent)) => {
"Limit increase requested."
}
(
AddCreditsNudgeCreditType::UsageLimit,
Ok(AddCreditsNudgeEmailStatus::CooldownActive),
) => "A limit increase was already requested recently.",
(AddCreditsNudgeCreditType::UsageLimit, Err(_)) => {
"Could not request a limit increase. Please try again."
}
};
self.add_to_history(history_cell::new_info_event(
message.to_string(),
/*hint*/ None,
));
self.request_redraw();
}
pub(crate) fn set_rate_limit_switch_prompt_hidden(&mut self, hidden: bool) {
self.local_settings.notices.hide_rate_limit_model_nudge = Some(hidden);
if hidden {
self.rate_limit_switch_prompt = RateLimitSwitchPromptState::Idle;
}
}
}