Skip to content

feat(quota): report A6API credit usage - #693

Open
byongshintv wants to merge 6 commits into
lidge-jun:devfrom
byongshintv:feat/a6api-quota
Open

feat(quota): report A6API credit usage#693
byongshintv wants to merge 6 commits into
lidge-jun:devfrom
byongshintv:feat/a6api-quota

Conversation

@byongshintv

@byongshintv byongshintv commented Jul 29, 2026

Copy link
Copy Markdown

Summary

  • Probe A6API billing and token-usage endpoints for canonical a6api key providers.
  • Convert provider quota units to USD using the reported subscription limit and show the consumed percentage plus exact remaining credit in the existing custom quota window UI.
  • Fail closed for non-canonical origins, redirects, missing keys, non-success responses, and malformed or zero-limit payloads.

Verification

  • bun test tests/provider-quota.test.ts (27 passed)
  • bun run typecheck
  • bun run privacy:scan
  • bun test tests/quota-bars-rows.test.ts tests/provider-workspace-rail.test.ts (19 passed)
  • Full bun run test: 5,883 passed; the initial clean-clone run had 2 module-resolution errors because GUI dependencies were not yet installed. After gui/bun install --frozen-lockfile, both affected suites passed.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed. No docs change is required: the provider quota surface already renders custom quota windows.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults. API keys are sent only to the exact A6API origin and redirects are disabled.

Summary by CodeRabbit

  • New Features
    • Added A6API credit quota probing for key-based providers, deriving remaining vs total “API credits” (USD) and displaying utilization in a custom credit window, including when the provider name is customized.
  • Bug Fixes
    • Probing now requires the canonical A6API host and an active API key; credentialed or malformed/non-consistent billing data won’t produce misleading quota reports. Quota cache is now tied to the active key.
  • Documentation
    • Documented “A6API credit quota” setup, refresh/dashboard behavior, and reset rules.
  • Tests
    • Expanded coverage for canonical-host validation, key-change behavior, and upstream request details.

Co-authored-by: OpenAI Codex <codex@openai.com>
@github-actions github-actions Bot added the enhancement New feature or request label Jul 29, 2026
@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Changes

A6API key-auth providers now query canonical billing endpoints, validate usage totals, calculate USD credit utilization, and emit a custom quota window. Cache entries include the active key identity. Tests and localized documentation cover routing, validation, request security, and configuration.

A6API Quota Support

Layer / File(s) Summary
Quota fetching and provider routing
src/providers/quota.ts
Adds canonical-host checks, concurrent billing requests, USD credit calculations, report construction, credential rejection, and key-auth A6API routing.
API-key-aware quota caching
src/providers/quota.ts
Includes the resolved API-key pool entry in the quota cache key.
Quota behavior validation
tests/provider-quota.test.ts
Tests successful reports, request headers and redirects, host and payload rejection, custom provider names, malformed unrelated providers, and cache updates after API-key changes.
A6API quota configuration documentation
docs-site/src/content/docs/{guides,ja/ko/ru/zh-cn/guides}/providers.md
Documents canonical configuration, credit-meter reporting, token-expiration handling, and invalid billing behavior across supported locales.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant maybeFetchProviderQuota
  participant fetchA6apiQuota
  participant A6API
  participant ProviderQuotaReport
  maybeFetchProviderQuota->>fetchA6apiQuota: route canonical key-auth configuration
  fetchA6apiQuota->>A6API: request subscription and token usage
  A6API-->>fetchA6apiQuota: return billing and usage payloads
  fetchA6apiQuota->>ProviderQuotaReport: calculate and return credit quota report
Loading

