- _, err = fmt.Fprintf(out, "onechat: %s on %s, %s at %s effort, in %s\n", agent.config.Name, listener.Addr(), agent.config.Model, agent.config.Effort, agent.config.Workdir)
- if err != nil {
- return errors.Join(err, listener.Close())
- }
-
- var telegram *telegramBot
- if hasBot {
- telegram = newTelegramBot(newBotAPI(telegramAPI, botSettings.Token), botSettings.Owner, filepath.Join(agent.dir, offsetFileName), log.New(out, "telegram: ", log.LstdFlags))
- _, err = fmt.Fprintln(out, "onechat: the Telegram bot waits for messages of its owner")
- if err != nil {
- return errors.Join(err, listener.Close())
- }
- }
-
- return serveAgent(ctx, listener, telegram, func(notify func(Event)) (*loop, error) {
- return newLoop(agent, memory, client, time.Now, notify)
- })
-}
-
-// serveAgent runs the loop that start makes and serves the clients of
-// listener until ctx ends or the loop fails. It closes listener. When bot is
-// not nil, it runs the bot too, and a failure of the bot stops the agent.
-func serveAgent(ctx context.Context, listener net.Listener, bot *telegramBot, start func(notify func(Event)) (*loop, error)) error {
- ctx, cancel := context.WithCancel(ctx)
- defer cancel()
-
- hub := &hub{
- clients: make(map[*hubClient]bool),
- }
-
- notify := hub.Broadcast
- if bot != nil {
- notify = func(event Event) {
- hub.Broadcast(event)
- bot.Notify(event)
- }
- }