mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-26 12:26:38 -06:00
e9620fba9d
* fix(gateway): refresh provider usage asynchronously
* fix(ui): report a stalled provider-usage refresh on Model Providers
The page observed the incomplete-usage marker but discarded the exhausted
outcome, so once the retry budget was spent it rendered ordinary provider
cards with no usage and no explanation — indistinguishable from providers
that report no usage at all. Keep the outcome and render the warning the
Usage page already owns, reusing usage.providerUsage.stalled rather than
minting a Model Providers key so no locale baseline churns.
A user-initiated refresh now restarts the retry budget. The notice tells the
operator to refresh, so the button has to hand back attempts to spend; only
the forced path resets it, or the budget could never exhaust.
Also fixes tsgo:core:test on the current head: createStore's inferred literal
had no usageStats, so the run-bookkeeping case could not stamp it, and
view.test.ts needed the new prop.
Closes the ClawSweeper P2 at model-providers-page.ts:169-175.
* fix(ui): keep the stalled usage notice when usage.status starts rejecting
loadModelProvidersData turned a rejected usage.status into providerUsage:
null, which the page read as a completed load. observe(false) then reset the
retry budget and cleared the stalled callout, so a permanently broken usage
endpoint rendered as ordinary cards with no usage and no explanation — the
same silent failure the callout was added to prevent. The reset also fired
mid-cycle: one incomplete response followed by one rejection restarted the
budget, so the notice could be deferred indefinitely.
Record the failure at its producer instead of inferring it downstream. A null
providerUsage also means "not loaded yet", and no caller can tell the two
apart, so load.ts now reports providerUsageFailed explicitly and the page
treats a failed read as unresolved rather than resolved-empty.
Found by a Codex review of 417d43b65d.
* revert(gateway): drop the opportunistic model-catalog fast path
It broke two chat.history tests on main — both assert the cold catalog loader
runs exactly once, and reading the prepared snapshot first means it never does.
checks-node-compact-small-10 was red for that reason.
The change was a separate-surface latency fix that this PR picked up in passing,
and the body already offered to split it. Dropping it is the honest resolution:
rewriting main's assertions to accommodate a drive-by optimization would trade
one concern's proof for another's convenience. optional-model-catalog.ts,
server-model-catalog-auth.ts and their test return to the merge-base.
This PR is now only the usage.status non-blocking contract and its clients.
* fix(usage): preserve incomplete retry state
* perf(ui): keep usage capability startup-neutral
* fix(ui): restore provider usage retry convergence
* fix(usage): restore retry and cache invariants
* fix(usage): stabilize provider convergence
* test(ui): exercise provider recovery path
* test(ui): remove stale usage route fixture field
* fix(macos): show provider usage errors
* fix(macos): bound usage retries per menu open
* fix(macos): end usage retries on menu close
---------
Co-authored-by: Josh Lehman <550978+jalehman@users.noreply.github.com>
119 lines
3.8 KiB
Swift
119 lines
3.8 KiB
Swift
import Foundation
|
|
|
|
struct GatewayUsageWindow: Codable {
|
|
let label: String
|
|
let usedPercent: Double
|
|
let resetAt: Double?
|
|
}
|
|
|
|
struct GatewayUsageProvider: Codable {
|
|
let provider: String
|
|
let displayName: String
|
|
let windows: [GatewayUsageWindow]
|
|
let plan: String?
|
|
let error: String?
|
|
}
|
|
|
|
struct GatewayUsageSummary: Codable {
|
|
let updatedAt: Double
|
|
let providers: [GatewayUsageProvider]
|
|
let refreshing: Bool?
|
|
|
|
init(updatedAt: Double, providers: [GatewayUsageProvider], refreshing: Bool? = nil) {
|
|
self.updatedAt = updatedAt
|
|
self.providers = providers
|
|
self.refreshing = refreshing
|
|
}
|
|
}
|
|
|
|
struct UsageRow: Identifiable {
|
|
let id: String
|
|
let providerId: String
|
|
let displayName: String
|
|
let plan: String?
|
|
let windowLabel: String?
|
|
let usedPercent: Double?
|
|
let resetAt: Date?
|
|
let errorText: String?
|
|
|
|
var titleText: String {
|
|
if let plan, !plan.isEmpty { return "\(self.displayName) (\(plan))" }
|
|
return self.displayName
|
|
}
|
|
|
|
var remainingPercent: Int? {
|
|
guard let usedPercent, usedPercent.isFinite else { return nil }
|
|
return max(0, min(100, Int(round(100 - usedPercent))))
|
|
}
|
|
|
|
func detailText(now: Date = .init()) -> String {
|
|
if let errorText, !errorText.isEmpty { return errorText }
|
|
guard let remaining = self.remainingPercent else { return "No data" }
|
|
var parts = ["\(remaining)% left"]
|
|
if let windowLabel, !windowLabel.isEmpty { parts.append(windowLabel) }
|
|
if let resetAt {
|
|
let reset = UsageRow.formatResetRemaining(target: resetAt, now: now)
|
|
if let reset { parts.append("⏱\(reset)") }
|
|
}
|
|
return parts.joined(separator: " · ")
|
|
}
|
|
|
|
private static func formatResetRemaining(target: Date, now: Date) -> String? {
|
|
let diff = target.timeIntervalSince(now)
|
|
if diff <= 0 { return "now" }
|
|
let minutes = Int(floor(diff / 60))
|
|
if minutes < 60 { return "\(minutes)m" }
|
|
let hours = minutes / 60
|
|
let mins = minutes % 60
|
|
if hours < 24 { return mins > 0 ? "\(hours)h \(mins)m" : "\(hours)h" }
|
|
let days = hours / 24
|
|
if days < 7 { return "\(days)d \(hours % 24)h" }
|
|
let formatter = DateFormatter()
|
|
formatter.dateFormat = "MMM d"
|
|
return formatter.string(from: target)
|
|
}
|
|
}
|
|
|
|
extension GatewayUsageSummary {
|
|
func primaryRows() -> [UsageRow] {
|
|
self.providers.compactMap { provider in
|
|
if let window = provider.windows.max(by: { $0.usedPercent < $1.usedPercent }) {
|
|
return UsageRow(
|
|
id: "\(provider.provider)-\(window.label)",
|
|
providerId: provider.provider,
|
|
displayName: provider.displayName,
|
|
plan: provider.plan,
|
|
windowLabel: window.label,
|
|
usedPercent: window.usedPercent,
|
|
resetAt: window.resetAt.map { Date(timeIntervalSince1970: $0 / 1000) },
|
|
errorText: nil)
|
|
}
|
|
|
|
guard let error = provider.error?.trimmingCharacters(in: .whitespacesAndNewlines),
|
|
!error.isEmpty
|
|
else { return nil }
|
|
|
|
return UsageRow(
|
|
id: "\(provider.provider)-error",
|
|
providerId: provider.provider,
|
|
displayName: provider.displayName,
|
|
plan: provider.plan,
|
|
windowLabel: nil,
|
|
usedPercent: nil,
|
|
resetAt: nil,
|
|
errorText: error)
|
|
}
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
enum UsageLoader {
|
|
static func loadSummary() async throws -> GatewayUsageSummary {
|
|
let data = try await ControlChannel.shared.request(
|
|
method: "usage.status",
|
|
params: nil,
|
|
timeoutMs: 5000)
|
|
return try JSONDecoder().decode(GatewayUsageSummary.self, from: data)
|
|
}
|
|
}
|