"context"
"errors"
"fmt"
- "log"
+ "log/slog"
"path/filepath"
"sync"
"time"
// 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) {
+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
runs *SubagentRuns
layout *ViewLayout
clock func() time.Time
- logger *log.Logger
+ logger *slog.Logger
notify func(Event)
wake chan struct{}
}
l.mutex.Lock()
+
l.pending = append(l.pending, message)
l.mutex.Unlock()
}
l.mutex.Lock()
+
l.pending = append(l.pending, messages...)
l.mutex.Unlock()
// 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()
defer cancel()
l.mutex.Lock()
+
l.cancel = cancel
l.mutex.Unlock()
defer func() {
l.mutex.Lock()
+
l.cancel = nil
l.mutex.Unlock()
}()
err := l.chain(ctx)
if err != nil && !errors.Is(err, errChainLogWrite) {
if ctx.Err() == nil {
- l.logger.Printf("turn failed: %v", err)
+ l.logger.Error("turn failed", "err", err)
}
l.notify(Event{