Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion Sources/CodexBar/MenuCardView+ModelHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,64 @@ extension UsageMenuCardView.Model {
AmpProviderDescriptor.secondaryLabel(snapshot: snapshot) ?? input.metadata.weeklyLabel
} else if input.provider == .alibabatokenplan {
AlibabaTokenPlanProviderDescriptor.secondaryLabel(window: snapshot.secondary) ?? input.metadata.weeklyLabel
} else if input.provider == .sub2api {
"Weekly"
Comment on lines +474 to +475

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Update the provider gatekeeper fingerprint

Adding this .sub2api branch (together with the new .sub2api ternary just below) changes the exact provider-specific reference cluster in MenuCardView+ModelHelpers.swift, but ProviderArchitectureGatekeeperTests still allowlists the rateWindowLabels cluster with the old reference count/fingerprint. In CI, that drift tripwire will report the cluster as changed until the allowlist is updated alongside these new provider-specific references.

Useful? React with 👍 / 👎.

} else {
input.metadata.weeklyLabel
}
let tertiaryLabel = input.provider == .sub2api
? L("Monthly")
: input.metadata.opusLabel.map(L) ?? L("Sonnet")
return (
L(primaryLabel),
L(secondaryLabel),
input.metadata.opusLabel.map(L) ?? L("Sonnet"),
tertiaryLabel,
input.metadata.supportsOpus)
}

static func sub2APILocalizedDetails(_ details: [ProviderDetailSection]) -> [ProviderDetailSection] {
details.map { section in
guard section.title == "Usage summary" else { return section }

do {
var rows: [ProviderDetailSection.Row] = []
var consumedLabels: Set<String> = []

if let balance = section.rows.first(where: { $0.label == "Balance" }) {
try rows.append(ProviderDetailSection.Row(
label: L("Balance"),
value: balance.value,
secondaryValue: balance.secondaryValue))
consumedLabels.insert(balance.label)
}

for period in [
(label: "Today", requests: "Today requests", tokens: "Today tokens"),
(label: "Total", requests: "All time requests", tokens: "All time tokens"),
] {
guard let requests = section.rows.first(where: { $0.label == period.requests }),
let tokens = section.rows.first(where: { $0.label == period.tokens })
else { continue }

var secondaryParts = ["\(tokens.value) \(L("tokens"))"]
if let cost = tokens.secondaryValue {
secondaryParts.append("\(L("Cost")): \(cost)")
}
try rows.append(ProviderDetailSection.Row(
label: L(period.label),
value: "\(requests.value) \(L("requests"))",
secondaryValue: secondaryParts.joined(separator: " · ")))
consumedLabels.formUnion([period.requests, period.tokens])
}

rows.append(contentsOf: section.rows.filter { !consumedLabels.contains($0.label) })
return try ProviderDetailSection(title: L("Usage"), rows: rows, chart: section.chart)
} catch {
return section
}
}
}

static func resetText(
for window: RateWindow,
style: ResetTimeDisplayStyle,
Expand Down
3 changes: 3 additions & 0 deletions Sources/CodexBar/MenuCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,9 @@ extension UsageMenuCardView.Model {
}
}
}
if input.provider == .sub2api {
details = Self.sub2APILocalizedDetails(details)
}
guard input.hidePersonalInfo else { return details }
return details.compactMap { section in
let rows = section.rows.compactMap { row in
Expand Down
63 changes: 60 additions & 3 deletions Tests/CodexBarTests/Sub2APIMenuCardModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,66 @@ struct Sub2APIMenuCardModelTests {

#expect(model.planText == "Enterprise")
#expect(model.usageNotes.isEmpty)
#expect(model.providerDetails.first?.rows.map(\.label) == [
"Balance", "Today requests", "Today tokens", "All time requests", "All time tokens",
])
let usage = try #require(model.providerDetails.first)
#expect(usage.title == "Usage")
#expect(usage.rows.map(\.label) == ["Balance", "Today", "Total"])
#expect(usage.rows[1].value == "4 requests")
#expect(usage.rows[1].secondaryValue == "1,200 tokens · Cost: $1.25")
#expect(usage.rows[2].value == "40 requests")
#expect(usage.rows[2].secondaryValue == "12,000 tokens · Cost: $25.00")
}

@Test
func `usage summary and quota labels follow the selected language`() async throws {
let now = Date(timeIntervalSince1970: 1_720_440_000)
let json = """
{
"mode": "unrestricted",
"subscription": {
"daily_usage_usd": 12,
"weekly_usage_usd": 70,
"monthly_usage_usd": 280,
"daily_limit_usd": 120,
"weekly_limit_usd": 700,
"monthly_limit_usd": 2800
},
"usage": {
"today": { "requests": 4, "total_tokens": 1200, "actual_cost": 1.25 },
"total": { "requests": 40, "total_tokens": 12000, "actual_cost": 25 }
}
}
"""
let snapshot = try await Self.snapshot(json, now: now)
let metadata = try #require(ProviderDefaults.metadata[.sub2api])

let model = CodexBarLocalizationOverride.$appLanguage.withValue("zh-Hans") {
UsageMenuCardView.Model.make(.init(
provider: .sub2api,
metadata: metadata,
snapshot: snapshot,
credits: nil,
creditsError: nil,
dashboard: nil,
dashboardError: nil,
tokenSnapshot: nil,
tokenError: nil,
account: AccountInfo(email: nil, plan: nil),
isRefreshing: false,
lastError: nil,
usageBarsShowUsed: false,
resetTimeDisplayStyle: .countdown,
tokenCostUsageEnabled: false,
showOptionalCreditsAndExtraUsage: true,
hidePersonalInfo: false,
now: now))
}

#expect(model.metrics.map(\.title) == ["每日配额", "每周", "每月"])
let usage = try #require(model.providerDetails.first)
#expect(usage.title == "用量")
#expect(usage.rows.map(\.label) == ["余额", "今天", "总计"])
#expect(usage.rows[1].value == "4 请求")
#expect(usage.rows[1].secondaryValue == "1,200 token · 费用: $1.25")
}

@Test
Expand Down
Loading