-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
104 lines (92 loc) · 2.33 KB
/
task.go
File metadata and controls
104 lines (92 loc) · 2.33 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
package agent
import (
"sync"
"time"
"github.com/peerclaw/peerclaw-core/envelope"
)
// TaskState represents the lifecycle state of an A2A task.
type TaskState string
const (
TaskSubmitted TaskState = "submitted"
TaskWorking TaskState = "working"
TaskCompleted TaskState = "completed"
TaskFailed TaskState = "failed"
TaskCanceled TaskState = "canceled"
TaskInputRequired TaskState = "input_required"
)
// Task maps an Envelope request-response exchange to an A2A task lifecycle.
type Task struct {
ID string
TraceID string
AgentID string // destination agent
State TaskState
Request *envelope.Envelope
Response *envelope.Envelope // terminal response (nil until completed/failed)
CreatedAt time.Time
UpdatedAt time.Time
}
// TaskTracker manages the lifecycle of tasks keyed by TraceID.
type TaskTracker struct {
mu sync.RWMutex
tasks map[string]*Task
}
// NewTaskTracker creates an empty TaskTracker.
func NewTaskTracker() *TaskTracker {
return &TaskTracker{
tasks: make(map[string]*Task),
}
}
// Submit creates a new task in Submitted state from an outgoing request envelope.
func (tt *TaskTracker) Submit(env *envelope.Envelope) *Task {
now := time.Now()
t := &Task{
ID: env.ID,
TraceID: env.TraceID,
AgentID: env.Destination,
State: TaskSubmitted,
Request: env,
CreatedAt: now,
UpdatedAt: now,
}
tt.mu.Lock()
tt.tasks[env.TraceID] = t
tt.mu.Unlock()
return t
}
// Update transitions a task to a new state with an optional response envelope.
func (tt *TaskTracker) Update(traceID string, state TaskState, resp *envelope.Envelope) {
tt.mu.Lock()
defer tt.mu.Unlock()
t, ok := tt.tasks[traceID]
if !ok {
return
}
t.State = state
t.UpdatedAt = time.Now()
if resp != nil {
t.Response = resp
}
}
// Get returns a task by its TraceID.
func (tt *TaskTracker) Get(traceID string) (*Task, bool) {
tt.mu.RLock()
defer tt.mu.RUnlock()
t, ok := tt.tasks[traceID]
return t, ok
}
// List returns all tracked tasks.
func (tt *TaskTracker) List() []*Task {
tt.mu.RLock()
defer tt.mu.RUnlock()
tasks := make([]*Task, 0, len(tt.tasks))
for _, t := range tt.tasks {
tasks = append(tasks, t)
}
return tasks
}
// Remove deletes a task by its TraceID.
func (tt *TaskTracker) Remove(traceID string) {
tt.mu.Lock()
delete(tt.tasks, traceID)
tt.mu.Unlock()
}