]> git.giacomolli.uk - onechat/blobdiff - daemon.go
Send the compaction view in block messages
[onechat] / daemon.go
index 1366870a6322273239e959cd40747301af2ab891..3a950da5c91af35f2dc622837f45020f0a89f352 100644 (file)
--- a/daemon.go
+++ b/daemon.go
@@ -63,6 +63,7 @@ func daemon(ctx context.Context, out io.Writer) (err error) {
                err = errors.Join(err, tokens.Close())
        }()
 
                err = errors.Join(err, tokens.Close())
        }()
 
+       whatsappSetDeviceProps()
        whatsapp, err := openWhatsApp(ctx, dir, time.Now, log.New(out, "whatsapp: ", log.LstdFlags), whatsappNewClient)
        if err != nil {
                return err
        whatsapp, err := openWhatsApp(ctx, dir, time.Now, log.New(out, "whatsapp: ", log.LstdFlags), whatsappNewClient)
        if err != nil {
                return err
@@ -92,6 +93,11 @@ func daemon(ctx context.Context, out io.Writer) (err error) {
                err = errors.Join(err, memory.Close())
        }()
 
                err = errors.Join(err, memory.Close())
        }()
 
+       schedules, err := openSchedules(filepath.Join(agent.dir, scheduleFileName), time.Now)
+       if err != nil {
+               return err
+       }
+
        subagents := newSubagents(filepath.Join(agent.dir, subagentDirName), time.Now, subagentNameRandom)
        defer func() {
                err = errors.Join(err, subagents.Close())
        subagents := newSubagents(filepath.Join(agent.dir, subagentDirName), time.Now, subagentNameRandom)
        defer func() {
                err = errors.Join(err, subagents.Close())
@@ -116,8 +122,8 @@ func daemon(ctx context.Context, out io.Writer) (err error) {
                }
        }
 
                }
        }
 
-       return daemonServe(ctx, listener, telegram, whatsapp, func(notify func(Event)) (*TurnLoop, error) {
-               return newTurnLoop(agent, memory, subagents, whatsapp, client, time.Now, notify)
+       return daemonServe(ctx, listener, telegram, whatsapp, schedules, func(notify func(Event)) (*TurnLoop, error) {
+               return newTurnLoop(agent, memory, subagents, whatsapp, schedules, client, time.Now, notify)
        })
 }
 
        })
 }
 
@@ -307,9 +313,10 @@ func daemonListenSocket(path string) (net.Listener, error) {
 // daemonServe runs the loop that start makes and serves the clients of listener
 // until ctx ends or the loop fails. It closes listener. It runs whatsapp, which
 // logs its batches and notes in the loop and tells all clients its notices, and
 // daemonServe runs the loop that start makes and serves the clients of listener
 // until ctx ends or the loop fails. It closes listener. It runs whatsapp, which
 // logs its batches and notes in the loop and tells all clients its notices, and
-// a failure of whatsapp stops the agent. When bot is not nil, it runs the bot
-// too, and a failure of the bot stops the agent.
-func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, whatsapp *WhatsApp, start func(notify func(Event)) (*TurnLoop, error)) error {
+// a failure of whatsapp stops the agent. It runs schedules, which log their
+// runs in the loop, and a failure of schedules stops the agent too. When bot is
+// not nil, it runs the bot too, and a failure of the bot stops the agent.
+func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, whatsapp *WhatsApp, schedules *Schedules, start func(notify func(Event)) (*TurnLoop, error)) error {
        ctx, cancel := context.WithCancel(ctx)
        defer cancel()
 
        ctx, cancel := context.WithCancel(ctx)
        defer cancel()
 
@@ -347,6 +354,15 @@ func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, w
                cancel()
        }()
 
                cancel()
        }()
 
+       var schedulesErr error
+       workers.Add(1)
+       go func() {
+               defer workers.Done()
+
+               schedulesErr = schedules.Run(ctx, l)
+               cancel()
+       }()
+
        var botErr error
        if bot != nil {
                workers.Add(1)
        var botErr error
        if bot != nil {
                workers.Add(1)
@@ -368,7 +384,7 @@ func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, w
        hub.CloseAll()
        workers.Wait()
 
        hub.CloseAll()
        workers.Wait()
 
-       return errors.Join(err, whatsappErr, botErr)
+       return errors.Join(err, whatsappErr, schedulesErr, botErr)
 }
 
 // daemonRunRequest runs one request of a client. The answers of a WhatsApp
 }
 
 // daemonRunRequest runs one request of a client. The answers of a WhatsApp