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.
This commit is contained in:
Max 2025-08-22 11:25:46 +08:00
parent 82b11caa42
commit 9587cbf981
2 changed files with 62 additions and 10 deletions

View file

@ -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,

View file

@ -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,