feat: 新增FAQ条目获取接口,支持按ID查询单个FAQ条目;增强标签删除功能,支持content_only模式仅删除标签下内容

This commit is contained in:
wizardchen
2025-12-23 11:25:39 +08:00
parent 4159c9d3a5
commit 529b31da23
9 changed files with 126 additions and 11 deletions
+17
View File
@@ -209,6 +209,23 @@ func (c *Client) CreateFAQEntry(ctx context.Context,
return response.Data, nil
}
// GetFAQEntry retrieves a single FAQ entry by ID.
func (c *Client) GetFAQEntry(ctx context.Context,
knowledgeBaseID, entryID string,
) (*FAQEntry, error) {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/faq/entries/%s", knowledgeBaseID, entryID)
resp, err := c.doRequest(ctx, http.MethodGet, path, nil, nil)
if err != nil {
return nil, err
}
var response FAQEntryResponse
if err := parseResponse(resp, &response); err != nil {
return nil, err
}
return response.Data, nil
}
// UpdateFAQEntry updates a single FAQ entry.
func (c *Client) UpdateFAQEntry(ctx context.Context,
knowledgeBaseID, entryID string, payload *FAQEntryPayload,
+7 -2
View File
@@ -137,15 +137,20 @@ func (c *Client) UpdateTag(ctx context.Context,
return response.Data, nil
}
// DeleteTag deletes a tag. Set force to true to delete even if the tag is referenced.
// DeleteTag deletes a tag.
// Set force to true to delete even if the tag is referenced.
// Set contentOnly to true to only delete the content under the tag but keep the tag itself.
func (c *Client) DeleteTag(ctx context.Context,
knowledgeBaseID, tagID string, force bool,
knowledgeBaseID, tagID string, force bool, contentOnly bool,
) error {
path := fmt.Sprintf("/api/v1/knowledge-bases/%s/tags/%s", knowledgeBaseID, tagID)
query := url.Values{}
if force {
query.Add("force", "true")
}
if contentOnly {
query.Add("content_only", "true")
}
resp, err := c.doRequest(ctx, http.MethodDelete, path, nil, query)
if err != nil {
+41
View File
@@ -3318,6 +3318,47 @@ func (s *knowledgeService) CreateFAQEntry(ctx context.Context,
return entry, nil
}
// GetFAQEntry retrieves a single FAQ entry by ID.
func (s *knowledgeService) GetFAQEntry(ctx context.Context,
kbID string, entryID string,
) (*types.FAQEntry, error) {
if entryID == "" {
return nil, werrors.NewBadRequestError("条目ID不能为空")
}
kb, err := s.validateFAQKnowledgeBase(ctx, kbID)
if err != nil {
return nil, err
}
kb.EnsureDefaults()
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
// 获取chunk
chunk, err := s.chunkService.GetChunkByID(ctx, entryID)
if err != nil {
return nil, err
}
// 验证chunk属于当前知识库
if chunk.KnowledgeBaseID != kb.ID || chunk.TenantID != tenantID {
return nil, werrors.NewNotFoundError("FAQ条目不存在")
}
// 验证是FAQ类型
if chunk.ChunkType != types.ChunkTypeFAQ {
return nil, werrors.NewNotFoundError("FAQ条目不存在")
}
// 转换为FAQEntry返回
entry, err := s.chunkToFAQEntry(chunk, kb)
if err != nil {
return nil, err
}
return entry, nil
}
// UpdateFAQEntry updates a single FAQ entry.
func (s *knowledgeService) UpdateFAQEntry(ctx context.Context,
kbID string, entryID string, payload *types.FAQEntryPayload,
+15 -1
View File
@@ -153,7 +153,8 @@ func (s *knowledgeTagService) UpdateTag(
}
// DeleteTag deletes a tag. When force=true, also deletes all chunks under this tag.
func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bool) error {
// When contentOnly=true, only deletes the content under the tag but keeps the tag itself.
func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bool, contentOnly bool) error {
if id == "" {
return werrors.NewBadRequestError("标签ID不能为空")
}
@@ -166,6 +167,19 @@ func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bo
if err != nil {
return err
}
// contentOnly mode: only delete content, keep the tag
if contentOnly {
if cCount > 0 {
if err := s.chunkRepo.DeleteChunksByTagID(ctx, tenantID, tag.KnowledgeBaseID, tag.ID); err != nil {
logger.Errorf(ctx, "Failed to delete chunks by tag ID %s: %v", tag.ID, err)
return werrors.NewInternalServerError("删除标签下的数据失败")
}
logger.Infof(ctx, "Deleted %d chunks under tag %s (content_only mode)", cCount, tag.ID)
}
return nil
}
if !force && (kCount > 0 || cCount > 0) {
return werrors.NewBadRequestError("标签仍有知识或FAQ条目引用,无法删除")
}
+32
View File
@@ -351,6 +351,38 @@ func (h *FAQHandler) ExportEntries(c *gin.Context) {
c.Data(http.StatusOK, "text/csv; charset=utf-8", append(bom, csvData...))
}
// GetEntry godoc
// @Summary 获取FAQ条目详情
// @Description 根据ID获取单个FAQ条目的详情
// @Tags FAQ管理
// @Accept json
// @Produce json
// @Param id path string true "知识库ID"
// @Param entry_id path string true "FAQ条目ID"
// @Success 200 {object} map[string]interface{} "FAQ条目详情"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Failure 404 {object} errors.AppError "条目不存在"
// @Security Bearer
// @Security ApiKeyAuth
// @Router /knowledge-bases/{id}/faq/entries/{entry_id} [get]
func (h *FAQHandler) GetEntry(c *gin.Context) {
ctx := c.Request.Context()
kbID := secutils.SanitizeForLog(c.Param("id"))
entryID := secutils.SanitizeForLog(c.Param("entry_id"))
entry, err := h.knowledgeService.GetFAQEntry(ctx, kbID, entryID)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": entry,
})
}
// GetImportProgress godoc
// @Summary 获取FAQ导入进度
// @Description 获取FAQ导入任务的进度
+9 -7
View File
@@ -157,15 +157,16 @@ func (h *TagHandler) UpdateTag(c *gin.Context) {
// DeleteTag godoc
// @Summary 删除标签
// @Description 删除标签,可使用force=true强制删除被引用的标签
// @Description 删除标签,可使用force=true强制删除被引用的标签,content_only=true仅删除标签下的内容而保留标签本身
// @Tags 标签管理
// @Accept json
// @Produce json
// @Param id path string true "知识库ID"
// @Param tag_id path string true "标签ID"
// @Param force query bool false "强制删除"
// @Success 200 {object} map[string]interface{} "删除成功"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Param id path string true "知识库ID"
// @Param tag_id path string true "标签ID"
// @Param force query bool false "强制删除"
// @Param content_only query bool false "仅删除内容,保留标签"
// @Success 200 {object} map[string]interface{} "删除成功"
// @Failure 400 {object} errors.AppError "请求参数错误"
// @Security Bearer
// @Security ApiKeyAuth
// @Router /knowledge-bases/{id}/tags/{tag_id} [delete]
@@ -174,8 +175,9 @@ func (h *TagHandler) DeleteTag(c *gin.Context) {
tagID := secutils.SanitizeForLog(c.Param("tag_id"))
force := c.Query("force") == "true"
contentOnly := c.Query("content_only") == "true"
if err := h.tagService.DeleteTag(ctx, tagID, force); err != nil {
if err := h.tagService.DeleteTag(ctx, tagID, force, contentOnly); err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
"tag_id": tagID,
})
+1
View File
@@ -183,6 +183,7 @@ func RegisterFAQRoutes(r *gin.RouterGroup, handler *handler.FAQHandler) {
{
faq.GET("/entries", handler.ListEntries)
faq.GET("/entries/export", handler.ExportEntries)
faq.GET("/entries/:entry_id", handler.GetEntry)
faq.POST("/entries", handler.UpsertEntries)
faq.POST("/entry", handler.CreateEntry)
faq.PUT("/entries/:entry_id", handler.UpdateEntry)
+2
View File
@@ -86,6 +86,8 @@ type KnowledgeService interface {
UpsertFAQEntries(ctx context.Context, kbID string, payload *types.FAQBatchUpsertPayload) (string, error)
// CreateFAQEntry creates a single FAQ entry synchronously.
CreateFAQEntry(ctx context.Context, kbID string, payload *types.FAQEntryPayload) (*types.FAQEntry, error)
// GetFAQEntry retrieves a single FAQ entry by ID.
GetFAQEntry(ctx context.Context, kbID string, entryID string) (*types.FAQEntry, error)
// UpdateFAQEntry updates a single FAQ entry.
UpdateFAQEntry(ctx context.Context, kbID string, entryID string, payload *types.FAQEntryPayload) error
// UpdateFAQEntryFieldsBatch updates multiple fields for FAQ entries in batch.
+2 -1
View File
@@ -15,7 +15,8 @@ type KnowledgeTagService interface {
// UpdateTag updates tag basic information.
UpdateTag(ctx context.Context, id string, name *string, color *string, sortOrder *int) (*types.KnowledgeTag, error)
// DeleteTag deletes a tag.
DeleteTag(ctx context.Context, id string, force bool) error
// When contentOnly=true, only deletes the content under the tag but keeps the tag itself.
DeleteTag(ctx context.Context, id string, force bool, contentOnly bool) error
// FindOrCreateTagByName finds a tag by name or creates it if not exists.
FindOrCreateTagByName(ctx context.Context, kbID string, name string) (*types.KnowledgeTag, error)
}