From 9d269cf61ec94bb368fad6697afcc0579f66c575 Mon Sep 17 00:00:00 2001 From: OpeOginni Date: Mon, 8 Sep 2025 16:43:59 +0200 Subject: [PATCH 1/2] feat: support for dynamic baseURLs - add optional customBaseUrl to local Auth config - Update handling of providers with customBaseUrl, these can be set in two places - When loading apikeys - As custom load functions --- packages/opencode/src/auth/index.ts | 1 + packages/opencode/src/cli/cmd/auth.ts | 18 ++++++++++++++++ packages/opencode/src/provider/provider.ts | 25 ++++++++++++++++++---- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/opencode/src/auth/index.ts b/packages/opencode/src/auth/index.ts index a0914343810..6fbbc0fa9c2 100644 --- a/packages/opencode/src/auth/index.ts +++ b/packages/opencode/src/auth/index.ts @@ -17,6 +17,7 @@ export namespace Auth { .object({ type: z.literal("api"), key: z.string(), + customBaseUrl: z.string().optional(), }) .openapi({ ref: "ApiAuth" }) diff --git a/packages/opencode/src/cli/cmd/auth.ts b/packages/opencode/src/cli/cmd/auth.ts index 382232f5ace..d64b81ae48c 100644 --- a/packages/opencode/src/cli/cmd/auth.ts +++ b/packages/opencode/src/cli/cmd/auth.ts @@ -253,6 +253,23 @@ export const AuthLoginCommand = cmd({ prompts.log.info("You can create an api key at https://vercel.link/ai-gateway-token") } + let customBaseUrl: string | undefined + if (provider === "cloudflare-workers-ai") { + + if(!providers[provider].api) { + UI.error("Cloudflare API URL not found") + return + } + + const accountIdInput = await prompts.text({ + message: "Enter your Cloudflare Account ID", + validate: (x) => (x && x.length > 0 ? undefined : "Required"), + }) + if (prompts.isCancel(accountIdInput)) throw new UI.CancelledError() + + customBaseUrl = providers[provider].api?.replace("{CLOUDFLARE_ACCOUNT_ID}", accountIdInput) + } + const key = await prompts.password({ message: "Enter your API key", validate: (x) => (x && x.length > 0 ? undefined : "Required"), @@ -261,6 +278,7 @@ export const AuthLoginCommand = cmd({ await Auth.set(provider, { type: "api", key, + ...(customBaseUrl && { customBaseUrl }), }) prompts.outro("Done") diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index 4cc23a71d9b..565f5801aa6 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -17,7 +17,7 @@ export namespace Provider { type CustomLoader = ( provider: ModelsDev.Provider, - api?: string, + auth?: Auth.Info, ) => Promise<{ autoload: boolean getModel?: (sdk: any, modelID: string) => Promise @@ -141,6 +141,18 @@ export namespace Provider { }, } }, + "cloudflare-workers-ai": async (_provider, auth) => { + if (!auth || auth.type !== "api" || !auth.customBaseUrl) { + return { autoload: false, options: {} } + } + + return { + autoload: true, + options: { + baseURL: auth.customBaseUrl, + }, + } + }, } const state = Instance.state(async () => { @@ -253,18 +265,23 @@ export namespace Provider { ) } - // load apikeys + // load apikeys and customBaseUrl for (const [providerID, provider] of Object.entries(await Auth.all())) { if (disabled.has(providerID)) continue if (provider.type === "api") { - mergeProvider(providerID, { apiKey: provider.key }, "api") + const options: Record = { apiKey: provider.key } + if (provider.customBaseUrl) { + options["baseURL"] = provider.customBaseUrl + } + mergeProvider(providerID, options, "api") } } // load custom for (const [providerID, fn] of Object.entries(CUSTOM_LOADERS)) { if (disabled.has(providerID)) continue - const result = await fn(database[providerID]) + const auth = await Auth.get(providerID) + const result = await fn(database[providerID], auth) if (result && (result.autoload || providers[providerID])) { mergeProvider(providerID, result.options ?? {}, "custom", result.getModel) } From f5617c847bcd5d9ffe69a3cf8ad48cc68a7d3181 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 15 Sep 2025 14:21:43 +0000 Subject: [PATCH 2/2] chore: format code --- packages/opencode/src/cli/cmd/auth.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/opencode/src/cli/cmd/auth.ts b/packages/opencode/src/cli/cmd/auth.ts index d64b81ae48c..09ec964c3bf 100644 --- a/packages/opencode/src/cli/cmd/auth.ts +++ b/packages/opencode/src/cli/cmd/auth.ts @@ -255,8 +255,7 @@ export const AuthLoginCommand = cmd({ let customBaseUrl: string | undefined if (provider === "cloudflare-workers-ai") { - - if(!providers[provider].api) { + if (!providers[provider].api) { UI.error("Cloudflare API URL not found") return }