-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.go
More file actions
174 lines (147 loc) · 3.33 KB
/
Copy pathcommands.go
File metadata and controls
174 lines (147 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/fsnotify/fsnotify"
)
type PRDUpdatedMsg struct {
PRD PRD
Err error
}
type OutputLineMsg struct {
Line string
Timestamp time.Time
}
type ProcessStartedMsg struct {
Iteration int
StoryID string
Cmd *exec.Cmd
}
type ProcessExitedMsg struct {
ExitCode int
Complete bool
Err error
}
type TickMsg time.Time
type ErrorMsg struct {
Err error
}
func watchPRDCmd(path string) tea.Cmd {
return func() tea.Msg {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return ErrorMsg{Err: err}
}
defer watcher.Close()
if err := watcher.Add(path); err != nil {
return ErrorMsg{Err: err}
}
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return nil
}
if event.Op&fsnotify.Write == fsnotify.Write {
prd, err := LoadPRD(path)
return PRDUpdatedMsg{PRD: prd, Err: err}
}
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
return ErrorMsg{Err: err}
}
}
}
}
func runIterationCmd(promptPath, projectRoot string, iteration int, storyID string, msgChan chan<- interface{}) tea.Cmd {
return func() tea.Msg {
promptContent, err := os.ReadFile(promptPath)
if err != nil {
return ProcessExitedMsg{ExitCode: 1, Complete: false, Err: err}
}
cmd := exec.Command("opencode", "run", string(promptContent))
cmd.Dir = projectRoot
stdout, err := cmd.StdoutPipe()
if err != nil {
return ProcessExitedMsg{ExitCode: 1, Complete: false, Err: err}
}
stderr, err := cmd.StderrPipe()
if err != nil {
return ProcessExitedMsg{ExitCode: 1, Complete: false, Err: err}
}
if err := cmd.Start(); err != nil {
return ProcessExitedMsg{ExitCode: 1, Complete: false, Err: err}
}
msgChan <- ProcessStartedMsg{
Iteration: iteration,
StoryID: storyID,
Cmd: cmd,
}
go streamOutput(stdout, msgChan)
go streamOutput(stderr, msgChan)
err = cmd.Wait()
exitCode := 0
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode = exitErr.ExitCode()
} else {
exitCode = 1
}
}
return ProcessExitedMsg{
ExitCode: exitCode,
Complete: false,
Err: nil,
}
}
}
func streamOutput(reader io.Reader, msgChan chan<- interface{}) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
msgChan <- OutputLineMsg{
Line: line,
Timestamp: time.Now(),
}
}
}
func listenForOutputCmd(msgChan <-chan interface{}) tea.Cmd {
return func() tea.Msg {
msg := <-msgChan
return msg
}
}
func tickCmd() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return TickMsg(t)
})
}
var storyPatterns = []*regexp.Regexp{
regexp.MustCompile(`Working on (S\d+)`),
regexp.MustCompile(`Next story: (S\d+)`),
regexp.MustCompile(`feat: (S\d+)`),
regexp.MustCompile(`\[(S\d+)\]`),
}
func parseStoryFromLine(line string) (storyID string, found bool) {
for _, pattern := range storyPatterns {
if matches := pattern.FindStringSubmatch(line); len(matches) > 1 {
return matches[1], true
}
}
return "", false
}
func checkCompleteSignal(line string) bool {
return strings.Contains(line, "<promise>COMPLETE</promise>")
}
func formatTimestamp(t time.Time) string {
return fmt.Sprintf("[%s]", t.Format("15:04:05"))
}