Add a first-class `kimi` provider and `kimi://` DSN scheme for Moonshot AI's OpenAI-compatible Chat Completions API. Both reuse provider/openai (no new client, mirroring llama-swap's chat path). Default endpoint is the international host; the China endpoint is reachable via a kimi:// LLM_* DSN. - Credential is KIMI_API_KEY, read through the registry's injected envLookup so it stays hermetically testable. WithAPIKey is passed unconditionally so an unset KIMI_API_KEY can never fall through to the openai client's OPENAI_API_KEY default. - New openai.WithAPIKeyName option customizes the missing-key error hint (default OPENAI_API_KEY); kimi names KIMI_API_KEY. - Hermetic tests: built-in base URL + bearer, missing-key hint names KIMI_API_KEY with no OPENAI fallthrough and no network hit, kimi:// scheme round-trips against the China host. - Docs in sync: README built-in table + DSN scheme list + support matrix, .env.example, env.go DSN doc, ADR-0026 (+ index, backfilling 0024/0025), progress.md. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
167 lines
6.4 KiB
Go
167 lines
6.4 KiB
Go
package majordomo
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"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"
|
|
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"
|
|
|
|
// 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
|
|
}
|
|
|
|
// Kimi (Moonshot AI): OpenAI-compatible Chat Completions, so it reuses the
|
|
// openai client (like llama-swap). Defaults to Moonshot's international
|
|
// endpoint and the KIMI_API_KEY credential. WithAPIKey is passed
|
|
// unconditionally — even empty — so an unset KIMI_API_KEY can never fall
|
|
// through to the openai client's OPENAI_API_KEY default; WithAPIKeyName
|
|
// makes the missing-key error name KIMI_API_KEY.
|
|
r.providers[ProviderKimi] = openai.New(openaiOpts(
|
|
openai.WithName(ProviderKimi),
|
|
openai.WithBaseURL(kimiBaseURL),
|
|
openai.WithAPIKey(r.envLookup("KIMI_API_KEY")),
|
|
openai.WithAPIKeyName("KIMI_API_KEY"),
|
|
)...)
|
|
// kimi:// DSN scheme: an OpenAI-compatible target labeled kimi, base URL
|
|
// from the DSN host (e.g. kimi://[email protected]/v1 for China).
|
|
r.schemes[ProviderKimi] = func(name string, dsn DSN) (llm.Provider, error) {
|
|
return openai.New(openaiOpts(
|
|
openai.WithName(name),
|
|
openai.WithBaseURL(dsn.BaseURL()),
|
|
openai.WithAPIKey(dsn.Token),
|
|
openai.WithAPIKeyName("KIMI_API_KEY"),
|
|
)...), nil
|
|
}
|
|
|
|
// 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
|
|
}
|