diff --git a/CHANGELOG.md b/CHANGELOG.md index 16c706e7..759d9cda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,35 @@ is only released once its **exit criterion** is met, not when its code lands. Until v1.0.0 the minor version is the breaking one: configuration formats and Go APIs change between minors without a deprecation period. +## Unreleased + +### Everything that can be changed on a running cluster, from the console + +`sudo llmgw` only watched the cluster and edited the configuration files; +changing a model's replicas still meant `llmgw ctl`. Now Enter on a row opens +what can be done to it, as forms and questions: + +- **Models**: replicas (min, max, idle timeout), class, owner, resources, LoRA + adapters, restart, retry when parked, why it is placed where it is, remove; + A adds one from a definition file. +- **Nodes**: cordon, uncordon, drain, the vLLM and SGLang builds, upgrade its + agent, delete model files nothing uses, remove. +- **Replicas**: restart, replaced or in place. +- **Quotas** (new): capacity quotas, set and removed. +- **Mirror** (new): what it holds and which models use it; download into it, + remove from it. +- **People** (new): accounts — approve, suspend, reinstate, the cluster + dashboard, memberships, limits, a new password, delete, create; API keys — + issue (shown once), limits, revoke; organizations, teams and projects — add, + rename, move, remove, limits for the level and for each member, capacity + quotas, how long conversations and agent notes are kept. +- **Do**: serve a model from Hugging Face, upgrade the cluster, restart every + replica, back up now. + +The people side runs the same commands as `llmgw users`, `keys` and `org`, +against the same store, so their checks and messages are the ones shown. A +sidebar taller than the terminal now scrolls with the current entry. + ## v0.11.1 — 2026-10-03 ### The console's overview no longer spills over the screen diff --git a/internal/cli/tui_actions.go b/internal/cli/tui_actions.go new file mode 100644 index 00000000..e9a614f9 --- /dev/null +++ b/internal/cli/tui_actions.go @@ -0,0 +1,686 @@ +package cli + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/url" + "strconv" + "strings" + "time" + + tea "github.com/charmbracelet/bubbletea" + + "gitea.mynodes.xyz/ilker/llmgw/internal/control" + "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi" + "gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog" + "gitea.mynodes.xyz/ilker/llmgw/internal/release" + "gitea.mynodes.xyz/ilker/llmgw/internal/version" +) + +// What can be done from the console's tables: Enter on a row opens a menu of +// what applies to it — its details first — and each item is a form, a +// question, or done at once, through the same admin API `llmgw ctl` uses. + +type menuItem struct { + label string + act func(m *consoleModel) tea.Cmd +} + +type menuDialog struct { + title string + items []menuItem + sel int +} + +type formField struct { + label, hint string + text textField + options []string + opt int +} + +func (f formField) value() string { + if f.options != nil { + return f.options[f.opt] + } + return strings.TrimSpace(f.text.Value()) +} + +type formDialog struct { + title, note string + fields []formField + focus int + err string + submit func(m *consoleModel, vals []string) (tea.Cmd, error) +} + +type confirmDialog struct { + title, text string + yes func(m *consoleModel) tea.Cmd +} + +// actionDoneMsg is an action's outcome, shown as a dialog. +type actionDoneMsg struct { + title, text string + err error +} + +func textF(label, value, hint string) formField { + t := newTextField() + t.Width = 30 + t.SetValue(value) + return formField{label: label, hint: hint, text: t} +} + +func choiceF(label string, options []string, current string) formField { + f := formField{label: label, options: options} + for i, o := range options { + if o == current { + f.opt = i + } + } + return f +} + +func (m *consoleModel) openForm(f *formDialog) { + if len(f.fields) > 0 { + f.fields[0].text.Focus() + } + m.formD = f +} + +// call runs one admin API request in the background and reports it. +func (m *consoleModel) call(title, method, path string, body any, say func(raw json.RawMessage) string) tea.Cmd { + m.busy = title + c := m.client + return func() tea.Msg { + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + defer cancel() + var raw json.RawMessage + // A change may answer with no body at all. + if err := c.do(ctx, method, path, body, &raw); err != nil && !errors.Is(err, io.EOF) { + return actionDoneMsg{title: title, err: err} + } + text := "Done." + if say != nil { + text = say(raw) + } + return actionDoneMsg{title: title, text: text} + } +} + +func (m consoleModel) actionKey(k tea.KeyMsg) (tea.Model, tea.Cmd, bool) { + switch { + case m.formD != nil: + f := m.formD + switch k.Type { + case tea.KeyEsc: + m.formD = nil + case tea.KeyTab, tea.KeyDown: + f.fields[f.focus].text.Blur() + f.focus = (f.focus + 1) % (len(f.fields) + 1) + if f.focus < len(f.fields) { + f.fields[f.focus].text.Focus() + } + case tea.KeyShiftTab, tea.KeyUp: + if f.focus < len(f.fields) { + f.fields[f.focus].text.Blur() + } + f.focus = (f.focus + len(f.fields)) % (len(f.fields) + 1) + if f.focus < len(f.fields) { + f.fields[f.focus].text.Focus() + } + case tea.KeyLeft, tea.KeyRight: + if f.focus < len(f.fields) && f.fields[f.focus].options != nil { + fl := &f.fields[f.focus] + d := 1 + if k.Type == tea.KeyLeft { + d = len(fl.options) - 1 + } + fl.opt = (fl.opt + d) % len(fl.options) + } + case tea.KeyEnter: + if f.focus < len(f.fields) { + f.fields[f.focus].text.Blur() + f.focus++ + if f.focus < len(f.fields) { + f.fields[f.focus].text.Focus() + } + return m, nil, true + } + vals := make([]string, len(f.fields)) + for i, fl := range f.fields { + vals[i] = fl.value() + } + cmd, err := f.submit(&m, vals) + if err != nil { + f.err = err.Error() + return m, nil, true + } + m.formD = nil + return m, cmd, true + default: + if f.focus < len(f.fields) && f.fields[f.focus].options == nil { + var cmd tea.Cmd + f.fields[f.focus].text, cmd = f.fields[f.focus].text.Update(k) + return m, cmd, true + } + } + return m, nil, true + case m.confirmD != nil: + c := m.confirmD + m.confirmD = nil + if strings.EqualFold(k.String(), "y") { + return m, c.yes(&m), true + } + return m, nil, true + case m.menuD != nil: + d := m.menuD + switch k.String() { + case "up", "k": + d.sel = (d.sel - 1 + len(d.items)) % len(d.items) + case "down", "j", "tab": + d.sel = (d.sel + 1) % len(d.items) + case "esc", "q": + m.menuD = nil + case "enter": + m.menuD = nil + return m, d.items[d.sel].act(&m), true + } + return m, nil, true + } + return m, nil, false +} + +// actionDialog draws whichever of the three is open, or "". +func (m consoleModel) actionDialog() (title, body string) { + switch { + case m.formD != nil: + f := m.formD + var b strings.Builder + if f.note != "" { + b.WriteString(f.note + "\n\n") + } + w := 0 + for _, fl := range f.fields { + w = max(w, len(fl.label)) + } + for i, fl := range f.fields { + label := fmt.Sprintf("%-*s", w, fl.label) + if i == f.focus { + label = fSelected.Render(label) + } + v := fl.text.View() + if fl.options != nil && len(fl.options) > 3 { + v = fmt.Sprintf("◀ %s ▶ %s", sOK.Render(fl.options[fl.opt]), sDim.Render(fmt.Sprintf("%d of %d", fl.opt+1, len(fl.options)))) + } else if fl.options != nil { + var opts []string + for j, o := range fl.options { + if j == fl.opt { + opts = append(opts, "("+sOK.Render("•")+") "+o) + } else { + opts = append(opts, "( ) "+sDim.Render(o)) + } + } + v = strings.Join(opts, " ") + } + fmt.Fprintf(&b, "%s %s\n", label, v) + if fl.hint != "" { + fmt.Fprintf(&b, "%s %s\n", strings.Repeat(" ", w), sDim.Render(fl.hint)) + } + } + b.WriteString("\n" + button(f.focus == len(f.fields), "Apply") + "\n") + if f.err != "" { + b.WriteString("\n" + sBad.Render(f.err) + "\n") + } + b.WriteString("\n" + sDim.Render("Tab next · ←→ choose · Enter on Apply · Esc cancel")) + return f.title, b.String() + case m.confirmD != nil: + return m.confirmD.title, m.confirmD.text + "\n\n" + sDim.Render("Y yes · any other key no") + case m.menuD != nil: + var b strings.Builder + for i, it := range m.menuD.items { + if i == m.menuD.sel { + b.WriteString(fSelected.Render("› "+it.label) + "\n") + } else { + b.WriteString(" " + it.label + "\n") + } + } + b.WriteString("\n" + sDim.Render("↑↓ choose · Enter · Esc close")) + return m.menuD.title, b.String() + } + return "", "" +} + +func details(m *consoleModel) tea.Cmd { + m.detail, m.scroll = true, 0 + return nil +} + +// --- models --- + +func (m consoleModel) modelMenu(md adminapi.ModelView) *menuDialog { + path := "/api/v1/models/" + url.PathEscape(md.Name) + items := []menuItem{ + {"Details", details}, + {"Replicas (scale)…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Scale " + md.Name, + note: "How many replicas it keeps. Max 0 stops it; min 0 with max above loads it on demand.", + fields: []formField{ + textF("min", strconv.Itoa(md.Min), "replicas kept running"), + textF("max", strconv.Itoa(md.Max), "the most it scales to"), + textF("idle timeout", "", "on demand only: unload after this long unused, like 30m"), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + minR, err1 := strconv.Atoi(v[0]) + maxR, err2 := strconv.Atoi(v[1]) + if err1 != nil || err2 != nil || minR < 0 || maxR < minR { + return nil, fmt.Errorf("min and max are whole numbers, max at least min") + } + req := adminapi.ScaleRequest{Min: minR, Max: maxR} + if v[2] != "" { + d, err := time.ParseDuration(v[2]) + if err != nil { + return nil, fmt.Errorf("the idle timeout is a duration, like 30m") + } + req.IdleTimeout = d + } + return m.call("Scale "+md.Name, http.MethodPost, path+"/scale", req, + func(json.RawMessage) string { return fmt.Sprintf("%s keeps %d to %d replicas.", md.Name, minR, maxR) }), nil + }}) + return nil + }}, + {"Class…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Class of " + md.Name, + note: "How much its capacity is worth against another's when there is not enough for both.", + fields: []formField{choiceF("class", []string{"critical", "standard", "opportunistic"}, md.Class)}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + return m.call("Class of "+md.Name, http.MethodPost, path+"/class", map[string]string{"class": v[0]}, + func(json.RawMessage) string { return md.Name + " is " + v[0] + "." }), nil + }}) + return nil + }}, + {"Owner…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Owner of " + md.Name, + note: "Whose capacity quota it counts against. Empty: the cluster's, nobody's.", + fields: []formField{textF("owner", md.Owner, "org:ID, team:ID or project:ID")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + return m.call("Owner of "+md.Name, http.MethodPost, path+"/owner", map[string]string{"owner": v[0]}, nil), nil + }}) + return nil + }}, + {"Resources…", func(m *consoleModel) tea.Cmd { + profiles := md.Profiles + if len(profiles) == 0 { + profiles = []string{""} + } + m.openForm(&formDialog{title: "Resources of " + md.Name, + note: "What one replica reserves of its node.", + fields: []formField{ + choiceF("profile", profiles, profiles[0]), + textF("memory GB", strconv.Itoa(md.MemoryGB), fmt.Sprintf("measured: %.1f GB", md.MeasuredMemoryGB)), + textF("CPU cores", strconv.Itoa(md.CPUCores), "for a profile that computes on them"), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + mem, err1 := strconv.Atoi(v[1]) + cores, err2 := strconv.Atoi(v[2]) + if err1 != nil || err2 != nil || mem < 0 || cores < 0 { + return nil, fmt.Errorf("memory and cores are whole numbers") + } + return m.call("Resources of "+md.Name, http.MethodPost, path+"/resources", + adminapi.ResourcesRequest{Profile: v[0], MemoryGB: mem, CPUCores: cores}, nil), nil + }}) + return nil + }}, + {"LoRA adapters…", func(m *consoleModel) tea.Cmd { + var cur []string + for _, a := range md.Adapters { + s := a.Name + "=" + a.Source + if a.Scale != 0 { + s += "@" + strconv.FormatFloat(a.Scale, 'f', -1, 64) + } + cur = append(cur, s) + } + m.openForm(&formDialog{title: "Adapters of " + md.Name, + note: "Every replica loads every adapter; a request names one as its model.", + fields: []formField{textF("adapters", strings.Join(cur, ", "), "name=source[@scale], separated by commas; empty removes all")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + var next []catalog.Adapter + for _, part := range strings.Split(v[0], ",") { + if part = strings.TrimSpace(part); part == "" { + continue + } + name, rest, ok := strings.Cut(part, "=") + if !ok { + return nil, fmt.Errorf("%q is not name=source", part) + } + a := catalog.Adapter{Name: strings.TrimSpace(name), Source: strings.TrimSpace(rest)} + if src, sc, ok := strings.Cut(a.Source, "@"); ok { + f, err := strconv.ParseFloat(sc, 64) + if err != nil { + return nil, fmt.Errorf("%q: the scale is a number", part) + } + a.Source, a.Scale = src, f + } + next = append(next, a) + } + return m.call("Adapters of "+md.Name, http.MethodPost, path+"/adapters", + adminapi.AdaptersRequest{Adapters: next}, nil), nil + }}) + return nil + }}, + {"Restart its replicas…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Restart " + md.Name, + note: "Each replica is replaced: a new one started before the old is stopped, unless in place.", + fields: []formField{choiceF("how", []string{"one by one", "in place"}, "one by one")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + return m.call("Restart "+md.Name, http.MethodPost, "/api/v1/restart", + adminapi.RestartRequest{Models: []string{md.Name}, InPlace: v[0] == "in place"}, nil), nil + }}) + return nil + }}, + {"Why is it placed where it is", func(m *consoleModel) tea.Cmd { + return m.call("Placement of "+md.Name, http.MethodGet, "/api/v1/explain-placement?model="+url.QueryEscape(md.Name), nil, + func(raw json.RawMessage) string { + var e adminapi.ExplainView + if json.Unmarshal(raw, &e) != nil { + return string(raw) + } + var b strings.Builder + if e.Placed { + b.WriteString("Placed.\n") + } + for _, r := range e.Rejections { + fmt.Fprintf(&b, "%s on %s %s: %s\n", r.Profile, r.Pool, r.Node, r.Reason) + } + if b.Len() == 0 { + return "Nothing to explain: it has what it asked for." + } + return b.String() + }) + }}, + } + if md.Parked { + items = append(items, menuItem{"Try again (it is parked)", func(m *consoleModel) tea.Cmd { + return m.call("Retry "+md.Name, http.MethodPost, path+"/retry", map[string]string{}, nil) + }}) + } + items = append(items, menuItem{"Remove…", func(m *consoleModel) tea.Cmd { + m.confirmD = &confirmDialog{title: "Remove " + md.Name, + text: "Its replicas stop and it leaves the catalog. A model from server.yaml cannot be removed here.", + yes: func(m *consoleModel) tea.Cmd { + return m.call("Remove "+md.Name, http.MethodDelete, path, nil, nil) + }} + return nil + }}) + return &menuDialog{title: md.Name, items: items} +} + +// --- nodes --- + +func (m consoleModel) nodeMenu(n adminapi.NodeView) *menuDialog { + path := "/api/v1/nodes/" + url.PathEscape(n.ID) + state := func(s, label, title string) menuItem { + return menuItem{label, func(m *consoleModel) tea.Cmd { + return m.call(title+" "+n.ID, http.MethodPost, path+"/state", map[string]string{"state": s}, + func(json.RawMessage) string { return n.ID + " is " + s + "." }) + }} + } + items := []menuItem{{"Details", details}} + if n.State == "cordoned" || n.State == "draining" { + items = append(items, state("ready", "Uncordon", "Uncordon")) + } else { + items = append(items, state("cordoned", "Cordon: place nothing new here", "Cordon")) + items = append(items, menuItem{"Drain…", func(m *consoleModel) tea.Cmd { + m.confirmD = &confirmDialog{title: "Drain " + n.ID, + text: "Its replicas move off one at a time, each replacement up before the original stops.", + yes: func(m *consoleModel) tea.Cmd { + return m.call("Drain "+n.ID, http.MethodPost, path+"/state", map[string]string{"state": "draining"}, nil) + }} + return nil + }}) + } + if n.GPUTotal > 0 { + for _, engine := range []string{"vllm", "sglang"} { + items = append(items, menuItem{engine + " build…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: engine + " on " + n.ID, + note: "Which build of " + engine + " this node installs: auto follows its hardware, none installs none.", + fields: []formField{choiceF("build", control.EngineBuilds(engine), n.EngineBuilds[engine])}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + return m.call(engine+" on "+n.ID, http.MethodPost, path+"/engines", + adminapi.NodeEngineRequest{Engine: engine, Build: v[0]}, nil), nil + }}) + return nil + }}) + } + } + items = append(items, + menuItem{"Upgrade its agent…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Upgrade " + n.ID, + note: "It fetches the release for its platform, checks it, swaps it in and restarts; engines keep serving.", + fields: []formField{textF("version", version.Version, "a release, like v0.11.1")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + return m.call("Upgrade "+n.ID, http.MethodPost, path+"/upgrade", adminapi.UpgradeRequest{Version: v[0]}, nil), nil + }}) + return nil + }}, + menuItem{"Delete model files nothing uses", func(m *consoleModel) tea.Cmd { + return m.call("Unused files on "+n.ID, http.MethodPost, "/api/v1/weights/collect", map[string]string{"node": n.ID}, + func(raw json.RawMessage) string { + var rs []struct { + Files int `json:"files"` + Bytes int64 `json:"bytes"` + } + _ = json.Unmarshal(raw, &rs) + var files int + var b int64 + for _, r := range rs { + files, b = files+r.Files, b+r.Bytes + } + return fmt.Sprintf("%d files, %.1f GB freed.", files, float64(b)/(1<<30)) + }) + }}, + menuItem{"Remove from the cluster…", func(m *consoleModel) tea.Cmd { + m.confirmD = &confirmDialog{title: "Remove " + n.ID, + text: "The control plane forgets it. Its replicas are placed elsewhere; the machine is not touched.", + yes: func(m *consoleModel) tea.Cmd { + return m.call("Remove "+n.ID, http.MethodDelete, path, nil, nil) + }} + return nil + }}, + ) + return &menuDialog{title: n.ID, items: items} +} + +// --- replicas --- + +func (m consoleModel) replicaMenu(r adminapi.ReplicaView) *menuDialog { + return &menuDialog{title: r.ID, items: []menuItem{ + {"Restart it…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Restart " + r.ID, + fields: []formField{choiceF("how", []string{"replace", "in place"}, "replace")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + return m.call("Restart "+r.ID, http.MethodPost, "/api/v1/restart", + adminapi.RestartRequest{Replicas: []string{r.ID}, InPlace: v[0] == "in place"}, nil), nil + }}) + return nil + }}, + }} +} + +// --- capacity quotas --- + +func intPtr(s string) (*int, error) { + s = strings.TrimSpace(s) + if s == "" { + return nil, nil + } + n, err := strconv.Atoi(s) + if err != nil || n < 0 { + return nil, fmt.Errorf("%q is not a whole number; leave it empty for none", s) + } + return &n, nil +} + +func ptrText(p *int) string { + if p == nil { + return "" + } + return strconv.Itoa(*p) +} + +func (m *consoleModel) quotaForm(q adminapi.QuotaView) { + scope := textF("scope", q.Scope, "org:ID, team:ID or project:ID") + m.openForm(&formDialog{title: "Capacity quota", + note: "Guaranteed is never taken for someone else's model; at most is the ceiling. Empty is not governed.", + fields: []formField{scope, + textF("guaranteed GPUs", ptrText(q.Guarantee.GPUs), ""), + textF("guaranteed cores", ptrText(q.Guarantee.CPUCores), ""), + textF("guaranteed memory GB", ptrText(q.Guarantee.MemoryGB), ""), + textF("at most GPUs", ptrText(q.Limit.GPUs), ""), + textF("at most cores", ptrText(q.Limit.CPUCores), ""), + textF("at most memory GB", ptrText(q.Limit.MemoryGB), ""), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + req := adminapi.QuotaRequest{Scope: v[0]} + dst := []**int{&req.Guarantee.GPUs, &req.Guarantee.CPUCores, &req.Guarantee.MemoryGB, + &req.Limit.GPUs, &req.Limit.CPUCores, &req.Limit.MemoryGB} + for i, d := range dst { + p, err := intPtr(v[i+1]) + if err != nil { + return nil, err + } + *d = p + } + return m.call("Quota for "+v[0], http.MethodPut, "/api/v1/quotas", req, nil), nil + }}) +} + +func (m consoleModel) quotaMenu(q adminapi.QuotaView) *menuDialog { + return &menuDialog{title: q.Scope, items: []menuItem{ + {"Change…", func(m *consoleModel) tea.Cmd { m.quotaForm(q); return nil }}, + {"Remove…", func(m *consoleModel) tea.Cmd { + m.confirmD = &confirmDialog{title: "Remove the quota on " + q.Scope, + text: "Its models then share the cluster by their class alone.", + yes: func(m *consoleModel) tea.Cmd { + return m.call("Quota on "+q.Scope, http.MethodDelete, "/api/v1/quotas/"+url.PathEscape(q.Scope), nil, nil) + }} + return nil + }}, + }} +} + +type consoleQuotasMsg struct { + rows []adminapi.QuotaView + err error +} + +func (m consoleModel) fetchQuotas() tea.Cmd { + c := m.client + return func() tea.Msg { + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + var rows []adminapi.QuotaView + err := c.get(ctx, "/api/v1/quotas", &rows) + return consoleQuotasMsg{rows, err} + } +} + +func (m consoleModel) quotasPanel(pw int) (string, string, string) { + keys := "→ or Enter open · ↑↓ choose · Q leave" + if m.inPanel { + keys = "↑↓ row · Enter actions · A add · ← back · Q leave" + } + head := "\n " + sDim.Render("Capacity each org, team or project is guaranteed and may at most hold. A to add one.") + "\n\n" + if m.quotasErr != nil { + return "Quotas", head + " " + sBad.Render(m.quotasErr.Error()), keys + } + cell := func(p *int) string { + if p == nil { + return "—" + } + return strconv.Itoa(*p) + } + var rows [][]string + for _, q := range m.quotas { + name := q.Name + if name == "" { + name = q.Scope + } + rows = append(rows, []string{name, + cell(q.Guarantee.GPUs) + " / " + cell(q.Limit.GPUs), + cell(q.Guarantee.CPUCores) + " / " + cell(q.Limit.CPUCores), + cell(q.Guarantee.MemoryGB) + " / " + cell(q.Limit.MemoryGB), + fmt.Sprintf("%d GPU, %d cores, %d GB", q.Holds.GPUs, q.Holds.CPUCores, q.Holds.MemoryGB)}) + } + if len(rows) == 0 { + return "Quotas", head + " " + sDim.Render("None: models share the cluster by their class alone."), keys + } + return "Quotas", head + table([]string{"WHO", "GPUS (GUAR / MAX)", "CORES", "MEMORY GB", "HOLDS"}, rows, m.row, pw, m.inPanel), keys +} + +// --- the cluster --- + +// runAfter closes the screen and runs llmgw with these arguments, showing its +// output; the console comes back after. +func (m *consoleModel) runAfter(args ...string) tea.Cmd { + m.action = consoleAction{kind: "run", targets: append([]string{}, args...)} + return tea.Quit +} + +func (m *consoleModel) doEntry() tea.Cmd { + server := "--server=" + m.client.base + switch m.menu { + case cServe: + m.openForm(&formDialog{title: "Serve a model", + note: "A GGUF repository on Hugging Face, like bartowski/Qwen2.5-3B-Instruct-GGUF.", + fields: []formField{ + textF("repository", "", "OWNER/REPO"), + textF("quantisation", "Q4_K_M", ""), + textF("name", "", "in the catalog; empty takes it from the repository"), + textF("replicas", "1", "0 runs none until it is first asked for"), + textF("context length", "4096", "what one request may use"), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if !strings.Contains(v[0], "/") { + return nil, fmt.Errorf("the repository is OWNER/REPO") + } + args := []string{"ctl", server, "model", "serve", v[0], "--quant=" + v[1], "--replicas=" + v[3], + "--context-length=" + v[4]} + if v[3] == "0" { + args = append(args, "--on-demand") + } + if v[2] != "" { + args = append(args, "--name="+v[2]) + } + return m.runAfter(args...), nil + }}) + case cUpgrade: + m.openForm(&formDialog{title: "Upgrade the cluster", + note: "This machine runs " + version.Version + ".", + fields: []formField{textF("version", "", "like v0.11.2"), choiceF("this machine too", []string{"yes", "no, nodes only"}, "yes")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if !release.Tag.MatchString(v[0]) { + return nil, fmt.Errorf("%q is not a release version (v1.2.3)", v[0]) + } + args := []string{"ctl", server, "upgrade", v[0]} + if v[1] != "yes" { + args = append(args, "--skip-control-plane") + } + return m.runAfter(args...), nil + }}) + case cRestartAll: + m.confirmD = &confirmDialog{title: "Restart every replica", + text: "Each is replaced in turn, the new one up before the old one stops.", + yes: func(m *consoleModel) tea.Cmd { + return m.call("Restart every replica", http.MethodPost, "/api/v1/restart", adminapi.RestartRequest{All: true}, nil) + }} + case cBackup: + return m.runAfter("server", "backup", "--config="+m.file) + } + return nil +} diff --git a/internal/cli/tui_actions_test.go b/internal/cli/tui_actions_test.go new file mode 100644 index 00000000..79e05c8e --- /dev/null +++ b/internal/cli/tui_actions_test.go @@ -0,0 +1,259 @@ +package cli + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "os" + "strings" + "sync" + "testing" + + tea "github.com/charmbracelet/bubbletea" + + "gitea.mynodes.xyz/ilker/llmgw/internal/config" + "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi" + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +type seen struct { + mu sync.Mutex + reqs []string +} + +func (s *seen) last() string { + s.mu.Lock() + defer s.mu.Unlock() + if len(s.reqs) == 0 { + return "" + } + return s.reqs[len(s.reqs)-1] +} + +// adminStub answers every change with 204 and records it as "METHOD path body". +func adminStub(t *testing.T) (*seen, string) { + s := &seen{} + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet { + _, _ = w.Write([]byte("[]")) + return + } + b, _ := io.ReadAll(r.Body) + s.mu.Lock() + s.reqs = append(s.reqs, r.Method+" "+r.URL.Path+" "+strings.TrimSpace(string(b))) + s.mu.Unlock() + w.WriteHeader(http.StatusNoContent) + })) + t.Cleanup(srv.Close) + return s, srv.URL +} + +// pressRun presses keys and runs, one level deep, whatever the last one +// returns that is not a tick, the way the program would. +func pressRun(m tea.Model, keys ...string) tea.Model { + m = press(m, keys[:len(keys)-1]...) + var msg tea.Msg = tea.KeyMsg{Type: tea.KeyEnter} + if k := keys[len(keys)-1]; k != "enter" { + msg = tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(k)} + } + m, cmd := m.Update(msg) + if cmd != nil { + if done, ok := cmd().(actionDoneMsg); ok { + m, _ = m.Update(done) + } + } + return m +} + +func consoleWith(t *testing.T, menu int) (consoleModel, *seen) { + s, url := adminStub(t) + m := newConsoleModel(url, "/nonexistent/server.yaml") + m.loaded, m.menu, m.inPanel = true, menu, true + m.data.models = []adminapi.ModelView{{Name: "qwen", Min: 1, Max: 1, Class: "standard", Profiles: []string{"cpu"}}} + m.data.nodes = []adminapi.NodeView{{ID: "n1", State: "ready"}} + m.data.replicas = []adminapi.ReplicaView{{ID: "qwen-1", Model: "qwen"}} + return m, s +} + +func TestAModelsReplicasAreSetFromItsRow(t *testing.T) { + m, s := consoleWith(t, cModels) + var tm tea.Model = m + tm = press(tm, "enter") + if !strings.Contains(tm.View(), "Replicas (scale)") { + t.Fatalf("no action menu:\n%s", tm.View()) + } + tm = press(tm, "down", "enter") + f := tm.(consoleModel).formD + if f == nil { + t.Fatal("no form") + } + f.fields[1].text.SetValue("3") + // A max below the min is refused in the form, and nothing is sent. + f.fields[0].text.SetValue("5") + tm = press(tm, "tab", "tab", "tab", "enter") + if f.err == "" || s.last() != "" { + t.Fatalf("a max below the min went through: %q %q", f.err, s.last()) + } + f.fields[0].text.SetValue("2") + tm = pressRun(tm, "enter") + var body adminapi.ScaleRequest + got := s.last() + if !strings.HasPrefix(got, "POST /api/v1/models/qwen/scale ") || + json.Unmarshal([]byte(strings.SplitN(got, " ", 3)[2]), &body) != nil || body.Min != 2 || body.Max != 3 { + t.Fatalf("sent %q", got) + } + if !strings.Contains(tm.View(), "keeps 2 to 3") { + t.Errorf("the outcome was not shown:\n%s", tm.View()) + } +} + +func TestANodeIsCordonedAndAModelRemovedOnlyWhenConfirmed(t *testing.T) { + m, s := consoleWith(t, cNodes) + var tm tea.Model = m + tm = pressRun(tm, "enter", "down", "enter") + if got := s.last(); got != `POST /api/v1/nodes/n1/state {"state":"cordoned"}` { + t.Fatalf("sent %q", got) + } + + m, s = consoleWith(t, cModels) + tm = m + tm = press(tm, "enter") + n := len(tm.(consoleModel).menuD.items) + keys := []string{} + for range n - 1 { + keys = append(keys, "down") + } + tm = press(tm, append(keys, "enter")...) + if tm.(consoleModel).confirmD == nil { + t.Fatal("removing did not ask first") + } + tm = press(tm, "n") + if s.last() != "" { + t.Fatalf("removed though the answer was no: %q", s.last()) + } + tm = press(tm, "enter") + tm = press(tm, append(keys, "enter")...) + _ = pressRun(tm, "y") + if got := s.last(); got != "DELETE /api/v1/models/qwen " { + t.Fatalf("sent %q", got) + } +} + +func TestAQuotaIsAddedWithOnlyWhatIsGiven(t *testing.T) { + m, s := consoleWith(t, cQuotas) + var tm tea.Model = press(m, "a") + f := tm.(consoleModel).formD + f.fields[0].text.SetValue("team:acme/ml") + f.fields[4].text.SetValue("2") + for range len(f.fields) { + tm = press(tm, "tab") + } + _ = pressRun(tm, "enter") + if got := s.last(); got != `PUT /api/v1/quotas {"scope":"team:acme/ml","guarantee":{},"limit":{"gpus":2}}` { + t.Fatalf("sent %q", got) + } +} + +func TestPeopleAreManagedFromTheConsoleAgainstItsStore(t *testing.T) { + dir := t.TempDir() + cfgPath := dir + "/server.yaml" + yaml := "auth:\n store: " + dir + "/llmgw.db\n" + if err := os.WriteFile(cfgPath, []byte(yaml), 0o600); err != nil { + t.Fatal(err) + } + if _, err := config.Load(cfgPath); err != nil { + t.Fatalf("the test's configuration: %v", err) + } + m := newConsoleModel("127.0.0.1:1", cfgPath) + defer m.st.close() + m.loaded, m.inPanel = true, true + + refresh := func(tm tea.Model) tea.Model { + cm := tm.(consoleModel) + msg := cm.fetchPeople()() + tm, _ = tm.Update(msg) + return tm + } + submit := func(tm tea.Model, vals ...string) tea.Model { + f := tm.(consoleModel).formD + if f == nil { + t.Fatal("no form") + } + for i, v := range vals { + f.fields[i].text.SetValue(v) + } + f.focus = len(f.fields) + tm = pressRun(tm, "enter") + if v := tm.View(); !strings.Contains(v, "✓") { + t.Fatalf("did not work:\n%s", v) + } + return refresh(press(tm, "x")) + } + + m.menu = cOrgs + var tm tea.Model = m + tm = submit(press(tm, "a"), "Acme") + // Into the organization: a team, then a project in it. + tm = submit(press(tm, "enter", "enter"), "ml") + cm := tm.(consoleModel) + if len(cm.people.orgs) != 2 || cm.people.orgs[1].path != "Acme/ml" { + t.Fatalf("tree: %+v", cm.people.orgs) + } + tm = submit(press(tm, "down", "enter", "enter"), "training") + + cm = tm.(consoleModel) + cm.menu, cm.row = cAccounts, 0 + tm = submit(press(cm, "a"), "ayse@example.com", "a-long-s3cret-pass", "Acme/ml/training") + cm = tm.(consoleModel) + if len(cm.people.users) != 1 || cm.people.users[0].State != store.UserActive || + !strings.Contains(strings.Join(cm.people.where[cm.people.users[0].ID], ","), "training") { + t.Fatalf("account: %+v %v", cm.people.users, cm.people.where) + } + + cm.menu, cm.row = cKeys, 0 + tm = press(cm, "a") + f := tm.(consoleModel).formD + f.fields[0].text.SetValue("ci") + f.fields[1].text.SetValue("Acme/ml/training") + f.focus = len(f.fields) + tm = pressRun(tm, "enter") + if v := tm.View(); !strings.Contains(v, "sk-") { + t.Fatalf("the new key was not shown:\n%s", v) + } + tm = refresh(press(tm, "x")) + if n := len(tm.(consoleModel).people.keys); n != 1 { + t.Fatalf("%d keys", n) + } + // Limits on the project, read back in its row. + cm = tm.(consoleModel) + cm.menu, cm.row = cOrgs, 2 + tm = press(cm, "enter") + items := tm.(consoleModel).menuD.items + i := 0 + for ; items[i].label != "Limits…"; i++ { + } + keys := []string{} + for range i { + keys = append(keys, "down") + } + tm = submit(press(tm, append(keys, "enter")...), "60") + if c := limitsCell(tm.(consoleModel).people.limits[store.LimitOwner{Scope: store.ScopeProject, + ID: tm.(consoleModel).people.orgs[2].id}.CounterKey()]); c != "60 rpm" { + t.Fatalf("limits: %q", c) + } +} + +func TestTheSidebarFitsAShortScreen(t *testing.T) { + m, _ := consoleWith(t, cOrgs) + m.inPanel = false + for _, h := range []int{20, 30, 50} { + m.height = h + if n := len(strings.Split(m.View(), "\n")); n > h { + t.Errorf("height %d: %d lines drawn", h, n) + } + if !strings.Contains(m.View(), "Organizations") { + t.Errorf("height %d: the current entry is not shown", h) + } + } +} diff --git a/internal/cli/tui_console.go b/internal/cli/tui_console.go index 25e104f1..b93c0e20 100644 --- a/internal/cli/tui_console.go +++ b/internal/cli/tui_console.go @@ -35,21 +35,31 @@ const ( cModels cReplicas cUsage + cQuotas + cMirror cLogs + cAccounts + cKeys + cOrgs cSettings cAddNode cReplicate + cServe + cUpgrade + cRestartAll + cBackup cLeave cCount ) -var consoleLabels = [cCount]string{"Overview", "Nodes", "GPUs", "Models", "Replicas", "Usage", "Logs", - "server.yaml", "Add a node", "Replicate the store", "Leave"} +var consoleLabels = [cCount]string{"Overview", "Nodes", "GPUs", "Models", "Replicas", "Usage", "Quotas", "Mirror", "Logs", "Accounts", "API keys", "Organizations", + "server.yaml", "Add a node", "Replicate the store", "Serve a model", "Upgrade the cluster", + "Restart all replicas", "Back up now", "Leave"} // consoleAction is what is done once the screen closes: each runs its own // screen, and the console comes back after. type consoleAction struct { - kind string // "", "node", "patroni", "restart" + kind string // "", "node", "patroni", "restart", "run" (llmgw with targets as its arguments) targets []string } @@ -127,12 +137,27 @@ type consoleModel struct { editKind settings.Kind action consoleAction + + // What can be done to a row: a menu, a form, a question, and the request + // under way. + menuD *menuDialog + formD *formDialog + confirmD *confirmDialog + busy string + + quotas []adminapi.QuotaView + quotasErr error + + st *storeHolder + people peopleData + mirror consoleMirrorMsg } var usageGroupings = []string{"team", "project", "org"} func newConsoleModel(addr, file string) consoleModel { - m := consoleModel{client: newCtlClient(addr), spin: newSpinner(), width: 100, height: 30, file: file} + m := consoleModel{client: newCtlClient(addr), spin: newSpinner(), width: 100, height: 30, file: file, + st: &storeHolder{file: file}} m.node = strings.HasSuffix(file, "agent.yaml") if m.node { m.menu = cSettings @@ -235,6 +260,15 @@ func (m consoleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.menu == cUsage { cmds = append(cmds, m.fetchUsage()) } + if m.menu == cQuotas { + cmds = append(cmds, m.fetchQuotas()) + } + if m.menu == cMirror { + cmds = append(cmds, m.fetchMirror()) + } + if m.peopleView() { + cmds = append(cmds, m.fetchPeople()) + } if m.menu == cLogs && m.detail && m.logs.node != "" { cmds = append(cmds, m.fetchLogs(m.logs.node)) } @@ -250,6 +284,22 @@ func (m consoleModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.usage, m.usageErr = msg.rows, msg.err case consoleLogsMsg: m.logs = msg + case consoleMirrorMsg: + m.mirror = msg + case consolePeopleMsg: + m.people = peopleData(msg) + case consoleQuotasMsg: + m.quotas, m.quotasErr = msg.rows, msg.err + case actionDoneMsg: + m.busy = "" + m.dialog, m.dialogTitle = "error", msg.title + if msg.err != nil { + m.dialogText = sBad.Render("✗ ") + msg.err.Error() + } else { + m.dialogText = sOK.Render("✓ ") + msg.text + } + m.dialogText += "\n\n" + sDim.Render("any key") + return m, tea.Batch(m.fetch(), m.onEnter()) case tea.KeyMsg: if msg.Type == tea.KeyCtrlC { return m, tea.Quit @@ -269,9 +319,15 @@ func (m consoleModel) entryOK(i int) bool { func (m consoleModel) storeReplicated() bool { return len(m.data.sum.Store) > 0 } func (m consoleModel) key(k tea.KeyMsg) (tea.Model, tea.Cmd) { + if m.busy != "" { + return m, nil + } if m.dialog != "" { return m.dialogKey(k) } + if mm, cmd, ok := m.actionKey(k); ok { + return mm, cmd + } switch k.Type { case tea.KeyPgDown: m.scroll += max(1, m.height-8) @@ -310,6 +366,8 @@ func (m consoleModel) key(k tea.KeyMsg) (tea.Model, tea.Cmd) { return m.ask("Replicate the store", "user@address of database machine", 2) case cLeave: return m.leave() + case cServe, cUpgrade, cRestartAll, cBackup: + return m, m.doEntry() case cOverview: return m, nil } @@ -331,13 +389,46 @@ func (m consoleModel) key(k tea.KeyMsg) (tea.Model, tea.Cmd) { m.row++ } case "enter": - if !m.detail && n > 0 && m.menu != cGPUs && m.menu != cReplicas && m.menu != cUsage { + if m.detail || n == 0 || m.row >= n { + break + } + switch m.menu { + case cLogs: m.detail, m.scroll = true, 0 - if m.menu == cLogs { - node := m.data.nodes[m.row].ID - m.logs = consoleLogsMsg{node: node} - return m, m.fetchLogs(node) - } + node := m.data.nodes[m.row].ID + m.logs = consoleLogsMsg{node: node} + return m, m.fetchLogs(node) + case cNodes: + m.menuD = m.nodeMenu(m.data.nodes[m.row]) + case cModels: + m.menuD = m.modelMenu(m.data.models[m.row]) + case cReplicas: + m.menuD = m.replicaMenu(m.data.replicas[m.row]) + case cQuotas: + m.menuD = m.quotaMenu(m.quotas[m.row]) + case cMirror: + m.menuD = m.mirrorMenu(m.mirror.res.Models[m.row]) + case cAccounts: + m.menuD = m.userMenu(m.people.users[m.row]) + case cKeys: + m.menuD = m.keyMenu(m.people.keys[m.row]) + case cOrgs: + m.menuD = m.orgMenu(m.people.orgs[m.row]) + } + case "a": + switch m.menu { + case cQuotas: + m.quotaForm(adminapi.QuotaView{}) + case cModels: + m.addModelForm() + case cMirror: + m.mirrorFetchForm() + case cAccounts: + m.createUserForm() + case cKeys: + m.createKeyForm() + case cOrgs: + m.createOrgForm() } case "right", "l": if m.menu == cUsage { @@ -358,12 +449,24 @@ func (m consoleModel) key(k tea.KeyMsg) (tea.Model, tea.Cmd) { // onEnter loads what a view needs when it is chosen. func (m consoleModel) onEnter() tea.Cmd { - if m.menu == cUsage { + switch m.menu { + case cUsage: return m.fetchUsage() + case cQuotas: + return m.fetchQuotas() + case cMirror: + return m.fetchMirror() + case cAccounts, cKeys, cOrgs: + // The quotas too, for an organization's quota form. + return tea.Batch(m.fetchPeople(), m.fetchQuotas()) } return nil } +func (m consoleModel) peopleView() bool { + return m.menu == cAccounts || m.menu == cKeys || m.menu == cOrgs +} + func (m consoleModel) leave() (tea.Model, tea.Cmd) { if m.doc != nil && m.doc.Changed > 0 { m.dialog, m.dialogTitle = "confirm-leave", "Unsaved changes" @@ -659,6 +762,16 @@ func (m consoleModel) rowCount() int { return len(m.data.replicas) case cUsage: return len(m.usage) + case cQuotas: + return len(m.quotas) + case cMirror: + return len(m.mirror.res.Models) + case cAccounts: + return len(m.people.users) + case cKeys: + return len(m.people.keys) + case cOrgs: + return len(m.people.orgs) } return 0 } @@ -674,6 +787,8 @@ func (m consoleModel) View() string { continue } switch i { + case cAccounts: + nav = append(nav, navItem{Label: "", Heading: true}, navItem{Label: "People", Heading: true}) case cSettings: nav = append(nav, navItem{Label: "", Heading: true}, navItem{Label: "Settings", Heading: true}) case cAddNode, cLeave: @@ -718,6 +833,10 @@ func (m consoleModel) View() string { } f.Dialog = q + "\n\n" + m.prompt.View() + "\n\n" + sDim.Render("Enter · Esc cancel") } + } else if m.busy != "" { + f.DialogTitle, f.Dialog = m.busy, m.spin.View()+" asking the control plane…" + } else if t, d := m.actionDialog(); d != "" { + f.DialogTitle, f.Dialog = t, d } return f.render() } @@ -725,7 +844,7 @@ func (m consoleModel) View() string { // panel is the right-hand side for the entry chosen. func (m consoleModel) panel() (title, body, keys string) { pw := max(40, m.width-navWidth-8) - rowKeys := "↑↓ row · Enter detail · ← back · PgUp/PgDn · Q leave" + rowKeys := "↑↓ row · Enter actions · ← back · PgUp/PgDn · Q leave" if !m.inPanel { keys = "↑↓ choose · → or Enter open · Q leave · refreshed every three seconds" } @@ -809,7 +928,7 @@ func (m consoleModel) panel() (title, body, keys string) { strings.Join(md.Profiles, ","), warn}) } if m.inPanel { - keys = rowKeys + keys = "↑↓ row · Enter actions · A add from a file · ← back · Q leave" } return "Models", "\n" + table([]string{"MODEL", "READY", "STATUS", "CLASS", "PROFILES", ""}, rows, m.row, pw, m.inPanel), keys case cReplicas: @@ -823,7 +942,7 @@ func (m consoleModel) panel() (title, body, keys string) { (time.Duration(r.AgeSec) * time.Second).Round(time.Second).String()}) } if m.inPanel { - keys = "↑↓ row · ← back · PgUp/PgDn · Q leave" + keys = rowKeys } return "Replicas", "\n" + table([]string{"REPLICA", "MODEL", "STATE", "POOL", "ON", "AGE"}, rows, m.row, pw, m.inPanel), keys case cUsage: @@ -841,6 +960,12 @@ func (m consoleModel) panel() (title, body, keys string) { keys = "↑↓ row · → grouping · ← back · Q leave" } return "Usage", head + table([]string{"WHO", "REQUESTS", "TOKENS", "ERRORS", "SPENT"}, rows, m.row, pw, m.inPanel), keys + case cQuotas: + return m.quotasPanel(pw) + case cAccounts, cKeys, cOrgs: + return m.peoplePanel(pw) + case cMirror: + return m.mirrorPanel(pw) case cSettings: return m.settingsPanel(pw) } @@ -853,6 +978,18 @@ func (m consoleModel) panel() (title, body, keys string) { item = "\n Moves the store onto two machines under Patroni, replicated,\n so losing one loses nothing.\n\n " + sDim.Render("On the command line:") + "\n sudo --preserve-env=SSH_AUTH_SOCK llmgw server init \\\n --patroni @ --patroni @ --no-mirror --no-node --no-model" + case cServe: + item = "\n Takes a GGUF model from Hugging Face to a replica that answers:\n the mirror fetches it, its checksum and memory are checked, it joins\n the catalog, and a replica is started and asked to say hello.\n\n " + + sDim.Render("On the command line:") + "\n llmgw ctl model serve OWNER/REPO" + case cUpgrade: + item = "\n Moves the cluster to a release: this machine first, then each node\n in turn. Engines keep serving; a node that does not come back stops it.\n\n " + + sDim.Render("On the command line:") + "\n sudo llmgw ctl upgrade VERSION" + case cRestartAll: + item = "\n Replaces every replica, one at a time, each new one up before the\n old one stops.\n\n " + + sDim.Render("On the command line:") + "\n llmgw ctl restart --all" + case cBackup: + item = "\n Writes the store and /etc/llmgw to one archive in " + DefaultBackupDir + ",\n and keeps the last few. Copy it off this machine.\n\n " + + sDim.Render("On the command line:") + "\n sudo llmgw server backup" case cLeave: item = "\n Closes the console. The cluster keeps running." } @@ -1098,6 +1235,7 @@ func runConsole(ctx context.Context, out io.Writer, file string) error { return err } fm := final.(consoleModel) + fm.st.close() menu = fm.menu act := fm.action switch act.kind { @@ -1114,6 +1252,13 @@ func runConsole(ctx context.Context, out io.Writer, file string) error { // Already set up: only the store changes. in.noMirror, in.noNode, in.noModel = true, true, true err = in.runInitTUI(ctx, out) + case "run": + c := Root() + c.SetArgs(act.targets) + c.SetOut(out) + c.SetErr(out) + fmt.Fprintln(out, sDim.Render("llmgw "+strings.Join(act.targets, " "))) + err = c.ExecuteContext(ctx) case "restart": fmt.Fprintf(out, "restarting %s…\n", fm.service()) if b, e := exec.CommandContext(ctx, "systemctl", "restart", fm.service()).CombinedOutput(); e != nil { diff --git a/internal/cli/tui_frame.go b/internal/cli/tui_frame.go index 1c25c267..5786e0e7 100644 --- a/internal/cli/tui_frame.go +++ b/internal/cli/tui_frame.go @@ -68,14 +68,18 @@ func (f frame) render() string { ph := h - 2 inner := ph - 2 // borders - var nav strings.Builder + var navLines []string + cur := 0 for _, it := range f.Nav { mark := " " label := it.Label if it.Heading { - nav.WriteString(sDim.Render(label) + "\n") + navLines = append(navLines, sDim.Render(label)) continue } + if it.Current || it.State == "current" { + cur = len(navLines) + } switch { case it.State == "done": mark = sOK.Render("✓ ") @@ -88,9 +92,22 @@ func (f frame) render() string { case it.Dim || it.State == "todo": label = sDim.Render(label) } - nav.WriteString(truncateWidth(mark+label, navWidth-2) + "\n") + navLines = append(navLines, truncateWidth(mark+label, navWidth-2)) } - left := box(fBorder, f.NavTitle, nav.String(), navWidth, inner) + // Taller than the screen: the part around the current entry, with a mark + // at an edge that has more beyond it. + if len(navLines) > inner { + total := len(navLines) + top := min(max(0, cur-inner/2), total-inner) + navLines = append([]string{}, navLines[top:top+inner]...) + if top > 0 { + navLines[0] = sDim.Render(" ↑ more") + } + if top+inner < total { + navLines[inner-1] = sDim.Render(" ↓ more") + } + } + left := box(fBorder, f.NavTitle, strings.Join(navLines, "\n"), navWidth, inner) pw := w - navWidth - 2 - 2 lines := strings.Split(strings.TrimRight(f.Body, "\n"), "\n") diff --git a/internal/cli/tui_mirror.go b/internal/cli/tui_mirror.go new file mode 100644 index 00000000..bd2295ab --- /dev/null +++ b/internal/cli/tui_mirror.go @@ -0,0 +1,182 @@ +package cli + +import ( + "context" + "errors" + "fmt" + "os" + "strings" + "time" + + tea "github.com/charmbracelet/bubbletea" + + "gitea.mynodes.xyz/ilker/llmgw/internal/config" + "gitea.mynodes.xyz/ilker/llmgw/internal/mirror" +) + +// The mirror, as the control plane reaches it: its management API, with the +// address and certificate server.yaml gives under mirror. + +type consoleMirrorMsg struct { + res mirror.ModelsResponse + err error +} + +func (m consoleModel) mirrorClient() (*mirror.Client, error) { + cfg, err := config.Load(m.file) + if err != nil { + return nil, err + } + if cfg.Mirror.API == "" { + return nil, errors.New("no mirror: mirror.api in server.yaml is empty") + } + return mirror.NewClient(cfg.Mirror.API, cfg.Mirror.TLS, 10*time.Second) +} + +func (m consoleModel) fetchMirror() tea.Cmd { + return func() tea.Msg { + c, err := m.mirrorClient() + if err != nil { + return consoleMirrorMsg{err: err} + } + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + res, err := c.List(ctx) + return consoleMirrorMsg{res, err} + } +} + +// mirrorCall runs one request to the mirror in the background and reports it. +func (m *consoleModel) mirrorCall(title string, do func(context.Context, *mirror.Client) (string, error)) tea.Cmd { + m.busy = title + mm := *m + return func() tea.Msg { + c, err := mm.mirrorClient() + if err != nil { + return actionDoneMsg{title: title, err: err} + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + text, err := do(ctx, c) + return actionDoneMsg{title: title, text: text, err: err} + } +} + +// usedBy names the catalog's models whose source is this file. +func (m consoleModel) usedBy(base string, s mirror.Status) []string { + u := mirror.Entry{Model: s.Model, Version: s.Version, File: s.File}.URL(base) + dir := strings.TrimSuffix(mirror.Entry{Model: s.Model, Version: s.Version}.URL(base), "/") + "/" + var names []string + for _, md := range m.data.models { + if md.Source == u || strings.HasPrefix(md.Source, dir) { + names = append(names, md.Name) + } + } + return names +} + +func (m consoleModel) mirrorMenu(s mirror.Status) *menuDialog { + what := s.Model + "/" + s.Version + used := m.usedBy(m.mirror.res.Base, s) + items := []menuItem{{"Where nodes fetch it", func(m *consoleModel) tea.Cmd { + m.dialog, m.dialogTitle = "error", what + m.dialogText = mirror.Entry{Model: s.Model, Version: s.Version, File: s.File}.URL(m.mirror.res.Base) + + "\n\n" + sDim.Render("the source to give a model definition · any key") + return nil + }}} + items = append(items, menuItem{"Remove from the mirror…", func(m *consoleModel) tea.Cmd { + text := "Every file of this version is deleted from the mirror." + if len(used) > 0 { + text += "\n\n" + sWarn.Render("! ") + "The catalog uses it: " + strings.Join(used, ", ") + + ".\nNodes that do not hold it already cannot start them." + } + m.confirmD = &confirmDialog{title: "Remove " + what, text: text, + yes: func(m *consoleModel) tea.Cmd { + return m.mirrorCall("Remove "+what, func(ctx context.Context, c *mirror.Client) (string, error) { + removed, err := c.Remove(ctx, s.Model, s.Version) + return fmt.Sprintf("Removed %d file(s).", len(removed)), err + }) + }} + return nil + }}) + return &menuDialog{title: what, items: items} +} + +func (m *consoleModel) mirrorFetchForm() { + m.openForm(&formDialog{title: "Download into the mirror", + note: "The mirror downloads it once; nodes then fetch it from the mirror.", + fields: []formField{ + textF("from", "", "an https:// URL, like a Hugging Face resolve link"), + textF("model", "", "one directory level, like qwen2.5-7b"), + textF("version", time.Now().Format("2006-01-02"), "one directory level; a date works well"), + textF("file", "", "optional: taken from the URL"), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if !strings.HasPrefix(v[0], "http://") && !strings.HasPrefix(v[0], "https://") { + return nil, fmt.Errorf("from is a URL") + } + if v[1] == "" || v[2] == "" { + return nil, fmt.Errorf("a model and a version") + } + req := mirror.FetchRequest{Source: v[0], Model: v[1], Version: v[2], File: v[3]} + return m.mirrorCall("Download "+v[1], func(ctx context.Context, c *mirror.Client) (string, error) { + r, err := c.Fetch(ctx, req) + if err != nil { + return "", err + } + return "Downloading. Once it is here, a model's source is\n" + r.URL, nil + }), nil + }}) +} + +func (m consoleModel) mirrorPanel(pw int) (string, string, string) { + keys := "→ or Enter open · ↑↓ choose · Q leave" + if m.inPanel { + keys = "↑↓ row · Enter actions · A download one · ← back · Q leave" + } + if m.mirror.err != nil { + return "Mirror", "\n " + sBad.Render(m.mirror.err.Error()), keys + } + head := "\n " + sDim.Render("Nodes fetch from "+m.mirror.res.Base) + "\n\n" + var rows [][]string + for _, s := range m.mirror.res.Models { + state := string(s.State) + switch s.State { + case mirror.Fetching: + if p, ok := s.Percent(); ok { + state = sWarn.Render(fmt.Sprintf("fetching %d%%", p)) + } else { + state = sWarn.Render("fetching " + bytesShort(s.BytesDone)) + } + case mirror.Failed: + state = sBad.Render("failed") + } + size := bytesShort(s.BytesDone) + used := strings.Join(m.usedBy(m.mirror.res.Base, s), ", ") + if used == "" { + used = sDim.Render("unused") + } + rows = append(rows, []string{s.Model, s.Version, s.File, state, size, used}) + } + if len(rows) == 0 { + return "Mirror", head + " " + sDim.Render("Empty. A downloads one."), keys + } + return "Mirror", head + table([]string{"MODEL", "VERSION", "FILE", "STATE", "SIZE", "USED BY"}, rows, m.row, pw, m.inPanel), keys +} + +// addModelForm adds a model from a definition file on this machine: the +// same YAML a model takes in server.yaml. +func (m *consoleModel) addModelForm() { + server := "--server=" + m.client.base + m.openForm(&formDialog{title: "Add a model", + note: "From a definition file on this machine: the YAML a model takes in\nserver.yaml's models. Serve a model, under Do, needs none.", + fields: []formField{ + textF("file", "", "like /root/qwen.yaml"), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if _, err := os.Stat(v[0]); err != nil { + return nil, err + } + return m.runCLI("Add a model", "ctl", server, "model", "add", "-f", v[0]), nil + }}) +} diff --git a/internal/cli/tui_people.go b/internal/cli/tui_people.go new file mode 100644 index 00000000..a1003f1f --- /dev/null +++ b/internal/cli/tui_people.go @@ -0,0 +1,545 @@ +package cli + +import ( + "bytes" + "context" + "fmt" + "strconv" + "strings" + "sync" + "time" + + tea "github.com/charmbracelet/bubbletea" + + "gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi" + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +// The people side of the console: accounts, keys and the organizations they +// belong to. These live in the store rather than behind the admin API — the +// API mints no credentials — so the console reads the store as `llmgw users` +// does, and changes it by running those same commands, their checks and +// messages included. + +// storeHolder is the store, opened on first use and shared by every copy of +// the console's model. +type storeHolder struct { + mu sync.Mutex + file string + db *store.DB +} + +func (h *storeHolder) get(ctx context.Context) (*store.DB, error) { + h.mu.Lock() + defer h.mu.Unlock() + if h.db == nil { + sf := storeFlags{config: h.file} + db, err := sf.openCtx(ctx) + if err != nil { + return nil, err + } + h.db = db + } + return h.db, nil +} + +func (h *storeHolder) close() { + h.mu.Lock() + defer h.mu.Unlock() + if h.db != nil { + _ = h.db.Close() + h.db = nil + } +} + +// orgRow is one line of the flattened tenancy tree. +type orgRow struct { + level string // org, team, project + id, name, path string + members, keys int +} + +type peopleData struct { + users []store.User + where map[string][]string // user id → paths it is a member of + keys []store.KeyRow + orgs []orgRow + limits map[string]store.Limits + threads map[string]int // project id → hours kept, conversations + notes map[string]int // project id → hours kept, agent notes + err error +} + +type consolePeopleMsg peopleData + +func (m consoleModel) fetchPeople() tea.Cmd { + h := m.st + return func() tea.Msg { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + var d peopleData + s, err := h.get(ctx) + if err != nil { + d.err = err + return consolePeopleMsg(d) + } + if d.users, d.err = s.Users(ctx, ""); d.err != nil { + return consolePeopleMsg(d) + } + all, _ := s.AllMemberships(ctx) + d.where = map[string][]string{} + for _, ms := range all { + d.where[ms.UserID] = append(d.where[ms.UserID], ms.Path) + } + d.keys, _ = s.AllKeys(ctx) + d.limits, _ = s.AllLimits(ctx) + tree, _ := s.Tree(ctx) + for _, o := range tree { + d.orgs = append(d.orgs, orgRow{level: "org", id: o.ID, name: o.Name, path: o.Name, members: o.Members}) + for _, t := range o.Teams { + tp := o.Name + "/" + t.Name + d.orgs = append(d.orgs, orgRow{level: "team", id: t.ID, name: t.Name, path: tp, members: t.Members}) + for _, p := range t.Projects { + d.orgs = append(d.orgs, orgRow{level: "project", id: p.ID, name: p.Name, path: tp + "/" + p.Name, + members: p.Users, keys: p.ActiveKeys}) + } + } + } + d.threads, d.notes = map[string]int{}, map[string]int{} + for _, r := range d.orgs { + if r.level != "project" { + continue + } + if t, err := s.ThreadRetention(ctx, r.id); err == nil { + d.threads[r.id] = int(t.Hours()) + } + if t, err := s.MemoryRetention(ctx, r.id); err == nil { + d.notes[r.id] = int(t.Hours()) + } + } + return consolePeopleMsg(d) + } +} + +// runCLI runs llmgw with these arguments in the background, against this +// console's store, and shows what it printed. +func (m *consoleModel) runCLI(title string, args ...string) tea.Cmd { + m.busy = title + file := m.file + return func() tea.Msg { + var out bytes.Buffer + c := Root() + c.SetArgs(append(append([]string{"--log-level=error"}, args...), "--config="+file)) + c.SetOut(&out) + c.SetErr(&out) + c.SetIn(strings.NewReader("")) + err := c.ExecuteContext(context.Background()) + text := strings.TrimSpace(out.String()) + if text == "" { + text = "Done." + } + if err != nil { + if text != "Done." { + err = fmt.Errorf("%v\n%s", err, text) + } + return actionDoneMsg{title: title, err: err} + } + return actionDoneMsg{title: title, text: text} + } +} + +func pwF(label, hint string) formField { + f := textF(label, "", hint) + f.text.Mask = true + return f +} + +// --- accounts --- + +func (m consoleModel) userMenu(u store.User) *menuDialog { + who := u.Email + cli := func(title string, args ...string) menuItem { + return menuItem{title, func(m *consoleModel) tea.Cmd { return m.runCLI(title+" "+who, args...) }} + } + var items []menuItem + switch u.State { + case store.UserPending: + items = append(items, menuItem{"Approve…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Approve " + who, + note: "Approved, they sign in and issue their own keys.", + fields: []formField{textF("project", "", "optional: a project to put them in, like Acme/ml/training")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + args := []string{"users", "approve", u.ID, "--by=console"} + if v[0] != "" { + args = append(args, "--project="+v[0]) + } + return m.runCLI("Approve "+who, args...), nil + }}) + return nil + }}) + case store.UserSuspended: + items = append(items, cli("Reinstate", "users", "reinstate", u.ID)) + default: + items = append(items, menuItem{"Suspend…", func(m *consoleModel) tea.Cmd { + m.confirmD = &confirmDialog{title: "Suspend " + who, + text: "The account stops working and its sessions close; it and its history are kept.", + yes: func(m *consoleModel) tea.Cmd { return m.runCLI("Suspend "+who, "users", "suspend", u.ID) }} + return nil + }}) + } + if u.IsAdmin { + items = append(items, cli("Take away the cluster dashboard", "users", "demote", u.ID)) + } else { + items = append(items, cli("Give the cluster dashboard (/admin)", "users", "promote", u.ID)) + } + items = append(items, + menuItem{"Add to an organization, team or project…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Membership for " + who, + note: "A member of a team or organization may use every project in it.", + fields: []formField{ + choiceF("level", []string{"org", "team", "project"}, "team"), + textF("which", "", "Acme, Acme/ml or Acme/ml/training"), + choiceF("role", []string{store.RoleMember, "manager"}, store.RoleMember), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if v[1] == "" { + return nil, fmt.Errorf("which %s?", v[0]) + } + return m.runCLI("Membership for "+who, "users", "join", u.ID, "--"+v[0]+"="+v[1], "--role="+v[2], "--by=console"), nil + }}) + return nil + }}, + menuItem{"Take out of one…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Take " + who + " out", + note: "Keys they made in a project they can no longer reach are revoked.", + fields: []formField{ + choiceF("level", []string{"org", "team", "project"}, "team"), + textF("which", "", strings.Join(m.people.where[u.ID], "; ")), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if v[1] == "" { + return nil, fmt.Errorf("which %s?", v[0]) + } + return m.runCLI("Take "+who+" out", "users", "leave", u.ID, "--"+v[0]+"="+v[1]), nil + }}) + return nil + }}, + menuItem{"Their limits…", func(m *consoleModel) tea.Cmd { m.limitsForm(store.ScopeUser, u.ID, who); return nil }}, + menuItem{"Set a new password…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "New password for " + who, + note: "Their sessions close; tell them out of band and have them change it.", + fields: []formField{pwF("password", "at least 12 characters"), pwF("again", "")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if v[0] == "" || v[0] != v[1] { + return nil, fmt.Errorf("the two are not the same") + } + return m.runCLI("Password for "+who, "users", "reset-password", u.ID, "--password="+v[0]), nil + }}) + return nil + }}, + menuItem{"Delete the account…", func(m *consoleModel) tea.Cmd { + m.confirmD = &confirmDialog{title: "Delete " + who, + text: "They are signed out, every key they made is revoked, and the account is removed.\nTo keep it, suspend it instead.", + yes: func(m *consoleModel) tea.Cmd { return m.runCLI("Delete "+who, "users", "delete", u.ID, "--yes") }} + return nil + }}, + ) + return &menuDialog{title: who, items: items} +} + +func (m *consoleModel) createUserForm() { + m.openForm(&formDialog{title: "New account", + note: "Created already approved. The password should be changed by its owner.", + fields: []formField{ + textF("email", "", ""), + pwF("password", "at least 12 characters"), + textF("project", "", "optional: a project to put them in"), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if !strings.Contains(v[0], "@") || v[1] == "" { + return nil, fmt.Errorf("an email and a password") + } + args := []string{"users", "create", v[0], "--password=" + v[1]} + if v[2] != "" { + args = append(args, "--project="+v[2]) + } + return m.runCLI("New account", args...), nil + }}) +} + +// --- keys --- + +func (m consoleModel) keyMenu(k store.KeyRow) *menuDialog { + name := k.Name + " (" + k.Prefix + ")" + return &menuDialog{title: name, items: []menuItem{ + {"Its limits…", func(m *consoleModel) tea.Cmd { m.limitsForm(store.ScopeKey, k.ID, name); return nil }}, + {"Revoke…", func(m *consoleModel) tea.Cmd { + m.confirmD = &confirmDialog{title: "Revoke " + name, + text: "It stops working at once. It is kept, revoked, for the usage that refers to it.", + yes: func(m *consoleModel) tea.Cmd { return m.runCLI("Revoke "+name, "keys", "revoke", k.ID) }} + return nil + }}, + }} +} + +func (m *consoleModel) createKeyForm() { + m.openForm(&formDialog{title: "New API key", + note: "Shown once, here, and never again: only its hash is kept.", + fields: []formField{ + textF("name", "", "what it is for"), + textF("project", "", "Acme/ml/training; empty with a person below is their personal key"), + textF("for", "", "optional: the person it belongs to, by email"), + textF("expires in", "", "optional: like 720h; empty never expires"), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if v[0] == "" { + return nil, fmt.Errorf("a name") + } + args := []string{"keys", "create", v[0]} + if v[1] != "" { + args = append(args, "--project="+v[1]) + } + if v[2] != "" { + args = append(args, "--for="+v[2]) + } + if v[3] != "" { + if _, err := time.ParseDuration(v[3]); err != nil { + return nil, fmt.Errorf("expires in is a duration, like 720h") + } + args = append(args, "--expires-in="+v[3]) + } + return m.runCLI("New key "+v[0], args...), nil + }}) +} + +// --- limits, at any level --- + +func (m *consoleModel) limitsForm(scope store.Scope, id, who string) { + l := m.people.limits[store.LimitOwner{Scope: scope, ID: id}.CounterKey()] + budget, period := "", "month" + if l.Budget != nil { + budget = strconv.FormatFloat(float64(*l.Budget)/1e6, 'f', -1, 64) + } + if l.BudgetPeriod != "" { + period = string(l.BudgetPeriod) + } + models := "every model" + if l.AllowedModels != nil { + models = strings.Join(l.AllowedModels, ",") + } + m.openForm(&formDialog{title: "Limits on " + who, + note: "Each level only narrows what is above it. Empty is no limit here.", + fields: []formField{ + textF("requests/min", ptrText(l.RPM), ""), + textF("tokens/min", ptrText(l.TPM), ""), + textF("budget", budget, "spend per period, in whole units"), + choiceF("per", []string{"day", "month"}, period), + textF("models", models, "allowed, separated by commas; \"every model\" for no list"), + }, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + args := []string{"keys", "limits", "--scope=" + string(scope), "--id=" + id} + for i, flag := range []string{"rpm", "tpm"} { + if v[i] != "" { + if _, err := strconv.Atoi(v[i]); err != nil { + return nil, fmt.Errorf("%s is a whole number", flag) + } + args = append(args, "--"+flag+"="+v[i]) + } + } + if v[2] != "" { + if _, err := strconv.ParseFloat(v[2], 64); err != nil { + return nil, fmt.Errorf("the budget is a number") + } + args = append(args, "--budget="+v[2], "--period="+v[3]) + } + if v[4] != "every model" { + args = append(args, "--models="+strings.ReplaceAll(v[4], " ", "")) + } + return m.runCLI("Limits on "+who, args...), nil + }}) +} + +// --- organizations --- + +func (m consoleModel) orgMenu(r orgRow) *menuDialog { + child := map[string]string{"org": "team", "team": "project"}[r.level] + parent := map[string]string{"team": "organization", "project": "team"}[r.level] + var items []menuItem + if child != "" { + items = append(items, menuItem{"Add a " + child + " in it…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "New " + child + " in " + r.path, + fields: []formField{textF("name", "", "")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if v[0] == "" { + return nil, fmt.Errorf("a name") + } + return m.runCLI("New "+child, "org", "add", child, v[0], "--in="+r.id), nil + }}) + return nil + }}) + } + items = append(items, menuItem{"Rename…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Rename " + r.path, + fields: []formField{textF("name", r.name, "")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + return m.runCLI("Rename "+r.path, "org", "rename", r.level, r.id, v[0]), nil + }}) + return nil + }}) + if parent != "" { + items = append(items, menuItem{"Move to another " + parent + "…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: "Move " + r.path, + fields: []formField{textF(parent, "", "")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + return m.runCLI("Move "+r.path, "org", "move", r.level, r.id, "--to="+v[0]), nil + }}) + return nil + }}) + } + items = append(items, + menuItem{"Limits…", func(m *consoleModel) tea.Cmd { m.limitsForm(store.Scope(r.level), r.id, r.path); return nil }}, + menuItem{"Limits for each member…", func(m *consoleModel) tea.Cmd { + m.limitsForm(store.MemberScope(store.Scope(r.level)), r.id, "each member of "+r.path) + return nil + }}, + menuItem{"Capacity quota…", func(m *consoleModel) tea.Cmd { + q := adminapi.QuotaView{Scope: r.level + ":" + r.id} + for _, x := range m.quotas { + if x.Scope == q.Scope { + q = x + } + } + m.quotaForm(q) + return nil + }}, + ) + if r.level == "project" { + retention := func(label, sub, what string, now int) menuItem { + return menuItem{label + "…", func(m *consoleModel) tea.Cmd { + m.openForm(&formDialog{title: label + " · " + r.path, + note: "Off by default: llmgw keeps no " + what + " unless a project asks for it.", + fields: []formField{textF("hours", strconv.Itoa(now), "how long they are kept; 0 keeps none")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + h, err := strconv.Atoi(v[0]) + if err != nil || h < 0 { + return nil, fmt.Errorf("hours is a whole number") + } + args := []string{"keys", sub, "--project=" + r.id} + if h == 0 { + args = append(args, "--off") + } else { + args = append(args, "--retain-hours="+v[0]) + } + return m.runCLI(label, args...), nil + }}) + return nil + }} + } + items = append(items, + retention("Keep conversations", "threads", "conversations", m.people.threads[r.id]), + retention("Keep agent notes", "memory", "notes", m.people.notes[r.id])) + } + items = append(items, menuItem{"Remove…", func(m *consoleModel) tea.Cmd { + m.confirmD = &confirmDialog{title: "Remove " + r.path, + text: "Only something empty is removed: an organization with no teams, a team with\nno projects, a project with no accounts and no keys.", + yes: func(m *consoleModel) tea.Cmd { return m.runCLI("Remove "+r.path, "org", "remove", r.level, r.id) }} + return nil + }}) + return &menuDialog{title: r.level + " " + r.path, items: items} +} + +func (m *consoleModel) createOrgForm() { + m.openForm(&formDialog{title: "New organization", + fields: []formField{textF("name", "", "")}, + submit: func(m *consoleModel, v []string) (tea.Cmd, error) { + if v[0] == "" { + return nil, fmt.Errorf("a name") + } + return m.runCLI("New organization", "org", "add", "org", v[0]), nil + }}) +} + +// --- the views --- + +func (m consoleModel) peoplePanel(pw int) (string, string, string) { + title := consoleLabels[m.menu] + keys := "→ or Enter open · ↑↓ choose · Q leave" + if m.inPanel { + keys = "↑↓ row · Enter actions · A add · ← back · Q leave" + } + if m.people.err != nil { + return title, "\n " + sBad.Render("Cannot read the store: ") + m.people.err.Error(), keys + } + if m.people.users == nil && m.people.orgs == nil { + return title, "\n " + m.spin.View() + " reading the store…", keys + } + now := time.Now() + var head []string + var rows [][]string + switch m.menu { + case cAccounts: + head = []string{"EMAIL", "STATE", "ADMIN", "MEMBER OF", "SIGNED UP", "LAST LOGIN"} + for _, u := range m.people.users { + state := string(u.State) + if u.State == store.UserPending { + state = sWarn.Render(state) + } + admin := "" + if u.IsAdmin { + admin = "yes" + } + where := strings.Join(m.people.where[u.ID], "; ") + if where == "" { + where = sDim.Render("on their own") + } + rows = append(rows, []string{u.Email, state, admin, where, ago(u.CreatedAt, now), ago(u.LastLoginAt, now)}) + } + case cKeys: + head = []string{"NAME", "KEY", "PROJECT", "OWNER", "STATE", "LAST USED"} + for _, k := range m.people.keys { + where := k.ProjectPath + if where == "" { + where = "personal" + } + rows = append(rows, []string{k.Name, k.Prefix, where, dash(k.OwnerEmail), keyState(k.Key, now), ago(k.LastUsedAt, now)}) + } + case cOrgs: + head = []string{"NAME", "LEVEL", "MEMBERS", "KEYS", "LIMITS"} + for _, r := range m.people.orgs { + name := r.name + switch r.level { + case "team": + name = " " + name + case "project": + name = " " + name + } + keysCell := "" + if r.level == "project" { + keysCell = strconv.Itoa(r.keys) + } + rows = append(rows, []string{name, r.level, strconv.Itoa(r.members), keysCell, + limitsCell(m.people.limits[store.LimitOwner{Scope: store.Scope(r.level), ID: r.id}.CounterKey()])}) + } + } + if len(rows) == 0 { + return title, "\n " + sDim.Render("None yet. A adds one."), keys + } + return title, "\n" + table(head, rows, m.row, pw, m.inPanel), keys +} + +func limitsCell(l store.Limits) string { + var p []string + if l.RPM != nil { + p = append(p, fmt.Sprintf("%d rpm", *l.RPM)) + } + if l.TPM != nil { + p = append(p, fmt.Sprintf("%s tpm", compactNum(int64(*l.TPM)))) + } + if l.Budget != nil { + p = append(p, fmt.Sprintf("%.2f/%s", float64(*l.Budget)/1e6, l.BudgetPeriod)) + } + if l.AllowedModels != nil { + p = append(p, fmt.Sprintf("%d models", len(l.AllowedModels))) + } + return strings.Join(p, ", ") +}