Files
magpie/internal/agent/omp_protocol.go
T
yetone 255ccdd7ca omp: a Protocol row picks the API magpie's models are asked on (imty42 on X)
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.
2026-11-17 12:46:56 +08:00

110 lines
3.5 KiB
Go

package agent
import (
"encoding/json"
"slices"
"strings"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
)
// The protocol omp asks magpie's models on, picked in omp's row as Pi's is
// (pi_protocol.go; imty42 on X). Unpicked, magpie's provider in models.yml
// is on openai-completions and each model on the API its provider serves
// natively (ompProviderAt): a group whose members don't all take Responses
// stays on Chat (provider.GroupAPI), and an api typed into models.yml was
// written over on the next sync, so an omp that wanted every model on
// Responses had no way to get there. omp takes each of piProtocols for its
// provider and its models (KnownApi in pi-catalog types.ts, the api enum of
// models-config-schema-bundle.ts, omp 18.9.1), and the gateway serves every
// model on each. A pick puts every chat model on it; the provider's api
// typed into models.yml by hand stays (ompKept) and shows, until a pick is
// made after it.
// ompProtocolKey is where the stash keeps the protocol picked for the omp
// whose agent folder is dir at a place, "" for each model's own: each
// profile is an omp of its own.
func ompProtocolKey(at place, dir string) string { return at.key("omp.protocol") + ":" + dir }
// ompProtocol is the protocol picked for the omp at key.
func ompProtocol(key string) string {
if v := stashShared()[key]; slices.Contains(piProtocols, v) {
return v
}
return ""
}
// ompOn is magpie's entry e with every chat model asked on the api on, as
// it is for "". On anthropic-messages a model names the gateway's root, as
// omp adds the /v1 the provider's baseUrl has, and thinks as ompProviderAt
// has a Messages model think; on Chat and Responses it is asked an effort.
// An image model stays on openai-images.
func ompOn(e ompProviderEntry, on string) ompProviderEntry {
if on == "" {
return e
}
gw := strings.TrimSuffix(e.BaseURL, "/v1")
e.API = on
e.Models = slices.Clone(e.Models)
for i := range e.Models {
m := &e.Models[i]
if m.Kind != "" {
continue
}
m.API, m.BaseURL = "", ""
mode := "effort"
if on == "anthropic-messages" {
m.API, m.BaseURL = on, gw
mode = "budget"
if gateway.AdaptiveThinking(m.ID) {
mode = "anthropic-adaptive"
}
}
if m.Thinking != nil {
th := *m.Thinking
th.Mode = mode
m.Thinking = &th
}
}
return e
}
// ompAPIKept is e with the provider's api the user typed into the
// models.yml at path in place of magpie's, as Pi's (pi_api_kept.go): one
// that is none of the APIs magpie has written there (piAPIsWanted, only
// ever openai-completions before there was a record) is theirs. What e
// wants is recorded as magpie's. A model's api is magpie's, the list being
// magpie's whole.
func ompAPIKept(path string, e ompProviderEntry) ompProviderEntry {
want := e.API
if file, _ := edit.GetYAML(path, "providers."+magpieID+".api"); file != "" && file != want && !slices.Contains(piAPIsWanted(path), file) {
e.API = file
}
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{}
}
was, known := rec[piProviderAPI]
if !known {
was = []string{"openai-completions"}
}
if slices.Contains(was, want) && known {
return e
}
if !slices.Contains(was, want) {
was = append(slices.Clone(was), want)
if len(was) > outputsRemembered {
was = was[len(was)-outputsRemembered:]
}
}
rec[piProviderAPI] = was
b, _ := json.Marshal(rec)
m[apisWantedKey(path)] = string(b)
stashWrite(m)
return e
}