-
Notifications
You must be signed in to change notification settings - Fork 6.7k
fix(bedrock): support region and bearer token configuration #6332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0d7f685
fix(bedrock): respect options.region parameter in getModel
wnkz 8a471d2
fix(bedrock): read bearer token from auth.json when env var not set
wnkz ae8c726
Apply suggestions from code review
rekram1-node 1db6300
Refactor awsBearerToken initialization with IIFE
rekram1-node File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| import { test, expect } from "bun:test" | ||
| import path from "path" | ||
| import { tmpdir } from "../fixture/fixture" | ||
| import { Instance } from "../../src/project/instance" | ||
| import { Provider } from "../../src/provider/provider" | ||
| import { Env } from "../../src/env" | ||
| import { Auth } from "../../src/auth" | ||
| import { Global } from "../../src/global" | ||
|
|
||
| test("Bedrock: config region takes precedence over AWS_REGION env var", async () => { | ||
| await using tmp = await tmpdir({ | ||
| init: async (dir) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| provider: { | ||
| "amazon-bedrock": { | ||
| options: { | ||
| region: "eu-west-1", | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
| await Instance.provide({ | ||
| directory: tmp.path, | ||
| init: async () => { | ||
| Env.set("AWS_REGION", "us-east-1") | ||
| Env.set("AWS_PROFILE", "default") | ||
| }, | ||
| fn: async () => { | ||
| const providers = await Provider.list() | ||
| expect(providers["amazon-bedrock"]).toBeDefined() | ||
| // Region from config should be used (not env var) | ||
| expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1") | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| test("Bedrock: falls back to AWS_REGION env var when no config", async () => { | ||
| await using tmp = await tmpdir({ | ||
| init: async (dir) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
| await Instance.provide({ | ||
| directory: tmp.path, | ||
| init: async () => { | ||
| Env.set("AWS_REGION", "eu-west-1") | ||
| Env.set("AWS_PROFILE", "default") | ||
| }, | ||
| fn: async () => { | ||
| const providers = await Provider.list() | ||
| expect(providers["amazon-bedrock"]).toBeDefined() | ||
| expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1") | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| test("Bedrock: without explicit region config, uses AWS_REGION env or defaults", async () => { | ||
| await using tmp = await tmpdir({ | ||
| init: async (dir) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
| await Instance.provide({ | ||
| directory: tmp.path, | ||
| init: async () => { | ||
| Env.set("AWS_PROFILE", "default") | ||
| // AWS_REGION might be set in the environment, use that or default | ||
| }, | ||
| fn: async () => { | ||
| const providers = await Provider.list() | ||
| expect(providers["amazon-bedrock"]).toBeDefined() | ||
| // Should have some region set (either from env or default) | ||
| expect(providers["amazon-bedrock"].options?.region).toBeDefined() | ||
| expect(typeof providers["amazon-bedrock"].options?.region).toBe("string") | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| test("Bedrock: uses config region in provider options", async () => { | ||
| await using tmp = await tmpdir({ | ||
| init: async (dir) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| provider: { | ||
| "amazon-bedrock": { | ||
| options: { | ||
| region: "eu-north-1", | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
| await Instance.provide({ | ||
| directory: tmp.path, | ||
| init: async () => { | ||
| Env.set("AWS_PROFILE", "default") | ||
| }, | ||
| fn: async () => { | ||
| const providers = await Provider.list() | ||
| const bedrockProvider = providers["amazon-bedrock"] | ||
| expect(bedrockProvider).toBeDefined() | ||
| expect(bedrockProvider.options?.region).toBe("eu-north-1") | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| test("Bedrock: respects config region for different instances", async () => { | ||
| // First instance with EU config | ||
| await using tmp1 = await tmpdir({ | ||
| init: async (dir) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| provider: { | ||
| "amazon-bedrock": { | ||
| options: { | ||
| region: "eu-west-1", | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| await Instance.provide({ | ||
| directory: tmp1.path, | ||
| init: async () => { | ||
| Env.set("AWS_PROFILE", "default") | ||
| Env.set("AWS_REGION", "us-east-1") | ||
| }, | ||
| fn: async () => { | ||
| const providers1 = await Provider.list() | ||
| expect(providers1["amazon-bedrock"].options?.region).toBe("eu-west-1") | ||
| }, | ||
| }) | ||
|
|
||
| // Second instance with US config | ||
| await using tmp2 = await tmpdir({ | ||
| init: async (dir) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| provider: { | ||
| "amazon-bedrock": { | ||
| options: { | ||
| region: "us-west-2", | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| await Instance.provide({ | ||
| directory: tmp2.path, | ||
| init: async () => { | ||
| Env.set("AWS_PROFILE", "default") | ||
| Env.set("AWS_REGION", "eu-west-1") | ||
| }, | ||
| fn: async () => { | ||
| const providers2 = await Provider.list() | ||
| expect(providers2["amazon-bedrock"].options?.region).toBe("us-west-2") | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| test("Bedrock: loads when bearer token from auth.json is present", async () => { | ||
| await using tmp = await tmpdir({ | ||
| init: async (dir) => { | ||
| await Bun.write( | ||
| path.join(dir, "opencode.json"), | ||
| JSON.stringify({ | ||
| $schema: "https://opencode.ai/config.json", | ||
| provider: { | ||
| "amazon-bedrock": { | ||
| options: { | ||
| region: "eu-west-1", | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ) | ||
| }, | ||
| }) | ||
|
|
||
| // Setup auth.json with bearer token for amazon-bedrock | ||
| const authPath = path.join(Global.Path.data, "auth.json") | ||
| await Bun.write( | ||
| authPath, | ||
| JSON.stringify({ | ||
| "amazon-bedrock": { | ||
| type: "api", | ||
| key: "test-bearer-token", | ||
| }, | ||
| }), | ||
| ) | ||
|
|
||
| await Instance.provide({ | ||
| directory: tmp.path, | ||
| init: async () => { | ||
| // Clear env vars so only auth.json should trigger autoload | ||
| Env.set("AWS_PROFILE", "") | ||
| Env.set("AWS_ACCESS_KEY_ID", "") | ||
| Env.set("AWS_BEARER_TOKEN_BEDROCK", "") | ||
| }, | ||
| fn: async () => { | ||
| const providers = await Provider.list() | ||
| expect(providers["amazon-bedrock"]).toBeDefined() | ||
| expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1") | ||
| }, | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.