GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(bedrock): config options and authentication precedence (#6377) · f2dmarques/opencode@e3b4d4a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • opencode
      • src
        • cli/cmd
          • auth.ts
        • provider
          • provider.ts
      • test
        • preload.ts
        • provider
          • amazon-bedrock.test.ts
    • web/src/content/docs
      • config.mdx
      • providers.mdx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/opencode/src/cli/cmd/auth.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/auth.ts
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,11 @@ export const AuthLoginCommand = cmd({
335335

336336
if (provider === "amazon-bedrock") {
337337
prompts.log.info(
338-
"Amazon bedrock can be configured with standard AWS environment variables like AWS_BEARER_TOKEN_BEDROCK, AWS_PROFILE or AWS_ACCESS_KEY_ID",
338+
"Amazon Bedrock authentication priority:\n" +
339+
" 1. Bearer token (AWS_BEARER_TOKEN_BEDROCK or /connect)\n" +
340+
" 2. AWS credential chain (profile, access keys, IAM roles)\n\n" +
341+
"Configure via opencode.json options (profile, region, endpoint) or\n" +
342+
"AWS environment variables (AWS_PROFILE, AWS_REGION, AWS_ACCESS_KEY_ID).",
339343
)
340344
prompts.outro("Done")
341345
return
Collapse file

‎packages/opencode/src/provider/provider.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/provider/provider.ts
+32-10Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { Flag } from "../flag/flag"
1515
import { iife } from "@/util/iife"
1616

1717
// Direct imports for bundled providers
18-
import { createAmazonBedrock } from "@ai-sdk/amazon-bedrock"
18+
import { createAmazonBedrock, type AmazonBedrockProviderSettings } from "@ai-sdk/amazon-bedrock"
1919
import { createAnthropic } from "@ai-sdk/anthropic"
2020
import { createAzure } from "@ai-sdk/azure"
2121
import { createGoogleGenerativeAI } from "@ai-sdk/google"
@@ -168,10 +168,22 @@ export namespace Provider {
168168
}
169169
},
170170
"amazon-bedrock": async () => {
171+
const config = await Config.get()
172+
const providerConfig = config.provider?.["amazon-bedrock"]
173+
171174
const auth = await Auth.get("amazon-bedrock")
172-
const awsProfile = Env.get("AWS_PROFILE")
175+
176+
// Region precedence: 1) config file, 2) env var, 3) default
177+
const configRegion = providerConfig?.options?.region
178+
const envRegion = Env.get("AWS_REGION")
179+
const defaultRegion = configRegion ?? envRegion ?? "us-east-1"
180+
181+
// Profile: config file takes precedence over env var
182+
const configProfile = providerConfig?.options?.profile
183+
const envProfile = Env.get("AWS_PROFILE")
184+
const profile = configProfile ?? envProfile
185+
173186
const awsAccessKeyId = Env.get("AWS_ACCESS_KEY_ID")
174-
const awsRegion = Env.get("AWS_REGION")
175187

176188
const awsBearerToken = iife(() => {
177189
const envToken = Env.get("AWS_BEARER_TOKEN_BEDROCK")
@@ -183,17 +195,27 @@ export namespace Provider {
183195
return undefined
184196
})
185197

186-
if (!awsProfile && !awsAccessKeyId && !awsBearerToken) return { autoload: false }
187-
188-
const defaultRegion = awsRegion ?? "us-east-1"
198+
if (!profile && !awsAccessKeyId && !awsBearerToken) return { autoload: false }
189199

190200
const { fromNodeProviderChain } = await import(await BunProc.install("@aws-sdk/credential-providers"))
201+
202+
// Build credential provider options (only pass profile if specified)
203+
const credentialProviderOptions = profile ? { profile } : {}
204+
205+
const providerOptions: AmazonBedrockProviderSettings = {
206+
region: defaultRegion,
207+
credentialProvider: fromNodeProviderChain(credentialProviderOptions),
208+
}
209+
210+
// Add custom endpoint if specified (endpoint takes precedence over baseURL)
211+
const endpoint = providerConfig?.options?.endpoint ?? providerConfig?.options?.baseURL
212+
if (endpoint) {
213+
providerOptions.baseURL = endpoint
214+
}
215+
191216
return {
192217
autoload: true,
193-
options: {
194-
region: defaultRegion,
195-
credentialProvider: fromNodeProviderChain(),
196-
},
218+
options: providerOptions,
197219
async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
198220
// Skip region prefixing if model already has global prefix
199221
if (modelID.startsWith("global.")) {
Collapse file

‎packages/opencode/test/preload.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/test/preload.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ delete process.env["GOOGLE_GENERATIVE_AI_API_KEY"]
4242
delete process.env["AZURE_OPENAI_API_KEY"]
4343
delete process.env["AWS_ACCESS_KEY_ID"]
4444
delete process.env["AWS_PROFILE"]
45+
delete process.env["AWS_REGION"]
46+
delete process.env["AWS_BEARER_TOKEN_BEDROCK"]
4547
delete process.env["OPENROUTER_API_KEY"]
4648
delete process.env["GROQ_API_KEY"]
4749
delete process.env["MISTRAL_API_KEY"]
Collapse file

‎packages/opencode/test/provider/amazon-bedrock.test.ts‎

Copy file name to clipboard
+71-102Lines changed: 71 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
1-
import { test, expect } from "bun:test"
1+
import { test, expect, mock } from "bun:test"
22
import path from "path"
3-
import { tmpdir } from "../fixture/fixture"
4-
import { Instance } from "../../src/project/instance"
5-
import { Provider } from "../../src/provider/provider"
6-
import { Env } from "../../src/env"
7-
import { Auth } from "../../src/auth"
8-
import { Global } from "../../src/global"
3+
4+
// === Mocks ===
5+
// These mocks are required because Provider.list() triggers:
6+
// 1. BunProc.install("@aws-sdk/credential-providers") - in bedrock custom loader
7+
// 2. Plugin.list() which calls BunProc.install() for default plugins
8+
// Without mocks, these would attempt real package installations that timeout in tests.
9+
10+
mock.module("../../src/bun/index", () => ({
11+
BunProc: {
12+
install: async (pkg: string) => pkg,
13+
run: async () => {
14+
throw new Error("BunProc.run should not be called in tests")
15+
},
16+
which: () => process.execPath,
17+
InstallFailedError: class extends Error {},
18+
},
19+
}))
20+
21+
mock.module("@aws-sdk/credential-providers", () => ({
22+
fromNodeProviderChain: () => async () => ({
23+
accessKeyId: "mock-access-key-id",
24+
secretAccessKey: "mock-secret-access-key",
25+
}),
26+
}))
27+
28+
const mockPlugin = () => ({})
29+
mock.module("opencode-copilot-auth", () => ({ default: mockPlugin }))
30+
mock.module("opencode-anthropic-auth", () => ({ default: mockPlugin }))
31+
32+
// Import after mocks are set up
33+
const { tmpdir } = await import("../fixture/fixture")
34+
const { Instance } = await import("../../src/project/instance")
35+
const { Provider } = await import("../../src/provider/provider")
36+
const { Env } = await import("../../src/env")
37+
const { Global } = await import("../../src/global")
938

1039
test("Bedrock: config region takes precedence over AWS_REGION env var", async () => {
1140
await using tmp = await tmpdir({
@@ -34,13 +63,12 @@ test("Bedrock: config region takes precedence over AWS_REGION env var", async ()
3463
fn: async () => {
3564
const providers = await Provider.list()
3665
expect(providers["amazon-bedrock"]).toBeDefined()
37-
// Region from config should be used (not env var)
3866
expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1")
3967
},
4068
})
4169
})
4270

43-
test("Bedrock: falls back to AWS_REGION env var when no config", async () => {
71+
test("Bedrock: falls back to AWS_REGION env var when no config region", async () => {
4472
await using tmp = await tmpdir({
4573
init: async (dir) => {
4674
await Bun.write(
@@ -65,34 +93,7 @@ test("Bedrock: falls back to AWS_REGION env var when no config", async () => {
6593
})
6694
})
6795

68-
test("Bedrock: without explicit region config, uses AWS_REGION env or defaults", async () => {
69-
await using tmp = await tmpdir({
70-
init: async (dir) => {
71-
await Bun.write(
72-
path.join(dir, "opencode.json"),
73-
JSON.stringify({
74-
$schema: "https://opencode.ai/config.json",
75-
}),
76-
)
77-
},
78-
})
79-
await Instance.provide({
80-
directory: tmp.path,
81-
init: async () => {
82-
Env.set("AWS_PROFILE", "default")
83-
// AWS_REGION might be set in the environment, use that or default
84-
},
85-
fn: async () => {
86-
const providers = await Provider.list()
87-
expect(providers["amazon-bedrock"]).toBeDefined()
88-
// Should have some region set (either from env or default)
89-
expect(providers["amazon-bedrock"].options?.region).toBeDefined()
90-
expect(typeof providers["amazon-bedrock"].options?.region).toBe("string")
91-
},
92-
})
93-
})
94-
95-
test("Bedrock: uses config region in provider options", async () => {
96+
test("Bedrock: loads when bearer token from auth.json is present", async () => {
9697
await using tmp = await tmpdir({
9798
init: async (dir) => {
9899
await Bun.write(
@@ -102,62 +103,43 @@ test("Bedrock: uses config region in provider options", async () => {
102103
provider: {
103104
"amazon-bedrock": {
104105
options: {
105-
region: "eu-north-1",
106+
region: "eu-west-1",
106107
},
107108
},
108109
},
109110
}),
110111
)
111112
},
112113
})
114+
115+
const authPath = path.join(Global.Path.data, "auth.json")
116+
await Bun.write(
117+
authPath,
118+
JSON.stringify({
119+
"amazon-bedrock": {
120+
type: "api",
121+
key: "test-bearer-token",
122+
},
123+
}),
124+
)
125+
113126
await Instance.provide({
114127
directory: tmp.path,
115128
init: async () => {
116-
Env.set("AWS_PROFILE", "default")
129+
Env.set("AWS_PROFILE", "")
130+
Env.set("AWS_ACCESS_KEY_ID", "")
131+
Env.set("AWS_BEARER_TOKEN_BEDROCK", "")
117132
},
118133
fn: async () => {
119134
const providers = await Provider.list()
120-
const bedrockProvider = providers["amazon-bedrock"]
121-
expect(bedrockProvider).toBeDefined()
122-
expect(bedrockProvider.options?.region).toBe("eu-north-1")
135+
expect(providers["amazon-bedrock"]).toBeDefined()
136+
expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1")
123137
},
124138
})
125139
})
126140

127-
test("Bedrock: respects config region for different instances", async () => {
128-
// First instance with EU config
129-
await using tmp1 = await tmpdir({
130-
init: async (dir) => {
131-
await Bun.write(
132-
path.join(dir, "opencode.json"),
133-
JSON.stringify({
134-
$schema: "https://opencode.ai/config.json",
135-
provider: {
136-
"amazon-bedrock": {
137-
options: {
138-
region: "eu-west-1",
139-
},
140-
},
141-
},
142-
}),
143-
)
144-
},
145-
})
146-
147-
await Instance.provide({
148-
directory: tmp1.path,
149-
init: async () => {
150-
Env.set("AWS_PROFILE", "default")
151-
Env.set("AWS_REGION", "us-east-1")
152-
},
153-
fn: async () => {
154-
const providers1 = await Provider.list()
155-
expect(providers1["amazon-bedrock"].options?.region).toBe("eu-west-1")
156-
},
157-
})
158-
159-
// Second instance with US config
160-
await using tmp2 = await tmpdir({
141+
test("Bedrock: config profile takes precedence over AWS_PROFILE env var", async () => {
142+
await using tmp = await tmpdir({
161143
init: async (dir) => {
162144
await Bun.write(
163145
path.join(dir, "opencode.json"),
@@ -166,29 +148,30 @@ test("Bedrock: respects config region for different instances", async () => {
166148
provider: {
167149
"amazon-bedrock": {
168150
options: {
169-
region: "us-west-2",
151+
profile: "my-custom-profile",
152+
region: "us-east-1",
170153
},
171154
},
172155
},
173156
}),
174157
)
175158
},
176159
})
177-
178160
await Instance.provide({
179-
directory: tmp2.path,
161+
directory: tmp.path,
180162
init: async () => {
181163
Env.set("AWS_PROFILE", "default")
182-
Env.set("AWS_REGION", "eu-west-1")
164+
Env.set("AWS_ACCESS_KEY_ID", "test-key-id")
183165
},
184166
fn: async () => {
185-
const providers2 = await Provider.list()
186-
expect(providers2["amazon-bedrock"].options?.region).toBe("us-west-2")
167+
const providers = await Provider.list()
168+
expect(providers["amazon-bedrock"]).toBeDefined()
169+
expect(providers["amazon-bedrock"].options?.region).toBe("us-east-1")
187170
},
188171
})
189172
})
190173

191-
test("Bedrock: loads when bearer token from auth.json is present", async () => {
174+
test("Bedrock: includes custom endpoint in options when specified", async () => {
192175
await using tmp = await tmpdir({
193176
init: async (dir) => {
194177
await Bun.write(
@@ -198,39 +181,25 @@ test("Bedrock: loads when bearer token from auth.json is present", async () => {
198181
provider: {
199182
"amazon-bedrock": {
200183
options: {
201-
region: "eu-west-1",
184+
endpoint: "https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com",
202185
},
203186
},
204187
},
205188
}),
206189
)
207190
},
208191
})
209-
210-
// Setup auth.json with bearer token for amazon-bedrock
211-
const authPath = path.join(Global.Path.data, "auth.json")
212-
await Bun.write(
213-
authPath,
214-
JSON.stringify({
215-
"amazon-bedrock": {
216-
type: "api",
217-
key: "test-bearer-token",
218-
},
219-
}),
220-
)
221-
222192
await Instance.provide({
223193
directory: tmp.path,
224194
init: async () => {
225-
// Clear env vars so only auth.json should trigger autoload
226-
Env.set("AWS_PROFILE", "")
227-
Env.set("AWS_ACCESS_KEY_ID", "")
228-
Env.set("AWS_BEARER_TOKEN_BEDROCK", "")
195+
Env.set("AWS_PROFILE", "default")
229196
},
230197
fn: async () => {
231198
const providers = await Provider.list()
232199
expect(providers["amazon-bedrock"]).toBeDefined()
233-
expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1")
200+
expect(providers["amazon-bedrock"].options?.endpoint).toBe(
201+
"https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com",
202+
)
234203
},
235204
})
236205
})
Collapse file

‎packages/web/src/content/docs/config.mdx‎

Copy file name to clipboardExpand all lines: packages/web/src/content/docs/config.mdx
+35Lines changed: 35 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,41 @@ You can also configure [local models](/docs/models#local). [Learn more](/docs/mo
205205

206206
---
207207

208+
#### Provider-Specific Options
209+
210+
Some providers support additional configuration options beyond the generic `timeout` and `apiKey` settings.
211+
212+
##### Amazon Bedrock
213+
214+
Amazon Bedrock supports AWS-specific configuration:
215+
216+
```json title="opencode.json"
217+
{
218+
"$schema": "https://opencode.ai/config.json",
219+
"provider": {
220+
"amazon-bedrock": {
221+
"options": {
222+
"region": "us-east-1",
223+
"profile": "my-aws-profile",
224+
"endpoint": "https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com"
225+
}
226+
}
227+
}
228+
}
229+
```
230+
231+
- `region` - AWS region for Bedrock (defaults to `AWS_REGION` env var or `us-east-1`)
232+
- `profile` - AWS named profile from `~/.aws/credentials` (defaults to `AWS_PROFILE` env var)
233+
- `endpoint` - Custom endpoint URL for VPC endpoints. This is an alias for the generic `baseURL` option using AWS-specific terminology. If both are specified, `endpoint` takes precedence.
234+
235+
:::note
236+
Bearer tokens (`AWS_BEARER_TOKEN_BEDROCK` or `/connect`) take precedence over profile-based authentication. See [authentication precedence](/docs/providers#authentication-precedence) for details.
237+
:::
238+
239+
[Learn more about Amazon Bedrock configuration](/docs/providers#amazon-bedrock).
240+
241+
---
242+
208243
### Themes
209244

210245
You can configure the theme you want to use in your OpenCode config through the `theme` option.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.