VS Code Stable takes other vendors' models through its Custom Endpoint provider since 1.122 (customoai, the older OpenAI Compatible one, is Insiders only), signed in to GitHub or not. The provider groups live in chatLanguageModels.json beside settings.json in the default profile's User folder (~/Library/Application Support/Code/User, ~/.config/Code/User, %APPDATA%\Code\User), a JSONC array VS Code watches. magpie adds {"vendor":"customendpoint","name":"magpie","apiType":"chat-completions","models":[...]} with each catalog model at the gateway's /v1/chat/completions, toolCalling true (agent mode lists only tool-calling models), vision, contextWindow and maxOutputTokens (capped at a quarter of the window, as VS Code keeps the output budget out of the prompt's), and the model's efforts as supportsReasoningEffort. A group's apiKey is a secret property that VS Code reads from its secret storage only (languageModels.ts _resolveConfiguration), so it can't be written to the file; each model instead sends Authorization: Bearer magpie in requestHeaders, which CustomEndpointOAIEndpoint lets through and which stops it sending its own key header. The model a new chat starts on is chat.defaultModel in settings.json (matched by model id, so the catalog id); its old value is stashed and put back. The group is the agent's Joined: picking one of VS Code's own (auto) keeps magpie's models listed, and switching off (Unwire) takes the group out and puts chat.defaultModel back. Sync rewrites the group as the catalog changes, Check says when its url or header no longer reach the gateway, the Notice says where the models are and to reload the window if they don't show, and requests are credited to it by GitHubCopilotChat/<version>, a User-Agent a model's headers can't change.
internal/edit: SetJSONItem/GetJSONItem/DelJSONItem edit one element of a JSONC file's top-level array, found by its members, keeping comments, trailing commas and the other elements; SetJSON/DelJSON take a backslash-escaped dot as part of a key, as gjson does, for VS Code's flat "chat.defaultModel".
TestVSCode (connect keeps the user's settings and groups with their comments, the group's url/header/toolCalling/window and no apiKey or discovery url, Sync, Check, an own model keeping the group, disconnect back to the user's last own model), TestVSCodeRoundTrip (on and off leaves both files byte for byte; no files at all) and TestVSCodeUA; TestJSONItem, TestDelJSONItemAmongOthers and TestJSONEscapedDot. go vet ./internal/..., go build -tags nogui ./..., GOOS=windows and GOOS=linux builds pass; go test of internal/agent, edit, usage and gateway (each sandboxes HOME in TestMain) and internal/gui's page and agent connect tests pass; the full go test under a temp HOME was not run (the sandbox refused the HOME override). No GUI code changed (the generic connected wording fits), so no webkit test. VS Code itself is not installed here: not tried against a real VS Code.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
5.6 KiB
Go
174 lines
5.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/jsonc"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// VS Code's chat: switched on, magpie's group joins the user's in
|
|
// chatLanguageModels.json, its token in each model's Authorization header
|
|
// and no apiKey (VS Code reads that from its secret storage only), and a new
|
|
// chat starts on the model picked; switched off, both files are as they were
|
|
func TestVSCode(t *testing.T) {
|
|
home := syncHome(t)
|
|
dir := filepath.Join(home, "vscode", "User")
|
|
a := vscodeAt(dir)
|
|
settings := `{
|
|
// my editor
|
|
"editor.fontSize": 14,
|
|
"chat.defaultModel": "gpt-5",
|
|
"chat.agent.enabled": true,
|
|
}
|
|
`
|
|
groups := `// models I added
|
|
[
|
|
{
|
|
"name": "Ollama",
|
|
"vendor": "ollama", // local
|
|
"url": "http://localhost:11434"
|
|
},
|
|
]
|
|
`
|
|
writeFile(t, a.Path, settings)
|
|
lm := filepath.Join(dir, "chatLanguageModels.json")
|
|
writeFile(t, lm, groups)
|
|
f := a.Field("model")
|
|
if f.Get() != "gpt-5" || !a.Detected() || a.Wired() {
|
|
t.Fatalf("before: %q %v", f.Get(), a.Wired())
|
|
}
|
|
if err := a.Connect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f.Get() != "magpie/relay/glm-4.6" || !a.Wired() || a.Check() != "" {
|
|
t.Fatalf("connected: %q %v %q", f.Get(), a.Wired(), a.Check())
|
|
}
|
|
if v, _ := edit.GetJSON(a.Path, vscodeDefault); v != "relay/glm-4.6" {
|
|
t.Fatalf("chat.defaultModel %q\n%s", v, readFile(a.Path))
|
|
}
|
|
for _, keep := range []string{"// my editor", `"editor.fontSize": 14`, `"chat.agent.enabled": true`} {
|
|
if !strings.Contains(readFile(a.Path), keep) {
|
|
t.Fatalf("settings lost %q:\n%s", keep, readFile(a.Path))
|
|
}
|
|
}
|
|
var list []map[string]any
|
|
if err := json.Unmarshal(jsonc.ToJSON([]byte(readFile(lm))), &list); err != nil || len(list) != 2 || list[0]["vendor"] != "ollama" {
|
|
t.Fatalf("groups: %v %v\n%s", err, list, readFile(lm))
|
|
}
|
|
for _, keep := range []string{"// models I added", "// local"} {
|
|
if !strings.Contains(readFile(lm), keep) {
|
|
t.Fatalf("groups lost %q:\n%s", keep, readFile(lm))
|
|
}
|
|
}
|
|
g, _ := edit.GetJSONItem(lm, vscodeGroup)
|
|
for k, want := range map[string]string{
|
|
"vendor": "customendpoint", "name": "magpie", "apiType": "chat-completions",
|
|
"models.0.id": "relay/glm-4.6", "models.0.url": gatewayV1() + "/chat/completions",
|
|
"models.0.toolCalling": "true", "models.0.contextWindow": "204800",
|
|
"models.0.requestHeaders.Authorization": "Bearer magpie",
|
|
} {
|
|
if got := gjson.Get(g, k).String(); got != want {
|
|
t.Errorf("%s = %q, want %q", k, got, want)
|
|
}
|
|
}
|
|
if gjson.Get(g, "apiKey").Exists() || gjson.Get(g, "models.0.apiKey").Exists() || gjson.Get(g, "url").Exists() {
|
|
t.Fatalf("a key or a discovery url: %s", g)
|
|
}
|
|
if n := gjson.Get(g, "models.0.maxOutputTokens").Int(); n <= 0 || n > 204800/4 {
|
|
t.Fatalf("output %d", n)
|
|
}
|
|
|
|
// a model added to the catalog is listed once synced
|
|
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)
|
|
}
|
|
if g, _ := edit.GetJSONItem(lm, vscodeGroup); gjson.Get(g, "models.#").Int() != 2 {
|
|
t.Fatalf("not synced: %s", g)
|
|
}
|
|
|
|
// the gateway's URL changed under it: Check says so
|
|
b := readFile(lm)
|
|
writeFile(t, lm, strings.ReplaceAll(b, gatewayV1(), "http://127.0.0.1:9/v1"))
|
|
if c := a.Check(); !strings.Contains(c, "chatLanguageModels.json") {
|
|
t.Fatalf("check: %q", c)
|
|
}
|
|
writeFile(t, lm, b)
|
|
|
|
// one of VS Code's own picked: magpie's models stay in its list
|
|
if err := a.Apply("model", "auto"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f.Get() != "auto" || !a.Wired() {
|
|
t.Fatalf("own: %q %v", f.Get(), a.Wired())
|
|
}
|
|
if err := a.Apply("model", "magpie/relay/other"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// switched off: back to auto, the last of the user's own, group gone
|
|
if err := a.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v, _ := edit.GetJSON(a.Path, vscodeDefault); v != "auto" || a.Wired() {
|
|
t.Fatalf("disconnected: %q %v", v, a.Wired())
|
|
}
|
|
if _, ok := edit.GetJSONItem(lm, vscodeGroup); ok {
|
|
t.Fatalf("group left:\n%s", readFile(lm))
|
|
}
|
|
}
|
|
|
|
// on and off again with nothing else changed: both files byte for byte
|
|
func TestVSCodeRoundTrip(t *testing.T) {
|
|
home := syncHome(t)
|
|
dir := filepath.Join(home, "vscode", "User")
|
|
a := vscodeAt(dir)
|
|
settings := "{\n\t// mine\n\t\"chat.defaultModel\": \"claude-sonnet-4.5\",\n\t\"files.autoSave\": \"afterDelay\"\n}\n"
|
|
groups := "[\n\t{\n\t\t\"name\": \"Ollama\",\n\t\t\"vendor\": \"ollama\"\n\t}\n]\n"
|
|
lm := filepath.Join(dir, "chatLanguageModels.json")
|
|
writeFile(t, a.Path, settings)
|
|
writeFile(t, lm, groups)
|
|
if err := a.Connect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if readFile(a.Path) != settings || readFile(lm) != groups {
|
|
t.Fatalf("not as it was:\n%s\n%s", readFile(a.Path), readFile(lm))
|
|
}
|
|
|
|
// no files at all: made, and emptied of magpie again
|
|
home = syncHome(t)
|
|
dir = filepath.Join(home, "fresh", "User")
|
|
a = vscodeAt(dir)
|
|
lm = filepath.Join(dir, "chatLanguageModels.json")
|
|
if err := a.Connect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.Field("model").Get() != "magpie/relay/glm-4.6" || a.Check() != "" {
|
|
t.Fatalf("fresh: %q\n%s", a.Field("model").Get(), readFile(lm))
|
|
}
|
|
if err := a.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v, ok := edit.GetJSON(a.Path, vscodeDefault); ok || a.Wired() {
|
|
t.Fatalf("fresh disconnected: %q\n%s", v, readFile(a.Path))
|
|
}
|
|
}
|
|
|
|
// its requests come with GitHubCopilotChat/<version>
|
|
func TestVSCodeUA(t *testing.T) {
|
|
if got := usage.AgentOf("GitHubCopilotChat/0.69.0"); got != "vscode" {
|
|
t.Fatalf("%q", got)
|
|
}
|
|
}
|