From 1773af53603bf59a5157463ff2b36e2bdff57e10 Mon Sep 17 00:00:00 2001 From: Nithwin Date: Thu, 16 Jul 2026 16:49:42 +0530 Subject: [PATCH] refactor: lock provider base URLs and add command-line flags for model and provider overrides --- cmd/chat.go | 19 +++++++++++++++++++ cmd/set.go | 2 +- internal/config/config.go | 10 +--------- internal/providers/anthropic/provider.go | 9 +-------- internal/providers/groq/provider.go | 9 +-------- internal/providers/ollama/provider.go | 9 +-------- internal/providers/openai/provider.go | 9 +-------- 7 files changed, 25 insertions(+), 42 deletions(-) diff --git a/cmd/chat.go b/cmd/chat.go index 341e3d9..042da8b 100644 --- a/cmd/chat.go +++ b/cmd/chat.go @@ -13,6 +13,11 @@ import ( "github.com/spf13/cobra" ) +var ( + flagModel string + flagProvider string +) + var chatCmd = &cobra.Command{ Use: "chat ", Args: cobra.MinimumNArgs(1), @@ -24,6 +29,18 @@ var chatCmd = &cobra.Command{ log.Fatal(err) } + if flagProvider != "" { + if err := cfg.SetProvider(flagProvider); err != nil { + log.Fatal(err) + } + } + + if flagModel != "" { + if err := cfg.SetModel(cfg.AI.Provider, flagModel); err != nil { + log.Fatal(err) + } + } + provider, err := ai.New(cfg) if err != nil { log.Fatal(err) @@ -44,5 +61,7 @@ var chatCmd = &cobra.Command{ } func init() { + chatCmd.Flags().StringVarP(&flagModel, "model", "m", "", "AI model to use (e.g. gpt-4o, claude-3-5-sonnet-latest, qwen2.5:8b)") + chatCmd.Flags().StringVarP(&flagProvider, "provider", "p", "", "AI provider to use (e.g. gemini, ollama, groq, openai, anthropic)") rootCmd.AddCommand(chatCmd) } diff --git a/cmd/set.go b/cmd/set.go index 936e09c..bebf232 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -32,7 +32,7 @@ var setCmd = &cobra.Command{ err = cfg.SetAPIKey(cfg.AI.Provider, value) case "base_url": - err = cfg.SetBaseURL(cfg.AI.Provider, value) + log.Fatal("base_url cannot be set or changed by the user for any provider") case "theme": cfg.SetTheme(value) diff --git a/internal/config/config.go b/internal/config/config.go index 178d77f..b5aeac9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -104,15 +104,7 @@ func (c *Config) SetAPIKey(providerName, apiKey string) error { // SetBaseURL updates a provider base URL. func (c *Config) SetBaseURL(providerName, baseURL string) error { - provider, ok := c.Providers[providerName] - if !ok { - return fmt.Errorf("unsupported provider: %s", providerName) - } - - provider.BaseURL = baseURL - c.Providers[providerName] = provider - - return nil + return fmt.Errorf("base_url cannot be set or changed by the user for any provider") } // SetTheme updates the UI theme. diff --git a/internal/providers/anthropic/provider.go b/internal/providers/anthropic/provider.go index 53a80c2..fba6ced 100644 --- a/internal/providers/anthropic/provider.go +++ b/internal/providers/anthropic/provider.go @@ -2,7 +2,6 @@ package anthropic import ( "context" - "strings" "github.com/Nithwin/WindMist/internal/ai" "github.com/Nithwin/WindMist/internal/config" @@ -20,13 +19,7 @@ type Provider struct { // New creates a new Anthropic provider instance. func New(cfg config.ProviderConfig) ai.Provider { - baseURL := strings.TrimRight(cfg.BaseURL, "/") - if baseURL == "" { - baseURL = "https://api.anthropic.com/v1" - } - if !strings.HasSuffix(baseURL, "/v1") { - baseURL = baseURL + "/v1" - } + baseURL := "https://api.anthropic.com/v1" model := cfg.Model if model == "" { diff --git a/internal/providers/groq/provider.go b/internal/providers/groq/provider.go index 88d00b3..e2be7e5 100644 --- a/internal/providers/groq/provider.go +++ b/internal/providers/groq/provider.go @@ -2,7 +2,6 @@ package groq import ( "context" - "strings" "github.com/Nithwin/WindMist/internal/ai" "github.com/Nithwin/WindMist/internal/config" @@ -20,13 +19,7 @@ type Provider struct { // New creates a new Groq provider instance. func New(cfg config.ProviderConfig) ai.Provider { - baseURL := strings.TrimRight(cfg.BaseURL, "/") - if baseURL == "" { - baseURL = "https://api.groq.com/openai/v1" - } - if !strings.HasSuffix(baseURL, "/v1") && !strings.HasSuffix(baseURL, "/openai/v1") { - baseURL = baseURL + "/openai/v1" - } + baseURL := "https://api.groq.com/openai/v1" model := cfg.Model if model == "" { diff --git a/internal/providers/ollama/provider.go b/internal/providers/ollama/provider.go index acb4ef9..6fedb36 100644 --- a/internal/providers/ollama/provider.go +++ b/internal/providers/ollama/provider.go @@ -2,7 +2,6 @@ package ollama import ( "context" - "strings" "github.com/Nithwin/WindMist/internal/ai" "github.com/Nithwin/WindMist/internal/config" @@ -20,13 +19,7 @@ type Provider struct { // New creates a new Ollama provider instance. func New(cfg config.ProviderConfig) ai.Provider { - baseURL := strings.TrimRight(cfg.BaseURL, "/") - if baseURL == "" { - baseURL = "http://localhost:11434" - } - if !strings.HasSuffix(baseURL, "/v1") { - baseURL = baseURL + "/v1" - } + baseURL := "http://localhost:11434/v1" model := cfg.Model if model == "" { diff --git a/internal/providers/openai/provider.go b/internal/providers/openai/provider.go index 42d64db..b83d525 100644 --- a/internal/providers/openai/provider.go +++ b/internal/providers/openai/provider.go @@ -2,7 +2,6 @@ package openai import ( "context" - "strings" "github.com/Nithwin/WindMist/internal/ai" "github.com/Nithwin/WindMist/internal/config" @@ -20,13 +19,7 @@ type Provider struct { // New creates a new OpenAI provider instance. func New(cfg config.ProviderConfig) ai.Provider { - baseURL := strings.TrimRight(cfg.BaseURL, "/") - if baseURL == "" { - baseURL = "https://api.openai.com/v1" - } - if !strings.HasSuffix(baseURL, "/v1") { - baseURL = baseURL + "/v1" - } + baseURL := "https://api.openai.com/v1" model := cfg.Model if model == "" {