package tui import ( "fmt" "os" "strings" "time" "git.a-wan.net/alan/pipelines/internal/protocol" "github.com/charmbracelet/bubbles/textarea" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/x/ansi" ) type runTick struct{} func runPoll() tea.Cmd { return tea.Tick(time.Second, func(time.Time) tea.Msg { return runTick{} }) } func (m Model) openRuns() (tea.Model, tea.Cmd) { scope := int64(0) if m.keys.detail { scope = m.requestedPipelineID if scope == 0 && m.detail != nil { scope = m.detail.ID } } m.closeRuns() m.runPipelineID = scope m.runOpen = true m.keys.runs = true m.runIndex = 0 return m.runRequest(protocol.Message{Type: protocol.ListRuns}) } func (m Model) runRequest(request protocol.Message) (tea.Model, tea.Cmd) { return m.sendRunRequest(request) } func (m Model) sendRunRequest(request protocol.Message) (tea.Model, tea.Cmd) { if request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.CancelRun { if !m.actionApproved { m.actionConfirm = &request m.actionConfirmTop = 0 return m, nil } m.actionApproved = false } if m.conn == nil { m.appendLine("not connected") return m, nil } if request.Type == protocol.GetRun { m.runError = "" } m.nextID++ request.ID = fmt.Sprintf("run-%d", m.nextID) if request.Type == protocol.GetRun && m.requestedRunID != request.RunID { m.requestedRunID = request.RunID m.runDetail = nil m.runStepIndex = 0 m.runTop = 0 m.runDetailFocus = false m.runCollapsed = nil m.runError = "" } if request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun { if request.Type == protocol.RetryRun { m.runOpen = true m.requestedRunID = 0 m.runDetail = nil m.runStepIndex = 0 m.runTop = 0 m.runCollapsed = nil } if request.Type == protocol.RunPipeline && request.Workspace == "" { cwd, err := os.Getwd() if err != nil { m.appendLine("cannot determine current directory: " + err.Error()) return m, nil } request.Workspace = cwd } m.runStartRequest = request.ID m.keys.runs = true m.runError = "" } return m, m.send(request) } func (m Model) beginRun() (tea.Model, tea.Cmd) { if m.detail == nil || m.detailLoading { m.appendLine("open an exact pipeline version first") return m, nil } if m.runWorkspace == "" { cwd, err := os.Getwd() if err != nil { m.appendLine("cannot determine current directory: " + err.Error()) return m, nil } m.runWorkspace = cwd } m.runForm = 1 m.runInput = newInput() m.runPrompt = textarea.New() m.runPrompt.CharLimit = 32000 m.runPrompt.Placeholder = "Describe the task. Enter adds a line; Ctrl+S runs it." m.runPrompt.SetWidth(max(1, min(96, m.width-12))) m.runPrompt.SetHeight(max(1, min(10, m.height-14))) m.runPrompt.ShowLineNumbers = false return m, m.runPrompt.Focus() } func (m Model) updateRunForm(msg tea.KeyMsg) (tea.Model, tea.Cmd) { if m.runForm == 1 && msg.String() != "esc" { if msg.String() == "ctrl+s" || msg.String() == "alt+enter" { if strings.TrimSpace(m.runPrompt.Value()) == "" { return m, nil } m.runTask = m.runPrompt.Value() cmd := m.startSelectedRun() return m, cmd } var cmd tea.Cmd m.runPrompt, cmd = m.runPrompt.Update(msg) return m, cmd } switch msg.String() { case "esc": m.runForm = 0 return m, nil } var cmd tea.Cmd m.runInput, cmd = m.runInput.Update(msg) return m, cmd } func (m *Model) startSelectedRun() tea.Cmd { m.runForm = 0 m.runPipelineID = m.detail.ID m.runSearch = newInput() m.runSearchOpen = false m.runOpen = true m.requestedRunID = 0 m.runStepIndex = 0 m.runDetailFocus = false m.runCollapsed = nil m.runDetail = nil m.runTop = 0 request := protocol.Message{Type: protocol.RunPipeline, PipelineID: m.detail.ID, Version: m.detail.Version, RequestKey: fmt.Sprintf("tui-%d", time.Now().UnixNano()), Title: m.cliOptions.Title, Input: initialRunInput(m.runTask), AllowTools: m.runHasMutationTools(), WritePaths: m.runPaths, Workspace: m.runWorkspace, Model: m.cliOptions.Model, StageModels: m.cliOptions.StageModels, NotifyMode: m.cliOptions.NotifyMode, OutputFormat: m.cliOptions.OutputFormat} updated, cmd := m.runRequest(request) *m = updated.(Model) return cmd } func initialRunInput(prompt string) map[string]any { return map[string]any{"prompt": prompt} } func (m Model) runHasMutationTools() bool { if len(m.runPaths) > 0 { return true } if m.detail != nil { for _, s := range m.detail.Graph.Stages { if _, explicit := s.Blocks["tools"]; !explicit && s.Type != "ntfy" { return true } for _, tool := range strings.Fields(s.Blocks["tools"]) { if tool == "shell" || tool == "edit" { return true } } } } return false } func (m Model) runFormView(height int) string { title := "Run pipeline" if m.detail != nil { title = fmt.Sprintf("Run %s · v%d", m.detail.Name, m.detail.Version) } field := m.runPrompt field.SetWidth(max(1, m.width-6)) field.SetHeight(max(1, height-6)) headers := []string{bar(title, "", max(1, m.width-4), true), ansi.Truncate("Repository: "+m.runWorkspace, max(1, m.width-4), "…")} return panel(m.width, height, headers, strings.Split(field.View(), "\n"), m.runScopeLabel(), true) } func (m Model) runScopeLabel() string { if m.detail == nil { return "Scope: defined by the selected pipeline" } var tools, notifications []string seen := map[string]bool{} for _, s := range m.detail.Graph.Stages { if s.Type == "ntfy" { notifications = append(notifications, "publish Markdown to ntfy destination "+s.Configs["destination"]) continue } declared, explicit := s.Blocks["tools"] if !explicit { declared = "read glob grep edit shell" } for _, tool := range strings.Fields(declared) { if !seen[tool] { seen[tool] = true tools = append(tools, tool) } } } label := "Stage tools: none" if len(tools) > 0 { label = "Stage tools: " + strings.Join(tools, ", ") } if len(notifications) > 0 { label += " · " + strings.Join(notifications, "; ") } return label } func (m Model) updateRuns(msg tea.KeyMsg) (tea.Model, tea.Cmd) { if m.runSearchOpen && m.runDetail == nil { switch msg.String() { case "enter": m.runSearchOpen = false m.runSearch.Blur() return m, nil case "esc": m.runSearchOpen = false m.runSearch.SetValue("") m.runSearch.Blur() m.runIndex = 0 return m, nil case "down", "ctrl+n": m.runIndex = min(m.runIndex+1, max(0, len(m.visibleRuns())-1)) return m, nil case "up", "ctrl+p": m.runIndex = max(0, m.runIndex-1) return m, nil } var cmd tea.Cmd m.runSearch, cmd = m.runSearch.Update(msg) m.runIndex = 0 return m, cmd } if msg.String() == "/" && m.runDetail == nil && m.requestedRunID == 0 && m.runStartRequest == "" { if m.runSearch.Placeholder == "" { m.runSearch = newInput() } m.runSearch.Placeholder = "Search runs…" m.runSearchOpen = true return m, m.runSearch.Focus() } if m.runDetail != nil && (msg.String() == "n" || msg.String() == "p") { delta := 1 if msg.String() == "p" { delta = -1 } m.selectRunStep(max(0, min(len(m.runDetail.Steps), m.runStepIndex+delta))) if m.runGraphFocus { m.runGraphTop, m.runGraphPan = m.runGraphFollow() } return m, nil } if m.runDetail != nil && (msg.String() == "tab" || msg.String() == "shift+tab") { m.runGraphFocus = !m.runGraphFocus m.runDetailFocus = !m.runGraphFocus if m.runGraphFocus { m.runGraphTop, m.runGraphPan = m.runGraphFollow() } return m, nil } runs := m.visibleRuns() if msg.String() == "g" && m.runDetail != nil { m.runGraphFocus = !m.runGraphFocus m.runDetailFocus = !m.runGraphFocus if m.runGraphFocus { m.runGraphTop, m.runGraphPan = m.runGraphFollow() } return m, nil } if m.runGraphFocus { switch msg.String() { case "c": if m.runDetail != nil && m.runDetail.Status == protocol.RunStatusWaiting { return m.openDecision() } m.runGraphTop, m.runGraphPan = m.runGraphFollow() return m, nil case "j", "down": m.runGraphTop++ return m, nil case "k", "up": m.runGraphTop = max(0, m.runGraphTop-1) return m, nil case "h", "left": m.runGraphPan = max(0, m.runGraphPan-8) return m, nil case "l", "right": m.runGraphPan += 8 return m, nil case "pgdown", "ctrl+d": m.runGraphTop += max(1, m.height/2-4) return m, nil case "pgup", "ctrl+u": m.runGraphTop = max(0, m.runGraphTop-max(1, m.height/2-4)) return m, nil case "home": m.runGraphTop = 0 return m, nil case "end": m.runGraphTop = len(m.runGraphLines(max(3, m.width-4))) return m, nil } } switch msg.String() { case "P": return m.openRunPipeline() case "p": return m.openRunPipeline() case "D": return m.debugRun() case "a": return m.openOpenCodeEvidence() case "A": if m.runDetail != nil { m.openArtifacts() } return m, nil case "r": return m.retrySelectedRun() case "esc": m.runGraphFocus = false m.artifactsOpen = false if m.runDetail != nil || m.requestedRunID > 0 || m.runStartRequest != "" { m.runDetail = nil m.requestedRunID = 0 m.runStartRequest = "" m.runError = "" m.runTop = 0 return m.runRequest(protocol.Message{Type: protocol.ListRuns}) } m.closeRuns() return m, nil case "j", "down": if m.runDetail == nil { m.runIndex = min(m.runIndex+1, max(0, len(runs)-1)) } else { m.runTop++ } case "k", "up": if m.runDetail == nil { m.runIndex = max(0, m.runIndex-1) } else { m.runTop = max(0, m.runTop-1) } case "enter": if m.runDetail == nil && m.requestedRunID == 0 && len(runs) > 0 { return m.runRequest(protocol.Message{Type: protocol.GetRun, RunID: runs[min(m.runIndex, len(runs)-1)].ID}) } if m.runDetail != nil { m.runDetailFocus = true } case "h", "left": if m.runDetail != nil { m.selectRunStep(max(0, m.runStepIndex-1)) m.runDetailFocus = true } case "l", "right": if m.runDetail != nil { m.selectRunStep(min(len(m.runDetail.Steps), m.runStepIndex+1)) m.runDetailFocus = true } case "pgdown", "ctrl+d": if m.runDetail != nil { m.runDetailFocus = true m.runTop += m.runPageSize() } else { m.runIndex = min(max(0, len(runs)-1), m.runIndex+m.runPageSize()) } case "pgup", "ctrl+u": if m.runDetail != nil { m.runDetailFocus = true m.runTop -= m.runPageSize() } else { m.runIndex = max(0, m.runIndex-m.runPageSize()) } case "home": if m.runDetail != nil { m.runTop = 0 } else { m.runIndex = 0 } case "end": if m.runDetail != nil { m.runTop = len(m.runSectionLines(m.runDetailWidth())) } else { m.runIndex = max(0, len(runs)-1) } case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9": if m.runDetail != nil { index := int(msg.String()[0] - '0') if index <= len(m.runDetail.Steps) { m.selectRunStep(index) } } case "s", "i", "o", "t", "m": if m.runDetail != nil { if index, ok := runSectionForShortcut(msg.String()); ok { m.toggleRunSection(index) } } case "d": if m.runDetail != nil { m.runGraphFocus = false m.runDetailFocus = true _, focused := runFocusedSection(m.runSectionLines(m.runDetailWidth()), m.runTop) if m.runSectionClosed(runSectionFiles) || focused == runSections[runSectionFiles].Title { m.toggleRunSection(runSectionFiles) } marker := "[" + runSections[runSectionFiles].Shortcut + "]" for i, line := range m.runSectionLines(m.runDetailWidth()) { if strings.HasPrefix(ansi.Strip(line), marker) { m.runTop = i break } } } case "x": if m.runDetail != nil { return m.runRequest(protocol.Message{Type: protocol.CancelRun, RunID: m.runDetail.ID}) } case "c": if m.runDetail != nil && m.runDetail.Status == protocol.RunStatusWaiting { return m.openDecision() } } if m.runDetail != nil { m.runTop = max(0, min(m.runTop, max(0, len(m.runSectionLines(m.runDetailWidth()))-m.runPageSize()))) } return m, nil }