Files
magpie/internal/provider/copilot_ghe.go
T
yetone e4197144a8 perf: every package's package-level regexps compile on first use
v0.1.3516 made gateway's and redact's patterns lazy. The other 299
package-level regexp.MustCompile calls, in 148 files across agent,
provider, gui, library, codexcat, sessions, usage, plugin and the rest,
now go through lazyre.New too, so a launch that never reaches them
(`magpie statusline`, run by Claude Code on every message) doesn't
compile them. Fields, maps and params that held them are *lazyre.RE.

Measured on mote (M5 Pro), sandbox HOME, v0.1.3516-equivalent build
(t42) against this one (t43):
- GODEBUG=inittrace=1 sum: 7.39 MB / 69,837 allocs -> 4.62 MB / 46,765
- `magpie statusline`, 3 runs each: instructions retired ~212M -> ~176M,
  peak memory footprint 19.2-19.4 MB -> 16.8-17.0 MB

Run: go vet ./... (darwin) ok; GOOS=windows go vet on each of the 30
changed packages ok; GOOS=linux go vet ok except ., internal/cli and
internal/gui, which stop in wails v3's cgo files when cross-compiled
(the same at main). New TestAllLazyPatternsCompile at the root compiles
all 574 registered patterns (ok), so a bad pattern fails a test rather
than a first use.
Not run before push: the changed packages' own test suites; they run
on mote after it.
2026-11-10 12:54:39 +08:00

127 lines
3.8 KiB
Go

package provider
// Copilot on GitHub Enterprise Cloud with data residency (#723): an
// enterprise's accounts live at <name>.ghe.com, not github.com. They sign in
// with the same device flow and OAuth app at https://<name>.ghe.com, trade
// their token at https://api.<name>.ghe.com/copilot_internal/v2/token, are
// looked up at api.<name>.ghe.com, and are served at the API their session
// names (endpoints.api: https://copilot-api.<name>.ghe.com), as VS Code's
// Copilot Chat, copilot.lua and `copilot login --host` do. An account
// remembers its host; one on github.com has none, and every URL it uses is
// the one it always was.
import (
"errors"
"net/url"
"strings"
"github.com/yetone/magpie/internal/lazyre"
)
// gheName is an enterprise's subdomain of ghe.com: one DNS label.
var gheName = lazyre.New(`^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$`)
// CopilotHost is where a Copilot account signs in, from what a user typed
// ("acme.ghe.com", "https://acme.ghe.com/", "github.com"): "" for
// github.com, "<name>.ghe.com" for an enterprise on GHE.com. Nothing else
// is taken, so a GitHub token is never sent to a host of the user's choosing.
func CopilotHost(s string) (string, error) {
h := strings.ToLower(strings.TrimSpace(s))
if strings.Contains(h, "://") {
u, err := url.Parse(h)
if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.User != nil || u.Port() != "" || (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.Fragment != "" {
return "", errors.New("not a GitHub host: " + strings.TrimSpace(s))
}
h = u.Hostname()
}
h = strings.TrimSuffix(h, "/")
if h == "" || h == "github.com" || h == "www.github.com" {
return "", nil
}
if name, ok := strings.CutSuffix(h, ".ghe.com"); ok && gheName.MatchString(name) {
return h, nil
}
return "", errors.New("Copilot signs in on github.com or an enterprise's <name>.ghe.com, not " + strings.TrimSpace(s))
}
// copilotGHEOrigin is https://[sub.]host; a var so tests can point an
// enterprise's hosts elsewhere.
var copilotGHEOrigin = func(host, sub string) string {
if sub == "" {
return "https://" + host
}
return "https://" + sub + "." + host
}
// copilotHostOf is an account's host from the Copilot CLI's
// ("https://acme.ghe.com"); ok is false for one magpie can't serve.
func copilotHostOf(cli string) (string, bool) {
if cli == "" {
return "", true
}
h, err := CopilotHost(cli)
return h, err == nil
}
// The URLs an account uses: github.com's own (vars tests point elsewhere)
// with no host, its enterprise's otherwise.
func copilotDeviceURL(host string) string {
if host == "" {
return gitHubDeviceURL
}
return copilotGHEOrigin(host, "") + "/login/device/code"
}
func copilotOAuthURL(host string) string {
if host == "" {
return gitHubTokenURL
}
return copilotGHEOrigin(host, "") + "/login/oauth/access_token"
}
func copilotDevicePage(host string) string {
if host == "" {
return "https://github.com/login/device"
}
return copilotGHEOrigin(host, "") + "/login/device"
}
func copilotTokenURL(host string) string {
if host == "" {
return CopilotTokenURL
}
return copilotGHEOrigin(host, "api") + "/copilot_internal/v2/token"
}
func copilotUserURL(host string) string {
if host == "" {
return CopilotUserURL
}
return copilotGHEOrigin(host, "api") + "/copilot_internal/user"
}
func gitHubUserURL(host string) string {
if host == "" {
return GitHubUserURL
}
return copilotGHEOrigin(host, "api") + "/user"
}
// copilotBaseOf is the Copilot API an account is served at when its
// session names none.
func copilotBaseOf(host string) string {
if host == "" {
return copilotBase
}
return copilotGHEOrigin(host, "copilot-api")
}
// apiBase is where this session's requests go.
func (s copilotSession) apiBase(host string) string {
if s.Endpoints.API != "" {
return s.Endpoints.API
}
return copilotBaseOf(host)
}