]> git.giacomolli.uk - onechat/blobdiff - daemon.go
Send WhatsApp messages with an ID
[onechat] / daemon.go
index d67c5be60c663aa9f712de3ac5819776a2ff3785..1366870a6322273239e959cd40747301af2ab891 100644 (file)
--- a/daemon.go
+++ b/daemon.go
@@ -117,7 +117,7 @@ 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, client, time.Now, notify)
+               return newTurnLoop(agent, memory, subagents, whatsapp, client, time.Now, notify)
        })
 }
 
@@ -305,9 +305,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 keeps whatsapp
-// connected. When bot is not nil, it runs the bot too, and a failure of the bot
-// stops the agent.
+// 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 {
        ctx, cancel := context.WithCancel(ctx)
        defer cancel()
@@ -338,10 +339,12 @@ func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, w
                hub.Accept(ctx, listener, l)
        }()
 
+       var whatsappErr error
        go func() {
                defer workers.Done()
 
-               whatsapp.Run(ctx)
+               whatsappErr = whatsapp.Run(ctx, l, notify)
+               cancel()
        }()
 
        var botErr error
@@ -365,7 +368,7 @@ func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, w
        hub.CloseAll()
        workers.Wait()
 
-       return errors.Join(err, botErr)
+       return errors.Join(err, whatsappErr, botErr)
 }
 
 // daemonRunRequest runs one request of a client. The answers of a WhatsApp