Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312package tui
import ( "albedo/cli/internal/daemon" "maps" "slices" "strings" "unicode" "unicode/utf8")
// mergeModels lists seeds first, then the rest of listed, each id once. A// listed entry lends its catalog facts to the seed of the same id.func mergeModels(seeds []string, listed []daemon.Model) []daemon.Model { byID := make(map[string]daemon.Model, len(listed)) for _, m := range listed { byID[m.ID] = m } seen := map[string]bool{"": true} var out []daemon.Model add := func(m daemon.Model) { if !seen[m.ID] { seen[m.ID] = true out = append(out, m) } } for _, id := range seeds { m, ok := byID[id] if !ok { m = daemon.Model{ID: id} } add(m) } for _, m := range listed { add(m) } return out}
func searchKey(s string) string { return strings.Map(func(r rune) rune { if unicode.IsSpace(r) || strings.ContainsRune("-_.:/", r) { return -1 } return unicode.ToLower(r) }, s)}
// matchRank orders search matches, compared element by element: an exact id// first, then the word-match tier sum, then the positional/compactness score.type matchRank [3]int
func (a matchRank) compare(b matchRank) int { return slices.Compare(a[:], b[:]) }
type searchToken struct { text []rune start int}
func rangeHits(start, n int) []int { hits := make([]int, n) for i := range n { hits[i] = start + i } return hits}
func matchWord(qw, rawField string) (rank matchRank, hits []int, ok bool) { q := []rune(qw) field := []rune(strings.Map(unicode.ToLower, rawField)) var offsets []int for offset := range rawField { offsets = append(offsets, offset) } defer func() { for i, hit := range hits { hits[i] = offsets[hit] } }() var tokens []searchToken var folded []rune var foldedHits []int isToken := func(r rune) bool { return unicode.IsLetter(r) || unicode.IsNumber(r) || unicode.IsMark(r) || strings.ContainsRune(qw, r) } for i := 0; i < len(field); { if !isToken(field[i]) { i++ continue } start := i for i++; i < len(field) && isToken(field[i]); i++ { } tokens = append(tokens, searchToken{field[start:i], start}) folded = append(folded, field[start:i]...) foldedHits = append(foldedHits, rangeHits(start, i-start)...) } if len(tokens) == 0 { return matchRank{}, nil, false }
if slices.Equal(folded[:min(len(folded), len(q))], q) { tier := 4 if len(folded) == len(q) { tier = 5 } return matchRank{tier, 2, 100}, foldedHits[:len(q)], true } for wi, t := range tokens { if slices.Equal(t.text[:min(len(t.text), len(q))], q) { sub, score := 1, 85-wi if wi == 0 { sub = 2 } if len(t.text) == len(q) { score += 10 } return matchRank{4, sub, score}, rangeHits(t.start, len(q)), true } }
spaceWords := len(strings.Fields(rawField)) if spaceWords <= 2 && len(q) >= 2 && len(tokens) >= 2 { maxSkip := 0 if spaceWords == 1 && len(tokens) <= 5 { maxSkip = 1 } var chain func(start, qi, ti, skips int) (int, []int, bool) chain = func(start, qi, ti, skips int) (score int, hits []int, ok bool) { if qi == len(q) { return 78 - start*2 - skips*8, nil, true } if ti >= len(tokens) { return 0, nil, false } t := tokens[ti] for n := min(len(t.text), len(q)-qi); n >= 1; n-- { if slices.Equal(t.text[:n], q[qi:qi+n]) { if sc, h, found := chain(start, qi+n, ti+1, skips); found { score, hits, ok = sc+(n-1)*3, append(rangeHits(t.start, n), h...), true break } } } if skips < maxSkip { if sc, h, found := chain(start, qi, ti+1, skips+1); found && (!ok || sc > score) { score, hits, ok = sc, h, true } } return } for st, t := range tokens { if t.text[0] == q[0] { if sc, h, found := chain(st, 0, st, 0); found && (!ok || sc > rank[2]) { rank, hits, ok = matchRank{3, 0, sc}, h, true } } } if ok { if hits[0] == tokens[0].start { rank[1] = 1 } return } }
for _, t := range tokens { if before, _, found := strings.Cut(string(t.text), qw); found { start := t.start + utf8.RuneCountInString(before) return matchRank{2, 0, 65 - min(start, 40)}, rangeHits(start, len(q)), true } } if len(q) >= 2 { for wi, t := range tokens { if t.text[0] != q[0] || len(t.text) < len(q) { continue } pos := 0 var h []int for _, r := range q { at := slices.Index(t.text[pos:], r) if at < 0 { break } pos += at + 1 h = append(h, t.start+pos-1) } if len(h) != len(q) { continue } if sc := 55 - (pos-len(q))*3 - wi; !ok || sc > rank[2] { rank, hits, ok = matchRank{1, 0, sc}, h, true } } } return}
// matchModel ranks a model against the search words. Each word must fuzzy// match the model id or the profile name; typing profile and id together// is an exact match too.func matchModel(words []string, profile, id string) (rank matchRank, hits []int, ok bool) { rank, hits, ok = matchFields(words, id, profile) if whole := strings.Join(words, ""); ok && whole != "" && whole == searchKey(profile+id) { rank[0] = 1 } return rank, hits, ok}
// searchWords splits a query into the keys its words match by.func searchWords(query string) []string { var words []string for field := range strings.FieldsSeq(query) { if word := searchKey(field); word != "" { words = append(words, word) } } return words}
// matchFields ranks a row's fields against the search words, which every// list screen shares: each word must match some field. The hits are// the characters of the first field to underline. An empty search matches// everything equally.func matchFields(words []string, fields ...string) (rank matchRank, hits []int, ok bool) { if len(words) == 0 { return matchRank{}, nil, true } if whole := strings.Join(words, ""); whole != "" && slices.ContainsFunc(fields, func(f string) bool { return whole == searchKey(f) }) { rank[0] = 1 } seenHits := map[int]bool{} for _, word := range words { var best matchRank var bestHits []int matchedAny := false for i, f := range fields { r, h, found := matchWord(word, f) if !found { continue } r[2] -= i * 2 if i != 0 { h = nil } if !matchedAny || r.compare(best) > 0 { best, bestHits, matchedAny = r, h, true } } if !matchedAny { return matchRank{}, nil, false } rank[1] += best[0]*4 + best[1] rank[2] += best[2] for _, hit := range bestHits { seenHits[hit] = true } } return rank, slices.Sorted(maps.Keys(seenHits)), true}
// refilter rebuilds the rows from the catalogs and the search text. The// cursor stays on its row unless reset.func (m *ModelPickerModel) refilter(reset bool) { keep := "" if r, ok := m.highlighted(); ok && !reset { keep = r.key() } query := strings.TrimSpace(m.search.Value()) words := searchWords(query)
// Rows rank within their profile, and profiles by their best // row. Ties, and an empty search, keep the catalog order. type scored struct { row modelRow rank matchRank } var groups [][]scored var typed []modelRow canType := query != "" && !strings.ContainsFunc(query, unicode.IsSpace) && validateModelID(query) == nil for _, c := range m.catalogs { var group []scored for _, model := range c.models { if rank, hits, ok := matchModel(words, c.name, model.ID); ok { group = append(group, scored{modelRow{profile: c.name, model: model, hits: hits}, rank}) } if !c.loading && !c.failed && model.CyberStatus == "supported" { if rank, hits, ok := matchFields(words, model.ID, c.name, "Daybreak Blue"); ok { group = append(group, scored{modelRow{profile: c.name, model: model, hits: hits, cyberAccess: "daybreak_blue"}, rank}) } } } if len(group) > 0 { slices.SortStableFunc(group, func(a, b scored) int { return b.rank.compare(a.rank) }) groups = append(groups, group) } // An id has no spaces, so a search of several words offers none. if canType && !slices.ContainsFunc(c.models, func(model daemon.Model) bool { return model.ID == query }) { typed = append(typed, modelRow{profile: c.name, model: daemon.Model{ID: query}, typed: true}) } } slices.SortStableFunc(groups, func(a, b []scored) int { return b[0].rank.compare(a[0].rank) }) m.rows = nil for _, group := range groups { for _, s := range group { m.rows = append(m.rows, s.row) } } m.rows = append(m.rows, typed...) m.cursor = max(0, slices.IndexFunc(m.rows, func(r modelRow) bool { return r.key() == keep }))}