package agents import ( "albedo/cli/internal/daemon" "albedo/cli/internal/terminalui" "fmt" "maps" "slices" "strings" "albedo/cli/internal/tui/chrome" "github.com/charmbracelet/x/ansi" ) func (m Model) paneHeader() []string { // One column goes to the space after the divider. width := m.paneWidth() - 1 if width <= 0 { width = max(1, m.Width-2) } rows := make([]string, 0, 12) if f := m.folds[m.selected]; f != nil { return m.foldPane(f, width) } n := m.nodes[m.selected] if n == nil || width <= 0 { return rows } colors := chrome.AgentColors() styled := func(hue chrome.RGB, s string) string { return chrome.Sgr(hue.Hex(), "") + s + chrome.AnsiReset } state := "idle" switch { case n.id == agentsYou: state = "that's you" case n.closed: state = "closed" case n.session.Status.Phase != nil: switch *n.session.Status.Phase { case daemon.PhasePreparing: state = "preparing" case daemon.PhaseInterrupted: state = "interrupting" case daemon.PhaseIdle, daemon.PhaseResting: state = "idle" default: state = "running" } case n.running: state = "running" case n.peer: state = "outside this tree" } rows = append(rows, styled(n.hue, "● ")+chrome.DefaultStyles.Bold.Render(n.name)+" "+chrome.DefaultStyles.Faint.Render(state)) var meta []string if n.model != "" { meta = append(meta, n.model) } if n.id != agentsYou { meta = append(meta, fmt.Sprintf("depth %d", n.depth)) } if parent := m.nodes[n.parent]; parent != nil { meta = append(meta, "parent "+parent.name) } rows = append(rows, chrome.DefaultStyles.Faint.Render(ansi.Truncate(strings.Join(meta, " · "), width, "…"))) if usage := providerCall(n.usage); usage != "" { rows = append(rows, chrome.DefaultStyles.Faint.Render(ansi.Truncate(usage, width, "…"))) } rows = append(rows, "") for _, field := range []struct{ label, text string }{{"request", n.currentRequest}, {"latest", n.latestProgress}} { if field.text == "" { continue } rows = append(rows, chrome.DefaultStyles.Muted.Render(field.label)) wrapped := strings.Split(ansi.Wrap(field.text, max(1, width), " "), "\n") if len(wrapped) > 3 { wrapped = wrapped[:3] wrapped[2] = ansi.Truncate(wrapped[2], max(1, width-1), "") + "…" } rows = append(rows, wrapped...) } if len(n.mail) > 0 { rows = append(rows, chrome.DefaultStyles.Muted.Render("mail")) for _, mail := range n.mail[max(0, len(n.mail)-5):] { var arrow string if mail.incoming { arrow = styled(colors.Mail, "← ") } else { arrow = chrome.DefaultStyles.Faint.Render("→ ") } rows = append(rows, ansi.Truncate(arrow+mail.who+" "+chrome.DefaultStyles.Faint.Render(mail.kind), width, "…")) } rows = append(rows, "") } live := chrome.DefaultStyles.Muted.Render("tail") if n.running { live += " " + styled(n.hue, "●") + chrome.DefaultStyles.Faint.Render(" live") } if n.tail.omitted || n.preview.omitted { live += chrome.DefaultStyles.Faint.Render(" · earlier output omitted") } return append(rows, live) } // providerCall words what the provider reported for an agent's latest call, or // is empty when no call was reported. func providerCall(usage *daemon.Usage) string { if usage == nil { return "" } var parts []string if usage.PromptTokens != nil { parts = append(parts, compactCount(*usage.PromptTokens)+" in") } if usage.CompletionTokens != nil { parts = append(parts, compactCount(*usage.CompletionTokens)+" out") } return "last call " + strings.Join(parts, " · ") + " tok" } // foldPane lists a fold's closed agents, newest turn first, ending in a // blank row where an agent's pane has its tail heading. func (m Model) foldPane(f *agentFold, width int) []string { glyph, verb := "▸ ", "enter or → shows them" if f.open { glyph, verb = "▾ ", "enter or ← folds them" } rows := []string{ chrome.DefaultStyles.Faint.Render(glyph) + chrome.DefaultStyles.Bold.Render(fmt.Sprintf("%d closed agents", len(f.members))), chrome.DefaultStyles.Faint.Render(ansi.Truncate("under "+m.label(f.parent, "")+" · "+verb, width, "…")), "", } for _, id := range f.members { n := m.nodes[id] rows = append(rows, ansi.Truncate(chrome.DefaultStyles.Faint.Render("✓ ")+n.name+chrome.DefaultStyles.Faint.Render(" "+n.model), width, "…")) } return append(rows, "") } func (m Model) pane(height int) []string { rows := m.paneHeader() if m.folds[m.selected] != nil { return rows } room := max(0, height-len(rows)) if room == 0 || len(rows) == 0 { return rows } wrapped := m.tailCache.rows if len(wrapped) == 0 { return append(rows, chrome.DefaultStyles.Faint.Render("nothing yet")) } return append(rows, wrapped[max(0, len(wrapped)-room):]...) } // drawTail wraps one tail line to width and styles it by kind. func drawTail(line tailLine, width, limit int) []string { gutter, inner := "", width format := func(s ...string) string { return strings.Join(s, "") } switch line.kind { case tailThinking: format = func(s ...string) string { return chrome.DefaultStyles.Faint.Render(chrome.AnsiItalic + s[0]) } case tailCode: gutter, inner, format = chrome.DefaultStyles.Decor.Render("│ "), width-2, chrome.DefaultStyles.Muted.Render case tailOutput: gutter, inner, format = chrome.DefaultStyles.Decor.Render("⎿ "), width-2, chrome.DefaultStyles.Faint.Render case tailMeta: format = chrome.DefaultStyles.Faint.Render } wrapped := ansi.Hardwrap(line.text, max(8, inner), true) start := len(wrapped) for range limit { index := strings.LastIndexByte(wrapped[:start], '\n') if index < 0 { start = 0 break } start = index } if start > 0 { start++ } var out []string for part := range strings.SplitSeq(wrapped[start:], "\n") { // A cached row must not retain the rest of a long wrapped line. out = append(out, gutter+format(strings.Clone(part))) } return out } // graphOffset scrolls a tall swarm so the selected dot and its children's // dots stay in view, the selected one first when both do not fit. func (m Model) graphOffset(height int) int { _, y, ok := m.at(m.selected) if !ok { return 0 } bottom := y + 3 for _, child := range m.nodes { if child.parent == m.selected { bottom = max(bottom, child.y+2) } } return max(0, min(bottom-height, y-1)) } func (m Model) View() string { if m.Width <= 0 || m.Height <= 0 { return "" } live, written := 0, 0 for id, n := range m.nodes { if id == agentsYou { continue } if n.running { live++ } written += n.outputBytes } right := chrome.DefaultStyles.Faint.Render(fmt.Sprintf("%d agents · %d running · ▴%s out", len(m.nodes)-1, live, compactBytes(written))) page := terminalui.Graph{ Header: chrome.TitleRule(m.Width-1, chrome.Brand("albedo")+" "+chrome.DefaultStyles.Muted.Render("/agents"), right), Divider: chrome.DefaultStyles.Decor.Render("│"), Detail: m.pane, Canvas: func(w, h int) []string { if h == 0 { return nil } canvas := newCanvas(w, h, m.graphOffset(h)) m.drawEdges(canvas) m.drawNodes(canvas) lines := make([]string, h) for i := range lines { lines[i] = canvas.line(i) } return lines }, } if m.paneWidth() == 0 { // The stacked detail has no live tail. Omit its tail heading. header := m.paneHeader() header = header[:max(0, len(header)-1)] for len(header) > 0 && header[len(header)-1] == "" { header = header[:len(header)-1] } page.Summary = header page.Rule = chrome.DefaultStyles.Decor.Render(strings.Repeat("─", max(0, m.Width-2))) } status := "" switch { case m.rename.Active(): status = chrome.DefaultStyles.Muted.Render("rename ") + chrome.DefaultStyles.Bold.Render(m.label(m.rename.ID, "")) if n := m.nodes[m.rename.ID]; n != nil && n.address != "" { status += chrome.DefaultStyles.Faint.Render(" · its family can still mail it at ") + chrome.DefaultStyles.Muted.Render(n.address) } case m.err != nil: status = chrome.DefaultStyles.Error.Render(m.err.Error() + " · ctrl+l retries") case len(m.pendingOperations) > 0: ids := slices.Sorted(maps.Keys(m.pendingOperations)) status = chrome.DefaultStyles.Error.Render("Unresolved request: " + strings.Join(ids, ", ")) case m.noticeT > 0: status = chrome.DefaultStyles.Muted.Render(m.notice) } if m.confirm.Asking() { page.Actions = strings.Split(m.confirm.Footer(max(1, m.Width), ""), "\n") } else { page.Actions = append(page.Actions, " "+status) } target := m.label(m.selected, "agent") input := m.input.View() switch { case m.input.Value() != "": case m.folds[m.selected] != nil: input = chrome.DefaultStyles.Faint.Render("closed agents, folded · pick one to message it") default: input = chrome.DefaultStyles.Faint.Render("message " + target + "… or /spawn ") } hints := []chrome.Hint{{Key: "tab", Does: "next agent"}, {Key: "enter", Does: "open or send"}, {Key: "ctrl+r", Does: "rename"}, {Key: "ctrl+x", Does: "delete"}, {Key: "ctrl+f", Does: "find"}, {Key: "esc", Does: "back"}} if f := m.folds[m.selected]; f != nil { hints = []chrome.Hint{{Key: "tab", Does: "next agent"}, {Key: "enter", Does: "show"}, {Key: "ctrl+f", Does: "find"}, {Key: "esc", Does: "back"}} if f.open { hints[1].Does = "fold" } } switch n := m.nodes[m.rename.ID]; { case m.find.active(): page.Actions = append(page.Actions, " "+m.findLine(), chrome.FooterLine(m.Width, []chrome.Hint{{Key: "↑↓", Does: "next match"}, {Key: "enter", Does: "go there"}, {Key: "esc", Does: "back"}}, "", false)) case n != nil && m.rename.Active(): page.Actions = append(page.Actions, " "+chrome.DefaultStyles.Prompt.Render("✎ ")+m.rename.View(m.Width-chrome.PromptMarkWidth-1), " "+chrome.RenameHints("restores "+renameBlank(n))) default: page.Actions = append(page.Actions, " "+chrome.PromptLead()+input, chrome.FooterLine(m.Width, hints, "", false)) } return page.Render(m.Width, m.Height) }