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.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485package tui
import ( "cmp" "context" "slices"
"albedo/cli/internal/daemon"
tea "charm.land/bubbletea/v2")
// termsLoadedMsg carries the daemon's sign-ins, for their terms warnings.type termsLoadedMsg struct { signIns []daemon.SignIn err error}
// termsSeenMsg says the terms warning id was answered with a message sent.type termsSeenMsg struct{ ID string }
// termsSavedMsg is the daemon's answer to saving a dismissed warning.type termsSavedMsg struct { prefs daemon.UIPreferences err error}
func (m *AppModel) loadTermsCmd() tea.Cmd { conn := m.Conn if conn == nil { return nil } return func() tea.Msg { listed, err := daemon.SignInList(context.Background(), conn) return termsLoadedMsg{signIns: listed.Logins, err: err} }}
func (m *AppModel) handleTerms(msg tea.Msg) (tea.Cmd, bool) { switch msg := msg.(type) { case termsLoadedMsg: // Without the list no warning shows until a login screen reads it. if msg.err == nil { m.signIns = msg.signIns } return nil, true case termsSeenMsg: if !slices.Contains(m.UI.DismissedNotices, msg.ID) { m.UI.DismissedNotices = append(slices.Clip(m.UI.DismissedNotices), msg.ID) } conn, id := m.Conn, msg.ID return func() tea.Msg { prefs, err := daemon.DismissNotice(context.Background(), conn, id) return termsSavedMsg{prefs: prefs, err: err} }, true case termsSavedMsg: if msg.err != nil { m.AddError("Could not save that the terms warning was seen: " + msg.err.Error()) return nil, true } m.ApplyUI(msg.prefs) return nil, true } return nil, false}
// termsFor is the terms warning a message on profile still owes: the one its// sign-in carries, while the profile has no API key of its own and the// warning was never dismissed.func (m *AppModel) termsFor(profile string) *daemon.Terms { settings, known := m.Profiles.Providers[cmp.Or(profile, m.Profiles.Active)] if !known || settings.HasKey { return nil } i := slices.IndexFunc(m.signIns, func(s daemon.SignIn) bool { return s.Provider == settings.Extension }) if i < 0 { return nil } terms := m.signIns[i].Terms if terms == nil || slices.Contains(m.UI.DismissedNotices, terms.ID) { return nil } return terms}