Skip to content

Commit f3f77f2

Browse files
committed
Simplify freebuff model picker changes
1 parent b2b1233 commit f3f77f2

5 files changed

Lines changed: 27 additions & 87 deletions

File tree

cli/src/components/freebuff-model-selector.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import { useFreebuffModelStore } from '../state/freebuff-model-store'
1818
import { useFreebuffSessionStore } from '../state/freebuff-session-store'
1919
import { useTerminalDimensions } from '../hooks/use-terminal-dimensions'
2020
import { useTheme } from '../hooks/use-theme'
21-
import {
22-
nextFreebuffModelId,
23-
resolveFreebuffModelCommitTarget,
24-
} from '../utils/freebuff-model-navigation'
21+
import { nextFreebuffModelId } from '../utils/freebuff-model-navigation'
2522

2623
import type { KeyEvent } from '@opentui/core'
2724

@@ -203,14 +200,9 @@ export const FreebuffModelSelector: React.FC = () => {
203200
name === 'return' || name === 'enter' || name === 'space'
204201
if (!isForward && !isBackward && !isCommit) return
205202
if (isCommit) {
206-
const targetId = resolveFreebuffModelCommitTarget({
207-
focusedId,
208-
committedId: committedModelId,
209-
isSelectable: isJoinable,
210-
})
211-
if (targetId) {
203+
if (isJoinable(focusedId) && focusedId !== committedModelId) {
212204
key.preventDefault?.()
213-
pick(targetId)
205+
pick(focusedId)
214206
}
215207
return
216208
}

cli/src/hooks/use-freebuff-session.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -532,19 +532,11 @@ export function useFreebuffSession(): UseFreebuffSessionResult {
532532
) {
533533
return
534534
}
535-
const depths =
536-
response.status === 'none' || response.status === 'queued'
537-
? response.queueDepthByModel
538-
: undefined
539-
const rateLimits =
540-
'rateLimitsByModel' in response
541-
? response.rateLimitsByModel
542-
: undefined
543-
if (depths || rateLimits) {
535+
if (response.status === 'none' || response.status === 'queued') {
544536
apply({
545537
status: 'none',
546-
...(depths ? { queueDepthByModel: depths } : {}),
547-
...(rateLimits ? { rateLimitsByModel: rateLimits } : {}),
538+
queueDepthByModel: response.queueDepthByModel,
539+
rateLimitsByModel: response.rateLimitsByModel,
548540
})
549541
}
550542
})

cli/src/utils/__tests__/freebuff-model-navigation.test.ts

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { describe, expect, test } from 'bun:test'
22

3-
import {
4-
nextFreebuffModelId,
5-
resolveFreebuffModelCommitTarget,
6-
} from '../freebuff-model-navigation'
3+
import { nextFreebuffModelId } from '../freebuff-model-navigation'
74

85
describe('nextFreebuffModelId', () => {
96
test('moves to the next model when moving forward', () => {
@@ -52,35 +49,3 @@ describe('nextFreebuffModelId', () => {
5249
).toBeNull()
5350
})
5451
})
55-
56-
describe('resolveFreebuffModelCommitTarget', () => {
57-
test('returns null when focus is on a closed model', () => {
58-
expect(
59-
resolveFreebuffModelCommitTarget({
60-
focusedId: 'glm',
61-
committedId: null,
62-
isSelectable: (id) => id !== 'glm',
63-
}),
64-
).toBeNull()
65-
})
66-
67-
test('commits the focused model when it is selectable', () => {
68-
expect(
69-
resolveFreebuffModelCommitTarget({
70-
focusedId: 'minimax',
71-
committedId: null,
72-
isSelectable: (id) => id === 'minimax',
73-
}),
74-
).toBe('minimax')
75-
})
76-
77-
test('returns null when the target is already committed', () => {
78-
expect(
79-
resolveFreebuffModelCommitTarget({
80-
focusedId: 'minimax',
81-
committedId: 'minimax',
82-
isSelectable: () => true,
83-
}),
84-
).toBeNull()
85-
})
86-
})

cli/src/utils/freebuff-model-navigation.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,3 @@ export function nextFreebuffModelId(params: {
1212
const step = direction === 'forward' ? 1 : -1
1313
return modelIds[(currentIdx + step + modelIds.length) % modelIds.length]
1414
}
15-
16-
export function resolveFreebuffModelCommitTarget(params: {
17-
focusedId: string
18-
committedId: string | null
19-
isSelectable: (modelId: string) => boolean
20-
}): string | null {
21-
const { focusedId, committedId, isSelectable } = params
22-
23-
if (!isSelectable(focusedId) || focusedId === committedId) return null
24-
return focusedId
25-
}

web/src/server/free-session/public-api.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,14 @@ async function fetchRateLimitSnapshot(
9090
}
9191
}
9292

93-
async function fetchRateLimitSnapshotsByModel(
93+
async function fetchRateLimitsByModel(
9494
userId: string,
9595
deps: SessionDeps,
96-
opts: { includeUnused?: boolean } = {},
9796
): Promise<Record<string, FreebuffSessionRateLimit>> {
9897
const entries = await Promise.all(
9998
Object.keys(RATE_LIMITS).map(async (model) => {
10099
const snapshot = await fetchRateLimitSnapshot(userId, model, deps)
101-
return snapshot && (opts.includeUnused || snapshot.info.recentCount > 0)
102-
? ([model, snapshot.info] as const)
103-
: null
100+
return snapshot ? ([model, snapshot.info] as const) : null
104101
}),
105102
)
106103
return Object.fromEntries(
@@ -111,6 +108,16 @@ async function fetchRateLimitSnapshotsByModel(
111108
)
112109
}
113110

111+
function onlyUsedRateLimitsByModel(
112+
rateLimitsByModel: Record<string, FreebuffSessionRateLimit>,
113+
): Record<string, FreebuffSessionRateLimit> {
114+
return Object.fromEntries(
115+
Object.entries(rateLimitsByModel).filter(
116+
([, snapshot]) => snapshot.recentCount > 0,
117+
),
118+
)
119+
}
120+
114121
function nonEmptyRateLimitsByModel(
115122
rateLimitsByModel: Record<string, FreebuffSessionRateLimit>,
116123
): { rateLimitsByModel: Record<string, FreebuffSessionRateLimit> } | {} {
@@ -397,21 +404,14 @@ async function attachRateLimit(
397404
return snapshot ? { ...view, rateLimit: snapshot.info } : view
398405
}
399406

400-
const allRateLimitsByModel = await fetchRateLimitSnapshotsByModel(
401-
userId,
402-
deps,
403-
{ includeUnused: true },
404-
)
407+
const allRateLimitsByModel = await fetchRateLimitsByModel(userId, deps)
405408
const rateLimit = allRateLimitsByModel[view.model]
406-
const rateLimitsByModel = Object.fromEntries(
407-
Object.entries(allRateLimitsByModel).filter(
408-
([, snapshot]) => snapshot.recentCount > 0,
409-
),
410-
)
411409
return {
412410
...view,
413411
...(rateLimit ? { rateLimit } : {}),
414-
...nonEmptyRateLimitsByModel(rateLimitsByModel),
412+
...nonEmptyRateLimitsByModel(
413+
onlyUsedRateLimitsByModel(allRateLimitsByModel),
414+
),
415415
}
416416
}
417417

@@ -454,12 +454,14 @@ export async function getSessionState(params: {
454454
const noneResponse = async (): Promise<FreebuffSessionServerResponse> => {
455455
const [queueDepthByModel, rateLimitsByModel] = await Promise.all([
456456
deps.queueDepthsByModel(),
457-
fetchRateLimitSnapshotsByModel(params.userId, deps),
457+
fetchRateLimitsByModel(params.userId, deps),
458458
])
459459
return {
460460
status: 'none',
461461
queueDepthByModel,
462-
...nonEmptyRateLimitsByModel(rateLimitsByModel),
462+
...nonEmptyRateLimitsByModel(
463+
onlyUsedRateLimitsByModel(rateLimitsByModel),
464+
),
463465
}
464466
}
465467

0 commit comments

Comments
 (0)