A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Sort model search by release date and close the picker on selection
Esteban Suarez23c56232a917
diff --git a/app/api/models/route.ts b/app/api/models/route.tsindex d621efc..71e1d40 100644--- a/app/api/models/route.ts+++ b/app/api/models/route.ts@@ -44,13 +44,16 @@ export async function GET() {const entries: { id: string; released?: number }[] = []for (const model of payload.data ?? []) {if (typeof model.id !== 'string' || (typeof model.type === 'string' && model.type.toLowerCase() !== 'language')) continue- catalog.models.push(model.id)- entries.push({ id: model.id, released: typeof model.released === 'number' ? model.released : undefined })+ entries.push({ id: model.id, released: typeof model.released === 'number' && Number.isFinite(model.released) ? model.released : undefined })const values = model.reasoning_options?.find((option) => option.type === 'effort')?.valuesif (values?.length) catalog.efforts[model.id] = valuesif (model.tags?.includes('fast')) catalog.fast.push(model.id)}- catalog.models = [...new Set(catalog.models)].sort()+ // Search preserves this order among equally relevant matches: newest releases first.+ // Models without release metadata follow dated ones; ties are deterministic.+ catalog.models = [...new Set(entries+ .sort((a, b) => (b.released ?? 0) - (a.released ?? 0) || a.id.localeCompare(b.id))+ .map(({ id }) => id))]catalog.fast = [...new Set(catalog.fast)]catalog.featured = featuredModels(entries)cache = { at: Date.now(), catalog }diff --git a/components/composer.tsx b/components/composer.tsxindex 996cc4a..b045c6a 100644--- a/components/composer.tsx+++ b/components/composer.tsx@@ -1038,7 +1038,10 @@ export default function Composer({type="button"role="option"aria-selected={resolvedModel === candidate}- {...fastClick(() => void chooseModel(candidate))}+ {...fastClick(() => {+ setModelPickerOpen(false)+ void chooseModel(candidate)+ })}className={`flex min-h-8 w-full items-center gap-2 px-2.5 text-left text-sm outline-none hover:bg-accent hover:text-foreground focus-visible:bg-accent focus-visible:text-foreground ${model === candidate ? 'bg-accent text-foreground' : 'text-muted-foreground'}`}><span className="min-w-0 flex-1 truncate">{candidate.split('/').at(-1) ?? candidate}</span>diff --git a/tests/guest-ui.test.mjs b/tests/guest-ui.test.mjsindex 084b036..53855ab 100644--- a/tests/guest-ui.test.mjs+++ b/tests/guest-ui.test.mjs@@ -1109,6 +1109,48 @@ test('the model picker opens on the selected model, then the featured ones, thenassert.deepEqual(rows.filter((name) => String(name).endsWith('-model')), ['default-model', 'z-model', 'a-model'])})+for (const input of ['pointer', 'keyboard']) {+ test(`selecting a model closes the picker immediately (${input})`, async (t) => {+ let releaseSave+ const saved = new Promise((resolve) => { releaseSave = resolve })+ const ui = browser(t, {+ '/api/model?session=chat-1': (init) => saved.then(() => Response.json(JSON.parse(init.body))),+ })+ const view = composerAt(ui, 'chat-1', {+ model: 'a-model', defaultModel: 'default-model', effort: '', fast: false,+ stored: { model: 'a-model', effort: '', fast: false },+ })+ await tick()+ const picker = (tree) => find(tree, (node) => typeof node.props?.onOpenChange === 'function' && 'open' in node.props)+ picker(view.render()).props.onOpenChange(true)+ const tree = view.render()+ assert.equal(picker(tree).props.open, true)+ const row = find(tree, (node) => node.props?.role === 'option' && node.props.children?.[0]?.props?.children === 'z-model')+ if (input === 'pointer') row.props.onPointerDown({ button: 0 })+ else row.props.onClick({ detail: 0 })+ const after = view.render()+ assert.equal(picker(after).props.open, false, 'close without waiting for the settings request')+ assert.equal(view.ref.current.getGeneration().model, 'z-model')+ releaseSave()+ await tick()+ await tick()+ view.render()+ const writes = ui.requests.filter(({ url, init }) => url === '/api/model?session=chat-1' && init?.method === 'POST')+ assert.equal(writes.length, 1)+ assert.equal(JSON.parse(writes[0].init.body).model, 'z-model')+ })+}++test('model search preserves catalog recency order for equally matching names', async (t) => {+ const ui = browser(t)+ const view = composerAt(ui, 'draft', { model: '', defaultModel: 'default-model', effort: '', fast: false })+ await tick()+ find(view.render(), (node) => node.props?.id === 'model-search').props.onChange({ target: { value: 'model' } })+ const rows = []+ find(view.render(), (node) => { if (node.props?.role === 'option') rows.push(node.props.children?.[0]?.props?.children); return false })+ assert.deepEqual(rows, ['a-model', 'default-model', 'z-model'], 'equally ranked matches keep the API order, not the featured order')+})+test('a reloaded chat uses the settings the page rendered, and asks only for later chats', async (t) => {const ui = browser(t, { '/api/model?session=chat-2': () => Response.json({ model: 'z-model', effort: '', fast: false }) })const stored = { model: 'a-model', effort: 'high', fast: true }diff --git a/tests/route-security.test.mjs b/tests/route-security.test.mjsindex 26e80e1..943e992 100644--- a/tests/route-security.test.mjs+++ b/tests/route-security.test.mjs@@ -1043,7 +1043,12 @@ test('model catalog is public, credential-free, shared, and never reports an autreturn calls.length === 1? Response.json({ data: [{ id: 'anthropic/fast', type: 'language', tags: ['fast'], reasoning_options: [{ type: 'effort', values: ['low', 'high'] }] },- { id: 'openai/image', type: 'image' },+ { id: 'openai/image', type: 'image', released: 900 },+ { id: 'deepseek/older', type: 'language', released: 100 },+ { id: 'deepseek/newest', type: 'language', released: 300 },+ { id: 'deepseek/newer', type: 'language', released: 200 },+ { id: 'deepseek/newest', type: 'language', released: 300 },+ { id: 'zai/undated', type: 'language' },] }): new Response(null, { status: 401 })})@@ -1052,7 +1057,10 @@ test('model catalog is public, credential-free, shared, and never reports an aut// Shared by everyone, so the CDN may serve it.assert.equal(first.headers.get('cache-control'), 'public, max-age=0, s-maxage=300, stale-while-revalidate=86400')const { defaultModel, ...catalog } = await first.json()- assert.deepEqual(catalog, { models: ['anthropic/fast'], efforts: { 'anthropic/fast': ['low', 'high'] }, fast: ['anthropic/fast'], featured: [] })+ assert.deepEqual(catalog, {+ models: ['deepseek/newest', 'deepseek/newer', 'deepseek/older', 'anthropic/fast', 'zai/undated'],+ efforts: { 'anthropic/fast': ['low', 'high'] }, fast: ['anthropic/fast'], featured: [],+ }, 'models are newest first, deduplicated, with undated entries last and image models excluded')assert.equal(typeof defaultModel, 'string')assert.equal(calls[0].url, 'https://ai-gateway.vercel.sh/coding-agent/v1/models')assert.equal(calls[0].headers.get('authorization'), null)