Suggested reviewers: lidge-jun, wibias, ingwannu

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding A6API credit usage reporting for quota tracking.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/providers/quota.ts`:
- Around line 162-167: Update the quota validation guard in the reporting
function around limitUsd, grantedUnits, usedUnits, and availableUnits to reject
negative usedUnits or availableUnits before calculating usdPerUnit and reporting
results. Preserve the existing null behavior for invalid inputs, and add a
regression case covering negative usage quantities, including negative available
units.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 2e72700a-69ab-4bda-9fbc-4e9a9ba526b8

📥 Commits

Reviewing files that changed from the base of the PR and between c7e48fb and c299ed7.

📒 Files selected for processing (2)
  • src/providers/quota.ts
  • tests/provider-quota.test.ts

Comment thread src/providers/quota.ts

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

Copy link
Copy Markdown

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: c299ed7c10

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/providers/quota.ts Outdated
Comment thread src/providers/quota.ts
Comment thread src/providers/quota.ts Outdated
Comment thread src/providers/quota.ts Outdated
Co-authored-by: OpenAI Codex <codex@openai.com>
@byongshintv

Copy link
Copy Markdown
Author

@codex review

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/providers/quota.ts`:
- Around line 167-179: Update the validation guard in the quota calculation
before deriving usdPerUnit to reject payloads where usedUnits plus
availableUnits exceeds grantedUnits, returning null like other malformed inputs.
Add a regression test for totals such as grantedUnits=100, usedUnits=80, and
availableUnits=80, ensuring no report is produced.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: b7fc2718-80e2-4411-bb5e-bc1c3fa07350

📥 Commits

Reviewing files that changed from the base of the PR and between c299ed7 and 2903177.

📒 Files selected for processing (2)
  • src/providers/quota.ts
  • tests/provider-quota.test.ts

Comment thread src/providers/quota.ts

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

Copy link
Copy Markdown

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: 2903177e9b

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/providers/quota.ts
Comment thread src/providers/quota.ts Outdated
Comment thread src/providers/quota.ts Outdated
Co-authored-by: OpenAI Codex <codex@openai.com>
@byongshintv

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 93d994f25d

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/providers/quota.ts (1)

169-180: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Reject under-accounted A6API totals in src/providers/quota.ts:169-180. total_granted should reconcile with total_used + total_available; the current guard only rejects over-accounting, so a deficit still renders a misleading credits bar and remaining amount. Fail closed on mismatch (or allow a documented fractional tolerance) and add a regression test for the under-accounted case.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/providers/quota.ts` around lines 169 - 180, Update the validation guard
in the A6API quota calculation before usdPerUnit is computed to reject totals
whenever grantedUnits does not reconcile with usedUnits plus availableUnits,
including under-accounted deficits; use only a documented fractional tolerance
if the existing data contract requires it. Preserve the current null return
behavior and add a regression test covering a granted total greater than used
plus available.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs-site/src/content/docs/guides/providers.md`:
- Around line 224-248: Sync the new A6API credit-quota section from the English
providers guide into the Japanese, Korean, Russian, and Simplified Chinese
provider guides. Preserve the canonical HTTPS endpoints, openai-chat/authMode
key requirements, quota conversion and display behavior, token-expiration note,
active-key-only probing, redirect rejection, and invalid billing-total handling;
place each section alongside the generic OpenAI-compatible endpoint content.

In `@tests/provider-quota.test.ts`:
- Around line 325-341: The test “A6API quota accepts equivalent canonical HTTPS
URLs only” currently covers only /v1 URL variants; add a table-driven case
covering the documented root URL https://api.a6api.com alongside the existing
canonical forms, and assert it is accepted with the expected quota report while
credentialed URLs remain rejected.

---

Outside diff comments:
In `@src/providers/quota.ts`:
- Around line 169-180: Update the validation guard in the A6API quota
calculation before usdPerUnit is computed to reject totals whenever grantedUnits
does not reconcile with usedUnits plus availableUnits, including under-accounted
deficits; use only a documented fractional tolerance if the existing data
contract requires it. Preserve the current null return behavior and add a
regression test covering a granted total greater than used plus available.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 16cacd64-7281-47cd-80cf-9a2f64c094e8

📥 Commits

Reviewing files that changed from the base of the PR and between 2903177 and 93d994f.

📒 Files selected for processing (3)
  • docs-site/src/content/docs/guides/providers.md
  • src/providers/quota.ts
  • tests/provider-quota.test.ts

