Skip to content

Add Perplexity provider#449

Open
BeelixGit wants to merge 3 commits intosteipete:mainfrom
BeelixGit:feature/perplexity-provider
Open

Add Perplexity provider#449
BeelixGit wants to merge 3 commits intosteipete:mainfrom
BeelixGit:feature/perplexity-provider

Conversation

@BeelixGit
Copy link

Worked with Claude Code to add Perplexity Max usage tracking to CodexBar. Seems to work for me but I am sure there are things I missed.

What this adds

  • Tracks recurring (monthly), promotional (bonus), and on-demand purchased credits from Perplexity Pro/Max plans
  • Fetches from /rest/billing/credits using a browser session cookie (auto-imported from Chrome/Chromium on macOS, or set manually via cookie header / PERPLEXITY_SESSION_TOKEN env var)
  • Waterfall attribution: usage is charged against recurring first, then purchased, then promotional — matching how Perplexity actually bills
  • Plan detection (Free / Pro / Max) inferred from the recurring credit pool size
  • All 3 bars always shown; empty pools render as fully depleted rather than full
  • 9 unit tests covering JSON parsing, waterfall attribution, expired grant exclusion, plan name inference, and UsageSnapshot conversion

Test plan

  • Build: bash Scripts/compile_and_run.sh
  • With a valid Perplexity session cookie all 3 bars populate correctly
  • Without credentials provider is hidden / shows login prompt
  • Unit tests: swift test --filter PerplexityUsageFetcherTests
  • Expired promotional grants excluded from the promo bar
  • Plan name (Pro / Max) appears in the account badge

🤖 Generated with Claude Code

@gitguardian
Copy link

gitguardian bot commented Feb 27, 2026

️✅ There are no secrets present in this pull request anymore.

If these secrets were true positive and are still valid, we highly recommend you to revoke them.
While these secrets were previously flagged, we no longer have a reference to the
specific commits where they were detected. Once a secret has been leaked into a git
repository, you should consider it compromised, even if it was deleted immediately.
Find here more information about risks.


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c932edbee2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +66 to +68
let primaryPercent = recurringTotal > 0
? min(100, max(0, recurringUsed / recurringTotal * 100))
: 0.0

Choose a reason for hiding this comment

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

P2 Badge Mark zero recurring credits as fully used

When recurringTotal is 0, this branch sets primaryPercent to 0.0, which renders the recurring bar as full even though there are no recurring credits available. This affects free/expired accounts (or any response without recurring grants) and contradicts the empty-pool handling used for promo/purchased bars, so the UI reports the recurring pool in the opposite state.

Useful? React with 👍 / 👎.

Copy link
Author

Choose a reason for hiding this comment

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

Good catch — this was fixed in a subsequent commit. The original code did set primaryPercent to 0.0 for zero recurring credits. It now correctly uses 100.0, matching the empty-pool treatment for promo/purchased bars. Test toUsageSnapshotZeroRecurringBarIsFullyDepleted covers this.

Comment on lines +7 to +10
let raw = environment["PERPLEXITY_SESSION_TOKEN"]
?? environment["perplexity_session_token"]
?? environment["PERPLEXITY_COOKIE"]
return self.cleaned(raw)

Choose a reason for hiding this comment

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

P2 Badge Parse PERPLEXITY_COOKIE as cookie header

This reader accepts PERPLEXITY_COOKIE but returns it verbatim as a session token; if users provide a full cookie header (which the variable name strongly implies), the fetcher later wraps it again as __Secure-next-auth.session-token=<value>, producing a malformed Cookie header and authentication failures. PERPLEXITY_COOKIE should be normalized through the same token-extraction path used for manual cookie input.

Useful? React with 👍 / 👎.

Copy link
Author

Choose a reason for hiding this comment

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

Good catch — this was fixed in a subsequent commit. The original code did return PERPLEXITY_COOKIE verbatim via nil-coalescing. It now handles it separately, piping through PerplexityCookieHeader.override(from:)?.token to extract just the bare session token before returning it.

@BeelixGit BeelixGit force-pushed the feature/perplexity-provider branch 4 times, most recently from fabc673 to 56d03e5 Compare March 5, 2026 20:01
@warrendt
Copy link

When are you going to merge this one @BeelixGit Keen to use it.

John Budnick and others added 3 commits March 23, 2026 19:23
Tracks usage-based API credits from Perplexity Pro/Max plans via the
/rest/billing/credits endpoint using a browser session cookie.

- Three-tier display: recurring credits (monthly), bonus credits
  (promotional grants), and purchased on-demand credits
- Infers plan name (Free/Pro/Max) from recurring credit pool size
- Cookie auth: auto browser import from Chrome/Chromium, manual paste,
  or PERPLEXITY_SESSION_TOKEN / PERPLEXITY_COOKIE env var fallback
- Waterfall attribution: recurring → purchased → promotional
- Empty credit pools render as depleted bars, not full ones
- Login flow opens perplexity.ai/signin with the standard key icon
- 10 unit tests covering parsing, waterfall logic, plan inference,
  empty-pool rendering, and UsageSnapshot conversion

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…warning

- Cache Perplexity session cookies via CookieHeaderCache (matching
  Claude/Cursor pattern): try cache before browser import, cache on
  success, clear + retry on 401/403, preserve cache on transient errors
- Fix PerplexityCreditGrant.expiresAtTs to be optional — the API returns
  null for purchased grants, causing parse failures
- Remove deprecated kSecUseAuthenticationUIFail (macOS 14+ only needs
  LAContext.interactionNotAllowed)
- Add debug logging for parse failures (raw response preview)
- Add 6 tests for cookie cache behavior

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Perplexity now sends purchased credits as a credit_grant with
type="purchased" instead of (or in addition to) the top-level
current_period_purchased_cents field. Take the larger of the two
sources to avoid double-counting while catching either path.

Without this fix, 40k purchased credits were invisible to the
waterfall calculation, causing bonus credits to show 100% used
when they still had ~42% remaining.
@BeelixGit BeelixGit force-pushed the feature/perplexity-provider branch from f131b52 to 0e218b9 Compare March 23, 2026 23:30
@BeelixGit
Copy link
Author

Just rebased onto latest main — all conflicts resolved, build is clean, and tests are passing (284 related tests + 24 Perplexity-specific). Ready for review whenever you get a chance. @ratulsarna

@BeelixGit
Copy link
Author

Hey @warrendt — thanks for the interest! I'm the PR author but I don't have merge access — that's up to the repo maintainers. I'm pretty new to open source and still figuring out how all of this works honestly (been leaning on Claude Code for most of it). Just rebased the branch so it's conflict-free and ready to go on their end. Hopefully it gets picked up soon!

@BeelixGit
Copy link
Author

I have had Claude Code do near everything on this insofar as even checking and replying to the comments here so If I am doing anything the wrong way or should do differently please just let me know

@warrendt
Copy link

I have had Claude Code do near everything on this insofar as even checking and replying to the comments here so If I am doing anything the wrong way or should do differently please just let me know

Sweet, I guess we just wait for @steipete to do the merge then since it passed all the checks. I have built and run your version and it works great.

@ratulsarna
Copy link
Collaborator

ratulsarna commented Mar 26, 2026

Opened a superseding PR here: #606

Thanks again @BeelixGit for the original Perplexity provider work and all the follow-up fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants