Files
majordomo/builtin.go
T
steveandClaude Opus 5 f1f2b653c3
CI / Tidy (pull_request) Successful in 9m25s
CI / Build & Test (pull_request) Successful in 9m50s
refactor: gadfly round 2 — both halves of an OpenAI-compat built-in register together
Same class of finding as round 1, one level in: I factored the DSN-scheme half
of the kimi/qwen duplication into openaiCompatScheme and left the eager
provider half copy-pasted, so a third built-in still had six lines to clone —
including both credential rules, which is exactly the pair you do not want
re-typed.

registerOpenAICompatBuiltin now installs both halves from one call. The rules
that matter hold by construction for every future caller: WithAPIKey passed
unconditionally (an unset key must not fall through to OPENAI_API_KEY), and
WithAPIKeyName naming that same variable in the 401 hint. Registering kimi and
qwen is now one line each.

Also fixed a cross-reference the ADR got wrong: Qwen's image-input caveat is
README matrix footnote ⁴, not ³ — ³ is kimi's. I wrote "³, shared with kimi"
in the ADR and then gave Qwen its own footnote in the README.

The break-check harness needed fixing before any of this could be trusted:
three of its mutations targeted lines this refactor moved, so they matched
nothing, the code was never broken, and the suite reported "test still passed"
— identical output to a test that genuinely misses the bug. Mutations are now
verified to have landed (sha before/after) and the suite fails loudly if one
doesn't. Two new cases cover the helper: dropping the unconditional WithAPIKey,
and dropping the scheme-half registration. 8/8 apply and are caught.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-08-12 16:25:38 -04:00

217 lines
9.3 KiB
Go

package majordomo
import (
"net/http"
"strings"
"gitea.stevedudenhoeffer.com/steve/majordomo/llm"
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/anthropic"
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/google"
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/llamaswap"
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/ollama"
"gitea.stevedudenhoeffer.com/steve/majordomo/provider/openai"
)
// Built-in provider names.
const (
ProviderOpenAI = "openai"
// ProviderKimi is Moonshot AI's Kimi models over their OpenAI-compatible
// Chat Completions endpoint. Reuses the openai client (like llama-swap);
// keyed by KIMI_API_KEY, default base URL kimiBaseURL.
ProviderKimi = "kimi"
// ProviderQwen is Alibaba's Qwen models over Model Studio's
// OpenAI-compatible Chat Completions endpoint. Reuses the openai client
// (like kimi and llama-swap); keyed by QWEN_API_KEY, default base URL
// qwenBaseURL. Why OpenAI-compat and not the Anthropic-compat endpoint
// Model Studio also exposes: see ADR-0027 — the OpenAI surface is the
// first-class one there (reasoning_effort, json_schema structured output,
// cached_tokens accounting all ride it), while the Anthropic shim exists
// mainly to host Claude Code.
ProviderQwen = "qwen"
ProviderAnthropic = "anthropic"
ProviderGoogle = "google"
ProviderOllama = "ollama"
ProviderOllamaCloud = "ollama-cloud"
ProviderForeman = "foreman"
ProviderLlamaSwap = "llama-swap"
// ProviderLlamaSwapTLS is the DSN scheme for a TLS-fronted llama-swap
// (https base URL). It is a scheme only, not a default built-in provider
// name. Why a separate scheme rather than auto-detecting: a DSN carries no
// reliable signal for http vs https, so the choice is explicit
// (llama-swap = http local-first, llama-swaps = https), mirroring rediss.
ProviderLlamaSwapTLS = "llama-swaps"
)
// kimiBaseURL is Moonshot AI's international OpenAI-compatible endpoint. The
// China endpoint (api.moonshot.cn/v1) is reachable via a kimi:// LLM_* DSN.
const kimiBaseURL = "https://api.moonshot.ai/v1"
// qwenBaseURL is Alibaba Model Studio's international (Singapore) endpoint in
// OpenAI-compatible mode. The China endpoint
// (dashscope.aliyuncs.com/compatible-mode/v1) and any regional host are
// reachable via a qwen:// LLM_* DSN.
const qwenBaseURL = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
// openaiCompatScheme builds the DSN factory shared by every built-in that is
// "the openai client pointed somewhere else" (kimi, qwen, ...). The provider
// is named after the LLM_<NAME> var that defined it, takes its credential from
// the DSN token — not the built-in's own env var, which does nothing for a
// DSN-defined provider — and so names that same LLM_<NAME> var in the
// missing-key hint, matching the lazy-resolution key form in providerFor.
//
// wrap is the caller's option-decorator (it injects the registry's HTTP
// client), so a DSN provider is built exactly like the eager built-ins.
func openaiCompatScheme(wrap func(...openai.Option) []openai.Option) SchemeFactory {
return func(name string, dsn DSN) (llm.Provider, error) {
return openai.New(wrap(
openai.WithName(name),
openai.WithBaseURL(dsn.BaseURL()),
openai.WithAPIKey(dsn.Token),
openai.WithAPIKeyName("LLM_"+strings.ToUpper(strings.ReplaceAll(name, "-", "_"))),
)...), nil
}
}
// registerOpenAICompatBuiltin installs BOTH halves of an OpenAI-compat
// built-in: the eager provider under name (credential from keyEnv) and the
// matching name:// DSN scheme. Why both in one call: the two halves are a pair
// — a built-in whose scheme is missing resolves as a spec but not from an
// LLM_* DSN, and the credential rules below have to hold identically in each.
// Adding the next one is a single line rather than six lines to copy.
//
// The two credential rules, holding by construction for every caller:
// - WithAPIKey is passed UNCONDITIONALLY, even when the lookup comes back
// empty. openai.New defaults its key to OPENAI_API_KEY, so anything less
// lets an unset keyEnv silently authenticate as OpenAI.
// - WithAPIKeyName makes the synthetic-401 hint name keyEnv, so a keyless
// call tells the operator the variable that actually fixes it.
func registerOpenAICompatBuiltin(r *Registry, wrap func(...openai.Option) []openai.Option, name, baseURL, keyEnv string) {
r.providers[name] = openai.New(wrap(
openai.WithName(name),
openai.WithBaseURL(baseURL),
openai.WithAPIKey(r.envLookup(keyEnv)),
openai.WithAPIKeyName(keyEnv),
)...)
r.schemes[name] = openaiCompatScheme(wrap)
}
// registerBuiltins installs the built-in providers and env-DSN scheme
// factories into a fresh registry. httpClient, when non-nil, is used by
// every provider and factory the registry itself constructs.
func registerBuiltins(r *Registry, httpClient *http.Client) {
ollamaOpts := func(extra ...ollama.Option) []ollama.Option {
if httpClient != nil {
extra = append(extra, ollama.WithHTTPClient(httpClient))
}
return extra
}
// Native-Ollama family: three names over one client with presets.
r.providers[ProviderOllama] = ollama.Local(ollamaOpts()...)
r.providers[ProviderOllamaCloud] = ollama.Cloud(ollamaOpts()...)
// foreman has no default URL; the no-DSN registration resolves but
// errors on use with a clear message (use an LLM_* DSN or
// ollama.Foreman(...) + RegisterProvider).
r.providers[ProviderForeman] = ollama.New(ollamaOpts(ollama.WithName(ProviderForeman))...)
ollamaScheme := func(name string, dsn DSN) (llm.Provider, error) {
return ollama.New(ollamaOpts(
ollama.WithName(name),
ollama.WithBaseURL(dsn.BaseURL()),
ollama.WithToken(dsn.Token),
)...), nil
}
r.schemes[ProviderOllama] = ollamaScheme
r.schemes[ProviderOllamaCloud] = ollamaScheme
r.schemes[ProviderForeman] = ollamaScheme
// OpenAI and OpenAI-compatible endpoints.
openaiOpts := func(extra ...openai.Option) []openai.Option {
if httpClient != nil {
extra = append(extra, openai.WithHTTPClient(httpClient))
}
return extra
}
r.providers[ProviderOpenAI] = openai.New(openaiOpts()...)
r.schemes[ProviderOpenAI] = func(name string, dsn DSN) (llm.Provider, error) {
return openai.New(openaiOpts(
openai.WithName(name),
openai.WithBaseURL(dsn.BaseURL()),
openai.WithAPIKey(dsn.Token),
)...), nil
}
// Third-party endpoints that ARE the openai client at another base URL —
// no new package, mirroring llama-swap's chat path. Each gets the eager
// built-in plus its name:// DSN scheme, and the credential rules hold by
// construction (see registerOpenAICompatBuiltin).
//
// kimi (ADR-0026): Moonshot's international endpoint; China host via
// kimi://[email protected]/v1.
registerOpenAICompatBuiltin(r, openaiOpts, ProviderKimi, kimiBaseURL, "KIMI_API_KEY")
// qwen (ADR-0027): Alibaba Model Studio's international host. Model Studio
// also exposes an Anthropic-compatible endpoint; the ADR records why the
// OpenAI one is the built-in. China / workspace-scoped regional hosts via
// qwen://[email protected]/compatible-mode/v1.
registerOpenAICompatBuiltin(r, openaiOpts, ProviderQwen, qwenBaseURL, "QWEN_API_KEY")
// llama-swap: OpenAI-compatible chat + image generation + management
// endpoints over a model-swapping proxy. Chat reuses the openai client
// (provider/llamaswap delegates). Two schemes: "llama-swap" builds an
// http:// base URL (local-first default), "llama-swaps" builds https://
// for a TLS-fronted instance (mirrors redis/rediss). The no-DSN built-in
// errors on use with a clear message, mirroring foreman.
llamaSwapOpts := func(extra ...llamaswap.Option) []llamaswap.Option {
if httpClient != nil {
extra = append(extra, llamaswap.WithHTTPClient(httpClient))
}
return extra
}
llamaSwapScheme := func(urlScheme string) SchemeFactory {
return func(name string, dsn DSN) (llm.Provider, error) {
return llamaswap.New(llamaSwapOpts(
llamaswap.WithName(name),
llamaswap.WithBaseURL(urlScheme+"://"+dsn.Host),
llamaswap.WithToken(dsn.Token),
)...), nil
}
}
r.providers[ProviderLlamaSwap] = llamaswap.New(llamaSwapOpts(llamaswap.WithName(ProviderLlamaSwap))...)
r.schemes[ProviderLlamaSwap] = llamaSwapScheme("http")
r.schemes[ProviderLlamaSwapTLS] = llamaSwapScheme("https")
// Anthropic and Anthropic-compatible endpoints.
anthropicOpts := func(extra ...anthropic.Option) []anthropic.Option {
if httpClient != nil {
extra = append(extra, anthropic.WithHTTPClient(httpClient))
}
return extra
}
r.providers[ProviderAnthropic] = anthropic.New(anthropicOpts()...)
r.schemes[ProviderAnthropic] = func(name string, dsn DSN) (llm.Provider, error) {
return anthropic.New(anthropicOpts(
anthropic.WithName(name),
anthropic.WithBaseURL(dsn.BaseURL()),
anthropic.WithAPIKey(dsn.Token),
)...), nil
}
// Google (Gemini) on the official SDK; "gemini" is an alternate scheme.
googleOpts := func(extra ...google.Option) []google.Option {
if httpClient != nil {
extra = append(extra, google.WithHTTPClient(httpClient))
}
return extra
}
r.providers[ProviderGoogle] = google.New(googleOpts()...)
googleScheme := func(name string, dsn DSN) (llm.Provider, error) {
return google.New(googleOpts(
google.WithName(name),
google.WithBaseURL(dsn.BaseURL()),
google.WithAPIKey(dsn.Token),
)...), nil
}
r.schemes[ProviderGoogle] = googleScheme
r.schemes["gemini"] = googleScheme
}