X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/0018014ab36f3de61a4b8a5515009dab803d6c38..396af2e18eb02d4ec1e35edbc62f01e84bae2b46:/daemon.go?ds=inline diff --git a/daemon.go b/daemon.go index 41b3ab6..ca3d5ab 100644 --- 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()) }() + whatsappSetDeviceProps() whatsapp, err := openWhatsApp(ctx, dir, time.Now, log.New(out, "whatsapp: ", log.LstdFlags), whatsappNewClient) if err != nil { return err @@ -117,7 +118,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) }) } @@ -306,8 +307,9 @@ 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 notes in the loop and tells all clients its notices. When bot is not nil, it runs the bot too, and a failure of the bot -// stops the agent. +// 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 +340,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, l, notify) + whatsappErr = whatsapp.Run(ctx, l, notify) + cancel() }() var botErr error @@ -365,7 +369,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