X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/ec230eee6b06580031637bc97c99ec6db7b41926..0b97bb2cc4bc0b8f8fe27cc7aa3f9d1d89ed28c8:/daemon.go?ds=inline diff --git a/daemon.go b/daemon.go index d67c5be..1366870 100644 --- 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