Compare commits

...

1 Commits

Author SHA1 Message Date
rekram1-node d82952225f fix(core): send console anthropic api key header 2026-08-20 16:07:24 +00:00
2 changed files with 40 additions and 0 deletions
@@ -192,6 +192,21 @@ export const OpencodePlugin = define<HttpClient.HttpClient | Bus.Service | Scope
}
})
yield* ctx.session.hook(
"http.request",
(event) =>
Effect.sync(() => {
if (!event.request.headers.has("anthropic-version")) return
if (event.request.headers.has("x-api-key")) return
const authorization = event.request.headers.get("authorization")
const match = authorization?.match(/^Bearer\s+(.+)$/i)
if (!match) return
event.request.headers.set("x-api-key", match[1])
event.request.headers.delete("authorization")
}),
{ providerID: Provider.ID.opencode },
)
const refresh = () => loading.withPermit(load().pipe(Effect.andThen(ctx.catalog.reload())))
yield* bus.subscribe(Integration.Event.ConnectionUpdated).pipe(
Stream.filter((event) => event.data.integrationID === Integration.ID.make("opencode")),
@@ -2,14 +2,17 @@ import { describe, expect } from "bun:test"
import { Money } from "@opencode-ai/schema/money"
import { Effect } from "effect"
import { Catalog } from "@opencode-ai/core/catalog"
import { Agent } from "@opencode-ai/core/agent"
import { Credential } from "@opencode-ai/core/credential"
import { Bus } from "@opencode-ai/core/bus"
import { Integration } from "@opencode-ai/core/integration"
import { Model } from "@opencode-ai/core/model"
import { Plugin } from "@opencode-ai/core/plugin"
import { PluginHost } from "@opencode-ai/core/plugin/host"
import { PluginHooks } from "@opencode-ai/core/plugin/hooks"
import { OpencodePlugin } from "@opencode-ai/core/plugin/provider/opencode"
import { Provider } from "@opencode-ai/core/provider"
import { Session } from "@opencode-ai/core/session"
import { testEffect } from "../lib/effect"
import { PluginTestLayer } from "./fixture"
@@ -92,6 +95,28 @@ describe("OpencodePlugin", () => {
}),
)
it.effect("sends OpenCode Anthropic credentials as x-api-key", () =>
Effect.gen(function* () {
yield* addPlugin()
const hooks = yield* PluginHooks.Service
const event = yield* hooks.trigger("session", "http.request", {
sessionID: Session.ID.make("ses_test"),
agent: Agent.ID.make("build"),
model: Model.Ref.make({ providerID: Provider.ID.opencode, id: Model.ID.make("claude-fable-5") }),
request: new Request("https://opencode.ai/inference/anthropic/v1/messages", {
method: "POST",
headers: {
authorization: "Bearer console-token",
"anthropic-version": "2023-06-01",
},
}),
})
expect(event.request.headers.get("x-api-key")).toBe("console-token")
expect(event.request.headers.get("authorization")).toBeNull()
}),
)
it.live("uses a canonical custom server throughout device authorization", () =>
Effect.acquireUseRelease(
Effect.sync(() => {