diff --git a/Sources/PingClaude/ClaudeInfoView.swift b/Sources/PingClaude/ClaudeInfoView.swift index 877f146..66ab6a2 100644 --- a/Sources/PingClaude/ClaudeInfoView.swift +++ b/Sources/PingClaude/ClaudeInfoView.swift @@ -85,87 +85,34 @@ struct ClaudeInfoView: View { // Usage Pace GroupBox(label: Text("Usage Pace").font(.headline)) { VStack(alignment: .leading, spacing: 8) { - Group { - // Detected model indicator - HStack { - Text("Active model:") - .font(.system(size: 12, weight: .medium)) - .frame(width: 90, alignment: .leading) - Text(velocityTracker.detectedModelDisplay) - .font(.system(size: 12, weight: .semibold, design: .monospaced)) - .foregroundColor(.accentColor) - Spacer() - } - - HStack { - Text("This session:") - .font(.system(size: 12, weight: .medium)) - .frame(width: 90, alignment: .leading) - Text(velocityTracker.sessionVelocityString) - .font(.system(size: 12, design: .monospaced)) - Spacer() - Text(velocityTracker.timeRemainingString) - .font(.system(size: 12, weight: .semibold)) - .foregroundColor(timeRemainingColor) - } - } - - Group { - HStack { - Text("Past week:") - .font(.system(size: 12, weight: .medium)) - .frame(width: 90, alignment: .leading) - Text(velocityTracker.weeklyVelocityString) - .font(.system(size: 12, design: .monospaced)) - Spacer() - } - - HStack { - Text("All time:") - .font(.system(size: 12, weight: .medium)) - .frame(width: 90, alignment: .leading) - Text(velocityTracker.allTimeVelocityString) - .font(.system(size: 12, design: .monospaced)) - Spacer() - } + HStack { + Text("This session:") + .font(.system(size: 12, weight: .medium)) + .frame(width: 90, alignment: .leading) + Text(velocityTracker.sessionVelocityString) + .font(.system(size: 12, design: .monospaced)) + Spacer() + Text(velocityTracker.timeRemainingString) + .font(.system(size: 12, weight: .semibold)) + .foregroundColor(timeRemainingColor) } - // Per-model time remaining table - if !velocityTracker.modelTimeEstimates.isEmpty && - velocityTracker.modelTimeEstimates.contains(where: { $0.timeRemaining != nil }) { - Divider() - Text("Estimated time by model:") - .font(.system(size: 11, weight: .medium)) - .foregroundColor(.secondary) - - ForEach(velocityTracker.modelTimeEstimates) { estimate in - HStack { - Text(estimate.model.capitalized) - .font(.system(size: 12, weight: estimate.isCurrent ? .semibold : .regular)) - .frame(width: 60, alignment: .leading) - Text(estimate.displayString) - .font(.system(size: 12, design: .monospaced)) - .foregroundColor(modelTimeColor(estimate)) - if estimate.isCurrent { - Text("(current)") - .font(.system(size: 10)) - .foregroundColor(.secondary) - } - Spacer() - } - } + HStack { + Text("Past week:") + .font(.system(size: 12, weight: .medium)) + .frame(width: 90, alignment: .leading) + Text(velocityTracker.weeklyVelocityString) + .font(.system(size: 12, design: .monospaced)) + Spacer() } - // Budget advisor tip - if let advice = velocityTracker.budgetAdvisorMessage { - HStack(spacing: 4) { - Text("\u{1F4A1}") - .font(.system(size: 11)) - Text(advice) - .font(.system(size: 11, weight: .medium)) - .foregroundColor(.orange) - } - .padding(.vertical, 2) + HStack { + Text("All time:") + .font(.system(size: 12, weight: .medium)) + .frame(width: 90, alignment: .leading) + Text(velocityTracker.allTimeVelocityString) + .font(.system(size: 12, design: .monospaced)) + Spacer() } Text("Based on \(velocityTracker.sessionSampleCount) samples since last reset") @@ -299,14 +246,6 @@ struct ClaudeInfoView: View { return .green } - private func modelTimeColor(_ estimate: ModelTimeEstimate) -> Color { - guard let remaining = estimate.timeRemaining else { return .secondary } - let hours = remaining / 3600 - if hours < 1 { return .red } - if hours < 2 { return .orange } - return .green - } - private func resetDetail(_ usage: UsageData) -> String { var parts: [String] = [] if let remaining = usage.sessionResetsInString { diff --git a/Sources/PingClaude/Constants.swift b/Sources/PingClaude/Constants.swift index 238ee8e..ae3c3d8 100644 --- a/Sources/PingClaude/Constants.swift +++ b/Sources/PingClaude/Constants.swift @@ -76,30 +76,6 @@ enum Constants { // API ping timeout (longer than CLI since it involves create + send + delete) static let apiPingTimeoutSeconds: TimeInterval = 45 - // Model pricing ratios (relative cost: Opus is 15x Haiku, Sonnet is 3x Haiku) - enum ModelPricing { - static let ratios: [String: Double] = ["opus": 15.0, "sonnet": 3.0, "haiku": 1.0] - static let modelsInCostOrder = ["haiku", "sonnet", "opus"] // cheapest first - static let advisorThresholdHours: Double = 2.0 - - /// Estimate velocity on `toModel` given observed velocity on `fromModel` - static func estimateVelocity(observed: Double, fromModel: String, toModel: String) -> Double { - let fromRatio = ratios[fromModel] ?? 1.0 - let toRatio = ratios[toModel] ?? 1.0 - return observed * (toRatio / fromRatio) - } - - /// Short display label for a model: "O", "S", "H" - static func shortLabel(_ model: String) -> String { - switch model { - case "opus": return "O" - case "sonnet": return "S" - case "haiku": return "H" - default: return model.prefix(1).uppercased() - } - } - } - // Interval options (in minutes) static let intervalOptions: [(label: String, minutes: Int)] = [ ("15 minutes", 15), diff --git a/Sources/PingClaude/StatusBarController.swift b/Sources/PingClaude/StatusBarController.swift index ec194e8..76eef6c 100644 --- a/Sources/PingClaude/StatusBarController.swift +++ b/Sources/PingClaude/StatusBarController.swift @@ -29,7 +29,6 @@ class StatusBarController { private var breakdownMenuItems: [NSMenuItem] = [] private var usageErrorMenuItem: NSMenuItem! private var velocityMenuItem: NSMenuItem! - private var budgetAdvisorMenuItem: NSMenuItem! private var scheduleToggleMenuItem: NSMenuItem! private let timeFormatter: DateFormatter = { @@ -96,11 +95,6 @@ class StatusBarController { velocityMenuItem.isEnabled = false menu.addItem(velocityMenuItem) - budgetAdvisorMenuItem = NSMenuItem(title: "", action: nil, keyEquivalent: "") - budgetAdvisorMenuItem.isEnabled = false - budgetAdvisorMenuItem.isHidden = true - menu.addItem(budgetAdvisorMenuItem) - weeklyUsageMenuItem = NSMenuItem(title: "Weekly: --", action: nil, keyEquivalent: "") weeklyUsageMenuItem.isEnabled = false menu.addItem(weeklyUsageMenuItem) @@ -263,20 +257,6 @@ class StatusBarController { } .store(in: &cancellables) - // Watch budget advisor - velocityTracker.$budgetAdvisorMessage - .receive(on: RunLoop.main) - .sink { [weak self] message in - guard let self = self else { return } - if let message = message { - self.budgetAdvisorMenuItem.title = "\u{1F4A1} \(message)" - self.budgetAdvisorMenuItem.isHidden = false - } else { - self.budgetAdvisorMenuItem.isHidden = true - } - } - .store(in: &cancellables) - // Watch plan tier usageService.$planTier .receive(on: RunLoop.main) @@ -320,15 +300,10 @@ class StatusBarController { break // fall through to usage-based display } - // If we have live usage data, show "CC·44%·O" (with model indicator) + // If we have live usage data, show "CC·44%" if let usage = usageService.latestUsage { let pct = Int(usage.sessionUtilization) - if let model = velocityTracker.detectedModel { - let modelChar = Constants.ModelPricing.shortLabel(model) - button.title = "CC\u{00B7}\(pct)%\u{00B7}\(modelChar)" - } else { - button.title = "CC\u{00B7}\(pct)%" - } + button.title = "CC\u{00B7}\(pct)%" return } @@ -392,13 +367,12 @@ class StatusBarController { private func updateVelocityDisplay() { let timeStr = velocityTracker.timeRemainingString let rateStr = velocityTracker.sessionVelocityString - let modelTag = velocityTracker.detectedModel.map { " [\($0.capitalized)]" } ?? "" if velocityTracker.sessionVelocity == nil { velocityMenuItem.title = "Pace: \(rateStr)" } else if let vel = velocityTracker.sessionVelocity, vel <= 0 { velocityMenuItem.title = "Pace: \(rateStr)" } else { - velocityMenuItem.title = "\u{23F1} \(timeStr) (\(rateStr))\(modelTag)" + velocityMenuItem.title = "\u{23F1} \(timeStr) (\(rateStr))" } } diff --git a/Sources/PingClaude/UsageVelocityTracker.swift b/Sources/PingClaude/UsageVelocityTracker.swift index fc842b3..8bd2bfe 100644 --- a/Sources/PingClaude/UsageVelocityTracker.swift +++ b/Sources/PingClaude/UsageVelocityTracker.swift @@ -37,46 +37,12 @@ struct UsageSample: Codable { } } -/// Per-model time estimate for display in SwiftUI -struct ModelTimeEstimate: Identifiable { - let model: String - let timeRemaining: TimeInterval? // seconds, nil if can't estimate - let isCurrent: Bool - - var id: String { model } - - var displayString: String { - guard let remaining = timeRemaining else { return "—" } - if remaining <= 0 { return "exhausted" } - let hours = Int(remaining) / 3600 - let minutes = (Int(remaining) % 3600) / 60 - if hours > 0 { - return "~\(hours)h \(minutes)m" - } - return "~\(minutes)m" - } - - var shortDisplayString: String { - guard let remaining = timeRemaining else { return "—" } - if remaining <= 0 { return "0m" } - let hours = Int(remaining) / 3600 - let minutes = (Int(remaining) % 3600) / 60 - if hours > 0 { - return "~\(hours)h\(minutes)m" - } - return "~\(minutes)m" - } -} - class UsageVelocityTracker: ObservableObject { @Published var sessionVelocity: Double? // %/hr, nil if insufficient data @Published var weeklyVelocity: Double? // %/hr @Published var allTimeVelocity: Double? // %/hr @Published var sessionTimeRemaining: TimeInterval? // seconds until 100% @Published var sessionSampleCount: Int = 0 - @Published var detectedModel: String? // "opus", "sonnet", "haiku", or nil - @Published var perModelTimeRemaining: [String: TimeInterval] = [:] - @Published var budgetAdvisorMessage: String? private let settingsStore: SettingsStore private let usageService: UsageService @@ -140,18 +106,10 @@ class UsageVelocityTracker: ObservableObject { } private func recordSample(_ usage: UsageData) { - // Build model snapshots from breakdowns - let snapshots = extractModelSnapshots(from: usage.breakdowns) - - // Detect active model by diffing with previous sample - let detected = detectActiveModel(newSnapshots: snapshots, sessionDelta: usage.sessionUtilization - (samples.last?.sessionUtilization ?? 0)) - let sample = UsageSample( timestamp: Date(), sessionUtilization: usage.sessionUtilization, - sessionResetsAt: usage.sessionResetsAt, - modelSnapshots: snapshots, - detectedModel: detected + sessionResetsAt: usage.sessionResetsAt ) // Skip duplicate if utilization hasn't changed and last sample was recent (< 30s) @@ -167,70 +125,6 @@ class UsageVelocityTracker: ObservableObject { recalculate(currentUtilization: usage.sessionUtilization) } - // MARK: - Model Detection - - /// Extract per-model 7-day snapshots from API breakdowns - private func extractModelSnapshots(from breakdowns: [UsageBreakdown]) -> [ModelSnapshot] { - var snapshots: [ModelSnapshot] = [] - for breakdown in breakdowns { - let label = breakdown.label.lowercased() - if label.contains("opus") { - snapshots.append(ModelSnapshot(label: "opus", utilization: breakdown.utilization)) - } else if label.contains("sonnet") { - snapshots.append(ModelSnapshot(label: "sonnet", utilization: breakdown.utilization)) - } - // Note: Haiku has no dedicated 7-day breakdown in the API - } - return snapshots - } - - /// Detect which model is active by comparing 7-day breakdown changes. - /// Returns nil when per-model data isn't available (recalculate will use velocity heuristic). - private func detectActiveModel(newSnapshots: [ModelSnapshot], sessionDelta: Double) -> String? { - guard let lastSample = samples.last else { return nil } - let oldSnapshots = lastSample.modelSnapshots - - // If neither old nor new have model data, return nil — let velocity heuristic handle it - guard !newSnapshots.isEmpty || !oldSnapshots.isEmpty else { return nil } - - // Build lookup of old values - var oldValues: [String: Double] = [:] - for s in oldSnapshots { - oldValues[s.label] = s.utilization - } - - // Find which model's 7-day increased the most - var bestModel: String? - var bestIncrease: Double = 0 - let threshold: Double = 0.05 // minimum increase to count - - for snapshot in newSnapshots { - let oldVal = oldValues[snapshot.label] ?? 0 - let increase = snapshot.utilization - oldVal - if increase > threshold && increase > bestIncrease { - bestIncrease = increase - bestModel = snapshot.label - } - } - - // Only infer Haiku when we DO have snapshot data but no model moved - // (Haiku has no 7-day breakdown, so session climbing with no model change = Haiku) - if bestModel == nil && !newSnapshots.isEmpty && sessionDelta > 0.5 { - bestModel = "haiku" - } - - return bestModel ?? lastSample.detectedModel - } - - /// Infer model from observed velocity when API doesn't provide per-model breakdowns. - /// Based on pricing ratios: Opus burns ~15x faster than Haiku, Sonnet ~3x. - /// For a 5h session (20%/hr baseline): Opus >50%/hr, Sonnet 15-50%/hr, Haiku <15%/hr. - private func inferModelFromVelocity(_ velocity: Double) -> String { - if velocity > 50 { return "opus" } - if velocity > 15 { return "sonnet" } - return "haiku" - } - // MARK: - Pruning private func pruneSamples() { @@ -263,7 +157,6 @@ class UsageVelocityTracker: ObservableObject { private func recalculate(currentUtilization: Double) { let now = Date() - // Find session boundary: last time sessionResetsAt changed (jumped forward) let sessionSamples = samplesForCurrentSession() sessionSampleCount = sessionSamples.count @@ -273,87 +166,13 @@ class UsageVelocityTracker: ObservableObject { }) allTimeVelocity = computeVelocity(from: samples) - // Update detected model: use snapshot-based detection if available, - // otherwise fall back to velocity-based heuristic - let snapshotDetection = samples.last?.detectedModel - let hasAnySnapshots = sessionSamples.contains { !$0.modelSnapshots.isEmpty } - - if hasAnySnapshots, let detected = snapshotDetection { - // Per-model API data exists — trust snapshot-based detection - detectedModel = detected - } else if let vel = sessionVelocity, vel > 0 { - // No per-model data from API — infer from burn rate - detectedModel = inferModelFromVelocity(vel) - } else { - detectedModel = nil - } - - // Estimate time remaining based on session velocity + // Estimate time remaining based on aggregate session velocity if let vel = sessionVelocity, vel > 0, currentUtilization < 100 { let hoursLeft = (100 - currentUtilization) / vel sessionTimeRemaining = hoursLeft * 3600 } else { sessionTimeRemaining = nil } - - // Compute per-model time estimates - computePerModelEstimates(currentUtilization: currentUtilization) - - // Generate budget advisor message - generateBudgetAdvice(currentUtilization: currentUtilization) - } - - /// Compute estimated time remaining for each model using pricing ratios - private func computePerModelEstimates(currentUtilization: Double) { - guard let vel = sessionVelocity, vel > 0, currentUtilization < 100 else { - perModelTimeRemaining = [:] - return - } - - let currentModel = detectedModel ?? "sonnet" // default assumption - var estimates: [String: TimeInterval] = [:] - - for model in Constants.availableModels { - let modelVel = Constants.ModelPricing.estimateVelocity( - observed: vel, fromModel: currentModel, toModel: model - ) - if modelVel > 0 { - let hoursLeft = (100 - currentUtilization) / modelVel - estimates[model] = hoursLeft * 3600 - } - } - - perModelTimeRemaining = estimates - } - - /// Generate budget advice when time is running low - private func generateBudgetAdvice(currentUtilization: Double) { - guard let currentModel = detectedModel, - let currentRemaining = perModelTimeRemaining[currentModel], - currentRemaining > 0 else { - budgetAdvisorMessage = nil - return - } - - let thresholdSeconds = Constants.ModelPricing.advisorThresholdHours * 3600 - - // Only advise if current model has < threshold time remaining - guard currentRemaining < thresholdSeconds else { - budgetAdvisorMessage = nil - return - } - - // Find the cheapest model that would give more time - for cheaperModel in Constants.ModelPricing.modelsInCostOrder { - if cheaperModel == currentModel { break } // stop at current model - if let cheaperRemaining = perModelTimeRemaining[cheaperModel], cheaperRemaining > currentRemaining { - let estimate = ModelTimeEstimate(model: cheaperModel, timeRemaining: cheaperRemaining, isCurrent: false) - budgetAdvisorMessage = "Switch to \(cheaperModel.capitalized) for \(estimate.displayString) remaining" - return - } - } - - budgetAdvisorMessage = nil } private func samplesForCurrentSession() -> [UsageSample] { @@ -442,20 +261,4 @@ class UsageVelocityTracker: ObservableObject { return remaining / 3600 } - var detectedModelDisplay: String { - guard let model = detectedModel else { return "unknown" } - return model.capitalized - } - - /// Build model time estimates for SwiftUI display - var modelTimeEstimates: [ModelTimeEstimate] { - // Show in cost order: opus, sonnet, haiku (most expensive first) - Constants.ModelPricing.modelsInCostOrder.reversed().map { model in - ModelTimeEstimate( - model: model, - timeRemaining: perModelTimeRemaining[model], - isCurrent: model == detectedModel - ) - } - } }