gui, cli: a model id typed with a space in it stays one model, "Opus 5.5" not Opus and 5.5 (#1976)

elonnzhang on #1976: on a Baidu OneAPI relay, typing `Opus 5.5` in the
provider editor's add box (or the add form's Models field) and pressing
Enter added two models, `Opus` and `5.5`. Both read the box with
split(/[,\s]+/), while their hints say "comma separated".

- app.js: one typedIds() for both, split at commas (and line breaks)
  with each id trimmed. chosenIds() (the add box, and an id left in it
  at Save) and the add form's Models field use it.
- CLI siblings, which split the same way: `magpie provider set
  models=` now splits at commas only. A group's models=, models-=,
  fast= and weight= split at commas, and an item with a space that
  names no model the group can have is still split at its spaces, so
  `models=m,b/x a/m` (TestApplyGroupPairs) still works. levels= and
  `magpie visible` (effort levels and family names) are unchanged.
- Tests: the reporter's two GUI assertions in models-add-and-icons
  (from their fork branch). A new model-id-space.test.cjs covers both
  boxes in en, zh, zh-TW, ja and de, Chromium and WebKit, at 1100 and
  420 wide. TestModelIDsKeepTheirSpace covers the CLI. The fork's change
  to provider-add-models.test.cjs is not taken: filling the Models field
  there replaces the picks it holds, so it failed on its own terms.

Run: on the old split, model-id-space fails (4/4 for en and de at 420
in Chromium) and TestModelIDsKeepTheirSpace fails (models = ["Opus"
"5.5" ...]). With the whole-item check forced off, it fails on members.
With the fix: model-id-space 40/40, models-add-and-icons 4/4 and
provider-add-models 4/4 pass. `go test -tags nogui ./internal/cli` is
ok. gofmt is clean, and go vet passes for darwin, linux (-tags nogui)
and windows.
Not tried: a real relay with a spaced model id served end to end.
This commit is contained in:
yetone
2026-10-13 18:16:03 +08:00
parent 47ec2a969f
commit c4e6abf3cc
6 changed files with 194 additions and 8 deletions
+37 -4
View File
@@ -5,6 +5,7 @@ import (
"slices"
"strconv"
"strings"
"unicode"
"github.com/charmbracelet/lipgloss"
@@ -189,6 +190,35 @@ func splitList(v string) []string {
return strings.FieldsFunc(v, func(r rune) bool { return r == ',' || r == ' ' })
}
// splitIDs splits a list of model ids at commas, each trimmed: an id can
// hold a space ("Opus 5.5" on a relay, #1976), which splitList would cut in
// two. An item with a space in it that whole doesn't take (no model the
// group can have) is split at its spaces, as such a list was before; with
// whole nil, never.
func splitIDs(v string, whole func(string) bool) []string {
var out []string
for _, id := range strings.Split(v, ",") {
if id = strings.TrimSpace(id); id == "" {
continue
}
if whole != nil && strings.ContainsFunc(id, unicode.IsSpace) && !whole(id) {
out = append(out, strings.Fields(id)...)
continue
}
out = append(out, id)
}
return out
}
// inGroup: whether v names one of members, as pickMember finds it
func inGroup(members []string) func(string) bool {
return func(v string) bool {
v, _ = provider.MemberFast(v)
_, err := pickMember(members, v)
return err == nil
}
}
// memberResolver spells a model the user typed as the catalog's
// provider/model id: the id itself, or a bare model id one provider
// serves — with the effort it is fixed at after it, when one was typed
@@ -332,7 +362,7 @@ func applyGroupPairs(g *provider.Group, pairs []string, resolve func(string) (st
members := func(v string) ([]string, error) {
var out []string
patterns = nil
for _, m := range splitList(v) {
for _, m := range splitIDs(v, func(m string) bool { _, err := resolve(m); return err == nil }) {
if provider.IsPattern(m) {
ps, err := provider.CleanPatterns([]string{m})
if err != nil {
@@ -390,7 +420,7 @@ func applyGroupPairs(g *provider.Group, pairs []string, resolve func(string) (st
}
}
case "models-", "members-", "model-":
for _, m := range splitList(v) {
for _, m := range splitIDs(v, inGroup(g.Members)) {
if provider.IsPattern(m) {
m = strings.TrimPrefix(strings.TrimSpace(m), "magpie/")
i := slices.Index(g.Match, m)
@@ -472,7 +502,7 @@ func applyGroupPairs(g *provider.Group, pairs []string, resolve func(string) (st
// the members sent in their vendor's fast mode, as they are in
// the group
g.Fast = nil
for _, m := range splitList(v) {
for _, m := range splitIDs(v, inGroup(g.Members)) {
m, _ = provider.MemberFast(m)
id, perr := pickMember(g.Members, m)
if perr != nil {
@@ -486,7 +516,10 @@ func applyGroupPairs(g *provider.Group, pairs []string, resolve func(string) (st
if strings.TrimSpace(v) == "" {
g.Weights = nil
}
for _, mw := range splitList(v) {
for _, mw := range splitIDs(v, func(mw string) bool {
at := strings.LastIndex(mw, "=")
return at > 0 && inGroup(g.Members)(mw[:at])
}) {
at := strings.LastIndex(mw, "=")
if at < 0 {
return fmt.Errorf("weight=<model>=<n>, as weight=%s=3, not %q", mw, mw)