Comment thread docs-site/src/content/docs/guides/providers.md
Comment thread tests/provider-quota.test.ts
Co-authored-by: OpenAI Codex <codex@openai.com>
@byongshintv

Copy link
Copy Markdown
Author

Addressed the outside-diff A6API reconciliation finding in 16e9b75. Used and available units must now match granted units within a documented relative floating-point tolerance of 1e-9; both over-accounted and under-accounted payloads fail closed, with regression coverage.

@byongshintv

Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 16e9b7521e

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs-site/src/content/docs/ja/guides/providers.md`:
- Around line 157-162: Update the A6API usage paragraphs in
docs-site/src/content/docs/ja/guides/providers.md:157-162,
docs-site/src/content/docs/ko/guides/providers.md:157-162,
docs-site/src/content/docs/ru/guides/providers.md:168-174, and
docs-site/src/content/docs/zh-cn/guides/providers.md:148-152 to state that token
units are converted to USD using the account’s hard credit limit, keeping the
translated guides synchronized with the English guide.

In `@src/providers/quota.ts`:
- Around line 172-179: Update the reconciliationTolerance calculation in the
quota validation flow to use strictly relative scaling: multiply
Math.abs(grantedUnits) directly by 1e-9 without flooring the scale at 1. Add a
regression test covering a sub-unit grantedUnits value such as 0.1 with a
reconciled total whose relative error exceeds the tolerance, and verify it
returns null.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 58ceb15f-14a5-421b-9d79-cd161cd964cc

📥 Commits

Reviewing files that changed from the base of the PR and between 93d994f and 16e9b75.

📒 Files selected for processing (7)
  • docs-site/src/content/docs/guides/providers.md
  • docs-site/src/content/docs/ja/guides/providers.md
  • docs-site/src/content/docs/ko/guides/providers.md
  • docs-site/src/content/docs/ru/guides/providers.md
  • docs-site/src/content/docs/zh-cn/guides/providers.md
  • src/providers/quota.ts
  • tests/provider-quota.test.ts

Comment thread docs-site/src/content/docs/ja/guides/providers.md
Comment thread src/providers/quota.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/providers/quota.ts (1)

927-929: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Suppress stale A6API reports when validation fails.

fetchA6apiQuota returns null for malformed, inconsistent, zero-limit, or non-success responses, but fetchProviderQuotaReports at src/providers/quota.ts Lines 936-981 treats every null result as a transient probe failure and restores the previous report. After one valid refresh, a later invalid A6API payload can therefore continue displaying stale credit usage instead of emitting no report, contradicting the fail-closed contract.

Make A6API terminal failures distinguishable from transient failures (or explicitly evict/suppress that provider’s previous row), and add a regression that performs a valid refresh followed by a malformed refresh with the same cache key and expects no A6API report.

As per path instructions, malformed or internally inconsistent A6API billing totals must not produce a quota report.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/providers/quota.ts` around lines 927 - 929, Update
fetchProviderQuotaReports and the fetchA6apiQuota path so malformed,
inconsistent, zero-limit, and non-success A6API responses are distinguished from
transient probe failures and do not restore or retain a previous cached report.
Ensure a valid refresh followed by a malformed refresh with the same cache key
produces no A6API report, and add the requested regression test.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/providers/quota.ts`:
- Around line 927-929: Update fetchProviderQuotaReports and the fetchA6apiQuota
path so malformed, inconsistent, zero-limit, and non-success A6API responses are
distinguished from transient probe failures and do not restore or retain a
previous cached report. Ensure a valid refresh followed by a malformed refresh
with the same cache key produces no A6API report, and add the requested
regression test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: be773c5a-5600-436e-b657-bdf451622ddf

📥 Commits

Reviewing files that changed from the base of the PR and between 16e9b75 and 7fa479f.

📒 Files selected for processing (6)
  • docs-site/src/content/docs/ja/guides/providers.md
  • docs-site/src/content/docs/ko/guides/providers.md
  • docs-site/src/content/docs/ru/guides/providers.md
  • docs-site/src/content/docs/zh-cn/guides/providers.md
  • src/providers/quota.ts
  • tests/provider-quota.test.ts

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant