-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinject.go
More file actions
55 lines (47 loc) · 1.61 KB
/
inject.go
File metadata and controls
55 lines (47 loc) · 1.61 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
package agentcore
import "fmt"
// InjectDisposition describes how an injected message was delivered.
type InjectDisposition string
const (
InjectSteeredCurrentRun InjectDisposition = "steered_current_run"
InjectResumedIdleRun InjectDisposition = "resumed_idle_run"
InjectQueued InjectDisposition = "queued"
)
// InjectResult reports the delivery outcome of Agent.Inject.
type InjectResult struct {
Disposition InjectDisposition
}
// Inject delivers a message as soon as the current agent state allows.
//
// Outcomes:
// - running → steer into current run
// - idle + assistant tail → enqueue and Continue()
// - idle + no assistant tail → enqueue for next run
//
// Returns an error if idle resume was attempted but Continue() failed.
// In that case the message remains in the steering queue and will be
// delivered on the next run.
func (a *Agent) Inject(msg AgentMessage) (InjectResult, error) {
if msg == nil {
return InjectResult{}, fmt.Errorf("inject message is nil")
}
a.mu.Lock()
if a.isRunning {
a.steeringQ = append(a.steeringQ, msg)
a.mu.Unlock()
return InjectResult{Disposition: InjectSteeredCurrentRun}, nil
}
canResume := false
if n := len(a.messages); n > 0 && a.messages[n-1] != nil {
canResume = a.messages[n-1].GetRole() == RoleAssistant
}
a.steeringQ = append(a.steeringQ, msg)
a.mu.Unlock()
if !canResume {
return InjectResult{Disposition: InjectQueued}, nil
}
if err := a.Continue(); err != nil {
return InjectResult{Disposition: InjectQueued}, fmt.Errorf("inject: idle resume failed: %w", err)
}
return InjectResult{Disposition: InjectResumedIdleRun}, nil
}