-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Add usage limit detection and upgrade prompt #1791
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
charlesvien
wants to merge
16
commits into
main
Choose a base branch
from
04-21-extract_shared_isusageexceeded_util_and_gate_usage_polling
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.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d1e0a8e
Add usage limit detection and upgrade prompt
charlesvien 506c890
Extract shared isUsageExceeded util and gate usage polling
charlesvien e712d26
Invalidate gateway plan cache after billing changes
charlesvien f135a0a
Update seatStore.test.ts
charlesvien 88f2f27
Fix review feedback for billing usage detection
charlesvien 32e0a92
Remove redundant billing flag guard from SidebarContent
charlesvien 2b73634
Preserve usage limit context during dialog exit animation
charlesvien 73e6009
Update usageLimitStore.test.ts
charlesvien 2fb9bbd
Polish sidebar usage bar and fix billing flag race
charlesvien be008c1
Wait for seat data before rendering billing UI
charlesvien 1b0d503
Clean up dead code and improve configs
charlesvien 4a572f9
lint
charlesvien 0a9737d
Update seatStore.test.ts
charlesvien 081bf3a
Revert "Update seatStore.test.ts"
charlesvien c2a3f9a
Gate seat store actions on billing feature flag
charlesvien ed3eff5
Remove store-level billing flag guards to fix race condition
charlesvien 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
59 changes: 59 additions & 0 deletions
59
apps/code/src/renderer/features/billing/components/SidebarUsageBar.tsx
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,59 @@ | ||
| import { useUsage } from "@features/billing/hooks/useUsage"; | ||
| import { isUsageExceeded } from "@features/billing/utils"; | ||
| import { useSettingsDialogStore } from "@features/settings/stores/settingsDialogStore"; | ||
| import { useFeatureFlag } from "@hooks/useFeatureFlag"; | ||
| import { useSeat } from "@hooks/useSeat"; | ||
| import { Circle } from "@phosphor-icons/react"; | ||
| import { BILLING_FLAG } from "@shared/constants"; | ||
|
|
||
| export function SidebarUsageBar() { | ||
| const billingEnabled = useFeatureFlag(BILLING_FLAG); | ||
| const { seat, isPro } = useSeat(); | ||
| const seatLoaded = seat !== null; | ||
| const { usage } = useUsage({ | ||
| enabled: billingEnabled && seatLoaded && !isPro, | ||
| }); | ||
|
|
||
| if (!billingEnabled || !seatLoaded || isPro || !usage) return null; | ||
|
|
||
| const usagePercent = Math.max( | ||
| usage.sustained.used_percent, | ||
| usage.burst.used_percent, | ||
| ); | ||
| const exceeded = isUsageExceeded(usage); | ||
|
|
||
| const handleUpgrade = () => { | ||
| useSettingsDialogStore.getState().open("plan-usage"); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="shrink-0 border-gray-6 border-t px-3 py-3"> | ||
| <div className="flex items-center justify-between"> | ||
| <span className="font-medium text-gray-11 text-xs"> | ||
| Free plan | ||
| <Circle | ||
| size={4} | ||
| weight="fill" | ||
| className="mx-1.5 inline text-gray-8" | ||
| /> | ||
| <span className="font-normal text-gray-10"> | ||
| {exceeded ? "Limit reached" : `${Math.round(usagePercent)}% used`} | ||
| </span> | ||
| </span> | ||
| <button | ||
| type="button" | ||
| className="bg-transparent font-medium text-accent-11 text-xs transition-colors hover:text-accent-12" | ||
| onClick={handleUpgrade} | ||
| > | ||
| Upgrade | ||
| </button> | ||
| </div> | ||
| <div className="mt-2 h-2.5 w-full overflow-hidden rounded-full bg-gray-4"> | ||
| <div | ||
| className={`h-full rounded-full transition-all ${exceeded ? "bg-red-9" : "bg-accent-9"}`} | ||
| style={{ width: `${Math.min(Math.ceil(usagePercent), 100)}%` }} | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
47 changes: 47 additions & 0 deletions
47
apps/code/src/renderer/features/billing/components/UsageLimitModal.tsx
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,47 @@ | ||
| import { useUsageLimitStore } from "@features/billing/stores/usageLimitStore"; | ||
| import { useSettingsDialogStore } from "@features/settings/stores/settingsDialogStore"; | ||
| import { WarningCircle } from "@phosphor-icons/react"; | ||
| import { Button, Dialog, Flex, Text } from "@radix-ui/themes"; | ||
|
|
||
| export function UsageLimitModal() { | ||
| const isOpen = useUsageLimitStore((s) => s.isOpen); | ||
| const context = useUsageLimitStore((s) => s.context); | ||
| const hide = useUsageLimitStore((s) => s.hide); | ||
|
|
||
| const handleUpgrade = () => { | ||
| hide(); | ||
| useSettingsDialogStore.getState().open("plan-usage"); | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog.Root open={isOpen}> | ||
| <Dialog.Content | ||
| maxWidth="400px" | ||
| onInteractOutside={(e) => e.preventDefault()} | ||
| onEscapeKeyDown={hide} | ||
| > | ||
| <Flex direction="column" gap="3"> | ||
| <Flex align="center" gap="2"> | ||
| <WarningCircle size={20} weight="bold" color="var(--red-9)" /> | ||
| <Dialog.Title className="mb-0">Usage limit reached</Dialog.Title> | ||
| </Flex> | ||
| <Dialog.Description> | ||
| <Text size="2" color="gray"> | ||
| {context === "mid-task" | ||
| ? "You've hit your free plan usage limit. Your current task can't continue until usage resets or you upgrade to Pro." | ||
| : "You've reached your free plan usage limit. Upgrade to Pro for unlimited usage."} | ||
| </Text> | ||
| </Dialog.Description> | ||
| <Flex justify="end" gap="3" mt="2"> | ||
| <Button type="button" variant="soft" color="gray" onClick={hide}> | ||
| Not now | ||
| </Button> | ||
| <Button type="button" onClick={handleUpgrade}> | ||
| View upgrade options | ||
| </Button> | ||
| </Flex> | ||
| </Flex> | ||
| </Dialog.Content> | ||
| </Dialog.Root> | ||
| ); | ||
| } |
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,17 @@ | ||
| import { useTRPC } from "@renderer/trpc"; | ||
| import { useRendererWindowFocusStore } from "@stores/rendererWindowFocusStore"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
|
|
||
| const USAGE_REFETCH_INTERVAL_MS = 60_000; | ||
|
|
||
| export function useUsage({ enabled = true }: { enabled?: boolean } = {}) { | ||
| const trpc = useTRPC(); | ||
| const focused = useRendererWindowFocusStore((s) => s.focused); | ||
| const { data: usage, isLoading } = useQuery({ | ||
| ...trpc.llmGateway.usage.queryOptions(), | ||
| enabled, | ||
| refetchInterval: focused && enabled ? USAGE_REFETCH_INTERVAL_MS : false, | ||
| refetchIntervalInBackground: false, | ||
| }); | ||
| return { usage: usage ?? null, isLoading }; | ||
| } |
38 changes: 38 additions & 0 deletions
38
apps/code/src/renderer/features/billing/hooks/useUsageLimitDetection.ts
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,38 @@ | ||
| import { useUsageLimitStore } from "@features/billing/stores/usageLimitStore"; | ||
| import { isUsageExceeded } from "@features/billing/utils"; | ||
| import { useSessionStore } from "@features/sessions/stores/sessionStore"; | ||
| import { useSeat } from "@hooks/useSeat"; | ||
| import { useEffect, useRef } from "react"; | ||
| import { useUsage } from "./useUsage"; | ||
|
|
||
| export function useUsageLimitDetection(billingEnabled: boolean) { | ||
| const { seat, isPro } = useSeat(); | ||
| const seatLoaded = seat !== null; | ||
| const { usage } = useUsage({ | ||
| enabled: billingEnabled && seatLoaded && !isPro, | ||
| }); | ||
| const hasAlertedRef = useRef(false); | ||
|
|
||
| useEffect(() => { | ||
| if (!billingEnabled || !seatLoaded || isPro || !usage) return; | ||
|
|
||
| const exceeded = isUsageExceeded(usage); | ||
|
|
||
| if (exceeded && !hasAlertedRef.current) { | ||
| hasAlertedRef.current = true; | ||
|
|
||
| const sessions = useSessionStore.getState().sessions; | ||
| const hasActiveSession = Object.values(sessions).some( | ||
| (s) => s.status === "connected" && s.isPromptPending, | ||
| ); | ||
|
|
||
| useUsageLimitStore | ||
| .getState() | ||
| .show(hasActiveSession ? "mid-task" : "idle"); | ||
| } | ||
|
|
||
| if (!exceeded) { | ||
| hasAlertedRef.current = false; | ||
| } | ||
| }, [billingEnabled, seatLoaded, isPro, usage]); | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'm seeing this condition replicated in a 5+ spots, it might be worth to extract these checks into their own helper.