mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-21 04:12:08 +03:00
feat(ai): apiKey: false for keyless providers (#60770)
This commit is contained in:
committed by
GitHub
Unverified
parent
bc9971744f
commit
12f7e1b59a
@@ -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:
|
||||
|
||||
@@ -46,7 +46,7 @@ export type Config = Location &
|
||||
export type Settings<Options = ChatOptionsInput> = Location &
|
||||
ProviderPackage.Settings &
|
||||
Options & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
}
|
||||
|
||||
const hosts = new Map<string, string>([
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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<Record<string, string>>
|
||||
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 = <Body, Prepared, Compact extends CompactionOperations | undefined>(
|
||||
|
||||
@@ -24,7 +24,7 @@ export interface BasetenDecisionOptions {
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
OpenAIProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
OpenAIProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -16,7 +16,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
readonly providerOptions?: ProviderOptions
|
||||
}
|
||||
export type Settings<Options = CohereChat.ProviderOptionsInput> = 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({
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -17,7 +17,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
OpenAIProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
OpenAIProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
}
|
||||
|
||||
export type Settings<Options = OpenAIProviderOptionsInput> = ProviderPackage.Settings &
|
||||
Options & { readonly apiKey?: string }
|
||||
Options & { readonly apiKey?: string | false }
|
||||
|
||||
export const protocol = Protocol.make({
|
||||
id: "digitalocean-chat",
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -17,7 +17,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
OpenAIProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
ProviderOptions & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
ProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings<Options = MessagesOptionsInput> = ProviderPackage.Settings &
|
||||
Options & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
ProviderOptions & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings<Options = ChatOptionsInput> = ProviderPackage.Settings &
|
||||
Options & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
OpenAIProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL: string
|
||||
readonly provider?: string
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -94,7 +94,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings<Options = OpenRouterProviderOptionsInput> = ProviderPackage.Settings &
|
||||
Options & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
OpenAIProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -18,7 +18,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
}
|
||||
export type Settings<Options = ChatOptionsInput> = ProviderPackage.Settings &
|
||||
Options & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
readonly queryParams?: Readonly<Record<string, string>>
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
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)),
|
||||
|
||||
@@ -25,7 +25,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
XAIProviderOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings<Options = ChatOptionsInput> = ProviderPackage.Settings &
|
||||
Options & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ export type Config = Omit<RouteDefaultsInput, "providerOptions"> &
|
||||
|
||||
export type Settings = ProviderPackage.Settings &
|
||||
ChatOptionsInput & {
|
||||
readonly apiKey?: string
|
||||
readonly apiKey?: string | false
|
||||
readonly baseURL?: string
|
||||
}
|
||||
|
||||
|
||||
@@ -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<string> | Config.Config<string | Redacted.Redacted<string>> | false
|
||||
|
||||
export type OptionalApiKeyAuth = {
|
||||
readonly apiKey?: string | Redacted.Redacted<string> | Config.Config<string | Redacted.Redacted<string>>
|
||||
readonly apiKey?: ApiKey
|
||||
readonly auth?: never
|
||||
}
|
||||
|
||||
export type RequiredApiKeyAuth = {
|
||||
readonly apiKey: string | Redacted.Redacted<string> | Config.Config<string | Redacted.Redacted<string>>
|
||||
readonly apiKey: ApiKey
|
||||
readonly auth?: never
|
||||
}
|
||||
|
||||
@@ -32,21 +35,28 @@ export type AtLeastOne<T> = {
|
||||
}[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<string>,
|
||||
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 <key>`. */
|
||||
export const bearer = (options: ProviderAuthOption<"optional">, envVar: string | ReadonlyArray<string>) =>
|
||||
apiKey(options, envVar, (credential) => credential.bearer())
|
||||
|
||||
export * as AuthOptions from "./auth-options.js"
|
||||
|
||||
@@ -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<string>, or Config<string | Redacted<string>>.
|
||||
// @ts-expect-error apiKey only accepts string, Redacted<string>, Config<string | Redacted<string>>, or false.
|
||||
OpenAI.configure({ apiKey: true })
|
||||
|
||||
// @ts-expect-error apiKey only accepts string, Redacted<string>, Config<string | Redacted<string>>, 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", {})
|
||||
|
||||
|
||||
@@ -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<Headers> = []
|
||||
let server: ReturnType<typeof Bun.serve>
|
||||
|
||||
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()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user