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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480package tui
import ( "cmp" "fmt" "slices" "strings"
"charm.land/bubbles/v2/textinput" "charm.land/lipgloss/v2" "github.com/charmbracelet/x/ansi")
// modelColumns are the widths every listed row shares, so the effort ladders// line up.type modelColumns struct{ name, slots, label, tag int }
func (c modelColumns) cluster() int { switch { case c.slots == 0: return 0 case c.label == 0: return c.slots + 4 } return c.slots + 5 + c.label}
func (r modelRow) displayID() string { if r.cyberAccess == "daybreak_blue" { return r.model.ID + " · Daybreak Blue" } return r.model.ID}
func (m ModelPickerModel) layout(width int) modelColumns { var c modelColumns for _, r := range m.rows { if r.typed { continue } c.name = max(c.name, ansi.StringWidth(r.displayID())) c.slots = max(c.slots, len(effortSteps(r.model.Efforts))) for _, level := range r.model.Efforts { c.label = max(c.label, ansi.StringWidth(level)) } c.tag = max(c.tag, ansi.StringWidth(m.tag(r))) } room := width - modelRowPrefix - 1 if c.tag > 0 { room -= c.tag + 2 } // Narrow rows shorten names first, then drop the label, then the ladder. longest := c.name over := func() bool { return c.name+min(c.cluster(), 1)*2+c.cluster() > room } fit := func() { c.name = min(longest, max(min(longest, 16), room-min(c.cluster(), 1)*2-c.cluster())) } if over() { fit() } if over() { c.label = 0 fit() } if over() { c.slots = 0 } c.name = max(1, min(c.name, room)) return c}
// tag names a listed row's standing: the session's model, or the model the// profile opens new sessions with.func (m ModelPickerModel) tag(r modelRow) string { switch { case r.typed: return "" case r.profile == m.Profile && r.model.ID == m.Model && r.cyberAccess == m.CyberAccess: return "current" case r.cyberAccess == "" && r.model.ID == m.catalog(r.profile).settings.Model: return "default" } return ""}
func (m ModelPickerModel) View() string { width := cmp.Or(m.Width, 80) f := listFrame{ title: brand("albedo") + " " + DefaultStyles.Muted.Render("/model"), right: m.selection(), filter: promptLead() + m.search.View(), list: m.list, pane: m.details, footer: m.footer(width), } if r, ok := m.highlighted(); ok { f.summary = m.summary(r) } return f.view(m.Width, m.Height)}
// selection is the session's profile, model and effort for the title.func (m ModelPickerModel) selection() string { label := m.Model if m.CyberAccess == "daybreak_blue" { label += " · Daybreak Blue (current)" } model := DefaultStyles.Muted.Render(label) if m.Effort != "" { model += DefaultStyles.Faint.Render(":" + m.Effort) } return DefaultStyles.Faint.Render(m.Profile) + DefaultStyles.Decor.Render(" · ") + model}
// list renders the rows grouped by profile at exactly width × height.func (m ModelPickerModel) list(width, height int) []string { cols := m.layout(width) // Rules and notes are drawn here; a model row only once it shows. type line struct { text string row int } var all []line selectedAt := -1 note := func(text string) { all = append(all, line{text: text, row: -1}) } section := func(label string, count int) { if len(all) > 0 && height >= 10 { note("") } note(sectionRule(label, count, width)) } add := func(i int) { if i == m.cursor { selectedAt = len(all) } all = append(all, line{row: i}) }
i := 0 for i < len(m.rows) && !m.rows[i].typed { start, profile := i, m.rows[i].profile for i < len(m.rows) && !m.rows[i].typed && m.rows[i].profile == profile { i++ } section(profile, i-start) for j := start; j < i; j++ { add(j) } switch c := m.catalog(profile); { case c.loading: note(DefaultStyles.Faint.Render(" listing models…")) case c.failed: note(DefaultStyles.Faint.Render(" Could not list models. Type a model ID to continue.")) } } if i < len(m.rows) { section("as typed", len(m.rows)-i) for ; i < len(m.rows); i++ { add(i) } } if len(m.rows) == 0 { note(DefaultStyles.Faint.Render(" no models match")) } return drawWindow(len(all), func(i int) string { l := all[i] if l.row < 0 { return l.text } return m.row(m.rows[l.row], l.row == m.cursor, width, cols) }, selectedAt, width, height)}
func (m ModelPickerModel) row(r modelRow, selected bool, width int, cols modelColumns) string { bar, nameStyle := " ", lipgloss.NewStyle() if selected { bar, nameStyle = selectBar(), DefaultStyles.Bold } line := bar + " " if r.typed { line += nameStyle.Render(ansi.Truncate(r.model.ID, max(1, width/2), "…")) + DefaultStyles.Faint.Render(" on "+r.profile) } else { line += markedCell(r.displayID(), r.hits, cols.name, nameStyle) if cols.cluster() > 0 { line += " " + m.ladder(r, selected, cols) } if tag := m.tag(r); tag != "" { style := DefaultStyles.Faint if tag == "current" { style = DefaultStyles.Success } line += strings.Repeat(" ", max(2, width-ansi.StringWidth(line)-ansi.StringWidth(tag)-1)) + style.Render(tag) } } if selected { return selectedLine(line, width) } return line}
// markedCell fits s to width like svCell and draws the characters at hits in// the prompt color, so a search shows what it matched.func markedCell(s string, hits []int, width int, base lipgloss.Style) string { if width <= 0 { return "" } plain := ansi.Truncate(s, width, "…") kept := len(plain) if plain != s { kept -= len("…") } hit := DefaultStyles.Prompt.Inherit(base) var b strings.Builder start, marked := 0, false flush := func(end int) { if end > start { style := base if marked { style = hit } b.WriteString(style.Render(plain[start:end])) } } for i := range plain { if m := i < kept && slices.Contains(hits, i); m != marked { flush(i) start, marked = i, m } } flush(len(plain)) return b.String() + strings.Repeat(" ", max(0, width-ansi.StringWidth(plain)))}
// ladder draws a row's effort as squares filling toward the highest level,// with arrows on the selected row where there is a level to step to.func (m ModelPickerModel) ladder(r modelRow, selected bool, cols modelColumns) string { levels := r.model.Efforts if len(levels) == 0 { return strings.Repeat(" ", cols.cluster()) } effort := m.effort(r) steps := effortSteps(levels) filled := slices.Index(steps, effort) + 1 var squares strings.Builder for i := range cols.slots { switch { case i >= len(steps): squares.WriteByte(' ') case i >= filled: squares.WriteString(DefaultStyles.Decor.Render("▱")) case selected: squares.WriteString(brandInk(float64(i) / float64(max(1, len(steps)-1))).Render("▰")) default: squares.WriteString(DefaultStyles.Muted.Render("▰")) } } arrow := func(glyph string, open bool) string { if !selected { return " " } if open { return DefaultStyles.Muted.Render(glyph) } return DefaultStyles.Decor.Render(glyph) } at := slices.Index(levels, effort) out := arrow("‹", at > 0) + " " + squares.String() + " " + arrow("›", at < len(levels)-1) if cols.label > 0 { label := DefaultStyles.Faint if selected { label = lipgloss.NewStyle() } out += " " + label.Render(svCell(effort, cols.label, false)) } return out}
// facts are what the catalog knows about a row's model, as label and value.func (m ModelPickerModel) facts(r modelRow) [][2]string { var facts [][2]string if r.model.Context > 0 { facts = append(facts, [2]string{"context", compactTokens(m.window(r)) + " tokens"}) } if raisable(r.model) { state := DefaultStyles.Faint.Render("default") + DefaultStyles.Decor.Render(" · ") + DefaultStyles.Faint.Render("tab raises to "+compactTokens(r.model.MaxContext)) if r.model.Raised { state = brandInk(1).Bold(true).Render("raised") + DefaultStyles.Decor.Render(" · ") + DefaultStyles.Faint.Render("tab restores "+compactTokens(r.model.Context)) } facts = append(facts, [2]string{"cap", state}) } if compactable(r) { facts = append(facts, [2]string{"compact", m.compactFact(r)}) } if r.model.Output > 0 { facts = append(facts, [2]string{"output", compactTokens(r.model.Output) + " tokens"}) } if len(r.model.Input) > 0 { facts = append(facts, [2]string{"input", strings.Join(r.model.Input, ", ")}) } return facts}
// compactFact is where a row's model compacts: the window field while it is// open, else the saved window.func (m ModelPickerModel) compactFact(r modelRow) string { faint, dot := DefaultStyles.Faint, DefaultStyles.Decor.Render(" · ") switch { case m.editing: return m.edit.View() case r.model.CompactAt == 0: return faint.Render("model window") } return "at " + compactTokens(r.model.CompactAt) + dot + faint.Render("all sessions")}
// compactTokensOrEmpty seeds the window field: nothing for no window.func compactTokensOrEmpty(n int) string { if n == 0 { return "" } return compactTokens(n)}
// newWindowField is the details pane's window input.func newWindowField() textinput.Model { input := newField() input.Prompt = "" input.Placeholder = "100k" input.SetWidth(10) st := input.Styles() st.Focused.Placeholder = DefaultStyles.Faint input.SetStyles(st) input.Focus() return input}
// compactTokens writes a token count as 8k, 400k or 1.05m.func compactTokens(n int) string { switch { case n >= 1_000_000: return strings.TrimSuffix(strings.TrimRight(fmt.Sprintf("%.2f", float64(n)/1e6), "0"), ".") + "m" case n >= 1000: return fmt.Sprintf("%dk", n/1000) } return fmt.Sprint(n)}
// summary is the highlighted row's facts on one line, for narrow screens.func (m ModelPickerModel) summary(r modelRow) string { if r.typed { return DefaultStyles.Faint.Render("sent to " + r.profile + " as typed") } var parts []string if r.model.Context > 0 { parts = append(parts, compactTokens(m.window(r))+" context") } if raisable(r.model) && r.model.Raised { parts = append(parts, "cap raised") } if r.model.CompactAt > 0 { parts = append(parts, "compacts at "+compactTokens(r.model.CompactAt)) } if r.model.Output > 0 { parts = append(parts, compactTokens(r.model.Output)+" output") } if len(r.model.Input) > 0 { parts = append(parts, strings.Join(r.model.Input, ", ")) } if effort := m.effort(r); effort != "" { parts = append(parts, effort+" effort") } return DefaultStyles.Faint.Render(strings.Join(parts, " · "))}
// details renders the highlighted row's pane at exactly width × height.func (m ModelPickerModel) details(width, height int) []string { inner := max(1, width-2) r, ok := m.highlighted() if !ok { return paneBox([]string{"", DefaultStyles.Muted.Render("nothing matches"), DefaultStyles.Faint.Render("Type a model ID to use it as entered")}, inner, width, height) } var lines []string for _, l := range svWrap(r.displayID(), inner, 2) { lines = append(lines, DefaultStyles.Bold.Render(l)) } meta := []string{DefaultStyles.Muted.Render(r.profile)} if protocol := m.catalog(r.profile).settings.Protocol; protocol != "" { meta = append(meta, DefaultStyles.Faint.Render(protocol)) } if tag := m.tag(r); tag != "" { style := DefaultStyles.Faint if tag == "current" { style = DefaultStyles.Success } meta = append(meta, style.Render(tag)) } lines = append(lines, strings.Join(meta, DefaultStyles.Decor.Render(" · ")), DefaultStyles.Decor.Render(strings.Repeat("─", inner)))
note := func(text string) { for _, l := range svWrap(text, inner, 3) { lines = append(lines, DefaultStyles.Faint.Render(l)) } } if r.typed { note(r.profile + " does not list this id, so it is sent as typed and the daemon picks the effort.") } else { if r.model.CyberStatus == "unknown" { note("Daybreak Blue could not be verified. " + r.model.CyberDetail) } facts := m.facts(r) if levels := r.model.Efforts; len(levels) > 0 { chosen := m.effort(r) marks := make([]string, len(levels)) for i, level := range levels { marks[i] = DefaultStyles.Faint.Render(level) if level == chosen { marks[i] = brandInk(1).Bold(true).Render(level) } } facts = append(facts, [2]string{"effort", strings.Join(marks, DefaultStyles.Decor.Render(" · "))}) } for _, f := range facts { lines = append(lines, factRows(f[0], f[1], inner)...) } if len(facts) == 0 { note("the catalog knows nothing else about this model.") } } lines = append(lines, "") note("enter switches this session and saves its model/effort as the default. New sessions start Standard.") return paneBox(lines, inner, width, height)}
func (m ModelPickerModel) footer(width int) string { hints := []hint{{"↑↓", "move"}} if !m.Saving { hints = append(hints, hint{"enter", "switch"}) } hints = append(hints, hint{"←→", "effort"}) if r, ok := m.highlighted(); ok && raisable(r.model) { hints = append(hints, hint{"tab", "cap"}) } if r, ok := m.highlighted(); ok && compactable(r) { hints = append(hints, hint{"ctrl+e", "edit"}) } hints = append(hints, hint{"esc", "back"}) if m.editing { hints = []hint{{"enter", "save"}, {"esc", "drop"}} } loading, models := 0, 0 for _, c := range m.catalogs { if c.loading { loading++ } models += len(c.models) } var right string switch { case m.Saving: right = DefaultStyles.Busy.Render("switching…") case m.saving != nil: right = DefaultStyles.Busy.Render("saving…") case m.Error != "": right = DefaultStyles.Error.Render(m.Error) case loading > 0: right = DefaultStyles.Faint.Render(fmt.Sprintf("%d of %d profiles listed…", len(m.catalogs)-loading, len(m.catalogs))) default: right = DefaultStyles.Faint.Render(counted(models, "model")) + DefaultStyles.Decor.Render(" · ") + DefaultStyles.Faint.Render(counted(len(m.catalogs), "profile")) } return footerLine(width, hints, right, m.Saving || m.saving != nil || m.Error != "")}
func counted(n int, noun string) string { if n == 1 { return "1 " + noun } return fmt.Sprintf("%d %ss", n, noun)}