Skip to content

Commit 6a390b8

Browse files
andrewesweetclaude
andcommitted
feat(provider): key provider behaviour off npm where unique
Switch the custom-loader iteration to walk merged provider entries and resolve loaders by exact ID match or unique-npm fallback. A user-defined provider entry with `npm: "@ai-sdk/google-vertex"` (and any other unique-npm provider) now inherits the canonical loader's setup, enabling multi-region Vertex AI in one session. Migrate behavioural literal-ID checks in provider.ts, transform.ts, and agent.ts to `model.api.npm === NPM.X` wherever the canonical loader's npm value is unique. ID-keyed checks stay in place for the `@ai-sdk/openai-compatible` family (7 loaders share that npm) and the azure pair (`azure` plus `azure-cognitive-services`). Add `packages/opencode/src/provider/npm.ts` with named constants so the migrated equality checks live in one place and typos become compile errors. Fix `google-vertex-anthropic` loader to honour `provider.options.project` and `provider.options.location` from config, aligning it with the `google-vertex` loader (drive-by, two-line change). Refs anomalyco#28524 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent a568f61 commit 6a390b8

5 files changed

Lines changed: 373 additions & 82 deletions

File tree

‎packages/opencode/src/agent/agent.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { generateObject, streamObject, type ModelMessage } from "ai"
66
import { Truncate } from "@/tool/truncate"
77
import { Auth } from "../auth"
88
import { ProviderTransform } from "@/provider/transform"
9+
import { NPM } from "@/provider/npm"
910

