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
14 changes: 12 additions & 2 deletions packages/opencode/src/config/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,18 @@ export class Info extends Schema.Class<Info>("ProviderConfig")({
env: Schema.optional(Schema.mutable(Schema.Array(Schema.String))),
id: Schema.optional(Schema.String),
npm: Schema.optional(Schema.String),
whitelist: Schema.optional(Schema.mutable(Schema.Array(Schema.String))),
blacklist: Schema.optional(Schema.mutable(Schema.Array(Schema.String))),
whitelist: Schema.optional(
Schema.mutable(Schema.Array(Schema.String)).annotate({
description:
"Allowed model filters. Plain strings match exact model IDs. Entries wrapped in /.../ are regex patterns matched against model IDs. Invalid regex patterns are ignored. Regex flags are not supported.",
}),
),
blacklist: Schema.optional(
Schema.mutable(Schema.Array(Schema.String)).annotate({
description:
"Blocked model filters. Plain strings match exact model IDs. Entries wrapped in /.../ are regex patterns matched against model IDs. Invalid regex patterns are ignored. Regex flags are not supported.",
}),
),
options: Schema.optional(
Schema.StructWithRest(
Schema.Struct({
Expand Down
16 changes: 14 additions & 2 deletions packages/opencode/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ function shouldUseCopilotResponsesApi(modelID: string): boolean {
return Number(match[1]) >= 5 && !modelID.startsWith("gpt-5-mini")
}

function matchesModelFilter(entry: string, modelID: string) {
if (entry.startsWith("/") && entry.endsWith("/") && entry.length >= 2) {
try {
return new RegExp(entry.slice(1, -1)).test(modelID)
} catch {
return false
}
}

return entry === modelID
}

function wrapSSE(res: Response, ms: number, ctl: AbortController) {
if (typeof ms !== "number" || ms <= 0) return res
if (!res.body) return res
Expand Down Expand Up @@ -1336,8 +1348,8 @@ const layer: Layer.Layer<
if (model.status === "alpha" && !Flag.OPENCODE_ENABLE_EXPERIMENTAL_MODELS) delete provider.models[modelID]
if (model.status === "deprecated") delete provider.models[modelID]
if (
(configProvider?.blacklist && configProvider.blacklist.includes(modelID)) ||
(configProvider?.whitelist && !configProvider.whitelist.includes(modelID))
(configProvider?.blacklist && configProvider.blacklist.some((entry) => matchesModelFilter(entry, modelID))) ||
(configProvider?.whitelist && !configProvider.whitelist.some((entry) => matchesModelFilter(entry, modelID)))
)
delete provider.models[modelID]

Expand Down
126 changes: 126 additions & 0 deletions packages/opencode/test/provider/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,99 @@ test("model blacklist excludes specific models", async () => {
})
})

test("model whitelist supports regex filters", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
anthropic: {
whitelist: ["/^claude-sonnet-/"],
},
},
}),
)
},
})
await Instance.provide({
directory: tmp.path,
init: async () => {
set("ANTHROPIC_API_KEY", "test-api-key")
},
fn: async () => {
const providers = await list()
expect(providers[ProviderID.anthropic]).toBeDefined()
const models = Object.keys(providers[ProviderID.anthropic].models)
expect(models).toContain("claude-sonnet-4-20250514")
expect(models.every((model) => model.startsWith("claude-sonnet-"))).toBeTrue()
},
})
})

test("model blacklist supports regex filters", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
anthropic: {
blacklist: ["/^claude-opus-/"],
},
},
}),
)
},
})
await Instance.provide({
directory: tmp.path,
init: async () => {
set("ANTHROPIC_API_KEY", "test-api-key")
},
fn: async () => {
const providers = await list()
expect(providers[ProviderID.anthropic]).toBeDefined()
const models = Object.keys(providers[ProviderID.anthropic].models)
expect(models).not.toContain("claude-opus-4-20250514")
expect(models.some((model) => model.startsWith("claude-sonnet-"))).toBeTrue()
},
})
})

test("invalid regex filters are ignored", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
anthropic: {
blacklist: ["/[abc/"],
},
},
}),
)
},
})
await Instance.provide({
directory: tmp.path,
init: async () => {
set("ANTHROPIC_API_KEY", "test-api-key")
},
fn: async () => {
const providers = await list()
expect(providers[ProviderID.anthropic]).toBeDefined()
const models = Object.keys(providers[ProviderID.anthropic].models)
expect(models).toContain("claude-sonnet-4-20250514")
expect(models).toContain("claude-opus-4-20250514")
},
})
})

test("custom model alias via config", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
Expand Down Expand Up @@ -889,6 +982,39 @@ test("whitelist and blacklist can be combined", async () => {
})
})

test("whitelist and blacklist can be combined with regex filters", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Bun.write(
path.join(dir, "opencode.json"),
JSON.stringify({
$schema: "https://opencode.ai/config.json",
provider: {
anthropic: {
whitelist: ["/^claude-/"],
blacklist: ["/^claude-opus-/"],
},
},
}),
)
},
})
await Instance.provide({
directory: tmp.path,
init: async () => {
set("ANTHROPIC_API_KEY", "test-api-key")
},
fn: async () => {
const providers = await list()
expect(providers[ProviderID.anthropic]).toBeDefined()
const models = Object.keys(providers[ProviderID.anthropic].models)
expect(models).toContain("claude-sonnet-4-20250514")
expect(models).not.toContain("claude-opus-4-20250514")
expect(models.every((model) => model.startsWith("claude-"))).toBeTrue()
},
})
})

test("model modalities default correctly", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -11147,12 +11147,14 @@
"type": "string"
},
"whitelist": {
"description": "Allowed model filters. Plain strings match exact model IDs. Entries wrapped in /.../ are regex patterns matched against model IDs. Invalid regex patterns are ignored. Regex flags are not supported.",
"type": "array",
"items": {
"type": "string"
}
},
"blacklist": {
"description": "Blocked model filters. Plain strings match exact model IDs. Entries wrapped in /.../ are regex patterns matched against model IDs. Invalid regex patterns are ignored. Regex flags are not supported.",
"type": "array",
"items": {
"type": "string"
Expand Down
Loading