Files
magpie/internal/agent/copilotjetbrains.go
T
yetone bc320e249f agent: T3 Code, Air, OpenChamber and the rest send the LAN key to a gateway pinned beyond loopback (#1647, ggbdpq)
The gateway takes gateway.Token, and the keys that name an agent
(gateway.TokenFor), only from loopback. When MAGPIE_ADDR pins the
gateway to an address beyond loopback and sharing is on, keyAt gives the
LAN sharing key, and most writers use it. T3 Code's instance still wrote
gateway.Token, so every call it made got 401. Air's and OpenChamber's
Checks compared against gateway.Token, so they said the wiring Sync had
just written was off.

The same mistake was in every writer that pairs gateway.URL() or
gatewayV1() with a fixed token:
- agy, Alma, Cindy, Claude Desktop, Copilot for JetBrains, Cursor's
  local agent, OpenHanako, Pencil, VS Code / Insiders / VSCodium,
  WorkBuddy-family models.json, ZCode (both files), Zed's credential,
  Aside's provider, and the legacy Claude token rename.
- Fix: these now take agentKey(agent) or hereKey(), which are keyAt and
  agentKeyAt for gateway.URL(). On loopback they are unchanged.
- Alma re-keys a provider that holds any of magpie's keys other than the
  one it should have now.
- WorkBuddy and Aside also count the new key as theirs.
- ZCode treats a saved address equal to the pinned gateway as this
  machine's, not another magpie's.

What was run:
- New tests: TestT3CodeKeyBeyondLoopback and
  TestAirAndOpenChamberChecksTakeTheKeyForTheGateway (from the
  reporter's patch), and TestPinnedGatewayKeyForEveryWriter, which covers
  the other writers.
- With the product files reverted, all three are red. The T3 token reads
  "magpie", and each of the 11 writers shows its old key. They are green
  with the fix.
- go test -tags nogui ./internal/agent/ is green.
- gofmt -l and go vet are clean on darwin, linux and windows.

Not tried with a real T3 Code, Air, OpenChamber or a real pinned
MAGPIE_ADDR on a second machine.
2026-10-12 00:44:47 +08:00

253 lines
9.2 KiB
Go

package agent
// GitHub Copilot in JetBrains IDEs (IntelliJ IDEA, PyCharm, GoLand, …, and
// Android Studio; the github-copilot-intellij plugin) takes models of other
// vendors through Bring Your Own Key: Copilot Chat's model picker, Manage
// Models…, Add Custom Endpoint Provider (KamToHung on Discord). The plugin
// keeps none of it itself: it asks the Copilot language server it bundles
// (copilot-language-server, the npm @github/copilot-language-server), which
// keeps every provider in one JSON object, byok.json in
// $XDG_CONFIG_HOME/github-copilot when that is set (and absolute),
// %USERPROFILE%\AppData\Local\github-copilot on Windows and
// ~/.config/github-copilot elsewhere, read again at every use. A custom
// endpoint provider <name> is three keys there, as the server's own
// copilot/byok/saveCustomProviderConfig and saveModel write them:
//
// "<name>-provider-config": {"groupName": <name>, "apiType": "chatCompletions"}
// "<name>-api-key": "<key, in plain text>"
// "<name>-models-config": {"<model id>": {"deploymentUrl": …, "isRegistered": true,
// "isCustomModel": true, "modelCapabilities": {"name": …, "maxInputTokens": …,
// "maxOutputTokens": …, "toolCalling": true, "vision": …}}}
//
// So magpie adds the provider "magpie" there: its catalog, each model at the
// gateway's /v1/chat/completions, sent with the key as a Bearer token (the
// key is the file's, not the IDE's password safe). Tool calling is on, as
// Copilot's agent mode lists only models that call tools; a model's
// maxInputTokens is the window the server fills, its output kept within it.
// The user's other providers and keys stay as they are.
//
// The IDE lists the server's providers when Copilot signs in, so the models
// show after the IDE restarts. The model is picked in Copilot Chat, kept in
// the IDE's project files, so what magpie sets is whether its models are in
// that picker. BYOK needs a GitHub sign-in with Copilot: the server offers
// it to a personal plan (Free, Pro, Pro+) and to Business or Enterprise
// where the organisation's policy allows it. Its requests say
// "GithubCopilot/<server version>" as every client of that server does, so
// they are told apart by their key. Xcode's and Eclipse's Copilot run the
// same server and read the same file: they list magpie's models too.
//
// Checked with the language server 1.545.1 bundled in plugin 1.18.0, under
// a sandbox HOME signed in to a real Copilot account: the file magpie
// writes is listed by copilot/byok/listCustomProviderConfigs and
// listModels, and a Chat turn on one of its models reached the gateway with
// magpie's key. Not with a JetBrains IDE's own window.
import (
"os"
"path/filepath"
"runtime"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/edit"
)
// copilotJBID is the agent's id, and what its token names.
const copilotJBID = "copilot-jetbrains"
// The keys of magpie's provider in byok.json.
const (
copilotJBConfig = magpieID + "-provider-config"
copilotJBKey = magpieID + "-api-key"
copilotJBModels = magpieID + "-models-config"
)
// copilotJBPlugin is the folder the plugin is installed in, under an IDE's
// plugins folder.
const copilotJBPlugin = "github-copilot-intellij"
func copilotJetBrains(home string) *Agent {
return copilotJetBrainsAt(copilotBYOKPath(home), func() bool { return copilotJBInstalled(home) })
}
// copilotBYOKPath is byok.json where the language server keeps it
// (getXdgConfigPath).
func copilotBYOKPath(home string) string {
if x := appdir.Getenv("XDG_CONFIG_HOME"); x != "" && filepath.IsAbs(x) {
return filepath.Join(x, "github-copilot", "byok.json")
}
if runtime.GOOS == "windows" {
// the server's os.homedir(), USERPROFILE, which home is too
return filepath.Join(home, "AppData", "Local", "github-copilot", "byok.json")
}
return filepath.Join(home, ".config", "github-copilot", "byok.json")
}
// copilotJBInstalled reports whether some JetBrains IDE, or Android Studio,
// has the Copilot plugin installed: in the plugins folder of an IDE's
// config folder on macOS and Windows, in the IDE's data folder itself on
// Linux.
func copilotJBInstalled(home string) bool {
var base, sub string
switch runtime.GOOS {
case "darwin":
base, sub = filepath.Join(home, "Library", "Application Support"), "plugins"
case "windows":
if base = appdir.Getenv("APPDATA"); base == "" {
base = filepath.Join(home, "AppData", "Roaming")
}
sub = "plugins"
default:
if base = appdir.Getenv("XDG_DATA_HOME"); base == "" {
base = filepath.Join(home, ".local", "share")
}
}
for _, vendor := range []string{"JetBrains", "Google"} {
ms, _ := filepath.Glob(filepath.Join(base, vendor, "*", sub, copilotJBPlugin))
for _, m := range ms {
if isDir(m) {
return true
}
}
}
return false
}
func copilotJetBrainsAt(path string, installed func() bool) *Agent {
wired := func() bool { _, ok := edit.GetJSON(path, copilotJBConfig); return ok }
return atomic(&Agent{
ID: copilotJBID, Name: "Copilot (JetBrains)", Icon: "githubcopilot",
Aliases: []string{"jetbrains", "jetbrains-copilot", "copilot-idea", "copilot-intellij", "intellij", "idea"},
Dir: filepath.Dir(path), Path: path,
// the folder is every Copilot's (VS Code's sign-in too): the
// plugin installed in an IDE, or magpie's provider written, says
// it is here
detect: func() bool { return installed() || wired() },
Notice: func() string {
if !wired() {
return ""
}
return noticeCopilotjetbrains.String()
},
Sync: func() error {
if !wired() {
return nil
}
return copilotJBWrite(path)
},
Check: func() string {
if !wired() {
return ""
}
// every model at the gateway: the first that isn't, if any (none
// listed while the catalog is empty)
raw, _ := edit.GetJSON(path, copilotJBModels)
url := vscodeURL()
for _, m := range gjson.Parse(raw).Map() {
if url = m.Get("deploymentUrl").String(); url != vscodeURL() {
break
}
}
return wiringOff("Copilot (JetBrains)", path, func(k string) (string, bool) {
if k == "deploymentUrl" {
return url, url != ""
}
return edit.GetJSON(path, k)
}, "deploymentUrl", vscodeURL(), copilotJBKey, agentKey(copilotJBID))
},
// its picker lists the models picked for it (#1508)
ListsModels: true,
Fields: []Field{{
Key: "provider", Label: "provider",
Get: func() string {
if wired() {
return magpieID
}
return ""
},
Set: func(v string) error {
if v == "" {
return edit.DelJSON(path, copilotJBConfig, copilotJBKey, copilotJBModels)
}
return copilotJBWrite(path)
},
Options: func(map[string]string) []Option {
return []Option{{Value: magpieID, Label: "magpie", Icon: "magpie", Note: "every magpie model in Copilot Chat's model picker"}}
},
}},
}, path)
}
// copilotJBWrite writes magpie's provider into byok.json, made private as
// the language server makes it when there is none. A model of magpie's the
// user hid in the IDE's Manage Models stays hidden.
func copilotJBWrite(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
if err := os.WriteFile(path, []byte("{}\n"), 0o600); err != nil {
return err
}
}
hidden := map[string]bool{}
if raw, ok := edit.GetJSON(path, copilotJBModels); ok {
for id, m := range gjson.Parse(raw).Map() {
if r := m.Get("isRegistered"); r.Exists() && !r.Bool() {
hidden[id] = true
}
}
}
models := copilotJBModelsJSON(hidden)
kvs := []edit.KV{
{Path: copilotJBConfig, Value: map[string]string{"groupName": magpieID, "apiType": "chatCompletions"}},
{Path: copilotJBKey, Value: agentKey(copilotJBID)},
{Path: copilotJBModels, Value: models},
}
// written only where it differs, so a sync with nothing new leaves the
// file as the server last wrote it
var set []edit.KV
for _, kv := range kvs {
if cur, ok := edit.GetJSON(path, kv.Path); !ok || !sameJSON(cur, kv.Value) {
set = append(set, kv)
}
}
if len(set) == 0 {
return nil
}
return edit.SetJSON(path, set...)
}
// copilotJBModelsJSON is magpie's models-config: the catalog as Copilot
// (JetBrains) is shown it, keyed by model id.
func copilotJBModelsJSON(hidden map[string]bool) map[string]any {
out := map[string]any{}
for _, m := range magpieModels(copilotJBID) {
context := m.Context
if context == 0 {
context = 128000 // the server's own default for a custom endpoint
}
// the output is reserved out of the window the prompt fills
output := maxTokens(m)
if output <= 0 {
output = 16000
}
output = min(output, context/4)
out[m.ID] = map[string]any{
"deploymentUrl": vscodeURL(),
"isRegistered": !hidden[m.ID],
"isCustomModel": true,
"modelCapabilities": map[string]any{
"name": m.Name, "maxInputTokens": context, "maxOutputTokens": output,
"toolCalling": true, "vision": m.Images,
},
}
}
return out
}
// what copilotjetbrains says after a change (notice.go)
var (
noticeCopilotjetbrains = newNotice("magpie's models are in Copilot Chat's model picker in your JetBrains IDE, under magpie, once the IDE restarts. They need Copilot signed in to GitHub on a plan with Bring Your Own Key: Copilot Free, Pro and Pro+ have it, Business and Enterprise when the organisation's policy allows it.")
)