fix(core): stop merging credential metadata into model settings (#60057)

This commit is contained in:
Aiden Cline
2026-10-28 19:41:11 -05:00
committed by GitHub
Unverified
parent 01abece663
commit c5d9c30825
14 changed files with 300 additions and 24 deletions
+6 -6
View File
@@ -231,10 +231,10 @@ const resolveCatalogModel = Effect.fn("ModelResolver.resolveCatalogModel")(funct
if (!loadAISDK) return yield* unsupported(resolved)
const settings = yield* prepareProviderSettings(
resolved,
Provider.mergeOverlay(resolved.settings, {
...nativeCredentialSettings(resolved, resolved.package ?? "", credential),
...credential?.metadata,
}) ?? {},
Provider.mergeOverlay(
resolved.settings,
nativeCredentialSettings(resolved, resolved.package ?? "", credential),
) ?? {},
)
return yield* loadAISDK({ ...resolved, settings }).pipe(
Effect.mapError((error) => initialization(resolved, "init", error.cause)),
@@ -270,7 +270,7 @@ const loadCatalogPackage = Effect.fn("ModelResolver.loadCatalogPackage")(functio
dependencies?: Dependencies,
) {
const resolved = prepareRuntimeModel(model)
const configured = { ...resolved.settings, ...credential?.metadata }
const configured = resolved.settings ?? {}
const specifier = Provider.packageName(resolved.package)
if (!specifier || Provider.isAISDK(specifier)) return yield* unsupported(resolved)
const mapped = yield* prepareProviderSettings(resolved, Provider.nativeSettings(configured))
@@ -384,7 +384,7 @@ const nativeCredentialSettings = (model: RuntimeInfo, specifier: string, credent
if (!credential) return {}
if (credential.type === "key") return { apiKey: credential.key }
if (credential.type === "oauth") return tokenSettings(specifier, credential.access)
// The saved profile reaches the package through metadata; SigV4 keeps an ambient bearer token from taking over.
// The Bedrock plugin projects the saved profile into settings; SigV4 keeps an ambient bearer token from taking over.
if (specifier.startsWith("@opencode/ai/providers/amazon-bedrock")) return { auth: "sigv4" }
// The Azure plugin's request hooks replace this with an Entra ID token from the Azure CLI; it only gets the
// request past the package's own credential check.
@@ -4,6 +4,7 @@ import { define } from "@opencode/plugin/effect/plugin"
import { FSUtil } from "@opencode/util/fs-util"
import { Global } from "@opencode/util/global"
import { Provider } from "../../provider.js"
import { credentialSettings, metadataSettings } from "./credential-settings.js"
// Ambient inputs the AWS default credential chain can turn into credentials
// without any key stored in opencode. Mirrors the presence checks the AWS CLI
@@ -82,11 +83,20 @@ export const AmazonBedrockPlugin = define({
},
})
})
// The AWS profile method stores its form answer as credential metadata.
const login = yield* credentialSettings(ctx, Provider.ID.amazonBedrock, (value) =>
value.type === "external" && value.methodID === "aws-profile"
? metadataSettings(value.metadata, ["profile"])
: {},
)
yield* ctx.provider.transform((evt) => {
for (const item of evt.list()) {
if (!isBedrock(item.provider)) continue
evt.update(item.provider.id, (provider) => {
const settings = provider.settings ?? {}
const settings = {
...provider.settings,
...((provider.integrationID ?? provider.id) === Provider.ID.amazonBedrock ? login.settings : {}),
}
const chain = typeof settings.profile === "string" || CHAIN_ENV.some((name) => process.env[name])
// SigV4 authenticates through the AWS default chain rather than a key
// credential, so ambient AWS configuration is what makes Bedrock usable.
+1 -1
View File
@@ -337,7 +337,7 @@ export function make(
const refresh = () => rebind().pipe(Effect.andThen(FiberHandle.run(discovery, discover())))
// The connection's resource wins for Azure itself, matching the runtime merge of credentials over settings.
// The connection's resource wins over configured settings for Azure itself.
const resourceFor = (provider: Provider.Info) =>
provider.id === Provider.ID.azure
? (loaded.resource ?? resolveResourceName(provider.settings))
@@ -83,7 +83,6 @@ type RemoteModel = typeof RemoteModel.Type
const RemoteModels = Schema.Struct({ models: Schema.Array(RemoteModel) })
const decodeCachedModels = Schema.decodeUnknownOption(Schema.Array(RemoteModel))
// Credential metadata is merged into provider settings; these keys are not OpenAI request options.
const decodeMetadata = Schema.decodeUnknownOption(
Schema.Struct({
clientID: Schema.String,
@@ -4,6 +4,7 @@ import { define } from "@opencode/plugin/effect/plugin"
import { Form } from "@opencode/schema/form"
import { Provider } from "../../provider.js"
import { configuredSettings } from "./configured.js"
import { credentialSettings, metadataSettings } from "./credential-settings.js"
const providerID = Provider.ID.make("cloudflare-ai-gateway")
@@ -46,15 +47,20 @@ export const CloudflareAIGatewayPlugin = define({
},
})
})
// The key method's form answers are stored as credential metadata.
const login = yield* credentialSettings(ctx, providerID, (value) =>
value.type === "key" ? metadataSettings(value.metadata, ["accountId", "gatewayId"]) : {},
)
yield* ctx.provider.transform((evt) => {
const item = evt.get(providerID)
if (!item || (!accountId && !gatewayId)) return
if (!item || (!accountId && !gatewayId && Object.keys(login.settings).length === 0)) return
evt.update(item.provider.id, (provider) => {
if (typeof provider.settings?.baseURL === "string") return
provider.settings = {
...(accountId ? { accountId } : {}),
...(gatewayId ? { gatewayId } : {}),
...provider.settings,
...login.settings,
}
})
})
@@ -3,6 +3,7 @@ import { define } from "@opencode/plugin/effect/plugin"
import { Form } from "@opencode/schema/form"
import { Provider } from "../../provider.js"
import { configuredSettings } from "./configured.js"
import { credentialSettings, metadataSettings } from "./credential-settings.js"
const providerID = Provider.ID.make("cloudflare-workers-ai")
@@ -36,12 +37,16 @@ export const CloudflareWorkersAIPlugin = define({
},
})
})
// The key method's form answer is stored as credential metadata.
const login = yield* credentialSettings(ctx, providerID, (value) =>
value.type === "key" ? metadataSettings(value.metadata, ["accountId"]) : {},
)
yield* ctx.provider.transform((evt) => {
const item = evt.get(providerID)
if (!item) return
evt.update(item.provider.id, (provider) => {
if (typeof provider.settings?.baseURL === "string") return
const accountId = resolveAccountId(provider.settings ?? {})
const accountId = resolveAccountId({ ...provider.settings, ...login.settings })
if (!accountId) return
provider.settings =
provider.package === "@opencode/ai/providers/cloudflare-workers-ai"
@@ -0,0 +1,47 @@
import { Effect, Stream } from "effect"
import type { Context } from "@opencode/plugin/effect/plugin"
import { Bus } from "../../bus.js"
import { Credential } from "../../credential.js"
import type { Provider } from "../../provider.js"
/**
* Tracks provider settings a plugin derives from the active connection's stored credential, such as login form
* answers kept as credential metadata. The model resolver does not pass credential metadata to packages, so a
* plugin that needs it applies these settings in its provider transform. A credential update or switch reloads
* providers when the derived settings change.
*/
export const credentialSettings = Effect.fn("ProviderPlugin.credentialSettings")(function* (
ctx: Context,
integrationID: string,
select: (value: Credential.Value) => Provider.Settings,
) {
const credentials = yield* Credential.Service
const bus = yield* Bus.Service
const load = Effect.fn("ProviderPlugin.credentialSettings.load")(function* () {
const connection = yield* ctx.integration.connection.active(integrationID)
// Reads the stored value rather than resolving the connection, so startup never refreshes anything.
const stored =
connection?.type === "credential" ? yield* credentials.get(Credential.ID.make(connection.id)) : undefined
return stored ? select(stored.value) : {}
})
const current = { settings: yield* load() }
yield* bus.subscribe([Credential.Event.Updated, Credential.Event.Switched]).pipe(
Stream.runForEach(() =>
Effect.gen(function* () {
const next = yield* load()
if (JSON.stringify(next) === JSON.stringify(current.settings)) return
current.settings = next
yield* ctx.provider.reload()
}),
),
Effect.forkScoped({ startImmediately: true }),
)
return current
})
/** Picks the string metadata entries a provider package accepts as settings. */
export function metadataSettings(metadata: Readonly<Record<string, unknown>> | undefined, keys: readonly string[]) {
return Object.fromEntries(
keys.flatMap((key) => (typeof metadata?.[key] === "string" && metadata[key] !== "" ? [[key, metadata[key]]] : [])),
)
}
@@ -14,6 +14,7 @@ import { Model } from "../../model.js"
import { OauthCallbackPage } from "../../oauth/page.js"
import { Provider } from "../../provider.js"
import type { PluginInternal } from "../internal.js"
import { credentialSettings, metadataSettings } from "./credential-settings.js"
const providerID = Provider.ID.gitlab
const integrationID = Integration.ID.make("gitlab")
@@ -261,7 +262,15 @@ export const GitLabPlugin = define({
})
})
// An OAuth login pins the instance it was issued against, so requests go there too.
const login = yield* credentialSettings(ctx, integrationID, (value) =>
value.type === "oauth" ? metadataSettings(value.metadata, ["instanceUrl"]) : {},
)
yield* ctx.provider.transform((editor) => {
if (login.settings.instanceUrl && editor.get(providerID))
editor.update(providerID, (provider) => {
provider.settings = { ...provider.settings, ...login.settings }
})
const item = editor.get(providerID)
if (!item || !loaded.models?.length) return
editor.add({
+66 -1
View File
@@ -15,8 +15,10 @@ import { RequestExecutor } from "@opencode/ai/route"
import { OpenAIChat } from "@opencode/ai/protocols"
import { TestLLM } from "@opencode/ai/testing"
import { AISDK } from "@opencode/core/aisdk"
import { Credential } from "@opencode/core/credential"
import { Generate } from "@opencode/core/generate"
import { Integration } from "@opencode/core/integration"
import { IntegrationConnection } from "@opencode/core/integration/connection"
import { ModelResolver } from "@opencode/core/model-resolver"
import { ModelsDev } from "@opencode/core/models-dev"
import { ID, Info, Model, Ref, VariantID } from "@opencode/core/model"
@@ -280,6 +282,11 @@ describe("image and decision generation", () => {
type: "image",
package: "@opencode/ai/providers/openai/images",
}),
Info.make({
...Info.default(Provider.ID.opencode, ID.make("gpt-image-2.5-sunburst")),
type: "image",
package: "@opencode/ai/providers/openai/images",
}),
Info.make({
...Info.default(Provider.ID.make("zai"), ID.make("glm-image")),
type: "image",
@@ -324,6 +331,51 @@ describe("image and decision generation", () => {
),
available: (types) => Effect.succeed(catalog.filter((model) => (types ?? ["chat"]).includes(model.type ?? "chat"))),
})
// An OpenCode Console login keeps its server and organization as credential metadata.
const mediaIntegrations = Layer.mock(Integration.Service, {
revision: () => 0,
connection: {
active: (id) =>
Effect.succeed(
id === Integration.ID.make("opencode")
? IntegrationConnection.CredentialInfo.make({
type: "credential",
id: Credential.ID.make("console"),
label: "Acme",
method: "oauth",
})
: undefined,
),
resolve: () =>
Effect.succeed(
Credential.OAuth.make({
type: "oauth",
methodID: Integration.MethodID.make("device"),
access: "console-token",
refresh: "refresh",
expires: Number.MAX_SAFE_INTEGER,
metadata: { server: "https://console.example", orgID: "org_123", orgName: "Acme" },
}),
),
key: () => Effect.die("unused"),
external: () => Effect.die("unused"),
activate: () => Effect.die("unused"),
update: () => Effect.die("unused"),
remove: () => Effect.die("unused"),
status: () => Effect.die("unused"),
},
oauth: {
connect: () => Effect.die("unused"),
status: () => Effect.die("unused"),
complete: () => Effect.die("unused"),
cancel: () => Effect.die("unused"),
},
command: {
connect: () => Effect.die("unused"),
status: () => Effect.die("unused"),
cancel: () => Effect.die("unused"),
},
})
const requests: { url: string; headers: Record<string, string>; body: unknown }[] = []
const http = Layer.succeed(
HttpClient.HttpClient,
@@ -365,7 +417,7 @@ describe("image and decision generation", () => {
)
const executor = RequestExecutor.layer.pipe(Layer.provide(http))
const mediaResolver = ModelResolver.layer.pipe(
Layer.provide(Layer.mergeAll(mediaProviders, mediaModels, integrations, npm, aisdk)),
Layer.provide(Layer.mergeAll(mediaProviders, mediaModels, mediaIntegrations, npm, aisdk)),
)
const mediaIt = testEffect(
Generate.layer.pipe(Layer.provide(Layer.mergeAll(mediaResolver, client, mediaModels, executor))),
@@ -396,6 +448,19 @@ describe("image and decision generation", () => {
}),
)
mediaIt.effect("keeps credential metadata out of image requests", () =>
Effect.gen(function* () {
requests.length = 0
const generate = yield* Generate.Service
yield* generate.image({ model: ref("opencode", "gpt-image-2.5-sunburst"), prompt: "A black circle" })
expect(requests[0]).toMatchObject({
url: "https://api.openai.com/v1/images/generations",
headers: { authorization: "Bearer console-token" },
})
expect(requests[0]?.body).toStrictEqual({ model: "gpt-image-2.5-sunburst", prompt: "A black circle" })
}),
)
mediaIt.effect("downloads URL images and detects their type from the bytes", () =>
Effect.gen(function* () {
const generate = yield* Generate.Service
+30 -11
View File
@@ -758,8 +758,14 @@ describe("ModelResolver", () => {
methodID: Integration.MethodID.make("azure-cli"),
metadata: { resourceName: "cli-resource" },
})
// The Azure plugin projects the credential's resource into provider settings.
const azure = yield* ModelResolver.fromCatalogModel(
model(Provider.aisdk("@ai-sdk/azure"), { providerID: Provider.ID.azure, headers: {}, body: {} }),
model(Provider.aisdk("@ai-sdk/azure"), {
providerID: Provider.ID.azure,
settings: { resourceName: "cli-resource" },
headers: {},
body: {},
}),
credential,
)
const foundry = yield* ModelResolver.fromCatalogModel(
@@ -787,10 +793,10 @@ describe("ModelResolver", () => {
),
)
it.effect("does not project API key metadata into the request body", () =>
it.effect("does not project API key metadata into settings or the request body", () =>
Effect.gen(function* () {
// V1 auth.json stored connect-form answers as API key metadata, and the legacy import preserves them there.
const resolved = yield* ModelResolver.fromCatalogModel(
const failure = yield* ModelResolver.fromCatalogModel(
model(Provider.aisdk("@ai-sdk/azure"), {
providerID: Provider.ID.azure,
modelID: "responses-deployment",
@@ -799,9 +805,20 @@ describe("ModelResolver", () => {
body: {},
}),
Credential.Key.make({ type: "key", key: "secret", metadata: { resourceName: "migrated-resource" } }),
).pipe(Effect.flip)
const resolved = yield* ModelResolver.fromCatalogModel(
model(Provider.aisdk("@ai-sdk/azure"), {
providerID: Provider.ID.azure,
modelID: "responses-deployment",
settings: { apiVersion: "2025-01-01-preview", resourceName: "configured-resource" },
headers: {},
body: {},
}),
Credential.Key.make({ type: "key", key: "secret", metadata: { resourceName: "migrated-resource" } }),
)
expect(resolved.route.endpoint.baseURL).toBe("https://migrated-resource.openai.azure.com/openai/v1")
expect(failure).toBeInstanceOf(ModelResolver.ModelConfigurationError)
expect(resolved.route.endpoint.baseURL).toBe("https://configured-resource.openai.azure.com/openai/v1")
expect(resolved.route.defaults.http?.body).toEqual({})
}),
)
@@ -982,7 +999,7 @@ describe("ModelResolver", () => {
}),
)
it.effect("maps OAuth credentials to native provider auth settings", () =>
it.effect("maps OAuth credentials to native provider auth settings without their metadata", () =>
Effect.gen(function* () {
const native = yield* ModelResolver.fromCatalogModel(
model(Provider.aisdk("@ai-sdk/openai"), {
@@ -995,6 +1012,7 @@ describe("ModelResolver", () => {
access: "oauth-token",
refresh: "refresh",
expires: Date.now() + 60_000,
metadata: { server: "https://console.example", orgID: "org_123", orgName: "Acme" },
})
const packages = [
["@opencode/ai/providers/google-vertex", "accessToken"],
@@ -1014,6 +1032,9 @@ describe("ModelResolver", () => {
model: (modelID, settings) => {
expect(settings).toMatchObject({ [key]: "oauth-token" })
expect(settings).not.toHaveProperty("apiKey")
expect(settings).not.toHaveProperty("server")
expect(settings).not.toHaveProperty("orgID")
expect(settings).not.toHaveProperty("orgName")
return LanguageModel.make({ id: modelID, provider: "package-provider", route: native.route })
},
}),
@@ -1350,12 +1371,9 @@ describe("ModelResolver", () => {
const workers = yield* ModelResolver.fromCatalogModel(
model("@opencode/ai/providers/cloudflare-workers-ai", {
modelID: "@cf/meta/llama-3.1-8b-instruct",
settings: { accountId: "account" },
}),
Credential.Key.make({
type: "key",
key: "workers-secret",
metadata: { accountId: "account" },
}),
Credential.Key.make({ type: "key", key: "workers-secret" }),
)
expect(google.route.id).toBe("gemini")
@@ -1425,10 +1443,11 @@ describe("ModelResolver", () => {
modelID: "perplexity-api-model",
providerID: "test-provider",
package: Provider.aisdk("@ai-sdk/perplexity"),
settings: { project: "test", apiKey: "fallback-secret", accountId: "account" },
settings: { project: "test", apiKey: "fallback-secret" },
headers: { "x-aisdk": "header" },
body: { custom: true },
})
expect(runtime.settings).not.toHaveProperty("accountId")
return LanguageModel.make({
id: runtime.modelID ?? runtime.id,
provider: runtime.providerID,
@@ -1,5 +1,6 @@
import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { Credential } from "@opencode/core/credential"
import { Integration } from "@opencode/core/integration"
import { Plugin } from "@opencode/core/plugin"
import { PluginHost } from "@opencode/core/plugin/host"
@@ -76,6 +77,27 @@ describe("AmazonBedrockPlugin", () => {
),
)
it.effect("gives the package the profile of a stored AWS profile login", () =>
withEnv(noAmbientAWS, () =>
Effect.gen(function* () {
const catalog = yield* seedBedrock()
const credentials = yield* Credential.Service
yield* credentials.create({
integrationID: Integration.ID.make(Provider.ID.amazonBedrock),
value: Credential.External.make({
type: "external",
methodID: Integration.MethodID.make("aws-profile"),
metadata: { profile: "work" },
}),
})
yield* addPlugin()
const result = required(yield* catalog.get(Provider.ID.amazonBedrock))
expect(result.settings).toEqual({ profile: "work", region: "us-east-1" })
expect(result.activation).toBe("enabled")
}),
),
)
it.effect("keeps an explicit baseURL over endpoint", () =>
withEnv(noAmbientAWS, () =>
Effect.gen(function* () {
@@ -1,5 +1,6 @@
import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { Effect, Schedule } from "effect"
import { Credential } from "@opencode/core/credential"
import { Integration } from "@opencode/core/integration"
import { Plugin } from "@opencode/core/plugin"
import { PluginHost } from "@opencode/core/plugin/host"
@@ -18,6 +19,16 @@ const seed = Effect.fn(function* (fn: (provider: Provider.MutableInfo) => void)
return catalog
})
const settingsWhen = (predicate: (settings: Provider.Settings | undefined) => boolean) =>
Effect.gen(function* () {
const catalog = yield* Provider.Service
return (yield* catalog.get(providerID))?.settings
}).pipe(
Effect.filterOrFail(predicate, () => new Error("settings not applied")),
Effect.retry({ schedule: Schedule.spaced("1 millis") }),
Effect.timeout("10 seconds"),
)
const addPlugin = Effect.fn(function* () {
const plugin = yield* Plugin.Service
const host = yield* PluginHost.make(plugin)
@@ -94,6 +105,47 @@ describe("CloudflareAIGatewayPlugin", () => {
),
)
it.effect("gives the provider the account and gateway IDs of a stored form login", () =>
withEnv({ CLOUDFLARE_ACCOUNT_ID: undefined, CLOUDFLARE_GATEWAY_ID: undefined }, () =>
Effect.gen(function* () {
const catalog = yield* seed(() => {})
const credentials = yield* Credential.Service
yield* credentials.create({
integrationID: Integration.ID.make("cloudflare-ai-gateway"),
value: Credential.Key.make({
type: "key",
key: "token",
metadata: { accountId: "form-account", gatewayId: "form-gateway" },
}),
})
yield* addPlugin()
expect((yield* catalog.get(providerID))?.settings).toEqual({
accountId: "form-account",
gatewayId: "form-gateway",
})
}),
),
)
it.live("applies the account and gateway IDs of a form login made after startup", () =>
withEnv({ CLOUDFLARE_ACCOUNT_ID: undefined, CLOUDFLARE_GATEWAY_ID: undefined }, () =>
Effect.gen(function* () {
yield* seed(() => {})
yield* addPlugin()
const integrations = yield* Integration.Service
yield* integrations.connection.key({
integrationID: Integration.ID.make("cloudflare-ai-gateway"),
key: "token",
answer: { accountId: "form-account", gatewayId: "form-gateway" },
})
expect(yield* settingsWhen((settings) => settings?.accountId !== undefined)).toEqual({
accountId: "form-account",
gatewayId: "form-gateway",
})
}),
),
)
it.effect("leaves a configured endpoint URL untouched", () =>
withEnv({ CLOUDFLARE_ACCOUNT_ID: "account", CLOUDFLARE_GATEWAY_ID: "gateway" }, () =>
Effect.gen(function* () {
@@ -1,5 +1,6 @@
import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { Credential } from "@opencode/core/credential"
import { Plugin } from "@opencode/core/plugin"
import { PluginHost } from "@opencode/core/plugin/host"
import { CloudflareWorkersAIPlugin } from "@opencode/core/plugin/provider/cloudflare-workers-ai"
@@ -103,6 +104,23 @@ describe("CloudflareWorkersAIPlugin", () => {
),
)
it.effect("gives the native Workers AI package the account ID of a stored form login", () =>
withEnv({ CLOUDFLARE_ACCOUNT_ID: undefined }, () =>
Effect.gen(function* () {
const catalog = yield* seed((provider) => {
provider.package = "@opencode/ai/providers/cloudflare-workers-ai"
})
const credentials = yield* Credential.Service
yield* credentials.create({
integrationID: Integration.ID.make("cloudflare-workers-ai"),
value: Credential.Key.make({ type: "key", key: "secret", metadata: { accountId: "form-acct" } }),
})
yield* addPlugin()
expect(required(yield* catalog.get(providerID)).settings).toEqual({ accountId: "form-acct" })
}),
),
)
it.effect("preserves a configured endpoint URL instead of deriving one from account ID", () =>
withEnv({ CLOUDFLARE_ACCOUNT_ID: "acct" }, () =>
Effect.gen(function* () {
@@ -156,6 +156,30 @@ describe("GitLabPlugin", () => {
),
)
it.effect("sends requests to the instance an OAuth login was issued against", () =>
withEnv({ GITLAB_TOKEN: undefined }, () =>
Effect.gen(function* () {
const fixture = yield* discoveryFixture
yield* fixture.credentials.create({
integrationID,
value: {
type: "oauth",
methodID: Integration.MethodID.make("oauth"),
access: "access-token",
refresh: "refresh-token",
expires: 0,
metadata: { instanceUrl: "https://self-managed.gitlab.example", clientID: "client" },
},
})
yield* addPlugin()
yield* drain
expect((yield* fixture.providers.get(providerID))?.settings).toEqual({
instanceUrl: "https://self-managed.gitlab.example",
})
}),
),
)
it.effect("does not discover with only an ambient GITLAB_TOKEN or configured apiKey", () =>
withEnv({ GITLAB_TOKEN: "env-token" }, () =>
Effect.gen(function* () {