* agents: `magpie omp auto` sets omp's thinking to auto, and the panel's effort slider shows a level it doesn't list as it is omp keeps defaultThinkingLevel at minimal … max or auto (omp picks a level each turn), but magpie offered only the six levels. So `magpie omp auto` found no field offering auto and took it for a model: modelRoles.default became the literal "auto". auto is now offered first, as omp's own picker has it, and kept out of ompEfforts, which a model's thinking levels in models.yml are filtered by. The tray panel's effort slider drew a value that isn't one of its options (an agent with no effort set, a level magpie doesn't list) at the first stop, named as the lowest level, and a touch there wrote that level over it. It now gets a stop of its own, as the window's picker gives the current value, so it reads as it is and a touch leaves it. * agents: the note on omp's thinking picker holds for omp 16.3.5 too omp 16.3.5 takes auto for defaultThinkingLevel as 18.4.4 does (config/settings-schema.ts there), as checked on #339.
270 lines
10 KiB
Go
270 lines
10 KiB
Go
package agent
|
|
|
|
// omp (oh-my-pi, a fork of Pi) keeps its settings in ~/.omp/agent/config.yml
|
|
// (or where ompDir says its variables move it),
|
|
// the model of each role under modelRoles as "provider/model", and providers
|
|
// of the user's own in models.yml beside it. magpie adds itself there as the
|
|
// provider "magpie", keyless (auth: none), with the catalog as its models; a
|
|
// model through magpie is "magpie/<provider>/<model>", which omp matches
|
|
// whole against provider/id. The other roles, the fallback chains and the
|
|
// like may name them as well (ompRefKeys); magpie stays while any does.
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// ompEfforts are the thinking levels omp knows.
|
|
var ompEfforts = []string{"minimal", "low", "medium", "high", "xhigh", "max"}
|
|
|
|
// ompRefKeys are where omp's config names models: each role (a list to try
|
|
// in order, "a,b" or a sequence), the retry fallback chains (model keys and
|
|
// their entries), the models it cycles through (enabledModels, also scoped
|
|
// to paths) and the models of task agents. An entry may end in a thinking
|
|
// level ("magpie/deepseek/pro:max").
|
|
var ompRefKeys = []string{"modelRoles", "retry.fallbackChains", "enabledModels", "task.agentModelOverrides"}
|
|
|
|
// ompRefs calls fn on each model of a value under ompRefKeys, the spaces
|
|
// after a comma kept, and answers the value with fn's answers in place.
|
|
func ompRefs(v string, fn func(string) string) string {
|
|
parts := strings.Split(v, ",")
|
|
for i, p := range parts {
|
|
lead := len(p) - len(strings.TrimLeft(p, " \t"))
|
|
parts[i] = p[:lead] + fn(p[lead:])
|
|
}
|
|
return strings.Join(parts, ",")
|
|
}
|
|
|
|
// ompProfileName is a profile name omp takes (pi-utils dirs.ts,
|
|
// normalizeProfileName); it refuses any other.
|
|
var ompProfileName = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]{0,63}$`)
|
|
|
|
// ompDir is omp's agent folder, found as omp's pi-utils (dirs.ts) finds it:
|
|
// under ~/.omp, or ~/$PI_CONFIG_DIR; a profile's (OMP_PROFILE, else
|
|
// PI_PROFILE; "default" is none) is profiles/<name>/agent there; with none,
|
|
// PI_CODING_AGENT_DIR — the variable Pi reads — moves it, else it is
|
|
// agent there. omp takes that variable as given, without expanding "~".
|
|
func ompDir(home string) string {
|
|
root := filepath.Join(home, ".omp")
|
|
if d := os.Getenv("PI_CONFIG_DIR"); d != "" {
|
|
root = filepath.Join(home, d)
|
|
}
|
|
p, set := os.LookupEnv("OMP_PROFILE")
|
|
if !set {
|
|
p = os.Getenv("PI_PROFILE")
|
|
}
|
|
if p = strings.TrimSpace(p); p != "" && p != "default" && ompProfileName.MatchString(p) && !strings.HasSuffix(p, ".") {
|
|
return filepath.Join(root, "profiles", p, "agent")
|
|
}
|
|
if d := os.Getenv("PI_CODING_AGENT_DIR"); filepath.IsAbs(d) {
|
|
return filepath.Clean(d)
|
|
}
|
|
return filepath.Join(root, "agent")
|
|
}
|
|
|
|
func omp(home string) *Agent {
|
|
dir := ompDir(home)
|
|
// omp reads the .yml and falls back to the .yaml
|
|
pick := func(name string) string {
|
|
yml := filepath.Join(dir, name+".yml")
|
|
if _, err := os.Stat(yml); err != nil {
|
|
if _, err := os.Stat(filepath.Join(dir, name+".yaml")); err == nil {
|
|
return filepath.Join(dir, name+".yaml")
|
|
}
|
|
}
|
|
return yml
|
|
}
|
|
path := pick("config")
|
|
get := func() string { v, _ := edit.GetYAML(path, "modelRoles.default"); return v }
|
|
dropMagpie := func() error {
|
|
// another role, a fallback chain, … may still go through magpie
|
|
used := false
|
|
if err := edit.EditYAMLStrings(path, ompRefKeys, func(v string) string {
|
|
ompRefs(v, func(m string) string { used = used || usesMagpie(m); return m })
|
|
return v
|
|
}); err != nil || used {
|
|
return err
|
|
}
|
|
return edit.DelYAML(pick("models"), "providers."+magpieID)
|
|
}
|
|
writeMagpie := func() error {
|
|
models := pick("models")
|
|
if _, err := os.Stat(models); err != nil {
|
|
// omp moves an older models.json to models.yml only while there is
|
|
// no models.yml, so one magpie writes first has to carry it over
|
|
if err := edit.JSONToYAML(filepath.Join(dir, "models.json"), models); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return edit.SetYAML(models, edit.KV{Path: "providers." + magpieID, Value: ompProvider()})
|
|
}
|
|
return &Agent{
|
|
ID: "omp", Name: "omp", Icon: "omp", Aliases: []string{"oh-my-pi"},
|
|
UA: []string{"oh-my-pi"},
|
|
Bin: "omp", Dir: dir, Path: path,
|
|
Sync: func() error {
|
|
return syncYAML(pick("models"), "providers."+magpieID, func() any { return ompProvider() })
|
|
},
|
|
// a provider renamed takes its models' ids in models.yml with it; a
|
|
// name left on the old one omp would pass over, with a warning
|
|
RenameRefs: func(from, to string) (bool, error) {
|
|
old, now := magpieID+"/"+from+"/", magpieID+"/"+to+"/"
|
|
moved := false
|
|
err := edit.EditYAMLStrings(path, ompRefKeys, func(v string) string {
|
|
return ompRefs(v, func(m string) string {
|
|
if rest, ok := strings.CutPrefix(m, old); ok {
|
|
moved = true
|
|
return now + rest
|
|
}
|
|
return m
|
|
})
|
|
})
|
|
return moved, err
|
|
},
|
|
Notice: func() string {
|
|
if Running(`(^|/)omp( |$)`, `@oh-my-pi/pi-coding-agent`) {
|
|
return "omp reads its settings at start-up — restart open omp sessions to use this."
|
|
}
|
|
return ""
|
|
},
|
|
Check: func() string {
|
|
if !usesMagpie(get()) {
|
|
return ""
|
|
}
|
|
models := pick("models")
|
|
return wiringOff("omp", models, func(k string) (string, bool) { return edit.GetYAML(models, "providers."+magpieID+"."+k) },
|
|
"baseUrl", gatewayV1())
|
|
},
|
|
Fields: []Field{{
|
|
Key: "model", Label: "model",
|
|
Get: get,
|
|
Set: func(v string) error {
|
|
if v == "" {
|
|
if err := edit.DelYAML(path, "modelRoles.default"); err != nil {
|
|
return err
|
|
}
|
|
return dropMagpie()
|
|
}
|
|
if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) {
|
|
if err := writeMagpie(); err != nil {
|
|
return err
|
|
}
|
|
return edit.SetYAML(path, edit.KV{Path: "modelRoles.default", Value: v})
|
|
}
|
|
if err := edit.SetYAML(path, edit.KV{Path: "modelRoles.default", Value: v}); err != nil {
|
|
return err
|
|
}
|
|
return dropMagpie()
|
|
},
|
|
Options: func(cur map[string]string) []Option {
|
|
return append(ownOptions("", cur["model"]), viaMagpie("omp", magpieID+"/")...)
|
|
},
|
|
}, {
|
|
// the thinking level sessions start with, as omp's settings save
|
|
// it; unset omp takes high. auto has omp pick a level each turn:
|
|
// not a level a model lists, so it is offered here and kept out of
|
|
// ompEfforts, which a model's thinking levels are filtered by. First,
|
|
// as omp's own picker has it (16.3.5 and 18.4.4 alike)
|
|
Key: "effort", Label: "thinking",
|
|
Get: func() string { v, _ := edit.GetYAML(path, "defaultThinkingLevel"); return v },
|
|
Set: func(v string) error {
|
|
if v == "" {
|
|
return edit.DelYAML(path, "defaultThinkingLevel")
|
|
}
|
|
return edit.SetYAML(path, edit.KV{Path: "defaultThinkingLevel", Value: v})
|
|
},
|
|
Options: func(map[string]string) []Option { return static(append([]string{"auto"}, ompEfforts...)...) },
|
|
}},
|
|
}
|
|
}
|
|
|
|
type ompModel struct {
|
|
ID string `yaml:"id"`
|
|
Name string `yaml:"name,omitempty"`
|
|
API string `yaml:"api,omitempty"`
|
|
BaseURL string `yaml:"baseUrl,omitempty"`
|
|
Reasoning bool `yaml:"reasoning"`
|
|
Thinking *ompThinking `yaml:"thinking,omitempty"`
|
|
Context int `yaml:"contextWindow,omitempty"`
|
|
MaxTokens int `yaml:"maxTokens,omitempty"`
|
|
Input []string `yaml:"input,omitempty"`
|
|
}
|
|
|
|
type ompThinking struct {
|
|
Mode string `yaml:"mode"`
|
|
Efforts []string `yaml:"efforts"`
|
|
}
|
|
|
|
type ompProviderEntry struct {
|
|
BaseURL string `yaml:"baseUrl"`
|
|
API string `yaml:"api"`
|
|
Auth string `yaml:"auth"`
|
|
// Headers name omp to the gateway: omp 16.x asks with Bun's User-Agent
|
|
// and only a later one with its own (omp/18.4.4), so its requests went
|
|
// to "Bun" in usage and past omp's own rules and stand-ins
|
|
Headers map[string]string `yaml:"headers,omitempty"`
|
|
Models []ompModel `yaml:"models"`
|
|
}
|
|
|
|
// ompProvider is magpie's entry in models.yml. The thinking efforts are the
|
|
// levels omp offers for the model; on Chat it sends them as
|
|
// reasoning_effort.
|
|
//
|
|
// As for Pi, each model is asked on the API its provider speaks natively, so
|
|
// the gateway relays what omp sent as it is instead of translating Chat: a
|
|
// Claude over Chat lost its thinking's signatures between tool turns, as
|
|
// Chat has no place for them. One served on OpenAI's Responses API goes to
|
|
// baseUrl/responses; one on Anthropic's Messages API to the gateway's
|
|
// /v1/messages (omp adds the /v1). That one thinks adaptively when it takes
|
|
// nothing else, else on a budget: omp's anthropic-budget-effort would also
|
|
// send output_config.effort, which Sonnet 4.5 and Haiku 4.5 refuse.
|
|
func ompProvider() ompProviderEntry {
|
|
ms := []ompModel{}
|
|
for _, m := range magpieModels("omp") {
|
|
e := ompModel{ID: m.ID, Name: m.Name, Context: m.Context, MaxTokens: maxTokens(m)}
|
|
mode := "effort"
|
|
switch {
|
|
case slices.Contains(m.APIs, string(provider.Responses)):
|
|
e.API = "openai-responses"
|
|
case slices.Contains(m.APIs, string(provider.Anthropic)):
|
|
e.API, e.BaseURL = "anthropic-messages", gateway.URL()
|
|
mode = "budget"
|
|
if gateway.AdaptiveThinking(m.ID) {
|
|
mode = "anthropic-adaptive"
|
|
}
|
|
}
|
|
// Without input omp takes it from a bundled model its fuzzy id match
|
|
// finds, else text only; a model the source never answered for is
|
|
// left to that guess.
|
|
switch {
|
|
case m.Images:
|
|
e.Input = []string{"text", "image"}
|
|
case m.ImageInput != nil:
|
|
e.Input = []string{"text"}
|
|
}
|
|
var efforts []string
|
|
for _, x := range ompEfforts { // in omp's order
|
|
// a model's efforts stop at xhigh (omp 16.3.5 turns the whole
|
|
// file away over a max): a model whose top is max offers xhigh,
|
|
// which the gateway fits to max
|
|
if x == "xhigh" && slices.Contains(m.Efforts, "max") || x != "max" && slices.Contains(m.Efforts, x) {
|
|
efforts = append(efforts, x)
|
|
}
|
|
}
|
|
if len(efforts) > 0 {
|
|
e.Reasoning = true
|
|
e.Thinking = &ompThinking{Mode: mode, Efforts: efforts}
|
|
}
|
|
ms = append(ms, e)
|
|
}
|
|
return ompProviderEntry{BaseURL: gatewayV1(), API: "openai-completions", Auth: "none",
|
|
Headers: map[string]string{"User-Agent": "omp"}, Models: ms}
|
|
}
|