-
Notifications
You must be signed in to change notification settings - Fork 47
feat(web): allow free-model sessions on zero balance and unify access… #4367
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
wombatepiclandingstudio
wants to merge
4
commits into
Kilo-Org:main
Choose a base branch
from
wombatepiclandingstudio:allow-sessions-with-free-models-on-zero-balance
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
4 commits
Select commit
Hold shift + click to select a range
9e37184
feat(web): allow free-model sessions on zero balance and unify access…
wombatepiclandingstudio 036b694
fix(web): allow submit on free/BYOK models for limited-access Cloud A…
wombatepiclandingstudio 2b4fa9e
Merge branch 'main' into allow-sessions-with-free-models-on-zero-balance
wombatepiclandingstudio fe82309
Merge branch 'main' into allow-sessions-with-free-models-on-zero-balance
wombatepiclandingstudio 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| export type AccessLevel = 'full' | 'limited' | 'blocked'; | ||
|
|
||
| export type AccessLevelEligibility = { | ||
| balance: number; | ||
| minBalance: number; | ||
| accessLevel: AccessLevel; | ||
| isEligible: boolean; | ||
| }; | ||
|
|
||
| export function buildAccessLevelEligibility( | ||
| balance: number, | ||
| minBalance: number | ||
| ): AccessLevelEligibility { | ||
| const accessLevel: AccessLevel = balance >= minBalance ? 'full' : 'limited'; | ||
| return { | ||
| balance, | ||
| minBalance, | ||
| accessLevel, | ||
| isEligible: accessLevel === 'full', | ||
| }; | ||
| } |
143 changes: 143 additions & 0 deletions
143
apps/web/src/lib/cloud-agent-next/balance-check-eligibility.test.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,143 @@ | ||
| const mockIsFreeModel = jest.fn(); | ||
| const mockGetModelUserByokProviders = jest.fn(); | ||
| const mockGetUserByokProviderIds = jest.fn(); | ||
| const mockGetOrganizationByokProviderIds = jest.fn(); | ||
|
|
||
| jest.mock('@/lib/ai-gateway/is-free-model', () => ({ | ||
| isFreeModel: (...args: unknown[]) => mockIsFreeModel(...args), | ||
| })); | ||
|
|
||
| jest.mock('@/lib/ai-gateway/byok', () => ({ | ||
| getModelUserByokProviders: (...args: unknown[]) => mockGetModelUserByokProviders(...args), | ||
| getUserByokProviderIds: (...args: unknown[]) => mockGetUserByokProviderIds(...args), | ||
| getOrganizationByokProviderIds: (...args: unknown[]) => | ||
| mockGetOrganizationByokProviderIds(...args), | ||
| })); | ||
|
|
||
| import { computeCloudAgentNextBalanceCheckEligibility } from './balance-check-eligibility'; | ||
|
|
||
| const KILO_EXCLUSIVE_MODEL = 'deepseek/deepseek-v4-pro:discounted'; | ||
| const NON_EXCLUSIVE_MODEL = 'anthropic/claude-sonnet-4'; | ||
|
|
||
| const fakeDb = {} as never; | ||
| const fakeUser = { id: 'user-1' }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockIsFreeModel.mockResolvedValue(false); | ||
| mockGetModelUserByokProviders.mockResolvedValue([]); | ||
| mockGetUserByokProviderIds.mockResolvedValue([]); | ||
| mockGetOrganizationByokProviderIds.mockResolvedValue([]); | ||
| }); | ||
|
|
||
| describe('computeCloudAgentNextBalanceCheckEligibility', () => { | ||
| it('returns isFree and skips BYOK when the model is free', async () => { | ||
| mockIsFreeModel.mockResolvedValueOnce(true); | ||
|
|
||
| const result = await computeCloudAgentNextBalanceCheckEligibility({ | ||
| fromDb: fakeDb, | ||
| user: fakeUser, | ||
| modelId: 'kilo/free-model', | ||
| }); | ||
|
|
||
| expect(result).toEqual({ isFree: true, hasUserByokAvailable: false }); | ||
| expect(mockGetModelUserByokProviders).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns hasUserByokAvailable: false for a Kilo-exclusive model even when BYOK providers can serve it', async () => { | ||
| const result = await computeCloudAgentNextBalanceCheckEligibility({ | ||
| fromDb: fakeDb, | ||
| user: fakeUser, | ||
| modelId: KILO_EXCLUSIVE_MODEL, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); | ||
| expect(mockGetModelUserByokProviders).not.toHaveBeenCalled(); | ||
| expect(mockGetUserByokProviderIds).not.toHaveBeenCalled(); | ||
| expect(mockGetOrganizationByokProviderIds).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns hasUserByokAvailable: false for a Kilo-exclusive model even when the user has an enabled matching BYOK provider', async () => { | ||
| mockGetUserByokProviderIds.mockResolvedValueOnce(['openrouter']); | ||
|
|
||
| const result = await computeCloudAgentNextBalanceCheckEligibility({ | ||
| fromDb: fakeDb, | ||
| user: fakeUser, | ||
| modelId: KILO_EXCLUSIVE_MODEL, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); | ||
| expect(mockGetModelUserByokProviders).not.toHaveBeenCalled(); | ||
| expect(mockGetUserByokProviderIds).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns hasUserByokAvailable: false for a Kilo-exclusive model in an organization context', async () => { | ||
| mockGetOrganizationByokProviderIds.mockResolvedValueOnce(['openrouter']); | ||
|
|
||
| const result = await computeCloudAgentNextBalanceCheckEligibility({ | ||
| fromDb: fakeDb, | ||
| user: fakeUser, | ||
| modelId: KILO_EXCLUSIVE_MODEL, | ||
| organizationId: 'org-1', | ||
| }); | ||
|
|
||
| expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); | ||
| expect(mockGetModelUserByokProviders).not.toHaveBeenCalled(); | ||
| expect(mockGetOrganizationByokProviderIds).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns hasUserByokAvailable: true for a non-Kilo-exclusive paid model with a matching enabled user BYOK provider', async () => { | ||
| mockGetModelUserByokProviders.mockResolvedValueOnce(['openrouter']); | ||
| mockGetUserByokProviderIds.mockResolvedValueOnce(['openrouter']); | ||
|
|
||
| const result = await computeCloudAgentNextBalanceCheckEligibility({ | ||
| fromDb: fakeDb, | ||
| user: fakeUser, | ||
| modelId: NON_EXCLUSIVE_MODEL, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ isFree: false, hasUserByokAvailable: true }); | ||
| }); | ||
|
|
||
| it('returns hasUserByokAvailable: false for a non-Kilo-exclusive paid model with no matching BYOK provider', async () => { | ||
| mockGetModelUserByokProviders.mockResolvedValueOnce(['openrouter']); | ||
| mockGetUserByokProviderIds.mockResolvedValueOnce(['anthropic']); | ||
|
|
||
| const result = await computeCloudAgentNextBalanceCheckEligibility({ | ||
| fromDb: fakeDb, | ||
| user: fakeUser, | ||
| modelId: NON_EXCLUSIVE_MODEL, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); | ||
| }); | ||
|
|
||
| it('returns hasUserByokAvailable: false for a non-Kilo-exclusive paid model with no resolvable providers', async () => { | ||
| mockGetModelUserByokProviders.mockResolvedValueOnce([]); | ||
|
|
||
| const result = await computeCloudAgentNextBalanceCheckEligibility({ | ||
| fromDb: fakeDb, | ||
| user: fakeUser, | ||
| modelId: NON_EXCLUSIVE_MODEL, | ||
| }); | ||
|
|
||
| expect(result).toEqual({ isFree: false, hasUserByokAvailable: false }); | ||
| expect(mockGetUserByokProviderIds).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('uses organization BYOK providers for a non-Kilo-exclusive paid model when organizationId is provided', async () => { | ||
| mockGetModelUserByokProviders.mockResolvedValueOnce(['openrouter']); | ||
| mockGetOrganizationByokProviderIds.mockResolvedValueOnce(['openrouter']); | ||
|
|
||
| const result = await computeCloudAgentNextBalanceCheckEligibility({ | ||
| fromDb: fakeDb, | ||
| user: fakeUser, | ||
| modelId: NON_EXCLUSIVE_MODEL, | ||
| organizationId: 'org-1', | ||
| }); | ||
|
|
||
| expect(result).toEqual({ isFree: false, hasUserByokAvailable: true }); | ||
| expect(mockGetOrganizationByokProviderIds).toHaveBeenCalledWith(fakeDb, 'org-1'); | ||
| expect(mockGetUserByokProviderIds).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
66 changes: 66 additions & 0 deletions
66
apps/web/src/lib/cloud-agent-next/balance-check-eligibility.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,66 @@ | ||
| import 'server-only'; | ||
| import { type db } from '@/lib/drizzle'; | ||
| import { isFreeModel } from '@/lib/ai-gateway/is-free-model'; | ||
| import { isKiloExclusiveModel } from '@/lib/ai-gateway/models'; | ||
| import { | ||
| getModelUserByokProviders, | ||
| getOrganizationByokProviderIds, | ||
| getUserByokProviderIds, | ||
| } from '@/lib/ai-gateway/byok'; | ||
| import type { User } from '@kilocode/db/schema'; | ||
|
|
||
| export type BalanceCheckModelEligibility = { | ||
| isFree: boolean; | ||
| hasUserByokAvailable: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Decide whether `prepareSession` should skip the worker-side $1 balance | ||
| * minimum for the chosen model. | ||
| * | ||
| * Skips the check when either: | ||
| * - the model is Kilo-funded (free for the user), or | ||
| * - the model is not Kilo-exclusive AND the user has a BYOK provider | ||
| * configured that can serve it, so the session is billed against the | ||
| * user's own key rather than their balance. | ||
| * | ||
| * Kilo-exclusive models (e.g. `deepseek/deepseek-v4-pro:discounted`) are | ||
| * always excluded from the BYOK bypass: they are Kilo-funded and platform | ||
| * billed, so even when `getModelUserByokProviders` reports a provider that | ||
| * can route the model, they must still go through the worker-side balance | ||
| * check and cannot be legitimately served via a user's own BYOK key. | ||
| * | ||
| * The resulting predicate is a strict subset of the | ||
| * `isFree || hasUserByokAvailable` predicate used by the NewSessionPanel | ||
| * model picker to filter `hasLimitedAccess` users: this router additionally | ||
| * forces Kilo-exclusive models through the balance check, so the picker | ||
| * may offer a model as free while the router still requires a balance. | ||
| */ | ||
| export async function computeCloudAgentNextBalanceCheckEligibility(params: { | ||
| fromDb: typeof db; | ||
| user: Pick<User, 'id'>; | ||
| modelId: string; | ||
| organizationId?: string; | ||
| }): Promise<BalanceCheckModelEligibility> { | ||
| const isFree = await isFreeModel(params.modelId); | ||
| if (isFree) { | ||
| return { isFree: true, hasUserByokAvailable: false }; | ||
| } | ||
|
|
||
| if (isKiloExclusiveModel(params.modelId)) { | ||
| return { isFree: false, hasUserByokAvailable: false }; | ||
| } | ||
|
|
||
| const modelProviders = await getModelUserByokProviders(params.modelId); | ||
| if (modelProviders.length === 0) { | ||
| return { isFree: false, hasUserByokAvailable: false }; | ||
| } | ||
|
|
||
| const enabledProviderIds = params.organizationId | ||
| ? await getOrganizationByokProviderIds(params.fromDb, params.organizationId) | ||
| : await getUserByokProviderIds(params.fromDb, params.user.id); | ||
|
|
||
| const enabled = new Set(enabledProviderIds); | ||
| const hasUserByokAvailable = modelProviders.some(provider => enabled.has(provider)); | ||
| return { isFree: false, hasUserByokAvailable }; | ||
| } |
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.
CRITICAL:
isFormValidstill blocks submission for limited-access users, defeating this PR's goalFurther down in this component,
isFormValidrequires!hasInsufficientBalance, andhasInsufficientBalanceistruewhenevereligibilityData.isEligibleisfalse— which is exactly the case wheneveraccessLevelis'limited'(i.e. wheneverhasLimitedAccesshere istrue). So for every zero/low-balance user, the submit button (disabled={!isFormValid || ...}) and the Cmd/Ctrl+Enter shortcut stay disabled even after they pick a free or BYOK-capable model from the filteredmodelOptions. The stated purpose of this PR — letting zero-balance users start Cloud Agent sessions on free models — can't actually be exercised from this UI as written.isFormValidneeds to permit submission whenhasLimitedAccessis true and the selected model is free/BYOK-capable, mirroring the filter already applied tomodelOptions.Reply with
@kilocode-bot fix itto have Kilo Code address this issue.