Files
magpie/internal/agent/openpencil.go
T
yetone bb478f0e50 agent: a look that only ranges over magpie's models reads the list kept
magpieModels gives each caller its own copy of the list it keeps per
agent. Most callers only range over it, or count it, and a range copies
each model anyway, so the copy guarded nothing for them (the models'
own lists were shared before too: the copy was shallow). sharedModels
is the list kept, read only; every `range magpieModels(` and
`len(magpieModels(` in internal/agent (39 files) takes it. Callers that
keep the list, append to it or hand it on still get their copy.

Real accounts (sandbox copy of the owner's HOME, network blocked), 40
polls of /api/state, /api/providers and drift, pprof alloc_space
between two heap snapshots, 616405061 (mrows) vs this (mshare):
  stampedLists.get's slices.Clone 36.7 MB -> gone (under pprof's cut);
  most of it was piOffered (22.5 MB) and claudeViaMagpie (13.7 MB)
  total 637 MB -> 584 MB (runs vary +-25 MB)
  /api/state and drift bodies byte-identical, /api/providers equal but
  for `fetched` and `lapsed` (a refused refresh in the blocked sandbox)

TestModelReadersShareTheList: sharedModels gives the kept array, not a
copy, and no non-test file in internal/agent ranges over or counts
magpieModels(. Broken by hand on mote: sharedModels through get fails
"a shared look copies the kept list"; one `range magpieModels("kimi")`
put back fails naming kimi.go.

Run on mote at this tree (before rebase): gofmt; go vet for darwin,
windows and linux; the new test with TestMagpieModelsKeptWhileCatalogStays,
TestKeptListSharedWithoutACopy and TestViaMagpieKept 3/3 ok;
go test ./internal/agent ok (201s); go test ./internal/gui: only the
known reds TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners (#3949).
Not run: the full ./... suite.
2026-11-01 06:09:56 +08:00

316 lines
11 KiB
Go

package agent
// OpenPencil (ZSeven-W/openpencil, the openpencil-desktop app; @JC_be_better
// on X) is a design canvas whose chat runs models through "builtin agents"
// of the user's own: an Anthropic or OpenAI-compatible endpoint, a key and
// a list of models, each listed in its chat's model picker. It keeps them
// in one settings.json in the platform's config folder, dirs::config_dir()
// joined with openpencil: ~/Library/Application Support/openpencil on a
// Mac, $XDG_CONFIG_HOME/openpencil (~/.config/openpencil) on Linux,
// %APPDATA%\openpencil on Windows (op-editor-host-core's settings_io.rs,
// settings_path). Its fields (SettingsPayload, BuiltinAgentPayload,
// settings_payload.rs, main at 40902a3f and v0.8.4):
//
// {"version":1, …,
// "builtin_agents":[{"id":"magpie","preset":"custom","display_name":"magpie",
// "kind":"openai-compat","api_key":"magpie-openpencil",
// "model":"<first model>","models":["<model>",…],
// "base_url":"http://127.0.0.1:3425/v1","enabled":true}], …}
//
// version is required (no serde default): a file without it fails to parse,
// is set aside as settings.json.corrupt-<time>, and OpenPencil then refuses
// to save at all; a version other than 1 has the whole file ignored. So
// magpie writes version 1 in a file it makes, and leaves alone a file whose
// version it doesn't know. kind openai-compat posts to base_url +
// /chat/completions, so base_url is the gateway's /v1. models is the list
// (at most 64, written only when there are two or more, the first again as
// model); v0.8.4 knows only model and reads past models, so it lists the
// first model alone.
//
// OpenPencil reads the file once, when it starts, and writes the whole of
// it back from what it holds when a setting changes and when it quits
// (app_handler.rs, settings_io::save). A change magpie makes while it runs
// is written over when it quits, so magpie refuses one then, where it can
// tell (Running can't on Windows, where the notice says so instead).
//
// The model is picked in OpenPencil's own chat picker
// (builtin:magpie:<model>), not in a file, so what magpie sets is whether
// its models are in that picker: one entry of builtin_agents, id magpie
// (OpenPencil numbers its own builtin-N, and reads a picker value up to its
// second colon). The user's other entries and settings stay, and so do the
// name and the on/off they give magpie's entry in OpenPencil's settings.
// Its User-Agent isn't known, so its requests are told apart by their key.
//
// open-pencil/open-pencil, the other app of the name, keeps its providers in
// its webview's localStorage, no file magpie can write: it reaches magpie
// through an agent magpie wires (Pi, an ACP agent) or a base URL typed in.
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
// openPencilApp is OpenPencil's macOS bundle, as its release builds it
// (scripts/bundle-macos.sh).
var openPencilApp = desktopApp{id: "com.zseven-w.openpencil", names: []string{"OpenPencil.app"}}
// openPencilMaxModels is how many models OpenPencil keeps for one entry
// (MAX_BUILTIN_AGENT_MODELS); it drops the rest when it next saves.
const openPencilMaxModels = 64
// openPencilProc is OpenPencil's process, its binary's name on every system
// (the .app's CFBundleExecutable, the .deb's and AppImage's, the .exe).
var openPencilProc = []string{`(^|/)openpencil-desktop( |$)`}
var noticeOpenPencil = newNotice("OpenPencil reads its settings when it starts and writes them all back when it quits — quit OpenPencil before changing this, then open it and pick a magpie model in its chat's model picker.")
// errOpenPencilRunning is a change refused while OpenPencil runs.
var errOpenPencilRunning = errors.New("OpenPencil writes its settings back when it quits, over what magpie writes now: quit OpenPencil, then try again")
func openPencil(home, cfg string) *Agent {
dir := openPencilDir(home, cfg)
path := filepath.Join(dir, "settings.json")
write := func(on bool) error {
if !on {
return openPencilUnwire(path)
}
return openPencilWire(path, openPencilEntry(gateway.URL()))
}
return atomic(&Agent{
ID: "openpencil", Name: "OpenPencil", Icon: "generic", Aliases: []string{"open-pencil", "openpencil-desktop"},
Dir: dir, Path: path,
// its settings folder once it has run, ~/.openpencil once its op
// CLI or MCP server has, else its app. No command: op is its CLI's
// name, and other tools' too
detect: func() bool {
return isDir(dir) || isDir(filepath.Join(home, ".openpencil")) || macBundle(openPencilApp) || openPencilWindowsApp()
},
Notice: func() string {
if openPencilWired(path) && Running(openPencilProc...) {
return noticeOpenPencil.String()
}
return ""
},
// a sync while it runs is written as well: a quit puts back what it
// read, and the next sync puts magpie's models back again
Sync: func() error {
if !openPencilWired(path) {
return nil
}
return write(true)
},
// its picker lists the models picked for it (#1508)
ListsModels: true,
Fields: []Field{{
Key: "provider", Label: "provider",
Get: func() string {
if openPencilWired(path) {
return magpieID
}
return ""
},
Set: func(v string) error {
if openPencilRunning() {
return errOpenPencilRunning
}
return write(v != "")
},
Options: func(map[string]string) []Option {
return []Option{{Value: magpieID, Label: "magpie", Icon: "magpie", Note: "every magpie model in OpenPencil's chat model picker"}}
},
}},
}, path)
}
// openPencilRunning says OpenPencil is open where magpie can tell; on
// Windows, where Running can't, no, and the notice asks to quit it first.
var openPencilRunning = func() bool {
return runtime.GOOS != "windows" && Running(openPencilProc...)
}
// openPencilDir is the folder of OpenPencil's settings.json, as Rust's
// dirs::config_dir() finds the platform's config folder.
func openPencilDir(home, cfg string) string {
return openPencilDirOn(runtime.GOOS, home, cfg, appdir.Getenv("APPDATA"))
}
// openPencilDirOn is openPencilDir on the system goos, with the
// XDG_CONFIG_HOME folder cfg and %APPDATA% appData.
func openPencilDirOn(goos, home, cfg, appData string) string {
switch goos {
case "darwin":
return filepath.Join(home, "Library", "Application Support", "openpencil")
case "windows":
d := appData
if d == "" {
d = filepath.Join(home, "AppData", "Roaming")
}
return filepath.Join(d, "openpencil")
}
return filepath.Join(cfg, "openpencil")
}
// openPencilWindowsApp says OpenPencil's installer has put it where it
// installs it, %ProgramFiles%\OpenPencil (scripts/package-windows.nsi).
func openPencilWindowsApp() bool {
if runtime.GOOS != "windows" {
return false
}
for _, v := range []string{"ProgramW6432", "ProgramFiles"} {
if d := appdir.Getenv(v); d != "" && isFile(filepath.Join(d, "OpenPencil", "openpencil-desktop.exe")) {
return true
}
}
return false
}
// openPencilAgent is an entry of builtin_agents in the order OpenPencil
// writes one.
type openPencilAgent struct {
ID string `json:"id"`
Preset string `json:"preset"`
DisplayName string `json:"display_name"`
Kind string `json:"kind"`
APIKey string `json:"api_key"`
Model string `json:"model"`
Models []string `json:"models,omitempty"`
BaseURL string `json:"base_url"`
Enabled bool `json:"enabled"`
}
// openPencilEntry is magpie's entry for a gateway at gw: its Chat
// Completions, the key that names OpenPencil, and the models picked for it
// as OpenPencil keeps a list (normalize_builtin_models): each once, at most
// 64, the list written only for two or more.
func openPencilEntry(gw string) openPencilAgent {
var ms []string
seen := map[string]bool{}
for _, m := range sharedModels("openpencil") {
if m.ID == "" || seen[m.ID] || len([]rune(m.ID)) > 256 {
continue
}
seen[m.ID] = true
ms = append(ms, m.ID)
if len(ms) == openPencilMaxModels {
break
}
}
e := openPencilAgent{ID: magpieID, Preset: "custom", DisplayName: "magpie", Kind: "openai-compat",
APIKey: agentKeyAt("openpencil", gw), BaseURL: gw + "/v1", Enabled: true}
if len(ms) > 0 {
e.Model = ms[0]
}
if len(ms) > 1 {
e.Models = ms
}
return e
}
// openPencilWired says settings.json has magpie's entry.
func openPencilWired(path string) bool {
_, i, _ := openPencilAgents(path)
return i >= 0
}
// openPencilAgents are the entries of settings.json's builtin_agents as
// written, and the place of magpie's among them (-1 for none). A file that
// isn't there or holds nothing has none; one OpenPencil wouldn't read, or
// whose version it doesn't know, is an error, so magpie leaves it alone.
func openPencilAgents(path string) (list []json.RawMessage, i int, err error) {
i = -1
b, err := edit.Read(path)
if err != nil {
return nil, i, err
}
if len(bytes.TrimSpace(b)) == 0 {
return nil, i, nil
}
if !json.Valid(b) || !gjson.ParseBytes(b).IsObject() {
return nil, i, fmt.Errorf("%s isn't JSON OpenPencil reads", path)
}
if v := gjson.GetBytes(b, "version"); v.Type != gjson.Number || v.Raw != "1" {
return nil, i, fmt.Errorf("%s says version %s, not the 1 OpenPencil reads, so magpie leaves it alone", path, v.Raw)
}
raw := gjson.GetBytes(b, "builtin_agents")
if !raw.Exists() || raw.Type == gjson.Null {
return nil, i, nil
}
if !raw.IsArray() || json.Unmarshal([]byte(raw.Raw), &list) != nil {
return nil, i, fmt.Errorf("%s: builtin_agents is not a list", path)
}
for j, e := range list {
if gjson.GetBytes(e, "id").String() == magpieID {
return list, j, nil
}
}
return list, i, nil
}
// openPencilWire puts magpie's entry into settings.json, the file made when
// there is none; an entry there already keeps the name and the on/off the
// user gave it in OpenPencil. The file is written only when the entry says
// something else.
func openPencilWire(path string, want openPencilAgent) error {
list, i, err := openPencilAgents(path)
if err != nil {
return err
}
if i >= 0 {
if n := gjson.GetBytes(list[i], "display_name"); n.Type == gjson.String {
want.DisplayName = n.String()
}
if e := gjson.GetBytes(list[i], "enabled"); e.IsBool() {
want.Enabled = e.Bool()
}
}
w, err := json.Marshal(want)
if err != nil {
return err
}
if i >= 0 && sameJSON(string(list[i]), want) {
return nil
}
if i >= 0 {
list[i] = w
} else {
list = append(list, w)
}
if _, has := edit.GetJSON(path, "version"); !has {
return edit.SetJSON(path, edit.KV{Path: "version", Value: 1}, edit.KV{Path: "builtin_agents", Value: list})
}
return edit.SetJSON(path, edit.KV{Path: "builtin_agents", Value: list})
}
// openPencilUnwire takes magpie's entry out of settings.json, and the file
// when nothing else is left in it: the version and an empty list are what
// magpie wrote in a file OpenPencil hasn't saved yet.
func openPencilUnwire(path string) error {
list, i, err := openPencilAgents(path)
if err != nil || i < 0 {
return err
}
list = append(list[:i], list[i+1:]...)
if len(list) == 0 {
var rest map[string]json.RawMessage
if b, err := edit.Read(path); err == nil && json.Unmarshal(b, &rest) == nil {
delete(rest, "version")
delete(rest, "builtin_agents")
if len(rest) == 0 {
return os.Remove(path)
}
}
}
return edit.SetJSON(path, edit.KV{Path: "builtin_agents", Value: list})
}