From 2c376c5abc764d99ebb62742c6f427c6cf6fbe4d Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Thu, 12 Jun 2025 23:39:52 -0400 Subject: [PATCH] bedrock loader --- packages/opencode/src/bun/index.ts | 25 ++++++++++++++++++++++ packages/opencode/src/provider/provider.ts | 25 +++++++++++++--------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/packages/opencode/src/bun/index.ts b/packages/opencode/src/bun/index.ts index bee19a2bf..fc8e7e839 100644 --- a/packages/opencode/src/bun/index.ts +++ b/packages/opencode/src/bun/index.ts @@ -1,4 +1,9 @@ +import { z } from "zod" +import { Global } from "../global" import { Log } from "../util/log" +import path from "path" +import { NamedError } from "../util/error" + export namespace BunProc { const log = Log.create({ service: "bun" }) @@ -30,4 +35,24 @@ export namespace BunProc { export function which() { return process.execPath } + + export const InstallFailedError = NamedError.create( + "BunInstallFailedError", + z.object({ + pkg: z.string(), + version: z.string(), + }), + ) + export async function install(pkg: string, version = "latest") { + const dir = path.join(Global.Path.cache, `node_modules`, pkg) + if (!(await Bun.file(path.join(dir, "package.json")).exists())) { + log.info("installing", { pkg }) + await BunProc.run(["add", `${pkg}@${version}`], { + cwd: Global.Path.cache, + }).catch(() => { + throw new InstallFailedError({ pkg, version }) + }) + } + return dir + } } diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index 687b1484b..a2bdfd5cc 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -48,16 +48,23 @@ export namespace Provider { } } return { - source: "oauth", - options: { - apiKey: "", - headers: { - authorization: `Bearer ${access}`, - "anthropic-beta": "oauth-2025-04-20", - }, + apiKey: "", + headers: { + authorization: `Bearer ${access}`, + "anthropic-beta": "oauth-2025-04-20", }, } }, + "amazon-bedrock": async () => { + if (!process.env["AWS_PROFILE"]) return false + const { fromNodeProviderChain } = await import( + await BunProc.install("@aws-sdk/credential-providers") + ) + return { + region: process.env["AWS_REGION"] ?? "us-east-1", + credentialProvider: fromNodeProviderChain(), + } + }, } const state = App.state("provider", async () => { @@ -144,9 +151,7 @@ export namespace Provider { // load custom for (const [providerID, fn] of Object.entries(CUSTOM_LOADERS)) { const result = await fn(database[providerID]) - if (result) { - mergeProvider(providerID, result, "custom") - } + if (result) mergeProvider(providerID, result, "custom") } // load config