package main import ( "bufio" "context" "encoding/json" "errors" "fmt" "io" "log" "net" "os" "path/filepath" "sync" "time" ) const ( // daemonAgentDirName is the directory of the root agent in the data // directory. daemonAgentDirName = "agent" // daemonClientEvents is the number of events that can wait for a slow // client before the daemon drops it. daemonClientEvents = 1024 ) // daemon runs "onechat daemon": it owns the root agent and its files and serves // the clients on the Unix socket in the data directory until ctx ends. When // telegram.json exists, it also runs the Telegram bot. It holds the lock of the // token file, so it is the only process that refreshes the tokens. func daemon(ctx context.Context, out io.Writer) (err error) { dir, err := dataDir() if err != nil { return err } // Check the socket path before the daemon writes a file, so that a data // directory with a long path stays empty. socket := filepath.Join(dir, socketFileName) err = checkSocketPath(socket) if err != nil { return err } config, err := loadConfig(dir) if err != nil { return err } botSettings, hasBot, err := loadTelegramConfig(dir) if err != nil { return err } tokens, err := lockTokens(dir) if err != nil { return err } defer func() { err = errors.Join(err, tokens.Close()) }() address, auth, err := modelEndpoint(config, dir) if err != nil { return err } agent, err := openAgent(filepath.Join(dir, daemonAgentDirName)) if err != nil { return err } client := newModelClient(address, auth, agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl")) memory, err := openMemory(ctx, agent.dir, time.Now, newCompactor(client, agent.Prompt, config.Compactor)) if err != nil { return err } defer func() { err = errors.Join(err, memory.Close()) }() listener, err := daemonListenSocket(socket) if err != nil { return err } _, 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(newTelegramAPI(telegramAPIURL, botSettings.Token), botSettings.Owner, filepath.Join(agent.dir, telegramOffsetFileName), 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 daemonServe(ctx, listener, telegram, func(notify func(Event)) (*TurnLoop, error) { return newTurnLoop(agent, memory, client, time.Now, notify) }) } // DaemonHub holds the connected clients and sends each event to all of them. type DaemonHub struct { mutex sync.Mutex clients map[*DaemonClient]bool workers sync.WaitGroup } // Accept serves each client of listener until the listener closes. A new // client gets a ready event with the name of the agent, and a busy event when a // turn runs. func (h *DaemonHub) Accept(ctx context.Context, listener net.Listener, l *TurnLoop) { for { conn, err := listener.Accept() if err != nil { return } client := &DaemonClient{ conn: conn, events: make(chan Event, daemonClientEvents), } h.mutex.Lock() h.clients[client] = true h.mutex.Unlock() h.workers.Add(2) go h.write(client) go h.read(ctx, client, l) h.Send(client, Event{ Kind: "ready", Text: l.agent.config.Name, }) if l.Busy() { h.Send(client, Event{ Kind: "busy", }) } } } // write sends the events of client as JSON lines until its channel closes. func (h *DaemonHub) write(client *DaemonClient) { defer h.workers.Done() encoder := json.NewEncoder(client.conn) for event := range client.events { err := encoder.Encode(event) if err != nil { h.drop(client) } } } // read runs the requests of client until it closes its connection. func (h *DaemonHub) read(ctx context.Context, client *DaemonClient, l *TurnLoop) { defer h.workers.Done() defer h.drop(client) scanner := bufio.NewScanner(client.conn) scanner.Buffer(make([]byte, 64*1024), socketRequestBytes) for ctx.Err() == nil && scanner.Scan() { var request SocketRequest err := json.Unmarshal(scanner.Bytes(), &request) if err == nil { err = daemonRunRequest(l, request) } if err != nil { h.Send(client, Event{ Kind: "error", Text: err.Error(), }) } } } // Send gives event to one client, or drops the client when its events do not // fit. func (h *DaemonHub) Send(client *DaemonClient, event Event) { h.mutex.Lock() defer h.mutex.Unlock() h.send(client, event) } // drop removes client and closes its connection. func (h *DaemonHub) drop(client *DaemonClient) { h.mutex.Lock() defer h.mutex.Unlock() h.remove(client) } // send gives event to client. The caller holds the mutex. func (h *DaemonHub) send(client *DaemonClient, event Event) { if !h.clients[client] { return } select { case client.events <- event: default: h.remove(client) } } // remove removes client, closes its connection, and ends its writer. The // caller holds the mutex. func (h *DaemonHub) remove(client *DaemonClient) { delete(h.clients, client) client.once.Do(func() { close(client.events) _ = client.conn.Close() }) } // Broadcast gives event to every client. func (h *DaemonHub) Broadcast(event Event) { h.mutex.Lock() defer h.mutex.Unlock() for client := range h.clients { h.send(client, event) } } // CloseAll drops every client and waits for their readers and writers. func (h *DaemonHub) CloseAll() { h.mutex.Lock() for client := range h.clients { h.remove(client) } h.mutex.Unlock() h.workers.Wait() } // DaemonClient is one connected client. Its events wait in a channel for the // writer, so a slow client does not stop the agent. type DaemonClient struct { conn net.Conn events chan Event once sync.Once } // daemonListenSocket listens on the Unix socket at path, with mode 0600. The // caller owns the chat, so a socket file at path is left from a daemon that // ended, and daemonListenSocket removes it. func daemonListenSocket(path string) (net.Listener, error) { err := checkSocketPath(path) if err != nil { return nil, err } err = os.Remove(path) if err != nil && !errors.Is(err, os.ErrNotExist) { return nil, fmt.Errorf("remove old socket: %w", err) } listener, err := net.Listen("unix", path) if err != nil { return nil, fmt.Errorf("listen on socket: %w", err) } err = os.Chmod(path, 0o600) if err != nil { return nil, errors.Join(fmt.Errorf("set socket mode: %w", err), listener.Close()) } return listener, nil } // daemonServe 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 daemonServe(ctx context.Context, listener net.Listener, bot *TelegramBot, start func(notify func(Event)) (*TurnLoop, error)) error { ctx, cancel := context.WithCancel(ctx) defer cancel() hub := &DaemonHub{ clients: make(map[*DaemonClient]bool), } notify := hub.Broadcast if bot != nil { notify = func(event Event) { hub.Broadcast(event) bot.Notify(event) } } l, err := start(notify) if err != nil { return errors.Join(err, listener.Close()) } var workers sync.WaitGroup workers.Add(1) go func() { defer workers.Done() hub.Accept(ctx, listener, l) }() var botErr error if bot != nil { workers.Add(1) go func() { defer workers.Done() botErr = bot.Run(ctx, l) cancel() }() } err = l.Run(ctx) if errors.Is(err, context.Canceled) { err = nil } cancel() err = errors.Join(err, listener.Close()) hub.CloseAll() workers.Wait() return errors.Join(err, botErr) } // daemonRunRequest runs one request of a client. func daemonRunRequest(l *TurnLoop, request SocketRequest) error { switch request.Type { case "send": if request.Text == "" { return errors.New("empty message") } _, err := l.Submit(terminalSource, request.Text) return err case "interrupt": if !l.Interrupt() { return errors.New("no turn runs") } return nil default: return fmt.Errorf("unknown request %q", request.Type) } }