From 467d61f9d0ef3121e29d7d59d9547abbc07812ad Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Sun, 25 Oct 2026 13:18:53 -0700 Subject: [PATCH] chore(app): drop remeda from app and ui (#59016) --- bun.lock | 2 - packages/app/package.json | 1 - .../app/src/providers/models/models.test.ts | 55 +++++++++++++++ packages/app/src/providers/models/models.tsx | 52 ++++++++------ packages/ui/package.json | 1 - .../ui/src/hooks/use-filtered-list.test.ts | 41 +++++++++++ packages/ui/src/hooks/use-filtered-list.tsx | 69 +++++++++++-------- 7 files changed, 167 insertions(+), 54 deletions(-) create mode 100644 packages/app/src/providers/models/models.test.ts create mode 100644 packages/ui/src/hooks/use-filtered-list.test.ts diff --git a/bun.lock b/bun.lock index b2310667f97..2c964a302a1 100644 --- a/bun.lock +++ b/bun.lock @@ -88,7 +88,6 @@ "effect": "catalog:", "fuzzysort": "catalog:", "qr-scanner": "1.4.2", - "remeda": "catalog:", "solid-js": "catalog:", "tailwindcss": "4.3.3", }, @@ -863,7 +862,6 @@ "katex": "0.16.47", "marked": "catalog:", "motion": "12.34.5", - "remeda": "catalog:", "solid-list": "catalog:", "solid-sonner": "catalog:", }, diff --git a/packages/app/package.json b/packages/app/package.json index 33cd5a18c6c..d65cc256029 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -85,7 +85,6 @@ "effect": "catalog:", "fuzzysort": "catalog:", "qr-scanner": "1.4.2", - "remeda": "catalog:", "solid-js": "catalog:", "tailwindcss": "4.3.3" } diff --git a/packages/app/src/providers/models/models.test.ts b/packages/app/src/providers/models/models.test.ts new file mode 100644 index 00000000000..e60c7f79e96 --- /dev/null +++ b/packages/app/src/providers/models/models.test.ts @@ -0,0 +1,55 @@ +import { expect, test } from "bun:test" +import { latestModels, uniqueModels } from "./models" + +const model = (provider: string, id: string, release_date: string, family?: string) => ({ + id, + family, + release_date, + provider: { id: provider }, +}) + +test("keeps the newest model of each provider and family", () => { + expect( + latestModels([ + model("a", "old", "2026-01-01", "f"), + model("a", "new", "2026-03-01", "f"), + model("a", "other", "2026-02-01", "g"), + model("b", "b-only", "2026-01-01", "f"), + ]), + ).toEqual([ + { providerID: "a", modelID: "new" }, + { providerID: "a", modelID: "other" }, + { providerID: "b", modelID: "b-only" }, + ]) +}) + +test("a tie in release date keeps the first model listed", () => { + expect( + latestModels([model("a", "first", "2026-01-01", "f"), model("a", "second", "2026-01-01", "f")]), + ).toEqual([{ providerID: "a", modelID: "first" }]) +}) + +test("groups free models apart from their paid twin", () => { + const free = { ...model("opencode", "x-free", "2026-01-01", "x"), cost: { input: 0 } } + const paid = { ...model("opencode", "x", "2026-01-01", "x"), cost: { input: 1 } } + + expect(latestModels([free, paid])).toEqual([ + { providerID: "opencode", modelID: "x-free" }, + { providerID: "opencode", modelID: "x" }, + ]) +}) + +test("uniqueModels keeps the first of each model in order", () => { + expect( + uniqueModels([ + { providerID: "a", modelID: "1" }, + { providerID: "b", modelID: "1" }, + { providerID: "a", modelID: "1" }, + { providerID: "a", modelID: "2" }, + ]), + ).toEqual([ + { providerID: "a", modelID: "1" }, + { providerID: "b", modelID: "1" }, + { providerID: "a", modelID: "2" }, + ]) +}) diff --git a/packages/app/src/providers/models/models.tsx b/packages/app/src/providers/models/models.tsx index 33b67a9d520..1ddc68602e6 100644 --- a/packages/app/src/providers/models/models.tsx +++ b/packages/app/src/providers/models/models.tsx @@ -1,5 +1,4 @@ import { type Accessor, createMemo } from "solid-js" -import { filter, firstBy, flat, groupBy, mapValues, pipe, uniqueBy, values } from "remeda" import { createSimpleContext } from "@opencode/ui/context" import { useProviders } from "@/providers/catalog/providers" import { useGlobal } from "@/runtime/server/runtime" @@ -20,6 +19,33 @@ function modelKey(model: ModelKey) { export const isFreeModel = (model: { provider: { id: string }; cost?: { input: number } }) => model.provider.id === "opencode" && (!model.cost || model.cost.input === 0) +// The newest release of each family per provider. A free model shares its family and release date with its paid twin, +// so the two are grouped apart. A tie keeps the first model listed. +export function latestModels( + models: T[], +) { + return [...Map.groupBy(models, (x) => x.provider.id).values()].flatMap((provider) => + [...Map.groupBy(provider, (x) => `${x.family}:${isFreeModel(x)}`).values()].map((group) => { + const first = group.reduce((best, x) => (x.release_date > best.release_date ? x : best)) + + return { modelID: first.id, providerID: first.provider.id } + }), + ) +} + +export function uniqueModels(models: ModelKey[]) { + const seen = new Set() + + return models.filter((model) => { + const key = modelKey(model) + + if (seen.has(key)) return false + seen.add(key) + + return true + }) +} + const createModelsController = (directory: Accessor) => { const providers = useProviders(() => directory()) const models = useGlobal().models @@ -46,30 +72,12 @@ const createModelsController = (directory: Accessor) => { ) const latest = createMemo(() => - pipe( - available(), - filter((x) => { + latestModels( + available().filter((x) => { const released = release().get(modelKey({ providerID: x.provider.id, modelID: x.id })) ?? NaN return Math.abs(Date.now() - released) < sixMonths }), - groupBy((x) => x.provider.id), - mapValues((models) => - pipe( - models, - // A free model shares its family and release date with its paid twin, so group them apart. - groupBy((x) => `${x.family}:${isFreeModel(x)}`), - values(), - (groups) => - groups.flatMap((g) => { - const first = firstBy(g, [(x) => x.release_date, "desc"]) - - return first ? [{ modelID: first.id, providerID: first.provider.id }] : [] - }), - ), - ), - values(), - flat(), ), ) @@ -124,7 +132,7 @@ const createModelsController = (directory: Accessor) => { } const push = (model: ModelKey) => { - const uniq = uniqueBy([model, ...store.recent], (x) => `${x.providerID}:${x.modelID}`) + const uniq = uniqueModels([model, ...store.recent]) if (uniq.length > RECENT_LIMIT) uniq.pop() setStore("recent", uniq) diff --git a/packages/ui/package.json b/packages/ui/package.json index b82162fa99b..2f480406747 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -121,7 +121,6 @@ "katex": "0.16.47", "marked": "catalog:", "motion": "12.34.5", - "remeda": "catalog:", "solid-list": "catalog:", "solid-sonner": "catalog:" }, diff --git a/packages/ui/src/hooks/use-filtered-list.test.ts b/packages/ui/src/hooks/use-filtered-list.test.ts new file mode 100644 index 00000000000..cb9dd1a9639 --- /dev/null +++ b/packages/ui/src/hooks/use-filtered-list.test.ts @@ -0,0 +1,41 @@ +import { expect, test } from "bun:test" +import { groupItems } from "./use-filtered-list" + +test("groups under one empty category without a groupBy", () => { + expect(groupItems(["b", "a"], {})).toEqual([{ category: "", items: ["b", "a"] }]) +}) + +test("keeps item order within a group and first-seen order between groups", () => { + const items = [ + { name: "x1", group: "x" }, + { name: "y1", group: "y" }, + { name: "x2", group: "x" }, + ] + + expect(groupItems(items, { groupBy: (item) => item.group }).map((g) => [g.category, g.items.map((i) => i.name)])).toEqual([ + ["x", ["x1", "x2"]], + ["y", ["y1"]], + ]) +}) + +test("lists numeric-looking categories first, as object keys do", () => { + expect(groupItems(["b", "10", "a", "2"], { groupBy: (item) => item }).map((g) => g.category)).toEqual([ + "2", + "10", + "b", + "a", + ]) +}) + +test("sorts items within groups and then the groups", () => { + const groups = groupItems([3, 1, 4, 2], { + groupBy: (n) => (n % 2 ? "odd" : "even"), + sortBy: (a, b) => a - b, + sortGroupsBy: (a, b) => a.category.localeCompare(b.category), + }) + + expect(groups).toEqual([ + { category: "even", items: [2, 4] }, + { category: "odd", items: [1, 3] }, + ]) +}) diff --git a/packages/ui/src/hooks/use-filtered-list.tsx b/packages/ui/src/hooks/use-filtered-list.tsx index 4e2bfcb9dc5..0635f3fef8e 100644 --- a/packages/ui/src/hooks/use-filtered-list.tsx +++ b/packages/ui/src/hooks/use-filtered-list.tsx @@ -1,5 +1,4 @@ import fuzzysort from "fuzzysort" -import { entries, flatMap, groupBy, map, pipe } from "remeda" import { createEffect, createMemo, createResource, on } from "solid-js" import { createStore } from "solid-js/store" import { createList } from "solid-list" @@ -17,6 +16,27 @@ export interface FilteredListProps { noInitialSelection?: boolean } +export function groupItems( + items: T[], + props: Pick, "groupBy" | "sortBy" | "sortGroupsBy">, +) { + // A plain object keeps the group order of `Object.entries`, where numeric-looking categories sort first. + const byCategory: Record = {} + + for (const item of items) { + const category = props.groupBy ? props.groupBy(item) : "" + byCategory[category] ??= [] + byCategory[category].push(item) + } + + const groups = Object.entries(byCategory).map(([category, group]) => ({ + category, + items: props.sortBy ? group.sort(props.sortBy) : group, + })) + + return props.sortGroupsBy ? groups.sort(props.sortGroupsBy) : groups +} + export function useFilteredList(props: FilteredListProps) { const [store, setStore] = createStore<{ filter: string }>({ filter: "" }) @@ -24,9 +44,26 @@ export function useFilteredList(props: FilteredListProps) { const empty: Group[] = [] + const search = (all: T[], needle: string) => { + const skipFilter = props.skipFilter + const filterable = skipFilter ? all.filter((item) => !skipFilter(item)) : all + const skipped = skipFilter ? all.filter(skipFilter) : [] + + // oxlint-disable-next-line anti-slop/no-runtime-typeof -- plain string lists have no keys to search by + const strings = !props.filterKeys && filterable.every((e) => typeof e === "string") + + // SAFETY: when `strings` holds, every element is a string, so the matched targets are the items. + const filtered = strings + ? (fuzzysort.go(needle, filterable as string[]).map((x) => x.target) as T[]) + : fuzzysort.go(needle, filterable, { keys: props.filterKeys! }).map((x) => x.obj) + + return skipped.length ? [...filtered, ...skipped] : filtered + } + const [grouped, { refetch }] = createResource( () => ({ filter: store.filter, + // oxlint-disable-next-line anti-slop/no-runtime-typeof -- `items` is either a list or a loader for one items: typeof props.items === "function" ? props.items(store.filter) : props.items, }), async ({ filter, items }) => { @@ -34,38 +71,14 @@ export function useFilteredList(props: FilteredListProps) { const needle = query.toLowerCase() const all = (await Promise.resolve(items)) || [] - const result = pipe( - all, - (x) => { - if (!needle) return x - const skipFilter = props.skipFilter - const filterable = skipFilter ? x.filter((item) => !skipFilter(item)) : x - const skipped = skipFilter ? x.filter(skipFilter) : [] + const matched = needle ? search(all, needle) : all - const filtered = - !props.filterKeys && Array.isArray(filterable) && filterable.every((e) => typeof e === "string") - ? (fuzzysort.go(needle, filterable).map((x) => x.target) as T[]) - : fuzzysort.go(needle, filterable, { keys: props.filterKeys! }).map((x) => x.obj) - - return skipped.length ? [...filtered, ...skipped] : filtered - }, - groupBy((x) => (props.groupBy ? props.groupBy(x) : "")), - entries(), - map(([k, v]) => ({ category: k, items: props.sortBy ? v.sort(props.sortBy) : v })), - (groups) => (props.sortGroupsBy ? groups.sort(props.sortGroupsBy) : groups), - ) - - return result + return groupItems(matched, props) }, { initialValue: empty }, ) - const flat = createMemo(() => { - return pipe( - grouped.latest || [], - flatMap((x) => x.items), - ) - }) + const flat = createMemo(() => (grouped.latest || []).flatMap((x) => x.items)) function initialActive() { if (props.noInitialSelection) return ""