Files
pipelines/internal/tui/panels.go
T
alan c42a19f7d3 Improve workflow UI, retain stage diffs, and add portable pipeline transfers
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
2026-10-10 20:55:42 +01:00

495 lines
16 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tui
import (
"fmt"
"sort"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/ansi"
"git.a-wan.net/alan/pipelines/internal/protocol"
stageconfig "git.a-wan.net/alan/pipelines/internal/stage"
)
type paneGeometry struct {
graphWidth, graphHeight, stageWidth, stageHeight int
}
func (m Model) pipelineGeometry(height int) paneGeometry {
remaining := max(0, height-len(m.pipelineToolbar()))
if m.stageFocus {
if remaining < 14 {
return paneGeometry{stageWidth: m.width, stageHeight: remaining}
}
return paneGeometry{graphWidth: m.width, graphHeight: 2, stageWidth: m.width, stageHeight: remaining - 2}
}
if remaining < 14 {
return paneGeometry{graphWidth: m.width, graphHeight: remaining}
}
return paneGeometry{graphWidth: m.width, graphHeight: remaining - 2, stageWidth: m.width, stageHeight: 2}
}
func fitBlock(lines []string, width, height int) string {
if height <= 0 {
return ""
}
rows := make([]string, height)
for i := range rows {
if i < len(lines) {
rows[i] = ansi.Truncate(lines[i], max(1, width), "…")
}
rows[i] += strings.Repeat(" ", max(0, width-lipgloss.Width(rows[i])))
}
return strings.Join(rows, "\n")
}
func panel(width, height int, headers, body []string, footer string, focused bool) string {
if height <= 0 || width <= 0 {
return ""
}
if width < 6 || height < len(headers)+3 {
return fitBlock(append(append([]string{}, headers...), body...), width, height)
}
inner := width - 4
contentHeight := height - len(headers) - 3
lines := append([]string{}, headers...)
visible := strings.Split(fitBlock(body, inner, contentHeight), "\n")
if contentHeight > 0 {
lines = append(lines, visible...)
}
lines = append(lines, lipgloss.NewStyle().Foreground(themeMuted).Render(footer))
return lipgloss.NewStyle().Background(themeSurface).Foreground(themeText).
Padding(1, 2).Width(width).Render(surfaceContent(fitBlock(lines, inner, height-2), themeSurface))
}
func bar(title, right string, width int, focused bool) string {
left := ansi.Truncate(title, max(1, width), "…")
if lipgloss.Width(left)+lipgloss.Width(right)+1 <= width {
left += strings.Repeat(" ", width-lipgloss.Width(left)-lipgloss.Width(right)) + lipgloss.NewStyle().Foreground(themeMuted).Bold(false).Render(right)
}
left += strings.Repeat(" ", max(0, width-lipgloss.Width(left)))
s := lipgloss.NewStyle().Foreground(themeText).Bold(true)
if focused {
s = s.Foreground(themeAccent)
}
return s.Render(left)
}
func dateLabel(value string) string {
if stamp, err := time.Parse(time.RFC3339, value); err == nil {
return stamp.UTC().Format("02 Jan 2006 15:04") + " UTC"
}
if value == "" {
return "—"
}
return value
}
func (m Model) pipelineToolbar() []string {
d := m.detail
if d == nil {
return []string{"", "", ""}
}
state := lipgloss.NewStyle().Foreground(themeSuccess).Bold(true).Render("LATEST")
if d.Version != d.LatestVersion {
state = lipgloss.NewStyle().Foreground(themeAccent).Render(fmt.Sprintf("HISTORY · latest v%d", d.LatestVersion))
}
buttons := action(fmt.Sprintf("v Version %d ▾", d.Version), true) + " " + action("e Edit source", false) + " " + action("Esc Home", false)
if lipgloss.Width(buttons)+lipgloss.Width(state)+1 <= m.width {
buttons += strings.Repeat(" ", m.width-lipgloss.Width(buttons)-lipgloss.Width(state)) + state
}
meta := lipgloss.NewStyle().Foreground(themeMuted).Render("Created " + dateLabel(d.CreatedAt) + " · Updated " + dateLabel(d.UpdatedAt))
if settings := d.Graph.Settings; settings != nil {
var schedules []string
for _, rule := range settings.Schedules {
when := "at " + strings.Join(rule.Times, ",")
if rule.Every != "" {
when = "every " + rule.Every
}
days := "everyday"
if len(rule.Days) > 0 {
days = strings.Join(rule.Days, ",")
}
zone := rule.Timezone
if zone == "" {
zone = "server local"
}
schedules = append(schedules, days+" "+when+" ("+zone+")")
}
meta = "Schedule: " + strings.Join(schedules, "; ")
if d.Version != d.LatestVersion {
meta += " · historical, inactive"
}
}
if m.width < 80 {
short := action(fmt.Sprintf("v Version %d", d.Version), true) + " " + action("e Edit", false)
result := "Not run yet · no recorded results"
if run := m.latestPipelineRun(d.ID, d.Version); run != nil {
result = resultLabel(*run)
}
lines := []string{bar(short, state, m.width, false), ansi.Truncate(result, max(1, m.width), "…")}
if d.Graph.Settings != nil && len(d.Graph.Settings.Schedules) > 0 {
lines = append(lines, ansi.Truncate(meta, max(1, m.width), "…"))
}
return lines
}
lines := []string{buttons}
lines = append(lines, m.pipelineResultLines()...)
return append(lines, meta, "")
}
func (m Model) pipelineView(height int) string {
if m.detailLoading {
label := "Loading pipeline…"
if m.requestedVersion > 0 {
label = fmt.Sprintf("Loading version %d…", m.requestedVersion)
}
return fitBlock([]string{m.loading(label)}, m.width, height)
}
if m.detailError != "" {
return fitBlock([]string{lipgloss.NewStyle().Foreground(themeError).Render("Cannot load pipeline: " + m.detailError), "Esc returns home."}, m.width, height)
}
if m.detail == nil {
return fitBlock([]string{"No pipeline selected."}, m.width, height)
}
toolbar := m.pipelineToolbar()
if height <= 3 {
return fitBlock(toolbar, m.width, height)
}
g := m.pipelineGeometry(height)
graph := m.graphPanel(g.graphWidth, g.graphHeight)
stages := m.stagePanel(g.stageWidth, g.stageHeight)
content := graph
if graph != "" && stages != "" {
content = graph + "\n" + stages
} else if stages != "" {
content = stages
}
return fitBlock(append(toolbar, strings.Split(content, "\n")...), m.width, height)
}
func (m Model) selectedStage() *protocol.Stage {
if m.detail == nil || len(m.detail.Graph.Stages) == 0 {
return nil
}
index := max(0, min(m.stageIndex, len(m.detail.Graph.Stages)-1))
return &m.detail.Graph.Stages[index]
}
func (m Model) graphLines() []string {
if m.detail == nil {
return nil
}
selected := m.selectedStage()
if selected == nil {
return renderGraph(m.detail.Graph)
}
return renderGraph(m.detail.Graph, selected.Key)
}
func (m Model) graphPanel(width, height int) string {
if height <= 2 && height > 0 {
return fitBlock([]string{bar("Graph", "Tab to expand", width, false)}, width, height)
}
label := "Graph"
if !m.stageFocus {
label = "▸ " + label
}
return graphViewport(label, m.graphLines(), width, height, m.pipelineTop, m.graphPan, !m.stageFocus, "Tab pane · r runs · * fallback error")
}
// Shared graph viewport for definitions and executed run paths.
func graphViewport(label string, lines []string, width, height, scrollTop, pan int, focused bool, footer string) string {
if width <= 0 || height <= 0 {
return ""
}
inner := max(1, width-4)
rows := max(0, height-5)
top := max(0, min(scrollTop, max(0, len(lines)-rows)))
end := min(len(lines), top+rows)
graphWidth := 0
for _, line := range lines {
graphWidth = max(graphWidth, ansi.StringWidth(line))
}
pan = max(0, min(pan, max(0, graphWidth-inner)))
leftPad, topPad := max(0, (inner-graphWidth)/2), max(0, (rows-len(lines))/2)
body := make([]string, topPad)
for _, line := range lines[top:end] {
body = append(body, strings.Repeat(" ", leftPad)+ansi.Cut(line, pan, pan+inner-leftPad))
}
if pan > 0 {
label = "← " + label
}
if pan+inner < graphWidth {
label += " →"
}
headers := []string{runPaneTitle(strings.TrimPrefix(label, "▸ "), fmt.Sprintf("%d–%d / %d", min(top+1, len(lines)), end, len(lines)), inner, focused), ""}
if graphWidth > inner {
footer = "←/→ pan · " + footer
}
return panel(width, height, headers, body, footer, focused)
}
func (m Model) stageTabs(width int) string {
if m.selectedStage() == nil {
return "No stages"
}
stages := m.detail.Graph.Stages
index := max(0, min(m.stageIndex, len(stages)-1))
if width < 4 {
return stageTab(stages[index].Key, ansi.Truncate(fmt.Sprintf("%d %s", index+1, stages[index].Key), max(1, width), "…"), true, m.detail.Graph)
}
start := index
labelFor := func(i int) string {
label := fmt.Sprintf("%d %s", i+1, stages[i].Key)
if i == index && width < lipgloss.Width(label)+2 {
return stages[i].Key
}
return label
}
used := min(width, lipgloss.Width(labelFor(index))+2)
for start > 0 {
prior := lipgloss.Width(labelFor(start-1)) + 2
marker := 0
if start > 1 {
marker = 2
}
if used+prior+marker > width {
break
}
start--
used += prior
}
result := ""
if start > 0 && used+2 <= width {
result = lipgloss.NewStyle().Foreground(themeMuted).Render("‹ ")
}
for i := start; i < len(stages); i++ {
label := labelFor(i)
if i == index {
label = ansi.Truncate(label, max(1, width-lipgloss.Width(result)-2), "…")
}
tab := stageTab(stages[i].Key, " "+label+" ", i == index, m.detail.Graph)
if lipgloss.Width(result)+lipgloss.Width(tab) > width {
if i > index && lipgloss.Width(result)+2 <= width {
result += " ›"
}
break
}
result += tab
}
return ansi.Truncate(result, width, "…")
}
type stageField struct{ title, text string }
func (m Model) stageFields() []stageField {
stage := m.selectedStage()
if stage == nil {
return nil
}
keys := make([]string, 0, len(stage.Configs))
for k := range stage.Configs {
keys = append(keys, k)
}
sort.Strings(keys)
configs := make([]string, 0, len(keys))
if stage.Type != "" {
configs = append(configs, lipgloss.NewStyle().Foreground(themeMuted).Render("type ")+stage.Type)
}
for _, k := range keys {
configs = append(configs, lipgloss.NewStyle().Foreground(themeMuted).Render(k+" ")+stage.Configs[k])
}
if stage.Configs["timeout"] == "" {
configs = append(configs, fmt.Sprintf("timeout %s (default per attempt)", stageconfig.DefaultTimeout))
}
if stage.Type == "" || stage.Type == stageconfig.ModelType {
if stage.Configs["retries"] == "" {
configs = append(configs, fmt.Sprintf("retries %d (default)", stageconfig.DefaultRetries))
}
for _, key := range []string{"budget_usd", "budget_tokens"} {
if stage.Configs[key] == "" {
configs = append(configs, key+" unlimited")
}
}
}
var edges []graphEdge
maxLabel := 0
for _, edge := range graphEdges(m.detail.Graph) {
if edge.source != stage.Key {
continue
}
edges = append(edges, edge)
maxLabel = max(maxLabel, lipgloss.Width(outcomeLabel(edge.outcome, fmt.Sprintf("%s [%d]", edge.label, edge.id))))
}
routes := make([]string, 0, len(edges))
for _, edge := range edges {
label := outcomeLabel(edge.outcome, fmt.Sprintf("%s [%d]", edge.label, edge.id))
padding := strings.Repeat(" ", maxLabel-lipgloss.Width(label))
target := nodeLabel(edge.target)
routes = append(routes, label+padding+" "+lipgloss.NewStyle().Foreground(themeMuted).Render("→")+" "+stageTitle(edge.target, target, false, m.detail.Graph))
}
fields := []stageField{{"OUTPUT", ansi.Strip(stage.Output)}}
if stage.Type == "" || stage.Type == "model" {
fields = append(fields, stageField{"PROMPT", ansi.Strip(stage.Prompt)})
}
fields = append(fields, stageField{"CONFIGURATION", strings.Join(configs, "\n")}, stageField{"OUTCOMES", strings.Join(routes, "\n")})
if tools, ok := stage.Blocks["tools"]; ok {
if strings.TrimSpace(tools) == "" {
tools = "None"
}
fields = append(fields, stageField{"TOOLS", ansi.Strip(tools)})
}
if policy, ok := stage.Blocks["permissions"]; ok {
fields = append(fields, stageField{"PERMISSIONS", ansi.Strip(policy)})
}
if stage.Notify != nil {
fields = append(fields, stageField{"NOTIFY", "On: " + strings.Join(stage.Notify.On, ", ") + "\nConnection: global\nArtifacts: " + strings.Join(stage.Notify.Artifacts, ", ")})
}
return fields
}
func (m Model) stageColumns(width int) (string, []string) {
header, rows, _ := m.stageColumnsWithHeaders(width)
return header, rows
}
func (m Model) stageColumnsWithHeaders(width int) (string, []string, []string) {
fields := m.stageFields()
if len(fields) == 0 {
return "Stages", []string{"No stages."}, []string{"Stages"}
}
order := map[string]int{"OUTPUT": 0, "TOOLS": 1, "OUTCOMES": 2, "NOTIFY": 3, "PROMPT": 4, "PERMISSIONS": 5, "CONFIGURATION": 6}
sort.SliceStable(fields, func(i, j int) bool { return order[fields[i].title] < order[fields[j].title] })
var header string
var result []string
var rowHeaders []string
selected := m.selectedStage()
collapsedConfig := !m.stageConfigExpanded && selected != nil && (selected.Type == "" || selected.Type == stageconfig.ModelType)
for i, field := range fields {
title := field.title
if title == "CONFIGURATION" && collapsedConfig {
title = "▸ CONFIGURATION · c expand"
}
label := lipgloss.NewStyle().Foreground(themeAccent).Bold(true).Render(title)
if i == 0 {
header = label
} else {
result = append(result, "", label)
rowHeaders = append(rowHeaders, label, label)
}
if field.title == "CONFIGURATION" && collapsedConfig {
continue
}
for _, line := range strings.Split(ansi.Wrap(field.text, max(1, width), ""), "\n") {
result = append(result, line)
rowHeaders = append(rowHeaders, label)
}
}
return header, result, rowHeaders
}
func (m Model) stageLines(width int) []string {
_, rows := m.stageColumns(width)
return rows
}
func (m Model) stagePanel(width, height int) string {
if width <= 0 || height <= 0 {
return ""
}
if width < 80 {
inner := max(1, width-4)
heading, lines, labels := m.stageColumnsWithHeaders(inner)
page := max(0, height-4)
top := max(0, min(m.stageTop, max(0, len(lines)-page)))
if top < len(labels) {
heading = labels[top]
}
rows := []string{runPaneTitle("Stages", "", width, m.stageFocus), m.stageTabs(width), heading}
rows = append(rows, lines[top:min(len(lines), top+page)]...)
for len(rows) < height-1 {
rows = append(rows, "")
}
rows = append(rows, bar("c configuration", fmt.Sprintf("%d–%d / %d", top+1, min(top+page, len(lines)), len(lines)), width, false))
return fitBlock(rows, width, height)
}
inner := max(1, width-4)
rows := max(0, height-6)
columns, lines, rowHeaders := m.stageColumnsWithHeaders(inner)
top := max(0, min(m.stageTop, max(0, len(lines)-rows)))
if top < len(rowHeaders) {
columns = rowHeaders[top]
}
label := "Stages"
if m.stageFocus {
label = "▸ " + label
}
headers := []string{runPaneTitle(strings.TrimPrefix(label, "▸ "), fmt.Sprintf("%d / %d · rows %d–%d/%d", m.stageIndex+1, len(m.detail.Graph.Stages), min(top+1, len(lines)), min(len(lines), top+rows), len(lines)), inner, m.stageFocus), m.stageTabs(inner), columns}
return panel(width, height, headers, lines[top:min(len(lines), top+rows)], "c configuration · stage definition, not a run result", m.stageFocus)
}
func (m *Model) resetPipelineView() {
m.pipelineTop = 0
m.graphPan = 0
m.stageIndex = 0
m.stageTop = 0
m.stageFocus = false
m.revealStage()
}
func (m *Model) revealStage() {
if m.detail == nil {
return
}
if stage := m.selectedStage(); stage != nil {
row := graphStageRow(m.detail.Graph, stage.Key)
page := max(1, m.pipelineGeometry(max(0, m.height-4)).graphHeight-5)
if row < m.pipelineTop {
m.pipelineTop = row
} else if row >= m.pipelineTop+page {
m.pipelineTop = row - page + 1
}
if !compactGraph(m.detail.Graph) {
width := max(1, m.pipelineGeometry(max(0, m.height-4)).graphWidth-4)
for _, node := range layoutGraph(m.detail.Graph).nodes {
if node.key != stage.Key {
continue
}
visible := min(page, node.height)
if node.y < m.pipelineTop {
m.pipelineTop = node.y
} else if node.y+visible > m.pipelineTop+page {
m.pipelineTop = node.y + visible - page
}
if node.x < m.graphPan {
m.graphPan = node.x
} else if node.x+node.width > m.graphPan+width {
m.graphPan = node.x + node.width - width
}
}
}
}
}
func (m *Model) clampPipelineScroll() {
if m.detail == nil {
return
}
m.stageIndex = max(0, min(m.stageIndex, max(0, len(m.detail.Graph.Stages)-1)))
g := m.pipelineGeometry(max(0, m.height-4))
m.pipelineTop = max(0, min(m.pipelineTop, max(0, len(m.graphLines())-max(1, g.graphHeight-5))))
stagePage := g.stageHeight - 6
if g.stageWidth < 80 {
stagePage = g.stageHeight - 4
}
m.stageTop = max(0, min(m.stageTop, max(0, len(m.stageLines(max(1, g.stageWidth-4)))-max(1, stagePage))))
width := 0
for _, line := range m.graphLines() {
width = max(width, lipgloss.Width(line))
}
m.graphPan = max(0, min(m.graphPan, max(0, width-max(1, g.graphWidth-4))))
}