| 1 |
package clnkrapp |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
"sync" |
| 8 |
|
| 9 |
"github.com/clnkr-ai/clnkr" |
| 10 |
"github.com/clnkr-ai/clnkr/cmd/internal/compaction" |
| 11 |
) |
| 12 |
|
| 13 |
// DriverEvent is a sealed interface for frontend-facing driver events. |
| 14 |
type DriverEvent interface{ driverEvent() } |
| 15 |
|
| 16 |
type EventApprovalRequest struct { |
| 17 |
Commands []clnkr.BashAction |
| 18 |
Prompt string |
| 19 |
} |
| 20 |
type EventClarificationRequest struct{ Question string } |
| 21 |
type EventDone struct{ Summary string } |
| 22 |
type EventCompacted struct{ Stats clnkr.CompactStats } |
| 23 |
type EventError struct{ Err error } |
| 24 |
|
| 25 |
func (EventApprovalRequest) driverEvent() {} |
| 26 |
func (EventClarificationRequest) driverEvent() {} |
| 27 |
func (EventDone) driverEvent() {} |
| 28 |
func (EventCompacted) driverEvent() {} |
| 29 |
func (EventError) driverEvent() {} |
| 30 |
|
| 31 |
const ( |
| 32 |
PromptModeApproval = "approval" |
| 33 |
PromptModeFullSend = "full_send" |
| 34 |
) |
| 35 |
|
| 36 |
const ( |
| 37 |
PendingNone = "" |
| 38 |
PendingApproval = "approval" |
| 39 |
PendingClarification = "clarification" |
| 40 |
) |
| 41 |
|
| 42 |
type Driver struct { |
| 43 |
agent *clnkr.Agent |
| 44 |
compactorFactory compaction.Factory |
| 45 |
events chan DriverEvent |
| 46 |
|
| 47 |
mu sync.Mutex |
| 48 |
running bool |
| 49 |
pending *pendingReply |
| 50 |
} |
| 51 |
|
| 52 |
type pendingReply struct { |
| 53 |
kind string |
| 54 |
reply chan string |
| 55 |
done chan struct{} |
| 56 |
} |
| 57 |
|
| 58 |
func NewDriver(agent *clnkr.Agent, compactorFactory compaction.Factory) *Driver { |
| 59 |
return &Driver{ |
| 60 |
agent: agent, |
| 61 |
compactorFactory: compactorFactory, |
| 62 |
events: make(chan DriverEvent, 16), |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func (d *Driver) Events() <-chan DriverEvent { |
| 67 |
return d.events |
| 68 |
} |
| 69 |
|
| 70 |
func (d *Driver) Prompt(ctx context.Context, text string, mode string) error { |
| 71 |
if err := d.setRunning(true); err != nil { |
| 72 |
_ = d.emit(ctx, EventError{Err: err}) |
| 73 |
return err |
| 74 |
} |
| 75 |
defer d.setRunning(false) //nolint:errcheck |
| 76 |
|
| 77 |
stats, compacted, err := HandleCompactCommand(ctx, d.agent, text, d.compactorFactory) |
| 78 |
if err != nil { |
| 79 |
_ = d.emit(ctx, EventError{Err: err}) |
| 80 |
return err |
| 81 |
} |
| 82 |
if compacted { |
| 83 |
return d.emit(ctx, EventCompacted{Stats: stats}) |
| 84 |
} |
| 85 |
|
| 86 |
var doneSummary string |
| 87 |
previousNotify := d.agent.Notify |
| 88 |
d.agent.Notify = func(event clnkr.Event) { |
| 89 |
if response, ok := event.(clnkr.EventResponse); ok { |
| 90 |
switch turn := response.Turn.(type) { |
| 91 |
case *clnkr.DoneTurn: |
| 92 |
doneSummary = turn.Summary |
| 93 |
} |
| 94 |
} |
| 95 |
if previousNotify != nil { |
| 96 |
previousNotify(event) |
| 97 |
} |
| 98 |
} |
| 99 |
defer func() { |
| 100 |
d.agent.Notify = previousNotify |
| 101 |
}() |
| 102 |
|
| 103 |
runOpts := clnkr.RunOptions{ |
| 104 |
ContextLengthBackstop: d.contextLengthBackstop, |
| 105 |
} |
| 106 |
switch mode { |
| 107 |
case PromptModeFullSend: |
| 108 |
err = d.agent.RunWithPolicyOptions(ctx, text, clnkr.FullSendPolicy{}, runOpts) |
| 109 |
case PromptModeApproval, "": |
| 110 |
err = d.agent.RunWithPolicyOptions(ctx, text, d, runOpts) |
| 111 |
default: |
| 112 |
err = fmt.Errorf("unknown prompt mode %q", mode) |
| 113 |
} |
| 114 |
if err != nil { |
| 115 |
_ = d.emit(ctx, EventError{Err: err}) |
| 116 |
return err |
| 117 |
} |
| 118 |
return d.emit(ctx, EventDone{Summary: doneSummary}) |
| 119 |
} |
| 120 |
|
| 121 |
func (d *Driver) contextLengthBackstop(ctx context.Context, _ error) error { |
| 122 |
stats, err := compactTranscript(ctx, d.agent, d.compactorFactory) |
| 123 |
if err != nil { |
| 124 |
return err |
| 125 |
} |
| 126 |
return d.emit(ctx, EventCompacted{Stats: stats}) |
| 127 |
} |
| 128 |
|
| 129 |
// Compact runs the compaction flow through the driver. |
| 130 |
func (d *Driver) Compact(ctx context.Context) error { |
| 131 |
if err := d.setRunning(true); err != nil { |
| 132 |
_ = d.emit(ctx, EventError{Err: err}) |
| 133 |
return err |
| 134 |
} |
| 135 |
defer d.setRunning(false) //nolint:errcheck |
| 136 |
|
| 137 |
stats, err := compactTranscript(ctx, d.agent, d.compactorFactory) |
| 138 |
if err != nil { |
| 139 |
_ = d.emit(ctx, EventError{Err: err}) |
| 140 |
return err |
| 141 |
} |
| 142 |
return d.emit(ctx, EventCompacted{Stats: stats}) |
| 143 |
} |
| 144 |
|
| 145 |
func (d *Driver) Reply(ctx context.Context, text string) error { |
| 146 |
d.mu.Lock() |
| 147 |
pending := d.pending |
| 148 |
d.mu.Unlock() |
| 149 |
|
| 150 |
if pending == nil { |
| 151 |
return fmt.Errorf("reply: no pending request") |
| 152 |
} |
| 153 |
select { |
| 154 |
case pending.reply <- text: |
| 155 |
case <-pending.done: |
| 156 |
return fmt.Errorf("reply: no pending request") |
| 157 |
case <-ctx.Done(): |
| 158 |
return ctx.Err() |
| 159 |
} |
| 160 |
select { |
| 161 |
case <-pending.done: |
| 162 |
return nil |
| 163 |
case <-ctx.Done(): |
| 164 |
return ctx.Err() |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func (d *Driver) Pending() string { |
| 169 |
d.mu.Lock() |
| 170 |
defer d.mu.Unlock() |
| 171 |
|
| 172 |
if d.pending == nil { |
| 173 |
return PendingNone |
| 174 |
} |
| 175 |
return d.pending.kind |
| 176 |
} |
| 177 |
|
| 178 |
func (d *Driver) DecideAct( |
| 179 |
ctx context.Context, |
| 180 |
proposal clnkr.ActProposal, |
| 181 |
) (clnkr.ActDecision, error) { |
| 182 |
reply, err := d.waitForReply(ctx, PendingApproval, EventApprovalRequest{ |
| 183 |
Commands: proposal.Commands, |
| 184 |
Prompt: proposal.Prompt, |
| 185 |
}) |
| 186 |
if err != nil { |
| 187 |
return clnkr.ActDecision{}, err |
| 188 |
} |
| 189 |
if strings.TrimSpace(reply) != "y" { |
| 190 |
return clnkr.ActDecision{Kind: clnkr.ActDecisionReject, Guidance: reply}, nil |
| 191 |
} |
| 192 |
return clnkr.ActDecision{Kind: clnkr.ActDecisionApprove}, nil |
| 193 |
} |
| 194 |
|
| 195 |
func (d *Driver) Clarify(ctx context.Context, question string) (string, error) { |
| 196 |
return d.waitForReply(ctx, PendingClarification, EventClarificationRequest{Question: question}) |
| 197 |
} |
| 198 |
|
| 199 |
func (d *Driver) setRunning(running bool) error { |
| 200 |
d.mu.Lock() |
| 201 |
defer d.mu.Unlock() |
| 202 |
|
| 203 |
if running && d.running { |
| 204 |
return fmt.Errorf("driver run already in progress") |
| 205 |
} |
| 206 |
d.running = running |
| 207 |
return nil |
| 208 |
} |
| 209 |
|
| 210 |
func (d *Driver) waitForReply(ctx context.Context, kind string, event DriverEvent) (string, error) { |
| 211 |
for { |
| 212 |
reply, err := d.requestReply(ctx, kind, event) |
| 213 |
if err != nil { |
| 214 |
return "", err |
| 215 |
} |
| 216 |
reply = strings.TrimSpace(reply) |
| 217 |
if reply == "" { |
| 218 |
continue |
| 219 |
} |
| 220 |
if err := RejectCompactCommand(reply); err != nil { |
| 221 |
if emitErr := d.emit(ctx, EventError{Err: err}); emitErr != nil { |
| 222 |
return "", emitErr |
| 223 |
} |
| 224 |
continue |
| 225 |
} |
| 226 |
return reply, nil |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
func (d *Driver) requestReply(ctx context.Context, kind string, event DriverEvent) (string, error) { |
| 231 |
pending := &pendingReply{ |
| 232 |
kind: kind, |
| 233 |
reply: make(chan string), |
| 234 |
done: make(chan struct{}), |
| 235 |
} |
| 236 |
if err := d.setPending(pending); err != nil { |
| 237 |
return "", err |
| 238 |
} |
| 239 |
defer d.clearPending(pending) |
| 240 |
|
| 241 |
if err := d.emit(ctx, event); err != nil { |
| 242 |
return "", err |
| 243 |
} |
| 244 |
select { |
| 245 |
case reply := <-pending.reply: |
| 246 |
return reply, nil |
| 247 |
case <-ctx.Done(): |
| 248 |
return "", ctx.Err() |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
func (d *Driver) setPending(pending *pendingReply) error { |
| 253 |
d.mu.Lock() |
| 254 |
defer d.mu.Unlock() |
| 255 |
|
| 256 |
if d.pending != nil { |
| 257 |
return fmt.Errorf("driver pending request already exists") |
| 258 |
} |
| 259 |
d.pending = pending |
| 260 |
return nil |
| 261 |
} |
| 262 |
|
| 263 |
func (d *Driver) clearPending(pending *pendingReply) { |
| 264 |
d.mu.Lock() |
| 265 |
defer d.mu.Unlock() |
| 266 |
|
| 267 |
if d.pending == pending { |
| 268 |
d.pending = nil |
| 269 |
close(pending.done) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
func (d *Driver) emit(ctx context.Context, event DriverEvent) error { |
| 274 |
select { |
| 275 |
case d.events <- event: |
| 276 |
return nil |
| 277 |
case <-ctx.Done(): |
| 278 |
return ctx.Err() |
| 279 |
} |
| 280 |
} |