Repository navigation
feat(billing): require a paid plan or prepaid credits for Chat #9551
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
Merged
Merged
Changes from all commits
Commits
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
43 changes: 43 additions & 0 deletions
43
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat-plan-gate.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,43 @@ | ||
| 'use client' | ||
|
|
||
| import { useCallback } from 'react' | ||
| import { toast } from '@sim/emcn' | ||
| import { useRouter } from 'next/navigation' | ||
| import { useSession } from '@/lib/auth/client' | ||
| import { CHAT_PLAN_REQUIRED_MESSAGE } from '@/lib/billing/chat-plan' | ||
| import { buildUpgradeHref } from '@/lib/billing/upgrade-reasons' | ||
| import { canManageWorkspaceBilling } from '@/lib/billing/workspace-permissions' | ||
| import { useOptionalWorkspaceHostContext } from '@/app/workspace/[workspaceId]/providers/workspace-host-provider' | ||
|
|
||
| /** | ||
| * Stops a send from a workspace whose payer may not use Chat before it starts a turn that run | ||
| * admission would refuse. A member who can manage the workspace's billing goes to its upgrade | ||
| * page; anyone else is told who can upgrade. The returned callback reports whether the send was | ||
| * stopped. | ||
| * | ||
| * The verdict is the host context's `features.chat`, which the server resolves with the same | ||
| * check admission runs. A surface without a workspace host (an organization conversation), or a | ||
| * context that predates the flag, is not stopped here; admission still answers it with the | ||
| * upgrade card. | ||
| */ | ||
| export function useChatPlanGate(): () => boolean { | ||
| const hostContext = useOptionalWorkspaceHostContext() | ||
| const { data: session } = useSession() | ||
| const viewerId = session?.user?.id | ||
| const router = useRouter() | ||
|
|
||
| return useCallback(() => { | ||
| if (!hostContext || hostContext.features?.chat !== false) return false | ||
|
|
||
| if (canManageWorkspaceBilling(hostContext, viewerId)) { | ||
| router.push(buildUpgradeHref(hostContext.workspace.id)) | ||
| } else { | ||
| toast.error( | ||
| hostContext.hostOrganizationId | ||
| ? `${CHAT_PLAN_REQUIRED_MESSAGE} Contact an organization admin to upgrade this workspace’s plan.` | ||
| : `${CHAT_PLAN_REQUIRED_MESSAGE} Ask the workspace owner to upgrade this workspace’s plan.` | ||
| ) | ||
| } | ||
| return true | ||
| }, [hostContext, viewerId, router]) | ||
| } |
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
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,5 @@ | ||
| /** The upgrade card's `reason` for a payer whose plan does not include Chat. */ | ||
| export const CHAT_PLAN_REQUIRED_REASON = 'plan_required' as const | ||
|
|
||
| /** Lead sentence of every refusal that withholds Chat from a plan. */ | ||
| export const CHAT_PLAN_REQUIRED_MESSAGE = 'Chat is included on paid plans.' |
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,40 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import { getCreditBalanceForEntity } from '@/lib/billing/credits/balance' | ||
| import { isFree } from '@/lib/billing/plan-helpers' | ||
| import type { UsageUpgradePayer } from '@/lib/billing/usage-upgrade' | ||
| import { isBillingEnabled, isHosted } from '@/lib/core/config/env-flags' | ||
|
|
||
| const logger = createLogger('ChatAccess') | ||
|
|
||
| /** | ||
| * Whether a payer may use Chat (talking to Sim). Every turn spends hosted model credits, so a | ||
| * hosted deployment that bills withholds Chat from a payer on the free plan — unless that payer | ||
| * holds prepaid credits, which an admin grants to customers billed outside a subscription. | ||
| * | ||
| * The plan is the payer's entitled subscription (active or past due), so a canceled | ||
| * plan reads as free; a blocked payer is refused by the usage check every caller runs next. | ||
| * | ||
| * This is the single verdict behind every Chat plan gate: run admission, chat titles, scheduled | ||
| * job dispatch, Copilot key admission, and the workspace host context the composer reads, so the | ||
| * composer never offers a send that admission will refuse. A balance that cannot be read admits | ||
| * the payer: a database blip must not lock a paying customer out, and a free payer stays bounded | ||
| * by its usage limit. | ||
| */ | ||
| export async function payerHasChatAccess(payer: UsageUpgradePayer): Promise<boolean> { | ||
| if (!isHosted || !isBillingEnabled) return true | ||
| if (!isFree(payer.payerSubscription?.plan)) return true | ||
| try { | ||
| const balance = await getCreditBalanceForEntity( | ||
| payer.billingEntity.type, | ||
| payer.billingEntity.id | ||
| ) | ||
| return balance > 0 | ||
| } catch (error) { | ||
| logger.warn('Credit balance unavailable; admitting the payer to Chat', { | ||
| billingEntity: payer.billingEntity, | ||
| error: getErrorMessage(error), | ||
| }) | ||
| return true | ||
|
waleedlatif1 marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
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.