| package loop |
| |
| import ( |
| "fmt" |
| "time" |
| ) |
| |
| type Agent interface { |
| Run(todo *ToDo) error |
| } |
| |
| type UserAgent struct { |
| pr PromptReader |
| } |
| |
| func (a *UserAgent) Run(todo *ToDo) error { |
| for { |
| unlocked := false |
| todo.Lock() |
| items := findActionableItems(todo, "user") |
| if len(items) == 0 { |
| fmt.Println("## USER NO ITEMS") |
| todo.Unlock() |
| time.Sleep(30 * time.Second) |
| continue |
| } |
| fmt.Println(todo.String()) |
| fmt.Printf("-- YOU START WORKING %d\n", len(items)) |
| for _, i := range items { |
| fmt.Printf("YOU %s %s: ", i.ID, i.Title) |
| comment, err := a.pr.Read() |
| if err != nil { |
| return err |
| } |
| if comment == "DONE" { |
| i.Done = true |
| continue |
| } |
| if comment == "BREAK" { |
| unlocked = true |
| todo.Unlock() |
| time.Sleep(30 * time.Second) |
| break |
| } |
| if comment == "ASSIGN" { |
| i.AssignedTo = "assistant" |
| continue |
| } |
| i.Discussion = append(i.Discussion, Comment{ |
| Author: "user", |
| Comment: comment, |
| }) |
| i.AssignedTo = "assistant" |
| } |
| fmt.Println("-- YOU END WORKING") |
| if !unlocked { |
| todo.Unlock() |
| } |
| } |
| } |
| |
| func findActionableItems(todo *ToDo, assignedTo string) []*ToDo { |
| if todo.Done { |
| return nil |
| } |
| var ret []*ToDo |
| for _, i := range todo.Items { |
| ret = append(ret, findActionableItems(i, assignedTo)...) |
| if len(ret) > 0 && !todo.Parallel { |
| return ret |
| } |
| } |
| if len(ret) == 0 && todo.AssignedTo == assignedTo && !todo.Done { |
| for _, i := range todo.Items { |
| if !i.Done { |
| return nil |
| } |
| } |
| return []*ToDo{todo} |
| } |
| return ret |
| } |