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 {