275 lines
7.8 KiB
Go
275 lines
7.8 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
"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) 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 {
|
|
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
|
|
}
|
|
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 continues."
|
|
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()
|
|
m.runForm = 2
|
|
m.runInput = newInput()
|
|
m.runInput.Placeholder = "Exact Go edit paths, comma-separated; blank = no edits"
|
|
return m, m.runInput.Focus()
|
|
}
|
|
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
|
|
case "enter":
|
|
switch m.runForm {
|
|
case 2:
|
|
m.runPaths = nil
|
|
for _, path := range strings.Split(m.runInput.Value(), ",") {
|
|
if path = strings.TrimSpace(path); path != "" {
|
|
m.runPaths = append(m.runPaths, path)
|
|
}
|
|
}
|
|
m.runForm = 3
|
|
m.runInput.SetValue("")
|
|
m.runInput.Placeholder = "Type RUN to confirm"
|
|
case 3:
|
|
if m.runInput.Value() != "RUN" {
|
|
return m, nil
|
|
}
|
|
cmd := m.startSelectedRun()
|
|
return m, cmd
|
|
}
|
|
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
|
|
approval := m.runToolApproval()
|
|
if m.cliRun && m.cliOptions.Yes {
|
|
approval = m.cliOptions.AllowTools
|
|
}
|
|
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: approval, 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) runToolApproval() bool {
|
|
if len(m.runPaths) > 0 {
|
|
return true
|
|
}
|
|
if m.detail != nil {
|
|
for _, s := range m.detail.Graph.Stages {
|
|
for _, tool := range strings.Fields(s.Blocks["tools"]) {
|
|
if tool == "shell" {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (m Model) runFormView(height int) string {
|
|
title := []string{"", "Run: task", "Run: edit scope", "Confirm execution"}[m.runForm]
|
|
input := m.runInput
|
|
input.Width = max(1, min(90, m.width-12))
|
|
lines := []string{title, input.View(), "Esc cancels"}
|
|
if m.runForm == 1 {
|
|
lines = []string{title, "Repository: " + m.runWorkspace, "Enter newline · Ctrl+S continue · Esc cancel"}
|
|
lines = append(lines, strings.Split(m.runPrompt.View(), "\n")...)
|
|
return popup(m.width, height, 104, lines)
|
|
}
|
|
if m.runForm == 3 {
|
|
lines[2] = "Type RUN and press Enter to start · Esc cancels"
|
|
lines = append(lines, "Repository: "+m.runWorkspace, "Prompt: "+ansi.Truncate(strings.ReplaceAll(m.runTask, "\n", " ↵ "), max(1, m.width-12), "…"))
|
|
lines = append(lines, fmt.Sprintf("%s v%d · 30 steps / 5 visits / 30 minutes", m.detail.Name, m.detail.Version))
|
|
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 && len(m.runPaths) > 0 {
|
|
label = "legacy project read/edit and Go checks"
|
|
}
|
|
lines = append(lines, s.Key+" tools: "+label)
|
|
}
|
|
if m.runToolApproval() {
|
|
lines = append(lines, "RUN approves scoped shell/edit access where declared.", "Go checks execute project code with your user privileges.")
|
|
}
|
|
lines = append(lines, "Changes are not rolled back on cancellation.", "Edit paths: "+strings.Join(m.runPaths, ", "))
|
|
}
|
|
return popup(m.width, height, 100, lines)
|
|
}
|
|
|
|
func (m Model) updateRuns(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|
runs := m.visibleRuns()
|
|
switch msg.String() {
|
|
case "esc":
|
|
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
|
|
}
|