GeezerVison is showing github.com — fix(core): stop merging credential metadata into model settings (#60057) · anomalyco/opencode@c5d9c30
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/model-resolver.ts +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -231,10 +231,10 @@ const resolveCatalogModel = Effect.fn("ModelResolver.resolveCatalogModel")(funct | 231 | 231 | if (!loadAISDK) return yield* unsupported(resolved) | 232 | 232 | const settings = yield* prepareProviderSettings( | 233 | 233 | resolved, | 234 | | - Provider.mergeOverlay(resolved.settings, { | 235 | | - ...nativeCredentialSettings(resolved, resolved.package ?? "", credential), | 236 | | - ...credential?.metadata, | 237 | | - }) ?? {}, | | 234 | + Provider.mergeOverlay( | | 235 | + resolved.settings, | | 236 | + nativeCredentialSettings(resolved, resolved.package ?? "", credential), | | 237 | + ) ?? {}, | 238 | 238 | ) | 239 | 239 | return yield* loadAISDK({ ...resolved, settings }).pipe( | 240 | 240 | Effect.mapError((error) => initialization(resolved, "init", error.cause)), | @@ -270,7 +270,7 @@ const loadCatalogPackage = Effect.fn("ModelResolver.loadCatalogPackage")(functio | 270 | 270 | dependencies?: Dependencies, | 271 | 271 | ) { | 272 | 272 | const resolved = prepareRuntimeModel(model) | 273 | | - const configured = { ...resolved.settings, ...credential?.metadata } | | 273 | + const configured = resolved.settings ?? {} | 274 | 274 | const specifier = Provider.packageName(resolved.package) | 275 | 275 | if (!specifier || Provider.isAISDK(specifier)) return yield* unsupported(resolved) | 276 | 276 | const mapped = yield* prepareProviderSettings(resolved, Provider.nativeSettings(configured)) | @@ -384,7 +384,7 @@ const nativeCredentialSettings = (model: RuntimeInfo, specifier: string, credent | 384 | 384 | if (!credential) return {} | 385 | 385 | if (credential.type === "key") return { apiKey: credential.key } | 386 | 386 | if (credential.type === "oauth") return tokenSettings(specifier, credential.access) | 387 | | - // The saved profile reaches the package through metadata; SigV4 keeps an ambient bearer token from taking over. | | 387 | + // The Bedrock plugin projects the saved profile into settings; SigV4 keeps an ambient bearer token from taking over. | 388 | 388 | if (specifier.startsWith("@opencode/ai/providers/amazon-bedrock")) return { auth: "sigv4" } | 389 | 389 | // The Azure plugin's request hooks replace this with an Entra ID token from the Azure CLI; it only gets the | 390 | 390 | // request past the package's own credential check. | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/provider/amazon-bedrock.ts +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import { define } from "@opencode/plugin/effect/plugin" | 4 | 4 | import { FSUtil } from "@opencode/util/fs-util" | 5 | 5 | import { Global } from "@opencode/util/global" | 6 | 6 | import { Provider } from "../../provider.js" | | 7 | +import { credentialSettings, metadataSettings } from "./credential-settings.js" | 7 | 8 |
| 8 | 9 | // Ambient inputs the AWS default credential chain can turn into credentials | 9 | 10 | // without any key stored in opencode. Mirrors the presence checks the AWS CLI | @@ -82,11 +83,20 @@ export const AmazonBedrockPlugin = define({ | 82 | 83 | }, | 83 | 84 | }) | 84 | 85 | }) | | 86 | + // The AWS profile method stores its form answer as credential metadata. | | 87 | + const login = yield* credentialSettings(ctx, Provider.ID.amazonBedrock, (value) => | | 88 | + value.type === "external" && value.methodID === "aws-profile" | | 89 | + ? metadataSettings(value.metadata, ["profile"]) | | 90 | + : {}, | | 91 | + ) | 85 | 92 | yield* ctx.provider.transform((evt) => { | 86 | 93 | for (const item of evt.list()) { | 87 | 94 | if (!isBedrock(item.provider)) continue | 88 | 95 | evt.update(item.provider.id, (provider) => { | 89 | | - const settings = provider.settings ?? {} | | 96 | + const settings = { | | 97 | + ...provider.settings, | | 98 | + ...((provider.integrationID ?? provider.id) === Provider.ID.amazonBedrock ? login.settings : {}), | | 99 | + } | 90 | 100 | const chain = typeof settings.profile === "string" || CHAIN_ENV.some((name) => process.env[name]) | 91 | 101 | // SigV4 authenticates through the AWS default chain rather than a key | 92 | 102 | // credential, so ambient AWS configuration is what makes Bedrock usable. | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/provider/azure.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -337,7 +337,7 @@ export function make( | 337 | 337 |
| 338 | 338 | const refresh = () => rebind().pipe(Effect.andThen(FiberHandle.run(discovery, discover()))) | 339 | 339 |
| 340 | | - // The connection's resource wins for Azure itself, matching the runtime merge of credentials over settings. | | 340 | + // The connection's resource wins over configured settings for Azure itself. | 341 | 341 | const resourceFor = (provider: Provider.Info) => | 342 | 342 | provider.id === Provider.ID.azure | 343 | 343 | ? (loaded.resource ?? resolveResourceName(provider.settings)) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/provider/chatgpt.ts -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -83,7 +83,6 @@ type RemoteModel = typeof RemoteModel.Type | 83 | 83 | const RemoteModels = Schema.Struct({ models: Schema.Array(RemoteModel) }) | 84 | 84 | const decodeCachedModels = Schema.decodeUnknownOption(Schema.Array(RemoteModel)) | 85 | 85 |
| 86 | | -// Credential metadata is merged into provider settings; these keys are not OpenAI request options. | 87 | 86 | const decodeMetadata = Schema.decodeUnknownOption( | 88 | 87 | Schema.Struct({ | 89 | 88 | clientID: Schema.String, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/provider/cloudflare-ai-gateway.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,7 @@ import { define } from "@opencode/plugin/effect/plugin" | 4 | 4 | import { Form } from "@opencode/schema/form" | 5 | 5 | import { Provider } from "../../provider.js" | 6 | 6 | import { configuredSettings } from "./configured.js" | | 7 | +import { credentialSettings, metadataSettings } from "./credential-settings.js" | 7 | 8 |
| 8 | 9 | const providerID = Provider.ID.make("cloudflare-ai-gateway") | 9 | 10 |
| @@ -46,15 +47,20 @@ export const CloudflareAIGatewayPlugin = define({ | 46 | 47 | }, | 47 | 48 | }) | 48 | 49 | }) | | 50 | + // The key method's form answers are stored as credential metadata. | | 51 | + const login = yield* credentialSettings(ctx, providerID, (value) => | | 52 | + value.type === "key" ? metadataSettings(value.metadata, ["accountId", "gatewayId"]) : {}, | | 53 | + ) | 49 | 54 | yield* ctx.provider.transform((evt) => { | 50 | 55 | const item = evt.get(providerID) | 51 | | - if (!item || (!accountId && !gatewayId)) return | | 56 | + if (!item || (!accountId && !gatewayId && Object.keys(login.settings).length === 0)) return | 52 | 57 | evt.update(item.provider.id, (provider) => { | 53 | 58 | if (typeof provider.settings?.baseURL === "string") return | 54 | 59 | provider.settings = { | 55 | 60 | ...(accountId ? { accountId } : {}), | 56 | 61 | ...(gatewayId ? { gatewayId } : {}), | 57 | 62 | ...provider.settings, | | 63 | + ...login.settings, | 58 | 64 | } | 59 | 65 | }) | 60 | 66 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/provider/cloudflare-workers-ai.ts +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ import { define } from "@opencode/plugin/effect/plugin" | 3 | 3 | import { Form } from "@opencode/schema/form" | 4 | 4 | import { Provider } from "../../provider.js" | 5 | 5 | import { configuredSettings } from "./configured.js" | | 6 | +import { credentialSettings, metadataSettings } from "./credential-settings.js" | 6 | 7 |
| 7 | 8 | const providerID = Provider.ID.make("cloudflare-workers-ai") | 8 | 9 |
| @@ -36,12 +37,16 @@ export const CloudflareWorkersAIPlugin = define({ | 36 | 37 | }, | 37 | 38 | }) | 38 | 39 | }) | | 40 | + // The key method's form answer is stored as credential metadata. | | 41 | + const login = yield* credentialSettings(ctx, providerID, (value) => | | 42 | + value.type === "key" ? metadataSettings(value.metadata, ["accountId"]) : {}, | | 43 | + ) | 39 | 44 | yield* ctx.provider.transform((evt) => { | 40 | 45 | const item = evt.get(providerID) | 41 | 46 | if (!item) return | 42 | 47 | evt.update(item.provider.id, (provider) => { | 43 | 48 | if (typeof provider.settings?.baseURL === "string") return | 44 | | - const accountId = resolveAccountId(provider.settings ?? {}) | | 49 | + const accountId = resolveAccountId({ ...provider.settings, ...login.settings }) | 45 | 50 | if (!accountId) return | 46 | 51 | provider.settings = | 47 | 52 | provider.package === "@opencode/ai/providers/cloudflare-workers-ai" | |
Collapse file Copy file name to clipboard +47Lines changed: 47 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { Effect, Stream } from "effect" | | 2 | +import type { Context } from "@opencode/plugin/effect/plugin" | | 3 | +import { Bus } from "../../bus.js" | | 4 | +import { Credential } from "../../credential.js" | | 5 | +import type { Provider } from "../../provider.js" | | 6 | + | | 7 | +/** | | 8 | + * Tracks provider settings a plugin derives from the active connection's stored credential, such as login form | | 9 | + * answers kept as credential metadata. The model resolver does not pass credential metadata to packages, so a | | 10 | + * plugin that needs it applies these settings in its provider transform. A credential update or switch reloads | | 11 | + * providers when the derived settings change. | | 12 | + */ | | 13 | +export const credentialSettings = Effect.fn("ProviderPlugin.credentialSettings")(function* ( | | 14 | + ctx: Context, | | 15 | + integrationID: string, | | 16 | + select: (value: Credential.Value) => Provider.Settings, | | 17 | +) { | | 18 | + const credentials = yield* Credential.Service | | 19 | + const bus = yield* Bus.Service | | 20 | + const load = Effect.fn("ProviderPlugin.credentialSettings.load")(function* () { | | 21 | + const connection = yield* ctx.integration.connection.active(integrationID) | | 22 | + // Reads the stored value rather than resolving the connection, so startup never refreshes anything. | | 23 | + const stored = | | 24 | + connection?.type === "credential" ? yield* credentials.get(Credential.ID.make(connection.id)) : undefined | | 25 | + return stored ? select(stored.value) : {} | | 26 | + }) | | 27 | + const current = { settings: yield* load() } | | 28 | + yield* bus.subscribe([Credential.Event.Updated, Credential.Event.Switched]).pipe( | | 29 | + Stream.runForEach(() => | | 30 | + Effect.gen(function* () { | | 31 | + const next = yield* load() | | 32 | + if (JSON.stringify(next) === JSON.stringify(current.settings)) return | | 33 | + current.settings = next | | 34 | + yield* ctx.provider.reload() | | 35 | + }), | | 36 | + ), | | 37 | + Effect.forkScoped({ startImmediately: true }), | | 38 | + ) | | 39 | + return current | | 40 | +}) | | 41 | + | | 42 | +/** Picks the string metadata entries a provider package accepts as settings. */ | | 43 | +export function metadataSettings(metadata: Readonly<Record<string, unknown>> | undefined, keys: readonly string[]) { | | 44 | + return Object.fromEntries( | | 45 | + keys.flatMap((key) => (typeof metadata?.[key] === "string" && metadata[key] !== "" ? [[key, metadata[key]]] : [])), | | 46 | + ) | | 47 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/plugin/provider/gitlab.ts +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ import { Model } from "../../model.js" | 14 | 14 | import { OauthCallbackPage } from "../../oauth/page.js" | 15 | 15 | import { Provider } from "../../provider.js" | 16 | 16 | import type { PluginInternal } from "../internal.js" | | 17 | +import { credentialSettings, metadataSettings } from "./credential-settings.js" | 17 | 18 |
| 18 | 19 | const providerID = Provider.ID.gitlab | 19 | 20 | const integrationID = Integration.ID.make("gitlab") | @@ -261,7 +262,15 @@ export const GitLabPlugin = define({ | 261 | 262 | }) | 262 | 263 | }) | 263 | 264 |
| | 265 | + // An OAuth login pins the instance it was issued against, so requests go there too. | | 266 | + const login = yield* credentialSettings(ctx, integrationID, (value) => | | 267 | + value.type === "oauth" ? metadataSettings(value.metadata, ["instanceUrl"]) : {}, | | 268 | + ) | 264 | 269 | yield* ctx.provider.transform((editor) => { | | 270 | + if (login.settings.instanceUrl && editor.get(providerID)) | | 271 | + editor.update(providerID, (provider) => { | | 272 | + provider.settings = { ...provider.settings, ...login.settings } | | 273 | + }) | 265 | 274 | const item = editor.get(providerID) | 266 | 275 | if (!item || !loaded.models?.length) return | 267 | 276 | editor.add({ | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/generate.test.ts +66-1Lines changed: 66 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,8 +15,10 @@ import { RequestExecutor } from "@opencode/ai/route" | 15 | 15 | import { OpenAIChat } from "@opencode/ai/protocols" | 16 | 16 | import { TestLLM } from "@opencode/ai/testing" | 17 | 17 | import { AISDK } from "@opencode/core/aisdk" | | 18 | +import { Credential } from "@opencode/core/credential" | 18 | 19 | import { Generate } from "@opencode/core/generate" | 19 | 20 | import { Integration } from "@opencode/core/integration" | | 21 | +import { IntegrationConnection } from "@opencode/core/integration/connection" | 20 | 22 | import { ModelResolver } from "@opencode/core/model-resolver" | 21 | 23 | import { ModelsDev } from "@opencode/core/models-dev" | 22 | 24 | import { ID, Info, Model, Ref, VariantID } from "@opencode/core/model" | @@ -280,6 +282,11 @@ describe("image and decision generation", () => { | 280 | 282 | type: "image", | 281 | 283 | package: "@opencode/ai/providers/openai/images", | 282 | 284 | }), | | 285 | + Info.make({ | | 286 | + ...Info.default(Provider.ID.opencode, ID.make("gpt-image-2.5-sunburst")), | | 287 | + type: "image", | | 288 | + package: "@opencode/ai/providers/openai/images", | | 289 | + }), | 283 | 290 | Info.make({ | 284 | 291 | ...Info.default(Provider.ID.make("zai"), ID.make("glm-image")), | 285 | 292 | type: "image", | @@ -324,6 +331,51 @@ describe("image and decision generation", () => { | 324 | 331 | ), | 325 | 332 | available: (types) => Effect.succeed(catalog.filter((model) => (types ?? ["chat"]).includes(model.type ?? "chat"))), | 326 | 333 | }) | | 334 | + // An OpenCode Console login keeps its server and organization as credential metadata. | | 335 | + const mediaIntegrations = Layer.mock(Integration.Service, { | | 336 | + revision: () => 0, | | 337 | + connection: { | | 338 | + active: (id) => | | 339 | + Effect.succeed( | | 340 | + id === Integration.ID.make("opencode") | | 341 | + ? IntegrationConnection.CredentialInfo.make({ | | 342 | + type: "credential", | | 343 | + id: Credential.ID.make("console"), | | 344 | + label: "Acme", | | 345 | + method: "oauth", | | 346 | + }) | | 347 | + : undefined, | | 348 | + ), | | 349 | + resolve: () => | | 350 | + Effect.succeed( | | 351 | + Credential.OAuth.make({ | | 352 | + type: "oauth", | | 353 | + methodID: Integration.MethodID.make("device"), | | 354 | + access: "console-token", | | 355 | + refresh: "refresh", | | 356 | + expires: Number.MAX_SAFE_INTEGER, | | 357 | + metadata: { server: "https://console.example", orgID: "org_123", orgName: "Acme" }, | | 358 | + }), | | 359 | + ), | | 360 | + key: () => Effect.die("unused"), | | 361 | + external: () => Effect.die("unused"), | | 362 | + activate: () => Effect.die("unused"), | | 363 | + update: () => Effect.die("unused"), | | 364 | + remove: () => Effect.die("unused"), | | 365 | + status: () => Effect.die("unused"), | | 366 | + }, | | 367 | + oauth: { | | 368 | + connect: () => Effect.die("unused"), | | 369 | + status: () => Effect.die("unused"), | | 370 | + complete: () => Effect.die("unused"), | | 371 | + cancel: () => Effect.die("unused"), | | 372 | + }, | | 373 | + command: { | | 374 | + connect: () => Effect.die("unused"), | | 375 | + status: () => Effect.die("unused"), | | 376 | + cancel: () => Effect.die("unused"), | | 377 | + }, | | 378 | + }) | 327 | 379 | const requests: { url: string; headers: Record<string, string>; body: unknown }[] = [] | 328 | 380 | const http = Layer.succeed( | 329 | 381 | HttpClient.HttpClient, | @@ -365,7 +417,7 @@ describe("image and decision generation", () => { | 365 | 417 | ) | 366 | 418 | const executor = RequestExecutor.layer.pipe(Layer.provide(http)) | 367 | 419 | const mediaResolver = ModelResolver.layer.pipe( | 368 | | - Layer.provide(Layer.mergeAll(mediaProviders, mediaModels, integrations, npm, aisdk)), | | 420 | + Layer.provide(Layer.mergeAll(mediaProviders, mediaModels, mediaIntegrations, npm, aisdk)), | 369 | 421 | ) | 370 | 422 | const mediaIt = testEffect( | 371 | 423 | Generate.layer.pipe(Layer.provide(Layer.mergeAll(mediaResolver, client, mediaModels, executor))), | @@ -396,6 +448,19 @@ describe("image and decision generation", () => { | 396 | 448 | }), | 397 | 449 | ) | 398 | 450 |
| | 451 | + mediaIt.effect("keeps credential metadata out of image requests", () => | | 452 | + Effect.gen(function* () { | | 453 | + requests.length = 0 | | 454 | + const generate = yield* Generate.Service | | 455 | + yield* generate.image({ model: ref("opencode", "gpt-image-2.5-sunburst"), prompt: "A black circle" }) | | 456 | + expect(requests[0]).toMatchObject({ | | 457 | + url: "https://api.openai.com/v1/images/generations", | | 458 | + headers: { authorization: "Bearer console-token" }, | | 459 | + }) | | 460 | + expect(requests[0]?.body).toStrictEqual({ model: "gpt-image-2.5-sunburst", prompt: "A black circle" }) | | 461 | + }), | | 462 | + ) | | 463 | + | 399 | 464 | mediaIt.effect("downloads URL images and detects their type from the bytes", () => | 400 | 465 | Effect.gen(function* () { | 401 | 466 | const generate = yield* Generate.Service | |
|