From ce63c28ed140db54c4098ded01375077a43e006e Mon Sep 17 00:00:00 2001 From: Cytown Date: Mon, 9 Mar 2026 19:29:17 +0800 Subject: [PATCH] fix local-model can not set as default issue --- cmd/picoclaw/internal/model/command.go | 13 ++++-------- cmd/picoclaw/internal/model/command_test.go | 22 ++------------------- 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/cmd/picoclaw/internal/model/command.go b/cmd/picoclaw/internal/model/command.go index 21b88e64e..a00ab5859 100644 --- a/cmd/picoclaw/internal/model/command.go +++ b/cmd/picoclaw/internal/model/command.go @@ -9,6 +9,8 @@ import ( "github.com/sipeed/picoclaw/pkg/config" ) +const LocalModel = "local-model" + func NewModelCommand() *cobra.Command { cmd := &cobra.Command{ Use: "model [model_name]", @@ -38,10 +40,6 @@ Examples: return nil } - if len(args) > 1 { - return fmt.Errorf("wrong command, should be model [model_name]") - } - // Set new default model modelName := args[0] return setDefaultModel(configPath, cfg, modelName) @@ -96,11 +94,8 @@ func setDefaultModel(configPath string, cfg *config.Config, modelName string) er } } - if !modelFound { - fmt.Printf("Error: Model '%s' not found in config.\n\n", modelName) - fmt.Println("Available models:") - listAvailableModels(cfg) - panic("") + if !modelFound && modelName != LocalModel { + return fmt.Errorf("Model '%s' not found in config.", modelName) } // Update the default model diff --git a/cmd/picoclaw/internal/model/command_test.go b/cmd/picoclaw/internal/model/command_test.go index 5c55eb99b..99c6ef2e7 100644 --- a/cmd/picoclaw/internal/model/command_test.go +++ b/cmd/picoclaw/internal/model/command_test.go @@ -267,23 +267,7 @@ func TestSetDefaultModel_InvalidModel(t *testing.T) { }, } - oldStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - - // This should call os.Exit(1), so we expect a panic`` - assert.Panics(t, func() { - _ = setDefaultModel(configPath, cfg, "nonexistent-model") - }) - - w.Close() - os.Stdout = oldStdout - - var buf bytes.Buffer - io.Copy(&buf, r) - output := buf.String() - assert.Contains(t, output, "Error: Model 'nonexistent-model' not found in config.") - assert.Contains(t, output, "Available models:") + assert.Error(t, setDefaultModel(configPath, cfg, "nonexistent-model")) } func TestSetDefaultModel_ModelWithoutAPIKey(t *testing.T) { @@ -301,9 +285,7 @@ func TestSetDefaultModel_ModelWithoutAPIKey(t *testing.T) { }, } - assert.Panics(t, func() { - setDefaultModel(configPath, cfg, "no-key-model") - }) + assert.Error(t, setDefaultModel(configPath, cfg, "no-key-model")) } func TestSetDefaultModel_SaveConfigError(t *testing.T) {