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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176package tui
import ( "errors" "net/url" "time"
"albedo/cli/internal/daemon"
tea "charm.land/bubbletea/v2")
// usageNow is the clock every usage shot is drawn at, which the answers below// carry: the screen labels its days by them, never by the clock it is drawn on.var usageNow = time.Date(2026, 10, 25, 18, 0, 0, 0, time.UTC)
func usageTotals(calls, errs, in, cached, writes, out, reasoning int64) daemon.UsageTotals { return daemon.UsageTotals{Calls: calls, Errors: errs, InputTokens: in, CachedInputTokens: cached, CacheCreationTokens: writes, OutputTokens: out, ReasoningTokens: reasoning}}
// usageWeek is seven days ending with usageNow's, the earlier ones lighter and// one of them idle.func usageWeek() daemon.UsageSummary { totals := []daemon.UsageTotals{ usageTotals(14, 0, 210_000, 160_000, 0, 9_400, 1_200), usageTotals(0, 0, 0, 0, 0, 0, 0), usageTotals(31, 2, 640_000, 520_000, 18_000, 22_000, 4_100), usageTotals(9, 0, 88_000, 60_000, 0, 3_100, 0), usageTotals(52, 1, 1_450_000, 1_210_000, 30_000, 61_000, 9_800), usageTotals(17, 0, 300_000, 240_000, 0, 12_500, 2_000), usageTotals(22, 1, 410_000, 330_000, 0, 15_800, 2_600), } var days []daemon.UsageDay for i, t := range totals { start := time.Date(2026, 10, 19+i, 0, 0, 0, 0, time.UTC) days = append(days, daemon.UsageDay{Start: start, UsageTotals: t}) } return daemon.UsageSummary{GeneratedAt: usageNow, Days: days}}
// usageToday is the day alone, for its total and its models, and the scopes the// window saw.func usageToday() daemon.UsageSummary { return daemon.UsageSummary{ GeneratedAt: usageNow, Days: []daemon.UsageDay{{Start: time.Date(2026, 10, 25, 0, 0, 0, 0, time.UTC), UsageTotals: usageTotals(22, 1, 410_000, 330_000, 0, 15_800, 2_600)}}, Lines: []daemon.UsageLine{ {Profile: "work", Provider: "claude", Account: "dawn@example.com", Model: "example-large", UsageTotals: usageTotals(15, 0, 380_000, 310_000, 0, 12_000, 2_000)}, {Profile: "home", Provider: "responses:https://chatgpt.com/backend-api", Model: "example-mini", UsageTotals: usageTotals(7, 1, 30_000, 20_000, 0, 3_800, 600)}, }, Scopes: []daemon.UsageScope{ {Provider: "claude", Account: "dawn@example.com"}, {Provider: "responses:https://chatgpt.com/backend-api"}, }, }}
// usageHeavy is a week a busy account reaches: a billion tokens in one model, a// cache-heavy day, and names longer than any column.func usageHeavy() (daemon.UsageSummary, daemon.UsageSummary) { week := usageWeek() week.Days[len(week.Days)-1].UsageTotals = usageTotals(1_204, 3, 2_345_700_000, 2_100_000_000, 0, 41_000_000, 6_000_000) week.Days[len(week.Days)-2].UsageTotals = usageTotals(980, 0, 1_120_000_000, 990_000_000, 0, 22_000_000, 3_000_000) today := usageToday() today.Days[0].UsageTotals = week.Days[len(week.Days)-1].UsageTotals today.Lines = []daemon.UsageLine{ {Profile: "work", Provider: "chat_completions:https://api.anthropic.com", Account: "dawn.marchetti@example-corporation.com", Model: "example-opus-4-5-extended-thinking-preview", UsageTotals: usageTotals(1_180, 3, 2_310_000_000, 2_080_000_000, 0, 39_000_000, 5_800_000)}, {Profile: "home", Provider: "chat_completions:http://127.0.0.1:57188/t/1", Model: "example-mini", UsageTotals: usageTotals(24, 0, 35_700_000, 20_000_000, 0, 2_000_000, 200_000)}, } return today, week}
// usagePools is what the real ledger records: each pooled account is the uuid// the provider minted, ollama records its own profile name, and one line of it// records no account at all.func usagePools() (daemon.UsageSummary, daemon.UsageSummary) { today := usageToday() today.Lines = []daemon.UsageLine{ {Profile: "codex", Provider: "responses:https://chatgpt.com/backend-api", Account: "df3eba1d-874a-4762-8d64-7e2feb475634", Model: "example-large", UsageTotals: usageTotals(12, 0, 340_000, 290_000, 0, 11_000, 1_800)}, {Profile: "codex", Provider: "responses:https://chatgpt.com/backend-api", Account: "b7e889d5-3c41-4f0a-9a6e-2d1f5b8c7a10", Model: "example-large", UsageTotals: usageTotals(6, 0, 120_000, 98_000, 0, 4_200, 700)}, {Profile: "ollama", Provider: "chat_completions:https://ollama.com/v1", Model: "example-mini", UsageTotals: usageTotals(3, 0, 24_000, 0, 0, 2_600, 0)}, {Profile: "ollama", Provider: "chat_completions:https://ollama.com/v1", Account: "ollama", Model: "example-mini", UsageTotals: usageTotals(1, 0, 6_000, 0, 0, 800, 0)}, } return today, usageWeek()}
func usageModel(width, height int) UsageModel { m := NewUsageModel(nil) m.SetSize(width, height) m, _ = m.Update(usageLoadedMsg{Gen: m.Generation, Today: usageToday(), Week: usageWeek()}) return m}
// usageShot draws the screen after these keys, from the answers given.func usageShotFrom(answers func() (daemon.UsageSummary, daemon.UsageSummary), keys ...tea.Msg) func(width, height int) string { return func(width, height int) string { m := NewUsageModel(nil) m.SetSize(width, height) today, week := answers() m, _ = m.Update(usageLoadedMsg{Gen: m.Generation, Today: today, Week: week}) for _, key := range keys { m, _ = m.Update(key) } return m.View() }}
func usageShot(keys ...tea.Msg) func(width, height int) string { return usageShotFrom(func() (daemon.UsageSummary, daemon.UsageSummary) { return usageToday(), usageWeek() }, keys...)}
func init() { down := tea.KeyPressMsg{Code: tea.KeyDown} // The rows are today's models, then the days newest first: two models, then // today, the 24th, the busy 23rd, and so on back to the idle 20th. registerShots("usage", shotState{"list", usageShot()}, shotState{"a-model", usageShot(down)}, shotState{"a-day", usageShot(down, down)}, shotState{"a-busy-day", usageShot(down, down, down, down)}, shotState{"an-idle-day", usageShot(down, down, down, down, down, down, down)}, shotState{"filtering", usageShot(shotTyped("mini")...)}, shotState{"no-match", usageShot(shotTyped("zzz")...)}, shotState{"refreshing", usageShot(tea.KeyPressMsg{Code: 'r', Mod: tea.ModCtrl})}, shotState{"loading", func(width, height int) string { m := NewUsageModel(nil) m.SetSize(width, height) return m.View() }}, shotState{"first-day", func(width, height int) string { m := NewUsageModel(nil) m.SetSize(width, height) one := daemon.UsageSummary{GeneratedAt: usageNow, Days: usageToday().Days} m, _ = m.Update(usageLoadedMsg{Gen: m.Generation, Today: one, Week: one}) return m.View() }}, // A window with more than the busiest hundred models: the list is cut, // and says so rather than looking whole. shotState{"cut-lines", func(width, height int) string { m := usageModel(width, height) today := usageToday() today.LinesTruncated = true m, _ = m.Update(usageLoadedMsg{Gen: m.Generation, Today: today, Week: usageWeek()}) return m.View() }}, // A week a heavy account reaches: billions in one model, a cache-heavy // day, and names that do not fit their columns. shotState{"long-names", usageShotFrom(usageHeavy)}, // The accounts the ledger really records: uuids under one profile, an // account named after its profile, and one with no account at all. shotState{"uuid-accounts", usageShotFrom(usagePools)}, // A refresh that failed with the last good read still on screen. shotState{"failed-refresh", func(width, height int) string { m := usageModel(width, height) m, _ = m.Update(usageLoadedMsg{Gen: m.Generation, Err: usageError{&url.Error{ Op: "Get", URL: "http://127.0.0.1:4318/usage?days=1", Err: errors.New("dial tcp 127.0.0.1:4318: connect: connection refused"), }}}) return m.View() }}, shotState{"empty", func(width, height int) string { m := NewUsageModel(nil) m.SetSize(width, height) m, _ = m.Update(usageLoadedMsg{Gen: m.Generation}) return m.View() }}, shotState{"load-failed", func(width, height int) string { m := NewUsageModel(nil) m.SetSize(width, height) m, _ = m.Update(usageLoadedMsg{Gen: m.Generation, Err: errors.New("daemon connection unavailable")}) return m.View() }}, )}