Files
magpie/internal/provider/localid.go
T
yetoneandID-VerNe 3f6a101fe1 providers: Images per request in the editor's Advanced, per model, learned from 100milionseek's and Coarena's words (#6849)
Yuu_jiang_ on X (#6849): 100milionseek and Coarena take at most 8 images
a request, counted over the whole conversation, tool results included,
and every turn after the ninth screenshot failed with no way on but
/clear. ID-VerNe's max-images (the commit before) set one number from
the CLI. This builds on it:

- The cap is per provider, and optionally per model: max-images=8, or
  max-images=8,gemini-3-flash=10. The exact model is looked up first,
  then its upstream name, then "*" for all. The provider's cap moves
  with a model's local id like the other per-model limits.
- 100milionseek's "At most 8 images per request (MAX_IMAGES)." and
  Coarena's "A request may carry at most 8 images." are learned as
  Copilot's words are, by provider and model, so the first refusal is
  the last. A user's own number is fitted before any refusal; a learned
  one that is smaller still lowers it.
- Images are counted everywhere, a tool_result's too; the oldest are
  left out first and the latest user turn keeps its own. Only the
  request sent changes; session files are not touched. A request with
  no cap, or within it, goes out byte for byte as it came.
- The provider editor's Advanced (folded unless set) has Images per
  request under Max output, in maximages.js, imported as that field is
  drawn, in every language. A GUI save keeps the value when the field
  isn't drawn. An editor opened and closed is not dirty (the draft
  takes the value only when typed), and the import, like keyrates.js's,
  draws nothing once the editor has closed: before that, a quick close
  threw "Cannot read properties of null (reading 'maxImages')" and
  ('keyPriceRates'), which add-sheet and key-box-once caught.
- Budgets: app.js and the core +354, said in core_budget_test.go.

What was run:
- go test -count=1 -timeout 45m ./... on mote at 618bfc1a9 + these two
  commits: 102 packages ok; internal/tui red on TestEveryWordHasZh
  ("subagent pool", red on main, fixed by 6fbc24a05). After rebasing on
  origin/main c9e8422fa: internal/gui (budgets), tui, agent, testenv,
  gateway and provider: all ok. gofmt clean; GOOS=linux go vet -tags
  nogui ./... and GOOS=windows go vet ./... ok on mica.
