1package serve23import (4 "errors"5 "fmt"6 "path/filepath"7 "strings"89 "src.rest/cat/012/internal/cowork"10)1112// ListenForAgents lets agents on the server's machine, run by the13// server's user, join the files people have open (live mode,14// docs/agents/live.md): 012 mcp --attach names a file in the served15// folder, or a named room (@name), and the agent sits in its room with16// the people there. It needs sharing on.17func (s *Server) ListenForAgents(version string) (*cowork.Listener, error) {18 if s.rooms == nil {19 return nil, errors.New("sharing is off (--share off): agents join shared workbooks")20 }21 return cowork.Listen(cowork.Options{Registry: s.rooms, Room: s.agentRoom, Kind: "serve", Workbook: s.Dir(), Version: version})22}2324// agentRoom is the room of the file the agent names, which someone must25// have open.26func (s *Server) agentRoom(name string) (string, error) {27 if name == "" {28 return "", errors.New("name the file to join: 012 mcp --attach budget.012")29 }30 key := name31 if !strings.HasPrefix(name, "@") {32 p, err := s.root.Resolve(name)33 if err != nil {34 return "", fmt.Errorf("%s: %w", name, err)35 }36 if key, err = filepath.Abs(p); err != nil {37 return "", err38 }39 }40 if !s.rooms.Has(key) {41 return "", fmt.Errorf("nobody has %s open in 012 serve: an agent joins a file someone has open", name)42 }43 return key, nil44}