Skip to content
Open
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
82 changes: 82 additions & 0 deletions packages/app/src/providers/models/capability-tags.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { For, Show, createMemo, type Component } from "solid-js"
import { Badge } from "@opencode/ui/badge"
import { useLanguage } from "@/runtime/i18n/language"
import type { ModelSelection } from "./selection"

type ModelInfo = NonNullable<ReturnType<ModelSelection["current"]>>

/** Input modalities worth a badge; text is implied by every model and would only add noise. */
const MODALITIES = ["image", "video", "audio", "pdf"] as const

const MODALITY_KEYS = {
image: "model.details.input.image",
video: "model.details.input.video",
audio: "model.details.input.audio",
pdf: "model.details.input.pdf",
} as const

function contextAndOutput(model: ModelInfo) {
return { context: model.limit?.context ?? 0, output: model.limit?.output ?? 0 }
}

function money(value: number) {
return `$${Number(value.toFixed(2))}`
}

/** Whether a row has anything to badge, so a description line only appears when it earns its space. */
export function hasCapabilityTags(model: ModelInfo) {
const input = model.capabilities?.input
const { context, output } = contextAndOutput(model)
return (
MODALITIES.some((modality) => input?.[modality] === true) ||
model.capabilities?.toolcall === true ||
context > 0 ||
output > 0 ||
(model.cost?.input ?? 0) > 0
)
}

