Files
magpie/internal/agent/qwen.go
T
yetone cdfdb62c69 agent: a model field's list is joined with the catalog once while neither changed
Each agent's model field returned append(its own models,
viaMagpieShared(...)...), a copy of the whole catalog's options for
each field of each agent on every poll: Picks was 94.7 MB of 496 MB
over forty polls of the Agents page with real accounts. withVia keeps
the joined list per agent and prefix while the own models read the same
(sameOption, field by field) and the catalog's list is the very one
kept (same array), and joins it again when either changed. The list is
read only and clipped, as viaMagpieShared's is; every caller of a
field's Options reads it, Picks clones it before it drops anything.
The 52 `return append(own, viaMagpieShared(...)...)` sites call it.
omp's and Reasonix's planner edit the joined list after the append, and
Grok's writes Same into its copy, so they keep their own copies.

TestWithViaKeepsTheJoinedList: the same own models give the same array,
own models changed in a value or only an icon are joined anew, a
provider saved since is listed, and nothing joined is the catalog's own
list. TestSameOptionReadsEveryField sets each field of Option alone and
fails when sameOption doesn't see it. Broken by hand: never answering
from the kept list ("the same own models joined the catalog again"),
and FastFor left out of sameOption ("sameOption doesn't read
Option.FastFor").

Run on mote: both tests alone 3 of 3; internal/agent, internal/tui and
internal/cli ok; internal/gui red only on TestSettingsSaveKeepsWhatItDoesNotSend
(#4514) and TestMainWindowCorners (#3949); gofmt, vet and GOOS=windows
and linux vet clean. Real accounts in a sandbox HOME, forty polls:
Picks 94.7 MB -> 51.2 MB, state 190.8 MB -> 154.0 MB, state and drift
replies byte for byte the same as before. Footprint peak 136 MB.
2026-11-01 07:54:54 +08:00

442 lines
14 KiB
Go

package agent
// Qwen Code (`qwen`, QwenLM/qwen-code, a Gemini CLI fork) keeps its
// settings in ~/.qwen/settings.json ($QWEN_HOME when set). Models of the
// user's own are the modelProviders.<protocol> arrays, each
// {baseUrl, envKey, id, name}: id is the model asked for, name what its
// /model picker shows, and envKey names a variable in the same file's env
// object (or ~/.qwen/.env) with the key. The model sessions start on is
// settings.model {name, baseUrl}, baseUrl telling two entries of one id
// apart, and security.auth.selectedType "openai" skips the /auth prompt
// on start.
//
// magpie appends one entry per catalog model to modelProviders.openai,
// the wire id the gateway takes ("<provider>/<model>") and the name
// "magpie/<provider>/<model>", every one on the same env key of its own,
// which is how an entry of magpie's is told from the user's own, and
// points settings.model and security.auth.selectedType at a pick. The key
// itself lives in the settings' env object — a shell-exported or
// ~/.qwen/.env variable of the same name would win over it, which the
// qwen-specific name makes unlikely. The user's entries stay as they were
// and in front, so their own ids don't move, and the default, auth type
// and any key of theirs they had are stashed and put back when magpie
// steps out.
import (
"encoding/json"
"path/filepath"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/jsonc"
"github.com/yetone/magpie/internal/edit"
)
const (
qwenModels = "modelProviders.openai"
qwenModel = "model.name"
qwenModelAt = "model.baseUrl"
qwenAuth = "security.auth.selectedType"
qwenEnv = "env." + qwenEnvKey
// qwenEnvKey names the variable magpie's provider entries take their
// key from: qwen-specific so a shell or ~/.qwen/.env value of the
// name is unlikely to shadow the settings' (their priority over it is
// why "MAGPIE_API_KEY" is not used), and set in the settings' env
// object while magpie is wired in, which names one of magpie's
// entries wherever it appears.
qwenEnvKey = "MAGPIE_QWEN_API_KEY"
)
// qwenEntry is one modelProviders.openai member magpie writes, fields
// ordered as Qwen Code orders its own (alphabetically).
type qwenEntry struct {
BaseURL string `json:"baseUrl"`
EnvKey string `json:"envKey"`
Generation *qwenGen `json:"generationConfig,omitempty"`
ID string `json:"id"`
Name string `json:"name"`
}
// qwenGen is the generationConfig a model's entry may carry.
type qwenGen struct {
ContextWindowSize int `json:"contextWindowSize,omitempty"`
}
// qwenEntriesAt are magpie's entries for a qwen reaching the gateway at
// v1.
func qwenEntriesAt(v1 string) []qwenEntry {
var out []qwenEntry
for _, m := range sharedModels("qwen") {
// name is what /model shows, the id beside it when they differ:
// the model's name as every agent's list calls it, by the Provider
// in model names setting (magpie/<id> before, which none of the
// setting's ways changed)
e := qwenEntry{ID: m.ID, Name: m.Name, BaseURL: v1, EnvKey: qwenEnvKey}
if m.Context > 0 {
e.Generation = &qwenGen{ContextWindowSize: m.Context}
}
out = append(out, e)
}
return out
}
// qwenCustom is one modelProviders.openai member as it is in the file:
// its raw JSON, and what of it the picker needs.
type qwenCustom struct {
raw json.RawMessage
id, name string
base, envKey string
ours bool
}
// qwenCustoms reads modelProviders.openai of a settings.json.
func qwenCustoms(path string) []qwenCustom {
raw, err := edit.Read(path)
if err != nil || len(raw) == 0 {
return nil
}
var out []qwenCustom
all := gjson.GetBytes(jsonc.ToJSONInPlace(raw), qwenModels)
if !all.IsArray() {
// no modelProviders (or none of openai): none of magpie's and
// none of the user's, rather than an error ForEach gives for null
return nil
}
all.ForEach(func(_, v gjson.Result) bool {
c := qwenCustom{
raw: json.RawMessage(v.Raw),
id: v.Get("id").String(),
name: v.Get("name").String(),
base: v.Get("baseUrl").String(),
envKey: v.Get("envKey").String(),
}
c.ours = c.envKey == qwenEnvKey
out = append(out, c)
return true
})
return out
}
// qwenModelOf reads settings.model: the model's id, and the baseUrl
// telling one of its two entries apart ("" when unset).
func qwenModelOf(path string) (name, base string) {
name, _ = edit.GetJSON(path, qwenModel)
base, _ = edit.GetJSON(path, qwenModelAt)
return name, base
}
// qwenPrune takes a parent key out once the last thing in it is gone: the
// "env": {} or "modelProviders": {} that emptying magpie's writes would
// leave was never the user's.
func qwenPrune(path, parent string) error {
if v, ok := edit.GetJSON(path, parent); ok && strings.TrimSpace(v) == "{}" {
return edit.DelJSON(path, parent)
}
return nil
}
// qwenFind finds the entry settings.model points at: by id and baseUrl,
// or the first of the id when no baseUrl is set.
func qwenFind(cs []qwenCustom, name, base string) *qwenCustom {
for i := range cs {
if cs[i].id == name && (base == "" || cs[i].base == base) {
return &cs[i]
}
}
return nil
}
func qwen(home string) *Agent { return qwenIn(here(home)) }
// qwenIn is Qwen Code at a place: this machine's home, or a WSL distro's
// (see wsl.go), whose entries name the gateway as the distro reaches it,
// with the key it takes from there.
func qwenIn(at place) *Agent {
dir := at.join(".qwen")
if d := at.getenv("QWEN_HOME"); d != "" {
dir = d
}
path := filepath.Join(dir, "settings.json")
entries := func() []qwenEntry { return qwenEntriesAt(at.v1()) }
keyName := "qwen:" + path + ":" + qwenModel
keyBase := "qwen:" + path + ":" + qwenModelAt
keyAuth := "qwen:" + path + ":" + qwenAuth
// keyAuthSeen marks the user's own auth type already stashed (or
// known absent), so a later wire doesn't take magpie's own "openai"
// for it
keyAuthSeen := keyAuth + ".seen"
keyEnv := "qwen:" + path + ":" + qwenEnv
// get spells an entry of magpie's as the catalog does
get := func() string {
name, base := qwenModelOf(path)
if name == "" {
return ""
}
if c := qwenFind(qwenCustoms(path), name, base); c != nil && c.ours {
return magpieID + "/" + c.id
}
return name
}
// setEnvKey writes env.MAGPIE_API_KEY as the gateway's key; with=false
// puts back what was stashed, or takes the key out where it still is
// magpie's. A value of the user's own under that name is stashed before
// it is replaced and never removed: magpie only removes what it wrote.
setEnvKey := func(with bool) error {
if with {
if v, ok := edit.GetJSON(path, qwenEnv); ok && v != at.gwKey() {
stash(map[string]string{keyEnv: v})
}
return edit.SetJSON(path, edit.KV{Path: qwenEnv, Value: at.gwKey()})
}
if was := unstash(keyEnv); was != "" {
return edit.SetJSON(path, edit.KV{Path: qwenEnv, Value: was})
}
if v, ok := edit.GetJSON(path, qwenEnv); ok && v == at.gwKey() {
if err := edit.DelJSON(path, qwenEnv); err != nil {
return err
}
return qwenPrune(path, "env")
}
return nil
}
// setAuth sets security.auth.selectedType to openai while magpie's
// models are the pick (skipping the /auth prompt), stashing the user's
// own; with=false puts it back, or takes the key out where it never
// was, pruning the empty security.auth and security it would leave.
setAuth := func(with bool) error {
if with {
// the user's own value is stashed once, whatever it is —
// "openai" of their own too, or it can't be told from
// magpie's on a later wire or Sync and comes off with it
if _, seen := stashShared()[keyAuthSeen]; !seen {
kv := map[string]string{keyAuthSeen: "1"}
if v, ok := edit.GetJSON(path, qwenAuth); ok {
kv[keyAuth] = v
}
stash(kv)
}
return edit.SetJSON(path, edit.KV{Path: qwenAuth, Value: "openai"})
}
forget(keyAuthSeen)
if was := unstash(keyAuth); was != "" {
return edit.SetJSON(path, edit.KV{Path: qwenAuth, Value: was})
}
if err := edit.DelJSON(path, qwenAuth); err != nil {
return err
}
if err := qwenPrune(path, "security.auth"); err != nil {
return err
}
return qwenPrune(path, "security")
}
// setProviders writes modelProviders.openai with the user's own and,
// when with, magpie's after them; none left takes the array out. A
// with=false when none were magpie's changes nothing.
setProviders := func(with bool) error {
var ms []any
var had bool
for _, c := range qwenCustoms(path) {
if c.ours {
had = true
continue
}
ms = append(ms, c.raw)
}
if with {
if err := setEnvKey(true); err != nil {
return err
}
if err := setAuth(true); err != nil {
return err
}
for _, e := range entries() {
ms = append(ms, e)
}
} else if !had {
// nothing of magpie's left: a key it left behind, and the
// auth type it set, still go
if err := setEnvKey(false); err != nil {
return err
}
return setAuth(false)
}
if len(ms) > 0 {
if err := edit.SetJSON(path, edit.KV{Path: qwenModels, Value: ms}); err != nil {
return err
}
} else if err := edit.DelJSON(path, qwenModels); err != nil {
return err
}
if !with {
if err := setEnvKey(false); err != nil {
return err
}
if err := setAuth(false); err != nil {
return err
}
return qwenPrune(path, "modelProviders")
}
return nil
}
// put sets settings.model to (name, base), or takes it out for "":
// the "model": {} that Qwen Code never wrote is removed with its last
// member.
put := func(name, base string) error {
if name != "" {
if base != "" {
if err := edit.SetJSON(path, edit.KV{Path: qwenModelAt, Value: base}); err != nil {
return err
}
} else if err := edit.DelJSON(path, qwenModelAt); err != nil {
// baseUrl unset stays unset: qwen treats "" as a real
// endpoint of its own in the composite match
return err
}
return edit.SetJSON(path, edit.KV{Path: qwenModel, Value: name})
}
if err := edit.DelJSON(path, qwenModelAt); err != nil {
return err
}
if err := edit.DelJSON(path, qwenModel); err != nil {
return err
}
if v, ok := edit.GetJSON(path, "model"); ok && strings.TrimSpace(v) == "{}" {
return edit.DelJSON(path, "model")
}
return nil
}
return atomic(&Agent{
ID: "qwen", Name: "Qwen Code", Icon: "qwen-color", Aliases: []string{"qwen-code", "qwen-cli"}, Spelled: prefixed,
// qwen's requests carry QwenCode/<version> (packages/core/src/core/
// openaiContentGenerator/provider/default.ts buildHeaders)
UA: []string{"qwencode"},
Bin: "qwen", Dir: dir, Path: path,
ListOnly: true,
Sync: func() error {
for _, c := range qwenCustoms(path) {
if c.ours {
return setProviders(true)
}
}
return nil
},
Notice: func() string {
if Running(`(^|/)qwen( |$)`) {
return noticeQwen.String()
}
return ""
},
Check: func() string {
// settings.model is magpie's by the entry it names, wherever
// that entry points: another address is drift to report, not
// a model of the user's
name, _ := qwenModelOf(path)
if name == "" {
return ""
}
var c *qwenCustom
cs := qwenCustoms(path)
for i := range cs {
if cs[i].ours && cs[i].id == name {
c = &cs[i]
break
}
}
if c == nil {
return ""
}
if v, _ := edit.GetJSON(path, qwenEnv); !keyHolds(v, at.gwKey()) {
return "Qwen Code's " + qwenEnv + " (settings.json) no longer is magpie's key, so magpie's models have none"
}
if v, ok := edit.GetJSON(path, qwenAuth); !ok || v != "openai" {
return "Qwen Code's security.auth.selectedType (settings.json) no longer is openai, so it asks /auth instead of using magpie"
}
return wiringOff("Qwen Code", path, func(k string) (string, bool) {
g := gjson.GetBytes(c.raw, k)
return g.String(), g.Exists()
}, "baseUrl", at.v1())
},
Fields: []Field{{
Key: "model", Label: "model",
Get: get,
Set: func(v string) error {
curName, curBase := qwenModelOf(path)
ours := qwenFind(qwenCustoms(path), curName, curBase)
onOurs := ours != nil && ours.ours
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if !onOurs {
kv := map[string]string{keyBase: curBase}
if curName != "" {
kv[keyName] = curName
} else {
forget(keyName)
}
stash(kv)
}
if err := setProviders(true); err != nil {
return err
}
return put(ref, at.v1())
}
base := ""
fromStash := false
if onOurs {
// back to what the user had, name and its baseUrl
if v == "" {
v, base, fromStash = unstash(keyName), unstash(keyBase), true
} else {
forget(keyName, keyBase)
}
}
if v != "" && !fromStash {
if c := qwenFind(qwenCustoms(path), v, ""); c != nil {
base = c.base
}
}
if err := put(v, base); err != nil {
return err
}
return setProviders(false)
},
Options: func(cur map[string]string) []Option {
return withVia(qwenOwnOptions(path, cur["model"]), "qwen", magpieID+"/")
},
}},
}, path)
}
// qwenOwnOptions are the models of the user's own the settings file has:
// every modelProviders.openai entry that isn't magpie's, and the current
// pick.
func qwenOwnOptions(path, cur string) []Option {
seen := map[string]bool{}
var out []Option
for _, c := range qwenCustoms(path) {
if c.ours || seen[c.id] {
continue
}
seen[c.id] = true
label := c.name
if label == "" || label == c.id {
label = ""
}
out = append(out, Option{Value: c.id, Label: label, Icon: modelIcon("", c.id)})
}
if cur != "" && !seen[cur] && !strings.HasPrefix(cur, magpieID+"/") {
out = append([]Option{{Value: cur, Icon: modelIcon("", cur)}}, out...)
}
return group("Qwen Code", out)
}
// what qwen says after a change (notice.go)
var (
noticeQwen = newNotice("Qwen Code reads a session's model at start-up — start a new session, or /model anew, to use this.")
)