Repository navigation
fix(billing): reserve Sim's hosted model and tool keys for paid plans #10313
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
+210
−97
Merged
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
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
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
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,51 @@ | ||
| import { resolveWorkspaceBillingPayer } from '@/lib/billing/core/attribution' | ||
| import { payerHasPaidAccess } from '@/lib/billing/core/chat-access' | ||
| import type { UsageUpgradePayer } from '@/lib/billing/usage-upgrade' | ||
| import { isBillingEnabled, isHosted } from '@/lib/core/config/env-flags' | ||
| import { HttpError } from '@/lib/core/utils/http-error' | ||
|
|
||
| /** | ||
| * Sim's hosted key was withheld because the run's payer is on the free plan. Carries a 402 so the | ||
| * run fails as the caller's plan, not as a Sim fault. | ||
| */ | ||
| export class HostedKeyRequiresPaidPlanError extends HttpError { | ||
| readonly statusCode = 402 | ||
|
|
||
| /** @param subject - what asked for the key: a tool's name or a model id */ | ||
| constructor(subject: string) { | ||
| super( | ||
| `${subject} with Sim's hosted key is available on paid plans. Upgrade your plan, or add your own API key under Settings > BYOK.` | ||
| ) | ||
| this.name = 'HostedKeyRequiresPaidPlanError' | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Whether a run may spend on Sim's hosted provider keys, for models and tools alike: only a payer | ||
| * with paid access ({@link payerHasPaidAccess}) may. The payer is the run's admitted attribution, | ||
| * or the workspace's payer for a surface that carries none; a workspace without a payer, or a free | ||
| * payer whose credit balance cannot be read, is refused. | ||
| */ | ||
| export async function mayUseHostedKey( | ||
| workspaceId: string | null | undefined, | ||
| billingAttribution?: UsageUpgradePayer | ||
| ): Promise<boolean> { | ||
| if (!isHosted || !isBillingEnabled) return true | ||
|
|
||
| let payer = billingAttribution ?? null | ||
| if (!payer && workspaceId) { | ||
| const workspacePayer = await resolveWorkspaceBillingPayer(workspaceId, { | ||
| onMissing: 'return-null', | ||
| }) | ||
| if (workspacePayer) { | ||
| payer = { | ||
| billingEntity: workspacePayer.organizationId | ||
| ? { type: 'organization', id: workspacePayer.organizationId } | ||
| : { type: 'user', id: workspacePayer.billedAccountUserId }, | ||
| payerSubscription: workspacePayer.payerSubscription, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return payer !== null && payerHasPaidAccess(payer, { onBalanceUnavailable: 'deny' }) | ||
| } | ||
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.
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.