/** The model's input modalities, tool support, token limits and price, for a picker row.
* `compact` keeps the large dialog's single-line row from crowding: limits and price only,
* the rest already live in the hover flyout (ModelDetails). */
export const ModelCapabilityTags: Component<{ model: ModelInfo; compact?: boolean }> = (props) => {
const language = useLanguage()
const compact = createMemo(() =>
new Intl.NumberFormat(language.intl(), {
notation: "compact",
maximumFractionDigits: 0,
roundingMode: "trunc",
}),
)
const limits = () => {
const { context, output } = contextAndOutput(props.model)
if (context && output) return `${compact().format(context)}/${compact().format(output)}`
if (context) return compact().format(context)
if (output) return compact().format(output)
return ""
}
const price = () => {
const cost = props.model.cost
if (!cost || cost.input === 0) return ""
return `${money(cost.input)}/${money(cost.output)}`
}

return (
<Show when={hasCapabilityTags(props.model)}>
<Show when={!props.compact}>
<For each={MODALITIES.filter((modality) => props.model.capabilities?.input[modality] === true)}>
{(modality) => <Badge class="shrink-0">{language.t(MODALITY_KEYS[modality])}</Badge>}
</For>
<Show when={props.model.capabilities?.toolcall}>
<Badge class="shrink-0">{language.t("model.details.tools")}</Badge>
</Show>
</Show>
<Show when={limits()}>
{(value) => <Badge class="shrink-0 tabular-nums">{value()}</Badge>}
</Show>
<Show when={price()}>
{(value) => <Badge class="shrink-0 tabular-nums">{value()}</Badge>}
</Show>
</Show>
)
}
18 changes: 15 additions & 3 deletions packages/app/src/providers/models/details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ export function ModelDetails(props: {
usage?: ModelUsageLabel
}) {
const language = useLanguage()
const input = () => inputs.filter((type) => props.model.capabilities.input[type])
const input = () => inputs.filter((type) => props.model.capabilities?.input[type])
const context = () => props.model.limit?.context ?? 0
const output = () => props.model.limit?.output ?? 0

const modalities = () =>
inputs.filter((type) => props.model.capabilities.input[type] || props.model.capabilities.output[type])
inputs.filter(
(type) => props.model.capabilities?.input[type] || props.model.capabilities?.output[type],
)

const label = (type: Input) => language.t(`model.details.input.${type}`)

Expand Down Expand Up @@ -67,8 +71,16 @@ export function ModelDetails(props: {
<dt class="min-w-0 flex-1 truncate font-[530] text-v2-text-text-faint">
{language.t("model.tooltip.context.label")}
</dt>
<dd class="shrink-0">{props.model.limit.context.toLocaleString(language.intl())}</dd>
<dd class="shrink-0">{context().toLocaleString(language.intl())}</dd>
</div>
<Show when={output() > 0}>
<div class="flex h-6 min-w-0 items-center gap-2 px-2">
<dt class="min-w-0 flex-1 truncate font-[530] text-v2-text-text-faint">
{language.t("model.details.output")}
</dt>
<dd class="shrink-0">{output().toLocaleString(language.intl())}</dd>
</div>
</Show>
</dl>
<Show when={props.usage}>
{(usage) => (
Expand Down
11 changes: 11 additions & 0 deletions packages/app/src/providers/models/select-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Tooltip } from "@opencode/ui/tooltip"
import { TextInput } from "@opencode/ui/text-input"
import { ModelTooltip } from "./tooltip"
import { ModelDetails } from "./details"
import { ModelCapabilityTags, hasCapabilityTags } from "./capability-tags"
import { useLanguage } from "@/runtime/i18n/language"
import { ExternalLink } from "@/runtime/platform/external-link"
import { useData } from "@/runtime/server/current"
Expand Down Expand Up @@ -219,6 +220,11 @@ const ModelList: Component<{
</Show>
<Show when={usage(item)}>{(value) => <ModelUsageTag usage={value()} />}</Show>
</div>
<Show when={hasCapabilityTags(item)}>
<div data-slot="settings-row-description" class="flex items-center gap-1">
<ModelCapabilityTags model={item} />
</div>
</Show>
</div>
<div data-slot="settings-row-control" class="size-4">
<Show when={controller.current() === modelKey(item)}>
Expand Down Expand Up @@ -966,6 +972,11 @@ export function DialogSelectModel(props: { provider?: string; model?: ModelState
<Show when={item().latest}>
<Badge class="shrink-0">{language.t("model.tag.latest")}</Badge>
</Show>
<Show when={hasCapabilityTags(item())}>
<span class="flex shrink-0 items-center gap-1">
<ModelCapabilityTags model={item()} compact />
</span>
</Show>
</span>
<Show when={usage(item())}>{(value) => <ModelUsageTag usage={value()} />}</Show>
<Show when={active()}>
Expand Down
8 changes: 6 additions & 2 deletions packages/tui/src/component/dialog-model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { modelPreferenceKey } from "../model-preference"
import { useLocation } from "../context/location"
import { useNewDesigns } from "../config"
import { DialogFreeModels } from "./dialog-model-free"
import { FavoriteMarker, ModelDetail, isFreeModel, prioritizeFavorites, sortModelOptions } from "./model-options"
import { FavoriteMarker, ModelDetail, isFreeModel, modelLimits, prioritizeFavorites, sortModelOptions } from "./model-options"
import { DialogUsage, useUsageLimits } from "./usage"
import { formatDollars, usageLevel } from "../util/usage-limits"

Expand Down Expand Up @@ -85,7 +85,11 @@ function DialogConnectedModels(props: {
// Under new designs the detail row shows the price, so the trailing slot only marks favorites.
const trailing = (model: ModelInfo, favorite: boolean) => {
if (newDesigns()) return favorite ? <FavoriteMarker /> : undefined
return isFreeModel(model) ? "Free" : undefined
const parts: string[] = []
if (isFreeModel(model)) parts.push("Free")
const limits = modelLimits(model)
if (limits) parts.push(limits)
return parts.length ? parts.join(" ") : undefined
}
// OpenCode's own free models get their own group at the end of the list instead of joining "OpenCode".
const freeGroup = (model: ModelInfo) =>
Expand Down
52 changes: 41 additions & 11 deletions packages/tui/src/component/model-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export function sortModelOptions<
})
}

export function isFreeModel(model: { cost: ReadonlyArray<{ input: number }> }) {
return model.cost.length > 0 && model.cost.every((cost) => cost.input === 0)
export function isFreeModel(model: { cost?: ReadonlyArray<{ input: number }> }) {
// A config-defined model can omit `cost` entirely, so read it as optional even though the type says otherwise.
const cost = model.cost ?? []
return cost.length > 0 && cost.every((item) => item.input === 0)
}

/** The favorite marker. A styled span keeps its color on the highlighted row, where plain trailing text turns secondary. */
Expand All @@ -46,8 +48,8 @@ export function FavoriteMarker() {
}

/**
* Price, input modalities, context window and reasoning support for the selected model, after what its usage limits
* leave when `usage` says so.
* Price, input modalities, context and output limits, reasoning and tool support for the selected model, after what
* its usage limits leave when `usage` says so.
*/
export function ModelDetail(props: {
model: ModelInfo
Expand All @@ -57,16 +59,11 @@ export function ModelDetail(props: {
const theme = useTheme().surface("dialog")
const price = () => {
if (isFreeModel(props.model)) return "Free"
const cost = props.model.cost[0]
const cost = props.model.cost?.[0]
if (!cost) return ""
return `$${dollars(cost.input)}/$${dollars(cost.output)}`
}
const facts = () =>
[
props.model.capabilities.input.map(modality).join(", "),
tokens(props.model.limit.context),
...(props.model.variants.length > 0 ? ["Reasoning"] : []),
].filter(Boolean)
const facts = () => modelFacts(props.model)
const list = (items: string[]) => (
<text fg={theme.text.muted} wrapMode="none" flexShrink={0}>
<For each={items}>
Expand Down Expand Up @@ -119,3 +116,36 @@ function tokens(value: number) {
if (value >= 1_000) return `${Math.round(value / 1_000)}K`
return String(value)
}

/**
* Catalog fields a config-defined model may omit, so the picker still reads when one is missing. `ModelDetail`
* passes a whole `ModelInfo`, which satisfies it.
*/
type FactSource = {
capabilities?: ModelInfo["capabilities"]
limit?: ModelInfo["limit"]
variants?: ModelInfo["variants"]
}

/** Context window and max output tokens, e.g. `200K/8K`, or `""` when the model declares neither. */
export function modelLimits(model: FactSource) {
const context = model.limit?.context ?? 0
const output = model.limit?.output ?? 0
if (context && output) return `${tokens(context)}/${tokens(output)}`
if (context) return tokens(context)
if (output) return tokens(output)
return ""
}

/**
* What the picker states about a model: accepted input, context and output limits, reasoning and tool support.
* Kept separate from `ModelDetail` so a row can be asserted without rendering it.
*/
export function modelFacts(model: FactSource) {
return [
(model.capabilities?.input ?? []).map(modality).join(", "),
modelLimits(model),
...(model.variants?.length ? ["Reasoning"] : []),
...(model.capabilities?.tools ? ["Tools"] : []),
].filter(Boolean)
}
51 changes: 50 additions & 1 deletion packages/tui/test/cli/cmd/tui/model-options.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { describe, expect, test } from "bun:test"
import { go } from "fuzzysort"
import { prioritizeFavorites, sortModelOptions } from "../../../../src/component/model-options"
import { isFreeModel, modelFacts, prioritizeFavorites, sortModelOptions } from "../../../../src/component/model-options"

function model(overrides: Partial<Parameters<typeof modelFacts>[0]> = {}) {
return {
capabilities: { tools: true, input: ["text"], output: ["text"] },
limit: { context: 10_000, output: 1_000 },
variants: [],
...overrides,
}
}

describe("prioritizeFavorites", () => {
test("uses the favorite order captured when the dialog opened", () => {
Expand Down Expand Up @@ -103,3 +112,43 @@ describe("sortModelOptions", () => {
expect(sortModelOptions(options, true).map((model) => model.title)).toEqual(serverOrder)
})
})

describe("modelFacts", () => {
test("states input, context and output limits, and tool support", () => {
expect(
modelFacts(
model({
capabilities: { tools: true, input: ["text", "image", "pdf"], output: ["text"] },
limit: { context: 200_000, output: 8_192 },
}),
),
).toEqual(["Text, Image, PDF", "200K/8K", "Tools"])
})

test("states the output limit on its own when the model declares no context window", () => {
expect(modelFacts(model({ limit: { context: 0, output: 2_048 } }))).toEqual(["Text", "2K", "Tools"])
})

test("leaves the limits out when the model declares neither", () => {
expect(modelFacts(model({ limit: { context: 0, output: 0 } }))).toEqual(["Text", "Tools"])
})

test("marks reasoning when the model carries variants", () => {
expect(modelFacts(model({ variants: [{ id: "high" }] }))).toEqual(["Text", "10K/1K", "Reasoning", "Tools"])
})

test("reads a hand-built model that has no catalog data at all", () => {
expect(modelFacts({ capabilities: undefined, limit: undefined, variants: undefined })).toEqual([])
})
})

describe("isFreeModel", () => {
test("is false for a model that declares no cost", () => {
expect(isFreeModel({})).toBe(false)
})

test("is true only when every tier is free", () => {
expect(isFreeModel({ cost: [{ input: 0 }, { input: 0 }] })).toBe(true)
expect(isFreeModel({ cost: [{ input: 0 }, { input: 3 }] })).toBe(false)
})
})
Loading