Files
magpie/internal/agent/paimon.go
T
yetone 5e3f14f21f agent: Paimon, magpie's models through its litellm provider (ditangongyuan on X)
Paimon (aisk/paimon, `uv tool install paimon`) is a terminal coding agent
on pydantic-ai. Read at 7f7fa51 (config.py, llm.py): its settings are one
JSON file, $PAIMON_CONFIG_HOME/config.json, else ~/.config/paimon/config.json
on every OS. "model" is provider:name, and "providers" keeps each
provider's api_base and api_key.

magpie connects it through pydantic-ai's litellm provider, an
OpenAI-compatible proxy that profiles a vendor/model name by its vendor,
as magpie's ids are spelled, and which Paimon's login never offers:
model = litellm:<catalog id>, providers.litellm = the gateway's /v1 and
magpie's key for Paimon (0600, as Paimon writes it), and
compaction.context_window = the model's window. Paimon compacts against
that only for names genai-prices doesn't know, which magpie's ids aren't;
for a model whose window magpie doesn't know it gets the user's own back,
never the last model's. What the user had in the three keys is stashed and
put back on Disconnect (or `magpie paimon default`); the rest of the file
is untouched, and a file magpie made goes again. A model picked in Paimon
itself while magpie's provider stays is the user's from then on. Requests
are told apart by Paimon's paimon/<version> User-Agent. WSL distros, the
PAIMON_CONFIG_HOME isolation, notices in every language, icon, READMEs,
docs/reference.md, agent-wiring.md and the site's list come with it.

Run:
mote refused SSH all session (kex reset), so the Linux runs went
through wsl-run (magpie-desktop) and Windows through win-run:
- go vet: linux -tags nogui, GOOS=windows, GOOS=darwin -tags nogui on
  internal/agent, agentenv, library: ok. gofmt -l: clean.
- go test -tags nogui ./internal/agent/... agentenv library gui cli (WSL,
  base f03ec97b8): all ok except TestGrokInstalled, the known WSL binfmt
  red #3548.
- Windows: TestPaimon, TestHandWired*, TestEveryNoticeIsTranslated,
  TestPortMovesEveryAgent ok (file-mode checks skipped there).
- GUI, chromium + webkit (WSL): i18n-keys, i18n-en-no-cjk,
  agent-notice-lang, gui-de, gui-ja, gui-zh-tw, icon-one-tone, icon-tile,
  mono-icon-redraw, routing-client-icons, usage-agent-filter-icons,
  agent-connect(-no-models), agent-models(-own, -picker), agent-layout:
  150/151; the red is gui-ja's usage price string, untouched here, filed
  as #5943.
- Real Paimon 0.2.8 (uv pip install paimon) in a sandbox HOME on WSL,
  against a sandbox magpie gateway over a fake OpenAI upstream:
  `magpie paimon magpie/fakeup/m1` wrote litellm:fakeup/m1 and
  providers.litellm at 0600; `paimon -p ping` answered "pong from fake
  upstream", the upstream saw POST /v1/chat/completions with
  "paimon/0.2.8 (Python 3.10.19; Linux x86_64)", and magpie usage counted
  the call under Paimon; `magpie paimon default` put the file back equal
  to the user's.
- After the rebase on origin/main 9c39edb92: the three vets, the targeted
  agent tests and internal/gui ok; internal/library's
  TestEveryAgentInLibrary red for avibe from 9c39edb92 (#5945, not this
  change); i18n-keys, i18n-en-no-cjk, agent-notice-lang, gui-de, gui-zh-tw
  ok; icon-one-tone's every-logo case ok in both engines, its webkit
  Plugins-page case timed out on page.goto's 5s at WSL load 92 (#5425).

Broken by hand, each red in TestPaimon:
- the user's window not put back for a model magpie doesn't know (switch
  window: <nil>);
- stashing again while providers.litellm is magpie's (switch window: 200000);
- a file magpie made not removed (made file left: {});
- a model picked in Paimon not kept as the user's (restore after a pick).

Not tried: macOS (mote unreachable) and the GUI's own Connect click with
the real binary; Paimon's auto-compaction at a window magpie wrote (the
fake model had none, so Paimon said compaction was off, as it does for any
name it doesn't know); a model picked in Paimon's own /model while
connected beyond the unit test.
2026-11-10 19:54:37 +08:00

