From ec540312da9905f9f2ced6df268a8c3b19a333bd Mon Sep 17 00:00:00 2001 From: I Putu Eddy Irawan Date: Sun, 1 Mar 2026 08:48:04 +0700 Subject: [PATCH 1/4] feat: add Kimi/Moonshot and Opencode provider support - Add "kimi", "kimi-code", "moonshot" provider cases in factory.go with default API base https://api.kimi.com/coding/v1 - Add Kimi Code API User-Agent header (KimiCLI/0.77) for api.kimi.com - Add "opencode" provider with default API base https://opencode.ai/zen/v1 - Add "opencode" to recognized HTTP-compatible protocols in factory_provider - Add Opencode field to ProvidersConfig, IsEmpty, HasProvidersConfig - Add opencode migration entry in ConvertProvidersToModelList - Update moonshot fallback API base from api.moonshot.cn to api.kimi.com --- pkg/config/config.go | 7 +++++-- pkg/config/migration.go | 17 +++++++++++++++++ pkg/providers/factory.go | 20 +++++++++++++++++++- pkg/providers/factory_provider.go | 4 +++- pkg/providers/openai_compat/provider.go | 4 ++++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index d84772d2b..de887114e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -401,6 +401,7 @@ type ProvidersConfig struct { Antigravity ProviderConfig `json:"antigravity"` Qwen ProviderConfig `json:"qwen"` Mistral ProviderConfig `json:"mistral"` + Opencode ProviderConfig `json:"opencode"` } // IsEmpty checks if all provider configs are empty (no API keys or API bases set) @@ -423,7 +424,8 @@ func (p ProvidersConfig) IsEmpty() bool { p.GitHubCopilot.APIKey == "" && p.GitHubCopilot.APIBase == "" && p.Antigravity.APIKey == "" && p.Antigravity.APIBase == "" && p.Qwen.APIKey == "" && p.Qwen.APIBase == "" && - p.Mistral.APIKey == "" && p.Mistral.APIBase == "" + p.Mistral.APIKey == "" && p.Mistral.APIBase == "" && + p.Opencode.APIKey == "" && p.Opencode.APIBase == "" } // MarshalJSON implements custom JSON marshaling for ProvidersConfig @@ -760,7 +762,8 @@ func (c *Config) HasProvidersConfig() bool { v.GitHubCopilot.APIKey != "" || v.GitHubCopilot.APIBase != "" || v.Antigravity.APIKey != "" || v.Antigravity.APIBase != "" || v.Qwen.APIKey != "" || v.Qwen.APIBase != "" || - v.Mistral.APIKey != "" || v.Mistral.APIBase != "" + v.Mistral.APIKey != "" || v.Mistral.APIBase != "" || + v.Opencode.APIKey != "" || v.Opencode.APIBase != "" } // ValidateModelList validates all ModelConfig entries in the model_list. diff --git a/pkg/config/migration.go b/pkg/config/migration.go index 5deb09270..105e35fce 100644 --- a/pkg/config/migration.go +++ b/pkg/config/migration.go @@ -356,6 +356,23 @@ func ConvertProvidersToModelList(cfg *Config) []ModelConfig { }, true }, }, + { + providerNames: []string{"opencode"}, + protocol: "opencode", + buildConfig: func(p ProvidersConfig) (ModelConfig, bool) { + if p.Opencode.APIKey == "" && p.Opencode.APIBase == "" { + return ModelConfig{}, false + } + return ModelConfig{ + ModelName: "opencode", + Model: "opencode/auto", + APIKey: p.Opencode.APIKey, + APIBase: p.Opencode.APIBase, + Proxy: p.Opencode.Proxy, + RequestTimeout: p.Opencode.RequestTimeout, + }, true + }, + }, } // Process each provider migration diff --git a/pkg/providers/factory.go b/pkg/providers/factory.go index 11af14da4..a332c39ee 100644 --- a/pkg/providers/factory.go +++ b/pkg/providers/factory.go @@ -181,6 +181,24 @@ func resolveProviderSelection(cfg *config.Config) (providerSelection, error) { sel.apiBase = "https://api.mistral.ai/v1" } } + case "opencode": + if cfg.Providers.Opencode.APIKey != "" { + sel.apiKey = cfg.Providers.Opencode.APIKey + sel.apiBase = cfg.Providers.Opencode.APIBase + sel.proxy = cfg.Providers.Opencode.Proxy + if sel.apiBase == "" { + sel.apiBase = "https://opencode.ai/zen/v1" + } + } + case "kimi", "kimi-code", "moonshot": + if cfg.Providers.Moonshot.APIKey != "" { + sel.apiKey = cfg.Providers.Moonshot.APIKey + sel.apiBase = cfg.Providers.Moonshot.APIBase + sel.proxy = cfg.Providers.Moonshot.Proxy + if sel.apiBase == "" { + sel.apiBase = "https://api.kimi.com/coding/v1" + } + } case "github_copilot", "copilot": sel.providerType = providerTypeGitHubCopilot if cfg.Providers.GitHubCopilot.APIBase != "" { @@ -201,7 +219,7 @@ func resolveProviderSelection(cfg *config.Config) (providerSelection, error) { sel.apiBase = cfg.Providers.Moonshot.APIBase sel.proxy = cfg.Providers.Moonshot.Proxy if sel.apiBase == "" { - sel.apiBase = "https://api.moonshot.cn/v1" + sel.apiBase = "https://api.kimi.com/coding/v1" } case strings.HasPrefix(model, "openrouter/") || strings.HasPrefix(model, "anthropic/") || diff --git a/pkg/providers/factory_provider.go b/pkg/providers/factory_provider.go index 53f7a08a0..1ddd056a4 100644 --- a/pkg/providers/factory_provider.go +++ b/pkg/providers/factory_provider.go @@ -94,7 +94,7 @@ func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, err case "openrouter", "groq", "zhipu", "gemini", "nvidia", "ollama", "moonshot", "shengsuanyun", "deepseek", "cerebras", - "volcengine", "vllm", "qwen", "mistral": + "volcengine", "vllm", "qwen", "mistral", "opencode": // All other OpenAI-compatible HTTP providers if cfg.APIKey == "" && cfg.APIBase == "" { return nil, "", fmt.Errorf("api_key or api_base is required for HTTP-based protocol %q", protocol) @@ -206,6 +206,8 @@ func getDefaultAPIBase(protocol string) string { return "http://localhost:8000/v1" case "mistral": return "https://api.mistral.ai/v1" + case "opencode": + return "https://opencode.ai/zen/v1" default: return "" } diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index 5dab9b03e..636a6ae97 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -176,6 +176,10 @@ func (p *Provider) Chat( if p.apiKey != "" { req.Header.Set("Authorization", "Bearer "+p.apiKey) } + // Kimi Code API requires a coding agent User-Agent + if strings.Contains(p.apiBase, "api.kimi.com") { + req.Header.Set("User-Agent", "KimiCLI/0.77") + } resp, err := p.httpClient.Do(req) if err != nil { From 9c91d66427bef2475743336c3db6551e3cd17083 Mon Sep 17 00:00:00 2001 From: I Putu Eddy Irawan Date: Sun, 1 Mar 2026 09:22:49 +0700 Subject: [PATCH 2/4] Address Copilot review feedback for Kimi/Opencode providers - Allow APIBase-only config for opencode provider selection (like VLLM) - Keep moonshot provider on moonshot.cn/v1 default, only use kimi.com/coding/v1 for kimi/kimi-code - Use url.Parse hostname match for Kimi User-Agent check instead of strings.Contains - Add opencode to DefaultAPIBase test cases in factory_provider_test.go - Add opencode migration tests (full config + APIBase-only) in migration_test.go - Update AllProviders test count to include opencode (18 -> 19) Co-Authored-By: Claude Opus 4.6 --- pkg/config/migration_test.go | 66 +++++++++++++++++++++++-- pkg/providers/factory.go | 14 ++++-- pkg/providers/factory_provider_test.go | 1 + pkg/providers/openai_compat/provider.go | 2 +- 4 files changed, 76 insertions(+), 7 deletions(-) diff --git a/pkg/config/migration_test.go b/pkg/config/migration_test.go index db8f4657d..7fda3a1fc 100644 --- a/pkg/config/migration_test.go +++ b/pkg/config/migration_test.go @@ -132,14 +132,15 @@ func TestConvertProvidersToModelList_AllProviders(t *testing.T) { Antigravity: ProviderConfig{AuthMethod: "oauth"}, Qwen: ProviderConfig{APIKey: "key17"}, Mistral: ProviderConfig{APIKey: "key18"}, + Opencode: ProviderConfig{APIKey: "key19"}, }, } result := ConvertProvidersToModelList(cfg) - // All 18 providers should be converted - if len(result) != 18 { - t.Errorf("len(result) = %d, want 18", len(result)) + // All 19 providers should be converted + if len(result) != 19 { + t.Errorf("len(result) = %d, want 19", len(result)) } } @@ -551,6 +552,65 @@ func TestBuildModelWithProtocol_DifferentPrefix(t *testing.T) { } } +func TestConvertProvidersToModelList_Opencode(t *testing.T) { + cfg := &Config{ + Providers: ProvidersConfig{ + Opencode: ProviderConfig{ + APIKey: "oc-test-key", + APIBase: "https://custom.opencode.ai/v1", + Proxy: "http://proxy:9090", + RequestTimeout: 60, + }, + }, + } + + result := ConvertProvidersToModelList(cfg) + + if len(result) != 1 { + t.Fatalf("len(result) = %d, want 1", len(result)) + } + + mc := result[0] + if mc.ModelName != "opencode" { + t.Errorf("ModelName = %q, want %q", mc.ModelName, "opencode") + } + if mc.Model != "opencode/auto" { + t.Errorf("Model = %q, want %q", mc.Model, "opencode/auto") + } + if mc.APIKey != "oc-test-key" { + t.Errorf("APIKey = %q, want %q", mc.APIKey, "oc-test-key") + } + if mc.APIBase != "https://custom.opencode.ai/v1" { + t.Errorf("APIBase = %q, want %q", mc.APIBase, "https://custom.opencode.ai/v1") + } + if mc.Proxy != "http://proxy:9090" { + t.Errorf("Proxy = %q, want %q", mc.Proxy, "http://proxy:9090") + } + if mc.RequestTimeout != 60 { + t.Errorf("RequestTimeout = %d, want %d", mc.RequestTimeout, 60) + } +} + +func TestConvertProvidersToModelList_Opencode_APIBaseOnly(t *testing.T) { + cfg := &Config{ + Providers: ProvidersConfig{ + Opencode: ProviderConfig{ + APIBase: "https://custom.opencode.ai/v1", + }, + }, + } + + result := ConvertProvidersToModelList(cfg) + + if len(result) != 1 { + t.Fatalf("len(result) = %d, want 1 (APIBase-only should create entry)", len(result)) + } + + if result[0].ModelName != "opencode" { + t.Errorf("ModelName = %q, want %q", result[0].ModelName, "opencode") + } +} + // Test for legacy config with protocol prefix in model name func TestConvertProvidersToModelList_LegacyModelWithProtocolPrefix(t *testing.T) { cfg := &Config{ diff --git a/pkg/providers/factory.go b/pkg/providers/factory.go index a332c39ee..3f46d0f3d 100644 --- a/pkg/providers/factory.go +++ b/pkg/providers/factory.go @@ -182,7 +182,7 @@ func resolveProviderSelection(cfg *config.Config) (providerSelection, error) { } } case "opencode": - if cfg.Providers.Opencode.APIKey != "" { + if cfg.Providers.Opencode.APIKey != "" || cfg.Providers.Opencode.APIBase != "" { sel.apiKey = cfg.Providers.Opencode.APIKey sel.apiBase = cfg.Providers.Opencode.APIBase sel.proxy = cfg.Providers.Opencode.Proxy @@ -196,7 +196,11 @@ func resolveProviderSelection(cfg *config.Config) (providerSelection, error) { sel.apiBase = cfg.Providers.Moonshot.APIBase sel.proxy = cfg.Providers.Moonshot.Proxy if sel.apiBase == "" { - sel.apiBase = "https://api.kimi.com/coding/v1" + if providerName == "moonshot" { + sel.apiBase = "https://api.moonshot.cn/v1" + } else { + sel.apiBase = "https://api.kimi.com/coding/v1" + } } } case "github_copilot", "copilot": @@ -219,7 +223,11 @@ func resolveProviderSelection(cfg *config.Config) (providerSelection, error) { sel.apiBase = cfg.Providers.Moonshot.APIBase sel.proxy = cfg.Providers.Moonshot.Proxy if sel.apiBase == "" { - sel.apiBase = "https://api.kimi.com/coding/v1" + if strings.Contains(lowerModel, "moonshot") || strings.HasPrefix(model, "moonshot/") { + sel.apiBase = "https://api.moonshot.cn/v1" + } else { + sel.apiBase = "https://api.kimi.com/coding/v1" + } } case strings.HasPrefix(model, "openrouter/") || strings.HasPrefix(model, "anthropic/") || diff --git a/pkg/providers/factory_provider_test.go b/pkg/providers/factory_provider_test.go index e0c0eddef..eccb8cd40 100644 --- a/pkg/providers/factory_provider_test.go +++ b/pkg/providers/factory_provider_test.go @@ -112,6 +112,7 @@ func TestCreateProviderFromConfig_DefaultAPIBase(t *testing.T) { {"vllm", "vllm"}, {"deepseek", "deepseek"}, {"ollama", "ollama"}, + {"opencode", "opencode"}, } for _, tt := range tests { diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index 636a6ae97..98e69fd2a 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -177,7 +177,7 @@ func (p *Provider) Chat( req.Header.Set("Authorization", "Bearer "+p.apiKey) } // Kimi Code API requires a coding agent User-Agent - if strings.Contains(p.apiBase, "api.kimi.com") { + if parsedURL, parseErr := url.Parse(p.apiBase); parseErr == nil && parsedURL.Hostname() == "api.kimi.com" { req.Header.Set("User-Agent", "KimiCLI/0.77") } From 2fc87985d2b43b5482d4be021d08e9911ce4c737 Mon Sep 17 00:00:00 2001 From: I Putu Eddy Irawan Date: Tue, 3 Mar 2026 09:18:26 +0700 Subject: [PATCH 3/4] fix: add kimi-code migration alias and User-Agent test - Add "kimi-code" to the moonshot provider's providerNames in ConvertProvidersToModelList so configs using agents.defaults.provider: "kimi-code" migrate correctly. - Add TestProviderChat_KimiCodeUserAgent verifying that User-Agent: KimiCLI/0.77 is set when apiBase hostname is api.kimi.com and not set for other hosts. Co-Authored-By: Claude Opus 4.6 --- pkg/config/migration.go | 2 +- pkg/providers/openai_compat/provider_test.go | 78 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/pkg/config/migration.go b/pkg/config/migration.go index 105e35fce..2475f5aa9 100644 --- a/pkg/config/migration.go +++ b/pkg/config/migration.go @@ -208,7 +208,7 @@ func ConvertProvidersToModelList(cfg *Config) []ModelConfig { }, }, { - providerNames: []string{"moonshot", "kimi"}, + providerNames: []string{"moonshot", "kimi", "kimi-code"}, protocol: "moonshot", buildConfig: func(p ProvidersConfig) (ModelConfig, bool) { if p.Moonshot.APIKey == "" && p.Moonshot.APIBase == "" { diff --git a/pkg/providers/openai_compat/provider_test.go b/pkg/providers/openai_compat/provider_test.go index d9e6ba871..014451144 100644 --- a/pkg/providers/openai_compat/provider_test.go +++ b/pkg/providers/openai_compat/provider_test.go @@ -2,9 +2,11 @@ package openai_compat import ( "encoding/json" + "io" "net/http" "net/http/httptest" "net/url" + "strings" "testing" "time" ) @@ -411,3 +413,79 @@ func TestProvider_FunctionalOptionRequestTimeoutNonPositive(t *testing.T) { t.Fatalf("http timeout = %v, want %v", p.httpClient.Timeout, defaultRequestTimeout) } } + +// roundTripFunc adapts a function to http.RoundTripper for test injection. +type roundTripFunc func(*http.Request) (*http.Response, error) + +func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { + return f(r) +} + +func TestProviderChat_KimiCodeUserAgent(t *testing.T) { + okBody := `{"choices":[{"message":{"content":"ok"},"finish_reason":"stop"}]}` + + tests := []struct { + name string + apiBase string + wantAgent string + }{ + { + name: "sets KimiCLI User-Agent for api.kimi.com", + apiBase: "https://api.kimi.com/coding/v1", + wantAgent: "KimiCLI/0.77", + }, + { + name: "does not set KimiCLI User-Agent for other hosts", + apiBase: "https://api.example.com/v1", + wantAgent: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var gotUserAgent string + + p := NewProvider("key", tt.apiBase, "") + p.httpClient.Transport = roundTripFunc( + func(r *http.Request) (*http.Response, error) { + gotUserAgent = r.Header.Get("User-Agent") + return &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser( + strings.NewReader(okBody), + ), + Header: http.Header{ + "Content-Type": {"application/json"}, + }, + }, nil + }, + ) + + _, err := p.Chat( + t.Context(), + []Message{{Role: "user", Content: "hi"}}, + nil, + "kimi-k2.5", + nil, + ) + if err != nil { + t.Fatalf("Chat() error = %v", err) + } + + if tt.wantAgent != "" { + if gotUserAgent != tt.wantAgent { + t.Fatalf( + "User-Agent = %q, want %q", + gotUserAgent, tt.wantAgent, + ) + } + } else { + if gotUserAgent == "KimiCLI/0.77" { + t.Fatalf( + "User-Agent should not be KimiCLI/0.77 for non-kimi host", + ) + } + } + }) + } +} From e54b1d39a5bf1991f150ae9230631a37d3f3017c Mon Sep 17 00:00:00 2001 From: I Putu Eddy Irawan Date: Tue, 3 Mar 2026 21:34:31 +0700 Subject: [PATCH 4/4] refactor: parse Kimi API hostname once in constructor instead of per-call Avoid re-parsing apiBase URL on every Chat() invocation by computing isKimiAPI once in NewProvider(). Also document why the KimiCLI/0.77 User-Agent string is required. Co-Authored-By: Claude Opus 4.6 --- pkg/providers/openai_compat/provider.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index a617cc565..b0718384f 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -33,6 +33,7 @@ type Provider struct { apiBase string maxTokensField string // Field name for max tokens (e.g., "max_completion_tokens" for o1/glm models) httpClient *http.Client + isKimiAPI bool // true when apiBase points to api.kimi.com } type Option func(*Provider) @@ -69,10 +70,17 @@ func NewProvider(apiKey, apiBase, proxy string, opts ...Option) *Provider { } } + trimmedBase := strings.TrimRight(apiBase, "/") + var isKimi bool + if parsed, err := url.Parse(trimmedBase); err == nil { + isKimi = parsed.Hostname() == "api.kimi.com" + } + p := &Provider{ apiKey: apiKey, - apiBase: strings.TrimRight(apiBase, "/"), + apiBase: trimmedBase, httpClient: client, + isKimiAPI: isKimi, } for _, opt := range opts { @@ -176,8 +184,10 @@ func (p *Provider) Chat( if p.apiKey != "" { req.Header.Set("Authorization", "Bearer "+p.apiKey) } - // Kimi Code API requires a coding agent User-Agent - if parsedURL, parseErr := url.Parse(p.apiBase); parseErr == nil && parsedURL.Hostname() == "api.kimi.com" { + // Kimi Code API rejects requests without a recognized coding-agent + // User-Agent. "KimiCLI/0.77" is the minimum version string accepted + // by the api.kimi.com/coding/v1 endpoint (per Kimi's API docs). + if p.isKimiAPI { req.Header.Set("User-Agent", "KimiCLI/0.77") }