]> git.giacomolli.uk - onechat/blobdiff - turn.go
Resume queued appends at their saved log ID
[onechat] / turn.go
diff --git a/turn.go b/turn.go
index e490068659da9fc4194a4f6eeb04a851ac464dee..2e5e50eef577cd3da3656dbaaa3122569ed112da 100644 (file)
--- a/turn.go
+++ b/turn.go
@@ -4,7 +4,7 @@ import (
        "context"
        "errors"
        "fmt"
        "context"
        "errors"
        "fmt"
-       "log"
+       "log/slog"
        "path/filepath"
        "sync"
        "time"
        "path/filepath"
        "sync"
        "time"
@@ -13,8 +13,9 @@ import (
 // newTurnLoop returns the loop of agent with its memory, its subagents, and the
 // WhatsApp link and the schedules of its tools. Its calls go through client,
 // and notify gets each event. The clock gives the date of a turn. logger gets
 // newTurnLoop returns the loop of agent with its memory, its subagents, and the
 // WhatsApp link and the schedules of its tools. Its calls go through client,
 // and notify gets each event. The clock gives the date of a turn. logger gets
-// the failed turns and the ends of the subagents.
-func newTurnLoop(agent *Agent, memory *Memory, subagents *Subagents, whatsapp *WhatsApp, schedules *Schedules, client ChainClient, clock func() time.Time, logger *log.Logger, notify func(Event)) (*TurnLoop, error) {
+// the failed turns and the ends of the subagents. A child has no WhatsApp
+// tools, even when the caller supplies a link.
+func newTurnLoop(agent *Agent, memory *Memory, subagents *Subagents, whatsapp *WhatsApp, schedules *Schedules, client ChainClient, clock func() time.Time, logger *slog.Logger, notify func(Event)) (*TurnLoop, error) {
        layout, err := loadViewLayout(filepath.Join(agent.dir, viewLayoutFileName))
        if err != nil {
                return nil, err
        layout, err := loadViewLayout(filepath.Join(agent.dir, viewLayoutFileName))
        if err != nil {
                return nil, err
@@ -46,6 +47,10 @@ func newTurnLoop(agent *Agent, memory *Memory, subagents *Subagents, whatsapp *W
                running:   make(map[string]*SubagentRun),
        }
 
                running:   make(map[string]*SubagentRun),
        }
 
+       if agent.config.Parent != "" {
+               whatsapp = nil
+       }
+
        l.tools = &Toolbox{
                log:       l,
                chat:      memory,
        l.tools = &Toolbox{
                log:       l,
                chat:      memory,
@@ -92,7 +97,7 @@ type TurnLoop struct {
        runs   *SubagentRuns
        layout *ViewLayout
        clock  func() time.Time
        runs   *SubagentRuns
        layout *ViewLayout
        clock  func() time.Time
-       logger *log.Logger
+       logger *slog.Logger
        notify func(Event)
        wake   chan struct{}
 
        notify func(Event)
        wake   chan struct{}
 
@@ -112,6 +117,7 @@ func (l *TurnLoop) Submit(source, text string) (Message, error) {
        }
 
        l.mutex.Lock()
        }
 
        l.mutex.Lock()
+
        l.pending = append(l.pending, message)
        l.mutex.Unlock()
 
        l.pending = append(l.pending, message)
        l.mutex.Unlock()
 
@@ -167,6 +173,7 @@ func (l *TurnLoop) Wake(messages ...Message) {
        }
 
        l.mutex.Lock()
        }
 
        l.mutex.Lock()
+
        l.pending = append(l.pending, messages...)
        l.mutex.Unlock()
 
        l.pending = append(l.pending, messages...)
        l.mutex.Unlock()
 
@@ -285,6 +292,7 @@ func (l *TurnLoop) waiting() bool {
 // fail stops the loop with err.
 func (l *TurnLoop) fail(err error) {
        l.mutex.Lock()
 // fail stops the loop with err.
 func (l *TurnLoop) fail(err error) {
        l.mutex.Lock()
+
        l.failure = errors.Join(l.failure, err)
        l.mutex.Unlock()
 
        l.failure = errors.Join(l.failure, err)
        l.mutex.Unlock()
 
@@ -313,11 +321,13 @@ func (l *TurnLoop) turn(ctx context.Context) error {
        defer cancel()
 
        l.mutex.Lock()
        defer cancel()
 
        l.mutex.Lock()
+
        l.cancel = cancel
        l.mutex.Unlock()
 
        defer func() {
                l.mutex.Lock()
        l.cancel = cancel
        l.mutex.Unlock()
 
        defer func() {
                l.mutex.Lock()
+
                l.cancel = nil
                l.mutex.Unlock()
        }()
                l.cancel = nil
                l.mutex.Unlock()
        }()
@@ -329,7 +339,7 @@ func (l *TurnLoop) turn(ctx context.Context) error {
        err := l.chain(ctx)
        if err != nil && !errors.Is(err, errChainLogWrite) {
                if ctx.Err() == nil {
        err := l.chain(ctx)
        if err != nil && !errors.Is(err, errChainLogWrite) {
                if ctx.Err() == nil {
-                       l.logger.Printf("the turn stops before its end, and the agent waits for the next message: %v", err)
+                       l.logger.Error("turn failed", "err", err)
                }
 
                l.notify(Event{
                }
 
                l.notify(Event{
@@ -381,7 +391,7 @@ func (l *TurnLoop) chain(ctx context.Context) error {
                        Model:        l.agent.config.Model,
                        Effort:       l.agent.config.Effort,
                        Instructions: instructions,
                        Model:        l.agent.config.Model,
                        Effort:       l.agent.config.Effort,
                        Instructions: instructions,
-                       Tools:        promptTools(),
+                       Tools:        l.agent.Tools(),
                },
                take: l.take,
        }
                },
                take: l.take,
        }