feat(usage): show OpenCode Go, Cursor, and Grok subscription limits #12115
Mergedmaria-rcksmerged 5 commits into
main fromrefs/pull/12115/headmerged by maria-rcks View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/provider/Layers/cursorUsageLimits.ts lines 104 to 106Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +73,21 @@73+ // An explicit API key can name a different account from the stored login.74+ if (!token && environment.CURSOR_API_KEY?.trim()) {75+ return makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" });76+ }77+ if (!token) {78+ const home =79+ (platform === "win32" ? environment.USERPROFILE : environment.HOME) || NodeOS.homedir();80+ const directory =81+ platform === "win32"82+ ? path.join(environment.APPDATA || path.join(home, "AppData", "Roaming"), "Cursor")83+ : platform === "darwin"84+ ? path.join(home, ".cursor")85+ : path.join(environment.XDG_CONFIG_HOME || path.join(home, ".config"), "cursor");86+ const credentials = yield* fs.readFileString(path.join(directory, "auth.json")).pipe(87+ Effect.catchTag("PlatformError", (error) =>88+ error.reason._tag === "NotFound" ? Effect.succeed("{}") : Effect.fail(error),89+ ),90+ Effect.flatMap(decodeCredentials),91+ );92+ token = credentials.accessToken?.trim();93+ }
- apps/server/src/provider/Layers/openCodeUsageLimits.ts line 58Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +39,21 @@39+40+ return yield* Effect.gen(function* () {41+ const fs = yield* FileSystem.FileSystem;42+ const path = yield* Path.Path;43+ const env = input.environment;44+ const dataHome =45+ env.XDG_DATA_HOME ||46+ path.join(env.HOME || env.USERPROFILE || NodeOS.homedir(), ".local", "share");47+ const authPath = path.join(dataHome, "opencode", "auth.json");48+ const contents =49+ env.OPENCODE_AUTH_CONTENT ||50+ (yield* fs51+ .readFileString(authPath)52+ .pipe(53+ Effect.catch((error) =>54+ error.reason._tag === "NotFound" ? Effect.succeed("{}") : Effect.fail(error),55+ ),56+ ));57+ const auth = yield* decodeAuthFile(contents);58+ const apiAuth = decodeApiAuth(auth["opencode-go"]);59+ // OpenCode overlays stored API credentials after environment credentials.
- @@ -0,0 +11,21 @@11+12+import {13+ clampPercent,14+ makeUnavailableUsageLimits,15+ makeUsageLimits,16+} from "../providerUsageLimits.ts";17+18+const AuthFile = Schema.Struct({ "opencode-go": Schema.optionalKey(Schema.Unknown) });19+const ApiAuth = Schema.Struct({ type: Schema.Literal("api"), key: Schema.String });20+const decodeAuthFile = Schema.decodeEffect(Schema.fromJsonString(AuthFile));21+const decodeApiAuth = Schema.decodeUnknownOption(ApiAuth);22+const UsageWindow = Schema.Struct({23+ percent: Schema.Finite,24+ resetsAt: Schema.DateTimeUtcFromString,25+});26+const UsageResponse = Schema.Struct({27+ usage: Schema.Struct({ rolling: UsageWindow, weekly: UsageWindow, monthly: UsageWindow }),28+});29+30+/** External OpenCode servers own their credentials; never read the host's account for them. */31+export const readOpenCodeGoUsageLimits = Effect.fn("readOpenCodeGoUsageLimits")(function* (input: {@@ -0,0 +43,21 @@43+ ["totalPercentUsed", "Monthly"],44+ ["autoPercentUsed", "Monthly · Auto"],45+ ["apiPercentUsed", "Monthly · API"],46+ ] as const) {47+ const usedPercent = response.planUsage[key];48+ if (usedPercent === undefined || !Number.isFinite(usedPercent)) continue;49+ windows.push({50+ id: key,51+ kind: "monthly",52+ label,53+ usedPercent: clampPercent(usedPercent),54+ ...(resetsAt ? { resetsAt } : {}),55+ });56+ }57+ }58+ return windows.length > 059+ ? makeUsageLimits({ checkedAt, windows })60+ : makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" });61+}62+63+export const readCursorUsageLimits = Effect.fn("readCursorUsageLimits")(function* (
- apps/server/src/provider/Layers/cursorUsageLimits.ts lines 109 to 116Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +96,21 @@96+ ? path.join(home, ".cursor")97+ : path.join(environment.XDG_CONFIG_HOME || path.join(home, ".config"), "cursor");98+ const credentials = yield* fs.readFileString(path.join(directory, "auth.json")).pipe(99+ Effect.catchTags({100+ PlatformError: (error) =>101+ error.reason._tag === "NotFound" ? Effect.succeed("{}") : Effect.fail(error),102+ }),103+ Effect.flatMap(decodeCredentials),104+ );105+ token = credentials.accessToken?.trim();106+ }107+ if (!token) return makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" });108+ const client = yield* HttpClient.HttpClient;109+ const endpoint = (110+ settings.apiEndpoint.trim() ||111+ environment.CURSOR_API_ENDPOINT?.trim() ||112+ "https://api2.cursor.sh"113+ ).replace(/\/$/, "");114+ const response = yield* client.execute(115+ HttpClientRequest.post(`${endpoint}/aiserver.v1.DashboardService/GetCurrentPeriodUsage`).pipe(116+ HttpClientRequest.bearerToken(token),docs/user/usage.md lines 73 to 74OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -69,3 +69,6 @@6969 closes when you dismiss it or send your next message. It uses the same snapshot as **Usage → Limits**, so it does not run the agent or refresh7070 anything. The command is offered only for providers that appear under **Usage → Limits**.717172+OpenCode Go reports its session, weekly, and monthly allowance when OpenCode runs locally in73+the environment; external OpenCode servers do not expose their account limits. Cursor reports74+its monthly allowance, including separate Auto and API usage, using a file-based CLI login or
maria-rcks changed the title
- @@ -90,3 +93,4 @@9093 const textGeneration = yield* makeGrokTextGeneration(effectiveConfig, processEnv);91949295 const checkProvider = checkGrokProviderStatus(effectiveConfig, processEnv, cwd).pipe(96+ Effect.flatMap((snapshot) =>@@ -0,0 +117,21 @@117+ }),118+ ));119+ const credentials = yield* decodeCredentials(contents);120+ // Never pick an arbitrary account from other deployments stored in the same file.121+ const credential =122+ credentials["https://auth.x.ai::b1a00492-073a-47ea-816f-4c329264a828"] ??123+ credentials["https://accounts.x.ai/sign-in"];124+ const token = credential?.auth_mode === "api_key" ? undefined : credential?.key?.trim();125+ if (!token) return makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" });126+ const client = yield* HttpClient.HttpClient;127+ const response = yield* client.execute(128+ HttpClientRequest.get("https://cli-chat-proxy.grok.com/v1/billing?format=credits").pipe(129+ HttpClientRequest.bearerToken(token),130+ ),131+ );132+ const body = yield* HttpClientResponse.schemaBodyJson(GrokUsageResponse)(133+ yield* HttpClientResponse.filterStatusOk(response),134+ );135+ return grokUsageResponseToLimits(body, checkedAt);136+ }).pipe(137+ Effect.timeout("10 seconds"),
maria-rcks merged thiseff44be
maria-rcks closed this
Sign in to join this conversation.




Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — This PR adds a substantial, automatically executed quota-reporting capability across three providers, including credential-file access and authenticated network requests. Its runtime and security-sensitive surface is broader than a bounded additive change and merits human review.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).