Repository navigation
feat(mobile): show DeepSeek API balance #23294
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
innocarpe
wants to merge
3
commits into
stablyai:main
Choose a base branch
from
innocarpe:fix/mobile-deepseek-account-usage
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.
+356
−0
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { Wallet } from 'lucide-react-native' | ||
| import { Text, View } from 'react-native' | ||
| import type { AccountsSnapshot } from '../components/accounts-snapshot' | ||
| import { getDeepSeekAccountUsage } from '../components/deepseek-account-usage' | ||
| import { colors } from '../theme/mobile-theme' | ||
| import { styles } from './mobile-accounts-screen-styles' | ||
|
|
||
| export function DeepSeekAccountUsageSection({ snapshot }: { snapshot: AccountsSnapshot }) { | ||
| const usage = getDeepSeekAccountUsage(snapshot) | ||
| if (!usage) { | ||
| return null | ||
| } | ||
|
|
||
| return ( | ||
| <View style={styles.section}> | ||
| <View style={styles.sectionHeader}> | ||
| <Wallet size={14} color={colors.textMuted} /> | ||
| <Text style={styles.sectionHeading}>DeepSeek API</Text> | ||
| </View> | ||
| <View style={styles.card}> | ||
| <View style={styles.row}> | ||
| <View style={styles.rowMain}> | ||
| <Text style={styles.rowTitle}>Balance</Text> | ||
| <Text accessibilityLiveRegion="polite" style={styles.rowSubtitle} numberOfLines={1}> | ||
| {usage.balanceLabel ?? usage.statusLabel} | ||
| </Text> | ||
| <Text style={styles.rowSubtitle}>Read from the connected host</Text> | ||
| {usage.balanceLabel && usage.status !== 'available' ? ( | ||
| <Text accessibilityLiveRegion="polite" style={styles.errorText}> | ||
| {usage.statusLabel} | ||
| </Text> | ||
| ) : null} | ||
| </View> | ||
| </View> | ||
| </View> | ||
| </View> | ||
| ) | ||
| } |
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,229 @@ | ||
| import { readFileSync } from 'node:fs' | ||
| import { createElement } from 'react' | ||
| import { Text } from 'react-native' | ||
| import { act, create, type ReactTestRenderer } from 'react-test-renderer' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| vi.mock('react-native', () => ({ | ||
| Text: 'Text', | ||
| View: 'View', | ||
| Pressable: 'Pressable', | ||
| ActivityIndicator: 'ActivityIndicator', | ||
| StyleSheet: { create: (styles: Record<string, unknown>) => styles } | ||
| })) | ||
| vi.mock('lucide-react-native', () => ({ Wallet: 'Wallet' })) | ||
| vi.mock('./AgentIcons', () => ({ ClaudeIcon: 'ClaudeIcon', OpenAIIcon: 'OpenAIIcon' })) | ||
|
|
||
| import { decodeAccountsSnapshot, type AccountsSnapshot } from './accounts-snapshot' | ||
| import { getDeepSeekAccountUsage, hasDeepSeekAccountUsage } from './deepseek-account-usage' | ||
| import { DeepSeekAccountUsageSection } from '../accounts/DeepSeekAccountUsageSection' | ||
|
|
||
| function deepSeekLimits(overrides: Record<string, unknown> = {}) { | ||
| return { | ||
| provider: 'deepseek', | ||
| session: null, | ||
| weekly: null, | ||
| monthly: null, | ||
| updatedAt: 10, | ||
| error: null, | ||
| status: 'ok', | ||
| ...overrides | ||
| } | ||
| } | ||
|
|
||
| function decode(rateLimits: Record<string, unknown>) { | ||
| return decodeAccountsSnapshot({ | ||
| claude: { accounts: [], activeAccountId: null }, | ||
| codex: { accounts: [], activeAccountId: null }, | ||
| rateLimits: { | ||
| claude: null, | ||
| codex: null, | ||
| inactiveClaudeAccounts: [], | ||
| inactiveCodexAccounts: [], | ||
| ...rateLimits | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| describe('getDeepSeekAccountUsage', () => { | ||
| it('shows the remaining balance from the host usage snapshot', () => { | ||
| const snapshot = decode({ | ||
| deepseekAuthConfigured: true, | ||
| deepseek: deepSeekLimits({ | ||
| monthly: { | ||
| usedPercent: 0, | ||
| windowMinutes: 43_200, | ||
| resetsAt: null, | ||
| resetDescription: 'USD 110.00' | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| expect(getDeepSeekAccountUsage(snapshot)).toEqual({ | ||
| balanceLabel: 'USD 110.00', | ||
| status: 'available', | ||
| statusLabel: 'Balance available' | ||
| }) | ||
| expect(hasDeepSeekAccountUsage(snapshot)).toBe(true) | ||
| }) | ||
|
|
||
| it('shows configured DeepSeek while the first balance request is pending', () => { | ||
| const snapshot = decode({ deepseekAuthConfigured: true }) | ||
|
|
||
| expect(getDeepSeekAccountUsage(snapshot)).toEqual({ | ||
| balanceLabel: null, | ||
| status: 'loading', | ||
| statusLabel: 'Checking balance…' | ||
| }) | ||
| }) | ||
|
|
||
| it('shows a depleted balance state', () => { | ||
| const snapshot = decode({ | ||
| deepseek: deepSeekLimits({ | ||
| monthly: { | ||
| usedPercent: 100, | ||
| windowMinutes: 43_200, | ||
| resetsAt: null, | ||
| resetDescription: 'USD 0.00' | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| expect(getDeepSeekAccountUsage(snapshot)).toMatchObject({ | ||
| balanceLabel: 'USD 0.00', | ||
| status: 'depleted', | ||
| statusLabel: 'No balance remaining' | ||
| }) | ||
| }) | ||
|
|
||
| it('keeps the last balance visible while reporting a refresh error', () => { | ||
| const snapshot = decode({ | ||
| deepseekAuthConfigured: true, | ||
| deepseek: deepSeekLimits({ | ||
| status: 'error', | ||
| error: 'temporary network failure', | ||
| monthly: { | ||
| usedPercent: 0, | ||
| windowMinutes: 43_200, | ||
| resetsAt: null, | ||
| resetDescription: 'USD 72.00' | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| expect(getDeepSeekAccountUsage(snapshot)).toMatchObject({ | ||
| balanceLabel: 'USD 72.00', | ||
| status: 'refresh-error', | ||
| statusLabel: 'Balance refresh failed' | ||
| }) | ||
| }) | ||
|
|
||
| it('hides DeepSeek when the host has no configured key and no balance window', () => { | ||
| const snapshot = decode({ deepseek: deepSeekLimits({ monthly: null }) }) | ||
|
|
||
| expect(getDeepSeekAccountUsage(snapshot)).toBeNull() | ||
| }) | ||
|
|
||
| it('ignores a malformed DeepSeek slot without dropping Claude usage', () => { | ||
| const snapshot = decode({ | ||
| claude: { | ||
| ...deepSeekLimits(), | ||
| provider: 'claude', | ||
| session: { | ||
| usedPercent: 25, | ||
| windowMinutes: 300, | ||
| resetsAt: null, | ||
| resetDescription: null | ||
| } | ||
| }, | ||
| deepseek: { provider: 'claude', status: 'ok' }, | ||
| deepseekAuthConfigured: true | ||
| }) | ||
|
|
||
| expect(snapshot.rateLimits.claude?.status).toBe('ok') | ||
| expect(getDeepSeekAccountUsage(snapshot)).toMatchObject({ | ||
| balanceLabel: null, | ||
| status: 'unavailable', | ||
| statusLabel: 'Balance unavailable' | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('DeepSeek usage surfaces', () => { | ||
| let mounted: ReactTestRenderer | null = null | ||
|
|
||
| afterEach(async () => { | ||
| await act(async () => mounted?.unmount()) | ||
| mounted = null | ||
| }) | ||
|
|
||
| it.each([ | ||
| { name: 'pending', raw: undefined, text: 'Checking balance…', balance: null }, | ||
| { name: 'malformed', raw: { provider: 'claude' }, text: 'Balance unavailable', balance: null }, | ||
| { | ||
| name: 'available', | ||
| raw: deepSeekLimits({ monthly: balanceWindow(0, 'USD 72.00') }), | ||
| text: null, | ||
| balance: 'USD 72.00' | ||
| }, | ||
| { | ||
| name: 'depleted', | ||
| raw: deepSeekLimits({ monthly: balanceWindow(100, 'USD 0.00') }), | ||
| text: 'No balance remaining', | ||
| balance: 'USD 0.00' | ||
| }, | ||
| { | ||
| name: 'refresh error', | ||
| raw: deepSeekLimits({ status: 'error', monthly: balanceWindow(0, 'USD 72.00') }), | ||
| text: 'Balance refresh failed', | ||
| balance: 'USD 72.00' | ||
| } | ||
| ])('renders the $name balance on Accounts', async ({ raw, text, balance }) => { | ||
| const snapshot = decode({ deepseekAuthConfigured: true, deepseek: raw }) | ||
| const tree = await renderSection(snapshot) | ||
| const texts = tree.root.findAll((node) => node.type === Text) | ||
| const shown = texts.map((node) => node.children.join('')) | ||
| expect(shown.filter((value) => value === 'DeepSeek API')).toHaveLength(1) | ||
| const primary = balance ?? text | ||
| if (primary) { | ||
| expect(shown).toContain(primary) | ||
| expect( | ||
| texts.find((node) => node.children.join('') === primary)?.props.accessibilityLiveRegion | ||
| ).toBe('polite') | ||
| } | ||
| if (text && balance) { | ||
| expect(shown).toContain(text) | ||
| expect( | ||
| texts.find((node) => node.children.join('') === text)?.props.accessibilityLiveRegion | ||
| ).toBe('polite') | ||
| } | ||
| }) | ||
|
|
||
| async function renderSection(snapshot: AccountsSnapshot): Promise<ReactTestRenderer> { | ||
| await act(async () => { | ||
| mounted = create(createElement(DeepSeekAccountUsageSection, { snapshot })) | ||
| }) | ||
| if (!mounted) { | ||
| throw new Error('usage section did not mount') | ||
| } | ||
| return mounted | ||
| } | ||
|
|
||
| it('mounts the read-only DeepSeek section on Accounts', () => { | ||
| const accounts = readFileSync( | ||
| new URL('../accounts/MobileAccountsScreen.tsx', import.meta.url), | ||
| 'utf8' | ||
| ) | ||
| const section = readFileSync( | ||
| new URL('../accounts/DeepSeekAccountUsageSection.tsx', import.meta.url), | ||
| 'utf8' | ||
| ) | ||
|
|
||
| expect(accounts).toContain('<DeepSeekAccountUsageSection snapshot={snapshot} />') | ||
| expect(section).toContain('getDeepSeekAccountUsage(snapshot)') | ||
| }) | ||
| }) | ||
|
|
||
| function balanceWindow(usedPercent: number, resetDescription: string) { | ||
| return { usedPercent, windowMinutes: 43_200, resetsAt: null, resetDescription } | ||
| } |
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,87 @@ | ||
| import { z } from 'zod' | ||
| import { ProviderRateLimitsSchema, type AccountsSnapshot } from './accounts-snapshot' | ||
|
|
||
| const DeepSeekProviderRateLimitsSchema = ProviderRateLimitsSchema.extend({ | ||
| provider: z.literal('deepseek') | ||
| }) | ||
|
|
||
| export type DeepSeekAccountUsageStatus = | ||
| | 'loading' | ||
| | 'available' | ||
| | 'depleted' | ||
| | 'refresh-error' | ||
| | 'unavailable' | ||
|
|
||
| export type DeepSeekAccountUsage = { | ||
| balanceLabel: string | null | ||
| status: DeepSeekAccountUsageStatus | ||
| statusLabel: string | ||
| } | ||
|
|
||
| function readDeepSeekAuthConfigured(snapshot: AccountsSnapshot): boolean { | ||
| return snapshot.rateLimits['deepseekAuthConfigured'] === true | ||
| } | ||
|
|
||
| // Why: isolate the optional provider slot so a malformed DeepSeek payload cannot | ||
| // make the existing Claude/Codex account snapshot unreadable. | ||
| function readDeepSeekLimits(snapshot: AccountsSnapshot): { | ||
| limits: z.infer<typeof DeepSeekProviderRateLimitsSchema> | null | ||
| invalid: boolean | ||
| } { | ||
| const raw = snapshot.rateLimits['deepseek'] | ||
| if (raw == null) { | ||
| return { limits: null, invalid: false } | ||
| } | ||
| const parsed = DeepSeekProviderRateLimitsSchema.safeParse(raw) | ||
| if (!parsed.success) { | ||
| return { limits: null, invalid: true } | ||
| } | ||
| return { limits: parsed.data, invalid: false } | ||
| } | ||
|
|
||
| export function getDeepSeekAccountUsage(snapshot: AccountsSnapshot): DeepSeekAccountUsage | null { | ||
| const { limits, invalid } = readDeepSeekLimits(snapshot) | ||
| const monthly = limits?.monthly ?? null | ||
| if (!readDeepSeekAuthConfigured(snapshot) && !monthly) { | ||
| return null | ||
| } | ||
|
|
||
| const balanceLabel = monthly?.resetDescription?.trim() || null | ||
| const isLoading = !limits || limits.status === 'idle' || limits.status === 'fetching' | ||
| const status: DeepSeekAccountUsageStatus = invalid | ||
| ? 'unavailable' | ||
| : limits?.status === 'error' && monthly | ||
| ? 'refresh-error' | ||
| : monthly | ||
| ? monthly.usedPercent >= 100 | ||
| ? 'depleted' | ||
| : 'available' | ||
| : isLoading | ||
| ? 'loading' | ||
| : 'unavailable' | ||
|
|
||
| return { | ||
| balanceLabel, | ||
| status, | ||
| statusLabel: getDeepSeekAccountUsageStatusLabel(status) | ||
| } | ||
| } | ||
|
|
||
| export function hasDeepSeekAccountUsage(snapshot: AccountsSnapshot): boolean { | ||
| return getDeepSeekAccountUsage(snapshot) !== null | ||
| } | ||
|
|
||
| function getDeepSeekAccountUsageStatusLabel(status: DeepSeekAccountUsageStatus): string { | ||
| switch (status) { | ||
| case 'loading': | ||
| return 'Checking balance…' | ||
| case 'available': | ||
| return 'Balance available' | ||
| case 'depleted': | ||
| return 'No balance remaining' | ||
| case 'refresh-error': | ||
| return 'Balance refresh failed' | ||
| case 'unavailable': | ||
| return 'Balance unavailable' | ||
| } | ||
| } | ||
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.