-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodejob.go
More file actions
237 lines (205 loc) · 6.05 KB
/
codejob.go
File metadata and controls
237 lines (205 loc) · 6.05 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package devflow
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/tinywasm/context"
"github.com/tinywasm/wizard"
"golang.org/x/term"
)
// DefaultIssuePromptPath is the conventional location for the task description file.
const DefaultIssuePromptPath = "docs/PLAN.md"
// CodeJob orchestrates sending a coding task to a chain of AI agent drivers.
// It validates the prompt file, then tries each driver in priority order,
// falling back to the next on failure.
type CodeJob struct {
drivers []CodeJobDriver
log func(...any)
publisher Publisher
}
// NewCodeJob creates a CodeJob with the given ordered drivers.
func NewCodeJob(drivers ...CodeJobDriver) *CodeJob {
return &CodeJob{
drivers: drivers,
log: func(...any) {},
}
}
// SetLog sets the logging function for the orchestrator.
func (c *CodeJob) SetLog(fn func(...any)) {
if fn != nil {
c.log = fn
}
}
// SetPublisher injects a Publisher for close-loop operations.
func (c *CodeJob) SetPublisher(p Publisher) { c.publisher = p }
// Run implements the unified API logic.
func (c *CodeJob) Run(message, tag string) (string, error) {
env := NewDotEnv(".env")
// 1. If message provided -> close the loop
if message != "" {
if _, ok := env.Get("CODEJOB_PR"); !ok {
return "", fmt.Errorf("no pending PR found in .env (CODEJOB_PR missing)")
}
if c.publisher == nil {
return "", fmt.Errorf("no publisher configured")
}
res, err := MergeAndPublish(c.publisher, message, tag)
if err != nil {
return "", err
}
if res.Tag == "RE_DISPATCH" {
fmt.Println(res.Summary)
return c.Send(DefaultIssuePromptPath)
}
return res.Summary, nil
}
// 2. No message -> check status or dispatch
if val, ok := env.Get("CODEJOB"); ok {
return c.checkStatus(env, val)
}
// 3. Auto-merge pending PR before dispatching new work
if prURL, ok := env.Get("CODEJOB_PR"); ok && prURL != "" {
if c.publisher == nil {
return "", fmt.Errorf("no publisher configured")
}
res, err := MergeAndPublish(c.publisher, "chore: merge agent PR", "")
if err != nil {
return "", err
}
if res.Tag == "RE_DISPATCH" {
fmt.Println(res.Summary)
return c.Send(DefaultIssuePromptPath)
}
return res.Summary, nil
}
// 4. Auto-setup if API key missing
auth, err := NewJulesAuth()
if err == nil && !auth.HasKey() {
if err := c.runSetupWizard(); err != nil {
return "", err
}
}
// 5. Dispatch
return c.Send(DefaultIssuePromptPath)
}
func (c *CodeJob) checkStatus(env *DotEnv, val string) (string, error) {
parts := strings.SplitN(val, ":", 2)
if len(parts) != 2 {
return "", fmt.Errorf("invalid CODEJOB value in .env: %s", val)
}
driverName := parts[0]
sessionID := parts[1]
if driverName != "jules" {
return "", fmt.Errorf("unsupported driver in .env: %s", driverName)
}
auth, _ := NewJulesAuth()
apiKey, err := auth.EnsureAPIKey()
if err != nil {
return "", err
}
msg, prURL, done, err := JulesSessionState(sessionID, apiKey, &http.Client{})
if err != nil {
return "", err
}
if done {
git, _ := NewGit()
if err := HandleDone(env, git, prURL); err != nil {
return msg, fmt.Errorf("cleanup error: %w", err)
}
}
return msg, nil
}
func (c *CodeJob) runSetupWizard() error {
wiz := wizard.New(func(_ *context.Context) {
fmt.Println("\n✅ Jules API key saved. Run 'codejob' to dispatch a task.")
}, c)
for wiz.WaitingForUser() {
label := wiz.Label()
fmt.Fprintf(os.Stderr, "\n%s: ", label)
raw, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Fprintln(os.Stderr)
if err != nil {
return fmt.Errorf("could not read API key: %w", err)
}
wiz.Change(string(raw))
if !wiz.WaitingForUser() {
break
}
}
return nil
}
// GetSteps for wizard
func (c *CodeJob) GetSteps() []*wizard.Step {
return []*wizard.Step{
{
LabelText: "Jules API Key (get yours at " + termLink(julesAPIKeyURL, julesAPIKeyURL) + ")",
DefaultFn: func(ctx *context.Context) string { return "" },
OnInputFn: func(in string, ctx *context.Context) (bool, error) {
in = strings.TrimSpace(in)
if in == "" {
return false, fmt.Errorf("API key cannot be empty")
}
kr, err := NewKeyring()
if err != nil {
return false, fmt.Errorf("could not initialize keyring: %w", err)
}
if err := kr.Set(julesAPIKeyKey, in); err != nil {
c.log(fmt.Sprintf("warning: could not save API key to keyring: %v", err))
}
return true, nil
},
},
}
}
// Send validates issuePromptPath, publishes pending changes, then tries each
// driver in order until one succeeds. Returns an error if the file is missing,
// empty, the publish fails, or all drivers fail.
func (c *CodeJob) Send(issuePromptPath string) (string, error) {
info, err := os.Stat(issuePromptPath)
if err != nil {
return "", fmt.Errorf("prompt file not found: %w", err)
}
if info.Size() == 0 {
return "", fmt.Errorf("prompt file is empty: %s", issuePromptPath)
}
// PUBLISH BEFORE SEND (Stage 2, step 2.3)
if c.publisher != nil {
_, err := c.publisher.Publish("chore: sync before codejob dispatch", "", true, true, true, true, true)
if err != nil {
return "", fmt.Errorf("failed to sync repo before dispatch: %w", err)
}
}
if len(c.drivers) == 0 {
return "", fmt.Errorf("no drivers configured")
}
prompt := "Execute the implementation plan described in " + issuePromptPath
title := autoDetectTitle()
var lastErr error
for _, d := range c.drivers {
d.SetLog(c.log)
result, err := d.Send(prompt, title)
if err == nil {
// Try to persist session ID to .env
if sp, ok := d.(SessionProvider); ok {
if id := sp.SessionID(); id != "" {
env := NewDotEnv(".env")
_ = env.Set("CODEJOB", strings.ToLower(d.Name())+":"+id)
}
}
return result, nil
}
c.log(fmt.Sprintf("driver %s failed: %v", d.Name(), err))
lastErr = err
}
return "", fmt.Errorf("all agents failed, last error: %w", lastErr)
}
// autoDetectTitle returns "owner/repo" for the current git repository,
// or "" if detection fails (non-fatal; driver will use its own fallback).
func autoDetectTitle() string {
owner, repo, err := autoDetectOwnerRepo()
if err != nil {
return ""
}
return owner + "/" + repo
}