]> git.giacomolli.uk - onechat/blobdiff - turn.go
Bind external events to permanent agent IDs
[onechat] / turn.go
diff --git a/turn.go b/turn.go
index c08e94a0eed7d94a22e787d7bbaf97a69ce89289..4652321ad2105976a595a74154e1310b1f892443 100644 (file)
--- a/turn.go
+++ b/turn.go
@@ -16,7 +16,9 @@ import (
 // run. ID is the ID of a logged message. Source names the client that sent a
 // user message, such as terminal or telegram, so that each client can show the
 // messages of the others, or the subagent of a report. Status tells how the run
 // run. ID is the ID of a logged message. Source names the client that sent a
 // user message, such as terminal or telegram, so that each client can show the
 // messages of the others, or the subagent of a report. Status tells how the run
-// of a report ended: finished, stopped, or failed.
+// of a report ended: finished, stopped, or failed. A private notify receipt
+// uses ID for its queue sequence and Status for accepted or duplicate, not
+// processed.
 type Event struct {
        Kind   string `json:"kind"`
        ID     int    `json:"id,omitempty"`
 type Event struct {
        Kind   string `json:"kind"`
        ID     int    `json:"id,omitempty"`
@@ -283,10 +285,11 @@ func (l *TurnLoop) Flush(ctx context.Context) error {
        return l.memory.Flush(ctx)
 }
 
        return l.memory.Flush(ctx)
 }
 
-// Run runs a turn each time user messages or reports wait, until ctx ends. It
-// returns the error of a failed log write, which stops the agent. At its start
-// it resumes the subagents that a stop of the daemon cut, and at its end it
-// ends the runs of the subagents and waits for them.
+// Run starts a turn when user messages, reports, or queued external events
+// wait, until ctx ends. It returns the error of a failed log write, which stops
+// the agent. External events enter memory only between chains. At its start it
+// resumes the subagents that a stop of the daemon cut, and at its end it ends
+// the runs of the subagents and waits for them.
 func (l *TurnLoop) Run(ctx context.Context) error {
        defer l.runs.Close()
 
 func (l *TurnLoop) Run(ctx context.Context) error {
        defer l.runs.Close()
 
@@ -296,12 +299,23 @@ func (l *TurnLoop) Run(ctx context.Context) error {
        }
 
        for {
        }
 
        for {
-               for ctx.Err() == nil && l.waiting() {
+               for ctx.Err() == nil {
                        err := l.failed()
                        if err != nil {
                                return fmt.Errorf("check turn loop failure: %w", err)
                        }
 
                        err := l.failed()
                        if err != nil {
                                return fmt.Errorf("check turn loop failure: %w", err)
                        }
 
+                       if l.messages != nil {
+                               err = l.messages.Notifications(l)
+                               if err != nil {
+                                       return fmt.Errorf("prepare external events: %w", err)
+                               }
+                       }
+
+                       if !l.waiting() {
+                               break
+                       }
+
                        err = l.turn(ctx)
                        if err != nil {
                                return fmt.Errorf("run turn: %w", err)
                        err = l.turn(ctx)
                        if err != nil {
                                return fmt.Errorf("run turn: %w", err)