Files
magpie/internal/agent/pencil.go
T
yetone 31d904d938 agents: Pencil (pen.dev) is an agent magpie sets up, its custom models in ~/.pencil/models.json (徐小小 on Discord: Pencil keeps its custom model file at ~/.pencil/models.json, in Pi's format) — Pencil's settings take 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, and its models.json is Pi's: {"providers":{"<id>":{"name","baseUrl","api","models":[{"id","name","reasoning","input","cost":{input,output,cacheRead,cacheWrite},"contextWindow","maxTokens","compat"}]}}}. The new pencil agent (pencil.go) adds magpie there as the provider "magpie": baseUrl the gateway's /v1, api openai-completions, apiKey magpie-pencil (its User-Agent isn't known, so its requests are told by the key), and every model as Pi's entry — Pi's model loop is now piModelJSON, shared — without a model's own api and baseUrl, which Pi reads but Pencil's own editor never writes, so every model goes to the gateway's Chat endpoint whatever Pencil makes of them; each model carries every field Pencil writes for one, Pi's defaults (128K window, 16K out) where magpie knows none, no cost and an empty compat. The model is picked in Pencil's agent composer, not in a file known to magpie, so its field is "provider", as for WorkBuddy and ZCode: whether magpie's models are in Pencil's picker; the user's other providers, their keys and Pencil's other keys stay, off takes only magpie's out, and the catalog sync rewrites it only where it is. It is found by ~/.pencil (the app's and pen.dev's CLI's) or, on a Mac, Pencil.app / pen.dev.app in /Applications or ~/Applications; not by a command, pencil and pen being common names. After a change it asks for Pencil to be restarted when Pencil is running (and always on Windows, where that can't be told): whether it reloads models.json on its own isn't known. No official icon with a licence that allows it was found, so it has the generic one. Nothing of Pencil's was downloaded or run; whether Pencil reads a model's thinkingLevelMap, a settings file naming a default model, and its paths on Windows and Linux (taken as ~/.pencil, as Node's homedir gives) aren't verified. TestPencilModels (the reporter's file with a key and a key of Pencil's: kept through on, sync and off; magpie's provider decoded strictly as Pencil's format, every model's fields of their types, no per-model api or baseUrl; found only with ~/.pencil; among All()) and TestStateListsPencil (the page's state lists Pencil with its provider field once ~/.pencil is there, not before) fail without the change. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass (TestClaudeSignedOut failed once on its 5 s wait, passes alone; untouched).
2026-10-02 14:43:06 +08:00

117 lines
4.4 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 "Pencil reads its custom models at start-up — restart Pencil to see magpie's models in its model picker."
}
return ""
},
Sync: func() error {
return syncJSON(path, key, func() any { return pencilProviderJSON(gateway.URL()) })
},
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: pencilProviderJSON(gateway.URL())})
},
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 []string{"/Applications", filepath.Join(home, "Applications")} {
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": gateway.TokenFor("pencil"), "models": ms}
}