* Add German translation and harden paths against unset env vars Add a German locale to the GUI, tray menu, usage alerts and website home page, and refuse to start when the home folder is unset or relative so nothing is written under the working folder; the pi plugin no longer adopts the user's PI_CODING_AGENT_DIR, and an inherited APPIMAGE no longer selects portable mode. Scripts refuse to run from the wrong folder or empty a shared out folder, and a new internal/testenv sandboxes every test package from the user's real files, which also fixes the Claude picker and Termux installer tests on Windows. * Makefile: know magpie's folder by its go.mod, so paths with spaces work The check compared $(CURDIR) with $(dir $(abspath …)), which make splits on spaces, so a checkout under 'My Projects' or 'John Doe' refused every target. It now reads go.mod relative to the working folder and wants magpie's module there: make -C and a symlinked Makefile work, make -f from another folder still stops before any recipe runs. * i18n: German for the strings main added since, with a parity test 75 strings main added (group order, patterns, slow-start wait, usage caps, RTK savings …) get their German, and two that main dropped go. gui-de.test.cjs checks, as gui-ja does, that de has every zh string with its placeholders and tags, and that the window reads in German when picked or on a German system. * appdir: ignore relative folder variables when reading, keep them for children CheckEnv no longer unsets anything. magpie reads the XDG folders, APPDATA/LOCALAPPDATA and every agent's folder variable through appdir.Getenv/LookupEnv, which pass over a relative value; the environment stays as it is, so the agents magpie starts get the variable as the user set it. That covers what proc.UserPath lends from the login shell too, which is set after main's check and reached the desktop app unchecked. On Windows a path rooted on the current drive (\Users\x, Git Bash's /c/x) counts as usable, and magpie version and help answer without a home, before run makes any folder. * Sandbox the tests main added since internal/wslrun gets testenv's TestMain, and four tests main added that set HOME alone set their home with testenv.SetHome, so on Windows they don't read the runner's profile through USERPROFILE. * agentenv: PI_CONFIG_DIR and OPENCODE_DB take relative names on purpose omp puts $PI_CONFIG_DIR under the home and OpenCode a relative $OPENCODE_DB in its data folder, so neither depends on the working folder: appdir no longer passes them over. * i18n: German for the Library's multi-select and System One's decision API The 19 strings main added since (#791's skill multi-select, the decision API snippets) get their German; the one main replaced (four APIs → five APIs) goes. * appdir: tidy the import into magpie's group; say what Go makes of /c/x The appdir import joins the group of magpie's own imports where a file has one. rooted's comment now says that Go takes a Git Bash path like /c/x as \c\x on the current drive, not C:\x.
178 lines
5.8 KiB
Go
178 lines
5.8 KiB
Go
package provider
|
|
|
|
// PLUGIN-SERVED (see AGENTS.md): Cursor ("cursor") is a deprecated built-in
|
|
// subscription served by its plugin, @magpie-community/opencode-cursor-auth,
|
|
// once moved onto it (provider.Moved; the default for a new sign-in). A
|
|
// moved one's sign-ins, models, requests and usage are all the plugin's,
|
|
// never this code's (only the move, in migrate*.go, still reads its
|
|
// accounts). A fix here alone doesn't reach those users; fix the plugin
|
|
// (github.com/magpie-community/plugins, packages/cursor) and raise the
|
|
// mover's min in internal/provider/migrate_side.go.
|
|
|
|
// How much of a Cursor plan's included usage is gone, as the CLI's own
|
|
// usage view reads it: the dashboard's current period, split into the
|
|
// Cursor Models and Other Models pools.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
)
|
|
|
|
// cursorBase is Cursor's API; a var so tests can point it elsewhere.
|
|
var cursorBase = "https://api2.cursor.sh"
|
|
|
|
// cursorKeychain reads cursor-agent's token from the macOS Keychain; a var
|
|
// so tests stay off the real one.
|
|
var cursorKeychain = runtime.GOOS == "darwin"
|
|
|
|
// cursorToken is the access token cursor-agent signed in with: in the
|
|
// Keychain on a Mac, in its auth.json elsewhere.
|
|
func cursorToken() (string, error) {
|
|
if cursorKeychain {
|
|
if tok := cursorKeychainToken(false); tok != "" {
|
|
return tok, nil
|
|
}
|
|
}
|
|
var auth struct {
|
|
AccessToken string `json:"accessToken"`
|
|
}
|
|
if path := cursorAuthPath(); path != "" && readJSON(path, &auth) && auth.AccessToken != "" {
|
|
return auth.AccessToken, nil
|
|
}
|
|
return "", errorf("cursor-agent is not signed in")
|
|
}
|
|
|
|
// cursorAuthPath is where cursor-agent keeps its sign-in outside the Keychain.
|
|
func cursorAuthPath() string {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
dir := appdir.Getenv("APPDATA")
|
|
if dir == "" {
|
|
dir = filepath.Join(home, "AppData", "Roaming")
|
|
}
|
|
return filepath.Join(dir, "Cursor", "auth.json")
|
|
case "darwin":
|
|
return filepath.Join(home, ".cursor", "auth.json")
|
|
}
|
|
dir := appdir.Getenv("XDG_CONFIG_HOME")
|
|
if dir == "" {
|
|
dir = filepath.Join(home, ".config")
|
|
}
|
|
return filepath.Join(dir, "cursor", "auth.json")
|
|
}
|
|
|
|
func cursorSubscriptionUsage(ctx context.Context, plan string) SubscriptionQuota {
|
|
q := SubscriptionQuota{Provider: "cursor", Name: "Cursor", Icon: "cursor", Plan: plan, Windows: []QuotaWindow{}}
|
|
tok, err := cursorToken()
|
|
if err == nil {
|
|
q.Windows, err = cursorWindows(ctx, tok)
|
|
}
|
|
if err != nil {
|
|
q.Error = err.Error()
|
|
}
|
|
return q
|
|
}
|
|
|
|
// cursorWindows: the plan's included usage this billing period. An
|
|
// enterprise plan reports spend instead, and gets no windows.
|
|
func cursorWindows(ctx context.Context, token string) ([]QuotaWindow, error) {
|
|
var data struct {
|
|
BillingCycleEnd string `json:"billingCycleEnd"` // epoch millis
|
|
AutoBucketModels []string `json:"autoBucketModels"`
|
|
PlanUsage *struct {
|
|
Auto float64 `json:"autoPercentUsed"`
|
|
API float64 `json:"apiPercentUsed"`
|
|
Total float64 `json:"totalPercentUsed"`
|
|
} `json:"planUsage"`
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, cursorBase+"/aiserver.v1.DashboardService/GetCurrentPeriodUsage", bytes.NewReader([]byte("{}")))
|
|
if err != nil {
|
|
return []QuotaWindow{}, err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Connect-Protocol-Version", "1")
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return []QuotaWindow{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
|
|
if res.StatusCode < 200 || res.StatusCode >= 300 {
|
|
return []QuotaWindow{}, &accountStatusError{status: res.StatusCode}
|
|
}
|
|
if err := json.Unmarshal(b, &data); err != nil {
|
|
return []QuotaWindow{}, err
|
|
}
|
|
if data.PlanUsage == nil {
|
|
return []QuotaWindow{}, nil
|
|
}
|
|
var resets *time.Time
|
|
if ms, err := strconv.ParseInt(data.BillingCycleEnd, 10, 64); err == nil && ms > 0 {
|
|
t := time.UnixMilli(ms)
|
|
resets = &t
|
|
}
|
|
u := data.PlanUsage
|
|
inCursorPool := func(model string) bool {
|
|
model = cursorPoolBase(model)
|
|
if model == "auto" {
|
|
model = "default" // the CLI's Auto is default in Cursor's API
|
|
}
|
|
// the server names a family (grok-4.8); the CLI asks for one at an
|
|
// effort or speed (grok-4.8-high-fast)
|
|
return slices.ContainsFunc(data.AutoBucketModels, func(m string) bool { return cursorPoolBase(m) == model }) || cursorFirstPartyModel(model)
|
|
}
|
|
// the two pools fit the line; the total goes in its tooltip
|
|
return []QuotaWindow{
|
|
{Name: "Cursor Models", Used: u.Auto, ResetsAt: resets, matches: inCursorPool},
|
|
{Name: "Other Models", Used: u.API, ResetsAt: resets, matches: func(model string) bool { return !inCursorPool(model) }},
|
|
{Name: "Total", Used: u.Total, ResetsAt: resets, Aside: true},
|
|
}, nil
|
|
}
|
|
|
|
// cursorPoolBase names a model's family: lower case, without cursor- and
|
|
// the effort and speed the CLI adds to it.
|
|
func cursorPoolBase(model string) string {
|
|
model = strings.TrimPrefix(strings.ToLower(strings.TrimSpace(model)), "cursor-")
|
|
for {
|
|
b := cursorVariant.ReplaceAllString(model, "")
|
|
if b == model {
|
|
return model
|
|
}
|
|
model = b
|
|
}
|
|
}
|
|
|
|
// Cursor's autoBucketModels can lag model releases: it still omitted Grok
|
|
// 4.6/4.7 when the published Cursor Models pool already included them.
|
|
// Keep those documented families alongside the server's exact model list.
|
|
// See https://cursor.com/docs/models-and-pricing.
|
|
func cursorFirstPartyModel(model string) bool {
|
|
model = strings.TrimPrefix(model, "cursor-")
|
|
if model == "default" || model == "composer" || strings.HasPrefix(model, "composer-") {
|
|
return true
|
|
}
|
|
for _, base := range []string{"grok-4.5", "grok-4.6", "grok-4.7"} {
|
|
if model == base || strings.HasPrefix(model, base+"-") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|