blob: bd7c0d2c8272d70324ae190cec801d9c7687ecbb [file] [log] [blame]
Sketch🕴️305f8172026-02-27 13:58:43 +04001package loop
2
3import (
4 "fmt"
5 "time"
6)
7
8type Agent interface {
9 Run(todo *ToDo) error
10}
11
12type UserAgent struct {
13 pr PromptReader
14}
15
16func (a *UserAgent) Run(todo *ToDo) error {
17 for {
Sketch🕴️00202652026-02-28 21:10:00 +040018 unlocked := false
19 todo.Lock()
Sketch🕴️305f8172026-02-27 13:58:43 +040020 items := findActionableItems(todo, "user")
21 if len(items) == 0 {
Sketch🕴️00202652026-02-28 21:10:00 +040022 fmt.Println("## USER NO ITEMS")
23 todo.Unlock()
Sketch🕴️305f8172026-02-27 13:58:43 +040024 time.Sleep(30 * time.Second)
25 continue
26 }
Sketch🕴️00202652026-02-28 21:10:00 +040027 fmt.Println(todo.String())
28 fmt.Printf("-- YOU START WORKING %d\n", len(items))
Sketch🕴️305f8172026-02-27 13:58:43 +040029 for _, i := range items {
30 fmt.Printf("YOU %s %s: ", i.ID, i.Title)
31 comment, err := a.pr.Read()
32 if err != nil {
33 return err
34 }
Sketch🕴️00202652026-02-28 21:10:00 +040035 if comment == "DONE" {
36 i.Done = true
37 continue
38 }
39 if comment == "BREAK" {
40 unlocked = true
41 todo.Unlock()
42 time.Sleep(30 * time.Second)
43 break
44 }
45 if comment == "ASSIGN" {
46 i.AssignedTo = "assistant"
47 continue
48 }
Sketch🕴️305f8172026-02-27 13:58:43 +040049 i.Discussion = append(i.Discussion, Comment{
50 Author: "user",
51 Comment: comment,
52 })
53 i.AssignedTo = "assistant"
54 }
Sketch🕴️00202652026-02-28 21:10:00 +040055 fmt.Println("-- YOU END WORKING")
56 if !unlocked {
57 todo.Unlock()
58 }
Sketch🕴️305f8172026-02-27 13:58:43 +040059 }
60}
61
62func findActionableItems(todo *ToDo, assignedTo string) []*ToDo {
Sketch🕴️00202652026-02-28 21:10:00 +040063 if todo.Done {
64 return nil
65 }
66 var ret []*ToDo
Sketch🕴️305f8172026-02-27 13:58:43 +040067 for _, i := range todo.Items {
Sketch🕴️00202652026-02-28 21:10:00 +040068 ret = append(ret, findActionableItems(i, assignedTo)...)
69 if len(ret) > 0 && !todo.Parallel {
Sketch🕴️305f8172026-02-27 13:58:43 +040070 return ret
71 }
72 }
Sketch🕴️00202652026-02-28 21:10:00 +040073 if len(ret) == 0 && todo.AssignedTo == assignedTo && !todo.Done {
74 for _, i := range todo.Items {
75 if !i.Done {
76 return nil
77 }
78 }
Sketch🕴️305f8172026-02-27 13:58:43 +040079 return []*ToDo{todo}
80 }
Sketch🕴️00202652026-02-28 21:10:00 +040081 return ret
Sketch🕴️305f8172026-02-27 13:58:43 +040082}