Adds the `qwen` built-in provider and the `qwen://` DSN scheme, keyed by QWEN_API_KEY and defaulting to Model Studio's international host. Like kimi (ADR-0026) it is `provider/openai` pointed elsewhere — no new client. Model Studio serves the same models over two protocols, so the real decision was which wire format to speak. ADR-0027 records why it is the OpenAI one: down the anthropic client `ReasoningEffort` is ignored by design, structured output rides the first-party `output_config.format` mechanism the shim does not implement, and cached-token accounting reads Anthropic-only usage fields. Each of those fails silently rather than loudly, which is what makes the choice worth writing down. The shim stays reachable ad hoc via an `anthropic://` DSN. The kimi and qwen DSN factories were byte-identical, so they now share one `openaiCompatScheme` helper: the "credential comes from the DSN token, and the missing-key hint names LLM_<NAME>" rules hold by construction instead of by copy. Tests are hermetic and break-checked (all six fail on a deliberate mutation), including the reverse credential leak — a visible QWEN_API_KEY must not authenticate the openai built-in — and reasoning_effort asserted on the wire body, which is the ADR's load-bearing claim. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
214 lines
9.0 KiB
Go
214 lines
9.0 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
|
|
}
|
|
}
|
|
|
|
// 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] = openaiCompatScheme(openaiOpts)
|
|
|
|
// Qwen (Alibaba Model Studio): same shape as kimi — an OpenAI-compatible
|
|
// Chat Completions endpoint, so it reuses the openai client rather than a
|
|
// new package. Model Studio also exposes an Anthropic-compatible endpoint;
|
|
// ADR-0027 records why the OpenAI one is the built-in. Same unconditional
|
|
// WithAPIKey + WithAPIKeyName discipline as kimi: an unset QWEN_API_KEY
|
|
// must never fall through to OPENAI_API_KEY, and the missing-key error
|
|
// must name the variable the operator actually has to set.
|
|
r.providers[ProviderQwen] = openai.New(openaiOpts(
|
|
openai.WithName(ProviderQwen),
|
|
openai.WithBaseURL(qwenBaseURL),
|
|
openai.WithAPIKey(r.envLookup("QWEN_API_KEY")),
|
|
openai.WithAPIKeyName("QWEN_API_KEY"),
|
|
)...)
|
|
// qwen:// DSN scheme: an OpenAI-compatible target labeled qwen on any
|
|
// Model Studio host (e.g. qwen://[email protected]/compatible-mode/v1
|
|
// for China, or a workspace-scoped regional host).
|
|
r.schemes[ProviderQwen] = openaiCompatScheme(openaiOpts)
|
|
|
|
// 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
|
|
}
|