2025-08-05 07:08:07 +00:00
package handler
import (
2026-01-30 08:28:01 +00:00
"context"
2025-08-05 07:08:07 +00:00
"net/http"
"github.com/Tencent/WeKnora/internal/application/service"
"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"
2025-09-15 11:13:59 +00:00
secutils "github.com/Tencent/WeKnora/internal/utils"
2025-08-05 07:08:07 +00:00
"github.com/gin-gonic/gin"
)
// ChunkHandler defines HTTP handlers for chunk operations
type ChunkHandler struct {
2026-02-08 06:32:18 +00:00
service interfaces . ChunkService
kgService interfaces . KnowledgeService
kbShareService interfaces . KBShareService
agentShareService interfaces . AgentShareService
2025-08-05 07:08:07 +00:00
}
// NewChunkHandler creates a new chunk handler
2026-02-08 06:32:18 +00:00
func NewChunkHandler ( service interfaces . ChunkService , kgService interfaces . KnowledgeService , kbShareService interfaces . KBShareService , agentShareService interfaces . AgentShareService ) * ChunkHandler {
return & ChunkHandler { service : service , kgService : kgService , kbShareService : kbShareService , agentShareService : agentShareService }
2026-01-30 08:28:01 +00:00
}
// effectiveCtxForKnowledge resolves knowledge by ID, validates KB access (owner or shared with required role), and returns context with effectiveTenantID for downstream service calls.
func ( h * ChunkHandler ) effectiveCtxForKnowledge ( c * gin . Context , knowledgeID string , requiredPermission types . OrgMemberRole ) ( context . Context , error ) {
ctx := c . Request . Context ( )
tenantID := c . GetUint64 ( types . TenantIDContextKey . String ( ) )
if tenantID == 0 {
return nil , errors . NewUnauthorizedError ( "Unauthorized" )
}
userID , userExists := c . Get ( types . UserIDContextKey . String ( ) )
knowledge , err := h . kgService . GetKnowledgeByIDOnly ( ctx , knowledgeID )
if err != nil {
return nil , errors . NewNotFoundError ( "Knowledge not found" )
}
if knowledge . TenantID == tenantID {
return context . WithValue ( ctx , types . TenantIDContextKey , tenantID ) , nil
}
2026-02-08 06:32:18 +00:00
if ! userExists {
2026-01-30 08:28:01 +00:00
return nil , errors . NewForbiddenError ( "Permission denied to access this knowledge" )
}
2026-02-08 06:32:18 +00:00
if h . kbShareService != nil {
permission , isShared , permErr := h . kbShareService . CheckUserKBPermission ( ctx , knowledge . KnowledgeBaseID , userID . ( string ) )
if permErr == nil && isShared {
if ! permission . HasPermission ( requiredPermission ) {
return nil , errors . NewForbiddenError ( "Insufficient permission for this operation" )
}
return context . WithValue ( ctx , types . TenantIDContextKey , knowledge . TenantID ) , nil
}
2026-01-30 08:28:01 +00:00
}
2026-02-08 06:32:18 +00:00
if requiredPermission == types . OrgRoleViewer && h . agentShareService != nil {
kbRef := & types . KnowledgeBase { ID : knowledge . KnowledgeBaseID , TenantID : knowledge . TenantID }
can , err := h . agentShareService . UserCanAccessKBViaSomeSharedAgent ( ctx , userID . ( string ) , tenantID , kbRef )
if err == nil && can {
return context . WithValue ( ctx , types . TenantIDContextKey , knowledge . TenantID ) , nil
}
2026-01-30 08:28:01 +00:00
}
2026-02-08 06:32:18 +00:00
return nil , errors . NewForbiddenError ( "Permission denied to access this knowledge" )
2025-08-05 07:08:07 +00:00
}
2025-12-16 08:40:13 +00:00
// GetChunkByIDOnly godoc
// @Summary 通过ID获取分块
2026-01-30 08:28:01 +00:00
// @Description 仅通过分块ID获取分块详情( 不需要knowledge_id) ; 支持共享知识库下的分块访问
2025-12-16 08:40:13 +00:00
// @Tags 分块管理
// @Accept json
// @Produce json
// @Param id path string true "分块ID"
// @Success 200 {object} map[string]interface{} "分块详情"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Failure 404 {object} errors.AppError "分块不存在"
// @Security Bearer
2025-12-22 06:36:29 +00:00
// @Security ApiKeyAuth
2025-12-16 08:40:13 +00:00
// @Router /chunks/by-id/{id} [get]
2025-11-11 14:26:59 +00:00
func ( h * ChunkHandler ) GetChunkByIDOnly ( c * gin . Context ) {
ctx := c . Request . Context ( )
logger . Info ( ctx , "Start retrieving chunk by ID only" )
2025-11-25 16:19:26 +00:00
chunkID := secutils . SanitizeForLog ( c . Param ( "id" ) )
2025-11-11 14:26:59 +00:00
if chunkID == "" {
logger . Error ( ctx , "Chunk ID is empty" )
c . Error ( errors . NewBadRequestError ( "Chunk ID cannot be empty" ) )
return
}
2026-01-30 08:28:01 +00:00
// Get chunk by ID without tenant filter (chunk may belong to shared KB)
chunk , err := h . service . GetChunkByIDOnly ( ctx , chunkID )
2025-11-11 14:26:59 +00:00
if err != nil {
if err == service . ErrChunkNotFound {
2025-11-25 16:19:26 +00:00
logger . Warnf ( ctx , "Chunk not found, chunk ID: %s" , chunkID )
2025-11-11 14:26:59 +00:00
c . Error ( errors . NewNotFoundError ( "Chunk not found" ) )
return
}
logger . ErrorWithFields ( ctx , err , nil )
c . Error ( errors . NewInternalServerError ( err . Error ( ) ) )
return
}
2026-01-30 08:28:01 +00:00
_ , err = h . effectiveCtxForKnowledge ( c , chunk . KnowledgeID , types . OrgRoleViewer )
if err != nil {
c . Error ( err )
2025-11-11 14:26:59 +00:00
return
}
// 对 chunk 内容进行安全清理
if chunk . Content != "" {
chunk . Content = secutils . SanitizeForDisplay ( chunk . Content )
}
c . JSON ( http . StatusOK , gin . H {
"success" : true ,
"data" : chunk ,
} )
}
2025-12-16 08:40:13 +00:00
// ListKnowledgeChunks godoc
// @Summary 获取知识分块列表
// @Description 获取指定知识下的所有分块列表,支持分页
// @Tags 分块管理
// @Accept json
// @Produce json
// @Param knowledge_id path string true "知识ID"
// @Param page query int false "页码" default(1)
// @Param page_size query int false "每页数量" default(10)
// @Success 200 {object} map[string]interface{} "分块列表"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Security Bearer
2025-12-22 06:36:29 +00:00
// @Security ApiKeyAuth
2025-12-16 08:40:13 +00:00
// @Router /chunks/{knowledge_id} [get]
2025-08-05 07:08:07 +00:00
func ( h * ChunkHandler ) ListKnowledgeChunks ( c * gin . Context ) {
ctx := c . Request . Context ( )
logger . Info ( ctx , "Start retrieving knowledge chunks list" )
2025-11-25 16:19:26 +00:00
knowledgeID := secutils . SanitizeForLog ( c . Param ( "knowledge_id" ) )
2025-08-05 07:08:07 +00:00
if knowledgeID == "" {
logger . Error ( ctx , "Knowledge ID is empty" )
c . Error ( errors . NewBadRequestError ( "Knowledge ID cannot be empty" ) )
return
}
2026-01-30 08:28:01 +00:00
effCtx , err := h . effectiveCtxForKnowledge ( c , knowledgeID , types . OrgRoleViewer )
if err != nil {
c . Error ( err )
return
}
2025-08-05 07:08:07 +00:00
// Parse pagination parameters
var pagination types . Pagination
if err := c . ShouldBindQuery ( & pagination ) ; err != nil {
2025-11-26 05:14:29 +00:00
logger . Errorf ( ctx , "Failed to parse pagination parameters: %s" , secutils . SanitizeForLog ( err . Error ( ) ) )
2025-08-05 07:08:07 +00:00
c . Error ( errors . NewBadRequestError ( err . Error ( ) ) )
return
}
2025-11-26 09:58:31 +00:00
if pagination . Page < 1 {
pagination . Page = 1
}
if pagination . PageSize < 1 {
pagination . PageSize = 10
}
if pagination . PageSize > 100 {
pagination . PageSize = 100
}
2025-08-05 07:08:07 +00:00
2025-11-17 10:15:02 +00:00
chunkType := [ ] types . ChunkType { types . ChunkTypeText }
2026-01-30 08:28:01 +00:00
// Use pagination for query (effCtx has effectiveTenantID for shared KB)
result , err := h . service . ListPagedChunksByKnowledgeID ( effCtx , knowledgeID , & pagination , chunkType )
2025-08-05 07:08:07 +00:00
if err != nil {
logger . ErrorWithFields ( ctx , err , nil )
c . Error ( errors . NewInternalServerError ( err . Error ( ) ) )
return
}
2025-09-15 11:13:59 +00:00
// 对 chunk 内容进行安全清理
for _ , chunk := range result . Data . ( [ ] * types . Chunk ) {
if chunk . Content != "" {
chunk . Content = secutils . SanitizeForDisplay ( chunk . Content )
}
}
2025-08-05 07:08:07 +00:00
c . JSON ( http . StatusOK , gin . H {
"success" : true ,
"data" : result . Data ,
"total" : result . Total ,
"page" : result . Page ,
"page_size" : result . PageSize ,
} )
}
// UpdateChunkRequest defines the request structure for updating a chunk
type UpdateChunkRequest struct {
Content string ` json:"content" `
Embedding [ ] float32 ` json:"embedding" `
ChunkIndex int ` json:"chunk_index" `
IsEnabled bool ` json:"is_enabled" `
StartAt int ` json:"start_at" `
EndAt int ` json:"end_at" `
ImageInfo string ` json:"image_info" `
}
2026-01-30 08:28:01 +00:00
// validateAndGetChunk validates request parameters and retrieves the chunk (supports shared KB via effectiveTenantID).
// Returns chunk, knowledge ID, context with effectiveTenantID for downstream calls, and error.
func ( h * ChunkHandler ) validateAndGetChunk ( c * gin . Context ) ( * types . Chunk , string , context . Context , error ) {
2025-08-05 07:08:07 +00:00
ctx := c . Request . Context ( )
2025-11-25 16:19:26 +00:00
knowledgeID := secutils . SanitizeForLog ( c . Param ( "knowledge_id" ) )
2025-08-05 07:08:07 +00:00
if knowledgeID == "" {
logger . Error ( ctx , "Knowledge ID is empty" )
2026-01-30 08:28:01 +00:00
return nil , "" , nil , errors . NewBadRequestError ( "Knowledge ID cannot be empty" )
2025-08-05 07:08:07 +00:00
}
2025-11-25 16:19:26 +00:00
id := secutils . SanitizeForLog ( c . Param ( "id" ) )
2025-08-05 07:08:07 +00:00
if id == "" {
logger . Error ( ctx , "Chunk ID is empty" )
2026-01-30 08:28:01 +00:00
return nil , knowledgeID , nil , errors . NewBadRequestError ( "Chunk ID cannot be empty" )
2025-08-05 07:08:07 +00:00
}
2026-01-30 08:28:01 +00:00
effCtx , err := h . effectiveCtxForKnowledge ( c , knowledgeID , types . OrgRoleEditor )
if err != nil {
return nil , knowledgeID , nil , err
2025-08-05 07:08:07 +00:00
}
2025-11-25 16:19:26 +00:00
logger . Infof ( ctx , "Retrieving knowledge chunk information, knowledge ID: %s, chunk ID: %s" , knowledgeID , id )
2025-08-05 07:08:07 +00:00
2026-01-30 08:28:01 +00:00
chunk , err := h . service . GetChunkByID ( effCtx , id )
2025-08-05 07:08:07 +00:00
if err != nil {
if err == service . ErrChunkNotFound {
2025-11-25 16:19:26 +00:00
logger . Warnf ( ctx , "Chunk not found, knowledge ID: %s, chunk ID: %s" , knowledgeID , id )
2026-01-30 08:28:01 +00:00
return nil , knowledgeID , nil , errors . NewNotFoundError ( "Chunk not found" )
2025-08-05 07:08:07 +00:00
}
logger . ErrorWithFields ( ctx , err , nil )
2026-01-30 08:28:01 +00:00
return nil , knowledgeID , nil , errors . NewInternalServerError ( err . Error ( ) )
2025-08-05 07:08:07 +00:00
}
2026-01-30 08:28:01 +00:00
if chunk . KnowledgeID != knowledgeID {
logger . Warnf ( ctx , "Chunk does not belong to knowledge, knowledge ID: %s, chunk ID: %s" , knowledgeID , id )
return nil , knowledgeID , nil , errors . NewForbiddenError ( "No permission to access this chunk" )
2025-08-05 07:08:07 +00:00
}
2026-01-30 08:28:01 +00:00
return chunk , knowledgeID , effCtx , nil
2025-08-05 07:08:07 +00:00
}
2025-12-16 08:40:13 +00:00
// UpdateChunk godoc
// @Summary 更新分块
// @Description 更新指定分块的内容和属性
// @Tags 分块管理
// @Accept json
// @Produce json
// @Param knowledge_id path string true "知识ID"
// @Param id path string true "分块ID"
// @Param request body UpdateChunkRequest true "更新请求"
// @Success 200 {object} map[string]interface{} "更新后的分块"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Failure 404 {object} errors.AppError "分块不存在"
// @Security Bearer
2025-12-22 06:36:29 +00:00
// @Security ApiKeyAuth
2025-12-16 08:40:13 +00:00
// @Router /chunks/{knowledge_id}/{id} [put]
2025-08-05 07:08:07 +00:00
func ( h * ChunkHandler ) UpdateChunk ( c * gin . Context ) {
ctx := c . Request . Context ( )
logger . Info ( ctx , "Start updating knowledge chunk" )
2026-01-30 08:28:01 +00:00
chunk , knowledgeID , effCtx , err := h . validateAndGetChunk ( c )
2025-08-05 07:08:07 +00:00
if err != nil {
c . Error ( err )
return
}
var req UpdateChunkRequest
if err := c . ShouldBindJSON ( & req ) ; err != nil {
2025-11-26 05:14:29 +00:00
logger . Errorf ( ctx , "Failed to parse request parameters: %s" , secutils . SanitizeForLog ( err . Error ( ) ) )
2025-08-05 07:08:07 +00:00
c . Error ( errors . NewBadRequestError ( err . Error ( ) ) )
return
}
if req . Content != "" {
chunk . Content = req . Content
}
chunk . IsEnabled = req . IsEnabled
2026-01-30 08:28:01 +00:00
if err := h . service . UpdateChunk ( effCtx , chunk ) ; err != nil {
2025-08-05 07:08:07 +00:00
logger . ErrorWithFields ( ctx , err , nil )
c . Error ( errors . NewInternalServerError ( err . Error ( ) ) )
return
}
2025-11-25 15:33:34 +00:00
logger . Infof ( ctx , "Knowledge chunk updated successfully, knowledge ID: %s, chunk ID: %s" ,
secutils . SanitizeForLog ( knowledgeID ) , secutils . SanitizeForLog ( chunk . ID ) )
2025-08-05 07:08:07 +00:00
c . JSON ( http . StatusOK , gin . H {
"success" : true ,
"data" : chunk ,
} )
}
2025-12-16 08:40:13 +00:00
// DeleteChunk godoc
// @Summary 删除分块
// @Description 删除指定的分块
// @Tags 分块管理
// @Accept json
// @Produce json
// @Param knowledge_id path string true "知识ID"
// @Param id path string true "分块ID"
// @Success 200 {object} map[string]interface{} "删除成功"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Failure 404 {object} errors.AppError "分块不存在"
// @Security Bearer
2025-12-22 06:36:29 +00:00
// @Security ApiKeyAuth
2025-12-16 08:40:13 +00:00
// @Router /chunks/{knowledge_id}/{id} [delete]
2025-08-05 07:08:07 +00:00
func ( h * ChunkHandler ) DeleteChunk ( c * gin . Context ) {
ctx := c . Request . Context ( )
logger . Info ( ctx , "Start deleting knowledge chunk" )
2026-01-30 08:28:01 +00:00
chunk , _ , effCtx , err := h . validateAndGetChunk ( c )
2025-08-05 07:08:07 +00:00
if err != nil {
c . Error ( err )
return
}
2026-01-30 08:28:01 +00:00
if err := h . service . DeleteChunk ( effCtx , chunk . ID ) ; err != nil {
2025-08-05 07:08:07 +00:00
logger . ErrorWithFields ( ctx , err , nil )
c . Error ( errors . NewInternalServerError ( err . Error ( ) ) )
return
}
c . JSON ( http . StatusOK , gin . H {
"success" : true ,
"message" : "Chunk deleted" ,
} )
}
2025-12-16 08:40:13 +00:00
// DeleteChunksByKnowledgeID godoc
// @Summary 删除知识下所有分块
// @Description 删除指定知识下的所有分块
// @Tags 分块管理
// @Accept json
// @Produce json
// @Param knowledge_id path string true "知识ID"
// @Success 200 {object} map[string]interface{} "删除成功"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Security Bearer
2025-12-22 06:36:29 +00:00
// @Security ApiKeyAuth
2025-12-16 08:40:13 +00:00
// @Router /chunks/{knowledge_id} [delete]
2025-08-05 07:08:07 +00:00
func ( h * ChunkHandler ) DeleteChunksByKnowledgeID ( c * gin . Context ) {
ctx := c . Request . Context ( )
logger . Info ( ctx , "Start deleting all chunks under knowledge" )
2025-11-25 16:19:26 +00:00
knowledgeID := secutils . SanitizeForLog ( c . Param ( "knowledge_id" ) )
2025-08-05 07:08:07 +00:00
if knowledgeID == "" {
logger . Error ( ctx , "Knowledge ID is empty" )
c . Error ( errors . NewBadRequestError ( "Knowledge ID cannot be empty" ) )
return
}
2026-01-30 08:28:01 +00:00
effCtx , err := h . effectiveCtxForKnowledge ( c , knowledgeID , types . OrgRoleEditor )
if err != nil {
c . Error ( err )
return
}
err = h . service . DeleteChunksByKnowledgeID ( effCtx , knowledgeID )
2025-08-05 07:08:07 +00:00
if err != nil {
logger . ErrorWithFields ( ctx , err , nil )
c . Error ( errors . NewInternalServerError ( err . Error ( ) ) )
return
}
c . JSON ( http . StatusOK , gin . H {
"success" : true ,
"message" : "All chunks under knowledge deleted" ,
} )
}
2025-12-03 17:26:30 +00:00
2025-12-16 08:40:13 +00:00
// DeleteGeneratedQuestion godoc
// @Summary 删除生成的问题
// @Description 删除分块中生成的问题
// @Tags 分块管理
// @Accept json
// @Produce json
// @Param id path string true "分块ID"
// @Param request body object{question_id=string} true "问题ID"
// @Success 200 {object} map[string]interface{} "删除成功"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Failure 404 {object} errors.AppError "分块不存在"
// @Security Bearer
2025-12-22 06:36:29 +00:00
// @Security ApiKeyAuth
2025-12-16 08:40:13 +00:00
// @Router /chunks/by-id/{id}/questions [delete]
2025-12-03 17:26:30 +00:00
func ( h * ChunkHandler ) DeleteGeneratedQuestion ( c * gin . Context ) {
ctx := c . Request . Context ( )
logger . Info ( ctx , "Start deleting generated question from chunk" )
chunkID := secutils . SanitizeForLog ( c . Param ( "id" ) )
if chunkID == "" {
logger . Error ( ctx , "Chunk ID is empty" )
c . Error ( errors . NewBadRequestError ( "Chunk ID cannot be empty" ) )
return
}
var req struct {
QuestionID string ` json:"question_id" binding:"required" `
}
if err := c . ShouldBindJSON ( & req ) ; err != nil {
logger . Errorf ( ctx , "Failed to parse request parameters: %s" , secutils . SanitizeForLog ( err . Error ( ) ) )
c . Error ( errors . NewBadRequestError ( "Question ID is required" ) )
return
}
2026-01-30 08:28:01 +00:00
chunk , err := h . service . GetChunkByIDOnly ( ctx , chunkID )
2025-12-03 17:26:30 +00:00
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
}
2026-01-30 08:28:01 +00:00
effCtx , err := h . effectiveCtxForKnowledge ( c , chunk . KnowledgeID , types . OrgRoleEditor )
if err != nil {
c . Error ( err )
2025-12-03 17:26:30 +00:00
return
}
2026-01-30 08:28:01 +00:00
if err := h . service . DeleteGeneratedQuestion ( effCtx , chunkID , req . QuestionID ) ; err != nil {
2025-12-03 17:26:30 +00:00
logger . ErrorWithFields ( ctx , err , nil )
c . Error ( errors . NewBadRequestError ( err . Error ( ) ) )
return
}
logger . Infof ( ctx , "Generated question deleted successfully, chunk ID: %s, question ID: %s" ,
secutils . SanitizeForLog ( chunkID ) , secutils . SanitizeForLog ( req . QuestionID ) )
c . JSON ( http . StatusOK , gin . H {
"success" : true ,
"message" : "Generated question deleted" ,
} )
}