Repository navigation
feat(fx): show Vercel Token Pass credit under Usage → Limits #40422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kaeferfreund
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
kaeferfreund:feat/fx-token-pass-limits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−14
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { describe, expect, it } from "@effect/vitest"; | ||
| import * as Option from "effect/Option"; | ||
|
|
||
| import { fxCreditsToLimits } from "./usageLimits.ts"; | ||
|
|
||
| const checkedAt = "2026-11-21T16:00:00.000Z"; | ||
| const limits = (credits: unknown) => | ||
| fxCreditsToLimits({ | ||
| credits: Option.some(typeof credits === "string" ? credits : JSON.stringify(credits)), | ||
| checkedAt, | ||
| team: "example-team", | ||
| }); | ||
| // `fx credits --json` from fx 0.0.17 (src/core/output/output_contracts.zig). | ||
| const credits = (tokenPass: unknown) => ({ | ||
| kind: "credits", | ||
| balance: "9.731069815", | ||
| used: "125.268930185", | ||
| plan: null, | ||
| token_pass: tokenPass, | ||
| }); | ||
|
|
||
| describe("fx Token Pass limits", () => { | ||
| it("reads the credit left of the $150 a pass refills to", () => { | ||
| expect( | ||
| limits(credits({ remaining: "79.92906", resets_at: "2026-12-05T08:00:00.000Z" })), | ||
| ).toEqual({ | ||
| checkedAt, | ||
| windows: [ | ||
| { | ||
| id: "token_pass", | ||
| kind: "monthly", | ||
| label: "Token Pass", | ||
| usedPercent: expect.closeTo(46.71, 2), | ||
| resetsAt: "2026-12-05T08:00:00.000Z", | ||
| }, | ||
| ], | ||
| credentialFingerprint: "vercel-team:example-team", | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["a used-up pass", { remaining: "0", resets_at: "2026-12-05T08:00:00.000Z" }, 100], | ||
| ["an overdrawn pass", { remaining: "-0.42", resets_at: "2026-12-05T08:00:00.000Z" }, 100], | ||
| ["an untouched pass", { remaining: "150", resets_at: "2026-12-05T08:00:00.000Z" }, 0], | ||
| ["more credit than a pass refills to", { remaining: "180.5" }, 0], | ||
| ])("reports %s", (_label, tokenPass, usedPercent) => { | ||
| expect(limits(credits(tokenPass)).windows.map((window) => window.usedPercent)).toEqual([ | ||
| usedPercent, | ||
| ]); | ||
| }); | ||
|
|
||
| it("leaves out a reset time fx did not report and an account for an API key without a team", () => { | ||
| const withoutTeam = fxCreditsToLimits({ | ||
| credits: Option.some(JSON.stringify(credits({ remaining: "5", resets_at: null }))), | ||
| checkedAt, | ||
| team: undefined, | ||
| }); | ||
| expect(withoutTeam.windows[0]).not.toHaveProperty("resetsAt"); | ||
| expect(withoutTeam).not.toHaveProperty("credentialFingerprint"); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["a team without an active pass", credits(null)], | ||
| ["an fx older than Token Pass", { kind: "credits", balance: "1", used: "2", plan: null }], | ||
| ])("shows no limits for %s", (_label, output) => { | ||
| expect(limits(output)).toEqual({ | ||
| checkedAt, | ||
| windows: [], | ||
| unavailable: { reason: "unsupported" }, | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["a credit AI Gateway could not read", credits({ remaining: null })], | ||
| ["an amount that is not a number", credits({ remaining: "1.2.3" })], | ||
| ["a credits error", { kind: "credits", error: "failed to fetch credits from gateway" }], | ||
| ["output that is not JSON", "[credits] balance=1"], | ||
| ])("keeps the earlier reading for %s", (_label, output) => { | ||
| expect(limits(output).unavailable?.reason).toBe("probeFailed"); | ||
| }); | ||
|
|
||
| it("keeps the earlier reading when `fx credits` fails", () => { | ||
| expect( | ||
| fxCreditsToLimits({ credits: Option.none(), checkedAt, team: "example-team" }).unavailable, | ||
| ).toEqual({ reason: "probeFailed", message: "fx could not read the Token Pass credit." }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import type { ServerProviderUsageLimits } from "@t3tools/contracts"; | ||
| import * as DateTime from "effect/DateTime"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Schema from "effect/Schema"; | ||
| import { | ||
| clampPercent, | ||
| makeUnavailableUsageLimits, | ||
| makeUsageLimits, | ||
| } from "@t3tools/provider-core/server/usageLimits"; | ||
|
|
||
| // Vercel reports only the credit left. A pass refills to this each billing period, and a pass | ||
| // bought mid-period starts with a prorated share, so its first period reads as partly used. | ||
| const TOKEN_PASS_PERIOD_CREDIT_USD = 150; | ||
|
|
||
| /** | ||
| * `fx credits --json`, fx 0.0.17+. `token_pass` is null for a team without an | ||
| * active pass and absent on an older fx. `remaining` is US dollars, or null | ||
| * when AI Gateway could not read the credit. | ||
| */ | ||
| const FxCredits = Schema.fromJsonString( | ||
| Schema.Struct({ | ||
| error: Schema.optional(Schema.String), | ||
| token_pass: Schema.optional( | ||
| Schema.NullOr( | ||
| Schema.Struct({ | ||
| remaining: Schema.optional(Schema.NullOr(Schema.String)), | ||
| resets_at: Schema.optional(Schema.NullOr(Schema.String)), | ||
| }), | ||
| ), | ||
| ), | ||
| }), | ||
| ); | ||
| const decodeFxCredits = Schema.decodeUnknownOption(FxCredits); | ||
|
|
||
| /** The Token Pass credit of the Vercel team fx is signed in to, as one Limits window. */ | ||
| export function fxCreditsToLimits(input: { | ||
| /** Stdout of a successful `fx credits --json`, or none when it failed. */ | ||
| readonly credits: Option.Option<string>; | ||
| readonly checkedAt: string; | ||
| /** Every login on a team spends the same pass, so the team names the account. */ | ||
| readonly team: string | undefined; | ||
| }): ServerProviderUsageLimits { | ||
| const { checkedAt } = input; | ||
| const probeFailed = makeUnavailableUsageLimits({ | ||
| checkedAt, | ||
| reason: "probeFailed", | ||
| message: "fx could not read the Token Pass credit.", | ||
| }); | ||
| const credits = Option.getOrUndefined(Option.flatMap(input.credits, decodeFxCredits)); | ||
| if (credits === undefined || credits.error !== undefined) return probeFailed; | ||
| if (credits.token_pass === undefined || credits.token_pass === null) { | ||
| return makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" }); | ||
| } | ||
| const remaining = credits.token_pass.remaining?.trim(); | ||
| // The amounts fx passes on: an overdrawn pass is negative, and a bare point is allowed. | ||
| if (!remaining || !/^-?(\d+\.?\d*|\.\d+)$/.test(remaining)) return probeFailed; | ||
| const reset = DateTime.make(credits.token_pass.resets_at ?? ""); | ||
| return { | ||
| ...makeUsageLimits({ | ||
| checkedAt, | ||
| windows: [ | ||
| { | ||
| id: "token_pass", | ||
| kind: "monthly", | ||
| label: "Token Pass", | ||
| usedPercent: clampPercent(100 - (Number(remaining) / TOKEN_PASS_PERIOD_CREDIT_USD) * 100), | ||
| ...(Option.isSome(reset) ? { resetsAt: DateTime.formatIso(reset.value) } : {}), | ||
| }, | ||
| ], | ||
| }), | ||
| ...(input.team ? { credentialFingerprint: `vercel-team:${input.team}` } : {}), | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.