-
Notifications
You must be signed in to change notification settings - Fork 8
fix(calculator): SSR with ?g_model so share links open the right model #431
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
+156
−4
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| import type { Metadata } from 'next'; | ||
|
|
||
| import ThroughputCalculatorDisplay from '@/components/calculator/ThroughputCalculatorDisplay'; | ||
| import { resolveCalculatorUrlSeed } from '@/components/calculator/url-seed'; | ||
| import { tabMetadata } from '@/lib/tab-meta'; | ||
|
|
||
| export const metadata: Metadata = tabMetadata('calculator'); | ||
|
|
||
| export default function CalculatorPage() { | ||
| return <ThroughputCalculatorDisplay />; | ||
| interface Props { | ||
| searchParams: Promise<Record<string, string | string[] | undefined>>; | ||
| } | ||
|
|
||
| export default async function CalculatorPage({ searchParams }: Props) { | ||
| const sp = await searchParams; | ||
| const seed = resolveCalculatorUrlSeed(sp); | ||
| return <ThroughputCalculatorDisplay urlSeed={seed} />; | ||
| } |
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,56 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { resolveCalculatorUrlSeed } from './url-seed'; | ||
| import { Model, Precision, Sequence } from '@/lib/data-mappings'; | ||
|
|
||
| describe('resolveCalculatorUrlSeed', () => { | ||
| it('returns the model when g_model is a known enum value', () => { | ||
| expect(resolveCalculatorUrlSeed({ g_model: 'DeepSeek-V4-Pro' })).toEqual({ | ||
| model: Model.DeepSeek_V4_Pro, | ||
| }); | ||
| }); | ||
|
|
||
| it('ignores unknown g_model values so SSR falls back to the default', () => { | ||
| expect(resolveCalculatorUrlSeed({ g_model: 'not-a-model' })).toEqual({}); | ||
| }); | ||
|
|
||
| it('returns the sequence when i_seq is a known enum value', () => { | ||
| expect(resolveCalculatorUrlSeed({ i_seq: '1k/1k' })).toEqual({ | ||
| sequence: Sequence.OneK_OneK, | ||
| }); | ||
| }); | ||
|
|
||
| it('parses i_prec as a comma-separated list, dropping unknown precisions', () => { | ||
| expect(resolveCalculatorUrlSeed({ i_prec: 'fp8,not-real,bf16' })).toEqual({ | ||
| precisions: [Precision.FP8, Precision.BF16], | ||
| }); | ||
| }); | ||
|
|
||
| it('omits precisions when none of the supplied values are known', () => { | ||
| expect(resolveCalculatorUrlSeed({ i_prec: 'garbage' })).toEqual({}); | ||
| }); | ||
|
|
||
| it('combines model, sequence, and precisions from the same URL', () => { | ||
| expect( | ||
| resolveCalculatorUrlSeed({ | ||
| g_model: 'DeepSeek-V4-Pro', | ||
| i_seq: '1k/8k', | ||
| i_prec: 'fp4,fp8', | ||
| }), | ||
| ).toEqual({ | ||
| model: Model.DeepSeek_V4_Pro, | ||
| sequence: Sequence.OneK_EightK, | ||
| precisions: [Precision.FP4, Precision.FP8], | ||
| }); | ||
| }); | ||
|
|
||
| it('picks the first value when a param is repeated as an array', () => { | ||
| expect(resolveCalculatorUrlSeed({ g_model: ['DeepSeek-V4-Pro', 'GLM-5'] })).toEqual({ | ||
| model: Model.DeepSeek_V4_Pro, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns an empty seed for an empty searchParams object', () => { | ||
| expect(resolveCalculatorUrlSeed({})).toEqual({}); | ||
| }); | ||
| }); |
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,56 @@ | ||
| import { | ||
| Model, | ||
| MODEL_OPTIONS, | ||
| Precision, | ||
| PRECISION_OPTIONS, | ||
| Sequence, | ||
| SEQUENCE_OPTIONS, | ||
| } from '@/lib/data-mappings'; | ||
|
|
||
| export interface CalculatorUrlSeed { | ||
| model?: Model; | ||
| sequence?: Sequence; | ||
| precisions?: string[]; | ||
| } | ||
|
|
||
| function pickString(value: string | string[] | undefined): string | undefined { | ||
| if (typeof value === 'string') return value; | ||
| if (Array.isArray(value)) return value[0]; | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Read the URL params that the calculator can SSR with into a typed seed. | ||
| * Without this, `?g_model=DeepSeek-V4-Pro` only takes effect after client | ||
| * hydration runs the `useLayoutEffect` in `GlobalFilterContext` — so the | ||
| * initial paint (and any preview/scraper that doesn't run JS) shows the | ||
| * default model instead of the shared one. | ||
| */ | ||
| export function resolveCalculatorUrlSeed( | ||
| sp: Record<string, string | string[] | undefined>, | ||
| ): CalculatorUrlSeed { | ||
| const seed: CalculatorUrlSeed = {}; | ||
|
|
||
| const modelParam = pickString(sp.g_model); | ||
| if (modelParam && (MODEL_OPTIONS as readonly string[]).includes(modelParam)) { | ||
| seed.model = modelParam as Model; | ||
| } | ||
|
|
||
| const seqParam = pickString(sp.i_seq); | ||
| if (seqParam && (SEQUENCE_OPTIONS as readonly string[]).includes(seqParam)) { | ||
| seed.sequence = seqParam as Sequence; | ||
| } | ||
|
|
||
| const precParam = pickString(sp.i_prec); | ||
| if (precParam) { | ||
| const precs = precParam | ||
| .split(',') | ||
| .filter((p) => (PRECISION_OPTIONS as readonly string[]).includes(p)); | ||
| if (precs.length > 0) seed.precisions = precs; | ||
| } | ||
|
|
||
| return seed; | ||
| } | ||
|
|
||
| // Re-export Model/Precision/Sequence for callers that already import this module. | ||
| export { Model, Precision, Sequence }; |
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.