Files
magpie/internal/agent/cursorlocal_env_other.go
T
yetone f77ef6106e agent: Cursor Private Inference is connected by its row's switch, as other agents are, with no command to run (mamba on Discord: 这个单独搞个启动命令和别的供应商格格不入啊) — its Open configuration's base URL and key are kept in the state.vscdb regular Cursor shares, and are regular Cursor's own override settings too, so magpie still writes nothing of Cursor's. It sets CURSOR_LOCAL_AGENT_BASE_URL (<gateway>/v1) and CURSOR_LOCAL_AGENT_API_KEY (magpie-cursor-local), which only this build reads, for the user: launchctl setenv on the Mac (apps opened from the Dock or Finder from then on have them), HKCU\Environment plus a WM_SETTINGCHANGE on Windows, ~/.config/environment.d/magpie-cursor-local.conf (from the next login) and systemctl --user set-environment on Linux; switched off, it clears them. magpie notes it in cursor-local.env in its config folder, which the row reads as connected, and sets them again whenever it serves the gateway (launchd forgets them at a restart; the port may have changed). The row has the provider field (magpie) and the connect switch; connecting says to quit the app and open it again, and that a base URL in Open configuration still comes first. The launch command stays as the square beside it, for a start from a shell. TestCursorLocalConnects (connect sets them, a serve sets them again, disconnect clears them, off a serve sets nothing) fails on the old code; cursor-local-launch.test.cjs now checks the switch connects it with no command copied (WebKit, en and zh); agy-launch passes. Not tried with the real app (not installed here), and launchctl/the registry are not touched under tests. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-05 00:18:22 +08:00

57 lines
1.5 KiB
Go

//go:build !darwin && !windows
package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/proc"
)
// cursorLocalEnvD is the environment.d file systemd's user session reads
// at login, the variables in it for every app the desktop starts.
func cursorLocalEnvD() string {
cfg := appdir.Getenv("XDG_CONFIG_HOME")
if cfg == "" {
home, _ := os.UserHomeDir()
cfg = filepath.Join(home, ".config")
}
return filepath.Join(cfg, "environment.d", "magpie-cursor-local.conf")
}
// setUserEnv writes the variables to environment.d, for the sessions from
// the next login on, and gives them to the user's systemd now (for apps it
// starts from now on, where it is there to); nil removes them.
func setUserEnv(env map[string]string) error {
p := cursorLocalEnvD()
if env == nil {
if err := os.Remove(p); err != nil && !os.IsNotExist(err) {
return err
}
if !testing.Testing() {
proc.Command("systemctl", append([]string{"--user", "unset-environment"}, cursorLocalVars...)...).Run()
}
return nil
}
var b strings.Builder
var kv []string
for _, k := range cursorLocalVars {
b.WriteString(k + "=" + env[k] + "\n")
kv = append(kv, k+"="+env[k])
}
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return err
}
if err := os.WriteFile(p, []byte(b.String()), 0o600); err != nil {
return err
}
if !testing.Testing() {
proc.Command("systemctl", append([]string{"--user", "set-environment"}, kv...)...).Run()
}
return nil
}