ai-agent 0.13.4

Idiomatic agent sdk inspired by the claude code source leak
Documentation
//! Model 1M access checks.
//!
//! Translated from openclaudecode/src/utils/model/check1mAccess.ts

use std::sync::OnceLock;

// =============================================================================
// STUB: Overage disabled reasons
// =============================================================================

/// Overage disabled reason
#[derive(Debug, Clone, PartialEq)]
pub enum OverageDisabledReason {
    /// Out of credits
    OutOfCredits,
    /// Overage not provisioned
    OverageNotProvisioned,
    /// Organization level disabled
    OrgLevelDisabled,
    /// Organization level disabled until
    OrgLevelDisabledUntil,
    /// Seat tier level disabled
    SeatTierLevelDisabled,
    /// Member level disabled
    MemberLevelDisabled,
    /// Seat tier zero credit limit
    SeatTierZeroCreditLimit,
    /// Group zero credit limit
    GroupZeroCreditLimit,
    /// Member zero credit limit
    MemberZeroCreditLimit,
    /// Organization service level disabled
    OrgServiceLevelDisabled,
    /// Organization service zero credit limit
    OrgServiceZeroCreditLimit,
    /// No limits configured
    NoLimitsConfigured,
    /// Unknown
    Unknown,
}

// =============================================================================
// GLOBAL CONFIG STUB
// =============================================================================

/// Global config stub - would be from config module
struct GlobalConfig {
    cached_extra_usage_disabled_reason: Option<OverageDisabledReason>,
}

static GLOBAL_CONFIG: OnceLock<GlobalConfig> = OnceLock::new();

fn get_global_config() -> &'static GlobalConfig {
    GLOBAL_CONFIG.get_or_init(|| GlobalConfig {
        cached_extra_usage_disabled_reason: None,
    })
}

// =============================================================================
// EXTRA USAGE CHECK
// =============================================================================

/// Check if extra usage is enabled based on cached disabled reason.
/// Extra usage is considered enabled if there's no disabled reason,
/// or if the disabled reason indicates it's provisioned but temporarily unavailable.
fn is_extra_usage_enabled() -> bool {
    let reason = get_global_config()
        .cached_extra_usage_disabled_reason
        .clone();

    // undefined = no cache yet, treat as not enabled (conservative)
    if reason.is_none() {
        return false;
    }

    let reason = reason.unwrap();

    // null = no disabled reason from API, extra usage is enabled
    if reason == OverageDisabledReason::OutOfCredits {
        // Provisioned but credits depleted — still counts as enabled
        return true;
    }

    // Not provisioned or actively disabled
    matches!(
        reason,
        OverageDisabledReason::OverageNotProvisioned
            | OverageDisabledReason::OrgLevelDisabled
            | OverageDisabledReason::OrgLevelDisabledUntil
            | OverageDisabledReason::SeatTierLevelDisabled
            | OverageDisabledReason::MemberLevelDisabled
            | OverageDisabledReason::SeatTierZeroCreditLimit
            | OverageDisabledReason::GroupZeroCreditLimit
            | OverageDisabledReason::MemberZeroCreditLimit
            | OverageDisabledReason::OrgServiceLevelDisabled
            | OverageDisabledReason::OrgServiceZeroCreditLimit
            | OverageDisabledReason::NoLimitsConfigured
            | OverageDisabledReason::Unknown
    )
}

// =============================================================================
// 1M ACCESS CHECKS
// =============================================================================

/// Check if Opus 1M context access is available
pub fn check_opus_1m_access() -> bool {
    if is_1m_context_disabled() {
        return false;
    }

    if is_claude_ai_subscriber() {
        // Subscribers have access if extra usage is enabled for their account
        return is_extra_usage_enabled();
    }

    // Non-subscribers (API/PAYG) have access
    true
}

/// Check if Sonnet 1M context access is available
pub fn check_sonnet_1m_access() -> bool {
    if is_1m_context_disabled() {
        return false;
    }

    if is_claude_ai_subscriber() {
        // Subscribers have access if extra usage is enabled for their account
        return is_extra_usage_enabled();
    }

    // Non-subscribers (API/PAYG) have access
    true
}

// =============================================================================
// STUB FUNCTIONS
// =============================================================================

/// Check if 1M context is disabled
fn is_1m_context_disabled() -> bool {
    // Stub - would need context module
    false
}

/// Check if user is Claude AI subscriber
fn is_claude_ai_subscriber() -> bool {
    // Stub - would need auth module
    false
}