Skip to content

Commit e027128

Browse files
fix(core): reject v2 providers npm that conflicts with the resolved package (#30)
A `providers.<id>` block could override the endpoint through `api.url` or `request.body.baseURL` while its `npm` key was discarded as an unknown property. The catalog's SDK was then paired with the configured host, so the Anthropic `/v1/messages` path was sent to an OpenAI-compatible host and 404'd at request time with nothing reporting that the override was dropped. `npm` is now captured by the config schema and checked against the package resolution will actually use. A disagreement fails at config resolution with a message naming the requested package, the resolved package, and the URL the request would have used. Closes #28 Claude-Session: https://claude.ai/code/session_016rycAgbtqsRQH4qBv8nkE7
1 parent a2ec547 commit e027128

4 files changed

Lines changed: 182 additions & 0 deletions

File tree

‎.changeset/gentle-pugs-shave.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"opencode": patch
3+
---
4+
5+
Reject provider config that sets `npm` when the resolved provider package disagrees, instead of silently dropping the override. A `providers.<id>` block could override the endpoint (through `api.url` or `request.body.baseURL`) while its `npm` key was discarded as an unknown property, so the catalog's SDK was combined with the configured host into an endpoint neither source describes — for example the Anthropic `/v1/messages` path sent to an OpenAI-compatible host. The conflict now fails at config resolution with a message naming the requested package, the resolved package, and the URL the request would have used.

‎packages/core/src/config/plugin/provider.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,12 @@ export const Plugin = define({
4747
const model = ModelV2.parse(configuredDefault)
4848
catalog.model.default.set(model.providerID, model.modelID)
4949
}
50+
// Collected across every file so the check runs against the api a later file may still replace.
51+
const requested = new Map<string, string>()
5052
for (const file of files) {
5153
for (const [id, item] of Object.entries(file.info.providers ?? {})) {
5254
const providerID = id
55+
if (item.npm !== undefined) requested.set(providerID, item.npm)
5356
catalog.provider.update(providerID, (provider) => {
5457
if (item.name !== undefined) provider.name = item.name
5558
if (item.api !== undefined) provider.api = { ...item.api }
@@ -107,7 +110,31 @@ export const Plugin = define({
107110
}
108111
}
109112
}
113+
for (const [providerID, npm] of requested) {
114+
const conflict = describeConflict(providerID, npm, catalog.provider.get(providerID)?.provider.api)
115+
if (conflict) return yield* Effect.die(conflict)
116+
}
110117
}),
111118
)
112119
}),
113120
})
121+
122+
/**
123+
* A provider block may override the endpoint through `api.url` or `request.body.baseURL`, but `npm`
124+
* only names a package the catalog already chose. Honouring one half while dropping the other builds
125+
* an endpoint neither source describes, so a disagreement is reported instead of resolved.
126+
*/
127+
function describeConflict(providerID: string, npm: string, api: ProviderV2.Api | undefined) {
128+
const resolved = api?.type === "aisdk" ? api.package : undefined
129+
if (resolved === npm) return
130+
const url = api?.url ?? "the package default endpoint"
131+
return [
132+
`Provider "${providerID}" cannot be resolved: config asks for npm "${npm}"`,
133+
resolved === undefined
134+
? `but the provider resolves to a ${api?.type ?? "missing"} api with no package,`
135+
: `but the resolved package is "${resolved}",`,
136+
`so requests would go to ${url} using the wrong protocol.`,
137+
`Set providers.${providerID}.api to { "type": "aisdk", "package": "${npm}", "url": "<endpoint>" } so the package`,
138+
`and the URL come from the same source, or remove providers.${providerID}.npm to keep the catalog defaults.`,
139+
].join(" ")
140+
}

