Files
Agent-System/internal/server/chat_search_api.go
T
weichenandCursor f6813cc272 feat(api): add additive HTTP/SSE error envelopes
Public errors now emit code, safe message, and request_id while keeping the legacy error field. Raw SQL, paths, tokens, and XSS payloads are stripped; the frontend branches on code instead of substring matching.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-15 14:40:56 +00:00

76 lines
1.9 KiB
Go

package server
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"chat-agent-system/internal/app/chatflow"
"chat-agent-system/internal/ctxmgr"
)
// handleChatSearch 处理消息三模检索 (keyword/fuzzy/semantic/hybrid)
// GET /api/v1/chat/search?session_id=&q=&mode=&category=&date_from=&date_to=&limit=&user_id=
func (s *Server) handleChatSearch(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeSafeError(w, r, http.StatusMethodNotAllowed, "Method Not Allowed")
return
}
user, ok := s.requireAuthenticatedUser(w, r)
if !ok {
return
}
if s.chatLog == nil {
writeSafeError(w, r, http.StatusServiceUnavailable, "context manager not initialized")
return
}
sessionID := strings.TrimSpace(r.URL.Query().Get("session_id"))
if sessionID == "" {
writeSafeError(w, r, http.StatusBadRequest, "missing required query param 'session_id'")
return
}
query := strings.TrimSpace(r.URL.Query().Get("q"))
if query == "" {
writeSafeError(w, r, http.StatusBadRequest, "missing or empty query param 'q'")
return
}
mode := ctxmgr.ChatSearchMode(strings.TrimSpace(r.URL.Query().Get("mode")))
category := strings.TrimSpace(r.URL.Query().Get("category"))
dateFrom := strings.TrimSpace(r.URL.Query().Get("date_from"))
dateTo := strings.TrimSpace(r.URL.Query().Get("date_to"))
limit := 50
if limStr := r.URL.Query().Get("limit"); limStr != "" {
if val, err := strconv.Atoi(limStr); err == nil && val > 0 {
limit = val
}
}
hits, err := s.chatLog.Search(r.Context(), s.chatPrincipal(user, chatflow.ActionSearch), ctxmgr.ChatSearchQuery{
SessionID: sessionID,
Query: query,
Mode: mode,
Category: category,
DateFrom: dateFrom,
DateTo: dateTo,
Limit: limit,
})
if err != nil {
s.writeChatLogError(w, err)
return
}
if hits == nil {
hits = []ctxmgr.ChatSearchHit{}
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(hits)
}