]> git.giacomolli.uk - onechat/blobdiff - daemon.go
Send stored images to WhatsApp chats
[onechat] / daemon.go
index 3a54b1ae94374c7d53c3938f1eeff0ae8fcb9768..5c94a4f25daff64fbc3d2339e0451bb46d43edf8 100644 (file)
--- a/daemon.go
+++ b/daemon.go
@@ -353,6 +353,10 @@ func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, w
                return errors.Join(err, listener.Close())
        }
 
+       if bot != nil {
+               l.tools.imageShow.bot = bot
+       }
+
        loops := []*TurnLoop{l}
        for _, child := range children {
                loop, err := child.Loop(nil, func(Event) {
@@ -364,9 +368,22 @@ func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, w
                loops = append(loops, loop)
        }
 
+       messages, err := openDaemonMessages(loops)
+       if err != nil {
+               return errors.Join(fmt.Errorf("open agent messages: %w", err), listener.Close())
+       }
+
        hub.snapshot = func(ctx context.Context, snapshot func() error) error {
-               return daemonSnapshot(ctx, loops, snapshot)
+               return messages.SnapshotAgents(ctx, snapshot)
+       }
+
+       runtime := &DaemonChildren{
+               ctx:      ctx,
+               cancel:   cancel,
+               root:     l,
+               messages: messages,
        }
+       l.tools.children = runtime
 
        var workers sync.WaitGroup
 
@@ -442,7 +459,7 @@ func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, w
        hub.CloseAll()
        workers.Wait()
 
-       return errors.Join(err, whatsappErr, schedulesErr, botErr, errors.Join(childErrors...))
+       return errors.Join(err, whatsappErr, schedulesErr, botErr, errors.Join(childErrors...), runtime.Close())
 }
 
 // daemonRunChild runs a child's turn loop and its schedules until either stops.
@@ -480,6 +497,27 @@ func daemonRunRequest(l *TurnLoop, whatsapp *WhatsApp, request SocketRequest, an
                _, err := l.Submit(terminalSource, request.Text)
 
                return err
+       case "image":
+               if !filepath.IsAbs(request.Path) {
+                       return errors.New("use /image PATH [caption]")
+               }
+
+               data, err := imageReadFile(request.Path)
+               if err != nil {
+                       return fmt.Errorf("read terminal image: %w", err)
+               }
+
+               name, err := l.agent.Images().Save(data)
+               if err != nil {
+                       return fmt.Errorf("store terminal image: %w", err)
+               }
+
+               _, err = l.Submit(terminalSource, imageText([]string{name}, request.Text))
+               if err != nil {
+                       return fmt.Errorf("submit terminal image: %w", err)
+               }
+
+               return nil
        case "interrupt":
                if !l.Interrupt() {
                        return errors.New("no turn runs")