CUB-123: integrate gateway, wire PostgreSQL repositories, add SSE streaming
All checks were successful
Dev Build / build-test (pull_request) Successful in 2m23s
All checks were successful
Dev Build / build-test (pull_request) Successful in 2m23s
- Create repository/ package with pgx-backed CRUD for agents, sessions, tasks, projects - Define AgentRepo/SessionRepo/TaskRepo/ProjectRepo interfaces - Update handler to use repository interfaces instead of in-memory stores - Add SSE broker with GET /api/events endpoint (text/event-stream) - Add gateway client that polls OpenClaw for agent states - Add GATEWAY_URL and GATEWAY_POLL_INTERVAL config fields - Seed 5 demo agents (Otto, Rex, Dex, Hex, Pip) on empty DB - Update router to wire SSE broker - All 21 handler tests pass with mock repos
This commit is contained in:
@@ -1,43 +1,45 @@
|
||||
// Package handler contains HTTP handlers for the Control Center API.
|
||||
// Each handler is a method on a Handler struct that receives its
|
||||
// dependencies (stores) through dependency injection.
|
||||
// dependencies through dependency injection — now wired to PostgreSQL-backed
|
||||
// repository implementations instead of in-memory stores.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"code.cubecraftcreations.com/CubeCraft-Creations/Control-Center/go-backend/internal/models"
|
||||
"code.cubecraftcreations.com/CubeCraft-Creations/Control-Center/go-backend/internal/store"
|
||||
"code.cubecraftcreations.com/CubeCraft-Creations/Control-Center/go-backend/internal/repository"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
// Handler groups all route handlers and their dependencies.
|
||||
type Handler struct {
|
||||
AgentStore *store.AgentStore
|
||||
SessionStore *store.SessionStore
|
||||
TaskStore *store.TaskStore
|
||||
ProjectStore *store.ProjectStore
|
||||
validate *validator.Validate
|
||||
Agents repository.AgentRepo
|
||||
Sessions repository.SessionRepo
|
||||
Tasks repository.TaskRepo
|
||||
Projects repository.ProjectRepo
|
||||
validate *validator.Validate
|
||||
}
|
||||
|
||||
// NewHandler returns a fully wired Handler.
|
||||
// NewHandler returns a fully wired Handler with repository backends.
|
||||
func NewHandler(
|
||||
as *store.AgentStore,
|
||||
ss *store.SessionStore,
|
||||
ts *store.TaskStore,
|
||||
ps *store.ProjectStore,
|
||||
ar repository.AgentRepo,
|
||||
sr repository.SessionRepo,
|
||||
tr repository.TaskRepo,
|
||||
pr repository.ProjectRepo,
|
||||
) *Handler {
|
||||
v := validator.New()
|
||||
v.RegisterValidation("agentStatus", validateAgentStatus)
|
||||
return &Handler{
|
||||
AgentStore: as,
|
||||
SessionStore: ss,
|
||||
TaskStore: ts,
|
||||
ProjectStore: ps,
|
||||
validate: v,
|
||||
Agents: ar,
|
||||
Sessions: sr,
|
||||
Tasks: tr,
|
||||
Projects: pr,
|
||||
validate: v,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,15 +48,20 @@ func NewHandler(
|
||||
// ListAgents handles GET /api/agents.
|
||||
func (h *Handler) ListAgents(w http.ResponseWriter, r *http.Request) {
|
||||
statusFilter := models.AgentStatus(r.URL.Query().Get("status"))
|
||||
allAgents := h.AgentStore.List(statusFilter)
|
||||
allAgents, err := h.Agents.List(r.Context(), statusFilter)
|
||||
if err != nil {
|
||||
slog.Error("list agents failed", "error", err)
|
||||
writeJSON(w, http.StatusInternalServerError, models.ErrorResponse{Error: "failed to list agents"})
|
||||
return
|
||||
}
|
||||
|
||||
page, pageSize := parsePagination(r)
|
||||
start, end := paginateSlice(len(allAgents), page, pageSize)
|
||||
|
||||
pageSlice := allAgents[start:end]
|
||||
totalCount, _ := h.Agents.Count(r.Context())
|
||||
writeJSON(w, http.StatusOK, models.PaginatedResponse{
|
||||
Data: pageSlice,
|
||||
TotalCount: h.AgentStore.Count(),
|
||||
Data: allAgents[start:end],
|
||||
TotalCount: totalCount,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
HasMore: end < len(allAgents),
|
||||
@@ -64,8 +71,8 @@ func (h *Handler) ListAgents(w http.ResponseWriter, r *http.Request) {
|
||||
// GetAgent handles GET /api/agents/{id}.
|
||||
func (h *Handler) GetAgent(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
agent, ok := h.AgentStore.Get(id)
|
||||
if !ok {
|
||||
agent, err := h.Agents.Get(r.Context(), id)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, models.ErrorResponse{Error: "agent not found"})
|
||||
return
|
||||
}
|
||||
@@ -89,17 +96,17 @@ func (h *Handler) CreateAgent(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
agent := models.AgentCardData{
|
||||
ID: req.ID,
|
||||
DisplayName: req.DisplayName,
|
||||
Role: req.Role,
|
||||
Status: req.Status,
|
||||
CurrentTask: req.CurrentTask,
|
||||
SessionKey: req.SessionKey,
|
||||
Channel: req.Channel,
|
||||
ID: req.ID,
|
||||
DisplayName: req.DisplayName,
|
||||
Role: req.Role,
|
||||
Status: req.Status,
|
||||
CurrentTask: req.CurrentTask,
|
||||
SessionKey: req.SessionKey,
|
||||
Channel: req.Channel,
|
||||
LastActivity: time.Now().UTC().Format(time.RFC3339),
|
||||
}
|
||||
|
||||
if ok := h.AgentStore.Create(agent); !ok {
|
||||
if err := h.Agents.Create(r.Context(), agent); err != nil {
|
||||
writeJSON(w, http.StatusConflict, models.ErrorResponse{Error: "agent with this ID already exists"})
|
||||
return
|
||||
}
|
||||
@@ -124,8 +131,8 @@ func (h *Handler) UpdateAgent(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
agent, ok := h.AgentStore.Update(id, req)
|
||||
if !ok {
|
||||
agent, err := h.Agents.Update(r.Context(), id, req)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, models.ErrorResponse{Error: "agent not found"})
|
||||
return
|
||||
}
|
||||
@@ -135,7 +142,7 @@ func (h *Handler) UpdateAgent(w http.ResponseWriter, r *http.Request) {
|
||||
// DeleteAgent handles DELETE /api/agents/{id}.
|
||||
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
if ok := h.AgentStore.Delete(id); !ok {
|
||||
if err := h.Agents.Delete(r.Context(), id); err != nil {
|
||||
writeJSON(w, http.StatusNotFound, models.ErrorResponse{Error: "agent not found"})
|
||||
return
|
||||
}
|
||||
@@ -145,14 +152,11 @@ func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
|
||||
// AgentHistory handles GET /api/agents/{id}/history.
|
||||
func (h *Handler) AgentHistory(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
if _, ok := h.AgentStore.Get(id); !ok {
|
||||
if _, err := h.Agents.Get(r.Context(), id); err != nil {
|
||||
writeJSON(w, http.StatusNotFound, models.ErrorResponse{Error: "agent not found"})
|
||||
return
|
||||
}
|
||||
|
||||
history := h.AgentStore.History(id)
|
||||
if history == nil {
|
||||
history = []models.AgentStatusHistoryEntry{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, history)
|
||||
// History is not currently persisted in PostgreSQL — return stub.
|
||||
writeJSON(w, http.StatusOK, []models.AgentStatusHistoryEntry{})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user