-// TurnClient sends model calls.
-type TurnClient interface {
- Create(ctx context.Context, request ModelRequest) (ModelResponse, error)
+// 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. 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
+ }
+
+ l := &TurnLoop{
+ agent: agent,
+ memory: memory,
+ client: client,
+ layout: layout,
+ clock: clock,
+ logger: logger,
+ notify: notify,
+ wake: make(chan struct{}, 1),
+ }
+
+ l.runs = &SubagentRuns{
+ agent: agent,
+ memory: memory,
+ subagents: subagents,
+ layout: layout,
+ client: client,
+ state: l.state,
+ report: l.Report,
+ note: l.note,
+ fail: l.fail,
+ logger: logger,
+ notify: notify,
+ running: make(map[string]*SubagentRun),
+ }
+
+ if agent.config.Parent != "" {
+ whatsapp = nil
+ }
+
+ l.tools = &Toolbox{
+ log: l,
+ chat: memory,
+ subagents: subagents,
+ runs: l.runs,
+ whatsapp: whatsapp,
+ schedules: schedules,
+ workdir: agent.config.Workdir,
+ }
+
+ return l, nil