mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
chore(app): drop remeda from app and ui (#59016)
This commit is contained in:
committed by
GitHub
Unverified
parent
193a556e9e
commit
467d61f9d0
@@ -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:",
|
||||
},
|
||||
|
||||
@@ -85,7 +85,6 @@
|
||||
"effect": "catalog:",
|
||||
"fuzzysort": "catalog:",
|
||||
"qr-scanner": "1.4.2",
|
||||
"remeda": "catalog:",
|
||||
"solid-js": "catalog:",
|
||||
"tailwindcss": "4.3.3"
|
||||
}
|
||||
|
||||
@@ -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" },
|
||||
])
|
||||
})
|
||||
@@ -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<T extends { id: string; provider: { id: string }; family?: string; release_date: string }>(
|
||||
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<string>()
|
||||
|
||||
return models.filter((model) => {
|
||||
const key = modelKey(model)
|
||||
|
||||
if (seen.has(key)) return false
|
||||
seen.add(key)
|
||||
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
const createModelsController = (directory: Accessor<string | undefined>) => {
|
||||
const providers = useProviders(() => directory())
|
||||
const models = useGlobal().models
|
||||
@@ -46,30 +72,12 @@ const createModelsController = (directory: Accessor<string | undefined>) => {
|
||||
)
|
||||
|
||||
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<string | undefined>) => {
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -121,7 +121,6 @@
|
||||
"katex": "0.16.47",
|
||||
"marked": "catalog:",
|
||||
"motion": "12.34.5",
|
||||
"remeda": "catalog:",
|
||||
"solid-list": "catalog:",
|
||||
"solid-sonner": "catalog:"
|
||||
},
|
||||
|
||||
@@ -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] },
|
||||
])
|
||||
})
|
||||
@@ -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<T> {
|
||||
noInitialSelection?: boolean
|
||||
}
|
||||
|
||||
export function groupItems<T>(
|
||||
items: T[],
|
||||
props: Pick<FilteredListProps<T>, "groupBy" | "sortBy" | "sortGroupsBy">,
|
||||
) {
|
||||
// A plain object keeps the group order of `Object.entries`, where numeric-looking categories sort first.
|
||||
const byCategory: Record<string, T[]> = {}
|
||||
|
||||
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<T>(props: FilteredListProps<T>) {
|
||||
const [store, setStore] = createStore<{ filter: string }>({ filter: "" })
|
||||
|
||||
@@ -24,9 +44,26 @@ export function useFilteredList<T>(props: FilteredListProps<T>) {
|
||||
|
||||
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<T>(props: FilteredListProps<T>) {
|
||||
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 ""
|
||||
|
||||
Reference in New Issue
Block a user