yyddbds on Discord: in Xiaomi MiMo they added a custom provider pointing
at magpie and picked a few models by hand. MiMo's switch on the Agents
page was off, yet after an update the provider held every one of
magpie's models. SyncCatalog runs at each start and update and on every
catalog change, and MiMo Code's Sync (openCodeLike) brought any
provider.magpie block up to the catalog whether magpie wrote it or not.
magpie's default went over a list the user set.
An Agent whose Sync does nothing but keep a block of magpie's name up
to the catalog is now ListOnly, and SyncCatalog runs it only while the
agent is Wired, which is what the Agents page's switch shows. The same
sync was in every sibling, so ListOnly is on all of them: openCodeLike
(OpenCode, MiMo Code and their WSL forms), piLikeKeyed (Pi and forks),
Goose, Crush, Air, AtomCode, Command Code, Droid, Fx, jcode, Kimi,
MiniMax, Mothx, omp, OpenChamber, OpenClaw, OpenPencil, Penguin, Qwen
Code and T3 Code. Left out on purpose: Codex, Claude, Grok and Hermes,
whose Syncs also repair while not connected (Hermes keeps its bots'
magpie provider current) and have their own gates.
Trade-off: a list magpie wrote while connected doesn't follow the
catalog while the agent runs a model of its own (switch off); it does
again once a model of magpie's is picked. Users whose list was already
written over: magpie keeps no copy of the old list, so it stays full;
from this release on it is left alone, and they trim it once by hand
(or connect the agent in magpie and use its model list's Pick).
Tests: TestCatalogSyncLeavesAHandWrittenMagpieBlock (MiMo, Pi and Crush
with a hand-written magpie block stay byte for byte; connected, MiMo's
list follows the catalog; disconnected and written by hand again, left
alone; a magpie model picked in MiMo, followed again).
TestListOnlyAgentsReadConnected: every ListOnly agent magpie connects
reads as Wired (n >= 15), so none of them loses catalog updates.
Fixtures of TestSyncCatalogRewritesAgentLists,
TestModelNameAndLevelsReachAgentFiles, TestSuffixModesReachAgentFiles
and TestPiKeepsTheirKeysInMagpiesProvider, and internal/cli's
TestSaveAnswersBeforeTheAgentsSync (its Pi settings were {}), now have
Pi connected (settings.json defaultProvider magpie), as a block magpie
wrote implies.
docs/subsystems/agent-wiring.md says the rule.
Run (mote, sandbox HOME via the tests' syncHome/codexHome):
- old code + the new test: red (MiMo, Pi, Crush written over; after
Disconnect written over).
- broken by hand: ListOnly dropped from openCodeLike: red on MiMo
(lines 52, 82); the Wired gate in SyncCatalog replaced with true: red
on all three.
- go test -tags nogui -count=1 ./internal/agent: ok.
- gofmt -l clean; go vet ./... for darwin, GOOS=linux and GOOS=windows:
ok.
- full suite, go test -tags nogui -count=1 -timeout 45m ./... at base
3bebc3839 (logs kept in the worktree): every package ok but
internal/cli (TestSaveAnswersBeforeTheAgentsSync, mine, fixture fixed
above, then ok alone) and internal/gateway: TestAntigravityTurned*,
TestAntigravityTurnsCodexsGPT5ThreadAwayOnce and
TestCodexSpentGrokFailingIsTold, red the same on a fresh origin/main
b26fa0903 copy, filed as #4362.
- rebased on e8d42c02d: vet (darwin, linux, windows) of internal/agent and
internal/cli, and go test of internal/agent, internal/cli,
internal/library and internal/gui: all ok. Pushed on 764a032f6,
whose three commits since touch gateway, usage, provider and chat only.
Not tried: the real Xiaomi MiMo desktop app (the fixture is the OpenCode
shape MiMo Code writes; whether the reporter's provider id is literally
"magpie" is unconfirmed: a provider of another id was never touched).
Lists already written over are not restored.
125 lines
4.6 KiB
Go
125 lines
4.6 KiB
Go
package agent
|
|
|
|
// Pencil (pen.dev, the design canvas whose agent runs models) takes a
|
|
// custom provider of one's own in ~/.pencil/models.json, in Pi's format
|
|
// (徐小小 on Discord):
|
|
//
|
|
// {"providers":{"<id>":{"name":…,"baseUrl":…,"api":"openai-responses",
|
|
// "models":[{"id":…,"name":…,"reasoning":true,"input":["text","image"],
|
|
// "cost":{"input":0,"output":0,"cacheRead":0,"cacheWrite":0},
|
|
// "contextWindow":…,"maxTokens":…,"compat":{}}]}}}
|
|
//
|
|
// Its settings offer a custom provider on OpenAI Completions, OpenAI
|
|
// Responses or Anthropic Messages (docs.pen.dev, Authentication) — Pi's
|
|
// openai-completions, openai-responses and anthropic-messages — set for the
|
|
// provider as a whole. So magpie's models are Pi's entries (piModelJSON)
|
|
// with every model on the provider's openai-completions at the gateway's
|
|
// /v1, which the gateway takes for every model: a model's own api and
|
|
// baseUrl, which Pi reads but Pencil's own editor never writes, are left
|
|
// out. Each model carries every field Pencil writes for one. The model is
|
|
// picked in Pencil's agent composer, not in a file magpie knows of, so what
|
|
// magpie sets is whether its models are in that picker; the user's other
|
|
// providers stay as they are. Its requests carry magpie-pencil, as its
|
|
// User-Agent isn't known.
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
)
|
|
|
|
func pencil(home string) *Agent {
|
|
dir := filepath.Join(home, ".pencil")
|
|
path := filepath.Join(dir, "models.json")
|
|
key := "providers." + magpieID
|
|
wired := func() bool { _, ok := edit.GetJSON(path, key); return ok }
|
|
return &Agent{
|
|
ID: "pencil", Name: "Pencil", Icon: "generic", Aliases: []string{"pen.dev", "pen-dev", "pendev"},
|
|
Dir: dir, Path: path,
|
|
// ~/.pencil is the app's (and pen.dev's CLI's) once it has run; the
|
|
// app installed and never opened is found by its bundle on a Mac.
|
|
// No command: `pencil` and `pen` are common names.
|
|
detect: func() bool { return isDir(dir) || pencilApp(home) },
|
|
Notice: func() string {
|
|
if runtime.GOOS == "windows" || Running(`Pencil\.app/`, `pen\.dev\.app/`, `(^|/)[Pp]encil( |$)`) {
|
|
return noticePencil.String()
|
|
}
|
|
return ""
|
|
},
|
|
ListOnly: true,
|
|
Sync: func() error {
|
|
return syncJSON(path, key, theirsKept(path, key, func() any { return pencilProviderJSON(gateway.URL()) }, "models"))
|
|
},
|
|
// its picker lists the models picked for it (#1508)
|
|
ListsModels: true,
|
|
Fields: []Field{{
|
|
Key: "provider", Label: "provider",
|
|
Get: func() string {
|
|
if wired() {
|
|
return magpieID
|
|
}
|
|
return ""
|
|
},
|
|
Set: func(v string) error {
|
|
if v == "" {
|
|
return edit.DelJSON(path, key)
|
|
}
|
|
return edit.SetJSON(path, edit.KV{Path: key, Value: theirsKept(path, key, func() any { return pencilProviderJSON(gateway.URL()) }, "models")()})
|
|
},
|
|
Options: func(map[string]string) []Option {
|
|
return []Option{{Value: magpieID, Label: "magpie", Icon: "magpie", Note: "every magpie model in Pencil's model picker"}}
|
|
},
|
|
}},
|
|
}
|
|
}
|
|
|
|
// pencilApp reports whether Pencil's app is installed where a Mac keeps
|
|
// apps, under its name before and after the pen.dev rename.
|
|
func pencilApp(home string) bool {
|
|
if runtime.GOOS != "darwin" {
|
|
return false
|
|
}
|
|
for _, d := range macAppFolders(home) {
|
|
for _, n := range []string{"Pencil.app", "pen.dev.app"} {
|
|
if _, err := os.Stat(filepath.Join(d, n)); err == nil {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// pencilProviderJSON is magpie's provider in Pencil's models.json: Pi's
|
|
// entries (piModelJSON), on the provider's openai-completions throughout,
|
|
// each with the fields Pencil writes for a model — Pi's defaults where
|
|
// magpie knows no better (a 128K window, 16K out) and no cost, magpie's
|
|
// spend being counted by magpie.
|
|
func pencilProviderJSON(gw string) map[string]any {
|
|
ms := []map[string]any{}
|
|
for _, m := range magpieModels("pencil") {
|
|
e := piModelJSON(m, gw, false)
|
|
if _, ok := e["input"]; !ok {
|
|
e["input"] = []string{"text"}
|
|
}
|
|
if _, ok := e["contextWindow"]; !ok {
|
|
e["contextWindow"] = 128000
|
|
}
|
|
if _, ok := e["maxTokens"]; !ok {
|
|
e["maxTokens"] = min(16384, e["contextWindow"].(int))
|
|
}
|
|
e["cost"] = map[string]any{"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0}
|
|
e["compat"] = map[string]any{}
|
|
ms = append(ms, e)
|
|
}
|
|
return map[string]any{"name": "magpie", "baseUrl": gw + "/v1", "api": "openai-completions",
|
|
"apiKey": agentKeyAt("pencil", gw), "models": ms}
|
|
}
|
|
|
|
// what pencil says after a change (notice.go)
|
|
var (
|
|
noticePencil = newNotice("Pencil reads its custom models at start-up — restart Pencil to see magpie's models in its model picker.")
|
|
)
|