From 9587cbf981d7c815737676ca8cebb90c6a3312e9 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 22 Aug 2025 11:25:46 +0800 Subject: [PATCH] Enhance hit and vote retrieval logic in API - Implemented error handling for uninitialized knowledge base in GetHit and GetVote functions. - Added detailed error responses for cases where hits or votes are not found. - Updated response structures to return the retrieved hit and vote data along with associated identifiers. --- openapi/kb/hit.go | 36 +++++++++++++++++++++++++++++++----- openapi/kb/vote.go | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/openapi/kb/hit.go b/openapi/kb/hit.go index 71ab7139..642ba78a 100644 --- a/openapi/kb/hit.go +++ b/openapi/kb/hit.go @@ -1,7 +1,6 @@ package kb import ( - "net/http" "strconv" "strings" @@ -206,10 +205,37 @@ func GetHit(c *gin.Context) { return } - // TODO: Implement document permission validation for docID - // TODO: Implement get hit detail logic - c.JSON(http.StatusOK, gin.H{ - "hit": nil, + // Check if kb.Instance is available + if kb.Instance == nil { + errorResp := &response.ErrorResponse{ + Code: response.ErrServerError.Code, + ErrorDescription: "Knowledge base not initialized", + } + response.RespondWithError(c, response.StatusInternalServerError, errorResp) + return + } + + // Call GraphRag GetHit method + hit, err := kb.Instance.GetHit(c.Request.Context(), docID, segmentID, hitID) + if err != nil { + if err.Error() == "hit not found" { + errorResp := &response.ErrorResponse{ + Code: response.ErrInvalidRequest.Code, + ErrorDescription: "Hit not found", + } + response.RespondWithError(c, response.StatusNotFound, errorResp) + } else { + errorResp := &response.ErrorResponse{ + Code: response.ErrServerError.Code, + ErrorDescription: "Failed to get hit: " + err.Error(), + } + response.RespondWithError(c, response.StatusInternalServerError, errorResp) + } + return + } + + response.RespondWithSuccess(c, response.StatusOK, gin.H{ + "hit": hit, "doc_id": docID, "segment_id": segmentID, "hit_id": hitID, diff --git a/openapi/kb/vote.go b/openapi/kb/vote.go index 2859e421..e8afc458 100644 --- a/openapi/kb/vote.go +++ b/openapi/kb/vote.go @@ -1,7 +1,6 @@ package kb import ( - "net/http" "strconv" "strings" @@ -203,10 +202,37 @@ func GetVote(c *gin.Context) { return } - // TODO: Implement document permission validation for docID - // TODO: Implement get vote detail logic - c.JSON(http.StatusOK, gin.H{ - "vote": nil, + // Check if kb.Instance is available + if kb.Instance == nil { + errorResp := &response.ErrorResponse{ + Code: response.ErrServerError.Code, + ErrorDescription: "Knowledge base not initialized", + } + response.RespondWithError(c, response.StatusInternalServerError, errorResp) + return + } + + // Call GraphRag GetVote method + vote, err := kb.Instance.GetVote(c.Request.Context(), docID, segmentID, voteID) + if err != nil { + if err.Error() == "vote not found" { + errorResp := &response.ErrorResponse{ + Code: response.ErrInvalidRequest.Code, + ErrorDescription: "Vote not found", + } + response.RespondWithError(c, response.StatusNotFound, errorResp) + } else { + errorResp := &response.ErrorResponse{ + Code: response.ErrServerError.Code, + ErrorDescription: "Failed to get vote: " + err.Error(), + } + response.RespondWithError(c, response.StatusInternalServerError, errorResp) + } + return + } + + response.RespondWithSuccess(c, response.StatusOK, gin.H{ + "vote": vote, "doc_id": docID, "segment_id": segmentID, "vote_id": voteID,