Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go/core/cli/cmd/kagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"
"os/signal"
"syscall"
"time"
Expand Down Expand Up @@ -64,6 +65,10 @@ func main() {
_ = installCmd.RegisterFlagCompletionFunc("profile", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return profiles.Profiles, cobra.ShellCompDirectiveNoFileComp
})
installCmd.Flags().StringVar(&installCfg.Provider, "provider", "", fmt.Sprintf("LLM provider to use (%s). Overrides KAGENT_DEFAULT_MODEL_PROVIDER.", strings.Join(cli.ValidProviders(), ", ")))
_ = installCmd.RegisterFlagCompletionFunc("provider", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return cli.ValidProviders(), cobra.ShellCompDirectiveNoFileComp
})

uninstallCmd := &cobra.Command{
Use: "uninstall",
Expand Down
23 changes: 23 additions & 0 deletions go/core/cli/internal/cli/agent/const.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -63,3 +64,25 @@ func GetEnvVarWithDefault(envVar, defaultValue string) string {
}
return defaultValue
}

// ValidProviders returns the accepted --provider flag values (helm key format).
func ValidProviders() []string {
return []string{
GetModelProviderHelmValuesKey(v1alpha2.ModelProviderOpenAI),
GetModelProviderHelmValuesKey(v1alpha2.ModelProviderAnthropic),
GetModelProviderHelmValuesKey(v1alpha2.ModelProviderAzureOpenAI),
GetModelProviderHelmValuesKey(v1alpha2.ModelProviderOllama),
}
}

// applyProviderFlag validates the --provider value and sets KAGENT_DEFAULT_MODEL_PROVIDER so
// that GetModelProvider() picks it up. This lets users avoid setting the env var manually.
func applyProviderFlag(provider string) error {
valid := ValidProviders()
for _, v := range valid {
if provider == v {
return os.Setenv(env.KagentDefaultModelProvider.Name(), provider)
Copy link
Copy Markdown
Contributor

@iplay88keys iplay88keys Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be setting the env var here. We are already using Viper for our CLI, so we should be able to utilize viper.MustBindEnv when we read in the config. Something like:

// Config struct - add provider
  type Config struct {
    KAgentURL    string        `mapstructure:"kagent_url"`
    Namespace    string        `mapstructure:"namespace"`
    OutputFormat string        `mapstructure:"output_format"`
    Verbose      bool          `mapstructure:"verbose"`
    Timeout      time.Duration `mapstructure:"timeout"`
    Provider     string        `mapstructure:"provider"`
  }

// Init() - add defaults and env bindings alongside existing ones
  viper.SetDefault("provider", "openAI")

  viper.MustBindEnv("provider", "KAGENT_DEFAULT_MODEL_PROVIDER")

}
}
return fmt.Errorf("unknown provider %q: valid values: %s", provider, strings.Join(valid, ", "))
}
24 changes: 20 additions & 4 deletions go/core/cli/internal/cli/agent/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
)

type InstallCfg struct {
Config *config.Config
Profile string
Config *config.Config
Profile string
Provider string
}

// installChart installs or upgrades a Helm chart with the given parameters
Expand Down Expand Up @@ -76,16 +77,28 @@ func InstallCmd(ctx context.Context, cfg *InstallCfg) *PortForward {
return nil
}

// --provider flag takes precedence over KAGENT_DEFAULT_MODEL_PROVIDER env var
if cfg.Provider != "" {
if err := applyProviderFlag(cfg.Provider); err != nil {
fmt.Fprintln(os.Stderr, err)
return nil
}
}

// get model provider from KAGENT_DEFAULT_MODEL_PROVIDER environment variable or use DefaultModelProvider
modelProvider := GetModelProvider()

// If model provider is openai, check if the API key is set
// Check if the required API key is set for this provider
apiKeyName := GetProviderAPIKey(modelProvider)
apiKeyValue := os.Getenv(apiKeyName)

if apiKeyName != "" && apiKeyValue == "" {
fmt.Fprintf(os.Stderr, "%s is not set\n", apiKeyName)
fmt.Fprintf(os.Stderr, "Please set the %s environment variable\n", apiKeyName)
if cfg.Provider == "" && modelProvider == DefaultModelProvider && apiKeyName == env.OpenAIAPIKey.Name() {
fmt.Fprintf(os.Stderr, "Tip: use --provider to select a different LLM provider (e.g. --provider anthropic)\n")
fmt.Fprintf(os.Stderr, " or set %s=%s before running install\n", env.KagentDefaultModelProvider.Name(), GetModelProviderHelmValuesKey(v1alpha2.ModelProviderAnthropic))
}
return nil
}

Expand Down Expand Up @@ -120,13 +133,16 @@ func InteractiveInstallCmd(ctx context.Context, c *ishell.Context) *PortForward
// get model provider from KAGENT_DEFAULT_MODEL_PROVIDER environment variable or use DefaultModelProvider
modelProvider := GetModelProvider()

// if model provider is openai, check if the api key is set
// Check if the required API key is set for this provider
apiKeyName := GetProviderAPIKey(modelProvider)
apiKeyValue := os.Getenv(apiKeyName)

if apiKeyName != "" && apiKeyValue == "" {
fmt.Fprintf(os.Stderr, "%s is not set\n", apiKeyName)
fmt.Fprintf(os.Stderr, "Please set the %s environment variable\n", apiKeyName)
fmt.Fprintf(os.Stderr, "Tip: set %s to select a different provider (e.g. %s=%s)\n",
env.KagentDefaultModelProvider.Name(), env.KagentDefaultModelProvider.Name(),
GetModelProviderHelmValuesKey(v1alpha2.ModelProviderAnthropic))
return nil
}

Expand Down