]> git.giacomolli.uk - onechat/blobdiff - daemon.go
Separate commands from the entry point
[onechat] / daemon.go
index 958d6f7bd2283fe8b2796c14ae345e7b5f14e604..31a671478cefea874dc86fe6aa0fe8d3bdbacf0a 100644 (file)
--- a/daemon.go
+++ b/daemon.go
@@ -90,6 +90,8 @@ func daemon(ctx context.Context, out io.Writer) (err error) {
                err = errors.Join(err, agents.Close())
        }()
 
+       whatsapp.inbox.voice = &VoiceTranscriber{config: config.Voice}
+
        root := agents[0]
        agent := root.agent
        schedules := root.schedules
@@ -111,6 +113,8 @@ func daemon(ctx context.Context, out io.Writer) (err error) {
        if hasBot {
                telegram = newTelegramBot(newTelegramAPI(telegramAPIURL, botSettings.Token), botSettings.Owner, filepath.Join(agent.dir, telegramOffsetFileName), daemonLogger(out, "telegram: "))
 
+               telegram.voice = &VoiceTranscriber{config: config.Voice}
+
                logger.Info("Telegram bot started")
        }
 
@@ -353,6 +357,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) {
@@ -370,10 +378,16 @@ func daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, w
        }
 
        hub.snapshot = func(ctx context.Context, snapshot func() error) error {
-               return messages.Snapshot(func() 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
 
@@ -449,7 +463,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.
@@ -487,6 +501,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")