package tui import ( "fmt" "maps" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" ) func resultLabel(r protocol.RunSummary) string { preview := ansi.Strip(r.ResultPreview) if r.Status == "succeeded" { if preview != "" { return "Result: " + preview } return "Finished · no result recorded" } if r.Diagnostic != "" { return "Stopped: " + ansi.Strip(r.Diagnostic) } if preview != "" { return "Latest output: " + preview } if r.Status == protocol.RunStatusWaiting { if r.CurrentStage != "" { return "Waiting for a person · stage " + r.CurrentStage } return "Waiting for a person" } if r.Status == "running" || r.Status == "queued" { return "In progress · awaiting stage output" } return "No result recorded" } func (m Model) latestPipelineRun(id int64, version int) *protocol.RunSummary { var latest *protocol.RunSummary for i := range m.runs { r := &m.runs[i] if r.PipelineID == id && (version == 0 || r.Version == version) && (latest == nil || r.ID > latest.ID) { latest = r } } return latest } func (m Model) pipelineResultLines() []string { if m.detail == nil { return nil } r := m.latestPipelineRun(m.detail.ID, m.detail.Version) if r == nil { return []string{"Results · This version has not run yet. No accomplishments recorded."} } lines := []string{fmt.Sprintf("Results · run #%d · %s · %s · %d completed visits · %s", r.ID, r.Label(), ansi.Strip(m.progress(r.Status)), r.CompletedSteps, runDuration(r.CreatedAt, r.FinishedAt)), resultLabel(*r)} if r.Status != "succeeded" && r.ResultPreview != "" && r.Diagnostic != "" { lines = append(lines, "Partial output: "+ansi.Strip(r.ResultPreview)) } return lines } func runResultLines(r *protocol.RunDetail) []string { lines := []string{} if r.Diagnostic != "" { lines = append(lines, "Stopped: "+ansi.Strip(r.Diagnostic)) } lines = append(lines, waitingLines(r)...) if r.Status == "succeeded" { if preview := protocol.OutputPreview(r.Output); preview != "" { lines = append(lines, "Result: "+ansi.Strip(preview)) } else { lines = append(lines, "Finished · no result recorded") } } else { if r.Status == "running" || r.Status == "queued" { lines = append(lines, "In progress · final result pending") } for i := len(r.Steps) - 1; i >= 0; i-- { if r.Steps[i].Status == "completed" { if preview := protocol.OutputPreview(r.Steps[i].Output); preview != "" { lines = append(lines, "Latest completed output ("+r.Steps[i].Stage+"): "+ansi.Strip(preview)) } break } } } completed := 0 failed := 0 for _, s := range r.Steps { if s.Status == "completed" { completed++ } if s.Status == "failed" { failed++ } } lines = append(lines, fmt.Sprintf("Progress: %d completed visits · %d failed · %d recorded", completed, failed, len(r.Steps))) if r.CurrentStage != "" && (r.Status == "running" || r.Status == "queued") { lines = append(lines, "Working on: "+r.CurrentStage) } return lines } // waitingLines renders a run waiting on a human stage: the banner, the operator // instructions recorded with the wait, and the selectable options, so the choice // is visible without opening the stage view. func waitingLines(r *protocol.RunDetail) []string { if r.Status != protocol.RunStatusWaiting { return nil } lines := []string{"⏸ Waiting for a person · stage " + r.CurrentStage} if step, ok := lastWaitingStep(r); ok { if prompt := strings.TrimSpace(protocol.WaitingPrompt(step.Output)); prompt != "" { lines = append(lines, strings.Split(ansi.Strip(prompt), "\n")...) } if options := protocol.WaitingOptions(step.Output); len(options) > 0 { lines = append(lines, "Options: "+strings.Join(options, ", ")) } lines = append(lines, fmt.Sprintf("Decide: pipelines decide %d --option KEY [--feedback TEXT]", r.ID)) } return lines } func completedWorkLines(r *protocol.RunDetail) []string { var lines []string for _, s := range r.Steps { if s.Status != "completed" { continue } line := fmt.Sprintf("%d. %s · %s", s.Sequence, s.Stage, s.Outcome) if preview := protocol.OutputPreview(s.Output); preview != "" { line += " — " + ansi.Strip(preview) } else { line += " — no output recorded" } lines = append(lines, line) } if len(lines) == 0 { lines = append(lines, "No completed stage output yet.") } return append([]string{"Completed stage outputs (reported, not independently verified):"}, lines...) } func runOutputDocument(r *protocol.RunDetail, width int) []string { if r.Status == protocol.RunStatusWaiting { return waitingLines(r) } var notice []string if r.Diagnostic != "" { notice = append(notice, lipgloss.NewStyle().Foreground(themeError).Render("Stopped: "+ansi.Strip(r.Diagnostic)), "") } if r.Status == "succeeded" { if len(r.Output) == 0 { return []string{"Finished · no result recorded"} } return resultDocument(r.Output, width) } for i := len(r.Steps) - 1; i >= 0; i-- { if r.Steps[i].Status == "completed" && len(r.Steps[i].Output) > 0 { notice = append(notice, lipgloss.NewStyle().Foreground(themeMuted).Render("Partial result · "+r.Steps[i].Stage+" · final result not available"), "") return append(notice, resultDocument(r.Steps[i].Output, width)...) } } if len(r.Output) > 0 { values := maps.Clone(r.Output) delete(values, "input") return append(notice, resultDocument(values, width)...) } return append(notice, "No output recorded yet.") }