Something went wrong. Try again.
become more token aware with this one free trick! providers hate her
Something went wrong. Try again.
5.3 kB · 168 lines
Swift
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import Foundationimport SwiftUI
public struct UsageWindowItem: Identifiable, Equatable, Codable { public var id: String public var label: String public var usedPercent: Double public var resetsAt: Date? public var detail: String? public var isExhausted: Bool
public init( id: String, label: String, usedPercent: Double, resetsAt: Date? = nil, detail: String? = nil, isExhausted: Bool = false ) { self.id = id self.label = label self.usedPercent = max(0.0, min(100.0, usedPercent)) self.resetsAt = resetsAt self.detail = detail self.isExhausted = isExhausted || usedPercent >= 100.0 }
public var resetCountdownString: String? { resetCountdown(at: Date()) }
public func resetCountdown(at now: Date) -> String? { guard let resetsAt = resetsAt, usedPercent > 0 || isExhausted else { return nil } let interval = resetsAt.timeIntervalSince(now) if interval <= 0 { return "Resets now" } let totalSeconds = Int(interval) let days = totalSeconds / 86400 let hours = (totalSeconds % 86400) / 3600 let minutes = (totalSeconds % 3600) / 60
if days > 0 { return "\(days)d \(hours)h" } else if hours > 0 { return "\(hours)h \(minutes)m" } else { return "\(max(1, minutes))m" } }}
public struct ProviderUsage: Identifiable, Equatable, Codable { public var id: String public var name: String public var planType: String? public var email: String? public var windows: [UsageWindowItem] public var balance: String? public var notes: [String] public var errorMessage: String? public var isShared: Bool public var isRateLimited: Bool public var isCached: Bool public var lastFetched: Date?
public init( id: String, name: String, planType: String? = nil, email: String? = nil, windows: [UsageWindowItem] = [], balance: String? = nil, notes: [String] = [], isShared: Bool = false, isRateLimited: Bool = false, isCached: Bool = false, errorMessage: String? = nil, lastFetched: Date? = nil ) { self.id = id self.name = name self.planType = planType self.email = email self.windows = windows self.balance = balance self.notes = notes self.isShared = isShared self.isRateLimited = isRateLimited self.isCached = isCached self.errorMessage = errorMessage self.lastFetched = lastFetched }
public var maxUsedPercent: Double? { windows.map(\.usedPercent).max() }
public var isExhausted: Bool { windows.contains(where: \.isExhausted) }
public var cleanDisplayName: String { ProviderPresentation.of(id).shortName ?? name }
public var sfSymbol: String { ProviderPresentation.of(id).symbol }
public var accentColor: Color { ProviderPresentation.of(id).accent }}
/// How a provider is drawn. Keyed by the core's provider id, so a provider the app has never seen/// still gets a stable card: a generic symbol and the provider's own display name from the core.public struct ProviderPresentation { public var shortName: String? public var symbol: String public var accent: Color
public static func of(_ id: String) -> ProviderPresentation { table[id] ?? ProviderPresentation(shortName: nil, symbol: "network", accent: .secondary) }
static let table: [String: ProviderPresentation] = [ "anthropic": ProviderPresentation( shortName: "Claude Pro", symbol: "sparkles", accent: Color(red: 0.85, green: 0.47, blue: 0.36) ), "openai-codex": ProviderPresentation( shortName: "OpenAI Codex", symbol: "bolt.fill", accent: Color(red: 0.06, green: 0.64, blue: 0.50) ), "google-antigravity": ProviderPresentation( shortName: "Google Antigravity", symbol: "cpu.fill", accent: Color(red: 0.26, green: 0.52, blue: 0.96) ), "opencode-go": ProviderPresentation( shortName: "OpenCode Go", symbol: "chevron.left.forwardslash.chevron.right", accent: Color(red: 0.02, green: 0.71, blue: 0.83) ), "deepseek": ProviderPresentation( shortName: "DeepSeek", symbol: "circle.hexagongrid.fill", accent: Color(red: 0.30, green: 0.42, blue: 1.0) ), "zai": ProviderPresentation( shortName: "Z.ai GLM", symbol: "hexagon.fill", accent: Color(red: 0.60, green: 0.38, blue: 0.98) ), "hyper": ProviderPresentation( shortName: "Charm Hyper", symbol: "wand.and.stars", accent: Color(red: 0.86, green: 0.35, blue: 0.64) ), "alibaba": ProviderPresentation( shortName: "Alibaba Model Studio", symbol: "cloud.fill", accent: Color(red: 1.0, green: 0.42, blue: 0.10) ), ]}