1011
import PROMPT_GENERATE from "./generate.txt"
1112
import PROMPT_COMPACTION from "./prompt/compaction.txt"
@@ -399,7 +400,7 @@ export const layer = Layer.effect(
399400

400401
// TODO: clean this up so provider specific logic doesnt bleed over
401402
const authInfo = yield* auth.get(model.providerID).pipe(Effect.orDie)
402-
const isOpenaiOauth = model.providerID === "openai" && authInfo?.type === "oauth"
403+
const isOpenaiOauth = resolved.api.npm === NPM.OPENAI && authInfo?.type === "oauth"
403404

404405
const params = {
405406
experimental_telemetry: {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Centralised npm package identifiers for the AI SDK providers we discriminate
2+
// behaviour on. Use these constants in `model.api.npm === NPM.X` checks and as
3+
// keys in `BUNDLED_PROVIDERS` so the canonical strings live in one place.
4+
//
5+
// A given npm value may map to more than one canonical loader ID (the
6+
// `@ai-sdk/openai-compatible` family and the azure pair). Behaviour that
7+
// genuinely needs to discriminate within those groups must key off the
8+
// provider ID, not the npm value.
9+
10+
export const NPM = {
11+
GOOGLE_VERTEX: "@ai-sdk/google-vertex",
12+
GOOGLE_VERTEX_ANTHROPIC: "@ai-sdk/google-vertex/anthropic",
13+
AMAZON_BEDROCK: "@ai-sdk/amazon-bedrock",
14+
ANTHROPIC: "@ai-sdk/anthropic",
15+
OPENAI: "@ai-sdk/openai",
16+
OPENAI_COMPATIBLE: "@ai-sdk/openai-compatible",
17+
XAI: "@ai-sdk/xai",
18+
CEREBRAS: "@ai-sdk/cerebras",
19+
VERCEL_GATEWAY: "@ai-sdk/gateway",
20+
GOOGLE: "@ai-sdk/google",
21+
AZURE: "@ai-sdk/azure",
22+
MISTRAL: "@ai-sdk/mistral",
23+
ALIBABA: "@ai-sdk/alibaba",
24+
GITHUB_COPILOT_SHIM: "@ai-sdk/github-copilot",
25+
OPENROUTER: "@openrouter/ai-sdk-provider",
26+
SAP_AI: "@jerome-benoit/sap-ai-provider-v2",
27+
GITLAB: "gitlab-ai-provider",
28+
CLOUDFLARE_AI_GATEWAY: "ai-gateway-provider",
29+
VENICE: "venice-ai-sdk-provider",
30+
} as const
31+
32+
export type NpmValue = (typeof NPM)[keyof typeof NPM]

‎packages/opencode/src/provider/provider.ts‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { optionalOmitUndefined } from "@opencode-ai/core/schema"
2727
import * as ProviderTransform from "./transform"
2828
import { ModelID, ProviderID } from "./schema"
2929
import { ModelStatus } from "./model-status"
30+
import { NPM } from "./npm"
3031
import { RuntimeFlags } from "@/effect/runtime-flags"
3132

3233
const log = Log.create({ service: "provider" })
@@ -501,10 +502,13 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
501502
},
502503
}
503504
}),
504-
"google-vertex-anthropic": Effect.fnUntraced(function* () {
505+
"google-vertex-anthropic": Effect.fnUntraced(function* (provider: Info) {
505506
const env = yield* dep.env()
506-
const project = env["GOOGLE_CLOUD_PROJECT"] ?? env["GCP_PROJECT"] ?? env["GCLOUD_PROJECT"]
507-
const location = env["GOOGLE_CLOUD_LOCATION"] ?? env["VERTEX_LOCATION"] ?? "global"
507+
const project =
508+
provider.options?.project ?? env["GOOGLE_CLOUD_PROJECT"] ?? env["GCP_PROJECT"] ?? env["GCLOUD_PROJECT"]
509+
const location = String(
510+
provider.options?.location ?? env["GOOGLE_CLOUD_LOCATION"] ?? env["VERTEX_LOCATION"] ?? "global",
511+
)
508512
const autoload = Boolean(project)
509513
if (!autoload) return { autoload: false }
510514
return {
@@ -1399,15 +1403,26 @@ export const layer = Layer.effect(
13991403
mergeProvider(providerID, patch)
14001404
}
14011405

1402-
for (const [id, fn] of Object.entries(custom(dep))) {
1406+
const loaders = custom(dep)
1407+
const resolveLoader = (entryID: string, data: Info): CustomLoader | undefined => {
1408+
// Exact match: canonical loader entry. Preserves prior behaviour.
1409+
if (loaders[entryID]) return loaders[entryID]
1410+
// Alias: look at the entry's npm (from any model) and accept a unique
1411+
// canonical loader whose models.dev npm matches. Ambiguous npm values
1412+
// (e.g. shared by the @ai-sdk/openai-compatible family) fall through
1413+
// and the entry is handled by the generic SDK resolver.
1414+
const firstModel = Object.values(data.models)[0]
1415+
const entryNpm = firstModel?.api?.npm
1416+
if (!entryNpm) return undefined
1417+
const candidates = Object.keys(loaders).filter((id) => modelsDev[id]?.npm === entryNpm)
1418+
return candidates.length === 1 ? loaders[candidates[0]] : undefined
1419+
}
1420+
for (const [id, data] of Object.entries(database)) {
14031421
const providerID = ProviderID.make(id)
14041422
if (disabled.has(providerID)) continue
1405-
const data = database[providerID]
1406-
if (!data) {
1407-
log.error("Provider does not exist in model list " + providerID)
1408-
continue
1409-
}
1410-
const result = yield* fn(data)
1423+
const loader = resolveLoader(id, data)
1424+
if (!loader) continue
1425+
const result = yield* loader(data)
14111426
if (result && (result.autoload || providers[providerID])) {
14121427
if (result.getModel) modelLoaders[providerID] = result.getModel
14131428
if (result.vars) varsLoaders[providerID] = result.vars
@@ -1516,7 +1531,7 @@ export const layer = Layer.effect(
15161531
const provider = s.providers[model.providerID]
15171532
const options = { ...provider.options }
15181533

1519-
if (model.providerID === "google-vertex" && !model.api.npm.includes("@ai-sdk/openai-compatible")) {
1534+
if (model.api.npm === NPM.GOOGLE_VERTEX) {
15201535
delete options.fetch
15211536
}
15221537

@@ -1746,8 +1761,9 @@ export const layer = Layer.effect(
17461761
if (providerID.startsWith("github-copilot")) {
17471762
priority = ["gpt-5-mini", "claude-haiku-4.5", ...priority]
17481763
}
1764+
const firstModelNpm = Object.values(provider.models)[0]?.api?.npm
17491765
for (const item of priority) {
1750-
if (providerID === ProviderID.amazonBedrock) {
1766+
if (firstModelNpm === NPM.AMAZON_BEDROCK) {
17511767
const crossRegionPrefixes = ["global.", "us.", "eu."]
17521768
const candidates = Object.keys(provider.models).filter((m) => m.includes(item))
17531769

0 commit comments

Comments
 (0)