rankyurky asked on Discord #feedback for PenguinHarness (npm @prismshadow/penguin-cli, command penguin). Every value comes from Penguin's own source (0.2.17): - Config: $PENGUIN_HOME (else ~/.penguin/data, every OS) / $PENGUIN_PROJECT_ID (else default_project) /.project_config.toml. magpie writes [providers.magpie] (base_url, client_type, api_key), one [[models]] entry per magpie model (context_window, vision, max_tokens, [models.pricing] in usd_per_mtok) and default_model in Penguin's own inline spelling. Penguin's server reads the file again when its mtime changes, so no restart; a default naming no entry is refused by Penguin, so Check fails when one is missing. - A file magpie creates is 0600 in a 0700 folder, as Penguin keeps it. Disconnect restores the user's own default_model (or none), drops a magpie vision model, and removes the file when magpie made it. Penguin's own entries, providers and command_policy rules are kept. - A sync that changes nothing writes nothing (bytes and mtime kept), so Penguin's watcher isn't woken. - Install: curl -fsSL https://penguin.ooo/install.sh | sh, or irm https://penguin.ooo/install.ps1 | iex on Windows; update: npm, or penguin update --yes for the tarball, except on Windows where update.ts refuses it. - WSL: a penguin kind with no restart notice; the WSL test now expects an empty Notice for a kind with no restart. - agentenv: PENGUIN_HOME, PENGUIN_PROJECT_ID; the project id is in NotPaths, or appdir dropped a value like "work" as a relative path. - edit.SetTOMLArrayTables: replaces every [[name]] entry with key = value (with child tables) by blocks, or removes them; blank lines between replaced entries go with them, and an unchanged result isn't written. - GUI: penguin-color.svg icon; README (en, zh-CN), docs/reference.md and docs/subsystems/agent-wiring.md. Run (worktree atca9b5048e, GOMODCACHE/GOCACHE pinned): - go test -tags nogui -timeout 45m ./internal/agent ./internal/edit ./internal/agentenv ./internal/appdir ./internal/usage: all ok but TestWSLMoreAgentsPick (penguin has no restart notice), fixed in the test; rerun -run 'TestWSL|TestPenguin|TestEveryAgent|TestFolderVars| TestEveryNotice' ok. - internal/gui: TestCoreScriptBudget, TestAssetTypesHoldAgainstTheRegistry, icon and i18n guards ok; no budget raised. - GUI Playwright, chromium and webkit: i18n-keys, getting-started, agent-cli-missing, agent-update-all, gui-ja, gui-de, gui-zh-tw: 123 tests, 123 pass. The icon renders in colour in both engines. - gofmt -l internal/ clean; go vet -tags nogui ./... with GOOS linux, windows and darwin clean. - Real Penguin 0.2.17 (npm, Node 25) in a sandbox HOME against a sandbox magpie serve and a fake upstream: penguin run -m "Say PONG" answered "PONG from fake" through magpie (usage agent "penguin", /v1/responses -> /v1/chat/completions); switching the model in magpie took effect with no Penguin restart; a no-op sync kept bytes and mtime; magpie penguin default restored the user's custom/my-own and left no magpie line; with no prior file it was created 0600 and removed on disconnect. The fixture is the file Penguin itself wrote. - Rebased on origin/maind9a8148d7and run again there: internal/agent -run 'TestPenguin|TestWSL|TestEveryAgent|TestFolderVars|TestEveryNotice| TestSync', internal/edit, internal/agentenv and the internal/gui guards ok; gofmt clean. Broken by hand, each went red: the no-op skip (mtime test), the 0600 create, the inline default_model spelling, the own-default restore, removing magpie's file, dropping the vision model, the NotPaths entry, and both SetTOMLArrayTables fixes (blank-line growth, unchanged write). Not tried: the full ./... suite (machine load ~190-200, as asked); a Windows or WSL real binary; Penguin's desktop app; a real vendor key through Penguin. Penguin sends the openai SDK's User-Agent, so the agent is named from its key, not its UA.
368 lines
12 KiB
Go
368 lines
12 KiB
Go
package agent
|
|
|
|
// PenguinHarness (Prism-Shadow/penguin-harness, the penguin command; npm
|
|
// @prismshadow/penguin-cli) keeps each Project's models in one TOML file
|
|
// under its data root: $PENGUIN_HOME, else ~/.penguin/data on every system
|
|
// (core's resolveRoot, os.homedir()), the CLI's and the desktop app's
|
|
// alike. The CLI works in the Project $PENGUIN_PROJECT_ID, else
|
|
// default_project (client.ts), the one its server makes first, so the file
|
|
// is <root>/<project>/.project_config.toml (projectConfigPath). Its
|
|
// shape (core state/project-config.ts, penguin 0.2.17):
|
|
//
|
|
// default_model = { provider = "magpie", model_id = "<model>" }
|
|
// [providers.magpie]
|
|
// base_url = "http://127.0.0.1:3425/v1"
|
|
// client_type = "openai-responses"
|
|
// api_key = "magpie-penguin"
|
|
// [[models]]
|
|
// provider = "magpie"
|
|
// model_id = "<model>"
|
|
// display_name = "…"
|
|
// context_window = …
|
|
// vision = true
|
|
// max_tokens = …
|
|
// [models.pricing]
|
|
// unit = "usd_per_mtok"
|
|
// cache_read = …
|
|
// cache_write = …
|
|
// output = …
|
|
//
|
|
// A model is the pair (provider, model_id); one without a base_url, client
|
|
// type or key of its own takes its group's, [providers.<provider>]. Its
|
|
// requests go through MMSP's client of that type: openai-responses posts
|
|
// base_url + /responses, with its thinking level as reasoning.effort,
|
|
// which openai-chat leaves out. The server reads the file again whenever
|
|
// its mtime moves (ProjectConfigService), so a change needs no restart;
|
|
// its own Models page and `penguin config model …` write the whole file
|
|
// back (smol-toml), every key magpie writes being one it knows. A
|
|
// default_model naming no entry is refused, so the entries go in before
|
|
// the default and out after it.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
// penguinClient is how Penguin speaks to the gateway: OpenAI Responses,
|
|
// the one of its OpenAI clients that carries a thinking level.
|
|
const penguinClient = "openai-responses"
|
|
|
|
// penguinConfig is what magpie reads of a Project's .project_config.toml.
|
|
type penguinConfig struct {
|
|
DefaultModel *penguinRef `toml:"default_model"`
|
|
VisionModel *penguinRef `toml:"vision_model"`
|
|
Models []struct {
|
|
Provider string `toml:"provider"`
|
|
ModelID string `toml:"model_id"`
|
|
DisplayName string `toml:"display_name"`
|
|
} `toml:"models"`
|
|
}
|
|
|
|
type penguinRef struct {
|
|
Provider string `toml:"provider"`
|
|
ModelID string `toml:"model_id"`
|
|
}
|
|
|
|
// value is the pair as the model field spells it: provider/model_id.
|
|
func (r *penguinRef) value() string {
|
|
if r == nil || r.ModelID == "" {
|
|
return ""
|
|
}
|
|
return r.Provider + "/" + r.ModelID
|
|
}
|
|
|
|
// readPenguin is the config at path; a missing file is an empty one, and
|
|
// one that can't be read or parsed is an error.
|
|
func readPenguin(path string) (penguinConfig, error) {
|
|
var c penguinConfig
|
|
raw, err := edit.Read(path)
|
|
if err != nil || len(raw) == 0 {
|
|
return c, err
|
|
}
|
|
err = toml.Unmarshal(raw, &c)
|
|
return c, err
|
|
}
|
|
|
|
// PenguinRoot is PenguinHarness's data root under home: $PENGUIN_HOME,
|
|
// else ~/.penguin/data.
|
|
func PenguinRoot(home string) string { return penguinRoot(here(home)) }
|
|
|
|
// penguinRoot is the data root at a place; a WSL distro's variables
|
|
// aren't read, so it is ~/.penguin/data there.
|
|
func penguinRoot(at place) string {
|
|
if d := at.getenv("PENGUIN_HOME"); d != "" {
|
|
return d
|
|
}
|
|
return filepath.Join(at.home, ".penguin", "data")
|
|
}
|
|
|
|
// penguinProject is the Project the CLI works in: $PENGUIN_PROJECT_ID,
|
|
// where it names a folder of the root, else default_project.
|
|
func penguinProject(at place) string {
|
|
if p := strings.TrimSpace(at.getenv("PENGUIN_PROJECT_ID")); p != "" && p != "." && p != ".." && !strings.ContainsAny(p, `/\`) {
|
|
return p
|
|
}
|
|
return "default_project"
|
|
}
|
|
|
|
func penguin(home string) *Agent { return penguinIn(here(home)) }
|
|
|
|
// penguinIn is PenguinHarness at a place: this machine's home, or a WSL
|
|
// distro's (see wsl.go), its provider naming the gateway as it reaches it
|
|
// from there.
|
|
func penguinIn(at place) *Agent {
|
|
root := penguinRoot(at)
|
|
path := filepath.Join(root, penguinProject(at), ".project_config.toml")
|
|
providerTable := "providers." + magpieID
|
|
stashKey := at.key("penguin.default")
|
|
absentKey := at.key("penguin.before.absent")
|
|
key := func() string { return agentKeyAt("penguin", at.gw()) }
|
|
get := func() string { c, _ := readPenguin(path); return c.DefaultModel.value() }
|
|
writeMagpie := func() error {
|
|
// Penguin keeps its config private (it holds API keys), so a file
|
|
// magpie starts is made the same way.
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
return err
|
|
}
|
|
if err := os.WriteFile(path, nil, 0o600); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
kvs := []edit.KV{
|
|
{Path: "base_url", Value: at.v1()},
|
|
{Path: "client_type", Value: penguinClient},
|
|
{Path: "api_key", Value: key()},
|
|
}
|
|
// A sync with nothing new leaves the file, and its time, alone.
|
|
t, _ := edit.GetTOMLTable(path, providerTable)
|
|
same := t != nil
|
|
for _, kv := range kvs {
|
|
same = same && t[kv.Path] == kv.Value
|
|
}
|
|
if !same {
|
|
if err := kimiProvider(path, providerTable, kvs...); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return edit.SetTOMLArrayTables(path, "models", "provider", magpieID, penguinBlocks(magpieModels("penguin")))
|
|
}
|
|
dropMagpie := func() error {
|
|
if err := edit.SetTOMLArrayTables(path, "models", "provider", magpieID, nil); err != nil {
|
|
return err
|
|
}
|
|
return edit.DelTOMLTable(path, providerTable)
|
|
}
|
|
// setRef points key (default_model, vision_model) at provider/model_id,
|
|
// or takes it out for ""
|
|
setRef := func(k, v string) error {
|
|
if v == "" {
|
|
return edit.DelTOMLTop(path, k)
|
|
}
|
|
p, m, _ := strings.Cut(v, "/")
|
|
// spelled as Penguin's own writer spells it
|
|
return edit.SetTOMLTop(path, edit.KV{Path: k, Value: edit.Raw(
|
|
"{ provider = " + penguinString(p) + ", model_id = " + penguinString(m) + " }")})
|
|
}
|
|
// own reports whether Penguin has the entry v of the user's
|
|
own := func(v string) bool {
|
|
c, err := readPenguin(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
for _, m := range c.Models {
|
|
if m.Provider != magpieID && m.Provider+"/"+m.ModelID == v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
return atomic(&Agent{
|
|
ID: "penguin", Name: "PenguinHarness", Icon: "penguin-color", Bin: "penguin",
|
|
Aliases: []string{"penguin-harness", "penguinharness"}, Spelled: prefixed,
|
|
Dir: filepath.Dir(root), Path: path,
|
|
Sync: func() error {
|
|
t, err := edit.GetTOMLTable(path, providerTable)
|
|
if err != nil || t == nil {
|
|
return err
|
|
}
|
|
return writeMagpie()
|
|
},
|
|
Check: func() string {
|
|
c, err := readPenguin(path)
|
|
if err != nil {
|
|
return err.Error()
|
|
}
|
|
if c.DefaultModel == nil || c.DefaultModel.Provider != magpieID {
|
|
return ""
|
|
}
|
|
found := false
|
|
for _, m := range c.Models {
|
|
found = found || m.Provider == magpieID && m.ModelID == c.DefaultModel.ModelID
|
|
}
|
|
if !found {
|
|
return "PenguinHarness's model " + c.DefaultModel.ModelID + " (.project_config.toml) is gone, so it no longer reaches magpie"
|
|
}
|
|
t, err := edit.GetTOMLTable(path, providerTable)
|
|
if err != nil {
|
|
return err.Error()
|
|
}
|
|
if t == nil {
|
|
return "PenguinHarness's [" + providerTable + "] (.project_config.toml) is gone, so it no longer reaches magpie"
|
|
}
|
|
return wiringOff("PenguinHarness", path, func(k string) (string, bool) { v, ok := t[k]; return v, ok },
|
|
"base_url", at.v1(), "api_key", key())
|
|
},
|
|
// a file magpie made is taken away again once nothing is left in it
|
|
Restore: func() error {
|
|
if unstash(absentKey) != "1" {
|
|
return nil
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var now map[string]any
|
|
if toml.Unmarshal(raw, &now) != nil || len(now) > 0 {
|
|
return nil
|
|
}
|
|
if err := os.Remove(path); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
return err
|
|
}
|
|
return nil
|
|
},
|
|
Fields: []Field{{
|
|
// default_model, as provider/model_id
|
|
Key: "model", Label: "model",
|
|
Get: get,
|
|
Set: func(v string) error {
|
|
cur := get()
|
|
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
|
|
if !usesMagpie(cur) {
|
|
kept := map[string]string{stashKey: cur}
|
|
if t, _ := edit.GetTOMLTable(path, providerTable); t == nil {
|
|
if _, err := os.Stat(path); errors.Is(err, fs.ErrNotExist) {
|
|
kept[absentKey] = "1"
|
|
} else {
|
|
kept[absentKey] = ""
|
|
}
|
|
}
|
|
stash(kept)
|
|
}
|
|
if err := writeMagpie(); err != nil {
|
|
return err
|
|
}
|
|
return setRef("default_model", v)
|
|
}
|
|
if v != "" && !strings.Contains(strings.Trim(v, "/"), "/") {
|
|
return errors.New("expected provider/model_id, got " + strconv.Quote(v))
|
|
}
|
|
if v == "" && usesMagpie(cur) {
|
|
// back to the default the user had, if Penguin still has it
|
|
if was := unstash(stashKey); was != "" && !usesMagpie(was) && own(was) {
|
|
v = was
|
|
}
|
|
}
|
|
if err := setRef("default_model", v); err != nil {
|
|
return err
|
|
}
|
|
// a vision model picked among magpie's in Penguin goes with them
|
|
if c, err := readPenguin(path); err == nil && c.VisionModel != nil && c.VisionModel.Provider == magpieID {
|
|
if err := setRef("vision_model", ""); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return dropMagpie()
|
|
},
|
|
Options: func(cur map[string]string) []Option {
|
|
return append(penguinOwnOptions(path, cur["model"]), viaMagpie("penguin", magpieID+"/")...)
|
|
},
|
|
}},
|
|
}, path)
|
|
}
|
|
|
|
// penguinOwnOptions are the models of the user's own in the Project's
|
|
// config, as provider/model_id, and the current pick when it is none of
|
|
// them.
|
|
func penguinOwnOptions(path, cur string) []Option {
|
|
c, _ := readPenguin(path)
|
|
seen := map[string]bool{}
|
|
var out []Option
|
|
for _, m := range c.Models {
|
|
v := m.Provider + "/" + m.ModelID
|
|
if m.Provider == "" || m.ModelID == "" || m.Provider == magpieID || seen[v] {
|
|
continue
|
|
}
|
|
seen[v] = true
|
|
o := Option{Value: v, Icon: modelIcon(m.Provider, m.ModelID)}
|
|
if m.DisplayName != "" && m.DisplayName != m.ModelID {
|
|
o.Note = m.DisplayName
|
|
}
|
|
out = append(out, o)
|
|
}
|
|
if cur != "" && !seen[cur] && !usesMagpie(cur) {
|
|
out = append([]Option{{Value: cur, Icon: modelIcon("", cur)}}, out...)
|
|
}
|
|
return group("PenguinHarness", out)
|
|
}
|
|
|
|
// penguinBlocks are magpie's models as entries of Penguin's [[models]],
|
|
// each with what its own Models page records for one: the name, context
|
|
// window, cap on a reply, whether it takes images and its list price.
|
|
func penguinBlocks(ms []catalog.Model) []string {
|
|
out := make([]string, 0, len(ms))
|
|
for _, m := range ms {
|
|
var b strings.Builder
|
|
line := func(k, v string) { b.WriteString(k + " = " + v + "\n") }
|
|
b.WriteString("[[models]]\n")
|
|
line("provider", penguinString(magpieID))
|
|
line("model_id", penguinString(m.ID))
|
|
if m.Name != "" && m.Name != m.ID {
|
|
line("display_name", penguinString(m.Name))
|
|
}
|
|
if m.Context > 0 {
|
|
line("context_window", strconv.Itoa(m.Context))
|
|
}
|
|
// Penguin takes a model for one that reads images unless told not
|
|
line("vision", strconv.FormatBool(m.Images))
|
|
if n := maxTokens(m); n > 0 && m.Output > 0 {
|
|
line("max_tokens", strconv.Itoa(n))
|
|
}
|
|
// its three buckets: cache_write is input not read from a cache
|
|
if p := m.Price; p != nil && (p.Input > 0 || p.Output > 0 || p.CacheRead > 0) {
|
|
b.WriteString("\n[models.pricing]\n")
|
|
line("unit", penguinString("usd_per_mtok"))
|
|
line("cache_read", penguinFloat(p.CacheRead))
|
|
line("cache_write", penguinFloat(p.Input))
|
|
line("output", penguinFloat(p.Output))
|
|
}
|
|
out = append(out, b.String())
|
|
}
|
|
return out
|
|
}
|
|
|
|
// penguinString is s as a TOML basic string: JSON's escapes are all TOML's.
|
|
func penguinString(s string) string {
|
|
b, _ := json.Marshal(s)
|
|
return string(b)
|
|
}
|
|
|
|
// penguinFloat is f as a TOML float.
|
|
func penguinFloat(f float64) string {
|
|
s := strconv.FormatFloat(f, 'f', -1, 64)
|
|
if !strings.ContainsAny(s, ".eE") {
|
|
s += ".0"
|
|
}
|
|
return s
|
|
}
|