265 lines
8.4 KiB
Go

package agent
// Paimon (aisk/paimon, `uv tool install paimon`; ditangongyuan on X) is a
// coding agent in the terminal, built on pydantic-ai. Read at 7f7fa51
// (paimon/config.py, paimon/llm.py):
//
// Its settings are one JSON file, $PAIMON_CONFIG_HOME/config.json, else
// ~/.config/paimon/config.json (Windows too: Path.home()). "model" is
// "provider:name" (or the older "provider/name"), and "providers" keeps
// each provider's api_base and api_key, so a model is sent with its own
// provider's credentials only. The provider half picks pydantic-ai's
// provider class; a custom api_base on one that has no base_url argument
// goes through an OpenAI client of its own (llm.build_model).
//
// magpie uses pydantic-ai's litellm provider, an OpenAI-compatible proxy
// that profiles a "vendor/model" name by its vendor, as magpie's ids are
// spelled. Paimon's login doesn't offer it (it lists the providers of
// pydantic-ai's known model names), so it is almost never one of the
// user's own. magpie sets "model" to litellm:<catalog id>, providers.litellm
// to the gateway's /v1 and magpie's key for Paimon, and
// compaction.context_window to the window magpie knows the model to take:
// Paimon compacts against that only for a name genai-prices doesn't know,
// which magpie's ids aren't, and doesn't compact at all without it. What
// the user had in those three is stashed and put back when magpie steps
// out; the rest of the file is left alone.
//
// Paimon reads the file when it starts, and its own saves change only the
// keys they name (Config.save), so a running Paimon keeps its model until
// it starts again.
import (
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/edit"
)
// paimonProvider is the pydantic-ai provider magpie's models go through.
const paimonProvider = "litellm"
// paimonDir is Paimon's config folder at the place: $PAIMON_CONFIG_HOME
// when it is a whole path on this machine, else ~/.config/paimon.
func paimonDir(at place) string {
if d := at.getenv("PAIMON_CONFIG_HOME"); d != "" && filepath.IsAbs(d) {
return d
}
return at.join(".config", "paimon")
}
func paimon(home string) *Agent { return paimonAt(here(home)) }
// paimonIn is Paimon in a WSL distro (see wsl.go).
func paimonIn(at place) *Agent { return paimonAt(at) }
func paimonAt(at place) *Agent {
dir := paimonDir(at)
path := filepath.Join(dir, "config.json")
key := "paimon:" + path + ":"
keyModel, keyProv, keyWindow := key+"model", key+"provider", key+"window"
// keyNew marks a file magpie made, which goes again once magpie steps
// out and nothing is left in it
keyNew := key + "new"
gwKey := func() string { return agentKeyAt("paimon", at.gw()) }
read := func(p string) gjson.Result {
raw, err := edit.Read(path)
if err != nil {
return gjson.Result{}
}
return gjson.GetBytes(raw, p)
}
model := func() string { return read("model").String() }
prov := func() gjson.Result { return read("providers." + paimonProvider) }
// ours: providers.litellm is magpie's, told by its key
ours := func() bool { return keyHolds(prov().Get("api_key").String(), gwKey()) }
// on: the model one of magpie's, sent to magpie's provider
on := func() bool {
ref, ok := strings.CutPrefix(model(), paimonProvider+":")
return ok && ref != "" && ours()
}
window := func(ref string) int {
for _, m := range sharedModels("paimon") {
if m.ID == ref {
return m.Context
}
}
return 0
}
// prune takes out compaction and providers where magpie left them
// empty, and the file where magpie made it and left nothing
prune := func(made bool) error {
raw, err := edit.Read(path)
if err != nil {
return nil
}
var del []string
for _, k := range []string{"compaction", "providers"} {
if r := gjson.GetBytes(raw, k); r.IsObject() && len(r.Map()) == 0 {
del = append(del, k)
}
}
if len(del) > 0 {
if err := edit.DelJSON(path, del...); err != nil {
return err
}
}
if made {
if raw, err := edit.Read(path); err == nil && len(gjson.ParseBytes(raw).Map()) == 0 {
return edit.Remove(path)
}
}
return nil
}
// restore takes magpie out: the model, provider and window back to
// what the user had; then v, if any, as the model
restore := func(v string) error {
was, wasProv, wasWin := unstash(keyModel), unstash(keyProv), unstash(keyWindow)
made := unstash(keyNew) != ""
if !isFile(path) {
return nil
}
if err := edit.DelJSON(path, "model", "providers."+paimonProvider, "compaction.context_window"); err != nil {
return err
}
var kvs []edit.KV
if v != "" {
was = v
}
if was != "" {
kvs = append(kvs, edit.KV{Path: "model", Value: was})
}
if gjson.Valid(wasProv) && gjson.Parse(wasProv).IsObject() {
var p map[string]any
json.Unmarshal([]byte(wasProv), &p)
kvs = append(kvs, edit.KV{Path: "providers." + paimonProvider, Value: p})
}
if wasWin != "" {
if n, err := strconv.Atoi(wasWin); err == nil {
kvs = append(kvs, edit.KV{Path: "compaction.context_window", Value: n})
}
}
if len(kvs) > 0 {
if err := edit.SetJSON(path, kvs...); err != nil {
return err
}
}
return prune(made)
}
return atomic(&Agent{
ID: "paimon", Name: "Paimon", Icon: "paimon", Spelled: prefixed, NoNames: true,
// pydantic-ai's requests carry paimon/<version> (llm.user_agent)
UA: []string{"paimon/"},
Bin: "paimon", Dir: dir, Path: path,
Notice: func() string {
if Running(`(^|/)paimon( |$)`) {
return noticePaimon.String()
}
return ""
},
Check: func() string {
if !on() {
return ""
}
p := prov()
return wiringOff("Paimon", path, func(k string) (string, bool) {
r := p.Get(k)
return r.String(), r.Exists()
}, "api_base", at.v1())
},
Fields: []Field{{
Key: "model", Label: "model",
Get: func() string {
m := model()
if on() {
return magpieID + "/" + strings.TrimPrefix(m, paimonProvider+":")
}
return m
},
Set: func(v string) error {
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
if !ours() {
// what the user had, unless magpie stashed it already
forget(keyModel, keyProv, keyWindow, keyNew)
kv := map[string]string{
keyModel: model(),
keyProv: prov().Raw,
keyWindow: read("compaction.context_window").Raw,
}
if !isFile(path) {
kv[keyNew] = "1"
}
stash(kv)
} else if m := model(); !on() && m != "" {
// a model the user picked in Paimon since, on a
// provider of their own: theirs now, put back later
stash(map[string]string{keyModel: m})
}
if !isFile(path) {
// Paimon keeps keys in it: private, as Paimon makes it
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
if f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600); err == nil {
f.WriteString("{}\n")
f.Close()
}
}
kvs := []edit.KV{
{Path: "model", Value: paimonProvider + ":" + ref},
{Path: "providers." + paimonProvider, Value: map[string]any{
"api_base": at.v1(),
"api_key": gwKey(),
}},
}
// the model's window; for one magpie doesn't know, the
// user's own, never the last model's
n := window(ref)
if n == 0 {
n, _ = strconv.Atoi(unstashPeek(keyWindow))
}
if n > 0 {
kvs = append(kvs, edit.KV{Path: "compaction.context_window", Value: n})
} else if err := edit.DelJSON(path, "compaction.context_window"); err != nil {
return err
}
if err := edit.SetJSON(path, kvs...); err != nil {
return err
}
return prune(false)
}
if ours() {
return restore(v)
}
if v == "" {
if !isFile(path) {
return nil
}
return edit.DelJSON(path, "model")
}
return edit.SetJSON(path, edit.KV{Path: "model", Value: v})
},
Options: func(cur map[string]string) []Option {
var own []Option
if c := cur["model"]; c != "" && !usesMagpie(c) {
id := c
if _, rest, ok := strings.Cut(c, ":"); ok {
id = rest
} else if _, rest, ok := strings.Cut(c, "/"); ok {
id = rest
}
own = append(own, Option{Value: c, Icon: modelIcon("", id)})
}
return withVia(group("Paimon", own), "paimon", magpieID+"/")
},
}},
}, path)
}
// what Paimon says after a change (notice.go)
var noticePaimon = newNotice("Paimon reads its model at start-up — restart open paimon sessions to use this.")