From 1e0e869fc649b47ec2ce2edb22b03bd2a8804abd Mon Sep 17 00:00:00 2001 From: jrimmer Date: Thu, 25 Jun 2026 14:02:03 -0700 Subject: [PATCH 1/4] Add Neuralwatt provider --- README.md | 29 +- Sources/CodexBar/MenuCardView.swift | 8 +- Sources/CodexBar/MenuDescriptor.swift | 2 +- .../NeuralWattProviderImplementation.swift | 43 ++ .../NeuralWatt/NeuralWattSettingsStore.swift | 14 + .../ProviderImplementationRegistry.swift | 1 + .../Resources/ProviderIcon-neuralwatt.svg | 3 + Sources/CodexBar/UsageStore.swift | 3 +- .../Config/ProviderConfigEnvironment.swift | 3 + .../CodexBarCore/Logging/LogCategories.swift | 1 + .../NeuralWattProviderDescriptor.swift | 50 ++ .../NeuralWatt/NeuralWattSettingsReader.swift | 61 +++ .../NeuralWatt/NeuralWattUsageFetcher.swift | 437 ++++++++++++++++++ .../Providers/ProviderDescriptor.swift | 1 + .../Providers/ProviderTokenResolver.swift | 12 + .../CodexBarCore/Providers/Providers.swift | 2 + .../TokenAccountSupportCatalog+Data.swift | 7 + .../Vendored/CostUsage/CostUsageScanner.swift | 2 +- .../CodexBarWidgetProvider.swift | 1 + .../CodexBarWidget/CodexBarWidgetViews.swift | 3 + .../MenuCardNeuralWattTests.swift | 55 +++ .../NeuralWattUsageFetcherTests.swift | 373 +++++++++++++++ .../ProviderConfigEnvironmentTests.swift | 13 + .../ProviderEnvironmentResolverTests.swift | 12 + docs/configuration.md | 12 +- docs/neuralwatt.md | 79 ++++ docs/providers.md | 60 +++ 27 files changed, 1277 insertions(+), 10 deletions(-) create mode 100644 Sources/CodexBar/Providers/NeuralWatt/NeuralWattProviderImplementation.swift create mode 100644 Sources/CodexBar/Providers/NeuralWatt/NeuralWattSettingsStore.swift create mode 100644 Sources/CodexBar/Resources/ProviderIcon-neuralwatt.svg create mode 100644 Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift create mode 100644 Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattSettingsReader.swift create mode 100644 Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattUsageFetcher.swift create mode 100644 Tests/CodexBarTests/MenuCardNeuralWattTests.swift create mode 100644 Tests/CodexBarTests/NeuralWattUsageFetcherTests.swift create mode 100644 docs/neuralwatt.md diff --git a/README.md b/README.md index d6261847e3..605fef4467 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# CodexBar 🎚️ — May your tokens never run out. +# CodexBar 🎚️ — May your tokens never run out > Every AI coding limit, in your menu bar. @@ -25,36 +25,47 @@ Tiny macOS 14+ menu bar app that keeps **AI coding-provider limits visible** and ## Install ### Requirements + - macOS 14+ (Sonoma) ### GitHub Releases + Download: ### Homebrew + ```bash brew install --cask codexbar ``` ### CLI Tarballs (macOS/Linux) + Homebrew formula (Linux today): + ```bash brew install steipete/tap/codexbar ``` + Arch Linux AUR package: + ```bash yay -S codexbar-cli ``` + Or download release tarballs from GitHub Releases: + - macOS: `CodexBarCLI-v-macos-arm64.tar.gz`, `CodexBarCLI-v-macos-x86_64.tar.gz` - Linux (glibc): `CodexBarCLI-v-linux-aarch64.tar.gz`, `CodexBarCLI-v-linux-x86_64.tar.gz` - Linux (static musl): `CodexBarCLI-v-linux-musl-aarch64.tar.gz`, `CodexBarCLI-v-linux-musl-x86_64.tar.gz` ### First run + - Open Settings → Providers and enable what you use. - Install/sign in to the provider sources you rely on: CLIs, browser sessions, OAuth/device flow, API keys, local app files, or provider apps depending on the provider. - Optional: Settings → Providers → Codex → OpenAI cookies (Automatic or Manual) to add dashboard extras. ### Set API keys from the CLI + Provider toggles and API keys live in the resolved CodexBar config file. New installs use `~/.config/codexbar/config.json`; existing `~/.codexbar/config.json` installs still load from the legacy path. You can script the same provider list that Settings → Providers uses: @@ -128,13 +139,16 @@ See [CLI configuration](docs/cli-configuration.md) for the full flow. - [Deepgram](docs/deepgram.md) — API key usage summaries across speech, agent, token, and TTS metrics. - [Poe](docs/poe.md) — API key for current point balance and recent points history. - [Chutes](docs/chutes.md) — API key for subscription usage, rolling and monthly quota windows, and pay-as-you-go quotas. +- [Neuralwatt](docs/neuralwatt.md) — API key for USD credit balance and optional per-key spending allowance from Neuralwatt Cloud. - Open to new providers: [provider authoring guide](docs/provider.md). ## Icon & Screenshot + The menu bar icon is a tiny usage meter. Bar meaning is provider-specific, and errors/stale data can dim the icon or show an incident indicator. ## Features + - Multi-provider menu bar with per-provider toggles (Settings → Providers). - Provider-specific usage meters with reset countdowns. - Optional Codex web dashboard enrichments (code review remaining, usage breakdown, credits history). @@ -151,9 +165,11 @@ show an incident indicator. - Privacy-first: on-device parsing by default; browser cookies are opt-in and reused (no passwords stored). ## Privacy note + Wondering if CodexBar scans your disk? It doesn’t crawl your filesystem; it reads a small set of known locations (browser cookies/local storage, provider config files, local JSONL logs) when the related features are enabled. Provider tokens and token-account settings live in the CodexBar config file with restrictive file permissions. See the discussion and audit notes in [issue #12](https://github.com/steipete/CodexBar/issues/12). ## macOS permissions (why they’re needed) + - **Full Disk Access (optional)**: only required to read Safari cookies/local storage for web-based providers. If you don’t grant it, use another supported browser, manual cookies/API keys, OAuth, or CLI/local sources where that provider supports them. - **Keychain access (prompted by macOS)**: - Chromium cookie import needs the browser “Safe Storage” key to decrypt cookies. @@ -175,6 +191,7 @@ Wondering if CodexBar scans your disk? It doesn’t crawl your filesystem; it re - **What we do not request in the background**: no Screen Recording or Accessibility permissions; user-triggered helper actions may ask macOS for Automation permission to open Terminal. No passwords are stored (browser cookies are reused when you opt in). ## Docs + - Providers overview: [docs/providers.md](docs/providers.md) - Provider authoring: [docs/provider.md](docs/provider.md) - Issue labeling guide: [docs/ISSUE_LABELING.md](docs/ISSUE_LABELING.md) @@ -194,12 +211,14 @@ Wondering if CodexBar scans your disk? It doesn’t crawl your filesystem; it re - Changelog: [CHANGELOG.md](CHANGELOG.md) ## Getting started (dev) + - Clone the repo and open it in Xcode or run the scripts directly. - Launch once, then toggle providers in Settings → Providers. - Install/sign in to provider sources you rely on (CLIs, browser cookies, OAuth/device flow, API keys, or local app/config files). - Optional: set OpenAI cookies (Automatic or Manual) for Codex dashboard extras. ## Build from source + Requires macOS 14+ and Swift 6.2+. ```bash @@ -208,6 +227,7 @@ open CodexBar.app ``` Dev loop: + ```bash ./Scripts/compile_and_run.sh ./Scripts/compile_and_run.sh --test # also run the sharded test suite before packaging/relaunching @@ -216,30 +236,37 @@ make docs-list # list docs with frontmatter summaries ``` CLI install: + ```bash # after installing CodexBar.app in /Applications ./bin/install-codexbar-cli.sh ``` ## Related + - ✂️ [Trimmy](https://github.com/steipete/Trimmy) — “Paste once, run once.” Flatten multi-line shell snippets so they paste and run. - 🧳 [MCPorter](https://mcporter.dev) — TypeScript toolkit + CLI for Model Context Protocol servers. - 🧿 [oracle](https://askoracle.dev) — Ask the oracle when you're stuck. Invoke GPT-5 Pro with a custom context and files. ## Looking for a Windows version? + - [Win-CodexBar](https://github.com/Finesssee/Win-CodexBar) ## Linux desktop integration? + - [codexbar-waybar](https://github.com/Marouan-chak/codexbar-waybar) — Waybar custom module + GTK4 popover for Hyprland / Sway / other Wayland compositors, built on top of the bundled Linux CLI. - [Codexbar GNOME](https://extensions.gnome.org/extension/9841/codexbar/) — GNOME Shell extension that brings CodexBar usage into the desktop panel. - [noctalia-codex-usage](https://github.com/rayoplateado/noctalia-codex-usage) — Noctalia/Quickshell plugin that shows Codex 5-hour and weekly usage limits, built on top of the bundled Linux CLI. - [KodexBar](https://github.com/tylxr59/KodexBar) — KDE Plasma widget that shows CodexBar usage in the Plasma panel, built on top of the bundled Linux CLI. ## Status bar & terminal integration + - [showy-quota](https://github.com/enieuwy/showy-quota) — always-on AI plan quota strips for SketchyBar, tmux, and Zellij (standalone WASM plugin), built on `codexbar serve` / the bundled CLI. ## Credits + Inspired by [ccusage](https://github.com/ryoppippi/ccusage) (MIT), specifically the cost usage tracking. ## License + MIT • Peter Steinberger ([steipete](https://twitter.com/steipete)) diff --git a/Sources/CodexBar/MenuCardView.swift b/Sources/CodexBar/MenuCardView.swift index 532b508f16..766f2f79e1 100644 --- a/Sources/CodexBar/MenuCardView.swift +++ b/Sources/CodexBar/MenuCardView.swift @@ -1248,7 +1248,7 @@ extension UsageMenuCardView.Model { primaryDetailLeft = detail } if input.provider == .warp || input.provider == .kilo || input.provider == .mimo || input.provider == .deepseek - || input.provider == .litellm, + || input.provider == .neuralwatt || input.provider == .litellm, let detail = primary.resetDescription, !detail.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { @@ -1273,7 +1273,7 @@ extension UsageMenuCardView.Model { primaryDetailText = detail if input.provider == .manus { primaryResetText = nil } } - if [.warp, .kilo, .mimo, .deepseek, .litellm].contains(input.provider), primary.resetsAt == nil { + if [.warp, .kilo, .mimo, .deepseek, .neuralwatt, .litellm].contains(input.provider), primary.resetsAt == nil { primaryResetText = nil } // Abacus: show credits as detail, compute pace on the primary monthly window @@ -1339,8 +1339,8 @@ extension UsageMenuCardView.Model { primaryPacePercent = regen.pace.pacePercent primaryPaceOnTop = regen.pace.paceOnTop } - let primaryStatusText = input.provider == .deepseek ? primaryDetailText : nil - if input.provider == .deepseek { + let primaryStatusText = input.provider == .deepseek || input.provider == .neuralwatt ? primaryDetailText : nil + if input.provider == .deepseek || input.provider == .neuralwatt { primaryDetailText = nil } return Metric( diff --git a/Sources/CodexBar/MenuDescriptor.swift b/Sources/CodexBar/MenuDescriptor.swift index f63a8cdd88..12cbc6f808 100644 --- a/Sources/CodexBar/MenuDescriptor.swift +++ b/Sources/CodexBar/MenuDescriptor.swift @@ -153,7 +153,7 @@ struct MenuDescriptor { if let primary = snap.primary { let primaryDetail = primary.resetDescription?.trimmingCharacters(in: .whitespacesAndNewlines) let primaryDescriptionIsDetail = provider == .warp || provider == .kilo || provider == .abacus || - provider == .deepseek || provider == .azureopenai || provider == .mimo + provider == .deepseek || provider == .neuralwatt || provider == .azureopenai || provider == .mimo let primaryWindow = if primaryDescriptionIsDetail { // Some providers use resetDescription for non-reset detail // (e.g., "Unlimited", "X/Y credits"). Avoid rendering it as a "Resets ..." line. diff --git a/Sources/CodexBar/Providers/NeuralWatt/NeuralWattProviderImplementation.swift b/Sources/CodexBar/Providers/NeuralWatt/NeuralWattProviderImplementation.swift new file mode 100644 index 0000000000..cd67345e2b --- /dev/null +++ b/Sources/CodexBar/Providers/NeuralWatt/NeuralWattProviderImplementation.swift @@ -0,0 +1,43 @@ +import CodexBarCore +import Foundation + +struct NeuralWattProviderImplementation: ProviderImplementation { + let id: UsageProvider = .neuralwatt + + @MainActor + func presentation(context _: ProviderPresentationContext) -> ProviderPresentation { + ProviderPresentation { _ in "api" } + } + + @MainActor + func observeSettings(_ settings: SettingsStore) { + _ = settings.neuralWattAPIKey + } + + @MainActor + func isAvailable(context: ProviderAvailabilityContext) -> Bool { + if NeuralWattSettingsReader.apiKey(environment: context.environment) != nil { + return true + } + if !context.settings.neuralWattAPIKey.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { + return true + } + return !context.settings.tokenAccounts(for: .neuralwatt).isEmpty + } + + @MainActor + func settingsFields(context: ProviderSettingsContext) -> [ProviderSettingsFieldDescriptor] { + [ + ProviderSettingsFieldDescriptor( + id: "neuralwatt-api-key", + title: "API key", + subtitle: "Stored in the CodexBar config file. Get your key from portal.neuralwatt.com/dashboard/api-keys.", + kind: .secure, + placeholder: "sk-...", + binding: context.stringBinding(\.neuralWattAPIKey), + actions: [], + isVisible: nil, + onActivate: nil), + ] + } +} diff --git a/Sources/CodexBar/Providers/NeuralWatt/NeuralWattSettingsStore.swift b/Sources/CodexBar/Providers/NeuralWatt/NeuralWattSettingsStore.swift new file mode 100644 index 0000000000..c5aa7a1625 --- /dev/null +++ b/Sources/CodexBar/Providers/NeuralWatt/NeuralWattSettingsStore.swift @@ -0,0 +1,14 @@ +import CodexBarCore +import Foundation + +extension SettingsStore { + var neuralWattAPIKey: String { + get { self.configSnapshot.providerConfig(for: .neuralwatt)?.sanitizedAPIKey ?? "" } + set { + self.updateProviderConfig(provider: .neuralwatt) { entry in + entry.apiKey = self.normalizedConfigValue(newValue) + } + self.logSecretUpdate(provider: .neuralwatt, field: "apiKey", value: newValue) + } + } +} diff --git a/Sources/CodexBar/Providers/Shared/ProviderImplementationRegistry.swift b/Sources/CodexBar/Providers/Shared/ProviderImplementationRegistry.swift index f3b8237451..e7816efa9d 100644 --- a/Sources/CodexBar/Providers/Shared/ProviderImplementationRegistry.swift +++ b/Sources/CodexBar/Providers/Shared/ProviderImplementationRegistry.swift @@ -67,6 +67,7 @@ enum ProviderImplementationRegistry { case .deepgram: DeepgramProviderImplementation() case .poe: PoeProviderImplementation() case .chutes: ChutesProviderImplementation() + case .neuralwatt: NeuralWattProviderImplementation() } } diff --git a/Sources/CodexBar/Resources/ProviderIcon-neuralwatt.svg b/Sources/CodexBar/Resources/ProviderIcon-neuralwatt.svg new file mode 100644 index 0000000000..cf43777aca --- /dev/null +++ b/Sources/CodexBar/Resources/ProviderIcon-neuralwatt.svg @@ -0,0 +1,3 @@ + + + diff --git a/Sources/CodexBar/UsageStore.swift b/Sources/CodexBar/UsageStore.swift index e02a2443a5..ba9278459f 100644 --- a/Sources/CodexBar/UsageStore.swift +++ b/Sources/CodexBar/UsageStore.swift @@ -1098,7 +1098,8 @@ extension UsageStore { case .gemini, .antigravity, .opencode, .opencodego, .alibabatokenplan, .factory, .copilot, .devin, .vertexai, .kilo, .kiro, .kimi, .kimik2, .moonshot, .jetbrains, .perplexity, .mimo, .doubao, .sakana, .abacus, .mistral, .codebuff, .crof, .windsurf, .venice, .manus, .commandcode, .stepfun, - .bedrock, .grok, .groq, .t3chat, .llmproxy, .litellm, .zed, .deepgram, .poe, .chutes: + .bedrock, .grok, .groq, .t3chat, .llmproxy, .litellm, .zed, .deepgram, .poe, .chutes, + .neuralwatt: return unimplementedDebugLogMessages[provider] ?? "Debug log not yet implemented" } } diff --git a/Sources/CodexBarCore/Config/ProviderConfigEnvironment.swift b/Sources/CodexBarCore/Config/ProviderConfigEnvironment.swift index f38a560ea8..cf5bdefe5a 100644 --- a/Sources/CodexBarCore/Config/ProviderConfigEnvironment.swift +++ b/Sources/CodexBarCore/Config/ProviderConfigEnvironment.swift @@ -102,6 +102,7 @@ public enum ProviderConfigEnvironment { } } + // swiftlint:disable:next cyclomatic_complexity private static func directAPIKeyEnvironmentKey(for provider: UsageProvider) -> String? { switch provider { case .amp: @@ -126,6 +127,8 @@ public enum ProviderConfigEnvironment { OpenRouterSettingsReader.envKey case .elevenlabs: ElevenLabsSettingsReader.apiKeyEnvironmentKey + case .neuralwatt: + NeuralWattSettingsReader.apiKeyEnvironmentKey case .moonshot: MoonshotSettingsReader.apiKeyEnvironmentKeys.first case .kimi: diff --git a/Sources/CodexBarCore/Logging/LogCategories.swift b/Sources/CodexBarCore/Logging/LogCategories.swift index f0321295ec..b04d21ddd6 100644 --- a/Sources/CodexBarCore/Logging/LogCategories.swift +++ b/Sources/CodexBarCore/Logging/LogCategories.swift @@ -55,6 +55,7 @@ public enum LogCategories { public static let minimaxUsage = "minimax-usage" public static let minimaxWeb = "minimax-web" public static let moonshotUsage = "moonshot-usage" + public static let neuralWattUsage = "neuralwatt-usage" public static let notifications = "notifications" public static let openAIWeb = "openai-web" public static let openAIWebview = "openai-webview" diff --git a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift new file mode 100644 index 0000000000..28b9020967 --- /dev/null +++ b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift @@ -0,0 +1,50 @@ +import Foundation + +public enum NeuralWattProviderDescriptor { + public static let descriptor: ProviderDescriptor = Self.makeDescriptor() + + static func makeDescriptor() -> ProviderDescriptor { + ProviderDescriptor( + id: .neuralwatt, + metadata: ProviderMetadata( + id: .neuralwatt, + displayName: "Neuralwatt", + sessionLabel: "Credits", + weeklyLabel: "Spend", + opusLabel: nil, + supportsOpus: false, + supportsCredits: true, + creditsHint: "Energy-based USD credit balance.", + toggleTitle: "Show Neuralwatt usage", + cliName: "neuralwatt", + defaultEnabled: false, + isPrimaryProvider: false, + usesAccountFallback: false, + browserCookieOrder: nil, + dashboardURL: "https://portal.neuralwatt.com/dashboard", + subscriptionDashboardURL: "https://portal.neuralwatt.com/dashboard", + changelogURL: "https://portal.neuralwatt.com/docs/changelog", + statusPageURL: nil, + statusLinkURL: "https://portal.neuralwatt.com/status"), + branding: ProviderBranding( + iconStyle: .neuralwatt, + iconResourceName: "ProviderIcon-neuralwatt", + color: ProviderColor(red: 0.22, green: 0.85, blue: 0.55)), + tokenCost: ProviderTokenCostConfig( + supportsTokenCost: false, + noDataMessage: { "Neuralwatt token cost history is not available via the quota API." }), + fetchPlan: .apiToken( + strategyID: "neuralwatt.api", + resolveToken: { ProviderTokenResolver.neuralWattToken(environment: $0) }, + missingCredentialsError: { NeuralWattUsageError.missingCredentials }, + loadUsage: { apiKey, context in + try await NeuralWattUsageFetcher.fetchUsage( + apiKey: apiKey, + environment: context.env).toUsageSnapshot() + }), + cli: ProviderCLIConfig( + name: "neuralwatt", + aliases: ["nw", "neural"], + versionDetector: nil)) + } +} diff --git a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattSettingsReader.swift b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattSettingsReader.swift new file mode 100644 index 0000000000..cea42dab70 --- /dev/null +++ b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattSettingsReader.swift @@ -0,0 +1,61 @@ +import Foundation + +public enum NeuralWattSettingsReader { + public static let apiKeyEnvironmentKey = "NEURALWATT_API_KEY" + public static let apiKeyEnvironmentKeys = [ + Self.apiKeyEnvironmentKey, + ] + public static let apiURLEnvironmentKey = "NEURALWATT_API_URL" + + public static func apiKey(environment: [String: String] = ProcessInfo.processInfo.environment) -> String? { + for key in self.apiKeyEnvironmentKeys { + guard let token = self.cleaned(environment[key]) else { continue } + return token + } + return nil + } + + public static func apiURL(environment: [String: String] = ProcessInfo.processInfo.environment) -> URL { + if let override = self.validAPIURL(environment: environment) { + return override + } + return URL(string: "https://api.neuralwatt.com")! + } + + public static func validateEndpointOverrides( + environment: [String: String] = ProcessInfo.processInfo.environment) throws + { + guard let raw = self.cleaned(environment[self.apiURLEnvironmentKey]) else { return } + guard ProviderEndpointOverrideValidator.normalizedHTTPSURL(from: raw) == nil else { return } + throw NeuralWattSettingsError.invalidEndpointOverride(self.apiURLEnvironmentKey) + } + + static func cleaned(_ raw: String?) -> String? { + guard var value = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !value.isEmpty else { + return nil + } + if (value.hasPrefix("\"") && value.hasSuffix("\"")) || + (value.hasPrefix("'") && value.hasSuffix("'")) + { + value = String(value.dropFirst().dropLast()) + } + value = value.trimmingCharacters(in: .whitespacesAndNewlines) + return value.isEmpty ? nil : value + } + + private static func validAPIURL(environment: [String: String]) -> URL? { + guard let raw = self.cleaned(environment[self.apiURLEnvironmentKey]) else { return nil } + return ProviderEndpointOverrideValidator.normalizedHTTPSURL(from: raw) + } +} + +public enum NeuralWattSettingsError: LocalizedError, Sendable, Equatable { + case invalidEndpointOverride(String) + + public var errorDescription: String? { + switch self { + case let .invalidEndpointOverride(key): + "Neuralwatt endpoint override \(key) must use HTTPS or a bare host." + } + } +} diff --git a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattUsageFetcher.swift b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattUsageFetcher.swift new file mode 100644 index 0000000000..2b6167c7f8 --- /dev/null +++ b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattUsageFetcher.swift @@ -0,0 +1,437 @@ +import Foundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +// MARK: - Response models + +public struct NeuralWattBalance: Codable, Sendable, Equatable { + public let creditsRemainingUSD: Double? + public let totalCreditsUSD: Double? + public let creditsUsedUSD: Double? + public let accountingMethod: String? + + private enum CodingKeys: String, CodingKey { + case creditsRemainingUSD = "credits_remaining_usd" + case totalCreditsUSD = "total_credits_usd" + case creditsUsedUSD = "credits_used_usd" + case accountingMethod = "accounting_method" + } +} + +public struct NeuralWattUsagePeriod: Codable, Sendable, Equatable { + public let costUSD: Double? + public let requests: Int? + public let tokens: Int? + public let energyKWh: Double? + + private enum CodingKeys: String, CodingKey { + case costUSD = "cost_usd" + case requests + case tokens + case energyKWh = "energy_kwh" + } +} + +public struct NeuralWattUsage: Codable, Sendable, Equatable { + public let lifetime: NeuralWattUsagePeriod? + public let currentMonth: NeuralWattUsagePeriod? + + private enum CodingKeys: String, CodingKey { + case lifetime + case currentMonth = "current_month" + } +} + +public struct NeuralWattLimits: Codable, Sendable, Equatable { + public let overageLimitUSD: Double? + public let rateLimitTier: String? + + private enum CodingKeys: String, CodingKey { + case overageLimitUSD = "overage_limit_usd" + case rateLimitTier = "rate_limit_tier" + } +} + +public struct NeuralWattSubscription: Codable, Sendable, Equatable { + public let plan: String? + public let status: String? + public let billingInterval: String? + public let currentPeriodStart: Date? + public let currentPeriodEnd: Date? + public let autoRenew: Bool? + public let kwhIncluded: Double? + public let kwhUsed: Double? + public let kwhRemaining: Double? + public let inOverage: Bool? + + private enum CodingKeys: String, CodingKey { + case plan + case status + case billingInterval = "billing_interval" + case currentPeriodStart = "current_period_start" + case currentPeriodEnd = "current_period_end" + case autoRenew = "auto_renew" + case kwhIncluded = "kwh_included" + case kwhUsed = "kwh_used" + case kwhRemaining = "kwh_remaining" + case inOverage = "in_overage" + } +} + +public struct NeuralWattKeyAllowance: Codable, Sendable, Equatable { + public let limitUSD: Double? + public let period: String? + public let spentUSD: Double? + public let remainingUSD: Double? + public let blocked: Bool? + + private enum CodingKeys: String, CodingKey { + case limitUSD = "limit_usd" + case period + case spentUSD = "spent_usd" + case remainingUSD = "remaining_usd" + case blocked + } +} + +public struct NeuralWattKey: Codable, Sendable, Equatable { + public let name: String? + public let allowance: NeuralWattKeyAllowance? +} + +public struct NeuralWattQuotaResponse: Decodable, Sendable { + public let snapshotAt: String? + public let balance: NeuralWattBalance? + public let usage: NeuralWattUsage? + public let limits: NeuralWattLimits? + public let subscription: NeuralWattSubscription? + public let key: NeuralWattKey? + + private enum CodingKeys: String, CodingKey { + case snapshotAt = "snapshot_at" + case balance + case usage + case limits + case subscription + case key + } + + private init( + snapshotAt: String?, + balance: NeuralWattBalance?, + usage: NeuralWattUsage?, + limits: NeuralWattLimits?, + subscription: NeuralWattSubscription?, + key: NeuralWattKey?) + { + self.snapshotAt = snapshotAt + self.balance = balance + self.usage = usage + self.limits = limits + self.subscription = subscription + self.key = key + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.snapshotAt = try container.decodeIfPresent(String.self, forKey: .snapshotAt) + self.balance = try container.decodeIfPresent(NeuralWattBalance.self, forKey: .balance) + self.usage = try container.decodeIfPresent(NeuralWattUsage.self, forKey: .usage) + self.limits = try container.decodeIfPresent(NeuralWattLimits.self, forKey: .limits) + // `subscription` is documented as always-present: object when active, `null` otherwise. + self.subscription = try container.decodeIfPresent(NeuralWattSubscription.self, forKey: .subscription) + self.key = try container.decodeIfPresent(NeuralWattKey.self, forKey: .key) + } +} + +// MARK: - Snapshot + +public struct NeuralWattUsageSnapshot: Codable, Sendable, Equatable { + public let creditsRemainingUSD: Double? + public let totalCreditsUSD: Double? + public let creditsUsedUSD: Double? + public let accountingMethod: String? + public let currentMonthCostUSD: Double? + public let currentMonthEnergyKWh: Double? + public let subscription: NeuralWattSubscription? + public let keyAllowance: NeuralWattKeyAllowance? + public let rateLimitTier: String? + public let updatedAt: Date + + public init( + creditsRemainingUSD: Double?, + totalCreditsUSD: Double?, + creditsUsedUSD: Double?, + accountingMethod: String?, + currentMonthCostUSD: Double?, + currentMonthEnergyKWh: Double?, + subscription: NeuralWattSubscription?, + keyAllowance: NeuralWattKeyAllowance?, + rateLimitTier: String?, + updatedAt: Date) + { + self.creditsRemainingUSD = creditsRemainingUSD + self.totalCreditsUSD = totalCreditsUSD + self.creditsUsedUSD = creditsUsedUSD + self.accountingMethod = accountingMethod + self.currentMonthCostUSD = currentMonthCostUSD + self.currentMonthEnergyKWh = currentMonthEnergyKWh + self.subscription = subscription + self.keyAllowance = keyAllowance + self.rateLimitTier = rateLimitTier + self.updatedAt = updatedAt + } + + public var creditUsedPercent: Double { + if self.hasKnownZeroRemainingBalance { + return 100 + } + guard let used = self.effectiveUsedCredits, let total = self.effectiveTotalCredits, total > 0 else { + return 0 + } + return min(100, max(0, used / total * 100)) + } + + private var hasKnownZeroRemainingBalance: Bool { + Self.validNonNegative(self.creditsRemainingUSD) == 0 && self.effectiveTotalCredits == nil + } + + public var effectiveRemainingCredits: Double? { + if let remaining = Self.validNonNegative(self.creditsRemainingUSD) { return remaining } + guard let total = self.effectiveTotalCredits, let used = self.effectiveUsedCredits else { return nil } + return max(0, total - used) + } + + public var effectiveTotalCredits: Double? { + if let total = Self.validPositive(self.totalCreditsUSD) { return total } + guard let remaining = Self.validNonNegative(self.creditsRemainingUSD), + let used = Self.validNonNegative(self.creditsUsedUSD) + else { return nil } + let total = remaining + used + return total > 0 ? total : nil + } + + public var effectiveUsedCredits: Double? { + if let used = Self.validNonNegative(self.creditsUsedUSD) { return used } + guard let total = Self.validPositive(self.totalCreditsUSD), + let remaining = Self.validNonNegative(self.creditsRemainingUSD) + else { return nil } + return max(0, total - remaining) + } + + public var keyAllowanceUsedPercent: Double? { + guard let spent = self.keyAllowance?.spentUSD, let limit = self.keyAllowance?.limitUSD, limit > 0 else { + return nil + } + return min(100, max(0, spent / limit * 100)) + } + + public func toUsageSnapshot() -> UsageSnapshot { + // Neuralwatt is a credit-exhaustion model (like DeepSeek): USD credits deplete + // as you use the API and do not reset on a billing cycle. There is no renewal + // date to surface, so the primary window carries only the balance summary. + let primary = RateWindow( + usedPercent: self.creditUsedPercent, + windowMinutes: nil, + resetsAt: nil, + resetDescription: self.creditSummary) + + var extras: [NamedRateWindow] = [] + if let percent = self.keyAllowanceUsedPercent, let allowance = self.keyAllowance { + let periodTitle = (allowance.period ?? "allowance").capitalized + extras.append(NamedRateWindow( + id: "key-allowance", + title: "Key \(periodTitle)", + window: RateWindow( + usedPercent: percent, + windowMinutes: nil, + resetsAt: nil, + resetDescription: nil))) + } + + let identity = ProviderIdentitySnapshot( + providerID: .neuralwatt, + accountEmail: nil, + accountOrganization: nil, + loginMethod: self.displayLoginMethod) + + return UsageSnapshot( + primary: primary, + secondary: nil, + tertiary: nil, + extraRateWindows: extras.isEmpty ? nil : extras, + subscriptionRenewsAt: nil, + updatedAt: self.updatedAt, + identity: identity) + } + + private var creditSummary: String { + guard let remaining = self.effectiveRemainingCredits else { return "Balance unavailable" } + guard let total = self.effectiveTotalCredits else { + return "\(Self.formatUSD(remaining)) remaining" + } + return "\(Self.formatUSD(remaining)) remaining of \(Self.formatUSD(total))" + } + + private var displayLoginMethod: String? { + // Credits are account-wide; surface the accounting method (Token vs Energy) + // when present. Subscription plan is shown only as supplementary identity. + if let method = self.accountingMethod, !method.isEmpty { + return method.capitalized + } + return self.subscription?.plan?.replacingOccurrences(of: "_", with: " ").capitalized + } + + fileprivate static func validNonNegative(_ value: Double?) -> Double? { + guard let value, value.isFinite, value >= 0 else { return nil } + return value + } + + fileprivate static func validPositive(_ value: Double?) -> Double? { + guard let value, value.isFinite, value > 0 else { return nil } + return value + } + + private static func formatUSD(_ value: Double) -> String { + let formatter = NumberFormatter() + formatter.locale = Locale(identifier: "en_US_POSIX") + formatter.numberStyle = .currency + formatter.currencyCode = "USD" + formatter.maximumFractionDigits = 2 + formatter.minimumFractionDigits = 2 + return formatter.string(from: NSNumber(value: value)) ?? String(format: "$%.2f", value) + } +} + +// MARK: - Errors + +public enum NeuralWattUsageError: LocalizedError, Sendable { + case missingCredentials + case networkError(String) + case apiError(String) + case parseFailed(String) + + public var errorDescription: String? { + switch self { + case .missingCredentials: + "Missing Neuralwatt API key. Set apiKey in the CodexBar config file or NEURALWATT_API_KEY." + case let .networkError(message): + "Neuralwatt network error: \(message)" + case let .apiError(message): + "Neuralwatt API error: \(message)" + case let .parseFailed(message): + "Failed to parse Neuralwatt response: \(message)" + } + } +} + +// MARK: - Fetcher + +public struct NeuralWattUsageFetcher: Sendable { + private static let log = CodexBarLog.logger(LogCategories.neuralWattUsage) + private static let timeoutSeconds: TimeInterval = 15 + + public static func fetchUsage( + apiKey: String, + environment: [String: String] = ProcessInfo.processInfo.environment) async throws -> NeuralWattUsageSnapshot + { + let trimmed = apiKey.trimmingCharacters(in: .whitespacesAndNewlines) + guard !trimmed.isEmpty else { + throw NeuralWattUsageError.missingCredentials + } + try NeuralWattSettingsReader.validateEndpointOverrides(environment: environment) + + let url = Self.quotaURL(baseURL: NeuralWattSettingsReader.apiURL(environment: environment)) + var request = URLRequest(url: url) + request.httpMethod = "GET" + request.setValue("Bearer \(trimmed)", forHTTPHeaderField: "Authorization") + request.setValue("application/json", forHTTPHeaderField: "Accept") + request.timeoutInterval = Self.timeoutSeconds + + let response: ProviderHTTPResponse + do { + response = try await ProviderHTTPClient.shared.response(for: request) + } catch { + throw NeuralWattUsageError.networkError(error.localizedDescription) + } + + switch response.statusCode { + case 200: + return try Self.parseSnapshot(data: response.data, updatedAt: Date()) + case 401, 403: + throw NeuralWattUsageError.missingCredentials + default: + Self.log.error("Neuralwatt API returned \(response.statusCode)") + throw NeuralWattUsageError.apiError("HTTP \(response.statusCode)") + } + } + + static func _parseSnapshotForTesting(_ data: Data, updatedAt: Date) throws -> NeuralWattUsageSnapshot { + try self.parseSnapshot(data: data, updatedAt: updatedAt) + } + + private static func parseSnapshot(data: Data, updatedAt: Date) throws -> NeuralWattUsageSnapshot { + let decoder = JSONDecoder() + decoder.dateDecodingStrategy = .custom(Self.decodeISO8601Date) + let decoded: NeuralWattQuotaResponse + do { + decoded = try decoder.decode(NeuralWattQuotaResponse.self, from: data) + } catch { + throw NeuralWattUsageError.parseFailed(error.localizedDescription) + } + + guard let balance = decoded.balance else { + throw NeuralWattUsageError.parseFailed("Missing Neuralwatt balance object") + } + guard NeuralWattUsageSnapshot.validNonNegative(balance.creditsRemainingUSD) != nil || + NeuralWattUsageSnapshot.validNonNegative(balance.creditsUsedUSD) != nil || + NeuralWattUsageSnapshot.validPositive(balance.totalCreditsUSD) != nil + else { + throw NeuralWattUsageError.parseFailed("Missing Neuralwatt credit balance fields") + } + + return NeuralWattUsageSnapshot( + creditsRemainingUSD: balance.creditsRemainingUSD, + totalCreditsUSD: balance.totalCreditsUSD, + creditsUsedUSD: balance.creditsUsedUSD, + accountingMethod: balance.accountingMethod, + currentMonthCostUSD: decoded.usage?.currentMonth?.costUSD, + currentMonthEnergyKWh: decoded.usage?.currentMonth?.energyKWh, + subscription: decoded.subscription, + keyAllowance: decoded.key?.allowance, + rateLimitTier: decoded.limits?.rateLimitTier, + updatedAt: updatedAt) + } + + private static func decodeISO8601Date(from decoder: Decoder) throws -> Date { + let container = try decoder.singleValueContainer() + let value = try container.decode(String.self) + let standardFormatter = ISO8601DateFormatter() + standardFormatter.formatOptions = [.withInternetDateTime] + if let date = standardFormatter.date(from: value) { + return date + } + + let fractionalFormatter = ISO8601DateFormatter() + fractionalFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds] + if let date = fractionalFormatter.date(from: value) { + return date + } + + throw DecodingError.dataCorruptedError( + in: container, + debugDescription: "Invalid ISO8601 date: \(value)") + } + + private static func quotaURL(baseURL: URL) -> URL { + var url = baseURL + let pathComponents = url.path.split(separator: "/") + if pathComponents.last == "v1" { + url.append(path: "quota") + } else { + url.append(path: "v1/quota") + } + return url + } +} diff --git a/Sources/CodexBarCore/Providers/ProviderDescriptor.swift b/Sources/CodexBarCore/Providers/ProviderDescriptor.swift index fd38727e18..fb3cf1c5ea 100644 --- a/Sources/CodexBarCore/Providers/ProviderDescriptor.swift +++ b/Sources/CodexBarCore/Providers/ProviderDescriptor.swift @@ -107,6 +107,7 @@ public enum ProviderDescriptorRegistry { .deepgram: DeepgramProviderDescriptor.descriptor, .poe: PoeProviderDescriptor.descriptor, .chutes: ChutesProviderDescriptor.descriptor, + .neuralwatt: NeuralWattProviderDescriptor.descriptor, ] private static let bootstrap: Void = { for provider in UsageProvider.allCases { diff --git a/Sources/CodexBarCore/Providers/ProviderTokenResolver.swift b/Sources/CodexBarCore/Providers/ProviderTokenResolver.swift index 3bf7117fa7..947c0cb620 100644 --- a/Sources/CodexBarCore/Providers/ProviderTokenResolver.swift +++ b/Sources/CodexBarCore/Providers/ProviderTokenResolver.swift @@ -105,6 +105,12 @@ public enum ProviderTokenResolver { self.elevenLabsResolution(environment: environment)?.token } + public static func neuralWattToken( + environment: [String: String] = ProcessInfo.processInfo.environment) -> String? + { + self.neuralWattResolution(environment: environment)?.token + } + public static func groqToken(environment: [String: String] = ProcessInfo.processInfo.environment) -> String? { self.groqResolution(environment: environment)?.token } @@ -346,6 +352,12 @@ public enum ProviderTokenResolver { self.resolveEnv(ElevenLabsSettingsReader.apiKey(environment: environment)) } + public static func neuralWattResolution( + environment: [String: String] = ProcessInfo.processInfo.environment) -> ProviderTokenResolution? + { + self.resolveEnv(NeuralWattSettingsReader.apiKey(environment: environment)) + } + public static func groqResolution( environment: [String: String] = ProcessInfo.processInfo.environment) -> ProviderTokenResolution? { diff --git a/Sources/CodexBarCore/Providers/Providers.swift b/Sources/CodexBarCore/Providers/Providers.swift index 4fc5b4357a..1192f65ff2 100644 --- a/Sources/CodexBarCore/Providers/Providers.swift +++ b/Sources/CodexBarCore/Providers/Providers.swift @@ -57,6 +57,7 @@ public enum UsageProvider: String, CaseIterable, Sendable, Codable { case deepgram case poe case chutes + case neuralwatt } // swiftformat:enable sortDeclarations @@ -114,6 +115,7 @@ public enum IconStyle: String, Sendable, CaseIterable { case deepgram case poe case chutes + case neuralwatt case combined } diff --git a/Sources/CodexBarCore/TokenAccountSupportCatalog+Data.swift b/Sources/CodexBarCore/TokenAccountSupportCatalog+Data.swift index 4db3a50bca..abecfe7452 100644 --- a/Sources/CodexBarCore/TokenAccountSupportCatalog+Data.swift +++ b/Sources/CodexBarCore/TokenAccountSupportCatalog+Data.swift @@ -129,6 +129,13 @@ extension TokenAccountSupportCatalog { injection: .environment(key: ElevenLabsSettingsReader.apiKeyEnvironmentKey), requiresManualCookieSource: false, cookieName: nil), + .neuralwatt: TokenAccountSupport( + title: "API keys", + subtitle: "Store multiple Neuralwatt API keys.", + placeholder: "sk-...", + injection: .environment(key: NeuralWattSettingsReader.apiKeyEnvironmentKey), + requiresManualCookieSource: false, + cookieName: nil), .groq: TokenAccountSupport( title: "API keys", subtitle: "Store multiple Groq API keys.", diff --git a/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift b/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift index e739dad4d7..a2c6a46446 100644 --- a/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift +++ b/Sources/CodexBarCore/Vendored/CostUsage/CostUsageScanner.swift @@ -464,7 +464,7 @@ enum CostUsageScanner { .copilot, .devin, .minimax, .manus, .kilo, .kiro, .kimi, .kimik2, .moonshot, .augment, .jetbrains, .amp, .ollama, .t3chat, .synthetic, .openrouter, .elevenlabs, .warp, .perplexity, .mimo, .doubao, .sakana, .abacus, .mistral, .deepseek, .codebuff, .crof, .windsurf, .zed, .venice, .commandcode, .stepfun, - .bedrock, .grok, .groq, .llmproxy, .litellm, .deepgram, .poe, .chutes: + .bedrock, .grok, .groq, .llmproxy, .litellm, .deepgram, .poe, .chutes, .neuralwatt: return emptyReport } } diff --git a/Sources/CodexBarWidget/CodexBarWidgetProvider.swift b/Sources/CodexBarWidget/CodexBarWidgetProvider.swift index 4e35d422f1..b6c4a68fa6 100644 --- a/Sources/CodexBarWidget/CodexBarWidgetProvider.swift +++ b/Sources/CodexBarWidget/CodexBarWidgetProvider.swift @@ -108,6 +108,7 @@ enum ProviderChoice: String, AppEnum { case .poe: return nil // Poe not yet supported in widgets case .chutes: return nil // Chutes not yet supported in widgets case .zed: return nil // Zed not yet supported in widgets + case .neuralwatt: return nil // Neuralwatt not yet supported in widgets } } } diff --git a/Sources/CodexBarWidget/CodexBarWidgetViews.swift b/Sources/CodexBarWidget/CodexBarWidgetViews.swift index 640e4c0e47..58f54aa08d 100644 --- a/Sources/CodexBarWidget/CodexBarWidgetViews.swift +++ b/Sources/CodexBarWidget/CodexBarWidgetViews.swift @@ -323,6 +323,7 @@ private struct ProviderSwitchChip: View { case .poe: "Poe" case .chutes: "Chutes" case .zed: "Zed" + case .neuralwatt: "Neuralwatt" } } } @@ -878,6 +879,8 @@ enum WidgetColors { Color(red: 24 / 255, green: 160 / 255, blue: 88 / 255) case .zed: Color(red: 64 / 255, green: 156 / 255, blue: 255 / 255) + case .neuralwatt: + Color(red: 56 / 255, green: 217 / 255, blue: 140 / 255) } } } diff --git a/Tests/CodexBarTests/MenuCardNeuralWattTests.swift b/Tests/CodexBarTests/MenuCardNeuralWattTests.swift new file mode 100644 index 0000000000..4458eb1439 --- /dev/null +++ b/Tests/CodexBarTests/MenuCardNeuralWattTests.swift @@ -0,0 +1,55 @@ +import CodexBarCore +import Foundation +import Testing +@testable import CodexBar + +struct MenuCardNeuralWattTests { + @Test + func `model shows credit balance as status text without reset wording`() throws { + let now = Date(timeIntervalSince1970: 1_800_000_000) + let snapshot = NeuralWattUsageSnapshot( + creditsRemainingUSD: 51.00, + totalCreditsUSD: 77.04, + creditsUsedUSD: 26.04, + accountingMethod: "energy", + currentMonthCostUSD: 12.34, + currentMonthEnergyKWh: 0.25, + subscription: nil, + keyAllowance: nil, + rateLimitTier: "standard", + updatedAt: now) + .toUsageSnapshot() + let metadata = try #require(ProviderDefaults.metadata[.neuralwatt]) + + let model = UsageMenuCardView.Model.make(.init( + provider: .neuralwatt, + 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)) + + let primary = try #require(model.metrics.first) + #expect(primary.title == "Credits") + let statusText = primary.statusText?.replacingOccurrences(of: "\u{00A0}", with: "") + #expect(statusText == "$51.00 remaining of $77.04") + #expect(primary.detailText == nil) + #expect(primary.resetText == nil) + #expect(model.metrics.contains { $0.title == "This month" } == false) + #expect(model.metrics.allSatisfy { metric in + metric.resetText?.localizedCaseInsensitiveContains("reset") != true + }) + } +} diff --git a/Tests/CodexBarTests/NeuralWattUsageFetcherTests.swift b/Tests/CodexBarTests/NeuralWattUsageFetcherTests.swift new file mode 100644 index 0000000000..786b1e3c97 --- /dev/null +++ b/Tests/CodexBarTests/NeuralWattUsageFetcherTests.swift @@ -0,0 +1,373 @@ +import Foundation +import Testing +@testable import CodexBarCore + +@Suite(.serialized) +struct NeuralWattUsageFetcherTests { + @Test + func `parses quota response into usage snapshot`() throws { + let body = #""" + { + "snapshot_at": "2026-04-16T18:30:00Z", + "balance": { + "credits_remaining_usd": 32.6774, + "total_credits_usd": 52.34, + "credits_used_usd": 19.6626, + "accounting_method": "energy" + }, + "usage": { + "lifetime": { + "cost_usd": 243.9145, + "requests": 37801, + "tokens": 1235477176, + "energy_kwh": 15.6009 + }, + "current_month": { + "cost_usd": 160.1463, + "requests": 23902, + "tokens": 1116658995, + "energy_kwh": 9.7278 + } + }, + "limits": { + "overage_limit_usd": null, + "rate_limit_tier": "standard" + }, + "subscription": { + "plan": "standard", + "status": "active", + "billing_interval": "month", + "current_period_start": "2026-04-11T05:05:25Z", + "current_period_end": "2026-05-11T05:05:25Z", + "auto_renew": true, + "kwh_included": 20.0, + "kwh_used": 13.9023, + "kwh_remaining": 6.0977, + "in_overage": false + }, + "key": { + "name": "my-production-key", + "allowance": { + "limit_usd": 50.0, + "period": "monthly", + "spent_usd": 12.5, + "remaining_usd": 37.5, + "blocked": false + } + } + } + """# + + let snapshot = try NeuralWattUsageFetcher._parseSnapshotForTesting( + Data(body.utf8), + updatedAt: Date(timeIntervalSince1970: 1)) + let usage = snapshot.toUsageSnapshot() + + #expect(snapshot.totalCreditsUSD == 52.34) + #expect(snapshot.creditsUsedUSD == 19.6626) + let expectedCreditPercent = 19.6626 / 52.34 * 100 + #expect(abs(snapshot.creditUsedPercent - expectedCreditPercent) < 1e-6) + // Credits exhaust (DeepSeek-style); no kWh window surfaced. + #expect(snapshot.keyAllowanceUsedPercent == 25.0) + #expect(snapshot.currentMonthCostUSD == 160.1463) + let primaryPercent = usage.primary?.usedPercent + #expect(primaryPercent.map { abs($0 - expectedCreditPercent) < 1e-6 } == true) + // No reset cycle — credits deplete, they don't renew. + #expect(usage.primary?.resetsAt == nil) + #expect(usage.subscriptionRenewsAt == nil) + // loginMethod now reflects accounting method when present. + #expect(usage.loginMethod(for: .neuralwatt) == "Energy") + // Only key allowance is surfaced as an extra quota window. Current-month spend is telemetry, + // not a resettable rate window. + #expect(usage.extraRateWindows?.count == 1) + #expect(usage.extraRateWindows?.contains { $0.id == "subscription-kwh" } == false) + #expect(usage.extraRateWindows?.contains { $0.id == "current-month-spend" } == false) + let allowanceWindow = usage.extraRateWindows?.first { $0.id == "key-allowance" } + #expect(allowanceWindow?.title == "Key Monthly") + } + + @Test + func `parses response with null subscription using accounting method`() throws { + let body = #""" + { + "snapshot_at": "2026-04-16T18:30:00Z", + "balance": { + "credits_remaining_usd": 4.5, + "total_credits_usd": 5.0, + "credits_used_usd": 0.5, + "accounting_method": "energy" + }, + "usage": { + "lifetime": {"cost_usd": 0.5, "requests": 10, "tokens": 1000, "energy_kwh": 0.01}, + "current_month": {"cost_usd": 0.5, "requests": 10, "tokens": 1000, "energy_kwh": 0.01} + }, + "limits": {"overage_limit_usd": null, "rate_limit_tier": "free"}, + "subscription": null, + "key": {"name": "trial", "allowance": null} + } + """# + + let snapshot = try NeuralWattUsageFetcher._parseSnapshotForTesting( + Data(body.utf8), + updatedAt: Date(timeIntervalSince1970: 100)) + let usage = snapshot.toUsageSnapshot() + + #expect(snapshot.creditUsedPercent == 10) + #expect(snapshot.subscription == nil) + #expect(snapshot.keyAllowanceUsedPercent == nil) + #expect(usage.primary?.usedPercent == 10) + #expect(usage.subscriptionRenewsAt == nil) + #expect(usage.loginMethod(for: .neuralwatt) == "Energy") + // No resettable extra quota windows when there is no per-key allowance. + #expect(usage.extraRateWindows == nil) + } + + @Test + func `parses response with missing credits used derived from remaining`() throws { + let body = #""" + { + "balance": { + "credits_remaining_usd": 30.0, + "total_credits_usd": 100.0, + "accounting_method": "energy" + }, + "usage": {"lifetime": {}, "current_month": {}}, + "limits": {}, + "subscription": null, + "key": {"name": "x", "allowance": null} + } + """# + + let snapshot = try NeuralWattUsageFetcher._parseSnapshotForTesting( + Data(body.utf8), + updatedAt: Date(timeIntervalSince1970: 2)) + // credits_used_usd missing but derived as 100 - 30 = 70. + #expect(snapshot.effectiveUsedCredits == 70) + #expect(snapshot.creditUsedPercent == 70) + } + + @Test + func `marks known zero credit balance as exhausted`() throws { + let body = #""" + { + "balance": { + "credits_remaining_usd": 0.0, + "total_credits_usd": 0.0, + "accounting_method": "energy" + }, + "usage": {"lifetime": {}, "current_month": {}}, + "limits": {}, + "subscription": null, + "key": {"name": "x", "allowance": null} + } + """# + + let snapshot = try NeuralWattUsageFetcher._parseSnapshotForTesting( + Data(body.utf8), + updatedAt: Date(timeIntervalSince1970: 2)) + let usage = snapshot.toUsageSnapshot() + + #expect(snapshot.effectiveRemainingCredits == 0) + #expect(snapshot.effectiveTotalCredits == nil) + #expect(snapshot.creditUsedPercent == 100) + #expect(usage.primary?.usedPercent == 100) + let resetDescription = usage.primary?.resetDescription?.replacingOccurrences(of: "\u{00A0}", with: "") + #expect(resetDescription == "$0.00 remaining") + } + + @Test + func `parses fractional subscription dates`() throws { + let body = #""" + { + "balance": { + "credits_remaining_usd": 8.0, + "total_credits_usd": 10.0, + "credits_used_usd": 2.0, + "accounting_method": "energy" + }, + "usage": {"lifetime": {}, "current_month": {}}, + "limits": {}, + "subscription": { + "plan": "standard", + "status": "active", + "current_period_start": "2026-04-11T05:05:25.123Z", + "current_period_end": "2026-05-11T05:05:25.456Z" + }, + "key": {"name": "x", "allowance": null} + } + """# + + let snapshot = try NeuralWattUsageFetcher._parseSnapshotForTesting( + Data(body.utf8), + updatedAt: Date(timeIntervalSince1970: 3)) + + #expect(snapshot.subscription?.currentPeriodEnd != nil) + #expect(snapshot.creditUsedPercent == 20) + } + + @Test + func `rejects malformed successful response without balance`() throws { + let body = #"{"error":"temporarily unavailable"}"# + + do { + _ = try NeuralWattUsageFetcher._parseSnapshotForTesting( + Data(body.utf8), + updatedAt: Date(timeIntervalSince1970: 4)) + Issue.record("Expected NeuralWattUsageError.parseFailed") + } catch let error as NeuralWattUsageError { + guard case let .parseFailed(message) = error else { + Issue.record("Expected parseFailed, got \(error)") + return + } + #expect(message.contains("balance")) + } + } + + @Test + func `fetch usage rejects blank API key before request`() async throws { + do { + _ = try await NeuralWattUsageFetcher.fetchUsage( + apiKey: " ", + environment: [NeuralWattSettingsReader.apiURLEnvironmentKey: "https://api.neuralwatt.test"]) + Issue.record("Expected NeuralWattUsageError.missingCredentials") + } catch let error as NeuralWattUsageError { + guard case .missingCredentials = error else { + Issue.record("Expected missingCredentials, got \(error)") + return + } + } + } + + @Test + func `unauthorized fetch throws missing credentials`() async throws { + let registered = URLProtocol.registerClass(NeuralWattStubURLProtocol.self) + defer { + if registered { + URLProtocol.unregisterClass(NeuralWattStubURLProtocol.self) + } + NeuralWattStubURLProtocol.handler = nil + } + + NeuralWattStubURLProtocol.handler = { request in + guard let url = request.url else { throw URLError(.badURL) } + return Self.makeResponse(url: url, body: #"{"detail":"bad key"}"#, statusCode: 401) + } + + do { + _ = try await NeuralWattUsageFetcher.fetchUsage( + apiKey: "sk-test", + environment: [NeuralWattSettingsReader.apiURLEnvironmentKey: "https://api.neuralwatt.test"]) + Issue.record("Expected NeuralWattUsageError.missingCredentials") + } catch let error as NeuralWattUsageError { + guard case .missingCredentials = error else { + Issue.record("Expected missingCredentials, got \(error)") + return + } + } + } + + @Test + func `fetch usage sends bearer authorization header`() async throws { + let registered = URLProtocol.registerClass(NeuralWattStubURLProtocol.self) + defer { + if registered { + URLProtocol.unregisterClass(NeuralWattStubURLProtocol.self) + } + NeuralWattStubURLProtocol.handler = nil + } + + NeuralWattStubURLProtocol.handler = { request in + guard let url = request.url else { throw URLError(.badURL) } + #expect(url.path == "/v1/quota") + #expect(request.value(forHTTPHeaderField: "Authorization") == "Bearer sk-test") + #expect(request.timeoutInterval == 15) + + let body = #""" + { + "balance": {"credits_remaining_usd": 5.0, "total_credits_usd": 10.0, + "credits_used_usd": 5.0, "accounting_method": "energy"}, + "usage": {"lifetime": {}, "current_month": {}}, + "limits": {}, "subscription": null, "key": {"name": "k", "allowance": null} + } + """# + return Self.makeResponse(url: url, body: body, statusCode: 200) + } + + let usage = try await NeuralWattUsageFetcher.fetchUsage( + apiKey: " sk-test ", + environment: [NeuralWattSettingsReader.apiURLEnvironmentKey: "https://api.neuralwatt.test"]) + + #expect(usage.creditUsedPercent == 50) + } + + @Test + func `non success fetch throws generic HTTP error`() async throws { + let registered = URLProtocol.registerClass(NeuralWattStubURLProtocol.self) + defer { + if registered { + URLProtocol.unregisterClass(NeuralWattStubURLProtocol.self) + } + NeuralWattStubURLProtocol.handler = nil + } + + NeuralWattStubURLProtocol.handler = { request in + guard let url = request.url else { throw URLError(.badURL) } + return Self.makeResponse(url: url, body: #"{"detail":"bad key"}"#, statusCode: 500) + } + + do { + _ = try await NeuralWattUsageFetcher.fetchUsage( + apiKey: "sk-test", + environment: [NeuralWattSettingsReader.apiURLEnvironmentKey: "https://api.neuralwatt.test"]) + Issue.record("Expected NeuralWattUsageError.apiError") + } catch let error as NeuralWattUsageError { + guard case let .apiError(message) = error else { + Issue.record("Expected apiError, got \(error)") + return + } + #expect(message == "HTTP 500") + } + } + + private static func makeResponse( + url: URL, + body: String, + statusCode: Int = 200) -> (HTTPURLResponse, Data) + { + let response = HTTPURLResponse( + url: url, + statusCode: statusCode, + httpVersion: "HTTP/1.1", + headerFields: ["Content-Type": "application/json"])! + return (response, Data(body.utf8)) + } +} + +final class NeuralWattStubURLProtocol: URLProtocol { + nonisolated(unsafe) static var handler: ((URLRequest) throws -> (HTTPURLResponse, Data))? + + override static func canInit(with request: URLRequest) -> Bool { + request.url?.host == "api.neuralwatt.test" + } + + override static func canonicalRequest(for request: URLRequest) -> URLRequest { + request + } + + override func startLoading() { + guard let handler = Self.handler else { + self.client?.urlProtocol(self, didFailWithError: URLError(.badServerResponse)) + return + } + do { + let (response, data) = try handler(self.request) + self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) + self.client?.urlProtocol(self, didLoad: data) + self.client?.urlProtocolDidFinishLoading(self) + } catch { + self.client?.urlProtocol(self, didFailWithError: error) + } + } + + override func stopLoading() {} +} diff --git a/Tests/CodexBarTests/ProviderConfigEnvironmentTests.swift b/Tests/CodexBarTests/ProviderConfigEnvironmentTests.swift index 84a139c5c9..73e9d8748d 100644 --- a/Tests/CodexBarTests/ProviderConfigEnvironmentTests.swift +++ b/Tests/CodexBarTests/ProviderConfigEnvironmentTests.swift @@ -123,6 +123,19 @@ struct ProviderConfigEnvironmentTests { #expect(ProviderTokenResolver.elevenLabsToken(environment: env) == "xi-token") } + @Test + func `applies API key override for NeuralWatt`() { + let config = ProviderConfig(id: .neuralwatt, apiKey: "sk-neuralwatt-config") + let env = ProviderConfigEnvironment.applyAPIKeyOverride( + base: [:], + provider: .neuralwatt, + config: config) + + #expect(env[NeuralWattSettingsReader.apiKeyEnvironmentKey] == "sk-neuralwatt-config") + #expect(ProviderTokenResolver.neuralWattToken(environment: env) == "sk-neuralwatt-config") + #expect(ProviderConfigEnvironment.supportsAPIKeyOverride(for: .neuralwatt)) + } + @Test func `applies API key override for groq`() { let config = ProviderConfig(id: .groq, apiKey: "gsk-token") diff --git a/Tests/CodexBarTests/ProviderEnvironmentResolverTests.swift b/Tests/CodexBarTests/ProviderEnvironmentResolverTests.swift index 52bb2fd5cd..604257aea4 100644 --- a/Tests/CodexBarTests/ProviderEnvironmentResolverTests.swift +++ b/Tests/CodexBarTests/ProviderEnvironmentResolverTests.swift @@ -15,6 +15,18 @@ struct ProviderEnvironmentResolverTests { #expect(environment[ZaiSettingsReader.apiTokenKey] == "account-token") } + @Test + func `NeuralWatt selected API account overrides saved and ambient credentials`() { + let account = Self.account(token: "sk-neuralwatt-account") + let environment = ProviderEnvironmentResolver.resolve( + base: [NeuralWattSettingsReader.apiKeyEnvironmentKey: "ambient-token"], + provider: .neuralwatt, + config: ProviderConfig(id: .neuralwatt, apiKey: "saved-token"), + selectedAccount: account) + + #expect(environment[NeuralWattSettingsReader.apiKeyEnvironmentKey] == "sk-neuralwatt-account") + } + @Test func `OpenAI account removes project scoping from saved config`() { let account = Self.account(token: "sk-admin-account") diff --git a/docs/configuration.md b/docs/configuration.md index d787412a1f..36ac12c1a9 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -12,6 +12,7 @@ CodexBar reads a single JSON config file for CLI and app provider settings. API keys, manual cookie headers, source selection, ordering, and token accounts live here. Keychain is still used for runtime cookie caches, browser Safe Storage access, and provider OAuth/device-flow credentials where those flows require it. ## Location + - `CODEXBAR_CONFIG=/path/to/config.json` when set. - `$XDG_CONFIG_HOME/codexbar/config.json` when `XDG_CONFIG_HOME` is set to an absolute path. Relative values are ignored. @@ -21,6 +22,7 @@ API keys, manual cookie headers, source selection, ordering, and token accounts - Permissions are set to `0600` whenever CodexBar writes the file on macOS and Linux. ## Root shape + ```json { "version": 1, @@ -42,6 +44,7 @@ API keys, manual cookie headers, source selection, ordering, and token accounts ``` ## Provider fields + All provider fields are optional unless noted. - `id` (required): provider identifier. @@ -61,6 +64,7 @@ All provider fields are optional unless noted. - `tokenAccounts`: multi-account tokens for providers in `TokenAccountSupportCatalog`. ## Manual cookies + Use manual cookies when automatic browser import is unavailable, disabled, or too noisy for your setup. The app and CLI both read the same resolved config file, so a manual cookie saved in the UI is also used by `codexbar`, and a cookie written by tooling is shown in the app after reload. @@ -70,7 +74,7 @@ export. In browser DevTools, open the Network tab, select a request for the prov header named `Cookie`. You can paste either the full `Cookie: name=value; other=value` string or just `name=value; other=value`. -If you have a Netscape export, convert each non-comment row to `name=value` and join values with `; `. Do not paste +If you have a Netscape export, convert each non-comment row to `name=value` and join values with `;`. Do not paste the raw `# Netscape HTTP Cookie File` text into `cookieHeader`. Example placeholder config: @@ -149,6 +153,7 @@ Manual cookies are secrets. Keep the CodexBar config file private, leave its per and never paste real cookie values or readable DevTools screenshots into public issues. ### tokenAccounts + ```json { "version": 1, @@ -168,13 +173,16 @@ and never paste real cookie values or readable DevTools screenshots into public z.ai team accounts also use `usageScope`, `organizationId`, and `workspaceID`; see [z.ai](zai.md). ## Provider IDs + Current IDs (see `Sources/CodexBarCore/Providers/Providers.swift`): -`codex`, `openai`, `azureopenai`, `claude`, `cursor`, `opencode`, `opencodego`, `alibaba`, `alibabatokenplan`, `factory`, `gemini`, `antigravity`, `copilot`, `devin`, `zai`, `minimax`, `manus`, `kimi`, `kilo`, `kiro`, `vertexai`, `augment`, `jetbrains`, `kimik2`, `moonshot`, `amp`, `t3chat`, `ollama`, `synthetic`, `warp`, `openrouter`, `elevenlabs`, `windsurf`, `zed`, `perplexity`, `mimo`, `doubao`, `sakana`, `abacus`, `mistral`, `deepseek`, `codebuff`, `crof`, `venice`, `commandcode`, `stepfun`, `bedrock`, `grok`, `groq`, `llmproxy`, `litellm`, `deepgram`, `poe`, `chutes`. +`codex`, `openai`, `azureopenai`, `claude`, `cursor`, `opencode`, `opencodego`, `alibaba`, `alibabatokenplan`, `factory`, `gemini`, `antigravity`, `copilot`, `devin`, `zai`, `minimax`, `manus`, `kimi`, `kilo`, `kiro`, `vertexai`, `augment`, `jetbrains`, `kimik2`, `moonshot`, `amp`, `t3chat`, `ollama`, `synthetic`, `warp`, `openrouter`, `elevenlabs`, `windsurf`, `zed`, `perplexity`, `mimo`, `doubao`, `sakana`, `abacus`, `mistral`, `deepseek`, `codebuff`, `crof`, `venice`, `commandcode`, `stepfun`, `bedrock`, `grok`, `groq`, `llmproxy`, `litellm`, `deepgram`, `poe`, `chutes`, `neuralwatt`. ## Ordering + The order of `providers` controls display/order in the app and CLI. Reorder the array to change ordering. ## Notes + - Fields not relevant to a provider are ignored. - Omitted providers are appended with defaults during normalization. - Keep the file private; it contains secrets. diff --git a/docs/neuralwatt.md b/docs/neuralwatt.md new file mode 100644 index 0000000000..a811fce323 --- /dev/null +++ b/docs/neuralwatt.md @@ -0,0 +1,79 @@ +--- +summary: "Neuralwatt provider notes: API key setup and quota usage fields." +read_when: + - Adding or modifying the Neuralwatt provider + - Debugging Neuralwatt API keys or quota parsing + - Adjusting Neuralwatt credit labels +--- + +# Neuralwatt Provider + +The Neuralwatt provider reads account quota from the Neuralwatt Cloud API using an API key. +Neuralwatt Cloud is an OpenAI-compatible inference API with energy-based pricing. Credits are a +Deplete-as-you-go USD balance (like DeepSeek): they do **not** reset on a billing cycle, they +simply exhaust as the API is used and are refilled by topping up. The quota endpoint exposes +the USD credit balance, current-month spend fields, and optional per-key spending allowances. + +## Features + +- USD credit balance as the primary usage window (`credits_remaining_usd` / `total_credits_usd`). + The bar fills as credits are consumed; there is **no reset date** since credits deplete, not renew. +- Per-key spending allowance (`spent_usd` / `limit_usd`) as an extra rate window when configured. +- Accounting method (`Token` vs `Energy`) shown as the provider identity label. +- Current calendar-month spend is parsed for future/reporting use, but is not shown as a resettable quota window. + +## Setup + +### CLI + +Store the API key without opening Settings: + +```bash +printf '%s' "$NEURALWATT_API_KEY" | codexbar config set-api-key --provider neuralwatt --stdin +``` + +This trims the piped key, writes it to CodexBar's config file (`~/.config/codexbar/config.json` +by default, or the legacy `~/.codexbar/config.json` when already present), and enables Neuralwatt by +default. Use `--no-enable` to save the key without enabling the provider. + +### Settings + +1. Open **Settings → Providers** +2. Enable **Neuralwatt** +3. Open `https://portal.neuralwatt.com/dashboard/api-keys` +4. Create or copy an API key +5. Paste the key into CodexBar's Neuralwatt provider settings + +### Environment Variables + +CodexBar also accepts these environment variables: + +- `NEURALWATT_API_KEY` + +For tests or self-hosted/proxy setups, override the API base URL with `NEURALWATT_API_URL`. + +## How It Works + +- Endpoint: `GET https://api.neuralwatt.com/v1/quota` +- Auth header: `Authorization: Bearer sk-...` +- Fields used: `balance.credits_remaining_usd`, `balance.total_credits_usd`, + `balance.credits_used_usd`, `balance.accounting_method`, + `usage.current_month.cost_usd`, + `key.allowance.limit_usd`, `key.allowance.spent_usd`, `key.allowance.period` +- `credits_used_usd` is derived as `total_credits_usd − credits_remaining_usd` when the API omits it. +- `subscription` may be `null`; subscription periods are not rendered as resets because Neuralwatt + credits deplete until topped up. + +## Troubleshooting + +### "Missing Neuralwatt API key" + +Set the key with `codexbar config set-api-key --provider neuralwatt --stdin`, add it in +**Settings → Providers → Neuralwatt**, set `NEURALWATT_API_KEY`, or configure a Neuralwatt token +account in CodexBar. + +### "Neuralwatt API error" + +Confirm the API key is valid and that the current network can reach `api.neuralwatt.com`. The +quota endpoint is rate-limited to 1 request per second per customer; CodexBar refreshes on its +normal cycle so this should not be hit in practice. diff --git a/docs/providers.md b/docs/providers.md index 2e6788fc13..acd7f65596 100644 --- a/docs/providers.md +++ b/docs/providers.md @@ -12,6 +12,7 @@ CodexBar currently registers 54 provider IDs. Some companies expose multiple sur OpenCode vs OpenCode Go, because the auth source and quota shape differ. ## Fetch strategies (current) + Legend: web (browser cookies/WebView), cli (RPC/PTy or provider CLI), oauth (provider OAuth), api token, local probe, web dashboard. Source labels (CLI/header): `openai-web`, `web`, `oauth`, `api`, `local`, `cli`, plus provider-specific CLI labels (e.g. `codex-cli`, `claude`). @@ -73,9 +74,11 @@ headers, source selection, provider ordering, and token accounts are stored in ` | LiteLLM | API key + base URL → `/key/info`, then `/user/info` or `/team/info` budget usage (`api`). | | Deepgram | API key → project discovery and usage breakdown API (`api`). | | Chutes | API key from config/env → subscription usage and quota API (`api`). | +| Neuralwatt | API key from config/env → `/v1/quota` credit balance and spend (`api`). | | Zed | Zed editor Keychain session → `cloud.zed.dev/client/users/me` for plan and quota data (`local`). | ## Codex + - App Auto: OAuth API first; falls back to CLI only when OAuth credentials are missing or auth/refresh is invalid. - Web dashboard (optional, off by default): `https://chatgpt.com/codex/settings/usage` via WebView + browser cookies. - Battery saver toggle (currently off by default): reduces routine OpenAI web refreshes but still allows explicit manual refreshes. @@ -86,12 +89,14 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/codex.md`. ## OpenAI + - API key from `~/.codexbar/config.json`, `OPENAI_ADMIN_KEY`, or `OPENAI_API_KEY`. - Admin API keys are preferred and fetch organization costs plus completion usage for inline Today/7d/configured-window dashboards. - Normal API keys fall back to the legacy credit-grants balance endpoint when organization usage is unavailable. - Details: `docs/openai.md`. ## Azure OpenAI + - API key, endpoint, and deployment from `~/.codexbar/config.json` or `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_ENDPOINT`, and `AZURE_OPENAI_DEPLOYMENT_NAME`. - `AZURE_OPENAI_ENDPOINT` and configured endpoint overrides must be HTTPS URLs or bare hosts normalized to HTTPS; explicit `http://` URLs, user info, and encoded host-delimiter tricks fail closed before `api-key` headers are attached. - Validates the configured deployment with a minimal chat-completions request; it does not expose Azure spend or quota history. @@ -99,6 +104,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Status: Azure status page link. ## Claude + - Admin API: `sk-ant-admin...` key in Settings/config, token accounts, or `ANTHROPIC_ADMIN_KEY`. - Admin API shows organization spend/messages summaries with the same inline dashboard pattern as OpenAI API. - App Auto: OAuth API (`oauth`) → CLI PTY (`claude`) → Web API (`web`). @@ -108,6 +114,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/claude.md`. ## z.ai + - API token from `~/.codexbar/config.json` (`providers[].apiKey`) or `Z_AI_API_KEY` env var. - Supports global and BigModel CN quota hosts; override with `Z_AI_API_HOST` or `Z_AI_QUOTA_URL`. - z.ai endpoint overrides must be HTTPS or bare hosts normalized to HTTPS. `Z_AI_QUOTA_URL` takes precedence for @@ -116,6 +123,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/zai.md`. ## Devin + - Automatic auth reads the current `auth1_session` token and organization metadata from Chrome localStorage. - Manual auth accepts the `Authorization: Bearer ...` value from an app.devin.ai request. - Usage endpoint: `GET /api//billing/quota/usage`. @@ -123,12 +131,14 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/devin.md`. ## Manus + - Session token via browser `session_id` cookie, manual Settings entry, `MANUS_SESSION_TOKEN`, or `MANUS_COOKIE`. - Credits endpoint: `POST https://api.manus.im/user.v1.UserService/GetAvailableCredits`. - Auto mode prefers cached/browser cookies before env fallback; manual mode accepts either a bare `session_id` value or a full Cookie header. - Status: none yet. ## MiniMax + - Coding Plan API token or web session from configured/manual/browser sources. - Supports global and China mainland hosts via provider region settings and environment overrides. - Web-session billing history can render 30-day token charts plus top model/method breakdowns when MiniMax exposes it. @@ -136,6 +146,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/minimax.md`. ## Kimi + - Kimi Code API key via `~/.codexbar/config.json` or `KIMI_CODE_API_KEY`. - Web fallback uses the JWT from `kimi-auth` cookie via manual entry or `KIMI_AUTH_TOKEN` env var. - Shows weekly quota and 5-hour rate limit (300 minutes). @@ -143,6 +154,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/kimi.md`. ## Kilo + - API token from `~/.codexbar/config.json` (`providers[].apiKey`) or `KILO_API_KEY`. - Auto mode tries API first and falls back to CLI auth when API credentials are missing or unauthorized. - CLI auth source: `~/.local/share/kilo/auth.json` (`kilo.access`), typically created by `kilo login`. @@ -150,6 +162,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/kilo.md`. ## Kimi K2 (unofficial) + - API key via `~/.codexbar/config.json` or `KIMI_K2_API_KEY`/`KIMI_API_KEY` env var. - Shows credit usage from the legacy `kimi-k2.ai` consumed/remaining totals. - Use Moonshot / Kimi API for the official Kimi API account and billing surface. @@ -157,6 +170,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/kimi-k2.md`. ## Gemini + - OAuth-backed quota API (`retrieveUserQuota`) using Gemini CLI credentials. - Token refresh via Google OAuth if expired. - Tier detection via `loadCodeAssist`. @@ -164,23 +178,27 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/gemini.md`. ## Antigravity + - Local Antigravity language server (internal protocol, HTTPS on localhost). - `GetUserStatus` primary; `GetCommandModelConfigs` fallback. - Status: Google Workspace incidents (Gemini product). - Details: `docs/antigravity.md`. ## Cursor + - Web API via browser cookies (`cursor.com` + `cursor.sh`). - Fallback: stored WebKit session. - Status: Statuspage.io (Cursor). - Details: `docs/cursor.md`. ## OpenCode + - Web dashboard via browser cookies (`opencode.ai`). - Status: none yet. - Details: `docs/opencode.md`. ## OpenCode Go + - Web dashboard via browser or manual cookies (`opencode.ai`). - Auto mode falls back to local usage from `~/.local/share/opencode/opencode.db` on macOS and Linux. - Uses the workspace Go page/server data for rolling 5-hour, weekly, and optional monthly usage windows. @@ -189,6 +207,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/opencode.md`. ## Alibaba Coding Plan + - Web mode uses Alibaba console RPC with form payload + `sec_token`. - Cookie sources: browser import (`auto`) or manual header (`cookieSource: manual`). - API key fallback from Settings (`providers[].apiKey`) or `ALIBABA_CODING_PLAN_API_KEY` env var. @@ -198,6 +217,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/alibaba-coding-plan.md`. ## Alibaba Token Plan + - Web mode posts to the Bailian `GetSubscriptionSummary` endpoint with form-encoded params and optional `sec_token`. - Cookie sources: browser import (`auto`), manual Cookie header, or `ALIBABA_TOKEN_PLAN_COOKIE`. - Default quota URL: `https://bailian.console.aliyun.com/data/api.json?action=GetSubscriptionSummary&product=BssOpenAPI-V3`. @@ -206,18 +226,21 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/alibaba-token-plan.md`. ## Droid (Factory) + - Web API via Factory cookies, bearer tokens, and WorkOS refresh tokens. - Multiple fallback strategies (cookies → stored tokens → local storage → WorkOS cookies). - Status: `https://status.factory.ai`. - Details: `docs/factory.md`. ## Copilot + - GitHub device flow OAuth token + `api.github.com/copilot_internal/user`. - Supports multiple token accounts and account switching from provider settings/menu surfaces. - Status: Statuspage.io (GitHub). - Details: `docs/copilot.md`. ## Kiro + - CLI-based: runs `kiro-cli chat --no-interactive "/usage"` with 10s timeout. - Parses ANSI output for plan name, monthly credits percentage, and bonus credits. - Requires `kiro-cli` installed and logged in via AWS Builder ID. @@ -225,12 +248,14 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/kiro.md`. ## Warp + - API token from Settings or `WARP_API_KEY` / `WARP_TOKEN` env var. - Shows monthly credits usage and next refresh time. - Status: none yet. - Details: `docs/warp.md`. ## ElevenLabs + - API key from Settings, token accounts, `ELEVENLABS_API_KEY`, or `XI_API_KEY`. - Reads `GET /v1/user/subscription` from `api.elevenlabs.io`. - Shows character credit usage, reset timing, and voice slot usage when available. @@ -239,6 +264,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/elevenlabs.md`. ## Vertex AI + - OAuth credentials from `gcloud auth application-default login` (ADC). - Quota usage via Cloud Monitoring `consumer_quota` metrics for `aiplatform.googleapis.com`. - Token cost: uses the Claude local-log scanner filtered to Vertex AI-tagged entries. @@ -246,6 +272,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/vertexai.md`. ## JetBrains AI + - Local XML quota file from IDE configuration directory. - Auto-detects installed JetBrains IDEs; uses most recently used. - Reads `AIAssistantQuotaManager2.xml` for monthly credits and refill date. @@ -253,12 +280,14 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/jetbrains.md`. ## Zed + - Reads the signed-in Zed editor session from the macOS Keychain (`credentials_url` / `https://zed.dev`). - Calls `GET https://cloud.zed.dev/client/users/me` for plan, billing cycle, Edit Predictions quota, and overdue invoice flag. - Sign in to the Zed editor first. - Details: `docs/zed.md`. ## Augment + - Auto mode tries the `auggie` CLI first. - Web fallback uses browser cookies, with manual cookie header support. - Tracks credit usage and account/subscription data where available. @@ -266,6 +295,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/augment.md`. ## Amp + - Auto mode tries the local `amp usage` command first. - API mode calls Amp's balance endpoint with an access token. - Web fallback reads the legacy settings page with browser cookies. @@ -274,6 +304,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/amp.md`. ## T3 Chat + - Web tRPC endpoint (`https://t3.chat/api/trpc/getCustomerData`) via browser cookies. - Parses JSONL response lines and extracts customer data from the embedded tRPC payload. - Shows the 4-hour Base bucket and monthly Overage bucket documented in the T3 Chat FAQ. @@ -281,17 +312,20 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/t3chat.md`. ## Ollama + - Web settings page (`https://ollama.com/settings`) via browser cookies. - Parses Cloud Usage plan badge, session/weekly usage, and reset timestamps. - Status: none yet. - Details: `docs/ollama.md`. ## Synthetic + - API key from `~/.codexbar/config.json` (`providers[].apiKey`) or `SYNTHETIC_API_KEY`. - Shows rolling five-hour, weekly token, search-hourly, and cost/credit quota lanes when present. - Status: none yet. ## OpenRouter + - API token from `~/.codexbar/config.json` (`providers[].apiKey`) or `OPENROUTER_API_KEY` env var. - Reads credits and key rate-limit info from OpenRouter APIs. - Shows daily, weekly, and monthly API-key spend when `/api/v1/key` returns those fields. @@ -300,6 +334,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/openrouter.md`. ## Perplexity + - Browser session cookie from automatic import, manual header/token, or `PERPLEXITY_SESSION_TOKEN` / `PERPLEXITY_COOKIE`. - Tracks recurring credits, bonus/promotional credits, purchased credits, and renewal date when present. - Status: `https://status.perplexity.com/` (link only, no auto-polling). @@ -313,6 +348,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/mimo.md`. ## Doubao + - API key via `ARK_API_KEY`, `VOLCENGINE_API_KEY`, `DOUBAO_API_KEY`, or provider config. - Probes Volcengine Ark chat completions and reads request rate-limit headers when present. - Status: none yet. @@ -325,6 +361,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/sakana.md`. ## Abacus AI + - Browser cookies (`abacus.ai`, `apps.abacus.ai`) via automatic import or manual header. - Reads organization compute points and billing data. - Shows monthly credit gauge with pace tick and reserve/deficit estimate. @@ -332,6 +369,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/abacus.md`. ## Mistral + - Session cookie (`ory_session_*`) from browser auto-import or manual `Cookie:` header. - CSRF token (`csrftoken` cookie) sent as `X-CSRFTOKEN` for billing and Vibe usage requests. - Domains: `admin.mistral.ai` for API billing and `console.mistral.ai` for optional Vibe subscription usage. Console requests forward only `csrftoken` and `ory_session_*`; all other admin cookies stay origin-bound. @@ -343,12 +381,14 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Status: `https://status.mistral.ai` (link only, no auto-polling). ## DeepSeek + - API key via `DEEPSEEK_API_KEY` / `DEEPSEEK_KEY` env var or DeepSeek token accounts. - Shows total balance with paid vs. granted breakdown; USD preferred when multiple currencies present. - Status: `https://status.deepseek.com` (link only, no auto-polling). - Details: `docs/deepseek.md`. ## Moonshot / Kimi API + - API key via `MOONSHOT_API_KEY` / `MOONSHOT_KEY` env var or provider config. - Reads `GET /v1/users/me/balance` from the selected Moonshot region. - Region: international (`api.moonshot.ai`) or China mainland (`api.moonshot.cn`), configurable in Settings or `MOONSHOT_REGION`. @@ -357,12 +397,14 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/moonshot.md`. ## Venice + - API key via `VENICE_API_KEY` / `VENICE_KEY` env var or Venice token accounts. - Shows current DIEM or USD balance; DIEM epoch allocation progress when available. - Status: none yet. - Details: `docs/venice.md`. ## Codebuff + - API token from `~/.codexbar/config.json`, `CODEBUFF_API_KEY`, or `~/.config/manicode/credentials.json` created by `codebuff login`. - Reads usage and subscription data from Codebuff APIs. - Shows credit balance, weekly rate limit, reset timing, subscription status, and auto-top-up flag when present. @@ -371,6 +413,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/codebuff.md`. ## Crof + - API key from `~/.codexbar/config.json`, `CROF_API_KEY`, or `CROFAI_API_KEY`. - Reads `credits`, `requests_plan`, and `usable_requests` from `GET https://crof.ai/usage_api/`. - Shows request quota as the primary usage window and dollar credits as the secondary row. @@ -379,6 +422,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/crof.md`. ## Command Code + - Browser session cookies from automatic import or manual `Cookie:` header. - Linux CLI supports configured manual cookies; automatic browser import remains macOS-only. - Reads monthly USD credits and billing-cycle usage from `api.commandcode.ai`. @@ -387,6 +431,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/command-code.md`. ## Grok + - `grok agent stdio` (ACP) JSON-RPC `x.ai/billing` method; requires `grok login` (SuperGrok OAuth/OIDC). - Reads cached credentials from `~/.grok/auth.json` for identity (email, team). - Falls back to grok.com's billing gRPC-web endpoint via Chrome session cookies when the CLI does not expose billing. @@ -396,6 +441,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/grok.md`. ## AWS Bedrock + - AWS credentials from `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and optional `AWS_SESSION_TOKEN`. - Region from `AWS_REGION` / `AWS_DEFAULT_REGION`, defaulting to `us-east-1`. - Reads AWS Cost Explorer for Bedrock spend and can compare usage against `CODEXBAR_BEDROCK_BUDGET`. @@ -404,6 +450,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/bedrock.md`. ## Deepgram + - API key from config or `DEEPGRAM_API_KEY`. - Optional project ID from provider settings or `DEEPGRAM_PROJECT_ID`; otherwise aggregates all visible projects. - Optional API base URL override via `DEEPGRAM_API_URL`; overrides must be HTTPS or bare hosts normalized to HTTPS. @@ -411,6 +458,7 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/deepgram.md`. ## LiteLLM + - API key from config or `LITELLM_API_KEY`; base URL from config `enterpriseHost` or `LITELLM_BASE_URL`. - Reads `/key/info` first, then `/user/info?user_id=...` for user-bound keys or `/team/info?team_id=...` for team-only keys. - User-bound keys show personal budget usage as the primary window and the key's exact matching team as the secondary window. @@ -420,18 +468,30 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/litellm.md`. ## Poe + - API key from config or `POE_API_KEY`. - Reads the current point balance and recent points history from Poe's official usage API. - History failures are non-fatal; the current balance remains available. - Details: `docs/poe.md`. ## Chutes + - API key from config or `CHUTES_API_KEY`. - Reads subscription usage first, then fills missing rolling, monthly, or pay-as-you-go quota data from the quota APIs. - Uses Chutes' management API at `https://api.chutes.ai`; `CHUTES_API_URL` can override it with an HTTPS endpoint. - Details: `docs/chutes.md`. +## Neuralwatt + +- API key from config or `NEURALWATT_API_KEY`. +- Reads `GET /v1/quota` from `api.neuralwatt.com`; `NEURALWATT_API_URL` can override it with an HTTPS endpoint. +- Credit-exhaustion model (like DeepSeek): USD credits deplete as the API is used and do not reset; the primary window fills as credits are consumed. +- Extra windows: per-key spending allowance when configured. Current-month spend is parsed, but not rendered as a resettable quota window. +- Status: link only (`https://portal.neuralwatt.com/status`), no auto-polling. +- Details: `docs/neuralwatt.md`. + ## StepFun + - Username/password login or manual Oasis-Token. - Reads Step Plan 5-hour and weekly rate-limit windows from `platform.stepfun.com`. - Shows subscription plan name when the Step Plan status API returns one. From f992734e69e27cb79030e7a6b58eb7410c05eff3 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 1 Jul 2026 08:17:39 +0100 Subject: [PATCH 2/4] fix: harden Neuralwatt provider --- README.md | 30 +--------- .../NeuralWattProviderImplementation.swift | 2 +- .../Generated/CodexParserHash.generated.swift | 2 +- .../NeuralWattProviderDescriptor.swift | 4 +- .../NeuralWatt/NeuralWattUsageFetcher.swift | 9 ++- .../NeuralWattUsageFetcherTests.swift | 36 ++++++++++++ docs/configuration.md | 10 +--- docs/neuralwatt.md | 14 ++--- docs/providers.md | 58 +------------------ 9 files changed, 60 insertions(+), 105 deletions(-) diff --git a/README.md b/README.md index 605fef4467..f384b09741 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# CodexBar 🎚️ — May your tokens never run out +# CodexBar 🎚️ — May your tokens never run out. > Every AI coding limit, in your menu bar. @@ -25,47 +25,36 @@ Tiny macOS 14+ menu bar app that keeps **AI coding-provider limits visible** and ## Install ### Requirements - - macOS 14+ (Sonoma) ### GitHub Releases - Download: ### Homebrew - ```bash brew install --cask codexbar ``` ### CLI Tarballs (macOS/Linux) - Homebrew formula (Linux today): - ```bash brew install steipete/tap/codexbar ``` - Arch Linux AUR package: - ```bash yay -S codexbar-cli ``` - Or download release tarballs from GitHub Releases: - - macOS: `CodexBarCLI-v-macos-arm64.tar.gz`, `CodexBarCLI-v-macos-x86_64.tar.gz` - Linux (glibc): `CodexBarCLI-v-linux-aarch64.tar.gz`, `CodexBarCLI-v-linux-x86_64.tar.gz` - Linux (static musl): `CodexBarCLI-v-linux-musl-aarch64.tar.gz`, `CodexBarCLI-v-linux-musl-x86_64.tar.gz` ### First run - - Open Settings → Providers and enable what you use. - Install/sign in to the provider sources you rely on: CLIs, browser sessions, OAuth/device flow, API keys, local app files, or provider apps depending on the provider. - Optional: Settings → Providers → Codex → OpenAI cookies (Automatic or Manual) to add dashboard extras. ### Set API keys from the CLI - Provider toggles and API keys live in the resolved CodexBar config file. New installs use `~/.config/codexbar/config.json`; existing `~/.codexbar/config.json` installs still load from the legacy path. You can script the same provider list that Settings → Providers uses: @@ -139,16 +128,14 @@ See [CLI configuration](docs/cli-configuration.md) for the full flow. - [Deepgram](docs/deepgram.md) — API key usage summaries across speech, agent, token, and TTS metrics. - [Poe](docs/poe.md) — API key for current point balance and recent points history. - [Chutes](docs/chutes.md) — API key for subscription usage, rolling and monthly quota windows, and pay-as-you-go quotas. -- [Neuralwatt](docs/neuralwatt.md) — API key for USD credit balance and optional per-key spending allowance from Neuralwatt Cloud. +- [Neuralwatt](docs/neuralwatt.md) — API key for USD credit balance and optional per-key spending allowance. - Open to new providers: [provider authoring guide](docs/provider.md). ## Icon & Screenshot - The menu bar icon is a tiny usage meter. Bar meaning is provider-specific, and errors/stale data can dim the icon or show an incident indicator. ## Features - - Multi-provider menu bar with per-provider toggles (Settings → Providers). - Provider-specific usage meters with reset countdowns. - Optional Codex web dashboard enrichments (code review remaining, usage breakdown, credits history). @@ -165,11 +152,9 @@ show an incident indicator. - Privacy-first: on-device parsing by default; browser cookies are opt-in and reused (no passwords stored). ## Privacy note - Wondering if CodexBar scans your disk? It doesn’t crawl your filesystem; it reads a small set of known locations (browser cookies/local storage, provider config files, local JSONL logs) when the related features are enabled. Provider tokens and token-account settings live in the CodexBar config file with restrictive file permissions. See the discussion and audit notes in [issue #12](https://github.com/steipete/CodexBar/issues/12). ## macOS permissions (why they’re needed) - - **Full Disk Access (optional)**: only required to read Safari cookies/local storage for web-based providers. If you don’t grant it, use another supported browser, manual cookies/API keys, OAuth, or CLI/local sources where that provider supports them. - **Keychain access (prompted by macOS)**: - Chromium cookie import needs the browser “Safe Storage” key to decrypt cookies. @@ -191,7 +176,6 @@ Wondering if CodexBar scans your disk? It doesn’t crawl your filesystem; it re - **What we do not request in the background**: no Screen Recording or Accessibility permissions; user-triggered helper actions may ask macOS for Automation permission to open Terminal. No passwords are stored (browser cookies are reused when you opt in). ## Docs - - Providers overview: [docs/providers.md](docs/providers.md) - Provider authoring: [docs/provider.md](docs/provider.md) - Issue labeling guide: [docs/ISSUE_LABELING.md](docs/ISSUE_LABELING.md) @@ -211,14 +195,12 @@ Wondering if CodexBar scans your disk? It doesn’t crawl your filesystem; it re - Changelog: [CHANGELOG.md](CHANGELOG.md) ## Getting started (dev) - - Clone the repo and open it in Xcode or run the scripts directly. - Launch once, then toggle providers in Settings → Providers. - Install/sign in to provider sources you rely on (CLIs, browser cookies, OAuth/device flow, API keys, or local app/config files). - Optional: set OpenAI cookies (Automatic or Manual) for Codex dashboard extras. ## Build from source - Requires macOS 14+ and Swift 6.2+. ```bash @@ -227,7 +209,6 @@ open CodexBar.app ``` Dev loop: - ```bash ./Scripts/compile_and_run.sh ./Scripts/compile_and_run.sh --test # also run the sharded test suite before packaging/relaunching @@ -236,37 +217,30 @@ make docs-list # list docs with frontmatter summaries ``` CLI install: - ```bash # after installing CodexBar.app in /Applications ./bin/install-codexbar-cli.sh ``` ## Related - - ✂️ [Trimmy](https://github.com/steipete/Trimmy) — “Paste once, run once.” Flatten multi-line shell snippets so they paste and run. - 🧳 [MCPorter](https://mcporter.dev) — TypeScript toolkit + CLI for Model Context Protocol servers. - 🧿 [oracle](https://askoracle.dev) — Ask the oracle when you're stuck. Invoke GPT-5 Pro with a custom context and files. ## Looking for a Windows version? - - [Win-CodexBar](https://github.com/Finesssee/Win-CodexBar) ## Linux desktop integration? - - [codexbar-waybar](https://github.com/Marouan-chak/codexbar-waybar) — Waybar custom module + GTK4 popover for Hyprland / Sway / other Wayland compositors, built on top of the bundled Linux CLI. - [Codexbar GNOME](https://extensions.gnome.org/extension/9841/codexbar/) — GNOME Shell extension that brings CodexBar usage into the desktop panel. - [noctalia-codex-usage](https://github.com/rayoplateado/noctalia-codex-usage) — Noctalia/Quickshell plugin that shows Codex 5-hour and weekly usage limits, built on top of the bundled Linux CLI. - [KodexBar](https://github.com/tylxr59/KodexBar) — KDE Plasma widget that shows CodexBar usage in the Plasma panel, built on top of the bundled Linux CLI. ## Status bar & terminal integration - - [showy-quota](https://github.com/enieuwy/showy-quota) — always-on AI plan quota strips for SketchyBar, tmux, and Zellij (standalone WASM plugin), built on `codexbar serve` / the bundled CLI. ## Credits - Inspired by [ccusage](https://github.com/ryoppippi/ccusage) (MIT), specifically the cost usage tracking. ## License - MIT • Peter Steinberger ([steipete](https://twitter.com/steipete)) diff --git a/Sources/CodexBar/Providers/NeuralWatt/NeuralWattProviderImplementation.swift b/Sources/CodexBar/Providers/NeuralWatt/NeuralWattProviderImplementation.swift index cd67345e2b..54847e3f98 100644 --- a/Sources/CodexBar/Providers/NeuralWatt/NeuralWattProviderImplementation.swift +++ b/Sources/CodexBar/Providers/NeuralWatt/NeuralWattProviderImplementation.swift @@ -31,7 +31,7 @@ struct NeuralWattProviderImplementation: ProviderImplementation { ProviderSettingsFieldDescriptor( id: "neuralwatt-api-key", title: "API key", - subtitle: "Stored in the CodexBar config file. Get your key from portal.neuralwatt.com/dashboard/api-keys.", + subtitle: "Stored in the CodexBar config file. Manage keys from the Neuralwatt dashboard.", kind: .secure, placeholder: "sk-...", binding: context.stringBinding(\.neuralWattAPIKey), diff --git a/Sources/CodexBarCore/Generated/CodexParserHash.generated.swift b/Sources/CodexBarCore/Generated/CodexParserHash.generated.swift index c0bc2c1b2b..b82c9ed118 100644 --- a/Sources/CodexBarCore/Generated/CodexParserHash.generated.swift +++ b/Sources/CodexBarCore/Generated/CodexParserHash.generated.swift @@ -1,5 +1,5 @@ // Generated by Scripts/regenerate-codex-parser-hash.sh. Do not edit by hand. enum CodexParserHash { - static let value = "2e350d981415198e" + static let value = "752981c35622cb84" } diff --git a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift index 28b9020967..d7e3c7a4ad 100644 --- a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift +++ b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift @@ -23,9 +23,9 @@ public enum NeuralWattProviderDescriptor { browserCookieOrder: nil, dashboardURL: "https://portal.neuralwatt.com/dashboard", subscriptionDashboardURL: "https://portal.neuralwatt.com/dashboard", - changelogURL: "https://portal.neuralwatt.com/docs/changelog", + changelogURL: nil, statusPageURL: nil, - statusLinkURL: "https://portal.neuralwatt.com/status"), + statusLinkURL: nil), branding: ProviderBranding( iconStyle: .neuralwatt, iconResourceName: "ProviderIcon-neuralwatt", diff --git a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattUsageFetcher.swift b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattUsageFetcher.swift index 2b6167c7f8..33b351c290 100644 --- a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattUsageFetcher.swift +++ b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattUsageFetcher.swift @@ -334,7 +334,8 @@ public struct NeuralWattUsageFetcher: Sendable { public static func fetchUsage( apiKey: String, - environment: [String: String] = ProcessInfo.processInfo.environment) async throws -> NeuralWattUsageSnapshot + environment: [String: String] = ProcessInfo.processInfo.environment, + transport: any ProviderHTTPTransport = ProviderHTTPClient.shared) async throws -> NeuralWattUsageSnapshot { let trimmed = apiKey.trimmingCharacters(in: .whitespacesAndNewlines) guard !trimmed.isEmpty else { @@ -351,7 +352,11 @@ public struct NeuralWattUsageFetcher: Sendable { let response: ProviderHTTPResponse do { - response = try await ProviderHTTPClient.shared.response(for: request) + response = try await transport.response(for: request) + } catch is CancellationError { + throw CancellationError() + } catch let error as URLError where error.code == .cancelled { + throw CancellationError() } catch { throw NeuralWattUsageError.networkError(error.localizedDescription) } diff --git a/Tests/CodexBarTests/NeuralWattUsageFetcherTests.swift b/Tests/CodexBarTests/NeuralWattUsageFetcherTests.swift index 786b1e3c97..e4ac56859a 100644 --- a/Tests/CodexBarTests/NeuralWattUsageFetcherTests.swift +++ b/Tests/CodexBarTests/NeuralWattUsageFetcherTests.swift @@ -238,6 +238,42 @@ struct NeuralWattUsageFetcherTests { } } + @Test + func `fetch rejects endpoint override before sending API key`() async throws { + let transport = ProviderHTTPTransportHandler { _ in + Issue.record("Endpoint override validation must happen before the request") + throw URLError(.badURL) + } + + await #expect(throws: NeuralWattSettingsError.invalidEndpointOverride( + NeuralWattSettingsReader.apiURLEnvironmentKey)) + { + _ = try await NeuralWattUsageFetcher.fetchUsage( + apiKey: "sk-test", + environment: [NeuralWattSettingsReader.apiURLEnvironmentKey: "https://user@example.com"], + transport: transport) + } + } + + @Test + func `fetch preserves transport cancellation`() async throws { + let transport = ProviderHTTPTransportHandler { _ in + throw CancellationError() + } + + do { + _ = try await NeuralWattUsageFetcher.fetchUsage( + apiKey: "sk-test", + environment: [:], + transport: transport) + Issue.record("Expected CancellationError") + } catch is CancellationError { + // Expected: refresh cancellation must not become a provider error. + } catch { + Issue.record("Expected CancellationError, got \(error)") + } + } + @Test func `unauthorized fetch throws missing credentials`() async throws { let registered = URLProtocol.registerClass(NeuralWattStubURLProtocol.self) diff --git a/docs/configuration.md b/docs/configuration.md index 36ac12c1a9..f1be658f47 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -12,7 +12,6 @@ CodexBar reads a single JSON config file for CLI and app provider settings. API keys, manual cookie headers, source selection, ordering, and token accounts live here. Keychain is still used for runtime cookie caches, browser Safe Storage access, and provider OAuth/device-flow credentials where those flows require it. ## Location - - `CODEXBAR_CONFIG=/path/to/config.json` when set. - `$XDG_CONFIG_HOME/codexbar/config.json` when `XDG_CONFIG_HOME` is set to an absolute path. Relative values are ignored. @@ -22,7 +21,6 @@ API keys, manual cookie headers, source selection, ordering, and token accounts - Permissions are set to `0600` whenever CodexBar writes the file on macOS and Linux. ## Root shape - ```json { "version": 1, @@ -44,7 +42,6 @@ API keys, manual cookie headers, source selection, ordering, and token accounts ``` ## Provider fields - All provider fields are optional unless noted. - `id` (required): provider identifier. @@ -64,7 +61,6 @@ All provider fields are optional unless noted. - `tokenAccounts`: multi-account tokens for providers in `TokenAccountSupportCatalog`. ## Manual cookies - Use manual cookies when automatic browser import is unavailable, disabled, or too noisy for your setup. The app and CLI both read the same resolved config file, so a manual cookie saved in the UI is also used by `codexbar`, and a cookie written by tooling is shown in the app after reload. @@ -74,7 +70,7 @@ export. In browser DevTools, open the Network tab, select a request for the prov header named `Cookie`. You can paste either the full `Cookie: name=value; other=value` string or just `name=value; other=value`. -If you have a Netscape export, convert each non-comment row to `name=value` and join values with `;`. Do not paste +If you have a Netscape export, convert each non-comment row to `name=value` and join values with `; `. Do not paste the raw `# Netscape HTTP Cookie File` text into `cookieHeader`. Example placeholder config: @@ -153,7 +149,6 @@ Manual cookies are secrets. Keep the CodexBar config file private, leave its per and never paste real cookie values or readable DevTools screenshots into public issues. ### tokenAccounts - ```json { "version": 1, @@ -173,16 +168,13 @@ and never paste real cookie values or readable DevTools screenshots into public z.ai team accounts also use `usageScope`, `organizationId`, and `workspaceID`; see [z.ai](zai.md). ## Provider IDs - Current IDs (see `Sources/CodexBarCore/Providers/Providers.swift`): `codex`, `openai`, `azureopenai`, `claude`, `cursor`, `opencode`, `opencodego`, `alibaba`, `alibabatokenplan`, `factory`, `gemini`, `antigravity`, `copilot`, `devin`, `zai`, `minimax`, `manus`, `kimi`, `kilo`, `kiro`, `vertexai`, `augment`, `jetbrains`, `kimik2`, `moonshot`, `amp`, `t3chat`, `ollama`, `synthetic`, `warp`, `openrouter`, `elevenlabs`, `windsurf`, `zed`, `perplexity`, `mimo`, `doubao`, `sakana`, `abacus`, `mistral`, `deepseek`, `codebuff`, `crof`, `venice`, `commandcode`, `stepfun`, `bedrock`, `grok`, `groq`, `llmproxy`, `litellm`, `deepgram`, `poe`, `chutes`, `neuralwatt`. ## Ordering - The order of `providers` controls display/order in the app and CLI. Reorder the array to change ordering. ## Notes - - Fields not relevant to a provider are ignored. - Omitted providers are appended with defaults during normalization. - Keep the file private; it contains secrets. diff --git a/docs/neuralwatt.md b/docs/neuralwatt.md index a811fce323..6facf998a8 100644 --- a/docs/neuralwatt.md +++ b/docs/neuralwatt.md @@ -9,10 +9,10 @@ read_when: # Neuralwatt Provider The Neuralwatt provider reads account quota from the Neuralwatt Cloud API using an API key. -Neuralwatt Cloud is an OpenAI-compatible inference API with energy-based pricing. Credits are a -Deplete-as-you-go USD balance (like DeepSeek): they do **not** reset on a billing cycle, they -simply exhaust as the API is used and are refilled by topping up. The quota endpoint exposes -the USD credit balance, current-month spend fields, and optional per-key spending allowances. +Neuralwatt Cloud is an OpenAI-compatible inference API with energy-based pricing. Prepaid credits +are a deplete-as-you-go USD balance: they do **not** reset on a billing cycle and are refilled by +topping up. Active subscription usage is billed separately against a kWh allowance. The quota +endpoint exposes both surfaces plus current-month spend and optional per-key spending allowances. ## Features @@ -40,7 +40,7 @@ default. Use `--no-enable` to save the key without enabling the provider. 1. Open **Settings → Providers** 2. Enable **Neuralwatt** -3. Open `https://portal.neuralwatt.com/dashboard/api-keys` +3. Open `https://portal.neuralwatt.com/dashboard` 4. Create or copy an API key 5. Paste the key into CodexBar's Neuralwatt provider settings @@ -61,8 +61,8 @@ For tests or self-hosted/proxy setups, override the API base URL with `NEURALWAT `usage.current_month.cost_usd`, `key.allowance.limit_usd`, `key.allowance.spent_usd`, `key.allowance.period` - `credits_used_usd` is derived as `total_credits_usd − credits_remaining_usd` when the API omits it. -- `subscription` may be `null`; subscription periods are not rendered as resets because Neuralwatt - credits deplete until topped up. +- `subscription` may be `null`. Its separate kWh allowance is not rendered yet; landing that display + requires an explicit product decision because it changes the provider from balance-only to mixed quota semantics. ## Troubleshooting diff --git a/docs/providers.md b/docs/providers.md index acd7f65596..c3582a24f3 100644 --- a/docs/providers.md +++ b/docs/providers.md @@ -12,7 +12,6 @@ CodexBar currently registers 54 provider IDs. Some companies expose multiple sur OpenCode vs OpenCode Go, because the auth source and quota shape differ. ## Fetch strategies (current) - Legend: web (browser cookies/WebView), cli (RPC/PTy or provider CLI), oauth (provider OAuth), api token, local probe, web dashboard. Source labels (CLI/header): `openai-web`, `web`, `oauth`, `api`, `local`, `cli`, plus provider-specific CLI labels (e.g. `codex-cli`, `claude`). @@ -74,11 +73,10 @@ headers, source selection, provider ordering, and token accounts are stored in ` | LiteLLM | API key + base URL → `/key/info`, then `/user/info` or `/team/info` budget usage (`api`). | | Deepgram | API key → project discovery and usage breakdown API (`api`). | | Chutes | API key from config/env → subscription usage and quota API (`api`). | -| Neuralwatt | API key from config/env → `/v1/quota` credit balance and spend (`api`). | +| Neuralwatt | API key from config/env → `/v1/quota` credit balance and per-key allowance (`api`). | | Zed | Zed editor Keychain session → `cloud.zed.dev/client/users/me` for plan and quota data (`local`). | ## Codex - - App Auto: OAuth API first; falls back to CLI only when OAuth credentials are missing or auth/refresh is invalid. - Web dashboard (optional, off by default): `https://chatgpt.com/codex/settings/usage` via WebView + browser cookies. - Battery saver toggle (currently off by default): reduces routine OpenAI web refreshes but still allows explicit manual refreshes. @@ -89,14 +87,12 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/codex.md`. ## OpenAI - - API key from `~/.codexbar/config.json`, `OPENAI_ADMIN_KEY`, or `OPENAI_API_KEY`. - Admin API keys are preferred and fetch organization costs plus completion usage for inline Today/7d/configured-window dashboards. - Normal API keys fall back to the legacy credit-grants balance endpoint when organization usage is unavailable. - Details: `docs/openai.md`. ## Azure OpenAI - - API key, endpoint, and deployment from `~/.codexbar/config.json` or `AZURE_OPENAI_API_KEY`, `AZURE_OPENAI_ENDPOINT`, and `AZURE_OPENAI_DEPLOYMENT_NAME`. - `AZURE_OPENAI_ENDPOINT` and configured endpoint overrides must be HTTPS URLs or bare hosts normalized to HTTPS; explicit `http://` URLs, user info, and encoded host-delimiter tricks fail closed before `api-key` headers are attached. - Validates the configured deployment with a minimal chat-completions request; it does not expose Azure spend or quota history. @@ -104,7 +100,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Status: Azure status page link. ## Claude - - Admin API: `sk-ant-admin...` key in Settings/config, token accounts, or `ANTHROPIC_ADMIN_KEY`. - Admin API shows organization spend/messages summaries with the same inline dashboard pattern as OpenAI API. - App Auto: OAuth API (`oauth`) → CLI PTY (`claude`) → Web API (`web`). @@ -114,7 +109,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/claude.md`. ## z.ai - - API token from `~/.codexbar/config.json` (`providers[].apiKey`) or `Z_AI_API_KEY` env var. - Supports global and BigModel CN quota hosts; override with `Z_AI_API_HOST` or `Z_AI_QUOTA_URL`. - z.ai endpoint overrides must be HTTPS or bare hosts normalized to HTTPS. `Z_AI_QUOTA_URL` takes precedence for @@ -123,7 +117,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/zai.md`. ## Devin - - Automatic auth reads the current `auth1_session` token and organization metadata from Chrome localStorage. - Manual auth accepts the `Authorization: Bearer ...` value from an app.devin.ai request. - Usage endpoint: `GET /api//billing/quota/usage`. @@ -131,14 +124,12 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/devin.md`. ## Manus - - Session token via browser `session_id` cookie, manual Settings entry, `MANUS_SESSION_TOKEN`, or `MANUS_COOKIE`. - Credits endpoint: `POST https://api.manus.im/user.v1.UserService/GetAvailableCredits`. - Auto mode prefers cached/browser cookies before env fallback; manual mode accepts either a bare `session_id` value or a full Cookie header. - Status: none yet. ## MiniMax - - Coding Plan API token or web session from configured/manual/browser sources. - Supports global and China mainland hosts via provider region settings and environment overrides. - Web-session billing history can render 30-day token charts plus top model/method breakdowns when MiniMax exposes it. @@ -146,7 +137,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/minimax.md`. ## Kimi - - Kimi Code API key via `~/.codexbar/config.json` or `KIMI_CODE_API_KEY`. - Web fallback uses the JWT from `kimi-auth` cookie via manual entry or `KIMI_AUTH_TOKEN` env var. - Shows weekly quota and 5-hour rate limit (300 minutes). @@ -154,7 +144,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/kimi.md`. ## Kilo - - API token from `~/.codexbar/config.json` (`providers[].apiKey`) or `KILO_API_KEY`. - Auto mode tries API first and falls back to CLI auth when API credentials are missing or unauthorized. - CLI auth source: `~/.local/share/kilo/auth.json` (`kilo.access`), typically created by `kilo login`. @@ -162,7 +151,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/kilo.md`. ## Kimi K2 (unofficial) - - API key via `~/.codexbar/config.json` or `KIMI_K2_API_KEY`/`KIMI_API_KEY` env var. - Shows credit usage from the legacy `kimi-k2.ai` consumed/remaining totals. - Use Moonshot / Kimi API for the official Kimi API account and billing surface. @@ -170,7 +158,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/kimi-k2.md`. ## Gemini - - OAuth-backed quota API (`retrieveUserQuota`) using Gemini CLI credentials. - Token refresh via Google OAuth if expired. - Tier detection via `loadCodeAssist`. @@ -178,27 +165,23 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/gemini.md`. ## Antigravity - - Local Antigravity language server (internal protocol, HTTPS on localhost). - `GetUserStatus` primary; `GetCommandModelConfigs` fallback. - Status: Google Workspace incidents (Gemini product). - Details: `docs/antigravity.md`. ## Cursor - - Web API via browser cookies (`cursor.com` + `cursor.sh`). - Fallback: stored WebKit session. - Status: Statuspage.io (Cursor). - Details: `docs/cursor.md`. ## OpenCode - - Web dashboard via browser cookies (`opencode.ai`). - Status: none yet. - Details: `docs/opencode.md`. ## OpenCode Go - - Web dashboard via browser or manual cookies (`opencode.ai`). - Auto mode falls back to local usage from `~/.local/share/opencode/opencode.db` on macOS and Linux. - Uses the workspace Go page/server data for rolling 5-hour, weekly, and optional monthly usage windows. @@ -207,7 +190,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/opencode.md`. ## Alibaba Coding Plan - - Web mode uses Alibaba console RPC with form payload + `sec_token`. - Cookie sources: browser import (`auto`) or manual header (`cookieSource: manual`). - API key fallback from Settings (`providers[].apiKey`) or `ALIBABA_CODING_PLAN_API_KEY` env var. @@ -217,7 +199,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/alibaba-coding-plan.md`. ## Alibaba Token Plan - - Web mode posts to the Bailian `GetSubscriptionSummary` endpoint with form-encoded params and optional `sec_token`. - Cookie sources: browser import (`auto`), manual Cookie header, or `ALIBABA_TOKEN_PLAN_COOKIE`. - Default quota URL: `https://bailian.console.aliyun.com/data/api.json?action=GetSubscriptionSummary&product=BssOpenAPI-V3`. @@ -226,21 +207,18 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/alibaba-token-plan.md`. ## Droid (Factory) - - Web API via Factory cookies, bearer tokens, and WorkOS refresh tokens. - Multiple fallback strategies (cookies → stored tokens → local storage → WorkOS cookies). - Status: `https://status.factory.ai`. - Details: `docs/factory.md`. ## Copilot - - GitHub device flow OAuth token + `api.github.com/copilot_internal/user`. - Supports multiple token accounts and account switching from provider settings/menu surfaces. - Status: Statuspage.io (GitHub). - Details: `docs/copilot.md`. ## Kiro - - CLI-based: runs `kiro-cli chat --no-interactive "/usage"` with 10s timeout. - Parses ANSI output for plan name, monthly credits percentage, and bonus credits. - Requires `kiro-cli` installed and logged in via AWS Builder ID. @@ -248,14 +226,12 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/kiro.md`. ## Warp - - API token from Settings or `WARP_API_KEY` / `WARP_TOKEN` env var. - Shows monthly credits usage and next refresh time. - Status: none yet. - Details: `docs/warp.md`. ## ElevenLabs - - API key from Settings, token accounts, `ELEVENLABS_API_KEY`, or `XI_API_KEY`. - Reads `GET /v1/user/subscription` from `api.elevenlabs.io`. - Shows character credit usage, reset timing, and voice slot usage when available. @@ -264,7 +240,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/elevenlabs.md`. ## Vertex AI - - OAuth credentials from `gcloud auth application-default login` (ADC). - Quota usage via Cloud Monitoring `consumer_quota` metrics for `aiplatform.googleapis.com`. - Token cost: uses the Claude local-log scanner filtered to Vertex AI-tagged entries. @@ -272,7 +247,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/vertexai.md`. ## JetBrains AI - - Local XML quota file from IDE configuration directory. - Auto-detects installed JetBrains IDEs; uses most recently used. - Reads `AIAssistantQuotaManager2.xml` for monthly credits and refill date. @@ -280,14 +254,12 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/jetbrains.md`. ## Zed - - Reads the signed-in Zed editor session from the macOS Keychain (`credentials_url` / `https://zed.dev`). - Calls `GET https://cloud.zed.dev/client/users/me` for plan, billing cycle, Edit Predictions quota, and overdue invoice flag. - Sign in to the Zed editor first. - Details: `docs/zed.md`. ## Augment - - Auto mode tries the `auggie` CLI first. - Web fallback uses browser cookies, with manual cookie header support. - Tracks credit usage and account/subscription data where available. @@ -295,7 +267,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/augment.md`. ## Amp - - Auto mode tries the local `amp usage` command first. - API mode calls Amp's balance endpoint with an access token. - Web fallback reads the legacy settings page with browser cookies. @@ -304,7 +275,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/amp.md`. ## T3 Chat - - Web tRPC endpoint (`https://t3.chat/api/trpc/getCustomerData`) via browser cookies. - Parses JSONL response lines and extracts customer data from the embedded tRPC payload. - Shows the 4-hour Base bucket and monthly Overage bucket documented in the T3 Chat FAQ. @@ -312,20 +282,17 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/t3chat.md`. ## Ollama - - Web settings page (`https://ollama.com/settings`) via browser cookies. - Parses Cloud Usage plan badge, session/weekly usage, and reset timestamps. - Status: none yet. - Details: `docs/ollama.md`. ## Synthetic - - API key from `~/.codexbar/config.json` (`providers[].apiKey`) or `SYNTHETIC_API_KEY`. - Shows rolling five-hour, weekly token, search-hourly, and cost/credit quota lanes when present. - Status: none yet. ## OpenRouter - - API token from `~/.codexbar/config.json` (`providers[].apiKey`) or `OPENROUTER_API_KEY` env var. - Reads credits and key rate-limit info from OpenRouter APIs. - Shows daily, weekly, and monthly API-key spend when `/api/v1/key` returns those fields. @@ -334,7 +301,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/openrouter.md`. ## Perplexity - - Browser session cookie from automatic import, manual header/token, or `PERPLEXITY_SESSION_TOKEN` / `PERPLEXITY_COOKIE`. - Tracks recurring credits, bonus/promotional credits, purchased credits, and renewal date when present. - Status: `https://status.perplexity.com/` (link only, no auto-polling). @@ -348,7 +314,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/mimo.md`. ## Doubao - - API key via `ARK_API_KEY`, `VOLCENGINE_API_KEY`, `DOUBAO_API_KEY`, or provider config. - Probes Volcengine Ark chat completions and reads request rate-limit headers when present. - Status: none yet. @@ -361,7 +326,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/sakana.md`. ## Abacus AI - - Browser cookies (`abacus.ai`, `apps.abacus.ai`) via automatic import or manual header. - Reads organization compute points and billing data. - Shows monthly credit gauge with pace tick and reserve/deficit estimate. @@ -369,7 +333,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/abacus.md`. ## Mistral - - Session cookie (`ory_session_*`) from browser auto-import or manual `Cookie:` header. - CSRF token (`csrftoken` cookie) sent as `X-CSRFTOKEN` for billing and Vibe usage requests. - Domains: `admin.mistral.ai` for API billing and `console.mistral.ai` for optional Vibe subscription usage. Console requests forward only `csrftoken` and `ory_session_*`; all other admin cookies stay origin-bound. @@ -381,14 +344,12 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Status: `https://status.mistral.ai` (link only, no auto-polling). ## DeepSeek - - API key via `DEEPSEEK_API_KEY` / `DEEPSEEK_KEY` env var or DeepSeek token accounts. - Shows total balance with paid vs. granted breakdown; USD preferred when multiple currencies present. - Status: `https://status.deepseek.com` (link only, no auto-polling). - Details: `docs/deepseek.md`. ## Moonshot / Kimi API - - API key via `MOONSHOT_API_KEY` / `MOONSHOT_KEY` env var or provider config. - Reads `GET /v1/users/me/balance` from the selected Moonshot region. - Region: international (`api.moonshot.ai`) or China mainland (`api.moonshot.cn`), configurable in Settings or `MOONSHOT_REGION`. @@ -397,14 +358,12 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/moonshot.md`. ## Venice - - API key via `VENICE_API_KEY` / `VENICE_KEY` env var or Venice token accounts. - Shows current DIEM or USD balance; DIEM epoch allocation progress when available. - Status: none yet. - Details: `docs/venice.md`. ## Codebuff - - API token from `~/.codexbar/config.json`, `CODEBUFF_API_KEY`, or `~/.config/manicode/credentials.json` created by `codebuff login`. - Reads usage and subscription data from Codebuff APIs. - Shows credit balance, weekly rate limit, reset timing, subscription status, and auto-top-up flag when present. @@ -413,7 +372,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/codebuff.md`. ## Crof - - API key from `~/.codexbar/config.json`, `CROF_API_KEY`, or `CROFAI_API_KEY`. - Reads `credits`, `requests_plan`, and `usable_requests` from `GET https://crof.ai/usage_api/`. - Shows request quota as the primary usage window and dollar credits as the secondary row. @@ -422,7 +380,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/crof.md`. ## Command Code - - Browser session cookies from automatic import or manual `Cookie:` header. - Linux CLI supports configured manual cookies; automatic browser import remains macOS-only. - Reads monthly USD credits and billing-cycle usage from `api.commandcode.ai`. @@ -431,7 +388,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/command-code.md`. ## Grok - - `grok agent stdio` (ACP) JSON-RPC `x.ai/billing` method; requires `grok login` (SuperGrok OAuth/OIDC). - Reads cached credentials from `~/.grok/auth.json` for identity (email, team). - Falls back to grok.com's billing gRPC-web endpoint via Chrome session cookies when the CLI does not expose billing. @@ -441,7 +397,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/grok.md`. ## AWS Bedrock - - AWS credentials from `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and optional `AWS_SESSION_TOKEN`. - Region from `AWS_REGION` / `AWS_DEFAULT_REGION`, defaulting to `us-east-1`. - Reads AWS Cost Explorer for Bedrock spend and can compare usage against `CODEXBAR_BEDROCK_BUDGET`. @@ -450,7 +405,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/bedrock.md`. ## Deepgram - - API key from config or `DEEPGRAM_API_KEY`. - Optional project ID from provider settings or `DEEPGRAM_PROJECT_ID`; otherwise aggregates all visible projects. - Optional API base URL override via `DEEPGRAM_API_URL`; overrides must be HTTPS or bare hosts normalized to HTTPS. @@ -458,7 +412,6 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/deepgram.md`. ## LiteLLM - - API key from config or `LITELLM_API_KEY`; base URL from config `enterpriseHost` or `LITELLM_BASE_URL`. - Reads `/key/info` first, then `/user/info?user_id=...` for user-bound keys or `/team/info?team_id=...` for team-only keys. - User-bound keys show personal budget usage as the primary window and the key's exact matching team as the secondary window. @@ -468,30 +421,25 @@ headers, source selection, provider ordering, and token accounts are stored in ` - Details: `docs/litellm.md`. ## Poe - - API key from config or `POE_API_KEY`. - Reads the current point balance and recent points history from Poe's official usage API. - History failures are non-fatal; the current balance remains available. - Details: `docs/poe.md`. ## Chutes - - API key from config or `CHUTES_API_KEY`. - Reads subscription usage first, then fills missing rolling, monthly, or pay-as-you-go quota data from the quota APIs. - Uses Chutes' management API at `https://api.chutes.ai`; `CHUTES_API_URL` can override it with an HTTPS endpoint. - Details: `docs/chutes.md`. ## Neuralwatt - - API key from config or `NEURALWATT_API_KEY`. - Reads `GET /v1/quota` from `api.neuralwatt.com`; `NEURALWATT_API_URL` can override it with an HTTPS endpoint. -- Credit-exhaustion model (like DeepSeek): USD credits deplete as the API is used and do not reset; the primary window fills as credits are consumed. -- Extra windows: per-key spending allowance when configured. Current-month spend is parsed, but not rendered as a resettable quota window. -- Status: link only (`https://portal.neuralwatt.com/status`), no auto-polling. +- Shows the USD prepaid-credit balance and an optional per-key spending allowance. +- Active subscription kWh allowance is returned separately by Neuralwatt and is not yet surfaced pending a product decision. - Details: `docs/neuralwatt.md`. ## StepFun - - Username/password login or manual Oasis-Token. - Reads Step Plan 5-hour and weekly rate-limit windows from `platform.stepfun.com`. - Shows subscription plan name when the Step Plan status API returns one. From a2e33776fd4c0ab088ae9c716c4e28e13c6a464c Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 1 Jul 2026 10:47:24 +0100 Subject: [PATCH 3/4] fix: avoid duplicate Neuralwatt credits row --- .../Providers/NeuralWatt/NeuralWattProviderDescriptor.swift | 2 +- Tests/CodexBarTests/MenuCardNeuralWattTests.swift | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift index d7e3c7a4ad..98e3f0f0ab 100644 --- a/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift +++ b/Sources/CodexBarCore/Providers/NeuralWatt/NeuralWattProviderDescriptor.swift @@ -13,7 +13,7 @@ public enum NeuralWattProviderDescriptor { weeklyLabel: "Spend", opusLabel: nil, supportsOpus: false, - supportsCredits: true, + supportsCredits: false, creditsHint: "Energy-based USD credit balance.", toggleTitle: "Show Neuralwatt usage", cliName: "neuralwatt", diff --git a/Tests/CodexBarTests/MenuCardNeuralWattTests.swift b/Tests/CodexBarTests/MenuCardNeuralWattTests.swift index 4458eb1439..99b0ce901f 100644 --- a/Tests/CodexBarTests/MenuCardNeuralWattTests.swift +++ b/Tests/CodexBarTests/MenuCardNeuralWattTests.swift @@ -47,6 +47,7 @@ struct MenuCardNeuralWattTests { #expect(statusText == "$51.00 remaining of $77.04") #expect(primary.detailText == nil) #expect(primary.resetText == nil) + #expect(model.creditsText == nil) #expect(model.metrics.contains { $0.title == "This month" } == false) #expect(model.metrics.allSatisfy { metric in metric.resetText?.localizedCaseInsensitiveContains("reset") != true From b00c39edf95b6c9c493931a3bef52fe93c5595c7 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 1 Jul 2026 13:32:32 +0100 Subject: [PATCH 4/4] fix: respect Neuralwatt quota limits --- .../SettingsStore+MenuPreferences.swift | 2 +- .../CodexBar/UsageStore+TokenAccounts.swift | 28 ++++ Sources/CodexBarCLI/CLIDiagnoseCommand.swift | 2 + Sources/CodexBarCLI/CLIUsageCommand.swift | 11 +- .../CodexBarCore/TokenAccountSupport.swift | 5 +- .../TokenAccountSupportCatalog+Data.swift | 3 +- .../CLIDiagnoseCommandTests.swift | 13 ++ .../ProvidersPaneCoverageTests.swift | 14 ++ ...geStoreNeuralWattAccountRefreshTests.swift | 145 ++++++++++++++++++ 9 files changed, 219 insertions(+), 4 deletions(-) create mode 100644 Tests/CodexBarTests/UsageStoreNeuralWattAccountRefreshTests.swift diff --git a/Sources/CodexBar/SettingsStore+MenuPreferences.swift b/Sources/CodexBar/SettingsStore+MenuPreferences.swift index 9e644794b4..a0f464381d 100644 --- a/Sources/CodexBar/SettingsStore+MenuPreferences.swift +++ b/Sources/CodexBar/SettingsStore+MenuPreferences.swift @@ -143,7 +143,7 @@ extension SettingsStore { static func isBalanceOnlyProvider(_ provider: UsageProvider) -> Bool { switch provider { - case .deepseek, .mistral, .kimik2, .moonshot, .poe: + case .deepseek, .mistral, .kimik2, .moonshot, .neuralwatt, .poe: true default: false diff --git a/Sources/CodexBar/UsageStore+TokenAccounts.swift b/Sources/CodexBar/UsageStore+TokenAccounts.swift index 3d84aa2835..5fe4e3c7bc 100644 --- a/Sources/CodexBar/UsageStore+TokenAccounts.swift +++ b/Sources/CodexBar/UsageStore+TokenAccounts.swift @@ -586,6 +586,34 @@ extension UsageStore { return (index, account, descriptor, context) } + if let delay = TokenAccountSupportCatalog.support(for: provider)?.minimumDelayBetweenAccountRefreshes { + var results: [TokenAccountFetchResult] = [] + results.reserveCapacity(requests.count) + for request in requests { + if !results.isEmpty { + do { + try await Task.sleep(for: delay) + } catch { + for pending in requests.dropFirst(results.count) { + results.append(TokenAccountFetchResult( + index: pending.index, + account: pending.account, + outcome: ProviderFetchOutcome( + result: .failure(CancellationError()), + attempts: []))) + } + return results + } + } + let outcome = await request.descriptor.fetchOutcome(context: request.context) + results.append(TokenAccountFetchResult( + index: request.index, + account: request.account, + outcome: outcome)) + } + return results + } + return await withTaskGroup( of: TokenAccountFetchResult.self, returning: [TokenAccountFetchResult].self) diff --git a/Sources/CodexBarCLI/CLIDiagnoseCommand.swift b/Sources/CodexBarCLI/CLIDiagnoseCommand.swift index c5337d4d5b..b2e6fd4d63 100644 --- a/Sources/CodexBarCLI/CLIDiagnoseCommand.swift +++ b/Sources/CodexBarCLI/CLIDiagnoseCommand.swift @@ -237,6 +237,8 @@ extension CodexBarCLI { GroqSettingsReader.apiKey(environment: environment) != nil case .kilo: KiloSettingsReader.apiKey(environment: environment) != nil + case .neuralwatt: + NeuralWattSettingsReader.apiKey(environment: environment) != nil default: false } diff --git a/Sources/CodexBarCLI/CLIUsageCommand.swift b/Sources/CodexBarCLI/CLIUsageCommand.swift index 803a760ecc..7020790304 100644 --- a/Sources/CodexBarCLI/CLIUsageCommand.swift +++ b/Sources/CodexBarCLI/CLIUsageCommand.swift @@ -210,7 +210,16 @@ extension CodexBarCLI { let selections = Self.accountSelections(from: accounts) var output = UsageCommandOutput() - for account in selections { + let accountRefreshDelay = TokenAccountSupportCatalog + .support(for: provider)?.minimumDelayBetweenAccountRefreshes + for (index, account) in selections.enumerated() { + if index > 0, let accountRefreshDelay { + do { + try await Task.sleep(for: accountRefreshDelay) + } catch { + return output + } + } let result = await Self.fetchUsageOutput( provider: provider, account: account, diff --git a/Sources/CodexBarCore/TokenAccountSupport.swift b/Sources/CodexBarCore/TokenAccountSupport.swift index 37ec660a97..eeca07e208 100644 --- a/Sources/CodexBarCore/TokenAccountSupport.swift +++ b/Sources/CodexBarCore/TokenAccountSupport.swift @@ -13,6 +13,7 @@ public struct TokenAccountSupport: Sendable { public let requiresManualCookieSource: Bool public let cookieName: String? public let environmentKeysToScrub: [String] + public let minimumDelayBetweenAccountRefreshes: Duration? public init( title: String, @@ -21,7 +22,8 @@ public struct TokenAccountSupport: Sendable { injection: TokenAccountInjection, requiresManualCookieSource: Bool, cookieName: String?, - environmentKeysToScrub: [String] = []) + environmentKeysToScrub: [String] = [], + minimumDelayBetweenAccountRefreshes: Duration? = nil) { self.title = title self.subtitle = subtitle @@ -30,6 +32,7 @@ public struct TokenAccountSupport: Sendable { self.requiresManualCookieSource = requiresManualCookieSource self.cookieName = cookieName self.environmentKeysToScrub = environmentKeysToScrub + self.minimumDelayBetweenAccountRefreshes = minimumDelayBetweenAccountRefreshes } } diff --git a/Sources/CodexBarCore/TokenAccountSupportCatalog+Data.swift b/Sources/CodexBarCore/TokenAccountSupportCatalog+Data.swift index abecfe7452..5d55c9a42a 100644 --- a/Sources/CodexBarCore/TokenAccountSupportCatalog+Data.swift +++ b/Sources/CodexBarCore/TokenAccountSupportCatalog+Data.swift @@ -135,7 +135,8 @@ extension TokenAccountSupportCatalog { placeholder: "sk-...", injection: .environment(key: NeuralWattSettingsReader.apiKeyEnvironmentKey), requiresManualCookieSource: false, - cookieName: nil), + cookieName: nil, + minimumDelayBetweenAccountRefreshes: .seconds(1)), .groq: TokenAccountSupport( title: "API keys", subtitle: "Store multiple Groq API keys.", diff --git a/Tests/CodexBarTests/CLIDiagnoseCommandTests.swift b/Tests/CodexBarTests/CLIDiagnoseCommandTests.swift index 68175d630a..fd10e40f28 100644 --- a/Tests/CodexBarTests/CLIDiagnoseCommandTests.swift +++ b/Tests/CodexBarTests/CLIDiagnoseCommandTests.swift @@ -115,6 +115,19 @@ struct CLIDiagnoseCommandTests { #expect(summary.modes == ["api"]) } + @Test + func `generic diagnose auth summary detects Neuralwatt environment credentials`() { + let summary = CodexBarCLI._diagnosticAuthSummaryForTesting( + provider: .neuralwatt, + account: nil, + config: nil, + environment: [NeuralWattSettingsReader.apiKeyEnvironmentKey: "sk-test"], + settings: nil) + + #expect(summary.configured) + #expect(summary.modes == ["api"]) + } + @Test func `generic diagnose auth summary requires complete Bedrock credentials`() { let partial = CodexBarCLI._diagnosticAuthSummaryForTesting( diff --git a/Tests/CodexBarTests/ProvidersPaneCoverageTests.swift b/Tests/CodexBarTests/ProvidersPaneCoverageTests.swift index 97c219b472..a216943eee 100644 --- a/Tests/CodexBarTests/ProvidersPaneCoverageTests.swift +++ b/Tests/CodexBarTests/ProvidersPaneCoverageTests.swift @@ -225,6 +225,20 @@ struct ProvidersPaneCoverageTests { } } + @Test + func `Neuralwatt menu bar metric picker omits nonexistent spend lane`() { + Self.withEnglishLocalization { + let settings = Self.makeSettingsStore(suite: "ProvidersPaneCoverageTests-neuralwatt-picker") + let store = Self.makeUsageStore(settings: settings) + let pane = ProvidersPane(settings: settings, store: store) + + let picker = pane._test_menuBarMetricPicker(for: .neuralwatt) + #expect(picker?.options.map(\.id) == [ + MenuBarMetricPreference.automatic.rawValue, + ]) + } + } + @Test func `moonshot menu bar metric picker shows balance only copy`() { Self.withEnglishLocalization { diff --git a/Tests/CodexBarTests/UsageStoreNeuralWattAccountRefreshTests.swift b/Tests/CodexBarTests/UsageStoreNeuralWattAccountRefreshTests.swift new file mode 100644 index 0000000000..143ae6fab0 --- /dev/null +++ b/Tests/CodexBarTests/UsageStoreNeuralWattAccountRefreshTests.swift @@ -0,0 +1,145 @@ +import CodexBarCore +import Foundation +import Testing +@testable import CodexBar + +private actor NeuralWattAccountRefreshRecorder { + private(set) var dates: [Date] = [] + private var waiters: [(count: Int, continuation: CheckedContinuation)] = [] + + func record() { + self.dates.append(Date()) + let ready = self.waiters.filter { self.dates.count >= $0.count } + self.waiters.removeAll { self.dates.count >= $0.count } + ready.forEach { $0.continuation.resume() } + } + + func waitForCount(_ count: Int) async { + if self.dates.count >= count { return } + await withCheckedContinuation { continuation in + self.waiters.append((count, continuation)) + } + } +} + +private struct NeuralWattAccountRefreshStrategy: ProviderFetchStrategy { + let recorder: NeuralWattAccountRefreshRecorder + + let id = "neuralwatt-account-refresh-test" + let kind: ProviderFetchKind = .apiToken + + func isAvailable(_: ProviderFetchContext) async -> Bool { + true + } + + func fetch(_: ProviderFetchContext) async throws -> ProviderFetchResult { + await self.recorder.record() + let snapshot = UsageSnapshot( + primary: RateWindow( + usedPercent: 25, + windowMinutes: nil, + resetsAt: nil, + resetDescription: nil), + secondary: nil, + updatedAt: Date()) + return self.makeResult(usage: snapshot, sourceLabel: self.id) + } + + func shouldFallback(on _: Error, context _: ProviderFetchContext) -> Bool { + false + } +} + +@MainActor +@Suite(.serialized) +struct UsageStoreNeuralWattAccountRefreshTests { + @Test + func `multi-account refresh respects Neuralwatt quota rate limit`() async throws { + let recorder = NeuralWattAccountRefreshRecorder() + let store = try Self.makeStore(recorder: recorder) + let accounts = [ + ProviderTokenAccount(id: UUID(), label: "First", token: "sk-first", addedAt: 0, lastUsed: nil), + ProviderTokenAccount(id: UUID(), label: "Second", token: "sk-second", addedAt: 0, lastUsed: nil), + ] + + await store.refreshTokenAccounts(provider: .neuralwatt, accounts: accounts) + + let dates = await recorder.dates + #expect(dates.count == 2) + #expect(dates[1].timeIntervalSince(dates[0]) >= 0.95) + } + + @Test + func `cancelled delayed refresh preserves every prior account snapshot`() async throws { + let recorder = NeuralWattAccountRefreshRecorder() + let store = try Self.makeStore(recorder: recorder) + let accounts = (0..<3).map { index in + ProviderTokenAccount( + id: UUID(), + label: "Account \(index)", + token: "sk-\(index)", + addedAt: 0, + lastUsed: nil) + } + store.accountSnapshots[.neuralwatt] = accounts.map { account in + TokenAccountUsageSnapshot( + account: account, + snapshot: Self.snapshot(), + error: nil, + sourceLabel: "prior") + } + + let task = Task { @MainActor in + await store.refreshTokenAccounts(provider: .neuralwatt, accounts: accounts) + } + await recorder.waitForCount(1) + task.cancel() + await task.value + + #expect(store.accountSnapshots[.neuralwatt]?.map(\.account.id) == accounts.map(\.id)) + #expect(store.accountSnapshots[.neuralwatt]?.allSatisfy { $0.snapshot != nil } == true) + } + + private static func makeStore(recorder: NeuralWattAccountRefreshRecorder) throws -> UsageStore { + let settings = testSettingsStore( + suiteName: "UsageStoreNeuralWattAccountRefreshTests-\(UUID().uuidString)", + tokenAccountStore: InMemoryTokenAccountStore()) + settings.providerDetectionCompleted = true + settings.refreshFrequency = .manual + settings.statusChecksEnabled = false + let store = UsageStore( + fetcher: UsageFetcher(), + browserDetection: BrowserDetection(cacheTTL: 0), + settings: settings, + startupBehavior: .testing, + environmentBase: [:]) + let baseSpec = try #require(store.providerSpecs[.neuralwatt]) + let baseDescriptor = baseSpec.descriptor + let strategy = NeuralWattAccountRefreshStrategy(recorder: recorder) + store.providerSpecs[.neuralwatt] = ProviderSpec( + style: baseSpec.style, + isEnabled: baseSpec.isEnabled, + descriptor: ProviderDescriptor( + id: .neuralwatt, + metadata: baseDescriptor.metadata, + branding: baseDescriptor.branding, + tokenCost: baseDescriptor.tokenCost, + fetchPlan: ProviderFetchPlan( + sourceModes: [.auto, .api], + pipeline: ProviderFetchPipeline { _ in [strategy] }), + cli: baseDescriptor.cli), + makeFetchContext: baseSpec.makeFetchContext) + return store + } + + private static func snapshot() -> UsageSnapshot { + UsageSnapshot( + primary: RateWindow( + usedPercent: 25, + windowMinutes: nil, + resetsAt: nil, + resetDescription: nil), + secondary: nil, + updatedAt: Date()) + } +}