diff --git a/packages/ai/README.md b/packages/ai/README.md index 4a3622110ed..1299a0925d1 100644 --- a/packages/ai/README.md +++ b/packages/ai/README.md @@ -1476,6 +1476,8 @@ const gateway = CloudflareAIGateway.configure({ }).model("workers-ai/@cf/meta/llama-3.1-8b-instruct") ``` +For a provider that needs no key, such as a local server, pass `apiKey: false`: the package skips the environment lookup and sends no auth header (Amazon Bedrock and Google Vertex keep their own auth). + Included LLM providers: OpenAI, Anthropic, Google (Gemini), Google Vertex, Amazon Bedrock (Converse and Mantle), Azure OpenAI, Alibaba, Baseten, Cerebras, Cohere, Cloudflare AI Gateway, Cloudflare Workers AI, DeepInfra, DeepSeek, DigitalOcean, Fireworks, Groq, Meta, MiniMax, Mistral, Moonshot, OpenRouter, TogetherAI, Venice, Vercel AI Gateway, xAI, Z.ai, and Z.ai Coding Plan. TypeSafe AI and OpenCode expose decision models only. Generic Chat Completions, Responses, and Anthropic Messages-compatible entrypoints support custom endpoints. Each named provider owns its module, endpoint, authentication, and route setup. Providers with the same wire format compose the shared protocol directly: diff --git a/packages/ai/src/providers/alibaba.ts b/packages/ai/src/providers/alibaba.ts index c8de8ea6db4..4ae703a8c8f 100644 --- a/packages/ai/src/providers/alibaba.ts +++ b/packages/ai/src/providers/alibaba.ts @@ -46,7 +46,7 @@ export type Config = Location & export type Settings = Location & ProviderPackage.Settings & Options & { - readonly apiKey?: string + readonly apiKey?: string | false } const hosts = new Map([ diff --git a/packages/ai/src/providers/anthropic-compatible.ts b/packages/ai/src/providers/anthropic-compatible.ts index 7dac4d5ff20..09b648ed551 100644 --- a/packages/ai/src/providers/anthropic-compatible.ts +++ b/packages/ai/src/providers/anthropic-compatible.ts @@ -1,7 +1,7 @@ import type { ProviderPackage } from "../provider-package.js" import { AnthropicMessages } from "../protocols/anthropic-messages.js" import { Auth } from "../route/auth.js" -import type { ProviderAuthOption } from "../route/auth-options.js" +import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js" import type { RouteDefaultsInput } from "../route/client.js" import { ProviderConfigurationError, ProviderID, type ModelID } from "../schema/index.js" @@ -21,7 +21,7 @@ export type Config = RouteDefaultsInput & export type Settings = ProviderPackage.Settings & AnthropicMessages.ProviderOptionsInput & ( - | { readonly apiKey?: string; readonly authToken?: never } + | { readonly apiKey?: string | false; readonly authToken?: never } | { readonly apiKey?: never; readonly authToken?: string } ) & { readonly baseURL: string @@ -31,10 +31,7 @@ export type Settings = ProviderPackage.Settings & const compatibleRoute = AnthropicMessages.route.with({ id: "anthropic-compatible-messages", provider: id }) export const routes = [compatibleRoute] -const auth = (input: ProviderAuthOption<"optional">) => { - if ("auth" in input && input.auth) return input.auth - return Auth.optional("apiKey" in input ? input.apiKey : undefined, "apiKey").pipe(Auth.header("x-api-key")) -} +const auth = (input: ProviderAuthOption<"optional">) => AuthOptions.apiKey(input, [], Auth.header("x-api-key")) export const configure = (input: Config) => { const provider = input.provider ?? "anthropic-compatible" diff --git a/packages/ai/src/providers/anthropic.ts b/packages/ai/src/providers/anthropic.ts index eabff31d4f8..8071da941c5 100644 --- a/packages/ai/src/providers/anthropic.ts +++ b/packages/ai/src/providers/anthropic.ts @@ -1,6 +1,6 @@ import type { RouteDefaultsInput } from "../route/client.js" import { Auth } from "../route/auth.js" -import type { ProviderAuthOption } from "../route/auth-options.js" +import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js" import type { ProviderPackage } from "../provider-package.js" import { ProviderConfigurationError, ProviderID, type ModelID } from "../schema/index.js" import { AnthropicMessages } from "../protocols/anthropic-messages.js" @@ -23,18 +23,14 @@ export type Config = RouteDefaultsInput & export type Settings = ProviderPackage.Settings & AnthropicMessages.ProviderOptionsInput & ( - | { readonly apiKey?: string; readonly authToken?: never } + | { readonly apiKey?: string | false; readonly authToken?: never } | { readonly apiKey?: never; readonly authToken?: string } ) & { readonly baseURL?: string } -const auth = (options: ProviderAuthOption<"optional">) => { - if ("auth" in options && options.auth) return options.auth - return Auth.optional("apiKey" in options ? options.apiKey : undefined, "apiKey") - .orElse(Auth.config("ANTHROPIC_API_KEY")) - .pipe(Auth.header("x-api-key")) -} +const auth = (options: ProviderAuthOption<"optional">) => + AuthOptions.apiKey(options, "ANTHROPIC_API_KEY", Auth.header("x-api-key")) export const configure = (input: Config = {}) => { const { apiKey: _, auth: _auth, baseURL, ...rest } = input diff --git a/packages/ai/src/providers/assemblyai.ts b/packages/ai/src/providers/assemblyai.ts index cc91ed016ef..849b98ada72 100644 --- a/packages/ai/src/providers/assemblyai.ts +++ b/packages/ai/src/providers/assemblyai.ts @@ -1,5 +1,5 @@ import { Auth } from "../route/auth.js" -import type { ProviderAuthOption } from "../route/auth-options.js" +import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js" import { MediaRoute } from "../route/media.js" import { type HttpOptions, ProviderID, type ModelID } from "../schema/index.js" import { AssemblyAITranscription } from "../protocols/assemblyai-transcription.js" @@ -16,12 +16,8 @@ export type Config = ProviderAuthOption<"optional"> & { } // The key is the whole `authorization` value, without a scheme. -const auth = (options: ProviderAuthOption<"optional">) => { - if ("auth" in options && options.auth) return options.auth - return Auth.optional("apiKey" in options ? options.apiKey : undefined, "apiKey") - .orElse(Auth.config("ASSEMBLYAI_API_KEY")) - .pipe(Auth.header("authorization")) -} +const auth = (options: ProviderAuthOption<"optional">) => + AuthOptions.apiKey(options, "ASSEMBLYAI_API_KEY", Auth.header("authorization")) export const configure = (input: Config = {}) => { const media = MediaRoute.deployment(input, auth(input)) diff --git a/packages/ai/src/providers/azure.ts b/packages/ai/src/providers/azure.ts index 50e6d8213f2..84bba346c66 100644 --- a/packages/ai/src/providers/azure.ts +++ b/packages/ai/src/providers/azure.ts @@ -1,5 +1,5 @@ import { Auth } from "../route/auth.js" -import { type AtLeastOne, type ProviderAuthOption } from "../route/auth-options.js" +import { AuthOptions, type AtLeastOne, type ProviderAuthOption } from "../route/auth-options.js" import type { Route, RouteDefaultsInput, CompactionOperations } from "../route/client.js" import { Endpoint } from "../route/endpoint.js" import type { ProviderPackage } from "../provider-package.js" @@ -37,7 +37,7 @@ export type Config = AzureURL & export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & AzureURL & { - readonly apiKey?: string + readonly apiKey?: string | false readonly apiVersion?: string readonly queryParams?: Readonly> readonly useDeploymentBasedUrls?: boolean @@ -98,11 +98,7 @@ const defaults = (input: Config) => { const auth = (input: Config) => { if ("auth" in input && input.auth) return input.auth - return Auth.remove("authorization").andThen( - Auth.optional("apiKey" in input ? input.apiKey : undefined, "apiKey") - .orElse(Auth.config("AZURE_OPENAI_API_KEY")) - .pipe(Auth.header("api-key")), - ) + return Auth.remove("authorization").andThen(AuthOptions.apiKey(input, "AZURE_OPENAI_API_KEY", Auth.header("api-key"))) } const configuredRoute = ( diff --git a/packages/ai/src/providers/baseten.ts b/packages/ai/src/providers/baseten.ts index 326b13f8dbc..8e55738399d 100644 --- a/packages/ai/src/providers/baseten.ts +++ b/packages/ai/src/providers/baseten.ts @@ -24,7 +24,7 @@ export interface BasetenDecisionOptions { export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/cerebras.ts b/packages/ai/src/providers/cerebras.ts index 85ca4d2214e..b06fca4be6a 100644 --- a/packages/ai/src/providers/cerebras.ts +++ b/packages/ai/src/providers/cerebras.ts @@ -17,7 +17,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/cloudflare-ai-gateway.ts b/packages/ai/src/providers/cloudflare-ai-gateway.ts index cc4fc000ab5..e4f69e50fc3 100644 --- a/packages/ai/src/providers/cloudflare-ai-gateway.ts +++ b/packages/ai/src/providers/cloudflare-ai-gateway.ts @@ -4,7 +4,7 @@ import { CloudflareRunDecisions } from "../protocols/cloudflare-run-decisions.js import { AnthropicMessages, OpenAIChat, OpenAIResponses } from "../protocols/index.js" import { Auth } from "../route/auth.js" import { Endpoint } from "../route/endpoint.js" -import type { AtLeastOne, ProviderAuthOption } from "../route/auth-options.js" +import { AuthOptions, type AtLeastOne, type ProviderAuthOption } from "../route/auth-options.js" import type { RouteDefaultsInput } from "../route/client.js" import { ProviderConfigurationError, ProviderID, type ModelID, type ProviderOptions } from "../schema/index.js" import { encodeJson } from "../utils/json.js" @@ -39,7 +39,7 @@ export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & GatewayURL & GatewayOptions & { - readonly apiKey?: string + readonly apiKey?: string | false readonly gatewayApiKey?: string } @@ -94,10 +94,12 @@ export const route = OpenAIChat.route.with({ export const routes = [responsesRoute, messagesRoute, route] -const credential = (input: Config) => - Auth.optional(input.gatewayApiKey ?? ("apiKey" in input ? input.apiKey : undefined), "apiKey") - .orElse(Auth.config(authEnvVars[0])) - .orElse(Auth.config(authEnvVars[1])) +const credential = (input: Config, apply: (credential: Auth.Credential) => Auth.Definition) => + AuthOptions.apiKey( + { apiKey: input.gatewayApiKey ?? ("apiKey" in input ? input.apiKey : undefined) }, + authEnvVars, + apply, + ) const headers = (input: Config) => ({ ...(input.metadata === undefined ? {} : { "cf-aig-metadata": encodeJson(input.metadata) }), @@ -112,7 +114,7 @@ export const configure = (input: Config) => { const custom = "auth" in input && input.auth ? input.auth : undefined const rest = { endpoint: { baseURL: baseURL(input) }, - auth: custom ?? credential(input).bearer(), + auth: custom ?? credential(input, (credential) => credential.bearer()), headers: { "cf-aig-gateway-id": gatewayId(input), ...headers(input) }, http: input.http, providerOptions: input.providerOptions, @@ -124,7 +126,7 @@ export const configure = (input: Config) => { ? { ...rest, endpoint: { baseURL: passthroughURL(input, provider) }, - auth: custom ?? Auth.bearerHeader("cf-aig-authorization", credential(input)), + auth: custom ?? credential(input, Auth.bearerHeader("cf-aig-authorization")), headers: headers(input), } : rest diff --git a/packages/ai/src/providers/cloudflare-workers-ai.ts b/packages/ai/src/providers/cloudflare-workers-ai.ts index aa9b173afcc..2711c6634df 100644 --- a/packages/ai/src/providers/cloudflare-workers-ai.ts +++ b/packages/ai/src/providers/cloudflare-workers-ai.ts @@ -27,7 +27,7 @@ export type Config = WorkersAIURL & export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & WorkersAIURL & { - readonly apiKey?: string + readonly apiKey?: string | false } export type { CloudflareWorkersAIImageOptions } from "../protocols/cloudflare-workers-ai-images.js" diff --git a/packages/ai/src/providers/cohere.ts b/packages/ai/src/providers/cohere.ts index ebc8c6a968a..4c3320ff882 100644 --- a/packages/ai/src/providers/cohere.ts +++ b/packages/ai/src/providers/cohere.ts @@ -16,7 +16,7 @@ export type Config = Omit & readonly providerOptions?: ProviderOptions } export type Settings = ProviderPackage.Settings & - Options & { readonly apiKey?: string; readonly baseURL?: string } + Options & { readonly apiKey?: string | false; readonly baseURL?: string } export const route = CohereChat.route export const chatRoute = Route.make({ diff --git a/packages/ai/src/providers/deepgram.ts b/packages/ai/src/providers/deepgram.ts index d72f74074d4..d5afdbdad51 100644 --- a/packages/ai/src/providers/deepgram.ts +++ b/packages/ai/src/providers/deepgram.ts @@ -1,5 +1,5 @@ import { Auth } from "../route/auth.js" -import type { ProviderAuthOption } from "../route/auth-options.js" +import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js" import { MediaRoute } from "../route/media.js" import { type HttpOptions, ProviderID, type ModelID } from "../schema/index.js" import { DeepgramSpeech } from "../protocols/deepgram-speech.js" @@ -16,12 +16,8 @@ export type Config = ProviderAuthOption<"optional"> & { readonly http?: HttpOptions.Input } -const auth = (options: ProviderAuthOption<"optional">) => { - if ("auth" in options && options.auth) return options.auth - return Auth.optional("apiKey" in options ? options.apiKey : undefined, "apiKey") - .orElse(Auth.config("DEEPGRAM_API_KEY")) - .pipe(Auth.scheme("Token")) -} +const auth = (options: ProviderAuthOption<"optional">) => + AuthOptions.apiKey(options, "DEEPGRAM_API_KEY", Auth.scheme("Token")) export const configure = (input: Config = {}) => { const media = MediaRoute.deployment(input, auth(input)) diff --git a/packages/ai/src/providers/deepinfra.ts b/packages/ai/src/providers/deepinfra.ts index 32328b825a5..66100205b72 100644 --- a/packages/ai/src/providers/deepinfra.ts +++ b/packages/ai/src/providers/deepinfra.ts @@ -17,7 +17,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/deepseek.ts b/packages/ai/src/providers/deepseek.ts index dd62eb9ebe5..f482ae1aec4 100644 --- a/packages/ai/src/providers/deepseek.ts +++ b/packages/ai/src/providers/deepseek.ts @@ -17,7 +17,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/digitalocean.ts b/packages/ai/src/providers/digitalocean.ts index 5905a28bb0a..fc42a58b9b7 100644 --- a/packages/ai/src/providers/digitalocean.ts +++ b/packages/ai/src/providers/digitalocean.ts @@ -23,7 +23,7 @@ export type Config = Omit & } export type Settings = ProviderPackage.Settings & - Options & { readonly apiKey?: string } + Options & { readonly apiKey?: string | false } export const protocol = Protocol.make({ id: "digitalocean-chat", diff --git a/packages/ai/src/providers/elevenlabs.ts b/packages/ai/src/providers/elevenlabs.ts index 410e246002f..e39e79e21b4 100644 --- a/packages/ai/src/providers/elevenlabs.ts +++ b/packages/ai/src/providers/elevenlabs.ts @@ -1,5 +1,5 @@ import { Auth } from "../route/auth.js" -import type { ProviderAuthOption } from "../route/auth-options.js" +import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js" import { MediaRoute } from "../route/media.js" import { type HttpOptions, ProviderID, type ModelID } from "../schema/index.js" import { ElevenLabsSpeech } from "../protocols/elevenlabs-speech.js" @@ -16,12 +16,8 @@ export type Config = ProviderAuthOption<"optional"> & { readonly http?: HttpOptions.Input } -const auth = (options: ProviderAuthOption<"optional">) => { - if ("auth" in options && options.auth) return options.auth - return Auth.optional("apiKey" in options ? options.apiKey : undefined, "apiKey") - .orElse(Auth.config("ELEVENLABS_API_KEY")) - .pipe(Auth.header("xi-api-key")) -} +const auth = (options: ProviderAuthOption<"optional">) => + AuthOptions.apiKey(options, "ELEVENLABS_API_KEY", Auth.header("xi-api-key")) export const configure = (input: Config = {}) => { const media = MediaRoute.deployment(input, auth(input)) diff --git a/packages/ai/src/providers/fireworks.ts b/packages/ai/src/providers/fireworks.ts index 1cc6ce81cbd..d121a4c0e42 100644 --- a/packages/ai/src/providers/fireworks.ts +++ b/packages/ai/src/providers/fireworks.ts @@ -17,7 +17,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/google.ts b/packages/ai/src/providers/google.ts index 84ef23e3258..3c4cdd76233 100644 --- a/packages/ai/src/providers/google.ts +++ b/packages/ai/src/providers/google.ts @@ -1,6 +1,6 @@ import type { RouteDefaultsInput } from "../route/client.js" import { Auth } from "../route/auth.js" -import type { ProviderAuthOption } from "../route/auth-options.js" +import { AuthOptions, type ProviderAuthOption } from "../route/auth-options.js" import { MediaRoute } from "../route/media.js" import type { ProviderPackage } from "../provider-package.js" import { ProviderID, type ModelID, type ProviderOptions } from "../schema/index.js" @@ -31,16 +31,12 @@ export type Config = RouteDefaultsInput & export type Settings = ProviderPackage.Settings & Gemini.ProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } -const auth = (options: ProviderAuthOption<"optional">) => { - if ("auth" in options && options.auth) return options.auth - return Auth.optional("apiKey" in options ? options.apiKey : undefined, "apiKey") - .orElse(Auth.config("GOOGLE_GENERATIVE_AI_API_KEY")) - .pipe(Auth.header("x-goog-api-key")) -} +const auth = (options: ProviderAuthOption<"optional">) => + AuthOptions.apiKey(options, "GOOGLE_GENERATIVE_AI_API_KEY", Auth.header("x-goog-api-key")) const configuredRoute = (input: Config) => { const { apiKey: _, auth: _auth, baseURL, ...rest } = input diff --git a/packages/ai/src/providers/google/interactions.ts b/packages/ai/src/providers/google/interactions.ts index 313431256ee..693ee8d7f13 100644 --- a/packages/ai/src/providers/google/interactions.ts +++ b/packages/ai/src/providers/google/interactions.ts @@ -4,7 +4,7 @@ import type { GoogleInteractions } from "../../protocols/google-interactions.js" export type Settings = ProviderPackage.Settings & GoogleInteractions.ProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/groq.ts b/packages/ai/src/providers/groq.ts index 5b0584808d9..d6c11f4e537 100644 --- a/packages/ai/src/providers/groq.ts +++ b/packages/ai/src/providers/groq.ts @@ -28,7 +28,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & ProviderOptions & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/meta.ts b/packages/ai/src/providers/meta.ts index 13fac64f3b4..eb302d59b12 100644 --- a/packages/ai/src/providers/meta.ts +++ b/packages/ai/src/providers/meta.ts @@ -82,7 +82,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & ProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/minimax.ts b/packages/ai/src/providers/minimax.ts index ec5d77e7333..d16230a5950 100644 --- a/packages/ai/src/providers/minimax.ts +++ b/packages/ai/src/providers/minimax.ts @@ -44,7 +44,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & Options & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/mistral.ts b/packages/ai/src/providers/mistral.ts index efe22cfb767..92c43d0d778 100644 --- a/packages/ai/src/providers/mistral.ts +++ b/packages/ai/src/providers/mistral.ts @@ -16,7 +16,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & ProviderOptions & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/moonshot.ts b/packages/ai/src/providers/moonshot.ts index c408afe828c..ebc9fc3819b 100644 --- a/packages/ai/src/providers/moonshot.ts +++ b/packages/ai/src/providers/moonshot.ts @@ -44,7 +44,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & Options & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/openai-compatible-responses.ts b/packages/ai/src/providers/openai-compatible-responses.ts index 1b0e318ef13..0b4c1b19a61 100644 --- a/packages/ai/src/providers/openai-compatible-responses.ts +++ b/packages/ai/src/providers/openai-compatible-responses.ts @@ -18,7 +18,7 @@ export type Config = RouteDefaultsInput & export type Settings = ProviderPackage.Settings & OpenResponsesProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL: string readonly provider?: string } diff --git a/packages/ai/src/providers/openai-compatible.ts b/packages/ai/src/providers/openai-compatible.ts index 7b604a58fe7..df859e04c8d 100644 --- a/packages/ai/src/providers/openai-compatible.ts +++ b/packages/ai/src/providers/openai-compatible.ts @@ -18,7 +18,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL: string readonly provider?: string } diff --git a/packages/ai/src/providers/openai.ts b/packages/ai/src/providers/openai.ts index b08747b3f8b..62a8de5984d 100644 --- a/packages/ai/src/providers/openai.ts +++ b/packages/ai/src/providers/openai.ts @@ -74,7 +74,7 @@ export const imageGeneration = (options: ImageGenerationOptions = {}) => export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string readonly organization?: string readonly project?: string diff --git a/packages/ai/src/providers/opencode.ts b/packages/ai/src/providers/opencode.ts index 4523e1ba047..c75d6e4694b 100644 --- a/packages/ai/src/providers/opencode.ts +++ b/packages/ai/src/providers/opencode.ts @@ -12,7 +12,7 @@ export type Config = ProviderAuthOption<"optional"> & { readonly http?: HttpOptions.Input } -export type Settings = ProviderPackage.Settings & { readonly apiKey?: string } +export type Settings = ProviderPackage.Settings & { readonly apiKey?: string | false } export const configure = (input: Config = {}) => { const route = SystemOne.route.with({ diff --git a/packages/ai/src/providers/openrouter.ts b/packages/ai/src/providers/openrouter.ts index 5ca22465329..46bca45daf4 100644 --- a/packages/ai/src/providers/openrouter.ts +++ b/packages/ai/src/providers/openrouter.ts @@ -94,7 +94,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & Options & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/togetherai.ts b/packages/ai/src/providers/togetherai.ts index 8e7de7e94a5..1b7c37b0fdd 100644 --- a/packages/ai/src/providers/togetherai.ts +++ b/packages/ai/src/providers/togetherai.ts @@ -17,7 +17,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & OpenAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/typesafe-ai.ts b/packages/ai/src/providers/typesafe-ai.ts index 2f735831cb9..8f87560ce6b 100644 --- a/packages/ai/src/providers/typesafe-ai.ts +++ b/packages/ai/src/providers/typesafe-ai.ts @@ -12,7 +12,7 @@ export type Config = ProviderAuthOption<"optional"> & { readonly http?: HttpOptions.Input } -export type Settings = ProviderPackage.Settings & { readonly apiKey?: string } +export type Settings = ProviderPackage.Settings & { readonly apiKey?: string | false } export const configure = (input: Config = {}) => { const route = SystemOne.route.with({ diff --git a/packages/ai/src/providers/venice.ts b/packages/ai/src/providers/venice.ts index ecce69894b6..7ce5555e54e 100644 --- a/packages/ai/src/providers/venice.ts +++ b/packages/ai/src/providers/venice.ts @@ -18,7 +18,7 @@ export type Config = Omit & } export type Settings = ProviderPackage.Settings & Options & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string readonly queryParams?: Readonly> } diff --git a/packages/ai/src/providers/vercel-ai-gateway.ts b/packages/ai/src/providers/vercel-ai-gateway.ts index d87a1fd5118..4b34cf79d01 100644 --- a/packages/ai/src/providers/vercel-ai-gateway.ts +++ b/packages/ai/src/providers/vercel-ai-gateway.ts @@ -105,7 +105,7 @@ export type Config = Omit & readonly providerOptions?: ProviderOptionsInput } -export type Settings = ProviderPackage.Settings & ProviderOptionsInput & { readonly apiKey?: string } +export type Settings = ProviderPackage.Settings & ProviderOptionsInput & { readonly apiKey?: string | false } const GatewayOptionsSchema = Schema.Struct({ gateway: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)), diff --git a/packages/ai/src/providers/xai.ts b/packages/ai/src/providers/xai.ts index 879b8391624..4782a97158d 100644 --- a/packages/ai/src/providers/xai.ts +++ b/packages/ai/src/providers/xai.ts @@ -25,7 +25,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & XAIProviderOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/zai-coding-plan.ts b/packages/ai/src/providers/zai-coding-plan.ts index e5cc88c57c8..1a22dd22a6e 100644 --- a/packages/ai/src/providers/zai-coding-plan.ts +++ b/packages/ai/src/providers/zai-coding-plan.ts @@ -25,7 +25,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & Options & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/providers/zai.ts b/packages/ai/src/providers/zai.ts index f579a298874..4132bf2a5d7 100644 --- a/packages/ai/src/providers/zai.ts +++ b/packages/ai/src/providers/zai.ts @@ -20,7 +20,7 @@ export type Config = Omit & export type Settings = ProviderPackage.Settings & ChatOptionsInput & { - readonly apiKey?: string + readonly apiKey?: string | false readonly baseURL?: string } diff --git a/packages/ai/src/route/auth-options.ts b/packages/ai/src/route/auth-options.ts index b776243332d..88f907b59eb 100644 --- a/packages/ai/src/route/auth-options.ts +++ b/packages/ai/src/route/auth-options.ts @@ -8,13 +8,16 @@ export type AuthOverride = { readonly apiKey?: never } +/** `false` marks a provider that needs no key: no env lookup and no auth header from the package. */ +export type ApiKey = string | Redacted.Redacted | Config.Config> | false + export type OptionalApiKeyAuth = { - readonly apiKey?: string | Redacted.Redacted | Config.Config> + readonly apiKey?: ApiKey readonly auth?: never } export type RequiredApiKeyAuth = { - readonly apiKey: string | Redacted.Redacted | Config.Config> + readonly apiKey: ApiKey readonly auth?: never } @@ -32,21 +35,28 @@ export type AtLeastOne = { }[keyof T] /** - * Standard bearer-auth resolution for providers: honor an explicit `auth` - * override, otherwise resolve `apiKey` (option > config var) and apply it as - * a bearer token. + * Standard API-key resolution for providers: honor an explicit `auth` + * override, skip auth entirely for `apiKey: false`, otherwise resolve `apiKey` + * (option > config vars) and render it with `apply`. */ -export const bearer = ( +export const apiKey = ( options: ProviderAuthOption<"optional">, envVar: string | ReadonlyArray, + apply: (credential: Auth.Credential) => Auth.Definition, ): Auth.Definition => { if ("auth" in options && options.auth) return options.auth - return (Array.isArray(envVar) ? envVar : [envVar]) - .reduce( + const key = "apiKey" in options ? options.apiKey : undefined + if (key === false) return Auth.none + return apply( + (Array.isArray(envVar) ? envVar : [envVar]).reduce( (auth, name) => auth.orElse(Auth.config(name)), - Auth.optional("apiKey" in options ? options.apiKey : undefined, "apiKey"), - ) - .bearer() + Auth.optional(key, "apiKey"), + ), + ) } +/** `apiKey` resolution applied as `Authorization: Bearer `. */ +export const bearer = (options: ProviderAuthOption<"optional">, envVar: string | ReadonlyArray) => + apiKey(options, envVar, (credential) => credential.bearer()) + export * as AuthOptions from "./auth-options.js" diff --git a/packages/ai/test/auth-options.types.ts b/packages/ai/test/auth-options.types.ts index fd9fe4332c0..7f96b998dfe 100644 --- a/packages/ai/test/auth-options.types.ts +++ b/packages/ai/test/auth-options.types.ts @@ -49,6 +49,7 @@ optionalAuthModel("gpt-4.1-mini") optionalAuthModel("gpt-4.1-mini", {}) optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test" }) optionalAuthModel("gpt-4.1-mini", { apiKey: configApiKey }) +optionalAuthModel("gpt-4.1-mini", { apiKey: false }) optionalAuthModel("gpt-4.1-mini", { auth }) optionalAuthModel("gpt-4.1-mini", { auth, baseURL: "https://gateway.example.com/v1" }) optionalAuthModel("gpt-4.1-mini", { apiKey: "sk-test", headers: { "x-source": "test" } }) @@ -74,6 +75,8 @@ OpenAI.responses("gpt-4.1-mini") OpenAI.configure({}).responses("gpt-4.1-mini") OpenAI.configure({ apiKey: "sk-test" }).responses("gpt-4.1-mini") OpenAI.configure({ apiKey: configApiKey }).responses("gpt-4.1-mini") +OpenAI.configure({ apiKey: false, baseURL: "http://localhost:8080/v1" }).responses("gpt-4.1-mini") +OpenAI.model("gpt-4.1-mini", { apiKey: false, baseURL: "http://localhost:8080/v1" }) OpenAI.configure({ auth: Auth.bearer("oauth-token") }).responses("gpt-4.1-mini") OpenAI.configure({ auth: Auth.headers({ authorization: "Bearer gateway" }), @@ -87,7 +90,10 @@ OpenAI.configure({ // @ts-expect-error OpenAI model selectors only accept model ids. OpenAI.configure({ apiKey: "sk-test" }).responses("gpt-4.1-mini", {}) -// @ts-expect-error apiKey only accepts string, Redacted, or Config>. +// @ts-expect-error apiKey only accepts string, Redacted, Config>, or false. +OpenAI.configure({ apiKey: true }) + +// @ts-expect-error apiKey only accepts string, Redacted, Config>, or false. OpenAI.configure({ apiKey: 123 }) // @ts-expect-error provider helpers reject unknown top-level options. @@ -136,6 +142,7 @@ Azure.configure({ apiKey: "azure-key", resourceName: "resource" }).chat("deploym Azure.configure({ resourceName: "resource", apiKey: "azure-key", auth: Auth.header("api-key", "override") }) Anthropic.configure({ apiKey: "anthropic-key" }).model("claude-haiku") +Anthropic.model("claude-haiku", { apiKey: false, baseURL: "http://localhost:8080/v1" }) Anthropic.configure({ apiKey: "anthropic-key", providerOptions: { @@ -185,6 +192,8 @@ GoogleVertex.configure({ }).model("gemini-3.5-flash") GoogleVertex.configure({ accessToken: "vertex-token", project: "project" }).model("gemini-3.5-flash") GoogleVertex.configure({ auth: Auth.bearer("vertex-token"), project: "project" }).model("gemini-3.5-flash") +// @ts-expect-error Vertex keeps its ADC/express auth and does not accept apiKey: false. +GoogleVertex.configure({ apiKey: false }) // @ts-expect-error Vertex Gemini model selectors only accept model ids. GoogleVertex.configure({ apiKey: "vertex-key" }).model("gemini-3.5-flash", {}) // @ts-expect-error Vertex Gemini config accepts only one auth source. @@ -249,6 +258,8 @@ GoogleVertexMessages.configure({ }) AmazonBedrock.configure({ apiKey: "bedrock-key" }).model("anthropic.claude") +// @ts-expect-error Bedrock keeps its own SigV4/bearer auth and does not accept apiKey: false. +AmazonBedrock.configure({ apiKey: false }) // @ts-expect-error Bedrock model selectors only accept model ids. AmazonBedrock.configure({ apiKey: "bedrock-key" }).model("anthropic.claude", {}) diff --git a/packages/ai/test/keyless-auth.test.ts b/packages/ai/test/keyless-auth.test.ts new file mode 100644 index 00000000000..e13c64d3625 --- /dev/null +++ b/packages/ai/test/keyless-auth.test.ts @@ -0,0 +1,85 @@ +import { afterAll, beforeAll, describe, expect, test } from "bun:test" +import { ai } from "../src/index.js" +import { Anthropic, OpenAICompatible } from "../src/providers.js" +import { sseEvents } from "./lib/sse.js" + +const seen: Array = [] +let server: ReturnType + +beforeAll(() => { + server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch(request) { + seen.push(request.headers) + if (new URL(request.url).pathname.endsWith("/images/generations")) + return Response.json({ data: [{ b64_json: "AQID" }], output_format: "png" }) + if (new URL(request.url).pathname.endsWith("/messages")) + return new Response( + sseEvents( + { type: "message_start", message: { usage: { input_tokens: 1, output_tokens: 0 } } }, + { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }, + { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } }, + { type: "content_block_stop", index: 0 }, + { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } }, + { type: "message_stop" }, + ), + { headers: { "content-type": "text/event-stream" } }, + ) + return new Response( + sseEvents({ choices: [{ delta: { content: "ok" } }] }, { choices: [{ delta: {}, finish_reason: "stop" }] }), + { headers: { "content-type": "text/event-stream" } }, + ) + }, + }) +}) + +afterAll(() => server.stop(true)) + +const baseURL = () => `http://127.0.0.1:${server.port}/v1` + +const lastHeaders = () => { + const headers = seen.at(-1) + if (!headers) throw new Error("stub saw no request") + return headers +} + +describe("apiKey: false", () => { + test("an OpenAI-compatible model without a key still fails by default", async () => { + const model = OpenAICompatible.model("local", { baseURL: baseURL() }) + await expect(ai.generate({ model, prompt: "hi" })).rejects.toThrow("Missing auth credential: apiKey") + }) + + test("an OpenAI-compatible model sends no Authorization header", async () => { + const model = OpenAICompatible.model("local", { apiKey: false, baseURL: baseURL() }) + const response = await ai.generate({ model, prompt: "hi" }) + expect(response.text).toBe("ok") + expect(lastHeaders().get("authorization")).toBeNull() + }) + + test("custom headers are the only auth sent", async () => { + const model = OpenAICompatible.configure({ + apiKey: false, + baseURL: baseURL(), + headers: { "x-gateway-token": "t" }, + }).model("local") + await ai.generate({ model, prompt: "hi" }) + expect(lastHeaders().get("authorization")).toBeNull() + expect(lastHeaders().get("x-gateway-token")).toBe("t") + }) + + test("image models share the keyless auth", async () => { + const model = OpenAICompatible.configure({ apiKey: false, baseURL: baseURL() }).image("gpt-image-2") + const response = await ai.image.generate({ model, prompt: "a lighthouse" }) + expect(response.images).toHaveLength(1) + expect(lastHeaders().get("authorization")).toBeNull() + }) + + test("Anthropic sends no x-api-key header", async () => { + const model = Anthropic.model("claude-local", { apiKey: false, baseURL: baseURL() }) + const response = await ai.generate({ model, prompt: "hi" }) + expect(response.text).toBe("ok") + expect(lastHeaders().get("x-api-key")).toBeNull() + expect(lastHeaders().get("authorization")).toBeNull() + }) +})