- kimi:// DSN scheme: missing-credential hint now names the LLM_<NAME> env var that defines the provider (its token comes from the DSN, not KIMI_API_KEY), matching providerFor's lazy-resolution key form. Fixes the correctness/error-handling findings that the old hint misdirected users to set KIMI_API_KEY when the fix is adding a token to the DSN. - parse_test.go: add kimi to TestBuiltinsResolve. (llama-swap stays excluded and is now documented — its no-URL built-in errors at Model() construction, not just on use, so it can't resolve there; the finding's llama-swap half was a false lead the test surfaced.) - Add TestKimiSchemeMissingToken covering the corrected hint. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
172 lines
6.8 KiB
Go
172 lines
6.8 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"
|
|
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). Its
|
|
// credential is the DSN token, not KIMI_API_KEY, so the missing-key hint
|
|
// names the LLM_<NAME> env var that defines this provider (matching the
|
|
// lazy-resolution key form in providerFor) — the fix for a keyless target
|
|
// here is adding a token to that DSN.
|
|
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("LLM_"+strings.ToUpper(strings.ReplaceAll(name, "-", "_"))),
|
|
)...), 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
|
|
}
|