feat: Add web search results handling and enhance knowledge retrieval features

- Introduced a new endpoint to fetch chunks by ID, improving access to specific knowledge base entries.
- Updated tool result types to include web search results, enhancing the display capabilities in the chat component.
- Enhanced the AgentStreamDisplay component to support rendering web search results with improved HTML formatting.
- Implemented logic for managing web search result summaries and citations, ensuring a cohesive user experience.
- Refactored session management to accommodate new web search functionalities, improving overall system integration.
This commit is contained in:
wizardchen
2025-11-11 22:26:59 +08:00
parent d2adc38baf
commit 14d0e663cc
25 changed files with 1317 additions and 650 deletions
+58
View File
@@ -22,6 +22,64 @@ func NewChunkHandler(service interfaces.ChunkService) *ChunkHandler {
return &ChunkHandler{service: service}
}
// GetChunkByIDOnly gets a chunk by its ID only (without requiring knowledge_id)
func (h *ChunkHandler) GetChunkByIDOnly(c *gin.Context) {
ctx := c.Request.Context()
logger.Info(ctx, "Start retrieving chunk by ID only")
chunkID := c.Param("id")
if chunkID == "" {
logger.Error(ctx, "Chunk ID is empty")
c.Error(errors.NewBadRequestError("Chunk ID cannot be empty"))
return
}
// Get tenant ID from context
tenantID, exists := c.Get(types.TenantIDContextKey.String())
if !exists {
logger.Error(ctx, "Failed to get tenant ID")
c.Error(errors.NewUnauthorizedError("Unauthorized"))
return
}
logger.Infof(ctx, "Retrieving chunk by ID, chunk ID: %s, tenant ID: %d", chunkID, tenantID)
// Get chunk by ID
chunk, err := h.service.GetChunkByID(ctx, chunkID)
if err != nil {
if err == service.ErrChunkNotFound {
logger.Warnf(ctx, "Chunk not found, chunk ID: %s", chunkID)
c.Error(errors.NewNotFoundError("Chunk not found"))
return
}
logger.ErrorWithFields(ctx, err, nil)
c.Error(errors.NewInternalServerError(err.Error()))
return
}
// Validate tenant ID
if chunk.TenantID != tenantID.(uint) {
logger.Warnf(
ctx,
"Tenant has no permission to access chunk, chunk ID: %s, req tenant: %d, chunk tenant: %d",
chunkID, tenantID.(uint), chunk.TenantID,
)
c.Error(errors.NewForbiddenError("No permission to access this chunk"))
return
}
// 对 chunk 内容进行安全清理
if chunk.Content != "" {
chunk.Content = secutils.SanitizeForDisplay(chunk.Content)
}
logger.Infof(ctx, "Successfully retrieved chunk by ID, chunk ID: %s", chunkID)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": chunk,
})
}
// ListKnowledgeChunks lists all chunks for a given knowledge ID
func (h *ChunkHandler) ListKnowledgeChunks(c *gin.Context) {
ctx := c.Request.Context()