309 lines
8.0 KiB
Go
309 lines
8.0 KiB
Go
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"
|
|
)
|
|
|
|
type runTick struct{}
|
|
|
|
func runPoll() tea.Cmd { return tea.Tick(time.Second, func(time.Time) tea.Msg { return runTick{} }) }
|
|
|
|
func (m Model) runRequest(request protocol.Message) (tea.Model, tea.Cmd) {
|
|
if m.conn == nil {
|
|
m.appendLine("not connected")
|
|
return m, nil
|
|
}
|
|
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.runOpen = true
|
|
m.requestedRunID = 0
|
|
m.runStepIndex = 0
|
|
m.runDetailFocus = false
|
|
m.runCollapsed = nil
|
|
m.runDetail = nil
|
|
m.runTop = 0
|
|
updated, cmd := m.runRequest(protocol.Message{Type: protocol.RunPipeline, PipelineID: m.detail.ID, Version: m.detail.Version, RequestKey: fmt.Sprintf("tui-%d", time.Now().UnixNano()), Input: initialRunInput(m.runTask), AllowTools: m.runHasMutationTools(), WritePaths: m.runPaths, Workspace: m.runWorkspace})
|
|
*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 {
|
|
lines := []string{"Run: task", "Repository: " + m.runWorkspace, "Enter newline · Ctrl+S run · Esc cancel"}
|
|
lines = append(lines, strings.Split(m.runPrompt.View(), "\n")...)
|
|
if m.detail != nil {
|
|
for _, s := range m.detail.Graph.Stages {
|
|
if s.Type == "ntfy" {
|
|
lines = append(lines, s.Key+" action: publish Markdown to ntfy destination "+s.Configs["destination"])
|
|
continue
|
|
}
|
|
tools, explicit := s.Blocks["tools"]
|
|
label := strings.Join(strings.Fields(tools), ", ")
|
|
if label == "" {
|
|
label = "none"
|
|
}
|
|
if !explicit {
|
|
label = "project read/edit and shell"
|
|
}
|
|
lines = append(lines, s.Key+" tools: "+label)
|
|
}
|
|
}
|
|
return popup(m.width, height, 100, lines)
|
|
}
|
|
|
|
func (m Model) updateRuns(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|
runs := m.visibleRuns()
|
|
if msg.String() == "g" && m.runDetail != nil {
|
|
m.runGraphFocus = !m.runGraphFocus
|
|
if m.runGraphFocus {
|
|
m.runGraphTop, m.runGraphPan = m.runGraphFollow()
|
|
}
|
|
return m, nil
|
|
}
|
|
if m.runGraphFocus {
|
|
switch msg.String() {
|
|
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
|
|
case "tab", "shift+tab":
|
|
m.runGraphFocus = false
|
|
return m, nil
|
|
}
|
|
}
|
|
switch msg.String() {
|
|
case "p":
|
|
return m.openRunPipeline()
|
|
case "D":
|
|
return m.debugRun()
|
|
case "a":
|
|
return m.openOpenCodeEvidence()
|
|
case "r":
|
|
return m.retrySelectedRun()
|
|
case "esc":
|
|
m.runGraphFocus = 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 if !m.runDetailFocus {
|
|
m.selectRunStep(min(m.runStepIndex+1, len(m.runDetail.Steps)))
|
|
} else {
|
|
m.runTop++
|
|
}
|
|
case "k", "up":
|
|
if m.runDetail == nil {
|
|
m.runIndex = max(0, m.runIndex-1)
|
|
} else if !m.runDetailFocus {
|
|
m.selectRunStep(max(0, m.runStepIndex-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 "tab", "shift+tab":
|
|
if m.runDetail != nil {
|
|
m.runDetailFocus = !m.runDetailFocus
|
|
}
|
|
case "h", "left":
|
|
m.runDetailFocus = false
|
|
case "l", "right":
|
|
if m.runDetail != nil {
|
|
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.runDetailFocus {
|
|
m.runTop = 0
|
|
} else if m.runDetail != nil {
|
|
m.selectRunStep(0)
|
|
} else {
|
|
m.runIndex = 0
|
|
}
|
|
case "end":
|
|
if m.runDetailFocus {
|
|
m.runTop = len(m.runSectionLines(m.runDetailWidth()))
|
|
} else if m.runDetail != nil {
|
|
m.selectRunStep(len(m.runDetail.Steps))
|
|
} 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 {
|
|
m.toggleRunSection(strings.Index("siotm", msg.String()))
|
|
}
|
|
case "x":
|
|
if m.runDetail != nil {
|
|
return m.runRequest(protocol.Message{Type: protocol.CancelRun, RunID: m.runDetail.ID})
|
|
}
|
|
}
|
|
if m.runDetail != nil {
|
|
m.runTop = max(0, min(m.runTop, max(0, len(m.runSectionLines(m.runDetailWidth()))-m.runPageSize())))
|
|
}
|
|
return m, nil
|
|
}
|