diff --git a/internal/agent/grok.go b/internal/agent/grok.go index 478b02486..6aa316f56 100644 --- a/internal/agent/grok.go +++ b/internal/agent/grok.go @@ -231,7 +231,7 @@ func grokIn(at place) *Agent { if !ok { return nil } - return catalog.Efforts(magpieModels("grok"), ref) + return catalog.Efforts(sharedModels("grok"), ref) // only read } return atomic(&Agent{ ID: "grok", Name: "Grok Build", Icon: "xai", Aliases: []string{"grok-build", "grok-cli"}, Spelled: prefixed, @@ -425,23 +425,20 @@ func grokOwnOptions(dir, path, cur string) []Option { } // the user's own [model.""] tables, each on its own endpoint if path != "" { - if tables, err := edit.TOMLTables(path); err == nil { - for _, t := range tables { - id, ok := strings.CutPrefix(t, "model.") - if !ok { - continue - } - if u, err := strconv.Unquote(id); err == nil { - id = u - } - kv, _ := edit.GetTOMLTable(path, t) - direct := "xAI" - if h := hostOf(kv["base_url"]); h != "" { - direct = h - } - add(id, kv["name"], direct) + _ = edit.EachTOMLTable(path, func(t edit.TOMLTable) { + id, ok := strings.CutPrefix(t.Name(), "model.") + if !ok { + return } - } + if u, err := strconv.Unquote(id); err == nil { + id = u + } + direct := "xAI" + if h := hostOf(t.Scalar("base_url")); h != "" { + direct = h + } + add(id, t.Scalar("name"), direct) + }) } if cur != "" && !seen[cur] && !strings.HasPrefix(cur, magpieID+"/") { out = append([]Option{{Value: cur, Icon: modelIcon("", cur), Direct: "xAI"}}, out...) diff --git a/internal/edit/toml.go b/internal/edit/toml.go index a02af0027..c27c70ee0 100644 --- a/internal/edit/toml.go +++ b/internal/edit/toml.go @@ -38,6 +38,41 @@ func TOMLTables(path string) ([]string, error) { return doc.names, nil } +// TOMLTable is one ordinary table of a file EachTOMLTable read, shared +// with the file's kept parse, so read only. +type TOMLTable struct{ span *tomlTableSpan } + +// Name is the table's name as TOMLTables lists it. +func (t TOMLTable) Name() string { return t.span.name } + +// Scalar is the value of one scalar key as GetTOMLTable has it, "" when +// the table has none: the last of a key written twice, as the map holds. +func (t TOMLTable) Scalar(key string) string { + for i := len(t.span.keys) - 1; i >= 0; i-- { + if kv := &t.span.keys[i]; kv.scalar && kv.name == key { + return kv.value + } + } + return "" +} + +// EachTOMLTable calls fn with each ordinary table, in the order +// TOMLTables lists them, from one look at the file and nothing built: an +// agent listing its config's tables asked GetTOMLTable for each, a stat +// and a map a table on every look. +func EachTOMLTable(path string, fn func(TOMLTable)) error { + tables, err := tomlTablesOf(path) + if err != nil { + return err + } + for i := range tables { + if !tables[i].array { + fn(TOMLTable{&tables[i]}) + } + } + return nil +} + // GetTOMLTable returns the scalar keys of one table, or (nil, nil) when // the file or table is absent. Read and parse errors are returned with the path. // Strings are decoded; other scalars (booleans, numbers, dates, and times) diff --git a/internal/edit/toml_each_test.go b/internal/edit/toml_each_test.go new file mode 100644 index 000000000..b032ffd84 --- /dev/null +++ b/internal/edit/toml_each_test.go @@ -0,0 +1,60 @@ +package edit + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" +) + +// EachTOMLTable gives what TOMLTables and GetTOMLTable give, table by +// table, without a map or a stat for each: Grok Build's model list reads +// every [model.""] table of its config on each look. +func TestEachTOMLTableAgreesWithGetTOMLTable(t *testing.T) { + var b strings.Builder + b.WriteString("top = 1\n\n[[array]]\nname = \"skipped\"\n\n[plain]\nname = \"p\"\nlist = [1, 2]\n\n") + for i := range 200 { + fmt.Fprintf(&b, "[model.\"m-%d\"]\nname = \"Model %d\"\nbase_url = 'https://h%d.example/v1' # own\n\n", i, i, i) + } + path := filepath.Join(t.TempDir(), "config.toml") + if err := os.WriteFile(path, []byte(b.String()), 0o600); err != nil { + t.Fatal(err) + } + names, err := TOMLTables(path) + if err != nil { + t.Fatal(err) + } + var got []string + if err := EachTOMLTable(path, func(tb TOMLTable) { + got = append(got, tb.Name()) + want, _ := GetTOMLTable(path, tb.Name()) + for _, k := range []string{"name", "base_url", "list", "missing"} { + if v := tb.Scalar(k); v != want[k] { + t.Errorf("%s.%s: %q, GetTOMLTable has %q", tb.Name(), k, v, want[k]) + } + } + }); err != nil { + t.Fatal(err) + } + if strings.Join(got, "|") != strings.Join(names, "|") { + t.Fatalf("tables %v, TOMLTables lists %v", got, names) + } + seen := 0 + if n := testing.AllocsPerRun(10, func() { + seen = 0 + EachTOMLTable(path, func(tb TOMLTable) { + if tb.Scalar("base_url") != "" { + seen++ + } + }) + }); n > 5 { + t.Errorf("a look at 201 tables: %.0f allocations, want a few", n) + } + if seen != 200 { + t.Errorf("tables with a base_url: %d", seen) + } + if err := EachTOMLTable(filepath.Join(t.TempDir(), "none.toml"), func(TOMLTable) { t.Error("a missing file has tables") }); err != nil { + t.Errorf("missing file: %v", err) + } +}