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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443package tui
import ( "albedo/cli/internal/daemon" "context" "encoding/json" "errors" "maps" "slices" "strings"
"charm.land/bubbles/v2/textinput" tea "charm.land/bubbletea/v2")
type PageTone = daemon.PageTone
const ( TonePlain = daemon.TonePlain ToneActive = daemon.ToneActive ToneWarning = daemon.ToneWarning ToneMuted = daemon.ToneMuted)
type PageRow = daemon.PageRowtype PageAction = daemon.PageActiontype PageGlance = daemon.PageGlancetype PageDocument = daemon.PageDocument
type PageCancelMsg struct{}type PageViewChangedMsg struct{}
type pageLoadedMsg struct { Doc *PageDocument Err error Gen int}
type pageShortcutPreparedMsg struct { Prepared *daemon.PreparedPageAction Err error Gen int}
type pageActionExecutedMsg struct { Prepared *PageDocument Err error Message string Action PageAction Gen int}
type pageModeKind int
const ( modeBrowse pageModeKind = iota modeText modeChoice modeConfirm modeActions)
type PageViewModel struct { Conn *daemon.Connection Doc *PageDocument CurrentAction *PageAction SessionID string Command string SelectedID string TextInput textinput.Model page Mode pageModeKind ChoiceIndex int FieldIndex, ActionIndex int FormValues map[string]json.RawMessage Busy bool}
func NewPageViewModel(conn *daemon.Connection, sessionID, command string) PageViewModel { return PageViewModel{ Conn: conn, SessionID: sessionID, Command: command, TextInput: newField(), page: page{Generation: nextPageGeneration()}, Busy: true, }}
func (m *PageViewModel) SetSize(width, height int) { m.Width = width m.Height = height m.TextInput.SetWidth(max(10, width-20))}
func (m PageViewModel) Init() tea.Cmd { return m.loadPageCmd(m.Generation)}
func (m PageViewModel) loadPageCmd(gen int) tea.Cmd { previous := m.Doc return func() tea.Msg { if m.Conn == nil { return pageLoadedMsg{Err: errors.New("daemon connection unavailable"), Gen: gen} } doc, err := daemon.RefreshPage(context.Background(), m.Conn, m.SessionID, m.Command, previous) if err != nil { return pageLoadedMsg{Err: err, Gen: gen} }
return pageLoadedMsg{Doc: doc, Gen: gen} }}
func (m PageViewModel) executeActionCmd(act PageAction, row *PageRow, gen int) tea.Cmd { form := maps.Clone(m.FormValues) var session *daemon.Session if m.Doc != nil { session = m.Doc.Session } return func() tea.Msg { if m.Conn == nil { return pageActionExecutedMsg{Action: act, Err: errors.New("daemon connection unavailable"), Gen: gen} }
if act.Operation.OperationID == "mergeLinkGroups" { if raw, ok := form["other_workspace"]; ok { var target string if json.Unmarshal(raw, &target) == nil && session != nil { if _, hasValidator := form["other_etag"]; !hasValidator { prepared, err := daemon.PrepareLinkMerge(context.Background(), m.Conn, *session, target) return pageActionExecutedMsg{Action: act, Gen: gen, Prepared: prepared, Err: err} } } } } res, err := daemon.ExecutePageAction(context.Background(), m.Conn, daemon.PageActionRequest{Action: act, Row: row, Form: form, Session: session}) return pageActionExecutedMsg{Message: res.Message, Prepared: res.Page, Action: act, Err: err, Gen: gen} }}
func (m PageViewModel) currentRow() *PageRow { if m.Doc == nil || len(m.Doc.Rows) == 0 { return nil } return &m.Doc.Rows[m.currentIndex()]}
func (m PageViewModel) currentIndex() int { if m.Doc != nil { if i := slices.IndexFunc(m.Doc.Rows, func(r PageRow) bool { return r.ID == m.SelectedID }); i >= 0 { return i } } return 0}
// runAction marks the page busy and asks the daemon to run act.func (m *PageViewModel) runAction(act PageAction, entered string) tea.Cmd { if len(act.Fields) > 0 && m.FieldIndex < len(act.Fields) { field := act.Fields[m.FieldIndex] if entered != "" || field.Required || field.Default != nil { value, err := daemon.ParseActionField(field, entered) if err != nil { m.Error = err.Error() return nil } if m.FormValues == nil { m.FormValues = map[string]json.RawMessage{} } if value != nil { m.FormValues[field.Name] = value } } m.TextInput.Reset() m.FieldIndex++ if m.FieldIndex < len(act.Fields) { return m.promptField(act) } } m.Mode, m.CurrentAction = modeBrowse, nil m.Busy, m.Error, m.Notice = true, "", "" m.Generation = nextPageGeneration() cmd := m.executeActionCmd(act, m.currentRow(), m.Generation) m.TextInput.Reset() m.FormValues = nil m.FieldIndex = 0 return cmd}
// beginAction opens the interaction act needs: Update asks for a// confirmation first, then come text entry or a choice, or the action runs// at once. fresh clears a stale notice and error, as acting from the row// list does.func (m *PageViewModel) beginAction(act PageAction, fresh bool) tea.Cmd { if fresh { m.Notice, m.Error = "", "" } m.CurrentAction = &act if len(act.Fields) > 0 { m.FieldIndex = 0 m.FormValues = map[string]json.RawMessage{} return m.promptField(act) } return m.runAction(act, "")}
func (m PageViewModel) Update(msg tea.Msg) (PageViewModel, tea.Cmd) { switch msg := msg.(type) { case pageLoadedMsg: if !m.settle(msg.Gen, msg.Err, &m.Busy) { return m, nil } m.Doc = msg.Doc m.Error = "" if row := m.currentRow(); row != nil { m.SelectedID = row.ID } return m, nil
case pageShortcutPreparedMsg: if !m.settle(msg.Gen, msg.Err, &m.Busy) { return m, nil } prepared := msg.Prepared m.Doc = prepared.Page m.Error = "" if prepared.Request.Row != nil { m.SelectedID = prepared.Request.Row.ID } act := prepared.Request.Action if act.Confirm { m.CurrentAction = &act m.Mode = modeConfirm return m, nil } m.FormValues = prepared.Request.Form m.Generation = nextPageGeneration() m.Busy = true cmd := m.executeActionCmd(act, prepared.Request.Row, m.Generation) m.FormValues = nil return m, cmd
case pageActionExecutedMsg: if !m.settle(msg.Gen, msg.Err, &m.Busy) { return m, nil } if msg.Prepared != nil { m.Doc = msg.Prepared m.Mode = modeBrowse m.Busy = false m.CurrentAction = nil m.SelectedID = "" return m, nil } m.Notice = pageNotice(msg) m.Error = "" m.Busy = true m.Generation = nextPageGeneration() return m, tea.Batch( func() tea.Msg { return PageViewChangedMsg{} }, m.loadPageCmd(m.Generation), )
case tea.KeyPressMsg: if msg.String() == "ctrl+c" || msg.String() == "esc" { if m.Mode == modeBrowse { return m, func() tea.Msg { return PageCancelMsg{} } } m.Mode = modeBrowse m.CurrentAction = nil m.FormValues = nil m.TextInput.Reset() m.FieldIndex = 0 return m, nil } if m.Busy { return m, nil } if m.Doc == nil { if strings.ToLower(msg.String()) == "r" { m.Busy = true m.Error = "" m.Generation = nextPageGeneration() return m, m.loadPageCmd(m.Generation) } return m, nil } switch m.Mode { case modeConfirm: if msg.String() == "enter" && m.CurrentAction != nil { act := *m.CurrentAction m.CurrentAction = nil m.Mode = modeBrowse return m, m.beginAction(act, false) } case modeChoice: if opts := m.fieldChoices(); len(opts) > 0 { switch msg.String() { case "left", "up": m.ChoiceIndex = (m.ChoiceIndex - 1 + len(opts)) % len(opts) case "right", "down": m.ChoiceIndex = (m.ChoiceIndex + 1) % len(opts) case "enter": return m, m.runAction(*m.CurrentAction, opts[m.ChoiceIndex]) } } case modeText: if msg.String() != "enter" { var cmd tea.Cmd m.TextInput, cmd = m.TextInput.Update(msg) return m, cmd } if val := m.TextInput.Value(); m.CurrentAction != nil { act := *m.CurrentAction m.TextInput.Reset() return m, m.runAction(act, val) } case modeActions: switch msg.String() { case "up", "ctrl+p": m.ActionIndex = max(0, m.ActionIndex-1) case "down", "ctrl+n": m.ActionIndex = min(len(m.Doc.Actions)-1, m.ActionIndex+1) case "enter": if len(m.Doc.Actions) > 0 { act := m.Doc.Actions[m.ActionIndex] if act.Row && m.currentRow() == nil { m.Error = "Select a row for this action." break } if act.Confirm { m.CurrentAction = &act m.Mode = modeConfirm } else { return m, m.beginAction(act, true) } } } return m, nil case modeBrowse: idx := m.currentIndex() switch msg.String() { case "ctrl+a": m.Mode = modeActions m.ActionIndex = 0 return m, nil case "up", "ctrl+p": if idx > 0 { m.SelectedID = m.Doc.Rows[idx-1].ID } case "down", "ctrl+n": if idx < len(m.Doc.Rows)-1 { m.SelectedID = m.Doc.Rows[idx+1].ID } default: for _, act := range m.Doc.Actions { if act.Key != msg.String() { continue } if act.Row && m.currentRow() == nil { continue } if act.Confirm { m.Notice, m.Error = "", "" m.Mode = modeConfirm m.CurrentAction = &act return m, nil } return m, m.beginAction(act, true) } } } } if m.Mode == modeText && m.TextInput.Focused() { var cmd tea.Cmd m.TextInput, cmd = m.TextInput.Update(msg) return m, cmd } return m, nil}
func (m *PageViewModel) promptField(act PageAction) tea.Cmd { field := act.Fields[m.FieldIndex] var session *daemon.Session if m.Doc != nil { session = m.Doc.Session } resolved, err := daemon.ResolveFormField(field, m.currentRow(), session) if err != nil { m.Error = err.Error() m.Mode = modeBrowse return nil } field = resolved act.Fields = slices.Clone(act.Fields) act.Fields[m.FieldIndex] = field m.CurrentAction = &act if field.Type == "hidden" { return m.runAction(act, "") } if field.Type == "choice" || field.Type == "boolean" { m.Mode = modeChoice m.ChoiceIndex = daemon.FormChoiceDefault(field) return nil } m.Mode = modeText initial := "" if field.Default != nil { if json.Unmarshal(field.Default, &initial) != nil && string(field.Default) != "null" { initial = string(field.Default) } } return ask(&m.TextInput, initial, field.Type == "secret")}
func (m PageViewModel) currentField() daemon.FormField { if m.CurrentAction != nil && m.FieldIndex < len(m.CurrentAction.Fields) { return m.CurrentAction.Fields[m.FieldIndex] } return daemon.FormField{}}
func (m PageViewModel) fieldChoices() []string { field := m.currentField() if field.Type == "boolean" { return []string{"false", "true"} } labels := make([]string, len(field.Choices)) for i, choice := range field.Choices { labels[i] = choice.Label } return labels}
func (m PageViewModel) shortcutCmd(arguments string) tea.Cmd { conn, session, command, generation := m.Conn, m.SessionID, m.Command, m.Generation return func() tea.Msg { prepared, err := daemon.PreparePageShortcut(context.Background(), conn, session, command, arguments) return pageShortcutPreparedMsg{Prepared: prepared, Err: err, Gen: generation} }}