‎packages/core/src/config/provider.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class Model extends Schema.Class<Model>("ConfigV2.Model")({
6565
export class Info extends Schema.Class<Info>("ConfigV2.Provider")({
6666
name: Schema.String.pipe(Schema.optional),
6767
env: Schema.String.pipe(Schema.Array, Schema.optional),
68+
// Legacy selector kept only so it is rejected instead of silently dropped: the package
69+
// and the URL must come from the same source or the request goes to an impossible endpoint.
70+
npm: Schema.String.pipe(Schema.optional).annotate({
71+
description:
72+
"Legacy provider package selector. Not applied on its own; set api.package instead. Startup fails when it disagrees with the resolved provider package.",
73+
}),
6874
api: ProviderV2.Api.pipe(Schema.optional),
6975
request: Request.pipe(Schema.optional),
7076
models: Schema.Record(Schema.String, Model).pipe(Schema.optional),
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { describe, expect } from "bun:test"
2+
import { Cause, Effect, Exit, Layer, Schema } from "effect"
3+
import { Catalog } from "@opencode-ai/core/catalog"
4+
import { Config } from "@opencode-ai/core/config"
5+
import { ConfigProviderPlugin } from "@opencode-ai/core/config/plugin/provider"
6+
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
7+
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
8+
import { EventV2 } from "@opencode-ai/core/event"
9+
import { Integration } from "@opencode-ai/core/integration"
10+
import { Location } from "@opencode-ai/core/location"
11+
import { ModelV2 } from "@opencode-ai/core/model"
12+
import { ModelsDev } from "@opencode-ai/core/models-dev"
13+
import { ModelsDevPlugin } from "@opencode-ai/core/plugin/models-dev"
14+
import { ProviderV2 } from "@opencode-ai/core/provider"
15+
import { AbsolutePath } from "@opencode-ai/core/schema"
16+
import { location } from "../fixture/location"
17+
import { testEffect } from "../lib/effect"
18+
import { catalogHost, host, integrationHost } from "../plugin/host"
19+
20+
const it = testEffect(
21+
AppNodeBuilder.build(LayerNode.group([Catalog.node, Integration.node, EventV2.node]), [
22+
[
23+
Location.node,
24+
Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(import.meta.dir) }))),
25+
],
26+
]),
27+
)
28+
29+
const providerID = ProviderV2.ID.make("minimax")
30+
const modelID = ModelV2.ID.make("MiniMax-M2")
31+
32+
// The models.dev entry for minimax: an Anthropic-protocol SDK bound to an Anthropic-protocol host.
33+
const catalogData = {
34+
minimax: {
35+
id: "minimax",
36+
name: "MiniMax",
37+
env: ["MINIMAX_API_KEY"],
38+
npm: "@ai-sdk/anthropic",
39+
api: "https://api.minimax.io/anthropic/v1",
40+
models: {
41+
"MiniMax-M2": {
42+
id: "MiniMax-M2",
43+
name: "MiniMax M2",
44+
release_date: "2025-10-01",
45+
attachment: false,
46+
reasoning: true,
47+
temperature: true,
48+
tool_call: true,
49+
limit: { context: 200_000, output: 128_000 },
50+
},
51+
},
52+
},
53+
} satisfies Record<string, ModelsDev.Provider>
54+
55+
const decode = Schema.decodeUnknownSync(Config.Info)
56+
57+
const resolve = Effect.fn(function* (info: unknown) {
58+
const catalog = yield* Catalog.Service
59+
const integrations = yield* Integration.Service
60+
const context = host({ catalog: catalogHost(catalog), integration: integrationHost(integrations) })
61+
yield* ModelsDevPlugin.effect(context).pipe(
62+
Effect.provideService(
63+
ModelsDev.Service,
64+
ModelsDev.Service.of({ get: () => Effect.succeed(catalogData), refresh: () => Effect.void }),
65+
),
66+
)
67+
yield* ConfigProviderPlugin.Plugin.effect(context).pipe(
68+
Effect.provideService(
69+
Config.Service,
70+
Config.Service.of({
71+
entries: () => Effect.succeed([new Config.Document({ type: "document", info: decode(info) })]),
72+
}),
73+
),
74+
)
75+
return yield* catalog.model.get(providerID, modelID)
76+
})
77+
78+
describe("ConfigProviderPlugin npm coherence", () => {
79+
it.effect("rejects a provider block whose npm disagrees with the catalog package", () =>
80+
Effect.gen(function* () {
81+
const exit = yield* resolve({
82+
providers: {
83+
minimax: {
84+
npm: "@ai-sdk/openai-compatible",
85+
request: { body: { baseURL: "https://api.minimax.chat/v1" } },
86+
},
87+
},
88+
}).pipe(Effect.exit)
89+
90+
// Without the check the package stays "@ai-sdk/anthropic" while the url becomes the configured
91+
// host, so the request goes to https://api.minimax.chat/v1/messages, which exists nowhere.
92+
expect(Exit.isFailure(exit)).toBe(true)
93+
const message = Exit.isFailure(exit) ? String(Cause.squash(exit.cause)) : ""
94+
expect(message).toContain('Provider "minimax"')
95+
expect(message).toContain('npm "@ai-sdk/openai-compatible"')
96+
expect(message).toContain('resolved package is "@ai-sdk/anthropic"')
97+
expect(message).toContain("https://api.minimax.chat/v1")
98+
expect(message).toContain("providers.minimax.api")
99+
}),
100+
)
101+
102+
it.effect("rejects the legacy npm and options pair instead of dropping both", () =>
103+
Effect.gen(function* () {
104+
const exit = yield* resolve({
105+
providers: {
106+
minimax: { npm: "@ai-sdk/openai-compatible", options: { baseURL: "https://api.minimax.chat/v1" } },
107+
},
108+
}).pipe(Effect.exit)
109+
110+
expect(Exit.isFailure(exit)).toBe(true)
111+
const message = Exit.isFailure(exit) ? String(Cause.squash(exit.cause)) : ""
112+
expect(message).toContain('npm "@ai-sdk/openai-compatible"')
113+
expect(message).toContain('resolved package is "@ai-sdk/anthropic"')
114+
}),
115+
)
116+
117+
it.effect("accepts npm that agrees with the resolved package", () =>
118+
Effect.gen(function* () {
119+
const model = yield* resolve({
120+
providers: { minimax: { npm: "@ai-sdk/anthropic" } },
121+
})
122+
123+
expect(model?.api).toMatchObject({
124+
type: "aisdk",
125+
package: "@ai-sdk/anthropic",
126+
url: "https://api.minimax.io/anthropic/v1",
127+
})
128+
}),
129+
)
130+
131+
it.effect("keeps retargeting the catalog package at another host when npm is absent", () =>
132+
Effect.gen(function* () {
133+
const model = yield* resolve({
134+
providers: { minimax: { request: { body: { baseURL: "https://minimax.internal/anthropic/v1" } } } },
135+
})
136+
137+
expect(model?.api).toMatchObject({
138+
type: "aisdk",
139+
package: "@ai-sdk/anthropic",
140+
url: "https://minimax.internal/anthropic/v1",
141+
})
142+
}),
143+
)
144+
})

0 commit comments

Comments
 (0)