-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Add usage limit detection and upgrade prompt #1750
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
Closed
charlesvien
wants to merge
3
commits into
main
from
04-20-add_usage_limit_detection_and_upgrade_prompt
Closed
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
46 changes: 46 additions & 0 deletions
46
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,46 @@ | ||
| import { useUsage } from "@features/billing/hooks/useUsage"; | ||
| import { isUsageExceeded } from "@features/billing/utils"; | ||
| import { useSettingsDialogStore } from "@features/settings/stores/settingsDialogStore"; | ||
| import { useSeat } from "@hooks/useSeat"; | ||
| import { Box, Flex, Progress, Text } from "@radix-ui/themes"; | ||
|
|
||
| export function SidebarUsageBar() { | ||
| const { isPro } = useSeat(); | ||
| const { usage } = useUsage({ enabled: !isPro }); | ||
|
|
||
| if (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 ( | ||
| <Box px="2" py="1.5" className="shrink-0 border-gray-6 border-t"> | ||
| <Flex direction="column" gap="1"> | ||
| <Flex justify="between" align="center"> | ||
| <Text size="1" className="text-gray-11"> | ||
| {exceeded ? "Limit reached" : `${Math.round(usagePercent)}% used`} | ||
| </Text> | ||
| <button | ||
| type="button" | ||
| className="bg-transparent font-medium text-[11px] text-accent-11 transition-colors hover:text-accent-12" | ||
| onClick={handleUpgrade} | ||
| > | ||
| Upgrade | ||
| </button> | ||
| </Flex> | ||
| <Progress | ||
| value={Math.min(usagePercent, 100)} | ||
| size="1" | ||
| color={exceeded ? "red" : undefined} | ||
| /> | ||
| </Flex> | ||
| </Box> | ||
| ); | ||
| } |
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" | ||
| onEscapeKeyDown={(e) => e.preventDefault()} | ||
| onInteractOutside={(e) => e.preventDefault()} | ||
| > | ||
| <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 }; | ||
| } |
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
| import { useUsageLimitStore } from "@features/billing/stores/usageLimitStore"; | ||
| import { isUsageExceeded } from "@features/billing/utils"; | ||
| import { useSessionStore } from "@features/sessions/stores/sessionStore"; | ||
| import { useFeatureFlag } from "@hooks/useFeatureFlag"; | ||
| import { useSeat } from "@hooks/useSeat"; | ||
| import { useEffect, useRef } from "react"; | ||
| import { useUsage } from "./useUsage"; | ||
|
|
||
| export function useUsageLimitDetection() { | ||
| const billingEnabled = useFeatureFlag("posthog-code-billing"); | ||
| const { isPro } = useSeat(); | ||
| const { usage } = useUsage({ enabled: billingEnabled && !isPro }); | ||
| const hasAlertedRef = useRef(false); | ||
|
|
||
| useEffect(() => { | ||
| if (!billingEnabled || 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, isPro, usage]); | ||
| } |
23 changes: 23 additions & 0 deletions
23
apps/code/src/renderer/features/billing/stores/usageLimitStore.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,23 @@ | ||
| import { create } from "zustand"; | ||
|
|
||
| type UsageLimitContext = "mid-task" | "idle"; | ||
|
|
||
| interface UsageLimitState { | ||
| isOpen: boolean; | ||
| context: UsageLimitContext | null; | ||
| } | ||
|
|
||
| interface UsageLimitActions { | ||
| show: (context: UsageLimitContext) => void; | ||
| hide: () => void; | ||
| } | ||
|
|
||
| type UsageLimitStore = UsageLimitState & UsageLimitActions; | ||
|
|
||
| export const useUsageLimitStore = create<UsageLimitStore>()((set) => ({ | ||
| isOpen: false, | ||
| context: null, | ||
|
|
||
| show: (context) => set({ isOpen: true, context }), | ||
| hide: () => set({ isOpen: false, context: null }), | ||
| })); |
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,11 @@ | ||
| interface UsageLimitCheck { | ||
| sustained: { exceeded: boolean }; | ||
| burst: { exceeded: boolean }; | ||
| is_rate_limited: boolean; | ||
| } | ||
|
|
||
| export function isUsageExceeded(usage: UsageLimitCheck): boolean { | ||
| return ( | ||
| usage.is_rate_limited || usage.sustained.exceeded || usage.burst.exceeded | ||
| ); | ||
| } |
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
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.