Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions packages/tui/src/component/dialog-model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as fuzzysort from "fuzzysort"
import { useConnected } from "./use-connected"
import { useSync } from "../context/sync"
import { useTheme } from "../context/theme"
import * as ModelHealth from "../util/model-health"

export function DialogModel(props: { providerID?: string }) {
const local = useLocal()
Expand Down Expand Up @@ -46,6 +47,7 @@ export function DialogModel(props: { providerID?: string }) {
if (!provider) return []
const model = provider.models[item.modelID]
if (!model) return []
const state = category === "Pinned" ? health({ providerID: provider.id, modelID: model.id }) : undefined
return [
{
key: item,
Expand All @@ -54,23 +56,17 @@ export function DialogModel(props: { providerID?: string }) {
description: category === "Pinned" ? undefined : provider.name,
category,
disabled: provider.id === "opencode" && model.id.includes("-nano"),
titleView:
category === "Pinned"
? (() => {
const state = health({ providerID: provider.id, modelID: model.id })
return (
<>
{model.name ?? item.modelID} <span style={{ fg: state.color }}>● {state.status}</span>
</>
)
})()
titleView: state ? (
<>
{model.name ?? item.modelID} <span style={{ fg: state.color }}>● {state.status}</span>
</>
) : undefined,
footer: state
? state.checked
: model.cost?.input === 0 && provider.id === "opencode"
? "Free"
: undefined,
footer:
category === "Pinned"
? health({ providerID: provider.id, modelID: model.id }).checked
: model.cost?.input === 0 && provider.id === "opencode"
? "Free"
: undefined,
healthStatus: state?.status ?? "unknown",
onSelect: () => {
onSelect(provider.id, model.id)
},
Expand All @@ -79,7 +75,7 @@ export function DialogModel(props: { providerID?: string }) {
})
}

const favoriteOptions = toOptions(favorites, "Pinned")
const favoriteOptions = ModelHealth.sortByStatus(toOptions(favorites, "Pinned"), (option) => option.healthStatus)
const recentOptions = toOptions(
recents.filter(
(item) => !favorites.some((fav) => fav.providerID === item.providerID && fav.modelID === item.modelID),
Expand Down
12 changes: 12 additions & 0 deletions packages/tui/src/util/model-health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ export function age(entry: HealthEntry, now: number): string {
if (minutes < 60) return `${minutes}m ago`
return `${Math.floor(minutes / 60)}h ago`
}

const STATUS_ORDER: Record<HealthStatus, number> = {
working: 0,
degraded: 1,
unknown: 2,
failing: 3,
}

/** Sorts a copy by health, preserving the input order within each status. */
export function sortByStatus<T>(items: readonly T[], getStatus: (item: T) => HealthStatus): T[] {
return [...items].sort((a, b) => STATUS_ORDER[getStatus(a)] - STATUS_ORDER[getStatus(b)])
}
33 changes: 32 additions & 1 deletion packages/tui/test/model-health.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test"
import { age, due, next, PROBE_TTL_MS, STALE_MS, status } from "../src/util/model-health"
import { age, due, next, PROBE_TTL_MS, sortByStatus, STALE_MS, status } from "../src/util/model-health"

describe("model health", () => {
test("missing or stale evidence is unknown, never working", () => {
Expand All @@ -9,6 +9,37 @@ describe("model health", () => {
expect(status(ok, STALE_MS + 1)).toBe("unknown")
})

test("sorts models by health and preserves their order within each status", () => {
const models = [
{ id: "degraded-first", status: "degraded" as const },
{ id: "unknown", status: "unknown" as const },
{ id: "working-first", status: "working" as const },
{ id: "failing", status: "failing" as const },
{ id: "working-second", status: "working" as const },
{ id: "degraded-second", status: "degraded" as const },
]

const sorted = sortByStatus(models, (model) => model.status)

expect(sorted.map((model) => model.id)).toEqual([
"working-first",
"working-second",
"degraded-first",
"degraded-second",
"unknown",
"failing",
])
expect(models.map((model) => model.id)).toEqual([
"degraded-first",
"unknown",
"working-first",
"failing",
"working-second",
"degraded-second",
])
expect(sorted).not.toBe(models)
})

test("one rate limit or network error is not a permanent failure", () => {
const once = next(undefined, "transient", 0)
expect(status(once, 1)).toBe("degraded")
Expand Down
Loading