|
| 1 | +import CodexBarCore |
| 2 | +import Foundation |
| 3 | + |
| 4 | +struct SpendSourcePublication: Sendable, Equatable { |
| 5 | + enum Role: Sendable, Equatable { |
| 6 | + case subscription |
| 7 | + case enrichment |
| 8 | + } |
| 9 | + |
| 10 | + enum State: Sendable, Equatable { |
| 11 | + case loading |
| 12 | + case available |
| 13 | + case confirmedEmpty |
| 14 | + case unavailable |
| 15 | + case staleLastKnown |
| 16 | + } |
| 17 | + |
| 18 | + let id: String |
| 19 | + let provider: UsageProvider? |
| 20 | + let displayName: String |
| 21 | + let role: Role |
| 22 | + let state: State |
| 23 | +} |
| 24 | + |
| 25 | +struct SpendDashboardPublication: Sendable { |
| 26 | + let revision: UInt64 |
| 27 | + let generation: UInt64 |
| 28 | + let configuration: SpendDashboardConfiguration? |
| 29 | + let loadedAt: Date |
| 30 | + let isRefreshing: Bool |
| 31 | + let inputs: [SpendDashboardModel.ProviderInput] |
| 32 | + let sources: [SpendSourcePublication] |
| 33 | + |
| 34 | + static let empty = SpendDashboardPublication( |
| 35 | + revision: 0, |
| 36 | + generation: 0, |
| 37 | + configuration: nil, |
| 38 | + loadedAt: .distantPast, |
| 39 | + isRefreshing: false, |
| 40 | + inputs: [], |
| 41 | + sources: []) |
| 42 | + |
| 43 | + func model( |
| 44 | + requestedDays: Int, |
| 45 | + now: Date, |
| 46 | + calendar: Calendar, |
| 47 | + preferredCurrencyCode: String, |
| 48 | + hiddenSourceIDs: Set<String> = [], |
| 49 | + hideNativeCodexWhenOpenCodexPresent: Bool = false, |
| 50 | + selectedDay: Date? = nil, |
| 51 | + providerScope: Set<UsageProvider>? = nil) -> SpendDashboardModel |
| 52 | + { |
| 53 | + let staleSourceIDs = Set(self.sources.compactMap { source in |
| 54 | + source.state == .staleLastKnown ? source.id : nil |
| 55 | + }) |
| 56 | + let inputs = self.inputs.filter { input in |
| 57 | + (providerScope?.contains(input.provider) ?? true) && !staleSourceIDs.contains(input.id) |
| 58 | + } |
| 59 | + return SpendDashboardModel.build( |
| 60 | + inputs: inputs, |
| 61 | + requestedDays: requestedDays, |
| 62 | + now: now, |
| 63 | + calendar: calendar, |
| 64 | + preferredCurrencyCode: preferredCurrencyCode, |
| 65 | + hiddenSourceIDs: hiddenSourceIDs, |
| 66 | + hideNativeCodexWhenOpenCodexPresent: hideNativeCodexWhenOpenCodexPresent, |
| 67 | + selectedDay: selectedDay) |
| 68 | + } |
| 69 | + |
| 70 | + func subscriptionCount( |
| 71 | + providerScope: Set<UsageProvider>, |
| 72 | + hiddenSourceIDs: Set<String> = [], |
| 73 | + hideNativeCodexWhenOpenCodexPresent: Bool = false) -> Int |
| 74 | + { |
| 75 | + providerScope.reduce(into: 0) { count, provider in |
| 76 | + let rosterSources = self.subscriptionRosterSources(for: provider) |
| 77 | + let coverageSources = self.coverageSources( |
| 78 | + for: provider, |
| 79 | + hiddenSourceIDs: hiddenSourceIDs, |
| 80 | + hideNativeCodexWhenOpenCodexPresent: hideNativeCodexWhenOpenCodexPresent) |
| 81 | + if rosterSources.isEmpty, coverageSources.isEmpty { |
| 82 | + count += hiddenSourceIDs.contains(provider.rawValue) ? 0 : 1 |
| 83 | + } else { |
| 84 | + count += coverageSources.count |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + func knownCostSubscriptionCount( |
| 90 | + model: SpendDashboardModel, |
| 91 | + providerScope: Set<UsageProvider>, |
| 92 | + hiddenSourceIDs: Set<String> = [], |
| 93 | + hideNativeCodexWhenOpenCodexPresent: Bool = false) -> Int |
| 94 | + { |
| 95 | + let knownInputIDs = Set(model.groups.flatMap(\.providers).compactMap { row in |
| 96 | + row.totalCost == nil ? nil : row.id |
| 97 | + }) |
| 98 | + return self.knownSubscriptionCount( |
| 99 | + knownInputIDs: knownInputIDs, |
| 100 | + providerScope: providerScope, |
| 101 | + hiddenSourceIDs: hiddenSourceIDs, |
| 102 | + hideNativeCodexWhenOpenCodexPresent: hideNativeCodexWhenOpenCodexPresent) |
| 103 | + } |
| 104 | + |
| 105 | + func knownTokenSubscriptionCount( |
| 106 | + model: SpendDashboardModel, |
| 107 | + providerScope: Set<UsageProvider>, |
| 108 | + hiddenSourceIDs: Set<String> = [], |
| 109 | + hideNativeCodexWhenOpenCodexPresent: Bool = false) -> Int |
| 110 | + { |
| 111 | + let knownInputIDs = Set(model.groups.flatMap(\.providers).compactMap { row in |
| 112 | + row.totalTokens == nil ? nil : row.id |
| 113 | + }) |
| 114 | + return self.knownSubscriptionCount( |
| 115 | + knownInputIDs: knownInputIDs, |
| 116 | + providerScope: providerScope, |
| 117 | + hiddenSourceIDs: hiddenSourceIDs, |
| 118 | + hideNativeCodexWhenOpenCodexPresent: hideNativeCodexWhenOpenCodexPresent) |
| 119 | + } |
| 120 | + |
| 121 | + private func knownSubscriptionCount( |
| 122 | + knownInputIDs: Set<String>, |
| 123 | + providerScope: Set<UsageProvider>, |
| 124 | + hiddenSourceIDs: Set<String>, |
| 125 | + hideNativeCodexWhenOpenCodexPresent: Bool) -> Int |
| 126 | + { |
| 127 | + providerScope.reduce(into: 0) { count, provider in |
| 128 | + count += self.coverageSources( |
| 129 | + for: provider, |
| 130 | + hiddenSourceIDs: hiddenSourceIDs, |
| 131 | + hideNativeCodexWhenOpenCodexPresent: hideNativeCodexWhenOpenCodexPresent) |
| 132 | + .count { source in |
| 133 | + source.state == .confirmedEmpty || |
| 134 | + (source.state == .available && knownInputIDs.contains(source.id)) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private func subscriptionRosterSources(for provider: UsageProvider) -> [SpendSourcePublication] { |
| 140 | + self.sources.filter { $0.provider == provider && $0.role == .subscription } |
| 141 | + } |
| 142 | + |
| 143 | + private func coverageSources( |
| 144 | + for provider: UsageProvider, |
| 145 | + hiddenSourceIDs: Set<String>, |
| 146 | + hideNativeCodexWhenOpenCodexPresent: Bool) -> [SpendSourcePublication] |
| 147 | + { |
| 148 | + let rosterSources = self.subscriptionRosterSources(for: provider) |
| 149 | + .filter { !hiddenSourceIDs.contains($0.id) } |
| 150 | + // Provider-specific by design: OpenCodex replaces Codex coverage only with a canonical Codex payload. |
| 151 | + guard provider == .codex else { return rosterSources } |
| 152 | + let visibleOpenCodexInputIDs: Set<String> = Set(self.inputs.compactMap { input -> String? in |
| 153 | + guard input.provider == .codex, |
| 154 | + input.sourceKind == .openCodex, |
| 155 | + !hiddenSourceIDs.contains(input.id) |
| 156 | + else { return nil } |
| 157 | + return input.id |
| 158 | + }) |
| 159 | + let inputBackedEnrichmentSources = self.sources.filter { |
| 160 | + $0.provider == .codex && |
| 161 | + $0.role == .enrichment && |
| 162 | + visibleOpenCodexInputIDs.contains($0.id) |
| 163 | + } |
| 164 | + let canonicalReplacement = inputBackedEnrichmentSources.first { |
| 165 | + $0.id == SpendDashboardModel.openCodexSourceID |
| 166 | + } |
| 167 | + if hideNativeCodexWhenOpenCodexPresent, |
| 168 | + let canonicalReplacement, |
| 169 | + canonicalReplacement.state == SpendSourcePublication.State.available |
| 170 | + { |
| 171 | + return [canonicalReplacement] |
| 172 | + } |
| 173 | + if rosterSources.isEmpty, !inputBackedEnrichmentSources.isEmpty { |
| 174 | + return inputBackedEnrichmentSources |
| 175 | + } |
| 176 | + // Provider-specific by design: only canonical Codex enrichment can replace Codex subscription coverage. |
| 177 | + guard self.subscriptionRosterSources(for: provider).isEmpty, |
| 178 | + !hiddenSourceIDs.contains(SpendDashboardModel.openCodexSourceID), |
| 179 | + let openCodexObservation = self.sources.first(where: { |
| 180 | + $0.id == SpendDashboardModel.openCodexSourceID && |
| 181 | + $0.provider == .codex && |
| 182 | + $0.role == .enrichment |
| 183 | + }) |
| 184 | + else { return rosterSources } |
| 185 | + let hasCodexReplacementInput = self.inputs.contains { |
| 186 | + $0.id == SpendDashboardModel.openCodexSourceID && |
| 187 | + $0.provider == .codex && |
| 188 | + $0.sourceKind == .openCodex |
| 189 | + } |
| 190 | + return hasCodexReplacementInput || openCodexObservation.state == .confirmedEmpty |
| 191 | + ? [openCodexObservation] |
| 192 | + : rosterSources |
| 193 | + } |
| 194 | +} |
0 commit comments