diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 327c30af5..0cfef0d9a 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -52,6 +52,7 @@ something else shows up as drift. - omp's `models.yml` keeps `modelOverrides`, `discovery`, `disableStrictTools`, headers and compat flags (`ompKept`). Its `auth` and `apiKey` are magpie's, or gone. - Kimi Code's `[providers.magpie]` has magpie's three keys set in place, so `custom_headers`, `env` and `default_model` stay (`kimiProvider`). - Cline's `models.json` keeps the provider's other keys, and on each model magpie lists what magpie doesn't write, such as a VS Code override (`clineKept`). + - VS Code's, Insiders' and VSCodium's group in `chatLanguageModels.json` (each profile's too) keeps every key magpie doesn't write, such as `settings[""].reasoningEffort`, which VS Code writes there when the user picks an effort in Copilot Chat's model picker (`setModelConfiguration`); a whole rewrite on each Sync and pick sent the model at its first level (chim3744 on Discord). A model's settings stay after it leaves the catalog; the model list is magpie's (`vscodeGroupKept`, `TestVSCodeKeepsThePickedEffort`). - Goose's custom provider file keeps a name, `timeout_seconds`, `toolshim`, headers and the like (`gooseProviderKept`). - ZCode's provider rule keeps its name, logo, visibility, API headers and the key's management link (`zcodeRules`). - Each model magpie writes into ZCode's `providerModelRules` has native web search (`properties.supportsNativeWebSearch`, "原生联网搜索") on while the provider's format is Anthropic Messages, the only format ZCode sends it on (`zcodeWebSearch`). The models magpie turned it on for are kept in the stash under `zcode.web_search`, so a model the user unticked in ZCode (which takes the key out of the rule) stays off, a value in the rule stays, a manual rule is left alone, and a stash that can't be read turns nothing on. On another format magpie's own on is taken out. Disconnect forgets the list. diff --git a/internal/agent/vscode.go b/internal/agent/vscode.go index ba4b91893..ab82f69d7 100644 --- a/internal/agent/vscode.go +++ b/internal/agent/vscode.go @@ -201,10 +201,13 @@ func vscodeKindAt(k vscodeKind, dir string) *Agent { } } } - if cur, ok := edit.GetJSONItem(lm, vscodeGroupOf(vendor)); ok && sameJSON(cur, v) { - continue + var want any = v + if cur, ok := edit.GetJSONItem(lm, vscodeGroupOf(vendor)); ok { + if want = vscodeGroupKept(cur, v); sameJSON(cur, want) { + continue + } } - if err := edit.SetJSONItem(lm, vscodeGroupOf(vendor), v); err != nil { + if err := edit.SetJSONItem(lm, vscodeGroupOf(vendor), want); err != nil { return err } } @@ -453,6 +456,24 @@ func vscodeGroupJSON(k vscodeKind, vendor string) map[string]any { return map[string]any{"name": magpieID, "vendor": vscodeEndpoint, "apiType": "chat-completions", "models": list} } +// vscodeGroupKept is magpie's group v laid over the one in the file, cur, +// with every key of cur magpie doesn't write kept: VS Code itself writes +// the reasoning effort picked for a model in Chat's model picker into the +// group, as settings[""].reasoningEffort (languageModels.ts +// setModelConfiguration), and a whole rewrite on each Sync and pick sent +// that model at its first level (chim3744 on Discord: max went out as +// low). The settings of a model magpie no longer lists stay too, as the +// user's. The model list is magpie's whole, so a model gone from the +// catalog leaves it. +func vscodeGroupKept(cur string, v map[string]any) any { + var have, want map[string]json.RawMessage + b, err := json.Marshal(v) + if err != nil || json.Unmarshal([]byte(cur), &have) != nil || have == nil || json.Unmarshal(b, &want) != nil || want == nil { + return v + } + return mergeTheirs(have, want, []string{"models"}) +} + // vscodeChatID is Copilot Chat's extension id. const vscodeChatID = "github.copilot-chat" diff --git a/internal/agent/vscode_test.go b/internal/agent/vscode_test.go index 8c62c3f58..ba24362b9 100644 --- a/internal/agent/vscode_test.go +++ b/internal/agent/vscode_test.go @@ -500,3 +500,83 @@ func TestVSCodeChatInstalledWithoutExtensionsJSON(t *testing.T) { t.Fatalf("unreadable: %+v", c) } } + +// the reasoning effort picked for a model in Copilot Chat's model picker, +// which VS Code writes into magpie's group as +// settings[""].reasoningEffort, stays through a Sync with nothing +// changed, a Sync after the catalog changed and a model picked in magpie +// (chim3744 on Discord: max went out as low, the first level, after each) +func TestVSCodeKeepsThePickedEffort(t *testing.T) { + home := syncHome(t) + dir := filepath.Join(home, "vscode", "User") + a := vscodeAt(dir) + writeFile(t, a.Path, "{}\n") + if err := a.Connect(); err != nil { + t.Fatal(err) + } + lm := filepath.Join(dir, "chatLanguageModels.json") + // what VS Code 1.142 writes on a pick (setModelConfiguration), with a + // model magpie no longer lists picked before + g, ok := edit.GetJSONItem(lm, vscodeGroup) + if !ok { + t.Fatalf("no group:\n%s", readFile(lm)) + } + var group map[string]any + if err := json.Unmarshal([]byte(g), &group); err != nil { + t.Fatal(err) + } + group["settings"] = map[string]any{ + "relay/glm-4.6": map[string]any{"reasoningEffort": "max"}, + "relay/gone": map[string]any{"reasoningEffort": "high"}, + } + if err := edit.SetJSONItem(lm, vscodeGroup, group); err != nil { + t.Fatal(err) + } + kept := func(when string) { + t.Helper() + g, _ := edit.GetJSONItem(lm, vscodeGroup) + if e := gjson.Get(g, `settings.relay/glm-4\.6.reasoningEffort`).String(); e != "max" { + t.Fatalf("%s: the picked effort is %q, want max:\n%s", when, e, g) + } + if e := gjson.Get(g, "settings.relay/gone.reasoningEffort").String(); e != "high" { + t.Fatalf("%s: a gone model's effort is %q, want high:\n%s", when, e, g) + } + } + + before := readFile(lm) + if err := a.Sync(); err != nil { + t.Fatal(err) + } + kept("a sync with nothing changed") + if readFile(lm) != before { + t.Fatalf("a sync with nothing changed rewrote the file:\n%s\n---\n%s", before, readFile(lm)) + } + + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"glm-4.6", "other"}}); err != nil { + t.Fatal(err) + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + kept("a sync after the catalog changed") + if g, _ := edit.GetJSONItem(lm, vscodeGroup); gjson.Get(g, "models.#").Int() != 2 { + t.Fatalf("not synced: %s", g) + } + + if err := a.Apply("model", "magpie/relay/other"); err != nil { + t.Fatal(err) + } + kept("a model picked in magpie") + + // a model gone from the catalog still leaves the list + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"glm-4.6"}}); err != nil { + t.Fatal(err) + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + kept("a sync after a model went") + if g, _ := edit.GetJSONItem(lm, vscodeGroup); gjson.Get(g, "models.#").Int() != 1 { + t.Fatalf("a gone model stayed: %s", g) + } +}