feat(api): per-agent unified control + clear_memory + delete_cache

- Manager: RegisterUnifiedAgent/UnregisterUnifiedAgent/StopUnifiedAgent/
  IsUnifiedAgentRunning/UptimeSeconds — cancela goroutines individuales sin
  matar el launcher
- Manager: UptimeSeconds en AgentStatus via startedAt map
- api/server: AgentController interface + WithController/WithDataDir builders
  + rutas POST /agents/{id}/clear_memory y /agents/{id}/delete_cache
- api/handlers: handleStartAgent/Stop/Restart delegan a controller en modo
  unified; Messages24h enriquecido via queryMessages24h (cache 30s)
- api/handlers: handleClearMemory — para la goroutine, borra messages+facts de
  memory.db, responde {status,messages_deleted,facts_deleted}
- api/handlers: handleDeleteCache — para la goroutine, elimina crypto/ y cache/,
  responde {status,paths_deleted}
- launcher/registry: launchGoroutine extrae goroutine con contexto per-agente;
  deps.procMgr hookea RegisterUnified; startAgent permite relanzar via reload
- launcher/main: agentController implementa api.AgentController sobre registry;
  mgr compartido entre API y registry; WithController+WithDataDir cableados

Co-Authored-By: fn-orquestador <noreply@fn-registry>
This commit is contained in:
2026-05-22 22:56:46 +02:00
parent 3db4443b65
commit 261f96f71b
5 changed files with 482 additions and 52 deletions
+34 -5
View File
@@ -22,13 +22,28 @@ import (
"github.com/enmanuel/agents/shell/process"
)
// AgentController is an optional interface for per-agent unified-mode control.
// The launcher can implement this to allow the API to stop/start individual
// agent goroutines without restarting the whole process.
type AgentController interface {
// StopUnifiedAgent cancels the goroutine context for the agent with the given ID.
// Returns an error if the agent is not currently running in unified mode.
StopUnifiedAgent(id string) error
// StartUnifiedAgent re-launches the agent goroutine for the given ID.
// Returns an error if the agent is not registered.
StartUnifiedAgent(id string) error
}
// Server is the HTTP API server.
type Server struct {
mgr *process.Manager
apiKey string
port int
logger *slog.Logger
bus *Bus
mgr *process.Manager
apiKey string
port int
logger *slog.Logger
bus *Bus
controller AgentController // optional: per-agent unified control (nil = not available)
// dataDir is the base directory for agent runtime data used for memory/cache queries.
dataDir string
}
// New creates a new Server. apiKey is compared with subtle.ConstantTimeCompare.
@@ -46,6 +61,18 @@ func New(mgr *process.Manager, apiKey string, port int, logger *slog.Logger) *Se
}
}
// WithController attaches an AgentController for unified-mode per-agent control.
func (s *Server) WithController(c AgentController) *Server {
s.controller = c
return s
}
// WithDataDir sets the base directory for agent runtime data (memory.db, crypto/).
func (s *Server) WithDataDir(dir string) *Server {
s.dataDir = dir
return s
}
// Run starts the HTTP server and blocks until ctx is done.
// It also starts the status-diff poller that feeds /sse/status.
func (s *Server) Run(ctx context.Context) error {
@@ -61,6 +88,8 @@ func (s *Server) Run(ctx context.Context) error {
mux.Handle("POST /agents/{id}/stop", s.auth(http.HandlerFunc(s.handleStopAgent)))
mux.Handle("POST /agents/{id}/restart", s.auth(http.HandlerFunc(s.handleRestartAgent)))
mux.Handle("GET /agents/{id}/logs", s.auth(http.HandlerFunc(s.handleAgentLogs)))
mux.Handle("POST /agents/{id}/clear_memory", s.auth(http.HandlerFunc(s.handleClearMemory)))
mux.Handle("POST /agents/{id}/delete_cache", s.auth(http.HandlerFunc(s.handleDeleteCache)))
// SSE endpoints
mux.Handle("GET /sse/status", s.auth(http.HandlerFunc(s.handleSSEStatus)))