- At 618bfc1a9 + these commits, on mote: provider-max-images 60/0,
  add-sheet 6/0, add-button 6/0, key-box-once 20/0, nokey-editor 4/0,
  provider-base-api 4/0, add-sheet-custom 32/0, kimi-subagent-pool
  20/0, sync-signins-except 20/0; old-webkit 4/2 (#6030 headerpresets,
  #5919), i18n-keys 3/1 (#6877), provider-max-output 4/20 (#6209),
  editor-hints 30/10 (the same at origin/main, in #6934).
- The whole GUI suite (1569 files, chromium + webkit, every language)
  on mote at the pre-fix head under another session's suite: 153 files
  red. Each red file rerun at the fixed head and at origin/main
  ad585ccd6 on mica (and mote for the close ones). Red at head only:
  add-button (Escape showed the discard confirm: the dirty draft, fixed),
  add-sheet and key-box-once (the null draft above, fixed); now 3/0,
  6/0, 20/0, provider-max-images 60/0. Every other red file is red the
  same at origin/main, the same subtests: filed with its numbers as
  #6934 (the #6782 class). i18n-keys' ", with an image" is #6877,
  old-webkit #6030/#5919, provider-max-output #6209.

Broken by hand (each red, then put back): the relay words left out
(TestImageCapLearnedFromRelayWords), per-model lookup by upstream name
gone (TestMaxImagesOnAModelListedByItsOwnID), a copy made when nothing
applies (TestImageCapNoneIsTheSameBytes), the learned cap over the
user's (TestMaxImagesStandsOverTheLearnedCap), the GUI save dropping it
(TestProviderSaveKeepsMaxImages), the CLI's empty value
(TestProviderSetMaxImages), the cap not moving with a local id, the JS
parse without its 1000 limit and the field not unfolding Advanced
(provider-max-images), and the draft written on open (provider-max-
images' Escape check, 20 red).

Not tried: no real 100milionseek or Coarena account (the words are the
reporter's literal ones); no GUI run on Windows or Linux; the suite's
GUI half ran under load, so many reds are shared timeouts.

Co-authored-by: ID-VerNe <yuu_seeing@foxmail.com>
2026-11-17 15:39:12 +08:00

403 lines
12 KiB
Go

package provider
import (
"errors"
"fmt"
"slices"
"strings"
"unicode"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// A model can be listed at its provider by an id the user gives it
// (settings' ModelIDs), for a provider that serves one model under ids of
// its own (zeewell on X: deepseek-v4.1-flash-1017 at one relay,
// grok-4.6@500k at another). Everything magpie lists, picks, groups, finds
// routing groups by, limits a gateway key to and records usage under is
// that id; a request for it goes out under the provider's own, through
// the same wire names (settings' Wires) a ModelWires name goes out by.
// It is ModelWires turned round: a wire keeps magpie's id and changes the
// one sent, this keeps the one sent and changes magpie's.
//
// An account's and a plugin's provider take one too (zeewell on X:
// SuperGrok's grok-4.6@500k as grok-4.6, Cursor's, Devin's and
// WorkBuddy's plugin models). The body's model goes out by the wire name
// as at any provider, and what is read of the model by the vendor's id
// is read by the own id: a plugin's model (pluginModel, so its APIs,
// tiers and prices), Claude Code's run on a Claude account (the
// gateway's serveClaudeSubscription), Code Assist's and Antigravity's
// sent id (codeAssistBody, SentNameOnIn), and an account's own list
// (Account.Lists in the gateway's fallback, AccountServes).
// localIDs are the ids the user lists a provider's models by: from the
// provider's own id to the one the user gave, and back.
type localIDs struct {
local map[string]string // own id → the id it is listed by
own map[string]string // the id it is listed by → own id
}
// modelIDsOf are the ids the user gave the provider pid's models.
func modelIDsOf(pid string) localIDs {
return idsIn(settings.SharedModelIDs(), pid)
}
func idsIn(all map[string]string, pid string) localIDs {
var l localIDs
if len(all) == 0 {
return l
}
for k, own := range all {
model, ok := strings.CutPrefix(k, pid+"/")
own = strings.TrimSpace(own)
if !ok || model == "" || own == "" || own == model {
continue
}
if l.local == nil {
l.local, l.own = map[string]string{}, map[string]string{}
}
// two ids given one model (a hand-edited file): the first, in
// order, is its
if had, ok := l.local[own]; ok && had < model {
continue
}
if had, ok := l.local[own]; ok {
delete(l.own, had)
}
l.local[own], l.own[model] = model, own
}
return l
}
func (l localIDs) none() bool { return len(l.own) == 0 }
// ownOf is the provider's own id of the model listed as model.
func (l localIDs) ownOf(model string) string {
if own, ok := l.own[model]; ok {
return own
}
return model
}
// list is ms with each model given an id listed by the id the user gave
// it, and a model of the provider's own id one of those is given left
// out while the model given it is listed (the one id is one model).
func (l localIDs) list(ms []catalog.Model) []catalog.Model {
if l.none() || len(ms) == 0 {
return ms
}
has := make(map[string]bool, len(ms))
for _, m := range ms {
has[m.ID] = true
}
out := make([]catalog.Model, 0, len(ms))
seen := make(map[string]bool, len(ms))
for _, m := range ms {
if local, ok := l.local[m.ID]; ok {
if m.Name == "" || m.Name == m.ID {
m.Name = local
}
m.ID = local
// levels borrowed for the vendor's id are borrowed for the
// one it is listed by too, and for the model the user said
// that one is the same as: a plugin's qfmodel listed as
// claude-opus-5.5 has its levels (axelburks on Discord)
if m.EffortsBorrowed {
if e := borrowedEfforts(local); len(e) > 0 {
m.Efforts = e
}
}
} else if own, ok := l.own[m.ID]; ok && has[own] {
continue // listed by the model given its id
}
if seen[m.ID] {
continue
}
seen[m.ID] = true
out = append(out, m)
}
return out
}
// ids is set (of the provider's own ids) with each given another id by it.
func (l localIDs) ids(set map[string]bool) map[string]bool {
if l.none() {
return set
}
out := make(map[string]bool, len(set))
for id := range set {
if _, ok := l.local[id]; ok {
continue
}
if own, ok := l.own[id]; ok && set[own] {
continue
}
out[id] = true
}
for own, local := range l.local {
if set[own] {
out[local] = true
}
}
return out
}
// takesModelIDs is whether the user can list p's models by ids of their
// own: every provider with a chat list of its own, a key's, an account's
// or a plugin's. A provider that only decides (DecideOnly) lists Jev's
// models and nothing else.
func (p Provider) takesModelIDs() bool { return !p.DecideOnly() }
// TakesModelIDs is whether p's models can be listed by ids the user gives
// them (SetModelID).
func (p Provider) TakesModelIDs() bool { return p.takesModelIDs() }
// ids are the ids the user gave p's models, none for a provider that
// takes none.
func (p Provider) ids() localIDs {
if !p.takesModelIDs() {
return localIDs{}
}
return modelIDsOf(p.ID)
}
// OwnModelID is the provider's own id of its model listed as model: the
// id the vendor is asked for it by, the same as model unless the user
// lists it by another (SetModelID).
func (p Provider) OwnModelID(model string) string { return p.ids().ownOf(model) }
// ListedModelID is the id p lists its model of its own id own by: the one
// the user gave it, else own.
func (p Provider) ListedModelID(own string) string {
if local, ok := p.ids().local[own]; ok {
return local
}
return own
}
// ModelIDs are the ids the user lists p's models by, from each one to the
// provider's own id of it.
func (p Provider) ModelIDs() map[string]string {
out := map[string]string{}
for local, own := range p.ids().own {
out[local] = own
}
return out
}
// rawIDs are the ids of the models the provider itself lists for chat,
// before any is listed by another id, and those the user picked that its
// list doesn't have (typed in, or not fetched yet).
func (p Provider) rawIDs() map[string]bool {
out := map[string]bool{}
for _, m := range p.available() {
out[m.ID] = true
}
for _, id := range p.Models {
if _, given := p.ids().own[id]; !given {
out[id] = true
}
}
return out
}
// checkModelID is why id can't be one a model is listed by, if it can't.
func checkModelID(id string) error {
switch {
case id == "*" || strings.Contains(id, "*"):
return errors.New(`a model's id can't have "*" in it`)
case strings.HasPrefix(id, "-"):
return fmt.Errorf("a model's id can't start with -, not %q", id)
case strings.IndexFunc(id, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }) >= 0:
return fmt.Errorf("a model's id has no spaces in it, not %q", id)
case IsGroupRef(id):
return errors.New("that is a routing group's id, not a model's")
}
return nil
}
// SetModelID lists a provider's model, spelled "provider/model" by the id
// it is listed by now or by the provider's own, by id instead, and says
// which of its models had the id before (replaced) and whether anything
// changed: what
// magpie lists, picks, groups, routes and records it by, while the
// provider is still asked for it by its own id. A blank id, or the
// provider's own, lists it by its own again.
//
// One id is one model of a provider, so an id already given another of its
// models is that model's no more, which it answers (the latest one given
// it wins): that model is listed by its own id again. A model of the
// provider's own id is not listed while another is listed by it, and an
// agent's or a group's ref to the id reaches the model given it.
//
// What the user picked, grouped, set and said of the model goes with it to
// the new id: its picks, a key's models, its context, the routing groups'
// refs and fallbacks, the pickers' lists and every per-model setting — but
// a setting the user already has under the new id, which stays theirs.
// Agents' own files are not rewritten: a "provider/<own id>" they still
// name resolves, and is asked for under that id (Resolve).
func SetModelID(ref, id string) (replaced string, changed bool, err error) {
replaced, changed, err = setModelID(ref, id)
return replaced, changed, touchedIf(changed, err)
}
func setModelID(ref, id string) (string, bool, error) {
p, model, err := splitRef(ref)
if err != nil {
return "", false, err
}
if !p.takesModelIDs() {
return "", false, fmt.Errorf("%s only decides for the routing groups: it has no models of its own to list by another id", p.Name)
}
id = strings.TrimSpace(id)
if id != "" {
if err := checkModelID(id); err != nil {
return "", false, err
}
}
s := settings.Load()
ids := idsIn(s.ModelIDs, p.ID)
raw := p.rawIDs()
// model is the id it is listed by now, or the provider's own
cur, own := model, ids.ownOf(model)
if own == model {
if local, ok := ids.local[model]; ok {
cur = local
} else if !raw[model] {
return "", false, fmt.Errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID)
}
}
if id == "" {
id = own
}
if id == cur {
return "", false, nil
}
var replaced string
if had, ok := ids.own[id]; ok && had != own {
// the latest model given the id has it
replaced = had
}
if s.ModelIDs == nil {
s.ModelIDs = map[string]string{}
}
delete(s.ModelIDs, p.ID+"/"+cur)
for k, v := range s.ModelIDs {
// any other id the model went by (a hand-edited file)
if strings.HasPrefix(k, p.ID+"/") && strings.TrimSpace(v) == own {
delete(s.ModelIDs, k)
}
}
if id != own {
s.ModelIDs[p.ID+"/"+id] = own
}
if len(s.ModelIDs) == 0 {
s.ModelIDs = nil
}
if err := moveModelRefs(p.ID, cur, id, p.Account != nil); err != nil {
return "", false, err
}
s.MoveModel(p.ID+"/"+cur, p.ID+"/"+id)
moveModelLists(&s, p.ID+"/"+cur, p.ID+"/"+id)
if err := settings.Save(s); err != nil {
return "", false, err
}
if ModelMoved != nil {
if err := ModelMoved(p.ID+"/"+cur, p.ID+"/"+id); err != nil {
return replaced, true, err
}
}
return replaced, true, nil
}
// ModelMoved, when set, gives what another package keeps of a provider's
// model ("<provider>/<model>") the id it is listed by now (SetModelID): the
// gateway keys' models (access), which this package can't reach.
var ModelMoved func(from, to string) error
// moveModelRefs gives every ref the providers file keeps to the provider
// pid's model from as to: its picks, those set aside and listed when
// picked, a key's models and its context, the fallbacks and the routing
// groups' refs. A signed-in account's or a plugin's provider is kept
// there only once the user has set something on it (withPicks): with
// none kept (account), only the other providers' refs to it move.
func moveModelRefs(pid, from, to string, account bool) error {
f, err := read()
if err != nil {
return err
}
i := slices.IndexFunc(f.Providers, func(q Provider) bool { return q.ID == pid })
if i < 0 && !account {
return fmt.Errorf("no provider %q", pid)
}
moved := func(ids []string) []string {
if !slices.Contains(ids, from) {
return ids
}
out := make([]string, 0, len(ids))
for _, m := range ids {
if m == from {
m = to
}
if !slices.Contains(out, m) {
out = append(out, m)
}
}
return out
}
if i >= 0 {
q := &f.Providers[i]
q.Models, q.PickedFrom, q.Delisted = moved(q.Models), moved(q.PickedFrom), moved(q.Delisted)
for k, ms := range q.AccountModels {
q.AccountModels[k] = moved(ms)
}
if n, ok := q.Contexts[from]; ok {
if _, has := q.Contexts[to]; !has {
q.Contexts[to] = n
}
delete(q.Contexts, from)
}
if n, ok := q.MaxImages[from]; ok {
if _, has := q.MaxImages[to]; !has {
q.MaxImages[to] = n
}
delete(q.MaxImages, from)
}
}
ref := func(m string) string {
pre, rest := "", m
if r, ok := strings.CutPrefix(m, "magpie/"); ok {
pre, rest = "magpie/", r
}
if rest == pid+"/"+from {
return pre + pid + "/" + to
}
return m
}
mapModelRefs(&f, ref)
return store(f)
}
// moveModelLists gives the pickers' lists of the settings (hidden, picked
// for an agent, ordered, fast, Ultrafast) the model from as to.
func moveModelLists(s *settings.Settings, from, to string) {
for _, m := range []map[string][]string{s.HiddenModels, s.PickedModels, s.OrderedModels, s.FastPicks, s.UltraPicks} {
for k, ids := range m {
if !slices.Contains(ids, from) {
continue
}
out := make([]string, 0, len(ids))
for _, id := range ids {
if id == from {
id = to
}
if !slices.Contains(out, id) {
out = append(out, id)
}
}
m[k] = out
}
}
}