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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176// Package terminalclient owns the Bubble Tea event loop, screen routing and// terminal frame. Each mounted screen keeps its own input and request state.package terminalclient
import ( "albedo/cli/internal/terminalui" "strings" "time"
"bloomfx.dev/bloom/anim" tea "charm.land/bubbletea/v2")
type Route int
const ( Chat Route = iota Sessions Models Extensions Tree Context Page Accounts Capabilities Webhooks Discord Config Agents Folders Quota Usage Settings RouteCount)
// Screen binds a model's three terminal operations to the same route.type Screen struct { Page func() terminalui.Page Draw func() string Input func(tea.Msg) tea.Cmd Resize func(int, int)}
func BindPage[T any, P interface { *T Update(tea.Msg) (T, tea.Cmd) Page() terminalui.Page SetSize(int, int)}](p P) Screen { return Screen{Page: p.Page, Resize: p.SetSize, Input: func(msg tea.Msg) tea.Cmd { next, cmd := p.Update(msg) *p = next return cmd }}}
type Workspace interface { Start() tea.Cmd Route() Route Receive(tea.Msg) (bool, tea.Cmd) Size(int, int) WindowTitle() string Overlay(string) string Attached() *Client}
type Client struct { painted *tea.View theme func() terminalui.Chrome workspace Workspace screens [RouteCount]Screen entrance anim.Presence width, height int focused bool}
func New(workspace Workspace, screens [RouteCount]Screen, theme func() terminalui.Chrome) *Client { return &Client{workspace: workspace, screens: screens, theme: theme, focused: true}}
func (c *Client) Init() tea.Cmd { return c.workspace.Start() }
func (c *Client) visible() *Client { for c.workspace.Attached() != nil { c = c.workspace.Attached() } return c}
// Dispatch also serves attached daemons. Only the outermost client schedules// presentation frames, so a forwarded message cannot start a second animation.func (c *Client) Dispatch(msg tea.Msg) tea.Cmd { c.painted = nil if handled, cmd := c.workspace.Receive(msg); handled { return cmd } if size, ok := msg.(tea.WindowSizeMsg); ok { c.width, c.height = size.Width, size.Height c.workspace.Size(size.Width, size.Height) for _, s := range c.screens { s.Resize(size.Width, size.Height) } return nil } return c.screens[c.workspace.Route()].Input(msg)}
func (c *Client) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg.(type) { case anim.FrameMsg: return c, c.entrance.Update(msg) case tea.FocusMsg: c.focused = true case tea.BlurMsg: c.focused = false c.entrance = anim.Presence{} } before := c.visible() route := before.workspace.Route() cmd := c.Dispatch(msg) after := c.visible() if c.focused && (before != after || route != after.workspace.Route()) && after.workspace.Route() != Chat { c.entrance = anim.NewPresence(anim.Duration(180*time.Millisecond), anim.FPS(24)) cmd = tea.Batch(cmd, c.entrance.Show()) } return c, cmd}
func (c *Client) View() tea.View { host := c.visible() route := host.workspace.Route() animated := c.focused && route != Chat && c.entrance.Animating() if c.painted == nil || !animated { screen := host.screens[route] var body string if screen.Page != nil { body = screen.Page().Render(host.width, host.height, host.theme()) } else { body = screen.Draw() } view := tea.NewView(host.workspace.Overlay(body)) view.WindowTitle = host.workspace.WindowTitle() view.ReportFocus = true if route == Chat { view.MouseMode = tea.MouseModeCellMotion } if !animated { return view } c.painted = &view } view := *c.painted header, body, ok := strings.Cut(view.Content, "\n") if ok { progress := c.entrance.Progress() glyph := "·" if progress >= 0.45 { glyph = "✧" } header = strings.Replace(header, "✦", glyph, 1) // Pulse the existing rule without shifting the title or the filter below. offset := 0 for range int(progress * 12) { i := strings.Index(header[offset:], "─") if i < 0 { break } offset += i + len("─") } header = header[:offset] + strings.Replace(header[offset:], "─", "━", 1) view.SetContent(header + "\n" + body) } return view}