zaidu259434 on X asked why magpie's CLI/TUI pages don't support Chinese. The GUI had en/zh/zh-TW/ja/de; the TUI had English only. - internal/uilang: the language pick the tray already made (the setting, else the system's: zh-Hant regions to zh-TW, other zh to zh, ja, de) moved out of internal/gui so the TUI asks the same function. The tray calls it unchanged; its tests moved with it. - internal/tui/i18n.go: tr/trf look a string up by its English text in a zh table (464 entries), falling back to the English. zh-TW reads Simplified for now; ja and de stay English. Terms come from the GUI's i18n.js where the same word is there (供应商, 路由, 用量, 会话, 资源库, 分组, 方案 …). - Every user-visible string in the TUI goes through tr/trf; widths that counted runes (Library's name column, sessions' columns) use lipgloss.Width, so 共享指令 is no longer cut to 共…. - Names stay names: agent and model ids, flags, commands, and the field labels magpie/ultracode/harness/opus/sonnet/haiku/fable. TestEveryWordHasZh reads the TUI's source (every tr/trf literal, every hints label, the tables shown through tr, agent field labels) and fails on a word with no zh, on a zh format that takes the English one's arguments badly, and on a zh entry no longer shown. Not changed: the CLI's own output is still English (#6649). Run: - mica (macOS): go vet ./internal/uilang ./internal/gui ./internal/tui; go test -count=1 -timeout 45m ./... was still running when this was pushed (pushed on the coordinator's ask); its result goes on #6649. - wsl-run (Linux): go vet + go test ./internal/tui ./internal/uilang ok; GOOS=windows go vet ./internal/uilang ./internal/gui ./internal/tui ok; GOOS=darwin CGO_ENABLED=0 go vet ./internal/uilang ./internal/tui ok. - Real TUI (go build -tags nogui) in WSL magpie-desktop, sandbox HOME with settings.json {"lang":"zh"}, tmux 150x40: every page (Agents, Providers, Routing, Usage, Sessions, Library), the add-provider picker, sessions stats, the gateway line and footers read in Chinese. The Library row showed 共… before the lipgloss.Width fix, 共享指令 after. - Broken by hand: an added tr("zzz untranslated") and a zh format with its %s dropped each turned TestEveryWordHasZh red. Not tried: zh-TW, ja and de wording (they fall back as above); the TUI on Windows' console.
801 lines
26 KiB
Go
801 lines
26 KiB
Go
package tui
|
|
|
|
// The routing page: the routing groups — their models and the order they
|
|
// go in, how requests spread over them and how long a conversation stays,
|
|
// and the rules that send a turn to one of them first, with the classifier
|
|
// that tells a message's intent. What the app's Routing view does, in the
|
|
// terminal; the rules are typed as magpie group rule add takes them.
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
func (m *model) reloadGroups() {
|
|
m.groups = m.groups[:0]
|
|
var removed []provider.Group
|
|
for _, g := range provider.Groups() {
|
|
if g.Hidden {
|
|
removed = append(removed, g)
|
|
} else {
|
|
m.groups = append(m.groups, g)
|
|
}
|
|
}
|
|
// the found groups removed come last, to be brought back
|
|
m.shown = len(m.groups)
|
|
m.groups = append(m.groups, removed...)
|
|
m.grow = clamp(m.grow, len(m.groups))
|
|
m.provOff = map[string]bool{}
|
|
for _, p := range provider.All() {
|
|
if !p.On() {
|
|
m.provOff[p.ID] = true
|
|
}
|
|
}
|
|
}
|
|
|
|
// memberOff is whether a member of a group is a model of a provider
|
|
// switched off: the group keeps it (one magpie found too, #2671) and the
|
|
// gateway skips it until the provider is on again.
|
|
func (m model) memberOff(id string) bool {
|
|
if provider.IsGroupRef(id) {
|
|
return false
|
|
}
|
|
pid, _, ok := strings.Cut(id, "/")
|
|
return ok && m.provOff[pid]
|
|
}
|
|
|
|
// listedMembers are the members a group's line lists: of a group magpie
|
|
// found, those its providers being on lets it send to (XYenon on Discord:
|
|
// a provider switched off was still listed in it), as every model list
|
|
// leaves a provider switched off out, and how many it left out; all of
|
|
// them when that would leave none. A group of the user's lists what they
|
|
// put in.
|
|
func (m model) listedMembers(g provider.Group) ([]string, int) {
|
|
if !g.Auto {
|
|
return g.Members, 0
|
|
}
|
|
on := slices.DeleteFunc(slices.Clone(g.Members), m.memberOff)
|
|
if len(on) == 0 {
|
|
return g.Members, 0
|
|
}
|
|
return on, len(g.Members) - len(on)
|
|
}
|
|
|
|
var routings = []string{"", provider.Ordered, provider.Rotate, provider.LeastUsed, provider.Pace, provider.Weighted, provider.FreeFirst, provider.Fastest, provider.Cheapest, provider.Drain, provider.FreeExpiring, provider.Manual}
|
|
|
|
func routingName(v string) string {
|
|
switch v {
|
|
case provider.Ordered:
|
|
return tr("in order")
|
|
case provider.Rotate:
|
|
return tr("rotate")
|
|
case provider.LeastUsed:
|
|
return tr("least used")
|
|
case provider.Pace:
|
|
return tr("weekly pace")
|
|
case provider.Weighted:
|
|
return tr("by weight")
|
|
case provider.FreeFirst:
|
|
return tr("free first")
|
|
case provider.Fastest:
|
|
return tr("speed first")
|
|
case provider.Cheapest:
|
|
return tr("cheapest first")
|
|
case provider.Drain:
|
|
return tr("until used up")
|
|
case provider.FreeExpiring:
|
|
return tr("free, soonest to expire")
|
|
case provider.Manual:
|
|
return tr("manual")
|
|
}
|
|
return tr("smart")
|
|
}
|
|
|
|
// groupRouting is how a group routes: a manual one names its pick.
|
|
func groupRouting(g provider.Group) string {
|
|
if g.Routing == provider.Manual {
|
|
return tr("manual") + " → " + g.Picked()
|
|
}
|
|
return routingName(g.Routing)
|
|
}
|
|
|
|
func staysName(v string) string {
|
|
switch v {
|
|
case provider.AffinitySession:
|
|
return tr("stays the session")
|
|
case provider.AffinityTurn:
|
|
return tr("stays a turn")
|
|
case provider.AffinityOff:
|
|
return tr("never stays")
|
|
case provider.AffinityStrict:
|
|
return tr("stays strictly")
|
|
}
|
|
return tr("stays auto")
|
|
}
|
|
|
|
// saveGroup changes one group and saves it: one magpie found becomes the
|
|
// user's.
|
|
func saveGroup(id string, change func(*provider.Group) error, done string) tea.Cmd {
|
|
return func() tea.Msg {
|
|
g, ok := findGroup(id)
|
|
if !ok {
|
|
return flashMsg{text: trf("no group %s", id)}
|
|
}
|
|
if err := change(&g); err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
if err := provider.SaveGroup(g); err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
return flashMsg{text: done, ok: true}
|
|
}
|
|
}
|
|
|
|
// findGroup is a group by its id, without group/ in front.
|
|
func findGroup(id string) (provider.Group, bool) {
|
|
for _, g := range provider.Groups() {
|
|
if g.ID == id && !g.Hidden {
|
|
return g, true
|
|
}
|
|
}
|
|
return provider.Group{}, false
|
|
}
|
|
|
|
func nextRouting(g *provider.Group) error {
|
|
i := max(0, slices.Index(routings, g.Routing))
|
|
g.Routing = routings[(i+1)%len(routings)]
|
|
return nil
|
|
}
|
|
|
|
func nextStays(g *provider.Group) error {
|
|
i := max(0, slices.Index(provider.Affinities, g.Affinity))
|
|
g.Affinity = provider.Affinities[(i+1)%len(provider.Affinities)]
|
|
return nil
|
|
}
|
|
|
|
func (m model) updateGroups(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|
n := len(m.groups)
|
|
key := msg.String()
|
|
if key != "d" {
|
|
m.confirm = ""
|
|
}
|
|
switch key {
|
|
case "j", "down":
|
|
if n > 0 {
|
|
m.grow = (m.grow + 1) % n
|
|
}
|
|
return m, nil
|
|
case "k", "up":
|
|
if n > 0 {
|
|
m.grow = (m.grow + n - 1) % n
|
|
}
|
|
return m, nil
|
|
case "n":
|
|
m.openAsk(ask{crumbs: []string{tr("routing"), tr("new")}, input: newInput(tr("the group's name, e.g. opus-anywhere")),
|
|
hint: tr("then pick its first model; agents choose it as group/<id>"),
|
|
onEnter: func(name string) tea.Cmd {
|
|
return func() tea.Msg { return pickMsg{newGroup: name} }
|
|
}})
|
|
return m, nil
|
|
}
|
|
if n == 0 {
|
|
return m, nil
|
|
}
|
|
g := m.groups[m.grow]
|
|
if g.Hidden {
|
|
if key == "u" {
|
|
return m, func() tea.Msg {
|
|
if err := provider.ShowGroup(g.ID); err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
return flashMsg{text: trf("brought %s back", g.ID), ok: true}
|
|
}
|
|
}
|
|
if key != "r" {
|
|
m.flash, m.flashOK = trf("%s was removed · u brings it back", g.ID), false
|
|
}
|
|
return m, nil
|
|
}
|
|
switch key {
|
|
case "enter", " ":
|
|
m.gid, m.gsel = g.ID, 0
|
|
m.mode = modeGroup
|
|
case "o":
|
|
return m, saveGroup(g.ID, nextRouting, trf("%s routing changed", g.Name))
|
|
case "d":
|
|
if m.confirm != "group/"+g.ID {
|
|
m.confirm = "group/" + g.ID
|
|
m.flash, m.flashOK = trf("press d again to remove %s", g.Name), false
|
|
return m, nil
|
|
}
|
|
m.confirm = ""
|
|
return m, func() tea.Msg {
|
|
if err := provider.DeleteGroup(g.ID); err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
return flashMsg{text: trf("removed %s", g.Name), ok: true}
|
|
}
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// pickMsg opens the model picker for a group: to add to one, or to make
|
|
// one with its first model.
|
|
type pickMsg struct{ group, newGroup string }
|
|
|
|
// memberOptions is every model and group magpie has, but those in skip.
|
|
func memberOptions(skip []string, self string) []agent.Option {
|
|
var out []agent.Option
|
|
// a group is of decision models alone, asked at /v1/systemone, or of
|
|
// models that hold conversations (provider.cleanDecisionGroup): one
|
|
// holding either is offered more of the same, a new one both
|
|
g, _ := findGroup(self)
|
|
decides := self != "" && g.Decides()
|
|
if !decides {
|
|
// every model a group can be made of: those kept out of the lists
|
|
// agents see too (a provider kept for routing groups, a group's
|
|
// with listed=no, #1783), as the Routing view's picker offers
|
|
for _, e := range provider.Served() {
|
|
if slices.Contains(skip, e.ID) || slices.Contains(skip, provider.CanonicalRef(e.ID)) || e.Group != "" && e.Group == self {
|
|
continue
|
|
}
|
|
note := e.Name
|
|
if e.Group != "" {
|
|
note = tr("group") + " · " + e.Name
|
|
}
|
|
out = append(out, agent.Option{Value: e.ID, Note: note})
|
|
}
|
|
}
|
|
if self == "" || decides {
|
|
// those of a provider's picks, as the Routing view's picker offers
|
|
for _, e := range provider.OfferedDeciders(nil) {
|
|
if !slices.Contains(skip, e.ID) {
|
|
out = append(out, agent.Option{Value: e.ID, Note: e.Name + " · " + tr("decision model")})
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (m *model) openMemberPicker(p pickMsg) {
|
|
var skip []string
|
|
crumbs := []string{tr("routing"), p.newGroup, tr("first model")}
|
|
empty := tr("magpie has no models yet · add a provider first")
|
|
if p.group != "" {
|
|
g, _ := findGroup(p.group)
|
|
skip, crumbs = g.Members, []string{tr("routing"), g.Name, tr("add a model")}
|
|
empty = tr("every model magpie has is in it")
|
|
}
|
|
m.pk = picker{
|
|
crumbs: crumbs,
|
|
input: newInput(tr("filter models and groups")),
|
|
items: memberOptions(skip, p.group),
|
|
relist: func() []agent.Option { return memberOptions(skip, p.group) },
|
|
empty: empty,
|
|
onPick: func(id string) tea.Cmd {
|
|
if p.group != "" {
|
|
return saveGroup(p.group, func(g *provider.Group) error {
|
|
g.Members = append(g.Members, id)
|
|
return nil
|
|
}, trf("%s added", id))
|
|
}
|
|
return func() tea.Msg {
|
|
g := provider.Group{Name: p.newGroup, Members: []string{id}}
|
|
if err := provider.SaveGroup(g); err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
return flashMsg{text: trf("made group %s · ↵ on it adds more models", p.newGroup), ok: true}
|
|
}
|
|
},
|
|
}
|
|
m.pk.refilter()
|
|
m.mode = modePick
|
|
if p.group != "" {
|
|
m.back = modeGroup
|
|
}
|
|
}
|
|
|
|
// openClassifier picks the model that tells which intent a message is:
|
|
// a model, or another group whose models are tried in turn.
|
|
func (m *model) openClassifier(g provider.Group) {
|
|
list := func() []agent.Option {
|
|
var items []agent.Option
|
|
for _, e := range provider.Catalog() {
|
|
if e.Group == g.ID {
|
|
continue // it can't classify for itself
|
|
}
|
|
note := e.Name
|
|
if e.ID == g.Classifier {
|
|
note = "● " + tr("now") + " · " + note
|
|
}
|
|
items = append(items, agent.Option{Value: e.ID, Note: note})
|
|
}
|
|
return items
|
|
}
|
|
m.pk = picker{
|
|
crumbs: []string{tr("routing"), g.Name, tr("classifier")},
|
|
input: newInput(tr("filter models · best a small fast one without reasoning")),
|
|
items: list(),
|
|
relist: list,
|
|
empty: tr("magpie has no models yet · add a provider first"),
|
|
onPick: func(id string) tea.Cmd {
|
|
return saveGroup(g.ID, func(g *provider.Group) error { g.Classifier = id; return nil }, trf("%s classifies with %s", g.Name, id))
|
|
},
|
|
}
|
|
m.pk.refilter()
|
|
m.mode = modePick
|
|
m.back = modeGroup
|
|
}
|
|
|
|
// ruleHint is what a rule is typed as.
|
|
const ruleHint = `use=<model>, and any of: tokens=200k · images · effort=on|low|medium|high|xhigh|max · agents=codex,claude
|
|
intent="a quick question" (the group's classifier=<model> tells it) · compact
|
|
time=09:00-18:00 (22:00-08:00 runs past midnight) · days=mon-fri · tz=Asia/Shanghai (this computer's zone if none) · folders=~/work/acme,~/oss · except-folders=~/old · at=<n> for its place
|
|
when="effort is not off" · when="tokens < 20k" · when="agent is not codex" · when=subagent · when="message contains …" · when="system matches ^You are a title"
|
|
use-effort=low|medium|high|xhigh|max: the reasoning the model is asked for, whatever the agent asked
|
|
or pause=<model> time=… days=… agents=… folders=… except-folders=…: the model is left out of the group then, not even a fallback`
|
|
|
|
// openRule asks for a rule: a new one, or rule i (from 0) typed again.
|
|
func (m *model) openRule(g provider.Group, i int) {
|
|
in := newInput("use=<model> tokens=200k …")
|
|
in.CharLimit = 400
|
|
crumbs := []string{tr("routing"), g.Name, tr("new rule")}
|
|
if i >= 0 {
|
|
in.SetValue(g.Rules[i].Line())
|
|
in.CursorEnd()
|
|
crumbs[2] = trf("rule %d", i+1)
|
|
}
|
|
m.openAsk(ask{crumbs: crumbs, input: in, hint: tr(ruleHint) + "\n" + trf("its models: %s", strings.Join(g.Members, ", ")),
|
|
onEnter: func(line string) tea.Cmd {
|
|
done := tr("rule added")
|
|
if i >= 0 {
|
|
done = trf("rule %d changed", i+1)
|
|
}
|
|
return saveGroup(g.ID, func(g *provider.Group) error { return putRule(g, i, line) }, done)
|
|
}})
|
|
m.back = modeGroup
|
|
}
|
|
|
|
// putRule reads a typed rule into the group: in place of rule i, or
|
|
// added when i is -1.
|
|
func putRule(g *provider.Group, i int, line string) error {
|
|
r, at, classifier, err := provider.ParseRule(*g, provider.RuleWords(line))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r.InZoneHere()
|
|
if classifier != "" {
|
|
g.Classifier = classifier
|
|
}
|
|
if i >= 0 && i < len(g.Rules) {
|
|
g.Rules = slices.Delete(g.Rules, i, i+1)
|
|
if at == 0 {
|
|
at = i + 1
|
|
}
|
|
}
|
|
if at == 0 || at > len(g.Rules) {
|
|
g.Rules = append(g.Rules, r)
|
|
} else {
|
|
g.Rules = slices.Insert(g.Rules, at-1, r)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m model) group() (provider.Group, bool) {
|
|
for _, g := range m.groups {
|
|
if g.ID == m.gid && !g.Hidden {
|
|
return g, true
|
|
}
|
|
}
|
|
return provider.Group{}, false
|
|
}
|
|
|
|
// updateGroup is an open group: its models, then its rules, one list.
|
|
func (m model) updateGroup(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|
g, ok := m.group()
|
|
if !ok {
|
|
m.mode = modeList
|
|
return m, nil
|
|
}
|
|
n, nr := len(g.Members), len(g.Rules)
|
|
all := n + nr
|
|
onRule := m.gsel >= n
|
|
key := msg.String()
|
|
if key != "d" {
|
|
m.confirm = ""
|
|
}
|
|
move := func(by int) (tea.Model, tea.Cmd) {
|
|
to := m.gsel + by
|
|
if to < 0 || to >= all || (to >= n) != onRule {
|
|
return m, nil
|
|
}
|
|
from := m.gsel
|
|
m.gsel = to
|
|
if onRule {
|
|
return m, saveGroup(g.ID, func(g *provider.Group) error {
|
|
g.Rules[from-n], g.Rules[to-n] = g.Rules[to-n], g.Rules[from-n]
|
|
return nil
|
|
}, trf("rule %d moved to %d", from-n+1, to-n+1))
|
|
}
|
|
return m, saveGroup(g.ID, func(g *provider.Group) error {
|
|
g.Members[from], g.Members[to] = g.Members[to], g.Members[from]
|
|
return nil
|
|
}, trf("%s moved", g.Members[from]))
|
|
}
|
|
switch key {
|
|
case "esc", "q":
|
|
m.mode = modeList
|
|
case "j", "down":
|
|
if all > 0 {
|
|
m.gsel = (m.gsel + 1) % all
|
|
}
|
|
case "k", "up":
|
|
if all > 0 {
|
|
m.gsel = (m.gsel + all - 1) % all
|
|
}
|
|
case "J", "shift+down":
|
|
return move(1)
|
|
case "K", "shift+up":
|
|
return move(-1)
|
|
case "a":
|
|
m.openMemberPicker(pickMsg{group: g.ID})
|
|
case "n":
|
|
m.openRule(g, -1)
|
|
case "enter", "e":
|
|
if onRule {
|
|
m.openRule(g, m.gsel-n)
|
|
}
|
|
case "c":
|
|
if !slices.ContainsFunc(g.Rules, func(r provider.Rule) bool { return r.Intent != "" }) {
|
|
m.flash, m.flashOK = trf("%s has no rule with an intent to classify for · n adds one", g.Name), false
|
|
return m, nil
|
|
}
|
|
m.openClassifier(g)
|
|
case "E", "F":
|
|
// the model's own effort and fast mode, whatever the agent asks
|
|
if onRule || n == 0 || provider.IsGroupRef(g.Members[m.gsel]) {
|
|
return m, nil
|
|
}
|
|
id := g.Members[m.gsel]
|
|
if key == "F" {
|
|
fast := !g.IsFast(id)
|
|
said := trf("%s not fast", id)
|
|
if fast {
|
|
said = trf("%s fast", id)
|
|
}
|
|
return m, saveGroup(g.ID, func(g *provider.Group) error {
|
|
g.SetMemberFast(id, fast)
|
|
return nil
|
|
}, said)
|
|
}
|
|
model, effort := provider.MemberEffort(id)
|
|
_, acct := provider.MemberAccount(id) // on its own account still
|
|
to := provider.WithMemberAccount(provider.WithMemberEffort(model, nextMemberEffort(effort)), acct)
|
|
return m, saveGroup(g.ID, func(g *provider.Group) error {
|
|
g.RenameMember(id, to)
|
|
return nil
|
|
}, trf("%s set", to))
|
|
case "o":
|
|
return m, saveGroup(g.ID, nextRouting, trf("%s routing changed", g.Name))
|
|
case "s":
|
|
return m, saveGroup(g.ID, nextStays, trf("%s affinity changed", g.Name))
|
|
case "f":
|
|
in := newInput(tr("a tag, e.g. relay"))
|
|
in.SetValue(g.Family)
|
|
m.openAsk(ask{crumbs: []string{tr("routing"), g.Name, tr("family")}, input: in, empty: true,
|
|
hint: tr("agents can be shown only some families: magpie visible <agent> <family>"),
|
|
onEnter: func(v string) tea.Cmd {
|
|
return saveGroup(g.ID, func(g *provider.Group) error { g.Family = v; return nil }, trf("%s family %s", g.Name, dash(v)))
|
|
}})
|
|
m.back = modeGroup
|
|
case "N":
|
|
in := newInput(tr("your own word on it, e.g. office key only"))
|
|
in.SetValue(g.Note)
|
|
m.openAsk(ask{crumbs: []string{tr("routing"), g.Name, tr("note")}, input: in, empty: true,
|
|
hint: tr("shown under its name; empty clears it"),
|
|
onEnter: func(v string) tea.Cmd {
|
|
return saveGroup(g.ID, func(g *provider.Group) error { g.Note = v; return nil }, trf("%s note %s", g.Name, dash(provider.CleanNote(v))))
|
|
}})
|
|
m.back = modeGroup
|
|
case "x":
|
|
in := newInput(tr("tokens, e.g. 200k · empty for its models' own"))
|
|
if g.Context > 0 {
|
|
in.SetValue(fmt.Sprint(g.Context))
|
|
}
|
|
m.openAsk(ask{crumbs: []string{tr("routing"), g.Name, tr("context")}, input: in, empty: true,
|
|
hint: tr("how long a request agents are told the group takes, rather than its largest model's"),
|
|
onEnter: func(v string) tea.Cmd {
|
|
return saveGroup(g.ID, func(g *provider.Group) error {
|
|
if v == "" {
|
|
g.Context = 0
|
|
return nil
|
|
}
|
|
n, err := provider.ParseTokens(v)
|
|
g.Context = n
|
|
return err
|
|
}, trf("%s context %s", g.Name, dash(v)))
|
|
}})
|
|
m.back = modeGroup
|
|
case "l":
|
|
in := newInput(tr("e.g. low,medium,high,xhigh · empty for those its models share"))
|
|
in.SetValue(strings.Join(g.Levels, ","))
|
|
m.openAsk(ask{crumbs: []string{tr("routing"), g.Name, tr("levels")}, input: in, empty: true,
|
|
hint: trf("the reasoning levels agents are offered: %s; a model without the one asked is sent its nearest", strings.Join(provider.Levels, ", ")),
|
|
onEnter: func(v string) tea.Cmd {
|
|
return saveGroup(g.ID, func(g *provider.Group) error {
|
|
var err error
|
|
g.Levels, err = provider.CleanLevels(strings.FieldsFunc(v, func(r rune) bool { return r == ',' || r == ' ' }))
|
|
return err
|
|
}, trf("%s levels %s", g.Name, dash(v)))
|
|
}})
|
|
m.back = modeGroup
|
|
case "R":
|
|
in := newInput(tr("the group's new id"))
|
|
in.SetValue(g.ID)
|
|
in.CursorEnd()
|
|
m.openAsk(ask{crumbs: []string{tr("routing"), g.Name, tr("rename")}, input: in,
|
|
hint: tr("agents pick it as group/<id>: those on it, and the groups that have it in them, follow"),
|
|
onEnter: func(to string) tea.Cmd {
|
|
return func() tea.Msg {
|
|
if _, err := agent.RenameGroup(g.ID, to); err != nil {
|
|
return flashMsg{text: err.Error()}
|
|
}
|
|
return renamedMsg{from: g.ID, to: strings.ToLower(to)}
|
|
}
|
|
}})
|
|
m.back = modeGroup
|
|
case "d":
|
|
if onRule {
|
|
i := m.gsel - n
|
|
if m.confirm != fmt.Sprintf("rule/%d", i) {
|
|
m.confirm = fmt.Sprintf("rule/%d", i)
|
|
m.flash, m.flashOK = trf("press d again to remove rule %d", i+1), false
|
|
return m, nil
|
|
}
|
|
m.confirm = ""
|
|
m.gsel = clamp(m.gsel, all-1)
|
|
return m, saveGroup(g.ID, func(g *provider.Group) error {
|
|
g.Rules = slices.Delete(g.Rules, i, i+1)
|
|
return nil
|
|
}, trf("rule %d removed", i+1))
|
|
}
|
|
if n == 0 {
|
|
return m, nil
|
|
}
|
|
id := g.Members[m.gsel]
|
|
if n == 1 {
|
|
m.flash, m.flashOK = tr("a group needs a model in it · d on the routing list removes the group"), false
|
|
return m, nil
|
|
}
|
|
if m.confirm != "member/"+id {
|
|
m.confirm = "member/" + id
|
|
m.flash, m.flashOK = trf("press d again to take %s out", id), false
|
|
if slices.ContainsFunc(g.Rules, func(r provider.Rule) bool { _, kept := r.Without(id); return !kept }) {
|
|
m.flash += tr(" and its rules")
|
|
}
|
|
return m, nil
|
|
}
|
|
m.confirm = ""
|
|
m.gsel = clamp(m.gsel, all-1)
|
|
return m, saveGroup(g.ID, func(g *provider.Group) error {
|
|
g.Members = slices.DeleteFunc(g.Members, func(x string) bool { return x == id })
|
|
// a rule that sends to others too keeps them
|
|
var rules []provider.Rule
|
|
for _, r := range g.Rules {
|
|
if r, kept := r.Without(id); kept {
|
|
rules = append(rules, r)
|
|
}
|
|
}
|
|
g.Rules = rules
|
|
return nil
|
|
}, trf("%s taken out", id))
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// nextMemberEffort is the effort E sets a member at after effort: the
|
|
// agent's (""), then low to max, then the agent's again.
|
|
func nextMemberEffort(effort string) string {
|
|
cycle := []string{"", "low", "medium", "high", "xhigh", "max"}
|
|
return cycle[(slices.Index(cycle, effort)+1)%len(cycle)]
|
|
}
|
|
|
|
// renamedMsg is a group given another id: the page follows it.
|
|
type renamedMsg struct{ from, to string }
|
|
|
|
func (m model) viewGroups() string {
|
|
var b strings.Builder
|
|
b.WriteString(m.header())
|
|
b.WriteString("\n\n")
|
|
if len(m.groups) == 0 {
|
|
b.WriteString(pad + " " + sMuted.Render(tr("no routing groups yet · n makes one")))
|
|
return b.String()
|
|
}
|
|
nameW, idW := 0, 0
|
|
for _, g := range m.groups {
|
|
nameW, idW = max(nameW, lipgloss.Width(dash(g.Name))), max(idW, lipgloss.Width(g.Ref()))
|
|
}
|
|
visible := max(3, m.h-7)
|
|
start := 0
|
|
if m.grow >= visible {
|
|
start = m.grow - visible + 1
|
|
}
|
|
for i := start; i < min(len(m.groups), start+visible); i++ {
|
|
g := m.groups[i]
|
|
if i == m.shown {
|
|
b.WriteString("\n" + pad + " " + sFaint.Render(tr("removed · found by magpie, taken away: u brings one back")) + "\n")
|
|
}
|
|
marker, name := " ", sName.Render(padRight(dash(g.Name), nameW))
|
|
if g.Hidden {
|
|
name = sFaint.Render(padRight(dash(g.Name), nameW))
|
|
}
|
|
if i == m.grow {
|
|
marker, name = sCursor.Render("▸ "), sNameOn.Render(padRight(dash(g.Name), nameW))
|
|
}
|
|
line := pad + marker + name + " " + sFaint.Render(padRight(g.Ref(), idW))
|
|
if g.Hidden {
|
|
b.WriteString(line + "\n")
|
|
continue
|
|
}
|
|
listed, off := m.listedMembers(g)
|
|
notes := []string{trf("%d model%s", len(listed), plural(len(listed))), groupRouting(g)}
|
|
if off > 0 {
|
|
notes = append(notes, trf("%d switched off", off))
|
|
}
|
|
if g.Affinity != "" {
|
|
notes = append(notes, staysName(g.Affinity))
|
|
}
|
|
if len(g.Rules) > 0 {
|
|
notes = append(notes, trf("%d rule%s", len(g.Rules), plural(len(g.Rules))))
|
|
}
|
|
if g.Family != "" {
|
|
notes = append(notes, trf("family %s", g.Family))
|
|
}
|
|
if g.Auto {
|
|
notes = append(notes, tr("found"))
|
|
}
|
|
members := strings.Join(listed, " → ")
|
|
if len(g.Match) > 0 {
|
|
notes = append(notes, trf("%d pattern%s", len(g.Match), plural(len(g.Match))))
|
|
}
|
|
line += " " + sText.Render(strings.Join(notes, " · "))
|
|
if room := m.w - lipgloss.Width(line) - 4; room > 10 {
|
|
line += " " + sMuted.Render(trunc(members, room))
|
|
}
|
|
b.WriteString(line + "\n")
|
|
}
|
|
return strings.TrimRight(b.String(), "\n")
|
|
}
|
|
|
|
func (m model) viewGroup() string {
|
|
g, _ := m.group()
|
|
var b strings.Builder
|
|
b.WriteString(m.header(tr("routing"), g.Name))
|
|
b.WriteString("\n\n")
|
|
head := []string{g.Ref(), groupRouting(g), staysName(g.Affinity)}
|
|
if g.Context > 0 {
|
|
head = append(head, trf("context %s", fmtTokens(g.Context)))
|
|
}
|
|
if len(g.Levels) > 0 {
|
|
head = append(head, trf("levels %s", strings.Join(g.Levels, "/")))
|
|
}
|
|
if g.Family != "" {
|
|
head = append(head, trf("family %s", g.Family))
|
|
}
|
|
if g.Auto {
|
|
head = append(head, tr("found by magpie: a change makes it yours"))
|
|
}
|
|
b.WriteString(pad + " " + sMuted.Render(strings.Join(head, " · ")) + "\n")
|
|
if g.Note != "" {
|
|
b.WriteString(pad + " " + sText.Render(trunc(g.Note, max(m.w-8, 10))) + "\n")
|
|
}
|
|
b.WriteString("\n")
|
|
b.WriteString(pad + " " + sFaint.Render(tr("models · the first that can take a request gets it")) + "\n")
|
|
hits := provider.PatternHits(g)
|
|
memFrom, memTo, ruleFrom, ruleTo := groupWindow(len(g.Members), len(g.Rules), m.gsel, m.groupRows(g, len(hits)))
|
|
for i := memFrom; i < memTo; i++ {
|
|
id := g.Members[i]
|
|
marker, name := " ", sText.Render(id)
|
|
if i == m.gsel {
|
|
marker, name = sCursor.Render("▸ "), sNameOn.Render(id)
|
|
}
|
|
if g.IsFast(id) {
|
|
name += sMuted.Render(" · " + tr("fast"))
|
|
}
|
|
if slices.Contains(g.Matched, id) {
|
|
name += sMuted.Render(" · " + tr("by pattern"))
|
|
}
|
|
if m.memberOff(id) {
|
|
name += sMuted.Render(" · " + tr("switched off: skipped until it is on again"))
|
|
}
|
|
b.WriteString(pad + marker + sFaint.Render(fmt.Sprintf("%d ", i+1)) + name + "\n")
|
|
}
|
|
// patterns find models in the catalog each time (#766): how many now,
|
|
// and one that finds none said so rather than left an empty list
|
|
if len(hits) > 0 {
|
|
b.WriteString("\n" + pad + " " + sFaint.Render(tr("patterns · every model they match now, after those named")) + "\n")
|
|
for _, h := range hits {
|
|
n := sMuted.Render(" · " + trf("%d model%s", h.Models, plural(h.Models)))
|
|
if h.Models == 0 {
|
|
n = sBad.Render(" · " + tr("matches nothing now"))
|
|
}
|
|
b.WriteString(pad + " " + sText.Render(h.Pattern) + n + "\n")
|
|
}
|
|
}
|
|
b.WriteString("\n" + pad + " " + sFaint.Render(tr("rules · as a turn begins, the first that matches sends it to its model first; a pause leaves its model out while it holds")) + "\n")
|
|
if len(g.Rules) == 0 {
|
|
b.WriteString(pad + " " + sMuted.Render(tr("none · n adds one")) + "\n")
|
|
}
|
|
for i := ruleFrom; i < ruleTo; i++ {
|
|
r := g.Rules[i]
|
|
uses := strings.Join(r.Uses(), ", ") // in order: the next when one can't take it
|
|
marker, use := " ", sText.Render(uses)
|
|
if len(g.Members)+i == m.gsel {
|
|
marker, use = sCursor.Render("▸ "), sNameOn.Render(uses)
|
|
}
|
|
arrow := " → "
|
|
if r.Pause {
|
|
arrow = " → " + tr("pause") + " "
|
|
}
|
|
if r.UseEffort != "" {
|
|
use += sMuted.Render(" "+tr("at")+" ") + sText.Render(r.UseEffort)
|
|
}
|
|
b.WriteString(pad + marker + sFaint.Render(fmt.Sprintf("%d ", i+1)) + sMuted.Render(strings.Join(r.Conditions(), " · ")+arrow) + use + "\n")
|
|
}
|
|
if slices.ContainsFunc(g.Rules, func(r provider.Rule) bool { return r.Intent != "" }) {
|
|
b.WriteString("\n" + pad + " " + sFaint.Render(tr("classifier")+" ") + sText.Render(dash(g.Classifier)) + sFaint.Render(" · "+tr("tells which intent a message is")) + "\n")
|
|
}
|
|
return strings.TrimRight(b.String(), "\n")
|
|
}
|
|
|
|
// groupRows is how many members and rules the group page has room for:
|
|
// the terminal's height less every other line View draws around them —
|
|
// the header, the group's line, the section titles, the patterns, the
|
|
// classifier, and the blank line, the status and the two lines of keys
|
|
// under them, as a short group's page has. A group with
|
|
// more than that would otherwise scroll its header and the row picked
|
|
// off the top and freeze the page, as the library page did (#2220) and
|
|
// the agents page before it (#2428).
|
|
func (m model) groupRows(g provider.Group, hits int) int {
|
|
fixed := 7 + 6 // above the members and between them and the rules; below the body
|
|
if hits > 0 {
|
|
fixed += 2 + hits
|
|
}
|
|
if len(g.Rules) == 0 {
|
|
fixed++
|
|
}
|
|
if g.Note != "" {
|
|
fixed++ // its line under the head
|
|
}
|
|
if slices.ContainsFunc(g.Rules, func(r provider.Rule) bool { return r.Intent != "" }) {
|
|
fixed += 2
|
|
}
|
|
return max(3, m.h-fixed)
|
|
}
|
|
|
|
// groupWindow picks the members [memFrom, memTo) and rules [ruleFrom,
|
|
// ruleTo) to draw in rows lines, the row picked among them: a pick among
|
|
// the members keeps the first rules below it as room allows, a pick
|
|
// among the rules keeps the members' tail above it. Members and rules
|
|
// share one cursor, sel, the rules counted after the members.
|
|
func groupWindow(members, rules, sel, rows int) (memFrom, memTo, ruleFrom, ruleTo int) {
|
|
if members+rules <= rows {
|
|
return 0, members, 0, rules
|
|
}
|
|
if sel < members {
|
|
memFrom = max(0, sel+1-rows)
|
|
memTo = min(members, memFrom+rows)
|
|
return memFrom, memTo, 0, min(rules, rows-(memTo-memFrom))
|
|
}
|
|
ruleTo = min(rules, sel-members+1)
|
|
ruleFrom = max(0, ruleTo-rows)
|
|
return max(0, members-(rows-(ruleTo-ruleFrom))), members, ruleFrom, ruleTo
|
|
}
|