feat: implement chat history and retrieval settings
- Added new API endpoints for managing chat history configuration and retrieval settings, allowing tenants to enable message indexing and configure search parameters. - Introduced new Vue components for ChatHistorySettings and RetrievalSettings, providing a user-friendly interface for managing these configurations. - Updated localization files to include new settings descriptions and labels in multiple languages. - Enhanced the KnowledgeSearch view to support searching across both knowledge bases and chat history, improving the overall search functionality. These changes significantly enhance the application's capabilities in managing and retrieving chat history, contributing to a more robust user experience.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/errors"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
@@ -156,3 +157,97 @@ func (h *MessageHandler) DeleteMessage(c *gin.Context) {
|
||||
"message": "Message deleted successfully",
|
||||
})
|
||||
}
|
||||
|
||||
// SearchMessages godoc
|
||||
// @Summary 搜索历史对话
|
||||
// @Description 通过关键词和/或向量相似度搜索历史对话记录,支持关键词、向量、混合三种模式
|
||||
// @Tags 消息
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body SearchMessagesRequest true "搜索请求"
|
||||
// @Success 200 {object} map[string]interface{} "搜索结果"
|
||||
// @Failure 400 {object} errors.AppError "请求参数错误"
|
||||
// @Security Bearer
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /messages/search [post]
|
||||
func (h *MessageHandler) SearchMessages(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
logger.Info(ctx, "Start searching messages")
|
||||
|
||||
var request SearchMessagesRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error(ctx, "Failed to parse search request", err)
|
||||
c.Error(errors.NewBadRequestError(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if request.Query == "" {
|
||||
logger.Error(ctx, "Query content is empty")
|
||||
c.Error(errors.NewBadRequestError("Query content cannot be empty"))
|
||||
return
|
||||
}
|
||||
|
||||
params := &types.MessageSearchParams{
|
||||
Query: secutils.SanitizeForLog(request.Query),
|
||||
Mode: types.MessageSearchMode(request.Mode),
|
||||
Limit: request.Limit,
|
||||
SessionIDs: request.SessionIDs,
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Searching messages with params: query=%s, mode=%s, limit=%d, session_ids=%v",
|
||||
params.Query, params.Mode, params.Limit, params.SessionIDs)
|
||||
|
||||
result, err := h.MessageService.SearchMessages(ctx, params)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Message search completed, found %d results", result.Total)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": result,
|
||||
})
|
||||
}
|
||||
|
||||
// SearchMessagesRequest defines the request structure for searching messages
|
||||
type SearchMessagesRequest struct {
|
||||
// Query text for search
|
||||
Query string `json:"query" binding:"required"`
|
||||
// Search mode: "keyword", "vector", "hybrid" (default: "hybrid")
|
||||
Mode string `json:"mode"`
|
||||
// Maximum number of results to return (default: 20)
|
||||
Limit int `json:"limit"`
|
||||
// Filter by specific session IDs (optional)
|
||||
SessionIDs []string `json:"session_ids"`
|
||||
}
|
||||
|
||||
// GetChatHistoryKBStats godoc
|
||||
// @Summary 获取聊天历史知识库统计
|
||||
// @Description 获取聊天历史知识库的统计信息(已索引消息数、知识库大小等)
|
||||
// @Tags 消息
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]interface{} "统计信息"
|
||||
// @Security Bearer
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /messages/chat-history-stats [get]
|
||||
func (h *MessageHandler) GetChatHistoryKBStats(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
logger.Info(ctx, "Getting chat history KB stats")
|
||||
|
||||
stats, err := h.MessageService.GetChatHistoryKBStats(ctx)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": stats,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func (h *Handler) setupStopEventHandler(
|
||||
assistantMessage.Content = "用户停止了本次对话"
|
||||
// Use session's tenant for message update (ctx may have effectiveTenantID when using shared agent)
|
||||
updateCtx := context.WithValue(ctx, types.TenantIDContextKey, sessionTenantID)
|
||||
h.completeAssistantMessage(updateCtx, assistantMessage)
|
||||
h.completeAssistantMessage(updateCtx, assistantMessage, "") // empty query: stopped conversations are not indexed
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -420,7 +420,7 @@ func (h *Handler) executeNormalModeQA(reqCtx *qaRequestContext, generateTitle bo
|
||||
// Content already contains <think>...</think> tags from chat_completion_stream.go
|
||||
// Use session's tenant for message update (asyncCtx may have effectiveTenantID when using shared agent)
|
||||
updateCtx := context.WithValue(streamCtx.asyncCtx, types.TenantIDContextKey, reqCtx.session.TenantID)
|
||||
h.completeAssistantMessage(updateCtx, streamCtx.assistantMessage)
|
||||
h.completeAssistantMessage(updateCtx, streamCtx.assistantMessage, reqCtx.query)
|
||||
// Emit EventAgentComplete - this will trigger handleComplete which sends the SSE complete event
|
||||
// Note: Don't cancel context here, let the SSE handler close naturally after receiving the complete event
|
||||
streamCtx.eventBus.Emit(streamCtx.asyncCtx, event.Event{
|
||||
@@ -527,7 +527,7 @@ func (h *Handler) executeAgentModeQA(reqCtx *qaRequestContext) {
|
||||
}
|
||||
// Use session's tenant for message update (session belongs to session.TenantID; asyncCtx may have effectiveTenantID when using shared agent)
|
||||
updateCtx := context.WithValue(streamCtx.asyncCtx, types.TenantIDContextKey, reqCtx.session.TenantID)
|
||||
h.completeAssistantMessage(updateCtx, streamCtx.assistantMessage)
|
||||
h.completeAssistantMessage(updateCtx, streamCtx.assistantMessage, reqCtx.query)
|
||||
logger.Infof(streamCtx.asyncCtx, "Agent QA service completed for session: %s", sessionID)
|
||||
}()
|
||||
|
||||
@@ -561,9 +561,15 @@ func (h *Handler) executeAgentModeQA(reqCtx *qaRequestContext) {
|
||||
reqCtx.requestID, streamCtx.eventBus, reqCtx.session.Title == "")
|
||||
}
|
||||
|
||||
// completeAssistantMessage marks an assistant message as complete and updates it
|
||||
func (h *Handler) completeAssistantMessage(ctx context.Context, assistantMessage *types.Message) {
|
||||
// completeAssistantMessage marks an assistant message as complete, updates it,
|
||||
// and asynchronously indexes the Q&A pair into the chat history knowledge base.
|
||||
func (h *Handler) completeAssistantMessage(ctx context.Context, assistantMessage *types.Message, userQuery string) {
|
||||
assistantMessage.UpdatedAt = time.Now()
|
||||
assistantMessage.IsCompleted = true
|
||||
_ = h.messageService.UpdateMessage(ctx, assistantMessage)
|
||||
|
||||
// Asynchronously index the Q&A pair into the chat history knowledge base for vector search.
|
||||
// Use WithoutCancel so the goroutine survives after the HTTP request context is done.
|
||||
bgCtx := context.WithoutCancel(ctx)
|
||||
go h.messageService.IndexMessageToKB(bgCtx, userQuery, assistantMessage.Content, assistantMessage.ID, assistantMessage.SessionID)
|
||||
}
|
||||
|
||||
+189
-1
@@ -22,6 +22,7 @@ import (
|
||||
type TenantHandler struct {
|
||||
service interfaces.TenantService
|
||||
userService interfaces.UserService
|
||||
kbService interfaces.KnowledgeBaseService
|
||||
config *config.Config
|
||||
}
|
||||
|
||||
@@ -57,10 +58,11 @@ func (h *TenantHandler) authorizeTenantAccess(c *gin.Context, targetTenantID uin
|
||||
// - config: Application configuration
|
||||
//
|
||||
// Returns a pointer to the newly created TenantHandler
|
||||
func NewTenantHandler(service interfaces.TenantService, userService interfaces.UserService, config *config.Config) *TenantHandler {
|
||||
func NewTenantHandler(service interfaces.TenantService, userService interfaces.UserService, kbService interfaces.KnowledgeBaseService, config *config.Config) *TenantHandler {
|
||||
return &TenantHandler{
|
||||
service: service,
|
||||
userService: userService,
|
||||
kbService: kbService,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
@@ -632,6 +634,12 @@ func (h *TenantHandler) GetTenantKV(c *gin.Context) {
|
||||
case "storage-engine-config":
|
||||
h.GetTenantStorageEngineConfig(c)
|
||||
return
|
||||
case "chat-history-config":
|
||||
h.GetTenantChatHistoryConfig(c)
|
||||
return
|
||||
case "retrieval-config":
|
||||
h.GetTenantRetrievalConfig(c)
|
||||
return
|
||||
default:
|
||||
logger.Info(ctx, "KV key not supported", "key", key)
|
||||
c.Error(errors.NewBadRequestError("unsupported key"))
|
||||
@@ -672,6 +680,12 @@ func (h *TenantHandler) UpdateTenantKV(c *gin.Context) {
|
||||
case "storage-engine-config":
|
||||
h.updateTenantStorageEngineConfigInternal(c)
|
||||
return
|
||||
case "chat-history-config":
|
||||
h.updateTenantChatHistoryConfigInternal(c)
|
||||
return
|
||||
case "retrieval-config":
|
||||
h.updateTenantRetrievalConfigInternal(c)
|
||||
return
|
||||
default:
|
||||
logger.Info(ctx, "KV key not supported", "key", key)
|
||||
c.Error(errors.NewBadRequestError("unsupported key"))
|
||||
@@ -1014,3 +1028,177 @@ func (h *TenantHandler) GetPromptTemplates(c *gin.Context) {
|
||||
"data": templates,
|
||||
})
|
||||
}
|
||||
|
||||
// GetTenantChatHistoryConfig returns the tenant's chat history KB configuration.
|
||||
func (h *TenantHandler) GetTenantChatHistoryConfig(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
tenant, _ := types.TenantInfoFromContext(ctx)
|
||||
if tenant == nil {
|
||||
logger.Error(ctx, "Tenant is empty")
|
||||
c.Error(errors.NewBadRequestError("Tenant is empty"))
|
||||
return
|
||||
}
|
||||
data := tenant.ChatHistoryConfig
|
||||
if data == nil {
|
||||
data = &types.ChatHistoryConfig{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": data,
|
||||
})
|
||||
}
|
||||
|
||||
// updateTenantChatHistoryConfigInternal updates the tenant's chat history KB configuration.
|
||||
// When enabled with an embedding model and no KB exists yet, it auto-creates a hidden KB.
|
||||
func (h *TenantHandler) updateTenantChatHistoryConfigInternal(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
// The frontend sends: enabled, embedding_model_id
|
||||
// knowledge_base_id is managed internally.
|
||||
var req types.ChatHistoryConfig
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to parse request parameters", err)
|
||||
c.Error(errors.NewValidationError("Invalid request data").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
tenant, _ := types.TenantInfoFromContext(ctx)
|
||||
if tenant == nil {
|
||||
logger.Error(ctx, "Tenant is empty")
|
||||
c.Error(errors.NewBadRequestError("Tenant is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
existing := tenant.ChatHistoryConfig
|
||||
|
||||
// Build the new config, preserving the internally-managed knowledge_base_id
|
||||
cfg := &types.ChatHistoryConfig{
|
||||
Enabled: req.Enabled,
|
||||
EmbeddingModelID: req.EmbeddingModelID,
|
||||
KnowledgeBaseID: "", // will be set below
|
||||
}
|
||||
|
||||
// Carry over existing KB ID if the embedding model hasn't changed
|
||||
if existing != nil && existing.KnowledgeBaseID != "" {
|
||||
if existing.EmbeddingModelID == req.EmbeddingModelID {
|
||||
cfg.KnowledgeBaseID = existing.KnowledgeBaseID
|
||||
} else {
|
||||
// Embedding model changed — the old KB is incompatible.
|
||||
// We'll create a new one below. The old KB remains but is orphaned (can be cleaned up later).
|
||||
logger.Infof(ctx, "Embedding model changed from %s to %s, will create new chat history KB", existing.EmbeddingModelID, req.EmbeddingModelID)
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-create hidden KB if enabled + model set + no KB yet
|
||||
if cfg.Enabled && cfg.EmbeddingModelID != "" && cfg.KnowledgeBaseID == "" {
|
||||
kb := &types.KnowledgeBase{
|
||||
Name: "__chat_history__",
|
||||
Type: types.KnowledgeBaseTypeDocument,
|
||||
IsTemporary: true,
|
||||
Description: "Auto-managed knowledge base for chat history message indexing",
|
||||
EmbeddingModelID: cfg.EmbeddingModelID,
|
||||
}
|
||||
createdKB, err := h.kbService.CreateKnowledgeBase(ctx, kb)
|
||||
if err != nil {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError("Failed to create chat history knowledge base").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
cfg.KnowledgeBaseID = createdKB.ID
|
||||
logger.Infof(ctx, "Auto-created chat history KB: id=%s, embedding_model=%s", createdKB.ID, cfg.EmbeddingModelID)
|
||||
}
|
||||
|
||||
tenant.ChatHistoryConfig = cfg
|
||||
updatedTenant, err := h.service.UpdateTenant(ctx, tenant)
|
||||
if err != nil {
|
||||
if appErr, ok := errors.IsAppError(err); ok {
|
||||
c.Error(appErr)
|
||||
} else {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError("Failed to update chat history config").WithDetails(err.Error()))
|
||||
}
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": updatedTenant.ChatHistoryConfig,
|
||||
"message": "Chat history configuration updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
// GetTenantRetrievalConfig returns the tenant's global retrieval configuration.
|
||||
func (h *TenantHandler) GetTenantRetrievalConfig(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
tenant, _ := types.TenantInfoFromContext(ctx)
|
||||
if tenant == nil {
|
||||
logger.Error(ctx, "Tenant is empty")
|
||||
c.Error(errors.NewBadRequestError("Tenant is empty"))
|
||||
return
|
||||
}
|
||||
data := tenant.RetrievalConfig
|
||||
if data == nil {
|
||||
data = &types.RetrievalConfig{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": data,
|
||||
})
|
||||
}
|
||||
|
||||
// updateTenantRetrievalConfigInternal updates the tenant's global retrieval configuration.
|
||||
func (h *TenantHandler) updateTenantRetrievalConfigInternal(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
var cfg types.RetrievalConfig
|
||||
if err := c.ShouldBindJSON(&cfg); err != nil {
|
||||
logger.Error(ctx, "Failed to parse request parameters", err)
|
||||
c.Error(errors.NewValidationError("Invalid request data").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// Validate thresholds
|
||||
if cfg.VectorThreshold < 0 || cfg.VectorThreshold > 1 {
|
||||
c.Error(errors.NewBadRequestError("vector_threshold must be between 0 and 1"))
|
||||
return
|
||||
}
|
||||
if cfg.KeywordThreshold < 0 || cfg.KeywordThreshold > 1 {
|
||||
c.Error(errors.NewBadRequestError("keyword_threshold must be between 0 and 1"))
|
||||
return
|
||||
}
|
||||
if cfg.RerankThreshold < 0 || cfg.RerankThreshold > 1 {
|
||||
c.Error(errors.NewBadRequestError("rerank_threshold must be between 0 and 1"))
|
||||
return
|
||||
}
|
||||
if cfg.EmbeddingTopK < 0 || cfg.EmbeddingTopK > 200 {
|
||||
c.Error(errors.NewBadRequestError("embedding_top_k must be between 0 and 200"))
|
||||
return
|
||||
}
|
||||
if cfg.RerankTopK < 0 || cfg.RerankTopK > 200 {
|
||||
c.Error(errors.NewBadRequestError("rerank_top_k must be between 0 and 200"))
|
||||
return
|
||||
}
|
||||
|
||||
tenant, _ := types.TenantInfoFromContext(ctx)
|
||||
if tenant == nil {
|
||||
logger.Error(ctx, "Tenant is empty")
|
||||
c.Error(errors.NewBadRequestError("Tenant is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
tenant.RetrievalConfig = &cfg
|
||||
updatedTenant, err := h.service.UpdateTenant(ctx, tenant)
|
||||
if err != nil {
|
||||
if appErr, ok := errors.IsAppError(err); ok {
|
||||
c.Error(appErr)
|
||||
} else {
|
||||
logger.ErrorWithFields(ctx, err, nil)
|
||||
c.Error(errors.NewInternalServerError("Failed to update retrieval config").WithDetails(err.Error()))
|
||||
}
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": updatedTenant.RetrievalConfig,
|
||||
"message": "Retrieval configuration updated successfully",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user