diff --git a/packages/core/src/plugin/provider/credential-settings.ts b/packages/core/src/plugin/provider/credential-settings.ts index 545b498a51e..686acb125f3 100644 --- a/packages/core/src/plugin/provider/credential-settings.ts +++ b/packages/core/src/plugin/provider/credential-settings.ts @@ -7,8 +7,8 @@ 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. + * plugin that needs it applies these settings in its provider transform. Switching this integration's active + * credential reloads providers when the derived settings change. */ export const credentialSettings = Effect.fn("ProviderPlugin.credentialSettings")(function* ( ctx: Context, @@ -25,7 +25,8 @@ export const credentialSettings = Effect.fn("ProviderPlugin.credentialSettings") return stored ? select(stored.value) : {} }) const current = { settings: yield* load() } - yield* bus.subscribe([Credential.Event.Updated, Credential.Event.Switched]).pipe( + yield* bus.subscribe(Credential.Event.Switched).pipe( + Stream.filter((event) => event.data.integrationID === integrationID), Stream.runForEach(() => Effect.gen(function* () { const next = yield* load() diff --git a/packages/core/src/provider.ts b/packages/core/src/provider.ts index 83433913fcb..ffafa4df055 100644 --- a/packages/core/src/provider.ts +++ b/packages/core/src/provider.ts @@ -526,7 +526,7 @@ const layer = Layer.effect( }, ) }) - yield* bus.subscribe([Integration.Event.Updated, Credential.Event.Updated, Credential.Event.Switched]).pipe( + yield* bus.subscribe([Integration.Event.Updated, Credential.Event.Switched]).pipe( Stream.runForEach(() => notify), Effect.forkScoped({ startImmediately: true }), ) diff --git a/packages/core/test/plugin/provider-cloudflare-workers-ai.test.ts b/packages/core/test/plugin/provider-cloudflare-workers-ai.test.ts index 6ee8504dc33..fba8bfaf1da 100644 --- a/packages/core/test/plugin/provider-cloudflare-workers-ai.test.ts +++ b/packages/core/test/plugin/provider-cloudflare-workers-ai.test.ts @@ -6,6 +6,7 @@ import { PluginHost } from "@opencode/core/plugin/host" import { CloudflareWorkersAIPlugin } from "@opencode/core/plugin/provider/cloudflare-workers-ai" import { Provider } from "@opencode/core/provider" import { Integration } from "@opencode/core/integration" +import { drain } from "../lib/clock" import { testEffect } from "../lib/effect" import { PluginTestLayer } from "./fixture" @@ -121,6 +122,30 @@ describe("CloudflareWorkersAIPlugin", () => { ), ) + it.effect("follows the account ID when another form login becomes active", () => + 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 + const first = yield* credentials.create({ + integrationID: Integration.ID.make("cloudflare-workers-ai"), + value: Credential.Key.make({ type: "key", key: "secret", metadata: { accountId: "first-acct" } }), + }) + yield* credentials.create({ + integrationID: Integration.ID.make("cloudflare-workers-ai"), + value: Credential.Key.make({ type: "key", key: "secret", metadata: { accountId: "second-acct" } }), + }) + yield* addPlugin() + expect(required(yield* catalog.get(providerID)).settings).toEqual({ accountId: "second-acct" }) + yield* credentials.activate(first.id) + yield* drain + expect(required(yield* catalog.get(providerID)).settings).toEqual({ accountId: "first-acct" }) + }), + ), + ) + it.effect("preserves a configured endpoint URL instead of deriving one from account ID", () => withEnv({ CLOUDFLARE_ACCOUNT_ID: "acct" }, () => Effect.gen(function* () {