omp's models.yml had magpie's provider on openai-completions with no way
to change it: ompKept treated the provider's api as magpie's, so an api
typed there was written back on the next sync, and omp's row had no
protocol as Pi's has. An auto/* group whose members don't all take
Responses (provider.GroupAPI) could only be asked on Chat.
- omp's row gets the Protocol row Pi has (protocolField, now shared with
Pi's piProtocolField): each model's own (unpicked, as before),
openai-completions, openai-responses or anthropic-messages. omp takes
each for the provider and a model (KnownApi in pi-catalog types.ts and
the api enum of models-config-schema-bundle.ts, omp 18.9.1). The pick
is kept per place and agent folder, so each profile has its own.
`magpie omp protocol openai-responses` sets it from the CLI.
- ompOn puts the provider and every chat model on the pick: Chat and
Responses with no api of their own and thinking mode effort; Messages
at the gateway's root with budget, or anthropic-adaptive for a Claude
that thinks only adaptively. Image models stay on openai-images.
- The provider's api typed into models.yml by hand now stays and shows
as picked (ompAPIKept, the same api:<path>:wanted record as Pi's); a
pick made after it replaces it.
Unchanged: unpicked, Responses-native models and groups whose members
all take Responses already went on openai-responses per model.
Run at this head (3d0cadfce, the same tree; only this message differs),
on mote:
- gofmt -l internal/agent: nothing. go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./...: rc 0. go build, linux
nogui and windows builds: rc 0.
- go test -tags nogui -count=1 -timeout 45m ./... in a sandbox HOME
under suite.lock: rc 0, 103 packages ok, no FAIL.
- Broken by hand: without ompAPIKept, TestOmpProtocolPicked is red
("an api typed by hand: file openai-completions, field \"\"").
- Real omp 18.9.1 in a sandbox HOME against a stand-in server: with the
provider on openai-responses a group model posted to /v1/responses;
a model on anthropic-messages at the root posted to /v1/messages.
Not tried: the GUI suite (no GUI file changed; the Protocol row is the
generic one Pi's uses, shown for any agent with a protocol field), and
omp against the real gateway with a real auto group.
Not changed: a model's api typed by hand in omp's models.yml is still
magpie's (the model list is magpie's whole); site docs for omp.
179 lines
6.3 KiB
Go
179 lines
6.3 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
// The protocol Pi asks magpie's models on, picked in Pi's row
|
|
// (xiaomanweiman on X): T3 Code's Pi puts all 71 of its tools in the
|
|
// context on openai-completions and 3 on openai-responses, loading the
|
|
// rest when asked, and they had to set providers.magpie.api by hand
|
|
// (pi_api_kept.go keeps that). The gateway serves each of pi-ai's APIs
|
|
// below, every model on each, so any of them can be asked for all of
|
|
// magpie's models. Unpicked, it is as before: the provider on
|
|
// openai-completions, each model on the API its provider serves natively
|
|
// (piModelJSON). An api the user typed in models.json still wins, and the
|
|
// row shows it; a pick made after it is the newer word and replaces it.
|
|
|
|
// piProtocols are the APIs of pi-ai's (KnownApi, pi-ai 1.4.0) the gateway
|
|
// serves, as dshAPIs: Chat at /v1/chat/completions, Responses at
|
|
// /v1/responses, Messages at /v1/messages. google-generative-ai is left
|
|
// out, as piAsked leaves it out of Pi's folder.
|
|
var piProtocols = dshAPIs
|
|
|
|
// piProtocolNames are the protocols as the row's picker lists them.
|
|
var piProtocolNames = map[string]string{
|
|
"openai-completions": "OpenAI Chat Completions",
|
|
"openai-responses": "OpenAI Responses",
|
|
"anthropic-messages": "Anthropic Messages",
|
|
}
|
|
|
|
// piProtocolKey is where the stash keeps the protocol picked for Pi at a
|
|
// place, "" for each model's own.
|
|
func piProtocolKey(at place) string { return at.key("pi.protocol") }
|
|
|
|
// piProtocol is the protocol picked for Pi at a place.
|
|
func piProtocol(at place) string {
|
|
v := stashShared()[piProtocolKey(at)]
|
|
if !slices.Contains(piProtocols, v) {
|
|
return ""
|
|
}
|
|
return v
|
|
}
|
|
|
|
// piProviderOn is magpie's provider block for Pi's models.json, every
|
|
// model asked on the api on, or each on its own for "".
|
|
func piProviderOn(agentID string, models []catalog.Model, gw, on string) map[string]any {
|
|
ms := []map[string]any{}
|
|
for _, m := range models {
|
|
ms = append(ms, piModelOn(piAsked(agentID, m), gw, true, on))
|
|
}
|
|
api := "openai-completions"
|
|
if on != "" {
|
|
api = on
|
|
}
|
|
// Pi names its session to a provider only when asked to:
|
|
// sendSessionAffinityHeaders has it send it on Chat
|
|
// (x-session-affinity, session_id) and Anthropic's Messages API
|
|
// (x-session-affinity). On Responses it sends session_id anyway.
|
|
// Without it a turn of Pi's on Chat or Messages reached the gateway
|
|
// with no session: its usage had none and "Record gateway
|
|
// conversations" kept none of it (akic404 on Discord).
|
|
return map[string]any{"name": "magpie", "baseUrl": gw + "/v1", "api": api, "apiKey": keyAt(gw),
|
|
"compat": map[string]any{"sendSessionAffinityHeaders": true}, "models": ms}
|
|
}
|
|
|
|
// piProtocolField is Pi's protocol, the api magpie's provider has in the
|
|
// models.json at modelsPath; write rewrites the block when it is there,
|
|
// and off says magpie doesn't write the block (the pi-magpie extension
|
|
// serves the provider), so there is nothing to pick.
|
|
func piProtocolField(at place, modelsPath string, write func() error, off func() bool) Field {
|
|
return protocolField(at, piProtocolKey(at), modelsPath, func() (string, bool) {
|
|
v, has := edit.GetJSON(modelsPath, "providers."+magpieID+".api")
|
|
if _, block := edit.GetJSON(modelsPath, "providers."+magpieID); !block {
|
|
has = false
|
|
}
|
|
return v, has
|
|
}, write, off)
|
|
}
|
|
|
|
// protocolField is an agent's protocol, picked in its row and kept in the
|
|
// stash at key: the api magpie's provider has in the models file at
|
|
// modelsPath, which fileAPI reads (has: magpie's provider is in the file).
|
|
// write rewrites the provider when it is there, and off says magpie
|
|
// doesn't write it, so there is nothing to pick. Pi's (piProtocolField)
|
|
// and omp's (omp_protocol.go).
|
|
func protocolField(at place, key, modelsPath string, fileAPI func() (string, bool), write func() error, off func() bool) Field {
|
|
picked := func() string {
|
|
if v := stashShared()[key]; slices.Contains(piProtocols, v) {
|
|
return v
|
|
}
|
|
return ""
|
|
}
|
|
return Field{
|
|
Key: "protocol", Label: "protocol", Quiet: true,
|
|
Get: func() string {
|
|
picked := picked()
|
|
if at.cold {
|
|
// a stopped distro's files start it (#3896)
|
|
return picked
|
|
}
|
|
file, _ := fileAPI()
|
|
if file == "" {
|
|
return picked
|
|
}
|
|
want := picked
|
|
if want == "" {
|
|
want = "openai-completions"
|
|
}
|
|
// one the user typed there is what the agent asks on
|
|
if file != want && !slices.Contains(piAPIsWanted(modelsPath), file) {
|
|
return file
|
|
}
|
|
return picked
|
|
},
|
|
Set: func(v string) error {
|
|
if v != "" && !slices.Contains(piProtocols, v) {
|
|
return fmt.Errorf("protocol is one of %v or empty (each model's own), not %q", piProtocols, v)
|
|
}
|
|
// the api in the file now, the user's own included, gives way
|
|
// to the pick
|
|
file, has := fileAPI()
|
|
if file != "" {
|
|
piAPIWanted(modelsPath, file)
|
|
}
|
|
stash(map[string]string{key: v})
|
|
if !has || off() {
|
|
return nil
|
|
}
|
|
return write()
|
|
},
|
|
Options: func(map[string]string) []Option {
|
|
if at.cold || off() {
|
|
return nil
|
|
}
|
|
out := []Option{{Value: "", Label: "Each model's own", Note: "each model on the API its provider serves"}}
|
|
for _, p := range piProtocols {
|
|
out = append(out, Option{Value: p, Label: piProtocolNames[p], Note: p})
|
|
}
|
|
return out
|
|
},
|
|
}
|
|
}
|
|
|
|
// piAPIsWanted are the APIs magpie has handed its provider in the Pi
|
|
// models.json at path (piAPIsKept's record).
|
|
func piAPIsWanted(path string) []string {
|
|
rec := map[string][]string{}
|
|
if s := stashShared()[apisWantedKey(path)]; s != "" && json.Unmarshal([]byte(s), &rec) != nil {
|
|
return nil
|
|
}
|
|
if _, known := rec[piProviderAPI]; !known {
|
|
return []string{"openai-completions"}
|
|
}
|
|
return rec[piProviderAPI]
|
|
}
|
|
|
|
// piAPIWanted takes api, the provider's in the Pi models.json at path
|
|
// as a pick is made, for magpie's alone: the pick replaces it, the
|
|
// user's own included, and an older value magpie wrote that the user
|
|
// types there later is theirs again.
|
|
func piAPIWanted(path, api string) {
|
|
stashMu.Lock()
|
|
defer stashMu.Unlock()
|
|
m := stashLoad()
|
|
rec := map[string][]string{}
|
|
if s := m[apisWantedKey(path)]; s != "" && json.Unmarshal([]byte(s), &rec) != nil {
|
|
rec = map[string][]string{}
|
|
}
|
|
rec[piProviderAPI] = []string{api}
|
|
b, _ := json.Marshal(rec)
|
|
m[apisWantedKey(path)] = string(b)
|
|
stashWrite(m)
|
|
}
|