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
11 changes: 10 additions & 1 deletion cmd/opencodereview/config_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,16 @@ func setConfigValue(cfg *Config, key, value string) error {
}
cfg.Effort = string(e)
case "llm.url", "llm.URL":
cfg.Llm.URL = value
// Same treatment as providers.<name>.url, so a scheme-less or padded
// value is refused here rather than surfacing as an opaque
// "unsupported protocol scheme" on the first LLM request.
trimmedURL := strings.TrimSpace(value)
if trimmedURL != "" {
if err := validateBaseURL(trimmedURL); err != nil {
return fmt.Errorf("invalid URL for %s: %w", key, err)
}
}
cfg.Llm.URL = trimmedURL
case "llm.auth_token", "llm.AuthToken":
cfg.Llm.AuthToken = value
case "llm.auth_token_cmd", "llm.AuthTokenCmd":
Expand Down
37 changes: 37 additions & 0 deletions cmd/opencodereview/config_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,43 @@ func TestSetConfigValueLlmURL(t *testing.T) {
}
}

func TestSetConfigValueLlmURLTrimsAndValidates(t *testing.T) {
t.Run("trims a valid URL before storing", func(t *testing.T) {
cfg := &Config{}

if err := setConfigValue(cfg, "llm.url", " https://gateway.internal:8000/v1 "); err != nil {
t.Fatalf("setConfigValue: %v", err)
}
if got := cfg.Llm.URL; got != "https://gateway.internal:8000/v1" {
t.Errorf("URL = %q, want trimmed URL", got)
}
})

t.Run("clears with an empty value", func(t *testing.T) {
cfg := &Config{}
cfg.Llm.URL = "https://old.example/v1"

if err := setConfigValue(cfg, "llm.url", ""); err != nil {
t.Fatalf("setConfigValue: %v", err)
}
if cfg.Llm.URL != "" {
t.Errorf("URL = %q, want empty to clear the key", cfg.Llm.URL)
}
})

for _, value := range []string{"api.example.com/v1", "ftp://gateway.internal/v1", "://bad"} {
t.Run("rejects "+value, func(t *testing.T) {
cfg := &Config{}
if err := setConfigValue(cfg, "llm.url", value); err == nil {
t.Fatalf("setConfigValue accepted invalid URL %q", value)
}
if cfg.Llm.URL != "" {
t.Errorf("URL = %q, want the rejected value left unset", cfg.Llm.URL)
}
})
}
}

func TestSetConfigValueLlmAuthToken(t *testing.T) {
cfg := &Config{}
if err := setConfigValue(cfg, "llm.auth_token", "tok-123"); err != nil {
Expand Down
8 changes: 7 additions & 1 deletion cmd/opencodereview/provider_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,16 @@ func applyManualConfig(configPath string, cfg *Config, result providerTUIResult)
if result.model == "" {
return fmt.Errorf("model is required for manual configuration")
}
// The other writer of llm.url (ocr config set) validates the same way, so
// the two cannot disagree about what a valid value is.
baseURL := strings.TrimSpace(result.url)
if err := validateBaseURL(baseURL); err != nil {
return fmt.Errorf("invalid URL for manual configuration: %w", err)
}

cfg.Provider = ""
cfg.Model = ""
cfg.Llm.URL = result.url
cfg.Llm.URL = baseURL
cfg.Llm.Model = result.model
cfg.Llm.AuthToken = result.apiKey
authHeader, err := llm.NormalizeAuthHeader(result.authHeader)
Expand Down
28 changes: 28 additions & 0 deletions cmd/opencodereview/provider_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,34 @@ func TestApplyManualConfig_MissingModel(t *testing.T) {
}
}

func TestApplyManualConfig_InvalidURL(t *testing.T) {
for _, value := range []string{"api.example.com/v1", "ftp://gateway.internal/v1"} {
t.Run(value, func(t *testing.T) {
cfg := &Config{Provider: "openai", Model: "saved-model"}
err := applyManualConfig(filepath.Join(t.TempDir(), "config.json"), cfg, providerTUIResult{url: value, model: "some-model"})
if err == nil {
t.Fatal("expected error for invalid URL")
}
if cfg.Provider != "openai" || cfg.Model != "saved-model" || cfg.Llm.URL != "" {
t.Fatalf("configuration changed after rejected write: %#v", cfg)
}
})
}
}

func TestApplyManualConfig_TrimsURL(t *testing.T) {
configPath := filepath.Join(t.TempDir(), "config.json")
cfg := &Config{}

result := providerTUIResult{url: " https://gateway.internal:8000/v1 ", model: "some-model", apiKey: "sk-test", protocol: "anthropic"}
if err := applyManualConfig(configPath, cfg, result); err != nil {
t.Fatalf("applyManualConfig: %v", err)
}
if cfg.Llm.URL != "https://gateway.internal:8000/v1" {
t.Errorf("llm.url = %q, want the trimmed URL", cfg.Llm.URL)
}
}

func TestApplyCustomProviderConfig_MissingProvider(t *testing.T) {
err := applyCustomProviderConfig("", &Config{}, providerTUIResult{provider: "", model: "m"})
if err == nil {
Expand Down