Simplified API
This commit is contained in:
231
taskcp.go
231
taskcp.go
@@ -3,7 +3,6 @@ package taskcp
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"iter"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -13,24 +12,17 @@ type Service struct {
|
||||
}
|
||||
|
||||
type Project struct {
|
||||
ID int
|
||||
Tasks []*Task
|
||||
nextTaskID int
|
||||
mcpService string
|
||||
ID int
|
||||
PendingTasks []*Task
|
||||
RunningTasks []*Task
|
||||
SuccessTasks []*Task
|
||||
FailureTasks []*Task
|
||||
mcpService string
|
||||
nextTaskID int
|
||||
}
|
||||
|
||||
type TaskState string
|
||||
|
||||
const (
|
||||
TaskStatePending TaskState = "pending"
|
||||
TaskStateRunning TaskState = "running"
|
||||
TaskStateSuccess TaskState = "success"
|
||||
TaskStateFailure TaskState = "failure"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
ID int `json:"id"`
|
||||
State TaskState `json:"-"`
|
||||
Title string `json:"title"`
|
||||
Instructions string `json:"instructions"`
|
||||
Data map[string]any `json:"data,omitempty"`
|
||||
@@ -38,21 +30,21 @@ type Task struct {
|
||||
Error string `json:"-"`
|
||||
Notes string `json:"-"`
|
||||
|
||||
NextTaskID int `json:"-"`
|
||||
|
||||
completionCallback func(task *Task) error
|
||||
project *Project
|
||||
completionCallback func(project *Project, task *Task) error
|
||||
}
|
||||
|
||||
type TaskSummary struct {
|
||||
Title string `json:"title"`
|
||||
State TaskState `json:"state"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
Title string `json:"title"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type ProjectSummary struct {
|
||||
Tasks []TaskSummary `json:"tasks"`
|
||||
PendingTasks []*TaskSummary `json:"pending_tasks"`
|
||||
RunningTasks []*TaskSummary `json:"running_tasks"`
|
||||
SuccessTasks []*TaskSummary `json:"success_tasks"`
|
||||
FailureTasks []*TaskSummary `json:"failure_tasks"`
|
||||
}
|
||||
|
||||
func New(mcpService string) *Service {
|
||||
@@ -63,10 +55,13 @@ func New(mcpService string) *Service {
|
||||
|
||||
func (s *Service) AddProject() *Project {
|
||||
project := &Project{
|
||||
ID: len(s.projects),
|
||||
Tasks: []*Task{},
|
||||
nextTaskID: -1,
|
||||
mcpService: s.mcpService,
|
||||
ID: len(s.projects),
|
||||
PendingTasks: []*Task{},
|
||||
RunningTasks: []*Task{},
|
||||
SuccessTasks: []*Task{},
|
||||
FailureTasks: []*Task{},
|
||||
mcpService: s.mcpService,
|
||||
nextTaskID: 0,
|
||||
}
|
||||
s.projects = append(s.projects, project)
|
||||
return project
|
||||
@@ -80,106 +75,75 @@ func (s *Service) GetProject(id int) (*Project, error) {
|
||||
return s.projects[id], nil
|
||||
}
|
||||
|
||||
func (p *Project) InsertTaskBefore(beforeID int) *Task {
|
||||
newTask := p.newTask(beforeID)
|
||||
|
||||
if p.nextTaskID == -1 && beforeID == -1 {
|
||||
p.nextTaskID = newTask.ID
|
||||
} else {
|
||||
for t := range p.tasks() {
|
||||
if t.NextTaskID == beforeID {
|
||||
t.NextTaskID = newTask.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newTask
|
||||
func (p *Project) AddNextTask() *Task {
|
||||
t := p.newTask()
|
||||
p.PendingTasks = append([]*Task{t}, p.PendingTasks...)
|
||||
return t
|
||||
}
|
||||
|
||||
func (p *Project) GetNextTask() *Task {
|
||||
if p.nextTaskID == -1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
task := p.Tasks[p.nextTaskID]
|
||||
task.State = TaskStateRunning
|
||||
return task
|
||||
func (p *Project) AddLastTask() *Task {
|
||||
t := p.newTask()
|
||||
p.PendingTasks = append(p.PendingTasks, t)
|
||||
return t
|
||||
}
|
||||
|
||||
func (p *Project) SetTaskSuccess(id int, result string, notes string) (*Task, error) {
|
||||
task := p.Tasks[id]
|
||||
task.State = TaskStateSuccess
|
||||
task.Result = result
|
||||
task.Notes = notes
|
||||
|
||||
if task.completionCallback != nil {
|
||||
err := task.completionCallback(task.project, task)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
p.nextTaskID = task.NextTaskID
|
||||
|
||||
return p.GetNextTask(), nil
|
||||
}
|
||||
|
||||
func (p *Project) SetTaskFailure(id int, error string, notes string) (*Task, error) {
|
||||
task := p.Tasks[id]
|
||||
task.State = TaskStateFailure
|
||||
task.Error = error
|
||||
task.Notes = notes
|
||||
|
||||
if task.completionCallback != nil {
|
||||
err := task.completionCallback(task.project, task)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
p.nextTaskID = task.NextTaskID
|
||||
|
||||
return p.GetNextTask(), nil
|
||||
}
|
||||
|
||||
func (p *Project) newTask(nextTaskID int) *Task {
|
||||
func (p *Project) newTask() *Task {
|
||||
task := &Task{
|
||||
ID: len(p.Tasks),
|
||||
State: TaskStatePending,
|
||||
NextTaskID: nextTaskID,
|
||||
Data: map[string]any{},
|
||||
project: p,
|
||||
ID: p.nextTaskID,
|
||||
Data: map[string]any{},
|
||||
project: p,
|
||||
}
|
||||
|
||||
p.Tasks = append(p.Tasks, task)
|
||||
p.nextTaskID++
|
||||
|
||||
return task
|
||||
}
|
||||
|
||||
func (p *Project) tasks() iter.Seq[*Task] {
|
||||
return func(yield func(*Task) bool) {
|
||||
for tid := p.nextTaskID; tid != -1; tid = p.Tasks[tid].NextTaskID {
|
||||
t := p.Tasks[tid]
|
||||
if !yield(t) {
|
||||
return
|
||||
}
|
||||
}
|
||||
func (p *Project) PopNextTask() (*Task, error) {
|
||||
if len(p.PendingTasks) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
task := p.PendingTasks[0]
|
||||
p.PendingTasks = p.PendingTasks[1:]
|
||||
return task, nil
|
||||
}
|
||||
|
||||
func (p *Project) Summary() ProjectSummary {
|
||||
var tasks []TaskSummary
|
||||
for _, task := range p.Tasks {
|
||||
if task.State != TaskStatePending {
|
||||
tasks = append(tasks, TaskSummary{
|
||||
Title: task.Title,
|
||||
State: task.State,
|
||||
Error: task.Error,
|
||||
Notes: task.Notes,
|
||||
})
|
||||
func (p *Project) GetRunningTask(id int) (*Task, error) {
|
||||
for _, task := range p.RunningTasks {
|
||||
if task.ID == id {
|
||||
return task, nil
|
||||
}
|
||||
}
|
||||
return ProjectSummary{Tasks: tasks}
|
||||
|
||||
return nil, fmt.Errorf("task not found: %d", id)
|
||||
}
|
||||
|
||||
func (p *Project) Summary() *ProjectSummary {
|
||||
s := &ProjectSummary{
|
||||
PendingTasks: []*TaskSummary{},
|
||||
RunningTasks: []*TaskSummary{},
|
||||
SuccessTasks: []*TaskSummary{},
|
||||
FailureTasks: []*TaskSummary{},
|
||||
}
|
||||
|
||||
for _, task := range p.PendingTasks {
|
||||
s.PendingTasks = append(s.PendingTasks, task.AsSummary())
|
||||
}
|
||||
|
||||
for _, task := range p.RunningTasks {
|
||||
s.RunningTasks = append(s.RunningTasks, task.AsSummary())
|
||||
}
|
||||
|
||||
for _, task := range p.SuccessTasks {
|
||||
s.SuccessTasks = append(s.SuccessTasks, task.AsSummary())
|
||||
}
|
||||
|
||||
for _, task := range p.FailureTasks {
|
||||
s.FailureTasks = append(s.FailureTasks, task.AsSummary())
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (t *Task) WithTitle(title string) *Task {
|
||||
@@ -199,9 +163,40 @@ func (t *Task) WithData(key string, value any) *Task {
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *Task) Then(completionCallback func(project *Project, task *Task) error) *Task {
|
||||
func (t *Task) Then(completionCallback func(task *Task) error) {
|
||||
t.completionCallback = completionCallback
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *Task) SetSuccess(result string, notes string) (*Task, error) {
|
||||
t.Result = result
|
||||
t.Notes = notes
|
||||
|
||||
t.project.SuccessTasks = append(t.project.SuccessTasks, t)
|
||||
|
||||
if t.completionCallback != nil {
|
||||
err := t.completionCallback(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return t.project.PopNextTask()
|
||||
}
|
||||
|
||||
func (t *Task) SetFailure(error string, notes string) (*Task, error) {
|
||||
t.Error = error
|
||||
t.Notes = notes
|
||||
|
||||
t.project.FailureTasks = append(t.project.FailureTasks, t)
|
||||
|
||||
if t.completionCallback != nil {
|
||||
err := t.completionCallback(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return t.project.PopNextTask()
|
||||
}
|
||||
|
||||
func (t *Task) SuccessPrompt() string {
|
||||
@@ -225,6 +220,14 @@ func (t *Task) String() string {
|
||||
return string(json)
|
||||
}
|
||||
|
||||
func (t *Task) AsSummary() *TaskSummary {
|
||||
return &TaskSummary{
|
||||
Title: t.Title,
|
||||
Error: t.Error,
|
||||
Notes: t.Notes,
|
||||
}
|
||||
}
|
||||
|
||||
func (ps ProjectSummary) String() string {
|
||||
json, err := json.MarshalIndent(ps, "", " ")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user