Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions go/core/internal/httpserver/handlers/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"

ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kagent-dev/kagent/go/api/database"
"github.com/pgvector/pgvector-go"
)
Expand Down Expand Up @@ -69,6 +70,7 @@ type ListMemoryResponse struct {

// AddSession handles POST /api/memories/sessions
func (h *MemoryHandler) AddSession(w ErrorResponseWriter, r *http.Request) {
log := ctrllog.FromContext(r.Context())
var req AddSessionMemoryRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid request body")
Expand Down Expand Up @@ -106,12 +108,12 @@ func (h *MemoryHandler) AddSession(w ErrorResponseWriter, r *http.Request) {
}

if err := h.DatabaseService.StoreAgentMemory(r.Context(), memory); err != nil {
log.Printf("Failed to store agent memory: %v", err)
log.Error(err, "failed to store agent memory")
RespondWithError(w, http.StatusInternalServerError, fmt.Sprintf("failed to save memory: %v", err))
return
}

log.Printf("Successfully added memory ID %s for user %s agent %s", memory.ID, req.UserID, req.AgentName)
log.Info("added memory", "id", memory.ID, "userID", req.UserID, "agentName", req.AgentName)

RespondWithJSON(w, http.StatusCreated, map[string]string{"id": memory.ID})
}
Expand All @@ -123,6 +125,7 @@ type AddSessionMemoryBatchRequest struct {

// AddSessionBatch handles POST /api/memories/sessions/batch
func (h *MemoryHandler) AddSessionBatch(w ErrorResponseWriter, r *http.Request) {
log := ctrllog.FromContext(r.Context())
var req AddSessionMemoryBatchRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
RespondWithError(w, http.StatusBadRequest, "Invalid request body")
Expand Down Expand Up @@ -174,12 +177,12 @@ func (h *MemoryHandler) AddSessionBatch(w ErrorResponseWriter, r *http.Request)
}

if err := h.DatabaseService.StoreAgentMemories(r.Context(), memories); err != nil {
log.Printf("Failed to store agent memory batch: %v", err)
log.Error(err, "failed to store agent memory batch")
RespondWithError(w, http.StatusInternalServerError, fmt.Sprintf("failed to save memory batch: %v", err))
return
}

log.Printf("Successfully added %d memory items", len(memories))
log.Info("added memory batch", "count", len(memories))
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a consistent log key naming convention for structured fields (most handlers use camelCase keys like count, userID, agentName). Using count is fine, but if you add more fields later, stick to the established key style to keep log queries consistent.

Copilot uses AI. Check for mistakes.
RespondWithJSON(w, http.StatusCreated, map[string]int{"count": len(memories)})
}

Expand Down Expand Up @@ -242,6 +245,7 @@ func (h *MemoryHandler) Search(w ErrorResponseWriter, r *http.Request) {

// List handles GET /api/memories and returns all memories for an agent+user, ranked by access frequency
func (h *MemoryHandler) List(w ErrorResponseWriter, r *http.Request) {
log := ctrllog.FromContext(r.Context())
agentName := r.URL.Query().Get("agent_name")
userID := r.URL.Query().Get("user_id")

Expand All @@ -252,7 +256,7 @@ func (h *MemoryHandler) List(w ErrorResponseWriter, r *http.Request) {

memories, err := h.DatabaseService.ListAgentMemories(r.Context(), agentName, userID)
if err != nil {
log.Printf("Failed to list agent memories: %v", err)
log.Error(err, "failed to list agent memories")
RespondWithError(w, http.StatusInternalServerError, fmt.Sprintf("failed to list memories: %v", err))
return
}
Expand All @@ -276,6 +280,7 @@ func (h *MemoryHandler) List(w ErrorResponseWriter, r *http.Request) {

// Delete handles DELETE /api/memories
func (h *MemoryHandler) Delete(w ErrorResponseWriter, r *http.Request) {
log := ctrllog.FromContext(r.Context())
agentName := r.URL.Query().Get("agent_name")
userID := r.URL.Query().Get("user_id")

Expand All @@ -285,7 +290,7 @@ func (h *MemoryHandler) Delete(w ErrorResponseWriter, r *http.Request) {
}

if err := h.DatabaseService.DeleteAgentMemory(r.Context(), agentName, userID); err != nil {
log.Printf("Failed to delete agent memory: %v", err)
log.Error(err, "failed to delete agent memory")
RespondWithError(w, http.StatusInternalServerError, fmt.Sprintf("failed to delete memory: %v", err))
return
}
Expand Down
Loading