This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
fix/openclaw config merge #53
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,7 +129,7 @@ func writeOpenClawViaCLISequential(providerBlock map[string]any, modelsCatalog m | |
| providerName + "/" + reg.Coding().Slug, | ||
| providerName + "/" + reg.Sub().Slug, | ||
| }) | ||
| cmd = exec.Command("openclaw", "config", "set", "agents.defaults.model.fallbacks", string(fallbacksJSON)) | ||
| cmd = exec.Command("openclaw", "config", "set", "--merge", "agents.defaults.model.fallbacks", string(fallbacksJSON)) | ||
| if out, err := cmd.CombinedOutput(); err != nil { | ||
| return fmt.Errorf("openclaw config set agents.defaults.model.fallbacks: %s: %w", string(out), err) | ||
| } | ||
|
|
@@ -139,7 +139,7 @@ func writeOpenClawViaCLISequential(providerBlock map[string]any, modelsCatalog m | |
| if err != nil { | ||
| return fmt.Errorf("marshal models catalog: %w", err) | ||
| } | ||
| cmd = exec.Command("openclaw", "config", "set", "agents.defaults.models", string(modelsJSON)) | ||
| cmd = exec.Command("openclaw", "config", "set", "--merge", "agents.defaults.models", string(modelsJSON)) | ||
| if out, err := cmd.CombinedOutput(); err != nil { | ||
| return fmt.Errorf("openclaw config set agents.defaults.models: %s: %w", string(out), err) | ||
| } | ||
|
|
@@ -177,15 +177,10 @@ func writeOpenClawViaCLI(apiKey string, reg *models.Registry) error { | |
| // Check OpenClaw version to determine which approach to use | ||
| version := getOpenClawVersion() | ||
| if isBatchJSONSupported(version) { | ||
| // Use --batch-json to set all config in a single CLI call (~3s vs ~12s sequential). | ||
| // Use --batch-json for the fields that don't conflict with existing user config. | ||
| batchOps := []map[string]any{ | ||
| {"path": "models.providers.kimchi", "value": providerBlock}, | ||
| {"path": "agents.defaults.model.primary", "value": providerName + "/" + reg.Main().Slug}, | ||
| {"path": "agents.defaults.model.fallbacks", "value": []string{ | ||
| providerName + "/" + reg.Coding().Slug, | ||
| providerName + "/" + reg.Sub().Slug, | ||
| }}, | ||
| {"path": "agents.defaults.models", "value": modelsCatalog}, | ||
| } | ||
|
|
||
| batchJSON, err := json.Marshal(batchOps) | ||
|
|
@@ -197,6 +192,26 @@ func writeOpenClawViaCLI(apiKey string, reg *models.Registry) error { | |
| if out, err := cmd.CombinedOutput(); err != nil { | ||
| return fmt.Errorf("openclaw config set: %s: %w", string(out), err) | ||
| } | ||
|
|
||
| // Merge fallbacks so existing entries are preserved. | ||
| fallbacksJSON, _ := json.Marshal([]string{ | ||
| providerName + "/" + reg.Coding().Slug, | ||
| providerName + "/" + reg.Sub().Slug, | ||
| }) | ||
| cmd = exec.Command("openclaw", "config", "set", "--merge", "agents.defaults.model.fallbacks", string(fallbacksJSON)) | ||
| if out, err := cmd.CombinedOutput(); err != nil { | ||
| return fmt.Errorf("openclaw config set --merge agents.defaults.model.fallbacks: %s: %w", string(out), err) | ||
| } | ||
|
|
||
| // Merge models catalog so existing entries are preserved. | ||
| modelsJSON, err := json.Marshal(modelsCatalog) | ||
| if err != nil { | ||
| return fmt.Errorf("marshal models catalog: %w", err) | ||
| } | ||
| cmd = exec.Command("openclaw", "config", "set", "--merge", "agents.defaults.models", string(modelsJSON)) | ||
| if out, err := cmd.CombinedOutput(); err != nil { | ||
| return fmt.Errorf("openclaw config set --merge agents.defaults.models: %s: %w", string(out), err) | ||
| } | ||
| } else { | ||
| // Fall back to sequential calls for older versions | ||
| if err := writeOpenClawViaCLISequential(providerBlock, modelsCatalog, reg); err != nil { | ||
|
|
@@ -310,13 +325,25 @@ func writeOpenClawDirect(scope config.ConfigScope, apiKey string, reg *models.Re | |
| if defaults == nil { | ||
| defaults = make(map[string]any) | ||
| } | ||
| defaults["model"] = map[string]any{ | ||
| "primary": providerName + "/" + reg.Main().Slug, | ||
| "fallbacks": []any{providerName + "/" + reg.Coding().Slug, providerName + "/" + reg.Sub().Slug}, | ||
| // Merge into model config to preserve existing keys like temperature, max_tokens. | ||
| modelMap, _ := defaults["model"].(map[string]any) | ||
| if modelMap == nil { | ||
| modelMap = make(map[string]any) | ||
| } | ||
| modelMap["primary"] = providerName + "/" + reg.Main().Slug | ||
| // Merge fallbacks so existing entries are preserved. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ℹ️🔧 Maintainability The type assertion 💡 Suggestion: Consider logging a warning if the type assertion fails, or use reflection to handle type conversion safely if the source data format is uncertain. |
||
| existingFallbacks, _ := modelMap["fallbacks"].([]any) | ||
| modelMap["fallbacks"] = append(existingFallbacks, | ||
| providerName+"/"+reg.Coding().Slug, | ||
| providerName+"/"+reg.Sub().Slug, | ||
| ) | ||
| defaults["model"] = modelMap | ||
|
|
||
| // Merge into the allowed models catalog, preserving existing entries. | ||
| modelsCatalog, _ := defaults["models"].(map[string]any) | ||
| if modelsCatalog == nil { | ||
| modelsCatalog = make(map[string]any) | ||
| } | ||
|
|
||
| // Add models to the allowed models catalog. | ||
| modelsCatalog := make(map[string]any) | ||
| for _, m := range reg.All() { | ||
| modelsCatalog[providerName+"/"+m.Slug] = map[string]any{"alias": m.DisplayName} | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error return from
json.Marshalis discarded when marshalingfallbacksJSON. While unlikely to fail for a simple string slice, if it does return an error,fallbacksJSONcould benilandstring(nil)would result in an empty string being passed to theopenclaw config setcommand, potentially clearing the user's fallback configuration instead of merging.💡 Suggestion: Check the error from
json.Marshal:fallbacksJSON, err := json.Marshal(...); if err != nil { return fmt.Errorf("marshal fallbacks: %w", err) }