Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(tui): minimize provider sorting changes
  • Loading branch information
rekram1-node committed Jun 7, 2026
commit e65e951f7f89c675c55f239701d5b03014ad4bef
51 changes: 24 additions & 27 deletions packages/opencode/src/cli/cmd/tui/component/dialog-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import { isConsoleManagedProvider } from "@tui/util/provider-origin"
import { useConnected } from "./use-connected"
import { useBindings } from "../keymap"

const PROVIDER_PRIORITY: Record<string, number> = {
opencode: 0,
"opencode-go": 1,
openai: 2,
"github-copilot": 3,
anthropic: 4,
google: 5,
}

const CUSTOM_PROVIDER_OPTION_VALUE = "__opencode_custom_provider__"
const CUSTOM_PROVIDER_ID = /^[a-z0-9][a-z0-9-_]*$/

Expand All @@ -35,40 +44,28 @@ type ProviderOption =
type: "custom"
})

const popularProviders = ["opencode", "opencode-go", "anthropic", "github-copilot", "openai", "google", "openrouter", "vercel"]
const popularProviderSet = new Set(popularProviders)

export function providerOptions(list: { id: string; name: string }[]): ProviderOption[] {
const popular = list.filter((x) => popularProviderSet.has(x.id))
const rest = list.filter((x) => !popularProviderSet.has(x.id))

const toOption = (provider: { id: string; name: string }, category: string) => ({
type: "provider" as const,
title: provider.name,
value: provider.id,
providerID: provider.id,
description: {
opencode: "(Recommended)",
anthropic: "(API key)",
openai: "(ChatGPT Plus/Pro or API key)",
"opencode-go": "Low cost subscription for everyone",
}[provider.id],
category,
})

return [
...pipe(
popular,
sortBy((x) => popularProviders.indexOf(x.id)),
map((provider) => toOption(provider, "Popular")),
),
...pipe(
rest,
list,
sortBy(
(x) => PROVIDER_PRIORITY[x.id] ?? 99,
(x) => x.name.toLowerCase(),
(x) => x.id,
),
map((provider) => toOption(provider, "Providers")),
map((provider) => ({
type: "provider" as const,
title: provider.name,
value: provider.id,
providerID: provider.id,
description: {
opencode: "(Recommended)",
anthropic: "(API key)",
openai: "(ChatGPT Plus/Pro or API key)",
"opencode-go": "Low cost subscription for everyone",
}[provider.id],
category: provider.id in PROVIDER_PRIORITY ? "Popular" : "Providers",
})),
),
{
type: "custom",
Expand Down
45 changes: 10 additions & 35 deletions packages/opencode/test/cli/cmd/tui/provider-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,16 @@ describe("providerOptions", () => {
expect(providerOptions([{ id: "mistral", name: "Mistral" }])[0]?.category).toBe("Providers")
})

test("sorts popular providers first in defined order, then rest alphabetically, and keeps Other last", () => {
const options = providerOptions([
{ id: "openai", name: "OpenAI" },
{ id: "custom-z", name: "Zebra Provider" },
{ id: "anthropic", name: "Anthropic" },
{ id: "google", name: "Google" },
{ id: "mistral", name: "Mistral" },
{ id: "aws", name: "AWS Bedrock" },
])

expect(options.map((option) => option.value)).toEqual([
// Popular providers in defined order
"anthropic",
"openai",
"google",
// Non-popular providers sorted alphabetically by name
"aws",
"mistral",
"custom-z",
// Other always last
"__opencode_custom_provider__",
])
})

test("assigns Popular category to popular providers and Providers to others", () => {
const options = providerOptions([
{ id: "openai", name: "OpenAI" },
{ id: "mistral", name: "Mistral" },
])

const openai = options.find((o) => o.value === "openai")
const mistral = options.find((o) => o.value === "mistral")

expect(openai?.category).toBe("Popular")
expect(mistral?.category).toBe("Providers")
test("keeps popular providers first and sorts the rest alphabetically", () => {
expect(
providerOptions([
{ id: "openai", name: "OpenAI" },
{ id: "custom-z", name: "Zebra Provider" },
{ id: "anthropic", name: "Anthropic" },
{ id: "mistral", name: "Mistral" },
{ id: "aws", name: "AWS Bedrock" },
]).map((option) => option.value),
).toEqual(["openai", "anthropic", "aws", "mistral", "custom-z", "__opencode_custom_provider__"])
})

test("does not collide with a configured provider named other", () => {
Expand Down
Loading